dsh-coding 0.0.1 → 0.1.1-rc2

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.
Files changed (78) hide show
  1. package/AGENTS.md +80 -0
  2. package/LICENSE +21 -21
  3. package/README.md +120 -54
  4. package/cordis.patch.yml +274 -4
  5. package/docs/CHANGELOG.md +47 -0
  6. package/docs/DETAIL.md +272 -0
  7. package/icon.svg +6 -6
  8. package/lib/apply-patch.js +268 -0
  9. package/lib/client/capsule.js +120 -0
  10. package/lib/client/card.js +308 -0
  11. package/lib/client/command-row.js +76 -0
  12. package/lib/client/config-page.js +392 -0
  13. package/lib/client/definition.js +164 -0
  14. package/lib/client/index.js +129 -0
  15. package/lib/client/locales.js +170 -0
  16. package/lib/client/mode-picker.js +77 -0
  17. package/lib/client/styles.js +283 -0
  18. package/lib/client.js +1802 -0
  19. package/lib/coding-projection.js +349 -0
  20. package/lib/config.js +218 -0
  21. package/lib/contract.js +138 -0
  22. package/lib/editor.js +23 -0
  23. package/lib/failures.js +142 -0
  24. package/lib/handoff.js +347 -0
  25. package/lib/index.js +110 -7
  26. package/lib/language.js +136 -0
  27. package/lib/memory.js +336 -0
  28. package/lib/messages.js +43 -0
  29. package/lib/models.js +154 -0
  30. package/lib/patch.js +235 -0
  31. package/lib/preset.js +1552 -0
  32. package/lib/reasoning-levels.js +28 -0
  33. package/lib/reasoning-projection.js +178 -0
  34. package/lib/reasoning.js +585 -0
  35. package/lib/stages.js +344 -0
  36. package/lib/state.js +585 -0
  37. package/lib/system-language.js +56 -0
  38. package/lib/types/apply-patch.d.ts +13 -0
  39. package/lib/types/client/capsule.d.ts +9 -0
  40. package/lib/types/client/card.d.ts +76 -0
  41. package/lib/types/client/command-row.d.ts +42 -0
  42. package/lib/types/client/config-page.d.ts +136 -0
  43. package/lib/types/client/definition.d.ts +51 -0
  44. package/lib/types/client/index.d.ts +10 -0
  45. package/lib/types/client/locales.d.ts +18 -0
  46. package/lib/types/client/mode-picker.d.ts +25 -0
  47. package/lib/types/client/styles.d.ts +28 -0
  48. package/lib/types/coding-projection.d.ts +116 -0
  49. package/lib/types/config.d.ts +208 -0
  50. package/lib/types/contract.d.ts +33 -0
  51. package/lib/types/editor.d.ts +9 -0
  52. package/lib/types/failures.d.ts +62 -0
  53. package/lib/types/handoff.d.ts +132 -0
  54. package/lib/types/index.d.ts +150 -0
  55. package/lib/types/language.d.ts +56 -0
  56. package/lib/types/memory.d.ts +71 -0
  57. package/lib/types/messages.d.ts +27 -0
  58. package/lib/types/models.d.ts +82 -0
  59. package/lib/types/patch.d.ts +37 -0
  60. package/lib/types/preset.d.ts +22 -0
  61. package/lib/types/reasoning-levels.d.ts +20 -0
  62. package/lib/types/reasoning-projection.d.ts +64 -0
  63. package/lib/types/reasoning.d.ts +141 -0
  64. package/lib/types/stages.d.ts +193 -0
  65. package/lib/types/state.d.ts +273 -0
  66. package/lib/types/system-language.d.ts +10 -0
  67. package/lib/types/verification-tool.d.ts +32 -0
  68. package/lib/types/verification.d.ts +77 -0
  69. package/lib/types/verifier.d.ts +9 -0
  70. package/lib/types/volatile.d.ts +17 -0
  71. package/lib/verification-tool.js +176 -0
  72. package/lib/verification.js +337 -0
  73. package/lib/verifier.js +25 -0
  74. package/lib/volatile.js +20 -0
  75. package/locale/en.json +3 -2
  76. package/locale/zh.json +3 -2
  77. package/package.json +84 -9
  78. package/lib/index.d.ts +0 -11
