@hasna/switcher 0.1.2 → 0.1.3

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/dist/cli.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  #!/usr/bin/env bun
2
+ import { type ModelPolicy } from "./domain";
3
+ export declare function readModelPolicy(file: string | undefined, roleValues: string[] | undefined): Promise<ModelPolicy | undefined>;
2
4
  export declare function main(args?: string[]): Promise<void>;
@@ -0,0 +1,65 @@
1
+ import { type ModelPolicy } from "./model-policy-schema";
2
+ type Dict = Record<string, unknown>;
3
+ /** Keys which can redirect a Codex request outside Switcher's launch provider. */
4
+ export declare const codexRoutingKeys: Set<string>;
5
+ export declare const codexTransportKeys: Set<string>;
6
+ export type CodexAgentInput = {
7
+ description?: string;
8
+ config_file?: string;
9
+ nickname_candidates?: string[];
10
+ /** Parsed contents of config_file. The caller reads native config layers. */
11
+ config?: Dict;
12
+ trusted?: boolean;
13
+ };
14
+ export type CodexModelPolicyInput = {
15
+ model: string;
16
+ policy?: ModelPolicy;
17
+ /** Effective native global/project config, after Codex precedence is applied. */
18
+ effectiveConfig?: Dict;
19
+ agents?: Record<string, CodexAgentInput>;
20
+ switcherProvider: string;
21
+ switcherBaseUrl: string;
22
+ /** Project config is never activated unless the caller has established trust. */
23
+ trustedProject?: boolean;
24
+ };
25
+ export type CodexAgentOverride = {
26
+ name: string;
27
+ model: string;
28
+ config: Dict;
29
+ sourceConfigFile?: string;
30
+ trusted: boolean;
31
+ };
32
+ export type CodexModelPolicyResult = {
33
+ model: string;
34
+ subagentModel: string;
35
+ reviewModel: string;
36
+ provider: {
37
+ name: string;
38
+ base_url: string;
39
+ };
40
+ /** Structured -c values; the launcher serializes these as TOML. */
41
+ overrides: Record<string, unknown>;
42
+ agents: CodexAgentOverride[];
43
+ ignoredUntrustedAgents: string[];
44
+ };
45
+ /** Extract named role declarations from an already merged native config layer. */
46
+ export declare function codexAgentsFromEffectiveConfig(config: Dict, trustedProject?: boolean, baseDir?: string): Record<string, CodexAgentInput>;
47
+ /** Compile only routing controls; native instructions, permissions and descriptions remain intact. */
48
+ export declare function compileCodexModelPolicy(input: CodexModelPolicyInput): CodexModelPolicyResult;
49
+ /** Small deterministic TOML writer for the copied, sanitized agent config layer. */
50
+ export declare function renderCodexAgentToml(config: Dict): string;
51
+ /** Copy sanitized role layers into the per-launch state directory. */
52
+ export declare function writeCodexAgentOverrides(result: CodexModelPolicyResult, stateDir: string): Promise<Record<string, string>>;
53
+ export type PrepareCodexModelPolicyInput = Omit<CodexModelPolicyInput, "effectiveConfig" | "agents"> & {
54
+ cwd: string;
55
+ stateDir: string;
56
+ home?: string;
57
+ };
58
+ /** Load global Codex config plus trusted .codex/config.toml ancestors, then prepare role layers. */
59
+ export declare function prepareCodexModelPolicy(input: PrepareCodexModelPolicyInput): Promise<CodexModelPolicyResult & {
60
+ agentConfigPaths: Record<string, string>;
61
+ configPaths: string[];
62
+ }>;
63
+ /** Read a referenced role layer without activating it; callers must establish project trust. */
64
+ export declare function readCodexAgentConfig(path: string): Promise<Dict>;
65
+ export {};
@@ -1,6 +1,6 @@
1
1
  import { SwitcherClient, type Provider, type Profile } from "./sdk";
