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: auto-run required tools in headless mode (#15492)

Rylan Cai596440901d46
diff --git a/packages/agent-runtime/src/agents/GeneralChatAgent.ts b/packages/agent-runtime/src/agents/GeneralChatAgent.ts
index 772a75b2792..343306c8153 100644
--- a/packages/agent-runtime/src/agents/GeneralChatAgent.ts
+++ b/packages/agent-runtime/src/agents/GeneralChatAgent.ts
@@ -187,6 +187,8 @@ export class GeneralChatAgent implements Agent {
if (dynamicPolicy !== undefined) {
if (dynamicPolicy === 'never') {
toolsToExecute.push(toolCalling);
+ } else if (approvalMode === 'headless' && dynamicPolicy !== 'always') {
+ toolsToExecute.push(toolCalling);
} else {
toolsNeedingIntervention.push(toolCalling);
}
@@ -211,6 +213,13 @@ export class GeneralChatAgent implements Agent {
continue;
}
+ // Headless/CLI has no approval UI. Auto-run overridable tool-level policies,
+ // while preserving non-bypassable `always` blocks handled above.
+ if (approvalMode === 'headless') {
+ toolsToExecute.push(toolCalling);
+ continue;
+ }
+
// Phase 5: User config is 'auto-run', all tools execute directly
if (approvalMode === 'auto-run') {
toolsToExecute.push(toolCalling);
diff --git a/packages/agent-runtime/src/agents/__tests__/GeneralChatAgent.test.ts b/packages/agent-runtime/src/agents/__tests__/GeneralChatAgent.test.ts
index 2a510829724..abb842fc23e 100644
--- a/packages/agent-runtime/src/agents/__tests__/GeneralChatAgent.test.ts
+++ b/packages/agent-runtime/src/agents/__tests__/GeneralChatAgent.test.ts
@@ -2948,7 +2948,7 @@ describe('GeneralChatAgent', () => {
});
describe('headless mode (for async tasks)', () => {
- it('should resolve tool-level required tools in headless mode', async () => {
+ it('should execute tool-level required tools in headless mode', async () => {
const agent = new GeneralChatAgent({
agentConfig: { maxSteps: 100 },
operationId: 'test-session',
@@ -2983,14 +2983,58 @@ describe('GeneralChatAgent', () => {
const result = await agent.runner(context, state);
- // Headless/CLI cannot request tool-level approval, so return a blocked tool result for replan.
expect(result).toEqual([
{
- payload: {
- parentMessageId: 'msg-1',
- toolsCalling: [toolCall],
+ type: 'call_tool',
+ payload: { parentMessageId: 'msg-1', toolCalling: toolCall },
+ },
+ ]);
+ });
+
+ it('should execute API-level required runCommand in headless mode', async () => {
+ const agent = new GeneralChatAgent({
+ agentConfig: { maxSteps: 100 },
+ operationId: 'test-session',
+ modelRuntimeConfig: mockModelRuntimeConfig,
+ });
+
+ const toolCall: ChatToolPayload = {
+ id: 'call-1',
+ identifier: 'lobe-local-system',
+ apiName: 'runCommand',
+ arguments: '{"command":"which R","description":"Locate the R executable"}',
+ type: 'default',
+ };
+
+ const state = createMockState({
+ toolManifestMap: {
+ 'lobe-local-system': {
+ identifier: 'lobe-local-system',
+ api: [
+ {
+ name: 'runCommand',
+ humanIntervention: 'required',
+ },
+ ],
},
- type: 'resolve_blocked_tools',
+ },
+ userInterventionConfig: {
+ approvalMode: 'headless',
+ },
+ });
+
+ const context = createMockContext('llm_result', {
+ hasToolsCalling: true,
+ toolsCalling: [toolCall],
+ parentMessageId: 'msg-1',
+ });
+
+ const result = await agent.runner(context, state);
+
+ expect(result).toEqual([
+ {
+ type: 'call_tool',
+ payload: { parentMessageId: 'msg-1', toolCalling: toolCall },
},
]);
});
@@ -3158,12 +3202,16 @@ describe('GeneralChatAgent', () => {
const result = await agent.runner(context, state);
- // Headless/CLI batches all tools that need intervention into blocked results for replan.
+ // Headless/CLI auto-runs required tools and returns blocked results for non-bypassable tools.
expect(result).toEqual([
+ {
+ payload: { parentMessageId: 'msg-1', toolCalling: requiredTool },
+ type: 'call_tool',
+ },
{
payload: {
parentMessageId: 'msg-1',
- toolsCalling: [requiredTool, blacklistedTool, alwaysTool],
+ toolsCalling: [blacklistedTool, alwaysTool],
},
type: 'resolve_blocked_tools',
},
@@ -3211,16 +3259,72 @@ describe('GeneralChatAgent', () => {
const result = await agent.runner(context, state);
- // Headless/CLI returns blocked results for every tool that would otherwise need approval.
+ // Headless/CLI auto-runs required tools and returns blocked results for non-bypassable tools.
expect(result).toEqual([
+ {
+ payload: { parentMessageId: 'msg-1', toolCalling: tool1 },
+ type: 'call_tool',
+ },
{
payload: {
parentMessageId: 'msg-1',
- toolsCalling: [tool1, tool2],
+ toolsCalling: [tool2],
},
type: 'resolve_blocked_tools',
},
]);
});
+
+ it('should batch multiple tool-level required tools in headless mode', async () => {
+ const agent = new GeneralChatAgent({
+ agentConfig: { maxSteps: 100 },
+ operationId: 'test-session',
+ modelRuntimeConfig: mockModelRuntimeConfig,
+ });
+
+ const tool1: ChatToolPayload = {
+ id: 'call-1',
+ identifier: 'search',
+ apiName: 'search',
+ arguments: '{}',
+ type: 'default',
+ };
+
+ const tool2: ChatToolPayload = {
+ id: 'call-2',
+ identifier: 'crawl',
+ apiName: 'crawl',
+ arguments: '{}',
+ type: 'default',
+ };
+
+ const state = createMockState({
+ toolManifestMap: {
+ search: { identifier: 'search', humanIntervention: 'required' },
+ crawl: { identifier: 'crawl', humanIntervention: 'required' },
+ },
+ userInterventionConfig: {
+ approvalMode: 'headless',
+ },
+ });
+
+ const context = createMockContext('llm_result', {
+ hasToolsCalling: true,
+ toolsCalling: [tool1, tool2],
+ parentMessageId: 'msg-1',
+ });
+
+ const result = await agent.runner(context, state);
+
+ expect(result).toEqual([
+ {
+ payload: {
+ parentMessageId: 'msg-1',
+ toolsCalling: [tool1, tool2],
+ },
+ type: 'call_tools_batch',
+ },
+ ]);
+ });
});
});