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/host/protocol.js
CHANGED
|
@@ -1,24 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 宿主协议版本 —— **在这里定义**,不从 `@mocode/protocol` 值导入。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不能 import 值:`@mocode/protocol` 是 private workspace 包,不随根包发到 npm。
|
|
5
|
+
* 根包的 `dependencies` 里没有它,而 `bin/mocode-agent-host.js` 是用户真正会执行的入口。
|
|
6
|
+
* 一旦这里出现值导入,编译产物 `dist/host/protocol.js` 会留下裸 import,tarball 安装后
|
|
7
|
+
* 宿主一启动就是 `ERR_MODULE_NOT_FOUND: Cannot find package '@mocode/protocol'` ——
|
|
8
|
+
* 1.10.0 的隔离安装 smoke 就是这么发现的(类型导入会被编译期擦除,所以以前没暴露)。
|
|
9
|
+
*
|
|
10
|
+
* 版本号的事实源仍是 `packages/protocol/src/host.ts`;两处相等由
|
|
11
|
+
* `tests/host/protocol-version.test.ts` 扫描源码断言,改一处忘另一处会直接红。
|
|
12
|
+
*/
|
|
13
|
+
export const HOST_PROTOCOL_VERSION = 4;
|
|
1
14
|
function parseAttachments(value) {
|
|
2
15
|
if (!Array.isArray(value))
|
|
3
16
|
return undefined;
|
|
4
|
-
|
|
17
|
+
const out = [];
|
|
18
|
+
for (const item of value.slice(0, 8)) {
|
|
5
19
|
if (!item || typeof item !== 'object')
|
|
6
|
-
|
|
20
|
+
continue;
|
|
7
21
|
const entry = item;
|
|
8
|
-
if (typeof entry.name !== 'string'
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
22
|
+
if (typeof entry.name !== 'string')
|
|
23
|
+
continue;
|
|
24
|
+
const name = entry.name.slice(0, 240);
|
|
25
|
+
if (typeof entry.dataUrl === 'string' && /^data:image\/(?:png|jpeg|gif|webp);base64,/.test(entry.dataUrl)) {
|
|
26
|
+
out.push({ name, dataUrl: entry.dataUrl });
|
|
27
|
+
}
|
|
28
|
+
else if (typeof entry.text === 'string' && entry.text.trim() && entry.text.length <= 100_000) {
|
|
29
|
+
out.push({ name, text: entry.text, truncated: entry.truncated === true });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
/** 有限枚举白名单:拼错/未知取值一律 null,由调用方当作非法命令拒掉(fail-closed)。 */
|
|
35
|
+
function oneOf(value, allowed) {
|
|
36
|
+
return typeof value === 'string' && allowed.includes(value) ? value : null;
|
|
12
37
|
}
|
|
38
|
+
const SESSION_ACTIONS = ['list', 'search', 'resume', 'fork', 'workspaces'];
|
|
39
|
+
const SESSION_SCOPES = ['current', 'all'];
|
|
40
|
+
const MEMORY_ACTIONS = [
|
|
41
|
+
'overview',
|
|
42
|
+
'health',
|
|
43
|
+
'audit',
|
|
44
|
+
'profile',
|
|
45
|
+
'confirm',
|
|
46
|
+
'drop',
|
|
47
|
+
'search',
|
|
48
|
+
'forget',
|
|
49
|
+
'reflect',
|
|
50
|
+
];
|
|
51
|
+
const MODES = ['auto', 'plan'];
|
|
52
|
+
/** 正整数上限解析:非法/负数一律 undefined,交给 host 侧用默认值。 */
|
|
53
|
+
function positiveInt(value, max = 10_000) {
|
|
54
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
55
|
+
return undefined;
|
|
56
|
+
const n = Math.floor(value);
|
|
57
|
+
return n > 0 && n <= max ? n : undefined;
|
|
58
|
+
}
|
|
59
|
+
function nonNegativeInt(value) {
|
|
60
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
61
|
+
return undefined;
|
|
62
|
+
const n = Math.floor(value);
|
|
63
|
+
return n >= 0 ? n : undefined;
|
|
64
|
+
}
|
|
65
|
+
function optionalString(value, max = 500) {
|
|
66
|
+
return typeof value === 'string' ? value.slice(0, max) : undefined;
|
|
67
|
+
}
|
|
68
|
+
/** 文件路径列表:只收字符串,逐个截断,去重后限量。 */
|
|
69
|
+
function parsePaths(value, max = 200) {
|
|
70
|
+
if (!Array.isArray(value))
|
|
71
|
+
return undefined;
|
|
72
|
+
const out = [];
|
|
73
|
+
for (const item of value) {
|
|
74
|
+
if (typeof item !== 'string' || !item)
|
|
75
|
+
continue;
|
|
76
|
+
const trimmed = item.slice(0, 1000);
|
|
77
|
+
if (!out.includes(trimmed))
|
|
78
|
+
out.push(trimmed);
|
|
79
|
+
if (out.length >= max)
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
/** 设置 patch:只接受 string / number / boolean / null,其余键直接丢弃。 */
|
|
85
|
+
function parseSettingPatch(value) {
|
|
86
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
87
|
+
return undefined;
|
|
88
|
+
const out = {};
|
|
89
|
+
for (const [key, raw] of Object.entries(value)) {
|
|
90
|
+
if (!/^[A-Z][A-Z0-9_]{1,63}$/.test(key))
|
|
91
|
+
continue;
|
|
92
|
+
if (raw === null)
|
|
93
|
+
out[key] = null;
|
|
94
|
+
else if (typeof raw === 'string' || typeof raw === 'boolean')
|
|
95
|
+
out[key] = raw;
|
|
96
|
+
else if (typeof raw === 'number' && Number.isFinite(raw))
|
|
97
|
+
out[key] = raw;
|
|
98
|
+
}
|
|
99
|
+
return Object.keys(out).length ? out : undefined;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* NDJSON 行 → HostCommand。未知 type / 必填字段缺失 / 枚举越界一律返回 null。
|
|
103
|
+
*
|
|
104
|
+
* 这里是 host 的唯一入口,所以每条命令都必须显式解析(不做透传):客户端多塞的字段
|
|
105
|
+
* 不会自己流进业务层,避免「客户端决定 host 行为」。
|
|
106
|
+
*/
|
|
13
107
|
export function parseCommand(value) {
|
|
14
108
|
if (!value || typeof value !== 'object')
|
|
15
109
|
return null;
|
|
16
110
|
const input = value;
|
|
17
111
|
if (typeof input.id !== 'string' || typeof input.type !== 'string')
|
|
18
112
|
return null;
|
|
113
|
+
const id = input.id;
|
|
19
114
|
if (input.type === 'run' && typeof input.prompt === 'string') {
|
|
20
115
|
return {
|
|
21
|
-
id
|
|
116
|
+
id,
|
|
22
117
|
type: 'run',
|
|
23
118
|
prompt: input.prompt,
|
|
24
119
|
sessionId: typeof input.sessionId === 'string' ? input.sessionId : undefined,
|
|
@@ -26,12 +121,12 @@ export function parseCommand(value) {
|
|
|
26
121
|
};
|
|
27
122
|
}
|
|
28
123
|
if (input.type === 'cancel')
|
|
29
|
-
return { id
|
|
124
|
+
return { id, type: 'cancel' };
|
|
30
125
|
if (input.type === 'compact')
|
|
31
|
-
return { id
|
|
126
|
+
return { id, type: 'compact', focus: typeof input.focus === 'string' ? input.focus : undefined };
|
|
32
127
|
if (input.type === 'approval' && typeof input.approvalId === 'string') {
|
|
33
128
|
return {
|
|
34
|
-
id
|
|
129
|
+
id,
|
|
35
130
|
type: 'approval',
|
|
36
131
|
approvalId: input.approvalId,
|
|
37
132
|
// 白名单而不是强转:未知取值一律当取消(fail-closed),不给「拼错的 action 意外放行」留口子。
|
|
@@ -41,5 +136,105 @@ export function parseCommand(value) {
|
|
|
41
136
|
value: typeof input.value === 'string' ? input.value : undefined,
|
|
42
137
|
};
|
|
43
138
|
}
|
|
139
|
+
if (input.type === 'set_mode') {
|
|
140
|
+
const mode = oneOf(input.mode, MODES);
|
|
141
|
+
return mode ? { id, type: 'set_mode', mode } : null;
|
|
142
|
+
}
|
|
143
|
+
if (input.type === 'sessions') {
|
|
144
|
+
const action = oneOf(input.action, SESSION_ACTIONS);
|
|
145
|
+
if (!action)
|
|
146
|
+
return null;
|
|
147
|
+
return {
|
|
148
|
+
id,
|
|
149
|
+
type: 'sessions',
|
|
150
|
+
action,
|
|
151
|
+
query: optionalString(input.query, 200),
|
|
152
|
+
limit: positiveInt(input.limit, 200),
|
|
153
|
+
sessionId: optionalString(input.sessionId, 200),
|
|
154
|
+
// scope 只对 list 有意义;未知取值当缺省(current),不因此拒掉整条命令。
|
|
155
|
+
scope: oneOf(input.scope, SESSION_SCOPES) ?? undefined,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
if (input.type === 'rollback') {
|
|
159
|
+
const action = oneOf(input.action, ['list', 'apply']);
|
|
160
|
+
if (!action)
|
|
161
|
+
return null;
|
|
162
|
+
// apply 必须带合法 turnIndex,否则解析成 null —— 不给「缺参数时默认回滚最近一轮」留口子。
|
|
163
|
+
const turnIndex = nonNegativeInt(input.turnIndex);
|
|
164
|
+
if (action === 'apply' && turnIndex === undefined)
|
|
165
|
+
return null;
|
|
166
|
+
return { id, type: 'rollback', action, turnIndex, revertFiles: parsePaths(input.revertFiles) };
|
|
167
|
+
}
|
|
168
|
+
if (input.type === 'changes') {
|
|
169
|
+
const action = oneOf(input.action, ['list', 'diff']);
|
|
170
|
+
if (!action)
|
|
171
|
+
return null;
|
|
172
|
+
return {
|
|
173
|
+
id,
|
|
174
|
+
type: 'changes',
|
|
175
|
+
action,
|
|
176
|
+
turn: nonNegativeInt(input.turn),
|
|
177
|
+
path: optionalString(input.path, 1000),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
if (input.type === 'usage') {
|
|
181
|
+
const scope = oneOf(input.scope, ['session', 'global']);
|
|
182
|
+
if (!scope)
|
|
183
|
+
return null;
|
|
184
|
+
return {
|
|
185
|
+
id,
|
|
186
|
+
type: 'usage',
|
|
187
|
+
scope,
|
|
188
|
+
days: positiveInt(input.days, 366),
|
|
189
|
+
// session 口径可显式指定会话:懒启动的 host 进程内 sessionId 是空串。
|
|
190
|
+
sessionId: optionalString(input.sessionId, 200),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
if (input.type === 'context_usage') {
|
|
194
|
+
return { id, type: 'context_usage', sessionId: optionalString(input.sessionId, 200) };
|
|
195
|
+
}
|
|
196
|
+
if (input.type === 'memory') {
|
|
197
|
+
const action = oneOf(input.action, MEMORY_ACTIONS);
|
|
198
|
+
if (!action)
|
|
199
|
+
return null;
|
|
200
|
+
return {
|
|
201
|
+
id,
|
|
202
|
+
type: 'memory',
|
|
203
|
+
action,
|
|
204
|
+
query: optionalString(input.query, 200),
|
|
205
|
+
entryId: optionalString(input.entryId, 200),
|
|
206
|
+
limit: positiveInt(input.limit, 200),
|
|
207
|
+
confirm: input.confirm === true,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
if (input.type === 'commit_message') {
|
|
211
|
+
const diff = optionalString(input.diff, 120_000);
|
|
212
|
+
const stat = optionalString(input.stat, 24_000);
|
|
213
|
+
const files = parsePaths(input.files, 2_000);
|
|
214
|
+
if (!diff || !stat || !files?.length)
|
|
215
|
+
return null;
|
|
216
|
+
return {
|
|
217
|
+
id,
|
|
218
|
+
type: 'commit_message',
|
|
219
|
+
diff,
|
|
220
|
+
stat,
|
|
221
|
+
files,
|
|
222
|
+
truncated: input.truncated === true,
|
|
223
|
+
language: oneOf(input.language, ['zh-CN', 'en-US']) ?? undefined,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
if (input.type === 'cancel_commit_message') {
|
|
227
|
+
const targetId = optionalString(input.targetId, 200);
|
|
228
|
+
return targetId ? { id, type: 'cancel_commit_message', targetId } : null;
|
|
229
|
+
}
|
|
230
|
+
if (input.type === 'settings') {
|
|
231
|
+
const action = oneOf(input.action, ['get', 'set']);
|
|
232
|
+
if (!action)
|
|
233
|
+
return null;
|
|
234
|
+
const patch = parseSettingPatch(input.patch);
|
|
235
|
+
if (action === 'set' && !patch)
|
|
236
|
+
return null;
|
|
237
|
+
return { id, type: 'settings', action, patch };
|
|
238
|
+
}
|
|
44
239
|
return null;
|
|
45
240
|
}
|