Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat: expose router affinity hooks (#19648)

YuTengjing697f66a800f4
diff --git a/packages/model-runtime/src/core/RouterRuntime/createRuntime.test.ts b/packages/model-runtime/src/core/RouterRuntime/createRuntime.test.ts
index 90b020891ca..70f333cdb32 100644
--- a/packages/model-runtime/src/core/RouterRuntime/createRuntime.test.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/createRuntime.test.ts
@@ -1676,6 +1676,35 @@ describe('createRouterRuntime', () => {
expect(mockCreateVideo).toHaveBeenCalledWith(payload, undefined);
});
+ it('should identify video requests when sorting router options', async () => {
+ const mockCreateVideo = vi.fn().mockResolvedValue({ inferenceId: 'job-1' });
+ const sortRouterOptions = vi.fn(({ options }) => options);
+
+ class MockRuntime implements LobeRuntimeAI {
+ createVideo = mockCreateVideo;
+ }
+
+ const Runtime = createRouterRuntime({
+ id: 'test-runtime',
+ routers: [
+ {
+ apiType: 'openai',
+ models: ['sora-1'],
+ options: [{ apiKey: 'key-1' }, { apiKey: 'key-2' }],
+ runtime: MockRuntime as any,
+ },
+ ],
+ sortRouterOptions,
+ });
+
+ const runtime = new Runtime();
+ await runtime.createVideo({ model: 'sora-1', params: { prompt: 'a cat' } } as any);
+
+ expect(sortRouterOptions).toHaveBeenCalledWith(
+ expect.objectContaining({ method: 'createVideo', model: 'sora-1' }),
+ );
+ });
+
it('should forward options.metadata to onRouteAttempt', async () => {
const mockCreateVideo = vi.fn().mockResolvedValue({ inferenceId: 'job-1' });
const onRouteAttempt = vi.fn().mockResolvedValue(undefined);
@@ -1952,18 +1981,106 @@ describe('createRouterRuntime', () => {
const runtime = new Runtime();
await runtime.chat({ messages: [], model: 'gpt-4', temperature: 0.7 });
- expect(sortRouterOptions).toHaveBeenCalledWith({
- model: 'gpt-4',
- options: [
- expect.objectContaining({ id: 'channel-a' }),
- expect.objectContaining({ id: 'channel-b' }),
- ],
- routerId: 'router-a',
- });
+ expect(sortRouterOptions).toHaveBeenCalledWith(
+ expect.objectContaining({
+ method: 'chat',
+ model: 'gpt-4',
+ options: [
+ expect.objectContaining({ id: 'channel-a' }),
+ expect.objectContaining({ id: 'channel-b' }),
+ ],
+ routerId: 'router-a',
+ }),
+ );
// Reversed order: channel-b is tried first and succeeds
expect(attemptedKeys).toEqual(['key-2']);
});
+ it('passes the runtime user and request metadata to channel selection', async () => {
+ const sortRouterOptions = vi.fn(({ options }) => options);
+ const Runtime = createRouterRuntime({
+ id: 'test-runtime',
+ routers: [
+ {
+ apiType: 'openai',
+ id: 'router-a',
+ models: ['gpt-4'],
+ options: [{ apiKey: 'key-1' }, { apiKey: 'key-2' }],
+ runtime: createRecordingRuntime([]) as any,
+ },
+ ],
+ sortRouterOptions,
+ });
+
+ await new Runtime({ userId: 'owner-1' }).chat(
+ { messages: [], model: 'gpt-4', temperature: 0.7 },
+ { metadata: { topicId: 'topic-1' }, user: 'caller-1' },
+ );
+
+ expect(sortRouterOptions).toHaveBeenCalledWith(
+ expect.objectContaining({
+ metadata: { topicId: 'topic-1' },
+ userId: 'owner-1',
+ }),
+ );
+ });
+
+ it('waits for a successful fallback to update the selected channel', async () => {
+ const attemptedKeys: string[] = [];
+ let releaseBinding!: () => void;
+ const onRouteSuccess = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ releaseBinding = resolve;
+ }),
+ );
+ const Runtime = createRouterRuntime({
+ id: 'test-runtime',
+ onRouteSuccess,
+ routers: [
+ {
+ apiType: 'openai',
+ id: 'router-a',
+ models: ['gpt-4'],
+ options: [
+ { apiKey: 'key-1', id: 'channel-a', weight: 70 },
+ { apiKey: 'key-2', id: 'channel-b', weight: 30 },
+ ],
+ runtime: createRecordingRuntime(attemptedKeys, new Set(['key-1'])) as any,
+ },
+ ],
+ });
+
+ const request = new Runtime({ userId: 'owner-1' }).chat({
+ messages: [],
+ model: 'gpt-4',
+ temperature: 0.7,
+ });
+ await vi.waitFor(() => expect(onRouteSuccess).toHaveBeenCalledOnce());
+ let settled = false;
+ request.then(() => {
+ settled = true;
+ });
+ await Promise.resolve();
+ expect(settled).toBe(false);
+ releaseBinding();
+ await request;
+
+ expect(attemptedKeys).toEqual(['key-1', 'key-2']);
+ expect(onRouteSuccess).toHaveBeenCalledWith(
+ expect.objectContaining({
+ channelId: 'channel-b',
+ channelWeight: 30,
+ firstChannelId: 'channel-a',
+ method: 'chat',
+ model: 'gpt-4',
+ routerId: 'router-a',
+ userId: 'owner-1',
+ weighted: true,
+ }),
+ );
+ });
+
it('should still fall back through all options after reordering', async () => {
const attemptedKeys: string[] = [];
diff --git a/packages/model-runtime/src/core/RouterRuntime/createRuntime.ts b/packages/model-runtime/src/core/RouterRuntime/createRuntime.ts
index 58ba0f29581..bcd505a57d4 100644
--- a/packages/model-runtime/src/core/RouterRuntime/createRuntime.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/createRuntime.ts
@@ -81,6 +81,8 @@ interface RouterOptionItem extends ProviderIniOptions {
apiType?: ApiType;
id?: string;
remark?: string;
+ /** Relative share of new user bindings; zero keeps the channel as fallback only. */
+ weight?: number;
}
type RouterOptions = RouterOptionItem | RouterOptionItem[];
@@ -137,6 +139,7 @@ interface RouteAttemptMetadata {
interface RouteAttemptContext {
allowedApiTypes?: ReadonlySet<ApiType>;
metadata?: Record<string, unknown>;
+ method: RouterRuntimeMethod;
pricingContext?: ModelPricingContext;
toolsCount?: number;
user?: string;
@@ -158,11 +161,34 @@ interface RouteAttemptContextValidationParams extends RouteAttemptContext {
}
export interface SortRouterOptionsParams {
+ metadata?: Record<string, unknown>;
+ method: RouterRuntimeMethod;
model: string;
options: RouterOptionItem[];
routerId?: string;
+ userId?: string;
+}
+
+export interface RouteSuccessParams {
+ channelId?: string;
+ channelWeight?: number;
+ firstChannelId?: string;
+ method: RouterRuntimeMethod;
+ model: string;
+ routerId?: string;
+ userId?: string;
+ weighted: boolean;
}
+export type RouterRuntimeMethod =
+ | 'chat'
+ | 'createImage'
+ | 'createVideo'
+ | 'embeddings'
+ | 'generateObject'
+ | 'textToSpeech'
+ | 'transcribe';
+
export interface CreateRouterRuntimeOptions<T extends Record<string, any> = any> {
apiKey?: string;
chatCompletion?: {
@@ -218,6 +244,8 @@ export interface CreateRouterRuntimeOptions<T extends Record<string, any> = any>
transformModel?: (model: OpenAI.Model) => ChatModelCard;
};
onRouteAttempt?: (result: RouteAttemptResult) => Promise<void>;
+ /** Awaited before returning so a successful fallback can update routing affinity. */
+ onRouteSuccess?: (result: RouteSuccessParams) => void | Promise<void>;
responses?: {
handlePayload?: (
payload: ChatStreamPayload,
@@ -454,6 +482,7 @@ export const createRouterRuntime = ({
router: RouterInstance,
model: string,
routerOptions: RouterOptionItem[],
+ routeContext: RouteAttemptContext,
): Promise<RouterOptionItem[]> {
if (!params.sortRouterOptions || routerOptions.length <= 1) return routerOptions;
@@ -465,9 +494,14 @@ export const createRouterRuntime = ({
// baseline — validating a same-reference return would always pass, even
// after mutations like `options.pop()`.
const sorted = await params.sortRouterOptions({
+ metadata: routeContext.metadata,
+ method: routeContext.method,
model,
options: [...routerOptions],
routerId: router.id,
+ userId:
+ (typeof this._options.userId === 'string' ? this._options.userId : undefined) ||
+ routeContext.user,
});
const isPermutation =
Array.isArray(sorted) &&
@@ -519,6 +553,7 @@ export const createRouterRuntime = ({
}> {
const startedAt = Date.now();
const { apiType: optionApiType, id: channelId, remark, ...optionOverrides } = optionItem;
+ delete optionOverrides.weight;
const resolvedApiType = optionApiType ?? router.apiType;
const finalOptions = {
...this._params,
@@ -603,26 +638,27 @@ export const createRouterRuntime = ({
private async runWithFallback<T>(
model: string,
requestHandler: (runtime: LobeRuntimeAI) => Promise<T>,
- routeContext: RouteAttemptContext = {},
+ routeContext: RouteAttemptContext,
): Promise<T> {
const totalStartedAt = Date.now();
const { allowedApiTypes, metadata, pricingContext, toolsCount, user } = routeContext;
const matchedRouter = await this.resolveMatchedRouter(model, pricingContext);
- const sortedRouterOptions = await this.applySortRouterOptions(
+ const eligibleRouterOptions = this.normalizeRouterOptions(matchedRouter).filter(
+ (option) =>
+ !allowedApiTypes || allowedApiTypes.has(option.apiType ?? matchedRouter.apiType),
+ );
+ if (eligibleRouterOptions.length === 0) {
+ throw new TypeError(`No provider route supports raw audio input for model ${model}`);
+ }
+ const routerOptions = await this.applySortRouterOptions(
matchedRouter,
model,
- this.normalizeRouterOptions(matchedRouter),
+ eligibleRouterOptions,
+ routeContext,
);
- const routerOptions = allowedApiTypes
- ? sortedRouterOptions.filter((option) =>
- allowedApiTypes.has(option.apiType ?? matchedRouter.apiType),
- )
- : sortedRouterOptions;
const totalOptions = routerOptions.length;
-
- if (totalOptions === 0) {
- throw new TypeError(`No provider route supports raw audio input for model ${model}`);
- }
+ const firstChannelId = routerOptions[0]?.id;
+ const weighted = routerOptions.some((option) => option.weight !== undefined);
if (this._id === 'lobehub') {
timing(
@@ -657,6 +693,7 @@ export const createRouterRuntime = ({
apiType: resolvedApiType,
channelId,
metadata,
+ method: routeContext.method,
model,
routerId: matchedRouter.id,
toolsCount,
@@ -712,6 +749,24 @@ export const createRouterRuntime = ({
);
}
+ if (params.onRouteSuccess) {
+ try {
+ await params.onRouteSuccess({
+ channelId,
+ channelWeight: optionItem.weight,
+ firstChannelId,
+ method: routeContext.method,
+ model,
+ routerId: matchedRouter.id,
+ userId: routeAttemptUserId,
+ weighted,
+ });
+ } catch (error) {
+ // Affinity storage must not turn a successful upstream response into a fallback.
+ console.error('[RouterRuntime] onRouteSuccess callback failed:', error);
+ }
+ }
+
params
.onRouteAttempt?.({
apiType: resolvedApiType,
@@ -899,6 +954,7 @@ export const createRouterRuntime = ({
{
allowedApiTypes: containsRawAudio ? RAW_AUDIO_API_TYPES : undefined,
metadata: options?.metadata,
+ method: 'chat',
pricingContext: options?.pricingContext,
toolsCount: payload.tools?.length ?? 0,
user: options?.user,
@@ -921,7 +977,11 @@ export const createRouterRuntime = ({
return this.runWithFallback(
payload.model,
(runtime) => runtime.createImage!(payload, options),
- { metadata: options?.metadata, pricingContext: options?.pricingContext },
+ {
+ metadata: options?.metadata,
+ method: 'createImage',
+ pricingContext: options?.pricingContext,
+ },
);
}
@@ -929,7 +989,11 @@ export const createRouterRuntime = ({
return this.runWithFallback(
payload.model,
(runtime) => runtime.createVideo!(payload, options),
- { metadata: options?.metadata, pricingContext: options?.pricingContext },
+ {
+ metadata: options?.metadata,
+ method: 'createVideo',
+ pricingContext: options?.pricingContext,
+ },
);
}
@@ -963,6 +1027,7 @@ export const createRouterRuntime = ({
(runtime) => runtime.generateObject!(payload, options),
{
metadata: options?.metadata,
+ method: 'generateObject',
pricingContext: options?.pricingContext,
toolsCount: payload.tools?.length ?? 0,
user: options?.user,
@@ -976,6 +1041,7 @@ export const createRouterRuntime = ({
(runtime) => runtime.embeddings!(payload, options),
{
metadata: options?.metadata,
+ method: 'embeddings',
pricingContext: options?.pricingContext,
user: options?.user,
},
@@ -988,6 +1054,7 @@ export const createRouterRuntime = ({
(runtime) => runtime.textToSpeech!(payload, options),
{
metadata: options?.metadata,
+ method: 'textToSpeech',
pricingContext: options?.pricingContext,
user: options?.user,
},
@@ -998,7 +1065,7 @@ export const createRouterRuntime = ({
return this.runWithFallback(
payload.model,
(runtime) => runtime.transcribe!(payload, options),
- { user: options?.user },
+ { method: 'transcribe', user: options?.user },
);
}
};