Skip to content

feat(agent): support helper tool loops and injected prompts - #7439

Merged
c121914yu merged 3 commits into
labring:mainfrom
c121914yu:agent/agent-loop-helper-generation
Aug 4, 2026
Merged

feat(agent): support helper tool loops and injected prompts#7439
c121914yu merged 3 commits into
labring:mainfrom
c121914yu:agent/agent-loop-helper-generation

Conversation

@c121914yu

Copy link
Copy Markdown
Collaborator

What changed

  • Replace Agent Loop prompt modes with explicit caller-side system prompt composition.
  • Export the default Agent Loop prompt builder, including optional sandbox guidance.
  • Let auxiliary generation use the standard Agent Loop ask_user flow, runtime tools, userAnswer, and provider state.
  • Preserve raw system-message semantics in ToolCall while normalizing the final system prompt to a string.
  • Add focused tests and update the auxiliary-generation design document.

Why

Prompt construction and helper generation previously had parallel behavior outside the main Agent Loop. The new API makes ownership explicit and lets helper generation reuse the normal tool, pause/resume, transcript, and provider-state lifecycle.

Impact

Workflow Agent behavior remains caller-configured, while helper callers can disable planning, enable asking, register completion tools, and stream responses without custom pause detection.

Validation

  • Related service suite: 49 files, 252 tests passed.
  • Paired pro helper suite: 4 files, 12 tests passed.
  • Browser integration covered direct generation, clarification pause/resume, invalid-tool self-correction, history recall, and immediate config delivery.
  • Targeted ESLint and diff checks passed; one pre-existing unused-variable warning remains in agent/index.ts.

Dependency

Paired PR: https://github.com/labring/fastgpt-pro/pull/1048

This PR intentionally does not update the pro gitlink to a fork-only commit. Merge the paired fastgpt-pro PR first, then update this branch to the resulting upstream commit before marking it ready.

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 19.95% 1765 / 8847
🔵 Statements 19.94% 1855 / 9301
🔵 Functions 17.54% 372 / 2120
🔵 Branches 16.57% 833 / 5027
File CoverageNo changed files found.
Generated in workflow #1923 for commit f3f8c4b by the Vitest Coverage Report Action

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

Admin Preview Image Ready!

ghcr.io/labring/fastgpt-pr:admin_f3f8c4bc901ae6657c2a7f9b90c49e0c39646af3

🕒 Time: 2026-08-04 11:25:07 (UTC+8)

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

Build Successful - Preview fastgpt Image for this PR:

ghcr.io/labring/fastgpt-pr:fastgpt_f3f8c4bc901ae6657c2a7f9b90c49e0c39646af3

🕒 Time: 2026-08-04 11:28:19 (UTC+8)

@c121914yu
c121914yu marked this pull request as ready for review August 4, 2026 02:01
Copilot AI review requested due to automatic review settings August 4, 2026 02:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors Agent Loop prompt handling so that callers explicitly compose the final systemPrompt (including optional sandbox guidance and injected prompt extensions), while Agent Loop providers treat systemPrompt as an already-final string and avoid implicit prompt-mode behavior. It also updates auxiliary-generation to reuse the standard Agent Loop ask_user pause/resume and provider-state lifecycle, and adds/updates tests and design documentation accordingly.