2
- import { type Model } from "./domain";
2
+ import { type Model, type ModelPolicy } from "./domain";
3
3
  import { type PresetOptions } from "./presets";
4
4
  export declare function resolveLaunchProvider(client: SwitcherClient, selector: string, options?: PresetOptions): Promise<Provider>;
5
5
  export declare function selectModel(models: Model[], query?: string, harness?: Profile["harness"]): Promise<string>;
6
- export declare function ensureLaunchProfile(client: SwitcherClient, provider: Provider, harness: Profile["harness"], model: string): Promise<Profile>;
6
+ export declare function ensureLaunchProfile(client: SwitcherClient, provider: Provider, harness: Profile["harness"], model: string, modelPolicy?: ModelPolicy): Promise<Profile>;
package/dist/domain.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import { z } from "zod";
2
+ import { type RoutingEvent } from "./model-policy-schema";
3
+ export { modelPolicySchema, routingEventSchema, routingEventsSchema } from "./model-policy-schema";
4
+ export type { ModelPolicy, RoutingEvent } from "./model-policy-schema";
2
5
  export type { AuthStyle } from "./auth";
3
- export declare const VERSION = "0.1.2";
6
+ export declare const VERSION = "0.1.3";
4
7
  export declare const harnessSchema: z.ZodEnum<["claude", "codex", "grok", "opencode", "opencode2", "pi", "omp", "dsh", "cline", "hermes", "prime-agent", "gemini", "aider", "kilo"]>;
5
8
  export declare const protocolSchema: z.ZodEnum<["anthropic-messages", "openai-responses", "openai-chat", "gemini-generate-content"]>;
6
9
  export declare const idSchema: z.ZodString;
