minicode-ai 0.11.1 → 0.13.0
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/CHANGELOG.md +89 -0
- package/LICENSE +21 -21
- package/README.md +4 -4
- package/cli/args.ts +46 -0
- package/cli/commands/acp.ts +163 -10
- package/cli/commands/config.ts +33 -18
- package/cli/commands/daemon.ts +515 -0
- package/cli/commands/exec.ts +145 -53
- package/cli/commands/openwork.ts +986 -0
- package/cli/commands/sessions.ts +4 -1
- package/cli/commands/stats.ts +34 -4
- package/cli/commands.ts +217 -2
- package/cli/index.ts +146 -14
- package/cli/recovery-warning.ts +115 -0
- package/cli/router.ts +32 -1
- package/cli/setup.ts +2332 -171
- package/cli/tui.ts +113 -7
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +489 -0
- package/docs/OUTPUT_EVENT_MODEL.md +289 -0
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +391 -0
- package/docs/OUTPUT_PROTOCOL_SPEC.md +285 -0
- package/docs/OUTPUT_RENDERING_SPEC.md +248 -0
- package/docs/OUTPUT_UX_RULES.md +449 -0
- package/docs/PLAN_UIUX_V6.md +2 -2
- package/docs/README.md +4 -3
- package/docs/TERMINAL_CONTRACT.md +278 -71
- package/docs/architecture.md +89 -7
- package/docs/changelog.md +1 -1
- package/docs/choosing-mode.md +2 -2
- package/docs/cli.md +9 -5
- package/docs/concepts.md +1 -1
- package/docs/contributing.md +9 -2
- package/docs/environment.md +6 -2
- package/docs/exec.md +46 -5
- package/docs/memory-sessions.md +2 -0
- package/docs/policy-sandbox.md +1 -1
- package/docs/pricing-budget.md +1 -1
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +6 -2
- package/docs/security-model.md +1 -1
- package/docs/security.md +1 -1
- package/docs/skills.md +2 -2
- package/docs/terminal.md +9 -5
- package/docs/tools.md +1 -1
- package/package.json +35 -5
- package/src/app/session.ts +261 -15
- package/src/app/tool-layer.ts +94 -7
- package/src/daemon/approvals.ts +361 -0
- package/src/daemon/capability.ts +145 -0
- package/src/daemon/children.ts +142 -0
- package/src/daemon/client.ts +267 -0
- package/src/daemon/control.ts +350 -0
- package/src/daemon/discovery.ts +234 -0
- package/src/daemon/feed.ts +155 -0
- package/src/daemon/framing.ts +94 -0
- package/src/daemon/host.ts +316 -0
- package/src/daemon/protocol.ts +217 -0
- package/src/daemon/resource.ts +170 -0
- package/src/daemon/sanitize.ts +57 -0
- package/src/daemon/server.ts +627 -0
- package/src/daemon/store.ts +317 -0
- package/src/daemon/subscription.ts +270 -0
- package/src/daemon/telemetry.ts +81 -0
- package/src/hooks/run.ts +12 -1
- package/src/lib/atomic-write.ts +29 -3
- package/src/lib/db-path.ts +26 -0
- package/src/lib/keystore.ts +544 -42
- package/src/lib/net.ts +43 -1
- package/src/lib/safe-open.ts +116 -1
- package/src/lib/session-id.ts +15 -0
- package/src/lsp/client.ts +123 -5
- package/src/mcp/client.ts +139 -17
- package/src/mcp/server.ts +177 -50
- package/src/mcp/transport.ts +16 -19
- package/src/memory/vector.ts +770 -55
- package/src/policy/allowlist.ts +14 -2
- package/src/policy/bash-guard.ts +105 -10
- package/src/policy/compaction.ts +82 -5
- package/src/policy/consequence-engine.ts +220 -0
- package/src/policy/context.ts +132 -0
- package/src/policy/executor.ts +187 -121
- package/src/policy/no-progress.ts +559 -0
- package/src/policy/permission.ts +4 -8
- package/src/policy/pricing.ts +17 -3
- package/src/policy/scrub.ts +69 -2
- package/src/policy/tool-contract.ts +515 -0
- package/src/policy/usage.ts +310 -24
- package/src/policy/verifier.ts +6 -3
- package/src/policy/work-mode.ts +182 -0
- package/src/presentation/adapter.ts +533 -26
- package/src/presentation/events.ts +151 -0
- package/src/presentation/model.ts +180 -27
- package/src/presentation/projection.ts +858 -0
- package/src/presentation/reducer.ts +645 -310
- package/src/presentation/store.ts +12 -7
- package/src/providers/anthropic.ts +18 -10
- package/src/providers/responses.ts +2 -2
- package/src/runtime/capability.ts +409 -0
- package/src/runtime/child-execution.ts +274 -0
- package/src/runtime/composition.ts +958 -0
- package/src/runtime/dispatch.ts +591 -0
- package/src/runtime/execution-backend.ts +632 -0
- package/src/runtime/execution-events.ts +272 -0
- package/src/runtime/execution-id.ts +230 -0
- package/src/runtime/execution-journal.ts +926 -0
- package/src/runtime/execution-kernel.ts +456 -0
- package/src/runtime/execution-registry.ts +379 -0
- package/src/runtime/process-supervision.ts +276 -0
- package/src/runtime/production-execution.ts +551 -0
- package/src/runtime/production-runtime.ts +217 -0
- package/src/runtime/recovery-safety.ts +70 -0
- package/src/runtime/recovery.ts +477 -0
- package/src/runtime/runtime-capabilities.ts +179 -0
- package/src/runtime/runtime-diagnostics.ts +141 -0
- package/src/runtime/runtime-host.ts +365 -0
- package/src/runtime/runtime-status-facts.ts +222 -0
- package/src/runtime/runtime-status-provider.ts +74 -0
- package/src/runtime/runtime-status.ts +459 -0
- package/src/runtime/runtime-task-admission.ts +101 -0
- package/src/runtime/supervisor.ts +444 -0
- package/src/session/authority.ts +168 -0
- package/src/session/checkpoint.ts +88 -1
- package/src/session/context-adapter.ts +251 -0
- package/src/session/context-assembly.ts +242 -0
- package/src/session/context-identity.ts +389 -0
- package/src/session/context-projection.ts +248 -0
- package/src/session/context-selector.ts +500 -0
- package/src/session/cross-session-adoption.ts +249 -0
- package/src/session/full-history-fold.ts +171 -0
- package/src/session/identity.ts +192 -0
- package/src/session/journal.ts +805 -102
- package/src/session/open-work-intent.ts +732 -0
- package/src/session/open-work.ts +767 -0
- package/src/session/pending-tools.ts +94 -0
- package/src/session/persistence.ts +3569 -127
- package/src/session/runtime-header.ts +208 -0
- package/src/session/shadow-git.ts +13 -5
- package/src/session/verification.ts +1623 -0
- package/src/task/assignment.ts +136 -0
- package/src/task/autonomous-adapter.ts +168 -0
- package/src/task/autonomous-context.ts +630 -0
- package/src/task/autonomous-policy.ts +490 -0
- package/src/task/continuity.ts +179 -0
- package/src/task/execution-cancellation.ts +165 -0
- package/src/task/graph-validate.ts +379 -0
- package/src/task/graph.ts +305 -0
- package/src/task/identity.ts +230 -0
- package/src/task/model.ts +417 -0
- package/src/task/production-scheduler.ts +293 -0
- package/src/task/readiness.ts +229 -0
- package/src/task/scheduler-observability.ts +387 -0
- package/src/task/scheduler.ts +1197 -0
- package/src/task/scheduling-policy.ts +165 -0
- package/src/task/session-authority.ts +148 -0
- package/src/task/session-ownership.ts +211 -0
- package/src/task/store.ts +2887 -0
- package/src/task/sync.ts +343 -0
- package/src/task/trigger.ts +260 -0
- package/src/tools/bash.ts +377 -211
- package/src/tools/code_run.ts +1 -1
- package/src/tools/delete_file.ts +1 -1
- package/src/tools/edit.ts +33 -6
- package/src/tools/evidence.ts +30 -0
- package/src/tools/git.ts +4 -4
- package/src/tools/glob.ts +12 -3
- package/src/tools/grep.ts +1 -1
- package/src/tools/lsp.ts +4 -4
- package/src/tools/mcp_call.ts +11 -4
- package/src/tools/memory.ts +153 -18
- package/src/tools/move_file.ts +1 -1
- package/src/tools/patch.ts +1 -1
- package/src/tools/read_file.ts +1 -1
- package/src/tools/read_image.ts +1 -1
- package/src/tools/submit_result.ts +1 -1
- package/src/tools/task.ts +437 -41
- package/src/tools/todo.ts +662 -35
- package/src/tools/web_fetch.ts +4 -2
- package/src/tools/web_search.ts +9 -3
- package/src/tools/write_file.ts +1 -1
- package/src/ui/assistant/simple.ts +123 -50
- package/src/ui/assistant/turn-status.ts +17 -3
- package/src/ui/contract.ts +184 -0
- package/src/ui/footer.ts +52 -19
- package/src/ui/i18n/en.ts +58 -6
- package/src/ui/i18n/id.ts +58 -6
- package/src/ui/input/prompt-engine.ts +120 -25
- package/src/ui/render/errors.ts +39 -1
- package/src/ui/render/markdown-table.ts +455 -0
- package/src/ui/render/markdown.ts +22 -1
- package/src/ui/render/table-grid.ts +184 -0
- package/src/ui/render/table.ts +15 -76
- package/src/ui/runtime/screen.ts +7 -2
- package/src/ui/runtime/statusline.ts +25 -6
- package/src/ui/tui/app.ts +507 -169
- package/src/ui/tui/transcript.ts +531 -89
- package/vendor/minicore/VENDOR.md +1 -1
- package/vendor/minicore/src/core/budget.ts +25 -25
- package/vendor/minicore/src/core/compact.ts +75 -75
- package/vendor/minicore/src/core/errors.ts +59 -59
- package/vendor/minicore/src/core/events.ts +73 -73
- package/vendor/minicore/src/core/executor.ts +139 -139
- package/vendor/minicore/src/core/history.ts +31 -31
- package/vendor/minicore/src/core/index.ts +41 -41
- package/vendor/minicore/src/core/loop.ts +355 -355
- package/vendor/minicore/src/core/permission.ts +22 -22
- package/vendor/minicore/src/core/provider.ts +39 -39
- package/vendor/minicore/src/core/recovery.ts +35 -35
- package/vendor/minicore/src/core/session.ts +327 -327
- package/vendor/minicore/src/core/snapshot.ts +146 -146
- package/vendor/minicore/src/core/tokens.ts +107 -107
- package/vendor/minicore/src/core/tool.ts +115 -115
- package/vendor/minicore/src/core/types.ts +50 -50
- package/vendor/minicore/src/providers/openai-compat.ts +417 -417
- package/AGENTS.md +0 -45
- package/PLAN.md +0 -491
- package/docs/ARCHITECTURE.html +0 -1248
- package/docs/CONTROL-PLANE-MAP.md +0 -139
- package/docs/HARNESS.md +0 -150
- package/docs/SUMMARY.md +0 -52
- package/docs/UI_RENDER_PIPELINE.md +0 -169
- package/docs/USAGE.md +0 -11
|
@@ -1,417 +1,417 @@
|
|
|
1
|
-
// OpenAI-compatible provider adapter. Covers OpenAI, OpenRouter, Ollama,
|
|
2
|
-
// DeepSeek, vLLM, and any other /chat/completions endpoint. Lives OUTSIDE the
|
|
3
|
-
// kernel; implements the ModelProvider contract.
|
|
4
|
-
|
|
5
|
-
import { ProviderError } from "../core/errors.ts";
|
|
6
|
-
import type { ModelProvider, ProviderEvent, StreamRequest } from "../core/provider.ts";
|
|
7
|
-
import type { ToolSchema } from "../core/tool.ts";
|
|
8
|
-
import type { Content, Message } from "../core/types.ts";
|
|
9
|
-
|
|
10
|
-
// Provider-metadata side-map: toolCallId -> extra provider payload (e.g.
|
|
11
|
-
// Gemini thought_signature / extra_content). It must NOT travel inside
|
|
12
|
-
// `args` — the executor rejects unknown properties (all schemas are
|
|
13
|
-
// additionalProperties:false), so smuggling it there fails validation.
|
|
14
|
-
// Side-map keeps args clean; consume-once + cap so long sessions can't leak.
|
|
15
|
-
// The map lives per provider INSTANCE (inside createOpenAICompatProvider):
|
|
16
|
-
// a module-level map would be shared across concurrent sessions, and
|
|
17
|
-
// index-based fallback ids (`call_0`) could then echo one session's metadata
|
|
18
|
-
// into another session's history replay.
|
|
19
|
-
|
|
20
|
-
/** Sniff a well-known image format from magic bytes; undefined when unknown. */
|
|
21
|
-
function sniffImageMime(bytes: Uint8Array): string | undefined {
|
|
22
|
-
if (bytes.length >= 4 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) return "image/png";
|
|
23
|
-
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return "image/jpeg";
|
|
24
|
-
if (bytes.length >= 4 && bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38) return "image/gif";
|
|
25
|
-
if (bytes.length >= 12 && bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 && bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50) return "image/webp";
|
|
26
|
-
if (bytes.length >= 2 && bytes[0] === 0x42 && bytes[1] === 0x4d) return "image/bmp";
|
|
27
|
-
return undefined;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Configuration for an OpenAI-compatible chat completions endpoint. */
|
|
31
|
-
export interface OpenAICompatConfig {
|
|
32
|
-
/** Adapter id reported on the ModelProvider. Defaults to "openai-compat". */
|
|
33
|
-
id?: string;
|
|
34
|
-
/** Base URL of the API, e.g. "https://api.openai.com/v1". Trailing slashes are trimmed. */
|
|
35
|
-
baseUrl: string;
|
|
36
|
-
/** Models this endpoint serves (used by the runtime for routing). */
|
|
37
|
-
models: readonly string[];
|
|
38
|
-
/** Model used when a request carries none. */
|
|
39
|
-
defaultModel?: string;
|
|
40
|
-
/** Bearer token; omitted entirely when absent (e.g. local endpoints). */
|
|
41
|
-
apiKey?: string;
|
|
42
|
-
/** Extra headers; these override the default content-type/authorization. */
|
|
43
|
-
headers?: Readonly<Record<string, string>>;
|
|
44
|
-
/**
|
|
45
|
-
* Send stream_options.include_usage (P4). Some strict OpenAI-compat
|
|
46
|
-
* endpoints reject the unknown field with 400; when enabled the adapter
|
|
47
|
-
* retries once without it. Defaults to true.
|
|
48
|
-
*/
|
|
49
|
-
includeUsage?: boolean;
|
|
50
|
-
/** Generic reasoning effort knob — mapped per-wire (openai reasoning_effort, anthropic thinking). */
|
|
51
|
-
reasoningEffort?: string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Build a ModelProvider for any OpenAI-compatible /chat/completions endpoint.
|
|
56
|
-
* Wire errors are normalized to ProviderError categories; network-level
|
|
57
|
-
* failures (DNS, connection refused) surface as `ProviderError("network")`;
|
|
58
|
-
* user aborts propagate as-is so the kernel can map them to `aborted`.
|
|
59
|
-
*/
|
|
60
|
-
export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelProvider {
|
|
61
|
-
const baseUrl = config.baseUrl.replace(/\/+$/, "");
|
|
62
|
-
const endpoint = `${baseUrl}/chat/completions`;
|
|
63
|
-
const headers: Record<string, string> = {
|
|
64
|
-
"content-type": "application/json",
|
|
65
|
-
"accept": "text/event-stream",
|
|
66
|
-
...(config.apiKey ? { authorization: `Bearer ${config.apiKey}` } : {}),
|
|
67
|
-
...config.headers,
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
// Per-instance provider-metadata side-map (see comment at top of file).
|
|
71
|
-
const providerMetaByCallId = new Map<string, unknown>();
|
|
72
|
-
function stashProviderMeta(id: string, meta: unknown): void {
|
|
73
|
-
if (!id || meta == null) return;
|
|
74
|
-
providerMetaByCallId.set(id, meta);
|
|
75
|
-
// Cap: drop the oldest entry when a very long session piles up.
|
|
76
|
-
if (providerMetaByCallId.size > 500) {
|
|
77
|
-
const first = providerMetaByCallId.keys().next();
|
|
78
|
-
if (!first.done) providerMetaByCallId.delete(first.value);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
function takeProviderMeta(id: string): unknown {
|
|
82
|
-
const v = providerMetaByCallId.get(id);
|
|
83
|
-
if (v != null) providerMetaByCallId.delete(id);
|
|
84
|
-
return v;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
id: config.id ?? "openai-compat",
|
|
89
|
-
models: config.models,
|
|
90
|
-
async *stream(request: StreamRequest, signal: AbortSignal): AsyncIterable<ProviderEvent> {
|
|
91
|
-
const includeUsage = config.includeUsage ?? true;
|
|
92
|
-
const buildBody = (withUsage: boolean): string =>
|
|
93
|
-
JSON.stringify({
|
|
94
|
-
model: request.model ?? config.defaultModel,
|
|
95
|
-
messages: toMessages(request.messages, takeProviderMeta),
|
|
96
|
-
tools: request.tools?.length ? toTools(request.tools) : undefined,
|
|
97
|
-
stream: true,
|
|
98
|
-
// JSON.stringify drops undefined keys, so the field is omitted entirely.
|
|
99
|
-
stream_options: withUsage ? { include_usage: true } : undefined,
|
|
100
|
-
// DeepSeek-style thinking: provider seperti b.ai butuh reasoning_content
|
|
101
|
-
// di history saat thinking aktif. OFF via env agar multi-turn aman.
|
|
102
|
-
// MINICORE_THINKING adalah nama resmi; MINICODE_THINKING (brand lama)
|
|
103
|
-
// dipertahankan sebagai fallback kompatibilitas.
|
|
104
|
-
...((process.env.MINICORE_THINKING ?? process.env.MINICODE_THINKING) === "off" ? { enable_thinking: false } : {}),
|
|
105
|
-
...(config.reasoningEffort ? { reasoning_effort: config.reasoningEffort } : {}),
|
|
106
|
-
});
|
|
107
|
-
let body = buildBody(includeUsage);
|
|
108
|
-
let response = await fetchOrThrow(endpoint, headers, body, signal);
|
|
109
|
-
// P4: strict OpenAI-compat endpoints reject the unknown stream_options
|
|
110
|
-
// field with 400. Retry once without it; a second 400 is a real error.
|
|
111
|
-
if (!response.ok && includeUsage && response.status === 400) {
|
|
112
|
-
body = buildBody(false);
|
|
113
|
-
response = await fetchOrThrow(endpoint, headers, body, signal);
|
|
114
|
-
}
|
|
115
|
-
if (!response.ok) {
|
|
116
|
-
const text = await response.text().catch(() => "");
|
|
117
|
-
throw toProviderError(response.status, text, response.headers);
|
|
118
|
-
}
|
|
119
|
-
if (!response.body) throw new ProviderError("network", "empty response body");
|
|
120
|
-
|
|
121
|
-
const calls = new Map<number, { id: string; name: string; args: string }>();
|
|
122
|
-
const order: number[] = [];
|
|
123
|
-
// A non-conforming provider may repeat finish_reason across chunks; emit
|
|
124
|
-
// the accumulated tool calls and the finish event only once, otherwise
|
|
125
|
-
// the kernel would see (and execute) the same tool call twice.
|
|
126
|
-
let finished = false;
|
|
127
|
-
for await (const chunk of sse(response.body, signal)) {
|
|
128
|
-
const usage = chunk.usage as { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number } | undefined;
|
|
129
|
-
// P8 + race: beberapa provider (b.ai dll) mengirim `usage` pada chunk
|
|
130
|
-
// TERAKHIR setelah finish_reason. Jangan di-drop karena `finished`.
|
|
131
|
-
if (usage && (usage.prompt_tokens != null || usage.completion_tokens != null || usage.total_tokens != null)) {
|
|
132
|
-
yield {
|
|
133
|
-
type: "extension",
|
|
134
|
-
kind: "usage",
|
|
135
|
-
data: { inputTokens: usage.prompt_tokens, outputTokens: usage.completion_tokens, totalTokens: usage.total_tokens },
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
if (finished) {
|
|
139
|
-
const postTool = (chunk as { choices?: Array<{ delta?: Record<string, unknown> }> }).choices?.[0]?.delta?.tool_calls;
|
|
140
|
-
if (Array.isArray(postTool)) throw new ProviderError("network", "tool deltas after finish_reason");
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
const choices = chunk.choices as
|
|
144
|
-
| Array<{ delta?: Record<string, unknown>; finish_reason?: string | null }>
|
|
145
|
-
| undefined;
|
|
146
|
-
const first = choices?.[0];
|
|
147
|
-
if (!first) continue;
|
|
148
|
-
const delta = first.delta ?? {};
|
|
149
|
-
// Beberapa provider (mis. b.ai/Gemini) mengirim chunk berisi content
|
|
150
|
-
// SETELAH finish_reason — jangan di-forward (kernel tolak event setelah finish).
|
|
151
|
-
const content = delta.content;
|
|
152
|
-
if (!finished && typeof content === "string" && content.length > 0) yield { type: "text", text: content };
|
|
153
|
-
// Reasoning is surfaced as an extension event, never a kernel
|
|
154
|
-
// primitive. Handles DeepSeek-style delta.reasoning_content (string or
|
|
155
|
-
// content-part array) and OpenAI o-series delta.reasoning (string).
|
|
156
|
-
const rawReasoning = (delta as { reasoning_content?: unknown; reasoning?: unknown }).reasoning_content ??
|
|
157
|
-
(delta as { reasoning?: unknown }).reasoning;
|
|
158
|
-
let reasoningText: string | undefined;
|
|
159
|
-
if (typeof rawReasoning === "string") reasoningText = rawReasoning;
|
|
160
|
-
else if (Array.isArray(rawReasoning)) {
|
|
161
|
-
reasoningText = (rawReasoning as Array<{ type?: string; text?: unknown }>)
|
|
162
|
-
.filter((part) => part?.type === "text" && typeof part.text === "string")
|
|
163
|
-
.map((part) => part.text as string)
|
|
164
|
-
.join("");
|
|
165
|
-
}
|
|
166
|
-
if (!finished && reasoningText && reasoningText.length > 0) {
|
|
167
|
-
yield { type: "extension", kind: "reasoning", data: { text: reasoningText } };
|
|
168
|
-
}
|
|
169
|
-
const toolDeltas = delta.tool_calls;
|
|
170
|
-
if (Array.isArray(toolDeltas)) {
|
|
171
|
-
// A conforming provider sends every tool delta before finish_reason.
|
|
172
|
-
// Deltas that arrive after `finished` would be accumulated but never
|
|
173
|
-
// emitted, silently turning a broken stream into an empty success;
|
|
174
|
-
// fail loud as a retryable network error instead.
|
|
175
|
-
if (finished) {
|
|
176
|
-
throw new ProviderError("network", "tool deltas after finish_reason");
|
|
177
|
-
}
|
|
178
|
-
for (const td of toolDeltas as Array<Record<string, unknown>>) {
|
|
179
|
-
const index = Number(td.index ?? 0);
|
|
180
|
-
const acc = calls.get(index) ?? { id: "", name: "", args: "" };
|
|
181
|
-
calls.set(index, acc);
|
|
182
|
-
if (order.indexOf(index) < 0) order.push(index);
|
|
183
|
-
if (typeof td.id === "string" && td.id) acc.id = td.id;
|
|
184
|
-
// Preserve provider metadata (thought_signature / extra_content)
|
|
185
|
-
// verbatim in the side-map — never in args — for echo on
|
|
186
|
-
// history replay in toMessages.
|
|
187
|
-
const extra =
|
|
188
|
-
(td as Record<string, unknown>).extra_content ??
|
|
189
|
-
(td as Record<string, unknown>).thought_signature ??
|
|
190
|
-
(td as Record<string, unknown>).providerMeta;
|
|
191
|
-
if (extra != null) (acc as Record<string, unknown>)._extra = extra;
|
|
192
|
-
const fnExtra =
|
|
193
|
-
(td.function as Record<string, unknown> | undefined)?.extra_content ??
|
|
194
|
-
(td.function as Record<string, unknown> | undefined)?.thought_signature;
|
|
195
|
-
if (fnExtra != null) (acc as Record<string, unknown>)._extra = fnExtra;
|
|
196
|
-
const fn = td.function as { name?: unknown; arguments?: unknown } | undefined;
|
|
197
|
-
if (fn) {
|
|
198
|
-
// A tool name is never split across deltas (only `arguments` is),
|
|
199
|
-
// and some providers repeat the full name on every chunk. Take the
|
|
200
|
-
// first non-empty name instead of concatenating, which used to
|
|
201
|
-
// turn a repeated name into "echoecho" (unknown tool error).
|
|
202
|
-
if (typeof fn.name === "string" && fn.name) acc.name = acc.name || fn.name;
|
|
203
|
-
if (typeof fn.arguments === "string") acc.args += fn.arguments;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (!finished && first.finish_reason) {
|
|
208
|
-
finished = true;
|
|
209
|
-
for (const index of order) {
|
|
210
|
-
const acc = calls.get(index) as unknown as { id: string; name: string; args: string; _extra?: unknown };
|
|
211
|
-
if (!acc) continue;
|
|
212
|
-
let args: unknown = acc.args;
|
|
213
|
-
if (acc.args) {
|
|
214
|
-
try {
|
|
215
|
-
args = JSON.parse(acc.args);
|
|
216
|
-
} catch {
|
|
217
|
-
args = { raw: acc.args };
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
// Args stay clean (the executor rejects unknown properties);
|
|
221
|
-
// provider metadata is filed in the side-map by call id.
|
|
222
|
-
const callId = acc.id || `call_${index}`
|
|
223
|
-
if (acc._extra != null) stashProviderMeta(callId, acc._extra);
|
|
224
|
-
yield { type: "tool_call", id: callId, name: acc.name, args };
|
|
225
|
-
}
|
|
226
|
-
// P6: content_filter (OpenAI safety filter) must not be invisible as
|
|
227
|
-
// a plain "stop" — surface it as an extension event so observers can
|
|
228
|
-
// distinguish a blocked response from a normal completion.
|
|
229
|
-
if (first.finish_reason === "content_filter") {
|
|
230
|
-
yield { type: "extension", kind: "content_filter", data: {} };
|
|
231
|
-
}
|
|
232
|
-
yield { type: "finish", reason: mapFinishReason(first.finish_reason) };
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
// Provider quirky bisa berakhir tanpa finish_reason (mis. [DONE] langsung).
|
|
236
|
-
// Emit finish("stop") agar kernel tidak menganggap stream terputus.
|
|
237
|
-
if (!finished) {
|
|
238
|
-
yield { type: "finish", reason: "stop" };
|
|
239
|
-
}
|
|
240
|
-
},
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
function toMessages(messages: readonly Message[], takeMeta: (id: string) => unknown): unknown[] {
|
|
245
|
-
return messages.map((message) => {
|
|
246
|
-
switch (message.role) {
|
|
247
|
-
case "user":
|
|
248
|
-
return { role: "user", content: toContent(message.content) };
|
|
249
|
-
case "assistant":
|
|
250
|
-
return {
|
|
251
|
-
role: "assistant",
|
|
252
|
-
content: toContent(message.content),
|
|
253
|
-
// DeepSeek-style thinking: provider butuh reasoning_content di history
|
|
254
|
-
// saat thinking mode aktif, agar multi-turn tidak 400.
|
|
255
|
-
...((message as { reasoning?: string }).reasoning ? { reasoning_content: (message as { reasoning?: string }).reasoning } : {}),
|
|
256
|
-
tool_calls: message.toolCalls?.length
|
|
257
|
-
? message.toolCalls.map((call) => {
|
|
258
|
-
// Echo provider metadata from the side-map (consume-once);
|
|
259
|
-
// args are sent exactly as received, with no hidden keys.
|
|
260
|
-
const extra = takeMeta(call.id)
|
|
261
|
-
return {
|
|
262
|
-
id: call.id,
|
|
263
|
-
type: "function",
|
|
264
|
-
function: { name: call.name, arguments: JSON.stringify(call.args ?? {}) },
|
|
265
|
-
...(extra != null ? { extra_content: extra } : {}),
|
|
266
|
-
}
|
|
267
|
-
})
|
|
268
|
-
: undefined,
|
|
269
|
-
};
|
|
270
|
-
case "tool": {
|
|
271
|
-
const content = message.content;
|
|
272
|
-
// Binary tool results must never reach the wire as a JSON index map
|
|
273
|
-
// ({"0":1,...}): sniffed images travel as an image_url data-URL part;
|
|
274
|
-
// any other binary becomes an explicit placeholder string.
|
|
275
|
-
if (content instanceof Uint8Array) {
|
|
276
|
-
const mime = sniffImageMime(content);
|
|
277
|
-
const wire = mime
|
|
278
|
-
? [{ type: "image_url", image_url: { url: `data:${mime};base64,${bytesToBase64(content)}` } }]
|
|
279
|
-
: `[binary: ${content.byteLength} bytes]`;
|
|
280
|
-
return { role: "tool", tool_call_id: message.toolCallId, content: wire };
|
|
281
|
-
}
|
|
282
|
-
return {
|
|
283
|
-
role: "tool",
|
|
284
|
-
tool_call_id: message.toolCallId,
|
|
285
|
-
content: typeof content === "string" ? content : JSON.stringify(content),
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function toContent(content: Content): unknown {
|
|
293
|
-
if (typeof content === "string") return content;
|
|
294
|
-
const parts: unknown[] = [];
|
|
295
|
-
for (const part of content) {
|
|
296
|
-
if (part.type === "text") parts.push({ type: "text", text: part.text });
|
|
297
|
-
else parts.push({ type: "image_url", image_url: { url: `data:${part.mime};base64,${bytesToBase64(part.data)}` } });
|
|
298
|
-
}
|
|
299
|
-
return parts;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
function bytesToBase64(bytes: Uint8Array): string {
|
|
303
|
-
// btoa rejects inputs beyond ~0x80000000 bytes in some engines, and one giant
|
|
304
|
-
// string concat is wasteful for multi-MB images. Chunk the input. The chunk
|
|
305
|
-
// size is a multiple of 3 so every chunk ends on a base64 group boundary:
|
|
306
|
-
// concatenating the raw outputs reproduces the whole-buffer encoding exactly
|
|
307
|
-
// (only the final chunk carries '=' padding, which is then the correct
|
|
308
|
-
// padding for the whole buffer). A non-multiple-of-3 chunk size breaks the
|
|
309
|
-
// alignment and produces invalid base64 — see the chunked-base64 test.
|
|
310
|
-
let out = "";
|
|
311
|
-
const chunk = 0x7fe0; // 32736 = 10912 × 3
|
|
312
|
-
for (let i = 0; i < bytes.length; i += chunk) {
|
|
313
|
-
let part = "";
|
|
314
|
-
const end = Math.min(i + chunk, bytes.length);
|
|
315
|
-
for (let j = i; j < end; j++) part += String.fromCharCode(bytes[j]!);
|
|
316
|
-
out += btoa(part);
|
|
317
|
-
}
|
|
318
|
-
return out;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function toTools(tools: readonly ToolSchema[]): unknown[] {
|
|
322
|
-
return tools.map((tool) => ({
|
|
323
|
-
type: "function",
|
|
324
|
-
function: {
|
|
325
|
-
name: tool.name,
|
|
326
|
-
description: tool.description,
|
|
327
|
-
parameters: tool.parameters,
|
|
328
|
-
},
|
|
329
|
-
}));
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
function mapFinishReason(reason: string): "stop" | "tool_calls" | "length" {
|
|
333
|
-
if (reason === "tool_calls" || reason === "function_call") return "tool_calls";
|
|
334
|
-
if (reason === "length") return "length";
|
|
335
|
-
return "stop";
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
async function fetchOrThrow(url: string, headers: Record<string, string>, body: string, signal: AbortSignal): Promise<Response> {
|
|
339
|
-
// Network-level failures (DNS, refused, dropped) must surface as a
|
|
340
|
-
// retryable ProviderError("network"), not a raw TypeError that the kernel
|
|
341
|
-
// would later guess at as "unknown". A user abort is not a network failure:
|
|
342
|
-
// propagate the AbortError as-is so the kernel maps it to AgentError aborted.
|
|
343
|
-
try {
|
|
344
|
-
return await fetch(url, { method: "POST", headers, body, signal });
|
|
345
|
-
} catch (error) {
|
|
346
|
-
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
347
|
-
throw new ProviderError("network", error instanceof Error ? error.message : String(error));
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
async function* sse(body: ReadableStream<Uint8Array>, signal: AbortSignal): AsyncGenerator<Record<string, unknown>> {
|
|
352
|
-
const reader = body.getReader();
|
|
353
|
-
const decoder = new TextDecoder();
|
|
354
|
-
let buffer = "";
|
|
355
|
-
try {
|
|
356
|
-
while (true) {
|
|
357
|
-
if (signal.aborted) throw new Error("AbortError");
|
|
358
|
-
const { done, value } = await reader.read();
|
|
359
|
-
if (done) break;
|
|
360
|
-
buffer += decoder.decode(value, { stream: true });
|
|
361
|
-
let index: number;
|
|
362
|
-
while ((index = buffer.indexOf("\n")) >= 0) {
|
|
363
|
-
const line = buffer.slice(0, index).replace(/\r$/, "");
|
|
364
|
-
buffer = buffer.slice(index + 1);
|
|
365
|
-
if (!line.startsWith("data:")) continue;
|
|
366
|
-
const payload = line.slice(5).trim();
|
|
367
|
-
if (payload === "[DONE]") return;
|
|
368
|
-
if (payload) {
|
|
369
|
-
try {
|
|
370
|
-
yield JSON.parse(payload) as Record<string, unknown>;
|
|
371
|
-
} catch {
|
|
372
|
-
// Skip malformed SSE payloads; the next chunk may complete them.
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
} finally {
|
|
378
|
-
reader.releaseLock();
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
function toProviderError(status: number, body: string, headers: Headers): ProviderError {
|
|
383
|
-
const detail = body.slice(0, 500);
|
|
384
|
-
const retryAfterMs = parseRetryAfter(headers.get("retry-after"));
|
|
385
|
-
if (status === 429) {
|
|
386
|
-
return new ProviderError("rate_limit", `rate limited (${status}): ${detail}`, retryAfterMs);
|
|
387
|
-
}
|
|
388
|
-
if (status === 401 || status === 403) return new ProviderError("auth", `auth failed (${status}): ${detail}`);
|
|
389
|
-
if (status === 400 || status === 404 || status === 422) {
|
|
390
|
-
// Providers phrase context overflow differently: OpenAI "maximum context
|
|
391
|
-
// length", OpenRouter-style "context_length_exceeded", Anthropic "prompt
|
|
392
|
-
// is too long", Gemini-style "input is too long". Missing a phrase turns
|
|
393
|
-
// a compact-and-retry case into a terminal invalid_request throw.
|
|
394
|
-
const lower = body.toLowerCase();
|
|
395
|
-
const isContext = ["context_length", "context length", "maximum context", "prompt is too long", "input is too long"].some((phrase) =>
|
|
396
|
-
lower.includes(phrase),
|
|
397
|
-
);
|
|
398
|
-
return new ProviderError(isContext ? "context_length_exceeded" : "invalid_request", `${status}: ${detail}`);
|
|
399
|
-
}
|
|
400
|
-
if (status >= 500) return new ProviderError("server", `server error (${status}): ${detail}`);
|
|
401
|
-
return new ProviderError("unknown", `${status}: ${detail}`);
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
/**
|
|
405
|
-
* Parse a `Retry-After` header in either RFC form — delta-seconds ("120")
|
|
406
|
-
* or HTTP-date ("Wed, 21 Oct 2026 07:28:00 GMT") — into milliseconds from
|
|
407
|
-
* now. Returns undefined when absent or unparsable; a past date yields 0.
|
|
408
|
-
*/
|
|
409
|
-
function parseRetryAfter(value: string | null): number | undefined {
|
|
410
|
-
if (!value) return undefined;
|
|
411
|
-
const trimmed = value.trim();
|
|
412
|
-
if (trimmed === "") return undefined;
|
|
413
|
-
if (/^\d+(\.\d+)?$/.test(trimmed)) return Math.round(Number(trimmed) * 1_000);
|
|
414
|
-
const date = Date.parse(trimmed);
|
|
415
|
-
if (Number.isNaN(date)) return undefined;
|
|
416
|
-
return Math.max(0, date - Date.now());
|
|
417
|
-
}
|
|
1
|
+
// OpenAI-compatible provider adapter. Covers OpenAI, OpenRouter, Ollama,
|
|
2
|
+
// DeepSeek, vLLM, and any other /chat/completions endpoint. Lives OUTSIDE the
|
|
3
|
+
// kernel; implements the ModelProvider contract.
|
|
4
|
+
|
|
5
|
+
import { ProviderError } from "../core/errors.ts";
|
|
6
|
+
import type { ModelProvider, ProviderEvent, StreamRequest } from "../core/provider.ts";
|
|
7
|
+
import type { ToolSchema } from "../core/tool.ts";
|
|
8
|
+
import type { Content, Message } from "../core/types.ts";
|
|
9
|
+
|
|
10
|
+
// Provider-metadata side-map: toolCallId -> extra provider payload (e.g.
|
|
11
|
+
// Gemini thought_signature / extra_content). It must NOT travel inside
|
|
12
|
+
// `args` — the executor rejects unknown properties (all schemas are
|
|
13
|
+
// additionalProperties:false), so smuggling it there fails validation.
|
|
14
|
+
// Side-map keeps args clean; consume-once + cap so long sessions can't leak.
|
|
15
|
+
// The map lives per provider INSTANCE (inside createOpenAICompatProvider):
|
|
16
|
+
// a module-level map would be shared across concurrent sessions, and
|
|
17
|
+
// index-based fallback ids (`call_0`) could then echo one session's metadata
|
|
18
|
+
// into another session's history replay.
|
|
19
|
+
|
|
20
|
+
/** Sniff a well-known image format from magic bytes; undefined when unknown. */
|
|
21
|
+
function sniffImageMime(bytes: Uint8Array): string | undefined {
|
|
22
|
+
if (bytes.length >= 4 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) return "image/png";
|
|
23
|
+
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return "image/jpeg";
|
|
24
|
+
if (bytes.length >= 4 && bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38) return "image/gif";
|
|
25
|
+
if (bytes.length >= 12 && bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 && bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50) return "image/webp";
|
|
26
|
+
if (bytes.length >= 2 && bytes[0] === 0x42 && bytes[1] === 0x4d) return "image/bmp";
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Configuration for an OpenAI-compatible chat completions endpoint. */
|
|
31
|
+
export interface OpenAICompatConfig {
|
|
32
|
+
/** Adapter id reported on the ModelProvider. Defaults to "openai-compat". */
|
|
33
|
+
id?: string;
|
|
34
|
+
/** Base URL of the API, e.g. "https://api.openai.com/v1". Trailing slashes are trimmed. */
|
|
35
|
+
baseUrl: string;
|
|
36
|
+
/** Models this endpoint serves (used by the runtime for routing). */
|
|
37
|
+
models: readonly string[];
|
|
38
|
+
/** Model used when a request carries none. */
|
|
39
|
+
defaultModel?: string;
|
|
40
|
+
/** Bearer token; omitted entirely when absent (e.g. local endpoints). */
|
|
41
|
+
apiKey?: string;
|
|
42
|
+
/** Extra headers; these override the default content-type/authorization. */
|
|
43
|
+
headers?: Readonly<Record<string, string>>;
|
|
44
|
+
/**
|
|
45
|
+
* Send stream_options.include_usage (P4). Some strict OpenAI-compat
|
|
46
|
+
* endpoints reject the unknown field with 400; when enabled the adapter
|
|
47
|
+
* retries once without it. Defaults to true.
|
|
48
|
+
*/
|
|
49
|
+
includeUsage?: boolean;
|
|
50
|
+
/** Generic reasoning effort knob — mapped per-wire (openai reasoning_effort, anthropic thinking). */
|
|
51
|
+
reasoningEffort?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Build a ModelProvider for any OpenAI-compatible /chat/completions endpoint.
|
|
56
|
+
* Wire errors are normalized to ProviderError categories; network-level
|
|
57
|
+
* failures (DNS, connection refused) surface as `ProviderError("network")`;
|
|
58
|
+
* user aborts propagate as-is so the kernel can map them to `aborted`.
|
|
59
|
+
*/
|
|
60
|
+
export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelProvider {
|
|
61
|
+
const baseUrl = config.baseUrl.replace(/\/+$/, "");
|
|
62
|
+
const endpoint = `${baseUrl}/chat/completions`;
|
|
63
|
+
const headers: Record<string, string> = {
|
|
64
|
+
"content-type": "application/json",
|
|
65
|
+
"accept": "text/event-stream",
|
|
66
|
+
...(config.apiKey ? { authorization: `Bearer ${config.apiKey}` } : {}),
|
|
67
|
+
...config.headers,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// Per-instance provider-metadata side-map (see comment at top of file).
|
|
71
|
+
const providerMetaByCallId = new Map<string, unknown>();
|
|
72
|
+
function stashProviderMeta(id: string, meta: unknown): void {
|
|
73
|
+
if (!id || meta == null) return;
|
|
74
|
+
providerMetaByCallId.set(id, meta);
|
|
75
|
+
// Cap: drop the oldest entry when a very long session piles up.
|
|
76
|
+
if (providerMetaByCallId.size > 500) {
|
|
77
|
+
const first = providerMetaByCallId.keys().next();
|
|
78
|
+
if (!first.done) providerMetaByCallId.delete(first.value);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function takeProviderMeta(id: string): unknown {
|
|
82
|
+
const v = providerMetaByCallId.get(id);
|
|
83
|
+
if (v != null) providerMetaByCallId.delete(id);
|
|
84
|
+
return v;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
id: config.id ?? "openai-compat",
|
|
89
|
+
models: config.models,
|
|
90
|
+
async *stream(request: StreamRequest, signal: AbortSignal): AsyncIterable<ProviderEvent> {
|
|
91
|
+
const includeUsage = config.includeUsage ?? true;
|
|
92
|
+
const buildBody = (withUsage: boolean): string =>
|
|
93
|
+
JSON.stringify({
|
|
94
|
+
model: request.model ?? config.defaultModel,
|
|
95
|
+
messages: toMessages(request.messages, takeProviderMeta),
|
|
96
|
+
tools: request.tools?.length ? toTools(request.tools) : undefined,
|
|
97
|
+
stream: true,
|
|
98
|
+
// JSON.stringify drops undefined keys, so the field is omitted entirely.
|
|
99
|
+
stream_options: withUsage ? { include_usage: true } : undefined,
|
|
100
|
+
// DeepSeek-style thinking: provider seperti b.ai butuh reasoning_content
|
|
101
|
+
// di history saat thinking aktif. OFF via env agar multi-turn aman.
|
|
102
|
+
// MINICORE_THINKING adalah nama resmi; MINICODE_THINKING (brand lama)
|
|
103
|
+
// dipertahankan sebagai fallback kompatibilitas.
|
|
104
|
+
...((process.env.MINICORE_THINKING ?? process.env.MINICODE_THINKING) === "off" ? { enable_thinking: false } : {}),
|
|
105
|
+
...(config.reasoningEffort ? { reasoning_effort: config.reasoningEffort } : {}),
|
|
106
|
+
});
|
|
107
|
+
let body = buildBody(includeUsage);
|
|
108
|
+
let response = await fetchOrThrow(endpoint, headers, body, signal);
|
|
109
|
+
// P4: strict OpenAI-compat endpoints reject the unknown stream_options
|
|
110
|
+
// field with 400. Retry once without it; a second 400 is a real error.
|
|
111
|
+
if (!response.ok && includeUsage && response.status === 400) {
|
|
112
|
+
body = buildBody(false);
|
|
113
|
+
response = await fetchOrThrow(endpoint, headers, body, signal);
|
|
114
|
+
}
|
|
115
|
+
if (!response.ok) {
|
|
116
|
+
const text = await response.text().catch(() => "");
|
|
117
|
+
throw toProviderError(response.status, text, response.headers);
|
|
118
|
+
}
|
|
119
|
+
if (!response.body) throw new ProviderError("network", "empty response body");
|
|
120
|
+
|
|
121
|
+
const calls = new Map<number, { id: string; name: string; args: string }>();
|
|
122
|
+
const order: number[] = [];
|
|
123
|
+
// A non-conforming provider may repeat finish_reason across chunks; emit
|
|
124
|
+
// the accumulated tool calls and the finish event only once, otherwise
|
|
125
|
+
// the kernel would see (and execute) the same tool call twice.
|
|
126
|
+
let finished = false;
|
|
127
|
+
for await (const chunk of sse(response.body, signal)) {
|
|
128
|
+
const usage = chunk.usage as { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number } | undefined;
|
|
129
|
+
// P8 + race: beberapa provider (b.ai dll) mengirim `usage` pada chunk
|
|
130
|
+
// TERAKHIR setelah finish_reason. Jangan di-drop karena `finished`.
|
|
131
|
+
if (usage && (usage.prompt_tokens != null || usage.completion_tokens != null || usage.total_tokens != null)) {
|
|
132
|
+
yield {
|
|
133
|
+
type: "extension",
|
|
134
|
+
kind: "usage",
|
|
135
|
+
data: { inputTokens: usage.prompt_tokens, outputTokens: usage.completion_tokens, totalTokens: usage.total_tokens },
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (finished) {
|
|
139
|
+
const postTool = (chunk as { choices?: Array<{ delta?: Record<string, unknown> }> }).choices?.[0]?.delta?.tool_calls;
|
|
140
|
+
if (Array.isArray(postTool)) throw new ProviderError("network", "tool deltas after finish_reason");
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const choices = chunk.choices as
|
|
144
|
+
| Array<{ delta?: Record<string, unknown>; finish_reason?: string | null }>
|
|
145
|
+
| undefined;
|
|
146
|
+
const first = choices?.[0];
|
|
147
|
+
if (!first) continue;
|
|
148
|
+
const delta = first.delta ?? {};
|
|
149
|
+
// Beberapa provider (mis. b.ai/Gemini) mengirim chunk berisi content
|
|
150
|
+
// SETELAH finish_reason — jangan di-forward (kernel tolak event setelah finish).
|
|
151
|
+
const content = delta.content;
|
|
152
|
+
if (!finished && typeof content === "string" && content.length > 0) yield { type: "text", text: content };
|
|
153
|
+
// Reasoning is surfaced as an extension event, never a kernel
|
|
154
|
+
// primitive. Handles DeepSeek-style delta.reasoning_content (string or
|
|
155
|
+
// content-part array) and OpenAI o-series delta.reasoning (string).
|
|
156
|
+
const rawReasoning = (delta as { reasoning_content?: unknown; reasoning?: unknown }).reasoning_content ??
|
|
157
|
+
(delta as { reasoning?: unknown }).reasoning;
|
|
158
|
+
let reasoningText: string | undefined;
|
|
159
|
+
if (typeof rawReasoning === "string") reasoningText = rawReasoning;
|
|
160
|
+
else if (Array.isArray(rawReasoning)) {
|
|
161
|
+
reasoningText = (rawReasoning as Array<{ type?: string; text?: unknown }>)
|
|
162
|
+
.filter((part) => part?.type === "text" && typeof part.text === "string")
|
|
163
|
+
.map((part) => part.text as string)
|
|
164
|
+
.join("");
|
|
165
|
+
}
|
|
166
|
+
if (!finished && reasoningText && reasoningText.length > 0) {
|
|
167
|
+
yield { type: "extension", kind: "reasoning", data: { text: reasoningText } };
|
|
168
|
+
}
|
|
169
|
+
const toolDeltas = delta.tool_calls;
|
|
170
|
+
if (Array.isArray(toolDeltas)) {
|
|
171
|
+
// A conforming provider sends every tool delta before finish_reason.
|
|
172
|
+
// Deltas that arrive after `finished` would be accumulated but never
|
|
173
|
+
// emitted, silently turning a broken stream into an empty success;
|
|
174
|
+
// fail loud as a retryable network error instead.
|
|
175
|
+
if (finished) {
|
|
176
|
+
throw new ProviderError("network", "tool deltas after finish_reason");
|
|
177
|
+
}
|
|
178
|
+
for (const td of toolDeltas as Array<Record<string, unknown>>) {
|
|
179
|
+
const index = Number(td.index ?? 0);
|
|
180
|
+
const acc = calls.get(index) ?? { id: "", name: "", args: "" };
|
|
181
|
+
calls.set(index, acc);
|
|
182
|
+
if (order.indexOf(index) < 0) order.push(index);
|
|
183
|
+
if (typeof td.id === "string" && td.id) acc.id = td.id;
|
|
184
|
+
// Preserve provider metadata (thought_signature / extra_content)
|
|
185
|
+
// verbatim in the side-map — never in args — for echo on
|
|
186
|
+
// history replay in toMessages.
|
|
187
|
+
const extra =
|
|
188
|
+
(td as Record<string, unknown>).extra_content ??
|
|
189
|
+
(td as Record<string, unknown>).thought_signature ??
|
|
190
|
+
(td as Record<string, unknown>).providerMeta;
|
|
191
|
+
if (extra != null) (acc as Record<string, unknown>)._extra = extra;
|
|
192
|
+
const fnExtra =
|
|
193
|
+
(td.function as Record<string, unknown> | undefined)?.extra_content ??
|
|
194
|
+
(td.function as Record<string, unknown> | undefined)?.thought_signature;
|
|
195
|
+
if (fnExtra != null) (acc as Record<string, unknown>)._extra = fnExtra;
|
|
196
|
+
const fn = td.function as { name?: unknown; arguments?: unknown } | undefined;
|
|
197
|
+
if (fn) {
|
|
198
|
+
// A tool name is never split across deltas (only `arguments` is),
|
|
199
|
+
// and some providers repeat the full name on every chunk. Take the
|
|
200
|
+
// first non-empty name instead of concatenating, which used to
|
|
201
|
+
// turn a repeated name into "echoecho" (unknown tool error).
|
|
202
|
+
if (typeof fn.name === "string" && fn.name) acc.name = acc.name || fn.name;
|
|
203
|
+
if (typeof fn.arguments === "string") acc.args += fn.arguments;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (!finished && first.finish_reason) {
|
|
208
|
+
finished = true;
|
|
209
|
+
for (const index of order) {
|
|
210
|
+
const acc = calls.get(index) as unknown as { id: string; name: string; args: string; _extra?: unknown };
|
|
211
|
+
if (!acc) continue;
|
|
212
|
+
let args: unknown = acc.args;
|
|
213
|
+
if (acc.args) {
|
|
214
|
+
try {
|
|
215
|
+
args = JSON.parse(acc.args);
|
|
216
|
+
} catch {
|
|
217
|
+
args = { raw: acc.args };
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// Args stay clean (the executor rejects unknown properties);
|
|
221
|
+
// provider metadata is filed in the side-map by call id.
|
|
222
|
+
const callId = acc.id || `call_${index}`
|
|
223
|
+
if (acc._extra != null) stashProviderMeta(callId, acc._extra);
|
|
224
|
+
yield { type: "tool_call", id: callId, name: acc.name, args };
|
|
225
|
+
}
|
|
226
|
+
// P6: content_filter (OpenAI safety filter) must not be invisible as
|
|
227
|
+
// a plain "stop" — surface it as an extension event so observers can
|
|
228
|
+
// distinguish a blocked response from a normal completion.
|
|
229
|
+
if (first.finish_reason === "content_filter") {
|
|
230
|
+
yield { type: "extension", kind: "content_filter", data: {} };
|
|
231
|
+
}
|
|
232
|
+
yield { type: "finish", reason: mapFinishReason(first.finish_reason) };
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
// Provider quirky bisa berakhir tanpa finish_reason (mis. [DONE] langsung).
|
|
236
|
+
// Emit finish("stop") agar kernel tidak menganggap stream terputus.
|
|
237
|
+
if (!finished) {
|
|
238
|
+
yield { type: "finish", reason: "stop" };
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function toMessages(messages: readonly Message[], takeMeta: (id: string) => unknown): unknown[] {
|
|
245
|
+
return messages.map((message) => {
|
|
246
|
+
switch (message.role) {
|
|
247
|
+
case "user":
|
|
248
|
+
return { role: "user", content: toContent(message.content) };
|
|
249
|
+
case "assistant":
|
|
250
|
+
return {
|
|
251
|
+
role: "assistant",
|
|
252
|
+
content: toContent(message.content),
|
|
253
|
+
// DeepSeek-style thinking: provider butuh reasoning_content di history
|
|
254
|
+
// saat thinking mode aktif, agar multi-turn tidak 400.
|
|
255
|
+
...((message as { reasoning?: string }).reasoning ? { reasoning_content: (message as { reasoning?: string }).reasoning } : {}),
|
|
256
|
+
tool_calls: message.toolCalls?.length
|
|
257
|
+
? message.toolCalls.map((call) => {
|
|
258
|
+
// Echo provider metadata from the side-map (consume-once);
|
|
259
|
+
// args are sent exactly as received, with no hidden keys.
|
|
260
|
+
const extra = takeMeta(call.id)
|
|
261
|
+
return {
|
|
262
|
+
id: call.id,
|
|
263
|
+
type: "function",
|
|
264
|
+
function: { name: call.name, arguments: JSON.stringify(call.args ?? {}) },
|
|
265
|
+
...(extra != null ? { extra_content: extra } : {}),
|
|
266
|
+
}
|
|
267
|
+
})
|
|
268
|
+
: undefined,
|
|
269
|
+
};
|
|
270
|
+
case "tool": {
|
|
271
|
+
const content = message.content;
|
|
272
|
+
// Binary tool results must never reach the wire as a JSON index map
|
|
273
|
+
// ({"0":1,...}): sniffed images travel as an image_url data-URL part;
|
|
274
|
+
// any other binary becomes an explicit placeholder string.
|
|
275
|
+
if (content instanceof Uint8Array) {
|
|
276
|
+
const mime = sniffImageMime(content);
|
|
277
|
+
const wire = mime
|
|
278
|
+
? [{ type: "image_url", image_url: { url: `data:${mime};base64,${bytesToBase64(content)}` } }]
|
|
279
|
+
: `[binary: ${content.byteLength} bytes]`;
|
|
280
|
+
return { role: "tool", tool_call_id: message.toolCallId, content: wire };
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
role: "tool",
|
|
284
|
+
tool_call_id: message.toolCallId,
|
|
285
|
+
content: typeof content === "string" ? content : JSON.stringify(content),
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function toContent(content: Content): unknown {
|
|
293
|
+
if (typeof content === "string") return content;
|
|
294
|
+
const parts: unknown[] = [];
|
|
295
|
+
for (const part of content) {
|
|
296
|
+
if (part.type === "text") parts.push({ type: "text", text: part.text });
|
|
297
|
+
else parts.push({ type: "image_url", image_url: { url: `data:${part.mime};base64,${bytesToBase64(part.data)}` } });
|
|
298
|
+
}
|
|
299
|
+
return parts;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function bytesToBase64(bytes: Uint8Array): string {
|
|
303
|
+
// btoa rejects inputs beyond ~0x80000000 bytes in some engines, and one giant
|
|
304
|
+
// string concat is wasteful for multi-MB images. Chunk the input. The chunk
|
|
305
|
+
// size is a multiple of 3 so every chunk ends on a base64 group boundary:
|
|
306
|
+
// concatenating the raw outputs reproduces the whole-buffer encoding exactly
|
|
307
|
+
// (only the final chunk carries '=' padding, which is then the correct
|
|
308
|
+
// padding for the whole buffer). A non-multiple-of-3 chunk size breaks the
|
|
309
|
+
// alignment and produces invalid base64 — see the chunked-base64 test.
|
|
310
|
+
let out = "";
|
|
311
|
+
const chunk = 0x7fe0; // 32736 = 10912 × 3
|
|
312
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
313
|
+
let part = "";
|
|
314
|
+
const end = Math.min(i + chunk, bytes.length);
|
|
315
|
+
for (let j = i; j < end; j++) part += String.fromCharCode(bytes[j]!);
|
|
316
|
+
out += btoa(part);
|
|
317
|
+
}
|
|
318
|
+
return out;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function toTools(tools: readonly ToolSchema[]): unknown[] {
|
|
322
|
+
return tools.map((tool) => ({
|
|
323
|
+
type: "function",
|
|
324
|
+
function: {
|
|
325
|
+
name: tool.name,
|
|
326
|
+
description: tool.description,
|
|
327
|
+
parameters: tool.parameters,
|
|
328
|
+
},
|
|
329
|
+
}));
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function mapFinishReason(reason: string): "stop" | "tool_calls" | "length" {
|
|
333
|
+
if (reason === "tool_calls" || reason === "function_call") return "tool_calls";
|
|
334
|
+
if (reason === "length") return "length";
|
|
335
|
+
return "stop";
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async function fetchOrThrow(url: string, headers: Record<string, string>, body: string, signal: AbortSignal): Promise<Response> {
|
|
339
|
+
// Network-level failures (DNS, refused, dropped) must surface as a
|
|
340
|
+
// retryable ProviderError("network"), not a raw TypeError that the kernel
|
|
341
|
+
// would later guess at as "unknown". A user abort is not a network failure:
|
|
342
|
+
// propagate the AbortError as-is so the kernel maps it to AgentError aborted.
|
|
343
|
+
try {
|
|
344
|
+
return await fetch(url, { method: "POST", headers, body, signal });
|
|
345
|
+
} catch (error) {
|
|
346
|
+
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
347
|
+
throw new ProviderError("network", error instanceof Error ? error.message : String(error));
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async function* sse(body: ReadableStream<Uint8Array>, signal: AbortSignal): AsyncGenerator<Record<string, unknown>> {
|
|
352
|
+
const reader = body.getReader();
|
|
353
|
+
const decoder = new TextDecoder();
|
|
354
|
+
let buffer = "";
|
|
355
|
+
try {
|
|
356
|
+
while (true) {
|
|
357
|
+
if (signal.aborted) throw new Error("AbortError");
|
|
358
|
+
const { done, value } = await reader.read();
|
|
359
|
+
if (done) break;
|
|
360
|
+
buffer += decoder.decode(value, { stream: true });
|
|
361
|
+
let index: number;
|
|
362
|
+
while ((index = buffer.indexOf("\n")) >= 0) {
|
|
363
|
+
const line = buffer.slice(0, index).replace(/\r$/, "");
|
|
364
|
+
buffer = buffer.slice(index + 1);
|
|
365
|
+
if (!line.startsWith("data:")) continue;
|
|
366
|
+
const payload = line.slice(5).trim();
|
|
367
|
+
if (payload === "[DONE]") return;
|
|
368
|
+
if (payload) {
|
|
369
|
+
try {
|
|
370
|
+
yield JSON.parse(payload) as Record<string, unknown>;
|
|
371
|
+
} catch {
|
|
372
|
+
// Skip malformed SSE payloads; the next chunk may complete them.
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
} finally {
|
|
378
|
+
reader.releaseLock();
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function toProviderError(status: number, body: string, headers: Headers): ProviderError {
|
|
383
|
+
const detail = body.slice(0, 500);
|
|
384
|
+
const retryAfterMs = parseRetryAfter(headers.get("retry-after"));
|
|
385
|
+
if (status === 429) {
|
|
386
|
+
return new ProviderError("rate_limit", `rate limited (${status}): ${detail}`, retryAfterMs);
|
|
387
|
+
}
|
|
388
|
+
if (status === 401 || status === 403) return new ProviderError("auth", `auth failed (${status}): ${detail}`);
|
|
389
|
+
if (status === 400 || status === 404 || status === 422) {
|
|
390
|
+
// Providers phrase context overflow differently: OpenAI "maximum context
|
|
391
|
+
// length", OpenRouter-style "context_length_exceeded", Anthropic "prompt
|
|
392
|
+
// is too long", Gemini-style "input is too long". Missing a phrase turns
|
|
393
|
+
// a compact-and-retry case into a terminal invalid_request throw.
|
|
394
|
+
const lower = body.toLowerCase();
|
|
395
|
+
const isContext = ["context_length", "context length", "maximum context", "prompt is too long", "input is too long"].some((phrase) =>
|
|
396
|
+
lower.includes(phrase),
|
|
397
|
+
);
|
|
398
|
+
return new ProviderError(isContext ? "context_length_exceeded" : "invalid_request", `${status}: ${detail}`);
|
|
399
|
+
}
|
|
400
|
+
if (status >= 500) return new ProviderError("server", `server error (${status}): ${detail}`);
|
|
401
|
+
return new ProviderError("unknown", `${status}: ${detail}`);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Parse a `Retry-After` header in either RFC form — delta-seconds ("120")
|
|
406
|
+
* or HTTP-date ("Wed, 21 Oct 2026 07:28:00 GMT") — into milliseconds from
|
|
407
|
+
* now. Returns undefined when absent or unparsable; a past date yields 0.
|
|
408
|
+
*/
|
|
409
|
+
function parseRetryAfter(value: string | null): number | undefined {
|
|
410
|
+
if (!value) return undefined;
|
|
411
|
+
const trimmed = value.trim();
|
|
412
|
+
if (trimmed === "") return undefined;
|
|
413
|
+
if (/^\d+(\.\d+)?$/.test(trimmed)) return Math.round(Number(trimmed) * 1_000);
|
|
414
|
+
const date = Date.parse(trimmed);
|
|
415
|
+
if (Number.isNaN(date)) return undefined;
|
|
416
|
+
return Math.max(0, date - Date.now());
|
|
417
|
+
}
|