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
|
@@ -1,4 +1,26 @@
|
|
|
1
1
|
import crypto from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* The wire parameters this gateway does NOT implement but which change what
|
|
4
|
+
* the client believes it got. Ignoring them is the one behavior a standard
|
|
5
|
+
* client cannot detect, so each is rejected with `unsupported_parameter`
|
|
6
|
+
* instead (fail-visible over silent divergence).
|
|
7
|
+
*/
|
|
8
|
+
export function unsupportedResponsesParameter(req) {
|
|
9
|
+
if (req.conversation !== undefined && req.conversation !== null) {
|
|
10
|
+
return 'Unsupported parameter: "conversation" — the Conversations API is not implemented by this gateway; send the full conversation "input" on every turn and omit "conversation".';
|
|
11
|
+
}
|
|
12
|
+
if (req.background === true) {
|
|
13
|
+
return 'Unsupported parameter: "background" — this gateway has no asynchronous execution or polling endpoint; omit "background".';
|
|
14
|
+
}
|
|
15
|
+
if (req.truncation === 'auto') {
|
|
16
|
+
return 'Unsupported parameter: "truncation" — automatic context trimming is not implemented; shorten the input or send "truncation": "disabled".';
|
|
17
|
+
}
|
|
18
|
+
const format = req.text?.format;
|
|
19
|
+
if (format && typeof format === 'object' && format.type !== undefined && format.type !== 'text') {
|
|
20
|
+
return `Unsupported parameter: "text.format" (type "${String(format.type)}") — structured outputs are not implemented on this gateway.`;
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
2
24
|
/**
|
|
3
25
|
* Responses-wire tools are FLAT (`{type:'function', name, description, parameters}`);
|
|
4
26
|
* chat-completions tools are NESTED (`{type:'function', function:{...}}`). The
|
|
@@ -63,33 +85,108 @@ function contentToChatContent(content) {
|
|
|
63
85
|
return { type: 'text', text: p.text };
|
|
64
86
|
});
|
|
65
87
|
}
|
|
88
|
+
/** Thinking text carried by one Responses `reasoning` item (summary parts and/or content). */
|
|
89
|
+
function reasoningTextOf(item) {
|
|
90
|
+
const parts = [];
|
|
91
|
+
const collect = (value) => {
|
|
92
|
+
if (typeof value === 'string') {
|
|
93
|
+
if (value)
|
|
94
|
+
parts.push(value);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (!Array.isArray(value))
|
|
98
|
+
return;
|
|
99
|
+
for (const part of value) {
|
|
100
|
+
if (part && typeof part === 'object' && 'text' in part && typeof part.text === 'string' && part.text) {
|
|
101
|
+
parts.push(part.text);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
collect(item.summary);
|
|
106
|
+
collect(item.content);
|
|
107
|
+
return parts.length ? parts.join('\n') : undefined;
|
|
108
|
+
}
|
|
66
109
|
export function inputItemsToMessages(input) {
|
|
67
110
|
const messages = [];
|
|
68
111
|
const items = typeof input === 'string' ? [{ role: 'user', content: input }] : input || [];
|
|
112
|
+
// Thinking text waiting for the assistant turn it belongs to. In the normal
|
|
113
|
+
// Responses order a `reasoning` item precedes the message/function_call
|
|
114
|
+
// items of its turn; on a relayed stream the closing half emits it AFTER
|
|
115
|
+
// them, so a pending value is attached backward once a non-assistant item
|
|
116
|
+
// proves no assistant turn follows.
|
|
117
|
+
let pendingReasoning;
|
|
118
|
+
const takeReasoning = () => {
|
|
119
|
+
const text = pendingReasoning;
|
|
120
|
+
pendingReasoning = undefined;
|
|
121
|
+
return text;
|
|
122
|
+
};
|
|
123
|
+
const attachReasoning = (msg) => {
|
|
124
|
+
const text = takeReasoning();
|
|
125
|
+
if (text !== undefined && !msg.reasoning_content)
|
|
126
|
+
msg.reasoning_content = text;
|
|
127
|
+
};
|
|
128
|
+
const attachReasoningBackward = () => {
|
|
129
|
+
if (pendingReasoning === undefined)
|
|
130
|
+
return;
|
|
131
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
132
|
+
const candidate = messages[i];
|
|
133
|
+
if (candidate.role !== 'assistant')
|
|
134
|
+
continue;
|
|
135
|
+
if (!candidate.reasoning_content)
|
|
136
|
+
candidate.reasoning_content = takeReasoning();
|
|
137
|
+
pendingReasoning = undefined;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
pendingReasoning = undefined;
|
|
141
|
+
};
|
|
69
142
|
for (const item of items) {
|
|
70
|
-
const t = item
|
|
143
|
+
const t = item.type;
|
|
71
144
|
if (t === undefined || t === 'message') {
|
|
72
145
|
const m = item;
|
|
73
|
-
|
|
146
|
+
const message = {
|
|
74
147
|
role: (m.role || 'user'),
|
|
75
148
|
content: contentToChatContent(m.content),
|
|
76
|
-
}
|
|
149
|
+
};
|
|
150
|
+
if (message.role === 'assistant')
|
|
151
|
+
attachReasoning(message);
|
|
152
|
+
else
|
|
153
|
+
attachReasoningBackward();
|
|
154
|
+
messages.push(message);
|
|
77
155
|
}
|
|
78
156
|
else if (t === 'function_call') {
|
|
79
157
|
const fc = item;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
158
|
+
const call = {
|
|
159
|
+
id: fc.call_id,
|
|
160
|
+
type: 'function',
|
|
161
|
+
function: { name: fc.name, arguments: fc.arguments },
|
|
162
|
+
};
|
|
163
|
+
// A Responses turn emits ONE function_call item per call, while a
|
|
164
|
+
// chat-completions assistant turn carries ALL of its calls on a single
|
|
165
|
+
// message. Emitting one assistant message per call produces consecutive
|
|
166
|
+
// assistant messages, which strict upstreams (DeepSeek, OpenAI) reject:
|
|
167
|
+
// "An assistant message with 'tool_calls' must be followed by tool
|
|
168
|
+
// messages responding to each 'tool_call_id'". Fold each call into the
|
|
169
|
+
// directly preceding assistant message (the text item of the same turn
|
|
170
|
+
// or the call emitted just before it).
|
|
171
|
+
const prev = messages[messages.length - 1];
|
|
172
|
+
if (prev && prev.role === 'assistant') {
|
|
173
|
+
(prev.tool_calls ??= []).push(call);
|
|
174
|
+
attachReasoning(prev);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
const message = { role: 'assistant', content: '', tool_calls: [call] };
|
|
178
|
+
attachReasoning(message);
|
|
179
|
+
messages.push(message);
|
|
180
|
+
}
|
|
85
181
|
}
|
|
86
182
|
else if (t === 'function_call_output') {
|
|
87
|
-
|
|
88
|
-
messages.push({ role: 'tool', content: String(
|
|
183
|
+
attachReasoningBackward();
|
|
184
|
+
messages.push({ role: 'tool', content: String(item.output ?? ''), tool_call_id: item.call_id });
|
|
89
185
|
}
|
|
90
186
|
else if (t === 'reasoning') {
|
|
91
|
-
|
|
92
|
-
|
|
187
|
+
const text = reasoningTextOf(item);
|
|
188
|
+
if (text)
|
|
189
|
+
pendingReasoning = pendingReasoning ? `${pendingReasoning}\n${text}` : text;
|
|
93
190
|
}
|
|
94
191
|
else {
|
|
95
192
|
throw new Error(`Unsupported input item type: "${t}"`);
|
|
@@ -103,9 +200,33 @@ export function inputItemsToMessages(input) {
|
|
|
103
200
|
function shortId(prefix) {
|
|
104
201
|
return prefix + crypto.randomBytes(12).toString('hex');
|
|
105
202
|
}
|
|
106
|
-
export function chatToResponseObject(chatResp, respId, model
|
|
203
|
+
export function chatToResponseObject(chatResp, respId, model,
|
|
204
|
+
/** Pre-minted on the relayed leg so `response.created` and `response.completed` share one item id. */
|
|
205
|
+
messageId = shortId('msg_'),
|
|
206
|
+
/**
|
|
207
|
+
* The client's EFFECTIVE `store` flag (default true, OpenAI parity). The
|
|
208
|
+
* gateway honors it: `store:false` means the response is not retained, so it
|
|
209
|
+
* can never be a `previous_response_id` parent — and the response object
|
|
210
|
+
* says so instead of pretending a chain exists.
|
|
211
|
+
*/
|
|
212
|
+
store = true) {
|
|
107
213
|
const msg = chatResp.choices[0]?.message;
|
|
108
214
|
const output = [];
|
|
215
|
+
// Thinking text is a FIRST-CLASS output item on this wire: the client stores
|
|
216
|
+
// it and replays it as an input item on the next turn, which is the only way
|
|
217
|
+
// a stateless Responses client can satisfy thinking-mode upstreams (DeepSeek
|
|
218
|
+
// rejects a follow-up whose assistant turn lost its reasoning). The raw text
|
|
219
|
+
// rides `summary` parts — the schema-stable carrier every Responses parser
|
|
220
|
+
// keeps; `content` is accepted on the way back too (see reasoningTextOf).
|
|
221
|
+
const reasoning = typeof msg?.reasoning_content === 'string' ? msg.reasoning_content : '';
|
|
222
|
+
if (reasoning) {
|
|
223
|
+
output.push({
|
|
224
|
+
type: 'reasoning',
|
|
225
|
+
id: shortId('rs_'),
|
|
226
|
+
status: 'completed',
|
|
227
|
+
summary: [{ type: 'summary_text', text: reasoning }],
|
|
228
|
+
});
|
|
229
|
+
}
|
|
109
230
|
// Assistant prose and tool_calls coexist in Responses output (message item +
|
|
110
231
|
// function_call items); dropping the text when the model also called tools
|
|
111
232
|
// would silently lose its explanation/reasoning preamble.
|
|
@@ -113,7 +234,7 @@ export function chatToResponseObject(chatResp, respId, model) {
|
|
|
113
234
|
if (text) {
|
|
114
235
|
output.push({
|
|
115
236
|
type: 'message',
|
|
116
|
-
id:
|
|
237
|
+
id: messageId,
|
|
117
238
|
status: 'completed',
|
|
118
239
|
role: 'assistant',
|
|
119
240
|
content: [{ type: 'output_text', text, annotations: [] }],
|
|
@@ -134,7 +255,7 @@ export function chatToResponseObject(chatResp, respId, model) {
|
|
|
134
255
|
if (output.length === 0) {
|
|
135
256
|
output.push({
|
|
136
257
|
type: 'message',
|
|
137
|
-
id:
|
|
258
|
+
id: messageId,
|
|
138
259
|
status: 'completed',
|
|
139
260
|
role: 'assistant',
|
|
140
261
|
content: [{ type: 'output_text', text: '', annotations: [] }],
|
|
@@ -148,43 +269,189 @@ export function chatToResponseObject(chatResp, respId, model) {
|
|
|
148
269
|
status: 'completed',
|
|
149
270
|
model,
|
|
150
271
|
output,
|
|
272
|
+
// REQUIRED by strict Responses clients. JetBrains Junie declares `tools`
|
|
273
|
+
// with no default in its `Response` serializer, so an absent field aborts
|
|
274
|
+
// parsing of `response.created`/`response.completed` entirely — the client
|
|
275
|
+
// never sees a terminal event and reports a truncated stream. An empty
|
|
276
|
+
// array is the correct "no tools echoed on this response" value.
|
|
277
|
+
tools: [],
|
|
278
|
+
store,
|
|
151
279
|
usage: {
|
|
152
280
|
input_tokens: u?.prompt_tokens || 0,
|
|
281
|
+
// Standard breakdowns, emitted only when the upstream reported them:
|
|
282
|
+
// cached prompt tokens (KV/prefix hits) and reasoning tokens. Clients
|
|
283
|
+
// that meter cost read these; inventing zeros would be a lie, so an
|
|
284
|
+
// unreported detail is simply absent.
|
|
285
|
+
...(typeof u?.prompt_tokens_details?.cached_tokens === 'number'
|
|
286
|
+
? { input_tokens_details: { cached_tokens: u.prompt_tokens_details.cached_tokens } }
|
|
287
|
+
: {}),
|
|
153
288
|
output_tokens: u?.completion_tokens || 0,
|
|
289
|
+
...(typeof u?.completion_tokens_details?.reasoning_tokens === 'number'
|
|
290
|
+
? { output_tokens_details: { reasoning_tokens: u.completion_tokens_details.reasoning_tokens } }
|
|
291
|
+
: {}),
|
|
154
292
|
total_tokens: u?.total_tokens || (u ? u.prompt_tokens + u.completion_tokens : 0),
|
|
155
293
|
},
|
|
156
294
|
};
|
|
157
295
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
296
|
+
export function newResponseStreamSequence() {
|
|
297
|
+
return { value: 0 };
|
|
298
|
+
}
|
|
299
|
+
function sequenced(seq, payload) {
|
|
300
|
+
return { sequence_number: seq.value++, ...payload };
|
|
301
|
+
}
|
|
302
|
+
/** One `event:/data:` frame — the exact shape every Responses SSE consumer reads. */
|
|
303
|
+
export function responsesEventFrame(payload) {
|
|
304
|
+
return `event: ${payload.type}\ndata: ${JSON.stringify(payload)}\n\n`;
|
|
305
|
+
}
|
|
306
|
+
/** Id for the assistant message item; minted at parse time on the relayed leg. */
|
|
307
|
+
export function newResponseMessageId() {
|
|
308
|
+
return shortId('msg_');
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Frames emitted ONCE before the first upstream chunk of a relayed turn.
|
|
312
|
+
* `output_item.added` / `content_part.added` pin the item_id that every later
|
|
313
|
+
* `response.output_text.delta` must carry, so they cannot wait for the result.
|
|
314
|
+
*/
|
|
315
|
+
export function responsesStreamPrefix(respId, model, messageId, seq = newResponseStreamSequence(), store = true) {
|
|
316
|
+
const response = {
|
|
317
|
+
id: respId,
|
|
318
|
+
object: 'response',
|
|
319
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
320
|
+
status: 'in_progress',
|
|
321
|
+
model,
|
|
322
|
+
output: [],
|
|
323
|
+
// Same required field as the completed response — `response.created` is
|
|
324
|
+
// parsed into the very same Response schema by strict clients.
|
|
325
|
+
tools: [],
|
|
326
|
+
// Echoed on the opening object exactly like the terminal one: a client
|
|
327
|
+
// that asked for `store:false` must not be told a chain now exists.
|
|
328
|
+
store,
|
|
329
|
+
usage: { input_tokens: 0, output_tokens: 0, total_tokens: 0 },
|
|
330
|
+
};
|
|
331
|
+
return [
|
|
332
|
+
{ type: 'response.created', response },
|
|
333
|
+
{ type: 'response.in_progress', response },
|
|
334
|
+
{
|
|
335
|
+
type: 'response.output_item.added',
|
|
336
|
+
output_index: 0,
|
|
337
|
+
item: { type: 'message', id: messageId, status: 'in_progress', role: 'assistant', content: [] },
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
type: 'response.content_part.added',
|
|
341
|
+
item_id: messageId,
|
|
342
|
+
output_index: 0,
|
|
343
|
+
content_index: 0,
|
|
344
|
+
part: { type: 'output_text', text: '', annotations: [] },
|
|
345
|
+
},
|
|
346
|
+
].map((payload) => responsesEventFrame(sequenced(seq, payload)));
|
|
347
|
+
}
|
|
348
|
+
/** ONE upstream chat delta -> ONE Responses text delta frame. */
|
|
349
|
+
export function responsesDeltaFrame(messageId, delta, seq) {
|
|
350
|
+
return responsesEventFrame(sequenced(seq, {
|
|
351
|
+
type: 'response.output_text.delta',
|
|
352
|
+
item_id: messageId,
|
|
353
|
+
output_index: 0,
|
|
354
|
+
content_index: 0,
|
|
355
|
+
delta,
|
|
356
|
+
}));
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Full event payload sequence for one completed response.
|
|
360
|
+
*
|
|
361
|
+
* `relayed` distinguishes the two terminal legs:
|
|
362
|
+
* - buffered (`false`): synthesize the whole sequence from the final result
|
|
363
|
+
* after generation — created/in_progress, every added/delta/done pair.
|
|
364
|
+
* - relayed (`true`): the opening `created`/`in_progress` pair, item-0's
|
|
365
|
+
* `output_item.added`/`content_part.added`, and all live text deltas were
|
|
366
|
+
* ALREADY written during generation. Emit only the closing half.
|
|
367
|
+
*
|
|
368
|
+
* Tool-call arguments are NEVER relayed live (chat-completions upstreams carry
|
|
369
|
+
* tool_calls on the final result), so `output_item.added` +
|
|
370
|
+
* `function_call_arguments.delta` MUST be emitted here on the relayed leg too:
|
|
371
|
+
* strict clients reconstruct tool calls from exactly those two event types,
|
|
372
|
+
* not from the terminal `response.completed` object alone.
|
|
373
|
+
*/
|
|
374
|
+
function responseEventPayloads(chatResp, respId, model, messageId, relayed = false, store = true) {
|
|
375
|
+
const response = chatToResponseObject(chatResp, respId, model, messageId, store);
|
|
165
376
|
const output = response.output;
|
|
377
|
+
// Reasoning items are placed at the END on the relayed leg: the prefix has
|
|
378
|
+
// already announced the message item at output_index 0, so a reasoning item
|
|
379
|
+
// cannot take that slot without desynchronizing every index the client has
|
|
380
|
+
// seen. The buffered leg keeps the standard reasoning-first order.
|
|
381
|
+
if (relayed) {
|
|
382
|
+
const reasoningItems = output.filter((item) => item.type === 'reasoning');
|
|
383
|
+
if (reasoningItems.length > 0) {
|
|
384
|
+
const rest = output.filter((item) => item.type !== 'reasoning');
|
|
385
|
+
output.length = 0;
|
|
386
|
+
output.push(...rest);
|
|
387
|
+
// The prefix unconditionally announced item 0 as the parse-minted
|
|
388
|
+
// message item. A tool-call-only answer has no prose item in `output`,
|
|
389
|
+
// so insert the (empty) message item at index 0 to keep every
|
|
390
|
+
// output_index in the closing half aligned with what the client saw.
|
|
391
|
+
if (output[0]?.id !== messageId) {
|
|
392
|
+
output.unshift({
|
|
393
|
+
type: 'message',
|
|
394
|
+
id: messageId,
|
|
395
|
+
status: 'completed',
|
|
396
|
+
role: 'assistant',
|
|
397
|
+
content: [{ type: 'output_text', text: '', annotations: [] }],
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
output.push(...reasoningItems);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (relayed && output[0]?.id !== messageId) {
|
|
404
|
+
output.unshift({
|
|
405
|
+
type: 'message',
|
|
406
|
+
id: messageId,
|
|
407
|
+
status: 'completed',
|
|
408
|
+
role: 'assistant',
|
|
409
|
+
content: [{ type: 'output_text', text: '', annotations: [] }],
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
const events = [];
|
|
413
|
+
const emit = (payload) => { events.push(payload); };
|
|
414
|
+
if (!relayed) {
|
|
415
|
+
emit({ type: 'response.created', response: { ...response, status: 'in_progress', output: [] } });
|
|
416
|
+
emit({ type: 'response.in_progress', response: { ...response, status: 'in_progress', output: [] } });
|
|
417
|
+
}
|
|
166
418
|
for (let i = 0; i < output.length; i++) {
|
|
167
419
|
const item = output[i];
|
|
168
|
-
|
|
169
|
-
if (
|
|
170
|
-
const fullText = item.content?.[0]?.text || '';
|
|
420
|
+
const prefixAlreadySent = relayed && i === 0 && item.id === messageId;
|
|
421
|
+
if (!prefixAlreadySent) {
|
|
171
422
|
emit({
|
|
172
|
-
type: 'response.
|
|
173
|
-
item_id: item.id,
|
|
423
|
+
type: 'response.output_item.added',
|
|
174
424
|
output_index: i,
|
|
175
|
-
|
|
176
|
-
|
|
425
|
+
item: item.type === 'function_call'
|
|
426
|
+
? { ...item, status: 'in_progress', arguments: '' }
|
|
427
|
+
: { ...item, status: 'in_progress' },
|
|
177
428
|
});
|
|
178
|
-
|
|
179
|
-
|
|
429
|
+
}
|
|
430
|
+
if (item.type === 'message') {
|
|
431
|
+
const fullText = item.content?.[0]?.text || '';
|
|
432
|
+
if (!prefixAlreadySent) {
|
|
180
433
|
emit({
|
|
181
|
-
type: 'response.
|
|
434
|
+
type: 'response.content_part.added',
|
|
182
435
|
item_id: item.id,
|
|
183
436
|
output_index: i,
|
|
184
437
|
content_index: 0,
|
|
185
|
-
|
|
438
|
+
part: { type: 'output_text', text: '', annotations: [] },
|
|
186
439
|
});
|
|
187
440
|
}
|
|
441
|
+
// On the relayed leg the live upstream chunks already carried these
|
|
442
|
+
// deltas; replaying them here would duplicate every token.
|
|
443
|
+
if (!relayed) {
|
|
444
|
+
const chunkSize = 16;
|
|
445
|
+
for (let j = 0; j < fullText.length; j += chunkSize) {
|
|
446
|
+
emit({
|
|
447
|
+
type: 'response.output_text.delta',
|
|
448
|
+
item_id: item.id,
|
|
449
|
+
output_index: i,
|
|
450
|
+
content_index: 0,
|
|
451
|
+
delta: fullText.slice(j, j + chunkSize),
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
}
|
|
188
455
|
emit({ type: 'response.output_text.done', item_id: item.id, output_index: i, content_index: 0, text: fullText });
|
|
189
456
|
emit({ type: 'response.content_part.done', item_id: item.id, output_index: i, content_index: 0, part: item.content[0] });
|
|
190
457
|
}
|
|
@@ -206,3 +473,17 @@ export function chatToStreamEvents(chatResp, respId, model) {
|
|
|
206
473
|
emit({ type: 'response.completed', response });
|
|
207
474
|
return events;
|
|
208
475
|
}
|
|
476
|
+
/** Synthesized Responses SSE event sequence for one buffered completion. */
|
|
477
|
+
export function chatToStreamEvents(chatResp, respId, model, messageId = shortId('msg_'), seq = newResponseStreamSequence(), store = true) {
|
|
478
|
+
return responseEventPayloads(chatResp, respId, model, messageId, false, store)
|
|
479
|
+
.map((payload) => responsesEventFrame(sequenced(seq, payload)));
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Closing half of a relayed turn. Shares the SAME sequence counter as the
|
|
483
|
+
* prefix and the live deltas so `sequence_number` stays strictly increasing
|
|
484
|
+
* across the entire connection.
|
|
485
|
+
*/
|
|
486
|
+
export function responsesStreamSuffix(chatResp, respId, model, messageId, seq, store = true) {
|
|
487
|
+
return responseEventPayloads(chatResp, respId, model, messageId, true, store)
|
|
488
|
+
.map((payload) => responsesEventFrame(sequenced(seq, payload)));
|
|
489
|
+
}
|
package/backend/dist/server.js
CHANGED
|
@@ -22,6 +22,7 @@ import { ProviderRegistry } from './providers/registry.js';
|
|
|
22
22
|
import { FinOpsTracker } from './metrics/finops-tracker.js';
|
|
23
23
|
import { APP_VERSION } from './version.js';
|
|
24
24
|
import { setGatewayMetaDeps } from './cli/clients/model-meta.js';
|
|
25
|
+
import { noteClientActivityFromUserAgent } from './cli/clients/activity.js';
|
|
25
26
|
import { comboEnvelope, findMemberCatalogRecords, modelsDevBlock, tierAliasEnvelope, virtualModelsDevBlock, } from './providers/virtual-meta.js';
|
|
26
27
|
import { modelSupportsEfforts } from './types/router.js';
|
|
27
28
|
import { isInternalSessionId } from './session/session-manager.js';
|
|
@@ -462,6 +463,13 @@ providerConfigs = []) {
|
|
|
462
463
|
}
|
|
463
464
|
// Attach client identity onto request for tracing and logging
|
|
464
465
|
req.authInfo = validation;
|
|
466
|
+
// Display-only client-activity stamp for the Clients page's dynamic sort:
|
|
467
|
+
// fingerprint the User-Agent on the four inference wires only (model /
|
|
468
|
+
// admin browsing is not "using a client"). Forgeable by design of HTTP —
|
|
469
|
+
// never routing state, a spoofed UA costs at most a wrong card position.
|
|
470
|
+
if (path !== null && INFERENCE_ROUTE_PATHS.has(path)) {
|
|
471
|
+
noteClientActivityFromUserAgent(req.headers['user-agent']);
|
|
472
|
+
}
|
|
465
473
|
// ── Inbound sliding-window enforcement (apiKeys[].limits) ───────────────
|
|
466
474
|
// The client key's window caps are a CONTRACT with the caller, enforced at
|
|
467
475
|
// the gate: an over-limit request never enters the pipeline and never
|
|
@@ -987,7 +995,12 @@ providerConfigs = []) {
|
|
|
987
995
|
};
|
|
988
996
|
// ── Concrete providers: catalog passthrough, registry fallback ──
|
|
989
997
|
const allModels = registry.getAllModels();
|
|
990
|
-
|
|
998
|
+
// Keyed by the LOOKUP id (the registered `${provider}/${upstream}` form),
|
|
999
|
+
// never by the record's own `id`: a prefixed provider's catalog record keeps
|
|
1000
|
+
// the bare upstream key, so `[c.id, c]` missed EVERY prefixed provider and
|
|
1001
|
+
// silently dropped its attachment/modalities/limit/tool_call from the
|
|
1002
|
+
// public capability surface (only the registry fallback block survived).
|
|
1003
|
+
const catById = (await findMemberCatalogRecords(allModels.map((m) => m.id), listCatalogOnce)).byId;
|
|
991
1004
|
const CATALOG_PROVIDER_NAME = new Map(catalogOnce.map((p) => [p.id, p.name]));
|
|
992
1005
|
const ensureProvider = (id) => (providers[id] ??= { id, ...(CATALOG_PROVIDER_NAME.get(id) ? { name: CATALOG_PROVIDER_NAME.get(id) } : {}), models: {} });
|
|
993
1006
|
for (const m of allModels) {
|
|
@@ -1006,8 +1019,13 @@ providerConfigs = []) {
|
|
|
1006
1019
|
...(cat
|
|
1007
1020
|
? modelsDevBlock(cat)
|
|
1008
1021
|
: {
|
|
1009
|
-
reasoning: modelSupportsEfforts(m.supportedReasoningEfforts, m.supportsReasoningEffort),
|
|
1010
|
-
|
|
1022
|
+
reasoning: modelSupportsEfforts(m.supportedReasoningEfforts, m.supportsReasoningEffort, m.reasoningDisableable),
|
|
1023
|
+
// Omit `cost` entirely when the price is unknown — an absent block
|
|
1024
|
+
// makes the emitted catalog say "no price", whereas a synthesized
|
|
1025
|
+
// one would be indistinguishable from a real quote.
|
|
1026
|
+
...(m.pricing
|
|
1027
|
+
? { cost: { input: m.pricing.input, output: m.pricing.output, cache_read: m.pricing.cacheRead, ...(m.pricing.cacheWrite !== undefined ? { cache_write: m.pricing.cacheWrite } : {}) } }
|
|
1028
|
+
: {}),
|
|
1011
1029
|
}),
|
|
1012
1030
|
ocr: { tier: m.tier, model: m.id },
|
|
1013
1031
|
};
|
|
@@ -1071,8 +1089,16 @@ providerConfigs = []) {
|
|
|
1071
1089
|
return providers;
|
|
1072
1090
|
};
|
|
1073
1091
|
// 2B. Single model lookup (OpenAI standard)
|
|
1074
|
-
|
|
1075
|
-
|
|
1092
|
+
//
|
|
1093
|
+
// TWO routes, one handler: the gateway-visible id vocabulary contains a `/`
|
|
1094
|
+
// (`${provider}/${upstream}` — e.g. `qoder-cn/dmodel`), which find-my-way's
|
|
1095
|
+
// `:model` param does NOT capture (it stops at the segment boundary), so a
|
|
1096
|
+
// concrete model validated by the id its own listing handed out used to fall
|
|
1097
|
+
// through to the router's bare 404 — not even this handler's error shape. The
|
|
1098
|
+
// wildcard covers those; a single-segment id still takes the cheaper param
|
|
1099
|
+
// route.
|
|
1100
|
+
const singleModel = async (req, reply) => {
|
|
1101
|
+
const model = String(req.params['*'] ?? req.params.model ?? '').replace(/^\/+/, '');
|
|
1076
1102
|
const policy = getModelAccessPolicy(req.authInfo?.keyConfig);
|
|
1077
1103
|
const all = registry.getAllModels();
|
|
1078
1104
|
const specific = all.find(m => m.id === model);
|
|
@@ -1123,7 +1149,9 @@ providerConfigs = []) {
|
|
|
1123
1149
|
}
|
|
1124
1150
|
: {}),
|
|
1125
1151
|
};
|
|
1126
|
-
}
|
|
1152
|
+
};
|
|
1153
|
+
app.get('/v1/models/:model', singleModel);
|
|
1154
|
+
app.get('/v1/models/*', singleModel);
|
|
1127
1155
|
// 3. FinOps Economics & Analytics Dashboard
|
|
1128
1156
|
app.get('/api/admin/metrics', async () => {
|
|
1129
1157
|
return orchestrator.getTracker().getStats();
|
|
@@ -293,6 +293,12 @@ export declare class SessionManager {
|
|
|
293
293
|
* hands the same value to the resolver and the trace, so the console's alias
|
|
294
294
|
* and the session it names can never disagree.
|
|
295
295
|
*/
|
|
296
|
+
/**
|
|
297
|
+
* Caller-supplied conversation id. Reads the four explicit header names and
|
|
298
|
+
* nothing else: no mint, no anchor, no session-manager write. Archive code
|
|
299
|
+
* uses this when it needs a file name and the request is not a conversation.
|
|
300
|
+
*/
|
|
301
|
+
static explicitSessionId(headers?: Record<string, string | string[] | undefined>): string | undefined;
|
|
296
302
|
static clientRefOf(request: CanonicalRequest): ClientConversationRef | null;
|
|
297
303
|
/**
|
|
298
304
|
* Seamless Session Resolver — see the class doc for the full priority list.
|
|
@@ -366,6 +366,21 @@ export class SessionManager {
|
|
|
366
366
|
* hands the same value to the resolver and the trace, so the console's alias
|
|
367
367
|
* and the session it names can never disagree.
|
|
368
368
|
*/
|
|
369
|
+
/**
|
|
370
|
+
* Caller-supplied conversation id. Reads the four explicit header names and
|
|
371
|
+
* nothing else: no mint, no anchor, no session-manager write. Archive code
|
|
372
|
+
* uses this when it needs a file name and the request is not a conversation.
|
|
373
|
+
*/
|
|
374
|
+
static explicitSessionId(headers) {
|
|
375
|
+
const raw = headers?.['x-session-id'] ||
|
|
376
|
+
headers?.['x-conversation-id'] ||
|
|
377
|
+
headers?.['session-id'] ||
|
|
378
|
+
headers?.['conversation-id'];
|
|
379
|
+
if (!raw)
|
|
380
|
+
return undefined;
|
|
381
|
+
const id = (Array.isArray(raw) ? raw[0] : raw).trim();
|
|
382
|
+
return id.length > 0 ? id : undefined;
|
|
383
|
+
}
|
|
369
384
|
static clientRefOf(request) {
|
|
370
385
|
return SessionManager.normalizeClientRef(request.routerOptions?.clientRef);
|
|
371
386
|
}
|
|
@@ -383,15 +398,11 @@ export class SessionManager {
|
|
|
383
398
|
return resolved;
|
|
384
399
|
}
|
|
385
400
|
resolveIdentity(request, identity, clientRef) {
|
|
386
|
-
// 1. Explicit
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
if (explicitHeader) {
|
|
392
|
-
const id = Array.isArray(explicitHeader) ? explicitHeader[0] : explicitHeader;
|
|
393
|
-
return { sessionId: id.trim(), lookupType: 'explicit_header' };
|
|
394
|
-
}
|
|
401
|
+
// 1. Explicit header. The same reader names an archive file when a
|
|
402
|
+
// surface is not a conversation and must not mint one.
|
|
403
|
+
const explicit = SessionManager.explicitSessionId(identity?.headers);
|
|
404
|
+
if (explicit)
|
|
405
|
+
return { sessionId: explicit, lookupType: 'explicit_header' };
|
|
395
406
|
if (request.routerOptions?.sessionId) {
|
|
396
407
|
return { sessionId: request.routerOptions.sessionId.trim(), lookupType: 'explicit_header' };
|
|
397
408
|
}
|
|
@@ -22,7 +22,7 @@ import type { ChatSurface, TransportContext } from './types.js';
|
|
|
22
22
|
* trailing `ocr-summary` comment on every chat-shaped wire (chat places it
|
|
23
23
|
* before `[DONE]`; anthropic and responses place it after their terminal
|
|
24
24
|
* event);
|
|
25
|
-
* - a failure after the first byte ends the stream WITHOUT `[DONE]
|
|
26
|
-
*
|
|
25
|
+
* - a failure after the first byte ends the stream WITHOUT `[DONE]`. A wire
|
|
26
|
+
* may send an in-band failure frame; no separate JSON response or retry.
|
|
27
27
|
*/
|
|
28
28
|
export declare function runChatSurface(t: TransportContext, surface: ChatSurface, orchestrator: Pick<PipelineOrchestrator, 'process'>, body: unknown): Promise<void>;
|
|
@@ -36,8 +36,8 @@ function writeSseFrames(t, frames) {
|
|
|
36
36
|
* trailing `ocr-summary` comment on every chat-shaped wire (chat places it
|
|
37
37
|
* before `[DONE]`; anthropic and responses place it after their terminal
|
|
38
38
|
* event);
|
|
39
|
-
* - a failure after the first byte ends the stream WITHOUT `[DONE]
|
|
40
|
-
*
|
|
39
|
+
* - a failure after the first byte ends the stream WITHOUT `[DONE]`. A wire
|
|
40
|
+
* may send an in-band failure frame; no separate JSON response or retry.
|
|
41
41
|
*/
|
|
42
42
|
export async function runChatSurface(t, surface, orchestrator, body) {
|
|
43
43
|
// 1. Parse — protocol-level shape check only; model resolution stays with the orchestrator.
|
|
@@ -180,12 +180,22 @@ export async function runChatSurface(t, surface, orchestrator, body) {
|
|
|
180
180
|
}
|
|
181
181
|
catch (err) {
|
|
182
182
|
if (streamed) {
|
|
183
|
-
//
|
|
184
|
-
//
|
|
185
|
-
// retry would duplicate output the client has partly consumed. Close
|
|
186
|
-
// WITHOUT [DONE] — the standard "stream ended unexpectedly" signal.
|
|
183
|
+
// Headers are committed. Do not send a second HTTP response or retry;
|
|
184
|
+
// report the failure in-band when the wire supports it, then close.
|
|
187
185
|
t.logError(err, 'Streamed chat execution failed after the first byte');
|
|
188
|
-
|
|
186
|
+
try {
|
|
187
|
+
if (surface.streamError && !t.isClientGone()) {
|
|
188
|
+
const frame = surface.streamError(err);
|
|
189
|
+
if (frame !== null)
|
|
190
|
+
await t.writeSseChunk(frame);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
catch (writeErr) {
|
|
194
|
+
t.logError(writeErr, 'Failed to send SSE error frame');
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
t.endSse();
|
|
198
|
+
}
|
|
189
199
|
return;
|
|
190
200
|
}
|
|
191
201
|
// Execution failure: log loudly, map through the surface's own error
|
|
@@ -64,6 +64,21 @@ export const chatSurface = {
|
|
|
64
64
|
const chunk = encoder.encode(event, ctx.model);
|
|
65
65
|
return chunk ? `data: ${JSON.stringify(chunk)}\n\n` : null;
|
|
66
66
|
},
|
|
67
|
+
/**
|
|
68
|
+
* The chat encoder validates every canonical stream event up front
|
|
69
|
+
* (`assertCanonicalStreamEventSupported`) and throws `IrUnsupportedError`
|
|
70
|
+
* mid-turn for anything chat cannot represent — signed/encrypted reasoning
|
|
71
|
+
* (`reasoning_signature`), structured reasoning summaries
|
|
72
|
+
* (`reasoning_summary`), extra candidates, extension items, signed tool
|
|
73
|
+
* calls. That is NOT one code: gating on `reasoning_signature` alone leaves
|
|
74
|
+
* the same silent close (no `[DONE]`, no reason) for its siblings. Answer
|
|
75
|
+
* in-band for every chat-side rejection; anything else keeps the bare close.
|
|
76
|
+
*/
|
|
77
|
+
streamError(err) {
|
|
78
|
+
if (!(err instanceof IrUnsupportedError) || err.protocol !== 'openai-chat')
|
|
79
|
+
return null;
|
|
80
|
+
return `data: ${JSON.stringify(chatErrorBody(err.statusCode, err.message, 'unsupported_parameter'))}\n\n`;
|
|
81
|
+
},
|
|
67
82
|
errorBody(status, message, code) {
|
|
68
83
|
return chatErrorBody(status, message, code);
|
|
69
84
|
},
|