@@ -264,44 +267,295 @@ export declare const profileInputSchema: z.ZodObject<{
264
267
  providerId: z.ZodString;
265
268
  harness: z.ZodEnum<["claude", "codex", "grok", "opencode", "opencode2", "pi", "omp", "dsh", "cline", "hermes", "prime-agent", "gemini", "aider", "kilo"]>;
266
269
  model: z.ZodString;
270
+ modelPolicy: z.ZodOptional<z.ZodObject<{
271
+ version: z.ZodDefault<z.ZodLiteral<1>>;
272
+ roles: z.ZodOptional<z.ZodObject<{
273
+ subagent: z.ZodOptional<z.ZodString>;
274
+ fast: z.ZodOptional<z.ZodString>;
275
+ planning: z.ZodOptional<z.ZodString>;
276
+ review: z.ZodOptional<z.ZodString>;
277
+ summary: z.ZodOptional<z.ZodString>;
278
+ compaction: z.ZodOptional<z.ZodString>;
279
+ weak: z.ZodOptional<z.ZodString>;
280
+ editor: z.ZodOptional<z.ZodString>;
281
+ }, "strict", z.ZodTypeAny, {
282
+ subagent?: string | undefined;
283
+ fast?: string | undefined;
284
+ planning?: string | undefined;
285
+ review?: string | undefined;
286
+ summary?: string | undefined;
287
+ compaction?: string | undefined;
288
+ weak?: string | undefined;
289
+ editor?: string | undefined;
290
+ }, {
291
+ subagent?: string | undefined;
292
+ fast?: string | undefined;
293
+ planning?: string | undefined;
294
+ review?: string | undefined;
295
+ summary?: string | undefined;
296
+ compaction?: string | undefined;
297
+ weak?: string | undefined;
298
+ editor?: string | undefined;
299
+ }>>;
300
+ allowedModels: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
301
+ aliases: z.ZodOptional<z.ZodEffects<z.ZodRecord<z.ZodEffects<z.ZodString, string, string>, z.ZodString>, Record<string, string>, Record<string, string>>>;
302
+ fallbacks: z.ZodOptional<z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString, "many">>, Record<string, string[]>, Record<string, string[]>>>;
303
+ }, "strict", z.ZodTypeAny, {
304
+ version: 1;
305
+ roles?: {
306
+ subagent?: string | undefined;
307
+ fast?: string | undefined;
308
+ planning?: string | undefined;
309
+ review?: string | undefined;
310
+ summary?: string | undefined;
311
+ compaction?: string | undefined;
312
+ weak?: string | undefined;
313
+ editor?: string | undefined;
314
+ } | undefined;
315
+ allowedModels?: string[] | undefined;
316
+ aliases?: Record<string, string> | undefined;
317
+ fallbacks?: Record<string, string[]> | undefined;
318
+ }, {
319
+ version?: 1 | undefined;
320
+ roles?: {
321
+ subagent?: string | undefined;
322
+ fast?: string | undefined;
323
+ planning?: string | undefined;
324
+ review?: string | undefined;
325
+ summary?: string | undefined;
326
+ compaction?: string | undefined;
327
+ weak?: string | undefined;
328
+ editor?: string | undefined;
329
+ } | undefined;
330
+ allowedModels?: string[] | undefined;
331
+ aliases?: Record<string, string> | undefined;
332
+ fallbacks?: Record<string, string[]> | undefined;
333
+ }>>;
267
334
  }, "strict", z.ZodTypeAny, {
268
335
  id: string;
269
336
  name: string;
270
337
  providerId: string;
271
338
  harness: "gemini" | "claude" | "codex" | "grok" | "opencode" | "opencode2" | "pi" | "omp" | "dsh" | "cline" | "hermes" | "prime-agent" | "aider" | "kilo";
272
339
  model: string;
340
+ modelPolicy?: {
341
+ version: 1;
342
+ roles?: {
343
+ subagent?: string | undefined;
344
+ fast?: string | undefined;
345
+ planning?: string | undefined;
346
+ review?: string | undefined;
347
+ summary?: string | undefined;
348
+ compaction?: string | undefined;
349
+ weak?: string | undefined;
350
+ editor?: string | undefined;
351
+ } | undefined;
352
+ allowedModels?: string[] | undefined;
353
+ aliases?: Record<string, string> | undefined;
354
+ fallbacks?: Record<string, string[]> | undefined;
355
+ } | undefined;
273
356
  }, {
274
357
  id: string;
275
358
  name: string;
276
359
  providerId: string;
277
360
  harness: "gemini" | "claude" | "codex" | "grok" | "opencode" | "opencode2" | "pi" | "omp" | "dsh" | "cline" | "hermes" | "prime-agent" | "aider" | "kilo";
278
361
  model: string;
362
+ modelPolicy?: {
363
+ version?: 1 | undefined;
364
+ roles?: {
365
+ subagent?: string | undefined;
366
+ fast?: string | undefined;
367
+ planning?: string | undefined;
368
+ review?: string | undefined;
369
+ summary?: string | undefined;
370
+ compaction?: string | undefined;
371
+ weak?: string | undefined;
372
+ editor?: string | undefined;
373
+ } | undefined;
374
+ allowedModels?: string[] | undefined;
375
+ aliases?: Record<string, string> | undefined;
376
+ fallbacks?: Record<string, string[]> | undefined;
377
+ } | undefined;
279
378
  }>;
