@kingsoftcloud/agentkit-harness-node 0.0.22 → 0.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/engine-view.html +36 -16
- package/dist/index.mjs +86 -3
- package/dist/rpc-entry.mjs +86 -3
- package/package.json +2 -2
- package/src/native/engine-display-wakeup.mjs +25 -0
- package/src/native/engine-server.mjs +6 -1
- package/src/native/model-deepseek-dsml.mjs +86 -0
- package/src/native/model-openai-chat.mjs +10 -3
package/dist/index.mjs
CHANGED
|
@@ -2260,6 +2260,83 @@ function mergeChatUsage(previous, reported, policy, finalized = false) {
|
|
|
2260
2260
|
return { ...previous, ...reported };
|
|
2261
2261
|
}
|
|
2262
2262
|
|
|
2263
|
+
// packages/agentkit-harness-node/src/native/model-deepseek-dsml.mjs
|
|
2264
|
+
var markerHeads = ["<", "</"].flatMap((start) => ["|", "||"].flatMap((left) => ["||", "|"].map((right) => `${start}${left}DSML${right}`)));
|
|
2265
|
+
var maxHeadLength = Math.max(...markerHeads.map((head) => head.length));
|
|
2266
|
+
var tagNames = ["calls", "tool_calls", "function_calls", "invoke", "parameter"];
|
|
2267
|
+
var tagPrefix = /^(calls|tool_calls|function_calls|invoke|parameter)(?=[\t />])/u;
|
|
2268
|
+
function createDeepSeekContentProjector(binding) {
|
|
2269
|
+
if (!/(?:^|[/.:])deepseek[-_.]v(?:3[._-]2|4)(?=$|[._:-])/iu.test(binding?.model ?? "")) {
|
|
2270
|
+
return { push: (text) => text, seal: () => "" };
|
|
2271
|
+
}
|
|
2272
|
+
let pending = "";
|
|
2273
|
+
let hiddenBlock = null;
|
|
2274
|
+
let codeDelimiter = "";
|
|
2275
|
+
function read(text, sealed) {
|
|
2276
|
+
pending += text;
|
|
2277
|
+
let visible = "";
|
|
2278
|
+
while (pending) {
|
|
2279
|
+
const special = pending.search(/[<`~]/u);
|
|
2280
|
+
const length = special < 0 ? pending.length : special;
|
|
2281
|
+
if (length) {
|
|
2282
|
+
if (!hiddenBlock) visible += pending.slice(0, length);
|
|
2283
|
+
pending = pending.slice(length);
|
|
2284
|
+
continue;
|
|
2285
|
+
}
|
|
2286
|
+
if (pending[0] !== "<") {
|
|
2287
|
+
const delimiter = pending.match(/^(?:`+|~+)/u)[0];
|
|
2288
|
+
if (!sealed && delimiter.length === pending.length) break;
|
|
2289
|
+
if (!hiddenBlock) {
|
|
2290
|
+
if (codeDelimiter) {
|
|
2291
|
+
if (delimiter === codeDelimiter || codeDelimiter.length >= 3 && delimiter[0] === codeDelimiter[0] && delimiter.length > codeDelimiter.length) {
|
|
2292
|
+
codeDelimiter = "";
|
|
2293
|
+
}
|
|
2294
|
+
} else if (delimiter[0] === "`" || delimiter.length >= 3) {
|
|
2295
|
+
codeDelimiter = delimiter;
|
|
2296
|
+
}
|
|
2297
|
+
visible += delimiter;
|
|
2298
|
+
}
|
|
2299
|
+
pending = pending.slice(delimiter.length);
|
|
2300
|
+
continue;
|
|
2301
|
+
}
|
|
2302
|
+
if (!codeDelimiter) {
|
|
2303
|
+
const prefix = pending.slice(0, maxHeadLength).replaceAll("\uFF5C", "|");
|
|
2304
|
+
if (markerHeads.some((head2) => head2.startsWith(prefix))) {
|
|
2305
|
+
if (!sealed) break;
|
|
2306
|
+
if (prefix.endsWith("DSML")) {
|
|
2307
|
+
pending = "";
|
|
2308
|
+
break;
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
const head = markerHeads.find((candidate) => prefix.startsWith(candidate));
|
|
2312
|
+
if (head) {
|
|
2313
|
+
const afterHead = pending.slice(head.length);
|
|
2314
|
+
const rest = afterHead.replace(/^[\t ]+/u, "");
|
|
2315
|
+
const tag = rest.match(tagPrefix)?.[1];
|
|
2316
|
+
if (tag) {
|
|
2317
|
+
const end = pending.indexOf(">", head.length);
|
|
2318
|
+
if (end < 0 && !sealed) break;
|
|
2319
|
+
const closing = head.startsWith("</");
|
|
2320
|
+
if (closing && tag === hiddenBlock) hiddenBlock = null;
|
|
2321
|
+
if (!closing && !hiddenBlock && end >= 0 && pending[end - 1] !== "/") hiddenBlock = tag;
|
|
2322
|
+
pending = end < 0 ? "" : pending.slice(end + 1);
|
|
2323
|
+
continue;
|
|
2324
|
+
}
|
|
2325
|
+
if (tagNames.some((name) => name.startsWith(rest))) {
|
|
2326
|
+
if (!sealed) break;
|
|
2327
|
+
pending = "";
|
|
2328
|
+
continue;
|
|
2329
|
+
}
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
if (!hiddenBlock) visible += "<";
|
|
2333
|
+
pending = pending.slice(1);
|
|
2334
|
+
}
|
|
2335
|
+
return visible;
|
|
2336
|
+
}
|
|
2337
|
+
return { push: (text) => read(text, false), seal: () => read("", true) };
|
|
2338
|
+
}
|
|
2339
|
+
|
|
2263
2340
|
// packages/agentkit-harness-node/src/native/model-openai-chat.mjs
|
|
2264
2341
|
import { parseModelJson, stringifyModelJson, modelWireToolCall as modelWireToolCall2, FINAL_SUBMISSION_TOOL as FINAL_SUBMISSION_TOOL3 } from "@kingsoftcloud/agentkit-harness-core";
|
|
2265
2342
|
function strictParseJson(text) {
|
|
@@ -2462,6 +2539,8 @@ function parseOpenAIBuffered(bodyText, request, { captureReplay = false, binding
|
|
|
2462
2539
|
let text = message.content;
|
|
2463
2540
|
if (text == null) text = "";
|
|
2464
2541
|
if (typeof text !== "string") throw new ProtocolError("unsupported assistant content");
|
|
2542
|
+
const dsml = createDeepSeekContentProjector(binding);
|
|
2543
|
+
text = dsml.push(text) + dsml.seal();
|
|
2465
2544
|
text = projectInlineReasoning(text, inlineReasoningMarker(request.profile.inlineReasoningPolicy, binding), request.profile.inlineReasoningPolicy);
|
|
2466
2545
|
let calls;
|
|
2467
2546
|
const rawCalls = Array.isArray(message.tool_calls) && message.tool_calls.length > 0;
|
|
@@ -2585,6 +2664,7 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2585
2664
|
const emitThinking = profile.providerThinkingDisplay ?? profile.providerReplayMode !== "none";
|
|
2586
2665
|
const marker = inlineReasoningMarker(profile.inlineReasoningPolicy, binding);
|
|
2587
2666
|
const projector = createInlineReasoningProjector(marker, profile.inlineReasoningPolicy);
|
|
2667
|
+
const dsml = createDeepSeekContentProjector(binding);
|
|
2588
2668
|
let emitContent;
|
|
2589
2669
|
return {
|
|
2590
2670
|
text: "",
|
|
@@ -2688,8 +2768,9 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2688
2768
|
this.finish = finish;
|
|
2689
2769
|
if (reported) this.usageAtFinish = true;
|
|
2690
2770
|
}
|
|
2691
|
-
|
|
2692
|
-
|
|
2771
|
+
const wireText = dsml.push(content);
|
|
2772
|
+
this.text += wireText;
|
|
2773
|
+
const publicContent = projector.push(wireText);
|
|
2693
2774
|
if (publicContent) emit({ kind: "text", text: publicContent });
|
|
2694
2775
|
if (reasoning && emitThinking) emit({ kind: "thinking", text: reasoning });
|
|
2695
2776
|
try {
|
|
@@ -2739,9 +2820,11 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2739
2820
|
}
|
|
2740
2821
|
},
|
|
2741
2822
|
finalize({ errorKind = null, failureKind = null } = {}) {
|
|
2823
|
+
const wireTail = dsml.seal();
|
|
2824
|
+
this.text += wireTail;
|
|
2742
2825
|
const result = this.rawResult({ errorKind, failureKind });
|
|
2743
2826
|
if (!["provider_retryable_error", "provider_terminal_error"].includes(result.kind)) {
|
|
2744
|
-
const tail = projector.seal();
|
|
2827
|
+
const tail = projector.push(wireTail) + projector.seal();
|
|
2745
2828
|
if (tail) emitContent?.({ kind: "text", text: tail });
|
|
2746
2829
|
}
|
|
2747
2830
|
const text = projectInlineReasoning(result.text, marker, profile.inlineReasoningPolicy);
|
package/dist/rpc-entry.mjs
CHANGED
|
@@ -2271,6 +2271,83 @@ function mergeChatUsage(previous, reported, policy, finalized = false) {
|
|
|
2271
2271
|
return { ...previous, ...reported };
|
|
2272
2272
|
}
|
|
2273
2273
|
|
|
2274
|
+
// packages/agentkit-harness-node/src/native/model-deepseek-dsml.mjs
|
|
2275
|
+
var markerHeads = ["<", "</"].flatMap((start) => ["|", "||"].flatMap((left) => ["||", "|"].map((right) => `${start}${left}DSML${right}`)));
|
|
2276
|
+
var maxHeadLength = Math.max(...markerHeads.map((head) => head.length));
|
|
2277
|
+
var tagNames = ["calls", "tool_calls", "function_calls", "invoke", "parameter"];
|
|
2278
|
+
var tagPrefix = /^(calls|tool_calls|function_calls|invoke|parameter)(?=[\t />])/u;
|
|
2279
|
+
function createDeepSeekContentProjector(binding) {
|
|
2280
|
+
if (!/(?:^|[/.:])deepseek[-_.]v(?:3[._-]2|4)(?=$|[._:-])/iu.test(binding?.model ?? "")) {
|
|
2281
|
+
return { push: (text) => text, seal: () => "" };
|
|
2282
|
+
}
|
|
2283
|
+
let pending = "";
|
|
2284
|
+
let hiddenBlock = null;
|
|
2285
|
+
let codeDelimiter = "";
|
|
2286
|
+
function read(text, sealed) {
|
|
2287
|
+
pending += text;
|
|
2288
|
+
let visible = "";
|
|
2289
|
+
while (pending) {
|
|
2290
|
+
const special = pending.search(/[<`~]/u);
|
|
2291
|
+
const length = special < 0 ? pending.length : special;
|
|
2292
|
+
if (length) {
|
|
2293
|
+
if (!hiddenBlock) visible += pending.slice(0, length);
|
|
2294
|
+
pending = pending.slice(length);
|
|
2295
|
+
continue;
|
|
2296
|
+
}
|
|
2297
|
+
if (pending[0] !== "<") {
|
|
2298
|
+
const delimiter = pending.match(/^(?:`+|~+)/u)[0];
|
|
2299
|
+
if (!sealed && delimiter.length === pending.length) break;
|
|
2300
|
+
if (!hiddenBlock) {
|
|
2301
|
+
if (codeDelimiter) {
|
|
2302
|
+
if (delimiter === codeDelimiter || codeDelimiter.length >= 3 && delimiter[0] === codeDelimiter[0] && delimiter.length > codeDelimiter.length) {
|
|
2303
|
+
codeDelimiter = "";
|
|
2304
|
+
}
|
|
2305
|
+
} else if (delimiter[0] === "`" || delimiter.length >= 3) {
|
|
2306
|
+
codeDelimiter = delimiter;
|
|
2307
|
+
}
|
|
2308
|
+
visible += delimiter;
|
|
2309
|
+
}
|
|
2310
|
+
pending = pending.slice(delimiter.length);
|
|
2311
|
+
continue;
|
|
2312
|
+
}
|
|
2313
|
+
if (!codeDelimiter) {
|
|
2314
|
+
const prefix = pending.slice(0, maxHeadLength).replaceAll("\uFF5C", "|");
|
|
2315
|
+
if (markerHeads.some((head2) => head2.startsWith(prefix))) {
|
|
2316
|
+
if (!sealed) break;
|
|
2317
|
+
if (prefix.endsWith("DSML")) {
|
|
2318
|
+
pending = "";
|
|
2319
|
+
break;
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
const head = markerHeads.find((candidate) => prefix.startsWith(candidate));
|
|
2323
|
+
if (head) {
|
|
2324
|
+
const afterHead = pending.slice(head.length);
|
|
2325
|
+
const rest = afterHead.replace(/^[\t ]+/u, "");
|
|
2326
|
+
const tag = rest.match(tagPrefix)?.[1];
|
|
2327
|
+
if (tag) {
|
|
2328
|
+
const end = pending.indexOf(">", head.length);
|
|
2329
|
+
if (end < 0 && !sealed) break;
|
|
2330
|
+
const closing = head.startsWith("</");
|
|
2331
|
+
if (closing && tag === hiddenBlock) hiddenBlock = null;
|
|
2332
|
+
if (!closing && !hiddenBlock && end >= 0 && pending[end - 1] !== "/") hiddenBlock = tag;
|
|
2333
|
+
pending = end < 0 ? "" : pending.slice(end + 1);
|
|
2334
|
+
continue;
|
|
2335
|
+
}
|
|
2336
|
+
if (tagNames.some((name) => name.startsWith(rest))) {
|
|
2337
|
+
if (!sealed) break;
|
|
2338
|
+
pending = "";
|
|
2339
|
+
continue;
|
|
2340
|
+
}
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
if (!hiddenBlock) visible += "<";
|
|
2344
|
+
pending = pending.slice(1);
|
|
2345
|
+
}
|
|
2346
|
+
return visible;
|
|
2347
|
+
}
|
|
2348
|
+
return { push: (text) => read(text, false), seal: () => read("", true) };
|
|
2349
|
+
}
|
|
2350
|
+
|
|
2274
2351
|
// packages/agentkit-harness-node/src/native/model-openai-chat.mjs
|
|
2275
2352
|
import { parseModelJson, stringifyModelJson, modelWireToolCall as modelWireToolCall2, FINAL_SUBMISSION_TOOL as FINAL_SUBMISSION_TOOL3 } from "@kingsoftcloud/agentkit-harness-core";
|
|
2276
2353
|
function strictParseJson(text) {
|
|
@@ -2473,6 +2550,8 @@ function parseOpenAIBuffered(bodyText, request, { captureReplay = false, binding
|
|
|
2473
2550
|
let text = message.content;
|
|
2474
2551
|
if (text == null) text = "";
|
|
2475
2552
|
if (typeof text !== "string") throw new ProtocolError("unsupported assistant content");
|
|
2553
|
+
const dsml = createDeepSeekContentProjector(binding);
|
|
2554
|
+
text = dsml.push(text) + dsml.seal();
|
|
2476
2555
|
text = projectInlineReasoning(text, inlineReasoningMarker(request.profile.inlineReasoningPolicy, binding), request.profile.inlineReasoningPolicy);
|
|
2477
2556
|
let calls;
|
|
2478
2557
|
const rawCalls = Array.isArray(message.tool_calls) && message.tool_calls.length > 0;
|
|
@@ -2596,6 +2675,7 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2596
2675
|
const emitThinking = profile.providerThinkingDisplay ?? profile.providerReplayMode !== "none";
|
|
2597
2676
|
const marker = inlineReasoningMarker(profile.inlineReasoningPolicy, binding);
|
|
2598
2677
|
const projector = createInlineReasoningProjector(marker, profile.inlineReasoningPolicy);
|
|
2678
|
+
const dsml = createDeepSeekContentProjector(binding);
|
|
2599
2679
|
let emitContent;
|
|
2600
2680
|
return {
|
|
2601
2681
|
text: "",
|
|
@@ -2699,8 +2779,9 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2699
2779
|
this.finish = finish;
|
|
2700
2780
|
if (reported) this.usageAtFinish = true;
|
|
2701
2781
|
}
|
|
2702
|
-
|
|
2703
|
-
|
|
2782
|
+
const wireText = dsml.push(content);
|
|
2783
|
+
this.text += wireText;
|
|
2784
|
+
const publicContent = projector.push(wireText);
|
|
2704
2785
|
if (publicContent) emit({ kind: "text", text: publicContent });
|
|
2705
2786
|
if (reasoning && emitThinking) emit({ kind: "thinking", text: reasoning });
|
|
2706
2787
|
try {
|
|
@@ -2750,9 +2831,11 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2750
2831
|
}
|
|
2751
2832
|
},
|
|
2752
2833
|
finalize({ errorKind = null, failureKind = null } = {}) {
|
|
2834
|
+
const wireTail = dsml.seal();
|
|
2835
|
+
this.text += wireTail;
|
|
2753
2836
|
const result = this.rawResult({ errorKind, failureKind });
|
|
2754
2837
|
if (!["provider_retryable_error", "provider_terminal_error"].includes(result.kind)) {
|
|
2755
|
-
const tail = projector.seal();
|
|
2838
|
+
const tail = projector.push(wireTail) + projector.seal();
|
|
2756
2839
|
if (tail) emitContent?.({ kind: "text", text: tail });
|
|
2757
2840
|
}
|
|
2758
2841
|
const text = projectInlineReasoning(result.text, marker, profile.inlineReasoningPolicy);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kingsoftcloud/agentkit-harness-node",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.24",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Standalone Native Harness Node adapters: a durable SQLite run store, a one-request model HTTP port, and a minimal versioned JSON-RPC 2.0 over NDJSON process entry. Reuses the shared Native adapter implementations and externalizes @kingsoftcloud/agentkit-harness-core; depends on no Studio/DSH code at runtime.",
|
|
@@ -154,7 +154,7 @@
|
|
|
154
154
|
"src/package-integrity.mjs"
|
|
155
155
|
],
|
|
156
156
|
"dependencies": {
|
|
157
|
-
"@kingsoftcloud/agentkit-harness-core": "0.0.
|
|
157
|
+
"@kingsoftcloud/agentkit-harness-core": "0.0.24",
|
|
158
158
|
"@astral-sh/ruff-wasm-nodejs": "0.16.8",
|
|
159
159
|
"parse5": "7.3.0",
|
|
160
160
|
"playwright-core": "1.58.2",
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Ephemeral presentation notification, scoped to a Run. The journal remains
|
|
2
|
+
* authoritative; a timeout still observes durable changes and other processes. */
|
|
3
|
+
export function createDisplayWakeup() {
|
|
4
|
+
const waiting = new Map();
|
|
5
|
+
return {
|
|
6
|
+
notify(runId) { for (const wake of [...waiting.get(runId) ?? []]) wake(); },
|
|
7
|
+
wait(runId, timeoutMs, signal) {
|
|
8
|
+
signal.throwIfAborted();
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
let settled = false;
|
|
11
|
+
const listeners = waiting.get(runId) ?? new Set(); waiting.set(runId, listeners);
|
|
12
|
+
const finish = error => {
|
|
13
|
+
if (settled) return;
|
|
14
|
+
settled = true;
|
|
15
|
+
clearTimeout(timer); signal.removeEventListener('abort', abort); listeners.delete(wake);
|
|
16
|
+
if (!listeners.size) waiting.delete(runId);
|
|
17
|
+
if (error) reject(error); else resolve();
|
|
18
|
+
};
|
|
19
|
+
const wake = () => finish(), abort = () => finish(signal.reason);
|
|
20
|
+
const timer = setTimeout(wake, timeoutMs);
|
|
21
|
+
listeners.add(wake); signal.addEventListener('abort', abort, { once: true });
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -40,6 +40,7 @@ import { ENGINE_HOST_EFFECT, RECEIPT_WAIT_MS, hostReceiptReply, hostReceiptSourc
|
|
|
40
40
|
import { ENGINE_COMMAND_VERIFIER, ENGINE_VERIFICATION } from './engine-verification.mjs';
|
|
41
41
|
import { nativeTodoPlan } from './activity-status.mjs';
|
|
42
42
|
import { nativeModelStopMessage } from './native-terminal-failure.mjs';
|
|
43
|
+
import { createDisplayWakeup } from './engine-display-wakeup.mjs';
|
|
43
44
|
export { ENGINE_VIEW_CONTRACT };
|
|
44
45
|
|
|
45
46
|
export const ENGINE_PROTOCOL = 'agentkit.engine/v1';
|
|
@@ -205,6 +206,7 @@ export async function serveEngine({ input, output, log = () => {}, startup, env
|
|
|
205
206
|
const handlersInFlight = new Set(), lifetime = new AbortController();
|
|
206
207
|
const reportActivity = () => onActivity(handlersInFlight.size > 0 || active.size > 0);
|
|
207
208
|
const liveModelDisplay = new Map();
|
|
209
|
+
const displayWakeup = createDisplayWakeup();
|
|
208
210
|
function captureModelDisplay({ runId, operationKey }, event) {
|
|
209
211
|
if (!['text', 'thinking', 'tool_delta'].includes(event.kind)) return;
|
|
210
212
|
let current = liveModelDisplay.get(runId);
|
|
@@ -215,11 +217,13 @@ export async function serveEngine({ input, output, log = () => {}, startup, env
|
|
|
215
217
|
if (event.kind === 'thinking') {
|
|
216
218
|
current.end += event.text.length;
|
|
217
219
|
current.text = (current.text + event.text).slice(-THINKING_HISTORY_CHARS);
|
|
220
|
+
displayWakeup.notify(runId);
|
|
218
221
|
return;
|
|
219
222
|
}
|
|
220
223
|
if (event.kind === 'text') {
|
|
221
224
|
current.bodyEnd += event.text.length;
|
|
222
225
|
current.body = (current.body + event.text).slice(-THINKING_HISTORY_CHARS);
|
|
226
|
+
displayWakeup.notify(runId);
|
|
223
227
|
return;
|
|
224
228
|
}
|
|
225
229
|
// A draft has no call ID, argument data, or authority to execute.
|
|
@@ -230,6 +234,7 @@ export async function serveEngine({ input, output, log = () => {}, startup, env
|
|
|
230
234
|
if (current.tools.get(index) === label) return;
|
|
231
235
|
current.tools.set(index, label);
|
|
232
236
|
current.draftRevision++;
|
|
237
|
+
displayWakeup.notify(runId);
|
|
233
238
|
}
|
|
234
239
|
let host = null, runtime = null, runtimePromise, closing = false;
|
|
235
240
|
const viewRef = viewStore ? viewStore.archive(engineView()) : null;
|
|
@@ -508,7 +513,7 @@ export async function serveEngine({ input, output, log = () => {}, startup, env
|
|
|
508
513
|
...(body ? { text: body } : {}),
|
|
509
514
|
terminal: runtime.terminal(snapshot), running: active.has(runId) };
|
|
510
515
|
}
|
|
511
|
-
await wait(250,
|
|
516
|
+
await displayWakeup.wait(runId, Math.min(250, Math.max(1, deadline - Date.now())), lifetime.signal);
|
|
512
517
|
}
|
|
513
518
|
},
|
|
514
519
|
async 'run/resume'(params) {
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const markerHeads = ['<', '</'].flatMap(start => ['|', '||'].flatMap(left =>
|
|
2
|
+
['||', '|'].map(right => `${start}${left}DSML${right}`)));
|
|
3
|
+
const maxHeadLength = Math.max(...markerHeads.map(head => head.length));
|
|
4
|
+
const tagNames = ['calls', 'tool_calls', 'function_calls', 'invoke', 'parameter'];
|
|
5
|
+
const tagPrefix = /^(calls|tool_calls|function_calls|invoke|parameter)(?=[\t />])/u;
|
|
6
|
+
|
|
7
|
+
export function createDeepSeekContentProjector(binding) {
|
|
8
|
+
// Gate on the model ID so DeepSeek behind an OpenAI-compatible proxy behaves
|
|
9
|
+
// the same as a direct connection, and other models keep their wire content.
|
|
10
|
+
if (!/(?:^|[/.:])deepseek[-_.]v(?:3[._-]2|4)(?=$|[._:-])/iu.test(binding?.model ?? '')) {
|
|
11
|
+
return { push: text => text, seal: () => '' };
|
|
12
|
+
}
|
|
13
|
+
let pending = '';
|
|
14
|
+
let hiddenBlock = null;
|
|
15
|
+
let codeDelimiter = '';
|
|
16
|
+
|
|
17
|
+
function read(text, sealed) {
|
|
18
|
+
pending += text;
|
|
19
|
+
let visible = '';
|
|
20
|
+
while (pending) {
|
|
21
|
+
const special = pending.search(/[<`~]/u);
|
|
22
|
+
const length = special < 0 ? pending.length : special;
|
|
23
|
+
if (length) {
|
|
24
|
+
if (!hiddenBlock) visible += pending.slice(0, length);
|
|
25
|
+
pending = pending.slice(length);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (pending[0] !== '<') {
|
|
30
|
+
const delimiter = pending.match(/^(?:`+|~+)/u)[0];
|
|
31
|
+
if (!sealed && delimiter.length === pending.length) break;
|
|
32
|
+
if (!hiddenBlock) {
|
|
33
|
+
if (codeDelimiter) {
|
|
34
|
+
if (delimiter === codeDelimiter || codeDelimiter.length >= 3 &&
|
|
35
|
+
delimiter[0] === codeDelimiter[0] && delimiter.length > codeDelimiter.length) {
|
|
36
|
+
codeDelimiter = '';
|
|
37
|
+
}
|
|
38
|
+
} else if (delimiter[0] === '`' || delimiter.length >= 3) {
|
|
39
|
+
codeDelimiter = delimiter;
|
|
40
|
+
}
|
|
41
|
+
visible += delimiter;
|
|
42
|
+
}
|
|
43
|
+
pending = pending.slice(delimiter.length);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!codeDelimiter) {
|
|
48
|
+
const prefix = pending.slice(0, maxHeadLength).replaceAll('|', '|');
|
|
49
|
+
if (markerHeads.some(head => head.startsWith(prefix))) {
|
|
50
|
+
if (!sealed) break;
|
|
51
|
+
if (prefix.endsWith('DSML')) {
|
|
52
|
+
pending = '';
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const head = markerHeads.find(candidate => prefix.startsWith(candidate));
|
|
57
|
+
if (head) {
|
|
58
|
+
const afterHead = pending.slice(head.length);
|
|
59
|
+
const rest = afterHead.replace(/^[\t ]+/u, '');
|
|
60
|
+
const tag = rest.match(tagPrefix)?.[1];
|
|
61
|
+
if (tag) {
|
|
62
|
+
const end = pending.indexOf('>', head.length);
|
|
63
|
+
if (end < 0 && !sealed) break;
|
|
64
|
+
const closing = head.startsWith('</');
|
|
65
|
+
if (closing && tag === hiddenBlock) hiddenBlock = null;
|
|
66
|
+
if (!closing && !hiddenBlock && end >= 0 && pending[end - 1] !== '/') hiddenBlock = tag;
|
|
67
|
+
pending = end < 0 ? '' : pending.slice(end + 1);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (tagNames.some(name => name.startsWith(rest))) {
|
|
71
|
+
if (!sealed) break;
|
|
72
|
+
pending = '';
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (!hiddenBlock) visible += '<';
|
|
78
|
+
pending = pending.slice(1);
|
|
79
|
+
}
|
|
80
|
+
return visible;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Only transport markup is suppressed. Executable calls still come from the
|
|
84
|
+
// provider's structured tool_calls channel and its existing validation path.
|
|
85
|
+
return { push: text => read(text, false), seal: () => read('', true) };
|
|
86
|
+
}
|
|
@@ -17,6 +17,7 @@ import { inlineReasoningMarker, projectInlineReasoning, createInlineReasoningPro
|
|
|
17
17
|
import { applyProviderChatCompatibility, resolveProviderCompatibility } from './model-provider-compat.mjs';
|
|
18
18
|
import { classifyOpenAIProviderError } from './model-provider-error.mjs';
|
|
19
19
|
import { mergeChatUsage } from './model-chat-usage.mjs';
|
|
20
|
+
import { createDeepSeekContentProjector } from './model-deepseek-dsml.mjs';
|
|
20
21
|
// parseModelJson is the kernel's strict finite JSON parser: it rejects
|
|
21
22
|
// duplicate object keys and non-finite numbers (e.g. 1e400). Using it here keeps
|
|
22
23
|
// provider-envelope and tool-argument decoding consistent with the kernel's
|
|
@@ -240,6 +241,8 @@ export function parseOpenAIBuffered(bodyText, request, { captureReplay = false,
|
|
|
240
241
|
let text = message.content;
|
|
241
242
|
if (text == null) text = '';
|
|
242
243
|
if (typeof text !== 'string') throw new ProtocolError('unsupported assistant content');
|
|
244
|
+
const dsml = createDeepSeekContentProjector(binding);
|
|
245
|
+
text = dsml.push(text) + dsml.seal();
|
|
243
246
|
text = projectInlineReasoning(text, inlineReasoningMarker(request.profile.inlineReasoningPolicy, binding), request.profile.inlineReasoningPolicy);
|
|
244
247
|
let calls;
|
|
245
248
|
// tool_calls truthiness mirrors Python: an empty array carries no calls.
|
|
@@ -354,6 +357,7 @@ export function createOpenAIStreamTurn(profile, binding) {
|
|
|
354
357
|
const emitThinking = profile.providerThinkingDisplay ?? profile.providerReplayMode !== 'none';
|
|
355
358
|
const marker = inlineReasoningMarker(profile.inlineReasoningPolicy, binding);
|
|
356
359
|
const projector = createInlineReasoningProjector(marker, profile.inlineReasoningPolicy);
|
|
360
|
+
const dsml = createDeepSeekContentProjector(binding);
|
|
357
361
|
let emitContent;
|
|
358
362
|
return {
|
|
359
363
|
text: '',
|
|
@@ -444,8 +448,9 @@ export function createOpenAIStreamTurn(profile, binding) {
|
|
|
444
448
|
this.finish = finish;
|
|
445
449
|
if (reported) this.usageAtFinish = true;
|
|
446
450
|
}
|
|
447
|
-
|
|
448
|
-
|
|
451
|
+
const wireText = dsml.push(content);
|
|
452
|
+
this.text += wireText;
|
|
453
|
+
const publicContent = projector.push(wireText);
|
|
449
454
|
if (publicContent) emit({ kind: 'text', text: publicContent });
|
|
450
455
|
if (reasoning && emitThinking) emit({ kind: 'thinking', text: reasoning });
|
|
451
456
|
try {
|
|
@@ -499,9 +504,11 @@ export function createOpenAIStreamTurn(profile, binding) {
|
|
|
499
504
|
},
|
|
500
505
|
|
|
501
506
|
finalize({ errorKind = null, failureKind = null } = {}) {
|
|
507
|
+
const wireTail = dsml.seal();
|
|
508
|
+
this.text += wireTail;
|
|
502
509
|
const result = this.rawResult({ errorKind, failureKind });
|
|
503
510
|
if (!['provider_retryable_error', 'provider_terminal_error'].includes(result.kind)) {
|
|
504
|
-
const tail = projector.seal();
|
|
511
|
+
const tail = projector.push(wireTail) + projector.seal();
|
|
505
512
|
if (tail) emitContent?.({ kind: 'text', text: tail });
|
|
506
513
|
}
|
|
507
514
|
const text = projectInlineReasoning(result.text, marker, profile.inlineReasoningPolicy);
|