-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
214 lines (178 loc) · 8.48 KB
/
Copy pathapp.py
File metadata and controls
214 lines (178 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
ProductOS — 4-Role Agentic Product Builder
Flask server with SSE streaming
"""
import os, json, sqlite3, uuid
from datetime import datetime
from pathlib import Path
from flask import Flask, Response, request, send_from_directory, stream_with_context
from flask_cors import CORS
from dotenv import load_dotenv
load_dotenv()
BASE = Path(__file__).parent
# /tmp is the only writable directory on serverless platforms (Vercel, etc.)
DB_PATH = Path('/tmp/sessions.db') if os.getenv('VERCEL') else BASE / 'sessions.db'
app = Flask(__name__, static_folder=str(BASE))
CORS(app)
# In-memory config per session (cleared on restart — fine for a live tool)
_session_configs = {}
# ── Database ───────────────────────────────────────────────────────────────────
def get_db():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def init_db():
with get_db() as db:
db.executescript('''
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
idea TEXT NOT NULL,
status TEXT DEFAULT 'pending',
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS artifacts (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT,
created_at TEXT NOT NULL,
FOREIGN KEY (session_id) REFERENCES sessions(id)
);
''')
init_db()
# ── Key resolution ─────────────────────────────────────────────────────────────
def resolve_key(provider: str, user_key: str = '') -> str:
"""User key > env var. Ollama needs no key."""
if provider == 'ollama':
return 'ollama'
if user_key:
return user_key
env_var = 'ANTHROPIC_API_KEY' if provider == 'anthropic' else 'OPENAI_API_KEY'
return os.getenv(env_var, '')
# ── SSE helpers ────────────────────────────────────────────────────────────────
def sse(data: dict) -> str:
return f'data: {json.dumps(data)}\n\n'
# ── Pipeline ───────────────────────────────────────────────────────────────────
def stream_pipeline(idea: str, session_id: str):
from agents import Orchestrator
config = _session_configs.pop(session_id, {})
provider = config.get('provider', 'anthropic')
model = config.get('model', 'claude-sonnet-4-6')
api_key = config.get('api_key', '')
if not api_key:
yield sse({'type': 'error', 'message': f'No API key for {provider}. Add one in ⚙ settings.'})
return
try:
if provider == 'ollama':
from openai import OpenAI
client = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')
elif provider == 'openai':
from openai import OpenAI
client = OpenAI(api_key=api_key)
else:
import anthropic
client = anthropic.Anthropic(api_key=api_key)
except Exception as e:
yield sse({'type': 'error', 'message': f'Failed to init client: {e}'})
return
orch = Orchestrator(client, provider=provider, model=model)
with get_db() as db:
db.execute('UPDATE sessions SET status=? WHERE id=?', ('running', session_id))
yield sse({'type': 'session_start', 'session_id': session_id, 'idea': idea})
try:
for event in orch.run(idea):
if event['type'] == 'agent_done':
with get_db() as db:
db.execute(
'INSERT OR REPLACE INTO artifacts (id,session_id,role,content,created_at) VALUES (?,?,?,?,?)',
(str(uuid.uuid4()), session_id, event['role'], event['content'], datetime.now().isoformat())
)
elif event['type'] == 'keepalive':
# SSE comment — keeps the browser connection alive during inter-agent pauses
yield ': keepalive\n\n'
continue
yield sse(event)
except Exception as e:
import traceback
err = traceback.format_exc()
print(f'\n[ProductOS ERROR] Session {session_id}:\n{err}')
with get_db() as db:
db.execute('UPDATE sessions SET status=? WHERE id=?', ('error', session_id))
yield sse({'type': 'error', 'message': str(e)})
return
with get_db() as db:
db.execute('UPDATE sessions SET status=? WHERE id=?', ('complete', session_id))
yield sse({'type': 'complete', 'session_id': session_id})
# ── Routes ─────────────────────────────────────────────────────────────────────
@app.route('/')
def index():
return send_from_directory(str(BASE), 'index.html')
@app.route('/favicon.ico')
def favicon():
# Return a minimal SVG favicon — stops the 404 noise in the console
svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><rect x="0" y="0" width="7" height="7" fill="#FB923C"/><rect x="9" y="0" width="7" height="7" fill="#2DD4BF"/><rect x="0" y="9" width="7" height="7" fill="#A78BFA"/><rect x="9" y="9" width="7" height="7" fill="#4ADE80"/></svg>'
from flask import Response
return Response(svg, mimetype='image/svg+xml')
@app.route('/api/build', methods=['POST'])
def build():
"""Create a session and store config. Returns session_id for /api/stream."""
data = request.json or {}
idea = data.get('idea', '').strip()
provider = data.get('provider', 'anthropic')
model = data.get('model', 'claude-sonnet-4-6')
user_key = data.get('api_key', '').strip()
if not idea:
return {'error': 'idea is required'}, 400
api_key = resolve_key(provider, user_key)
if not api_key and provider != 'ollama':
return {'error': f'No API key found for {provider}. Add one in ⚙ settings.'}, 400
session_id = str(uuid.uuid4())[:8]
with get_db() as db:
db.execute(
'INSERT INTO sessions (id,idea,status,created_at) VALUES (?,?,?,?)',
(session_id, idea, 'pending', datetime.now().isoformat())
)
_session_configs[session_id] = {
'api_key': api_key,
'model': model,
'provider': provider,
}
return {'session_id': session_id}
@app.route('/api/stream')
def stream():
session_id = request.args.get('session', '').strip()
if not session_id:
return {'error': 'session param required'}, 400
with get_db() as db:
row = db.execute('SELECT idea FROM sessions WHERE id=?', (session_id,)).fetchone()
if not row:
return {'error': 'session not found'}, 404
idea = row['idea']
def generate():
yield from stream_pipeline(idea, session_id)
return Response(
stream_with_context(generate()),
mimetype='text/event-stream',
headers={'Cache-Control':'no-cache','X-Accel-Buffering':'no','Connection':'keep-alive'}
)
@app.route('/api/sessions')
def list_sessions():
with get_db() as db:
rows = db.execute(
'SELECT id,idea,status,created_at FROM sessions ORDER BY created_at DESC LIMIT 30'
).fetchall()
return [dict(r) for r in rows]
@app.route('/api/session/<sid>')
def get_session(sid):
with get_db() as db:
s = db.execute('SELECT * FROM sessions WHERE id=?', (sid,)).fetchone()
a = db.execute('SELECT * FROM artifacts WHERE session_id=? ORDER BY created_at', (sid,)).fetchall()
if not s:
return {'error': 'not found'}, 404
return {'session': dict(s), 'artifacts': [dict(x) for x in a]}
if __name__ == '__main__':
print('\n╔════════════════════════════════════════╗')
print('║ ProductOS · 4-Role Agentic Builder ║')
print('║ http://localhost:5002 ║')
print('╚════════════════════════════════════════╝\n')
app.run(port=5002, debug=False, threaded=True)