@hasna/switcher 0.1.7 → 0.1.9
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 +166 -4
- package/dist/chatgpt-launch.d.ts +5 -0
- package/dist/claude-desktop-launch.d.ts +11 -0
- package/dist/cli/index.js +1122 -368
- package/dist/codex-delegation.d.ts +7 -0
- package/dist/credentials.d.ts +7 -7
- package/dist/desktop-apps.d.ts +25 -0
- package/dist/desktop-state.d.ts +3 -0
- package/dist/domain.d.ts +26 -4
- package/dist/generated/api.d.ts +16 -0
- package/dist/harness-process.d.ts +1 -0
- package/dist/harness-types.d.ts +3 -0
- package/dist/harnesses.d.ts +7 -3
- package/dist/index.js +199 -69
- package/dist/inference-gateway.d.ts +2 -0
- package/dist/launcher.d.ts +6 -0
- package/dist/mcp/index.js +145 -78
- package/dist/model-policy-schema.d.ts +6 -0
- package/dist/presets.d.ts +2 -0
- package/dist/reasoning.d.ts +10 -0
- package/dist/sdk.d.ts +156 -0
- package/dist/sdk.js +199 -69
- package/dist/serve/index.js +290 -74
- package/openapi.json +210 -1
- package/package.json +3 -2
package/dist/sdk.js
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
// src/domain.ts
|
|
2
|
-
import { z as
|
|
2
|
+
import { z as z3 } from "zod";
|
|
3
3
|
|
|
4
|
-
// src/
|
|
4
|
+
// src/reasoning.ts
|
|
5
5
|
import { z } from "zod";
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var
|
|
6
|
+
var reasoningEffortSchema = z.enum(["none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra"]);
|
|
7
|
+
|
|
8
|
+
// src/model-policy-schema.ts
|
|
9
|
+
import { z as z2 } from "zod";
|
|
10
|
+
var policyModelIdSchema = z2.string().min(1).max(300).regex(/^[^\u0000-\u001f\u007f]+$/);
|
|
11
|
+
var modelPolicyRoleSchema = z2.enum(["subagent", "fast", "planning", "review", "summary", "compaction", "weak", "editor"]);
|
|
12
|
+
var boundedModelList = z2.array(policyModelIdSchema).max(500);
|
|
13
|
+
var aliasSchema = z2.string().regex(/^[A-Za-z0-9._/-]{1,120}$/).refine((v) => !["__proto__", "prototype", "constructor"].includes(v));
|
|
14
|
+
var boundedModelMap = z2.record(aliasSchema, policyModelIdSchema).superRefine((value, ctx) => {
|
|
11
15
|
if (Object.keys(value).length > 200)
|
|
12
16
|
ctx.addIssue({ code: "custom", message: "Model policy maps may contain at most 200 entries." });
|
|
13
17
|
});
|
|
14
|
-
var fallbacksSchema =
|
|
18
|
+
var fallbacksSchema = z2.record(policyModelIdSchema, z2.array(policyModelIdSchema).max(20)).superRefine((value, ctx) => {
|
|
15
19
|
if (Object.keys(value).length > 200)
|
|
16
20
|
ctx.addIssue({ code: "custom", message: "Model policy fallback maps may contain at most 200 entries." });
|
|
17
21
|
});
|
|
18
|
-
var modelPolicySchema =
|
|
19
|
-
version:
|
|
20
|
-
roles:
|
|
22
|
+
var modelPolicySchema = z2.object({
|
|
23
|
+
version: z2.literal(1).default(1),
|
|
24
|
+
roles: z2.object({
|
|
21
25
|
subagent: policyModelIdSchema.optional(),
|
|
22
26
|
fast: policyModelIdSchema.optional(),
|
|
23
27
|
planning: policyModelIdSchema.optional(),
|
|
@@ -31,26 +35,27 @@ var modelPolicySchema = z.object({
|
|
|
31
35
|
aliases: boundedModelMap.optional(),
|
|
32
36
|
fallbacks: fallbacksSchema.optional()
|
|
33
37
|
}).strict();
|
|
34
|
-
var routingDecisionSchema =
|
|
35
|
-
var routingEventRoleSchema =
|
|
36
|
-
var routingEventSchema =
|
|
37
|
-
at:
|
|
38
|
-
requestId:
|
|
38
|
+
var routingDecisionSchema = z2.enum(["allow", "alias", "reject", "fallback"]);
|
|
39
|
+
var routingEventRoleSchema = z2.enum(["main", ...modelPolicyRoleSchema.options]);
|
|
40
|
+
var routingEventSchema = z2.object({
|
|
41
|
+
at: z2.string().datetime({ offset: true }),
|
|
42
|
+
requestId: z2.string().regex(/^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/),
|
|
39
43
|
requestedModel: policyModelIdSchema,
|
|
40
44
|
resolvedModel: policyModelIdSchema.optional(),
|
|
41
45
|
reportedModel: policyModelIdSchema.optional(),
|
|
46
|
+
reasoningEffort: reasoningEffortSchema.optional(),
|
|
42
47
|
decision: routingDecisionSchema,
|
|
43
48
|
role: routingEventRoleSchema.optional(),
|
|
44
|
-
reason:
|
|
45
|
-
upstreamStatus:
|
|
49
|
+
reason: z2.string().regex(/^[a-z][a-z0-9_]{0,63}$/).optional(),
|
|
50
|
+
upstreamStatus: z2.number().int().min(100).max(599).optional()
|
|
46
51
|
}).strict();
|
|
47
|
-
var routingEventsSchema =
|
|
52
|
+
var routingEventsSchema = z2.array(routingEventSchema).max(1000);
|
|
48
53
|
// src/domain.ts
|
|
49
|
-
var harnessSchema =
|
|
50
|
-
var protocolSchema =
|
|
51
|
-
var idSchema =
|
|
52
|
-
var label =
|
|
53
|
-
var envRef =
|
|
54
|
+
var harnessSchema = z3.enum(["claude", "codex", "grok", "opencode", "opencode2", "pi", "omp", "dsh", "cline", "hermes", "prime-agent", "gemini", "aider", "kilo"]);
|
|
55
|
+
var protocolSchema = z3.enum(["anthropic-messages", "openai-responses", "openai-chat", "gemini-generate-content"]);
|
|
56
|
+
var idSchema = z3.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,79}$/);
|
|
57
|
+
var label = z3.string().min(1).max(200);
|
|
58
|
+
var envRef = z3.string().regex(/^SWITCHER_PROVIDER_[A-Z0-9_]+$/);
|
|
54
59
|
function endpoint(value) {
|
|
55
60
|
let url;
|
|
56
61
|
try {
|
|
@@ -63,85 +68,86 @@ function endpoint(value) {
|
|
|
63
68
|
throw new Fault(400, "invalid_url", "URL must use HTTPS, contain no credentials/query/fragment, or use HTTP on loopback.");
|
|
64
69
|
return url.href.replace(/\/+$/, "");
|
|
65
70
|
}
|
|
66
|
-
var urlSchema =
|
|
71
|
+
var urlSchema = z3.string().max(2000).superRefine((v, ctx) => {
|
|
67
72
|
try {
|
|
68
73
|
endpoint(v);
|
|
69
74
|
} catch {
|
|
70
75
|
ctx.addIssue({ code: "custom", message: "Invalid endpoint URL" });
|
|
71
76
|
}
|
|
72
77
|
}).transform(endpoint);
|
|
73
|
-
var expiresOnSchema =
|
|
78
|
+
var expiresOnSchema = z3.string().regex(/^\d{4}-\d{2}-\d{2}$/).refine((value) => {
|
|
74
79
|
const day = new Date(`${value}T00:00:00.000Z`);
|
|
75
80
|
return Number.isFinite(day.getTime()) && day.toISOString().slice(0, 10) === value;
|
|
76
81
|
}, "Use a real calendar date in YYYY-MM-DD format.");
|
|
77
|
-
var modelSchema =
|
|
78
|
-
id:
|
|
82
|
+
var modelSchema = z3.object({
|
|
83
|
+
id: z3.string().min(1).max(300).refine((value) => value === value.trim() && !/[\x00-\x1f\x7f-\x9f]/.test(value), "Use a nonblank model ID without surrounding whitespace or control characters."),
|
|
79
84
|
name: label,
|
|
80
|
-
description:
|
|
81
|
-
available:
|
|
85
|
+
description: z3.string().max(8000).optional(),
|
|
86
|
+
available: z3.boolean().optional(),
|
|
82
87
|
expiresOn: expiresOnSchema.optional(),
|
|
83
|
-
contextWindow:
|
|
84
|
-
maxOutputTokens:
|
|
85
|
-
inputModalities:
|
|
86
|
-
outputModalities:
|
|
87
|
-
supportedParameters:
|
|
88
|
-
|
|
88
|
+
contextWindow: z3.number().int().positive().optional(),
|
|
89
|
+
maxOutputTokens: z3.number().int().positive().optional(),
|
|
90
|
+
inputModalities: z3.array(z3.string().max(50)).max(20).optional(),
|
|
91
|
+
outputModalities: z3.array(z3.string().max(50)).max(20).optional(),
|
|
92
|
+
supportedParameters: z3.array(z3.string().max(100)).max(100).optional(),
|
|
93
|
+
reasoningEfforts: z3.array(reasoningEffortSchema).max(8).optional(),
|
|
94
|
+
supportedGenerationMethods: z3.array(z3.string().min(1).max(100)).max(100).optional()
|
|
89
95
|
}).strict();
|
|
90
|
-
var providerInputSchema =
|
|
96
|
+
var providerInputSchema = z3.object({
|
|
91
97
|
id: idSchema,
|
|
92
98
|
name: label,
|
|
93
99
|
baseUrl: urlSchema,
|
|
94
100
|
protocol: protocolSchema,
|
|
95
101
|
credentialEnv: envRef.optional(),
|
|
96
|
-
authStyle:
|
|
102
|
+
authStyle: z3.enum(["bearer", "x-api-key", "api-key"]).default("bearer"),
|
|
97
103
|
catalogBaseUrl: urlSchema.optional(),
|
|
98
|
-
catalogFormat:
|
|
99
|
-
catalogAuthStyle:
|
|
104
|
+
catalogFormat: z3.enum(["openai", "ollama", "mistral", "together", "fireworks", "dashscope", "gemini", "none"]).optional(),
|
|
105
|
+
catalogAuthStyle: z3.enum(["bearer", "x-api-key", "api-key", "none"]).optional(),
|
|
100
106
|
catalogCredentialEnv: envRef.optional(),
|
|
101
|
-
catalogAccountId:
|
|
102
|
-
modelsPath:
|
|
103
|
-
manualModels:
|
|
104
|
-
additionalModels:
|
|
107
|
+
catalogAccountId: z3.string().regex(/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/).optional(),
|
|
108
|
+
modelsPath: z3.string().regex(/^[a-zA-Z0-9_/-]+$/).max(200).default("models"),
|
|
109
|
+
manualModels: z3.array(modelSchema).max(1e4).default([]),
|
|
110
|
+
additionalModels: z3.array(modelSchema).max(1e4).optional()
|
|
105
111
|
}).strict().refine((p) => !p.modelsPath.split("/").includes("..") && !p.modelsPath.startsWith("/"), "modelsPath must be relative");
|
|
106
|
-
var providerPresetSchema =
|
|
112
|
+
var providerPresetSchema = z3.object({
|
|
107
113
|
id: idSchema,
|
|
108
114
|
name: label,
|
|
109
115
|
credentialEnv: envRef.optional(),
|
|
110
|
-
credentialAliases:
|
|
111
|
-
protocols:
|
|
116
|
+
credentialAliases: z3.array(z3.string().regex(/^[A-Z][A-Z0-9_]+$/)),
|
|
117
|
+
protocols: z3.array(z3.object({
|
|
112
118
|
protocol: protocolSchema,
|
|
113
119
|
baseUrl: urlSchema.optional(),
|
|
114
|
-
authStyle:
|
|
120
|
+
authStyle: z3.enum(["bearer", "x-api-key", "api-key"]),
|
|
115
121
|
catalogBaseUrl: urlSchema.optional(),
|
|
116
|
-
catalogFormat:
|
|
117
|
-
catalogAuthStyle:
|
|
118
|
-
modelsPath:
|
|
119
|
-
notes:
|
|
122
|
+
catalogFormat: z3.enum(["openai", "ollama", "mistral", "together", "fireworks", "dashscope", "gemini", "none"]),
|
|
123
|
+
catalogAuthStyle: z3.enum(["bearer", "x-api-key", "api-key", "none"]).optional(),
|
|
124
|
+
modelsPath: z3.string(),
|
|
125
|
+
notes: z3.array(z3.string())
|
|
120
126
|
}).strict()).min(1),
|
|
121
|
-
sources:
|
|
122
|
-
verification:
|
|
127
|
+
sources: z3.array(z3.string().url()),
|
|
128
|
+
verification: z3.literal("documented")
|
|
123
129
|
}).strict();
|
|
124
|
-
var profileInputSchema =
|
|
130
|
+
var profileInputSchema = z3.object({
|
|
125
131
|
id: idSchema,
|
|
126
132
|
name: label,
|
|
127
133
|
providerId: idSchema,
|
|
128
134
|
harness: harnessSchema,
|
|
129
|
-
model:
|
|
135
|
+
model: z3.string().min(1).max(300),
|
|
130
136
|
modelPolicy: modelPolicySchema.optional()
|
|
131
137
|
}).strict();
|
|
132
|
-
var runInputSchema =
|
|
133
|
-
modelPolicyVersion:
|
|
138
|
+
var runInputSchema = z3.object({
|
|
139
|
+
modelPolicyVersion: z3.literal(1),
|
|
134
140
|
profileId: idSchema,
|
|
135
141
|
harness: harnessSchema,
|
|
136
|
-
model:
|
|
142
|
+
model: z3.string().min(1).max(300),
|
|
137
143
|
modelPolicy: modelPolicySchema.optional(),
|
|
138
|
-
planToken:
|
|
144
|
+
planToken: z3.string().regex(/^[a-f0-9]{64}$/)
|
|
139
145
|
}).strict();
|
|
140
|
-
var runUpdateSchema =
|
|
141
|
-
status:
|
|
142
|
-
exitCode:
|
|
146
|
+
var runUpdateSchema = z3.object({
|
|
147
|
+
status: z3.enum(["exited", "failed", "interrupted"]),
|
|
148
|
+
exitCode: z3.number().int().min(0).max(255),
|
|
143
149
|
routingEvents: routingEventsSchema.optional(),
|
|
144
|
-
routingEventsDropped:
|
|
150
|
+
routingEventsDropped: z3.number().int().min(0).max(1e6).optional()
|
|
145
151
|
}).strict();
|
|
146
152
|
|
|
147
153
|
class Fault extends Error {
|
|
@@ -230,8 +236,9 @@ var preset = (id, name, protocols, sources, alias) => parse(providerPresetSchema
|
|
|
230
236
|
var providerPresets = [
|
|
231
237
|
preset("deepseek", "DeepSeek", [
|
|
232
238
|
route("openai-chat", "https://api.deepseek.com", { catalogBaseUrl: "https://api.deepseek.com" }),
|
|
239
|
+
route("openai-responses", "https://api.deepseek.com", { catalogBaseUrl: "https://api.deepseek.com" }),
|
|
233
240
|
route("anthropic-messages", "https://api.deepseek.com/anthropic/v1", { catalogBaseUrl: "https://api.deepseek.com" })
|
|
234
|
-
], ["https://api-docs.deepseek.com/guides/anthropic_api", "https://api-docs.deepseek.com/api/list-models"], "DEEPSEEK_API_KEY"),
|
|
241
|
+
], ["https://api-docs.deepseek.com/guides/anthropic_api", "https://api-docs.deepseek.com/api/list-models", "https://api-docs.deepseek.com/updates/", "https://api-docs.deepseek.com/guides/responses_api/"], "DEEPSEEK_API_KEY"),
|
|
235
242
|
preset("openrouter", "OpenRouter", ["openai-chat", "openai-responses", "anthropic-messages"].map((protocol) => route(protocol, "https://openrouter.ai/api/v1", { catalogAuthStyle: "none" })), ["https://openrouter.ai/docs/api/api-reference/models/list-all-models-and-their-properties", "https://openrouter.ai/docs/guides/overview"], "OPENROUTER_API_KEY"),
|
|
236
243
|
preset("anthropic", "Anthropic", [route("anthropic-messages", "https://api.anthropic.com/v1", { authStyle: "x-api-key" })], ["https://platform.claude.com/docs/en/api/overview", "https://platform.claude.com/docs/en/api/models/list"], "ANTHROPIC_API_KEY"),
|
|
237
244
|
preset("gemini", "Google Gemini", [route("gemini-generate-content", "https://generativelanguage.googleapis.com/v1beta", {
|
|
@@ -313,7 +320,7 @@ function providerFromPreset(presetId, options = {}) {
|
|
|
313
320
|
const suffix = selected.protocol === "anthropic-messages" ? "messages" : selected.protocol === "openai-responses" ? "responses" : "chat";
|
|
314
321
|
if (presetId === "fireworks" && !options.catalogBaseUrl && !options.catalogAccountId)
|
|
315
322
|
throw new Fault(400, "catalog_account_required", "Fireworks model discovery requires --catalog-account-id or an explicit --catalog-url.");
|
|
316
|
-
if (selected.catalogFormat === "none" && options.catalogFormat && !options.catalogBaseUrl)
|
|
323
|
+
if (selected.catalogFormat === "none" && options.catalogFormat && options.catalogFormat !== "none" && !options.catalogBaseUrl)
|
|
317
324
|
throw new Fault(400, "catalog_url_required", "This preset requires an explicit --catalog-url when enabling a catalog parser.");
|
|
318
325
|
const catalogBaseUrl = options.catalogBaseUrl ?? (presetId === "fireworks" && options.catalogAccountId ? `https://api.fireworks.ai/v1/accounts/${encodeURIComponent(options.catalogAccountId)}` : options.baseUrl ? undefined : selected.catalogBaseUrl);
|
|
319
326
|
return parse(providerInputSchema, {
|
|
@@ -330,12 +337,106 @@ function providerFromPreset(presetId, options = {}) {
|
|
|
330
337
|
catalogAccountId: options.catalogAccountId,
|
|
331
338
|
modelsPath: options.modelsPath ?? selected.modelsPath,
|
|
332
339
|
additionalModels: presetId === "deepseek" && new URL(baseUrl).origin === "https://api.deepseek.com" ? [{
|
|
333
|
-
id: "deepseek-
|
|
334
|
-
name: "DeepSeek V4.1 Flash
|
|
335
|
-
|
|
340
|
+
id: "deepseek-flash",
|
|
341
|
+
name: "DeepSeek V4.1 Flash",
|
|
342
|
+
inputModalities: ["text", "image"],
|
|
343
|
+
outputModalities: ["text"],
|
|
344
|
+
supportedParameters: ["tools"]
|
|
336
345
|
}] : []
|
|
337
346
|
});
|
|
338
347
|
}
|
|
348
|
+
// src/desktop-apps.ts
|
|
349
|
+
import { execFile } from "node:child_process";
|
|
350
|
+
import { access, stat } from "node:fs/promises";
|
|
351
|
+
import { constants } from "node:fs";
|
|
352
|
+
import { homedir } from "node:os";
|
|
353
|
+
import { isAbsolute, join } from "node:path";
|
|
354
|
+
import { promisify } from "node:util";
|
|
355
|
+
var execute = promisify(execFile);
|
|
356
|
+
var host = {
|
|
357
|
+
platform: process.platform,
|
|
358
|
+
home: homedir(),
|
|
359
|
+
async run(command, args) {
|
|
360
|
+
const { stdout } = await execute(command, args, { encoding: "utf8", timeout: 8000, maxBuffer: 64 * 1024, env: { PATH: "/usr/bin:/bin", HOME: homedir() } });
|
|
361
|
+
return stdout.trim();
|
|
362
|
+
},
|
|
363
|
+
async executable(path) {
|
|
364
|
+
try {
|
|
365
|
+
await access(path, constants.X_OK);
|
|
366
|
+
return (await stat(path)).isFile();
|
|
367
|
+
} catch {
|
|
368
|
+
return false;
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
async isDirectory(path) {
|
|
372
|
+
try {
|
|
373
|
+
return (await stat(path)).isDirectory();
|
|
374
|
+
} catch {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
async function detectChatGPTApp(appPath, system = host) {
|
|
380
|
+
if (system.platform !== "darwin")
|
|
381
|
+
throw new Fault(422, "unsupported_platform", "ChatGPT provider launches currently require the macOS desktop app.");
|
|
382
|
+
let candidates;
|
|
383
|
+
if (appPath !== undefined)
|
|
384
|
+
candidates = [appPath];
|
|
385
|
+
else {
|
|
386
|
+
candidates = ["ChatGPT.app", "Codex.app"].flatMap((name) => [join(system.home, "Applications", name), join("/Applications", name)]);
|
|
387
|
+
try {
|
|
388
|
+
const found = await system.run("/usr/bin/osascript", ["-l", "JavaScript", "-e", 'ObjC.import("AppKit"); const url = $.NSWorkspace.sharedWorkspace.URLForApplicationWithBundleIdentifier("com.openai.codex"); url.isNil() ? "" : ObjC.unwrap(url.path);']);
|
|
389
|
+
if (found)
|
|
390
|
+
candidates.push(found);
|
|
391
|
+
} catch {}
|
|
392
|
+
}
|
|
393
|
+
for (const path of new Set(candidates)) {
|
|
394
|
+
if (!isAbsolute(path) || !path.endsWith(".app") || !await system.isDirectory(path))
|
|
395
|
+
continue;
|
|
396
|
+
const plist = join(path, "Contents/Info.plist");
|
|
397
|
+
const field = (name) => system.run("/usr/bin/plutil", ["-extract", name, "raw", "-o", "-", plist]);
|
|
398
|
+
try {
|
|
399
|
+
const bundleId = await field("CFBundleIdentifier");
|
|
400
|
+
if (bundleId !== "com.openai.codex")
|
|
401
|
+
continue;
|
|
402
|
+
const name = await field("CFBundleExecutable"), version = await field("CFBundleShortVersionString");
|
|
403
|
+
if (!/^[A-Za-z0-9 _-]+$/.test(name))
|
|
404
|
+
continue;
|
|
405
|
+
const executable = join(path, "Contents/MacOS", name), codexExecutable = join(path, "Contents/Resources/codex");
|
|
406
|
+
if (await system.executable(executable) && await system.executable(codexExecutable))
|
|
407
|
+
return { path, executable, codexExecutable, bundleId, version };
|
|
408
|
+
} catch {}
|
|
409
|
+
}
|
|
410
|
+
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.");
|
|
411
|
+
}
|
|
412
|
+
async function detectClaudeDesktopApp(appPath, system = host) {
|
|
413
|
+
if (system.platform !== "darwin")
|
|
414
|
+
throw new Fault(422, "unsupported_platform", "Claude desktop provider launches currently require macOS.");
|
|
415
|
+
const candidates = appPath === undefined ? [join(system.home, "Applications/Claude.app"), "/Applications/Claude.app"] : [appPath];
|
|
416
|
+
if (appPath === undefined)
|
|
417
|
+
try {
|
|
418
|
+
const found = await system.run("/usr/bin/osascript", ["-l", "JavaScript", "-e", 'ObjC.import("AppKit"); const url = $.NSWorkspace.sharedWorkspace.URLForApplicationWithBundleIdentifier("com.anthropic.claudefordesktop"); url.isNil() ? "" : ObjC.unwrap(url.path);']);
|
|
419
|
+
if (found)
|
|
420
|
+
candidates.push(found);
|
|
421
|
+
} catch {}
|
|
422
|
+
for (const path of new Set(candidates)) {
|
|
423
|
+
if (!isAbsolute(path) || !path.endsWith(".app") || !await system.isDirectory(path))
|
|
424
|
+
continue;
|
|
425
|
+
const field = (name) => system.run("/usr/bin/plutil", ["-extract", name, "raw", "-o", "-", join(path, "Contents/Info.plist")]);
|
|
426
|
+
try {
|
|
427
|
+
const bundleId = await field("CFBundleIdentifier");
|
|
428
|
+
if (bundleId !== "com.anthropic.claudefordesktop")
|
|
429
|
+
continue;
|
|
430
|
+
const name = await field("CFBundleExecutable"), version = await field("CFBundleShortVersionString");
|
|
431
|
+
if (!/^[A-Za-z0-9 _-]+$/.test(name))
|
|
432
|
+
continue;
|
|
433
|
+
const executable = join(path, "Contents/MacOS", name);
|
|
434
|
+
if (await system.executable(executable))
|
|
435
|
+
return { path, executable, bundleId, version };
|
|
436
|
+
} catch {}
|
|
437
|
+
}
|
|
438
|
+
throw new Fault(404, "app_not_installed", "Install the current Claude macOS desktop app or supply its absolute .app path with --app-path.");
|
|
439
|
+
}
|
|
339
440
|
|
|
340
441
|
// src/sdk.ts
|
|
341
442
|
class SwitcherError extends Error {
|
|
@@ -483,6 +584,33 @@ class SwitcherClient {
|
|
|
483
584
|
deleteProvider(id, version, idempotencyKey) {
|
|
484
585
|
return this.request("DELETE", `/v1/providers/${encodeURIComponent(id)}`, undefined, { version, idempotencyKey });
|
|
485
586
|
}
|
|
587
|
+
async editModel(providerId, operation, value) {
|
|
588
|
+
const model = typeof value === "string" ? undefined : parse(modelSchema, value);
|
|
589
|
+
const id = model?.id ?? parse(modelSchema.shape.id, value);
|
|
590
|
+
const provider = await this.getProvider(providerId);
|
|
591
|
+
const { version, updatedAt, ...input } = provider;
|
|
592
|
+
const manual = provider.manualModels.some((item) => item.id === id);
|
|
593
|
+
const additional = (provider.additionalModels ?? []).some((item) => item.id === id);
|
|
594
|
+
if (operation === "add" && (manual || additional))
|
|
595
|
+
throw new SwitcherError(409, "model_exists", "This model is already configured. Use models update to replace its metadata.");
|
|
596
|
+
if (operation !== "add" && !manual && !additional)
|
|
597
|
+
throw new SwitcherError(404, "model_not_configured", "This model has no saved metadata. Use models add to configure a catalog model; remote catalog entries cannot be deleted locally.");
|
|
598
|
+
const useManual = manual || !additional && (provider.manualModels.length > 0 || provider.catalogFormat === "none");
|
|
599
|
+
const manualModels = provider.manualModels.filter((item) => item.id !== id);
|
|
600
|
+
const additionalModels = (provider.additionalModels ?? []).filter((item) => item.id !== id);
|
|
601
|
+
if (operation !== "remove")
|
|
602
|
+
(useManual ? manualModels : additionalModels).push(model);
|
|
603
|
+
return this.updateProvider({ ...input, manualModels, additionalModels }, version);
|
|
604
|
+
}
|
|
605
|
+
addModel(providerId, model) {
|
|
606
|
+
return this.editModel(providerId, "add", model);
|
|
607
|
+
}
|
|
608
|
+
updateModel(providerId, model) {
|
|
609
|
+
return this.editModel(providerId, "update", model);
|
|
610
|
+
}
|
|
611
|
+
removeModel(providerId, modelId) {
|
|
612
|
+
return this.editModel(providerId, "remove", modelId);
|
|
613
|
+
}
|
|
486
614
|
refreshModels(id, idempotencyKey) {
|
|
487
615
|
return this.request("POST", `/v1/providers/${encodeURIComponent(id)}/refresh`, {}, { idempotencyKey });
|
|
488
616
|
}
|
|
@@ -525,6 +653,8 @@ function clientFromEnv(env = process.env, options = {}) {
|
|
|
525
653
|
}
|
|
526
654
|
export {
|
|
527
655
|
providerFromPreset,
|
|
656
|
+
detectClaudeDesktopApp,
|
|
657
|
+
detectChatGPTApp,
|
|
528
658
|
clientFromEnv,
|
|
529
659
|
SwitcherError,
|
|
530
660
|
SwitcherClient
|