@workser/engine 0.1.10 → 0.1.12
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 +21 -0
- package/dist/src/compact.js +61 -2
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/session.js +8 -5
- package/package.json +1 -1
package/dist/src/compact.d.ts
CHANGED
|
@@ -24,12 +24,33 @@ export interface CompactionBudget {
|
|
|
24
24
|
threshold: number;
|
|
25
25
|
/** Recent tool output kept whole by the prune step, in tokens. */
|
|
26
26
|
keepToolTokens: number;
|
|
27
|
+
/**
|
|
28
|
+
* Compact by here whatever the window (tokens). Every turn resends the whole
|
|
29
|
+
* conversation, so what a turn costs is what it carries, not how much the
|
|
30
|
+
* model could take: a 1M-window model would otherwise carry 800k a turn.
|
|
31
|
+
*/
|
|
32
|
+
maxPromptTokens?: number;
|
|
27
33
|
}
|
|
34
|
+
/** Where a job makes room at the latest (prompt diet, 1 Nov 2026): the same point Workser's other AI agents summarize at. */
|
|
35
|
+
export declare const COST_COMPACT_TOKENS = 48000;
|
|
28
36
|
export declare const DEFAULT_BUDGET: CompactionBudget;
|
|
37
|
+
/** The prompt size (tokens) at which a turn makes room first. */
|
|
38
|
+
export declare function compactAt(budget: CompactionBudget): number;
|
|
29
39
|
export declare function estimateTokens(messages: ChatMessage[]): number;
|
|
30
40
|
export declare function overBudget(messages: ChatMessage[], budget: CompactionBudget, reportedPromptTokens?: number): boolean;
|
|
31
41
|
/** Step 1. Returns how many tool outputs were cleared (the array is changed in place). */
|
|
32
42
|
export declare function pruneToolOutputs(messages: ChatMessage[], keepToolTokens: number): number;
|
|
43
|
+
/** An old call's input longer than this (characters) keeps its head only. */
|
|
44
|
+
export declare const OLD_ARGUMENT_CHARS = 600;
|
|
45
|
+
/**
|
|
46
|
+
* Step 1, second half. The model's own long inputs (a whole file it wrote, an
|
|
47
|
+
* edit's old and new text) older than the most recent `keepTokens` keep their
|
|
48
|
+
* first 300 characters and a note that they were already sent. They rode
|
|
49
|
+
* along on every turn until a summary. The calls stay valid JSON and keep
|
|
50
|
+
* their ids; a question to the owner is never shortened. Changed in place;
|
|
51
|
+
* returns how many calls were shortened.
|
|
52
|
+
*/
|
|
53
|
+
export declare function shortenOldArguments(messages: ChatMessage[], keepTokens: number): number;
|
|
33
54
|
/**
|
|
34
55
|
* Where the kept tail starts for step 2. The system prompt (0) and the job's
|
|
35
56
|
* first owner message (1) always stay; the tail is the most recent turns
|
package/dist/src/compact.js
CHANGED
|
@@ -16,11 +16,18 @@
|
|
|
16
16
|
* route reported the real prompt size, which is then preferred.
|
|
17
17
|
*/
|
|
18
18
|
export const PRUNED_NOTE = '[Old tool result content cleared]';
|
|
19
|
+
/** Where a job makes room at the latest (prompt diet, 1 Nov 2026): the same point Workser's other AI agents summarize at. */
|
|
20
|
+
export const COST_COMPACT_TOKENS = 48_000;
|
|
19
21
|
export const DEFAULT_BUDGET = {
|
|
20
22
|
contextTokens: 128_000,
|
|
21
23
|
threshold: 0.8,
|
|
22
|
-
keepToolTokens:
|
|
24
|
+
keepToolTokens: 16_000,
|
|
25
|
+
maxPromptTokens: COST_COMPACT_TOKENS,
|
|
23
26
|
};
|
|
27
|
+
/** The prompt size (tokens) at which a turn makes room first. */
|
|
28
|
+
export function compactAt(budget) {
|
|
29
|
+
return Math.min(budget.contextTokens * budget.threshold, budget.maxPromptTokens ?? Number.POSITIVE_INFINITY);
|
|
30
|
+
}
|
|
24
31
|
export function estimateTokens(messages) {
|
|
25
32
|
let chars = 0;
|
|
26
33
|
for (const message of messages) {
|
|
@@ -32,7 +39,7 @@ export function estimateTokens(messages) {
|
|
|
32
39
|
}
|
|
33
40
|
export function overBudget(messages, budget, reportedPromptTokens) {
|
|
34
41
|
const tokens = Math.max(estimateTokens(messages), reportedPromptTokens ?? 0);
|
|
35
|
-
return tokens >= budget
|
|
42
|
+
return tokens >= compactAt(budget);
|
|
36
43
|
}
|
|
37
44
|
/** Step 1. Returns how many tool outputs were cleared (the array is changed in place). */
|
|
38
45
|
export function pruneToolOutputs(messages, keepToolTokens) {
|
|
@@ -52,6 +59,58 @@ export function pruneToolOutputs(messages, keepToolTokens) {
|
|
|
52
59
|
}
|
|
53
60
|
return cleared;
|
|
54
61
|
}
|
|
62
|
+
/** An old call's input longer than this (characters) keeps its head only. */
|
|
63
|
+
export const OLD_ARGUMENT_CHARS = 600;
|
|
64
|
+
const ARGUMENT_HEAD = 300;
|
|
65
|
+
function shortValue(value, changed) {
|
|
66
|
+
if (typeof value === 'string') {
|
|
67
|
+
if (value.length <= OLD_ARGUMENT_CHARS)
|
|
68
|
+
return value;
|
|
69
|
+
changed.yes = true;
|
|
70
|
+
return `${value.slice(0, ARGUMENT_HEAD)}[… ${value.length - ARGUMENT_HEAD} more characters not shown to save room. This input was already sent in full; do not send it again.]`;
|
|
71
|
+
}
|
|
72
|
+
if (Array.isArray(value))
|
|
73
|
+
return value.map((one) => shortValue(one, changed));
|
|
74
|
+
if (value && typeof value === 'object')
|
|
75
|
+
return Object.fromEntries(Object.entries(value).map(([key, one]) => [key, shortValue(one, changed)]));
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Step 1, second half. The model's own long inputs (a whole file it wrote, an
|
|
80
|
+
* edit's old and new text) older than the most recent `keepTokens` keep their
|
|
81
|
+
* first 300 characters and a note that they were already sent. They rode
|
|
82
|
+
* along on every turn until a summary. The calls stay valid JSON and keep
|
|
83
|
+
* their ids; a question to the owner is never shortened. Changed in place;
|
|
84
|
+
* returns how many calls were shortened.
|
|
85
|
+
*/
|
|
86
|
+
export function shortenOldArguments(messages, keepTokens) {
|
|
87
|
+
let size = 0;
|
|
88
|
+
let shortened = 0;
|
|
89
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
90
|
+
const message = messages[i];
|
|
91
|
+
size += estimateTokens([message]);
|
|
92
|
+
if (size <= keepTokens || message.role !== 'assistant' || !message.tool_calls?.length)
|
|
93
|
+
continue;
|
|
94
|
+
for (const call of message.tool_calls) {
|
|
95
|
+
if (/^ask/.test(call.function.name) || call.function.arguments.length <= OLD_ARGUMENT_CHARS)
|
|
96
|
+
continue;
|
|
97
|
+
let parsed;
|
|
98
|
+
try {
|
|
99
|
+
parsed = JSON.parse(call.function.arguments);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const changed = { yes: false };
|
|
105
|
+
const next = shortValue(parsed, changed);
|
|
106
|
+
if (!changed.yes)
|
|
107
|
+
continue;
|
|
108
|
+
call.function.arguments = JSON.stringify(next);
|
|
109
|
+
shortened++;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return shortened;
|
|
113
|
+
}
|
|
55
114
|
/**
|
|
56
115
|
* Where the kept tail starts for step 2. The system prompt (0) and the job's
|
|
57
116
|
* first owner message (1) always stay; the tail is the most recent turns
|
package/dist/src/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ 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, estimateTokens, pruneToolOutputs, cutPoint, withSummary, PRUNED_NOTE, type CompactionBudget, } from './compact.js';
|
|
10
|
+
export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, 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 { withCommandEnv } from './place/env.js';
|
package/dist/src/index.js
CHANGED
|
@@ -7,7 +7,7 @@ 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, estimateTokens, pruneToolOutputs, cutPoint, withSummary, PRUNED_NOTE, } from './compact.js';
|
|
10
|
+
export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, 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 { withCommandEnv } from './place/env.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, readTool, todoWriteTool, writeTool } from './tools/files.js';
|
|
5
5
|
import { applyPatchTool, usesPatch } from './tools/patch.js';
|
|
6
|
-
import { DEFAULT_BUDGET, cutPoint, overBudget, pruneToolOutputs, summaryRequest, withSummary, } from './compact.js';
|
|
6
|
+
import { DEFAULT_BUDGET, compactAt, cutPoint, overBudget, pruneToolOutputs, shortenOldArguments, summaryRequest, withSummary, } from './compact.js';
|
|
7
7
|
import { globTool, grepTool } from './tools/search.js';
|
|
8
8
|
import { PermissionDenied } from './tools/types.js';
|
|
9
9
|
export const ENGINE_NAME = 'workser-core';
|
|
@@ -13,7 +13,8 @@ const DELTA_FLUSH_CHARS = 200;
|
|
|
13
13
|
const DELTA_FLUSH_MS = 150;
|
|
14
14
|
/** Thinking a turn may put on the event stream; the rest is dropped (the answer is not). */
|
|
15
15
|
const REASONING_MAX_CHARS = 20_000;
|
|
16
|
-
|
|
16
|
+
/** Workser Code's default model (owner 2026-11-10); a job normally names its own (core picks it). Claude Sonnet 4.5 retires 30 Nov 2026. */
|
|
17
|
+
const DEFAULT_MODEL = 'deepseek/deepseek-v4.1-flash-1017';
|
|
17
18
|
const MAX_OUTPUT_IN_EVENT = 4_000;
|
|
18
19
|
/**
|
|
19
20
|
* OpenCode's agent loop, run where WE choose (plan D1, "loop in our service"):
|
|
@@ -378,13 +379,15 @@ export class CoreSession {
|
|
|
378
379
|
if (!overBudget(this.messages, budget, this.lastPromptTokens))
|
|
379
380
|
return;
|
|
380
381
|
const cleared = pruneToolOutputs(this.messages, budget.keepToolTokens);
|
|
382
|
+
const shortened = shortenOldArguments(this.messages, budget.keepToolTokens);
|
|
381
383
|
this.lastPromptTokens = 0; // the reported size is of the old conversation
|
|
382
384
|
if (!overBudget(this.messages, budget)) {
|
|
383
|
-
if (cleared)
|
|
384
|
-
this.options.emit('log', { level: 'info', text: `Cleared ${cleared} old tool results to keep the job
|
|
385
|
+
if (cleared || shortened)
|
|
386
|
+
this.options.emit('log', { level: 'info', text: `Cleared ${cleared} old tool results and shortened ${shortened} old inputs to keep the job small.` });
|
|
385
387
|
return;
|
|
386
388
|
}
|
|
387
|
-
|
|
389
|
+
// The kept tail is well under the point, so the next turn does not summarize again at once.
|
|
390
|
+
const cut = cutPoint(this.messages, Math.floor(compactAt(budget) * 0.4));
|
|
388
391
|
if (cut < 0)
|
|
389
392
|
return;
|
|
390
393
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workser/engine",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
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.18.23) 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",
|