@askalf/dario 3.19.2 → 3.19.3

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/README.md CHANGED
@@ -316,12 +316,13 @@ The OpenAI-compat backend forwards tool definitions byte-for-byte and doesn't ne
316
316
  | Flag / env | Description | Default |
317
317
  |---|---|---|
318
318
  | `--passthrough` / `--thin` | Thin proxy for the Claude backend — OAuth swap only, no template injection | off |
319
- | `--preserve-tools` / `--keep-tools` | Keep client tool schemas instead of remapping to CC's. Required for clients whose tools have fields CC doesn't — see [Custom tool schemas](#custom-tool-schemas). | off |
320
- | `--hybrid-tools` / `--context-inject` | Remap to CC tools **and** inject request-context values (`sessionId`, `requestId`, `channelId`, `userId`, `timestamp`) into client-declared fields CC's schema doesn't carry. See [Hybrid tool mode](#hybrid-tool-mode). | off |
319
+ | `--preserve-tools` / `--keep-tools` | Keep client tool schemas instead of remapping to CC's. Required for clients whose tools have fields CC doesn't — see [Custom tool schemas](#custom-tool-schemas). Auto-enabled for Cline / Kilo Code / Roo Code and forks (dario#40 — detected via system-prompt fingerprint). | off (auto for text-tool clients) |
320
+ | `--hybrid-tools` / `--context-inject` | Remap to CC tools **and** inject request-context values (`sessionId`, `requestId`, `channelId`, `userId`, `timestamp`) into client-declared fields CC's schema doesn't carry. See [Hybrid tool mode](#hybrid-tool-mode). Overrides the text-tool auto-detect. | off |
321
321
  | `--model=<name>` | Force a model. Shortcuts (`opus`, `sonnet`, `haiku`), full IDs (`claude-opus-4-7`), or a **provider prefix** (`openai:gpt-4o`, `groq:llama-3.3-70b`, `claude:opus`, `local:qwen-coder`) to force the backend server-wide. See [Provider prefix](#provider-prefix). | passthrough |
322
322
  | `--port=<n>` | Port to listen on | `3456` |
323
323
  | `--host=<addr>` / `DARIO_HOST` | Bind address. Use `0.0.0.0` for LAN, or a specific IP (e.g. a Tailscale interface). When non-loopback, also set `DARIO_API_KEY`. | `127.0.0.1` |
324
- | `--verbose` / `-v` | Log every request | off |
324
+ | `--verbose` / `-v` | Log every request (one line per request — method + path + billing bucket) | off |
325
+ | `--verbose=2` / `-vv` / `DARIO_LOG_BODIES=1` | Also dump the outbound request body (redacted: bearer tokens, `sk-ant-*` keys, JWTs stripped; capped at 8KB). For wire-level client-compat debugging (dario#40). | off |
325
326
  | `DARIO_API_KEY` | If set, all endpoints (except `/health`) require a matching `x-api-key` or `Authorization: Bearer` header. Required when `--host` binds non-loopback. | unset (open) |
326
327
  | `DARIO_CORS_ORIGIN` | Override browser CORS origin | `http://localhost:${port}` |
327
328
  | `DARIO_NO_BUN` | Disable automatic Bun relaunch | unset |
@@ -49,6 +49,43 @@ export declare const CC_AGENT_IDENTITY: string;
49
49
  */
50
50
  export declare function orderHeadersForOutbound(headers: Record<string, string>, overrideHeaderOrder?: string[] | undefined): Record<string, string> | Array<[string, string]>;
51
51
  export declare function scrubFrameworkIdentifiers(text: string): string;
52
+ /**
53
+ * Detect text-tool-protocol clients (Cline, Kilo Code, Roo Code and
54
+ * their forks) by fingerprinting the incoming system prompt.
55
+ *
56
+ * These clients ship their own XML-style tool invocation protocol in
57
+ * the system prompt (`<execute_command>`, `<replace_in_file>`,
58
+ * `<attempt_completion>`, …) and parse the model's output with a
59
+ * regex tuned to that exact shape. When dario's default mode
60
+ * substitutes CC's canonical tools into the `tools` array, the model
61
+ * correctly emits Anthropic's generic `<function_calls><invoke>`
62
+ * wrapper — which is well-formed for a CC-tool request but
63
+ * unparseable for a text-protocol client, so every edit surfaces as
64
+ * an error in the client UI even though the model produced a valid
65
+ * response (dario#40, reported by @ringge).
66
+ *
67
+ * The fix is preserve-tools behavior: skip the CC tool swap so the
68
+ * model sees the client's own schema and emits its native XML shape.
69
+ * Auto-detection saves users from having to discover the
70
+ * `--preserve-tools` flag exists; the flag is still honored as an
71
+ * explicit override and `--hybrid-tools` outranks detection.
72
+ *
73
+ * Detection must run BEFORE `scrubFrameworkIdentifiers` so brand
74
+ * names like "Cline" / "Roo" are still present. Tool-protocol
75
+ * markers are scrub-proof on their own.
76
+ *
77
+ * Returns the matched family (`cline` / `kilo` / `roo` / `cline-like`)
78
+ * or null when no text-tool protocol signature is present.
79
+ */
80
+ export declare function detectTextToolClient(systemText: string): string | null;
81
+ /**
82
+ * Flatten an Anthropic-shaped `system` field (string or array of text
83
+ * blocks) to a single joined string. Skips the billing-tag block so
84
+ * captured billing metadata isn't conflated with the operator's own
85
+ * prompt. Used both by the main request-build path (post-scrub) and
86
+ * by the early text-tool-client detector (pre-scrub).
87
+ */
88
+ export declare function extractSystemText(clientBody: Record<string, unknown>): string;
52
89
  /**
53
90
  * Client tool name → CC tool mapping with parameter translation.
54
91
  *
@@ -117,6 +154,7 @@ export declare function buildCCRequest(clientBody: Record<string, unknown>, bill
117
154
  body: Record<string, unknown>;
118
155
  toolMap: Map<string, ToolMapping>;
119
156
  unmappedTools: string[];
157
+ detectedClient?: string;
120
158
  };
121
159
  /**
122
160
  * Reverse-map CC tool calls in a non-streaming response back to the
@@ -116,6 +116,73 @@ export function scrubFrameworkIdentifiers(text) {
116
116
  }
117
117
  return result;
118
118
  }
119
+ /**
120
+ * Detect text-tool-protocol clients (Cline, Kilo Code, Roo Code and
121
+ * their forks) by fingerprinting the incoming system prompt.
122
+ *
123
+ * These clients ship their own XML-style tool invocation protocol in
124
+ * the system prompt (`<execute_command>`, `<replace_in_file>`,
125
+ * `<attempt_completion>`, …) and parse the model's output with a
126
+ * regex tuned to that exact shape. When dario's default mode
127
+ * substitutes CC's canonical tools into the `tools` array, the model
128
+ * correctly emits Anthropic's generic `<function_calls><invoke>`
129
+ * wrapper — which is well-formed for a CC-tool request but
130
+ * unparseable for a text-protocol client, so every edit surfaces as
131
+ * an error in the client UI even though the model produced a valid
132
+ * response (dario#40, reported by @ringge).
133
+ *
134
+ * The fix is preserve-tools behavior: skip the CC tool swap so the
135
+ * model sees the client's own schema and emits its native XML shape.
136
+ * Auto-detection saves users from having to discover the
137
+ * `--preserve-tools` flag exists; the flag is still honored as an
138
+ * explicit override and `--hybrid-tools` outranks detection.
139
+ *
140
+ * Detection must run BEFORE `scrubFrameworkIdentifiers` so brand
141
+ * names like "Cline" / "Roo" are still present. Tool-protocol
142
+ * markers are scrub-proof on their own.
143
+ *
144
+ * Returns the matched family (`cline` / `kilo` / `roo` / `cline-like`)
145
+ * or null when no text-tool protocol signature is present.
146
+ */
147
+ export function detectTextToolClient(systemText) {
148
+ if (!systemText)
149
+ return null;
150
+ if (/\bYou are Cline\b/.test(systemText))
151
+ return 'cline';
152
+ if (/\bYou are Kilo Code\b/.test(systemText))
153
+ return 'kilo';
154
+ if (/\bYou are Roo\b/.test(systemText))
155
+ return 'roo';
156
+ // Protocol-signature fallback — unique to the Cline family and its
157
+ // forks; survives a forked system prompt that edited the identity
158
+ // string out but kept the tool protocol intact.
159
+ if (/<attempt_completion>/.test(systemText))
160
+ return 'cline-like';
161
+ if (/<ask_followup_question>/.test(systemText))
162
+ return 'cline-like';
163
+ if (/<<<<<<< SEARCH\b/.test(systemText))
164
+ return 'cline-like';
165
+ return null;
166
+ }
167
+ /**
168
+ * Flatten an Anthropic-shaped `system` field (string or array of text
169
+ * blocks) to a single joined string. Skips the billing-tag block so
170
+ * captured billing metadata isn't conflated with the operator's own
171
+ * prompt. Used both by the main request-build path (post-scrub) and
172
+ * by the early text-tool-client detector (pre-scrub).
173
+ */
174
+ export function extractSystemText(clientBody) {
175
+ const sys = clientBody.system;
176
+ if (typeof sys === 'string')
177
+ return sys;
178
+ if (Array.isArray(sys)) {
179
+ return sys
180
+ .filter(b => b.text && !b.text.includes('x-anthropic-billing-header:'))
181
+ .map(b => b.text)
182
+ .join('\n\n');
183
+ }
184
+ return '';
185
+ }
119
186
  /**
120
187
  * Map from client-declared field name (lowercase) to the RequestContext
121
188
  * key that supplies its value. A field declared on the client's tool
@@ -580,6 +647,16 @@ export function buildCCRequest(clientBody, billingTag, cache1h, identity, opts =
580
647
  const messages = clientBody.messages || [];
581
648
  const clientTools = clientBody.tools;
582
649
  const stream = clientBody.stream ?? false;
650
+ // ── Detect text-tool-protocol clients up-front ──
651
+ // Cline / Kilo Code / Roo Code (and forks) ship an XML tool-invocation
652
+ // protocol in the system prompt. Peek at it before scrubbing so the
653
+ // brand name is still present, decide whether to auto-switch into
654
+ // preserve-tools behavior below. Explicit --hybrid-tools outranks the
655
+ // heuristic (operator opt-in wins). dario#40.
656
+ const rawSystemForDetection = extractSystemText(clientBody);
657
+ const detectedClient = detectTextToolClient(rawSystemForDetection) ?? undefined;
658
+ const autoPreserve = Boolean(detectedClient) && !opts.hybridTools;
659
+ const effectivePreserveTools = Boolean(opts.preserveTools) || autoPreserve;
583
660
  // ── Strip thinking from history ──
584
661
  for (const msg of messages) {
585
662
  if (msg.role === 'assistant' && Array.isArray(msg.content)) {
@@ -622,7 +699,7 @@ export function buildCCRequest(clientBody, billingTag, cache1h, identity, opts =
622
699
  // the fingerprint risk on their own account.
623
700
  const activeToolMap = new Map();
624
701
  const unmappedTools = [];
625
- if (clientTools && !opts.preserveTools) {
702
+ if (clientTools && !effectivePreserveTools) {
626
703
  // Two passes so the unmapped-tool distributor can avoid colliding with
627
704
  // CC tools the client already uses directly. Without this, a client
628
705
  // sending both `WebSearch` and some unmapped tool like `memory_get`
@@ -705,7 +782,7 @@ export function buildCCRequest(clientBody, billingTag, cache1h, identity, opts =
705
782
  }
706
783
  // ── Remap tool_use and tool_result references in message history ──
707
784
  // Skip in preserveTools mode — leave conversation history untouched.
708
- if (!opts.preserveTools) {
785
+ if (!effectivePreserveTools) {
709
786
  for (const msg of messages) {
710
787
  if (Array.isArray(msg.content)) {
711
788
  for (const block of msg.content) {
@@ -754,18 +831,11 @@ export function buildCCRequest(clientBody, billingTag, cache1h, identity, opts =
754
831
  }
755
832
  }
756
833
  // ── Merge system prompt ──
757
- let systemText = '';
758
- const sys = clientBody.system;
759
- if (typeof sys === 'string') {
760
- systemText = sys;
761
- }
762
- else if (Array.isArray(sys)) {
763
- systemText = sys
764
- .filter(b => b.text && !b.text.includes('x-anthropic-billing-header:'))
765
- .map(b => b.text)
766
- .join('\n\n');
767
- }
768
- systemText = scrubFrameworkIdentifiers(systemText);
834
+ // rawSystemForDetection holds the same text already used by the
835
+ // up-front detector above — reuse it here so we don't reparse the
836
+ // system array a second time per request. Scrub applies at this
837
+ // point so framework identifiers don't leak upstream.
838
+ let systemText = scrubFrameworkIdentifiers(rawSystemForDetection);
769
839
  // Also scrub framework identifiers from message content text blocks.
770
840
  // Clients often inject their product name into user/tool messages as well,
771
841
  // and the system-prompt-only scrub used to miss those.
@@ -815,7 +885,7 @@ export function buildCCRequest(clientBody, billingTag, cache1h, identity, opts =
815
885
  // preserveTools mode: pass client tools through unchanged (better for real
816
886
  // agents with custom schemas, but loses the CC tool fingerprint).
817
887
  if (clientTools && clientTools.length > 0) {
818
- ccRequest.tools = opts.preserveTools ? clientTools : CC_TOOL_DEFINITIONS;
888
+ ccRequest.tools = effectivePreserveTools ? clientTools : CC_TOOL_DEFINITIONS;
819
889
  }
820
890
  // Metadata
821
891
  ccRequest.metadata = {
@@ -833,7 +903,7 @@ export function buildCCRequest(clientBody, billingTag, cache1h, identity, opts =
833
903
  ccRequest.output_config = { effort: 'medium' };
834
904
  }
835
905
  ccRequest.stream = stream;
836
- return { body: ccRequest, toolMap: activeToolMap, unmappedTools };
906
+ return { body: ccRequest, toolMap: activeToolMap, unmappedTools, detectedClient };
837
907
  }
838
908
  /**
839
909
  * Build the CC-name → {clientName, mapping} reverse lookup used by both
package/dist/cli.js CHANGED
@@ -137,7 +137,14 @@ async function proxy() {
137
137
  console.error('[dario] Invalid --host. Must be an IP address or hostname.');
138
138
  process.exit(1);
139
139
  }
140
- const verbose = args.includes('--verbose') || args.includes('-v');
140
+ // --verbose=2 / -vv / DARIO_LOG_BODIES=1 emit redacted request bodies
141
+ // on every POST. -v alone is unchanged (one-line per-request summary).
142
+ // dario#40 (ringge asked for a body-dump mode when debugging client
143
+ // compatibility without having to attach a MITM).
144
+ const verboseBodies = args.includes('-vv')
145
+ || args.includes('--verbose=2')
146
+ || process.env.DARIO_LOG_BODIES === '1';
147
+ const verbose = verboseBodies || args.includes('--verbose') || args.includes('-v');
141
148
  const passthrough = args.includes('--passthrough') || args.includes('--thin');
142
149
  const preserveTools = args.includes('--preserve-tools') || args.includes('--keep-tools');
143
150
  const hybridTools = args.includes('--hybrid-tools') || args.includes('--context-inject');
@@ -147,7 +154,7 @@ async function proxy() {
147
154
  }
148
155
  const modelArg = args.find(a => a.startsWith('--model='));
149
156
  const model = modelArg ? modelArg.split('=')[1] : undefined;
150
- await startProxy({ port, host, verbose, model, passthrough, preserveTools, hybridTools });
157
+ await startProxy({ port, host, verbose, verboseBodies, model, passthrough, preserveTools, hybridTools });
151
158
  }
152
159
  async function accounts() {
153
160
  const sub = args[1];
@@ -387,6 +394,8 @@ async function help() {
387
394
  --host=ADDRESS Address to bind to (default: 127.0.0.1)
388
395
  Use 0.0.0.0 for LAN; see README for DARIO_API_KEY
389
396
  --verbose, -v Log all requests
397
+ --verbose=2, -vv Also dump redacted request bodies
398
+ (env: DARIO_LOG_BODIES=1)
390
399
 
391
400
  Quick start:
392
401
  dario login # auto-detects Claude Code credentials
package/dist/proxy.d.ts CHANGED
@@ -6,6 +6,7 @@ interface ProxyOptions {
6
6
  port?: number;
7
7
  host?: string;
8
8
  verbose?: boolean;
9
+ verboseBodies?: boolean;
9
10
  model?: string;
10
11
  passthrough?: boolean;
11
12
  preserveTools?: boolean;
package/dist/proxy.js CHANGED
@@ -363,6 +363,17 @@ export async function startProxy(opts = {}) {
363
363
  const host = opts.host ?? process.env.DARIO_HOST ?? DEFAULT_HOST;
364
364
  const verbose = opts.verbose ?? false;
365
365
  const passthrough = opts.passthrough ?? false;
366
+ // Text-tool-protocol client families that have already logged a
367
+ // "detected → auto-enabling preserve-tools" banner this session.
368
+ // Set once on first sighting per family so the startup log stays
369
+ // short even under heavy traffic. dario#40.
370
+ const detectedClientsLogged = new Set();
371
+ // Body-dump mode: set via --verbose=2 / -vv or DARIO_LOG_BODIES=1.
372
+ // When on, every request emits a redacted JSON body to stderr so
373
+ // operators can see exactly what dario forwards upstream. Default
374
+ // -v stays quiet because bodies can carry file content and tool
375
+ // output. Reported in dario#40 by @ringge.
376
+ const verboseBodies = Boolean(opts.verboseBodies) || process.env.DARIO_LOG_BODIES === '1';
366
377
  // Multi-provider backends (v3.6.0+). Loaded once at startup; the CLI
367
378
  // `dario backend add openai --key=…` writes to ~/.dario/backends/.
368
379
  // Routing: a GPT-family model arriving on /v1/chat/completions is
@@ -968,10 +979,22 @@ export async function startProxy(opts = {}) {
968
979
  const bodyIdentity = poolAccount
969
980
  ? poolAccount.identity
970
981
  : { deviceId: identity.deviceId, accountUuid: identity.accountUuid, sessionId: SESSION_ID };
971
- const { body: ccBody, toolMap } = buildCCRequest(r, billingTag, CACHE_1H, bodyIdentity, {
982
+ const { body: ccBody, toolMap, detectedClient } = buildCCRequest(r, billingTag, CACHE_1H, bodyIdentity, {
972
983
  preserveTools: opts.preserveTools ?? false,
973
984
  hybridTools: opts.hybridTools ?? false,
974
985
  });
986
+ // Log the auto-preserve-tools switch once per text-tool
987
+ // client family. Skip when the operator already opted into
988
+ // --preserve-tools or --hybrid-tools — they know what they
989
+ // picked and don't need a "hey, we heuristically agree"
990
+ // line on every new client seen. dario#40.
991
+ if (detectedClient
992
+ && !opts.preserveTools
993
+ && !opts.hybridTools
994
+ && !detectedClientsLogged.has(detectedClient)) {
995
+ detectedClientsLogged.add(detectedClient);
996
+ console.log(`[dario] detected ${detectedClient}-style text-tool protocol — auto-enabling preserve-tools for this client (pass --hybrid-tools to override, --preserve-tools to silence)`);
997
+ }
975
998
  // Store tool map for response reverse-mapping
976
999
  ccToolMap = toolMap;
977
1000
  // Replace request body entirely with CC template
@@ -987,6 +1010,20 @@ export async function startProxy(opts = {}) {
987
1010
  const modelInfo = modelOverride ? ` (model: ${modelOverride})` : '';
988
1011
  console.log(`[dario] #${requestCount} ${req.method} ${urlPath}${modelInfo}`);
989
1012
  }
1013
+ // Body dump — -vv / DARIO_LOG_BODIES=1. Runs on the outbound
1014
+ // body after the template build so operators see what actually
1015
+ // lands on the wire. sanitizeError's redaction strips bearer
1016
+ // tokens, sk-ant-* keys, and JWT triples in case any leaked
1017
+ // into the body (e.g. user pasted a curl). 8KB cap because the
1018
+ // CC system prompt alone is 25KB and dumping it every request
1019
+ // buries the useful content. dario#40.
1020
+ if (verboseBodies && finalBody) {
1021
+ const rendered = finalBody.toString('utf8');
1022
+ const capped = rendered.length > 8192
1023
+ ? rendered.slice(0, 8192) + `\n[...truncated ${rendered.length - 8192} bytes]`
1024
+ : rendered;
1025
+ console.log(`[dario] #${requestCount} request body:\n${sanitizeError(capped)}`);
1026
+ }
990
1027
  // Beta headers
991
1028
  const clientBeta = req.headers['anthropic-beta'];
992
1029
  let beta;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@askalf/dario",
3
- "version": "3.19.2",
3
+ "version": "3.19.3",
4
4
  "description": "A local LLM router. One endpoint, every provider — Claude subscriptions, OpenAI, OpenRouter, Groq, local LiteLLM, any OpenAI-compat endpoint — your tools don't need to change.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "scripts": {
23
23
  "build": "tsc && cp src/cc-template-data.json dist/ && node -e \"require('fs').mkdirSync('dist/shim',{recursive:true})\" && cp src/shim/runtime.cjs dist/shim/",
24
- "test": "node test/issue-29-tool-translation.mjs && node test/hybrid-tools.mjs && node test/tool-schema-contract.mjs && node test/scrub-paths.mjs && node test/provider-prefix.mjs && node test/analytics-recording.mjs && node test/analytics-billing-bucket.mjs && node test/failover-429.mjs && node test/pool-sticky.mjs && node test/sealed-pool.mjs && node test/live-fingerprint.mjs && node test/shim-runtime.mjs && node test/shim-e2e.mjs && node test/proxy-header-order.mjs && node test/drift-detection.mjs && node test/compat-range.mjs && node test/doctor-formatter.mjs && node test/atomic-write.mjs && node test/account-refresh-singleflight.mjs && node test/streaming-edge-cases.mjs",
24
+ "test": "node test/issue-29-tool-translation.mjs && node test/hybrid-tools.mjs && node test/tool-schema-contract.mjs && node test/scrub-paths.mjs && node test/provider-prefix.mjs && node test/analytics-recording.mjs && node test/analytics-billing-bucket.mjs && node test/failover-429.mjs && node test/pool-sticky.mjs && node test/sealed-pool.mjs && node test/live-fingerprint.mjs && node test/shim-runtime.mjs && node test/shim-e2e.mjs && node test/proxy-header-order.mjs && node test/drift-detection.mjs && node test/compat-range.mjs && node test/doctor-formatter.mjs && node test/atomic-write.mjs && node test/account-refresh-singleflight.mjs && node test/streaming-edge-cases.mjs && node test/client-detection.mjs",
25
25
  "audit": "npm audit --production --audit-level=high",
26
26
  "prepublishOnly": "npm run build",
27
27
  "start": "node dist/cli.js",