diff options
| author | Adam Malczewski <[email protected]> | 2026-06-02 15:03:54 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-02 15:03:54 +0900 |
| commit | 9d6b7a97e8e96429815503718e1437fae41bf5d5 (patch) | |
| tree | ba3b3a95454a6d150e34b595d92d39acacb8ad6a /packages/core/src | |
| parent | ecb001ec7a2e573d8dedf5064e860e5a3e7788fd (diff) | |
| parent | 40b0b6a23a5cbd494f9956315c2e424d16edb282 (diff) | |
| download | dispatch-9d6b7a97e8e96429815503718e1437fae41bf5d5.tar.gz dispatch-9d6b7a97e8e96429815503718e1437fae41bf5d5.zip | |
Merge branch 'dev' into td/todo-fix
Diffstat (limited to 'packages/core/src')
| -rw-r--r-- | packages/core/src/credentials/claude.ts | 38 | ||||
| -rw-r--r-- | packages/core/src/credentials/index.ts | 1 | ||||
| -rw-r--r-- | packages/core/src/db/tabs.ts | 14 | ||||
| -rw-r--r-- | packages/core/src/index.ts | 1 |
4 files changed, 54 insertions, 0 deletions
diff --git a/packages/core/src/credentials/claude.ts b/packages/core/src/credentials/claude.ts index 168d544..432e403 100644 --- a/packages/core/src/credentials/claude.ts +++ b/packages/core/src/credentials/claude.ts @@ -373,6 +373,44 @@ export function buildBillingHeaderValue( export const SYSTEM_IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude."; +/** + * Build the request body for a Claude "wake" probe — a tiny, cheap message + * whose only purpose is to keep the subscription's rate-limit window warm. + * + * This MUST mirror the shape of a genuine Claude Code CLI request, because + * Anthropic validates the `system[]` array on OAuth (Pro/Max) -authenticated, + * Claude-Code-billed requests. A bare `{ model, messages }` body (no system + * identity) is rejected (401/403) — which is exactly how the old probe silently + * failed. The valid shape is: + * + * system: [ + * { type: "text", text: "x-anthropic-billing-header: ..." }, // billing, no cache_control + * { type: "text", text: "You are Claude Code, Anthropic's official CLI for Claude." }, + * ] + * messages: [ { role: "user", content: "hi" } ] + * + * Mirrors the runtime `transformClaudeOAuthBody` output for a single short user + * turn. Pure: deterministic given its inputs (the billing header samples only + * the user text), so it can be unit-tested without touching the network. + */ +export function buildWakeProbeBody(model: string): { + model: string; + max_tokens: number; + system: Array<{ type: "text"; text: string }>; + messages: Array<{ role: "user"; content: string }>; +} { + const messages = [{ role: "user" as const, content: "hi" }]; + return { + model, + max_tokens: 16, + system: [ + { type: "text", text: buildBillingHeaderValue(messages) }, + { type: "text", text: SYSTEM_IDENTITY }, + ], + messages, + }; +} + // ─── Anthropic Request Headers ──────────────────────────────── export function getAnthropicHeaders(accessToken: string): Record<string, string> { diff --git a/packages/core/src/credentials/index.ts b/packages/core/src/credentials/index.ts index ff7392b..46fa5b6 100644 --- a/packages/core/src/credentials/index.ts +++ b/packages/core/src/credentials/index.ts @@ -9,6 +9,7 @@ export { export { ANTHROPIC_MODELS_FALLBACK, buildBillingHeaderValue, + buildWakeProbeBody, type ClaudeAccount, type ClaudeCredentials, type ClaudeProfile, diff --git a/packages/core/src/db/tabs.ts b/packages/core/src/db/tabs.ts index 8b290d2..f719a01 100644 --- a/packages/core/src/db/tabs.ts +++ b/packages/core/src/db/tabs.ts @@ -115,6 +115,20 @@ export function updateTabStatus(id: string, status: string): void { }); } +export function updateTabPositions(idsInOrder: string[]): void { + const db = getDatabase(); + const now = Date.now(); + const update = db.query("UPDATE tabs SET position = $position, updated_at = $now WHERE id = $id"); + // One transaction so a reorder is atomic: either every tab lands at its new + // slot or none does, never a half-applied ordering. + const applyAll = db.transaction(() => { + idsInOrder.forEach((id, index) => { + update.run({ $id: id, $position: index, $now: now }); + }); + }); + applyAll(); +} + export function archiveTab(id: string): void { const db = getDatabase(); db.query("UPDATE tabs SET is_open = 0, updated_at = $now WHERE id = $id").run({ diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index a7b1cad..8334102 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -56,6 +56,7 @@ export { shortestUniquePrefix, type TabRow, updateTabModel, + updateTabPositions, updateTabStatus, updateTabTitle, } from "./db/tabs.js"; |
