|
| 1 | +// GENERATED — do not edit. Source of truth: mothership worker packages/contracts/src/model-router.ts |
| 2 | +// Regenerate with `bun run contracts:sync` in the worker. |
| 3 | + |
| 4 | +import { z } from "zod"; |
| 5 | + |
| 6 | +/** |
| 7 | + * POST /api/model-router: the stateless classifier Sim calls before running a block whose model |
| 8 | + * is "auto". Sim sends compact signals about the pending task and its ordered candidates; the |
| 9 | + * worker answers with one candidate id. Sim owns what the ids mean and which model serves each. |
| 10 | + * Internal-key only: customer keys never reach it. |
| 11 | + */ |
| 12 | +export const ModelRouterCandidate = z.strictObject({ |
| 13 | + /** The token the classifier answers with, e.g. "1". */ |
| 14 | + id: z.string().trim().min(1).max(32).regex(/^\S+$/, "a candidate id is a single token"), |
| 15 | + /** One line saying when this candidate fits. */ |
| 16 | + hint: z.string().max(500), |
| 17 | +}); |
| 18 | +export type ModelRouterCandidate = z.infer<typeof ModelRouterCandidate>; |
| 19 | + |
| 20 | +/** Signals, never payloads: the text fields are excerpts of untrusted user content. */ |
| 21 | +export const ModelRouterSignals = z.strictObject({ |
| 22 | + systemPrompt: z.string().optional(), |
| 23 | + lastMessage: z.string().optional(), |
| 24 | + messageCount: z.number().int().nonnegative().optional(), |
| 25 | + toolNames: z.array(z.string()).optional(), |
| 26 | + hasMedia: z.boolean().optional(), |
| 27 | + hasResponseFormat: z.boolean().optional(), |
| 28 | + approxInputTokens: z.number().int().nonnegative().optional(), |
| 29 | +}); |
| 30 | +export type ModelRouterSignals = z.infer<typeof ModelRouterSignals>; |
| 31 | + |
| 32 | +export const ModelRouterRequest = z.strictObject({ |
| 33 | + signals: ModelRouterSignals, |
| 34 | + /** Ascending by capability and cost; ties break toward the later candidate. */ |
| 35 | + candidates: z |
| 36 | + .array(ModelRouterCandidate) |
| 37 | + .min(2) |
| 38 | + .max(8) |
| 39 | + .refine( |
| 40 | + (candidates) => new Set(candidates.map((c) => c.id.toLowerCase())).size === candidates.length, |
| 41 | + "candidate ids must be unique", |
| 42 | + ), |
| 43 | + /** Attribution for metering only; never part of the decision. */ |
| 44 | + workspaceId: z.string().max(200).optional(), |
| 45 | + userId: z.string().max(200).optional(), |
| 46 | + workflowId: z.string().max(200).optional(), |
| 47 | + blockId: z.string().max(200).optional(), |
| 48 | + executionId: z.string().max(200).optional(), |
| 49 | +}); |
| 50 | +export type ModelRouterRequest = z.infer<typeof ModelRouterRequest>; |
| 51 | + |
| 52 | +/** "llm": the classifier named a candidate. "fallback": it did not, so the upper-middle |
| 53 | + * candidate was chosen; the call still ran and its usage is still reported. */ |
| 54 | +export type ModelRouterDecidedBy = "llm" | "fallback"; |
| 55 | + |
| 56 | +/** The classifier call's own usage and list-price cost in USD, priced from the worker's catalog. */ |
| 57 | +export interface ModelRouterUsage { |
| 58 | + model: string; |
| 59 | + inputTokens: number; |
| 60 | + cachedInputTokens: number; |
| 61 | + outputTokens: number; |
| 62 | + cost: number; |
| 63 | +} |
| 64 | + |
| 65 | +export interface ModelRouterResponse { |
| 66 | + choice: string; |
| 67 | + decidedBy: ModelRouterDecidedBy; |
| 68 | + usage: ModelRouterUsage; |
| 69 | + /** Whether Sim charges the user for this call. Sim treats a missing or false value as no. */ |
| 70 | + billable: boolean; |
| 71 | +} |
0 commit comments