280
379
  export declare const runInputSchema: z.ZodObject<{
380
+ modelPolicyVersion: z.ZodLiteral<1>;
281
381
  profileId: z.ZodString;
282
382
  harness: z.ZodEnum<["claude", "codex", "grok", "opencode", "opencode2", "pi", "omp", "dsh", "cline", "hermes", "prime-agent", "gemini", "aider", "kilo"]>;
283
383
  model: z.ZodString;
384
+ modelPolicy: z.ZodOptional<z.ZodObject<{
385
+ version: z.ZodDefault<z.ZodLiteral<1>>;
386
+ roles: z.ZodOptional<z.ZodObject<{
387
+ subagent: z.ZodOptional<z.ZodString>;
388
+ fast: z.ZodOptional<z.ZodString>;
389
+ planning: z.ZodOptional<z.ZodString>;
390
+ review: z.ZodOptional<z.ZodString>;
391
+ summary: z.ZodOptional<z.ZodString>;
392
+ compaction: z.ZodOptional<z.ZodString>;
393
+ weak: z.ZodOptional<z.ZodString>;
394
+ editor: z.ZodOptional<z.ZodString>;
395
+ }, "strict", z.ZodTypeAny, {
396
+ subagent?: string | undefined;
397
+ fast?: string | undefined;
398
+ planning?: string | undefined;
399
+ review?: string | undefined;
400
+ summary?: string | undefined;
401
+ compaction?: string | undefined;
402
+ weak?: string | undefined;
403
+ editor?: string | undefined;
404
+ }, {
405
+ subagent?: string | undefined;
406
+ fast?: string | undefined;
407
+ planning?: string | undefined;
408
+ review?: string | undefined;
409
+ summary?: string | undefined;
410
+ compaction?: string | undefined;
411
+ weak?: string | undefined;
412
+ editor?: string | undefined;
413
+ }>>;
414
+ allowedModels: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
415
+ aliases: z.ZodOptional<z.ZodEffects<z.ZodRecord<z.ZodEffects<z.ZodString, string, string>, z.ZodString>, Record<string, string>, Record<string, string>>>;
416
+ fallbacks: z.ZodOptional<z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString, "many">>, Record<string, string[]>, Record<string, string[]>>>;
417
+ }, "strict", z.ZodTypeAny, {
418
+ version: 1;
419
+ roles?: {
420
+ subagent?: string | undefined;
421
+ fast?: string | undefined;
422
+ planning?: string | undefined;
423
+ review?: string | undefined;
424
+ summary?: string | undefined;
425
+ compaction?: string | undefined;
426
+ weak?: string | undefined;
427
+ editor?: string | undefined;
428
+ } | undefined;
429
+ allowedModels?: string[] | undefined;
430
+ aliases?: Record<string, string> | undefined;
431
+ fallbacks?: Record<string, string[]> | undefined;
432
+ }, {
433
+ version?: 1 | undefined;
434
+ roles?: {
435
+ subagent?: string | undefined;
436
+ fast?: string | undefined;
437
+ planning?: string | undefined;
438
+ review?: string | undefined;
439
+ summary?: string | undefined;
440
+ compaction?: string | undefined;
441
+ weak?: string | undefined;
442
+ editor?: string | undefined;
443
+ } | undefined;
444
+ allowedModels?: string[] | undefined;
445
+ aliases?: Record<string, string> | undefined;
446
+ fallbacks?: Record<string, string[]> | undefined;
447
+ }>>;
284
448
  planToken: z.ZodString;
285
449
  }, "strict", z.ZodTypeAny, {
286
450
  harness: "gemini" | "claude" | "codex" | "grok" | "opencode" | "opencode2" | "pi" | "omp" | "dsh" | "cline" | "hermes" | "prime-agent" | "aider" | "kilo";
287
451
  model: string;
452
+ modelPolicyVersion: 1;
288
453
  profileId: string;
289
454
  planToken: string;
455
+ modelPolicy?: {
456
+ version: 1;
457
+ roles?: {
458
+ subagent?: string | undefined;
459
+ fast?: string | undefined;
460
+ planning?: string | undefined;
461
+ review?: string | undefined;
462
+ summary?: string | undefined;
463
+ compaction?: string | undefined;
464
+ weak?: string | undefined;
465
+ editor?: string | undefined;
466
+ } | undefined;
467
+ allowedModels?: string[] | undefined;
468
+ aliases?: Record<string, string> | undefined;
469
+ fallbacks?: Record<string, string[]> | undefined;
470
+ } | undefined;
290
471
  }, {
291
472
  harness: "gemini" | "claude" | "codex" | "grok" | "opencode" | "opencode2" | "pi" | "omp" | "dsh" | "cline" | "hermes" | "prime-agent" | "aider" | "kilo";
292
473
  model: string;
474
+ modelPolicyVersion: 1;
293
475
  profileId: string;
294
476
  planToken: string;
477
+ modelPolicy?: {
478
+ version?: 1 | undefined;
479
+ roles?: {
480
+ subagent?: string | undefined;
481
+ fast?: string | undefined;
482
+ planning?: string | undefined;
483
+ review?: string | undefined;
484
+ summary?: string | undefined;
485
+ compaction?: string | undefined;
486
+ weak?: string | undefined;
487
+ editor?: string | undefined;
488
+ } | undefined;
489
+ allowedModels?: string[] | undefined;
490
+ aliases?: Record<string, string> | undefined;
491
+ fallbacks?: Record<string, string[]> | undefined;
492
+ } | undefined;
295
493
  }>;
