@d0nj/dsh-narration 0.0.0-stage → 0.3.0
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/LICENSE +24 -0
- package/README.md +80 -2
- package/cordis.patch.yml +7 -0
- package/dist/ansi.d.ts +13 -0
- package/dist/ansi.js +83 -0
- package/dist/client/assistant.d.ts +25 -0
- package/dist/client/assistant.js +208 -0
- package/dist/client/client.js +1609 -0
- package/dist/client/index.d.ts +16 -0
- package/dist/client/index.js +78 -0
- package/dist/client/style.d.ts +4 -0
- package/dist/client/style.js +373 -0
- package/dist/client/tool.d.ts +37 -0
- package/dist/client/tool.js +384 -0
- package/dist/diff.d.ts +31 -0
- package/dist/diff.js +181 -0
- package/dist/envelope.d.ts +7 -0
- package/dist/envelope.js +29 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +39 -0
- package/dist/live.d.ts +48 -0
- package/dist/live.js +124 -0
- package/dist/narration.d.ts +9 -0
- package/dist/narration.js +27 -0
- package/dist/pacing.d.ts +17 -0
- package/dist/pacing.js +92 -0
- package/icon.svg +16 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +69 -3
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { compactOutput, normalizeTerminalText, parseAnsi } from '../ansi.js';
|
|
2
|
+
import { stripEnvelope, splitReadRange } from '../envelope.js';
|
|
3
|
+
import { diffLines, diffRows, diffSummary } from '../diff.js';
|
|
4
|
+
import { TOOL_NARRATION, clip, formatElapsed, narrateCall, splitPath } from '../live.js';
|
|
5
|
+
const PREVIEW_LINES = 12;
|
|
6
|
+
function followProps(options) {
|
|
7
|
+
if (options === undefined)
|
|
8
|
+
return {};
|
|
9
|
+
const pin = options.pin;
|
|
10
|
+
return {
|
|
11
|
+
ref: (element) => {
|
|
12
|
+
if (element !== null && pin.current)
|
|
13
|
+
element.scrollTop = element.scrollHeight;
|
|
14
|
+
},
|
|
15
|
+
onScroll: (event) => {
|
|
16
|
+
const element = event.currentTarget;
|
|
17
|
+
pin.current = element.scrollHeight - element.scrollTop - element.clientHeight < 12;
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function asBlock(value) {
|
|
22
|
+
return value !== null && typeof value === 'object' ? value : undefined;
|
|
23
|
+
}
|
|
24
|
+
function text(value) {
|
|
25
|
+
return typeof value === 'string' ? value : '';
|
|
26
|
+
}
|
|
27
|
+
function maybeText(value) {
|
|
28
|
+
return typeof value === 'string' ? value : undefined;
|
|
29
|
+
}
|
|
30
|
+
function list(value) {
|
|
31
|
+
return Array.isArray(value) ? value : [];
|
|
32
|
+
}
|
|
33
|
+
export function toolRoot(node) {
|
|
34
|
+
const data = asBlock(asBlock(node)?.data);
|
|
35
|
+
return asBlock(data?.root);
|
|
36
|
+
}
|
|
37
|
+
export function toolName(block) {
|
|
38
|
+
return text(asBlock(block)?.name);
|
|
39
|
+
}
|
|
40
|
+
export function toolPhase(block) {
|
|
41
|
+
const entry = asBlock(block);
|
|
42
|
+
if (entry === undefined || entry.kind !== undefined)
|
|
43
|
+
return 'result';
|
|
44
|
+
return entry.phase === 'preparing' ? 'preparing' : 'start';
|
|
45
|
+
}
|
|
46
|
+
function argsReader(source) {
|
|
47
|
+
const args = asBlock(source);
|
|
48
|
+
const read = args?.text;
|
|
49
|
+
if (typeof read !== 'function')
|
|
50
|
+
return { text: () => undefined };
|
|
51
|
+
return {
|
|
52
|
+
text: (key) => {
|
|
53
|
+
try {
|
|
54
|
+
const value = read.call(args, key);
|
|
55
|
+
return typeof value === 'string' ? value : undefined;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function rawArgs(block) {
|
|
64
|
+
const call = asBlock(block.call);
|
|
65
|
+
const fromCall = text(call?.argsRaw);
|
|
66
|
+
return fromCall.length > 0 ? fromCall : text(block.argsRaw);
|
|
67
|
+
}
|
|
68
|
+
export function parsedArgs(block) {
|
|
69
|
+
const entry = asBlock(block);
|
|
70
|
+
if (entry === undefined)
|
|
71
|
+
return undefined;
|
|
72
|
+
const raw = rawArgs(entry);
|
|
73
|
+
if (raw.length > 0) {
|
|
74
|
+
try {
|
|
75
|
+
return asBlock(JSON.parse(raw));
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const args = entry.args;
|
|
82
|
+
const keys = asBlock(args)?.keys;
|
|
83
|
+
const names = typeof keys === 'function' ? keys.call(args) : [];
|
|
84
|
+
const reader = argsReader(args);
|
|
85
|
+
const out = {};
|
|
86
|
+
let found = false;
|
|
87
|
+
for (const key of list(names)) {
|
|
88
|
+
if (typeof key !== 'string')
|
|
89
|
+
continue;
|
|
90
|
+
const value = reader.text(key);
|
|
91
|
+
if (value === undefined)
|
|
92
|
+
continue;
|
|
93
|
+
out[key] = value;
|
|
94
|
+
found = true;
|
|
95
|
+
}
|
|
96
|
+
return found ? out : undefined;
|
|
97
|
+
}
|
|
98
|
+
export function liveCallOf(block) {
|
|
99
|
+
const entry = asBlock(block);
|
|
100
|
+
if (entry === undefined)
|
|
101
|
+
return undefined;
|
|
102
|
+
const callId = text(entry.callId);
|
|
103
|
+
const name = text(entry.name);
|
|
104
|
+
if (callId.length === 0 || name.length === 0)
|
|
105
|
+
return undefined;
|
|
106
|
+
return {
|
|
107
|
+
callId,
|
|
108
|
+
name,
|
|
109
|
+
phase: toolPhase(entry) === 'preparing' ? 'preparing' : 'start',
|
|
110
|
+
time: typeof entry.time === 'number' ? entry.time : 0,
|
|
111
|
+
args: argsReader(entry.args),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export function fileDiffOf(name, args) {
|
|
115
|
+
if (args === undefined)
|
|
116
|
+
return undefined;
|
|
117
|
+
const path = maybeText(args.file_path) ?? maybeText(args.path) ?? '';
|
|
118
|
+
if (name === 'edit') {
|
|
119
|
+
const before = maybeText(args.old_string);
|
|
120
|
+
const after = maybeText(args.new_string);
|
|
121
|
+
if (before === undefined || after === undefined)
|
|
122
|
+
return undefined;
|
|
123
|
+
return { path, before, after };
|
|
124
|
+
}
|
|
125
|
+
if (name === 'write') {
|
|
126
|
+
const after = maybeText(args.content);
|
|
127
|
+
if (after === undefined)
|
|
128
|
+
return undefined;
|
|
129
|
+
return { path, before: '', after };
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
export function patchTextOf(name, args) {
|
|
134
|
+
if (name !== 'apply_patch' || args === undefined)
|
|
135
|
+
return undefined;
|
|
136
|
+
return maybeText(args.patch) ?? maybeText(args.input) ?? maybeText(args.diff);
|
|
137
|
+
}
|
|
138
|
+
export function commandOf(name, args) {
|
|
139
|
+
if ((name !== 'bash' && name !== 'pwsh') || args === undefined)
|
|
140
|
+
return undefined;
|
|
141
|
+
return maybeText(args.command);
|
|
142
|
+
}
|
|
143
|
+
function prettyJson(raw) {
|
|
144
|
+
if (raw.length === 0)
|
|
145
|
+
return '';
|
|
146
|
+
try {
|
|
147
|
+
return JSON.stringify(JSON.parse(raw), null, 2);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return raw;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export function toolArgsText(block) {
|
|
154
|
+
const entry = asBlock(block);
|
|
155
|
+
if (entry === undefined)
|
|
156
|
+
return '';
|
|
157
|
+
const raw = rawArgs(entry);
|
|
158
|
+
if (raw.length > 0)
|
|
159
|
+
return prettyJson(raw);
|
|
160
|
+
const args = entry.args;
|
|
161
|
+
const keys = asBlock(args)?.keys;
|
|
162
|
+
const names = typeof keys === 'function' ? keys.call(args) : [];
|
|
163
|
+
const reader = argsReader(args);
|
|
164
|
+
const lines = [];
|
|
165
|
+
for (const key of list(names)) {
|
|
166
|
+
if (typeof key !== 'string')
|
|
167
|
+
continue;
|
|
168
|
+
const value = reader.text(key);
|
|
169
|
+
if (value === undefined || value.length === 0)
|
|
170
|
+
continue;
|
|
171
|
+
lines.push(key + ': ' + value);
|
|
172
|
+
}
|
|
173
|
+
return lines.join('\n');
|
|
174
|
+
}
|
|
175
|
+
export function toolOutputText(block) {
|
|
176
|
+
const entry = asBlock(block);
|
|
177
|
+
if (entry === undefined)
|
|
178
|
+
return { output: '', isError: false };
|
|
179
|
+
let output = '';
|
|
180
|
+
for (const part of list(entry.content)) {
|
|
181
|
+
const item = asBlock(part);
|
|
182
|
+
if (item === undefined || item.type !== 'text')
|
|
183
|
+
continue;
|
|
184
|
+
output += text(item.text);
|
|
185
|
+
}
|
|
186
|
+
const isError = entry.isError === true;
|
|
187
|
+
const error = asBlock(entry.error);
|
|
188
|
+
const reason = text(error?.reason).length > 0 ? text(error?.reason) : text(error?.code);
|
|
189
|
+
if (isError && reason.length > 0 && !output.includes(reason)) {
|
|
190
|
+
output = output.length > 0 ? output + '\n' + reason : reason;
|
|
191
|
+
}
|
|
192
|
+
return { output: output.trimEnd(), isError };
|
|
193
|
+
}
|
|
194
|
+
export function tailPreview(source, limit = PREVIEW_LINES) {
|
|
195
|
+
if (source.length === 0)
|
|
196
|
+
return { shown: '', hidden: 0 };
|
|
197
|
+
const lines = source.split('\n');
|
|
198
|
+
if (lines.length <= limit)
|
|
199
|
+
return { shown: source, hidden: 0 };
|
|
200
|
+
return { shown: lines.slice(lines.length - limit).join('\n'), hidden: lines.length - limit };
|
|
201
|
+
}
|
|
202
|
+
const HEADER_KEYS = new Set([
|
|
203
|
+
'command', 'content', 'description', 'diff', 'file_path', 'input', 'name',
|
|
204
|
+
'new_string', 'old_string', 'patch', 'path', 'pattern', 'prompt', 'query', 'url',
|
|
205
|
+
]);
|
|
206
|
+
function describeValue(value) {
|
|
207
|
+
if (typeof value === 'string')
|
|
208
|
+
return clip(value, 48);
|
|
209
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
210
|
+
return String(value);
|
|
211
|
+
if (Array.isArray(value))
|
|
212
|
+
return String(value.length) + ' items';
|
|
213
|
+
if (value === null || value === undefined)
|
|
214
|
+
return '';
|
|
215
|
+
return clip(JSON.stringify(value), 48);
|
|
216
|
+
}
|
|
217
|
+
export function argsSummary(args, limit = 6) {
|
|
218
|
+
if (args === undefined)
|
|
219
|
+
return '';
|
|
220
|
+
const parts = [];
|
|
221
|
+
for (const [key, value] of Object.entries(args)) {
|
|
222
|
+
if (HEADER_KEYS.has(key))
|
|
223
|
+
continue;
|
|
224
|
+
const described = describeValue(value);
|
|
225
|
+
if (described.length === 0)
|
|
226
|
+
continue;
|
|
227
|
+
parts.push(key + ' ' + described);
|
|
228
|
+
if (parts.length >= limit)
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
return parts.join(' \u00b7 ');
|
|
232
|
+
}
|
|
233
|
+
export function toolDuration(block, now) {
|
|
234
|
+
const entry = asBlock(block);
|
|
235
|
+
if (entry === undefined)
|
|
236
|
+
return 0;
|
|
237
|
+
const finished = typeof entry.time === 'number' ? entry.time : 0;
|
|
238
|
+
const started = typeof entry.callTime === 'number' ? entry.callTime : 0;
|
|
239
|
+
if (toolPhase(entry) === 'result')
|
|
240
|
+
return started > 0 && finished > started ? finished - started : 0;
|
|
241
|
+
return started > 0 ? Math.max(0, now - started) : 0;
|
|
242
|
+
}
|
|
243
|
+
const STATE_LABEL = {
|
|
244
|
+
preparing: 'preparing',
|
|
245
|
+
start: 'running',
|
|
246
|
+
result: 'done',
|
|
247
|
+
};
|
|
248
|
+
function renderDiffCard(React, before, after, options) {
|
|
249
|
+
const h = React.createElement;
|
|
250
|
+
const lines = diffLines(before, after);
|
|
251
|
+
const summary = diffSummary(lines);
|
|
252
|
+
const { rows } = diffRows(lines);
|
|
253
|
+
return h('pre', {
|
|
254
|
+
className: 'd0nj-diff',
|
|
255
|
+
'data-added': String(summary.added),
|
|
256
|
+
'data-removed': String(summary.removed),
|
|
257
|
+
...followProps(options),
|
|
258
|
+
}, rows.map((row, index) => h('span', { key: String(index), className: 'd0nj-diff-line', 'data-kind': row.kind }, row.kind === 'hunk'
|
|
259
|
+
? row.text
|
|
260
|
+
: [
|
|
261
|
+
h('span', { key: 's', className: 'd0nj-diff-sign' }, row.sign),
|
|
262
|
+
h('span', { key: 'n', className: 'd0nj-diff-no' }, row.number.padStart(row.digits)),
|
|
263
|
+
h('span', { key: 'b', className: 'd0nj-diff-bar' }, '\u2502'),
|
|
264
|
+
h('span', { key: 't', className: 'd0nj-diff-text' }, row.text),
|
|
265
|
+
])));
|
|
266
|
+
}
|
|
267
|
+
function renderPatchCard(React, patch, options) {
|
|
268
|
+
const h = React.createElement;
|
|
269
|
+
return h('pre', { className: 'd0nj-diff', ...followProps(options) }, patch.split('\n').map((line, index) => h('span', {
|
|
270
|
+
key: String(index),
|
|
271
|
+
className: 'd0nj-diff-line',
|
|
272
|
+
'data-kind': line.startsWith('+') ? 'add' : line.startsWith('-') ? 'remove' : 'equal',
|
|
273
|
+
}, line)));
|
|
274
|
+
}
|
|
275
|
+
function renderTerminal(React, text) {
|
|
276
|
+
const h = React.createElement;
|
|
277
|
+
return parseAnsi(text).map((span, index) => h('span', {
|
|
278
|
+
key: String(index),
|
|
279
|
+
className: 'd0nj-term',
|
|
280
|
+
'data-tone': span.tone,
|
|
281
|
+
'data-bold': span.bold ? 'true' : undefined,
|
|
282
|
+
'data-dim': span.dim ? 'true' : undefined,
|
|
283
|
+
'data-underline': span.underline ? 'true' : undefined,
|
|
284
|
+
}, span.text));
|
|
285
|
+
}
|
|
286
|
+
function renderOutput(React, output, options) {
|
|
287
|
+
const h = React.createElement;
|
|
288
|
+
if (output.length === 0)
|
|
289
|
+
return null;
|
|
290
|
+
const follow = followProps(options);
|
|
291
|
+
const preview = tailPreview(output, PREVIEW_LINES);
|
|
292
|
+
if (options === undefined || preview.hidden === 0) {
|
|
293
|
+
return h('pre', { className: 'd0nj-tool-output', ...follow }, renderTerminal(React, output));
|
|
294
|
+
}
|
|
295
|
+
return h('div', { className: 'd0nj-tool-out-wrap' }, h('button', {
|
|
296
|
+
type: 'button',
|
|
297
|
+
className: 'd0nj-tool-more',
|
|
298
|
+
'aria-expanded': options.expanded,
|
|
299
|
+
onClick: options.onToggle,
|
|
300
|
+
}, options.expanded ? 'collapse' : '\u2026 ' + String(preview.hidden) + ' earlier lines'), h('pre', { className: 'd0nj-tool-output', ...follow }, renderTerminal(React, options.expanded ? output : preview.shown)));
|
|
301
|
+
}
|
|
302
|
+
export function cleanOutput(output) {
|
|
303
|
+
const cleaned = compactOutput(normalizeTerminalText(stripEnvelope(output)));
|
|
304
|
+
const split = splitReadRange(cleaned);
|
|
305
|
+
return { body: split.text, range: split.range };
|
|
306
|
+
}
|
|
307
|
+
function renderBlock(React, block, now, depth, options) {
|
|
308
|
+
const h = React.createElement;
|
|
309
|
+
const entry = asBlock(block);
|
|
310
|
+
if (entry === undefined || depth > 4)
|
|
311
|
+
return null;
|
|
312
|
+
const phase = toolPhase(entry);
|
|
313
|
+
const name = toolName(entry);
|
|
314
|
+
const call = liveCallOf(entry);
|
|
315
|
+
const narration = call === undefined ? undefined : narrateCall(call, 240);
|
|
316
|
+
const args = parsedArgs(entry);
|
|
317
|
+
const { output, isError } = toolOutputText(entry);
|
|
318
|
+
const spent = toolDuration(entry, now);
|
|
319
|
+
const state = phase === 'result' ? (isError ? 'failed' : 'done') : STATE_LABEL[phase] ?? '';
|
|
320
|
+
const stateKey = isError ? 'failed' : phase === 'result' ? 'done' : phase;
|
|
321
|
+
const glyph = stateKey === 'failed'
|
|
322
|
+
? '\u2715'
|
|
323
|
+
: stateKey === 'done'
|
|
324
|
+
? '\u2713'
|
|
325
|
+
: stateKey === 'start'
|
|
326
|
+
? '\u25d0'
|
|
327
|
+
: '\u25cb';
|
|
328
|
+
const subCalls = list(entry.subCalls);
|
|
329
|
+
const editView = fileDiffOf(name, args);
|
|
330
|
+
const patch = patchTextOf(name, args);
|
|
331
|
+
const hasFileView = editView !== undefined || patch !== undefined;
|
|
332
|
+
const command = commandOf(name, args);
|
|
333
|
+
const parts = splitPath(command === undefined ? narration?.argument ?? '' : '');
|
|
334
|
+
const known = TOOL_NARRATION[name] !== undefined;
|
|
335
|
+
const extras = known ? argsSummary(args) : '';
|
|
336
|
+
const argsText = known ? '' : toolArgsText(entry);
|
|
337
|
+
const cleaned = cleanOutput(output);
|
|
338
|
+
const meta = [extras, cleaned.range ?? ''].filter((part) => part.length > 0).join(' \u00b7 ');
|
|
339
|
+
const showOutput = cleaned.body.length > 0 && (!hasFileView || isError);
|
|
340
|
+
const stat = editView === undefined
|
|
341
|
+
? ''
|
|
342
|
+
: (() => {
|
|
343
|
+
const summary = diffSummary(diffLines(editView.before, editView.after));
|
|
344
|
+
return '+' + String(summary.added) + ' \u2212' + String(summary.removed);
|
|
345
|
+
})();
|
|
346
|
+
const body = editView !== undefined
|
|
347
|
+
? renderDiffCard(React, editView.before, editView.after, options)
|
|
348
|
+
: patch !== undefined
|
|
349
|
+
? renderPatchCard(React, patch, options)
|
|
350
|
+
: command !== undefined
|
|
351
|
+
? h('pre', { className: 'd0nj-tool-command' }, h('span', { className: 'd0nj-tool-prompt' }, '\u276f '), renderTerminal(React, command))
|
|
352
|
+
: meta.length > 0
|
|
353
|
+
? h('div', { className: 'd0nj-tool-meta' }, meta)
|
|
354
|
+
: argsText.length > 0
|
|
355
|
+
? h('pre', { className: 'd0nj-tool-args' }, argsText)
|
|
356
|
+
: null;
|
|
357
|
+
return h('div', { className: 'd0nj-tool', 'data-error': String(isError) }, h('div', { className: 'd0nj-tool-head' }, h('span', { className: 'd0nj-tool-icon', 'data-state': stateKey }, glyph), h('span', { className: 'd0nj-tool-verb' }, narration?.verb ?? (name || 'Tool')), h('span', { className: 'd0nj-tool-target' }, parts.directory.length > 0 ? h('span', { className: 'd0nj-narr-dir' }, parts.directory) : null, h('span', { className: 'd0nj-narr-base' }, parts.base)), stat.length > 0 ? h('span', { className: 'd0nj-tool-stat' }, stat) : null, h('span', { className: 'd0nj-tool-state' }, spent > 0 ? formatElapsed(spent) + ' \u00b7 ' + state : state)), body, showOutput ? renderOutput(React, cleaned.body, options) : null, subCalls.length > 0
|
|
358
|
+
? h('div', { className: 'd0nj-tool-sub' }, subCalls.map((sub) => renderBlock(React, sub, now, depth + 1, undefined) ?? null))
|
|
359
|
+
: null);
|
|
360
|
+
}
|
|
361
|
+
export function createToolNarration(React) {
|
|
362
|
+
return function ToolNarration(props) {
|
|
363
|
+
const [now, setNow] = React.useState(() => Date.now());
|
|
364
|
+
const [expanded, setExpanded] = React.useState(false);
|
|
365
|
+
const pin = React.useRef(true);
|
|
366
|
+
const root = toolRoot(props.node);
|
|
367
|
+
const phase = toolPhase(root);
|
|
368
|
+
React.useEffect(() => {
|
|
369
|
+
if (phase === 'result')
|
|
370
|
+
return;
|
|
371
|
+
const tick = setInterval(() => setNow(Date.now()), 200);
|
|
372
|
+
return () => clearInterval(tick);
|
|
373
|
+
}, [phase]);
|
|
374
|
+
return renderBlock(React, root, now, 0, {
|
|
375
|
+
expanded,
|
|
376
|
+
pin,
|
|
377
|
+
onToggle: () => {
|
|
378
|
+
pin.current = true;
|
|
379
|
+
setExpanded((previous) => !previous);
|
|
380
|
+
},
|
|
381
|
+
});
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
//# sourceMappingURL=tool.js.map
|
package/dist/diff.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type DiffKind = 'equal' | 'add' | 'remove' | 'hunk';
|
|
2
|
+
export interface DiffLine {
|
|
3
|
+
readonly kind: DiffKind;
|
|
4
|
+
readonly text: string;
|
|
5
|
+
readonly oldLine: number | null;
|
|
6
|
+
readonly newLine: number | null;
|
|
7
|
+
}
|
|
8
|
+
export interface DiffSummary {
|
|
9
|
+
readonly added: number;
|
|
10
|
+
readonly removed: number;
|
|
11
|
+
}
|
|
12
|
+
interface Op {
|
|
13
|
+
readonly kind: 'equal' | 'add' | 'remove';
|
|
14
|
+
readonly text: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function diffOps(beforeSource: string, afterSource: string): Op[];
|
|
17
|
+
export declare function diffSummary(lines: readonly DiffLine[]): DiffSummary;
|
|
18
|
+
export interface DiffRow {
|
|
19
|
+
readonly kind: DiffKind;
|
|
20
|
+
readonly text: string;
|
|
21
|
+
readonly sign: string;
|
|
22
|
+
readonly number: string;
|
|
23
|
+
readonly digits: number;
|
|
24
|
+
}
|
|
25
|
+
export declare function diffRows(lines: readonly DiffLine[]): {
|
|
26
|
+
rows: DiffRow[];
|
|
27
|
+
digits: number;
|
|
28
|
+
};
|
|
29
|
+
export declare function diffLines(before: string, after: string, context?: number, maxLines?: number): DiffLine[];
|
|
30
|
+
export {};
|
|
31
|
+
//# sourceMappingURL=diff.d.ts.map
|
package/dist/diff.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
const MAX_TABLE = 2_000_000;
|
|
2
|
+
const MAX_LINES = 400;
|
|
3
|
+
function splitLines(source) {
|
|
4
|
+
if (source.length === 0)
|
|
5
|
+
return [];
|
|
6
|
+
return source.split('\n');
|
|
7
|
+
}
|
|
8
|
+
function commonPrefix(before, after) {
|
|
9
|
+
const limit = Math.min(before.length, after.length);
|
|
10
|
+
let index = 0;
|
|
11
|
+
while (index < limit && before[index] === after[index])
|
|
12
|
+
index += 1;
|
|
13
|
+
return index;
|
|
14
|
+
}
|
|
15
|
+
function commonSuffix(before, after, from) {
|
|
16
|
+
const limit = Math.min(before.length, after.length) - from;
|
|
17
|
+
let count = 0;
|
|
18
|
+
while (count < limit &&
|
|
19
|
+
before[before.length - 1 - count] === after[after.length - 1 - count]) {
|
|
20
|
+
count += 1;
|
|
21
|
+
}
|
|
22
|
+
return count;
|
|
23
|
+
}
|
|
24
|
+
function fallbackOps(before, after) {
|
|
25
|
+
const prefix = commonPrefix(before, after);
|
|
26
|
+
const suffix = commonSuffix(before, after, prefix);
|
|
27
|
+
const ops = [];
|
|
28
|
+
for (let index = 0; index < prefix; index += 1)
|
|
29
|
+
ops.push({ kind: 'equal', text: before[index] ?? '' });
|
|
30
|
+
for (let index = prefix; index < before.length - suffix; index += 1) {
|
|
31
|
+
ops.push({ kind: 'remove', text: before[index] ?? '' });
|
|
32
|
+
}
|
|
33
|
+
for (let index = prefix; index < after.length - suffix; index += 1) {
|
|
34
|
+
ops.push({ kind: 'add', text: after[index] ?? '' });
|
|
35
|
+
}
|
|
36
|
+
for (let index = after.length - suffix; index < after.length; index += 1) {
|
|
37
|
+
ops.push({ kind: 'equal', text: after[index] ?? '' });
|
|
38
|
+
}
|
|
39
|
+
return ops;
|
|
40
|
+
}
|
|
41
|
+
export function diffOps(beforeSource, afterSource) {
|
|
42
|
+
const before = splitLines(beforeSource);
|
|
43
|
+
const after = splitLines(afterSource);
|
|
44
|
+
const width = after.length + 1;
|
|
45
|
+
if ((before.length + 1) * width > MAX_TABLE)
|
|
46
|
+
return fallbackOps(before, after);
|
|
47
|
+
const table = new Int32Array((before.length + 1) * width);
|
|
48
|
+
for (let row = before.length - 1; row >= 0; row -= 1) {
|
|
49
|
+
for (let column = after.length - 1; column >= 0; column -= 1) {
|
|
50
|
+
table[row * width + column] =
|
|
51
|
+
before[row] === after[column]
|
|
52
|
+
? (table[(row + 1) * width + column + 1] ?? 0) + 1
|
|
53
|
+
: Math.max(table[(row + 1) * width + column] ?? 0, table[row * width + column + 1] ?? 0);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const ops = [];
|
|
57
|
+
let row = 0;
|
|
58
|
+
let column = 0;
|
|
59
|
+
while (row < before.length && column < after.length) {
|
|
60
|
+
if (before[row] === after[column]) {
|
|
61
|
+
ops.push({ kind: 'equal', text: before[row] ?? '' });
|
|
62
|
+
row += 1;
|
|
63
|
+
column += 1;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const down = table[(row + 1) * width + column] ?? 0;
|
|
67
|
+
const right = table[row * width + column + 1] ?? 0;
|
|
68
|
+
if (down >= right) {
|
|
69
|
+
ops.push({ kind: 'remove', text: before[row] ?? '' });
|
|
70
|
+
row += 1;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
ops.push({ kind: 'add', text: after[column] ?? '' });
|
|
74
|
+
column += 1;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
while (row < before.length) {
|
|
78
|
+
ops.push({ kind: 'remove', text: before[row] ?? '' });
|
|
79
|
+
row += 1;
|
|
80
|
+
}
|
|
81
|
+
while (column < after.length) {
|
|
82
|
+
ops.push({ kind: 'add', text: after[column] ?? '' });
|
|
83
|
+
column += 1;
|
|
84
|
+
}
|
|
85
|
+
return ops;
|
|
86
|
+
}
|
|
87
|
+
export function diffSummary(lines) {
|
|
88
|
+
let added = 0;
|
|
89
|
+
let removed = 0;
|
|
90
|
+
for (const line of lines) {
|
|
91
|
+
if (line.kind === 'add')
|
|
92
|
+
added += 1;
|
|
93
|
+
if (line.kind === 'remove')
|
|
94
|
+
removed += 1;
|
|
95
|
+
}
|
|
96
|
+
return { added, removed };
|
|
97
|
+
}
|
|
98
|
+
export function diffRows(lines) {
|
|
99
|
+
let highest = 0;
|
|
100
|
+
for (const line of lines) {
|
|
101
|
+
if (line.oldLine !== null)
|
|
102
|
+
highest = Math.max(highest, line.oldLine);
|
|
103
|
+
if (line.newLine !== null)
|
|
104
|
+
highest = Math.max(highest, line.newLine);
|
|
105
|
+
}
|
|
106
|
+
const digits = Math.max(3, String(highest).length);
|
|
107
|
+
let previous = '';
|
|
108
|
+
const rows = [];
|
|
109
|
+
for (const line of lines) {
|
|
110
|
+
if (line.kind === 'hunk') {
|
|
111
|
+
previous = '';
|
|
112
|
+
rows.push({ kind: 'hunk', text: line.text, sign: '', number: '', digits });
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const raw = line.kind === 'remove' ? line.oldLine : line.newLine;
|
|
116
|
+
const value = raw === null ? '' : String(raw);
|
|
117
|
+
const number = value === previous || value.length === 0 ? '' : value;
|
|
118
|
+
if (value.length > 0)
|
|
119
|
+
previous = value;
|
|
120
|
+
rows.push({
|
|
121
|
+
kind: line.kind,
|
|
122
|
+
text: line.text,
|
|
123
|
+
sign: line.kind === 'add' ? '+' : line.kind === 'remove' ? '\u2212' : ' ',
|
|
124
|
+
number,
|
|
125
|
+
digits,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return { rows, digits };
|
|
129
|
+
}
|
|
130
|
+
export function diffLines(before, after, context = 3, maxLines = MAX_LINES) {
|
|
131
|
+
const ops = diffOps(before, after);
|
|
132
|
+
const keep = new Uint8Array(ops.length);
|
|
133
|
+
for (let index = 0; index < ops.length; index += 1) {
|
|
134
|
+
if (ops[index]?.kind === 'equal')
|
|
135
|
+
continue;
|
|
136
|
+
const from = Math.max(0, index - context);
|
|
137
|
+
const to = Math.min(ops.length - 1, index + context);
|
|
138
|
+
for (let mark = from; mark <= to; mark += 1)
|
|
139
|
+
keep[mark] = 1;
|
|
140
|
+
}
|
|
141
|
+
const lines = [];
|
|
142
|
+
let oldLine = 1;
|
|
143
|
+
let newLine = 1;
|
|
144
|
+
let gap = 0;
|
|
145
|
+
for (let index = 0; index < ops.length; index += 1) {
|
|
146
|
+
const op = ops[index];
|
|
147
|
+
if (op === undefined)
|
|
148
|
+
continue;
|
|
149
|
+
if (keep[index] !== 1) {
|
|
150
|
+
gap += 1;
|
|
151
|
+
if (op.kind === 'equal') {
|
|
152
|
+
oldLine += 1;
|
|
153
|
+
newLine += 1;
|
|
154
|
+
}
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (gap > 0) {
|
|
158
|
+
lines.push({ kind: 'hunk', text: '\u2026 ' + String(gap) + ' unchanged', oldLine: null, newLine: null });
|
|
159
|
+
gap = 0;
|
|
160
|
+
}
|
|
161
|
+
if (op.kind === 'equal') {
|
|
162
|
+
lines.push({ kind: 'equal', text: op.text, oldLine, newLine });
|
|
163
|
+
oldLine += 1;
|
|
164
|
+
newLine += 1;
|
|
165
|
+
}
|
|
166
|
+
else if (op.kind === 'remove') {
|
|
167
|
+
lines.push({ kind: 'remove', text: op.text, oldLine, newLine: null });
|
|
168
|
+
oldLine += 1;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
lines.push({ kind: 'add', text: op.text, oldLine: null, newLine });
|
|
172
|
+
newLine += 1;
|
|
173
|
+
}
|
|
174
|
+
if (lines.length > maxLines) {
|
|
175
|
+
lines.push({ kind: 'hunk', text: '\u2026 diff truncated', oldLine: null, newLine: null });
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return lines;
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=diff.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface EnvelopeText {
|
|
2
|
+
readonly text: string;
|
|
3
|
+
readonly range: string | null;
|
|
4
|
+
}
|
|
5
|
+
export declare function stripEnvelope(source: string): string;
|
|
6
|
+
export declare function splitReadRange(source: string): EnvelopeText;
|
|
7
|
+
//# sourceMappingURL=envelope.d.ts.map
|
package/dist/envelope.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const CONTENT_OPEN = /^\s*<content(?:\s[^>]*)?>\s*$/u;
|
|
2
|
+
const CONTENT_CLOSE = /^\s*<\/content>\s*$/u;
|
|
3
|
+
const LEADING_CONTENT = /^\s*<content(?:\s[^>]*)?>\s*/u;
|
|
4
|
+
const TRAILING_CONTENT = /\s*<\/content>\s*$/u;
|
|
5
|
+
const METADATA = /^\s*<(path|type)>[^<\n]*<\/\1>\s*$/u;
|
|
6
|
+
const READ_RANGE = /^\(Showing lines (\d+)-(\d+) of (\d+)\. Use offset=\d+ to continue\.\)$/u;
|
|
7
|
+
export function stripEnvelope(source) {
|
|
8
|
+
const lines = source.split('\n');
|
|
9
|
+
const kept = [];
|
|
10
|
+
for (const line of lines) {
|
|
11
|
+
if (CONTENT_OPEN.test(line) || CONTENT_CLOSE.test(line) || METADATA.test(line))
|
|
12
|
+
continue;
|
|
13
|
+
kept.push(line);
|
|
14
|
+
}
|
|
15
|
+
return kept.join('\n').replace(LEADING_CONTENT, '').replace(TRAILING_CONTENT, '');
|
|
16
|
+
}
|
|
17
|
+
export function splitReadRange(source) {
|
|
18
|
+
const lines = source.split('\n');
|
|
19
|
+
const last = lines.length - 1;
|
|
20
|
+
const tail = last >= 0 ? (lines[last] ?? '').trim() : '';
|
|
21
|
+
const match = READ_RANGE.exec(tail);
|
|
22
|
+
if (match === null)
|
|
23
|
+
return { text: source, range: null };
|
|
24
|
+
return {
|
|
25
|
+
text: lines.slice(0, last).join('\n').replace(/\n+$/u, ''),
|
|
26
|
+
range: 'lines ' + String(match[1]) + '\u2013' + String(match[2]) + ' of ' + String(match[3]),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=envelope.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery';
|
|
2
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
3
|
+
export declare const name = "narration";
|
|
4
|
+
export declare const inject: readonly ["systemPrompt"];
|
|
5
|
+
export interface PluginConfig {
|
|
6
|
+
intentLine: boolean;
|
|
7
|
+
parallelReads: boolean;
|
|
8
|
+
intentWords: number;
|
|
9
|
+
liveReasoning: boolean;
|
|
10
|
+
verboseTools: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare const Config: z<PluginConfig>;
|
|
13
|
+
export declare function apply(ctx: Context, config: PluginConfig): () => void;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery';
|
|
2
|
+
import { SECTION_NAME, SECTION_ORDER, buildNarrationSection } from './narration.js';
|
|
3
|
+
export const name = 'narration';
|
|
4
|
+
export const inject = ['systemPrompt'];
|
|
5
|
+
export const Config = z.object({
|
|
6
|
+
liveReasoning: z
|
|
7
|
+
.boolean()
|
|
8
|
+
.default(true)
|
|
9
|
+
.description('Replace the assistant row with the live narration renderer. Off restores the shipped row, including its message actions and inline images.'),
|
|
10
|
+
verboseTools: z
|
|
11
|
+
.boolean()
|
|
12
|
+
.default(true)
|
|
13
|
+
.description('Render every tool call with its arguments and full output inline. Off restores the shipped tool rows and their per-tool views.'),
|
|
14
|
+
intentLine: z
|
|
15
|
+
.boolean()
|
|
16
|
+
.default(true)
|
|
17
|
+
.description('Require one sentence of intent in the same message as every tool call.'),
|
|
18
|
+
parallelReads: z
|
|
19
|
+
.boolean()
|
|
20
|
+
.default(true)
|
|
21
|
+
.description('Ask for independent read-only calls to be batched into one message.'),
|
|
22
|
+
intentWords: z
|
|
23
|
+
.number()
|
|
24
|
+
.step(1)
|
|
25
|
+
.min(8)
|
|
26
|
+
.max(60)
|
|
27
|
+
.default(24)
|
|
28
|
+
.description('Upper bound on the intent sentence, in words.'),
|
|
29
|
+
});
|
|
30
|
+
export function apply(ctx, config) {
|
|
31
|
+
const text = buildNarrationSection(config);
|
|
32
|
+
if (text === null)
|
|
33
|
+
return () => { };
|
|
34
|
+
const prompt = ctx.systemPrompt;
|
|
35
|
+
if (prompt === undefined)
|
|
36
|
+
return () => { };
|
|
37
|
+
return prompt.section({ name: SECTION_NAME, order: SECTION_ORDER, text });
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=index.js.map
|