@morning-start/dsh-code-toolkit 0.0.0-stage → 0.27.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/CHANGELOG.md +375 -0
- package/LICENSE +35 -0
- package/README.md +252 -2
- package/agents/caption/AGENT.md +165 -0
- package/agents/caption/examples/dispatch.md +83 -0
- package/agents/caption/examples/loop.md +66 -0
- package/agents/caption/examples/theater.md +40 -0
- package/agents/caption/references/dispatch.md +128 -0
- package/agents/caption/references/evidence.md +82 -0
- package/agents/caption/references/experts.md +141 -0
- package/agents/caption/references/flows.md +97 -0
- package/agents/caption/references/loop.md +75 -0
- package/agents/pua/AGENT.md +66 -0
- package/agents/pua/examples/run.md +41 -0
- package/agents/pua/references/flavors.md +33 -0
- package/agents/pua/references/methodology.md +32 -0
- package/agents/review/AGENT.md +112 -0
- package/agents/review/examples/clean.md +40 -0
- package/agents/review/examples/findings.md +49 -0
- package/agents/review/references/evidence.md +33 -0
- package/agents/review/references/output.md +59 -0
- package/agents/review/references/rubric.md +43 -0
- package/agents/review/references/severity.md +45 -0
- package/agents/scrub/AGENT.md +130 -0
- package/agents/scrub/examples/clean.md +70 -0
- package/agents/scrub/examples/report.md +66 -0
- package/agents/scrub/references/false-positives.md +92 -0
- package/agents/scrub/references/patterns.md +52 -0
- package/agents/scrub/references/redaction.md +53 -0
- package/agents/simplify/AGENT.md +160 -0
- package/agents/simplify/examples/apply.md +60 -0
- package/agents/simplify/examples/plan.md +65 -0
- package/agents/simplify/references/anti-patterns.md +45 -0
- package/agents/simplify/references/behavior-preservation.md +41 -0
- package/agents/simplify/references/conventions.md +34 -0
- package/agents/simplify/references/operations.md +51 -0
- package/agents/simplify/references/verification.md +74 -0
- package/cordis.patch.yml +23 -0
- package/icon.svg +8 -0
- package/index.js +207 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +62 -4
- package/src/command.js +328 -0
- package/src/delivery.js +48 -0
- package/src/i18n.js +164 -0
- package/src/journal.js +56 -0
- package/src/pua.js +48 -0
- package/src/report.js +202 -0
- package/src/runtime.js +94 -0
- package/src/selection.js +258 -0
package/src/command.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { resolveScope, scopeHasChanges, scopeInstruction, scopeLabel, selectScope, splitFlags } from './selection.js'
|
|
2
|
+
import { failureMessage, reportMessage, userMessage, SOURCE_KIND } from './delivery.js'
|
|
3
|
+
import { outputLanguage } from './i18n.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The receipt names where the task went and what it will do: the current
|
|
7
|
+
* session or an isolated agent, a plan or an edit. A bare "submitted" leaves the
|
|
8
|
+
* person guessing which of the four happened.
|
|
9
|
+
*/
|
|
10
|
+
function queuedKey(report, isolated) {
|
|
11
|
+
if (report) return isolated ? 'cmd.queuedPlanSub' : 'cmd.queuedPlan'
|
|
12
|
+
return isolated ? 'cmd.queuedSub' : 'cmd.queued'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* One command, one shell.
|
|
17
|
+
*
|
|
18
|
+
* Every command in this plugin talks to the session the same way: settle the
|
|
19
|
+
* handler at once, then either queue a task for the current agent or run an
|
|
20
|
+
* isolated one and deliver its report. What differs per command is only the
|
|
21
|
+
* spec — read-only or writable, scope or inject, structured output or plain
|
|
22
|
+
* text. Keeping that in one class is what makes the commands behave alike.
|
|
23
|
+
*/
|
|
24
|
+
export class SessionCommand {
|
|
25
|
+
/**
|
|
26
|
+
* @param {object} spec - the command's declarative shape.
|
|
27
|
+
* @param {object} host - shared services owned by `apply()`.
|
|
28
|
+
*/
|
|
29
|
+
constructor(spec, host) {
|
|
30
|
+
this.spec = spec
|
|
31
|
+
this.host = host
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Register the command against the host command plane. */
|
|
35
|
+
register(ctx) {
|
|
36
|
+
const { spec, host } = this
|
|
37
|
+
const t = (key) => host.translate(host.localeNow(), key)
|
|
38
|
+
const definition = {
|
|
39
|
+
name: spec.name,
|
|
40
|
+
description: t(spec.descriptionKey),
|
|
41
|
+
handler: (invocation) => this.handle(invocation),
|
|
42
|
+
}
|
|
43
|
+
if (spec.hintKey) definition.input = { hint: t(spec.hintKey) }
|
|
44
|
+
ctx.commands.register(definition)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** One entry point for every kind: local, inject, task. */
|
|
48
|
+
handle({ agent, rawInput, signal }) {
|
|
49
|
+
const { host, spec } = this
|
|
50
|
+
const locale = host.localeNow()
|
|
51
|
+
const t = (key) => host.translate(locale, key)
|
|
52
|
+
|
|
53
|
+
if (spec.kind === 'local') return spec.run({ agent, rawInput, t, host })
|
|
54
|
+
if (spec.kind === 'inject') return this.inject(agent, rawInput, locale, t)
|
|
55
|
+
return this.task(agent, rawInput, signal, locale, t)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Put text into the session: caption's protocol, pua's flavor. */
|
|
59
|
+
async inject(agent, rawInput, locale, t) {
|
|
60
|
+
const { spec, host } = this
|
|
61
|
+
if (spec.validate) {
|
|
62
|
+
const failure = spec.validate(rawInput, t)
|
|
63
|
+
if (failure) return { kind: 'error', text: failure }
|
|
64
|
+
}
|
|
65
|
+
if (spec.early) {
|
|
66
|
+
const result = spec.early(rawInput, t)
|
|
67
|
+
if (result) return result
|
|
68
|
+
}
|
|
69
|
+
if (typeof agent?.steer !== 'function') return { kind: 'error', text: t('error.noSteer') }
|
|
70
|
+
|
|
71
|
+
// A protocol is long; injecting it twice in one session doubles the context
|
|
72
|
+
// and gives the agent two copies to disagree with. Only the task is new.
|
|
73
|
+
const sessionId = agent.session?.id ?? agent.id
|
|
74
|
+
const previous = await host.journal.read(sessionId).catch(() => undefined)
|
|
75
|
+
const installed = previous?.command === spec.command && previous?.state === 'installed'
|
|
76
|
+
|
|
77
|
+
await agent.steer({
|
|
78
|
+
content: [{ type: 'text', text: spec.buildText({ rawInput, agent, locale, host, installed }) }],
|
|
79
|
+
// Every command delivers the same way: the host sees a plugin request, not
|
|
80
|
+
// something the person typed, so the UI folds it into one card instead of
|
|
81
|
+
// pasting the protocol into the conversation.
|
|
82
|
+
source: { kind: SOURCE_KIND },
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
await host.journal
|
|
86
|
+
.write(sessionId, {
|
|
87
|
+
state: 'installed',
|
|
88
|
+
command: spec.command,
|
|
89
|
+
at: new Date().toISOString(),
|
|
90
|
+
})
|
|
91
|
+
.catch(() => undefined)
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
kind: 'success',
|
|
95
|
+
text: installed ? spec.receipt(rawInput, t, true, agent) : spec.receipt(rawInput, t, false, agent),
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Scope-based task: inline by default, isolated with `sub`. */
|
|
100
|
+
task(agent, rawInput, signal, locale, t) {
|
|
101
|
+
const { sub, report, rest } = splitFlags(rawInput)
|
|
102
|
+
if (!agent.session?.header?.cwd) return { kind: 'error', text: t('error.noCwd') }
|
|
103
|
+
if (typeof agent?.followup !== 'function') return { kind: 'error', text: t('error.noFollowup') }
|
|
104
|
+
return sub
|
|
105
|
+
? this.startIsolated(agent, rest, signal, locale, t, report)
|
|
106
|
+
: this.startInline(agent, rest, signal, locale, t, report)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Queue the task for the current agent and settle immediately. */
|
|
110
|
+
startInline(agent, rawInput, signal, locale, t, report) {
|
|
111
|
+
const { host } = this
|
|
112
|
+
const controller = new AbortController()
|
|
113
|
+
const combined = AbortSignal.any([signal, controller.signal])
|
|
114
|
+
const sessionId = agent.session?.id ?? agent.id
|
|
115
|
+
const language = outputLanguage(locale)
|
|
116
|
+
const chinese = String(locale).startsWith('zh')
|
|
117
|
+
|
|
118
|
+
void this.prepare(agent, rawInput, combined, locale, t, sessionId, report)
|
|
119
|
+
.then(
|
|
120
|
+
(task) => {
|
|
121
|
+
if (combined.aborted) return
|
|
122
|
+
host.deliver(agent, userMessage(task))
|
|
123
|
+
},
|
|
124
|
+
(error) => {
|
|
125
|
+
if (combined.aborted) return
|
|
126
|
+
const detail = error instanceof Error ? error.message : String(error)
|
|
127
|
+
host.deliver(agent, failureMessage(detail, language, chinese))
|
|
128
|
+
},
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
return { kind: 'success', text: t(queuedKey(report, false)).replace('{scope}', '…') }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Run the isolated agent and deliver its report as a message. */
|
|
135
|
+
startIsolated(agent, rawInput, signal, locale, t, report) {
|
|
136
|
+
const { host, spec } = this
|
|
137
|
+
const key = agent.id
|
|
138
|
+
const stale = host.running.get(key)
|
|
139
|
+
// A run can hang on something outside its own control — an approval nobody
|
|
140
|
+
// answers, a child that never reports. Past the limit the slot is released
|
|
141
|
+
// so the next command is not blocked forever.
|
|
142
|
+
if (stale && Date.now() - stale.startedAt < host.runTimeoutMs) {
|
|
143
|
+
return { kind: 'error', text: t('error.busy') }
|
|
144
|
+
}
|
|
145
|
+
if (stale) {
|
|
146
|
+
stale.controller.abort()
|
|
147
|
+
host.running.delete(key)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const controller = new AbortController()
|
|
151
|
+
const combined = AbortSignal.any([signal, controller.signal])
|
|
152
|
+
host.running.set(key, { controller, label: host.translate(locale, spec.descriptionKey), startedAt: Date.now() })
|
|
153
|
+
const sessionId = agent.session?.id ?? agent.id
|
|
154
|
+
const language = outputLanguage(locale)
|
|
155
|
+
const chinese = String(locale).startsWith('zh')
|
|
156
|
+
// The receipt is resolved before the child starts, so it can name the scope
|
|
157
|
+
// the run actually took; a failure to resolve it is delivered as a message.
|
|
158
|
+
const receipt = this.resolveTask(agent, rawInput, combined, locale, t, sessionId, report, false)
|
|
159
|
+
|
|
160
|
+
void receipt
|
|
161
|
+
.then((resolved) => this.execute(agent, resolved, combined, locale, t, sessionId, report))
|
|
162
|
+
.then(
|
|
163
|
+
(text) => {
|
|
164
|
+
if (combined.aborted) return
|
|
165
|
+
host.deliver(agent, reportMessage(text, language, chinese))
|
|
166
|
+
},
|
|
167
|
+
(error) => {
|
|
168
|
+
if (combined.aborted) return
|
|
169
|
+
const detail = error instanceof Error ? error.message : String(error)
|
|
170
|
+
host.deliver(agent, failureMessage(detail, language, chinese))
|
|
171
|
+
},
|
|
172
|
+
)
|
|
173
|
+
.finally(() => host.running.delete(key))
|
|
174
|
+
|
|
175
|
+
return receipt.then((resolved) => ({ kind: 'success', text: resolved.receipt }))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The plan the person approved: the last finished dry-run report for the same
|
|
180
|
+
* command and scope. Undefined when there is none, or when it is too old to
|
|
181
|
+
* still describe the tree.
|
|
182
|
+
*/
|
|
183
|
+
async approvedPlan(sessionId, scope) {
|
|
184
|
+
const { host, spec } = this
|
|
185
|
+
const record = await host.journal.read(sessionId).catch(() => undefined)
|
|
186
|
+
if (!record || record.state !== 'finished' || record.command !== spec.command) return undefined
|
|
187
|
+
if (record.mode !== 'dry-run' || record.scope !== scope) return undefined
|
|
188
|
+
if (typeof record.result !== 'string' || !record.result.trim()) return undefined
|
|
189
|
+
const age = Date.now() - Date.parse(record.at ?? '')
|
|
190
|
+
if (!Number.isFinite(age) || age > host.planMaxAgeMs) return undefined
|
|
191
|
+
return record.result
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Resolve the scope and assemble the task text.
|
|
196
|
+
*
|
|
197
|
+
* Both delivery paths need the same thing — a concrete scope, a mode, the
|
|
198
|
+
* approved plan, the prompt — and differ only in what they do with it. Keeping
|
|
199
|
+
* the resolution here means the two paths cannot drift apart.
|
|
200
|
+
*/
|
|
201
|
+
async resolveTask(agent, rawInput, combined, locale, t, sessionId, report, inline) {
|
|
202
|
+
const { host, spec } = this
|
|
203
|
+
const cwd = agent.session?.header?.cwd
|
|
204
|
+
if (!cwd) throw new Error(t('error.noCwd'))
|
|
205
|
+
|
|
206
|
+
const { scope: symbolic } = await selectScope(host.ctx, agent, rawInput, combined, t, spec.defaultScope ?? 'project')
|
|
207
|
+
if (!symbolic) throw new Error(t('error.noScope'))
|
|
208
|
+
const scope = await resolveScope(symbolic, { cwd, signal: combined })
|
|
209
|
+
if (!(await scopeHasChanges(scope, { cwd, signal: combined }))) throw new Error(t('error.emptyScope'))
|
|
210
|
+
|
|
211
|
+
const mode = spec.forceDryRun || report ? 'dry-run' : 'apply'
|
|
212
|
+
// An apply run that follows an approved plan executes that plan; re-deriving
|
|
213
|
+
// it would let the two runs disagree about what was approved.
|
|
214
|
+
const plan = mode === 'apply' && spec.followsPlan
|
|
215
|
+
? await this.approvedPlan(sessionId, scopeLabel(scope))
|
|
216
|
+
: undefined
|
|
217
|
+
const prompt = await this.buildPrompt({ scope, mode, cwd, locale, signal: combined, inline, plan })
|
|
218
|
+
// The default scope is decided from the tree, so the receipt has to say
|
|
219
|
+
// which one it landed on — otherwise "current branch" hides whether the
|
|
220
|
+
// uncommitted work or the committed work is being looked at.
|
|
221
|
+
const receipt = t(queuedKey(report, !inline)).replace('{scope}', scopeLabel(scope))
|
|
222
|
+
|
|
223
|
+
return { scope, mode, prompt, receipt }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Resolve the scope and assemble the task text (shared by both paths). */
|
|
227
|
+
async prepare(agent, rawInput, combined, locale, t, sessionId, report) {
|
|
228
|
+
const { host, spec } = this
|
|
229
|
+
const { scope, mode, prompt } = await this.resolveTask(agent, rawInput, combined, locale, t, sessionId, report, true)
|
|
230
|
+
|
|
231
|
+
await host.journal
|
|
232
|
+
.write(sessionId, {
|
|
233
|
+
state: 'submitted',
|
|
234
|
+
command: spec.command,
|
|
235
|
+
scope: scopeLabel(scope),
|
|
236
|
+
mode,
|
|
237
|
+
at: new Date().toISOString(),
|
|
238
|
+
})
|
|
239
|
+
.catch(() => undefined)
|
|
240
|
+
|
|
241
|
+
return prompt
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** The isolated run: same preparation, then the child, then the report text. */
|
|
245
|
+
async execute(agent, { scope, mode, prompt }, combined, locale, t, sessionId) {
|
|
246
|
+
const { host, spec } = this
|
|
247
|
+
|
|
248
|
+
await host.journal
|
|
249
|
+
.write(sessionId, {
|
|
250
|
+
state: 'running',
|
|
251
|
+
command: spec.command,
|
|
252
|
+
scope: scopeLabel(scope),
|
|
253
|
+
mode,
|
|
254
|
+
at: new Date().toISOString(),
|
|
255
|
+
})
|
|
256
|
+
.catch(() => undefined)
|
|
257
|
+
|
|
258
|
+
const raw = await host.runIsolatedAgent(agent, {
|
|
259
|
+
persona: `${spec.persona.text}\n\n${outputLanguage(locale)}`,
|
|
260
|
+
prompt,
|
|
261
|
+
label: host.translate(locale, spec.descriptionKey),
|
|
262
|
+
signal: combined,
|
|
263
|
+
model: spec.model,
|
|
264
|
+
t,
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
const body = spec.structured ? host.renderReport(host.parseReport(raw), t) : raw
|
|
268
|
+
const text = [`> ${scopeLabel(scope)} · ${mode}`, '', body].join('\n')
|
|
269
|
+
|
|
270
|
+
await host.journal
|
|
271
|
+
.write(sessionId, {
|
|
272
|
+
state: 'finished',
|
|
273
|
+
command: spec.command,
|
|
274
|
+
scope: scopeLabel(scope),
|
|
275
|
+
mode,
|
|
276
|
+
result: text,
|
|
277
|
+
at: new Date().toISOString(),
|
|
278
|
+
})
|
|
279
|
+
.catch(() => undefined)
|
|
280
|
+
|
|
281
|
+
return text
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** The task text handed to either the current agent or the child. */
|
|
285
|
+
async buildPrompt({ scope, mode, cwd, locale, signal, inline, plan }) {
|
|
286
|
+
const { spec } = this
|
|
287
|
+
const chinese = String(locale).startsWith('zh')
|
|
288
|
+
const modeLine = mode === 'dry-run'
|
|
289
|
+
? (chinese ? '只出计划,不要改任何文件。' : (spec.dryRunText ?? 'Report only. Change no files.'))
|
|
290
|
+
: (chinese
|
|
291
|
+
? '按计划修改,并验证行为不变;验证不通过就回退那一处改动并如实报告。'
|
|
292
|
+
: 'Apply the changes, then verify that behavior is unchanged. If verification fails, revert that change and report it.')
|
|
293
|
+
const delivery = chinese
|
|
294
|
+
? '你的最终回复会原样发给用户,作为这次任务的结果:写清改了什么、跑了哪条命令、输出是什么、哪些没有验证。'
|
|
295
|
+
: 'Your final reply is delivered to the user as the result: say what changed, which command ran, what it printed, and what stayed unverified.'
|
|
296
|
+
const personaLine = chinese
|
|
297
|
+
? '先读参考目录下的 AGENT.md,按它的流程与判定标准执行。'
|
|
298
|
+
: 'Read AGENT.md in the reference directory first and follow its process and bar.'
|
|
299
|
+
const outputLine = inline && spec.structured
|
|
300
|
+
? (chinese
|
|
301
|
+
? '这次在会话内执行:直接写给人看的 Markdown 报告,不要输出 JSON 信封。'
|
|
302
|
+
: 'This run is in-session: write the report for a human in Markdown, not the JSON envelope.')
|
|
303
|
+
: ''
|
|
304
|
+
|
|
305
|
+
const planLine = plan
|
|
306
|
+
? (chinese
|
|
307
|
+
? '上一次的报告已由人看过并批准,下面就是那份计划:按它执行,不要重新分析、不要自己改方案。发现计划里有错或已经过期,停下来说明,不要擅自换方案。'
|
|
308
|
+
: 'The previous report was reviewed and approved by the person; it is below. Execute that plan — do not re-derive it and do not substitute your own. If the plan is wrong or stale, stop and say so instead of changing course.')
|
|
309
|
+
: ''
|
|
310
|
+
|
|
311
|
+
return [
|
|
312
|
+
chinese ? `工作目录:${cwd}` : `Working directory: ${cwd}`,
|
|
313
|
+
chinese ? `参考目录:${spec.persona.directory}` : `Reference directory: ${spec.persona.directory}`,
|
|
314
|
+
personaLine,
|
|
315
|
+
'',
|
|
316
|
+
await scopeInstruction(scope, { cwd, signal }),
|
|
317
|
+
'',
|
|
318
|
+
chinese ? `模式:${mode}。` : `Mode: ${mode}.`,
|
|
319
|
+
modeLine,
|
|
320
|
+
...(planLine ? ['', planLine, '', '---', plan, '---'] : []),
|
|
321
|
+
...(outputLine ? ['', outputLine] : []),
|
|
322
|
+
'',
|
|
323
|
+
delivery,
|
|
324
|
+
'',
|
|
325
|
+
outputLanguage(locale),
|
|
326
|
+
].join('\n')
|
|
327
|
+
}
|
|
328
|
+
}
|
package/src/delivery.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The plugin's own message source. The host rejects a shared `plugin` kind, so
|
|
5
|
+
* every producer names itself.
|
|
6
|
+
*/
|
|
7
|
+
export const SOURCE_KIND = 'morning-start-code-toolkit'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Build the user message the host queues for the current session.
|
|
11
|
+
*
|
|
12
|
+
* Mirrors `createUserMessage` from `@deepseek-ai/dsh-llm` (`{...input,
|
|
13
|
+
* role:'user', id}`), because a `link:`-installed plugin cannot resolve host
|
|
14
|
+
* packages: its bare imports start from the plugin's real path, not the
|
|
15
|
+
* profile's node_modules.
|
|
16
|
+
*/
|
|
17
|
+
export function userMessage(text) {
|
|
18
|
+
return {
|
|
19
|
+
id: randomUUID(),
|
|
20
|
+
role: 'user',
|
|
21
|
+
content: [{ type: 'text', text }],
|
|
22
|
+
source: { kind: SOURCE_KIND },
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The instruction that wraps a message the model must relay, not re-do.
|
|
28
|
+
* The two callers differ only in that opening sentence.
|
|
29
|
+
*/
|
|
30
|
+
function relayMessage(lead, body, language) {
|
|
31
|
+
return userMessage([lead, '', body, '', language].join('\n'))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The report is delivered as a task the model must relay, not re-do. */
|
|
35
|
+
export function reportMessage(report, language, chinese = false) {
|
|
36
|
+
const lead = chinese
|
|
37
|
+
? '下面是这次代码工具箱任务的完成报告。请原样转述给用户:不要增删或改写结论,也不要重新发起审查。'
|
|
38
|
+
: 'Here is the completed code-toolkit report. Present it to the user verbatim:\ndo not add, remove, or rewrite findings, and do not start another review.'
|
|
39
|
+
return relayMessage(lead, report, language)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A failed run still has to say why, or the user is left with a silent box. */
|
|
43
|
+
export function failureMessage(detail, language, chinese = false) {
|
|
44
|
+
const lead = chinese
|
|
45
|
+
? '这次代码工具箱任务没有完成。请把下面的原因原样转述给用户,不要自行重试:'
|
|
46
|
+
: 'The code-toolkit run did not complete. Present this status to the user verbatim\nand do not retry it on your own:'
|
|
47
|
+
return relayMessage(lead, detail, language)
|
|
48
|
+
}
|
package/src/i18n.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locale and strings for the plugin's own chrome.
|
|
3
|
+
*
|
|
4
|
+
* Content language follows the host's `locale.preference` setting when the
|
|
5
|
+
* host exposes it (all eight official languages); command descriptions and
|
|
6
|
+
* errors are translated for Chinese and English and fall back to English.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const SUPPORTED_LOCALES = ['zh', 'en', 'ja', 'es', 'fr', 'de', 'ru', 'pt']
|
|
10
|
+
|
|
11
|
+
const LANGUAGE_NAMES = {
|
|
12
|
+
zh: 'Simplified Chinese',
|
|
13
|
+
en: 'English',
|
|
14
|
+
ja: 'Japanese',
|
|
15
|
+
es: 'Spanish',
|
|
16
|
+
fr: 'French',
|
|
17
|
+
de: 'German',
|
|
18
|
+
ru: 'Russian',
|
|
19
|
+
pt: 'Portuguese',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const MESSAGES = {
|
|
23
|
+
zh: {
|
|
24
|
+
'cmd.simplify': '简化代码:功能逐位不变,默认直接改整个项目',
|
|
25
|
+
'cmd.review': '审查代码改动:默认出报告并修复发现的问题,加 report 只出报告',
|
|
26
|
+
'cmd.scrub': '清理隐私信息:密钥、密码、账号、本机路径,默认直接改',
|
|
27
|
+
'cmd.caption': '把难任务装进 caption 循环:一步一验,过程落成台账',
|
|
28
|
+
'hint.caption': '留空即继续当前任务;或写下任务与验收判据',
|
|
29
|
+
'caption.installed': '已装入 caption 循环:台账写到 {dir},每一步都要有命令和它的结果。',
|
|
30
|
+
'caption.reinstalled': 'caption 循环已在本次会话里,只补了任务:台账 {dir},继续按循环走。',
|
|
31
|
+
'error.noSteer': '当前宿主不支持向会话注入输入(agent.steer 不可用)。',
|
|
32
|
+
'error.noFollowup': '当前宿主不支持向会话投递任务(agent.followup 不可用)。',
|
|
33
|
+
'cmd.queued': '已交给当前会话,范围:{scope};完成后结果会作为一条消息发回。',
|
|
34
|
+
'cmd.queuedSub': '已交给独立子 agent,范围:{scope};完成后结果会作为一条消息发回。',
|
|
35
|
+
'cmd.queuedPlan': '已请它先出方案,范围:{scope},不改任何文件;方案回来后你确认,再执行。',
|
|
36
|
+
'cmd.queuedPlanSub': '已请独立子 agent 先出方案,范围:{scope},不改任何文件;方案回来后你确认,再执行。',
|
|
37
|
+
'cmd.pua': '给当前会话装上 PUA 协议:失败就换方法、拿证据、别敷衍',
|
|
38
|
+
'hint.pua': '留空用默认风味;或 list / alibaba / huawei / musk 等',
|
|
39
|
+
'pua.installed': '已装上 PUA 协议({flavor}):同类尝试失败两次就换方法,说完成必须给出命令和输出。',
|
|
40
|
+
'pua.list': '可用风味:',
|
|
41
|
+
'cmd.status': '查看代码工具箱任务状态或最近结果',
|
|
42
|
+
'cmd.cancel': '取消当前代码工具箱任务',
|
|
43
|
+
'hint.scope': '留空即整个项目(当前分支);或 --files a.js,b.js / --branch main(对比哪个分支,默认基线)/ --commit <sha> / 自定义说明;加 sub 用子 agent,加 report 只出报告',
|
|
44
|
+
'hint.scrub': '留空即整个项目;或 --files a.js,.env / 自定义说明;加 sub 用子 agent,加 report 只出报告',
|
|
45
|
+
'hint.review': '留空即审未提交的改动(都提交了就审分支相对基线);或 --files / --branch <对比分支> / --commit <sha>;默认出报告并修复,加 report 只出报告,加 sub 用子 agent',
|
|
46
|
+
'error.noCwd': '当前会话没有工作目录。',
|
|
47
|
+
'error.noProvider': '当前宿主缺少独立上下文的原生 spawn provider,无法运行独立 agent。',
|
|
48
|
+
'error.noActivation': '当前宿主缺少原生子 agent 启动接口,请升级 DeepSeek Harness。',
|
|
49
|
+
'error.noGit': '不是 Git 仓库,或 git 不可用:',
|
|
50
|
+
'error.aborted': '操作已取消。',
|
|
51
|
+
'error.noResult': '子 agent 没有返回报告。',
|
|
52
|
+
'error.busy': '当前会话已有代码工具箱任务在运行,请等待或先取消。',
|
|
53
|
+
'error.noScope': '没有选中有效范围。',
|
|
54
|
+
'error.emptyScope': '没有可审查的改动:当前分支与基线一致,工作区也是干净的。试试 --branch <别的分支>、--commit <sha> 或 --files <路径>。',
|
|
55
|
+
'error.emptyInstructions': '自定义说明不能为空。',
|
|
56
|
+
'error.reportDirectory': 'reportDirectory 必须是绝对路径。',
|
|
57
|
+
'error.model': '模型名不能为空。',
|
|
58
|
+
'error.ledgerDirectory': 'ledgerDirectory 不能为空。',
|
|
59
|
+
'error.runTimeout': 'runTimeoutMinutes 必须是非负数(0 表示不限制)。',
|
|
60
|
+
'error.planMaxAge': 'planMaxAgeMinutes 必须是非负数(0 表示不限制)。',
|
|
61
|
+
'status.none': '当前会话还没有代码工具箱任务记录。',
|
|
62
|
+
'status.running': '正在运行:',
|
|
63
|
+
'status.last': '最近一次:',
|
|
64
|
+
'status.interrupted': '上次任务有启动记录但没有完成报告,可以重新运行。',
|
|
65
|
+
'status.submitted': '已提交到当前会话,结果在会话里:',
|
|
66
|
+
'cancel.none': '没有正在运行的代码工具箱任务。',
|
|
67
|
+
'cancel.done': '已请求取消。',
|
|
68
|
+
'report.verdict': '结论:',
|
|
69
|
+
'report.noFindings': '没有发现需要处理的问题。',
|
|
70
|
+
'report.limitation': '未覆盖:',
|
|
71
|
+
'report.fixed': '已修复',
|
|
72
|
+
'report.verifiedBy': '验证:',
|
|
73
|
+
'mode.dryRun': 'dry-run(未改文件)',
|
|
74
|
+
'mode.apply': 'apply(已改文件)',
|
|
75
|
+
},
|
|
76
|
+
en: {
|
|
77
|
+
'cmd.simplify': 'Simplify code without changing behavior; writes the whole project by default',
|
|
78
|
+
'cmd.review': 'Review a change and fix what it finds; add report for the report only',
|
|
79
|
+
'cmd.scrub': 'Find and redact secrets, credentials, and personal data; writes by default',
|
|
80
|
+
'cmd.caption': 'Run a hard task through the caption loop: one step, one proof, a ledger',
|
|
81
|
+
'hint.caption': 'Leave empty to continue the current task; or state the task and its acceptance criteria',
|
|
82
|
+
'caption.installed': 'Caption loop installed: keep the ledger in {dir}, with a command and its result for every step.',
|
|
83
|
+
'caption.reinstalled': 'The caption loop is already installed in this session; only the task was added. Ledger: {dir}.',
|
|
84
|
+
'error.noSteer': 'This host cannot inject input into the session (agent.steer is unavailable).',
|
|
85
|
+
'error.noFollowup': 'This host cannot queue a task into the session (agent.followup is unavailable).',
|
|
86
|
+
'cmd.queued': 'Handed to this session; scope: {scope}. The result arrives as a message when the run finishes.',
|
|
87
|
+
'cmd.queuedSub': 'Handed to an isolated agent; scope: {scope}. The result arrives as a message when the run finishes.',
|
|
88
|
+
'cmd.queuedPlan': 'Asked for a plan first; scope: {scope}, no files will change. Confirm it when it arrives, then run the command again to execute.',
|
|
89
|
+
'cmd.queuedPlanSub': 'Asked the isolated agent for a plan first; scope: {scope}, no files will change. Confirm it when it arrives, then run the command again to execute.',
|
|
90
|
+
'cmd.pua': 'Install the PUA protocol in this session: change method, bring evidence, stop shrugging',
|
|
91
|
+
'hint.pua': 'Leave empty for the default flavor; or list / alibaba / huawei / musk',
|
|
92
|
+
'pua.installed': 'PUA protocol installed ({flavor}): two failures of the same kind mean change the method, and "done" needs a command and its output.',
|
|
93
|
+
'pua.list': 'Available flavors:',
|
|
94
|
+
'cmd.status': 'Show code-toolkit task status or the latest result',
|
|
95
|
+
'cmd.cancel': 'Cancel the running code-toolkit task',
|
|
96
|
+
'hint.scope': 'Leave empty for the whole project on the current branch; or --files a.js,b.js / --branch main (which branch to compare against; the base by default) / --commit <sha> / custom instructions; add sub for a subagent, report for a plan only',
|
|
97
|
+
'hint.scrub': 'Leave empty for the whole project; or --files a.js,.env / custom instructions; add sub for a subagent, report for a plan only',
|
|
98
|
+
'hint.review': 'Leave empty to review the uncommitted work (or the branch against its base when clean); or --files / --branch <ref> / --commit <sha>; fixes what it finds by default — add report for the report only, sub for a subagent',
|
|
99
|
+
'error.noCwd': 'This session has no working directory.',
|
|
100
|
+
'error.noProvider': 'This host has no independent-context native spawn provider, so no isolated agent can run.',
|
|
101
|
+
'error.noActivation': 'This host has no native subagent activation API; upgrade DeepSeek Harness.',
|
|
102
|
+
'error.noGit': 'Not a Git repository, or git is unavailable: ',
|
|
103
|
+
'error.aborted': 'Cancelled.',
|
|
104
|
+
'error.noResult': 'The subagent returned no report.',
|
|
105
|
+
'error.busy': 'A code-toolkit task is already running in this session; wait or cancel it first.',
|
|
106
|
+
'error.noScope': 'No valid scope was selected.',
|
|
107
|
+
'error.emptyScope': 'Nothing to review: the branch matches its base and the working tree is clean. Try --branch <another branch>, --commit <sha>, or --files <path>.',
|
|
108
|
+
'error.emptyInstructions': 'Custom instructions cannot be empty.',
|
|
109
|
+
'error.reportDirectory': 'reportDirectory must be an absolute path.',
|
|
110
|
+
'error.model': 'A model name cannot be empty.',
|
|
111
|
+
'error.ledgerDirectory': 'ledgerDirectory cannot be empty.',
|
|
112
|
+
'error.runTimeout': 'runTimeoutMinutes must be a non-negative number (0 disables the limit).',
|
|
113
|
+
'error.planMaxAge': 'planMaxAgeMinutes must be a non-negative number (0 disables the limit).',
|
|
114
|
+
'status.none': 'No code-toolkit task recorded in this session yet.',
|
|
115
|
+
'status.running': 'Running: ',
|
|
116
|
+
'status.last': 'Last run: ',
|
|
117
|
+
'status.interrupted': 'The previous run started but never finished; run it again.',
|
|
118
|
+
'status.submitted': 'Submitted to this session; the result is in the conversation: ',
|
|
119
|
+
'cancel.none': 'No code-toolkit task is running.',
|
|
120
|
+
'cancel.done': 'Cancellation requested.',
|
|
121
|
+
'report.verdict': 'Verdict: ',
|
|
122
|
+
'report.noFindings': 'No findings worth acting on.',
|
|
123
|
+
'report.limitation': 'Not checked: ',
|
|
124
|
+
'report.fixed': 'Fixed',
|
|
125
|
+
'report.verifiedBy': 'Verified by: ',
|
|
126
|
+
'mode.dryRun': 'dry-run (no files changed)',
|
|
127
|
+
'mode.apply': 'apply (files changed)',
|
|
128
|
+
},
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Narrow any tag to one of the eight supported locales. */
|
|
132
|
+
export function resolveLocale(value) {
|
|
133
|
+
if (typeof value !== 'string' || !value.trim()) return 'zh'
|
|
134
|
+
const tag = value.trim().toLowerCase()
|
|
135
|
+
const hit = SUPPORTED_LOCALES.find((locale) => tag.startsWith(locale))
|
|
136
|
+
return hit ?? 'en'
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Read `locale.preference` from the host settings service, if it is there. */
|
|
140
|
+
export function readHostLocale(ctx, fallback) {
|
|
141
|
+
try {
|
|
142
|
+
const settings = ctx.get('settings')
|
|
143
|
+
if (!settings || typeof settings.describe !== 'function') return fallback
|
|
144
|
+
const forms = settings.describe()
|
|
145
|
+
if (!Array.isArray(forms)) return fallback
|
|
146
|
+
const form = forms.find((item) => item && typeof item === 'object' && item.ns === 'locale')
|
|
147
|
+
const value = form?.value
|
|
148
|
+
const preference = value && typeof value === 'object' ? value.preference : value
|
|
149
|
+
return typeof preference === 'string' && preference.trim() ? resolveLocale(preference) : fallback
|
|
150
|
+
} catch {
|
|
151
|
+
return fallback
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Translate one key; unknown keys fall back to English, then to the key itself. */
|
|
156
|
+
export function translate(locale, key) {
|
|
157
|
+
const table = MESSAGES[locale] ?? MESSAGES.en
|
|
158
|
+
return table[key] ?? MESSAGES.en[key] ?? key
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The sentence appended to a task so the child answers in the user's language. */
|
|
162
|
+
export function outputLanguage(locale) {
|
|
163
|
+
return `Write all user-facing text in ${LANGUAGE_NAMES[locale] ?? LANGUAGE_NAMES.en}.`
|
|
164
|
+
}
|
package/src/journal.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
2
|
+
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { homedir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One durable record per session: what ran, when, and the rendered result.
|
|
8
|
+
*
|
|
9
|
+
* The session log is the source of truth for conversation; this is the
|
|
10
|
+
* plugin's own note, so `/code-status` still works after a restart and a run
|
|
11
|
+
* that died mid-flight is distinguishable from one that never started.
|
|
12
|
+
*/
|
|
13
|
+
export class Journal {
|
|
14
|
+
constructor(directory) {
|
|
15
|
+
this.directory = directory
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static defaultDirectory() {
|
|
19
|
+
const home = process.env.DSH_HOME ?? join(homedir(), '.dsh')
|
|
20
|
+
return join(home, 'code-toolkit')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
path(sessionId) {
|
|
24
|
+
return join(this.directory, `${createHash('sha256').update(sessionId).digest('hex')}.json`)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async read(sessionId) {
|
|
28
|
+
let raw
|
|
29
|
+
try {
|
|
30
|
+
raw = await readFile(this.path(sessionId), 'utf8')
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (error?.code === 'ENOENT') return undefined
|
|
33
|
+
throw error
|
|
34
|
+
}
|
|
35
|
+
const record = JSON.parse(raw)
|
|
36
|
+
if (!record || record.version !== 1) return undefined
|
|
37
|
+
return record
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async write(sessionId, record) {
|
|
41
|
+
await mkdir(this.directory, { recursive: true })
|
|
42
|
+
const destination = this.path(sessionId)
|
|
43
|
+
const temp = `${destination}.${randomUUID()}.tmp`
|
|
44
|
+
try {
|
|
45
|
+
await writeFile(temp, `${JSON.stringify({ version: 1, ...record })}\n`, { encoding: 'utf8', flag: 'wx' })
|
|
46
|
+
await rename(temp, destination)
|
|
47
|
+
} catch (error) {
|
|
48
|
+
try {
|
|
49
|
+
await unlink(temp)
|
|
50
|
+
} catch {
|
|
51
|
+
/* temp file never existed or is already gone */
|
|
52
|
+
}
|
|
53
|
+
throw error
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/pua.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Corporate flavors for the PUA protocol.
|
|
3
|
+
*
|
|
4
|
+
* Source: https://github.com/tanweai/pua (MIT). The rhetoric is quoted from that
|
|
5
|
+
* project; the escalation rules live in `agents/pua/AGENT.md` and do not change
|
|
6
|
+
* with the flavor.
|
|
7
|
+
*/
|
|
8
|
+
export const FLAVORS = [
|
|
9
|
+
{ id: 'alibaba', label: '阿里', line: '底层逻辑是什么?抓手在哪里?闭环了吗?', method: '定目标 → 追过程 → 拿结果;复盘四步法' },
|
|
10
|
+
{ id: 'bytedance', label: '字节', line: 'ROI 太低了。Always Day 1。要么上线,要么闭嘴。', method: '一切皆 A/B;数据说话;速度优先于完美' },
|
|
11
|
+
{ id: 'huawei', label: '华为', line: '烧不死的鸟才是凤凰。', method: '5-Why 追根因;蓝军自我攻击;压强集中' },
|
|
12
|
+
{ id: 'tencent', label: '腾讯', line: '我让另一个 agent 也在看这个,赛马。', method: '多方案并行;MVP;灰度发布' },
|
|
13
|
+
{ id: 'baidu', label: '百度', line: '先搜。简单可依赖。', method: '搜索是第一步,不是可选项' },
|
|
14
|
+
{ id: 'meituan', label: '美团', line: '做难而正确的事。', method: '效率优先;标准化再规模化;长期复利' },
|
|
15
|
+
{ id: 'musk', label: '马斯克', line: 'Extremely hardcore. Ship or die.', method: '质疑 → 删除 → 简化 → 加速 → 自动化' },
|
|
16
|
+
{ id: 'jobs', label: '乔布斯', line: 'A players or B players?', method: '减法优先于加法;DRI;像素级;原型驱动' },
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
export const DEFAULT_FLAVOR = 'alibaba'
|
|
20
|
+
|
|
21
|
+
/** Accept an id or a Chinese label; fall back to the default flavor. */
|
|
22
|
+
export function resolveFlavor(input) {
|
|
23
|
+
const wanted = String(input ?? '').trim().toLowerCase()
|
|
24
|
+
if (!wanted) return FLAVORS.find((flavor) => flavor.id === DEFAULT_FLAVOR)
|
|
25
|
+
return FLAVORS.find((flavor) => flavor.id === wanted || flavor.label === String(input).trim())
|
|
26
|
+
?? FLAVORS.find((flavor) => flavor.id === DEFAULT_FLAVOR)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** One line per flavor, for `/pua list`. */
|
|
30
|
+
export function flavorList(header) {
|
|
31
|
+
return [header, '', ...FLAVORS.map((flavor) => `- \`${flavor.id}\` ${flavor.label} — ${flavor.line}`)].join('\n')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The protocol plus the chosen flavor, ready to steer into the session. */
|
|
35
|
+
export function buildPua(flavor, persona, language) {
|
|
36
|
+
return [
|
|
37
|
+
persona.trim(),
|
|
38
|
+
'',
|
|
39
|
+
'## This session\'s flavor',
|
|
40
|
+
'',
|
|
41
|
+
`${flavor.label}:${flavor.line}`,
|
|
42
|
+
`Method: ${flavor.method}`,
|
|
43
|
+
'',
|
|
44
|
+
'The flavor sets the tone; the red lines and the five steps still decide what you do.',
|
|
45
|
+
'',
|
|
46
|
+
language,
|
|
47
|
+
].join('\n')
|
|
48
|
+
}
|