@geraldmaron/construct 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,183 @@
1
+ /**
2
+ * apps/chat/engine/ai-sdk-agent.mjs — the real owned-loop engine (Vercel AI SDK).
3
+ *
4
+ * Lazy-loaded by the launcher only when `construct chat` runs the rich/owned path,
5
+ * so the optional dependencies (`ai`, `@ai-sdk/*`, `zod`) never load in the zero-dep
6
+ * core or in tests of the mapping layer. It resolves a Construct model id (the
7
+ * router's `provider/model` form) to an AI SDK language model through the adapter
8
+ * registry (provider-adapters.mjs), builds the agent tool set from the tool
9
+ * registry, and runs streamText under the turn's compiled execution policy
10
+ * (turn-controls.mjs): the step cap, tool-group / schema budget, output cap, and
11
+ * caching eligibility all derive from the resolved capability profile, so the loop
12
+ * adapts to the model while staying behavior-preserving for hosted-direct. It
13
+ * yields the SDK fullStream parts unchanged; loop-driver.mjs owns the
14
+ * normalization into the event union.
15
+ */
16
+
17
+ import { listChatModels } from './models.mjs';
18
+ import { resolveLanguageModel } from './provider-adapters.mjs';
19
+ import { resolveTurnControls } from './turn-controls.mjs';
20
+ import { buildSystemPrompt } from '../../../lib/chat/system-prompt.mjs';
21
+ import { applyToolBudget } from '../../../lib/mcp/tool-budget.mjs';
22
+ import { recordPolicyTelemetry } from '../../../lib/chat/policy-telemetry.mjs';
23
+ import { maybeCompact, estimateTextTokens, messageText } from '../../../lib/chat/context-compactor.mjs';
24
+
25
+ // Real summarization of the compactible layers (tool results, prior assistant
26
+ // reasoning) the contract elides, run on the same language model as the turn.
27
+ // Bounded output and zero retries keep the once-per-compaction cost small; a throw
28
+ // is caught upstream and falls back to the deterministic extractive summary, so a
29
+ // summarizer failure degrades fidelity without breaking the turn.
30
+
31
+ function makeSummarizer({ generateText, languageModel, signal }) {
32
+ return async (text, meta = {}) => {
33
+ const { text: summary } = await generateText({
34
+ model: languageModel,
35
+ system: 'Compress earlier agent work into a faithful, terse recap for context continuation. Preserve decisions, findings, file paths, identifiers, and unresolved threads. Never invent facts. No preamble.',
36
+ prompt: `Summarize these ${meta.segmentCount || 'earlier'} turn(s) of an agent working in a repository. Use compact bullets, keep concrete identifiers, stay under 200 words:\n\n${text}`,
37
+ maxOutputTokens: 512,
38
+ maxRetries: 0,
39
+ abortSignal: signal,
40
+ });
41
+ return summary;
42
+ };
43
+ }
44
+
45
+ export async function createAiSdkAgent({ env = process.env, cwd = process.cwd(), model = null, handlers = {}, systemPrompt = '', tools = null, onTelemetry = recordPolicyTelemetry } = {}) {
46
+ const { streamText, stepCountIs, generateText } = await import('ai');
47
+
48
+ const { buildAgentTools } = await import('./tools/registry.mjs');
49
+ const sdkTools = await buildAgentTools({ env, cwd, handlers, only: tools });
50
+
51
+ const messages = [];
52
+ const languageModels = new Map();
53
+ let activeModelId = model;
54
+ let contextTokens = 0;
55
+
56
+ async function languageModelFor(modelId) {
57
+ const id = modelId || activeModelId;
58
+ if (!id) {
59
+ const err = new Error('No model selected and no configured provider found. Run `construct models` or set CX_MODEL_STANDARD.');
60
+ err.code = 'PROVIDER_MODEL_UNRESOLVED';
61
+ throw err;
62
+ }
63
+ if (!languageModels.has(id)) {
64
+ languageModels.set(id, await resolveLanguageModel(id, env));
65
+ }
66
+ return languageModels.get(id);
67
+ }
68
+
69
+ return {
70
+ sessionId: `construct-${Date.now()}`,
71
+ model,
72
+ listModels: () => listChatModels({ env }),
73
+ async *streamTurn(text, { signal, model: turnModel = null, turnOverlay = null } = {}) {
74
+ if (turnModel) activeModelId = turnModel;
75
+ const languageModel = await languageModelFor(activeModelId);
76
+
77
+ const controls = resolveTurnControls({ model: activeModelId, turnOverlay, env });
78
+ if (controls.degraded) {
79
+ onTelemetry({
80
+ kind: 'execution-policy-degraded',
81
+ model: activeModelId,
82
+ capabilityClass: controls.policy?.source?.capabilityClass || 'unknown',
83
+ reasons: controls.policy?.telemetry?.reasons || [],
84
+ });
85
+ }
86
+ const turnTools = applyToolBudget(sdkTools, {
87
+ allowedToolGroups: controls.allowedToolGroups,
88
+ maxToolSchemas: controls.maxToolSchemas,
89
+ });
90
+
91
+ // Per-turn routing policy belongs in the SYSTEM role (via buildSystemPrompt),
92
+ // never prepended to the user message: a user-role policy gets echoed in model
93
+ // reasoning and compounds in persisted history.
94
+ messages.push({ role: 'user', content: String(text) });
95
+ const systemText = buildSystemPrompt({ base: systemPrompt || undefined, overlay: turnOverlay });
96
+
97
+ const request = {
98
+ model: languageModel,
99
+ tools: turnTools,
100
+ stopWhen: stepCountIs(controls.iterations),
101
+ abortSignal: signal,
102
+ maxRetries: 0,
103
+ };
104
+ if (controls.outputCap) request.maxOutputTokens = controls.outputCap;
105
+
106
+ // A cache-eligible provider marks the stable system prefix as a cache
107
+ // breakpoint: the AI SDK maps a single leading system message onto the
108
+ // provider's system field, so the request stays output-identical and only the
109
+ // cache_control annotation is added. Every other provider keeps the plain
110
+ // system string — today's exact request shape.
111
+ if (controls.cacheEligible) {
112
+ const providerNs = String(activeModelId || '').split('/')[0] || 'anthropic';
113
+ request.messages = [
114
+ { role: 'system', content: systemText, providerOptions: { [providerNs]: { cacheControl: { type: 'ephemeral' } } } },
115
+ ...messages,
116
+ ];
117
+ } else {
118
+ request.system = systemText;
119
+ request.messages = messages;
120
+ }
121
+
122
+ const result = streamText(request);
123
+
124
+ // The compaction trigger needs the current context SIZE, which is the last
125
+ // model call's per-step input (system + full history), not the turn's
126
+ // aggregate input — totalUsage sums input across tool steps and would
127
+ // over-count a multi-step turn, tripping compaction below the real budget.
128
+ // Prefer the last finish-step's per-call usage; fall back to the aggregate
129
+ // only when the host reports no per-step usage. Host numbers only, no split.
130
+ let lastStepUsage = null;
131
+ let aggregateUsage = null;
132
+ for await (const part of result.fullStream) {
133
+ if (part.type === 'finish-step' && part.usage) lastStepUsage = part.usage;
134
+ if (part.type === 'finish') aggregateUsage = part.totalUsage || part.usage || aggregateUsage;
135
+ yield part;
136
+ }
137
+ const turnUsage = lastStepUsage || aggregateUsage;
138
+
139
+ // Persist the assistant turn (incl. tool exchanges) so the next prompt has history.
140
+ try {
141
+ const response = await result.response;
142
+ if (Array.isArray(response?.messages)) messages.push(...response.messages);
143
+ // Surface the model that actually answered — the OpenRouter free router
144
+ // resolves to an underlying model id, not the "openrouter/free" alias.
145
+ const resolved = response?.modelId;
146
+ if (resolved && resolved !== activeModelId && !String(activeModelId).endsWith(resolved)) {
147
+ yield { type: 'model-resolved', model: resolved };
148
+ }
149
+ } catch { /* history append is best-effort */ }
150
+
151
+ // The next turn's context ≈ this turn's input (system + full history) plus the
152
+ // output just appended. When that crosses the policy's continuation trigger,
153
+ // compact without silent loss. Behavior-preserving: hosted only crosses near
154
+ // its ~150k budget, so short conversations are never touched. Compaction is
155
+ // best-effort — any failure leaves the live history intact.
156
+ const input = Number(turnUsage?.inputTokens) || 0;
157
+ const output = Number(turnUsage?.outputTokens) || 0;
158
+ if (input || output) contextTokens = input + output;
159
+
160
+ const trigger = controls.continuation?.triggerTokens || null;
161
+ if (trigger && contextTokens >= trigger) {
162
+ try {
163
+ const outcome = await maybeCompact({
164
+ messages,
165
+ systemText,
166
+ triggerTokens: trigger,
167
+ contextTokens,
168
+ summarize: makeSummarizer({ generateText, languageModel, signal }),
169
+ });
170
+ if (outcome.packet) yield { type: 'context-continuation', packet: outcome.packet, compacted: outcome.compacted };
171
+ if (outcome.notice) {
172
+ yield { type: 'context-notice', level: outcome.blocker ? 'warn' : 'info', code: outcome.blocker ? 'context-blocker' : 'context-compacted', message: outcome.notice };
173
+ }
174
+ if (outcome.compacted && Array.isArray(outcome.messages)) {
175
+ messages.length = 0;
176
+ messages.push(...outcome.messages);
177
+ contextTokens = estimateTextTokens(systemText) + messages.reduce((n, m) => n + estimateTextTokens(messageText(m)), 0);
178
+ }
179
+ } catch { /* compaction must never break a turn */ }
180
+ }
181
+ },
182
+ };
183
+ }
@@ -0,0 +1,211 @@
1
+ /**
2
+ * apps/chat/engine/loop-driver.mjs — Construct's owned agent loop, exposed as a
3
+ * driver implementing the normalized event union (lib/chat/harness/driver.mjs).
4
+ *
5
+ * ADR-0041 reverses the delegate-the-loop posture: Construct now runs the loop
6
+ * (prompt -> model -> tool calls -> tool results -> repeat) on a provider-agnostic
7
+ * engine (Vercel AI SDK), so every token, tool result, and routing choice is
8
+ * first-party data. This module is the seam that keeps the rest of the surface
9
+ * host-agnostic: it owns the lifecycle (start/prompt/cancel/stop), maps the
10
+ * engine's fullStream parts onto the driver event union, and accumulates per-turn
11
+ * usage from the host's own numbers (no fabricated splits).
12
+ *
13
+ * The engine itself is injected as `createAgent` so this mapping layer is tested
14
+ * against a scripted mock with no network or API key — the same dependency-
15
+ * injection discipline the retired host adapters used (fetchImpl / spawnFn). The
16
+ * real engine lives in ai-sdk-agent.mjs and is lazy-imported only when chat runs.
17
+ */
18
+
19
+ import { AsyncEventQueue } from '../../../lib/chat/harness/driver.mjs';
20
+ import { listChatModels, recommendChatModel } from './models.mjs';
21
+
22
+ // Vercel AI SDK fullStream parts use a few delta field names across versions;
23
+ // read text from any of them so the mapping survives a minor SDK bump.
24
+
25
+ function partText(part) {
26
+ if (typeof part.text === 'string') return part.text;
27
+ if (typeof part.textDelta === 'string') return part.textDelta;
28
+ if (typeof part.delta === 'string') return part.delta;
29
+ return '';
30
+ }
31
+
32
+ // Usage arrives on finish parts as inputTokens/outputTokens/etc.; normalize to
33
+ // the union's token shape, emitting only fields the engine actually reported.
34
+
35
+ function normalizeUsage(part) {
36
+ const u = part.totalUsage || part.usage || null;
37
+ if (!u) return null;
38
+ const tokens = {};
39
+ if (Number.isFinite(u.inputTokens)) tokens.input = u.inputTokens;
40
+ if (Number.isFinite(u.outputTokens)) tokens.output = u.outputTokens;
41
+ if (Number.isFinite(u.reasoningTokens)) tokens.reasoning = u.reasoningTokens;
42
+ if (Number.isFinite(u.cachedInputTokens)) tokens.cacheRead = u.cachedInputTokens;
43
+ if (Number.isFinite(u.totalTokens)) tokens.total = u.totalTokens;
44
+ else if (Number.isFinite(tokens.input) || Number.isFinite(tokens.output)) {
45
+ tokens.total = (tokens.input || 0) + (tokens.output || 0) + (tokens.reasoning || 0);
46
+ }
47
+ if (!Object.keys(tokens).length) return null;
48
+ const event = { type: 'usage', tokens };
49
+ if (part.providerMetadata?.cost && Number.isFinite(part.providerMetadata.cost.amount)) {
50
+ event.cost = { amount: part.providerMetadata.cost.amount, currency: part.providerMetadata.cost.currency || 'USD' };
51
+ }
52
+ return event;
53
+ }
54
+
55
+ // One fullStream part -> zero or more normalized driver events on the turn queue.
56
+ // Tool ids are tracked so a tool_call is emitted once and tool_update carries the
57
+ // terminal state, matching the union the renderers already consume.
58
+
59
+ function mapPart(part, queue, state) {
60
+ switch (part.type) {
61
+ case 'text-delta':
62
+ case 'text': {
63
+ const t = partText(part);
64
+ if (t) queue.push({ type: 'text', text: t, messageId: part.id || null });
65
+ break;
66
+ }
67
+ case 'reasoning-delta':
68
+ case 'reasoning': {
69
+ const t = partText(part);
70
+ if (t) queue.push({ type: 'thinking', text: t, messageId: part.id || null });
71
+ break;
72
+ }
73
+ case 'model-resolved': {
74
+ if (part.model) queue.push({ type: 'model_resolved', model: part.model });
75
+ break;
76
+ }
77
+ case 'context-notice': {
78
+ queue.push({ type: 'notice', level: part.level || 'info', code: part.code || 'context', message: part.message || '' });
79
+ break;
80
+ }
81
+ case 'context-continuation': {
82
+ queue.push({ type: 'context_continuation', packet: part.packet || null, compacted: part.compacted === true });
83
+ break;
84
+ }
85
+ case 'tool-call': {
86
+ const id = part.toolCallId || part.id;
87
+ state.tools.add(id);
88
+ queue.push({ type: 'tool_call', id, title: part.toolName || 'tool', kind: part.toolName || 'other', status: 'pending', input: part.input ?? part.args ?? null });
89
+ break;
90
+ }
91
+ case 'tool-result': {
92
+ const id = part.toolCallId || part.id;
93
+ queue.push({ type: 'tool_update', id, status: 'completed', content: part.output ?? part.result ?? null });
94
+ break;
95
+ }
96
+ case 'tool-error': {
97
+ const id = part.toolCallId || part.id;
98
+ queue.push({ type: 'tool_update', id, status: 'failed', content: part.error ? String(part.error.message || part.error) : null });
99
+ break;
100
+ }
101
+ case 'finish-step':
102
+ case 'finish': {
103
+ const usage = normalizeUsage(part);
104
+ if (usage) queue.push(usage);
105
+ break;
106
+ }
107
+ case 'error':
108
+ state.errored = true;
109
+ queue.push({ type: 'error', message: part.error ? String(part.error.message || part.error) : 'engine error' });
110
+ break;
111
+ case 'abort':
112
+ state.aborted = true;
113
+ break;
114
+ default:
115
+ break;
116
+ }
117
+ }
118
+
119
+ export function createOwnedLoopDriver({
120
+ env = process.env,
121
+ cwd = process.cwd(),
122
+ model = null,
123
+ handlers = {},
124
+ systemPrompt = '',
125
+ tools = null,
126
+ createAgent,
127
+ } = {}) {
128
+ if (typeof createAgent !== 'function') {
129
+ throw new Error('createOwnedLoopDriver requires a `createAgent` factory (real engine or test mock)');
130
+ }
131
+
132
+ let agent = null;
133
+ let activeTurn = null;
134
+ let currentModel = model || null;
135
+
136
+ async function start() {
137
+ agent = await createAgent({ env, cwd, model: currentModel, handlers, systemPrompt, tools });
138
+ if (agent?.model && !currentModel) currentModel = agent.model;
139
+ return { sessionId: agent?.sessionId || `owned-${Date.now()}`, capabilities: { host: 'construct', ownedLoop: true } };
140
+ }
141
+
142
+ function prompt(text, opts = {}) {
143
+ if (!agent) throw new Error('prompt() called before start()');
144
+ const queue = new AsyncEventQueue();
145
+ const controller = new AbortController();
146
+ const turn = { queue, controller, tools: new Set(), aborted: false, errored: false };
147
+ activeTurn = turn;
148
+ if (opts.model) currentModel = opts.model;
149
+
150
+ (async () => {
151
+ try {
152
+ const stream = await agent.streamTurn(text, {
153
+ signal: controller.signal,
154
+ model: currentModel,
155
+ turnOverlay: opts.turnOverlay ?? null,
156
+ });
157
+ for await (const part of stream) {
158
+ if (queue.closed) break;
159
+ mapPart(part, queue, turn);
160
+ }
161
+ queue.push({ type: 'done', stopReason: turn.aborted ? 'cancelled' : turn.errored ? 'error' : 'end_turn' });
162
+ } catch (err) {
163
+ if (err?.name === 'AbortError' || turn.aborted) {
164
+ queue.push({ type: 'done', stopReason: 'cancelled' });
165
+ } else {
166
+ queue.push({ type: 'error', message: err?.message || String(err) });
167
+ queue.push({ type: 'done', stopReason: 'error' });
168
+ }
169
+ } finally {
170
+ queue.close();
171
+ if (activeTurn === turn) activeTurn = null;
172
+ }
173
+ })();
174
+
175
+ return queue;
176
+ }
177
+
178
+ function cancel() {
179
+ if (activeTurn) {
180
+ activeTurn.aborted = true;
181
+ try { activeTurn.controller.abort(); } catch { /* already aborted */ }
182
+ }
183
+ }
184
+
185
+ function stop() {
186
+ try { agent?.dispose?.(); } catch { /* nothing to dispose */ }
187
+ }
188
+
189
+ async function listModels() {
190
+ if (agent?.listModels) {
191
+ try { return await agent.listModels(); } catch { /* fall through to catalog */ }
192
+ }
193
+ return listChatModels({ env });
194
+ }
195
+
196
+ async function recommendModel() {
197
+ if (currentModel) return null;
198
+ return recommendChatModel({ env });
199
+ }
200
+
201
+ return {
202
+ start,
203
+ prompt,
204
+ cancel,
205
+ stop,
206
+ listModels,
207
+ recommendModel,
208
+ get model() { return currentModel; },
209
+ get sessionId() { return agent?.sessionId || null; },
210
+ };
211
+ }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * apps/chat/engine/models.mjs — model and provider resolution for the owned loop.
3
+ *
4
+ * The owned loop is provider-agnostic (ADR-0041/ADR-0003): model choice is never
5
+ * hardcoded. This module reuses the core router (lib/model-router.mjs) so the chat
6
+ * surface, the orchestration worker, and the embedded contract all resolve models
7
+ * the same way. resolveChatModelSelection validates CX_MODEL pins against live
8
+ * credential detection; resolveChatModelSelectionAsync also probes Copilot session
9
+ * exchange so a bad token falls through before the first turn.
10
+ */
11
+
12
+ import {
13
+ getProviderModelCatalog,
14
+ describeModelFamily,
15
+ resolveValidatedChatModel,
16
+ isChatModelAvailable,
17
+ } from '../../../lib/model-router.mjs';
18
+ import { resolveFirstSecret } from '../../../lib/providers/secret-resolver.mjs';
19
+
20
+ export function listChatModels({ env = process.env, cwd = process.cwd(), activeModelId = null } = {}) {
21
+ const { providers } = getProviderModelCatalog({ env, cwd, activeModelId });
22
+ const models = [];
23
+ const seen = new Set();
24
+ for (const provider of providers) {
25
+ for (const tier of ['reasoning', 'standard', 'fast']) {
26
+ for (const id of provider.options?.[tier] || []) {
27
+ if (seen.has(id)) continue;
28
+ seen.add(id);
29
+ models.push({
30
+ id,
31
+ label: id,
32
+ provider: provider.id,
33
+ configured: provider.configured,
34
+ local: provider.local === true,
35
+ suitable: true,
36
+ tier,
37
+ available: isChatModelAvailable(id, { env }).ok,
38
+ });
39
+ }
40
+ }
41
+ }
42
+ return models.sort((a, b) => Number(b.configured) - Number(a.configured) || a.id.localeCompare(b.id));
43
+ }
44
+
45
+ export { refreshLiveOpenRouterCatalog } from '../../../lib/models/catalog.mjs';
46
+
47
+ export function recommendChatModel({ env = process.env } = {}) {
48
+ const { providers } = getProviderModelCatalog({ env });
49
+ const configured = providers.find((p) => p.configured);
50
+ if (!configured) return null;
51
+ const id = configured.tiers?.standard || configured.tiers?.fast || null;
52
+ if (!id) return null;
53
+ const check = isChatModelAvailable(id, { env });
54
+ if (!check.ok) return null;
55
+ return { id, reason: `configured provider ${configured.label}` };
56
+ }
57
+
58
+ export function resolveChatModelSelection({ env = process.env, requested = null, excludeFamilies = [] } = {}) {
59
+ return resolveValidatedChatModel({ env, requested, excludeFamilies });
60
+ }
61
+
62
+ export async function resolveFreeOpenRouterModel({ env = process.env, tier = 'standard', exclude = [] } = {}) {
63
+ const apiKey = resolveFirstSecret(['OPENROUTER_API_KEY', 'OPEN_ROUTER_API_KEY'], { env });
64
+ if (!apiKey) return null;
65
+ const excludeSet = new Set(Array.isArray(exclude) ? exclude : []);
66
+ const { pollFreeModels, topForTier } = await import('../../../lib/model-free-selector.mjs');
67
+ const freeModels = await pollFreeModels(apiKey);
68
+ for (const candidate of topForTier(freeModels, tier, 20)) {
69
+ const modelId = candidate.id.startsWith('openrouter/') ? candidate.id : `openrouter/${candidate.id}`;
70
+ if (excludeSet.has(modelId)) continue;
71
+ if (isChatModelAvailable(modelId, { env }).ok) return modelId;
72
+ }
73
+ return null;
74
+ }
75
+
76
+ export async function resolveSessionModel(session, { env = process.env, exclude = [], tier = 'standard' } = {}) {
77
+ if (session?.modelMode === 'free-router') {
78
+ const merged = [...new Set([...getExcludeFromSession(session), ...exclude])];
79
+ return resolveFreeOpenRouterModel({ env, tier, exclude: merged });
80
+ }
81
+ return session?.model || session?.savedModel || null;
82
+ }
83
+
84
+ function getExcludeFromSession(session) {
85
+ if (!session?.failedModels) return [];
86
+ return session.failedModels instanceof Set ? [...session.failedModels] : [];
87
+ }
88
+
89
+ export async function resolveChatModelSelectionAsync({
90
+ env = process.env,
91
+ requested = null,
92
+ fetchImpl = fetch,
93
+ } = {}) {
94
+ let resolution = resolveValidatedChatModel({ env, requested });
95
+ if (!resolution.id?.startsWith('github-copilot/')) return resolution;
96
+
97
+ const { preflightCopilotSession } = await import('../../../lib/providers/copilot-auth.mjs');
98
+ const probe = await preflightCopilotSession({ fetchImpl });
99
+ if (probe.ok) return resolution;
100
+
101
+ const fallback = resolveValidatedChatModel({ env, requested: null, excludeFamilies: ['github-copilot'] });
102
+ if (fallback.id) {
103
+ return {
104
+ ...fallback,
105
+ notice: `GitHub Copilot session failed (${probe.message}). Using ${fallback.id}.`,
106
+ };
107
+ }
108
+ return {
109
+ id: null,
110
+ source: null,
111
+ notice: `GitHub Copilot session failed: ${probe.message}`,
112
+ rejected: resolution.rejected,
113
+ };
114
+ }
115
+
116
+ export function resolveChatModel(opts = {}) {
117
+ return resolveChatModelSelection(opts).id;
118
+ }
119
+
120
+ export function describeChatModel(modelId, { env = process.env } = {}) {
121
+ return describeModelFamily(modelId, { env });
122
+ }
@@ -0,0 +1,171 @@
1
+ /**
2
+ * apps/chat/engine/provider-adapters.mjs — centralized provider execution adapters
3
+ * (construct-6zga.1.3).
4
+ *
5
+ * One registry, keyed by provider group, owns provider execution. Each adapter
6
+ * owns its own credential resolution, base URL, native model-id translation, auth
7
+ * scheme, and AI SDK provider construction; the dispatcher does no
8
+ * provider/model-prefix behavior branch — it extracts the structural provider
9
+ * group (the model id's first segment, the same identity lib/models keys on) and
10
+ * looks the adapter up. Adding a compatible provider is a registry entry plus
11
+ * matrix fixtures, never a dispatch edit.
12
+ *
13
+ * Public model ids are preserved: the native id handed to the SDK is the model id
14
+ * with its group segment stripped, so anthropic/claude-x → claude-x and
15
+ * openrouter/anthropic/claude-x → anthropic/claude-x (OpenRouter's own id).
16
+ * Credentials resolve through the shared secret resolver (env, dotenv, shell rc,
17
+ * 1Password op:// refs); a missing key fails fast with a remediation hint.
18
+ */
19
+ import { resolveFirstSecret } from '../../../lib/providers/secret-resolver.mjs';
20
+ import { providerGroupForModel } from '../../../lib/models/execution-capability-profile.mjs';
21
+
22
+ const OPENROUTER_BASE = 'https://openrouter.ai/api/v1';
23
+
24
+ function envKey(env, ...names) {
25
+ return resolveFirstSecret(names, { env });
26
+ }
27
+
28
+ function missingKey(provider, varName) {
29
+ const err = new Error(`No credentials for ${provider}: set ${varName} (or run \`construct creds\`) and retry.`);
30
+ err.code = 'PROVIDER_KEY_MISSING';
31
+ return err;
32
+ }
33
+
34
+ // Adapter descriptors. `describe` is the serializable contract (auth scheme,
35
+ // protocol, credential env, base URL source) for tracing and conformance fixtures;
36
+ // `createModel` builds the AI SDK handle from the native id. The @ai-sdk/* imports
37
+ // stay lazy so the zero-dep core never loads them.
38
+
39
+ const ADAPTERS = {
40
+ anthropic: {
41
+ id: 'anthropic',
42
+ auth: 'api_key',
43
+ protocol: 'anthropic-messages',
44
+ credentialEnv: ['ANTHROPIC_API_KEY'],
45
+ baseURL: 'default',
46
+ async createModel({ nativeModelId, env }) {
47
+ const apiKey = envKey(env, 'ANTHROPIC_API_KEY');
48
+ if (!apiKey) throw missingKey('Anthropic', 'ANTHROPIC_API_KEY');
49
+ const { createAnthropic } = await import('@ai-sdk/anthropic');
50
+ return createAnthropic({ apiKey })(nativeModelId);
51
+ },
52
+ },
53
+ openai: {
54
+ id: 'openai',
55
+ auth: 'api_key',
56
+ protocol: 'openai-chat-completions',
57
+ credentialEnv: ['OPENAI_API_KEY'],
58
+ baseURL: 'default',
59
+ async createModel({ nativeModelId, env }) {
60
+ const apiKey = envKey(env, 'OPENAI_API_KEY');
61
+ if (!apiKey) throw missingKey('OpenAI', 'OPENAI_API_KEY');
62
+ const { createOpenAI } = await import('@ai-sdk/openai');
63
+ return createOpenAI({ apiKey })(nativeModelId);
64
+ },
65
+ },
66
+ openrouter: {
67
+ id: 'openrouter',
68
+ auth: 'api_key',
69
+ protocol: 'openai-compatible',
70
+ credentialEnv: ['OPENROUTER_API_KEY', 'OPEN_ROUTER_API_KEY'],
71
+ baseURL: OPENROUTER_BASE,
72
+ async createModel({ nativeModelId, env }) {
73
+ const apiKey = envKey(env, 'OPENROUTER_API_KEY', 'OPEN_ROUTER_API_KEY');
74
+ if (!apiKey) throw missingKey('OpenRouter', 'OPENROUTER_API_KEY');
75
+ const { createOpenAICompatible } = await import('@ai-sdk/openai-compatible');
76
+ return createOpenAICompatible({ name: 'openrouter', baseURL: OPENROUTER_BASE, apiKey })(nativeModelId);
77
+ },
78
+ },
79
+ ollama: {
80
+ id: 'ollama',
81
+ auth: 'none',
82
+ protocol: 'openai-compatible',
83
+ credentialEnv: [],
84
+ baseURL: 'env:OLLAMA_BASE_URL',
85
+ async createModel({ modelId, nativeModelId, env }) {
86
+ const { isOllamaModelInstalled, formatOllamaModelMissingMessage } = await import('../../../lib/ollama/installed-models.mjs');
87
+ if (isOllamaModelInstalled(modelId, { env }) === false) {
88
+ const err = new Error(formatOllamaModelMissingMessage(nativeModelId));
89
+ err.code = 'OLLAMA_MODEL_NOT_PULLED';
90
+ throw err;
91
+ }
92
+ const baseURL = envKey(env, 'OLLAMA_BASE_URL') || 'http://localhost:11434/v1';
93
+ const { createOpenAICompatible } = await import('@ai-sdk/openai-compatible');
94
+ return createOpenAICompatible({ name: 'ollama', baseURL, apiKey: 'ollama' })(nativeModelId);
95
+ },
96
+ },
97
+ local: {
98
+ id: 'local',
99
+ auth: 'api_key',
100
+ protocol: 'openai-compatible',
101
+ credentialEnv: ['LOCAL_LLM_API_KEY'],
102
+ baseURL: 'env:LOCAL_LLM_BASE_URL',
103
+ async createModel({ nativeModelId, env }) {
104
+ const baseURL = envKey(env, 'LOCAL_LLM_BASE_URL');
105
+ if (!baseURL) throw missingKey('local OpenAI-compatible server', 'LOCAL_LLM_BASE_URL');
106
+ const { createOpenAICompatible } = await import('@ai-sdk/openai-compatible');
107
+ return createOpenAICompatible({ name: 'local', baseURL, apiKey: envKey(env, 'LOCAL_LLM_API_KEY') || 'local' })(nativeModelId);
108
+ },
109
+ },
110
+ 'github-copilot': {
111
+ id: 'github-copilot',
112
+ auth: 'oauth',
113
+ protocol: 'openai-chat-completions',
114
+ credentialEnv: [],
115
+ baseURL: 'copilot',
116
+ async createModel({ nativeModelId }) {
117
+ const { getCopilotToken, copilotApiHeaders, COPILOT_API_BASE } = await import('../../../lib/providers/copilot-auth.mjs');
118
+ const { createOpenAICompatible } = await import('@ai-sdk/openai-compatible');
119
+ const copilotFetch = async (url, init = {}) => {
120
+ const token = await getCopilotToken();
121
+ const headers = new Headers(init.headers);
122
+ for (const [key, value] of Object.entries(copilotApiHeaders())) headers.set(key, value);
123
+ headers.set('Authorization', `Bearer ${token}`);
124
+ return fetch(url, { ...init, headers });
125
+ };
126
+ return createOpenAICompatible({ name: 'github-copilot', baseURL: COPILOT_API_BASE, apiKey: 'via-fetch', fetch: copilotFetch })(nativeModelId);
127
+ },
128
+ },
129
+ };
130
+
131
+ export function getProviderAdapter(group) {
132
+ return ADAPTERS[group] || null;
133
+ }
134
+
135
+ // Serializable adapter contract for tracing and conformance fixtures (no secrets,
136
+ // no SDK handles) — the metadata a new provider must register.
137
+
138
+ export function describeProviderAdapters() {
139
+ return Object.values(ADAPTERS).map((a) => ({
140
+ id: a.id,
141
+ auth: a.auth,
142
+ protocol: a.protocol,
143
+ credentialEnv: a.credentialEnv,
144
+ baseURL: a.baseURL,
145
+ }));
146
+ }
147
+
148
+ export function nativeModelId(modelId, group = providerGroupForModel(modelId)) {
149
+ return String(modelId).slice(group.length + 1);
150
+ }
151
+
152
+ /**
153
+ * Map a Construct "provider/model" id onto an AI SDK language model via the
154
+ * adapter registry. No provider/model-prefix behavior branch: the group is a
155
+ * structural key, the adapter owns the behavior.
156
+ */
157
+ export async function resolveLanguageModel(modelId, env) {
158
+ if (!modelId) {
159
+ const err = new Error('No model selected and no configured provider found. Run `construct models` or set CX_MODEL_STANDARD.');
160
+ err.code = 'PROVIDER_MODEL_UNRESOLVED';
161
+ throw err;
162
+ }
163
+ const group = providerGroupForModel(modelId);
164
+ const adapter = ADAPTERS[group];
165
+ if (!adapter) {
166
+ const err = new Error(`Provider for model '${modelId}' is not wired into the owned loop yet. Try an anthropic/, openai/, openrouter/, ollama/, local/, or github-copilot/ model.`);
167
+ err.code = 'PROVIDER_UNSUPPORTED';
168
+ throw err;
169
+ }
170
+ return adapter.createModel({ modelId, nativeModelId: nativeModelId(modelId, group), env });
171
+ }