296
494
  export declare const runUpdateSchema: z.ZodObject<{
297
495
  status: z.ZodEnum<["exited", "failed", "interrupted"]>;
298
496
  exitCode: z.ZodNumber;
497
+ routingEvents: z.ZodOptional<z.ZodArray<z.ZodObject<{
498
+ at: z.ZodString;
499
+ requestId: z.ZodString;
500
+ requestedModel: z.ZodString;
501
+ resolvedModel: z.ZodOptional<z.ZodString>;
502
+ reportedModel: z.ZodOptional<z.ZodString>;
503
+ decision: z.ZodEnum<["allow", "alias", "reject", "fallback"]>;
504
+ role: z.ZodOptional<z.ZodEnum<["main", "subagent", "fast", "planning", "review", "summary", "compaction", "weak", "editor"]>>;
505
+ reason: z.ZodOptional<z.ZodString>;
506
+ upstreamStatus: z.ZodOptional<z.ZodNumber>;
507
+ }, "strict", z.ZodTypeAny, {
508
+ at: string;
509
+ requestId: string;
510
+ requestedModel: string;
511
+ decision: "allow" | "alias" | "reject" | "fallback";
512
+ resolvedModel?: string | undefined;
513
+ reportedModel?: string | undefined;
514
+ role?: "main" | "subagent" | "fast" | "planning" | "review" | "summary" | "compaction" | "weak" | "editor" | undefined;
515
+ reason?: string | undefined;
516
+ upstreamStatus?: number | undefined;
517
+ }, {
518
+ at: string;
519
+ requestId: string;
520
+ requestedModel: string;
521
+ decision: "allow" | "alias" | "reject" | "fallback";
522
+ resolvedModel?: string | undefined;
523
+ reportedModel?: string | undefined;
524
+ role?: "main" | "subagent" | "fast" | "planning" | "review" | "summary" | "compaction" | "weak" | "editor" | undefined;
525
+ reason?: string | undefined;
526
+ upstreamStatus?: number | undefined;
527
+ }>, "many">>;
528
+ routingEventsDropped: z.ZodOptional<z.ZodNumber>;
299
529
  }, "strict", z.ZodTypeAny, {
300
530
  status: "exited" | "failed" | "interrupted";
301
531
  exitCode: number;
532
+ routingEvents?: {
533
+ at: string;
534
+ requestId: string;
535
+ requestedModel: string;
536
+ decision: "allow" | "alias" | "reject" | "fallback";
537
+ resolvedModel?: string | undefined;
538
+ reportedModel?: string | undefined;
539
+ role?: "main" | "subagent" | "fast" | "planning" | "review" | "summary" | "compaction" | "weak" | "editor" | undefined;
540
+ reason?: string | undefined;
541
+ upstreamStatus?: number | undefined;
542
+ }[] | undefined;
543
+ routingEventsDropped?: number | undefined;
302
544
  }, {
303
545
  status: "exited" | "failed" | "interrupted";
304
546
  exitCode: number;
547
+ routingEvents?: {
548
+ at: string;
549
+ requestId: string;
550
+ requestedModel: string;
551
+ decision: "allow" | "alias" | "reject" | "fallback";
552
+ resolvedModel?: string | undefined;
553
+ reportedModel?: string | undefined;
554
+ role?: "main" | "subagent" | "fast" | "planning" | "review" | "summary" | "compaction" | "weak" | "editor" | undefined;
555
+ reason?: string | undefined;
556
+ upstreamStatus?: number | undefined;
557
+ }[] | undefined;
558
+ routingEventsDropped?: number | undefined;
305
559
  }>;