@@ -0,0 +1,308 @@
1
+ /**
2
+ * Per-turn Coding control card, rendered at the tail of every closed Turn.
3
+ *
4
+ * This is the surface the old plugin had — one card per Turn showing the mode
5
+ * that Turn ran under and letting the newest card switch it — extended with the
6
+ * runtime's stage, plan, and verification state. It reads the durable
7
+ * `dshCodingStage` projection, never the Host's in-memory machine, so a
8
+ * reloaded session shows what actually happened.
9
+ *
10
+ * Only the newest Turn's card is a control. Older cards keep the mode and stage
11
+ * they ran under, with their buttons disabled, so a later switch never rewrites
12
+ * history. Mode buttons go back through the Host as the silent `/coding set`
13
+ * verb, so pressing one applies the mode without starting a Turn or producing a
14
+ * model reply.
15
+ *
16
+ * All visual styling lives in `./styles.ts` and follows DSH's own recipes; this
17
+ * component only composes the shipped classes.
18
+ *
19
+ * @module dsh-coding/client/card
20
+ */
21
+ import { createElement as h, memo, useState } from 'react';
22
+ import { CODING_PROJECTION_KEY, } from '../coding-projection.js';
23
+ import { REASONING_PROJECTION_KEY, } from '../reasoning-projection.js';
24
+ import { levelForEffort } from '../reasoning-levels.js';
25
+ import { STAGES_BY_MODE } from '../stages.js';
26
+ import { commandRejected } from './command-row.js';
27
+ /** Modes the card can switch to, in roster order. */
28
+ const MODES = ['auto', 'consult', 'review', 'codex'];
29
+ /**
30
+ * The state one card renders: the live projection for the newest closed Turn,
31
+ * or the frozen snapshot that Turn ran under. Pure, so the split between a
32
+ * control card and a history card is testable without React.
33
+ * @param view - current projection value.
34
+ * @param turn - Turn the card belongs to, when the owner declares one.
35
+ * @returns the values rendered, plus whether this card may switch the mode
36
+ * (`live`) and whether it may write the handoff document (`canMemory`).
37
+ */
38
+ export function cardState(view, turn) {
39
+ const turns = Array.isArray(view.turns) ? view.turns : [];
40
+ const frozen = turn === undefined ? undefined : turns.find(item => item.turn === turn);
41
+ // A tail is published on `turn/end`, so a Turn that merely started has no
42
+ // card; treating a newer Turn as live keeps the newest card switchable.
43
+ const live = turn === undefined || turn >= view.latest;
44
+ const mode = (live ? view.mode : frozen?.mode ?? view.mode);
45
+ const stage = (live ? view.stage : frozen?.stage ?? view.stage);
46
+ const verification = (live ? view.verification : frozen?.verification ?? view.verification) ?? null;
47
+ // Freeze numbers with the rest of the turn: a history card must not show the
48
+ // live session's plan progress or edit total.
49
+ const numbers = live || frozen === undefined
50
+ ? { planItems: view.planItems, openPlanItems: view.openPlanItems, edits: view.edits }
51
+ : { planItems: frozen.planItems, openPlanItems: frozen.openPlanItems, edits: frozen.edits };
52
+ // 只有「最新一轮 + codex + 该轮以 deliver 结束且验证通过」才提供交接文档按钮:
53
+ // 正常完成的轮次,不是历史轮,也不是 BLOCKED 交付。
54
+ const canMemory = live && mode === 'codex' && stage === 'deliver' && verification?.passed === true;
55
+ // A wire value from an older build may name a mode this build does not know;
56
+ // fall back to the codex stage set instead of crashing the chat render.
57
+ return {
58
+ live,
59
+ mode,
60
+ stage,
61
+ verification,
62
+ allowed: STAGES_BY_MODE[mode] ?? STAGES_BY_MODE.codex,
63
+ ...numbers,
64
+ canMemory,
65
+ };
66
+ }
67
+ /**
68
+ * The command line one mode switch sends back through the Host. A card sits at
69
+ * a closed turn's tail, and switching mode is a control action, not a prompt:
70
+ * the silent `set` verb applies the mode and tool table without starting a
71
+ * Turn, so the model never answers a button press. Typed `/coding <mode>` keeps
72
+ * its "switch and open a turn" meaning.
73
+ */
74
+ export function commandFor(mode) {
75
+ return `/coding set ${mode}`;
76
+ }
77
+ /** 生成交接文档要回流 Host 的命令行。 */
78
+ export function memoryCommand() {
79
+ return '/coding memory';
80
+ }
81
+ /** Read the Turn number out of the owner props. */
82
+ function turnOf(value) {
83
+ if (typeof value === 'number' && Number.isFinite(value))
84
+ return value;
85
+ const turn = value?.turn;
86
+ return typeof turn === 'number' && Number.isFinite(turn) ? turn : undefined;
87
+ }
88
+ /** Verification text for one card. */
89
+ /**
90
+ * The card's verification cell: status only, never the command. The command
91
+ * still rides the capsule tooltip and the `run_verification` result, where a
92
+ * reader can act on it; the 12px card row stays scannable.
93
+ * @param verification - recorded outcome, or null before the first run.
94
+ * @param t - locale lookup.
95
+ * @returns the label and the color class that carries its meaning.
96
+ */
97
+ export function verificationText(verification, t) {
98
+ if (verification === null) {
99
+ return { text: t('verification.none'), className: 'dsh-coding-value' };
100
+ }
101
+ return verification.passed
102
+ ? { text: t('verification.passed'), className: 'dsh-coding-success' }
103
+ : { text: t('verification.failed'), className: 'dsh-coding-warn' };
104
+ }
105
+ /**
106
+ * Read one override/effort slice. The state is read defensively because a wire
107
+ * value from an older build may miss a field the current type declares.
108
+ */
109
+ function sliceDisplay(slice) {
110
+ if (slice.override === 'low' || slice.override === 'high' || slice.override === 'max') {
111
+ return { level: slice.override, source: 'override' };
112
+ }
113
+ if (typeof slice.userEffort === 'string' && slice.userEffort.trim() !== '') {
114
+ return { level: levelForEffort(slice.userEffort.trim()), source: 'pick' };
115
+ }
116
+ if (typeof slice.effort === 'string' && slice.effort.trim() !== '') {
117
+ return { level: levelForEffort(slice.effort.trim()), source: 'auto' };
118
+ }
119
+ return { level: null, source: 'pending' };
120
+ }
121
+ /**
122
+ * The reasoning row one card renders. A closed turn keeps the state frozen at
123
+ * its own `turn/end`, so history cards never borrow a later turn's level; an
124
+ * unknown history turn stays pending instead of copying the live state.
125
+ * @param state - durable reasoning projection, when the Host serves it.
126
+ * @param turn - Turn the card belongs to.
127
+ * @param settings - configured policy: `auto`, or a pinned level.
128
+ */
129
+ export function reasoningDisplay(state, turn, settings) {
130
+ const pinned = settings === 'low' || settings === 'high' || settings === 'max' ? settings : undefined;
131
+ if (state === undefined) {
132
+ return pinned === undefined
133
+ ? { level: null, source: 'pending' }
134
+ : { level: pinned, source: 'settings' };
135
+ }
136
+ const turns = Array.isArray(state.turns) ? state.turns : [];
137
+ const frozen = turn === undefined ? undefined : turns.find(item => item.turn === turn);
138
+ if (frozen !== undefined) {
139
+ const display = sliceDisplay(frozen);
140
+ // A pin labels the turn only when the turn actually ran at that level.
141
+ return pinned !== undefined && display.level === pinned
142
+ ? { level: pinned, source: 'settings' }
143
+ : display;
144
+ }
145
+ const latest = typeof state.latest === 'number' ? state.latest : 0;
146
+ if (turn !== undefined && turn < latest)
147
+ return { level: null, source: 'pending' };
148
+ if (pinned !== undefined)
149
+ return { level: pinned, source: 'settings' };
150
+ return sliceDisplay(state);
151
+ }
152
+ /** Locale key per reasoning source. */
153
+ const REASONING_SOURCE_KEY = {
154
+ settings: 'reasoning.sourceSettings',
155
+ override: 'reasoning.sourceOverride',
156
+ pick: 'reasoning.sourcePick',
157
+ auto: 'reasoning.sourceAuto',
158
+ pending: 'reasoning.pending',
159
+ };
160
+ /** Display label for a level; `Low`/`High`/`Max` are provider-level names. */
161
+ function levelLabel(level) {
162
+ return level === null ? null : level === 'low' ? 'Low' : level === 'high' ? 'High' : 'Max';
163
+ }
164
+ /** The per-turn Coding control card. */
165
+ export const CodingTurnCard = memo(function CodingTurnCard(props) {
166
+ const { t } = props;
167
+ const raw = props.useProjection?.(CODING_PROJECTION_KEY);
168
+ const reasoningRaw = props.useProjection?.(REASONING_PROJECTION_KEY);
169
+ const view = raw === null || typeof raw !== 'object' ? undefined : raw;
170
+ const [busy, setBusy] = useState(null);
171
+ const [error, setError] = useState(null);
172
+ // 交接文档的按键状态机:idle → confirm → busy → done;失败回到 idle 并给出原因。
173
+ const [memory, setMemory] = useState('idle');
174
+ const [memoryError, setMemoryError] = useState(null);
175
+ if (view === undefined || view.active !== true)
176
+ return null;
177
+ const turn = turnOf(props.turn);
178
+ const { live, mode, stage, verification, allowed, canMemory, ...numbers } = cardState(view, turn);
179
+ const reasoningState = reasoningRaw === null || typeof reasoningRaw !== 'object'
180
+ ? undefined
181
+ : reasoningRaw;
182
+ const reasoning = reasoningDisplay(reasoningState, turn, props.settings?.());
183
+ const stageIndex = allowed.indexOf(stage);
184
+ // A wire value from a partial or older build still renders numbers.
185
+ const number = (value) => (typeof value === 'number' && Number.isFinite(value) ? value : 0);
186
+ const planItems = number(numbers.planItems);
187
+ const openPlanItems = number(numbers.openPlanItems);
188
+ const state = verificationText(verification, t);
189
+ const enabled = live && busy === null;
190
+ // 模式切换失败与交接文档失败共用一个错误段落,先出现的那个优先。
191
+ const failure = error ?? memoryError;
192
+ async function choose(next) {
193
+ if (next === mode || busy !== null || !live)
194
+ return;
195
+ setBusy(next);
196
+ setError(null);
197
+ try {
198
+ const result = await props.execute?.(commandFor(next));
199
+ if (commandRejected(result))
200
+ setError(t('card.switchFailed'));
201
+ }
202
+ catch {
203
+ setError(t('card.switchFailed'));
204
+ }
205
+ finally {
206
+ setBusy(null);
207
+ }
208
+ }
209
+ async function writeMemory() {
210
+ if (memory === 'busy')
211
+ return;
212
+ setMemory('busy');
213
+ setMemoryError(null);
214
+ try {
215
+ const result = await props.execute?.(memoryCommand());
216
+ // 没有注入 execute 也算失败:这一轮不该悄悄什么都不发生。
217
+ if (result === undefined || commandRejected(result)) {
218
+ setMemory('idle');
219
+ setMemoryError(t('card.memoryFailed'));
220
+ }
221
+ else {
222
+ setMemory('done');
223
+ }
224
+ }
225
+ catch {
226
+ setMemory('idle');
227
+ setMemoryError(t('card.memoryFailed'));
228
+ }
229
+ }
230
+ /** 交接文档行;`canMemory` 为假时整行不渲染。 */
231
+ function memoryRow() {
232
+ if (!canMemory)
233
+ return null;
234
+ let content;
235
+ if (memory === 'idle') {
236
+ content = [h('button', {
237
+ key: 'memory-idle',
238
+ type: 'button',
239
+ className: 'dsh-coding-button',
240
+ onClick: () => setMemory('confirm'),
241
+ }, t('card.memory'))];
242
+ }
243
+ else if (memory === 'confirm') {
244
+ content = [
245
+ h('span', { key: 'memory-hint', className: 'dsh-coding-hint' }, t('card.memoryHint')),
246
+ h('button', {
247
+ key: 'memory-confirm',
248
+ type: 'button',
249
+ className: 'dsh-coding-button',
250
+ onClick: () => { void writeMemory(); },
251
+ }, t('card.memoryConfirm')),
252
+ h('button', {
253
+ key: 'memory-cancel',
254
+ type: 'button',
255
+ className: 'dsh-coding-button',
256
+ onClick: () => {
257
+ setMemory('idle');
258
+ setMemoryError(null);
259
+ },
260
+ }, t('card.memoryCancel')),
261
+ ];
262
+ }
263
+ else if (memory === 'busy') {
264
+ content = [h('button', {
265
+ key: 'memory-busy',
266
+ type: 'button',
267
+ className: 'dsh-coding-button',
268
+ disabled: true,
269
+ }, t('card.memoryBusy'))];
270
+ }
271
+ else {
272
+ content = [h('span', { key: 'memory-done', className: 'dsh-coding-success' }, t('card.memoryDone'))];
273
+ }
274
+ return h('div', { className: 'dsh-coding-row', 'data-dsh-coding-memory': 'true' }, ...content);
275
+ }
276
+ return h('div', {
277
+ className: 'dsh-coding-card',
278
+ 'data-dsh-coding-card': 'true',
279
+ 'data-live': live ? 'true' : 'false',
280
+ 'aria-label': t('aria'),
281
+ }, h('div', { className: 'dsh-coding-row' }, h('span', { className: 'dsh-coding-label' }, t('card.mode')), h('div', { className: 'dsh-coding-actions' }, ...MODES.map(option => h('button', {
282
+ key: option,
283
+ type: 'button',
284
+ className: 'dsh-coding-button',
285
+ disabled: !enabled,
286
+ 'aria-pressed': option === mode,
287
+ onClick: () => { void choose(option); },
288
+ }, t(`mode.${option}`)))), live ? null : h('span', { className: 'dsh-coding-hint' }, t('card.stale'))), h('div', { className: 'dsh-coding-row' }, h('span', { className: 'dsh-coding-label' }, t('card.stage')), ...allowed.flatMap((item, index) => [
289
+ ...index === 0 ? [] : [h('span', { key: `${item}-arrow`, className: 'dsh-coding-arrow', 'aria-hidden': 'true' }, '→')],
290
+ h('span', {
291
+ key: item,
292
+ className: 'dsh-coding-stage',
293
+ 'data-current': item === stage ? 'true' : undefined,
294
+ 'data-done': index < stageIndex ? 'true' : undefined,
295
+ 'data-stage': item,
296
+ }, t(`stage.${item}`)),
297
+ ]), h('span', { className: 'dsh-coding-hint' }, t('card.stageHint'))), h('div', { className: 'dsh-coding-row' }, h('span', { className: 'dsh-coding-label' }, t('card.reasoning')), h('span', {
298
+ className: reasoning.level === null ? 'dsh-coding-hint' : 'dsh-coding-value',
299
+ 'data-reasoning-level': reasoning.level ?? 'pending',
300
+ 'data-reasoning-source': reasoning.source,
301
+ }, levelLabel(reasoning.level) ?? t('reasoning.pending')), h('span', { className: 'dsh-coding-faint', 'aria-hidden': 'true' }, '·'), h('span', { className: 'dsh-coding-hint' }, t(REASONING_SOURCE_KEY[reasoning.source]))), h('div', { className: 'dsh-coding-row' }, h('span', { className: 'dsh-coding-label' }, t('card.verify')), h('span', { className: state.className }, state.text), h('span', { className: 'dsh-coding-faint', 'aria-hidden': 'true' }, '·'), h('span', { className: 'dsh-coding-value' }, planItems === 0
302
+ ? t('plan.none')
303
+ : t('plan.progress')
304
+ .replace('{done}', String(Math.max(0, planItems - openPlanItems)))
305
+ .replace('{total}', String(planItems))), h('span', { className: 'dsh-coding-faint', 'aria-hidden': 'true' }, '·'), h('span', { className: 'dsh-coding-value' }, t('files').replace('{count}', String(number(numbers.edits)))), view.pending === null
306
+ ? null
307
+ : h('span', { className: 'dsh-coding-value' }, t('pending').replace('{tool}', view.pending.tool))), memoryRow(), failure === null ? null : h('p', { className: 'dsh-coding-error' }, failure));
308
+ });
@@ -0,0 +1,76 @@
1
+ /**
2
+ * One `/coding` command receipt in the Chat flow.
3
+ *
4
+ * Mode switching is already visible where it belongs: the blank-hero picker
5
+ * highlights the chosen mode, the newest turn card shows it as `aria-pressed`,
6
+ * and the next stage capsule records it. Repeating that as a command card in
7
+ * the transcript adds nothing and — before this row existed — a failed switch
8
+ * left a red error card behind. So `/coding set <mode>` and the bare
9
+ * `/coding <mode>` switch render nothing here: the change reads off the
10
+ * controls, not the transcript.
11
+ *
12
+ * Every other `/coding` line still renders one plain status line — the command
13
+ * and its own text — because a status query or a stage move is worth keeping.
14
+ *
15
+ * @module dsh-coding/client/command-row
16
+ */
17
+ import { createElement as h, memo } from 'react';
18
+ /** The modes a bare `/coding <mode>` switches to, in roster order. */
19
+ const SWITCH_WORDS = ['auto', 'consult', 'review', 'codex'];
20
+ /**
21
+ * Decide what one command receipt renders. Pure, so the hiding rule — the whole
22
+ * point of this module — is testable without a renderer.
23
+ * @param node - the `CommandNode` the Chat owner passes down.
24
+ * @returns the visible/absent split for this receipt.
25
+ */
26
+ export function commandRowOf(node) {
27
+ const line = `/${node.name ?? 'coding'}${node.args ?? ''}`;
28
+ const words = (node.args ?? '').trim().toLowerCase().split(/\s+/u).filter(Boolean);
29
+ // A mode switch: either the silent `set <mode>` or the bare switch command.
30
+ const switches = words[0] === 'set'
31
+ || (words.length === 1 && SWITCH_WORDS.includes(words[0]));
32
+ if (switches)
33
+ return { hidden: true, line, text: null, failed: false };
34
+ const outcome = node.outcome;
35
+ // A command that has not settled yet has no body worth a row.
36
+ if (outcome == null)
37
+ return { hidden: true, line, text: null, failed: false };
38
+ const text = typeof outcome.text === 'string' && outcome.text.length > 0 ? outcome.text : null;
39
+ return { hidden: false, line, text, failed: outcome.kind === 'error' };
40
+ }
41
+ /**
42
+ * Whether a commands Remote result is a command-level failure.
43
+ *
44
+ * Two shapes mean failure: the Remote itself failed (`{ ok: false }`), or the
45
+ * Remote succeeded while the command handler rejected the line
46
+ * (`{ ok: true, value: { result: { kind: 'error' } } }`). Only the first used to
47
+ * be checked, so a rejected switch looked like a success.
48
+ * @param result - whatever `remote.commands.execute()` returned.
49
+ * @returns true when the command did not apply.
50
+ */
51
+ export function commandRejected(result) {
52
+ if (result === null || typeof result !== 'object')
53
+ return false;
54
+ if (result.ok === false)
55
+ return true;
56
+ const value = result.value;
57
+ if (value === null || typeof value !== 'object')
58
+ return false;
59
+ const inner = value.result;
60
+ if (inner === null || typeof inner !== 'object')
61
+ return false;
62
+ return inner.kind === 'error';
63
+ }
64
+ /** One `/coding` command receipt, or nothing for a mode switch. */
65
+ export const CodingCommandRow = memo(function CodingCommandRow(props) {
66
+ const view = commandRowOf(props.node);
67
+ if (view.hidden)
68
+ return null;
69
+ return h('div', {
70
+ className: 'dsh-coding-command',
71
+ 'data-command': props.node.name ?? 'coding',
72
+ ...view.failed ? { 'data-error': 'true' } : {},
73
+ }, h('span', { className: 'dsh-coding-label' }, view.line), view.text === null
74
+ ? null
75
+ : h('span', { className: view.failed ? 'dsh-coding-error' : 'dsh-coding-value' }, view.text));
76
+ });