@workser/engine 0.1.9 → 0.1.11

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.
@@ -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
@@ -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: 40_000,
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.contextTokens * budget.threshold;
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
@@ -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';
@@ -39,6 +39,28 @@ export declare class ModelError extends Error {
39
39
  readonly status: number;
40
40
  constructor(message: string, status: number);
41
41
  }
42
+ type WirePart = {
43
+ type: 'text';
44
+ text: string;
45
+ cache_control?: {
46
+ type: 'ephemeral';
47
+ };
48
+ };
49
+ type WireMessage = Omit<ChatMessage, 'content'> & {
50
+ content: string | WirePart[] | null;
51
+ };
52
+ /**
53
+ * The messages as sent, with prompt-cache breakpoints for a route that needs them.
54
+ *
55
+ * Every turn resends the whole conversation; without a marker an Anthropic model
56
+ * bills all of it at the full input price each time (a 40-turn job cost about six
57
+ * times what a cached one does). Caching is a prefix match, so the markers go at
58
+ * the end of what stays byte-identical: the system prompt (the tool list is sent
59
+ * before it, so it is covered too) and the two newest user or tool turns, which
60
+ * the next turn then reads back. Three markers of Anthropic's four. The
61
+ * session's own messages are never changed; only the request body is.
62
+ */
63
+ export declare function cachedMessages(model: string, messages: ChatMessage[]): WireMessage[];
42
64
  export declare function chat(input: {
43
65
  baseUrl: string;
44
66
  key: string;
@@ -60,3 +82,4 @@ export declare function chat(input: {
60
82
  */
61
83
  onReasoning?: (text: string) => void;
62
84
  }): Promise<ChatResult>;
85
+ export {};
package/dist/src/model.js CHANGED
@@ -6,6 +6,37 @@ export class ModelError extends Error {
6
6
  this.name = 'ModelError';
7
7
  }
8
8
  }
9
+ /** Routes that cache a prompt only where it is marked (others cache on their own, with no marker). */
10
+ const MARKED_CACHE_PREFIXES = ['anthropic/', 'google/'];
11
+ /**
12
+ * The messages as sent, with prompt-cache breakpoints for a route that needs them.
13
+ *
14
+ * Every turn resends the whole conversation; without a marker an Anthropic model
15
+ * bills all of it at the full input price each time (a 40-turn job cost about six
16
+ * times what a cached one does). Caching is a prefix match, so the markers go at
17
+ * the end of what stays byte-identical: the system prompt (the tool list is sent
18
+ * before it, so it is covered too) and the two newest user or tool turns, which
19
+ * the next turn then reads back. Three markers of Anthropic's four. The
20
+ * session's own messages are never changed; only the request body is.
21
+ */
22
+ export function cachedMessages(model, messages) {
23
+ if (!MARKED_CACHE_PREFIXES.some((prefix) => model.startsWith(prefix)))
24
+ return messages;
25
+ const marked = new Set();
26
+ const system = messages.findIndex((one) => one.role === 'system');
27
+ if (system >= 0)
28
+ marked.add(system);
29
+ for (let i = messages.length - 1, n = 0; i >= 0 && n < 2; i--) {
30
+ const role = messages[i].role;
31
+ if ((role === 'user' || role === 'tool') && messages[i].content) {
32
+ marked.add(i);
33
+ n++;
34
+ }
35
+ }
36
+ return messages.map((one, i) => marked.has(i) && one.content
37
+ ? { ...one, content: [{ type: 'text', text: one.content, cache_control: { type: 'ephemeral' } }] }
38
+ : one);
39
+ }
9
40
  export async function chat(input) {
10
41
  const doFetch = input.fetch ?? fetch;
11
42
  const url = `${input.baseUrl.replace(/\/+$/, '')}/chat/completions`;
@@ -19,7 +50,7 @@ export async function chat(input) {
19
50
  headers: { authorization: `Bearer ${input.key}`, 'content-type': 'application/json' },
20
51
  body: JSON.stringify({
21
52
  model: input.model,
22
- messages: input.messages,
53
+ messages: cachedMessages(input.model, input.messages),
23
54
  ...(input.tools.length ? { tools: input.tools, tool_choice: 'auto' } : {}),
24
55
  usage: { include: true },
25
56
  ...(stream ? { stream: true, stream_options: { include_usage: true } } : {}),
@@ -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';
@@ -378,13 +378,15 @@ export class CoreSession {
378
378
  if (!overBudget(this.messages, budget, this.lastPromptTokens))
379
379
  return;
380
380
  const cleared = pruneToolOutputs(this.messages, budget.keepToolTokens);
381
+ const shortened = shortenOldArguments(this.messages, budget.keepToolTokens);
381
382
  this.lastPromptTokens = 0; // the reported size is of the old conversation
382
383
  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 in the model's window.` });
384
+ if (cleared || shortened)
385
+ this.options.emit('log', { level: 'info', text: `Cleared ${cleared} old tool results and shortened ${shortened} old inputs to keep the job small.` });
385
386
  return;
386
387
  }
387
- const cut = cutPoint(this.messages, Math.floor(budget.contextTokens * 0.3));
388
+ // The kept tail is well under the point, so the next turn does not summarize again at once.
389
+ const cut = cutPoint(this.messages, Math.floor(compactAt(budget) * 0.4));
388
390
  if (cut < 0)
389
391
  return;
390
392
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workser/engine",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
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",