@workser/engine 0.1.6 → 0.1.9

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.
@@ -53,4 +53,10 @@ export declare function chat(input: {
53
53
  * with plain JSON instead of a stream is read as before.
54
54
  */
55
55
  onDelta?: (text: string) => void;
56
+ /**
57
+ * The model's own thinking, when its route streams it (`reasoning` or
58
+ * `reasoning_content` beside `content`). Never part of the message; a route
59
+ * that sends none never calls this.
60
+ */
61
+ onReasoning?: (text: string) => void;
56
62
  }): Promise<ChatResult>;
package/dist/src/model.js CHANGED
@@ -44,7 +44,7 @@ export async function chat(input) {
44
44
  if (res.ok && stream && type.includes('text/event-stream') && res.body) {
45
45
  // Once text has reached the owner, a retry would say it twice: a
46
46
  // stream that breaks midway is an error, not a retry.
47
- return readStream(res.body, input.onDelta, res.status);
47
+ return readStream(res.body, input.onDelta, res.status, input.onReasoning);
48
48
  }
49
49
  const text = await res.text();
50
50
  let body = null;
@@ -60,6 +60,9 @@ export async function chat(input) {
60
60
  const choice = body?.choices?.[0];
61
61
  if (!choice?.message)
62
62
  throw new ModelError('The model answered with no message.', res.status);
63
+ const thought = reasoningOf(choice.message);
64
+ if (thought && input.onReasoning)
65
+ input.onReasoning(thought);
63
66
  if (choice.message.content && input.onDelta)
64
67
  input.onDelta(choice.message.content);
65
68
  return {
@@ -76,8 +79,16 @@ export async function chat(input) {
76
79
  }
77
80
  throw lastError instanceof Error ? lastError : new ModelError('The model route could not be reached.', 0);
78
81
  }
82
+ /** The thinking a message or delta carries: OpenRouter-style `reasoning`, or DeepSeek-style `reasoning_content`. */
83
+ function reasoningOf(part) {
84
+ if (typeof part?.reasoning === 'string' && part.reasoning)
85
+ return part.reasoning;
86
+ if (typeof part?.reasoning_content === 'string' && part.reasoning_content)
87
+ return part.reasoning_content;
88
+ return '';
89
+ }
79
90
  /** An OpenAI-compatible SSE stream, put back together into one message. */
80
- async function readStream(body, onDelta, status) {
91
+ async function readStream(body, onDelta, status, onReasoning) {
81
92
  const reader = body.getReader();
82
93
  const decoder = new TextDecoder();
83
94
  let buffer = '';
@@ -107,6 +118,9 @@ async function readStream(body, onDelta, status) {
107
118
  if (choice.finish_reason)
108
119
  finishReason = choice.finish_reason;
109
120
  const delta = choice.delta ?? {};
121
+ const thought = reasoningOf(delta);
122
+ if (thought && onReasoning)
123
+ onReasoning(thought);
110
124
  if (typeof delta.content === 'string' && delta.content) {
111
125
  content += delta.content;
112
126
  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.6",
3
+ "version": "0.1.9",
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",