Commit 43bd4f9c by YeYuheng Committed by GitHub

fix(agent): recognize internal runtime tools (#7367)

parent 583d7297
......@@ -182,7 +182,11 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({
toolCatalog: normalized
};
const hasRuntimeTools = normalized.runtimeTools.length > 0;
const hasRuntimeTools =
normalized.runtimeTools.length > 0 ||
(normalized.sandboxTools?.length ?? 0) > 0 ||
!!normalized.readFileTool ||
!!normalized.datasetSearchTool;
let pendingAsk:
| {
......
......@@ -403,7 +403,11 @@ export const runPiAgentLoop = async <TChildrenResponse = unknown>({
? input.systemPrompt || ''
: getMainAgentSystemPrompt({
systemPrompt: input.systemPrompt,
hasRuntimeTools: runtime.toolCatalog.runtimeTools.length > 0
hasRuntimeTools:
runtime.toolCatalog.runtimeTools.length > 0 ||
runtime.systemTools?.sandbox?.enabled === true ||
runtime.systemTools?.readFile?.enabled === true ||
runtime.systemTools?.datasetSearch?.enabled === true
});
const agent = new Agent({
initialState: {
......
......@@ -165,6 +165,58 @@ describe('runFastAgentMainLoop', () => {
expect(mainAgentPrompt).toContain('options:2 到 5 个');
});
it.each([
{
name: 'no runtime-capable tools',
toolCatalog: { runtimeTools: [] },
expectedConstraint: true
},
{
name: 'sandbox tools only',
toolCatalog: { runtimeTools: [], sandboxTools: [tool('sandbox_shell')] },
expectedConstraint: false
},
{
name: 'read file tool only',
toolCatalog: { runtimeTools: [], readFileTool: tool('read_files') },
expectedConstraint: false
},
{
name: 'dataset search tool only',
toolCatalog: { runtimeTools: [], datasetSearchTool: tool('dataset_search') },
expectedConstraint: false
}
] satisfies Array<{
name: string;
toolCatalog: AgentLoopRuntime['toolCatalog'];
expectedConstraint: boolean;
}>)(
'sets the runtime tool constraint correctly with $name',
async ({ toolCatalog, expectedConstraint }) => {
mockCreateLLMResponseQueue(createLLMResponseMock, [
text({
requestId: 'req_runtime_tool_constraint',
content: 'direct answer'
})
]);
await runFastAgentMainLoop({
runtime: createRuntime({ toolCatalog }),
input: {
messages: [
{
role: ChatCompletionRequestMessageRoleEnum.User,
content: 'hello'
}
]
}
});
const mainAgentPrompt = createLLMResponseMock.mock.calls[0][0].body.messages[0].content;
expect(mainAgentPrompt.includes('<tool_constraint>')).toBe(expectedConstraint);
}
);
it('creates an active plan through set_plan', async () => {
const events: any[] = [];
mockCreateLLMResponseQueue(createLLMResponseMock, [
......
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { LLMModelItemType } from '@fastgpt/global/core/ai/model.schema';
import { ModelTypeEnum } from '@fastgpt/global/core/ai/constants';
import type { AgentLoopSystemTools } from '@fastgpt/service/core/ai/llm/agentLoop/domain';
const {
agentPromptMock,
......@@ -366,6 +367,73 @@ describe('runPiAgentLoop', () => {
]);
});
it.each([
{
name: 'no runtime-capable tools',
systemTools: undefined,
expectedConstraint: true
},
{
name: 'sandbox tools only',
systemTools: {
sandbox: {
enabled: true,
client: {} as any
}
},
expectedConstraint: false
},
{
name: 'read file tool only',
systemTools: {
readFile: {
enabled: true,
maxFileAmount: 20,
execute: vi.fn()
}
},
expectedConstraint: false
},
{
name: 'dataset search tool only',
systemTools: {
datasetSearch: {
enabled: true,
execute: vi.fn()
}
},
expectedConstraint: false
}
] satisfies Array<{
name: string;
systemTools?: AgentLoopSystemTools;
expectedConstraint: boolean;
}>)(
'sets the runtime tool constraint correctly with $name',
async ({ systemTools, expectedConstraint }) => {
await runPiAgentLoop({
input: {
messages: [{ role: 'user', content: 'hello' }]
},
runtime: {
llmParams: {
model: 'gpt-5'
},
systemTools,
toolCatalog: {
runtimeTools: []
},
executeTool: vi.fn(),
checkIsStopping: vi.fn(() => false)
}
});
expect(
agentConstructorArgs.at(-1).initialState.systemPrompt.includes('<tool_constraint>')
).toBe(expectedConstraint);
}
);
it('preserves multimodal content in the current user prompt', async () => {
await runPiAgentLoop({
input: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment