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): report provider finish reason on route attempts (#19923)

YuTengjing6a3f86d21152
diff --git a/packages/model-runtime/src/core/RouterRuntime/chatAttempt.test.ts b/packages/model-runtime/src/core/RouterRuntime/chatAttempt.test.ts
index c8dbaffb908..b24811c84af 100644
--- a/packages/model-runtime/src/core/RouterRuntime/chatAttempt.test.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/chatAttempt.test.ts
@@ -294,6 +294,34 @@ describe('observeChatAttempt', () => {
}
});
+ it.each([
+ { expectedOutcome: 'completed', text: 'I cannot help with that.' },
+ { expectedOutcome: 'empty', text: '' },
+ ])(
+ 'reports the provider finish reason on a $expectedOutcome attempt',
+ async ({ expectedOutcome, text }) => {
+ const finished = vi.fn();
+ const response = await observeChatAttempt(
+ async ({ callback }) => {
+ if (text) await callback?.onText?.(text);
+ await callback?.onFinal?.({ finishReason: 'refusal', text });
+ return new Response('done');
+ },
+ undefined,
+ attempt,
+ true,
+ finished,
+ );
+ await response.text().catch(() => {});
+
+ expect(finished).toHaveBeenCalledTimes(1);
+ expect(finished.mock.calls[0][0]).toMatchObject({
+ finishReason: 'refusal',
+ outcome: expectedOutcome,
+ });
+ },
+ );
+
it('keeps concurrent attempt identities isolated even with shared options', async () => {
const finals: OnFinishData[] = [];
const options: ChatMethodOptions = {
diff --git a/packages/model-runtime/src/core/RouterRuntime/chatAttempt.ts b/packages/model-runtime/src/core/RouterRuntime/chatAttempt.ts
index 00d6efe201a..2967c721027 100644
--- a/packages/model-runtime/src/core/RouterRuntime/chatAttempt.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/chatAttempt.ts
@@ -164,6 +164,7 @@ export const observeChatAttempt = async (
diagnostics: attemptDiagnostics,
durationMs: durationMs ?? completedAt - attempt.startedAt,
error: finalError,
+ finishReason: finishData?.finishReason,
outcome,
speed: finishData?.speed
? {
diff --git a/packages/model-runtime/src/core/RouterRuntime/routeAttempt.ts b/packages/model-runtime/src/core/RouterRuntime/routeAttempt.ts
index 56e50123635..205d5ec7901 100644
--- a/packages/model-runtime/src/core/RouterRuntime/routeAttempt.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/routeAttempt.ts
@@ -40,6 +40,11 @@ export interface RouteAttemptFinished extends RouteAttemptResult {
attemptId: string;
completedAt: number;
diagnostics: ModelRuntimeDiagnostics;
+ /**
+ * Raw terminal finish reason reported by the provider (e.g. `end_turn`, `refusal`,
+ * `content_filter`). Undefined when the attempt ended before the provider sent one.
+ */
+ finishReason?: string;
outcome: RouteAttemptOutcome;
requestId: string;
speed?: ModelPerformance;