variant round: roll retired, chance is variant literature (strict parse, crypto pick on reception, picked-only rendering, /story variance metric); entity round: /story the whole deliverable, /table the play projection, the slidwin carry (smart contwin cutoff, whole exchanges, housekeeper reads session-minus-tail); dormant-session rework: universal session lifecycle (dormant->live->finished, sessions/<n> storage), move-at-stop, start lint + auto-send rule, rebaseable housekeeping, turn-0 -> carryover.jsonl, seed -> memory.json, draft-only save-package; format house rules enforced by the nine-rule check-repo (ascii bytes, no text-mutating css); smoke 105/105 + restart + reminder + browser pass green, irene hand-migrated to the new layout
2026-11-15 14:35:00 +00:00
|
|
|
// server/provider.mjs -- the endpoint client. One OpenAI-compatible
|
2026-11-14 03:15:53 +00:00
|
|
|
// endpoint per deployment (server.json "endpoint"); the model name
|
|
|
|
|
// comes from the run (run.json override) or the config default.
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
/** @typedef {import('./shapes.mjs').ViewMessage} ViewMessage */
|
|
|
|
|
/** @typedef {import('./shapes.mjs').ToolCall} ToolCall */
|
|
|
|
|
/** @typedef {import('./shapes.mjs').ChatResponse} ChatResponse */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {{ baseUrl?: string, apiKey?: string, apiKeyEnv?: string }} endpoint
|
|
|
|
|
*/
|
2026-11-14 03:15:53 +00:00
|
|
|
export function makeProvider(endpoint) {
|
|
|
|
|
const base = String(endpoint.baseUrl ?? '').replace(/\/+$/, '');
|
|
|
|
|
if (!base) throw new Error('server.json: endpoint.baseUrl is required');
|
|
|
|
|
const key = endpoint.apiKey ?? process.env[endpoint.apiKeyEnv ?? 'RP_API_KEY'] ?? '';
|
|
|
|
|
const headers = { 'content-type': 'application/json', authorization: `Bearer ${key}` };
|
|
|
|
|
|
|
|
|
|
return {
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
/**
|
|
|
|
|
* @param {?string} model @param {ViewMessage[]} messages
|
|
|
|
|
* @param {?import('./shapes.mjs').ToolDef[]} [tools] @param {Record<string, unknown>} [params]
|
|
|
|
|
* @returns {Promise<ChatResponse>}
|
|
|
|
|
*/
|
2026-11-14 03:15:53 +00:00
|
|
|
async chat(model, messages, tools, params = {}) {
|
|
|
|
|
const res = await fetch(`${base}/chat/completions`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers,
|
|
|
|
|
body: JSON.stringify({ model, messages, ...(tools ? { tools, tool_choice: 'auto' } : {}), ...params }),
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
throw new Error(`API ${res.status}: ${(await res.text()).slice(0, 300)}`);
|
|
|
|
|
}
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
const data = /** @type {ChatResponse} */ (await res.json());
|
2026-11-14 03:15:53 +00:00
|
|
|
return data;
|
|
|
|
|
},
|
|
|
|
|
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
/** @returns {Promise<string[]>} */
|
2026-11-14 03:15:53 +00:00
|
|
|
async models() {
|
|
|
|
|
const res = await fetch(`${base}/models`, { headers });
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
throw new Error(`API ${res.status}: ${(await res.text()).slice(0, 300)}`);
|
|
|
|
|
}
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
const data = /** @type {{ data: { id: string }[] }} */ (await res.json());
|
2026-11-14 03:15:53 +00:00
|
|
|
return (data.data ?? []).map((m) => m.id).sort();
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|