Changes:

  • Remove prompt-mode-driven prompt injection and expose buildDefaultAgentSystemPrompt for caller-side system prompt composition (with optional sandbox + extension sections).
  • Update Workflow Agent + ToolCall to pass a normalized systemPrompt separately from conversation messages (system messages stripped from the message list).
  • Extend auxiliary-generation to support ask_user loops and injected runtime tools/executor, preserving paused/providerState semantics; update tests and design doc.

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/service/test/core/workflow/dispatch/ai/toolcall/toolcall/toolCall.test.ts Adds coverage to ensure system messages are extracted into systemPrompt and removed from conversation messages.
packages/service/test/core/workflow/dispatch/ai/agentLoopCore/context/messages.test.ts Removes tests for deleted agentLoopCore prompt helper utilities.
packages/service/test/core/workflow/dispatch/ai/agent/index.test.ts Updates expectations to reflect default system prompt builder + sandbox section behavior.
packages/service/test/core/ai/llm/agentLoop/piAgentProvider.test.ts Aligns provider tests with “caller supplies final systemPrompt” behavior.
packages/service/test/core/ai/llm/agentLoop/mainPrompt.test.ts Replaces getMainAgentSystemPrompt tests with buildDefaultAgentSystemPrompt tests (extension/user prompt separation, sandbox injection).
packages/service/test/core/ai/llm/agentLoop/fastAgentProvider.test.ts Adds coverage ensuring provider uses final input.systemPrompt and strips stale system messages from input.messages.
packages/service/test/core/ai/llm/agentLoop/fastAgentLoop.test.ts Updates main loop tests to pass buildDefaultAgentSystemPrompt() explicitly; removes runtime-tool-constraint prompt-mode assertions.
packages/service/test/core/ai/auxiliaryGeneration/agentLoop.test.ts New test validating auxiliary-generation Agent Loop supports ask pause/resume + provider state + runtime tools injection.
packages/service/core/workflow/dispatch/ai/toolcall/toolCall.ts Extracts system prompt from system-role messages (including text-part arrays) and passes it separately to agent-loop input.
packages/service/core/workflow/dispatch/ai/agentLoopCore/interface/index.ts Stops exporting deleted prompt helper.
packages/service/core/workflow/dispatch/ai/agentLoopCore/application/context/prompt.ts Deletes legacy prompt rewrite/merge helpers.
packages/service/core/workflow/dispatch/ai/agentLoopCore/application/context/index.ts Removes re-export of deleted prompt module.
packages/service/core/workflow/dispatch/ai/agent/index.ts Switches to building final systemPrompt via buildDefaultAgentSystemPrompt after runtime creation; passes it into agent loop input.
packages/service/core/ai/llm/agentLoop/provider/piAgent/run.ts Removes provider-side prompt-mode branching; consumes input.systemPrompt directly.
packages/service/core/ai/llm/agentLoop/provider/fastAgent/loop/type.ts Removes promptMode from runtime type.
packages/service/core/ai/llm/agentLoop/provider/fastAgent/loop/index.ts Removes prompt-mode injection path; builds initial messages from input.systemPrompt + non-system conversation messages.
packages/service/core/ai/llm/agentLoop/provider/fastAgent/index.ts Stops threading removed promptMode into internal runtime.
packages/service/core/ai/llm/agentLoop/domain/runtime.ts Removes promptMode from LLM params type.
packages/service/core/ai/llm/agentLoop/domain/mainPrompt.ts Introduces buildDefaultAgentSystemPrompt (default + sandbox + extension + user section).
packages/service/core/ai/llm/agentLoop/domain/index.ts Re-exports mainPrompt domain module.
packages/service/core/ai/auxiliaryGeneration/type.ts Extends processor/run params to accept userAnswer; extends response to optionally return memories.
packages/service/core/ai/auxiliaryGeneration/service.ts Passes through userAnswer to auxiliary-generation processor.
packages/service/core/ai/auxiliaryGeneration/agentLoop.ts Reworks auxiliary-generation Agent Loop to enable ask_user, accept runtime tools/executor, forward deltas, and preserve pause/provider state.
.agents/design/core/ai/auxiliary-generation.md Updates design doc to match implemented auxiliary-generation Agent Loop semantics (ask pause/resume, tool injection, providerState persistence).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 100 to +104
/**
* 构建进入主 Agent 的初始消息链。
* raw 模式完全尊重调用方传入的 messages;fastAgent 模式会注入平台主提示词并剔除外部 system。
* systemPrompt 是调用方已经组装完成的最终提示词;messages 中的 system message 不再重复注入
*/
const buildInitialMessages = ({
input,
hasRuntimeTools,
promptMode = 'fastAgent'
}: {
input: FastAgentLoopInput;
hasRuntimeTools: boolean;
promptMode?: AgentLoopRuntime['promptMode'];
}): ChatCompletionMessageParam[] => {
if (promptMode === 'raw') {
return input.messages;
}

return [
createSystemMessage(
getMainAgentSystemPrompt({
systemPrompt: input.systemPrompt,
hasRuntimeTools
})
),
...stripSystemMessages(input.messages)
];
};
const buildInitialMessages = ({ input }: { input: FastAgentLoopInput }) => [
@c121914yu
c121914yu merged commit 199c0da into labring:main Aug 4, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants