@workser/engine 0.1.17 → 0.1.19
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/src/compact.d.ts +43 -4
- package/dist/src/compact.js +169 -10
- package/dist/src/dsml.d.ts +12 -0
- package/dist/src/dsml.js +44 -0
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.js +2 -1
- package/dist/src/model.d.ts +0 -5
- package/dist/src/model.js +18 -4
- package/dist/src/session.d.ts +5 -0
- package/dist/src/session.js +17 -4
- package/dist/src/tools/search.js +8 -3
- package/dist/src/tools/workser-tool.js +10 -5
- package/package.json +1 -1
package/dist/src/compact.d.ts
CHANGED
|
@@ -69,6 +69,27 @@ export declare function lowWater(budget: CompactionBudget): number;
|
|
|
69
69
|
export declare function tailBudget(messages: ChatMessage[], budget: CompactionBudget, overhead: number): number;
|
|
70
70
|
export declare function estimateTokens(messages: ChatMessage[]): number;
|
|
71
71
|
export declare function overBudget(messages: ChatMessage[], budget: CompactionBudget, reportedPromptTokens?: number): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* NEVER READ THE SAME FILE TWICE INTO THE CONTEXT (owner 17 Nov, trace 01a1e8ed: a restyle read page.tsx 32 times and
|
|
74
|
+
* the design skill 27 times, summarized twelve times in twelve minutes and wrote nothing). Three parts:
|
|
75
|
+
* - `dropSupersededReads`: a read of a file that was read again later, or changed after it, is out of date. It
|
|
76
|
+
* becomes a one-line note before anything else is pruned, so the room it frees costs nothing;
|
|
77
|
+
* - `openFiles` + `withSummary`: the latest read of each file still in play goes INTO the summary, so the job never
|
|
78
|
+
* needs to read it again after making room;
|
|
79
|
+
* - the session's stall guard (session.ts): two summaries with no change to any file in between double the next
|
|
80
|
+
* trigger and tell the job to write.
|
|
81
|
+
*/
|
|
82
|
+
export declare const READ_TOOLS: ReadonlySet<string>;
|
|
83
|
+
export declare const WRITE_TOOLS: ReadonlySet<string>;
|
|
84
|
+
/** The most of the open files' latest contents a summary carries, in tokens. */
|
|
85
|
+
export declare const OPEN_FILES_TOKENS = 20000;
|
|
86
|
+
/** Step 0. Out-of-date reads become a one-line note (changed in place); returns how many. */
|
|
87
|
+
export declare function dropSupersededReads(messages: ChatMessage[]): number;
|
|
88
|
+
/** The latest read of each file still as it was read (not changed since), newest first, within `maxTokens`. */
|
|
89
|
+
export declare function openFiles(messages: ChatMessage[], maxTokens?: number): Array<{
|
|
90
|
+
path: string;
|
|
91
|
+
content: string;
|
|
92
|
+
}>;
|
|
72
93
|
/** Step 1. Returns how many tool outputs were cleared (the array is changed in place). */
|
|
73
94
|
export declare function pruneToolOutputs(messages: ChatMessage[], keepToolTokens: number): number;
|
|
74
95
|
/** An old call's input longer than this (characters) keeps its head only. */
|
|
@@ -107,7 +128,25 @@ export interface RoomPlan {
|
|
|
107
128
|
* that is at or under the low water the job is done, else the summary's kept tail is sized to land there.
|
|
108
129
|
*/
|
|
109
130
|
export declare function makeRoom(messages: ChatMessage[], budget: CompactionBudget, reportedPromptTokens?: number): RoomPlan | null;
|
|
110
|
-
/**
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
131
|
+
/**
|
|
132
|
+
* The request that asks the model to summarize the part being replaced. `goal`: the job's request (the first owner
|
|
133
|
+
* message), which the part never holds. Without it every summary of trace 01a1e93d (17 Nov) began "Goal: not specified"
|
|
134
|
+
* and ended "re-read AGENTS.md ... proceed once the task is established", so after each one the job started over:
|
|
135
|
+
* 33 summaries, 246 reads, 3 writes.
|
|
136
|
+
*/
|
|
137
|
+
export declare function summaryRequest(part: ChatMessage[], goal?: string | null, earlier?: string | null): ChatMessage[];
|
|
138
|
+
/**
|
|
139
|
+
* The job's first owner message, split: the request as the owner gave it, and the summary an earlier compaction put
|
|
140
|
+
* after it (null before the first). The open files and the stall note are left out of `earlier`: the next summary gets
|
|
141
|
+
* the files again from `openFiles`, and a fresh note. So a second summary neither nests the first nor loses it.
|
|
142
|
+
*/
|
|
143
|
+
export declare function splitHead(content: string | null | undefined): {
|
|
144
|
+
request: string;
|
|
145
|
+
earlier: string | null;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Step 2's result: the system prompt, the goal with the summary, then the kept tail. The files the job had read and not
|
|
149
|
+
* changed since, from the part summarized away (the tail keeps its own), go with the summary as they were read, so the
|
|
150
|
+
* job carries on without reading them again. `note`: the session's stall warning, when it has one.
|
|
151
|
+
*/
|
|
152
|
+
export declare function withSummary(messages: ChatMessage[], cut: number, summary: string, note?: string): ChatMessage[];
|
package/dist/src/compact.js
CHANGED
|
@@ -71,6 +71,125 @@ export function overBudget(messages, budget, reportedPromptTokens) {
|
|
|
71
71
|
const tokens = Math.max(estimateTokens(messages), reportedPromptTokens ?? 0);
|
|
72
72
|
return tokens >= compactAt(budget);
|
|
73
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* NEVER READ THE SAME FILE TWICE INTO THE CONTEXT (owner 17 Nov, trace 01a1e8ed: a restyle read page.tsx 32 times and
|
|
76
|
+
* the design skill 27 times, summarized twelve times in twelve minutes and wrote nothing). Three parts:
|
|
77
|
+
* - `dropSupersededReads`: a read of a file that was read again later, or changed after it, is out of date. It
|
|
78
|
+
* becomes a one-line note before anything else is pruned, so the room it frees costs nothing;
|
|
79
|
+
* - `openFiles` + `withSummary`: the latest read of each file still in play goes INTO the summary, so the job never
|
|
80
|
+
* needs to read it again after making room;
|
|
81
|
+
* - the session's stall guard (session.ts): two summaries with no change to any file in between double the next
|
|
82
|
+
* trigger and tell the job to write.
|
|
83
|
+
*/
|
|
84
|
+
export const READ_TOOLS = new Set(['read']);
|
|
85
|
+
export const WRITE_TOOLS = new Set(['write', 'edit', 'apply_patch']);
|
|
86
|
+
/** The most of the open files' latest contents a summary carries, in tokens. */
|
|
87
|
+
export const OPEN_FILES_TOKENS = 20_000;
|
|
88
|
+
/** A tool call's file path (`filePath`, `path`, `file_path`), or null. */
|
|
89
|
+
function pathOfCall(call) {
|
|
90
|
+
if (!call)
|
|
91
|
+
return null;
|
|
92
|
+
try {
|
|
93
|
+
const args = JSON.parse(call.function.arguments || '{}');
|
|
94
|
+
const path = args.filePath ?? args.path ?? args.file_path;
|
|
95
|
+
return typeof path === 'string' && path.trim() ? path.trim() : null;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** The read's own window (a file read in parts is several reads): path, offset and limit. */
|
|
102
|
+
function readKey(call) {
|
|
103
|
+
const path = pathOfCall(call);
|
|
104
|
+
if (!path)
|
|
105
|
+
return null;
|
|
106
|
+
try {
|
|
107
|
+
const args = JSON.parse(call.function.arguments || '{}');
|
|
108
|
+
return `${path}#${args.offset ?? ''}#${args.limit ?? ''}`;
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return `${path}##`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Every tool call by its id. */
|
|
115
|
+
function callsById(messages) {
|
|
116
|
+
const calls = new Map();
|
|
117
|
+
for (const message of messages)
|
|
118
|
+
for (const call of message.tool_calls ?? [])
|
|
119
|
+
calls.set(call.id, call);
|
|
120
|
+
return calls;
|
|
121
|
+
}
|
|
122
|
+
/** Step 0. Out-of-date reads become a one-line note (changed in place); returns how many. */
|
|
123
|
+
export function dropSupersededReads(messages) {
|
|
124
|
+
const calls = callsById(messages);
|
|
125
|
+
const readLater = new Set();
|
|
126
|
+
const changedLater = new Set();
|
|
127
|
+
let dropped = 0;
|
|
128
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
129
|
+
const message = messages[i];
|
|
130
|
+
// Calls are seen before their results when walking back: a write here changes the files read before it.
|
|
131
|
+
for (const call of message.tool_calls ?? []) {
|
|
132
|
+
if (WRITE_TOOLS.has(call.function.name)) {
|
|
133
|
+
const path = pathOfCall(call);
|
|
134
|
+
if (path)
|
|
135
|
+
changedLater.add(path);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (message.role !== 'tool' || !message.tool_call_id || !message.content || message.content.startsWith('['))
|
|
139
|
+
continue;
|
|
140
|
+
const call = calls.get(message.tool_call_id);
|
|
141
|
+
if (!call || !READ_TOOLS.has(call.function.name))
|
|
142
|
+
continue;
|
|
143
|
+
const key = readKey(call);
|
|
144
|
+
const path = pathOfCall(call);
|
|
145
|
+
if (!key || !path)
|
|
146
|
+
continue;
|
|
147
|
+
if (readLater.has(key)) {
|
|
148
|
+
message.content = `[Read again later: ${path}]`;
|
|
149
|
+
dropped++;
|
|
150
|
+
}
|
|
151
|
+
else if (changedLater.has(path)) {
|
|
152
|
+
message.content = `[${path} was changed after this read]`;
|
|
153
|
+
dropped++;
|
|
154
|
+
}
|
|
155
|
+
readLater.add(key);
|
|
156
|
+
}
|
|
157
|
+
return dropped;
|
|
158
|
+
}
|
|
159
|
+
/** The latest read of each file still as it was read (not changed since), newest first, within `maxTokens`. */
|
|
160
|
+
export function openFiles(messages, maxTokens = OPEN_FILES_TOKENS) {
|
|
161
|
+
const calls = callsById(messages);
|
|
162
|
+
const seen = new Set();
|
|
163
|
+
const changed = new Set();
|
|
164
|
+
const out = [];
|
|
165
|
+
let used = 0;
|
|
166
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
167
|
+
const message = messages[i];
|
|
168
|
+
for (const call of message.tool_calls ?? []) {
|
|
169
|
+
if (WRITE_TOOLS.has(call.function.name)) {
|
|
170
|
+
const path = pathOfCall(call);
|
|
171
|
+
if (path)
|
|
172
|
+
changed.add(path);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (message.role !== 'tool' || !message.tool_call_id || !message.content || message.content.startsWith('['))
|
|
176
|
+
continue;
|
|
177
|
+
const call = calls.get(message.tool_call_id);
|
|
178
|
+
if (!call || !READ_TOOLS.has(call.function.name))
|
|
179
|
+
continue;
|
|
180
|
+
const path = pathOfCall(call);
|
|
181
|
+
const key = readKey(call);
|
|
182
|
+
if (!path || !key || seen.has(key) || changed.has(path))
|
|
183
|
+
continue;
|
|
184
|
+
seen.add(key);
|
|
185
|
+
const size = Math.ceil(message.content.length / 4);
|
|
186
|
+
if (used + size > maxTokens)
|
|
187
|
+
continue;
|
|
188
|
+
used += size;
|
|
189
|
+
out.push({ path, content: message.content });
|
|
190
|
+
}
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
74
193
|
/** Step 1. Returns how many tool outputs were cleared (the array is changed in place). */
|
|
75
194
|
export function pruneToolOutputs(messages, keepToolTokens) {
|
|
76
195
|
let kept = 0;
|
|
@@ -174,14 +293,21 @@ export function makeRoom(messages, budget, reportedPromptTokens) {
|
|
|
174
293
|
if (!overBudget(messages, budget, reportedPromptTokens))
|
|
175
294
|
return null;
|
|
176
295
|
const overhead = Math.max(0, (reportedPromptTokens ?? 0) - estimateTokens(messages));
|
|
177
|
-
|
|
296
|
+
// Out-of-date reads go first: the room they free loses nothing, and the newest read of each file outlives the prune.
|
|
297
|
+
const superseded = dropSupersededReads(messages);
|
|
298
|
+
const cleared = pruneToolOutputs(messages, budget.keepToolTokens) + superseded;
|
|
178
299
|
const shortened = shortenOldArguments(messages, budget.keepToolTokens);
|
|
179
300
|
if (estimateTokens(messages) + overhead <= lowWater(budget))
|
|
180
301
|
return { cleared, shortened, landed: true, cut: -1 };
|
|
181
302
|
return { cleared, shortened, landed: false, cut: cutPoint(messages, tailBudget(messages, budget, overhead)) };
|
|
182
303
|
}
|
|
183
|
-
/**
|
|
184
|
-
|
|
304
|
+
/**
|
|
305
|
+
* The request that asks the model to summarize the part being replaced. `goal`: the job's request (the first owner
|
|
306
|
+
* message), which the part never holds. Without it every summary of trace 01a1e93d (17 Nov) began "Goal: not specified"
|
|
307
|
+
* and ended "re-read AGENTS.md ... proceed once the task is established", so after each one the job started over:
|
|
308
|
+
* 33 summaries, 246 reads, 3 writes.
|
|
309
|
+
*/
|
|
310
|
+
export function summaryRequest(part, goal, earlier) {
|
|
185
311
|
const transcript = part
|
|
186
312
|
.map((message) => {
|
|
187
313
|
if (message.role === 'tool')
|
|
@@ -198,20 +324,53 @@ export function summaryRequest(part) {
|
|
|
198
324
|
content: 'You summarize a coding session so another AI agent can continue it without the full history. ' +
|
|
199
325
|
'Write plain text with these parts: the goal; what has been done; files created or changed (paths); ' +
|
|
200
326
|
'commands that matter and their results; decisions and constraints from the owner; what is left to do next. ' +
|
|
201
|
-
'Be specific (names, paths, errors). Do not invent anything.'
|
|
327
|
+
'Be specific (names, paths, errors). Do not invent anything. ' +
|
|
328
|
+
'The goal is the job\'s request, given first: state it, never write that it is unknown. ' +
|
|
329
|
+
'Write what was learned from each file that was read (its facts, not "it was read"), and never tell the agent ' +
|
|
330
|
+
'to read again a file whose result was cleared: it continues from your summary, it does not start over.',
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
role: 'user',
|
|
334
|
+
content: (goal?.trim() ? `The job's request:\n\n${goal.trim().slice(0, 12_000)}\n\n---\n` : '') +
|
|
335
|
+
(earlier?.trim() ? `The summary of the work before this part (carry what still matters into yours):\n\n${earlier.trim().slice(0, 12_000)}\n\n---\n` : '') +
|
|
336
|
+
`Summarize this session so far:\n\n${transcript.slice(-400_000)}`,
|
|
202
337
|
},
|
|
203
|
-
{ role: 'user', content: `Summarize this session so far:\n\n${transcript.slice(-400_000)}` },
|
|
204
338
|
];
|
|
205
339
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
340
|
+
const SUMMARIZED_MARK = '\n\n---\nThe conversation after this request was summarized to save space. Summary of the work so far:\n\n';
|
|
341
|
+
const OPEN_FILES_MARK = '\n\n---\nFiles you have open, as you last read them';
|
|
342
|
+
/**
|
|
343
|
+
* The job's first owner message, split: the request as the owner gave it, and the summary an earlier compaction put
|
|
344
|
+
* after it (null before the first). The open files and the stall note are left out of `earlier`: the next summary gets
|
|
345
|
+
* the files again from `openFiles`, and a fresh note. So a second summary neither nests the first nor loses it.
|
|
346
|
+
*/
|
|
347
|
+
export function splitHead(content) {
|
|
348
|
+
const text = content ?? '';
|
|
349
|
+
const at = text.indexOf(SUMMARIZED_MARK);
|
|
350
|
+
if (at < 0)
|
|
351
|
+
return { request: text, earlier: null };
|
|
352
|
+
const rest = text.slice(at + SUMMARIZED_MARK.length);
|
|
353
|
+
const files = rest.indexOf(OPEN_FILES_MARK);
|
|
354
|
+
return { request: text.slice(0, at), earlier: (files < 0 ? rest : rest.slice(0, files)).trim() || null };
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Step 2's result: the system prompt, the goal with the summary, then the kept tail. The files the job had read and not
|
|
358
|
+
* changed since, from the part summarized away (the tail keeps its own), go with the summary as they were read, so the
|
|
359
|
+
* job carries on without reading them again. `note`: the session's stall warning, when it has one.
|
|
360
|
+
*/
|
|
361
|
+
export function withSummary(messages, cut, summary, note) {
|
|
362
|
+
const goal = splitHead(messages[1]?.content).request.slice(0, 20_000);
|
|
363
|
+
const inTail = new Set(openFiles(messages.slice(cut), Number.MAX_SAFE_INTEGER).map((one) => one.path));
|
|
364
|
+
const open = openFiles(messages.slice(0, cut)).filter((one) => !inTail.has(one.path));
|
|
365
|
+
const files = open.length
|
|
366
|
+
? `${OPEN_FILES_MARK} (you have not changed them since; do not read them again unless you change them):\n\n` +
|
|
367
|
+
open.map((one) => `${one.content}`).join('\n\n')
|
|
368
|
+
: '';
|
|
209
369
|
const head = [
|
|
210
370
|
messages[0],
|
|
211
371
|
{
|
|
212
372
|
role: 'user',
|
|
213
|
-
content: `${goal}
|
|
214
|
-
`Summary of the work so far:\n\n${summary.trim()}`,
|
|
373
|
+
content: `${goal}${SUMMARIZED_MARK}${summary.trim()}${note ? `\n\n${note}` : ''}${files}`,
|
|
215
374
|
},
|
|
216
375
|
];
|
|
217
376
|
// Two owner messages in a row read as one; an acknowledgement keeps them apart.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** A whole message with its markup tags taken out (untouched, not even trimmed, when it has none). */
|
|
2
|
+
export declare function stripDsml(text: string): string;
|
|
3
|
+
/**
|
|
4
|
+
* The same for a reply that arrives in pieces: a tag can be cut anywhere (`<`, `|DSML|`, ` calls`, `>`), so what might
|
|
5
|
+
* still become a tag is held back until the next piece says. Everything else passes at once.
|
|
6
|
+
*/
|
|
7
|
+
export declare class DsmlFilter {
|
|
8
|
+
private held;
|
|
9
|
+
push(chunk: string): string;
|
|
10
|
+
/** The stream ended: an unfinished tag is dropped, a lone `<` (or the start of one that never came) is text. */
|
|
11
|
+
flush(): string;
|
|
12
|
+
}
|
package/dist/src/dsml.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DEEPSEEK'S OWN TOOL-CALL MARKUP, KEPT OUT OF THE OWNER'S CHAT (trace 01a1d4cc, 13 Nov: the owner's chat showed grey
|
|
3
|
+
* chips reading "< | DSML | calls>"). DeepSeek writes its tool calls with `<|DSML|function_calls>` style tags and now and
|
|
4
|
+
* then lets a bare tag leak into the reply text, in 4 of 57 calls of one job; the structured tool calls still arrived and
|
|
5
|
+
* ran, so only the visible text was wrong. A tag is `<` or `</`, then `|DSML|` (full-width bars), then a name, then `>`.
|
|
6
|
+
*/
|
|
7
|
+
const OPEN = '<|DSML|';
|
|
8
|
+
const CLOSE = '</|DSML|';
|
|
9
|
+
const TAG = /<\/?|DSML|[^>\n]*>/g;
|
|
10
|
+
/** A whole message with its markup tags taken out (untouched, not even trimmed, when it has none). */
|
|
11
|
+
export function stripDsml(text) {
|
|
12
|
+
if (!text.includes('|DSML|'))
|
|
13
|
+
return text;
|
|
14
|
+
const out = text.replace(TAG, '');
|
|
15
|
+
return out === text ? text : out.replace(/\n{3,}/g, '\n\n').trim();
|
|
16
|
+
}
|
|
17
|
+
function mayBecomeTag(tail) {
|
|
18
|
+
if (tail.includes('>') || tail.includes('\n'))
|
|
19
|
+
return false;
|
|
20
|
+
return OPEN.startsWith(tail) || CLOSE.startsWith(tail) || tail.startsWith(OPEN) || tail.startsWith(CLOSE);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The same for a reply that arrives in pieces: a tag can be cut anywhere (`<`, `|DSML|`, ` calls`, `>`), so what might
|
|
24
|
+
* still become a tag is held back until the next piece says. Everything else passes at once.
|
|
25
|
+
*/
|
|
26
|
+
export class DsmlFilter {
|
|
27
|
+
held = '';
|
|
28
|
+
push(chunk) {
|
|
29
|
+
let text = (this.held + chunk).replace(TAG, '');
|
|
30
|
+
this.held = '';
|
|
31
|
+
const at = text.lastIndexOf('<');
|
|
32
|
+
if (at >= 0 && mayBecomeTag(text.slice(at))) {
|
|
33
|
+
this.held = text.slice(at);
|
|
34
|
+
text = text.slice(0, at);
|
|
35
|
+
}
|
|
36
|
+
return text;
|
|
37
|
+
}
|
|
38
|
+
/** The stream ended: an unfinished tag is dropped, a lone `<` (or the start of one that never came) is text. */
|
|
39
|
+
flush() {
|
|
40
|
+
const rest = this.held;
|
|
41
|
+
this.held = '';
|
|
42
|
+
return rest.startsWith(OPEN) || rest.startsWith(CLOSE) ? '' : rest;
|
|
43
|
+
}
|
|
44
|
+
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -7,9 +7,10 @@ export { providerPrompt, environmentPrompt, bashDescription } from './prompts.js
|
|
|
7
7
|
export { needsAsk } from './tools/shell.js';
|
|
8
8
|
export { ShellSession } from './tools/shell.js';
|
|
9
9
|
export { applyPatchTool, parsePatch, applyChunks, usesPatch, PatchError } from './tools/patch.js';
|
|
10
|
-
export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, lowWater, tailBudget, makeRoom, type RoomPlan, LOW_WATER_SHARE, MIN_TAIL_TOKENS, SUMMARY_ALLOWANCE_TOKENS, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, PRUNED_NOTE, type CompactionBudget, } from './compact.js';
|
|
10
|
+
export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, lowWater, tailBudget, makeRoom, type RoomPlan, LOW_WATER_SHARE, MIN_TAIL_TOKENS, SUMMARY_ALLOWANCE_TOKENS, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, summaryRequest, splitHead, dropSupersededReads, openFiles, READ_TOOLS, WRITE_TOOLS, OPEN_FILES_TOKENS, PRUNED_NOTE, type CompactionBudget, } from './compact.js';
|
|
11
11
|
export { vaultFetchTool, type KeyRelay } from './tools/relay.js';
|
|
12
12
|
export { workserTool, type ToolsClient } from './tools/workser-tool.js';
|
|
13
13
|
export { publishFileTool, mimeOf, MAX_PUBLISH_BYTES, type FileClient } from './tools/publish.js';
|
|
14
14
|
export { withCommandEnv } from './place/env.js';
|
|
15
15
|
export { repairArgs, parseLoose, TOOL_ALIASES, type Repaired } from './tools/repair.js';
|
|
16
|
+
export { DsmlFilter, stripDsml } from './dsml.js';
|
package/dist/src/index.js
CHANGED
|
@@ -7,9 +7,10 @@ export { providerPrompt, environmentPrompt, bashDescription } from './prompts.js
|
|
|
7
7
|
export { needsAsk } from './tools/shell.js';
|
|
8
8
|
export { ShellSession } from './tools/shell.js';
|
|
9
9
|
export { applyPatchTool, parsePatch, applyChunks, usesPatch, PatchError } from './tools/patch.js';
|
|
10
|
-
export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, lowWater, tailBudget, makeRoom, LOW_WATER_SHARE, MIN_TAIL_TOKENS, SUMMARY_ALLOWANCE_TOKENS, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, PRUNED_NOTE, } from './compact.js';
|
|
10
|
+
export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, lowWater, tailBudget, makeRoom, LOW_WATER_SHARE, MIN_TAIL_TOKENS, SUMMARY_ALLOWANCE_TOKENS, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, summaryRequest, splitHead, dropSupersededReads, openFiles, READ_TOOLS, WRITE_TOOLS, OPEN_FILES_TOKENS, PRUNED_NOTE, } from './compact.js';
|
|
11
11
|
export { vaultFetchTool } from './tools/relay.js';
|
|
12
12
|
export { workserTool } from './tools/workser-tool.js';
|
|
13
13
|
export { publishFileTool, mimeOf, MAX_PUBLISH_BYTES } from './tools/publish.js';
|
|
14
14
|
export { withCommandEnv } from './place/env.js';
|
|
15
15
|
export { repairArgs, parseLoose, TOOL_ALIASES } from './tools/repair.js';
|
|
16
|
+
export { DsmlFilter, stripDsml } from './dsml.js';
|
package/dist/src/model.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* An OpenAI-compatible chat-completions client — Workser's gateway speaks
|
|
3
|
-
* this, and the key is the RUN token (the gateway books every call against
|
|
4
|
-
* the run). Nothing else: no provider key is known to the engine.
|
|
5
|
-
*/
|
|
6
1
|
export interface ChatMessage {
|
|
7
2
|
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
8
3
|
content: string | null;
|
package/dist/src/model.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An OpenAI-compatible chat-completions client — Workser's gateway speaks
|
|
3
|
+
* this, and the key is the RUN token (the gateway books every call against
|
|
4
|
+
* the run). Nothing else: no provider key is known to the engine.
|
|
5
|
+
*/
|
|
6
|
+
import { DsmlFilter, stripDsml } from './dsml.js';
|
|
1
7
|
export class ModelError extends Error {
|
|
2
8
|
status;
|
|
3
9
|
constructor(message, status) {
|
|
@@ -94,12 +100,13 @@ export async function chat(input) {
|
|
|
94
100
|
const thought = reasoningOf(choice.message);
|
|
95
101
|
if (thought && input.onReasoning)
|
|
96
102
|
input.onReasoning(thought);
|
|
97
|
-
|
|
98
|
-
|
|
103
|
+
const said = typeof choice.message.content === 'string' ? stripDsml(choice.message.content) : choice.message.content;
|
|
104
|
+
if (said && input.onDelta)
|
|
105
|
+
input.onDelta(said);
|
|
99
106
|
return {
|
|
100
107
|
message: {
|
|
101
108
|
role: 'assistant',
|
|
102
|
-
content:
|
|
109
|
+
content: said || null,
|
|
103
110
|
...(Array.isArray(choice.message.tool_calls) && choice.message.tool_calls.length
|
|
104
111
|
? { tool_calls: choice.message.tool_calls }
|
|
105
112
|
: {}),
|
|
@@ -128,6 +135,7 @@ async function readStream(body, onDelta, status, onReasoning) {
|
|
|
128
135
|
let finishReason = null;
|
|
129
136
|
let usage;
|
|
130
137
|
let provider = '';
|
|
138
|
+
const dsml = new DsmlFilter();
|
|
131
139
|
const calls = [];
|
|
132
140
|
let sawChoice = false;
|
|
133
141
|
const handle = (data) => {
|
|
@@ -158,7 +166,9 @@ async function readStream(body, onDelta, status, onReasoning) {
|
|
|
158
166
|
onReasoning(thought);
|
|
159
167
|
if (typeof delta.content === 'string' && delta.content) {
|
|
160
168
|
content += delta.content;
|
|
161
|
-
|
|
169
|
+
const shown = dsml.push(delta.content);
|
|
170
|
+
if (shown)
|
|
171
|
+
onDelta(shown);
|
|
162
172
|
}
|
|
163
173
|
for (const part of Array.isArray(delta.tool_calls) ? delta.tool_calls : []) {
|
|
164
174
|
const index = typeof part.index === 'number' ? part.index : calls.length;
|
|
@@ -193,6 +203,10 @@ async function readStream(body, onDelta, status, onReasoning) {
|
|
|
193
203
|
handle(buffer.trim().slice(5).trim());
|
|
194
204
|
if (!sawChoice)
|
|
195
205
|
throw new ModelError('The model answered with no message.', status);
|
|
206
|
+
const rest = dsml.flush();
|
|
207
|
+
if (rest)
|
|
208
|
+
onDelta(rest);
|
|
209
|
+
content = stripDsml(content);
|
|
196
210
|
const toolCalls = calls.filter(Boolean).map((call, i) => ({ ...call, id: call.id || `call_${i}` }));
|
|
197
211
|
return {
|
|
198
212
|
message: { role: 'assistant', content: content || null, ...(toolCalls.length ? { tool_calls: toolCalls } : {}) },
|
package/dist/src/session.d.ts
CHANGED
|
@@ -73,6 +73,11 @@ export declare class CoreSession implements EngineSession {
|
|
|
73
73
|
private compactions;
|
|
74
74
|
/** Turns to wait before trying to make room again after a summary that failed (each try is a paid model call). */
|
|
75
75
|
private compactCooldown;
|
|
76
|
+
/**
|
|
77
|
+
* Summaries in a row with no file changed between them (owner 17 Nov, trace 01a1e8ed: twelve in twelve minutes,
|
|
78
|
+
* nothing written). Each one doubles the next trigger (`budget`) and the summary tells the job to write.
|
|
79
|
+
*/
|
|
80
|
+
private stalls;
|
|
76
81
|
constructor(options: CoreSessionOptions);
|
|
77
82
|
/** The id `engine.session` names, so a restarted runner can resume this job. */
|
|
78
83
|
get id(): string;
|
package/dist/src/session.js
CHANGED
|
@@ -3,7 +3,7 @@ import { environmentPrompt, providerPrompt } from './prompts.js';
|
|
|
3
3
|
import { bashTool, ShellSession } from './tools/shell.js';
|
|
4
4
|
import { editTool, readTool, todoWriteTool, writeTool } from './tools/files.js';
|
|
5
5
|
import { applyPatchTool, usesPatch } from './tools/patch.js';
|
|
6
|
-
import { DEFAULT_BUDGET, makeRoom, summaryRequest, withSummary, } from './compact.js';
|
|
6
|
+
import { DEFAULT_BUDGET, makeRoom, summaryRequest, splitHead, withSummary, WRITE_TOOLS, } from './compact.js';
|
|
7
7
|
import { globTool, grepTool } from './tools/search.js';
|
|
8
8
|
import { repairArgs, TOOL_ALIASES } from './tools/repair.js';
|
|
9
9
|
import { PermissionDenied } from './tools/types.js';
|
|
@@ -54,6 +54,11 @@ export class CoreSession {
|
|
|
54
54
|
compactions = 0;
|
|
55
55
|
/** Turns to wait before trying to make room again after a summary that failed (each try is a paid model call). */
|
|
56
56
|
compactCooldown = 0;
|
|
57
|
+
/**
|
|
58
|
+
* Summaries in a row with no file changed between them (owner 17 Nov, trace 01a1e8ed: twelve in twelve minutes,
|
|
59
|
+
* nothing written). Each one doubles the next trigger (`budget`) and the summary tells the job to write.
|
|
60
|
+
*/
|
|
61
|
+
stalls = 0;
|
|
57
62
|
constructor(options) {
|
|
58
63
|
this.options = options;
|
|
59
64
|
this.done = new Promise((resolve) => (this.settle = resolve));
|
|
@@ -80,11 +85,13 @@ export class CoreSession {
|
|
|
80
85
|
}
|
|
81
86
|
get budget() {
|
|
82
87
|
const fromInput = Number(this.options.brief.run.input?.context_tokens);
|
|
83
|
-
|
|
88
|
+
const base = {
|
|
84
89
|
...DEFAULT_BUDGET,
|
|
85
90
|
...(Number.isFinite(fromInput) && fromInput >= 8_000 ? { contextTokens: fromInput } : {}),
|
|
86
91
|
...(this.options.budget ?? {}),
|
|
87
92
|
};
|
|
93
|
+
// The stall guard: each summary with no change to any file since the one before doubles the room, up to 4x.
|
|
94
|
+
return this.stalls ? { ...base, contextTokens: base.contextTokens * Math.min(4, 2 ** this.stalls) } : base;
|
|
88
95
|
}
|
|
89
96
|
get idle() {
|
|
90
97
|
return !this.working;
|
|
@@ -399,12 +406,15 @@ export class CoreSession {
|
|
|
399
406
|
const cut = plan.cut;
|
|
400
407
|
if (cut < 0)
|
|
401
408
|
return;
|
|
409
|
+
// Nothing written since the last summary (its messages are all after the head): this one is a stall.
|
|
410
|
+
const wrote = this.messages.slice(2).some((message) => (message.tool_calls ?? []).some((call) => WRITE_TOOLS.has(call.function.name)));
|
|
411
|
+
const stalled = this.compactions > 0 && !wrote;
|
|
402
412
|
try {
|
|
403
413
|
const result = await chat({
|
|
404
414
|
baseUrl: this.options.modelBaseUrl,
|
|
405
415
|
key: this.key(),
|
|
406
416
|
model: this.model,
|
|
407
|
-
messages: summaryRequest(this.messages.slice(2, cut)),
|
|
417
|
+
messages: summaryRequest(this.messages.slice(2, cut), splitHead(this.messages[1]?.content).request, splitHead(this.messages[1]?.content).earlier),
|
|
408
418
|
tools: [],
|
|
409
419
|
signal: this.abort.signal,
|
|
410
420
|
...(this.options.fetch ? { fetch: this.options.fetch } : {}),
|
|
@@ -417,7 +427,10 @@ export class CoreSession {
|
|
|
417
427
|
if (result.usage) {
|
|
418
428
|
this.options.emit('usage', usageEvent(this.model, result.usage, result.provider));
|
|
419
429
|
}
|
|
420
|
-
|
|
430
|
+
this.stalls = stalled ? this.stalls + 1 : 0;
|
|
431
|
+
const next = withSummary(this.messages, cut, summary, stalled
|
|
432
|
+
? 'You made room twice without changing a file. The files you need are below: make your change now, without reading them again.'
|
|
433
|
+
: undefined);
|
|
421
434
|
this.messages.splice(0, this.messages.length, ...next);
|
|
422
435
|
this.compactions += 1;
|
|
423
436
|
this.options.emit('log', { level: 'info', text: "Summarized the earlier conversation to keep the job in the model's window." });
|
package/dist/src/tools/search.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { vendored } from '../vendor.js';
|
|
2
2
|
import { absolute, str } from './types.js';
|
|
3
3
|
const LIMIT = 100;
|
|
4
|
+
/** Left out of `glob` unless the pattern names them. */
|
|
5
|
+
const SKIPPED_DIRS = ['node_modules', '.next', 'dist', 'build', '.turbo', '.vercel'];
|
|
4
6
|
const MAX_LINE_LENGTH = 2000;
|
|
5
7
|
function quote(value) {
|
|
6
8
|
return `'${value.replace(/'/g, `'\\''`)}'`;
|
|
@@ -23,10 +25,13 @@ export const globTool = {
|
|
|
23
25
|
async execute(args, ctx) {
|
|
24
26
|
const pattern = str(args.pattern, 'pattern');
|
|
25
27
|
const search = typeof args.path === 'string' && args.path ? absolute(ctx.root, args.path) : ctx.root;
|
|
28
|
+
// Installed packages and build output are never the job's files (trace 01a1e93d: `**/*` answered with 100 paths
|
|
29
|
+
// under node_modules/date-fns). A pattern that names one of them still finds it.
|
|
30
|
+
const skip = /node_modules|\.next|dist\b|build\b/.test(pattern) ? [] : SKIPPED_DIRS;
|
|
26
31
|
const result = await ctx.place.exec(
|
|
27
|
-
// ripgrep, as upstream; plain `find` where a machine has no rg.
|
|
28
|
-
`if command -v rg >/dev/null 2>&1; then rg --files --hidden --glob '!.git' --glob ${quote(pattern)} --sortr modified . 2>/dev/null; ` +
|
|
29
|
-
`else find . -type f -not -path './.git/*' \\( -path ${quote(`./${pattern.replace(/^\.\//, '')}`)} -o -name ${quote(pattern)} \\) 2>/dev/null; fi | head -n ${LIMIT}`, { cwd: search, timeoutMs: 60_000, signal: ctx.signal });
|
|
32
|
+
// ripgrep, as upstream (a later --glob wins, so the skips go after the pattern); plain `find` where a machine has no rg.
|
|
33
|
+
`if command -v rg >/dev/null 2>&1; then rg --files --hidden --glob '!.git' --glob ${quote(pattern)} ${skip.map((dir) => `--glob '!${dir}'`).join(' ')} --sortr modified . 2>/dev/null; ` +
|
|
34
|
+
`else find . -type f -not -path './.git/*' ${skip.map((dir) => `-not -path '*/${dir}/*'`).join(' ')} \\( -path ${quote(`./${pattern.replace(/^\.\//, '')}`)} -o -name ${quote(pattern)} \\) 2>/dev/null; fi | head -n ${LIMIT}`, { cwd: search, timeoutMs: 60_000, signal: ctx.signal });
|
|
30
35
|
const files = result.stdout.split('\n').map((one) => one.trim()).filter(Boolean).map((one) => absolute(search, one.replace(/^\.\//, '')));
|
|
31
36
|
const output = [];
|
|
32
37
|
if (files.length === 0)
|
|
@@ -14,7 +14,9 @@ export function workserTool(client) {
|
|
|
14
14
|
name: 'workser_tool',
|
|
15
15
|
description: "Use Workser's tools and the project's connected apps. action 'list' shows the tools (add query, e.g. \"db\", \"env\", \"payments\", to see only those); action 'call' runs one by name. " +
|
|
16
16
|
"A tool that changes something outside this job, or spends money, needs the owner's yes first: you get approval_required " +
|
|
17
|
-
'and an approval_id; call again with approval_id (and spend_approval_id for a spend over the limit) after they approve.'
|
|
17
|
+
'and an approval_id; call again with approval_id (and spend_approval_id for a spend over the limit) after they approve. ' +
|
|
18
|
+
'Send approval fields only with an id a result gave you, never empty. Every call needs its name. Work on the preview copy (its database, its settings) runs at once with no approval; ' +
|
|
19
|
+
'only the live site, live data, money and domains ask the owner first.',
|
|
18
20
|
parameters: {
|
|
19
21
|
type: 'object',
|
|
20
22
|
properties: {
|
|
@@ -37,13 +39,16 @@ export function workserTool(client) {
|
|
|
37
39
|
output: JSON.stringify({ ...list, tools: shown, ...(shown.length < list.tools.length ? { of: list.tools.length } : {}) }, null, 1).slice(0, 50_000),
|
|
38
40
|
};
|
|
39
41
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
// Trace 01a1d4cc (13 Nov): 4 of 57 calls of a job went out as {action: "call", approval_id: "", spend_approval_id: ""} with no
|
|
43
|
+
// name (the model reached for an approval on a preview DELETE that needed none), failed, and were sent again.
|
|
44
|
+
if (args.action !== 'call' || typeof args.name !== 'string' || !args.name) {
|
|
45
|
+
throw new Error("action is 'list' or 'call'. A call needs a name (the tool's, for example name: \"app_db_query\" with its arguments); approval_id only when a result gave you one.");
|
|
46
|
+
}
|
|
42
47
|
const out = await client.callTool(args.name, {
|
|
43
48
|
...(args.arguments && typeof args.arguments === 'object' ? { arguments: args.arguments } : {}),
|
|
44
|
-
...(typeof args.approval_id === 'string' ? { approval_id: args.approval_id } : {}),
|
|
49
|
+
...(typeof args.approval_id === 'string' && args.approval_id.trim() ? { approval_id: args.approval_id } : {}),
|
|
45
50
|
...(typeof args.amount_usd === 'number' ? { amount_usd: args.amount_usd } : {}),
|
|
46
|
-
...(typeof args.spend_approval_id === 'string' ? { spend_approval_id: args.spend_approval_id } : {}),
|
|
51
|
+
...(typeof args.spend_approval_id === 'string' && args.spend_approval_id.trim() ? { spend_approval_id: args.spend_approval_id } : {}),
|
|
47
52
|
});
|
|
48
53
|
return {
|
|
49
54
|
title: `${args.name}: ${out.status}`,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workser/engine",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.19",
|
|
4
4
|
"description": "Workser's coding engine for the 'loop in our service' mode: OpenCode's agent contract (its prompts, tool schemas and edit semantics, vendored from v1.19.5) running on Node, with every file and shell action going through a Place — so the machine can be a remote sandbox and no model key is ever on it.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|