@botlearn-course/daemon 0.0.24 → 0.0.25
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/agent-service-sandbox.d.ts +1 -7
- package/dist/agent-service-sandbox.js +10 -75
- package/dist/runtime-prompt.d.ts +9 -0
- package/dist/runtime-prompt.js +23 -0
- package/dist/runtimes/deepseek-tui.d.ts +1 -0
- package/dist/runtimes/deepseek-tui.js +27 -2
- package/dist/runtimes/engine.d.ts +2 -0
- package/dist/runtimes/engine.js +7 -2
- package/dist/types.d.ts +2 -0
- package/dist/types.js +0 -6
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@ import { type Logger } from "./log.js";
|
|
|
2
2
|
import type { WorkspaceScanMeasurement } from "./file-candidates.js";
|
|
3
3
|
import { type RuntimeSkillProviderFactory } from "./runtime-skills.js";
|
|
4
4
|
import { type PersistentSessionExecution, type PreparedPersistentTurn, type RunReportingClient } from "./run-dispatcher.js";
|
|
5
|
-
import type { CourseRuntime,
|
|
5
|
+
import type { CourseRuntime, RunEvent, RunEventReceipt, RunFileCandidate, RunFileRecord, RunStartPayload } from "./types.js";
|
|
6
6
|
export declare function workspaceDurabilityCapabilities(): string[];
|
|
7
7
|
export interface AgentServiceSandboxOptions {
|
|
8
8
|
wsUrl: string;
|
|
@@ -34,10 +34,6 @@ export declare class AgentServiceSandboxClient implements RunReportingClient, Pe
|
|
|
34
34
|
private readonly dispatcher;
|
|
35
35
|
/** agent_run_id → TURN scope(session/attempt/activation),事件与文件帧路由用。 */
|
|
36
36
|
private readonly turnScopes;
|
|
37
|
-
/** 每 run 内嵌 profile(turn payload 自带时优先)。 */
|
|
38
|
-
private readonly runProfiles;
|
|
39
|
-
/** activate 时装配的 per-session profile,turn.start 复用。 */
|
|
40
|
-
private readonly sessionProfiles;
|
|
41
37
|
/** activation-scoped 模型短凭据;只保存在内存中,绝不进入 state.json。 */
|
|
42
38
|
private readonly activationContexts;
|
|
43
39
|
private readonly activationCleanupTasks;
|
|
@@ -84,7 +80,6 @@ export declare class AgentServiceSandboxClient implements RunReportingClient, Pe
|
|
|
84
80
|
postEvent(agentRunId: string, event: RunEvent): Promise<RunEventReceipt>;
|
|
85
81
|
postFile(agentRunId: string, file: RunFileCandidate): Promise<RunFileRecord>;
|
|
86
82
|
recordWorkspaceScanMeasurement(agentRunId: string, measurement: WorkspaceScanMeasurement): Promise<void>;
|
|
87
|
-
getRunRuntimeProfile(agentRunId: string): Promise<CourseRuntimeProfile>;
|
|
88
83
|
private connectOnce;
|
|
89
84
|
private handleHello;
|
|
90
85
|
/**
|
|
@@ -110,7 +105,6 @@ export declare class AgentServiceSandboxClient implements RunReportingClient, Pe
|
|
|
110
105
|
private closeSession;
|
|
111
106
|
private reconcileInterruptedCommand;
|
|
112
107
|
private turnContextRevision;
|
|
113
|
-
private activationInstructions;
|
|
114
108
|
private validTurnPayload;
|
|
115
109
|
private scheduleLifecycle;
|
|
116
110
|
private currentLifecycleFence;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RUNTIME_PROMPT_CAPABILITY, validateRuntimePrompt } from "./runtime-prompt.js";
|
|
1
2
|
import { randomUUID } from "node:crypto";
|
|
2
3
|
import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
|
|
3
4
|
import path from "node:path";
|
|
@@ -347,10 +348,6 @@ export class AgentServiceSandboxClient {
|
|
|
347
348
|
dispatcher;
|
|
348
349
|
/** agent_run_id → TURN scope(session/attempt/activation),事件与文件帧路由用。 */
|
|
349
350
|
turnScopes = new Map();
|
|
350
|
-
/** 每 run 内嵌 profile(turn payload 自带时优先)。 */
|
|
351
|
-
runProfiles = new Map();
|
|
352
|
-
/** activate 时装配的 per-session profile,turn.start 复用。 */
|
|
353
|
-
sessionProfiles = new Map();
|
|
354
351
|
/** activation-scoped 模型短凭据;只保存在内存中,绝不进入 state.json。 */
|
|
355
352
|
activationContexts = new Map();
|
|
356
353
|
activationCleanupTasks = new Map();
|
|
@@ -420,12 +417,7 @@ export class AgentServiceSandboxClient {
|
|
|
420
417
|
delete runtimeEnv.DEEPSEEK_BASE_URL;
|
|
421
418
|
Object.assign(runtimeEnv, activation.runtimeEnv);
|
|
422
419
|
runtimeEnv.BOTLEARN_AGENT_SERVICE_ACTIVATION_ID = scope.activationId;
|
|
423
|
-
payload.
|
|
424
|
-
...activation.instructions,
|
|
425
|
-
...(Array.isArray(payload.context.instructions)
|
|
426
|
-
? payload.context.instructions.filter((item) => typeof item === "string")
|
|
427
|
-
: []),
|
|
428
|
-
];
|
|
420
|
+
validateRuntimePrompt(payload.runtime_prompt, activation.skillProvider?.catalog.map((entry) => entry.ref) ?? []);
|
|
429
421
|
return {
|
|
430
422
|
workspaceDir: prepared.workspaceDir,
|
|
431
423
|
transcriptFile: prepared.transcriptFile,
|
|
@@ -504,7 +496,6 @@ export class AgentServiceSandboxClient {
|
|
|
504
496
|
const activation = this.activationContexts.get(sessionId);
|
|
505
497
|
if (activation?.activationId === pending.activationId) {
|
|
506
498
|
this.activationContexts.delete(sessionId);
|
|
507
|
-
this.sessionProfiles.delete(sessionId);
|
|
508
499
|
}
|
|
509
500
|
if (!session.completedActivations.includes(pending.activationId)) {
|
|
510
501
|
// Session-close/generation-rotate are the retention bounds. Never evict an
|
|
@@ -604,7 +595,6 @@ export class AgentServiceSandboxClient {
|
|
|
604
595
|
revokeRuntimeSessionWorkspace(sessionId, this.sandboxGeneration, session.workspaceId ?? sessionId);
|
|
605
596
|
}
|
|
606
597
|
this.activationContexts.clear();
|
|
607
|
-
this.sessionProfiles.clear();
|
|
608
598
|
this.activeSessionId = null;
|
|
609
599
|
this.currentTurnSessionId = null;
|
|
610
600
|
this.persist();
|
|
@@ -744,16 +734,6 @@ export class AgentServiceSandboxClient {
|
|
|
744
734
|
},
|
|
745
735
|
}));
|
|
746
736
|
}
|
|
747
|
-
async getRunRuntimeProfile(agentRunId) {
|
|
748
|
-
const embedded = this.runProfiles.get(agentRunId);
|
|
749
|
-
if (embedded)
|
|
750
|
-
return embedded;
|
|
751
|
-
const scope = this.turnScopes.get(agentRunId);
|
|
752
|
-
const activated = scope ? this.sessionProfiles.get(scope.sessionId) : undefined;
|
|
753
|
-
if (!activated)
|
|
754
|
-
throw new Error("Agent Service sandbox runtime profile is unavailable");
|
|
755
|
-
return activated;
|
|
756
|
-
}
|
|
757
737
|
async connectOnce() {
|
|
758
738
|
const socket = new WebSocketClient(this.options.wsUrl, AGENT_SERVICE_WS_SUBPROTOCOL, {
|
|
759
739
|
headers: { Authorization: `Bearer ${this.state.reconnectToken}` },
|
|
@@ -852,9 +832,7 @@ export class AgentServiceSandboxClient {
|
|
|
852
832
|
}
|
|
853
833
|
this.state.sessions = {};
|
|
854
834
|
this.state.sandboxGeneration = frame.sandbox_generation;
|
|
855
|
-
this.sessionProfiles.clear();
|
|
856
835
|
this.activationContexts.clear();
|
|
857
|
-
this.runProfiles.clear();
|
|
858
836
|
this.turnScopes.clear();
|
|
859
837
|
this.activeSessionId = null;
|
|
860
838
|
this.currentTurnSessionId = null;
|
|
@@ -883,6 +861,7 @@ export class AgentServiceSandboxClient {
|
|
|
883
861
|
protocol_versions: [AGENT_SERVICE_WS_SCHEMA],
|
|
884
862
|
capabilities: [
|
|
885
863
|
WORKSPACE_FILE_READ_BINARY_CAPABILITY,
|
|
864
|
+
RUNTIME_PROMPT_CAPABILITY,
|
|
886
865
|
...workspaceDurabilityCapabilities(),
|
|
887
866
|
],
|
|
888
867
|
daemon_version: this.options.daemonVersion,
|
|
@@ -1201,14 +1180,6 @@ export class AgentServiceSandboxClient {
|
|
|
1201
1180
|
await this.sendCommandAck(frame, "rejected", error instanceof Error ? error.message : "invalid_input_attachment_grant");
|
|
1202
1181
|
return;
|
|
1203
1182
|
}
|
|
1204
|
-
let instructions;
|
|
1205
|
-
try {
|
|
1206
|
-
instructions = this.activationInstructions(frame.payload.instructions);
|
|
1207
|
-
}
|
|
1208
|
-
catch (error) {
|
|
1209
|
-
await this.sendCommandAck(frame, "rejected", error instanceof Error ? error.message : "invalid_instructions");
|
|
1210
|
-
return;
|
|
1211
|
-
}
|
|
1212
1183
|
const rawSkillProvider = frame.payload.skill_provider;
|
|
1213
1184
|
const hasSkillProvider = rawSkillProvider !== undefined && rawSkillProvider !== null;
|
|
1214
1185
|
if (hasSkillProvider && !runtimeSupportsCourseSkills(runtimeId)) {
|
|
@@ -1309,13 +1280,6 @@ export class AgentServiceSandboxClient {
|
|
|
1309
1280
|
return;
|
|
1310
1281
|
}
|
|
1311
1282
|
}
|
|
1312
|
-
const profile = frame.payload.profile;
|
|
1313
|
-
if (profile && typeof profile === "object" && !Array.isArray(profile)) {
|
|
1314
|
-
this.sessionProfiles.set(sessionId, profile);
|
|
1315
|
-
}
|
|
1316
|
-
else if (profile === null) {
|
|
1317
|
-
this.sessionProfiles.delete(sessionId);
|
|
1318
|
-
}
|
|
1319
1283
|
session.runtimeId = runtimeId;
|
|
1320
1284
|
session.contextRevision = contextRevision;
|
|
1321
1285
|
session.activationId = activationId;
|
|
@@ -1323,7 +1287,6 @@ export class AgentServiceSandboxClient {
|
|
|
1323
1287
|
this.activationContexts.set(sessionId, {
|
|
1324
1288
|
activationId,
|
|
1325
1289
|
runtimeEnv,
|
|
1326
|
-
instructions,
|
|
1327
1290
|
inputAttachmentGrant: attachmentGrant,
|
|
1328
1291
|
...(skillProvider ? { skillProvider } : {}),
|
|
1329
1292
|
...(skillGrantKey ? { skillGrantKey } : {}),
|
|
@@ -1375,20 +1338,19 @@ export class AgentServiceSandboxClient {
|
|
|
1375
1338
|
await this.sendCommandAck(frame, "rejected", "context_revision_mismatch");
|
|
1376
1339
|
return;
|
|
1377
1340
|
}
|
|
1378
|
-
const activatedProfile = this.sessionProfiles.get(sessionId);
|
|
1379
|
-
const expectedProfileHash = payload.context?.profileHash;
|
|
1380
|
-
if (typeof expectedProfileHash === "string" &&
|
|
1381
|
-
activatedProfile &&
|
|
1382
|
-
activatedProfile.profileHash !== expectedProfileHash) {
|
|
1383
|
-
await this.sendCommandAck(frame, "rejected", "profile_hash_mismatch");
|
|
1384
|
-
return;
|
|
1385
|
-
}
|
|
1386
1341
|
const scope = { sessionId, workerAttempt: attempt, activationId };
|
|
1387
1342
|
const existingScope = this.turnScopes.get(runId);
|
|
1388
1343
|
if (existingScope && !this.sameTurnScope(existingScope, scope)) {
|
|
1389
1344
|
await this.sendCommandAck(frame, "rejected", "turn_scope_mismatch");
|
|
1390
1345
|
return;
|
|
1391
1346
|
}
|
|
1347
|
+
try {
|
|
1348
|
+
validateRuntimePrompt(payload.runtime_prompt, activation.skillProvider?.catalog.map((entry) => entry.ref) ?? []);
|
|
1349
|
+
}
|
|
1350
|
+
catch (error) {
|
|
1351
|
+
await this.sendCommandAck(frame, "rejected", error instanceof Error ? error.message : "runtime_prompt_invalid");
|
|
1352
|
+
return;
|
|
1353
|
+
}
|
|
1392
1354
|
const commandId = `run:${runId}:${attempt}`;
|
|
1393
1355
|
if (session.completedCommands.includes(commandId) || this.inflightCommands.has(commandId)) {
|
|
1394
1356
|
await this.sendCommandAck(frame, "ok");
|
|
@@ -1411,10 +1373,6 @@ export class AgentServiceSandboxClient {
|
|
|
1411
1373
|
};
|
|
1412
1374
|
this.persist();
|
|
1413
1375
|
}
|
|
1414
|
-
const embeddedProfile = payload.context?.runtimeProfile;
|
|
1415
|
-
if (embeddedProfile && typeof embeddedProfile === "object") {
|
|
1416
|
-
this.runProfiles.set(runId, embeddedProfile);
|
|
1417
|
-
}
|
|
1418
1376
|
this.inflightCommands.add(commandId);
|
|
1419
1377
|
await this.sendCommandAck(frame, "ok");
|
|
1420
1378
|
void this.dispatcher.dispatch(payload)
|
|
@@ -1436,7 +1394,6 @@ export class AgentServiceSandboxClient {
|
|
|
1436
1394
|
})
|
|
1437
1395
|
.finally(() => {
|
|
1438
1396
|
this.inflightCommands.delete(commandId);
|
|
1439
|
-
this.runProfiles.delete(runId);
|
|
1440
1397
|
});
|
|
1441
1398
|
}
|
|
1442
1399
|
async handleTurnCancel(frame) {
|
|
@@ -1500,7 +1457,6 @@ export class AgentServiceSandboxClient {
|
|
|
1500
1457
|
this.resolvePendingEventAcksForSession(sessionId);
|
|
1501
1458
|
this.rejectPendingFilesForSession(sessionId, new Error("Agent Service runtime session was closed"));
|
|
1502
1459
|
this.activationContexts.delete(sessionId);
|
|
1503
|
-
this.sessionProfiles.delete(sessionId);
|
|
1504
1460
|
for (const runId of runIds)
|
|
1505
1461
|
this.dispatcher.cancel(runId);
|
|
1506
1462
|
if (!await this.dispatcher.waitForRuns(runIds, 10_000)) {
|
|
@@ -1523,7 +1479,6 @@ export class AgentServiceSandboxClient {
|
|
|
1523
1479
|
this.rejectPendingFilesForSession(sessionId, new Error("Agent Service runtime session was closed"));
|
|
1524
1480
|
for (const runId of runIds) {
|
|
1525
1481
|
this.turnScopes.delete(runId);
|
|
1526
|
-
this.runProfiles.delete(runId);
|
|
1527
1482
|
}
|
|
1528
1483
|
removeRuntimeSessionWorkspace(sessionId, this.sandboxGeneration, true, workspaceId);
|
|
1529
1484
|
delete this.state.sessions[sessionId];
|
|
@@ -1585,25 +1540,6 @@ export class AgentServiceSandboxClient {
|
|
|
1585
1540
|
}
|
|
1586
1541
|
return session.contextRevision;
|
|
1587
1542
|
}
|
|
1588
|
-
activationInstructions(value) {
|
|
1589
|
-
if (value === undefined || value === null)
|
|
1590
|
-
return [];
|
|
1591
|
-
if (!Array.isArray(value) || value.length > 64) {
|
|
1592
|
-
throw new Error("invalid_activation_instructions");
|
|
1593
|
-
}
|
|
1594
|
-
const instructions = [];
|
|
1595
|
-
let totalBytes = 0;
|
|
1596
|
-
for (const item of value) {
|
|
1597
|
-
if (typeof item !== "string" || item.length < 1) {
|
|
1598
|
-
throw new Error("invalid_activation_instructions");
|
|
1599
|
-
}
|
|
1600
|
-
totalBytes += Buffer.byteLength(item, "utf8");
|
|
1601
|
-
if (totalBytes > 100_000)
|
|
1602
|
-
throw new Error("activation_instructions_too_large");
|
|
1603
|
-
instructions.push(item);
|
|
1604
|
-
}
|
|
1605
|
-
return instructions;
|
|
1606
|
-
}
|
|
1607
1543
|
validTurnPayload(payload) {
|
|
1608
1544
|
return Boolean(payload &&
|
|
1609
1545
|
typeof payload === "object" &&
|
|
@@ -1687,7 +1623,6 @@ export class AgentServiceSandboxClient {
|
|
|
1687
1623
|
return true;
|
|
1688
1624
|
const runIds = this.runIdsForSession(sessionId);
|
|
1689
1625
|
this.activationContexts.delete(sessionId);
|
|
1690
|
-
this.sessionProfiles.delete(sessionId);
|
|
1691
1626
|
if (terminateWriters) {
|
|
1692
1627
|
revokeRuntimeSessionWorkspace(sessionId, this.sandboxGeneration, session.workspaceId ?? sessionId);
|
|
1693
1628
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const RUNTIME_PROMPT_CAPABILITY = "runtime_prompt_v1";
|
|
2
|
+
/** Server-owned model instructions. Audit metadata never becomes model text. */
|
|
3
|
+
export interface RuntimePrompt {
|
|
4
|
+
schemaVersion: "botlearn-runtime-prompt/0.1";
|
|
5
|
+
systemPrompt: string;
|
|
6
|
+
digest: string;
|
|
7
|
+
skillRefs: string[];
|
|
8
|
+
}
|
|
9
|
+
export declare function validateRuntimePrompt(value: unknown, expectedSkillRefs?: string[]): RuntimePrompt;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
export const RUNTIME_PROMPT_CAPABILITY = "runtime_prompt_v1";
|
|
3
|
+
export function validateRuntimePrompt(value, expectedSkillRefs) {
|
|
4
|
+
if (!value || typeof value !== "object")
|
|
5
|
+
throw new Error("runtime_prompt_missing");
|
|
6
|
+
const item = value;
|
|
7
|
+
if (Object.keys(item).sort().join(",") !== "digest,schemaVersion,skillRefs,systemPrompt"
|
|
8
|
+
|| item.schemaVersion !== "botlearn-runtime-prompt/0.1"
|
|
9
|
+
|| typeof item.systemPrompt !== "string"
|
|
10
|
+
|| !item.systemPrompt.trim()
|
|
11
|
+
|| Buffer.byteLength(item.systemPrompt) > 100_000
|
|
12
|
+
|| !Array.isArray(item.skillRefs)
|
|
13
|
+
|| item.skillRefs.some((ref) => typeof ref !== "string" || !ref)
|
|
14
|
+
|| new Set(item.skillRefs).size !== item.skillRefs.length)
|
|
15
|
+
throw new Error("runtime_prompt_invalid");
|
|
16
|
+
const digest = `sha256:${createHash("sha256").update(item.systemPrompt).digest("hex")}`;
|
|
17
|
+
if (item.digest !== digest)
|
|
18
|
+
throw new Error("runtime_prompt_digest_mismatch");
|
|
19
|
+
if (expectedSkillRefs && [...item.skillRefs].sort().join("\n") !== [...expectedSkillRefs].sort().join("\n")) {
|
|
20
|
+
throw new Error("runtime_prompt_skill_set_mismatch");
|
|
21
|
+
}
|
|
22
|
+
return item;
|
|
23
|
+
}
|
|
@@ -50,6 +50,7 @@ export declare class DeepseekTuiAdapter implements EngineAdapter {
|
|
|
50
50
|
private clearCompactionRequired;
|
|
51
51
|
private managedActivationId;
|
|
52
52
|
private createThread;
|
|
53
|
+
private logAppliedServerPrompt;
|
|
53
54
|
private patchThreadSettings;
|
|
54
55
|
private compactThread;
|
|
55
56
|
private runtimeSystemContext;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
1
2
|
import { spawn } from "node:child_process";
|
|
2
3
|
import { chmodSync, existsSync, lstatSync, mkdirSync, realpathSync, rmSync, writeFileSync, } from "node:fs";
|
|
3
4
|
import path from "node:path";
|
|
@@ -212,6 +213,9 @@ export class DeepseekTuiAdapter {
|
|
|
212
213
|
error: "deepseek-tui aborted before start",
|
|
213
214
|
};
|
|
214
215
|
}
|
|
216
|
+
if (this.managedActivationId(opts) && opts.systemPrompt === undefined) {
|
|
217
|
+
return { text: "", newSessionId: opts.sessionId ?? "", error: "runtime_prompt_missing" };
|
|
218
|
+
}
|
|
215
219
|
const turnAbort = new AbortController();
|
|
216
220
|
const onAbort = () => turnAbort.abort();
|
|
217
221
|
opts.signal.addEventListener("abort", onAbort, { once: true });
|
|
@@ -290,7 +294,8 @@ export class DeepseekTuiAdapter {
|
|
|
290
294
|
}
|
|
291
295
|
else {
|
|
292
296
|
const selection = parseDeepseekRuntimeSelection(opts.extraArgs);
|
|
293
|
-
if (opts.
|
|
297
|
+
if (opts.systemPrompt !== undefined || opts.systemContext !== undefined
|
|
298
|
+
|| opts.skillProvider || selection.model) {
|
|
294
299
|
await this.patchThreadSettings(handle.baseUrl, headers, threadId, opts, selection.model, turnAbort.signal);
|
|
295
300
|
}
|
|
296
301
|
if (managedActivationId && this.compactionRequired(opts)) {
|
|
@@ -355,6 +360,9 @@ export class DeepseekTuiAdapter {
|
|
|
355
360
|
? classifyEmptyCompletion(runResult.completion, usage)
|
|
356
361
|
: undefined;
|
|
357
362
|
const error = runResult.error ?? (emptyCompletion ? emptyCompletionError(emptyCompletion.errorCode, handle.stderrTail) : undefined);
|
|
363
|
+
const providerErrorCode = runResult.error
|
|
364
|
+
? classifyDeepseekProviderError(runResult.error)
|
|
365
|
+
: undefined;
|
|
358
366
|
return {
|
|
359
367
|
text,
|
|
360
368
|
// The interrupted provider thread contains a tool call without a tool result. Rebuild
|
|
@@ -372,6 +380,7 @@ export class DeepseekTuiAdapter {
|
|
|
372
380
|
...(error
|
|
373
381
|
? {
|
|
374
382
|
runtimeFailure: {
|
|
383
|
+
...(providerErrorCode ? { error_code: providerErrorCode } : {}),
|
|
375
384
|
...(runResult.userInputHandoff
|
|
376
385
|
? { error_code: "runtime_interactive_tool_handoff_failed" }
|
|
377
386
|
: {}),
|
|
@@ -650,11 +659,19 @@ export class DeepseekTuiAdapter {
|
|
|
650
659
|
const id = stringField(res, "id") ?? stringField(res, "thread_id");
|
|
651
660
|
if (!id)
|
|
652
661
|
throw new Error("create thread response missing id");
|
|
662
|
+
this.logAppliedServerPrompt(opts.systemPrompt);
|
|
653
663
|
return id;
|
|
654
664
|
}
|
|
665
|
+
logAppliedServerPrompt(prompt) {
|
|
666
|
+
if (prompt === undefined)
|
|
667
|
+
return;
|
|
668
|
+
log.info("deepseek-tui.system-prompt-applied", {
|
|
669
|
+
digest: `sha256:${createHash("sha256").update(prompt).digest("hex")}`,
|
|
670
|
+
});
|
|
671
|
+
}
|
|
655
672
|
async patchThreadSettings(baseUrl, headers, threadId, opts, model, signal) {
|
|
656
673
|
const body = {};
|
|
657
|
-
if (opts.systemContext !== undefined || opts.skillProvider) {
|
|
674
|
+
if (opts.systemPrompt !== undefined || opts.systemContext !== undefined || opts.skillProvider) {
|
|
658
675
|
body.system_prompt = this.runtimeSystemContext(opts) ?? "";
|
|
659
676
|
}
|
|
660
677
|
if (model)
|
|
@@ -665,6 +682,7 @@ export class DeepseekTuiAdapter {
|
|
|
665
682
|
body: JSON.stringify(body),
|
|
666
683
|
signal,
|
|
667
684
|
});
|
|
685
|
+
this.logAppliedServerPrompt(opts.systemPrompt);
|
|
668
686
|
}
|
|
669
687
|
async compactThread(args) {
|
|
670
688
|
const { baseUrl, headers, threadId, opts, signal, handle } = args;
|
|
@@ -735,6 +753,8 @@ export class DeepseekTuiAdapter {
|
|
|
735
753
|
}
|
|
736
754
|
}
|
|
737
755
|
runtimeSystemContext(opts) {
|
|
756
|
+
if (opts.systemPrompt !== undefined)
|
|
757
|
+
return opts.systemPrompt;
|
|
738
758
|
let context = this.progressPromptInjectionEnabled
|
|
739
759
|
? progressSystemContext(opts.systemContext)
|
|
740
760
|
: opts.systemContext;
|
|
@@ -1450,6 +1470,11 @@ function extractDeepseekError(eventName, payload) {
|
|
|
1450
1470
|
}
|
|
1451
1471
|
return undefined;
|
|
1452
1472
|
}
|
|
1473
|
+
function classifyDeepseekProviderError(error) {
|
|
1474
|
+
return /"code"\s*:\s*"insufficient_user_quota"/.test(error)
|
|
1475
|
+
? "model_token_quota_exhausted"
|
|
1476
|
+
: undefined;
|
|
1477
|
+
}
|
|
1453
1478
|
function parseSseFrame(raw) {
|
|
1454
1479
|
let event = "message";
|
|
1455
1480
|
const dataLines = [];
|
|
@@ -47,6 +47,8 @@ export interface EngineRunOptions {
|
|
|
47
47
|
signal: AbortSignal;
|
|
48
48
|
extraArgs?: string[];
|
|
49
49
|
systemContext?: string;
|
|
50
|
+
/** Final backend-owned prompt for managed execution; applied verbatim. */
|
|
51
|
+
systemPrompt?: string;
|
|
50
52
|
onBlock?: (block: StreamBlock) => void;
|
|
51
53
|
onStatus?: (event: RuntimeStatusEvent) => void;
|
|
52
54
|
env?: NodeJS.ProcessEnv;
|
package/dist/runtimes/engine.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { validateRuntimePrompt } from "../runtime-prompt.js";
|
|
1
2
|
import { RuntimeExecutionError, } from "../types.js";
|
|
2
3
|
function activeTaskText(payload) {
|
|
3
4
|
const activeTask = payload.context.activeTask;
|
|
@@ -163,8 +164,11 @@ export function wrapEngineAdapter(id, engine, opts) {
|
|
|
163
164
|
if (!text.trim()) {
|
|
164
165
|
throw new RuntimeExecutionError("empty task brief");
|
|
165
166
|
}
|
|
166
|
-
const
|
|
167
|
-
|
|
167
|
+
const serverPrompt = payload.runtime_prompt === undefined
|
|
168
|
+
? undefined : validateRuntimePrompt(payload.runtime_prompt).systemPrompt;
|
|
169
|
+
const instructions = serverPrompt === undefined ? payload.context.instructions : undefined;
|
|
170
|
+
const activeTaskInstruction = serverPrompt === undefined
|
|
171
|
+
? renderActiveTaskInstruction(payload) : undefined;
|
|
168
172
|
const systemInstructions = [
|
|
169
173
|
...(instructions && instructions.length > 0 ? instructions : []),
|
|
170
174
|
...(activeTaskInstruction ? [activeTaskInstruction] : []),
|
|
@@ -203,6 +207,7 @@ export function wrapEngineAdapter(id, engine, opts) {
|
|
|
203
207
|
: {}),
|
|
204
208
|
...(extraArgs.length > 0 ? { extraArgs } : {}),
|
|
205
209
|
...(systemContext !== undefined ? { systemContext } : {}),
|
|
210
|
+
...(serverPrompt !== undefined ? { systemPrompt: serverPrompt, systemContext: serverPrompt } : {}),
|
|
206
211
|
onBlock: (block) => {
|
|
207
212
|
if (block.kind === "progress") {
|
|
208
213
|
queueBlock({
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { RuntimePrompt } from "./runtime-prompt.js";
|
|
1
2
|
/**
|
|
2
3
|
* Course-native 协议与 runtime 契约(spec: docs/specs/lightweight-course-daemon-package.md)。
|
|
3
4
|
*
|
|
@@ -18,6 +19,7 @@ export interface MaterializedInputAttachment extends RunInputAttachment {
|
|
|
18
19
|
}
|
|
19
20
|
/** `GET /daemon/runs/next` 下发的 run.start 载荷(snake_case,与后端 RunStartPayloadOut 一致)。 */
|
|
20
21
|
export interface RunStartPayload {
|
|
22
|
+
runtime_prompt?: RuntimePrompt;
|
|
21
23
|
agent_run_id: string;
|
|
22
24
|
trace_id?: string;
|
|
23
25
|
course_run_id: string;
|
package/dist/types.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Course-native 协议与 runtime 契约(spec: docs/specs/lightweight-course-daemon-package.md)。
|
|
3
|
-
*
|
|
4
|
-
* 本包不依赖 BotCord Hub/room/owner-chat 语义;wire 类型与后端
|
|
5
|
-
* `services/course-api/botlearn_course/schemas.py` 的 daemon 契约严格对齐。
|
|
6
|
-
*/
|
|
7
1
|
/** runtime 执行失败(dispatcher 折叠为 run.failed)。 */
|
|
8
2
|
export class RuntimeExecutionError extends Error {
|
|
9
3
|
errorType;
|
package/package.json
CHANGED