dsh-plugin-om 0.0.44 → 0.0.46
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/README.md +11 -5
- package/dist/client.js.map +1 -1
- package/dist/compaction-log.d.ts +4 -4
- package/dist/compaction-log.d.ts.map +1 -1
- package/dist/compress-loop.d.ts +5 -3
- package/dist/compress-loop.d.ts.map +1 -1
- package/dist/compress-tools.d.ts +28 -28
- package/dist/compress-tools.d.ts.map +1 -1
- package/dist/compress-view.d.ts +22 -5
- package/dist/compress-view.d.ts.map +1 -1
- package/dist/constants.d.ts +3 -3
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +409 -145
- package/package.json +8 -1
package/dist/index.mjs
CHANGED
|
@@ -4,12 +4,12 @@ import { scopeOf } from "@deepseek-ai/dsh-scope";
|
|
|
4
4
|
import { SessionId, SessionSeq } from "@deepseek-ai/dsh-session";
|
|
5
5
|
import { renderPrompt } from "@deepseek-ai/dsh-system-prompt";
|
|
6
6
|
import { SUBAGENT_DESCRIPTOR_VERSION } from "@deepseek-ai/dsh-subagent";
|
|
7
|
+
import { z } from "zod";
|
|
7
8
|
import Schema from "@deepseek-ai/schemastery";
|
|
8
9
|
import { copyFileSync, existsSync, mkdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
9
10
|
import { homedir } from "node:os";
|
|
10
11
|
import path from "node:path";
|
|
11
12
|
import { fileURLToPath } from "node:url";
|
|
12
|
-
import { z } from "zod";
|
|
13
13
|
import { toJSONSchema } from "zod/v4/core";
|
|
14
14
|
import { AttachmentId } from "@deepseek-ai/dsh-attachment";
|
|
15
15
|
//#region \0rolldown/runtime.js
|
|
@@ -60,8 +60,8 @@ function historyFormatNote(sections = {}) {
|
|
|
60
60
|
/** skill 工具名:toolcall 条目的工具名为该值时视为 skill 加载,<history> 块中以 <skill_content> 元素呈现。 */
|
|
61
61
|
const SKILL_TOOL_NAME = "skill";
|
|
62
62
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
63
|
+
* 提问工具名:成功且问题与答案完整配对时视为合法 ask,失败调用按普通 toolcall 处理。
|
|
64
|
+
* 合法 ask 直接压缩需二次确认;未直接压缩的原始 ask 重写后以 q/a 形式保留。
|
|
65
65
|
*/
|
|
66
66
|
const ASK_USER_QUESTION_TOOL_NAME = "ask_user_question";
|
|
67
67
|
/**
|
|
@@ -273,35 +273,101 @@ async function recordCompressionSession(ctx, parentSession, options) {
|
|
|
273
273
|
origin: "subagent",
|
|
274
274
|
delegationDepth: (header.delegationDepth ?? 0) + 1
|
|
275
275
|
} });
|
|
276
|
+
const label = compressionRecordLabel(options.phase, options.stats.rounds.length, options.success);
|
|
276
277
|
child.append("subagent/descriptor", {
|
|
277
278
|
version: SUBAGENT_DESCRIPTOR_VERSION,
|
|
278
279
|
mode: "one-shot",
|
|
279
280
|
provider: COMPACTION_LOG_PROVIDER,
|
|
280
|
-
label
|
|
281
|
+
label
|
|
281
282
|
});
|
|
283
|
+
child.append("turn/start", { turn: 1 });
|
|
282
284
|
let step = 0;
|
|
283
|
-
|
|
285
|
+
const executed = new Set(options.messages.filter((message) => message.role === "tool").map((message) => message.toolCallId));
|
|
286
|
+
let pending = [];
|
|
287
|
+
const closeStep = () => {
|
|
288
|
+
if (step === 0) return;
|
|
289
|
+
for (const call of pending) {
|
|
290
|
+
if (executed.has(call.id)) continue;
|
|
291
|
+
const repair = createToolResultMessage({
|
|
292
|
+
callId: call.id,
|
|
293
|
+
content: [{
|
|
294
|
+
type: "text",
|
|
295
|
+
text: "压缩循环未执行此工具调用"
|
|
296
|
+
}],
|
|
297
|
+
isError: true
|
|
298
|
+
});
|
|
299
|
+
child.append("tool/result", {
|
|
300
|
+
turn: 1,
|
|
301
|
+
step,
|
|
302
|
+
message: {
|
|
303
|
+
...repair,
|
|
304
|
+
id: `forked-tool-result-${call.id}-${uuid()}`
|
|
305
|
+
},
|
|
306
|
+
error: {
|
|
307
|
+
name: "ToolNotStartedError",
|
|
308
|
+
code: "TOOL_NOT_STARTED"
|
|
309
|
+
}
|
|
310
|
+
}, { surfaceOp: "append" });
|
|
311
|
+
}
|
|
312
|
+
child.append("step/end", {
|
|
313
|
+
turn: 1,
|
|
314
|
+
step
|
|
315
|
+
});
|
|
316
|
+
pending = [];
|
|
317
|
+
};
|
|
318
|
+
for (const message of options.messages) if (message.role === "assistant") {
|
|
319
|
+
closeStep();
|
|
284
320
|
step += 1;
|
|
285
|
-
|
|
286
|
-
turn:
|
|
321
|
+
child.append("step/start", {
|
|
322
|
+
turn: 1,
|
|
323
|
+
step
|
|
324
|
+
});
|
|
325
|
+
child.append("assistant/message", {
|
|
326
|
+
turn: 1,
|
|
287
327
|
step,
|
|
288
328
|
message,
|
|
289
329
|
stream: []
|
|
290
330
|
}, { surfaceOp: "append" });
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
331
|
+
pending = message.content.filter((block) => block.type === "tool-call");
|
|
332
|
+
for (const call of pending) {
|
|
333
|
+
if (!executed.has(call.id)) continue;
|
|
334
|
+
child.append("tool/call", {
|
|
335
|
+
turn: 1,
|
|
336
|
+
step,
|
|
337
|
+
callId: call.id,
|
|
338
|
+
name: call.name,
|
|
339
|
+
arguments: call.arguments
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
} else if (message.role === "tool") child.append("tool/result", {
|
|
343
|
+
turn: 1,
|
|
344
|
+
step,
|
|
345
|
+
message
|
|
346
|
+
}, { surfaceOp: "append" });
|
|
347
|
+
else child.append("user/message", message, { surfaceOp: "append" });
|
|
348
|
+
closeStep();
|
|
298
349
|
child.append("user/message", makeStatsMessage(formatCompressionStats(options.phase, options.success, options.stats)), { surfaceOp: "append" });
|
|
350
|
+
child.append("turn/end", {
|
|
351
|
+
turn: 1,
|
|
352
|
+
reason: { kind: "completed" }
|
|
353
|
+
});
|
|
354
|
+
const persistence = ctx.get("sessionPersistence");
|
|
355
|
+
if (persistence === void 0) throw new Error("宿主未提供 sessionPersistence 服务");
|
|
356
|
+
const write = await persistence.create(child.header);
|
|
299
357
|
try {
|
|
300
|
-
await
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
358
|
+
await write.append(child.snapshotEvents());
|
|
359
|
+
await write.flush();
|
|
360
|
+
} finally {
|
|
361
|
+
await write.close();
|
|
304
362
|
}
|
|
363
|
+
parentSession.append("subagent/catalog", {
|
|
364
|
+
version: 0,
|
|
365
|
+
childId: child.id,
|
|
366
|
+
childCreatedAt: child.header.createdAt,
|
|
367
|
+
mode: "one-shot",
|
|
368
|
+
label
|
|
369
|
+
});
|
|
370
|
+
await ctx.sessions.flush(parentSession);
|
|
305
371
|
return child.id;
|
|
306
372
|
} catch (error) {
|
|
307
373
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -7700,101 +7766,79 @@ function skillNameOf(session, cm) {
|
|
|
7700
7766
|
}
|
|
7701
7767
|
return "";
|
|
7702
7768
|
}
|
|
7703
|
-
/**
|
|
7769
|
+
/** 未作答题目的呈现文本。 */
|
|
7704
7770
|
const ASK_UNANSWERED_TEXT = "(未回答)";
|
|
7705
|
-
/**
|
|
7706
|
-
|
|
7707
|
-
|
|
7708
|
-
|
|
7709
|
-
|
|
7710
|
-
|
|
7711
|
-
|
|
7712
|
-
|
|
7713
|
-
|
|
7714
|
-
|
|
7715
|
-
|
|
7716
|
-
|
|
7717
|
-
|
|
7718
|
-
|
|
7719
|
-
|
|
7720
|
-
|
|
7721
|
-
|
|
7722
|
-
|
|
7723
|
-
|
|
7724
|
-
|
|
7725
|
-
|
|
7726
|
-
|
|
7727
|
-
|
|
7728
|
-
|
|
7729
|
-
|
|
7730
|
-
|
|
7731
|
-
|
|
7732
|
-
|
|
7733
|
-
|
|
7734
|
-
|
|
7735
|
-
|
|
7736
|
-
|
|
7771
|
+
/** 提问参数的完整结构;非法项使整次调用回退。 */
|
|
7772
|
+
const askQuestionsSchema = z.object({ questions: z.array(z.object({
|
|
7773
|
+
id: z.string().refine((id) => id.trim() !== ""),
|
|
7774
|
+
question: z.string().refine((question) => question.trim() !== ""),
|
|
7775
|
+
options: z.array(z.object({
|
|
7776
|
+
label: z.string(),
|
|
7777
|
+
description: z.string().optional()
|
|
7778
|
+
})).optional()
|
|
7779
|
+
})).min(1) });
|
|
7780
|
+
/** 回答结果的完整结构;等待和失败状态不是合法问答。 */
|
|
7781
|
+
const askAnswersSchema = z.object({
|
|
7782
|
+
pending: z.literal(false).optional(),
|
|
7783
|
+
aborted: z.literal(false).optional(),
|
|
7784
|
+
isError: z.literal(false).optional(),
|
|
7785
|
+
error: z.never().optional(),
|
|
7786
|
+
answers: z.array(z.object({
|
|
7787
|
+
id: z.string().refine((id) => id.trim() !== ""),
|
|
7788
|
+
selected: z.array(z.string()),
|
|
7789
|
+
custom: z.string().optional()
|
|
7790
|
+
}))
|
|
7791
|
+
});
|
|
7792
|
+
/** 校验配对成功的原始 ask,按题序保存问题、选项和回答。 */
|
|
7793
|
+
function askUserQuestionData(session, cm) {
|
|
7794
|
+
const call = toolCallBlockOf(session, cm);
|
|
7795
|
+
const resultMessage = toolResultMessageOf(session, cm);
|
|
7796
|
+
if (!call || !resultMessage) return null;
|
|
7797
|
+
const resultSeq = cm.seqs[1];
|
|
7798
|
+
const resultEvent = resultSeq === void 0 ? void 0 : session.snapshotEvents()[resultSeq];
|
|
7799
|
+
if (resultEvent?.type !== "tool/result" || resultEvent.data.error !== void 0) return null;
|
|
7800
|
+
if (resultMessage.role === "tool" && (resultMessage.isError || resultMessage.toolCallId !== cm.callId)) return null;
|
|
7801
|
+
if (resultMessage.content.some((block) => {
|
|
7802
|
+
const legacy = block;
|
|
7803
|
+
return legacy.type === "tool-result" && legacy.isError === true;
|
|
7804
|
+
})) return null;
|
|
7805
|
+
const args = toolArgsJson(call.arguments);
|
|
7806
|
+
const result = renderMessageText(resultMessage);
|
|
7737
7807
|
try {
|
|
7738
|
-
|
|
7808
|
+
const questions = askQuestionsSchema.safeParse(JSON.parse(args));
|
|
7809
|
+
const answers = askAnswersSchema.safeParse(JSON.parse(result));
|
|
7810
|
+
if (!questions.success || !answers.success) return null;
|
|
7811
|
+
const ids = new Set(questions.data.questions.map((question) => question.id));
|
|
7812
|
+
const answerMap = new Map(answers.data.answers.map((answer) => [answer.id, answer]));
|
|
7813
|
+
if (ids.size !== questions.data.questions.length || answerMap.size !== answers.data.answers.length || answerMap.size !== ids.size || [...answerMap.keys()].some((id) => !ids.has(id))) return null;
|
|
7814
|
+
return { questions: questions.data.questions.map((question) => {
|
|
7815
|
+
const answer = answerMap.get(question.id);
|
|
7816
|
+
const text = answer ? [...answer.selected, ...answer.custom ? [answer.custom] : []].join(", ") : "";
|
|
7817
|
+
return {
|
|
7818
|
+
question: question.question,
|
|
7819
|
+
options: (question.options ?? []).map((option) => ({
|
|
7820
|
+
label: option.label,
|
|
7821
|
+
...option.description === void 0 ? {} : { description: option.description }
|
|
7822
|
+
})),
|
|
7823
|
+
answer: text === "" ? ASK_UNANSWERED_TEXT : text
|
|
7824
|
+
};
|
|
7825
|
+
}) };
|
|
7739
7826
|
} catch {
|
|
7740
7827
|
return null;
|
|
7741
7828
|
}
|
|
7742
|
-
if (!isRecord(parsed) || !Array.isArray(parsed.answers)) return null;
|
|
7743
|
-
const items = [];
|
|
7744
|
-
for (const a of parsed.answers) {
|
|
7745
|
-
if (!isRecord(a)) continue;
|
|
7746
|
-
const selected = Array.isArray(a.selected) ? a.selected.filter((s) => typeof s === "string") : [];
|
|
7747
|
-
const custom = typeof a.custom === "string" && a.custom !== "" ? [a.custom] : [];
|
|
7748
|
-
const id = typeof a.id === "string" && a.id !== "" ? a.id : void 0;
|
|
7749
|
-
const text = [...selected, ...custom].join(", ");
|
|
7750
|
-
items.push(id === void 0 ? { text } : {
|
|
7751
|
-
id,
|
|
7752
|
-
text
|
|
7753
|
-
});
|
|
7754
|
-
}
|
|
7755
|
-
return items;
|
|
7756
7829
|
}
|
|
7757
|
-
/**
|
|
7758
|
-
|
|
7759
|
-
|
|
7760
|
-
|
|
7761
|
-
|
|
7762
|
-
|
|
7763
|
-
|
|
7764
|
-
const lines = questions.map((q) => `q:${q.text}`);
|
|
7765
|
-
if (result.trim() !== "") lines.push(`a:${result}`);
|
|
7766
|
-
return lines;
|
|
7767
|
-
}
|
|
7768
|
-
const lines = [];
|
|
7769
|
-
const paired = /* @__PURE__ */ new Set();
|
|
7770
|
-
for (const q of questions) {
|
|
7771
|
-
const index = q.id === void 0 ? -1 : answers.findIndex((a, i) => !paired.has(i) && a.id === q.id);
|
|
7772
|
-
if (index >= 0) paired.add(index);
|
|
7773
|
-
const answer = index >= 0 ? answers[index]?.text ?? "" : "";
|
|
7774
|
-
lines.push(`q:${q.text} a:${answer === "" ? ASK_UNANSWERED_TEXT : answer}`);
|
|
7775
|
-
}
|
|
7776
|
-
answers.forEach((a, i) => {
|
|
7777
|
-
if (!paired.has(i) && a.text !== "") lines.push(`a:${a.text}`);
|
|
7778
|
-
});
|
|
7779
|
-
return lines;
|
|
7780
|
-
}
|
|
7781
|
-
/**
|
|
7782
|
-
* ask_user_question 条目正文:构造为原生 <ask-user-question> 包裹形态,内文见
|
|
7783
|
-
* askUserQuestionLines。问题与回答均取不到内容时返回 null(调用方回退为通用完整消息呈现)。
|
|
7784
|
-
*/
|
|
7785
|
-
function askUserQuestionText(session, cm) {
|
|
7786
|
-
const call = toolCallBlockOf(session, cm);
|
|
7787
|
-
const args = call === void 0 ? void 0 : toolArgsJson(call.arguments);
|
|
7788
|
-
const resultMessage = toolResultMessageOf(session, cm);
|
|
7789
|
-
const result = resultMessage && Array.isArray(resultMessage.content) ? renderMessageText(resultMessage) : "";
|
|
7790
|
-
const lines = askUserQuestionLines(askQuestionItems(args), result.trim() === "" ? [] : askAnswerItems(result), result);
|
|
7791
|
-
if (lines.length === 0) return null;
|
|
7792
|
-
return `<ask-user-question>${lines.join("\n")}</ask-user-question>`;
|
|
7830
|
+
/** 观察 ask 正文:保留多行原文,每题按 q/options/a 呈现。 */
|
|
7831
|
+
function askUserQuestionText(ask) {
|
|
7832
|
+
return `<ask-user-question>${ask.questions.flatMap((question) => [
|
|
7833
|
+
`q:${question.question}`,
|
|
7834
|
+
...question.options.map((option, index) => `o${index + 1}:${option.label}${option.description ? ` — ${option.description}` : ""}`),
|
|
7835
|
+
`a:${question.answer}`
|
|
7836
|
+
]).join("\n")}</ask-user-question>`;
|
|
7793
7837
|
}
|
|
7794
7838
|
/**
|
|
7795
7839
|
* 观察视图:被压缩区间(表层 seq 集合)内的完整消息投影为条目——
|
|
7796
|
-
* user → 原文(图片等非文本块为注释)、sys →
|
|
7797
|
-
* reasoning 作为参考条目置于其所属 assistant 条目之前(每条 assistant 消息输出一次;
|
|
7840
|
+
* user → 原文(图片等非文本块为注释)、sys → 空条目(手动加载的 skill 为 skill 条目)、
|
|
7841
|
+
* assistant/toolcall → 原文渲染,reasoning 作为参考条目置于其所属 assistant 条目之前(每条 assistant 消息输出一次;
|
|
7798
7842
|
* skipReasoning 时省略)。
|
|
7799
7843
|
* 要求区间为区间内完整消息的首尾 index。
|
|
7800
7844
|
*/
|
|
@@ -7815,6 +7859,23 @@ function buildObserveView(session, seqs, options = {}) {
|
|
|
7815
7859
|
for (const cm of indexCompleteMessages(session)) {
|
|
7816
7860
|
if (!cm.seqs.every((seq) => shadowed.has(seq))) continue;
|
|
7817
7861
|
if (cm.type === "sys") {
|
|
7862
|
+
const seq = cm.seqs[0];
|
|
7863
|
+
const event = seq === void 0 ? void 0 : session.snapshotEvents()[seq];
|
|
7864
|
+
const source = event?.type === "user/message" ? event.data.source : void 0;
|
|
7865
|
+
if (cm.kind === "skill-invocation" && source?.kind === "skill-invocation" && typeof source.name === "string") {
|
|
7866
|
+
const text = renderCompleteMessage(session, cm);
|
|
7867
|
+
if (text.trim() !== "") {
|
|
7868
|
+
entries.push({
|
|
7869
|
+
kind: "assistant",
|
|
7870
|
+
lo: cm.index,
|
|
7871
|
+
hi: cm.index,
|
|
7872
|
+
text,
|
|
7873
|
+
toolName: SKILL_TOOL_NAME,
|
|
7874
|
+
skillName: source.name
|
|
7875
|
+
});
|
|
7876
|
+
continue;
|
|
7877
|
+
}
|
|
7878
|
+
}
|
|
7818
7879
|
entries.push({
|
|
7819
7880
|
kind: "sys",
|
|
7820
7881
|
lo: cm.index,
|
|
@@ -7851,9 +7912,9 @@ function buildObserveView(session, seqs, options = {}) {
|
|
|
7851
7912
|
}
|
|
7852
7913
|
const toolName = cm.type === "toolcall" ? toolCallNameOf(session, cm) : void 0;
|
|
7853
7914
|
const isSkill = toolName === SKILL_TOOL_NAME;
|
|
7854
|
-
const
|
|
7855
|
-
const
|
|
7856
|
-
const text = isSkill ? renderToolResultText(session, cm) :
|
|
7915
|
+
const ask = toolName === "ask_user_question" ? askUserQuestionData(session, cm) : null;
|
|
7916
|
+
const isAsk = ask !== null;
|
|
7917
|
+
const text = isSkill ? renderToolResultText(session, cm) : ask !== null ? askUserQuestionText(ask) : renderCompleteMessage(session, cm);
|
|
7857
7918
|
if (text.trim() === "") continue;
|
|
7858
7919
|
const skillName = isSkill ? skillNameOf(session, cm) : void 0;
|
|
7859
7920
|
let toolArgs;
|
|
@@ -7873,7 +7934,8 @@ function buildObserveView(session, seqs, options = {}) {
|
|
|
7873
7934
|
...skillName === void 0 ? {} : { skillName },
|
|
7874
7935
|
...cm.callId === void 0 || toolArgs === void 0 ? {} : { callId: cm.callId },
|
|
7875
7936
|
...toolArgs === void 0 ? {} : { toolArgs },
|
|
7876
|
-
...toolResult === void 0 ? {} : { toolResult }
|
|
7937
|
+
...toolResult === void 0 ? {} : { toolResult },
|
|
7938
|
+
...ask === null ? {} : { ask }
|
|
7877
7939
|
});
|
|
7878
7940
|
}
|
|
7879
7941
|
return {
|
|
@@ -8169,6 +8231,12 @@ function entryToElement(doc, entry) {
|
|
|
8169
8231
|
}
|
|
8170
8232
|
return el;
|
|
8171
8233
|
}
|
|
8234
|
+
if (entry.kind === "assistant" && entry.ask !== void 0) {
|
|
8235
|
+
const el = doc.createElement("ask-user-question");
|
|
8236
|
+
if (entry.lo !== void 0 && entry.lo === entry.hi) el.setAttribute("index", String(entry.lo));
|
|
8237
|
+
appendCdataText(doc, el, askUserQuestionInner(askUserQuestionText(entry.ask)) ?? "");
|
|
8238
|
+
return el;
|
|
8239
|
+
}
|
|
8172
8240
|
if (entry.kind === "assistant" && entry.toolName === "ask_user_question") {
|
|
8173
8241
|
const inner = askUserQuestionInner(entry.text);
|
|
8174
8242
|
if (inner !== void 0) {
|
|
@@ -8234,11 +8302,22 @@ function renderEntriesXml(entries) {
|
|
|
8234
8302
|
}
|
|
8235
8303
|
//#endregion
|
|
8236
8304
|
//#region src/compress-tools.ts
|
|
8305
|
+
/** 判定 JSON 对象,不接受数组或 null。 */
|
|
8306
|
+
function isJsonRecord(value) {
|
|
8307
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
8308
|
+
}
|
|
8309
|
+
/** 合法原始 ask 或已处理的历史 ask;普通失败 toolcall 不包含在内。 */
|
|
8310
|
+
function isAskEntry(entry) {
|
|
8311
|
+
return entry.kind === "assistant" && (entry.ask !== void 0 || entry.toolName === "ask_user_question" && entry.toolArgs === void 0);
|
|
8312
|
+
}
|
|
8237
8313
|
/** 压缩会话的工具状态:视图 + 替换记录 + 二次确认标记(skill / ask_user_question)+ 完成标记。 */
|
|
8238
8314
|
var CompressionState = class CompressionState {
|
|
8239
8315
|
view;
|
|
8240
8316
|
replacements = [];
|
|
8241
8317
|
challengedSkills = /* @__PURE__ */ new Set();
|
|
8318
|
+
viewedAsks = /* @__PURE__ */ new Set();
|
|
8319
|
+
undeliveredAsks = /* @__PURE__ */ new Set();
|
|
8320
|
+
rewrittenAsks = /* @__PURE__ */ new Map();
|
|
8242
8321
|
_completed = false;
|
|
8243
8322
|
/**
|
|
8244
8323
|
* @param view 压缩视图(观察或反思),工具校验与最终块构建的数据源。
|
|
@@ -8246,11 +8325,19 @@ var CompressionState = class CompressionState {
|
|
|
8246
8325
|
constructor(view) {
|
|
8247
8326
|
this.view = view;
|
|
8248
8327
|
}
|
|
8249
|
-
/**
|
|
8328
|
+
/** 已成功应用的普通摘要替换区间数,不包含 ask 重写。 */
|
|
8250
8329
|
get replacementCount() {
|
|
8251
8330
|
return this.replacements.length;
|
|
8252
8331
|
}
|
|
8253
|
-
/**
|
|
8332
|
+
/** 本次视图是否存在原始合法 ask,整个循环内保持稳定。 */
|
|
8333
|
+
get hasPendingAsks() {
|
|
8334
|
+
return this.view.entries.some((entry) => entry.ask !== void 0);
|
|
8335
|
+
}
|
|
8336
|
+
/** 已成功重写且最终未被直接压缩覆盖的 ask 数量。 */
|
|
8337
|
+
get rewriteCount() {
|
|
8338
|
+
return this.view.entries.filter((entry) => entry.ask !== void 0 && entry.lo !== void 0 && this.rewrittenAsks.has(entry.lo) && !this.coveredBy(entry)).length;
|
|
8339
|
+
}
|
|
8340
|
+
/** 是否已成功调用 completeCompression。 */
|
|
8254
8341
|
get completed() {
|
|
8255
8342
|
return this._completed;
|
|
8256
8343
|
}
|
|
@@ -8259,7 +8346,7 @@ var CompressionState = class CompressionState {
|
|
|
8259
8346
|
* 未知工具名或参数非法 JSON 返回错误结果,不抛出。
|
|
8260
8347
|
*/
|
|
8261
8348
|
executeCall(name, rawArgs) {
|
|
8262
|
-
if (name === "getHistory" || name === "compressHistory" || name === "completeCompression") {
|
|
8349
|
+
if (name === "getHistory" || name === "compressHistory" || name === "completeCompression" || name === "rewriteAskUserQuestion") {
|
|
8263
8350
|
let args;
|
|
8264
8351
|
if (typeof rawArgs === "string" && rawArgs.trim() === "") args = {};
|
|
8265
8352
|
else try {
|
|
@@ -8270,16 +8357,26 @@ var CompressionState = class CompressionState {
|
|
|
8270
8357
|
isError: true
|
|
8271
8358
|
};
|
|
8272
8359
|
}
|
|
8360
|
+
if (name === "rewriteAskUserQuestion" && this.hasPendingAsks) return this.rewriteAskUserQuestion(args);
|
|
8361
|
+
if (name === "rewriteAskUserQuestion") return {
|
|
8362
|
+
text: "本次视图没有待处理的原始 ask",
|
|
8363
|
+
isError: true
|
|
8364
|
+
};
|
|
8273
8365
|
const record = args ?? {};
|
|
8274
8366
|
if (name === "getHistory") return this.getHistory(record);
|
|
8275
8367
|
if (name === "compressHistory") return this.compressHistory(record);
|
|
8276
8368
|
return this.complete();
|
|
8277
8369
|
}
|
|
8278
8370
|
return {
|
|
8279
|
-
text: `未知工具 ${name}(可用:getHistory / compressHistory / completeCompression)`,
|
|
8371
|
+
text: `未知工具 ${name}(可用:getHistory / compressHistory${this.hasPendingAsks ? " / rewriteAskUserQuestion" : ""} / completeCompression)`,
|
|
8280
8372
|
isError: true
|
|
8281
8373
|
};
|
|
8282
8374
|
}
|
|
8375
|
+
/** 下一模型请求已携带成功的 getHistory 结果,允许使用其选项重写。 */
|
|
8376
|
+
markHistoryDelivered() {
|
|
8377
|
+
for (const index of this.undeliveredAsks) this.viewedAsks.add(index);
|
|
8378
|
+
this.undeliveredAsks.clear();
|
|
8379
|
+
}
|
|
8283
8380
|
/** 解析可选数值参数(缺省 undefined;非有限数返回 NaN)。 */
|
|
8284
8381
|
optionalNumber(value) {
|
|
8285
8382
|
if (value === void 0 || value === null) return void 0;
|
|
@@ -8329,8 +8426,10 @@ var CompressionState = class CompressionState {
|
|
|
8329
8426
|
text: `区间 [${start}..${end}] 没有条目`,
|
|
8330
8427
|
isError: false
|
|
8331
8428
|
};
|
|
8429
|
+
const text = `<!-- 完整消息区间 [${start}..${end}],共 ${selected.length} 条 -->\n${renderEntriesXml(selected)}`;
|
|
8430
|
+
for (const entry of selected) if (entry.ask !== void 0 && entry.lo !== void 0) this.undeliveredAsks.add(entry.lo);
|
|
8332
8431
|
return {
|
|
8333
|
-
text
|
|
8432
|
+
text,
|
|
8334
8433
|
isError: false
|
|
8335
8434
|
};
|
|
8336
8435
|
}
|
|
@@ -8431,7 +8530,7 @@ var CompressionState = class CompressionState {
|
|
|
8431
8530
|
let challengedTool;
|
|
8432
8531
|
for (const entry of this.view.entries) {
|
|
8433
8532
|
if (entry.kind !== "assistant") continue;
|
|
8434
|
-
if (entry.toolName !== "skill" && entry
|
|
8533
|
+
if (entry.toolName !== "skill" && !isAskEntry(entry)) continue;
|
|
8435
8534
|
if (entry.lo === void 0 || entry.lo < lo || entry.lo > hi) continue;
|
|
8436
8535
|
if (!this.challengedSkills.has(entry.lo)) {
|
|
8437
8536
|
unchallenged.push(entry.lo);
|
|
@@ -8440,10 +8539,13 @@ var CompressionState = class CompressionState {
|
|
|
8440
8539
|
}
|
|
8441
8540
|
if (unchallenged.length > 0) {
|
|
8442
8541
|
for (const index of unchallenged) this.challengedSkills.add(index);
|
|
8443
|
-
if (challengedTool === "ask_user_question")
|
|
8444
|
-
|
|
8445
|
-
|
|
8446
|
-
|
|
8542
|
+
if (challengedTool === "ask_user_question") {
|
|
8543
|
+
const rawAsk = this.view.entries.some((entry) => entry.ask !== void 0 && entry.lo !== void 0 && unchallenged.includes(entry.lo));
|
|
8544
|
+
return {
|
|
8545
|
+
text: `${CompressionState.spanLabel(lo, hi)} 包含 ask_user_question 向用户的提问(完整消息 index ${unchallenged.join("、")})。请重新思考该提问是否确定与后续任务无关:确定不相关时再次调用 compressHistory 压缩该区间;不确定或相关时不要压缩该区间,${rawAsk ? "请先查看,再调用 rewriteAskUserQuestion 重写后保留" : "保持原样即可"}`,
|
|
8546
|
+
isError: true
|
|
8547
|
+
};
|
|
8548
|
+
}
|
|
8447
8549
|
return {
|
|
8448
8550
|
text: `${CompressionState.spanLabel(lo, hi)} 包含 skill 加载(完整消息 index ${unchallenged.join("、")})。请重新思考该 skill 是否确定与后续任务无关:确定不相关时再次调用 compressHistory 压缩该区间;不确定或相关时不要压缩该区间,保持原样即可`,
|
|
8449
8551
|
isError: true
|
|
@@ -8464,8 +8566,91 @@ var CompressionState = class CompressionState {
|
|
|
8464
8566
|
isError: false
|
|
8465
8567
|
};
|
|
8466
8568
|
}
|
|
8467
|
-
/**
|
|
8569
|
+
/** 条目是否已被成功直接压缩完全覆盖。 */
|
|
8570
|
+
coveredBy(entry) {
|
|
8571
|
+
return entry.lo !== void 0 && entry.hi !== void 0 && this.replacements.some((rep) => entry.lo !== void 0 && entry.hi !== void 0 && entry.lo >= rep.lo && entry.hi <= rep.hi);
|
|
8572
|
+
}
|
|
8573
|
+
/** 重写已查看、尚未压缩覆盖的原始 ask,一次提交全部题。 */
|
|
8574
|
+
rewriteAskUserQuestion(args) {
|
|
8575
|
+
if (!isJsonRecord(args) || Object.keys(args).some((key) => key !== "index" && key !== "qa")) return {
|
|
8576
|
+
text: "重写参数必须是仅含 index 与 qa 的对象",
|
|
8577
|
+
isError: true
|
|
8578
|
+
};
|
|
8579
|
+
const { index, qa } = args;
|
|
8580
|
+
if (typeof index !== "number" || !Number.isSafeInteger(index) || index < 0) return {
|
|
8581
|
+
text: "index 必须是非负整数",
|
|
8582
|
+
isError: true
|
|
8583
|
+
};
|
|
8584
|
+
const entry = this.view.entries.find((item) => item.kind === "assistant" && item.lo === index && item.hi === index && item.ask !== void 0);
|
|
8585
|
+
if (entry?.ask === void 0) return {
|
|
8586
|
+
text: `index ${index} 不是本次视图中的合法原始 ask`,
|
|
8587
|
+
isError: true
|
|
8588
|
+
};
|
|
8589
|
+
if (this.coveredBy(entry)) return {
|
|
8590
|
+
text: `index ${index} 已被 compressHistory 覆盖,不能重写`,
|
|
8591
|
+
isError: true
|
|
8592
|
+
};
|
|
8593
|
+
if (!this.viewedAsks.has(index)) return {
|
|
8594
|
+
text: `请先用 getHistory 查看 index ${index} 的问题、选项和回答,收到返回结果后的下一轮再重写,不要在同轮提交`,
|
|
8595
|
+
isError: true
|
|
8596
|
+
};
|
|
8597
|
+
if (!Array.isArray(qa) || qa.length !== entry.ask.questions.length) return {
|
|
8598
|
+
text: `qa 必须包含 index ${index} 的全部 ${entry.ask.questions.length} 题`,
|
|
8599
|
+
isError: true
|
|
8600
|
+
};
|
|
8601
|
+
const rewritten = [];
|
|
8602
|
+
const ordinals = /* @__PURE__ */ new Set();
|
|
8603
|
+
for (const item of qa) {
|
|
8604
|
+
if (!isJsonRecord(item) || Object.keys(item).some((key) => ![
|
|
8605
|
+
"ordinal",
|
|
8606
|
+
"q",
|
|
8607
|
+
"a"
|
|
8608
|
+
].includes(key))) return {
|
|
8609
|
+
text: "每题只能包含 ordinal、q、a,不要提交 options 或其他字段",
|
|
8610
|
+
isError: true
|
|
8611
|
+
};
|
|
8612
|
+
const { ordinal, q, a } = item;
|
|
8613
|
+
if (typeof ordinal !== "number" || !Number.isInteger(ordinal) || ordinal < 1 || ordinal > qa.length || ordinals.has(ordinal)) return {
|
|
8614
|
+
text: `ordinal 必须覆盖 1..${qa.length} 的全部题序且不能重复`,
|
|
8615
|
+
isError: true
|
|
8616
|
+
};
|
|
8617
|
+
if (typeof q !== "string" || q.trim() === "" || typeof a !== "string" || a.trim() === "") return {
|
|
8618
|
+
text: "q 与 a 必须是非空纯文本;未回答的题明确写 (未回答)",
|
|
8619
|
+
isError: true
|
|
8620
|
+
};
|
|
8621
|
+
if (q.includes("<![CDATA[") || a.includes("<![CDATA[")) return {
|
|
8622
|
+
text: "q 与 a 不要包含 CDATA 包裹,插件会自动添加",
|
|
8623
|
+
isError: true
|
|
8624
|
+
};
|
|
8625
|
+
if (/^\s*o\d+\s*[::]/m.test(q) || /^\s*o\d+\s*[::]/m.test(a)) return {
|
|
8626
|
+
text: "q 与 a 不要包含 oN: 选项列表行;请将回答中的选项引用展开为自足表述",
|
|
8627
|
+
isError: true
|
|
8628
|
+
};
|
|
8629
|
+
ordinals.add(ordinal);
|
|
8630
|
+
rewritten.push({
|
|
8631
|
+
ordinal,
|
|
8632
|
+
q,
|
|
8633
|
+
a
|
|
8634
|
+
});
|
|
8635
|
+
}
|
|
8636
|
+
rewritten.sort((a, b) => a.ordinal - b.ordinal);
|
|
8637
|
+
this.rewrittenAsks.set(index, rewritten);
|
|
8638
|
+
return {
|
|
8639
|
+
text: `已重写完整消息 index ${index} 的 ${rewritten.length} 题`,
|
|
8640
|
+
isError: false
|
|
8641
|
+
};
|
|
8642
|
+
}
|
|
8643
|
+
/** 尚未直接压缩或重写的原始 ask index。 */
|
|
8644
|
+
unprocessedAskIndices() {
|
|
8645
|
+
return this.view.entries.flatMap((entry) => entry.ask !== void 0 && entry.lo !== void 0 && !this.coveredBy(entry) && !this.rewrittenAsks.has(entry.lo) ? [entry.lo] : []);
|
|
8646
|
+
}
|
|
8647
|
+
/** completeCompression:所有原始合法 ask 已压缩或重写后标记完成。 */
|
|
8468
8648
|
complete() {
|
|
8649
|
+
const pending = this.unprocessedAskIndices();
|
|
8650
|
+
if (pending.length > 0) return {
|
|
8651
|
+
text: `尚未处理原始 ask(index ${pending.join("、")}):请直接压缩,或先查看再调用 rewriteAskUserQuestion 重写后保留`,
|
|
8652
|
+
isError: true
|
|
8653
|
+
};
|
|
8469
8654
|
this._completed = true;
|
|
8470
8655
|
return {
|
|
8471
8656
|
text: "压缩完成",
|
|
@@ -8475,15 +8660,16 @@ var CompressionState = class CompressionState {
|
|
|
8475
8660
|
/**
|
|
8476
8661
|
* 构建最终 <history> 块:按 index 顺序合并视图条目与替换记录——user / sys 条目
|
|
8477
8662
|
* 原样、被替换区间生成 <assistant index|start end> 摘要条目(content 以 CDATA 包裹嵌入)、
|
|
8478
|
-
*
|
|
8479
|
-
*
|
|
8480
|
-
* 仅在对应条目存在时包含),开标签携带 tip 属性。产物为合法 XML,无需校验。
|
|
8663
|
+
* 未替换普通与历史条目原样保留,原始 ask 使用重写后的 q/a;reasoning 不进产物。
|
|
8664
|
+
* 块首按最终条目动态生成格式说明注释,开标签携带 tip 属性。
|
|
8481
8665
|
*/
|
|
8482
8666
|
buildFinalBlock() {
|
|
8667
|
+
const pending = this.unprocessedAskIndices();
|
|
8668
|
+
if (pending.length > 0) throw new Error(`原始 ask 尚未处理:index ${pending.join("、")}`);
|
|
8483
8669
|
const doc = new import_lib.DOMParser({ onError: () => {} }).parseFromString(`<${HISTORY_TAG} />`, "text/xml");
|
|
8484
8670
|
const root = doc.documentElement;
|
|
8485
8671
|
if (!root) return "";
|
|
8486
|
-
const coveredBy = (entry) =>
|
|
8672
|
+
const coveredBy = (entry) => this.coveredBy(entry);
|
|
8487
8673
|
const reps = [...this.replacements].sort((a, b) => a.lo - b.lo);
|
|
8488
8674
|
let nextRep = 0;
|
|
8489
8675
|
const emitReplacement = (rep) => {
|
|
@@ -8509,21 +8695,77 @@ var CompressionState = class CompressionState {
|
|
|
8509
8695
|
if (entry.kind === "reasoning") continue;
|
|
8510
8696
|
if (coveredBy(entry)) continue;
|
|
8511
8697
|
flushReplacementsBefore(entry.lo);
|
|
8512
|
-
|
|
8698
|
+
const qa = entry.lo === void 0 ? void 0 : this.rewrittenAsks.get(entry.lo);
|
|
8699
|
+
const output = entry.ask !== void 0 && qa !== void 0 && entry.lo !== void 0 ? {
|
|
8700
|
+
kind: "assistant",
|
|
8701
|
+
lo: entry.lo,
|
|
8702
|
+
hi: entry.lo,
|
|
8703
|
+
toolName: ASK_USER_QUESTION_TOOL_NAME,
|
|
8704
|
+
text: `<ask-user-question>${qa.map((item) => `q:${item.q}\na:${item.a}`).join("\n")}</ask-user-question>`
|
|
8705
|
+
} : entry;
|
|
8706
|
+
root.appendChild(entryToElement(doc, output));
|
|
8513
8707
|
}
|
|
8514
8708
|
flushReplacementsBefore(void 0);
|
|
8515
8709
|
const note = historyFormatNote({
|
|
8516
8710
|
user: this.view.entries.some((e) => e.kind === "user"),
|
|
8517
8711
|
sys: this.view.entries.some((e) => e.kind === "sys"),
|
|
8518
8712
|
skill: this.view.entries.some((e) => e.kind === "assistant" && e.toolName === "skill" && !coveredBy(e)),
|
|
8519
|
-
askUserQuestion: this.view.entries.some((e) => e
|
|
8713
|
+
askUserQuestion: this.view.entries.some((e) => isAskEntry(e) && !coveredBy(e))
|
|
8520
8714
|
});
|
|
8521
8715
|
const serializer = new import_lib.XMLSerializer();
|
|
8522
8716
|
const inner = Array.from(root.childNodes).map((node) => serializer.serializeToString(node)).join("\n");
|
|
8523
8717
|
return `<${HISTORY_TAG} tip="${HISTORY_TIP}">\n${note}\n${inner}\n</${HISTORY_TAG}>`;
|
|
8524
8718
|
}
|
|
8525
8719
|
};
|
|
8526
|
-
/**
|
|
8720
|
+
/** 仅含原始合法 ask 的压缩会话使用的重写工具。 */
|
|
8721
|
+
const REWRITE_ASK_TOOL_SCHEMA = {
|
|
8722
|
+
name: "rewriteAskUserQuestion",
|
|
8723
|
+
description: "重写已用 getHistory 查看且未被 compressHistory 覆盖的原始 ask。按原题序提交全部 q/a,忠实精炼问题与回答,展开选项引用,未回答的题明确写 (未回答)。保留 ask 的产物只含 q/a,不提交 options 或 CDATA 包裹。",
|
|
8724
|
+
parameters: {
|
|
8725
|
+
type: "object",
|
|
8726
|
+
properties: {
|
|
8727
|
+
index: {
|
|
8728
|
+
type: "integer",
|
|
8729
|
+
minimum: 0,
|
|
8730
|
+
description: "原始合法 ask 的完整消息 index"
|
|
8731
|
+
},
|
|
8732
|
+
qa: {
|
|
8733
|
+
type: "array",
|
|
8734
|
+
minItems: 1,
|
|
8735
|
+
description: "该调用内全部题的重写结果,保留题数和原题序",
|
|
8736
|
+
items: {
|
|
8737
|
+
type: "object",
|
|
8738
|
+
properties: {
|
|
8739
|
+
ordinal: {
|
|
8740
|
+
type: "integer",
|
|
8741
|
+
minimum: 1,
|
|
8742
|
+
description: "原始题序,从 1 开始"
|
|
8743
|
+
},
|
|
8744
|
+
q: {
|
|
8745
|
+
type: "string",
|
|
8746
|
+
minLength: 1,
|
|
8747
|
+
description: "忠实精炼后的问题纯文本"
|
|
8748
|
+
},
|
|
8749
|
+
a: {
|
|
8750
|
+
type: "string",
|
|
8751
|
+
minLength: 1,
|
|
8752
|
+
description: "自足的回答纯文本,保留用户条件和决定;未回答写 (未回答)"
|
|
8753
|
+
}
|
|
8754
|
+
},
|
|
8755
|
+
required: [
|
|
8756
|
+
"ordinal",
|
|
8757
|
+
"q",
|
|
8758
|
+
"a"
|
|
8759
|
+
],
|
|
8760
|
+
additionalProperties: false
|
|
8761
|
+
}
|
|
8762
|
+
}
|
|
8763
|
+
},
|
|
8764
|
+
required: ["index", "qa"],
|
|
8765
|
+
additionalProperties: false
|
|
8766
|
+
}
|
|
8767
|
+
};
|
|
8768
|
+
/** 压缩会话基础工具的 wire 定义(GenerateOptions.tools)。 */
|
|
8527
8769
|
const COMPRESSION_TOOL_SCHEMAS = [
|
|
8528
8770
|
{
|
|
8529
8771
|
name: "getHistory",
|
|
@@ -8570,7 +8812,7 @@ const COMPRESSION_TOOL_SCHEMAS = [
|
|
|
8570
8812
|
},
|
|
8571
8813
|
{
|
|
8572
8814
|
name: "completeCompression",
|
|
8573
|
-
description: "
|
|
8815
|
+
description: "处理完成后调用,成功时立即结束压缩会话。原始合法 ask 必须已成功压缩,或先查看并重写后保留;其他未压缩条目原样保留。无待处理的原始 ask 时允许直接完成。",
|
|
8574
8816
|
parameters: {
|
|
8575
8817
|
type: "object",
|
|
8576
8818
|
properties: {}
|
|
@@ -8635,15 +8877,15 @@ async function gateRateLimit(waitMs, signal) {
|
|
|
8635
8877
|
//#region src/compress-loop.ts
|
|
8636
8878
|
/**
|
|
8637
8879
|
* 工具驱动的压缩循环:以新会话方式直连 ctx.llm.stream(),模型通过 getHistory /
|
|
8638
|
-
* compressHistory / completeCompression
|
|
8880
|
+
* compressHistory / completeCompression 完成压缩,存在原始合法提问时加入重写工具。
|
|
8639
8881
|
* 导出 runCompressionLoop / buildCompressionPrompt / buildCompressionTaskText /
|
|
8640
|
-
* CompressionLoopOptions / CompressionOutcome / CompressionStats / COMPRESSION_NUDGE_TEXT。
|
|
8882
|
+
* ASK_COMPRESSION_PROMPT / CompressionLoopOptions / CompressionOutcome / CompressionStats / COMPRESSION_NUDGE_TEXT。
|
|
8641
8883
|
*
|
|
8642
8884
|
* - 首条 user 消息仅含压缩指令与 start/end 区间(buildCompressionTaskText),不含
|
|
8643
8885
|
* 历史消息内容;共享压缩提示词作为 system
|
|
8644
8886
|
* - 每轮请求携带压缩工具 schemas(purpose='compaction',maxTokens 沿用配置);流经
|
|
8645
8887
|
* BlockAssembler 组装为 assistant 消息,工具执行结果以 tool-result 消息回填
|
|
8646
|
-
* - completeCompression
|
|
8888
|
+
* - completeCompression 校验通过后立即停止(同轮后续工具调用不再执行),最终 <history>
|
|
8647
8889
|
* 块由 CompressionState 构建,全程无需整块校验
|
|
8648
8890
|
* - 模型输出纯文本(无工具调用)时追加提醒消息继续,连续 2 轮仍无工具调用判失败
|
|
8649
8891
|
* - 429 限流走全局限流等待门(gateRateLimit / noteRateLimit);其余请求级错误依赖
|
|
@@ -8678,9 +8920,9 @@ function buildCompressionPrompt(skipReasoning) {
|
|
|
8678
8920
|
"- 摘要粒度越往后越细:靠近末尾(最近)的消息保留更多细节,开头(较早)的消息简写。",
|
|
8679
8921
|
"- <history> 条目正文以 CDATA 包裹(空的 <sys> 条目为自闭合空元素,无正文),CDATA 内为逐字原样内容。",
|
|
8680
8922
|
"- 工具调用条目为 <assistant index=\"N\" type=\"toolcall\" tool-name=\"…\" callId=\"…\">,内含 <tool-args>(调用参数 JSON)与 <tool-result>(工具返回内容)两个 CDATA 子元素。",
|
|
8681
|
-
"- 加载的 skill 以 <skill_content name=\"…\" index=\"N\"> 条目呈现,内含 <skill_resources> 与 <skill_instructions>
|
|
8682
|
-
"- ask_user_question
|
|
8683
|
-
"-
|
|
8923
|
+
"- 加载的 skill 以 <skill_content name=\"…\" index=\"N\"> 条目呈现,内含 <skill_resources> 与 <skill_instructions> 两段,内文为加载的 skill 内容,属于关键信息。仅在你明确判断该 skill 与后续任务无关时,压缩它;如果该 skill 与后续任务相关,或者你无法判断,不要压缩,**保持原文**。",
|
|
8924
|
+
"- 已有 <history> 块中的 ask_user_question 以 <ask-user-question index=\"N\"> 呈现,CDATA 内为逐题成对的 q:(问题文本)与 a:(用户回答)行,未作答的题记为 a:(未回答)。这些提问已经处理;仅在明确判断与后续任务无关时压缩,否则保持原样。原始提问按本会话的专用处理规则执行。",
|
|
8925
|
+
"- 除原始提问外,未压缩的条目将原样保留;宁可保留也不要强行压缩不确定的内容。",
|
|
8684
8926
|
"- 全部完成后调用 completeCompression 结束;不要输出与工具调用无关的文本。"
|
|
8685
8927
|
].join("\n");
|
|
8686
8928
|
}
|
|
@@ -8691,6 +8933,18 @@ function buildCompressionTaskText(phase, start, end) {
|
|
|
8691
8933
|
if (phase === "reflect") return `合并压缩全部 <history> 块,完整消息区间 [${start}..${end}]:用 getHistory 查看已有块条目,用 compressHistory 重新压缩合并 assistant 条目,完成后调用 completeCompression。`;
|
|
8692
8934
|
return `压缩完整消息区间 [${start}..${end}]:用 getHistory 查看条目,用 compressHistory 分批压缩 assistant 条目,完成后调用 completeCompression。`;
|
|
8693
8935
|
}
|
|
8936
|
+
/** 原始合法 ask 的处理约束,只在视图含待处理原始提问时注入。 */
|
|
8937
|
+
const ASK_COMPRESSION_PROMPT = [
|
|
8938
|
+
"【原始提问处理】",
|
|
8939
|
+
"- 原始合法 ask_user_question 在 getHistory 中以 <ask-user-question index=\"N\"> 展示,每题为 q:问题、o1:选项一、o2:选项二等及 a:回答;选项包含 label 与 description,无选项题省略 oN 行。",
|
|
8940
|
+
"- 每个原始提问必须二选一:直接用 compressHistory 压成普通 assistant 摘要;或者保留 ask 标签并用 rewriteAskUserQuestion 重写 q/a。不能原样跳过。",
|
|
8941
|
+
"- 直接压缩:仅在明确判断该提问与后续任务无关时执行;首次覆盖会被阻止一次,重新思考后再次调用才执行。不确定或相关时走保留重写路径。",
|
|
8942
|
+
"- 保留重写:先用 getHistory 查看该提问及全部选项,收到返回结果后的下一轮再调用 rewriteAskUserQuestion({index, qa:[{ordinal:1,q:\"问题\",a:\"回答\"},...]}),不要与查看同轮提交;ordinal 从 1 开始,按原题序提交全部题,不增删、不重复。",
|
|
8943
|
+
"- q/a 均可忠实精炼,保留语境与用户真实意图。将回答中对选项的编号、标签等引用展开为自足表述;产物仅有 q:/a:,不能保留 oN 选项列表或 options 字段。",
|
|
8944
|
+
"- 空答案明确为 (未回答),不得补造用户决定。仅提交纯文本 q/a,不自行拼接 XML 或 CDATA 包裹。",
|
|
8945
|
+
"- 允许切换路径;最终成功的直接压缩覆盖优先,已覆盖的 ask 不能再次重写。history 块中的 ask 已处理,不需重写。",
|
|
8946
|
+
"- completeCompression 仅在每个原始合法 ask 已成功直接压缩或重写后完成;若返回待处理 index,请处理后再次调用。"
|
|
8947
|
+
].join("\n");
|
|
8694
8948
|
/** 模型输出纯文本(无工具调用)时追加的提醒消息文本。 */
|
|
8695
8949
|
const COMPRESSION_NUDGE_TEXT = "请通过工具执行压缩:用 getHistory 查看区间条目,用 compressHistory 压缩 assistant 条目,完成后调用 completeCompression。不要输出与工具调用无关的文本。";
|
|
8696
8950
|
/** 构造插件自产 user 消息(压缩指令/提醒;source 为插件自有 kind)。 */
|
|
@@ -8754,6 +9008,8 @@ function isRateLimitFailure(message, status) {
|
|
|
8754
9008
|
async function runCompressionLoop(ctx, session, options) {
|
|
8755
9009
|
const logger = makeLogger(ctx, options.debug);
|
|
8756
9010
|
const state = new CompressionState(options.view);
|
|
9011
|
+
const tools = state.hasPendingAsks ? [...COMPRESSION_TOOL_SCHEMAS, REWRITE_ASK_TOOL_SCHEMA] : COMPRESSION_TOOL_SCHEMAS;
|
|
9012
|
+
const system = [buildCompressionPrompt(options.skipReasoning), ...state.hasPendingAsks ? [ASK_COMPRESSION_PROMPT] : []].join("\n\n");
|
|
8757
9013
|
const messages = [makePluginUserMessage(options.taskText)];
|
|
8758
9014
|
let rounds = 0;
|
|
8759
9015
|
let nudges = 0;
|
|
@@ -8808,11 +9064,12 @@ async function runCompressionLoop(ctx, session, options) {
|
|
|
8808
9064
|
sessionId: session.id,
|
|
8809
9065
|
purpose: "compaction",
|
|
8810
9066
|
...options.signal === void 0 ? {} : { signal: options.signal },
|
|
8811
|
-
system
|
|
9067
|
+
system,
|
|
8812
9068
|
messages: [...messages],
|
|
8813
|
-
tools
|
|
9069
|
+
tools
|
|
8814
9070
|
};
|
|
8815
9071
|
const assembler = new BlockAssembler();
|
|
9072
|
+
state.markHistoryDelivered();
|
|
8816
9073
|
try {
|
|
8817
9074
|
for await (const chunk of ctx.llm.stream(requestOptions)) assembler.push(chunk);
|
|
8818
9075
|
} catch (error) {
|
|
@@ -8867,19 +9124,15 @@ async function runCompressionLoop(ctx, session, options) {
|
|
|
8867
9124
|
continue;
|
|
8868
9125
|
}
|
|
8869
9126
|
nudges = 0;
|
|
8870
|
-
let completed = false;
|
|
8871
9127
|
for (const call of calls) {
|
|
8872
|
-
if (call.name === "completeCompression") {
|
|
8873
|
-
messages.push(toolResultOf(call, state.complete()));
|
|
8874
|
-
completed = true;
|
|
8875
|
-
break;
|
|
8876
|
-
}
|
|
8877
9128
|
messages.push(toolResultOf(call, state.executeCall(call.name, call.arguments)));
|
|
9129
|
+
if (state.completed) break;
|
|
8878
9130
|
}
|
|
8879
|
-
if (completed) {
|
|
9131
|
+
if (state.completed) {
|
|
9132
|
+
if (options.signal?.aborted) return await failWith(COMPACTION_ABORTED_ERROR, true);
|
|
8880
9133
|
const text = state.buildFinalBlock();
|
|
8881
|
-
if (state.replacementCount === 0) logger.warn("压缩完成但未执行任何压缩替换(空提交)");
|
|
8882
|
-
logger.info(`压缩循环完成(${rounds} 轮,${state.replacementCount}
|
|
9134
|
+
if (state.replacementCount === 0 && state.rewriteCount === 0) logger.warn("压缩完成但未执行任何压缩替换(空提交)");
|
|
9135
|
+
logger.info(`压缩循环完成(${rounds} 轮,${state.replacementCount} 次压缩替换,${state.rewriteCount} 条提问重写,输出 ${text.length} 字符)`);
|
|
8883
9136
|
const stats = buildStats();
|
|
8884
9137
|
const id = await recordSessionId(true, stats);
|
|
8885
9138
|
return {
|
|
@@ -10486,7 +10739,9 @@ const inject = [
|
|
|
10486
10739
|
"tokenMeter",
|
|
10487
10740
|
"sessions"
|
|
10488
10741
|
];
|
|
10489
|
-
/**
|
|
10742
|
+
/** OM 主会话在任务推进中保留规划与结果节点。 */
|
|
10743
|
+
const PROGRESS_PROMPT = "在任务推进的关键节点,简要输出后续规划,并在执行后说明对应结果;无需逐次播报工具调用。";
|
|
10744
|
+
/** 插件激活入口:解析配置、注册工具、接线系统提示与 pre-step 自动压缩。 */
|
|
10490
10745
|
function apply(ctx, config) {
|
|
10491
10746
|
const resolved = currentConfig(config);
|
|
10492
10747
|
const logger = makeLogger(ctx, () => currentConfig(config).debug);
|
|
@@ -10523,6 +10778,15 @@ function apply(ctx, config) {
|
|
|
10523
10778
|
}
|
|
10524
10779
|
syncTools();
|
|
10525
10780
|
ctx.on("loader/volatile-update", () => syncTools());
|
|
10781
|
+
ctx.on("system-prompt/assemble", async (_assembly, context, next) => {
|
|
10782
|
+
const assembly = await next();
|
|
10783
|
+
if (currentConfig(config).omEnabled && context.agent && isMainSession(context.agent.session)) assembly.sections.push({
|
|
10784
|
+
name: "dsh-plugin-om:progress",
|
|
10785
|
+
text: PROGRESS_PROMPT,
|
|
10786
|
+
interpolate: false
|
|
10787
|
+
});
|
|
10788
|
+
return assembly;
|
|
10789
|
+
});
|
|
10526
10790
|
ctx.on("agent/pre-step", async ({ agent, signal }, next) => {
|
|
10527
10791
|
let outcome = { failed: false };
|
|
10528
10792
|
try {
|