-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
450 lines (360 loc) · 15.5 KB
/
app.py
File metadata and controls
450 lines (360 loc) · 15.5 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import base64
import json
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
import requests as http_requests
from dotenv import load_dotenv
from flask import Flask, Response, jsonify, render_template, request
from proxy import (
build_auth_headers,
build_curl_string,
build_request,
build_result_url,
build_status_url,
check_done,
extract_error,
extract_outputs,
extract_value,
)
load_dotenv()
app = Flask(__name__)
# ---------------------------------------------------------------------------
# API key config from .env
# ---------------------------------------------------------------------------
API_KEYS = {}
# ---------------------------------------------------------------------------
# Definition loading
# ---------------------------------------------------------------------------
DEFINITIONS = {}
PROVIDER_DISPLAY_NAMES = {}
def load_definitions():
"""Walk the definitions/ folder, load all JSON files, and collect API keys."""
defs_dir = os.path.join(os.path.dirname(__file__), "definitions")
for root, _dirs, files in os.walk(defs_dir):
for fname in files:
if not fname.endswith(".json"):
continue
path = os.path.join(root, fname)
try:
with open(path) as f:
defn = json.load(f)
DEFINITIONS[defn["id"]] = defn
except (json.JSONDecodeError, KeyError, OSError) as e:
print(f"WARNING: skipping {path}: {e}")
continue
# Collect provider display name from definition
provider = defn.get("provider", "")
if provider and provider not in PROVIDER_DISPLAY_NAMES:
PROVIDER_DISPLAY_NAMES[provider] = defn.get(
"provider_display_name", provider.title()
)
# Load API key from auth.env_key (if not already loaded for this provider)
env_key = defn.get("auth", {}).get("env_key", "")
if provider and env_key and provider not in API_KEYS:
val = os.getenv(env_key, "")
if val:
API_KEYS[provider] = val
load_definitions()
# ---------------------------------------------------------------------------
# Routes — Pages
# ---------------------------------------------------------------------------
def provider_display_name(slug):
"""Return display name for a provider slug, falling back to title case."""
return PROVIDER_DISPLAY_NAMES.get(slug, slug.title())
@app.route("/")
def index():
"""Render the main playground page."""
definitions_list = []
for d in DEFINITIONS.values():
model_param = next(
(p for p in d.get("request", {}).get("params", []) if p.get("name") == "model"),
None,
)
model_count = len(model_param.get("options", [])) if model_param else 0
definitions_list.append({
"id": d["id"],
"name": d["name"],
"provider": d["provider"],
"provider_display_name": provider_display_name(d["provider"]),
"provider_url": d.get("provider_url", ""),
"output_type": d.get("response", {}).get("outputs", [{}])[0].get("type", "text"),
"model_count": model_count,
})
definitions_list.sort(key=lambda d: d["name"])
return render_template(
"index.html",
definitions=definitions_list,
api_keys=list(API_KEYS.keys()),
)
# ---------------------------------------------------------------------------
# Routes — API
# ---------------------------------------------------------------------------
def get_api_key(definition_id):
"""Look up the API key for a definition's provider, server-side."""
defn = DEFINITIONS.get(definition_id)
if not defn:
return None, None
return defn, API_KEYS.get(defn["provider"], "")
BOOKMARKS_FILE = os.path.join(os.path.dirname(__file__), "bookmarks.json")
@app.route("/api/bookmarks")
def get_bookmarks():
"""Return saved bookmarks."""
if not os.path.exists(BOOKMARKS_FILE):
return jsonify([])
with open(BOOKMARKS_FILE) as f:
return jsonify(json.load(f))
@app.route("/api/bookmarks", methods=["POST"])
def save_bookmarks():
"""Overwrite the full bookmarks array."""
data = request.get_json()
with open(BOOKMARKS_FILE, "w") as f:
json.dump(data, f, indent=2)
return jsonify({"ok": True})
@app.route("/api/preview", methods=["POST"])
def preview():
"""Return a curl command string for the given definition and params."""
data = request.get_json()
definition_id = data.get("definition_id")
params = data.get("params", {})
include_key = data.get("include_key", False)
defn = DEFINITIONS.get(definition_id)
if not defn:
return jsonify({"error": f"Definition '{definition_id}' not found"}), 404
api_key = None
if include_key:
_, api_key = get_api_key(definition_id)
try:
curl = build_curl_string(defn, params, api_key=api_key or None)
except ValueError as e:
return jsonify({"error": str(e)}), 400
return jsonify({"curl": curl})
@app.route("/api/definitions/<definition_id>")
def get_definition(definition_id):
"""Return the full definition JSON for client-side use."""
defn = DEFINITIONS.get(definition_id)
if not defn:
return jsonify({"error": f"Definition '{definition_id}' not found"}), 404
return jsonify(defn)
@app.route("/api/generate", methods=["POST"])
def generate():
"""Submit a generation request to the provider API."""
data = request.get_json()
definition_id = data.get("definition_id")
params = data.get("params", {})
defn, api_key = get_api_key(definition_id)
if not defn:
return jsonify({"error": f"Definition '{definition_id}' not found"}), 404
if not api_key:
return jsonify({"error": f"No API key configured for provider '{defn['provider']}'"}), 400
try:
url, headers, body = build_request(defn, params, api_key)
except ValueError as e:
return jsonify({"error": str(e)}), 400
# When a streaming definition is called via /api/generate (sync mode),
# override stream to false so the provider returns a complete response.
if body and body.get("stream") is True:
body["stream"] = False
try:
resp = http_requests.request(
method=defn["request"]["method"],
url=url,
headers=headers,
json=body,
timeout=60,
)
# Handle binary audio responses (TTS endpoints return raw audio)
content_type = resp.headers.get("Content-Type", "")
if resp.ok and ("audio" in content_type or "octet-stream" in content_type):
audio_b64 = base64.b64encode(resp.content).decode("utf-8")
mime = content_type.split(";")[0].strip()
data_url = f"data:{mime};base64,{audio_b64}"
resp_data = {"audio_url": data_url}
else:
resp_data = resp.json()
except http_requests.RequestException as e:
app.logger.error("Generate request failed: %s", e)
return jsonify({"error": "Upstream request failed"}), 502
except ValueError:
return jsonify({"error": "Non-JSON response from provider"}), 502
# For polling patterns, extract the request_id
interaction = defn.get("interaction", {})
result = {"response": resp_data, "status_code": resp.status_code}
if interaction.get("pattern") == "polling" and resp.ok:
rid_path = interaction.get("request_id_path", "$.request_id")
request_id = extract_value(resp_data, rid_path)
result["request_id"] = request_id
# For sync responses (including streaming defs called via /api/generate),
# extract typed outputs (images, audio, etc.)
if resp.ok and "request_id" not in result:
outputs = extract_outputs(defn, resp_data)
if outputs:
# Convert base64 image/audio values to data URLs for client rendering
for output_def, output in zip(defn.get("response", {}).get("outputs", []), outputs):
if output_def.get("source") == "base64" and output["type"] in ("image", "audio"):
mime = output_def.get("mime_type", "image/png" if output["type"] == "image" else "audio/wav")
output["value"] = [
f"data:{mime};base64,{v}" if v and not v.startswith("data:") else v
for v in output["value"]
]
result["outputs"] = outputs
# Check for provider errors
if not resp.ok:
error_msg = extract_error(defn, resp_data) or resp_data
result["error"] = error_msg
return jsonify(result), resp.status_code if resp.ok else 502
@app.route("/api/stream", methods=["POST"])
def stream():
"""Proxy a streaming SSE request to the provider and forward chunks."""
data = request.get_json()
definition_id = data.get("definition_id")
params = data.get("params", {})
defn, api_key = get_api_key(definition_id)
if not defn:
return jsonify({"error": f"Definition '{definition_id}' not found"}), 404
if not api_key:
return jsonify({"error": f"No API key configured for provider '{defn['provider']}'"}), 400
try:
url, headers, body = build_request(defn, params, api_key)
except ValueError as e:
return jsonify({"error": str(e)}), 400
stream_path = defn.get("interaction", {}).get("stream_path", "")
def generate():
try:
resp = http_requests.request(
method=defn["request"]["method"],
url=url,
headers=headers,
json=body,
stream=True,
timeout=60,
)
if not resp.ok:
error_data = resp.text
try:
error_json = resp.json()
error_msg = extract_error(defn, error_json) or error_data
except ValueError:
error_msg = error_data
yield f"event: error\ndata: {json.dumps({'error': str(error_msg)})}\n\n"
return
for line in resp.iter_lines(decode_unicode=True):
if not line:
continue
if line.startswith("data: "):
chunk_str = line[6:]
if chunk_str.strip() == "[DONE]":
yield f"event: done\ndata: {{}}\n\n"
return
try:
chunk = json.loads(chunk_str)
# Extract the token using stream_path
token = extract_value(chunk, stream_path) if stream_path else ""
if token:
yield f"data: {json.dumps({'token': token})}\n\n"
except (json.JSONDecodeError, KeyError, TypeError, IndexError):
pass
yield f"event: done\ndata: {{}}\n\n"
except http_requests.RequestException as e:
app.logger.error("Stream request failed: %s", e)
yield f"event: error\ndata: {json.dumps({'error': 'Upstream request failed'})}\n\n"
return Response(generate(), mimetype="text/event-stream")
@app.route("/api/status")
def check_status():
"""Check the status of an async job."""
definition_id = request.args.get("definition_id")
request_id = request.args.get("request_id", "")
defn, api_key = get_api_key(definition_id)
if not defn:
return jsonify({"error": f"Definition '{definition_id}' not found"}), 404
try:
url = build_status_url(defn, request_id)
except ValueError:
return jsonify({"error": "Invalid request_id", "poll_status": "error"}), 400
headers = build_auth_headers(defn, api_key)
try:
resp = http_requests.get(url, headers=headers, timeout=15)
resp_data = resp.json()
except (http_requests.RequestException, ValueError) as e:
app.logger.error("Status check failed: %s", e)
return jsonify({"error": "Upstream request failed", "poll_status": "error"}), 502
poll_status = check_done(defn, resp_data)
return jsonify({"poll_status": poll_status, "response": resp_data})
@app.route("/api/result")
def get_result():
"""Fetch the final result of a completed async job."""
definition_id = request.args.get("definition_id")
request_id = request.args.get("request_id", "")
defn, api_key = get_api_key(definition_id)
if not defn:
return jsonify({"error": f"Definition '{definition_id}' not found"}), 404
try:
url = build_result_url(defn, request_id)
except ValueError:
return jsonify({"error": "Invalid request_id"}), 400
headers = build_auth_headers(defn, api_key)
try:
resp = http_requests.get(url, headers=headers, timeout=30)
resp_data = resp.json()
except (http_requests.RequestException, ValueError) as e:
app.logger.error("Result fetch failed: %s", e)
return jsonify({"error": "Upstream request failed"}), 502
outputs = extract_outputs(defn, resp_data)
return jsonify({"response": resp_data, "outputs": outputs})
# ---------------------------------------------------------------------------
# Routes — Key validation
# ---------------------------------------------------------------------------
def _validate_provider(provider, api_key, auth_info):
"""Validate a single provider's API key. Returns (provider, status)."""
validation_url = auth_info.get("validation_url")
if not validation_url:
return provider, "unknown"
headers = {
auth_info.get("header", "Authorization"): auth_info.get("prefix", "Bearer ") + api_key
}
try:
resp = http_requests.get(validation_url, headers=headers, timeout=5)
if resp.status_code == 200:
return provider, "valid"
elif resp.status_code in (401, 403):
return provider, "invalid"
else:
return provider, "unknown"
except http_requests.RequestException:
return provider, "unknown"
@app.route("/api/validate-keys")
def validate_keys():
"""Validate all configured API keys by hitting each provider's validation_url."""
# Collect unique providers that have keys and validation URLs
to_validate = {}
for defn in DEFINITIONS.values():
provider = defn.get("provider", "")
if provider in to_validate or provider not in API_KEYS:
continue
auth = defn.get("auth", {})
if auth.get("validation_url"):
to_validate[provider] = auth
results = {}
# Mark providers with no key
all_providers = {d["provider"] for d in DEFINITIONS.values()}
for p in all_providers:
if p not in API_KEYS:
results[p] = "no_key"
# Validate in parallel
with ThreadPoolExecutor(max_workers=10) as pool:
futures = {
pool.submit(_validate_provider, provider, API_KEYS[provider], auth): provider
for provider, auth in to_validate.items()
}
for future in as_completed(futures):
provider, status = future.result()
results[provider] = status
# Providers with keys but no validation_url
for p in API_KEYS:
if p not in results:
results[p] = "unknown"
return jsonify(results)
# ---------------------------------------------------------------------------
# Run
# ---------------------------------------------------------------------------
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=os.getenv("FLASK_DEBUG", "false").lower() == "true")