@workser/engine 0.1.20 → 0.1.23
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 +24 -0
- package/dist/src/compact.js +78 -1
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.js +2 -1
- package/dist/src/session.js +7 -1
- package/dist/src/tools/files.d.ts +9 -0
- package/dist/src/tools/files.js +81 -2
- package/dist/src/tools/workser-tool.d.ts +9 -0
- package/dist/src/tools/workser-tool.js +28 -0
- package/package.json +1 -1
package/dist/src/compact.d.ts
CHANGED
|
@@ -37,6 +37,11 @@ export interface CompactionBudget {
|
|
|
37
37
|
* one on DeepSeek). Landing well below leaves room for many calls to append to a prompt that stays cached.
|
|
38
38
|
*/
|
|
39
39
|
lowWaterShare?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Clear out-of-date results (`tidyStale`) before a call once they add up to this many tokens, without waiting for
|
|
42
|
+
* the compaction. For models whose cached tokens are barely cheaper than fresh ones (Kimi); unset (off) by default.
|
|
43
|
+
*/
|
|
44
|
+
tidyStaleTokens?: number;
|
|
40
45
|
}
|
|
41
46
|
/** Where a job makes room at the latest (prompt diet, 1 Nov 2026): the same point Workser's other AI agents summarize at. */
|
|
42
47
|
export declare const COST_COMPACT_TOKENS = 48000;
|
|
@@ -85,6 +90,25 @@ export declare const WRITE_TOOLS: ReadonlySet<string>;
|
|
|
85
90
|
export declare const OPEN_FILES_TOKENS = 20000;
|
|
86
91
|
/** Step 0. Out-of-date reads become a one-line note (changed in place); returns how many. */
|
|
87
92
|
export declare function dropSupersededReads(messages: ChatMessage[]): number;
|
|
93
|
+
/** Commands whose output is replaced when the same command is run again. */
|
|
94
|
+
export declare const RUN_TOOLS: ReadonlySet<string>;
|
|
95
|
+
/**
|
|
96
|
+
* What is out of date in the conversation, without changing it: reads a later read or change made stale (as
|
|
97
|
+
* `dropSupersededReads`), and the outputs of a command that was run again later word for word (trace 01a1e9fc: the
|
|
98
|
+
* job's own browser check, run 12 times, each 4k characters, every old run resent on every call).
|
|
99
|
+
*/
|
|
100
|
+
export declare function staleResults(messages: ChatMessage[]): Array<{
|
|
101
|
+
message: ChatMessage;
|
|
102
|
+
note: string;
|
|
103
|
+
}>;
|
|
104
|
+
/**
|
|
105
|
+
* CLEAR WHAT IS OUT OF DATE AS SOON AS IT ADDS UP (owner 17 Nov: "optimize it more for Kimi"). Each call resends the whole
|
|
106
|
+
* conversation. On Kimi a cached token costs $0.18/1M against $0.253 fresh, so rewriting history costs one call about
|
|
107
|
+
* $0.003 more, while 6k tokens of stale output resent on the next twenty calls cost about $0.02. (On DeepSeek a cached
|
|
108
|
+
* token is about 1/50 of a fresh one, so there the rewrite costs more than it saves: the budget leaves this off.) Only
|
|
109
|
+
* what is out of date goes, so nothing the job still needs is lost. Returns how many results were cleared (in place).
|
|
110
|
+
*/
|
|
111
|
+
export declare function tidyStale(messages: ChatMessage[], minTokens: number): number;
|
|
88
112
|
/** The latest read of each file still as it was read (not changed since), newest first, within `maxTokens`. */
|
|
89
113
|
export declare function openFiles(messages: ChatMessage[], maxTokens?: number): Array<{
|
|
90
114
|
path: string;
|
package/dist/src/compact.js
CHANGED
|
@@ -156,6 +156,83 @@ export function dropSupersededReads(messages) {
|
|
|
156
156
|
}
|
|
157
157
|
return dropped;
|
|
158
158
|
}
|
|
159
|
+
/** Commands whose output is replaced when the same command is run again. */
|
|
160
|
+
export const RUN_TOOLS = new Set(['bash']);
|
|
161
|
+
function commandOf(call) {
|
|
162
|
+
try {
|
|
163
|
+
const command = JSON.parse(call.function.arguments || '{}').command;
|
|
164
|
+
return typeof command === 'string' && command.trim() ? command.trim() : null;
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* What is out of date in the conversation, without changing it: reads a later read or change made stale (as
|
|
172
|
+
* `dropSupersededReads`), and the outputs of a command that was run again later word for word (trace 01a1e9fc: the
|
|
173
|
+
* job's own browser check, run 12 times, each 4k characters, every old run resent on every call).
|
|
174
|
+
*/
|
|
175
|
+
export function staleResults(messages) {
|
|
176
|
+
const calls = callsById(messages);
|
|
177
|
+
const readLater = new Set();
|
|
178
|
+
const changedLater = new Set();
|
|
179
|
+
const ranLater = new Set();
|
|
180
|
+
const out = [];
|
|
181
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
182
|
+
const message = messages[i];
|
|
183
|
+
for (const call of message.tool_calls ?? []) {
|
|
184
|
+
if (WRITE_TOOLS.has(call.function.name)) {
|
|
185
|
+
const path = pathOfCall(call);
|
|
186
|
+
if (path)
|
|
187
|
+
changedLater.add(path);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (message.role !== 'tool' || !message.tool_call_id || !message.content || message.content.startsWith('['))
|
|
191
|
+
continue;
|
|
192
|
+
const call = calls.get(message.tool_call_id);
|
|
193
|
+
if (!call)
|
|
194
|
+
continue;
|
|
195
|
+
if (RUN_TOOLS.has(call.function.name)) {
|
|
196
|
+
const command = commandOf(call);
|
|
197
|
+
if (!command)
|
|
198
|
+
continue;
|
|
199
|
+
if (ranLater.has(command))
|
|
200
|
+
out.push({ message, note: `[Old output: this command was run again later: ${command.slice(0, 80)}]` });
|
|
201
|
+
ranLater.add(command);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (!READ_TOOLS.has(call.function.name))
|
|
205
|
+
continue;
|
|
206
|
+
const key = readKey(call);
|
|
207
|
+
const path = pathOfCall(call);
|
|
208
|
+
if (!key || !path)
|
|
209
|
+
continue;
|
|
210
|
+
if (readLater.has(key))
|
|
211
|
+
out.push({ message, note: `[Read again later: ${path}]` });
|
|
212
|
+
else if (changedLater.has(path))
|
|
213
|
+
out.push({ message, note: `[${path} was changed after this read]` });
|
|
214
|
+
readLater.add(key);
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* CLEAR WHAT IS OUT OF DATE AS SOON AS IT ADDS UP (owner 17 Nov: "optimize it more for Kimi"). Each call resends the whole
|
|
220
|
+
* conversation. On Kimi a cached token costs $0.18/1M against $0.253 fresh, so rewriting history costs one call about
|
|
221
|
+
* $0.003 more, while 6k tokens of stale output resent on the next twenty calls cost about $0.02. (On DeepSeek a cached
|
|
222
|
+
* token is about 1/50 of a fresh one, so there the rewrite costs more than it saves: the budget leaves this off.) Only
|
|
223
|
+
* what is out of date goes, so nothing the job still needs is lost. Returns how many results were cleared (in place).
|
|
224
|
+
*/
|
|
225
|
+
export function tidyStale(messages, minTokens) {
|
|
226
|
+
if (!(minTokens > 0))
|
|
227
|
+
return 0;
|
|
228
|
+
const stale = staleResults(messages);
|
|
229
|
+
const tokens = stale.reduce((sum, one) => sum + Math.ceil((one.message.content ?? '').length / 4), 0);
|
|
230
|
+
if (tokens < minTokens)
|
|
231
|
+
return 0;
|
|
232
|
+
for (const one of stale)
|
|
233
|
+
one.message.content = one.note;
|
|
234
|
+
return stale.length;
|
|
235
|
+
}
|
|
159
236
|
/** The latest read of each file still as it was read (not changed since), newest first, within `maxTokens`. */
|
|
160
237
|
export function openFiles(messages, maxTokens = OPEN_FILES_TOKENS) {
|
|
161
238
|
const calls = callsById(messages);
|
|
@@ -294,7 +371,7 @@ export function makeRoom(messages, budget, reportedPromptTokens) {
|
|
|
294
371
|
return null;
|
|
295
372
|
const overhead = Math.max(0, (reportedPromptTokens ?? 0) - estimateTokens(messages));
|
|
296
373
|
// 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);
|
|
374
|
+
const superseded = dropSupersededReads(messages) + tidyStale(messages, 1);
|
|
298
375
|
const cleared = pruneToolOutputs(messages, budget.keepToolTokens) + superseded;
|
|
299
376
|
const shortened = shortenOldArguments(messages, budget.keepToolTokens);
|
|
300
377
|
if (estimateTokens(messages) + overhead <= lowWater(budget))
|
package/dist/src/index.d.ts
CHANGED
|
@@ -6,8 +6,9 @@ export { OPENCODE_VERSION } from './vendor.js';
|
|
|
6
6
|
export { providerPrompt, environmentPrompt, bashDescription } from './prompts.js';
|
|
7
7
|
export { needsAsk } from './tools/shell.js';
|
|
8
8
|
export { ShellSession } from './tools/shell.js';
|
|
9
|
+
export { editMissHint } from './tools/files.js';
|
|
9
10
|
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, summaryRequest, splitHead, dropSupersededReads, openFiles, READ_TOOLS, WRITE_TOOLS, OPEN_FILES_TOKENS, PRUNED_NOTE, type CompactionBudget, } from './compact.js';
|
|
11
|
+
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, staleResults, tidyStale, RUN_TOOLS, openFiles, READ_TOOLS, WRITE_TOOLS, OPEN_FILES_TOKENS, PRUNED_NOTE, type CompactionBudget, } from './compact.js';
|
|
11
12
|
export { vaultFetchTool, type KeyRelay } from './tools/relay.js';
|
|
12
13
|
export { workserTool, type ToolsClient } from './tools/workser-tool.js';
|
|
13
14
|
export { publishFileTool, mimeOf, MAX_PUBLISH_BYTES, type FileClient } from './tools/publish.js';
|
package/dist/src/index.js
CHANGED
|
@@ -6,8 +6,9 @@ export { OPENCODE_VERSION } from './vendor.js';
|
|
|
6
6
|
export { providerPrompt, environmentPrompt, bashDescription } from './prompts.js';
|
|
7
7
|
export { needsAsk } from './tools/shell.js';
|
|
8
8
|
export { ShellSession } from './tools/shell.js';
|
|
9
|
+
export { editMissHint } from './tools/files.js';
|
|
9
10
|
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, summaryRequest, splitHead, dropSupersededReads, openFiles, READ_TOOLS, WRITE_TOOLS, OPEN_FILES_TOKENS, PRUNED_NOTE, } from './compact.js';
|
|
11
|
+
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, staleResults, tidyStale, RUN_TOOLS, openFiles, READ_TOOLS, WRITE_TOOLS, OPEN_FILES_TOKENS, PRUNED_NOTE, } from './compact.js';
|
|
11
12
|
export { vaultFetchTool } from './tools/relay.js';
|
|
12
13
|
export { workserTool } from './tools/workser-tool.js';
|
|
13
14
|
export { publishFileTool, mimeOf, MAX_PUBLISH_BYTES } from './tools/publish.js';
|
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, multiEditTool, readTool, todoWriteTool, writeTool } from './tools/files.js';
|
|
5
5
|
import { applyPatchTool, usesPatch } from './tools/patch.js';
|
|
6
|
-
import { DEFAULT_BUDGET, makeRoom, summaryRequest, splitHead, withSummary, WRITE_TOOLS, } from './compact.js';
|
|
6
|
+
import { DEFAULT_BUDGET, makeRoom, tidyStale, 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';
|
|
@@ -396,6 +396,12 @@ export class CoreSession {
|
|
|
396
396
|
}
|
|
397
397
|
// ONE STEP DOWN TO THE LOW WATER (compact.ts `makeRoom`): a compaction that stops just under its trigger is back at
|
|
398
398
|
// it a call later, and each one rewrites the history the provider has cached.
|
|
399
|
+
// Out-of-date results go once they add up, where rewriting history is cheap (compact.ts `tidyStale`).
|
|
400
|
+
const tidied = tidyStale(this.messages, budget.tidyStaleTokens ?? 0);
|
|
401
|
+
if (tidied) {
|
|
402
|
+
this.lastPromptTokens = 0; // the reported size is of the old conversation
|
|
403
|
+
this.options.emit('log', { level: 'info', text: `Cleared ${tidied} out-of-date tool results to keep the job small.` });
|
|
404
|
+
}
|
|
399
405
|
const plan = makeRoom(this.messages, budget, this.lastPromptTokens);
|
|
400
406
|
if (!plan)
|
|
401
407
|
return;
|
|
@@ -3,6 +3,15 @@ import { type ToolContext, type ToolSpec } from './types.js';
|
|
|
3
3
|
export declare const readTool: ToolSpec;
|
|
4
4
|
/** Upstream `write`: whole-file write; asks for the `edit` permission. */
|
|
5
5
|
export declare const writeTool: ToolSpec;
|
|
6
|
+
/**
|
|
7
|
+
* A MISSED EDIT SAYS WHERE TO LOOK (owner 17 Nov, trace 01a1e9fc: on Kimi one multiedit was sent four times with the same
|
|
8
|
+
* wrong line, guessing `.slice(0, 1500)`, `4500`, `5000`, `4000` instead of re-reading; a 15-edit multiedit was lost to
|
|
9
|
+
* one line that matched twice). Every retry resends the whole job, so the error carries what the retry needs: the file's
|
|
10
|
+
* closest lines, numbered as `read` numbers them, or the lines where the text matched more than once.
|
|
11
|
+
*/
|
|
12
|
+
export declare const HINT_MAX_LINES = 12;
|
|
13
|
+
/** The hint for an edit that did not apply, or '' when there is nothing useful to show. */
|
|
14
|
+
export declare function editMissHint(content: string, oldString: string, message: string): string;
|
|
6
15
|
/** Upstream `edit`: oldString → newString with upstream's own matching (vendored). */
|
|
7
16
|
export declare const editTool: ToolSpec;
|
|
8
17
|
/**
|
package/dist/src/tools/files.js
CHANGED
|
@@ -143,6 +143,85 @@ export const writeTool = {
|
|
|
143
143
|
};
|
|
144
144
|
},
|
|
145
145
|
};
|
|
146
|
+
/**
|
|
147
|
+
* A MISSED EDIT SAYS WHERE TO LOOK (owner 17 Nov, trace 01a1e9fc: on Kimi one multiedit was sent four times with the same
|
|
148
|
+
* wrong line, guessing `.slice(0, 1500)`, `4500`, `5000`, `4000` instead of re-reading; a 15-edit multiedit was lost to
|
|
149
|
+
* one line that matched twice). Every retry resends the whole job, so the error carries what the retry needs: the file's
|
|
150
|
+
* closest lines, numbered as `read` numbers them, or the lines where the text matched more than once.
|
|
151
|
+
*/
|
|
152
|
+
export const HINT_MAX_LINES = 12;
|
|
153
|
+
const HINT_LINE_CHARS = 240;
|
|
154
|
+
const words = (line) => line.toLowerCase().match(/[a-z0-9_$]+|[^\sa-z0-9_$]/g) ?? [];
|
|
155
|
+
/** How alike two lines are (0..1): shared words and marks over both lines' counts. */
|
|
156
|
+
function alike(a, b) {
|
|
157
|
+
const left = words(a);
|
|
158
|
+
const right = words(b);
|
|
159
|
+
if (!left.length || !right.length)
|
|
160
|
+
return 0;
|
|
161
|
+
const pool = new Map();
|
|
162
|
+
for (const w of right)
|
|
163
|
+
pool.set(w, (pool.get(w) ?? 0) + 1);
|
|
164
|
+
let shared = 0;
|
|
165
|
+
for (const w of left) {
|
|
166
|
+
const n = pool.get(w) ?? 0;
|
|
167
|
+
if (n > 0) {
|
|
168
|
+
shared++;
|
|
169
|
+
pool.set(w, n - 1);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return (2 * shared) / (left.length + right.length);
|
|
173
|
+
}
|
|
174
|
+
const numbered = (lines, from, to) => lines
|
|
175
|
+
.slice(from, to)
|
|
176
|
+
.map((line, i) => `${from + i + 1}: ${line.length > HINT_LINE_CHARS ? `${line.slice(0, HINT_LINE_CHARS)}…` : line}`)
|
|
177
|
+
.join('\n');
|
|
178
|
+
/** The hint for an edit that did not apply, or '' when there is nothing useful to show. */
|
|
179
|
+
export function editMissHint(content, oldString, message) {
|
|
180
|
+
const lines = content.split('\n');
|
|
181
|
+
if (/multiple matches/i.test(message)) {
|
|
182
|
+
const at = [];
|
|
183
|
+
for (let index = content.indexOf(oldString); index !== -1 && at.length < 8; index = content.indexOf(oldString, index + 1)) {
|
|
184
|
+
at.push(content.slice(0, index).split('\n').length);
|
|
185
|
+
}
|
|
186
|
+
return at.length > 1 ? `\nIt matches at lines ${at.join(', ')}: add a nearby line to oldString so only the one you mean matches.` : '';
|
|
187
|
+
}
|
|
188
|
+
if (!/could not find/i.test(message))
|
|
189
|
+
return '';
|
|
190
|
+
const wanted = oldString.split('\n');
|
|
191
|
+
// The search line: oldString's longest line (the most telling), and where it sits in oldString.
|
|
192
|
+
let key = 0;
|
|
193
|
+
wanted.forEach((line, i) => {
|
|
194
|
+
if (line.trim().length > wanted[key].trim().length)
|
|
195
|
+
key = i;
|
|
196
|
+
});
|
|
197
|
+
const keyLine = wanted[key].trim();
|
|
198
|
+
if (!keyLine)
|
|
199
|
+
return '';
|
|
200
|
+
let best = -1;
|
|
201
|
+
let score = 0;
|
|
202
|
+
lines.forEach((line, i) => {
|
|
203
|
+
const s = alike(keyLine, line.trim());
|
|
204
|
+
if (s > score) {
|
|
205
|
+
score = s;
|
|
206
|
+
best = i;
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
if (best < 0 || score < 0.4)
|
|
210
|
+
return '';
|
|
211
|
+
const span = Math.min(HINT_MAX_LINES, Math.max(1, wanted.length));
|
|
212
|
+
const from = Math.max(0, Math.min(best - key, lines.length - span));
|
|
213
|
+
return `\nThe closest text in the file (line ${best + 1}), as it is now:\n${numbered(lines, from, from + span)}\nCopy oldString from these lines exactly (without the line numbers), or read the file again.`;
|
|
214
|
+
}
|
|
215
|
+
/** `replace` whose error, when the text was not found or not unique, says where to look. */
|
|
216
|
+
function replaceOrHint(content, oldString, newString, replaceAll) {
|
|
217
|
+
try {
|
|
218
|
+
return replace(content, oldString, newString, replaceAll);
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
const message = error?.message ?? String(error);
|
|
222
|
+
throw new Error(`${message}${editMissHint(content, oldString, message)}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
146
225
|
/** Upstream `edit`: oldString → newString with upstream's own matching (vendored). */
|
|
147
226
|
export const editTool = {
|
|
148
227
|
name: 'edit',
|
|
@@ -176,7 +255,7 @@ export const editTool = {
|
|
|
176
255
|
const before = new TextDecoder().decode(await ctx.place.readFile(filepath));
|
|
177
256
|
const ending = before.includes('\r\n') ? '\r\n' : '\n';
|
|
178
257
|
const normal = before.replaceAll('\r\n', '\n');
|
|
179
|
-
const after =
|
|
258
|
+
const after = replaceOrHint(normal, oldString.replaceAll('\r\n', '\n'), newString.replaceAll('\r\n', '\n'), args.replaceAll === true);
|
|
180
259
|
await ctx.ask({ permission: 'edit', title: `Edit ${filepath}`, detail: filepath, patterns: [filepath] });
|
|
181
260
|
await ctx.place.writeFile(filepath, ending === '\r\n' ? after.replaceAll('\n', '\r\n') : after);
|
|
182
261
|
ctx.read.add(filepath);
|
|
@@ -237,7 +316,7 @@ export const multiEditTool = {
|
|
|
237
316
|
if (oldString === newString)
|
|
238
317
|
throw new Error(`Edit ${index + 1}: oldString and newString are identical.`);
|
|
239
318
|
try {
|
|
240
|
-
text =
|
|
319
|
+
text = replaceOrHint(text, oldString.replaceAll('\r\n', '\n'), newString.replaceAll('\r\n', '\n'), one.replaceAll === true);
|
|
241
320
|
}
|
|
242
321
|
catch (error) {
|
|
243
322
|
throw new Error(`Edit ${index + 1} of ${edits.length} failed, so none were applied: ${error?.message ?? String(error)}`);
|
|
@@ -8,6 +8,7 @@ export interface ToolsClient {
|
|
|
8
8
|
description?: string;
|
|
9
9
|
side_effect?: boolean;
|
|
10
10
|
spends?: boolean;
|
|
11
|
+
input_schema?: unknown;
|
|
11
12
|
}>;
|
|
12
13
|
ask_before_paying_usd?: number;
|
|
13
14
|
}>;
|
|
@@ -49,3 +50,11 @@ export declare function listedTools<T extends {
|
|
|
49
50
|
}>(tools: readonly T[], query: string): Array<T & {
|
|
50
51
|
description?: string;
|
|
51
52
|
}>;
|
|
53
|
+
/**
|
|
54
|
+
* The tools a call's arguments fit (a call that came without its name): every argument given is one of the tool's
|
|
55
|
+
* parameters, and every parameter it requires is given. Tools that don't describe their parameters never fit.
|
|
56
|
+
*/
|
|
57
|
+
export declare function toolsForArguments(tools: ReadonlyArray<{
|
|
58
|
+
name: string;
|
|
59
|
+
input_schema?: unknown;
|
|
60
|
+
}>, given: Record<string, unknown>): string[];
|
|
@@ -41,6 +41,15 @@ export function workserTool(client) {
|
|
|
41
41
|
}
|
|
42
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
43
|
// name (the model reached for an approval on a preview DELETE that needed none), failed, and were sent again.
|
|
44
|
+
// Owner 21 Nov: again, a count on the preview ({query, environment}) with no name stopped a job. A call with arguments
|
|
45
|
+
// that only one tool takes is that tool's call; several (or none) say which names would fit.
|
|
46
|
+
if (args.action === 'call' && (typeof args.name !== 'string' || !args.name) && args.arguments && typeof args.arguments === 'object') {
|
|
47
|
+
const fits = toolsForArguments((await client.listTools()).tools, args.arguments);
|
|
48
|
+
if (fits.length === 1)
|
|
49
|
+
args = { ...args, name: fits[0] };
|
|
50
|
+
else if (fits.length > 1)
|
|
51
|
+
throw new Error(`A call needs a name. These tools take those arguments: ${fits.slice(0, 6).join(', ')}. Call again with name set to the one you mean.`);
|
|
52
|
+
}
|
|
44
53
|
if (args.action !== 'call' || typeof args.name !== 'string' || !args.name) {
|
|
45
54
|
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
55
|
}
|
|
@@ -81,3 +90,22 @@ export function listedTools(tools, query) {
|
|
|
81
90
|
return { ...tool, ...(tool.description ? { description: first } : {}) };
|
|
82
91
|
});
|
|
83
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* The tools a call's arguments fit (a call that came without its name): every argument given is one of the tool's
|
|
95
|
+
* parameters, and every parameter it requires is given. Tools that don't describe their parameters never fit.
|
|
96
|
+
*/
|
|
97
|
+
export function toolsForArguments(tools, given) {
|
|
98
|
+
const keys = Object.keys(given);
|
|
99
|
+
if (!keys.length)
|
|
100
|
+
return [];
|
|
101
|
+
return tools
|
|
102
|
+
.filter((tool) => {
|
|
103
|
+
const schema = tool.input_schema && typeof tool.input_schema === 'object' ? tool.input_schema : null;
|
|
104
|
+
const properties = schema?.properties && typeof schema.properties === 'object' ? Object.keys(schema.properties) : null;
|
|
105
|
+
if (!properties?.length)
|
|
106
|
+
return false;
|
|
107
|
+
const required = Array.isArray(schema.required) ? schema.required.filter((one) => typeof one === 'string') : [];
|
|
108
|
+
return keys.every((key) => properties.includes(key)) && required.every((key) => keys.includes(key));
|
|
109
|
+
})
|
|
110
|
+
.map((tool) => tool.name);
|
|
111
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workser/engine",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.23",
|
|
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",
|