306
560
  export type ProviderInput = z.input<typeof providerInputSchema>;
307
561
  export type Provider = z.output<typeof providerInputSchema> & {
@@ -314,7 +568,8 @@ export type Profile = ProfileInput & {
314
568
  updatedAt: string;
315
569
  };
316
570
  export type Model = z.infer<typeof modelSchema>;
317
- export type Run = z.infer<typeof runInputSchema> & {
571
+ export type Run = Omit<z.infer<typeof runInputSchema>, "modelPolicyVersion"> & {
572
+ modelPolicyVersion?: 1;
318
573
  providerId: string;
319
574
  providerVersion: number;
320
575
  profileVersion: number;
@@ -323,6 +578,8 @@ export type Run = z.infer<typeof runInputSchema> & {
323
578
  startedAt: string;
324
579
  endedAt?: string;
325
580
  exitCode?: number;
581
+ routingEvents?: RoutingEvent[];
582
+ routingEventsDropped?: number;
326
583
  version: number;
327
584
  updatedAt: string;
328
585
  };
@@ -0,0 +1,56 @@
1
+ import { type Model } from "./domain";
2
+ import type { CompiledModelPolicy } from "./model-policy";
3
+ /** The settings fragment understood by Gemini CLI 0.58.0. */
4
+ export type GeminiModelPolicyFragment = {
5
+ modelConfigs: {
6
+ customOverrides: Array<{
7
+ match: {
8
+ model: string;
9
+ };
10
+ modelConfig: {
11
+ model: string;
12
+ };
13
+ }>;
14
+ modelChains: Record<string, Array<{
15
+ model: string;
16
+ isLastResort: true;
17
+ }>>;
18
+ modelDefinitions: Record<string, {
19
+ displayName: string;
20
+ tier: "custom";
21
+ family: "switcher";
22
+ isPreview: false;
23
+ isVisible: true;
24
+ features: {
25
+ thinking: false;
26
+ multimodalToolUse: boolean;
27
+ };
28
+ }>;
29
+ modelIdResolutions: Record<string, {
30
+ default: string;
31
+ contexts: [];
32
+ }>;
33
+ classifierIdResolutions: Record<string, {
34
+ default: string;
35
+ contexts: [];
36
+ }>;
37
+ };
38
+ agents?: {
39
+ overrides: {
40
+ codebase_investigator: {
41
+ modelConfig: {
42
+ model: string;
43
+ };
44
+ };
45
+ };
46
+ };
47
+ };
48
+ /**
49
+ * Compile the portable Switcher policy into native Gemini 0.58.0 settings.
50
+ *
51
+ * Gemini's stable native route controls are modelConfigs.modelIdResolutions
52
+ * and agents.overrides.<agent>.modelConfig.model. Its modelConfigs.modelChains
53
+ * field receives terminal selected-model chains; the gateway owns explicit
54
+ * ordered fallback and retry semantics.
55
+ */
56
+ export declare function compileGeminiModelPolicy(selectedModel: string, models: readonly Model[], compiled: CompiledModelPolicy): GeminiModelPolicyFragment;