@askalf/dario 3.0.2 → 3.0.4

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.
@@ -309,6 +309,8 @@ export function buildCCRequest(clientBody, billingTag, agentIdentity, cache1h, i
309
309
  systemText = systemText.replace(pattern, '');
310
310
  }
311
311
  // ── Build the CC request from template ──
312
+ // Key order matches CC v2.1.104 MITM capture exactly:
313
+ // model, messages, system, tools, metadata, max_tokens, thinking, context_management, output_config, stream
312
314
  const ccRequest = {
313
315
  model,
314
316
  messages,
@@ -317,17 +319,12 @@ export function buildCCRequest(clientBody, billingTag, agentIdentity, cache1h, i
317
319
  { type: 'text', text: agentIdentity, cache_control: cache1h },
318
320
  { type: 'text', text: systemText || 'You are a helpful assistant.', cache_control: cache1h },
319
321
  ],
320
- max_tokens: 64000,
321
322
  };
322
- // Model-specific fields (matches CC v2.1.104 exactly)
323
- if (!isHaiku) {
324
- ccRequest.thinking = { type: 'adaptive' };
325
- ccRequest.output_config = { effort: 'medium' };
326
- ccRequest.context_management = { edits: [{ type: 'clear_thinking_20251015', keep: 'all' }] };
327
- // CC sends temperature:1 explicitly when not in thinking-only mode
328
- ccRequest.temperature = 1;
323
+ // Tools come before metadata in CC's key order
324
+ if (clientTools && clientTools.length > 0) {
325
+ ccRequest.tools = CC_TOOL_DEFINITIONS;
329
326
  }
330
- // Always include metadata
327
+ // Metadata
331
328
  ccRequest.metadata = {
332
329
  user_id: JSON.stringify({
333
330
  device_id: identity.deviceId,
@@ -335,11 +332,14 @@ export function buildCCRequest(clientBody, billingTag, agentIdentity, cache1h, i
335
332
  session_id: identity.sessionId,
336
333
  }),
337
334
  };
338
- ccRequest.stream = stream;
339
- // Use CC's exact tool definitions not the client's
340
- if (clientTools && clientTools.length > 0) {
341
- ccRequest.tools = CC_TOOL_DEFINITIONS;
335
+ ccRequest.max_tokens = 64000;
336
+ // Model-specific fields order: thinking, context_management, output_config
337
+ if (!isHaiku) {
338
+ ccRequest.thinking = { type: 'adaptive' };
339
+ ccRequest.context_management = { edits: [{ type: 'clear_thinking_20251015', keep: 'all' }] };
340
+ ccRequest.output_config = { effort: 'medium' };
342
341
  }
342
+ ccRequest.stream = stream;
343
343
  return { body: ccRequest, toolMap: activeToolMap, unmappedTools };
344
344
  }
345
345
  /**
package/dist/oauth.js CHANGED
@@ -15,6 +15,9 @@ const OAUTH_TOKEN_URL = 'https://platform.claude.com/v1/oauth/token';
15
15
  const OAUTH_SCOPES = 'org:create_api_key user:profile user:inference user:sessions:claude_code user:mcp_servers user:file_upload';
16
16
  // Refresh 30 min before expiry
17
17
  const REFRESH_BUFFER_MS = 30 * 60 * 1000;
18
+ // After a failed refresh, don't retry for 60s to avoid spam
19
+ let lastRefreshFailure = 0;
20
+ const REFRESH_COOLDOWN_MS = 60 * 1000;
18
21
  // In-memory credential cache — avoids disk reads on every request
19
22
  let credentialsCache = null;
20
23
  let credentialsCacheTime = 0;
@@ -210,6 +213,8 @@ async function doRefreshTokens() {
210
213
  signal: AbortSignal.timeout(15000),
211
214
  });
212
215
  if (!res.ok) {
216
+ const errBody = await res.text().catch(() => '');
217
+ console.error(`[dario] Refresh attempt ${attempt + 1}/3 failed: HTTP ${res.status} — ${errBody.slice(0, 200)}`);
213
218
  if (res.status === 401 || res.status === 403) {
214
219
  throw new Error(`Refresh token rejected (${res.status}). Run \`dario login\` to re-authenticate.`);
215
220
  }
@@ -240,10 +245,22 @@ export async function getAccessToken() {
240
245
  if (oauth.expiresAt > Date.now() + REFRESH_BUFFER_MS) {
241
246
  return oauth.accessToken;
242
247
  }
243
- // Need refresh
248
+ // Need refresh — but don't spam if we just failed
249
+ if (Date.now() - lastRefreshFailure < REFRESH_COOLDOWN_MS) {
250
+ // Still in cooldown from a recent failure, use current token even if expiring
251
+ return oauth.accessToken;
252
+ }
244
253
  console.log('[dario] Token expiring soon, refreshing...');
245
- const refreshed = await refreshTokens();
246
- return refreshed.accessToken;
254
+ try {
255
+ const refreshed = await refreshTokens();
256
+ return refreshed.accessToken;
257
+ }
258
+ catch (err) {
259
+ lastRefreshFailure = Date.now();
260
+ console.error(`[dario] Refresh failed: ${err instanceof Error ? err.message : err}. Will retry in 60s. Run \`dario login\` if this persists.`);
261
+ // Return current token — it might still work for a few more minutes
262
+ return oauth.accessToken;
263
+ }
247
264
  }
248
265
  /**
249
266
  * Get token status info.
package/dist/proxy.js CHANGED
@@ -725,8 +725,10 @@ export async function startProxy(opts = {}) {
725
725
  }
726
726
  else {
727
727
  // Claude-optimized: full beta set matching real Claude Code (exact order from MITM capture)
728
- // Beta set from CC v2.1.104 binary RE some are CC-internal/gated, only include publicly accepted ones
729
- beta = 'claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advisor-tool-2026-03-01,effort-2025-11-24,fast-mode-2026-02-01,redact-thinking-2026-02-12,context-1m-2025-08-07,web-search-2025-03-05,advanced-tool-use-2025-11-20,tool-search-tool-2025-10-19';
728
+ // Exact beta set from CC v2.1.104 MITM capture (exact order)
729
+ // Only 8 betas — CC sends more conditionally (fast-mode, web-search, etc.)
730
+ // but the base set for a standard request is exactly this
731
+ beta = 'claude-code-20250219,oauth-2025-04-20,context-1m-2025-08-07,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advisor-tool-2026-03-01,effort-2025-11-24';
730
732
  if (clientBeta) {
731
733
  const baseSet = new Set(beta.split(','));
732
734
  const filtered = filterBillableBetas(clientBeta)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@askalf/dario",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
4
4
  "description": "Use your Claude subscription as an API. No API key needed. Local proxy for Claude Max/Pro subscriptions.",
5
5
  "type": "module",
6
6
  "bin": {