@workser/engine 0.1.16 → 0.1.18

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.
@@ -0,0 +1,12 @@
1
+ /** A whole message with its markup tags taken out (untouched, not even trimmed, when it has none). */
2
+ export declare function stripDsml(text: string): string;
3
+ /**
4
+ * The same for a reply that arrives in pieces: a tag can be cut anywhere (`<`, `|DSML|`, ` calls`, `>`), so what might
5
+ * still become a tag is held back until the next piece says. Everything else passes at once.
6
+ */
7
+ export declare class DsmlFilter {
8
+ private held;
9
+ push(chunk: string): string;
10
+ /** The stream ended: an unfinished tag is dropped, a lone `<` (or the start of one that never came) is text. */
11
+ flush(): string;
12
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * DEEPSEEK'S OWN TOOL-CALL MARKUP, KEPT OUT OF THE OWNER'S CHAT (trace 01a1d4cc, 13 Nov: the owner's chat showed grey
3
+ * chips reading "< | DSML | calls>"). DeepSeek writes its tool calls with `<|DSML|function_calls>` style tags and now and
4
+ * then lets a bare tag leak into the reply text, in 4 of 57 calls of one job; the structured tool calls still arrived and
5
+ * ran, so only the visible text was wrong. A tag is `<` or `</`, then `|DSML|` (full-width bars), then a name, then `>`.
6
+ */
7
+ const OPEN = '<|DSML|';
8
+ const CLOSE = '</|DSML|';
9
+ const TAG = /<\/?|DSML|[^>\n]*>/g;
10
+ /** A whole message with its markup tags taken out (untouched, not even trimmed, when it has none). */
11
+ export function stripDsml(text) {
12
+ if (!text.includes('|DSML|'))
13
+ return text;
14
+ const out = text.replace(TAG, '');
15
+ return out === text ? text : out.replace(/\n{3,}/g, '\n\n').trim();
16
+ }
17
+ function mayBecomeTag(tail) {
18
+ if (tail.includes('>') || tail.includes('\n'))
19
+ return false;
20
+ return OPEN.startsWith(tail) || CLOSE.startsWith(tail) || tail.startsWith(OPEN) || tail.startsWith(CLOSE);
21
+ }
22
+ /**
23
+ * The same for a reply that arrives in pieces: a tag can be cut anywhere (`<`, `|DSML|`, ` calls`, `>`), so what might
24
+ * still become a tag is held back until the next piece says. Everything else passes at once.
25
+ */
26
+ export class DsmlFilter {
27
+ held = '';
28
+ push(chunk) {
29
+ let text = (this.held + chunk).replace(TAG, '');
30
+ this.held = '';
31
+ const at = text.lastIndexOf('<');
32
+ if (at >= 0 && mayBecomeTag(text.slice(at))) {
33
+ this.held = text.slice(at);
34
+ text = text.slice(0, at);
35
+ }
36
+ return text;
37
+ }
38
+ /** The stream ended: an unfinished tag is dropped, a lone `<` (or the start of one that never came) is text. */
39
+ flush() {
40
+ const rest = this.held;
41
+ this.held = '';
42
+ return rest.startsWith(OPEN) || rest.startsWith(CLOSE) ? '' : rest;
43
+ }
44
+ }
@@ -13,3 +13,4 @@ export { workserTool, type ToolsClient } from './tools/workser-tool.js';
13
13
  export { publishFileTool, mimeOf, MAX_PUBLISH_BYTES, type FileClient } from './tools/publish.js';
14
14
  export { withCommandEnv } from './place/env.js';
15
15
  export { repairArgs, parseLoose, TOOL_ALIASES, type Repaired } from './tools/repair.js';
16
+ export { DsmlFilter, stripDsml } from './dsml.js';
package/dist/src/index.js CHANGED
@@ -13,3 +13,4 @@ export { workserTool } from './tools/workser-tool.js';
13
13
  export { publishFileTool, mimeOf, MAX_PUBLISH_BYTES } from './tools/publish.js';
14
14
  export { withCommandEnv } from './place/env.js';
15
15
  export { repairArgs, parseLoose, TOOL_ALIASES } from './tools/repair.js';
16
+ export { DsmlFilter, stripDsml } from './dsml.js';
@@ -1,8 +1,3 @@
1
- /**
2
- * An OpenAI-compatible chat-completions client — Workser's gateway speaks
3
- * this, and the key is the RUN token (the gateway books every call against
4
- * the run). Nothing else: no provider key is known to the engine.
5
- */
6
1
  export interface ChatMessage {
7
2
  role: 'system' | 'user' | 'assistant' | 'tool';
8
3
  content: string | null;
@@ -29,6 +24,8 @@ export interface ChatTool {
29
24
  export interface ChatResult {
30
25
  message: ChatMessage;
31
26
  finishReason: string | null;
27
+ /** Who served the call, when the route says (OpenRouter names it on every response). Cost audit 2026-11-13. */
28
+ provider?: string;
32
29
  usage?: {
33
30
  prompt_tokens?: number;
34
31
  completion_tokens?: number;
package/dist/src/model.js CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * An OpenAI-compatible chat-completions client — Workser's gateway speaks
3
+ * this, and the key is the RUN token (the gateway books every call against
4
+ * the run). Nothing else: no provider key is known to the engine.
5
+ */
6
+ import { DsmlFilter, stripDsml } from './dsml.js';
1
7
  export class ModelError extends Error {
2
8
  status;
3
9
  constructor(message, status) {
@@ -94,17 +100,19 @@ export async function chat(input) {
94
100
  const thought = reasoningOf(choice.message);
95
101
  if (thought && input.onReasoning)
96
102
  input.onReasoning(thought);
97
- if (choice.message.content && input.onDelta)
98
- input.onDelta(choice.message.content);
103
+ const said = typeof choice.message.content === 'string' ? stripDsml(choice.message.content) : choice.message.content;
104
+ if (said && input.onDelta)
105
+ input.onDelta(said);
99
106
  return {
100
107
  message: {
101
108
  role: 'assistant',
102
- content: choice.message.content ?? null,
109
+ content: said || null,
103
110
  ...(Array.isArray(choice.message.tool_calls) && choice.message.tool_calls.length
104
111
  ? { tool_calls: choice.message.tool_calls }
105
112
  : {}),
106
113
  },
107
114
  finishReason: choice.finish_reason ?? null,
115
+ ...(typeof body.provider === 'string' && body.provider ? { provider: body.provider } : {}),
108
116
  ...(body.usage ? { usage: body.usage } : {}),
109
117
  };
110
118
  }
@@ -126,6 +134,8 @@ async function readStream(body, onDelta, status, onReasoning) {
126
134
  let content = '';
127
135
  let finishReason = null;
128
136
  let usage;
137
+ let provider = '';
138
+ const dsml = new DsmlFilter();
129
139
  const calls = [];
130
140
  let sawChoice = false;
131
141
  const handle = (data) => {
@@ -142,6 +152,8 @@ async function readStream(body, onDelta, status, onReasoning) {
142
152
  throw new ModelError(chunk.error.message ?? 'The model stream failed.', status);
143
153
  if (chunk?.usage)
144
154
  usage = chunk.usage;
155
+ if (typeof chunk?.provider === 'string' && chunk.provider)
156
+ provider = chunk.provider;
145
157
  const choice = chunk?.choices?.[0];
146
158
  if (!choice)
147
159
  return;
@@ -154,7 +166,9 @@ async function readStream(body, onDelta, status, onReasoning) {
154
166
  onReasoning(thought);
155
167
  if (typeof delta.content === 'string' && delta.content) {
156
168
  content += delta.content;
157
- onDelta(delta.content);
169
+ const shown = dsml.push(delta.content);
170
+ if (shown)
171
+ onDelta(shown);
158
172
  }
159
173
  for (const part of Array.isArray(delta.tool_calls) ? delta.tool_calls : []) {
160
174
  const index = typeof part.index === 'number' ? part.index : calls.length;
@@ -189,10 +203,15 @@ async function readStream(body, onDelta, status, onReasoning) {
189
203
  handle(buffer.trim().slice(5).trim());
190
204
  if (!sawChoice)
191
205
  throw new ModelError('The model answered with no message.', status);
206
+ const rest = dsml.flush();
207
+ if (rest)
208
+ onDelta(rest);
209
+ content = stripDsml(content);
192
210
  const toolCalls = calls.filter(Boolean).map((call, i) => ({ ...call, id: call.id || `call_${i}` }));
193
211
  return {
194
212
  message: { role: 'assistant', content: content || null, ...(toolCalls.length ? { tool_calls: toolCalls } : {}) },
195
213
  finishReason,
214
+ ...(provider ? { provider } : {}),
196
215
  ...(usage ? { usage } : {}),
197
216
  };
198
217
  }
@@ -135,4 +135,4 @@ export declare const READ_ONLY_NUDGE: string;
135
135
  * tokens read from its cache and the hidden reasoning tokens. Owner 12 Nov: a trace showed no cache reads on
136
136
  * 40–50k-token calls only because these counts were dropped here, so nobody could tell whether caching worked.
137
137
  */
138
- export declare function usageEvent(model: string, usage: NonNullable<ChatResult['usage']>): Record<string, unknown>;
138
+ export declare function usageEvent(model: string, usage: NonNullable<ChatResult['usage']>, provider?: string): Record<string, unknown>;
@@ -316,7 +316,7 @@ export class CoreSession {
316
316
  this.messages.push(result.message);
317
317
  this.lastPromptTokens = Number(result.usage?.prompt_tokens ?? 0);
318
318
  if (result.usage) {
319
- this.options.emit('usage', usageEvent(this.model, result.usage));
319
+ this.options.emit('usage', usageEvent(this.model, result.usage, result.provider));
320
320
  }
321
321
  if (result.message.content) {
322
322
  this.options.emit('message', {
@@ -415,7 +415,7 @@ export class CoreSession {
415
415
  return;
416
416
  }
417
417
  if (result.usage) {
418
- this.options.emit('usage', usageEvent(this.model, result.usage));
418
+ this.options.emit('usage', usageEvent(this.model, result.usage, result.provider));
419
419
  }
420
420
  const next = withSummary(this.messages, cut, summary);
421
421
  this.messages.splice(0, this.messages.length, ...next);
@@ -591,11 +591,14 @@ export const READ_ONLY_NUDGE = `(Workser: ${READ_ONLY_TURNS} turns in a row with
591
591
  * tokens read from its cache and the hidden reasoning tokens. Owner 12 Nov: a trace showed no cache reads on
592
592
  * 40–50k-token calls only because these counts were dropped here, so nobody could tell whether caching worked.
593
593
  */
594
- export function usageEvent(model, usage) {
594
+ export function usageEvent(model, usage, provider) {
595
595
  const cached = usage.prompt_tokens_details?.cached_tokens;
596
596
  const reasoning = usage.completion_tokens_details?.reasoning_tokens;
597
597
  return {
598
598
  model,
599
+ // Who served this call: a cache lives on one provider's machines, so a call that moved provider reads none of it
600
+ // (cost audit 2026-11-13: 10-18% of cache misses came with no change to the history).
601
+ ...(provider ? { provider } : {}),
599
602
  tokens_in: Number(usage.prompt_tokens ?? 0),
600
603
  tokens_out: Number(usage.completion_tokens ?? 0),
601
604
  ...(typeof usage.cost === 'number' ? { cost_usd: usage.cost } : {}),
@@ -14,7 +14,9 @@ export function workserTool(client) {
14
14
  name: 'workser_tool',
15
15
  description: "Use Workser's tools and the project's connected apps. action 'list' shows the tools (add query, e.g. \"db\", \"env\", \"payments\", to see only those); action 'call' runs one by name. " +
16
16
  "A tool that changes something outside this job, or spends money, needs the owner's yes first: you get approval_required " +
17
- 'and an approval_id; call again with approval_id (and spend_approval_id for a spend over the limit) after they approve.',
17
+ 'and an approval_id; call again with approval_id (and spend_approval_id for a spend over the limit) after they approve. ' +
18
+ 'Send approval fields only with an id a result gave you, never empty. Every call needs its name. Work on the preview copy (its database, its settings) runs at once with no approval; ' +
19
+ 'only the live site, live data, money and domains ask the owner first.',
18
20
  parameters: {
19
21
  type: 'object',
20
22
  properties: {
@@ -37,13 +39,16 @@ export function workserTool(client) {
37
39
  output: JSON.stringify({ ...list, tools: shown, ...(shown.length < list.tools.length ? { of: list.tools.length } : {}) }, null, 1).slice(0, 50_000),
38
40
  };
39
41
  }
40
- if (args.action !== 'call' || typeof args.name !== 'string' || !args.name)
41
- throw new Error("action is 'list' or 'call' (call needs a name).");
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
+ // name (the model reached for an approval on a preview DELETE that needed none), failed, and were sent again.
44
+ if (args.action !== 'call' || typeof args.name !== 'string' || !args.name) {
45
+ 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
+ }
42
47
  const out = await client.callTool(args.name, {
43
48
  ...(args.arguments && typeof args.arguments === 'object' ? { arguments: args.arguments } : {}),
44
- ...(typeof args.approval_id === 'string' ? { approval_id: args.approval_id } : {}),
49
+ ...(typeof args.approval_id === 'string' && args.approval_id.trim() ? { approval_id: args.approval_id } : {}),
45
50
  ...(typeof args.amount_usd === 'number' ? { amount_usd: args.amount_usd } : {}),
46
- ...(typeof args.spend_approval_id === 'string' ? { spend_approval_id: args.spend_approval_id } : {}),
51
+ ...(typeof args.spend_approval_id === 'string' && args.spend_approval_id.trim() ? { spend_approval_id: args.spend_approval_id } : {}),
47
52
  });
48
53
  return {
49
54
  title: `${args.name}: ${out.status}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workser/engine",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
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",