@hasna/switcher 0.2.23 → 0.2.26
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/Dockerfile +3 -0
- package/README.md +22 -0
- package/dist/claude-context.d.ts +12 -1
- package/dist/claude-sessions.d.ts +29 -0
- package/dist/cli/index.js +1185 -493
- package/dist/cli.d.ts +13 -2
- package/dist/deepseek-messages.d.ts +41 -0
- package/dist/gateway-client.d.ts +5 -2
- package/dist/gateway-fallback.d.ts +42 -0
- package/dist/gateway-service.d.ts +6 -0
- package/dist/index.js +176 -23
- package/dist/inference-gateway.d.ts +3 -1
- package/dist/launcher.d.ts +18 -0
- package/dist/mcp/index.js +178 -25
- package/dist/provider-error.d.ts +23 -0
- package/dist/sdk.d.ts +27 -4
- package/dist/sdk.js +176 -23
- package/dist/serve/index.js +2 -2
- package/openapi.json +1 -1
- package/package.json +1 -1
package/Dockerfile
CHANGED
|
@@ -14,7 +14,10 @@ RUN bun run generate \
|
|
|
14
14
|
&& bun run build
|
|
15
15
|
|
|
16
16
|
FROM --platform=linux/arm64 oven/bun:1.3.14-alpine AS runtime
|
|
17
|
+
# Bun.SQL auto-pipelining off: in Bun 1.3.14 it can match a query to another query's result
|
|
18
|
+
# (the Todos fix, #4043). Bun reads the flag only at process start, so it is set here.
|
|
17
19
|
ENV NODE_ENV=production \
|
|
20
|
+
BUN_FEATURE_FLAG_DISABLE_SQL_AUTO_PIPELINING=1 \
|
|
18
21
|
PORT=8080
|
|
19
22
|
WORKDIR /app
|
|
20
23
|
RUN apk upgrade --no-cache \
|
package/README.md
CHANGED
|
@@ -631,6 +631,26 @@ switcher gateway stop | uninstall
|
|
|
631
631
|
|
|
632
632
|
Provider credentials stay in gateway memory only. After a gateway restart, a credential from `switcher credentials bind` is read again; an environment-only credential is not recoverable, and its sessions get a 401 naming the fix instead of a refused connection. `SWITCHER_GATEWAY=local` selects the previous per-launch gateway. See [persistent gateway](docs/GATEWAY-SERVICE.md).
|
|
633
633
|
|
|
634
|
+
When the persistent gateway cannot be used (it could not start, or it refused the registration), the launch still runs on a per-launch gateway, as before, but prints a `switcher: WARNING:` line with the reason and port, and records it in `~/.hasna/switcher/state/per-launch-gateways/launch-XXXXXX.json` (port, harness, model, working directory, launcher PID, reason; `endedAt` once the port closes; never a token). A session left running or resumed after that launch ends cannot reach the port.
|
|
635
|
+
|
|
636
|
+
## Resume a Claude session
|
|
637
|
+
|
|
638
|
+
A Claude session that Switcher launched has no provider route of its own. A plain `claude --resume <id>` sends its stored model to Claude's default endpoint and every turn fails. Resume it through Switcher instead:
|
|
639
|
+
|
|
640
|
+
```sh
|
|
641
|
+
switcher resume <claude-session-id> # relaunch on its profile, cwd and route
|
|
642
|
+
switcher resume <claude-session-id> --check # read-only: {"owned","profile","cwd","recordPath"}
|
|
643
|
+
switcher resume <claude-session-id> --print # prepare the route, print argv and env file, start nothing
|
|
644
|
+
switcher resume <claude-session-id> --dry-run # profile, cwd and argv; nothing registered
|
|
645
|
+
switcher resume <claude-session-id> --cwd DIR -- --fork-session
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
Each Claude launch's gateway records the sessions it serves: the first request of a session (its `x-claude-code-session-id` header, or `session_id` in the request's `metadata.user_id`) writes `~/.hasna/switcher/state/sessions/claude/<profile>/claude-sessions/<session id>.json` (mode 0600, the launch's working directory and the time inside; no token or content). That covers new, resumed, forked and continued sessions and those `/clear` starts. `switcher resume` reads the newest record across profiles and runs `switcher launch <profile> --cwd <recorded cwd> -- --resume <id>`: a fresh lease on the persistent gateway, the profile's `--settings` route file and model. A session no record owns is refused (exit 3), never resumed plainly. `--state-dir`, `--executable`, `--share-native-state`/`--no-share-native-state` and `--timeout` are passed to that launch; pass the native-state option the session was launched with.
|
|
649
|
+
|
|
650
|
+
`--print` registers the lease and writes the route file but does not start Claude. It prints `{"argv","cwd","envKeys","envFile","routeFile","lease"}`; the values (the loopback gateway token among them, never a provider credential) are only in `envFile`, `~/.hasna/switcher/state/resume/<session id>.env`, mode 0600, as `export NAME='value'` lines. Start the argv in `cwd` after sourcing that file. The lease is released when the command exits; the launch stays current for the gateway's grace period (10 minutes by default) and, because its Claude route is durable, the gateway serves it again on the first request from any process of this user for 30 days after it ends. `--print` needs that durable route and exits 5 when it cannot have it (for example with `SWITCHER_GATEWAY=local` or the gateway down).
|
|
651
|
+
|
|
652
|
+
Exit codes: 0 ok (the harness's own code once it runs), 2 usage, 3 not owned by Switcher, 4 profile missing or no longer a Claude profile, 5 route or lease unavailable, 1 other errors.
|
|
653
|
+
|
|
634
654
|
## Run a persistent service
|
|
635
655
|
|
|
636
656
|
For a private local/self-hosted service, inject a random operator token of at least 24 characters as `HASNA_SWITCHER_API_KEY`. For a hosted PostgreSQL service, inject `HASNA_SWITCHER_API_SIGNING_KEY` (or the shared `HASNA_API_SIGNING_KEY` / `API_KEY_SIGNING_SECRET`) and issue revocable, scoped `switcher:read` / `switcher:write` client keys through `@hasna/contracts`. Exactly one authentication mode is accepted. Provider credentials stay in each launcher process; hosted catalog refresh uploads metadata only.
|
|
@@ -778,6 +798,8 @@ console.log(plan.catalog.models.length);
|
|
|
778
798
|
|
|
779
799
|
The SDK supports Node and Bun. It has no database or launcher imports. Explicit credentials may be supplied as a string or a fresh resolver function; `clientFromEnv()` uses the shared contracts resolver with environment-only inputs and no disk fallback.
|
|
780
800
|
|
|
801
|
+
The SDK client retries a call at most three times, with exponential backoff (200 ms, 400 ms, 800 ms plus up to 50% jitter; no single wait exceeds 2 s, jitter included), and only for two failure shapes: any HTTP 503 (an edge in front of the API being briefly unreachable, or Switcher's own JSON `storage unavailable`, whose failed write rolled back), or a 5xx response with a non-JSON body such as the plain-text `error code: 500`. A GET is retried; a write is retried only when its idempotency key was supplied, and every attempt reuses that key, so the server replays the stored result instead of repeating the write. `createProvider`, `updateProvider`, `deleteProvider`, `refreshModels`, `saveCatalog`, `createProfile`, `updateProfile`, `deleteProfile`, `launchPlan`, `createRun` and `finishRun` mint one key per call when you pass none. A 401, a 403, any other 4xx, a 5xx other than 503 that has a JSON body, a refused connection and a non-JSON success body are never retried. Pass `retry: {retries, baseDelayMs, maxDelayMs}` to tune the policy (at most 5 retries and 10 s for any one wait; larger values are clamped) or `retry: false` to turn it off.
|
|
802
|
+
|
|
781
803
|
SDK and CLI API-error diagnostics validate code/request-ID fields, bound message length, remove terminal control characters, and redact the operator credential actually sent with that request. Redaction covers raw, JSON-string-escaped, URL-encoded and Base64/Base64url representations. Native harness stdout and terminal output remain under the native client's control.
|
|
782
804
|
|
|
783
805
|
Public lifecycle endpoints: `GET /health`, `/ready`, `/version`, `/openapi.json`, and `/v1/openapi.json`. Provider/profile CRUD, `/v1/provider-presets`, catalog list/refresh, version-checked catalog metadata commits, launch-plan validation and run metadata are under `/v1`. The SDK includes `listProviderPresets()`, `getProviderPreset()`, `health()`, `ready()`, and `version()`. SDK types are generated from that OpenAPI document. API errors include code, message and request ID.
|
package/dist/claude-context.d.ts
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
/** Keep enough room for Claude's next turn and compaction request on DeepSeek.
|
|
2
2
|
* https://api-docs.deepseek.com/quick_start/agent_integrations/claude_code/
|
|
3
3
|
* Inspect the original provider URL: the native URL is a loopback gateway.
|
|
4
|
+
*
|
|
5
|
+
* For any other provider, the selected model's catalog window sets the same headroom. Without
|
|
6
|
+
* CLAUDE_CODE_AUTO_COMPACT_WINDOW, Claude Code runs no auto-compaction at all for a `[1m]` model id
|
|
7
|
+
* it doesn't recognise, so the context grows until the provider refuses it (a glm session on
|
|
8
|
+
* OpenRouter grew past 1M tokens and every request failed, 04.11, SH-00074). Setting it turns
|
|
9
|
+
* proactive compaction back on: CLAUDE_CODE_AUTO_COMPACT_WINDOW (100k to 1M) compacts at three
|
|
10
|
+
* quarters of the window, DeepSeek's ratio (786432 of 1048576). Below 1M,
|
|
11
|
+
* CLAUDE_CODE_MAX_CONTEXT_TOKENS tells Claude the real window instead of its 200k assumption; at 1M
|
|
12
|
+
* and above the `[1m]` suffix does (claudeNativeModelId). Both are documented Claude Code settings:
|
|
13
|
+
* https://code.claude.com/docs/en/model-config#correct-the-window-for-a-gateway-or-custom-model-id
|
|
14
|
+
* A model without a declared window keeps Claude's own defaults; explicit settings always win.
|
|
4
15
|
*/
|
|
5
|
-
export declare function claudeContextEnvironment(providerBaseUrl: string, environment?: NodeJS.ProcessEnv): Record<string, string>;
|
|
16
|
+
export declare function claudeContextEnvironment(providerBaseUrl: string, environment?: NodeJS.ProcessEnv, contextWindow?: number): Record<string, string>;
|
|
6
17
|
/** Claude Code treats a model it doesn't know as a 200k-token one and compacts near 168k, even when
|
|
7
18
|
* the provider serves more and CLAUDE_CODE_AUTO_COMPACT_WINDOW is higher (measured on DeepSeek flash,
|
|
8
19
|
* 30.10). The `[1m]` suffix tells it the window is 1M; Claude strips it before calling the provider.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare const isClaudeSessionId: (value: unknown) => value is string;
|
|
2
|
+
export declare const claudeSessionsDirectory: (sessionDir: string) => string;
|
|
3
|
+
/** The session a Claude Code request belongs to, or undefined when it names none. */
|
|
4
|
+
export declare function claudeRequestSessionId(headers: Headers, body: unknown): string | undefined;
|
|
5
|
+
export type ClaudeSessionRecord = {
|
|
6
|
+
schema: 1;
|
|
7
|
+
sessionId: string;
|
|
8
|
+
cwd: string;
|
|
9
|
+
observedAt: string;
|
|
10
|
+
};
|
|
11
|
+
/** Write (or replace) the record of a session in a profile's session folder, owner-only. */
|
|
12
|
+
export declare function recordClaudeSession(sessionDir: string, session: {
|
|
13
|
+
sessionId: string;
|
|
14
|
+
cwd: string;
|
|
15
|
+
observedAt?: string;
|
|
16
|
+
}): Promise<string>;
|
|
17
|
+
export type ClaudeSessionOwner = {
|
|
18
|
+
sessionId: string;
|
|
19
|
+
profileId: string;
|
|
20
|
+
cwd: string;
|
|
21
|
+
observedAt: string;
|
|
22
|
+
record: string;
|
|
23
|
+
sessionDir: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The profile that last served a Claude session in this state root, or undefined. When a session
|
|
27
|
+
* ran under more than one profile, the most recent observation wins: that is the route it used last.
|
|
28
|
+
*/
|
|
29
|
+
export declare function findClaudeSession(root: string, sessionId: string): Promise<ClaudeSessionOwner | undefined>;
|