🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(context-engine): tell the model a deferred tool result is still pending (#20069)
Arvin Xuf31612d72647
diff --git a/packages/context-engine/src/processors/ToolCall.ts b/packages/context-engine/src/processors/ToolCall.tsindex 86b86dd76ee..0dd53f2e983 100644--- a/packages/context-engine/src/processors/ToolCall.ts+++ b/packages/context-engine/src/processors/ToolCall.ts@@ -30,6 +30,13 @@ export interface ToolCallConfig {* Tool Call Processor* Responsible for converting ChatMessage format tool calls to OpenAI format*/+/**+ * Stand-in for a deferred tool result that has not been delivered yet. Worded+ * so the model neither reports the call as empty nor re-runs it.+ */+export const PENDING_TOOL_RESULT_CONTENT =+ 'This call is still running in the background and has not returned a result yet. It is not an empty result: its output will be delivered to the turn that started it. Do not repeat the call; tell the user it is still in progress if they ask for it.';+export class ToolCallProcessor extends BaseProcessor {readonly name = 'ToolCallProcessor';@@ -135,19 +142,17 @@ export class ToolCallProcessor extends BaseProcessor {// whose tool_calls arguments are invalid JSON (e.g. persisted before the// server-side sanitizer landed, or produced by an older client). Strict// providers like NVIDIA NIM otherwise 400 on the entire request. See .- const tool_calls = message.tools.map(- (tool: any): MessageToolCall => ({- function: {- arguments: sanitizeToolCallArguments(tool.arguments),- name: this.config.genToolCallingName- ? this.config.genToolCallingName(tool.identifier, tool.apiName, tool.type)- : `${tool.identifier}.${tool.apiName}`,- },- id: tool.id,- thoughtSignature: tool.thoughtSignature,- type: 'function',- }),- );+ const tool_calls = message.tools.map((tool: any): MessageToolCall => ({+ function: {+ arguments: sanitizeToolCallArguments(tool.arguments),+ name: this.config.genToolCallingName+ ? this.config.genToolCallingName(tool.identifier, tool.apiName, tool.type)+ : `${tool.identifier}.${tool.apiName}`,+ },+ id: tool.id,+ thoughtSignature: tool.thoughtSignature,+ type: 'function',+ }));return { ...message, tool_calls };}@@ -156,10 +161,15 @@ export class ToolCallProcessor extends BaseProcessor {* Process tool message*/private processToolMessage(message: any, supportTools: boolean): any {+ const content = this.isPendingToolResult(message)+ ? PENDING_TOOL_RESULT_CONTENT+ : message.content;+if (!supportTools) {// If tools not supported, convert tool message to user messagereturn {...message,+ content,name: undefined,plugin: undefined,role: 'user',@@ -180,11 +190,27 @@ export class ToolCallProcessor extends BaseProcessor {return {...message,+ content,name: toolName,// Keep tool_call_id for association};}+ /**+ * A deferred tool (callSubAgent, callAgent, group members) persists an empty+ * placeholder with `pluginState.status: 'pending'` and parks its turn until+ * the result is backfilled. A NEW user turn on the same topic can run in the+ * meantime and would otherwise read that slot as an empty result.+ */+ private isPendingToolResult(message: any): boolean {+ const isEmpty =+ message.content === undefined ||+ message.content === null ||+ (typeof message.content === 'string' && !message.content.trim());++ return isEmpty && message.pluginState?.status === 'pending';+ }+/*** Validate tool call format*/diff --git a/packages/context-engine/src/processors/__tests__/ToolCall.test.ts b/packages/context-engine/src/processors/__tests__/ToolCall.test.tsindex f237a9407a2..dfe5940ab8d 100644--- a/packages/context-engine/src/processors/__tests__/ToolCall.test.ts+++ b/packages/context-engine/src/processors/__tests__/ToolCall.test.ts@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from 'vitest';import type { PipelineContext } from '../../types';import type { ToolCallConfig } from '../ToolCall';-import { ToolCallProcessor } from '../ToolCall';+import { PENDING_TOOL_RESULT_CONTENT, ToolCallProcessor } from '../ToolCall';describe('ToolCallProcessor', () => {const createContext = (messages: any[]): PipelineContext => ({@@ -339,6 +339,61 @@ describe('ToolCallProcessor', () => {expect(result.messages[0].role).toBe('tool');});+ // A new user turn can run while a deferred callSubAgent is still parked on+ // its empty placeholder; the model used to read that slot as an empty result.+ it('should replace a still-pending deferred tool placeholder with a running note', async () => {+ const processor = new ToolCallProcessor(defaultConfig);+ const pending = {+ content: '',+ id: 'msg-tool-1',+ plugin: { apiName: 'callSubAgent', identifier: 'lobe-agent', type: 'builtin' },+ pluginState: { status: 'pending', threadId: 'thd_1' },+ role: 'tool',+ tool_call_id: 'call_1',+ };++ const result = await processor.process(createContext([pending]));++ expect(result.messages[0].content).toBe(PENDING_TOOL_RESULT_CONTENT);+ });++ it('should keep the running note when the turn switched to a model without tool calling', async () => {+ const processor = new ToolCallProcessor({ ...defaultConfig, isCanUseFC: () => false });+ const pending = {+ content: '',+ id: 'msg-tool-1',+ plugin: { apiName: 'callSubAgent', identifier: 'lobe-agent', type: 'builtin' },+ pluginState: { status: 'pending' },+ role: 'tool',+ tool_call_id: 'call_1',+ };++ const result = await processor.process(createContext([pending]));++ expect(result.messages[0]).toMatchObject({+ content: PENDING_TOOL_RESULT_CONTENT,+ role: 'user',+ });+ });++ it('should keep the real result once a deferred tool has been backfilled', async () => {+ const processor = new ToolCallProcessor(defaultConfig);+ const done = {+ content: 'Rome was founded in 753 BC.',+ id: 'msg-tool-1',+ plugin: { apiName: 'callSubAgent', identifier: 'lobe-agent', type: 'builtin' },+ pluginState: { status: 'completed', threadId: 'thd_1' },+ role: 'tool',+ tool_call_id: 'call_1',+ };+ const emptyCompleted = { ...done, content: '', pluginState: { status: 'completed' } };++ const result = await processor.process(createContext([done, emptyCompleted]));++ expect(result.messages[0].content).toBe('Rome was founded in 753 BC.');+ expect(result.messages[1].content).toBe('');+ });+it('should use custom genToolCallingName for tool messages', async () => {const genToolCallingName = vi.fn((identifier, apiName, type) => `tool_${identifier}_${apiName}_${type}`,