@cleocode/core 2026.6.5 → 2026.6.7
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.
- package/dist/dispatch/contracts/output-contracts.d.ts +36 -0
- package/dist/dispatch/contracts/output-contracts.d.ts.map +1 -0
- package/dist/dispatch/contracts/output-contracts.js +38 -0
- package/dist/dispatch/contracts/output-contracts.js.map +1 -0
- package/dist/dispatch/describe-operation.d.ts +98 -0
- package/dist/dispatch/describe-operation.d.ts.map +1 -0
- package/dist/dispatch/describe-operation.js +101 -0
- package/dist/dispatch/describe-operation.js.map +1 -0
- package/dist/docs/export-document.js +933 -489
- package/dist/docs/export-document.js.map +3 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +2 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +6 -0
- package/dist/internal.js.map +1 -1
- package/dist/llm/api-mode.d.ts +64 -0
- package/dist/llm/api-mode.d.ts.map +1 -0
- package/dist/llm/api-mode.js +72 -0
- package/dist/llm/api-mode.js.map +1 -0
- package/dist/llm/api.d.ts.map +1 -1
- package/dist/llm/api.js +6 -37
- package/dist/llm/api.js.map +1 -1
- package/dist/llm/auxiliary-fallback.d.ts.map +1 -1
- package/dist/llm/auxiliary-fallback.js +24 -38
- package/dist/llm/auxiliary-fallback.js.map +1 -1
- package/dist/llm/index.d.ts +1 -3
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +1 -2
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/model-metadata.d.ts +14 -0
- package/dist/llm/model-metadata.d.ts.map +1 -1
- package/dist/llm/model-metadata.js +23 -0
- package/dist/llm/model-metadata.js.map +1 -1
- package/dist/llm/model-runner.d.ts +127 -0
- package/dist/llm/model-runner.d.ts.map +1 -0
- package/dist/llm/model-runner.js +312 -0
- package/dist/llm/model-runner.js.map +1 -0
- package/dist/llm/plugin-facade.js +30084 -1748
- package/dist/llm/plugin-facade.js.map +3 -3
- package/dist/llm/provider-registry/builtin/anthropic.d.ts.map +1 -1
- package/dist/llm/provider-registry/builtin/anthropic.js +4 -0
- package/dist/llm/provider-registry/builtin/anthropic.js.map +1 -1
- package/dist/llm/provider-registry/builtin/gemini.d.ts.map +1 -1
- package/dist/llm/provider-registry/builtin/gemini.js +4 -0
- package/dist/llm/provider-registry/builtin/gemini.js.map +1 -1
- package/dist/llm/provider-registry/builtin/ollama.d.ts.map +1 -1
- package/dist/llm/provider-registry/builtin/ollama.js +4 -0
- package/dist/llm/provider-registry/builtin/ollama.js.map +1 -1
- package/dist/llm/provider-registry/builtin/openai.d.ts.map +1 -1
- package/dist/llm/provider-registry/builtin/openai.js +6 -0
- package/dist/llm/provider-registry/builtin/openai.js.map +1 -1
- package/dist/llm/role-executor.d.ts +6 -5
- package/dist/llm/role-executor.d.ts.map +1 -1
- package/dist/llm/role-executor.js +40 -86
- package/dist/llm/role-executor.js.map +1 -1
- package/dist/llm/role-resolver.d.ts +28 -1
- package/dist/llm/role-resolver.d.ts.map +1 -1
- package/dist/llm/role-resolver.js +10 -0
- package/dist/llm/role-resolver.js.map +1 -1
- package/dist/llm/session-factory.d.ts +4 -6
- package/dist/llm/session-factory.d.ts.map +1 -1
- package/dist/llm/session-factory.js +6 -72
- package/dist/llm/session-factory.js.map +1 -1
- package/dist/llm/system-resolver.d.ts.map +1 -1
- package/dist/llm/system-resolver.js +6 -0
- package/dist/llm/system-resolver.js.map +1 -1
- package/dist/llm/tool-loop.d.ts.map +1 -1
- package/dist/llm/tool-loop.js +9 -32
- package/dist/llm/tool-loop.js.map +1 -1
- package/dist/llm/transports/index.d.ts +5 -3
- package/dist/llm/transports/index.d.ts.map +1 -1
- package/dist/llm/transports/index.js +5 -2
- package/dist/llm/transports/index.js.map +1 -1
- package/dist/reconciliation/reconciliation-engine.d.ts.map +1 -1
- package/dist/reconciliation/reconciliation-engine.js +3 -0
- package/dist/reconciliation/reconciliation-engine.js.map +1 -1
- package/dist/release/plan.d.ts +27 -0
- package/dist/release/plan.d.ts.map +1 -1
- package/dist/release/plan.js +36 -2
- package/dist/release/plan.js.map +1 -1
- package/dist/release/provenance-fk.d.ts +74 -0
- package/dist/release/provenance-fk.d.ts.map +1 -0
- package/dist/release/provenance-fk.js +122 -0
- package/dist/release/provenance-fk.js.map +1 -0
- package/dist/release/reconcile.d.ts +10 -0
- package/dist/release/reconcile.d.ts.map +1 -1
- package/dist/release/reconcile.js +107 -2
- package/dist/release/reconcile.js.map +1 -1
- package/dist/sticky/convert.d.ts.map +1 -1
- package/dist/sticky/convert.js +3 -0
- package/dist/sticky/convert.js.map +1 -1
- package/dist/store/exodus/column-transforms.d.ts +275 -0
- package/dist/store/exodus/column-transforms.d.ts.map +1 -0
- package/dist/store/exodus/column-transforms.js +478 -0
- package/dist/store/exodus/column-transforms.js.map +1 -0
- package/dist/store/exodus/count-parity.d.ts +71 -0
- package/dist/store/exodus/count-parity.d.ts.map +1 -0
- package/dist/store/exodus/count-parity.js +124 -0
- package/dist/store/exodus/count-parity.js.map +1 -0
- package/dist/store/exodus/health.d.ts +70 -0
- package/dist/store/exodus/health.d.ts.map +1 -0
- package/dist/store/exodus/health.js +130 -0
- package/dist/store/exodus/health.js.map +1 -0
- package/dist/store/exodus/index.d.ts +3 -0
- package/dist/store/exodus/index.d.ts.map +1 -1
- package/dist/store/exodus/index.js +3 -0
- package/dist/store/exodus/index.js.map +1 -1
- package/dist/store/exodus/migrate.d.ts.map +1 -1
- package/dist/store/exodus/migrate.js +103 -298
- package/dist/store/exodus/migrate.js.map +1 -1
- package/dist/store/exodus/plan.d.ts +48 -4
- package/dist/store/exodus/plan.d.ts.map +1 -1
- package/dist/store/exodus/plan.js +67 -9
- package/dist/store/exodus/plan.js.map +1 -1
- package/dist/store/exodus/seal.d.ts +69 -0
- package/dist/store/exodus/seal.d.ts.map +1 -0
- package/dist/store/exodus/seal.js +73 -0
- package/dist/store/exodus/seal.js.map +1 -0
- package/dist/store/exodus/types.d.ts +24 -1
- package/dist/store/exodus/types.d.ts.map +1 -1
- package/dist/store/exodus/types.js.map +1 -1
- package/dist/store/exodus/verify-migration.d.ts.map +1 -1
- package/dist/store/exodus/verify-migration.js +109 -24
- package/dist/store/exodus/verify-migration.js.map +1 -1
- package/dist/tasks/add.d.ts +13 -0
- package/dist/tasks/add.d.ts.map +1 -1
- package/dist/tasks/add.js +50 -18
- package/dist/tasks/add.js.map +1 -1
- package/dist/tasks/archive.d.ts.map +1 -1
- package/dist/tasks/archive.js +12 -7
- package/dist/tasks/archive.js.map +1 -1
- package/dist/tasks/child-disposition.d.ts +66 -0
- package/dist/tasks/child-disposition.d.ts.map +1 -0
- package/dist/tasks/child-disposition.js +80 -0
- package/dist/tasks/child-disposition.js.map +1 -0
- package/dist/tasks/delete-preview.js +1 -1
- package/dist/tasks/delete-preview.js.map +1 -1
- package/dist/tasks/deletion-strategy.d.ts +21 -3
- package/dist/tasks/deletion-strategy.d.ts.map +1 -1
- package/dist/tasks/deletion-strategy.js +61 -15
- package/dist/tasks/deletion-strategy.js.map +1 -1
- package/dist/tasks/engine-wrap.d.ts +8 -0
- package/dist/tasks/engine-wrap.d.ts.map +1 -1
- package/dist/tasks/engine-wrap.js +22 -9
- package/dist/tasks/engine-wrap.js.map +1 -1
- package/dist/tasks/update.d.ts.map +1 -1
- package/dist/tasks/update.js +12 -0
- package/dist/tasks/update.js.map +1 -1
- package/package.json +12 -12
- package/dist/llm/transports/openai.d.ts +0 -181
- package/dist/llm/transports/openai.d.ts.map +0 -1
- package/dist/llm/transports/openai.js +0 -645
- package/dist/llm/transports/openai.js.map +0 -1
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ModelRunner` — the single SSoT factory for LLM transports + language models.
|
|
3
|
+
*
|
|
4
|
+
* ## Why this exists (E9 · T11745 step 1 · T11761)
|
|
5
|
+
*
|
|
6
|
+
* CLEO previously constructed transports/SDK clients in FOUR places, each a
|
|
7
|
+
* near-duplicate of the others:
|
|
8
|
+
*
|
|
9
|
+
* - `session-factory.transportForProvider` (the canonical one)
|
|
10
|
+
* - `api.ts:_transportForConfig`
|
|
11
|
+
* - `tool-loop.ts:_transportForProvider`
|
|
12
|
+
* - the inline codex block in `role-executor.ts` (`:241`)
|
|
13
|
+
*
|
|
14
|
+
* plus the Vercel-AI-SDK `LanguageModel` construction duplicated in
|
|
15
|
+
* `memory/llm-backend-resolver.ts` and the provider adapters.
|
|
16
|
+
*
|
|
17
|
+
* `ModelRunner.build(descriptor)` is the ONE place `new *Transport` /
|
|
18
|
+
* `createAnthropic` / `createOpenAICompatible` may appear for the resolver
|
|
19
|
+
* path. It consumes a {@link ResolvedLLMDescriptor} (carrying `apiMode`,
|
|
20
|
+
* `baseUrl`, `authType`) and returns BOTH surfaces off the same sealed
|
|
21
|
+
* credential:
|
|
22
|
+
*
|
|
23
|
+
* - `session` — a transport-backed {@link LlmSession} for the
|
|
24
|
+
* cantbook/CLI/executor path (streaming, tool loops).
|
|
25
|
+
* - `languageModel` — a Vercel AI SDK {@link LanguageModel} for the
|
|
26
|
+
* sentient/adapters path (`generateText`/`generateObject`),
|
|
27
|
+
* or `null` when the provider has no Vercel binding here.
|
|
28
|
+
*
|
|
29
|
+
* Because the descriptor carries `apiMode`, codex "just works": a
|
|
30
|
+
* `codex_responses` descriptor builds a {@link CodexResponsesTransport} with
|
|
31
|
+
* the OAuth Cloudflare headers — no inline branch at the call-site.
|
|
32
|
+
*
|
|
33
|
+
* @module llm/model-runner
|
|
34
|
+
* @task T11745
|
|
35
|
+
* @task T11761
|
|
36
|
+
* @epic T11745
|
|
37
|
+
*/
|
|
38
|
+
import { getLogger } from '../logger.js';
|
|
39
|
+
import { deriveApiWire } from './api-mode.js';
|
|
40
|
+
import { ConcreteSession } from './concrete-session.js';
|
|
41
|
+
import { getKimiCodeMshHeaders } from './provider-registry/builtin/kimi-code.js';
|
|
42
|
+
import { AnthropicTransport } from './transports/anthropic.js';
|
|
43
|
+
import { BedrockTransport } from './transports/bedrock.js';
|
|
44
|
+
import { ChatCompletionsTransport } from './transports/chat-completions.js';
|
|
45
|
+
import { buildCodexOAuthHeaders } from './transports/codex-oauth-headers.js';
|
|
46
|
+
import { CodexResponsesTransport } from './transports/codex-responses.js';
|
|
47
|
+
import { GeminiTransport } from './transports/gemini.js';
|
|
48
|
+
import { OllamaTransport } from './transports/ollama.js';
|
|
49
|
+
const logger = getLogger('llm-model-runner');
|
|
50
|
+
/** Kimi Code chat endpoint — speaks the Anthropic Messages protocol. */
|
|
51
|
+
const KIMI_CODE_BASE_URL = 'https://api.kimi.com/coding';
|
|
52
|
+
/**
|
|
53
|
+
* Anthropic OAuth opt-in header. A `sk-ant-oat-` / `sk-ant-ort-` OAuth token is
|
|
54
|
+
* REJECTED by the Anthropic API unless the request carries
|
|
55
|
+
* `anthropic-beta: oauth-2025-04-20`. Centralising it here — the single SSoT
|
|
56
|
+
* transport factory — means EVERY resolver-path caller (session-factory, api,
|
|
57
|
+
* tool-loop, role-executor, auxiliary-fallback) inherits it automatically,
|
|
58
|
+
* instead of each call-site re-declaring it (role-executor hardcoded it; the
|
|
59
|
+
* session-factory / auxiliary-fallback paths OMITTED it → latent OAuth 401s).
|
|
60
|
+
* The SDK auto-sets `anthropic-version`, so only the beta opt-in is needed here.
|
|
61
|
+
*
|
|
62
|
+
* @task T11745
|
|
63
|
+
*/
|
|
64
|
+
const ANTHROPIC_OAUTH_HEADERS = {
|
|
65
|
+
'anthropic-beta': 'oauth-2025-04-20',
|
|
66
|
+
};
|
|
67
|
+
/** `codex_responses` — OpenAI Responses API (ChatGPT backend / xAI grok-via-responses). */
|
|
68
|
+
function buildCodexResponsesTransport(provider, credential) {
|
|
69
|
+
// OAuth tokens authenticate against the ChatGPT backend with the Cloudflare-
|
|
70
|
+
// bypass headers; an api_key codex credential carries only its own headers.
|
|
71
|
+
const defaultHeaders = credential.authType === 'oauth'
|
|
72
|
+
? { ...credential.extraHeaders, ...buildCodexOAuthHeaders(credential.token) }
|
|
73
|
+
: { ...credential.extraHeaders };
|
|
74
|
+
return new CodexResponsesTransport({
|
|
75
|
+
apiKey: credential.token,
|
|
76
|
+
baseUrl: credential.baseUrl ?? undefined,
|
|
77
|
+
defaultHeaders: Object.keys(defaultHeaders).length ? defaultHeaders : undefined,
|
|
78
|
+
provider,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/** `anthropic_messages` — native Anthropic SDK (+ the kimi-code coding endpoint quirk). */
|
|
82
|
+
function buildAnthropicMessagesTransport(provider, credential) {
|
|
83
|
+
if (provider === 'kimi-code') {
|
|
84
|
+
// Kimi Code speaks the Anthropic Messages protocol against its own endpoint
|
|
85
|
+
// (same protocol, different baseUrl + mandatory X-Msh headers — data, not a
|
|
86
|
+
// separate transport class).
|
|
87
|
+
return new AnthropicTransport({
|
|
88
|
+
authToken: credential.token,
|
|
89
|
+
baseUrl: credential.baseUrl ?? KIMI_CODE_BASE_URL,
|
|
90
|
+
defaultHeaders: getKimiCodeMshHeaders(),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
// Mirrors the canonical `session-factory.transportForProvider`: OAuth →
|
|
94
|
+
// authToken slot (+ the required beta header); api_key → apiKey slot.
|
|
95
|
+
const opts = credential.authType === 'oauth'
|
|
96
|
+
? {
|
|
97
|
+
authToken: credential.token,
|
|
98
|
+
baseUrl: credential.baseUrl ?? undefined,
|
|
99
|
+
defaultHeaders: { ...ANTHROPIC_OAUTH_HEADERS, ...credential.extraHeaders },
|
|
100
|
+
}
|
|
101
|
+
: {
|
|
102
|
+
apiKey: credential.token,
|
|
103
|
+
baseUrl: credential.baseUrl ?? undefined,
|
|
104
|
+
defaultHeaders: Object.keys(credential.extraHeaders).length
|
|
105
|
+
? credential.extraHeaders
|
|
106
|
+
: undefined,
|
|
107
|
+
};
|
|
108
|
+
return new AnthropicTransport(opts);
|
|
109
|
+
}
|
|
110
|
+
/** `bedrock_converse` — AWS Bedrock Converse API (SDK-credentialed, no token). */
|
|
111
|
+
function buildBedrockConverseTransport(_provider, credential) {
|
|
112
|
+
return new BedrockTransport({ awsProfile: credential.awsProfile ?? undefined });
|
|
113
|
+
}
|
|
114
|
+
/** `ollama_native` — Ollama `/api/chat` NDJSON-streaming protocol. */
|
|
115
|
+
function buildOllamaNativeTransport(_provider, credential) {
|
|
116
|
+
return new OllamaTransport({
|
|
117
|
+
baseUrl: credential.baseUrl ?? undefined,
|
|
118
|
+
apiKey: credential.token || undefined,
|
|
119
|
+
defaultHeaders: Object.keys(credential.extraHeaders).length
|
|
120
|
+
? credential.extraHeaders
|
|
121
|
+
: undefined,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/** `chat_completions` — OpenAI-compatible (openai/openrouter/deepseek/xai/groq/moonshot + gemini's compat shim). */
|
|
125
|
+
function buildChatCompletionsTransport(provider, credential) {
|
|
126
|
+
if (provider === 'gemini') {
|
|
127
|
+
// Gemini speaks the OpenAI-compatible shape against its own `/openai`
|
|
128
|
+
// endpoint via the dedicated GeminiTransport (extra-body thinking config).
|
|
129
|
+
return new GeminiTransport({
|
|
130
|
+
apiKey: credential.token,
|
|
131
|
+
baseUrl: credential.baseUrl ?? undefined,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
const defaultHeaders = { ...credential.extraHeaders };
|
|
135
|
+
if (credential.authType === 'oauth') {
|
|
136
|
+
defaultHeaders['Authorization'] = `Bearer ${credential.token}`;
|
|
137
|
+
}
|
|
138
|
+
return new ChatCompletionsTransport({
|
|
139
|
+
apiKey: credential.token,
|
|
140
|
+
baseUrl: credential.baseUrl ?? undefined,
|
|
141
|
+
defaultHeaders: Object.keys(defaultHeaders).length ? defaultHeaders : undefined,
|
|
142
|
+
provider,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The single SSoT adapter table: `ApiMode → TransportAdapter`. Construction of
|
|
147
|
+
* any provider's transport flows through this table keyed by the descriptor's
|
|
148
|
+
* resolved `apiMode` (T11767 AC1) — the previous per-provider if/else in
|
|
149
|
+
* {@link ModelRunner.buildTransportFromCredential} is gone.
|
|
150
|
+
*
|
|
151
|
+
* @task T11767
|
|
152
|
+
*/
|
|
153
|
+
const TRANSPORT_ADAPTERS = {
|
|
154
|
+
codex_responses: buildCodexResponsesTransport,
|
|
155
|
+
anthropic_messages: buildAnthropicMessagesTransport,
|
|
156
|
+
bedrock_converse: buildBedrockConverseTransport,
|
|
157
|
+
ollama_native: buildOllamaNativeTransport,
|
|
158
|
+
chat_completions: buildChatCompletionsTransport,
|
|
159
|
+
};
|
|
160
|
+
/** Ollama's OpenAI-compatible `/v1` shim base URL (Vercel `LanguageModel` path). */
|
|
161
|
+
const OLLAMA_OPENAI_COMPAT_BASE_URL = 'http://localhost:11434/v1';
|
|
162
|
+
/**
|
|
163
|
+
* Convert the descriptor's wire credential into a {@link ResolvedCredential}
|
|
164
|
+
* ready for a transport constructor.
|
|
165
|
+
*
|
|
166
|
+
* The descriptor carries the partial wire credential (`provider`, `apiKey`,
|
|
167
|
+
* `authType`); the transport needs the fuller {@link ResolvedCredential}. Safe
|
|
168
|
+
* defaults are supplied for fields the descriptor does not track (`refreshToken`,
|
|
169
|
+
* `extraHeaders`, `expiresAt`, `awsProfile`). The descriptor's top-level
|
|
170
|
+
* `baseUrl` wins so codex/ollama endpoints flow through.
|
|
171
|
+
*/
|
|
172
|
+
function descriptorToCredential(d) {
|
|
173
|
+
const authType = d.authType === 'oauth' ? 'oauth' : d.authType === 'aws_sdk' ? 'aws_sdk' : 'api_key';
|
|
174
|
+
return {
|
|
175
|
+
provider: d.provider,
|
|
176
|
+
label: d.credentialLabel ?? 'default',
|
|
177
|
+
token: d.credential?.apiKey ?? '',
|
|
178
|
+
authType,
|
|
179
|
+
expiresAt: null,
|
|
180
|
+
refreshToken: null,
|
|
181
|
+
extraHeaders: {},
|
|
182
|
+
baseUrl: d.baseUrl ?? null,
|
|
183
|
+
awsProfile: null,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The single SSoT model factory (E9 · T11745).
|
|
188
|
+
*
|
|
189
|
+
* Stateless: every method takes a fully-resolved {@link ResolvedLLMDescriptor}
|
|
190
|
+
* and constructs fresh transports / language models. No credential I/O, no
|
|
191
|
+
* resolution — that happens upstream in `resolveLLMForRole` /
|
|
192
|
+
* `resolveLLMForSystem`.
|
|
193
|
+
*/
|
|
194
|
+
export const ModelRunner = {
|
|
195
|
+
/**
|
|
196
|
+
* Construct the transport for a descriptor. This is the ONLY home for
|
|
197
|
+
* `new *Transport` on the resolver path — the three legacy transport
|
|
198
|
+
* factories delegate here (via {@link ModelRunner.buildTransportFromCredential}).
|
|
199
|
+
*
|
|
200
|
+
* Branches on `apiMode` (the descriptor's load-bearing field), falling back
|
|
201
|
+
* to provider name for the anthropic/gemini/bedrock/ollama families.
|
|
202
|
+
*
|
|
203
|
+
* @param d - Fully-resolved descriptor.
|
|
204
|
+
* @returns A wire-level transport bound to the descriptor's credential.
|
|
205
|
+
*/
|
|
206
|
+
buildTransport(d) {
|
|
207
|
+
return this.buildTransportFromCredential(d.provider, descriptorToCredential(d), d.apiMode);
|
|
208
|
+
},
|
|
209
|
+
/**
|
|
210
|
+
* Lower-level transport factory: the single home for `new *Transport`.
|
|
211
|
+
*
|
|
212
|
+
* Accepts a fully-formed {@link ResolvedCredential} (so callers that already
|
|
213
|
+
* hold one — `session-factory`, `api.ts`, `tool-loop.ts` — preserve their
|
|
214
|
+
* `extraHeaders`/`baseUrl` verbatim) plus an optional explicit `apiMode`.
|
|
215
|
+
* When `apiMode === 'codex_responses'` it builds the codex transport
|
|
216
|
+
* regardless of provider name (xAI grok-via-responses, openai-oauth).
|
|
217
|
+
*
|
|
218
|
+
* @param provider - Resolved provider transport.
|
|
219
|
+
* @param credential - Fully-resolved credential to wire in.
|
|
220
|
+
* @param apiMode - Optional wire-protocol override (codex routing).
|
|
221
|
+
* @returns A wire-level transport.
|
|
222
|
+
*/
|
|
223
|
+
buildTransportFromCredential(provider, credential, apiMode) {
|
|
224
|
+
// Construction is DATA-driven (T11767): the resolved `apiMode` selects the
|
|
225
|
+
// adapter from {@link TRANSPORT_ADAPTERS}. An explicit apiMode (e.g.
|
|
226
|
+
// `codex_responses` for an openai-oauth ChatGPT token, stamped by the
|
|
227
|
+
// resolver) wins; otherwise the provider's STATIC default mode is used —
|
|
228
|
+
// `deriveApiWire(provider, null)` intentionally omits the auth-derived
|
|
229
|
+
// openai→codex routing so a no-apiMode caller keeps an api_key/oauth openai
|
|
230
|
+
// on `chat_completions` exactly as before (the codex route requires an
|
|
231
|
+
// explicit apiMode from the resolver).
|
|
232
|
+
const mode = apiMode ?? deriveApiWire(provider, null).apiMode;
|
|
233
|
+
return TRANSPORT_ADAPTERS[mode](provider, credential);
|
|
234
|
+
},
|
|
235
|
+
/**
|
|
236
|
+
* Construct a Vercel AI SDK {@link LanguageModel} for the sentient/adapters
|
|
237
|
+
* path, or `null` when core has no Vercel binding for the descriptor's
|
|
238
|
+
* provider/apiMode.
|
|
239
|
+
*
|
|
240
|
+
* - `anthropic_messages` → `createAnthropic`
|
|
241
|
+
* - `chat_completions` / `ollama_native` → `createOpenAICompatible`
|
|
242
|
+
* - everything else (codex_responses, bedrock_converse) → `null`
|
|
243
|
+
*
|
|
244
|
+
* Returns `null` (rather than throwing) on any binding failure so the caller
|
|
245
|
+
* degrades to the transport path. Async because the AI-SDK provider modules
|
|
246
|
+
* are dynamically imported (keeps them out of the module-init chain).
|
|
247
|
+
*
|
|
248
|
+
* @param d - Fully-resolved descriptor.
|
|
249
|
+
* @returns A Vercel `LanguageModel`, or `null` when unsupported here.
|
|
250
|
+
*/
|
|
251
|
+
async buildLanguageModel(d) {
|
|
252
|
+
const apiKey = d.credential?.apiKey ?? null;
|
|
253
|
+
try {
|
|
254
|
+
if (d.apiMode === 'anthropic_messages' && d.provider === 'anthropic') {
|
|
255
|
+
if (!apiKey)
|
|
256
|
+
return null;
|
|
257
|
+
const { createAnthropic } = await import('@ai-sdk/anthropic');
|
|
258
|
+
return createAnthropic({ apiKey })(d.model);
|
|
259
|
+
}
|
|
260
|
+
if (d.apiMode === 'chat_completions' || d.apiMode === 'ollama_native') {
|
|
261
|
+
// The Vercel openai-compatible provider REQUIRES a base URL. Use the
|
|
262
|
+
// descriptor's override, the ollama localhost shim, or the canonical
|
|
263
|
+
// OpenAI endpoint; otherwise the caller falls back to the transport.
|
|
264
|
+
const baseURL = d.baseUrl ??
|
|
265
|
+
(d.provider === 'ollama'
|
|
266
|
+
? OLLAMA_OPENAI_COMPAT_BASE_URL
|
|
267
|
+
: d.provider === 'openai'
|
|
268
|
+
? 'https://api.openai.com/v1'
|
|
269
|
+
: null);
|
|
270
|
+
if (!baseURL)
|
|
271
|
+
return null;
|
|
272
|
+
// ollama needs no key; remote openai-compatible servers do.
|
|
273
|
+
if (!apiKey && d.provider !== 'ollama')
|
|
274
|
+
return null;
|
|
275
|
+
const { createOpenAICompatible } = await import('@ai-sdk/openai-compatible');
|
|
276
|
+
const provider = createOpenAICompatible({
|
|
277
|
+
name: d.provider,
|
|
278
|
+
baseURL,
|
|
279
|
+
...(apiKey ? { apiKey } : {}),
|
|
280
|
+
});
|
|
281
|
+
return provider(d.model);
|
|
282
|
+
}
|
|
283
|
+
// codex_responses / bedrock_converse have no core Vercel binding — the
|
|
284
|
+
// caller uses the transport session instead.
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
catch (err) {
|
|
288
|
+
logger.warn({ err: err instanceof Error ? err.message : String(err), provider: d.provider }, 'model-runner: languageModel construction failed; returning null (caller falls back to transport)');
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
/**
|
|
293
|
+
* Build BOTH surfaces (transport session + Vercel language model) from one
|
|
294
|
+
* descriptor. The transport session is always present; `languageModel` is
|
|
295
|
+
* `null` for providers without a core Vercel binding.
|
|
296
|
+
*
|
|
297
|
+
* @param d - Fully-resolved descriptor.
|
|
298
|
+
* @returns The {@link BuiltModel} pair.
|
|
299
|
+
*/
|
|
300
|
+
async build(d) {
|
|
301
|
+
const transport = this.buildTransport(d);
|
|
302
|
+
const credential = descriptorToCredential(d);
|
|
303
|
+
const session = new ConcreteSession({
|
|
304
|
+
transport,
|
|
305
|
+
model: d.model,
|
|
306
|
+
credential,
|
|
307
|
+
});
|
|
308
|
+
const languageModel = await this.buildLanguageModel(d);
|
|
309
|
+
return { session, languageModel };
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
//# sourceMappingURL=model-runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-runner.js","sourceRoot":"","sources":["../../src/llm/model-runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAOH,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,wBAAwB,EAAE,MAAM,kCAAkC,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAGzD,MAAM,MAAM,GAAG,SAAS,CAAC,kBAAkB,CAAC,CAAC;AAE7C,wEAAwE;AACxE,MAAM,kBAAkB,GAAG,6BAA6B,CAAC;AAEzD;;;;;;;;;;;GAWG;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