omnicell-router 0.7.0 → 0.8.2
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/backend/dist/cli/clients/activity.d.ts +35 -0
- package/backend/dist/cli/clients/activity.js +67 -0
- package/backend/dist/cli/clients/base.d.ts +13 -2
- package/backend/dist/cli/clients/claude.d.ts +2 -2
- package/backend/dist/cli/clients/codex.d.ts +2 -2
- package/backend/dist/cli/clients/core-adapters.d.ts +3 -4
- package/backend/dist/cli/clients/core-adapters.js +3 -4
- package/backend/dist/cli/clients/cursor-proxy.d.ts +88 -0
- package/backend/dist/cli/clients/cursor-proxy.js +421 -0
- package/backend/dist/cli/clients/dsh.d.ts +2 -2
- package/backend/dist/cli/clients/grok.d.ts +2 -2
- package/backend/dist/cli/clients/index.d.ts +8 -1
- package/backend/dist/cli/clients/index.js +55 -21
- package/backend/dist/cli/clients/kimi.d.ts +2 -2
- package/backend/dist/cli/clients/launcher.d.ts +8 -0
- package/backend/dist/cli/clients/model-meta.js +1 -1
- package/backend/dist/cli/clients/omp.d.ts +2 -2
- package/backend/dist/cli/clients/openclaw.d.ts +2 -2
- package/backend/dist/cli/clients/opencode-proxy.d.ts +11 -0
- package/backend/dist/cli/clients/opencode-proxy.js +351 -0
- package/backend/dist/cli/clients/opencode.d.ts +2 -2
- package/backend/dist/cli/clients/pi.d.ts +2 -2
- package/backend/dist/cli/clients/plugin-adapters.d.ts +3 -1
- package/backend/dist/cli/clients/plugin-adapters.js +6 -29
- package/backend/dist/cli/clients/qoder-proxy.d.ts +84 -0
- package/backend/dist/cli/clients/qoder-proxy.js +497 -0
- package/backend/dist/cli/clients/reserved.d.ts +2 -6
- package/backend/dist/cli/clients/reserved.js +2 -0
- package/backend/dist/cli/clients/workbuddy.d.ts +2 -2
- package/backend/dist/cli/index.js +60 -9
- package/backend/dist/cli/types.d.ts +29 -8
- package/backend/dist/compression/caveman-prompts.d.ts +1 -1
- package/backend/dist/config/comments-en.js +6 -12
- package/backend/dist/config/comments-zh.js +6 -12
- package/backend/dist/config/defaults.d.ts +56 -1
- package/backend/dist/config/defaults.js +68 -0
- package/backend/dist/config/index.d.ts +9 -2
- package/backend/dist/config/index.js +26 -3
- package/backend/dist/config/types.d.ts +55 -43
- package/backend/dist/forward-proxy/egress.d.ts +10 -2
- package/backend/dist/forward-proxy/egress.js +42 -4
- package/backend/dist/forward-proxy/ordered-relay.d.ts +29 -0
- package/backend/dist/forward-proxy/ordered-relay.js +46 -0
- package/backend/dist/forward-proxy/rules.d.ts +66 -19
- package/backend/dist/forward-proxy/rules.js +61 -14
- package/backend/dist/forward-proxy/server.js +226 -77
- package/backend/dist/forward-proxy/system-proxy.d.ts +42 -2
- package/backend/dist/forward-proxy/system-proxy.js +199 -22
- package/backend/dist/harness/common/market.d.ts +3 -3
- package/backend/dist/harness/host/adapters/amp.d.ts +1 -1
- package/backend/dist/harness/host/adapters/antigravity.d.ts +1 -1
- package/backend/dist/harness/host/adapters/claude.d.ts +1 -1
- package/backend/dist/harness/host/adapters/cline.d.ts +1 -1
- package/backend/dist/harness/host/adapters/codex.d.ts +1 -1
- package/backend/dist/harness/host/adapters/cursor.d.ts +1 -1
- package/backend/dist/harness/host/adapters/dsh.d.ts +2 -2
- package/backend/dist/harness/host/adapters/factory.d.ts +1 -1
- package/backend/dist/harness/host/adapters/gemini-cli.d.ts +1 -1
- package/backend/dist/harness/host/adapters/grok.d.ts +1 -1
- package/backend/dist/harness/host/adapters/kimi.d.ts +1 -1
- package/backend/dist/harness/host/adapters/omp.d.ts +1 -1
- package/backend/dist/harness/host/adapters/openclaw.d.ts +1 -1
- package/backend/dist/harness/host/adapters/opencode.d.ts +1 -1
- package/backend/dist/harness/host/adapters/pi.d.ts +1 -1
- package/backend/dist/harness/host/adapters/universal.d.ts +1 -1
- package/backend/dist/harness/host/adapters/windsurf.d.ts +1 -1
- package/backend/dist/harness/host/adapters/zed.d.ts +1 -1
- package/backend/dist/harness/skill/skills-sh.d.ts +1 -1
- package/backend/dist/harness/translation.js +64 -21
- package/backend/dist/harness/types.d.ts +1 -1
- package/backend/dist/harness/types.js +1 -1
- package/backend/dist/index.js +31 -18
- package/backend/dist/ir/adapters/anthropic-stream.d.ts +18 -0
- package/backend/dist/ir/adapters/anthropic-stream.js +247 -0
- package/backend/dist/ir/adapters/anthropic.d.ts +29 -0
- package/backend/dist/ir/adapters/anthropic.js +788 -0
- package/backend/dist/ir/adapters/chat.d.ts +15 -0
- package/backend/dist/ir/adapters/chat.js +602 -0
- package/backend/dist/ir/adapters/google-stream.d.ts +22 -0
- package/backend/dist/ir/adapters/google-stream.js +242 -0
- package/backend/dist/ir/adapters/google.d.ts +9 -0
- package/backend/dist/ir/adapters/google.js +317 -0
- package/backend/dist/ir/adapters/openai-responses-output.js +8 -5
- package/backend/dist/ir/adapters/openai-responses.d.ts +15 -0
- package/backend/dist/ir/adapters/openai-responses.js +26 -1
- package/backend/dist/ir/adapters/responses-output.d.ts +50 -0
- package/backend/dist/ir/adapters/responses-output.js +348 -0
- package/backend/dist/ir/adapters/responses-stream.d.ts +12 -0
- package/backend/dist/ir/adapters/responses-stream.js +178 -0
- package/backend/dist/ir/adapters/responses.d.ts +14 -0
- package/backend/dist/ir/adapters/responses.js +646 -0
- package/backend/dist/ir/errors.d.ts +2 -2
- package/backend/dist/ir/types.d.ts +2 -1
- package/backend/dist/metrics/request-cost.d.ts +11 -2
- package/backend/dist/metrics/request-cost.js +12 -3
- package/backend/dist/observability/request-identity.d.ts +1 -1
- package/backend/dist/observability/trace-id.d.ts +40 -0
- package/backend/dist/observability/trace-id.js +134 -0
- package/backend/dist/observability/upstream-events.d.ts +82 -60
- package/backend/dist/observability/upstream-events.js +142 -67
- package/backend/dist/opencode/catalog/types.d.ts +9 -0
- package/backend/dist/opencode/credentials/types.d.ts +2 -2
- package/backend/dist/opencode/probe.d.ts +24 -1
- package/backend/dist/opencode/probe.js +28 -4
- package/backend/dist/opencode/user-config.d.ts +5 -0
- package/backend/dist/pipeline/directives.d.ts +6 -2
- package/backend/dist/pipeline/directives.js +7 -3
- package/backend/dist/pipeline/orchestrator.d.ts +28 -27
- package/backend/dist/pipeline/orchestrator.js +220 -194
- package/backend/dist/pipeline/unresolved-model.d.ts +33 -0
- package/backend/dist/pipeline/unresolved-model.js +43 -0
- package/backend/dist/plugins/catalog-def.d.ts +143 -0
- package/backend/dist/plugins/catalog-def.js +177 -0
- package/backend/dist/plugins/gateways.d.ts +62 -3
- package/backend/dist/plugins/gateways.js +139 -2
- package/backend/dist/plugins/host.d.ts +42 -5
- package/backend/dist/plugins/host.js +454 -38
- package/backend/dist/plugins/manifest.d.ts +119 -10
- package/backend/dist/plugins/manifest.js +265 -14
- package/backend/dist/plugins/pack.js +26 -0
- package/backend/dist/plugins/types.d.ts +187 -21
- package/backend/dist/protocols/systemone/convert.d.ts +1 -0
- package/backend/dist/protocols/systemone/convert.js +6 -1
- package/backend/dist/protocols/systemone/decide.js +18 -8
- package/backend/dist/providers/anthropic-messages.d.ts +12 -0
- package/backend/dist/providers/anthropic-messages.js +53 -0
- package/backend/dist/providers/anthropic.d.ts +7 -8
- package/backend/dist/providers/anthropic.js +36 -324
- package/backend/dist/providers/base.d.ts +14 -2
- package/backend/dist/providers/boot-direct.js +68 -38
- package/backend/dist/providers/dispatch.d.ts +5 -19
- package/backend/dist/providers/dispatch.js +46 -58
- package/backend/dist/providers/executors/anthropic-messages.d.ts +1 -0
- package/backend/dist/providers/executors/anthropic-messages.js +3 -0
- package/backend/dist/providers/executors/dispatch.d.ts +16 -2
- package/backend/dist/providers/executors/dispatch.js +39 -9
- package/backend/dist/providers/executors/google-gemini.d.ts +1 -0
- package/backend/dist/providers/executors/google-gemini.js +3 -0
- package/backend/dist/providers/executors/http-stream.js +16 -3
- package/backend/dist/providers/executors/openai-chat.d.ts +1 -0
- package/backend/dist/providers/executors/openai-chat.js +3 -0
- package/backend/dist/providers/executors/openai-responses.d.ts +1 -0
- package/backend/dist/providers/executors/openai-responses.js +3 -0
- package/backend/dist/providers/executors/plugin-adapter.js +4 -4
- package/backend/dist/providers/google-gemini.d.ts +12 -0
- package/backend/dist/providers/google-gemini.js +50 -0
- package/backend/dist/providers/google.d.ts +7 -8
- package/backend/dist/providers/google.js +35 -308
- package/backend/dist/providers/http-json.d.ts +19 -0
- package/backend/dist/providers/http-json.js +104 -0
- package/backend/dist/providers/http-stream.d.ts +52 -0
- package/backend/dist/providers/http-stream.js +423 -0
- package/backend/dist/providers/member-match.d.ts +4 -5
- package/backend/dist/providers/member-match.js +4 -5
- package/backend/dist/providers/openai-chat.d.ts +13 -0
- package/backend/dist/providers/openai-chat.js +99 -0
- package/backend/dist/providers/openai-compatible.d.ts +7 -4
- package/backend/dist/providers/openai-compatible.js +71 -368
- package/backend/dist/providers/openai-responses.d.ts +12 -0
- package/backend/dist/providers/openai-responses.js +68 -0
- package/backend/dist/providers/opencode-proxy.d.ts +5 -5
- package/backend/dist/providers/opencode-proxy.js +49 -125
- package/backend/dist/providers/plugin-adapter.d.ts +9 -0
- package/backend/dist/providers/plugin-adapter.js +31 -0
- package/backend/dist/providers/registry.d.ts +49 -24
- package/backend/dist/providers/registry.js +97 -55
- package/backend/dist/providers/responses.d.ts +7 -28
- package/backend/dist/providers/responses.js +45 -253
- package/backend/dist/providers/tool-markup-leak.d.ts +41 -0
- package/backend/dist/providers/tool-markup-leak.js +57 -0
- package/backend/dist/providers/tool-markup-recovery.d.ts +27 -0
- package/backend/dist/providers/tool-markup-recovery.js +786 -0
- package/backend/dist/providers/virtual-meta.d.ts +21 -2
- package/backend/dist/providers/virtual-meta.js +49 -9
- package/backend/dist/proxy/asset-download.d.ts +54 -0
- package/backend/dist/proxy/asset-download.js +420 -0
- package/backend/dist/proxy/manual-node-spec.d.ts +16 -16
- package/backend/dist/proxy/mihomo-binary.d.ts +17 -2
- package/backend/dist/proxy/mihomo-binary.js +62 -51
- package/backend/dist/proxy/mihomo-client.js +15 -1
- package/backend/dist/proxy/mihomo-config.js +11 -2
- package/backend/dist/proxy/mihomo-geo.d.ts +87 -0
- package/backend/dist/proxy/mihomo-geo.js +213 -0
- package/backend/dist/proxy/mihomo-logs.d.ts +27 -0
- package/backend/dist/proxy/mihomo-logs.js +70 -0
- package/backend/dist/proxy/mihomo-pac.d.ts +11 -0
- package/backend/dist/proxy/mihomo-pac.js +15 -0
- package/backend/dist/proxy/mihomo-rules-import.d.ts +1 -1
- package/backend/dist/proxy/mihomo-rules-import.js +7 -7
- package/backend/dist/proxy/mihomo-sidecar.d.ts +17 -1
- package/backend/dist/proxy/mihomo-sidecar.js +148 -26
- package/backend/dist/quota/adapters/minimax.js +36 -13
- package/backend/dist/quota/cli-login.d.ts +8 -0
- package/backend/dist/quota/cli-login.js +18 -0
- package/backend/dist/quota/declarations.js +10 -0
- package/backend/dist/quota/dsl.js +18 -5
- package/backend/dist/quota/engine.d.ts +22 -1
- package/backend/dist/quota/engine.js +170 -29
- package/backend/dist/quota/prober.d.ts +5 -0
- package/backend/dist/quota/prober.js +11 -2
- package/backend/dist/quota/types.d.ts +87 -8
- package/backend/dist/quota/verdict.d.ts +35 -0
- package/backend/dist/quota/verdict.js +135 -0
- package/backend/dist/ratelimit/admission.d.ts +131 -0
- package/backend/dist/ratelimit/admission.js +233 -0
- package/backend/dist/ratelimit/rate-limiter.js +6 -0
- package/backend/dist/resilience/types.d.ts +2 -2
- package/backend/dist/router/layer2-decision.d.ts +1 -1
- package/backend/dist/router/layer2-decision.js +5 -8
- package/backend/dist/routes/console.js +248 -52
- package/backend/dist/routes/mihomo.d.ts +2 -0
- package/backend/dist/routes/mihomo.js +334 -29
- package/backend/dist/routes/responses.d.ts +87 -8
- package/backend/dist/routes/responses.js +314 -33
- package/backend/dist/server.js +34 -6
- package/backend/dist/session/session-manager.d.ts +6 -0
- package/backend/dist/session/session-manager.js +20 -9
- package/backend/dist/surfaces/chat-pipeline.d.ts +2 -2
- package/backend/dist/surfaces/chat-pipeline.js +17 -7
- package/backend/dist/surfaces/chat.js +15 -0
- package/backend/dist/surfaces/decision-pipeline.js +7 -10
- package/backend/dist/surfaces/direct-forward.d.ts +3 -0
- package/backend/dist/surfaces/direct-forward.js +206 -0
- package/backend/dist/surfaces/rate-limit-response.d.ts +17 -0
- package/backend/dist/surfaces/rate-limit-response.js +57 -0
- package/backend/dist/surfaces/types.d.ts +11 -0
- package/backend/dist/trace/tracker.d.ts +32 -1
- package/backend/dist/trace/tracker.js +29 -4
- package/backend/dist/types/router.d.ts +34 -12
- package/backend/dist/types/router.js +35 -6
- package/backend/dist/utils/chat-stream.d.ts +21 -8
- package/backend/dist/utils/chat-stream.js +46 -9
- package/backend/dist/utils/event-ring.d.ts +4 -0
- package/backend/dist/utils/event-ring.js +4 -0
- package/backend/dist/utils/popularity.js +29 -4
- package/backend/dist/utils/proxy.d.ts +7 -0
- package/backend/dist/utils/proxy.js +35 -10
- package/backend/dist/utils/retry-after.d.ts +21 -0
- package/backend/dist/utils/retry-after.js +36 -0
- package/bin/ocr.js +2 -2
- package/frontend/dist/assets/index-55VVwfFQ.js +1521 -0
- package/frontend/dist/assets/{index-CQSLGFTV.css → index-DxZ9MHKB.css} +1 -1
- package/frontend/dist/assets/workbuddy.color-DOMpQkxJ.svg +29 -0
- package/frontend/dist/index.html +2 -2
- package/package.json +1 -1
- package/frontend/dist/assets/index-CLvxJjp_.js +0 -1476
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ChatCompletionRequest, ChatCompletionResponse, ChatMessage, ChatMessageContentPart } from '../../types/openai.js';
|
|
2
|
+
import type { CanonicalRequest, CanonicalResponse, ContentPart, Usage } from '../types.js';
|
|
3
|
+
/** Options for the legacy execution bridge and the OpenAI-chat southbound wire. */
|
|
4
|
+
export interface ChatAdapterOptions {
|
|
5
|
+
/** Keep Anthropic/Responses-only cache markers. Generic chat wires disable this. */
|
|
6
|
+
carrierExtensions?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function partsFromChat(content: ChatMessage['content'] | null): ContentPart[];
|
|
9
|
+
export declare function chatContentFromParts(parts: ContentPart[], protocol?: string, options?: ChatAdapterOptions): string | ChatMessageContentPart[] | null;
|
|
10
|
+
export declare function chatRequestToIR(req: ChatCompletionRequest): CanonicalRequest;
|
|
11
|
+
/** IR → the legacy execution request / OpenAI-chat request. */
|
|
12
|
+
export declare function irToChatRequest(ir: CanonicalRequest, model: string, options?: ChatAdapterOptions): ChatCompletionRequest;
|
|
13
|
+
export declare function usageToIR(usage: ChatCompletionResponse['usage']): Usage;
|
|
14
|
+
export declare function chatResponseToIR(resp: ChatCompletionResponse): CanonicalResponse;
|
|
15
|
+
export declare function irToChatResponse(ir: CanonicalResponse, id: string, model: string): ChatCompletionResponse;
|
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
import { IrUnsupportedError } from '../errors.js';
|
|
2
|
+
import { DATA_URL_RE } from '../media.js';
|
|
3
|
+
import { assertCanonicalRequestSupported, assertCanonicalResponseSupported } from '../validate.js';
|
|
4
|
+
function unsupported(code, message) {
|
|
5
|
+
throw new IrUnsupportedError(code, message, 'openai-compatible');
|
|
6
|
+
}
|
|
7
|
+
function cacheOf(raw, prompt) {
|
|
8
|
+
const out = {};
|
|
9
|
+
if (raw && typeof raw === 'object') {
|
|
10
|
+
const rec = raw;
|
|
11
|
+
if (typeof rec.type === 'string')
|
|
12
|
+
out.type = rec.type;
|
|
13
|
+
else if (!prompt)
|
|
14
|
+
out.raw = rec;
|
|
15
|
+
}
|
|
16
|
+
if (prompt)
|
|
17
|
+
out.raw = prompt;
|
|
18
|
+
return out.type || out.raw ? out : undefined;
|
|
19
|
+
}
|
|
20
|
+
function cacheControlOf(cache) {
|
|
21
|
+
// `raw` is the Responses breakpoint when a part carries both spellings.
|
|
22
|
+
// Never leak that object into Anthropic's cache_control marker.
|
|
23
|
+
return cache.type ? { type: cache.type } : cache.raw ?? {};
|
|
24
|
+
}
|
|
25
|
+
export function partsFromChat(content) {
|
|
26
|
+
if (content === null || content === undefined)
|
|
27
|
+
return [];
|
|
28
|
+
if (typeof content === 'string')
|
|
29
|
+
return content ? [{ type: 'text', text: content }] : [];
|
|
30
|
+
return content.map((p) => {
|
|
31
|
+
const cache = cacheOf(p.cache_control, p.prompt_cache_breakpoint);
|
|
32
|
+
if (p.type === 'text') {
|
|
33
|
+
return {
|
|
34
|
+
type: 'text',
|
|
35
|
+
text: p.text ?? '',
|
|
36
|
+
...(cache ? { cache } : {}),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (p.type === 'image_url' && p.image_url?.url) {
|
|
40
|
+
const inline = DATA_URL_RE.exec(p.image_url.url);
|
|
41
|
+
return {
|
|
42
|
+
type: 'image',
|
|
43
|
+
source: inline
|
|
44
|
+
? { kind: 'base64', mediaType: inline[1], data: inline[2] }
|
|
45
|
+
: { kind: 'url', url: p.image_url.url },
|
|
46
|
+
...(p.image_url.detail ? { detail: p.image_url.detail } : {}),
|
|
47
|
+
...(cache ? { cache } : {}),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return unsupported('content_part', `Unsupported chat content part: ${String(p.type)}`);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
export function chatContentFromParts(parts, protocol = 'openai-compatible', options = {}) {
|
|
54
|
+
const carrierExtensions = options.carrierExtensions !== false;
|
|
55
|
+
if (parts.length === 0)
|
|
56
|
+
return null;
|
|
57
|
+
if (parts.every((p) => p.type === 'text' && (!p.cache || !carrierExtensions))) {
|
|
58
|
+
return parts.map((p) => (p.type === 'text' ? p.text : '')).join('');
|
|
59
|
+
}
|
|
60
|
+
return parts.map((p) => {
|
|
61
|
+
if (p.type === 'text') {
|
|
62
|
+
return {
|
|
63
|
+
type: 'text',
|
|
64
|
+
text: p.text,
|
|
65
|
+
...(carrierExtensions && p.cache ? { cache_control: cacheControlOf(p.cache) } : {}),
|
|
66
|
+
...(carrierExtensions && p.cache?.raw ? { prompt_cache_breakpoint: p.cache.raw } : {}),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (p.type === 'image' && p.source.kind !== 'file') {
|
|
70
|
+
const url = p.source.kind === 'base64'
|
|
71
|
+
? `data:${p.source.mediaType};base64,${p.source.data}`
|
|
72
|
+
: p.source.url;
|
|
73
|
+
return {
|
|
74
|
+
type: 'image_url',
|
|
75
|
+
image_url: { url, ...(p.detail ? { detail: p.detail } : {}) },
|
|
76
|
+
...(carrierExtensions && p.cache ? { cache_control: cacheControlOf(p.cache) } : {}),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
throw new IrUnsupportedError('content_part', `Chat cannot carry a ${p.type} part`, protocol);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function generationFromChat(req) {
|
|
83
|
+
const out = {};
|
|
84
|
+
if (req.temperature !== undefined)
|
|
85
|
+
out.temperature = req.temperature;
|
|
86
|
+
if (req.top_p !== undefined)
|
|
87
|
+
out.topP = req.top_p;
|
|
88
|
+
if (req.max_tokens !== undefined)
|
|
89
|
+
out.maxOutputTokens = req.max_tokens;
|
|
90
|
+
else if (req.max_completion_tokens !== undefined)
|
|
91
|
+
out.maxOutputTokens = req.max_completion_tokens;
|
|
92
|
+
if (req.stop !== undefined)
|
|
93
|
+
out.stop = Array.isArray(req.stop) ? req.stop : [req.stop];
|
|
94
|
+
if (req.seed !== undefined)
|
|
95
|
+
out.seed = req.seed;
|
|
96
|
+
if (req.presence_penalty !== undefined)
|
|
97
|
+
out.presencePenalty = req.presence_penalty;
|
|
98
|
+
if (req.frequency_penalty !== undefined)
|
|
99
|
+
out.frequencyPenalty = req.frequency_penalty;
|
|
100
|
+
return Object.keys(out).length ? out : undefined;
|
|
101
|
+
}
|
|
102
|
+
function textFromChat(req) {
|
|
103
|
+
const format = req.response_format;
|
|
104
|
+
if (!format)
|
|
105
|
+
return undefined;
|
|
106
|
+
if (format.type === 'json_schema') {
|
|
107
|
+
const detail = format.json_schema;
|
|
108
|
+
if (!detail?.schema) {
|
|
109
|
+
return unsupported('response_format', 'Chat json_schema response_format requires json_schema.schema.');
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
format: {
|
|
113
|
+
type: 'json_schema',
|
|
114
|
+
schema: detail.schema,
|
|
115
|
+
...(detail.name ? { name: detail.name } : {}),
|
|
116
|
+
...(detail.description ? { description: detail.description } : {}),
|
|
117
|
+
...(detail.strict !== undefined ? { strict: detail.strict } : {}),
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (format.type === 'json_object' || format.type === 'text')
|
|
122
|
+
return { format: { type: format.type } };
|
|
123
|
+
return unsupported('response_format', `Unsupported chat response_format: ${String(format.type)}`);
|
|
124
|
+
}
|
|
125
|
+
export function chatRequestToIR(req) {
|
|
126
|
+
const input = [];
|
|
127
|
+
for (const msg of req.messages) {
|
|
128
|
+
if (msg.role === 'tool') {
|
|
129
|
+
input.push({
|
|
130
|
+
type: 'tool_result',
|
|
131
|
+
callId: msg.tool_call_id ?? '',
|
|
132
|
+
content: partsFromChat(msg.content),
|
|
133
|
+
...(msg.cache_control ? { cache: cacheOf(msg.cache_control) } : {}),
|
|
134
|
+
});
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (msg.role === 'assistant' && msg.reasoning_content) {
|
|
138
|
+
input.push({ type: 'reasoning', text: msg.reasoning_content });
|
|
139
|
+
}
|
|
140
|
+
// Keep prose/refusal and tool calls in one contiguous assistant group. The
|
|
141
|
+
// reverse adapter folds that group back into ONE chat assistant message.
|
|
142
|
+
if (msg.role === 'assistant' &&
|
|
143
|
+
(msg.content !== null && msg.content !== undefined) &&
|
|
144
|
+
(typeof msg.content !== 'string' || msg.content.length > 0 || !msg.tool_calls?.length)) {
|
|
145
|
+
input.push({
|
|
146
|
+
type: 'message',
|
|
147
|
+
role: 'assistant',
|
|
148
|
+
content: partsFromChat(msg.content),
|
|
149
|
+
...(msg.name ? { name: msg.name } : {}),
|
|
150
|
+
...(msg.cache_control ? { cache: cacheOf(msg.cache_control) } : {}),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
if (msg.role === 'assistant' && msg.refusal) {
|
|
154
|
+
input.push({ type: 'message', role: 'assistant', content: [{ type: 'refusal', text: msg.refusal }] });
|
|
155
|
+
}
|
|
156
|
+
if (msg.role === 'assistant' && msg.tool_calls?.length) {
|
|
157
|
+
for (const call of msg.tool_calls) {
|
|
158
|
+
input.push({
|
|
159
|
+
type: 'tool_call',
|
|
160
|
+
id: call.id,
|
|
161
|
+
callId: call.id,
|
|
162
|
+
name: call.function.name,
|
|
163
|
+
arguments: call.function.arguments ?? '{}',
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (msg.role !== 'assistant') {
|
|
169
|
+
input.push({
|
|
170
|
+
type: 'message',
|
|
171
|
+
role: msg.role,
|
|
172
|
+
content: partsFromChat(msg.content),
|
|
173
|
+
...(msg.name ? { name: msg.name } : {}),
|
|
174
|
+
...(msg.cache_control ? { cache: cacheOf(msg.cache_control) } : {}),
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
else if (!msg.reasoning_content && !msg.refusal && (msg.content === null || msg.content === undefined)) {
|
|
178
|
+
// Preserve an explicitly empty assistant turn even without tool calls.
|
|
179
|
+
input.push({ type: 'message', role: 'assistant', content: [] });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const tools = req.tools?.map((t) => ({
|
|
183
|
+
type: 'function',
|
|
184
|
+
function: {
|
|
185
|
+
name: t.function.name,
|
|
186
|
+
...(t.function.description !== undefined ? { description: t.function.description } : {}),
|
|
187
|
+
parameters: t.function.parameters ?? { type: 'object', properties: {} },
|
|
188
|
+
...(typeof t.function.strict === 'boolean' ? { strict: t.function.strict } : {}),
|
|
189
|
+
...(t.function.cache_control ? { cache: cacheOf(t.function.cache_control) } : {}),
|
|
190
|
+
},
|
|
191
|
+
}));
|
|
192
|
+
const generation = generationFromChat(req);
|
|
193
|
+
const text = textFromChat(req);
|
|
194
|
+
return {
|
|
195
|
+
model: req.model,
|
|
196
|
+
stream: req.stream === true,
|
|
197
|
+
input,
|
|
198
|
+
// Reserved mock-mode controls are gateway-only and never rendered upstream.
|
|
199
|
+
...Object.fromEntries(Object.entries(req).filter(([key]) => key.startsWith('__simulate_'))),
|
|
200
|
+
...(req.router_options ? {
|
|
201
|
+
routerOptions: {
|
|
202
|
+
...(req.router_options.force_tier ? { forceTier: req.router_options.force_tier } : {}),
|
|
203
|
+
...(req.router_options.disable_fallback !== undefined ? { disableFallback: req.router_options.disable_fallback } : {}),
|
|
204
|
+
...(req.router_options.failover !== undefined ? { failover: req.router_options.failover } : {}),
|
|
205
|
+
...(req.router_options.session_id ? { sessionId: req.router_options.session_id } : {}),
|
|
206
|
+
...(req.router_options.client_ref ? { clientRef: req.router_options.client_ref } : {}),
|
|
207
|
+
},
|
|
208
|
+
} : {}),
|
|
209
|
+
...(req.logit_bias !== undefined ? { logitBias: req.logit_bias } : {}),
|
|
210
|
+
...(req.logprobs !== undefined ? { logprobs: req.logprobs } : {}),
|
|
211
|
+
...(req.top_logprobs !== undefined ? { topLogprobs: req.top_logprobs } : {}),
|
|
212
|
+
...(tools?.length ? { tools } : {}),
|
|
213
|
+
...(req.tool_choice !== undefined
|
|
214
|
+
? {
|
|
215
|
+
toolChoice: req.tool_choice === 'auto' || req.tool_choice === 'none' || req.tool_choice === 'required'
|
|
216
|
+
? { type: req.tool_choice }
|
|
217
|
+
: { type: 'function', name: req.tool_choice.function.name },
|
|
218
|
+
}
|
|
219
|
+
: {}),
|
|
220
|
+
...(req.parallel_tool_calls !== undefined ? { parallelToolCalls: req.parallel_tool_calls } : {}),
|
|
221
|
+
...(generation ? { generation } : {}),
|
|
222
|
+
...(text ? { text } : {}),
|
|
223
|
+
...(req.reasoning_effort || req.max_thinking_tokens !== undefined
|
|
224
|
+
? {
|
|
225
|
+
reasoning: {
|
|
226
|
+
...(req.reasoning_effort ? { effort: req.reasoning_effort } : {}),
|
|
227
|
+
...(req.max_thinking_tokens !== undefined ? { budgetTokens: req.max_thinking_tokens } : {}),
|
|
228
|
+
},
|
|
229
|
+
}
|
|
230
|
+
: {}),
|
|
231
|
+
...(req.prompt_cache_key || req.prompt_cache_retention || req.prompt_cache_options
|
|
232
|
+
? {
|
|
233
|
+
cache: {
|
|
234
|
+
...(req.prompt_cache_key ? { key: req.prompt_cache_key } : {}),
|
|
235
|
+
...(req.prompt_cache_retention ? { retention: req.prompt_cache_retention } : {}),
|
|
236
|
+
...(req.prompt_cache_options ? { options: req.prompt_cache_options } : {}),
|
|
237
|
+
},
|
|
238
|
+
}
|
|
239
|
+
: {}),
|
|
240
|
+
...(req.n !== undefined ? { n: req.n } : {}),
|
|
241
|
+
...(req.user !== undefined ? { user: req.user } : {}),
|
|
242
|
+
...(req.service_tier !== undefined ? { serviceTier: req.service_tier } : {}),
|
|
243
|
+
...(req.safety_identifier !== undefined ? { safetyIdentifier: req.safety_identifier } : {}),
|
|
244
|
+
...(req.stream_options !== undefined
|
|
245
|
+
? {
|
|
246
|
+
streamOptions: {
|
|
247
|
+
...(req.stream_options.include_usage !== undefined ? { includeUsage: req.stream_options.include_usage } : {}),
|
|
248
|
+
},
|
|
249
|
+
}
|
|
250
|
+
: {}),
|
|
251
|
+
...(req.metadata !== undefined ? { metadata: req.metadata } : {}),
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function appendAssistantContent(current, addition) {
|
|
255
|
+
if (addition === null)
|
|
256
|
+
return;
|
|
257
|
+
if (typeof current.content === 'string' && typeof addition === 'string') {
|
|
258
|
+
current.content += addition;
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const left = typeof current.content === 'string'
|
|
262
|
+
? current.content ? [{ type: 'text', text: current.content }] : []
|
|
263
|
+
: current.content ?? [];
|
|
264
|
+
const right = typeof addition === 'string'
|
|
265
|
+
? addition ? [{ type: 'text', text: addition }] : []
|
|
266
|
+
: addition;
|
|
267
|
+
current.content = [...left, ...right];
|
|
268
|
+
}
|
|
269
|
+
/** IR → the legacy execution request / OpenAI-chat request. */
|
|
270
|
+
export function irToChatRequest(ir, model, options = {}) {
|
|
271
|
+
assertCanonicalRequestSupported(ir, 'openai-compatible', {
|
|
272
|
+
stripCarrierExtensions: options.carrierExtensions === false,
|
|
273
|
+
});
|
|
274
|
+
const messages = [];
|
|
275
|
+
let assistant;
|
|
276
|
+
const ensureAssistant = () => {
|
|
277
|
+
if (!assistant) {
|
|
278
|
+
assistant = { role: 'assistant', content: '' };
|
|
279
|
+
messages.push(assistant);
|
|
280
|
+
}
|
|
281
|
+
return assistant;
|
|
282
|
+
};
|
|
283
|
+
if (ir.instructions)
|
|
284
|
+
messages.push({ role: 'system', content: ir.instructions });
|
|
285
|
+
for (const item of ir.input) {
|
|
286
|
+
if (item.type === 'reasoning') {
|
|
287
|
+
if (item.signature || item.encrypted) {
|
|
288
|
+
unsupported('reasoning_signature', 'Chat cannot replay signed or encrypted reasoning items.');
|
|
289
|
+
}
|
|
290
|
+
const msg = ensureAssistant();
|
|
291
|
+
msg.reasoning_content = [msg.reasoning_content, item.text, ...(item.summary ?? [])]
|
|
292
|
+
.filter((v) => typeof v === 'string' && v.length > 0)
|
|
293
|
+
.join('\n');
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (item.type === 'message' && item.role === 'assistant') {
|
|
297
|
+
const msg = ensureAssistant();
|
|
298
|
+
appendAssistantContent(msg, chatContentFromParts(item.content, 'openai-compatible', options));
|
|
299
|
+
const refusal = item.content.find((p) => p.type === 'refusal');
|
|
300
|
+
if (refusal?.type === 'refusal')
|
|
301
|
+
msg.refusal = [msg.refusal, refusal.text].filter(Boolean).join('\n');
|
|
302
|
+
if (item.name)
|
|
303
|
+
msg.name = item.name;
|
|
304
|
+
if (options.carrierExtensions !== false && item.cache)
|
|
305
|
+
msg.cache_control = cacheControlOf(item.cache);
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (item.type === 'tool_call') {
|
|
309
|
+
if (item.signature)
|
|
310
|
+
unsupported('tool_call_signature', 'Chat cannot replay a signed tool call.');
|
|
311
|
+
const msg = ensureAssistant();
|
|
312
|
+
msg.tool_calls = [
|
|
313
|
+
...(msg.tool_calls ?? []),
|
|
314
|
+
{
|
|
315
|
+
id: item.callId ?? item.id,
|
|
316
|
+
type: 'function',
|
|
317
|
+
function: { name: item.name, arguments: item.arguments },
|
|
318
|
+
},
|
|
319
|
+
];
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
assistant = undefined;
|
|
323
|
+
if (item.type === 'tool_result') {
|
|
324
|
+
if (!item.content.every((p) => p.type === 'text')) {
|
|
325
|
+
unsupported('tool_result_content', 'Chat tool messages carry text only.');
|
|
326
|
+
}
|
|
327
|
+
messages.push({
|
|
328
|
+
role: 'tool',
|
|
329
|
+
content: item.content.map((p) => (p.type === 'text' ? p.text : '')).join(''),
|
|
330
|
+
tool_call_id: item.callId,
|
|
331
|
+
...(options.carrierExtensions !== false && item.cache ? { cache_control: cacheControlOf(item.cache) } : {}),
|
|
332
|
+
});
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
if (item.type === 'message') {
|
|
336
|
+
messages.push({
|
|
337
|
+
role: item.role,
|
|
338
|
+
content: chatContentFromParts(item.content, 'openai-compatible', options) ?? '',
|
|
339
|
+
...(item.name ? { name: item.name } : {}),
|
|
340
|
+
...(options.carrierExtensions !== false && item.cache ? { cache_control: cacheControlOf(item.cache) } : {}),
|
|
341
|
+
});
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
if (item.type === 'item_reference') {
|
|
345
|
+
unsupported('item_reference', `Chat cannot resolve item_reference "${item.id}".`);
|
|
346
|
+
}
|
|
347
|
+
unsupported('input_item', `Chat cannot carry an extension item (${item.protocol ?? 'unknown'}).`);
|
|
348
|
+
}
|
|
349
|
+
const gen = ir.generation;
|
|
350
|
+
const tools = ir.tools?.map((t) => {
|
|
351
|
+
if (t.type !== 'function') {
|
|
352
|
+
unsupported('tool_type', `Chat carries function tools only, not ${t.type}.`);
|
|
353
|
+
}
|
|
354
|
+
return {
|
|
355
|
+
type: 'function',
|
|
356
|
+
function: {
|
|
357
|
+
name: t.function.name,
|
|
358
|
+
...(t.function.description !== undefined ? { description: t.function.description } : {}),
|
|
359
|
+
parameters: t.function.parameters ?? { type: 'object', properties: {} },
|
|
360
|
+
...(t.function.strict !== undefined ? { strict: t.function.strict } : {}),
|
|
361
|
+
...(options.carrierExtensions !== false && t.function.cache
|
|
362
|
+
? { cache_control: cacheControlOf(t.function.cache) }
|
|
363
|
+
: {}),
|
|
364
|
+
},
|
|
365
|
+
};
|
|
366
|
+
});
|
|
367
|
+
const toolChoice = ir.toolChoice
|
|
368
|
+
? ir.toolChoice.type === 'function'
|
|
369
|
+
? { type: 'function', function: { name: ir.toolChoice.name } }
|
|
370
|
+
: ir.toolChoice.type === 'auto' || ir.toolChoice.type === 'none' || ir.toolChoice.type === 'required'
|
|
371
|
+
? ir.toolChoice.type
|
|
372
|
+
: unsupported('tool_choice', `Chat cannot express tool_choice ${ir.toolChoice.type}.`)
|
|
373
|
+
: undefined;
|
|
374
|
+
if (ir.text?.verbosity)
|
|
375
|
+
unsupported('text_verbosity', 'Chat cannot represent text.verbosity on this execution bridge.');
|
|
376
|
+
if (ir.reasoning?.summary)
|
|
377
|
+
unsupported('reasoning_summary', 'Chat cannot represent reasoning.summary.');
|
|
378
|
+
for (const [code, value] of Object.entries({
|
|
379
|
+
store: ir.store,
|
|
380
|
+
previous_response_id: ir.previousResponseId,
|
|
381
|
+
conversation: ir.conversation,
|
|
382
|
+
prompt: ir.prompt,
|
|
383
|
+
background: ir.background,
|
|
384
|
+
include: ir.include,
|
|
385
|
+
truncation: ir.truncation,
|
|
386
|
+
})) {
|
|
387
|
+
if (value !== undefined)
|
|
388
|
+
unsupported(code, `Chat cannot represent ${code}.`);
|
|
389
|
+
}
|
|
390
|
+
return {
|
|
391
|
+
model,
|
|
392
|
+
stream: ir.stream,
|
|
393
|
+
messages,
|
|
394
|
+
...(tools?.length ? { tools } : {}),
|
|
395
|
+
...(toolChoice !== undefined ? { tool_choice: toolChoice } : {}),
|
|
396
|
+
...(ir.parallelToolCalls !== undefined ? { parallel_tool_calls: ir.parallelToolCalls } : {}),
|
|
397
|
+
...(gen?.temperature !== undefined ? { temperature: gen.temperature } : {}),
|
|
398
|
+
...(gen?.topP !== undefined ? { top_p: gen.topP } : {}),
|
|
399
|
+
...(gen?.maxOutputTokens !== undefined ? { max_tokens: gen.maxOutputTokens } : {}),
|
|
400
|
+
...(gen?.stop ? { stop: gen.stop } : {}),
|
|
401
|
+
...(gen?.seed !== undefined ? { seed: gen.seed } : {}),
|
|
402
|
+
...(gen?.presencePenalty !== undefined ? { presence_penalty: gen.presencePenalty } : {}),
|
|
403
|
+
...(gen?.frequencyPenalty !== undefined ? { frequency_penalty: gen.frequencyPenalty } : {}),
|
|
404
|
+
...(ir.n !== undefined ? { n: ir.n } : {}),
|
|
405
|
+
...(ir.logitBias !== undefined ? { logit_bias: ir.logitBias } : {}),
|
|
406
|
+
...(ir.logprobs !== undefined ? { logprobs: ir.logprobs } : {}),
|
|
407
|
+
...(ir.topLogprobs !== undefined ? { top_logprobs: ir.topLogprobs } : {}),
|
|
408
|
+
...(ir.text?.format
|
|
409
|
+
? {
|
|
410
|
+
response_format: ir.text.format.type === 'json_schema'
|
|
411
|
+
? {
|
|
412
|
+
type: 'json_schema',
|
|
413
|
+
json_schema: {
|
|
414
|
+
name: ir.text.format.name ?? 'output',
|
|
415
|
+
schema: ir.text.format.schema,
|
|
416
|
+
...(ir.text.format.description !== undefined ? { description: ir.text.format.description } : {}),
|
|
417
|
+
...(ir.text.format.strict !== undefined ? { strict: ir.text.format.strict } : {}),
|
|
418
|
+
},
|
|
419
|
+
}
|
|
420
|
+
: { type: ir.text.format.type },
|
|
421
|
+
}
|
|
422
|
+
: {}),
|
|
423
|
+
...(ir.reasoning?.effort
|
|
424
|
+
? {
|
|
425
|
+
reasoning_effort: ir.reasoning.effort === 'minimal'
|
|
426
|
+
? unsupported('reasoning_effort', 'Chat cannot represent reasoning effort "minimal".')
|
|
427
|
+
: ir.reasoning.effort,
|
|
428
|
+
}
|
|
429
|
+
: {}),
|
|
430
|
+
...(options.carrierExtensions !== false && ir.reasoning?.budgetTokens !== undefined
|
|
431
|
+
? { max_thinking_tokens: ir.reasoning.budgetTokens }
|
|
432
|
+
: {}),
|
|
433
|
+
...(ir.cache?.key ? { prompt_cache_key: ir.cache.key } : {}),
|
|
434
|
+
...(options.carrierExtensions !== false && ir.cache?.retention ? { prompt_cache_retention: ir.cache.retention } : {}),
|
|
435
|
+
...(options.carrierExtensions !== false && ir.cache?.options ? { prompt_cache_options: ir.cache.options } : {}),
|
|
436
|
+
...(ir.user !== undefined ? { user: ir.user } : {}),
|
|
437
|
+
...(ir.serviceTier !== undefined ? { service_tier: ir.serviceTier } : {}),
|
|
438
|
+
...(ir.safetyIdentifier !== undefined ? { safety_identifier: ir.safetyIdentifier } : {}),
|
|
439
|
+
...(ir.streamOptions !== undefined
|
|
440
|
+
? {
|
|
441
|
+
stream_options: {
|
|
442
|
+
...(ir.streamOptions.includeUsage !== undefined ? { include_usage: ir.streamOptions.includeUsage } : {}),
|
|
443
|
+
},
|
|
444
|
+
}
|
|
445
|
+
: {}),
|
|
446
|
+
...(ir.metadata !== undefined ? { metadata: ir.metadata } : {}),
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
export function usageToIR(usage) {
|
|
450
|
+
return {
|
|
451
|
+
inputTokens: usage?.prompt_tokens ?? 0,
|
|
452
|
+
outputTokens: usage?.completion_tokens ?? 0,
|
|
453
|
+
...(usage?.total_tokens !== undefined ? { totalTokens: usage.total_tokens } : {}),
|
|
454
|
+
...(usage?.prompt_tokens_details?.cached_tokens !== undefined
|
|
455
|
+
? { cachedInputTokens: usage.prompt_tokens_details.cached_tokens }
|
|
456
|
+
: {}),
|
|
457
|
+
...(usage?.prompt_tokens_details?.audio_tokens !== undefined
|
|
458
|
+
? { audioInputTokens: usage.prompt_tokens_details.audio_tokens }
|
|
459
|
+
: {}),
|
|
460
|
+
...(usage?.completion_tokens_details?.reasoning_tokens !== undefined
|
|
461
|
+
? { reasoningTokens: usage.completion_tokens_details.reasoning_tokens }
|
|
462
|
+
: {}),
|
|
463
|
+
...(usage?.completion_tokens_details?.audio_tokens !== undefined
|
|
464
|
+
? { audioOutputTokens: usage.completion_tokens_details.audio_tokens }
|
|
465
|
+
: {}),
|
|
466
|
+
...(usage?.completion_tokens_details?.accepted_prediction_tokens !== undefined
|
|
467
|
+
? { acceptedPredictionTokens: usage.completion_tokens_details.accepted_prediction_tokens }
|
|
468
|
+
: {}),
|
|
469
|
+
...(usage?.completion_tokens_details?.rejected_prediction_tokens !== undefined
|
|
470
|
+
? { rejectedPredictionTokens: usage.completion_tokens_details.rejected_prediction_tokens }
|
|
471
|
+
: {}),
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
function choiceToCandidate(choice) {
|
|
475
|
+
const output = [];
|
|
476
|
+
const reasoning = choice.message.reasoning_content ??
|
|
477
|
+
choice.message.reasoning;
|
|
478
|
+
if (reasoning)
|
|
479
|
+
output.push({ type: 'reasoning', text: reasoning });
|
|
480
|
+
if (choice.message.content !== null && choice.message.content !== undefined && choice.message.content !== '') {
|
|
481
|
+
output.push({ type: 'message', role: 'assistant', content: partsFromChat(choice.message.content) });
|
|
482
|
+
}
|
|
483
|
+
if (choice.message.refusal) {
|
|
484
|
+
output.push({ type: 'message', role: 'assistant', content: [{ type: 'refusal', text: choice.message.refusal }] });
|
|
485
|
+
}
|
|
486
|
+
for (const call of choice.message.tool_calls ?? []) {
|
|
487
|
+
output.push({
|
|
488
|
+
type: 'tool_call',
|
|
489
|
+
id: call.id,
|
|
490
|
+
callId: call.id,
|
|
491
|
+
name: call.function.name,
|
|
492
|
+
arguments: call.function.arguments ?? '{}',
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
const reason = choice.finish_reason;
|
|
496
|
+
return {
|
|
497
|
+
index: choice.index,
|
|
498
|
+
status: reason === 'length' || reason === 'content_filter' ? 'incomplete' : 'completed',
|
|
499
|
+
...(reason ? { reason } : {}),
|
|
500
|
+
output,
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
export function chatResponseToIR(resp) {
|
|
504
|
+
const first = resp.choices[0];
|
|
505
|
+
if (!first)
|
|
506
|
+
unsupported('empty_response', 'Chat response has no choices.');
|
|
507
|
+
const candidates = resp.choices.map(choiceToCandidate);
|
|
508
|
+
const primary = candidates[0];
|
|
509
|
+
return {
|
|
510
|
+
id: resp.id,
|
|
511
|
+
model: resp.model,
|
|
512
|
+
createdAt: resp.created,
|
|
513
|
+
status: primary.status,
|
|
514
|
+
...(primary.reason ? { reason: primary.reason } : {}),
|
|
515
|
+
output: primary.output,
|
|
516
|
+
...(candidates.length > 1 ? { candidates } : {}),
|
|
517
|
+
usage: usageToIR(resp.usage),
|
|
518
|
+
...(resp.system_fingerprint ? { systemFingerprint: resp.system_fingerprint } : {}),
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
function candidateToChatChoice(candidate) {
|
|
522
|
+
const message = { role: 'assistant', content: '' };
|
|
523
|
+
for (const item of candidate.output) {
|
|
524
|
+
if (item.type === 'reasoning') {
|
|
525
|
+
message.reasoning_content = [message.reasoning_content, item.text, ...(item.summary ?? [])]
|
|
526
|
+
.filter((v) => typeof v === 'string' && v.length > 0)
|
|
527
|
+
.join('\n');
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
if (item.type === 'tool_call') {
|
|
531
|
+
message.tool_calls = [
|
|
532
|
+
...(message.tool_calls ?? []),
|
|
533
|
+
{
|
|
534
|
+
id: item.callId ?? item.id,
|
|
535
|
+
type: 'function',
|
|
536
|
+
function: { name: item.name, arguments: item.arguments },
|
|
537
|
+
},
|
|
538
|
+
];
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
if (item.type === 'message') {
|
|
542
|
+
const refusal = item.content.find((p) => p.type === 'refusal');
|
|
543
|
+
const visible = item.content.filter((p) => p.type !== 'refusal');
|
|
544
|
+
appendAssistantContent(message, chatContentFromParts(visible) ?? '');
|
|
545
|
+
if (refusal?.type === 'refusal')
|
|
546
|
+
message.refusal = [message.refusal, refusal.text].filter(Boolean).join('\n');
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
unsupported('output_item', `Chat cannot carry an extension output item (${item.type}).`);
|
|
550
|
+
}
|
|
551
|
+
const finish = candidate.status === 'incomplete'
|
|
552
|
+
? candidate.reason === 'content_filter' ? 'content_filter' : 'length'
|
|
553
|
+
: candidate.status === 'failed'
|
|
554
|
+
? 'content_filter'
|
|
555
|
+
: candidate.reason === 'tool_calls' || candidate.output.some((i) => i.type === 'tool_call')
|
|
556
|
+
? 'tool_calls'
|
|
557
|
+
: 'stop';
|
|
558
|
+
return { index: candidate.index, message, finish_reason: finish };
|
|
559
|
+
}
|
|
560
|
+
export function irToChatResponse(ir, id, model) {
|
|
561
|
+
assertCanonicalResponseSupported(ir, 'openai-compatible');
|
|
562
|
+
const candidates = ir.candidates?.length
|
|
563
|
+
? ir.candidates
|
|
564
|
+
: [{ index: 0, status: ir.status, ...(ir.reason ? { reason: ir.reason } : {}), output: ir.output }];
|
|
565
|
+
return {
|
|
566
|
+
id,
|
|
567
|
+
object: 'chat.completion',
|
|
568
|
+
created: ir.createdAt ?? Math.floor(Date.now() / 1000),
|
|
569
|
+
model,
|
|
570
|
+
choices: candidates.map(candidateToChatChoice),
|
|
571
|
+
usage: {
|
|
572
|
+
prompt_tokens: ir.usage.inputTokens,
|
|
573
|
+
completion_tokens: ir.usage.outputTokens,
|
|
574
|
+
total_tokens: ir.usage.totalTokens ?? ir.usage.inputTokens + ir.usage.outputTokens,
|
|
575
|
+
...(ir.usage.cachedInputTokens !== undefined || ir.usage.audioInputTokens !== undefined
|
|
576
|
+
? {
|
|
577
|
+
prompt_tokens_details: {
|
|
578
|
+
...(ir.usage.cachedInputTokens !== undefined ? { cached_tokens: ir.usage.cachedInputTokens } : {}),
|
|
579
|
+
...(ir.usage.audioInputTokens !== undefined ? { audio_tokens: ir.usage.audioInputTokens } : {}),
|
|
580
|
+
},
|
|
581
|
+
}
|
|
582
|
+
: {}),
|
|
583
|
+
...(ir.usage.reasoningTokens !== undefined || ir.usage.audioOutputTokens !== undefined ||
|
|
584
|
+
ir.usage.acceptedPredictionTokens !== undefined ||
|
|
585
|
+
ir.usage.rejectedPredictionTokens !== undefined
|
|
586
|
+
? {
|
|
587
|
+
completion_tokens_details: {
|
|
588
|
+
...(ir.usage.reasoningTokens !== undefined ? { reasoning_tokens: ir.usage.reasoningTokens } : {}),
|
|
589
|
+
...(ir.usage.audioOutputTokens !== undefined ? { audio_tokens: ir.usage.audioOutputTokens } : {}),
|
|
590
|
+
...(ir.usage.acceptedPredictionTokens !== undefined
|
|
591
|
+
? { accepted_prediction_tokens: ir.usage.acceptedPredictionTokens }
|
|
592
|
+
: {}),
|
|
593
|
+
...(ir.usage.rejectedPredictionTokens !== undefined
|
|
594
|
+
? { rejected_prediction_tokens: ir.usage.rejectedPredictionTokens }
|
|
595
|
+
: {}),
|
|
596
|
+
},
|
|
597
|
+
}
|
|
598
|
+
: {}),
|
|
599
|
+
},
|
|
600
|
+
...(ir.systemFingerprint ? { system_fingerprint: ir.systemFingerprint } : {}),
|
|
601
|
+
};
|
|
602
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { StreamEvent } from '../types.js';
|
|
2
|
+
/** Gemini streamGenerateContent chunks → canonical item lifecycle events. */
|
|
3
|
+
export declare class GoogleStreamDecoder {
|
|
4
|
+
private readonly candidates;
|
|
5
|
+
private readonly id;
|
|
6
|
+
private readonly createdAt;
|
|
7
|
+
private createdSent;
|
|
8
|
+
private nativeTerminal;
|
|
9
|
+
private terminalSent;
|
|
10
|
+
private blockedReason;
|
|
11
|
+
get terminated(): boolean;
|
|
12
|
+
push(raw: unknown): StreamEvent[];
|
|
13
|
+
finish(): StreamEvent[];
|
|
14
|
+
private textDelta;
|
|
15
|
+
private reasoningDelta;
|
|
16
|
+
private completeMessagePart;
|
|
17
|
+
private closeActive;
|
|
18
|
+
private emitMetadata;
|
|
19
|
+
private ensureCreated;
|
|
20
|
+
private state;
|
|
21
|
+
private reason;
|
|
22
|
+
}
|