@workser/engine 0.1.8 → 0.1.10

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.
@@ -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;
@@ -53,4 +75,11 @@ export declare function chat(input: {
53
75
  * with plain JSON instead of a stream is read as before.
54
76
  */
55
77
  onDelta?: (text: string) => void;
78
+ /**
79
+ * The model's own thinking, when its route streams it (`reasoning` or
80
+ * `reasoning_content` beside `content`). Never part of the message; a route
81
+ * that sends none never calls this.
82
+ */
83
+ onReasoning?: (text: string) => void;
56
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 } } : {}),
@@ -44,7 +75,7 @@ export async function chat(input) {
44
75
  if (res.ok && stream && type.includes('text/event-stream') && res.body) {
45
76
  // Once text has reached the owner, a retry would say it twice: a
46
77
  // stream that breaks midway is an error, not a retry.
47
- return readStream(res.body, input.onDelta, res.status);
78
+ return readStream(res.body, input.onDelta, res.status, input.onReasoning);
48
79
  }
49
80
  const text = await res.text();
50
81
  let body = null;
@@ -60,6 +91,9 @@ export async function chat(input) {
60
91
  const choice = body?.choices?.[0];
61
92
  if (!choice?.message)
62
93
  throw new ModelError('The model answered with no message.', res.status);
94
+ const thought = reasoningOf(choice.message);
95
+ if (thought && input.onReasoning)
96
+ input.onReasoning(thought);
63
97
  if (choice.message.content && input.onDelta)
64
98
  input.onDelta(choice.message.content);
65
99
  return {
@@ -76,8 +110,16 @@ export async function chat(input) {
76
110
  }
77
111
  throw lastError instanceof Error ? lastError : new ModelError('The model route could not be reached.', 0);
78
112
  }
113
+ /** The thinking a message or delta carries: OpenRouter-style `reasoning`, or DeepSeek-style `reasoning_content`. */
114
+ function reasoningOf(part) {
115
+ if (typeof part?.reasoning === 'string' && part.reasoning)
116
+ return part.reasoning;
117
+ if (typeof part?.reasoning_content === 'string' && part.reasoning_content)
118
+ return part.reasoning_content;
119
+ return '';
120
+ }
79
121
  /** An OpenAI-compatible SSE stream, put back together into one message. */
80
- async function readStream(body, onDelta, status) {
122
+ async function readStream(body, onDelta, status, onReasoning) {
81
123
  const reader = body.getReader();
82
124
  const decoder = new TextDecoder();
83
125
  let buffer = '';
@@ -107,6 +149,9 @@ async function readStream(body, onDelta, status) {
107
149
  if (choice.finish_reason)
108
150
  finishReason = choice.finish_reason;
109
151
  const delta = choice.delta ?? {};
152
+ const thought = reasoningOf(delta);
153
+ if (thought && onReasoning)
154
+ onReasoning(thought);
110
155
  if (typeof delta.content === 'string' && delta.content) {
111
156
  content += delta.content;
112
157
  onDelta(delta.content);
@@ -100,7 +100,7 @@ export declare class CoreSession implements EngineSession {
100
100
  private finish;
101
101
  private chatTools;
102
102
  private runTurns;
103
- /** `message.delta` in pieces, not one event per token. */
103
+ /** `message.delta` (or `reasoning.delta`) in pieces, not one event per token. `max` stops a runaway stream. */
104
104
  private deltaWriter;
105
105
  /**
106
106
  * Keep the conversation inside the model's window: prune old tool output
@@ -11,6 +11,8 @@ const STATE_VERSION = 2;
11
11
  /** Deltas are sent in pieces of about this size or this often, not per token. */
12
12
  const DELTA_FLUSH_CHARS = 200;
13
13
  const DELTA_FLUSH_MS = 150;
14
+ /** Thinking a turn may put on the event stream; the rest is dropped (the answer is not). */
15
+ const REASONING_MAX_CHARS = 20_000;
14
16
  const DEFAULT_MODEL = 'anthropic/claude-sonnet-4.5';
15
17
  const MAX_OUTPUT_IN_EVENT = 4_000;
16
18
  /**
@@ -271,7 +273,8 @@ export class CoreSession {
271
273
  return;
272
274
  let result;
273
275
  const messageId = `m${this.turn + 1}`;
274
- const deltas = this.options.stream === false ? null : this.deltaWriter(messageId);
276
+ const deltas = this.options.stream === false ? null : this.deltaWriter(messageId, 'message.delta');
277
+ const thinking = this.options.stream === false ? null : this.deltaWriter(messageId, 'reasoning.delta', REASONING_MAX_CHARS);
275
278
  try {
276
279
  result = await chat({
277
280
  baseUrl: this.options.modelBaseUrl,
@@ -282,10 +285,13 @@ export class CoreSession {
282
285
  signal: this.abort.signal,
283
286
  ...(this.options.fetch ? { fetch: this.options.fetch } : {}),
284
287
  ...(deltas ? { onDelta: deltas.write } : {}),
288
+ ...(thinking ? { onReasoning: thinking.write } : {}),
285
289
  });
290
+ thinking?.flush();
286
291
  deltas?.flush();
287
292
  }
288
293
  catch (error) {
294
+ thinking?.flush();
289
295
  deltas?.flush();
290
296
  if (this.finished)
291
297
  return;
@@ -331,9 +337,10 @@ export class CoreSession {
331
337
  this.messages.push({ role: 'user', content: 'You have reached the step limit for this job. Stop and summarize what you did and what is left.' });
332
338
  }
333
339
  }
334
- /** `message.delta` in pieces, not one event per token. */
335
- deltaWriter(messageId) {
340
+ /** `message.delta` (or `reasoning.delta`) in pieces, not one event per token. `max` stops a runaway stream. */
341
+ deltaWriter(messageId, type, max = Infinity) {
336
342
  let buffer = '';
343
+ let sent = 0;
337
344
  let timer = null;
338
345
  const flush = () => {
339
346
  if (timer)
@@ -341,12 +348,17 @@ export class CoreSession {
341
348
  timer = null;
342
349
  if (!buffer)
343
350
  return;
344
- this.options.emit('message.delta', { message_id: messageId, text: buffer });
351
+ this.options.emit(type, { message_id: messageId, text: buffer });
345
352
  buffer = '';
346
353
  };
347
354
  return {
348
355
  write: (text) => {
349
- buffer += text;
356
+ if (sent >= max)
357
+ return;
358
+ const room = max - sent;
359
+ const piece = text.length > room ? text.slice(0, room) : text;
360
+ sent += piece.length;
361
+ buffer += piece;
350
362
  if (buffer.length >= DELTA_FLUSH_CHARS)
351
363
  flush();
352
364
  else
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workser/engine",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
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",