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.

🐛 fix: preserve backend traceId in error handler (#13607)

YuTengjingb068c427d494
diff --git a/src/store/chat/agents/__tests__/createAgentExecutors/call-llm.test.ts b/src/store/chat/agents/__tests__/createAgentExecutors/call-llm.test.ts
index 55cc4532efa..55d82626937 100644
--- a/src/store/chat/agents/__tests__/createAgentExecutors/call-llm.test.ts
+++ b/src/store/chat/agents/__tests__/createAgentExecutors/call-llm.test.ts
@@ -1727,4 +1727,110 @@ describe('call_llm executor', () => {
expect(finalContent).toBe('Partial output');
});
});
+
+ describe('Error traceId preservation', () => {
+ it('should preserve backend traceId when local traceId is undefined', async () => {
+ // Given
+ const mockStore = createMockStore();
+ const context = createTestContext();
+ const instruction = createCallLLMInstruction();
+ const state = createInitialState();
+
+ mockStore.dbMessagesMap[context.messageKey] = [];
+
+ const backendTraceId = 'backend-trace-id-123';
+
+ vi.mocked(chatService.createAssistantMessageStream).mockImplementation(
+ async (params: any) => {
+ if (params.onErrorHandle) {
+ params.onErrorHandle({
+ body: {
+ traceId: backendTraceId,
+ },
+ message: 'Provider error',
+ type: 'ProviderBizError',
+ });
+ }
+
+ if (params.onFinish) {
+ await params.onFinish('', { type: 'error' });
+ }
+ },
+ );
+
+ // When
+ await executeWithMockContext({
+ executor: 'call_llm',
+ instruction,
+ state,
+ mockStore,
+ context,
+ });
+
+ // Then
+ expect(mockStore.optimisticUpdateMessageError).toHaveBeenCalled();
+ const errorCall = vi.mocked(mockStore.optimisticUpdateMessageError).mock.calls[0];
+ const errorArg = errorCall[1] as any;
+ expect(errorArg.body.traceId).toBe(backendTraceId);
+ });
+
+ it('should use local traceId when available', async () => {
+ // Given
+ const mockStore = createMockStore();
+ const localTraceId = 'local-trace-id-456';
+ const context = createTestContext();
+ const instruction = createCallLLMInstruction();
+ const state = createInitialState();
+
+ // Set traceId on operation metadata
+ mockStore.operations[context.operationId] = {
+ abortController: new AbortController(),
+ childOperationIds: [],
+ context: {
+ agentId: context.agentId,
+ messageId: context.parentId,
+ topicId: context.topicId,
+ },
+ id: context.operationId,
+ metadata: { startTime: Date.now(), traceId: localTraceId },
+ status: 'running',
+ type: 'execAgentRuntime',
+ };
+
+ mockStore.dbMessagesMap[context.messageKey] = [];
+
+ vi.mocked(chatService.createAssistantMessageStream).mockImplementation(
+ async (params: any) => {
+ if (params.onErrorHandle) {
+ params.onErrorHandle({
+ body: {
+ traceId: 'backend-trace-id',
+ },
+ message: 'Provider error',
+ type: 'ProviderBizError',
+ });
+ }
+
+ if (params.onFinish) {
+ await params.onFinish('', { type: 'error' });
+ }
+ },
+ );
+
+ // When
+ await executeWithMockContext({
+ executor: 'call_llm',
+ instruction,
+ state,
+ mockStore,
+ context,
+ });
+
+ // Then - local traceId should take precedence
+ expect(mockStore.optimisticUpdateMessageError).toHaveBeenCalled();
+ const errorCall = vi.mocked(mockStore.optimisticUpdateMessageError).mock.calls[0];
+ const errorArg = errorCall[1] as any;
+ expect(errorArg.body.traceId).toBe(localTraceId);
+ });
+ });
});
diff --git a/src/store/chat/agents/createAgentExecutors.ts b/src/store/chat/agents/createAgentExecutors.ts
index e946a6bd1aa..2bdab878b1a 100644
--- a/src/store/chat/agents/createAgentExecutors.ts
+++ b/src/store/chat/agents/createAgentExecutors.ts
@@ -480,7 +480,7 @@ export const createAgentExecutors = (context: {
...error,
body: {
...error.body,
- traceId,
+ traceId: traceId ?? error.body?.traceId,
},
};
const localizedError = localizeError(enrichedError);