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.

🔨 chore(model-runtime): add onRouteAttempt callback to RouterRuntime (#12234)

YuTengjing7721261dc089
diff --git a/packages/model-runtime/src/core/RouterRuntime/createRuntime.ts b/packages/model-runtime/src/core/RouterRuntime/createRuntime.ts
index 84864026dde..7dace1e88df 100644
--- a/packages/model-runtime/src/core/RouterRuntime/createRuntime.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/createRuntime.ts
@@ -61,6 +61,7 @@ type RouterOptions = RouterOptionItem | RouterOptionItem[];
interface RouterInstance {
apiType: ApiType;
baseURLPattern?: RegExp;
+ id?: string;
models?: string[];
options: RouterOptions;
runtime?: RuntimeClass;
@@ -77,6 +78,18 @@ type Routers =
},
) => RouterInstance[] | Promise<RouterInstance[]>);
+export interface RouteAttemptResult {
+ apiType: string;
+ channelId?: string;
+ durationMs: number;
+ error?: unknown;
+ model: string;
+ providerId: string;
+ remark?: string;
+ routerId?: string;
+ success: boolean;
+}
+
export interface CreateRouterRuntimeOptions<T extends Record<string, any> = any> {
apiKey?: string;
chatCompletion?: {
@@ -123,6 +136,7 @@ export interface CreateRouterRuntimeOptions<T extends Record<string, any> = any>
| {
transformModel?: (model: OpenAI.Model) => ChatModelCard;
};
+ onRouteAttempt?: (result: RouteAttemptResult) => Promise<void>;
responses?: {
handlePayload?: (
payload: ChatStreamPayload,
@@ -287,6 +301,7 @@ export const createRouterRuntime = ({
for (const [index, optionItem] of routerOptions.entries()) {
const attempt = index + 1;
+ const startTime = Date.now();
const {
channelId,
id: resolvedApiType,
@@ -309,10 +324,37 @@ export const createRouterRuntime = ({
);
}
+ params.onRouteAttempt?.({
+ apiType: resolvedApiType,
+ channelId,
+ durationMs: Date.now() - startTime,
+ model,
+ providerId: id,
+ remark,
+ routerId: matchedRouter.id,
+ success: true,
+ }).catch((e) => {
+ log('onRouteAttempt callback error: %O', e);
+ });
+
return result;
} catch (error) {
lastError = error;
+ params.onRouteAttempt?.({
+ apiType: resolvedApiType,
+ channelId,
+ durationMs: Date.now() - startTime,
+ error,
+ model,
+ providerId: id,
+ remark,
+ routerId: matchedRouter.id,
+ success: false,
+ }).catch((e) => {
+ log('onRouteAttempt callback error: %O', e);
+ });
+
if (attempt < totalOptions) {
log(
'attempt %d/%d failed (model=%s apiType=%s channelId=%s remark=%s), trying next',
diff --git a/packages/model-runtime/src/core/RouterRuntime/index.ts b/packages/model-runtime/src/core/RouterRuntime/index.ts
index aae08842acc..864631827c0 100644
--- a/packages/model-runtime/src/core/RouterRuntime/index.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/index.ts
@@ -6,5 +6,5 @@ export interface RuntimeItem {
runtime: LobeRuntimeAI;
}
-export type { CreateRouterRuntimeOptions, UniformRuntime } from './createRuntime';
+export type { CreateRouterRuntimeOptions, RouteAttemptResult, UniformRuntime } from './createRuntime';
export { createRouterRuntime } from './createRuntime';