mocode-ai 1.9.9 → 1.10.1
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/index.js +13 -3
- package/dist/agent/mentioned-files.js +71 -0
- package/dist/agent/model-turn.js +83 -16
- package/dist/agent/progress-guard.js +67 -0
- package/dist/agent/run-coordinator.js +151 -23
- package/dist/agent/short-path-completion.js +109 -0
- package/dist/agent/spawn.js +46 -0
- package/dist/agent/stages/history-manager.js +1 -0
- package/dist/agent/tool-helpers.js +2 -2
- package/dist/agent/tool-turn.js +49 -4
- package/dist/agent/work-discipline.js +9 -9
- package/dist/changeset/unified-diff.js +59 -0
- package/dist/config/index.js +13 -10
- package/dist/context/relevance.js +2 -1
- package/dist/context/usage-snapshot.js +50 -0
- package/dist/headless.js +3 -1
- package/dist/host/commit-message.js +41 -0
- package/dist/host/protocol.js +205 -10
- package/dist/host/stdio.js +1096 -63
- package/dist/i18n/index.js +26 -10
- package/dist/llm/index.js +28 -3
- package/dist/llm/message-shape.js +45 -0
- package/dist/llm/providers/anthropic.js +2 -0
- package/dist/llm/reasoning.js +17 -0
- package/dist/memory/adoption.js +159 -27
- package/dist/memory/anchor-relocate.js +227 -0
- package/dist/memory/consolidate-skips.js +196 -0
- package/dist/memory/health.js +88 -6
- package/dist/memory/index.js +2 -1
- package/dist/memory/reflect.js +36 -4
- package/dist/memory/store.js +30 -0
- package/dist/models/reasoning-cap.js +19 -2
- package/dist/orchestrator/lane-executor.js +7 -1
- package/dist/orchestrator/repl.js +30 -49
- package/dist/prompt/system-prompt.js +30 -0
- package/dist/repl/commands/appearance.js +1 -2
- package/dist/repl/commands/compact.js +4 -0
- package/dist/repl/commands/diff.js +5 -46
- package/dist/repl/commands/memory.js +77 -4
- package/dist/repl/commands/model-actions.js +4 -3
- package/dist/repl/commands/model.js +2 -3
- package/dist/repl/message-format.js +7 -6
- package/dist/repl/running-input.js +27 -65
- package/dist/repl/runtime.js +129 -89
- package/dist/repl/status-bar.js +4 -38
- package/dist/rollback/store.js +36 -23
- package/dist/runtime/browser-diagnostics.js +77 -0
- package/dist/runtime/browser-manager.js +150 -22
- package/dist/runtime/runtime.js +1 -1
- package/dist/session/compact.js +44 -4
- package/dist/session/retention.js +4 -0
- package/dist/session/short-path-metrics.js +185 -0
- package/dist/session/store.js +78 -3
- package/dist/session/terminal-transcript.js +101 -0
- package/dist/tools/builtins/ask-human.js +2 -2
- package/dist/tools/builtins/browser.js +4 -1
- package/dist/tools/builtins/edit-file.js +28 -5
- package/dist/tools/builtins/grep.js +85 -35
- package/dist/tools/builtins/memory-save.js +1 -1
- package/dist/tools/builtins/memory-update.js +1 -1
- package/dist/tools/builtins/plan-update.js +4 -2
- package/dist/tools/builtins/read-file.js +95 -44
- package/dist/tools/builtins/run-command.js +57 -9
- package/dist/tools/builtins/task.js +4 -0
- package/dist/tools/constants.js +8 -0
- package/dist/tools/ripgrep-client.js +31 -11
- package/dist/tools/tool-runtime.js +29 -8
- package/dist/ui/layout-internal/content-write.js +7 -3
- package/dist/ui/layout-internal/core.js +1 -1
- package/dist/ui/layout-internal/state.js +0 -6
- package/dist/ui/layout-internal/statusbar.js +0 -30
- package/dist/ui/output-sink.js +24 -0
- package/dist/ui/spinner.js +3 -1
- package/package.json +4 -2
package/dist/i18n/index.js
CHANGED
|
@@ -175,10 +175,10 @@ const zhCN = {
|
|
|
175
175
|
'intervention.emptyInput': '自动返回空输入',
|
|
176
176
|
'intervention.nonInteractive': '[介入] {title}(非交互环境,{kind})',
|
|
177
177
|
'repl.idle': '空闲',
|
|
178
|
-
'repl.queuedCommandHeld': '排队的是斜杠命令,已填入输入框,请确认后手动发送',
|
|
179
178
|
'repl.loadFailed': '(加载失败)',
|
|
180
179
|
'repl.resumeAnchor': '已恢复会话 {id} · {count} 条消息 · 当前模型 {model}(/model switch 可切换)',
|
|
181
|
-
'repl.compactedNotice': '──
|
|
180
|
+
'repl.compactedNotice': '── 较早的模型上下文已压缩 · 以下是保存的摘要 ──',
|
|
181
|
+
'repl.modelCompacted': '── 模型上下文已压缩 · 聊天记录仍可查看 ──',
|
|
182
182
|
'repl.compactedTruncated': '…(摘要共 {total} 字符,仅显示前 {shown} 字符)',
|
|
183
183
|
'repl.noSessions': '(没有已保存的会话)',
|
|
184
184
|
'repl.noValue': '(无)',
|
|
@@ -320,9 +320,6 @@ const zhCN = {
|
|
|
320
320
|
'banner.disabled': '关闭',
|
|
321
321
|
'status.modeSwitch': 'Shift+Tab 切换',
|
|
322
322
|
'status.history': '历史 ↑{count}(PgDn 回底)',
|
|
323
|
-
'status.queued': '排队 ·',
|
|
324
|
-
'status.queuedCount': '排队 ×{count} ·',
|
|
325
|
-
'status.queuePlanTag': '(Plan)',
|
|
326
323
|
'status.planMoreSteps': '还有 {count} 步',
|
|
327
324
|
'status.measured': '实测',
|
|
328
325
|
'status.estimated': '估算',
|
|
@@ -330,6 +327,16 @@ const zhCN = {
|
|
|
330
327
|
'status.providerMeasured': 'provider 上一步实测 {tokens}:{pct}%',
|
|
331
328
|
'agent.sending': '发送中… (任意键 / Esc / Ctrl+C 撤回)',
|
|
332
329
|
'agent.thinking': '思考中',
|
|
330
|
+
'agent.openingTargetFile': '目标文件',
|
|
331
|
+
'agent.openingRead': '我先查看 {file},了解现有内容。',
|
|
332
|
+
'agent.openingEdit': '我先修改 {file}。',
|
|
333
|
+
'agent.openingSearch': '我先在工作区定位相关文件或代码。',
|
|
334
|
+
'agent.openingCommand': '我先运行命令,查看执行结果。',
|
|
335
|
+
'agent.openingWeb': '我先查找相关网页资料。',
|
|
336
|
+
'agent.openingClarify': '我先确认一项影响后续工作的细节。',
|
|
337
|
+
'agent.openingTools': '我先准备这一步需要的工具。',
|
|
338
|
+
'agent.openingGeneric': '我先执行这项任务的第一步操作。',
|
|
339
|
+
'agent.openingSteered': '按你的补充,{action}',
|
|
333
340
|
'agent.generating': '生成 {tool}',
|
|
334
341
|
'agent.executing': '执行 {tool}',
|
|
335
342
|
'agent.retrying': '连接异常,{seconds}s 后重试(第 {attempt} 次)',
|
|
@@ -362,6 +369,7 @@ const zhCN = {
|
|
|
362
369
|
'toolError.invalidJson': '错误:工具 {name} 的 arguments 不是合法 JSON: {arguments}',
|
|
363
370
|
'toolError.execution': '错误:工具 {name} 执行失败: {message}',
|
|
364
371
|
'command.outputTruncated': '...(输出已截断 {count} 字符,保留开头与结尾)...',
|
|
372
|
+
'command.encodingHint': '[输出包含较多 NUL 或替换字符。如需读取这些字节,请检查输出分隔符或字符编码。]',
|
|
365
373
|
'command.interrupted': '[已中断]',
|
|
366
374
|
'command.executionFailed': '执行失败: {message}',
|
|
367
375
|
'command.exitCode': '[退出码 {code}]',
|
|
@@ -680,10 +688,10 @@ const en = {
|
|
|
680
688
|
'intervention.emptyInput': 'automatically returned empty input',
|
|
681
689
|
'intervention.nonInteractive': '[intervention] {title} (non-interactive; {kind})',
|
|
682
690
|
'repl.idle': 'Idle',
|
|
683
|
-
'repl.queuedCommandHeld': 'Queued text is a slash command; filled into the input for you to send manually',
|
|
684
691
|
'repl.loadFailed': '(load failed)',
|
|
685
692
|
'repl.resumeAnchor': 'Resumed session {id} · {count} messages · model {model} (/model switch to change)',
|
|
686
|
-
'repl.compactedNotice': '── Earlier
|
|
693
|
+
'repl.compactedNotice': '── Earlier model context was compacted · Saved summary below ──',
|
|
694
|
+
'repl.modelCompacted': '── Model context compacted · Chat history is still available ──',
|
|
687
695
|
'repl.compactedTruncated': '… (summary is {total} chars, showing the first {shown})',
|
|
688
696
|
'repl.noSessions': '(no saved sessions)',
|
|
689
697
|
'repl.noValue': '(none)',
|
|
@@ -824,9 +832,6 @@ const en = {
|
|
|
824
832
|
'banner.disabled': 'Disabled',
|
|
825
833
|
'status.modeSwitch': 'Shift+Tab Switch',
|
|
826
834
|
'status.history': 'History ↑{count} (PgDn to bottom)',
|
|
827
|
-
'status.queued': 'queued ·',
|
|
828
|
-
'status.queuedCount': 'queued ×{count} ·',
|
|
829
|
-
'status.queuePlanTag': ' (Plan)',
|
|
830
835
|
'status.planMoreSteps': '{count} more step(s)',
|
|
831
836
|
'status.measured': 'measured',
|
|
832
837
|
'status.estimated': 'estimated',
|
|
@@ -834,6 +839,16 @@ const en = {
|
|
|
834
839
|
'status.providerMeasured': 'provider measured {tokens} last step:{pct}%',
|
|
835
840
|
'agent.sending': 'Sending… (any key / Esc / Ctrl+C to recall)',
|
|
836
841
|
'agent.thinking': 'Thinking',
|
|
842
|
+
'agent.openingTargetFile': 'the target file',
|
|
843
|
+
'agent.openingRead': 'I’ll inspect {file} to see what’s there.',
|
|
844
|
+
'agent.openingEdit': 'I’ll start by editing {file}.',
|
|
845
|
+
'agent.openingSearch': 'I’ll locate the relevant files or code in the workspace.',
|
|
846
|
+
'agent.openingCommand': 'I’ll run the command and check its result.',
|
|
847
|
+
'agent.openingWeb': 'I’ll look up the relevant web sources.',
|
|
848
|
+
'agent.openingClarify': 'I’ll clarify a detail that affects the next step.',
|
|
849
|
+
'agent.openingTools': 'I’ll prepare the tools needed for the next step.',
|
|
850
|
+
'agent.openingGeneric': 'I’ll start with the first operation for this task.',
|
|
851
|
+
'agent.openingSteered': 'With your update in mind, {action}',
|
|
837
852
|
'agent.generating': 'Generating {tool}',
|
|
838
853
|
'agent.executing': 'Running {tool}',
|
|
839
854
|
'agent.retrying': 'Connection lost, retrying in {seconds}s (attempt {attempt})',
|
|
@@ -868,6 +883,7 @@ const en = {
|
|
|
868
883
|
'command.executionFailed': 'Execution failed: {message}',
|
|
869
884
|
'command.exitCode': '[exit code {code}]',
|
|
870
885
|
'command.timedOut': '[timed out and terminated]',
|
|
886
|
+
'command.encodingHint': '[Output contains frequent NUL or replacement characters. Check the delimiter or encoding if you need to interpret those bytes.]',
|
|
871
887
|
'repl.errorLabel': '[Error]',
|
|
872
888
|
'repl.visionUnsupported': 'The current model, {model}, does not support vision input.',
|
|
873
889
|
'repl.originalError': 'Original: {message}',
|
package/dist/llm/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import { getPlanDisabledTools, getProfileDisabledTools } from '../tools/constant
|
|
|
4
4
|
import { imageSizeFromDataUrl } from '../attachments/image.js';
|
|
5
5
|
import { ThinkTagFilter } from './think-filter.js';
|
|
6
6
|
import { sanitizeToolSchemas } from './tool-schema.js';
|
|
7
|
-
import { sanitizeAssistantContent } from './message-shape.js';
|
|
7
|
+
import { prepareReasoningContent, sanitizeAssistantContent, sanitizeContentParts } from './message-shape.js';
|
|
8
8
|
import { isMarkedStreamInterrupted } from './stream-interrupt.js';
|
|
9
9
|
import { defaultAnthropicFetch, anthropicChatOnce } from './providers/anthropic.js';
|
|
10
10
|
import { vertexChatOnce } from './providers/vertex.js';
|
|
@@ -440,6 +440,15 @@ function logRetry(attempt, err, waitMs) {
|
|
|
440
440
|
export function __setChatCreateImpl(impl) {
|
|
441
441
|
defaultClientState.openAICreateImpl = impl;
|
|
442
442
|
}
|
|
443
|
+
/** 仅 DeepSeek 官方端点要求 tools 请求回传 reasoning_content;代理与 DashScope 不套用。 */
|
|
444
|
+
function isOfficialDeepSeekEndpoint(baseURL) {
|
|
445
|
+
try {
|
|
446
|
+
return /(?:^|\.)deepseek\.com$/i.test(new URL(baseURL).hostname);
|
|
447
|
+
}
|
|
448
|
+
catch {
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
443
452
|
/**
|
|
444
453
|
* 工具 schema 保持稳定数组引用,再在 MCP 发现或扩展变动后原地刷新;
|
|
445
454
|
* 这样 chat()/预算模块的默认参数不会继续指向陈旧的顶层快照。
|
|
@@ -647,6 +656,7 @@ async function chatWithRuntime(runtime, messages, handlers, signal, toolsOverrid
|
|
|
647
656
|
* 无需改写时返回原数组引用 —— 图片消息含大段 base64,不能无条件深拷贝。
|
|
648
657
|
*/
|
|
649
658
|
export function normalizeImageDetail(messages) {
|
|
659
|
+
messages = sanitizeContentParts(messages);
|
|
650
660
|
let changed = false;
|
|
651
661
|
const next = messages.map((message) => {
|
|
652
662
|
const content = message.content;
|
|
@@ -713,6 +723,7 @@ async function chatOnce(messages, handlers, signal, toolsOverride, runtime = def
|
|
|
713
723
|
provider: runtimeConfig.provider,
|
|
714
724
|
model: runtime.getModel(),
|
|
715
725
|
maxTokens: runtimeConfig.maxTokens,
|
|
726
|
+
baseURL: runtimeConfig.baseURL,
|
|
716
727
|
// 仅当激活预设来自目录且对应当前模型时携带,否则走正则兜底。
|
|
717
728
|
...(activePreset && activePreset.catalogProvider && activePreset.model === runtime.getModel()
|
|
718
729
|
? {
|
|
@@ -726,7 +737,10 @@ async function chatOnce(messages, handlers, signal, toolsOverride, runtime = def
|
|
|
726
737
|
});
|
|
727
738
|
const stream = await create({
|
|
728
739
|
model: runtime.getModel(),
|
|
729
|
-
messages: sanitizeAssistantContent(normalizeImageDetail(messages)),
|
|
740
|
+
messages: prepareReasoningContent(sanitizeContentParts(sanitizeAssistantContent(normalizeImageDetail(messages))),
|
|
741
|
+
// DeepSeek 官方 thinking 默认开启;带工具时必须回传完整历史思考,auto 也要保留。
|
|
742
|
+
// DashScope 与其它 OpenAI 兼容端点不认这个扩展字段,统一剥离。
|
|
743
|
+
activeTools.length > 0 && effort !== 'off' && isOfficialDeepSeekEndpoint(runtimeConfig.baseURL)),
|
|
730
744
|
stream: true,
|
|
731
745
|
// 空工具表(如摘要请求)不发 tools 字段——部分网关拒绝空数组。
|
|
732
746
|
...(activeTools.length > 0
|
|
@@ -744,6 +758,7 @@ async function chatOnce(messages, handlers, signal, toolsOverride, runtime = def
|
|
|
744
758
|
// content 内嵌 think 标签由独立增量状态机过滤。它只暂存“可能组成标签”的后缀,
|
|
745
759
|
// 因而既能覆盖标签任意位置跨 chunk,也不会让普通正文固定延迟数个字符。
|
|
746
760
|
let visibleContent = '';
|
|
761
|
+
let reasoningContent = '';
|
|
747
762
|
let consumedAny = false;
|
|
748
763
|
const thinkFilter = new ThinkTagFilter();
|
|
749
764
|
let usage;
|
|
@@ -802,6 +817,12 @@ async function chatOnce(messages, handlers, signal, toolsOverride, runtime = def
|
|
|
802
817
|
}
|
|
803
818
|
if (!delta)
|
|
804
819
|
continue; // 末尾 usage-only chunk 等无 delta
|
|
820
|
+
if (delta.reasoning_content) {
|
|
821
|
+
reasoningContent += delta.reasoning_content;
|
|
822
|
+
// reasoning_content 同样计费,但不作为可见正文或触发 onText。
|
|
823
|
+
if (handlers.onProgress)
|
|
824
|
+
countLive(delta.reasoning_content);
|
|
825
|
+
}
|
|
805
826
|
if (delta.content) {
|
|
806
827
|
if (handlers.onProgress)
|
|
807
828
|
countLive(delta.content);
|
|
@@ -849,6 +870,7 @@ async function chatOnce(messages, handlers, signal, toolsOverride, runtime = def
|
|
|
849
870
|
return {
|
|
850
871
|
content: consumedAny ? visibleContent : null,
|
|
851
872
|
toolCalls,
|
|
873
|
+
...(reasoningContent ? { reasoningContent } : {}),
|
|
852
874
|
usage,
|
|
853
875
|
finishReason,
|
|
854
876
|
};
|
|
@@ -962,7 +984,8 @@ const messageTokensCache = new WeakMap();
|
|
|
962
984
|
function messageSignature(m) {
|
|
963
985
|
const c = m.content;
|
|
964
986
|
const tcs = m.tool_calls;
|
|
965
|
-
|
|
987
|
+
const reasoning = m.role === 'assistant' ? m.reasoning_content : undefined;
|
|
988
|
+
return `${m.role ?? ''}|${typeof c === 'string' ? c.length : -1}|${tcs ? 1 : 0}|${reasoning?.length ?? 0}`;
|
|
966
989
|
}
|
|
967
990
|
/** 估算单条消息的 token 数:结构开销 + content + tool_calls 参数。内容签名命中则复用上次结果。 */
|
|
968
991
|
export function messageTokens(m) {
|
|
@@ -978,6 +1001,8 @@ export function messageTokens(m) {
|
|
|
978
1001
|
else if (role === 'tool')
|
|
979
1002
|
structural = 6;
|
|
980
1003
|
let body = contentTokens(m.content);
|
|
1004
|
+
if (m.role === 'assistant' && m.reasoning_content)
|
|
1005
|
+
body += estimateTokens(m.reasoning_content);
|
|
981
1006
|
const tcs = m.tool_calls;
|
|
982
1007
|
if (tcs) {
|
|
983
1008
|
for (const tc of tcs)
|
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话 part 上的文件名、附件标记、截断标记只供 GUI 回放,不属于 OpenAI 协议。
|
|
3
|
+
* 在 provider 边界移除,避免续聊时严格网关把整请求拒掉;原 history 保留元数据。
|
|
4
|
+
*/
|
|
5
|
+
export function sanitizeContentParts(messages) {
|
|
6
|
+
let changed = false;
|
|
7
|
+
const next = messages.map((message) => {
|
|
8
|
+
if (!Array.isArray(message.content))
|
|
9
|
+
return message;
|
|
10
|
+
let messageChanged = false;
|
|
11
|
+
const content = message.content.map((raw) => {
|
|
12
|
+
if (!raw || typeof raw !== 'object')
|
|
13
|
+
return raw;
|
|
14
|
+
const part = raw;
|
|
15
|
+
if (part.name === undefined && part.attachment === undefined && part.truncated === undefined)
|
|
16
|
+
return raw;
|
|
17
|
+
messageChanged = true;
|
|
18
|
+
const { name: _name, attachment: _attachment, truncated: _truncated, ...clean } = part;
|
|
19
|
+
return clean;
|
|
20
|
+
});
|
|
21
|
+
if (!messageChanged)
|
|
22
|
+
return message;
|
|
23
|
+
changed = true;
|
|
24
|
+
return { ...message, content };
|
|
25
|
+
});
|
|
26
|
+
return changed ? next : messages;
|
|
27
|
+
}
|
|
1
28
|
/**
|
|
2
29
|
* 把「无文本、无 tool_calls」的 assistant 消息的 `content: null` 改写成空字符串。
|
|
3
30
|
*
|
|
@@ -40,3 +67,21 @@ export function sanitizeAssistantContent(messages) {
|
|
|
40
67
|
});
|
|
41
68
|
return changed ? next : messages;
|
|
42
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* DeepSeek 官方 thinking + tools 要求所有历史 assistant 轮原样回传 reasoning_content。
|
|
72
|
+
* 字段仍完整保存在会话中;其它 provider / 无工具请求只在 transport 的副本上剥离。
|
|
73
|
+
* 无需改写时复用原数组和消息引用,保持长会话的前缀缓存稳定。
|
|
74
|
+
*/
|
|
75
|
+
export function prepareReasoningContent(messages, preserve) {
|
|
76
|
+
if (preserve)
|
|
77
|
+
return messages;
|
|
78
|
+
let changed = false;
|
|
79
|
+
const next = messages.map((message) => {
|
|
80
|
+
if (message.role !== 'assistant' || message.reasoning_content === undefined)
|
|
81
|
+
return message;
|
|
82
|
+
const { reasoning_content: _reasoning, ...clean } = message;
|
|
83
|
+
changed = true;
|
|
84
|
+
return clean;
|
|
85
|
+
});
|
|
86
|
+
return changed ? next : messages;
|
|
87
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { config, effectiveReasoningEffort, getActiveModel } from '../../config/index.js';
|
|
2
2
|
import { resolveReasoningParams } from '../reasoning.js';
|
|
3
3
|
import { markStreamInterrupted } from '../stream-interrupt.js';
|
|
4
|
+
import { sanitizeContentParts } from '../message-shape.js';
|
|
4
5
|
let fetchImplOverride = null;
|
|
5
6
|
/** 仅供单测注入;生产路径使用 Node 18+ 全局 fetch。 */
|
|
6
7
|
export function __setAnthropicFetchImpl(impl) {
|
|
@@ -68,6 +69,7 @@ function appendMessage(messages, role, blocks, cacheCandidates, cacheable = true
|
|
|
68
69
|
* history 保持旧格式以兼容已有 session;provider 边界负责 tool_use/tool_result 编码。
|
|
69
70
|
*/
|
|
70
71
|
export function encodeAnthropicMessages(input, promptCache = config.anthropicPromptCache) {
|
|
72
|
+
input = sanitizeContentParts(input);
|
|
71
73
|
const system = [];
|
|
72
74
|
const messages = [];
|
|
73
75
|
const cacheCandidates = [];
|
package/dist/llm/reasoning.js
CHANGED
|
@@ -68,6 +68,7 @@ export function recognizesReasoning(target) {
|
|
|
68
68
|
/gpt-?5/.test(m) ||
|
|
69
69
|
/qwen[-_]?3/.test(m) ||
|
|
70
70
|
/deepseek[-_]?r1/.test(m) ||
|
|
71
|
+
/deepseek[-_]?(?:v4|flash)/.test(m) ||
|
|
71
72
|
/glm[-_]?4[.-]?[56]/.test(m) ||
|
|
72
73
|
/glm[-_]?5/.test(m) ||
|
|
73
74
|
/doubao[-_]?seed/.test(m));
|
|
@@ -81,6 +82,9 @@ export function resolveReasoningParams(effort, target) {
|
|
|
81
82
|
// Gemini/Vertex provider 在 buildGeminiRequest 内直接生成 thinkingConfig;这里不应再注入 OpenAI 方言。
|
|
82
83
|
if (target.provider === 'google')
|
|
83
84
|
return {};
|
|
85
|
+
// 目录显式声明不支持思考是硬边界,不能因型号名命中正则又悄悄开启。
|
|
86
|
+
if (target.catalogReasoning === false)
|
|
87
|
+
return {};
|
|
84
88
|
// 目录优先:带 catalogProviderId 时按目录能力+厂商方言产出。
|
|
85
89
|
// 注意 anthropic 路径的思考在 anthropic provider 内仍走它自己的 target.provider,
|
|
86
90
|
// 目录只对 OpenAI 兼容路径有意义;Anthropic 官方协议继续走下方既有分支。
|
|
@@ -133,6 +137,19 @@ export function resolveReasoningParams(effort, target) {
|
|
|
133
137
|
return { thinking: { type: 'disabled' } };
|
|
134
138
|
return { thinking: { type: 'enabled' } };
|
|
135
139
|
}
|
|
140
|
+
// DeepSeek V4 / Flash 是混合思考模型。官方端点用 thinking + reasoning_effort;
|
|
141
|
+
// DashScope 兼容端点用 enable_thinking + thinking_budget(两种写法均已实测,按端点选厂商原生方言)。
|
|
142
|
+
if (/deepseek[-_]?(?:v4|flash)/.test(m)) {
|
|
143
|
+
if (/dashscope(?:-[a-z]+)?\.aliyuncs\.com/i.test(target.baseURL ?? '')) {
|
|
144
|
+
if (effort === 'off')
|
|
145
|
+
return { enable_thinking: false };
|
|
146
|
+
return { enable_thinking: true, thinking_budget: THINK_BUDGET[effort] };
|
|
147
|
+
}
|
|
148
|
+
if (effort === 'off')
|
|
149
|
+
return { thinking: { type: 'disabled' } };
|
|
150
|
+
// DeepSeek 官方只有 low/high/max 三档,归一化 medium 映射到 high。
|
|
151
|
+
return { thinking: { type: 'enabled' }, reasoning_effort: effort === 'low' ? 'low' : 'high' };
|
|
152
|
+
}
|
|
136
153
|
// DeepSeek-R1:无开关参数,任何档位都不下发。
|
|
137
154
|
if (/deepseek[-_]?r1/.test(m))
|
|
138
155
|
return {};
|
package/dist/memory/adoption.js
CHANGED
|
@@ -18,6 +18,7 @@ const CHANNELS = [
|
|
|
18
18
|
'memory_tail',
|
|
19
19
|
];
|
|
20
20
|
let serial = 0;
|
|
21
|
+
let suppressionBuckets = new Map();
|
|
21
22
|
function ledgerPath() {
|
|
22
23
|
return path.join(process.cwd(), '.mocode', 'memory-adoption.jsonl');
|
|
23
24
|
}
|
|
@@ -33,6 +34,7 @@ function emptyStats() {
|
|
|
33
34
|
notApplicable: 0,
|
|
34
35
|
unclear: 0,
|
|
35
36
|
pending: 0,
|
|
37
|
+
stranded: 0,
|
|
36
38
|
byChannel: {
|
|
37
39
|
proactive_trigger: 0,
|
|
38
40
|
proactive_anchor: 0,
|
|
@@ -105,21 +107,23 @@ function readSummary() {
|
|
|
105
107
|
return null;
|
|
106
108
|
}
|
|
107
109
|
}
|
|
108
|
-
function summarize(events) {
|
|
110
|
+
function summarize(events, currentSessionId = getCurrentSessionId()) {
|
|
109
111
|
const out = emptyStats();
|
|
110
|
-
const delivered = new
|
|
112
|
+
const delivered = new Map();
|
|
111
113
|
const evaluated = new Set();
|
|
112
114
|
for (const e of events) {
|
|
113
115
|
if (e.kind === 'delivered') {
|
|
114
|
-
delivered.
|
|
116
|
+
delivered.set(e.id, e);
|
|
115
117
|
out.delivered++;
|
|
116
118
|
out.byChannel[e.channel]++;
|
|
117
119
|
}
|
|
118
120
|
else if (e.kind === 'suppressed') {
|
|
119
|
-
|
|
121
|
+
// 聚合行带 count;历史逐条行缺省 1,两种形态必须给出同一组数字。
|
|
122
|
+
const n = e.count && e.count > 0 ? e.count : 1;
|
|
123
|
+
out.suppressed += n;
|
|
120
124
|
if (e.reason)
|
|
121
125
|
out.suppressedByReason[e.reason] =
|
|
122
|
-
(out.suppressedByReason[e.reason] ?? 0) +
|
|
126
|
+
(out.suppressedByReason[e.reason] ?? 0) + n;
|
|
123
127
|
}
|
|
124
128
|
else {
|
|
125
129
|
evaluated.add(e.deliveryId);
|
|
@@ -132,7 +136,16 @@ function summarize(events) {
|
|
|
132
136
|
out.unclear++;
|
|
133
137
|
}
|
|
134
138
|
}
|
|
135
|
-
|
|
139
|
+
// 未评估的送达分两类:本会话的(还能裁决)与旧会话的(永远等不到)。
|
|
140
|
+
// 缺 sessionId 的历史事件按「可评估」算——宁可让告警偏亮,也不要把真能处理的一批藏起来。
|
|
141
|
+
for (const [id, e] of delivered) {
|
|
142
|
+
if (evaluated.has(id))
|
|
143
|
+
continue;
|
|
144
|
+
if (!currentSessionId || !e.sessionId || e.sessionId === currentSessionId)
|
|
145
|
+
out.pending++;
|
|
146
|
+
else
|
|
147
|
+
out.stranded++;
|
|
148
|
+
}
|
|
136
149
|
return out;
|
|
137
150
|
}
|
|
138
151
|
function mergeStats(target, add) {
|
|
@@ -145,13 +158,15 @@ function mergeStats(target, add) {
|
|
|
145
158
|
(target.suppressedByReason[reason] ?? 0) + count;
|
|
146
159
|
}
|
|
147
160
|
}
|
|
161
|
+
function newEventId(now) {
|
|
162
|
+
return `adopt-${now.replace(/[-:.TZ]/g, '')}-${++serial}`;
|
|
163
|
+
}
|
|
148
164
|
function append(event) {
|
|
149
165
|
if (memoryWriteBlockReason())
|
|
150
166
|
return null;
|
|
151
167
|
const now = new Date().toISOString();
|
|
152
|
-
const id = `adopt-${now.replace(/[-:.TZ]/g, '')}-${++serial}`;
|
|
153
168
|
const record = {
|
|
154
|
-
id,
|
|
169
|
+
id: newEventId(now),
|
|
155
170
|
at: now,
|
|
156
171
|
...(getCurrentSessionId() ? { sessionId: getCurrentSessionId() } : {}),
|
|
157
172
|
...event,
|
|
@@ -160,23 +175,96 @@ function append(event) {
|
|
|
160
175
|
const p = ledgerPath();
|
|
161
176
|
mkdirSync(path.dirname(p), { recursive: true });
|
|
162
177
|
appendFileSync(p, JSON.stringify(record) + '\n', 'utf8');
|
|
163
|
-
return id;
|
|
178
|
+
return record.id;
|
|
164
179
|
}
|
|
165
180
|
catch {
|
|
166
181
|
return null;
|
|
167
182
|
}
|
|
168
183
|
}
|
|
184
|
+
/** 把内存聚合桶转成对外可读的事件形态(与落盘行同构,便于统计与展示共用一套代码)。
|
|
185
|
+
* 只取**当前**账本路径的桶:`/cd` 换工作区后账本是另一个文件,统计与落盘都必须各算各的。
|
|
186
|
+
*/
|
|
187
|
+
function bucketEvents() {
|
|
188
|
+
const current = ledgerPath();
|
|
189
|
+
return [...suppressionBuckets.values()]
|
|
190
|
+
.filter((b) => b.ledger === current)
|
|
191
|
+
.map((b) => ({
|
|
192
|
+
id: b.id,
|
|
193
|
+
at: b.lastAt,
|
|
194
|
+
...(b.sessionId ? { sessionId: b.sessionId } : {}),
|
|
195
|
+
memoryId: b.memoryId,
|
|
196
|
+
channel: b.channel,
|
|
197
|
+
kind: 'suppressed',
|
|
198
|
+
reason: b.reason,
|
|
199
|
+
count: b.count,
|
|
200
|
+
}));
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 落盘聚合后的抑制计数。调用时机与 `flushProactiveStats()` / `flushTurnRecallStats()` 一致:
|
|
204
|
+
* 轮末、退出、`/memory adoption`、`/memory health`。
|
|
205
|
+
*
|
|
206
|
+
* 失败(含 `/memory pause` 或总开关关闭)**不清桶**:抑制次数是可观测性数据,
|
|
207
|
+
* 下次 flush 再试即可,不该因为一次写盘失败就静默丢掉。桶的规模由
|
|
208
|
+
* (session, memoryId, channel, reason) 去重决定,不会无限增长。
|
|
209
|
+
*/
|
|
210
|
+
export function flushAdoptionSuppressions() {
|
|
211
|
+
if (suppressionBuckets.size === 0)
|
|
212
|
+
return;
|
|
213
|
+
if (memoryWriteBlockReason())
|
|
214
|
+
return;
|
|
215
|
+
const rows = bucketEvents();
|
|
216
|
+
if (rows.length === 0)
|
|
217
|
+
return;
|
|
218
|
+
const p = ledgerPath();
|
|
219
|
+
try {
|
|
220
|
+
mkdirSync(path.dirname(p), { recursive: true });
|
|
221
|
+
appendFileSync(p, rows.map((r) => JSON.stringify(r)).join('\n') + '\n', 'utf8');
|
|
222
|
+
// 只清当前账本的桶:其它工作区的未落盘次数要在它自己的目录里落盘。
|
|
223
|
+
for (const [key, bucket] of suppressionBuckets)
|
|
224
|
+
if (bucket.ledger === p)
|
|
225
|
+
suppressionBuckets.delete(key);
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
// 保守失败:桶保留,下次 flush 重试(重复落盘风险低于丢数据)。
|
|
229
|
+
}
|
|
230
|
+
}
|
|
169
231
|
/** 记录一条真正进入模型上下文或工具结果的记忆。 */
|
|
170
232
|
export function recordMemoryDelivery(memoryId, channel) {
|
|
171
233
|
if (!memoryId)
|
|
172
234
|
return null;
|
|
173
235
|
return append({ memoryId, channel, kind: 'delivered' });
|
|
174
236
|
}
|
|
175
|
-
/** 记录命中但被限流/去重治理挡住的记忆;它从未进入采纳分母。
|
|
237
|
+
/** 记录命中但被限流/去重治理挡住的记忆;它从未进入采纳分母。
|
|
238
|
+
*
|
|
239
|
+
* 只在内存里累加,由 `flushAdoptionSuppressions()` 落成一行带 `count` 的聚合记录。
|
|
240
|
+
* 抑制是纯计数信号:没有单条审计价值(不像 delivered 需要 id 供评估回填),而逐条
|
|
241
|
+
* 落盘会让账本按工具调用次数膨胀(实测一周 14 MB / 61k 行,99% 是重复抑制)。
|
|
242
|
+
*/
|
|
176
243
|
export function recordMemorySuppression(memoryId, channel, reason) {
|
|
177
244
|
if (!memoryId)
|
|
178
245
|
return;
|
|
179
|
-
|
|
246
|
+
if (memoryWriteBlockReason())
|
|
247
|
+
return;
|
|
248
|
+
const sessionId = getCurrentSessionId();
|
|
249
|
+
const ledger = ledgerPath();
|
|
250
|
+
const key = `${ledger}|${sessionId}|${memoryId}|${channel}|${reason}`;
|
|
251
|
+
const now = new Date().toISOString();
|
|
252
|
+
const bucket = suppressionBuckets.get(key);
|
|
253
|
+
if (bucket) {
|
|
254
|
+
bucket.count++;
|
|
255
|
+
bucket.lastAt = now;
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
suppressionBuckets.set(key, {
|
|
259
|
+
id: newEventId(now),
|
|
260
|
+
ledger,
|
|
261
|
+
...(sessionId ? { sessionId } : {}),
|
|
262
|
+
memoryId,
|
|
263
|
+
channel,
|
|
264
|
+
reason,
|
|
265
|
+
count: 1,
|
|
266
|
+
lastAt: now,
|
|
267
|
+
});
|
|
180
268
|
}
|
|
181
269
|
/** 当前会话里等待既有反思/压缩调用判定的送达事件。 */
|
|
182
270
|
export function pendingAdoptionDeliveries(limit = MAX_PENDING_FOR_EVALUATION) {
|
|
@@ -277,9 +365,13 @@ export function applyAdoptionEvaluations(evaluations) {
|
|
|
277
365
|
}
|
|
278
366
|
return count;
|
|
279
367
|
}
|
|
280
|
-
/** 当前 90 天明细 + 已压缩历史的总统计。
|
|
368
|
+
/** 当前 90 天明细 + 已压缩历史的总统计。
|
|
369
|
+
*
|
|
370
|
+
* 内存里尚未 flush 的抑制桶要一并计入:否则 `/memory adoption` 上看不到本轮刚发生的抑制
|
|
371
|
+
* (它只在轮末落盘),而这两处本来就在 flush 之后调用,多数情况下桶是空的。
|
|
372
|
+
*/
|
|
281
373
|
export function adoptionStats() {
|
|
282
|
-
const current = summarize(readLedger());
|
|
374
|
+
const current = summarize([...readLedger(), ...bucketEvents()]);
|
|
283
375
|
const summary = readSummary();
|
|
284
376
|
if (summary)
|
|
285
377
|
mergeStats(current, summary.totals);
|
|
@@ -287,30 +379,69 @@ export function adoptionStats() {
|
|
|
287
379
|
}
|
|
288
380
|
/** 只读详情,按条目过滤时用于 `/memory adoption <memory-id>`。 */
|
|
289
381
|
export function adoptionEvents(memoryId, limit = 30) {
|
|
290
|
-
const all = readLedger().filter((e) => !memoryId || e.memoryId === memoryId);
|
|
382
|
+
const all = [...readLedger(), ...bucketEvents()].filter((e) => !memoryId || e.memoryId === memoryId);
|
|
291
383
|
return all.slice(-Math.max(1, limit));
|
|
292
384
|
}
|
|
293
|
-
/**
|
|
385
|
+
/**
|
|
386
|
+
* 把同一 (session, memoryId, channel, reason) 的抑制行合并成一条 count 累加行。
|
|
387
|
+
* 历史逐条行与新的聚合行都是这个 key,所以老账本也能被同一套逻辑收缩。
|
|
388
|
+
* 其它事件原序不动——delivered 的 id 必须保留给评估回填。
|
|
389
|
+
*/
|
|
390
|
+
function compactSuppressions(events) {
|
|
391
|
+
const merged = new Map();
|
|
392
|
+
const out = [];
|
|
393
|
+
for (const e of events) {
|
|
394
|
+
if (e.kind !== 'suppressed') {
|
|
395
|
+
out.push(e);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
const key = `${e.sessionId ?? ''}|${e.memoryId}|${e.channel}|${e.reason ?? ''}`;
|
|
399
|
+
const prev = merged.get(key);
|
|
400
|
+
if (!prev) {
|
|
401
|
+
const copy = { ...e, count: e.count && e.count > 0 ? e.count : 1 };
|
|
402
|
+
merged.set(key, copy);
|
|
403
|
+
out.push(copy);
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
prev.count = (prev.count ?? 1) + (e.count && e.count > 0 ? e.count : 1);
|
|
407
|
+
// 保留较晚的时间戳:id 用首次出现的,at 用最后一次——语义是「这条聚合覆盖到何时」。
|
|
408
|
+
if (Date.parse(e.at) > Date.parse(prev.at)) {
|
|
409
|
+
prev.at = e.at;
|
|
410
|
+
prev.id = e.id;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return out;
|
|
414
|
+
}
|
|
415
|
+
/** 将超过 90 天的原始事件压为聚合统计,保留最近明细以便审计。
|
|
416
|
+
*
|
|
417
|
+
* 同时做一次去重压缩:旧版本逐条落盘的抑制行会在这里被合并成 count 聚合行,所以
|
|
418
|
+
* 升级后第一次 `/memory health` 或退出就会把历史账本从几十 MB 降到 KB 级,不需要手工清理。
|
|
419
|
+
*/
|
|
294
420
|
export function pruneAdoptionLedger(now = Date.now()) {
|
|
295
421
|
const all = readLedger();
|
|
296
422
|
if (all.length === 0)
|
|
297
423
|
return;
|
|
298
424
|
const cutoff = now - RETAIN_DAYS * 24 * 60 * 60 * 1000;
|
|
299
425
|
const old = all.filter((e) => Date.parse(e.at) < cutoff);
|
|
300
|
-
|
|
426
|
+
const retained = compactSuppressions(all.filter((e) => Date.parse(e.at) >= cutoff));
|
|
427
|
+
// 没有过期事件也可能需要收缩(升级后第一次),所以不能因 old 为空而早退;
|
|
428
|
+
// 真正无事可做时(行数没变且有 summary 在)直接返回,避免每次命中都重写文件。
|
|
429
|
+
const needsCompaction = retained.length !== all.length - old.length;
|
|
430
|
+
if (old.length === 0 && !needsCompaction)
|
|
301
431
|
return;
|
|
302
|
-
const retained = all.filter((e) => !old.includes(e));
|
|
303
432
|
try {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
433
|
+
if (old.length > 0) {
|
|
434
|
+
const existing = readSummary();
|
|
435
|
+
const totals = emptyStats();
|
|
436
|
+
if (existing)
|
|
437
|
+
mergeStats(totals, existing.totals);
|
|
438
|
+
mergeStats(totals, summarize(old));
|
|
439
|
+
const p = summaryPath();
|
|
440
|
+
mkdirSync(path.dirname(p), { recursive: true });
|
|
441
|
+
const tmp = p + '.tmp';
|
|
442
|
+
writeFileSync(tmp, JSON.stringify({ schemaVersion: 1, updatedAt: new Date(now).toISOString(), totals }, null, 2) + '\n', 'utf8');
|
|
443
|
+
renameSync(tmp, p);
|
|
444
|
+
}
|
|
314
445
|
const ledger = ledgerPath();
|
|
315
446
|
const ledgerTmp = ledger + '.tmp';
|
|
316
447
|
writeFileSync(ledgerTmp, retained.map((e) => JSON.stringify(e)).join('\n') + (retained.length ? '\n' : ''), 'utf8');
|
|
@@ -323,4 +454,5 @@ export function pruneAdoptionLedger(now = Date.now()) {
|
|
|
323
454
|
/** 测试隔离用。 */
|
|
324
455
|
export function __resetAdoptionForTests() {
|
|
325
456
|
serial = 0;
|
|
457
|
+
suppressionBuckets = new Map();
|
|
326
458
|
}
|