@hasna/switcher 0.2.8 → 0.2.10
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 +4 -2
- package/dist/cli/index.js +197 -234
- package/dist/credentials.d.ts +2 -12
- package/dist/domain.d.ts +1 -1
- package/dist/index.js +16 -16
- package/dist/mcp/index.js +127 -90
- package/dist/opencode-authentication.d.ts +11 -0
- package/dist/sdk.js +16 -16
- package/dist/serve/index.js +13 -9
- package/dist/version.d.ts +1 -0
- package/hasna.contract.json +1 -1
- package/openapi.json +1 -1
- package/package.json +2 -2
package/dist/credentials.d.ts
CHANGED
|
@@ -239,18 +239,8 @@ export declare class CredentialBindings {
|
|
|
239
239
|
}
|
|
240
240
|
export declare function credentialReference(selector: string): string;
|
|
241
241
|
export declare function validateVaultExecutable(path: string): Promise<string>;
|
|
242
|
-
/** Explicit
|
|
243
|
-
|
|
244
|
-
* No credential is resolved and the executable is never started. */
|
|
245
|
-
export declare function repairVaultExecutablePermissions(path: string, expectedSha256: string): Promise<{
|
|
246
|
-
changed: false;
|
|
247
|
-
mode: string;
|
|
248
|
-
sha256: string;
|
|
249
|
-
} | {
|
|
250
|
-
changed: true;
|
|
251
|
-
mode: string;
|
|
252
|
-
sha256: string;
|
|
253
|
-
}>;
|
|
242
|
+
/** Explicit, digest-verified finalization. Never resolves a credential or starts the executable. */
|
|
243
|
+
export declare function repairVaultExecutablePermissions(path: string, expectedSha256: string): Promise<import("@hasna/contracts/executable-trust").ExecutableRepairResult>;
|
|
254
244
|
export declare function bindingTarget(selector: string, override?: string, allowedOrigins?: string[]): {
|
|
255
245
|
credentialEnv: string;
|
|
256
246
|
origins: string[];
|
package/dist/domain.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type RoutingEvent } from "./model-policy-schema";
|
|
|
3
3
|
export { modelPolicySchema, routingEventSchema, routingEventsSchema } from "./model-policy-schema";
|
|
4
4
|
export type { ModelPolicy, RoutingEvent } from "./model-policy-schema";
|
|
5
5
|
export type { AuthStyle } from "./auth";
|
|
6
|
-
export
|
|
6
|
+
export { VERSION } from "./version";
|
|
7
7
|
export declare const harnessSchema: z.ZodEnum<["claude", "codex", "grok", "opencode", "opencode2", "pi", "omp", "dsh", "cline", "hermes", "prime-agent", "gemini", "antigravity", "junie", "aider", "kilo"]>;
|
|
8
8
|
export declare const protocolSchema: z.ZodEnum<["anthropic-messages", "openai-responses", "openai-chat", "gemini-generate-content"]>;
|
|
9
9
|
export declare const idSchema: z.ZodString;
|
package/dist/index.js
CHANGED
|
@@ -429,12 +429,12 @@ async function detectChatGPTApp(appPath, system = host) {
|
|
|
429
429
|
const bundleId = await field("CFBundleIdentifier");
|
|
430
430
|
if (bundleId !== "com.openai.codex")
|
|
431
431
|
continue;
|
|
432
|
-
const name = await field("CFBundleExecutable"),
|
|
432
|
+
const name = await field("CFBundleExecutable"), version2 = await field("CFBundleShortVersionString");
|
|
433
433
|
if (!/^[A-Za-z0-9 _-]+$/.test(name))
|
|
434
434
|
continue;
|
|
435
435
|
const executable = join(path, "Contents/MacOS", name), codexExecutable = join(path, "Contents/Resources/codex");
|
|
436
436
|
if (await system.executable(executable) && await system.executable(codexExecutable))
|
|
437
|
-
return { path, executable, codexExecutable, bundleId, version };
|
|
437
|
+
return { path, executable, codexExecutable, bundleId, version: version2 };
|
|
438
438
|
} catch {}
|
|
439
439
|
}
|
|
440
440
|
throw new Fault(404, "app_not_installed", "Install the current ChatGPT/Codex macOS app or use --app-path with its absolute .app path. Legacy ChatGPT Classic cannot run custom Codex providers.");
|
|
@@ -457,12 +457,12 @@ async function detectClaudeDesktopApp(appPath, system = host) {
|
|
|
457
457
|
const bundleId = await field("CFBundleIdentifier");
|
|
458
458
|
if (bundleId !== "com.anthropic.claudefordesktop")
|
|
459
459
|
continue;
|
|
460
|
-
const name = await field("CFBundleExecutable"),
|
|
460
|
+
const name = await field("CFBundleExecutable"), version2 = await field("CFBundleShortVersionString");
|
|
461
461
|
if (!/^[A-Za-z0-9 _-]+$/.test(name))
|
|
462
462
|
continue;
|
|
463
463
|
const executable = join(path, "Contents/MacOS", name);
|
|
464
464
|
if (await system.executable(executable))
|
|
465
|
-
return { path, executable, bundleId, version };
|
|
465
|
+
return { path, executable, bundleId, version: version2 };
|
|
466
466
|
} catch {}
|
|
467
467
|
}
|
|
468
468
|
throw new Fault(404, "app_not_installed", "Install the current Claude macOS desktop app or supply its absolute .app path with --app-path.");
|
|
@@ -608,17 +608,17 @@ class SwitcherClient {
|
|
|
608
608
|
createProvider(input, idempotencyKey) {
|
|
609
609
|
return this.request("POST", "/v1/providers", input, { idempotencyKey });
|
|
610
610
|
}
|
|
611
|
-
updateProvider(input,
|
|
612
|
-
return this.request("PUT", `/v1/providers/${encodeURIComponent(input.id)}`, input, { version, idempotencyKey });
|
|
611
|
+
updateProvider(input, version2, idempotencyKey) {
|
|
612
|
+
return this.request("PUT", `/v1/providers/${encodeURIComponent(input.id)}`, input, { version: version2, idempotencyKey });
|
|
613
613
|
}
|
|
614
|
-
deleteProvider(id,
|
|
615
|
-
return this.request("DELETE", `/v1/providers/${encodeURIComponent(id)}`, undefined, { version, idempotencyKey });
|
|
614
|
+
deleteProvider(id, version2, idempotencyKey) {
|
|
615
|
+
return this.request("DELETE", `/v1/providers/${encodeURIComponent(id)}`, undefined, { version: version2, idempotencyKey });
|
|
616
616
|
}
|
|
617
617
|
async editModel(providerId, operation, value) {
|
|
618
618
|
const model = typeof value === "string" ? undefined : parse(modelSchema, value);
|
|
619
619
|
const id = model?.id ?? parse(modelSchema.shape.id, value);
|
|
620
620
|
const provider = await this.getProvider(providerId);
|
|
621
|
-
const { version, updatedAt, ...input } = provider;
|
|
621
|
+
const { version: version2, updatedAt, ...input } = provider;
|
|
622
622
|
const manual = provider.manualModels.some((item) => item.id === id);
|
|
623
623
|
const additional = (provider.additionalModels ?? []).some((item) => item.id === id);
|
|
624
624
|
if (operation === "add" && (manual || additional))
|
|
@@ -630,7 +630,7 @@ class SwitcherClient {
|
|
|
630
630
|
const additionalModels = (provider.additionalModels ?? []).filter((item) => item.id !== id);
|
|
631
631
|
if (operation !== "remove")
|
|
632
632
|
(useManual ? manualModels : additionalModels).push(model);
|
|
633
|
-
return this.updateProvider({ ...input, manualModels, additionalModels },
|
|
633
|
+
return this.updateProvider({ ...input, manualModels, additionalModels }, version2);
|
|
634
634
|
}
|
|
635
635
|
addModel(providerId, model) {
|
|
636
636
|
return this.editModel(providerId, "add", model);
|
|
@@ -659,11 +659,11 @@ class SwitcherClient {
|
|
|
659
659
|
createProfile(input, idempotencyKey) {
|
|
660
660
|
return this.request("POST", "/v1/profiles", input, { idempotencyKey });
|
|
661
661
|
}
|
|
662
|
-
updateProfile(input,
|
|
663
|
-
return this.request("PUT", `/v1/profiles/${encodeURIComponent(input.id)}`, input, { version, idempotencyKey });
|
|
662
|
+
updateProfile(input, version2, idempotencyKey) {
|
|
663
|
+
return this.request("PUT", `/v1/profiles/${encodeURIComponent(input.id)}`, input, { version: version2, idempotencyKey });
|
|
664
664
|
}
|
|
665
|
-
deleteProfile(id,
|
|
666
|
-
return this.request("DELETE", `/v1/profiles/${encodeURIComponent(id)}`, undefined, { version, idempotencyKey });
|
|
665
|
+
deleteProfile(id, version2, idempotencyKey) {
|
|
666
|
+
return this.request("DELETE", `/v1/profiles/${encodeURIComponent(id)}`, undefined, { version: version2, idempotencyKey });
|
|
667
667
|
}
|
|
668
668
|
launchPlan(profileId, idempotencyKey) {
|
|
669
669
|
return this.request("POST", "/v1/launch-plans", { profileId }, { idempotencyKey });
|
|
@@ -677,8 +677,8 @@ class SwitcherClient {
|
|
|
677
677
|
createRun(input, idempotencyKey) {
|
|
678
678
|
return this.request("POST", "/v1/runs", { ...input, modelPolicyVersion: 1 }, { idempotencyKey });
|
|
679
679
|
}
|
|
680
|
-
finishRun(id,
|
|
681
|
-
return this.request("PATCH", `/v1/runs/${encodeURIComponent(id)}`, input, { version, idempotencyKey });
|
|
680
|
+
finishRun(id, version2, input, idempotencyKey) {
|
|
681
|
+
return this.request("PATCH", `/v1/runs/${encodeURIComponent(id)}`, input, { version: version2, idempotencyKey });
|
|
682
682
|
}
|
|
683
683
|
}
|
|
684
684
|
function clientFromEnv(env = process.env, options = {}) {
|
package/dist/mcp/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// src/mcp.ts
|
|
5
5
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
6
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
7
|
-
import { z as
|
|
7
|
+
import { z as z8 } from "zod";
|
|
8
8
|
|
|
9
9
|
// src/runtime.ts
|
|
10
10
|
import { randomBytes } from "crypto";
|
|
@@ -68,9 +68,13 @@ var routingEventsSchema = z2.array(routingEventSchema).max(1000);
|
|
|
68
68
|
function canonicalPolicyJSON(value) {
|
|
69
69
|
return JSON.stringify(value, (_key, item) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item);
|
|
70
70
|
}
|
|
71
|
+
// package.json
|
|
72
|
+
var version = "0.2.10";
|
|
73
|
+
|
|
74
|
+
// src/version.ts
|
|
75
|
+
var VERSION = version;
|
|
71
76
|
|
|
72
77
|
// src/domain.ts
|
|
73
|
-
var VERSION = "0.2.7";
|
|
74
78
|
var harnessSchema = z3.enum(["claude", "codex", "grok", "opencode", "opencode2", "pi", "omp", "dsh", "cline", "hermes", "prime-agent", "gemini", "antigravity", "junie", "aider", "kilo"]);
|
|
75
79
|
var protocolSchema = z3.enum(["anthropic-messages", "openai-responses", "openai-chat", "gemini-generate-content"]);
|
|
76
80
|
var idSchema = z3.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,79}$/);
|
|
@@ -556,17 +560,17 @@ class SwitcherClient {
|
|
|
556
560
|
createProvider(input, idempotencyKey) {
|
|
557
561
|
return this.request("POST", "/v1/providers", input, { idempotencyKey });
|
|
558
562
|
}
|
|
559
|
-
updateProvider(input,
|
|
560
|
-
return this.request("PUT", `/v1/providers/${encodeURIComponent(input.id)}`, input, { version, idempotencyKey });
|
|
563
|
+
updateProvider(input, version2, idempotencyKey) {
|
|
564
|
+
return this.request("PUT", `/v1/providers/${encodeURIComponent(input.id)}`, input, { version: version2, idempotencyKey });
|
|
561
565
|
}
|
|
562
|
-
deleteProvider(id,
|
|
563
|
-
return this.request("DELETE", `/v1/providers/${encodeURIComponent(id)}`, undefined, { version, idempotencyKey });
|
|
566
|
+
deleteProvider(id, version2, idempotencyKey) {
|
|
567
|
+
return this.request("DELETE", `/v1/providers/${encodeURIComponent(id)}`, undefined, { version: version2, idempotencyKey });
|
|
564
568
|
}
|
|
565
569
|
async editModel(providerId, operation, value) {
|
|
566
570
|
const model = typeof value === "string" ? undefined : parse(modelSchema, value);
|
|
567
571
|
const id = model?.id ?? parse(modelSchema.shape.id, value);
|
|
568
572
|
const provider = await this.getProvider(providerId);
|
|
569
|
-
const { version, updatedAt, ...input } = provider;
|
|
573
|
+
const { version: version2, updatedAt, ...input } = provider;
|
|
570
574
|
const manual = provider.manualModels.some((item) => item.id === id);
|
|
571
575
|
const additional = (provider.additionalModels ?? []).some((item) => item.id === id);
|
|
572
576
|
if (operation === "add" && (manual || additional))
|
|
@@ -578,7 +582,7 @@ class SwitcherClient {
|
|
|
578
582
|
const additionalModels = (provider.additionalModels ?? []).filter((item) => item.id !== id);
|
|
579
583
|
if (operation !== "remove")
|
|
580
584
|
(useManual ? manualModels : additionalModels).push(model);
|
|
581
|
-
return this.updateProvider({ ...input, manualModels, additionalModels },
|
|
585
|
+
return this.updateProvider({ ...input, manualModels, additionalModels }, version2);
|
|
582
586
|
}
|
|
583
587
|
addModel(providerId, model) {
|
|
584
588
|
return this.editModel(providerId, "add", model);
|
|
@@ -607,11 +611,11 @@ class SwitcherClient {
|
|
|
607
611
|
createProfile(input, idempotencyKey) {
|
|
608
612
|
return this.request("POST", "/v1/profiles", input, { idempotencyKey });
|
|
609
613
|
}
|
|
610
|
-
updateProfile(input,
|
|
611
|
-
return this.request("PUT", `/v1/profiles/${encodeURIComponent(input.id)}`, input, { version, idempotencyKey });
|
|
614
|
+
updateProfile(input, version2, idempotencyKey) {
|
|
615
|
+
return this.request("PUT", `/v1/profiles/${encodeURIComponent(input.id)}`, input, { version: version2, idempotencyKey });
|
|
612
616
|
}
|
|
613
|
-
deleteProfile(id,
|
|
614
|
-
return this.request("DELETE", `/v1/profiles/${encodeURIComponent(id)}`, undefined, { version, idempotencyKey });
|
|
617
|
+
deleteProfile(id, version2, idempotencyKey) {
|
|
618
|
+
return this.request("DELETE", `/v1/profiles/${encodeURIComponent(id)}`, undefined, { version: version2, idempotencyKey });
|
|
615
619
|
}
|
|
616
620
|
launchPlan(profileId, idempotencyKey) {
|
|
617
621
|
return this.request("POST", "/v1/launch-plans", { profileId }, { idempotencyKey });
|
|
@@ -625,8 +629,8 @@ class SwitcherClient {
|
|
|
625
629
|
createRun(input, idempotencyKey) {
|
|
626
630
|
return this.request("POST", "/v1/runs", { ...input, modelPolicyVersion: 1 }, { idempotencyKey });
|
|
627
631
|
}
|
|
628
|
-
finishRun(id,
|
|
629
|
-
return this.request("PATCH", `/v1/runs/${encodeURIComponent(id)}`, input, { version, idempotencyKey });
|
|
632
|
+
finishRun(id, version2, input, idempotencyKey) {
|
|
633
|
+
return this.request("PATCH", `/v1/runs/${encodeURIComponent(id)}`, input, { version: version2, idempotencyKey });
|
|
630
634
|
}
|
|
631
635
|
}
|
|
632
636
|
function clientFromEnv(env = process.env, options = {}) {
|
|
@@ -800,8 +804,8 @@ class Store {
|
|
|
800
804
|
}
|
|
801
805
|
return value;
|
|
802
806
|
}
|
|
803
|
-
async remove(kind, id,
|
|
804
|
-
const rows = await db.unsafe(`DELETE FROM switcher_${kind} WHERE id = $1 AND version = $2 RETURNING id`, [id,
|
|
807
|
+
async remove(kind, id, version2, db) {
|
|
808
|
+
const rows = await db.unsafe(`DELETE FROM switcher_${kind} WHERE id = $1 AND version = $2 RETURNING id`, [id, version2]);
|
|
805
809
|
if (!rows.length)
|
|
806
810
|
throw new Fault(409, "version_conflict", "Entry changed or is missing; reload before retrying.");
|
|
807
811
|
return { deleted: id };
|
|
@@ -1193,7 +1197,7 @@ var openapi_default = {
|
|
|
1193
1197
|
openapi: "3.0.3",
|
|
1194
1198
|
info: {
|
|
1195
1199
|
title: "Switcher API",
|
|
1196
|
-
version: "0.2.
|
|
1200
|
+
version: "0.2.10",
|
|
1197
1201
|
description: "Authenticated provider/profile/catalog control plane. Launches run locally; the API never returns provider credentials."
|
|
1198
1202
|
},
|
|
1199
1203
|
security: [
|
|
@@ -5505,7 +5509,7 @@ function createHandler(store, authentication, providerEnv = process.env, resolve
|
|
|
5505
5509
|
}
|
|
5506
5510
|
}
|
|
5507
5511
|
const fingerprint = hash(JSON.stringify([request.method, route2, body, request.headers.get("if-match")])).toString("hex");
|
|
5508
|
-
const
|
|
5512
|
+
const version2 = () => {
|
|
5509
5513
|
const v = request.headers.get("if-match");
|
|
5510
5514
|
if (!v || !/^[1-9]\d*$/.test(v))
|
|
5511
5515
|
throw new Fault(428, "version_required", "Supply the current numeric version in If-Match.");
|
|
@@ -5525,7 +5529,7 @@ function createHandler(store, authentication, providerEnv = process.env, resolve
|
|
|
5525
5529
|
const result = await store.mutate(key, fingerprint, async (db) => {
|
|
5526
5530
|
if ((resource === "providers" || resource === "profiles") && parts.length <= 3) {
|
|
5527
5531
|
if (request.method === "DELETE" && id2)
|
|
5528
|
-
return store.remove(resource, id2,
|
|
5532
|
+
return store.remove(resource, id2, version2(), db);
|
|
5529
5533
|
if (request.method === "POST" && !id2 || request.method === "PUT" && id2) {
|
|
5530
5534
|
const value = resource === "providers" ? parse(providerInputSchema, body) : parse(profileInputSchema, body);
|
|
5531
5535
|
if (id2 && value.id !== id2)
|
|
@@ -5535,7 +5539,7 @@ function createHandler(store, authentication, providerEnv = process.env, resolve
|
|
|
5535
5539
|
const provider = await store.get("providers", profile.providerId, db);
|
|
5536
5540
|
validateHarnessProvider(profile.harness, provider);
|
|
5537
5541
|
}
|
|
5538
|
-
const saved = await store.put(resource, value, id2 ?
|
|
5542
|
+
const saved = await store.put(resource, value, id2 ? version2() : undefined, db);
|
|
5539
5543
|
if (resource === "providers" && id2)
|
|
5540
5544
|
await db.unsafe("DELETE FROM switcher_catalogs WHERE id = $1", [id2]);
|
|
5541
5545
|
return saved;
|
|
@@ -5562,7 +5566,7 @@ function createHandler(store, authentication, providerEnv = process.env, resolve
|
|
|
5562
5566
|
if (store.engine === "postgresql")
|
|
5563
5567
|
await db.unsafe("SELECT id FROM switcher_providers WHERE id = $1 FOR UPDATE", [id2]);
|
|
5564
5568
|
const provider = await store.get("providers", id2, db);
|
|
5565
|
-
if (provider.version !==
|
|
5569
|
+
if (provider.version !== version2())
|
|
5566
5570
|
throw new Fault(409, "provider_changed", "Provider changed during local discovery; refresh again.");
|
|
5567
5571
|
const catalog = parse(catalogSchema, body);
|
|
5568
5572
|
const expectedSource = provider.manualModels.length ? "manual" : "remote";
|
|
@@ -5642,7 +5646,7 @@ function createHandler(store, authentication, providerEnv = process.env, resolve
|
|
|
5642
5646
|
const run = await store.get("runs", id2, db);
|
|
5643
5647
|
if (run.status !== "running")
|
|
5644
5648
|
throw new Fault(409, "run_finished", "Run has already finished.");
|
|
5645
|
-
return store.put("runs", { ...run, ...input, endedAt: new Date().toISOString() },
|
|
5649
|
+
return store.put("runs", { ...run, ...input, endedAt: new Date().toISOString() }, version2(), db);
|
|
5646
5650
|
}
|
|
5647
5651
|
throw new Fault(404, "not_found", "Route was not found.");
|
|
5648
5652
|
});
|
|
@@ -5816,9 +5820,10 @@ async function openCliRuntime(env = process.env, resolveCredential) {
|
|
|
5816
5820
|
|
|
5817
5821
|
// src/credentials.ts
|
|
5818
5822
|
import { z as z5 } from "zod";
|
|
5823
|
+
import { ExecutableTrustError, repairExecutablePermissions, validateExecutable } from "@hasna/contracts/executable-trust";
|
|
5819
5824
|
import { constants as constants2 } from "fs";
|
|
5820
|
-
import { open, readdir, unlink, link, lstat as lstat2
|
|
5821
|
-
import { join as join2, isAbsolute as isAbsolute2
|
|
5825
|
+
import { open, readdir, unlink, link, lstat as lstat2 } from "fs/promises";
|
|
5826
|
+
import { join as join2, isAbsolute as isAbsolute2 } from "path";
|
|
5822
5827
|
import { createHash as createHash4, randomUUID } from "crypto";
|
|
5823
5828
|
import { execFile as execFile2, spawn } from "child_process";
|
|
5824
5829
|
import { promisify as promisify2 } from "util";
|
|
@@ -5967,53 +5972,31 @@ class CredentialBindings {
|
|
|
5967
5972
|
return { removed: name };
|
|
5968
5973
|
}
|
|
5969
5974
|
}
|
|
5970
|
-
|
|
5971
|
-
|
|
5972
|
-
|
|
5973
|
-
|
|
5974
|
-
|
|
5975
|
-
|
|
5976
|
-
|
|
5977
|
-
|
|
5978
|
-
|
|
5979
|
-
|
|
5980
|
-
|
|
5981
|
-
|
|
5982
|
-
|
|
5983
|
-
|
|
5984
|
-
|
|
5985
|
-
|
|
5986
|
-
|
|
5987
|
-
|
|
5988
|
-
if (!part)
|
|
5989
|
-
continue;
|
|
5990
|
-
prefix = join2(prefix, part);
|
|
5991
|
-
if (seen.has(prefix))
|
|
5992
|
-
continue;
|
|
5993
|
-
seen.add(prefix);
|
|
5994
|
-
if (seen.size > 256)
|
|
5995
|
-
throw new Fault(422, "vault_exec_permissions", "The secrets executable path has too many symlink components.");
|
|
5996
|
-
const entry = await lstat2(prefix);
|
|
5997
|
-
const permittedFile = permitWritableFile && prefix === resolved && entry.isFile();
|
|
5998
|
-
if (entry.uid !== 0 && entry.uid !== process.getuid?.() || !entry.isSymbolicLink() && !permittedFile && entry.mode & 18)
|
|
5999
|
-
throw new Fault(422, "vault_exec_permissions", "The secrets executable and its ancestor directories must not be replaceable by other users. Choose an installation owned by this user or root without group/public write permissions.");
|
|
6000
|
-
if (entry.isSymbolicLink())
|
|
6001
|
-
await inspect(resolvePath(dirname2(prefix), await readlink(prefix)));
|
|
6002
|
-
}
|
|
6003
|
-
};
|
|
6004
|
-
try {
|
|
6005
|
-
await inspect(path);
|
|
6006
|
-
await inspect(resolved);
|
|
6007
|
-
} catch (error) {
|
|
6008
|
-
if (error instanceof Fault)
|
|
6009
|
-
throw error;
|
|
6010
|
-
throw new Fault(422, "vault_exec_unavailable", "The secrets executable path changed or could not be verified. Retry with a trusted installation.");
|
|
6011
|
-
}
|
|
6012
|
-
}
|
|
6013
|
-
return resolved;
|
|
5975
|
+
function vaultExecutableFault(error) {
|
|
5976
|
+
if (!(error instanceof ExecutableTrustError))
|
|
5977
|
+
throw error;
|
|
5978
|
+
const codes = {
|
|
5979
|
+
invalid_request: "invalid_request",
|
|
5980
|
+
executable_unavailable: "vault_exec_unavailable",
|
|
5981
|
+
executable_permissions: "vault_exec_permissions",
|
|
5982
|
+
executable_repair_unsafe: "vault_exec_repair_unsafe",
|
|
5983
|
+
executable_digest_mismatch: "vault_exec_digest_mismatch"
|
|
5984
|
+
};
|
|
5985
|
+
const messages = {
|
|
5986
|
+
invalid_request: error.message,
|
|
5987
|
+
executable_unavailable: "The configured secrets CLI must be an installed executable file. Use --vault-cli with its absolute path.",
|
|
5988
|
+
executable_permissions: "The secrets executable and its ancestor directories must be owned by this user or root and not writable by other users. Use credentials repair-executable REFERENCE --sha256 EXPECTED with the executable digest from a verified package artifact. Launch never repairs permissions automatically.",
|
|
5989
|
+
executable_repair_unsafe: error.message,
|
|
5990
|
+
executable_digest_mismatch: "Installed executable differs from the trusted artifact digest. Reinstall the verified package; no credential was accessed."
|
|
5991
|
+
};
|
|
5992
|
+
throw new Fault(error.status, codes[error.code], messages[error.code]);
|
|
6014
5993
|
}
|
|
6015
5994
|
async function validateVaultExecutable(path) {
|
|
6016
|
-
|
|
5995
|
+
try {
|
|
5996
|
+
return await validateExecutable(path);
|
|
5997
|
+
} catch (error) {
|
|
5998
|
+
return vaultExecutableFault(error);
|
|
5999
|
+
}
|
|
6017
6000
|
}
|
|
6018
6001
|
async function readKeychain(source) {
|
|
6019
6002
|
if (process.platform !== "darwin")
|
|
@@ -6233,18 +6216,70 @@ async function fetchVaultCredential(binding, env) {
|
|
|
6233
6216
|
}
|
|
6234
6217
|
|
|
6235
6218
|
// src/provider-credential-onboarding.ts
|
|
6236
|
-
import { z as
|
|
6219
|
+
import { z as z7 } from "zod";
|
|
6237
6220
|
import { createHash as createHash5 } from "crypto";
|
|
6238
6221
|
import { createInterface } from "readline/promises";
|
|
6222
|
+
|
|
6223
|
+
// src/opencode-authentication.ts
|
|
6224
|
+
import { z as z6 } from "zod";
|
|
6225
|
+
var endpoint2 = "https://opencode.ai/zen/v1/systemone";
|
|
6226
|
+
var body = JSON.stringify({
|
|
6227
|
+
model: "jev-1.13-free",
|
|
6228
|
+
state: "The fixed test number is 7.",
|
|
6229
|
+
questions: { match: { type: "noul", instructions: "Is the fixed test number 7?" } }
|
|
6230
|
+
});
|
|
6231
|
+
var responseSchema = z6.object({
|
|
6232
|
+
model: z6.enum(["jev-1.13", "jev-1.13.0", "jev-1.13-free"]),
|
|
6233
|
+
answers: z6.object({ match: z6.object({ type: z6.literal("noul"), noul: z6.number().finite().min(0).max(1) }) }),
|
|
6234
|
+
usage: z6.object({ input_tokens: z6.number().int().nonnegative(), output_tokens: z6.number().int().nonnegative() })
|
|
6235
|
+
});
|
|
6236
|
+
var authErrorSchema = z6.object({ error: z6.object({ type: z6.enum(["AuthError", "server_error"]) }) });
|
|
6237
|
+
var unavailable = () => new Fault(502, "provider_auth_unavailable", "OpenCode authentication could not be verified with its free check. The selected credential was preserved; no paid model was requested.");
|
|
6238
|
+
function supportsOpenCodeAuthentication(provider) {
|
|
6239
|
+
return provider.credentialEnv === "SWITCHER_PROVIDER_OPENCODE" && ["https://opencode.ai/zen/v1", "https://opencode.ai/zen/v1/"].includes(provider.baseUrl) && (["openai-chat", "openai-responses"].includes(provider.protocol) ? (provider.authStyle ?? "bearer") === "bearer" : provider.protocol === "anthropic-messages" && provider.authStyle === "x-api-key");
|
|
6240
|
+
}
|
|
6241
|
+
async function verifyOpenCodeAuthentication(credential, fetchImpl) {
|
|
6242
|
+
if (!credential || /[\s\x00-\x1f\x7f]/u.test(credential) || credential.toLowerCase() === "public")
|
|
6243
|
+
return { authenticated: false, status: 401 };
|
|
6244
|
+
const signal = AbortSignal.timeout(1e4);
|
|
6245
|
+
const request = async (value) => {
|
|
6246
|
+
try {
|
|
6247
|
+
const response = await fetchImpl(endpoint2, {
|
|
6248
|
+
method: "POST",
|
|
6249
|
+
redirect: "manual",
|
|
6250
|
+
cache: "no-store",
|
|
6251
|
+
signal,
|
|
6252
|
+
body,
|
|
6253
|
+
headers: { accept: "application/json", "content-type": "application/json", "user-agent": "hasna-switcher/credential-check", authorization: `Bearer ${value}` }
|
|
6254
|
+
});
|
|
6255
|
+
return { status: response.status, data: await boundedJson(response, 16 * 1024) };
|
|
6256
|
+
} catch {
|
|
6257
|
+
throw unavailable();
|
|
6258
|
+
}
|
|
6259
|
+
};
|
|
6260
|
+
for (const invalid of ["hasna-switcher-invalid-credential", "oc_sk_invalid"]) {
|
|
6261
|
+
const control = await request(invalid);
|
|
6262
|
+
if (control.status !== 401 || !authErrorSchema.safeParse(control.data).success)
|
|
6263
|
+
throw unavailable();
|
|
6264
|
+
}
|
|
6265
|
+
const result = await request(credential);
|
|
6266
|
+
if ((result.status === 401 || result.status === 403) && authErrorSchema.safeParse(result.data).success)
|
|
6267
|
+
return { authenticated: false, status: result.status };
|
|
6268
|
+
if (result.status !== 200 || !responseSchema.safeParse(result.data).success)
|
|
6269
|
+
throw unavailable();
|
|
6270
|
+
return { authenticated: true, status: 200 };
|
|
6271
|
+
}
|
|
6272
|
+
|
|
6273
|
+
// src/provider-credential-onboarding.ts
|
|
6239
6274
|
var absent = (error) => error instanceof SwitcherError && error.status === 404;
|
|
6240
6275
|
var MAX_CREDENTIAL_MATCHES = 256;
|
|
6241
|
-
var secretMetadataSchema =
|
|
6276
|
+
var secretMetadataSchema = z7.object({
|
|
6242
6277
|
key: vaultKeySchema,
|
|
6243
|
-
type:
|
|
6244
|
-
label:
|
|
6245
|
-
expires_at:
|
|
6246
|
-
created_at:
|
|
6247
|
-
updated_at:
|
|
6278
|
+
type: z7.enum(["api_key", "password", "token", "credential", "other"]),
|
|
6279
|
+
label: z7.string().max(1000).nullable().optional(),
|
|
6280
|
+
expires_at: z7.string().max(200).nullable().optional(),
|
|
6281
|
+
created_at: z7.string().max(200),
|
|
6282
|
+
updated_at: z7.string().max(200)
|
|
6248
6283
|
}).strict();
|
|
6249
6284
|
var cleanDisplay = (value) => value.replace(/[\x00-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]/g, "").slice(0, 500);
|
|
6250
6285
|
var credentialOrigins = (provider) => {
|
|
@@ -6353,6 +6388,8 @@ function authenticationProbe(provider) {
|
|
|
6353
6388
|
return;
|
|
6354
6389
|
}
|
|
6355
6390
|
async function verifyProviderAuthentication({ provider, credential, fetch: fetchImpl = fetch }) {
|
|
6391
|
+
if (!provider.credentialCheck && supportsOpenCodeAuthentication(provider))
|
|
6392
|
+
return verifyOpenCodeAuthentication(credential, fetchImpl);
|
|
6356
6393
|
const probe = authenticationProbe(provider);
|
|
6357
6394
|
if (!probe)
|
|
6358
6395
|
return { authenticated: false, unsupported: true };
|
|
@@ -6504,7 +6541,7 @@ var clientFromEnv2 = () => {
|
|
|
6504
6541
|
throw new Fault(503, "unavailable", "Switcher API runtime is not open.");
|
|
6505
6542
|
return runtime.client;
|
|
6506
6543
|
};
|
|
6507
|
-
var page = { limit:
|
|
6544
|
+
var page = { limit: z8.number().int().min(1).max(1000).optional(), offset: z8.number().int().nonnegative().optional(), search: z8.string().optional() };
|
|
6508
6545
|
function tool(name, description, schema, run) {
|
|
6509
6546
|
server.tool(name, description, schema, async (input) => {
|
|
6510
6547
|
try {
|
|
@@ -6515,18 +6552,18 @@ function tool(name, description, schema, run) {
|
|
|
6515
6552
|
});
|
|
6516
6553
|
}
|
|
6517
6554
|
tool("providers_list", "List provider profiles.", page, (p) => clientFromEnv2().listProviders(p));
|
|
6518
|
-
tool("providers_get", "Get a provider.", { id:
|
|
6555
|
+
tool("providers_get", "Get a provider.", { id: z8.string() }, (p) => clientFromEnv2().getProvider(p.id));
|
|
6519
6556
|
tool("providers_create", "Create a provider using credential environment references only.", providerInputSchema.innerType().shape, (p) => clientFromEnv2().createProvider(p));
|
|
6520
|
-
tool("providers_update", "Replace a provider at its current version.", { provider: providerInputSchema, version:
|
|
6521
|
-
tool("providers_delete", "Delete an unreferenced provider.", { id:
|
|
6522
|
-
tool("models_list", "List catalog with capability information.", { id:
|
|
6557
|
+
tool("providers_update", "Replace a provider at its current version.", { provider: providerInputSchema, version: z8.number().int() }, (p) => clientFromEnv2().updateProvider(p.provider, p.version));
|
|
6558
|
+
tool("providers_delete", "Delete an unreferenced provider.", { id: z8.string(), version: z8.number().int() }, (p) => clientFromEnv2().deleteProvider(p.id, p.version));
|
|
6559
|
+
tool("models_list", "List catalog with capability information.", { id: z8.string(), ...page }, (p) => {
|
|
6523
6560
|
const { id: id2, ...rest } = p;
|
|
6524
6561
|
return clientFromEnv2().listModels(id2, rest);
|
|
6525
6562
|
});
|
|
6526
|
-
tool("models_add", "Add saved model metadata; discovery stays active unless the provider uses a manual catalog.", { providerId:
|
|
6527
|
-
tool("models_update", "Replace all saved metadata for a configured model.", { providerId:
|
|
6528
|
-
tool("models_remove", "Remove saved model metadata; upstream catalog entries remain.", { providerId:
|
|
6529
|
-
tool("models_refresh", "Discover provider models.", { id:
|
|
6563
|
+
tool("models_add", "Add saved model metadata; discovery stays active unless the provider uses a manual catalog.", { providerId: z8.string(), model: modelSchema }, (p) => clientFromEnv2().addModel(p.providerId, p.model));
|
|
6564
|
+
tool("models_update", "Replace all saved metadata for a configured model.", { providerId: z8.string(), model: modelSchema }, (p) => clientFromEnv2().updateModel(p.providerId, p.model));
|
|
6565
|
+
tool("models_remove", "Remove saved model metadata; upstream catalog entries remain.", { providerId: z8.string(), modelId: modelSchema.shape.id }, (p) => clientFromEnv2().removeModel(p.providerId, p.modelId));
|
|
6566
|
+
tool("models_refresh", "Discover provider models.", { id: z8.string() }, async (p) => {
|
|
6530
6567
|
const client = clientFromEnv2(), provider = await client.getProvider(p.id);
|
|
6531
6568
|
if (runtime?.mode !== "remote")
|
|
6532
6569
|
return client.refreshModels(provider.id);
|
|
@@ -6535,13 +6572,13 @@ tool("models_refresh", "Discover provider models.", { id: z7.string() }, async (
|
|
|
6535
6572
|
return launchCatalog(client, provider, false, { clientSide: true, credential, resolveCredential: (candidate) => credentials.resolve(candidate) });
|
|
6536
6573
|
});
|
|
6537
6574
|
tool("profiles_list", "List harness launch profiles.", page, (p) => clientFromEnv2().listProfiles(p));
|
|
6538
|
-
tool("profiles_get", "Get a harness profile.", { id:
|
|
6575
|
+
tool("profiles_get", "Get a harness profile.", { id: z8.string() }, (p) => clientFromEnv2().getProfile(p.id));
|
|
6539
6576
|
tool("profiles_create", "Create a harness launch profile.", profileInputSchema.shape, (p) => clientFromEnv2().createProfile(p));
|
|
6540
|
-
tool("profiles_update", "Replace a harness profile at its version.", { profile: profileInputSchema, version:
|
|
6541
|
-
tool("profiles_delete", "Delete a profile without run history.", { id:
|
|
6542
|
-
tool("launch_plan", "Validate a local launch plan; does not execute a remote process.", { profileId:
|
|
6577
|
+
tool("profiles_update", "Replace a harness profile at its version.", { profile: profileInputSchema, version: z8.number().int() }, (p) => clientFromEnv2().updateProfile(p.profile, p.version));
|
|
6578
|
+
tool("profiles_delete", "Delete a profile without run history.", { id: z8.string(), version: z8.number().int() }, (p) => clientFromEnv2().deleteProfile(p.id, p.version));
|
|
6579
|
+
tool("launch_plan", "Validate a local launch plan; does not execute a remote process.", { profileId: z8.string() }, (p) => clientFromEnv2().launchPlan(p.profileId));
|
|
6543
6580
|
tool("runs_list", "List launch metadata.", page, (p) => clientFromEnv2().listRuns(p));
|
|
6544
|
-
tool("runs_get", "Get launch metadata.", { id:
|
|
6581
|
+
tool("runs_get", "Get launch metadata.", { id: z8.string() }, (p) => clientFromEnv2().getRun(p.id));
|
|
6545
6582
|
if (process.argv.includes("--version"))
|
|
6546
6583
|
console.log(VERSION);
|
|
6547
6584
|
else if (process.argv.includes("--help"))
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ProviderInput } from "./domain";
|
|
2
|
+
export declare function supportsOpenCodeAuthentication(provider: ProviderInput): boolean;
|
|
3
|
+
/** A valid supplied key is required even though this fixed synthetic request is free.
|
|
4
|
+
* Anonymous responses alone are not authentication evidence: first require rejection
|
|
5
|
+
* of invalid keys through both legacy and migrated routing. A legacy-shaped
|
|
6
|
+
* selected key may have been migrated, which cannot be inferred from its prefix.
|
|
7
|
+
*/
|
|
8
|
+
export declare function verifyOpenCodeAuthentication(credential: string, fetchImpl: typeof fetch): Promise<{
|
|
9
|
+
authenticated: boolean;
|
|
10
|
+
status: number;
|
|
11
|
+
}>;
|
package/dist/sdk.js
CHANGED
|
@@ -429,12 +429,12 @@ async function detectChatGPTApp(appPath, system = host) {
|
|
|
429
429
|
const bundleId = await field("CFBundleIdentifier");
|
|
430
430
|
if (bundleId !== "com.openai.codex")
|
|
431
431
|
continue;
|
|
432
|
-
const name = await field("CFBundleExecutable"),
|
|
432
|
+
const name = await field("CFBundleExecutable"), version2 = await field("CFBundleShortVersionString");
|
|
433
433
|
if (!/^[A-Za-z0-9 _-]+$/.test(name))
|
|
434
434
|
continue;
|
|
435
435
|
const executable = join(path, "Contents/MacOS", name), codexExecutable = join(path, "Contents/Resources/codex");
|
|
436
436
|
if (await system.executable(executable) && await system.executable(codexExecutable))
|
|
437
|
-
return { path, executable, codexExecutable, bundleId, version };
|
|
437
|
+
return { path, executable, codexExecutable, bundleId, version: version2 };
|
|
438
438
|
} catch {}
|
|
439
439
|
}
|
|
440
440
|
throw new Fault(404, "app_not_installed", "Install the current ChatGPT/Codex macOS app or use --app-path with its absolute .app path. Legacy ChatGPT Classic cannot run custom Codex providers.");
|
|
@@ -457,12 +457,12 @@ async function detectClaudeDesktopApp(appPath, system = host) {
|
|
|
457
457
|
const bundleId = await field("CFBundleIdentifier");
|
|
458
458
|
if (bundleId !== "com.anthropic.claudefordesktop")
|
|
459
459
|
continue;
|
|
460
|
-
const name = await field("CFBundleExecutable"),
|
|
460
|
+
const name = await field("CFBundleExecutable"), version2 = await field("CFBundleShortVersionString");
|
|
461
461
|
if (!/^[A-Za-z0-9 _-]+$/.test(name))
|
|
462
462
|
continue;
|
|
463
463
|
const executable = join(path, "Contents/MacOS", name);
|
|
464
464
|
if (await system.executable(executable))
|
|
465
|
-
return { path, executable, bundleId, version };
|
|
465
|
+
return { path, executable, bundleId, version: version2 };
|
|
466
466
|
} catch {}
|
|
467
467
|
}
|
|
468
468
|
throw new Fault(404, "app_not_installed", "Install the current Claude macOS desktop app or supply its absolute .app path with --app-path.");
|
|
@@ -608,17 +608,17 @@ class SwitcherClient {
|
|
|
608
608
|
createProvider(input, idempotencyKey) {
|
|
609
609
|
return this.request("POST", "/v1/providers", input, { idempotencyKey });
|
|
610
610
|
}
|
|
611
|
-
updateProvider(input,
|
|
612
|
-
return this.request("PUT", `/v1/providers/${encodeURIComponent(input.id)}`, input, { version, idempotencyKey });
|
|
611
|
+
updateProvider(input, version2, idempotencyKey) {
|
|
612
|
+
return this.request("PUT", `/v1/providers/${encodeURIComponent(input.id)}`, input, { version: version2, idempotencyKey });
|
|
613
613
|
}
|
|
614
|
-
deleteProvider(id,
|
|
615
|
-
return this.request("DELETE", `/v1/providers/${encodeURIComponent(id)}`, undefined, { version, idempotencyKey });
|
|
614
|
+
deleteProvider(id, version2, idempotencyKey) {
|
|
615
|
+
return this.request("DELETE", `/v1/providers/${encodeURIComponent(id)}`, undefined, { version: version2, idempotencyKey });
|
|
616
616
|
}
|
|
617
617
|
async editModel(providerId, operation, value) {
|
|
618
618
|
const model = typeof value === "string" ? undefined : parse(modelSchema, value);
|
|
619
619
|
const id = model?.id ?? parse(modelSchema.shape.id, value);
|
|
620
620
|
const provider = await this.getProvider(providerId);
|
|
621
|
-
const { version, updatedAt, ...input } = provider;
|
|
621
|
+
const { version: version2, updatedAt, ...input } = provider;
|
|
622
622
|
const manual = provider.manualModels.some((item) => item.id === id);
|
|
623
623
|
const additional = (provider.additionalModels ?? []).some((item) => item.id === id);
|
|
624
624
|
if (operation === "add" && (manual || additional))
|
|
@@ -630,7 +630,7 @@ class SwitcherClient {
|
|
|
630
630
|
const additionalModels = (provider.additionalModels ?? []).filter((item) => item.id !== id);
|
|
631
631
|
if (operation !== "remove")
|
|
632
632
|
(useManual ? manualModels : additionalModels).push(model);
|
|
633
|
-
return this.updateProvider({ ...input, manualModels, additionalModels },
|
|
633
|
+
return this.updateProvider({ ...input, manualModels, additionalModels }, version2);
|
|
634
634
|
}
|
|
635
635
|
addModel(providerId, model) {
|
|
636
636
|
return this.editModel(providerId, "add", model);
|
|
@@ -659,11 +659,11 @@ class SwitcherClient {
|
|
|
659
659
|
createProfile(input, idempotencyKey) {
|
|
660
660
|
return this.request("POST", "/v1/profiles", input, { idempotencyKey });
|
|
661
661
|
}
|
|
662
|
-
updateProfile(input,
|
|
663
|
-
return this.request("PUT", `/v1/profiles/${encodeURIComponent(input.id)}`, input, { version, idempotencyKey });
|
|
662
|
+
updateProfile(input, version2, idempotencyKey) {
|
|
663
|
+
return this.request("PUT", `/v1/profiles/${encodeURIComponent(input.id)}`, input, { version: version2, idempotencyKey });
|
|
664
664
|
}
|
|
665
|
-
deleteProfile(id,
|
|
666
|
-
return this.request("DELETE", `/v1/profiles/${encodeURIComponent(id)}`, undefined, { version, idempotencyKey });
|
|
665
|
+
deleteProfile(id, version2, idempotencyKey) {
|
|
666
|
+
return this.request("DELETE", `/v1/profiles/${encodeURIComponent(id)}`, undefined, { version: version2, idempotencyKey });
|
|
667
667
|
}
|
|
668
668
|
launchPlan(profileId, idempotencyKey) {
|
|
669
669
|
return this.request("POST", "/v1/launch-plans", { profileId }, { idempotencyKey });
|
|
@@ -677,8 +677,8 @@ class SwitcherClient {
|
|
|
677
677
|
createRun(input, idempotencyKey) {
|
|
678
678
|
return this.request("POST", "/v1/runs", { ...input, modelPolicyVersion: 1 }, { idempotencyKey });
|
|
679
679
|
}
|
|
680
|
-
finishRun(id,
|
|
681
|
-
return this.request("PATCH", `/v1/runs/${encodeURIComponent(id)}`, input, { version, idempotencyKey });
|
|
680
|
+
finishRun(id, version2, input, idempotencyKey) {
|
|
681
|
+
return this.request("PATCH", `/v1/runs/${encodeURIComponent(id)}`, input, { version: version2, idempotencyKey });
|
|
682
682
|
}
|
|
683
683
|
}
|
|
684
684
|
function clientFromEnv(env = process.env, options = {}) {
|