AI Engineering Workspace
PartialChat, files, terminal and preview in one place. Chat sessions run on a durable Postgres-backed queue (chat only: no tools or shell). Live models need an API key; the terminal is unavailable.
Project files
read-onlyapp/scripts/ai-adapters.mjs
1// Provider adapters (streaming). Each async generator yields {text} chunks, then {usage:{in,out}}. Shared by the worker and the adapter tests.2export const MAX_OUT = 2048;3export class ProviderError extends Error { constructor(msg, { status, retryAfterMs, retryable } = {}) { super(msg); this.status = status; this.retryAfterMs = retryAfterMs; this.retryable = retryable ?? (status === undefined || status === 429 || status >= 500); } }4async function* sse(res) {5 const dec = new TextDecoder(); let buf = "";6 for await (const chunk of res.body) { buf += dec.decode(chunk, { stream: true }); let i; while ((i = buf.indexOf("\n\n")) >= 0) { const ev = buf.slice(0, i); buf = buf.slice(i + 2); const d = ev.split("\n").filter(l => l.startsWith("data:")).map(l => l.slice(5).trim()).join("\n"); if (d) yield d; } }7}8async function httpCheck(res, name) {9 if (res.ok) return; const ra = Number(res.headers.get("retry-after")); let body = ""; try { body = (await res.text()).slice(0, 300); } catch {}10 throw new ProviderError(`${name} HTTP ${res.status}: ${body.replace(/\s+/g, " ")}`.slice(0, 400), { status: res.status, retryAfterMs: ra > 0 ? ra * 1000 : undefined });11}12export const ADAPTERS = {13 async *anthropic({ key, model, messages, signal, baseUrl }) {14 const res = await fetch(`${baseUrl || "https://api.anthropic.com"}/v1/messages`, { method: "POST", signal, headers: { "x-api-key": key, "anthropic-version": "2023-06-01", "content-type": "application/json" }, body: JSON.stringify({ model, max_tokens: MAX_OUT, stream: true, messages }) });15 await httpCheck(res, "anthropic"); let tin = 0, tout = 0;16 for await (const d of sse(res)) { let j; try { j = JSON.parse(d); } catch { continue; }17 if (j.type === "message_start") tin = j.message?.usage?.input_tokens ?? 0;18 else if (j.type === "content_block_delta" && j.delta?.type === "text_delta") yield { text: j.delta.text };19 else if (j.type === "message_delta") tout = j.usage?.output_tokens ?? tout;20 else if (j.type === "error") throw new ProviderError(`anthropic stream error: ${j.error?.type}`, { retryable: j.error?.type === "overloaded_error" }); }21 yield { usage: { in: tin, out: tout } };22 },23 async *openai({ key, model, messages, signal, baseUrl }) {24 const res = await fetch(`${baseUrl || "https://api.openai.com"}/v1/chat/completions`, { method: "POST", signal, headers: { authorization: `Bearer ${key}`, "content-type": "application/json" }, body: JSON.stringify({ model, max_tokens: MAX_OUT, stream: true, stream_options: { include_usage: true }, messages }) });25 await httpCheck(res, "openai"); let usage = null;26 for await (const d of sse(res)) { if (d === "[DONE]") break; let j; try { j = JSON.parse(d); } catch { continue; }27 const t = j.choices?.[0]?.delta?.content; if (t) yield { text: t }; if (j.usage) usage = { in: j.usage.prompt_tokens, out: j.usage.completion_tokens }; }28 if (usage) yield { usage };29 },30 // Test double. Never a real provider; every token is prefixed so it can't be mistaken for model output. Behaviour driven by the prompt for failure-injection tests.31 async *mock({ messages, signal, attempt }) {32 const last = messages[messages.length - 1].content; const sleep = ms => new Promise((r, j) => { const t = setTimeout(r, ms); signal?.addEventListener("abort", () => { clearTimeout(t); j(new Error("aborted")); }); });33 if (/\[fail429\]/.test(last) && attempt === 1) throw new ProviderError("mock rate limited", { status: 429, retryAfterMs: 1000 });34 if (/\[fail400\]/.test(last)) throw new ProviderError("mock bad request", { status: 400 });35 const words = `[mock provider] Echo of your request (${last.length} chars): ${last.replace(/\[[a-z0-9]+\]/g, "").trim().slice(0, 200)}. This is a controlled test double, not a real model.`.split(" ");36 const slow = /\[slow\]/.test(last); const n = slow ? 80 : words.length; for (let i = 0; i < n; i++) { yield { text: (words[i % words.length]) + " " }; await sleep(slow ? 150 : 40); }37 yield { usage: { in: Math.ceil(JSON.stringify(messages).length / 4), out: n } };38 },39};4041
Conversation
PartialSign in to start AI sessions.
Task checklist
- Application shell deployed
- Persistent projects
- Agent runtime
- Sandboxed terminal
Execution activity
No agent sessions. Git HEAD 1c27633 on master.
Approval requests
None pending.