Skip to content

Commit 915d82c

Browse files
committed
fix(mothership): type the auto-model router call from the worker's model-router contract
The worker now serves POST /api/model-router again (mothership D116); only the Go service did, so every sim-auto block has fallen back to its default model since the cutover. The resolver takes its request and response types from the synced contract instead of a hand-kept copy.
1 parent 45ce934 commit 915d82c

2 files changed

Lines changed: 79 additions & 19 deletions

File tree

‎apps/sim/lib/model-router/resolve.ts‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { createLogger } from '@sim/logger'
33
import { getErrorMessage } from '@sim/utils/errors'
44
import { env } from '@/lib/core/config/env'
55
import { getCostMultiplier, isHosted } from '@/lib/core/config/env-flags'
6+
import type {
7+
ModelRouterRequest,
8+
ModelRouterResponse,
9+
ModelRouterUsage,
10+
} from '@/lib/mothership/generated/model-router'
611
import { fetchGo } from '@/lib/mothership/request/go/fetch'
712
import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url'
813
import { validateModelProvider } from '@/ee/access-control/utils/permission-check'
@@ -100,22 +105,6 @@ export interface AutoRoutingSignals {
100105
approxInputTokens: number
101106
}
102107

103-
/** Mirrors mothership's model-router-v1 response usage block. */
104-
interface ModelRouterUsage {
105-
model: string
106-
inputTokens: number
107-
cachedInputTokens: number
108-
outputTokens: number
109-
cost: number
110-
}
111-
112-
interface ModelRouterResponse {
113-
choice?: string
114-
decidedBy?: string
115-
usage?: ModelRouterUsage
116-
billable?: boolean
117-
}
118-
119108
export interface AutoRoutingResult {
120109
model: string
121110
tier: AutoTierId | null
@@ -219,7 +208,7 @@ async function callModelRouter(
219208
signals: AutoRoutingSignals,
220209
ctx: ExecutionContext,
221210
blockId: string
222-
): Promise<ModelRouterResponse | null> {
211+
): Promise<Partial<ModelRouterResponse> | null> {
223212
const baseURL = await getMothershipBaseURL({ userId: ctx.userId ?? '' })
224213

225214
const res = await fetchGo(`${baseURL}/api/model-router`, {
@@ -244,7 +233,7 @@ async function callModelRouter(
244233
workflowId: ctx.workflowId,
245234
blockId,
246235
executionId: ctx.executionId,
247-
}),
236+
} satisfies ModelRouterRequest),
248237
signal: AbortSignal.timeout(MODEL_ROUTER_TIMEOUT_MS),
249238
spanName: 'sim → go /api/model-router',
250239
operation: 'model_router',
@@ -254,7 +243,7 @@ async function callModelRouter(
254243
logger.warn('Model router returned non-OK status', { status: res.status })
255244
return null
256245
}
257-
return (await res.json().catch(() => null)) as ModelRouterResponse | null
246+
return (await res.json().catch(() => null)) as Partial<ModelRouterResponse> | null
258247
}
259248

260249
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)