[PR #188] [CLOSED] universal agent tool support #4498

Closed
opened 2026-10-22 04:08:09 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/188
Author: @adunato
Created: 4/22/2026
Status: ❌ Closed

Base: main ← Head: upstream-main


📝 Commits (10+)

  • 75f5196 docs: init HLD and implementation plan for CR001 chat_summary_tools
  • aa058ab feat: implement read_chat_summary and append_chat_summary built-in tools
  • 8bb7add docs: init HLD and implementation plan for CR002 universal_agent_tool_support
  • 2576ef2 feat: relocate and generalize agent tool resolution
  • b20a9a0 feat: add debug logging for agent tool calls and results
  • bc57719 feat: improve agent-pipeline logging to show both batchable and tool-using agent names
  • 01eca24 local: update gitignore for dev scripts
  • 4044ccc Merge branch 'change/CR001-chat_summary_tools'
  • 75629e6 Merge branch 'change/CR002-universal_agent_tool_support'
  • c551479 local: clean up draft design file

📊 Changes

9 files changed (+293 additions, -62 deletions)

View changed files

➕ change_requests/CR001_chat_summary_tools/HLD.md (+15 -0)
➕ change_requests/CR001_chat_summary_tools/IMPLEMENTATION_PLAN.md (+25 -0)
📝 packages/server/src/routes/generate.routes.ts (+171 -61)
📝 packages/server/src/services/agents/agent-executor.ts (+2 -0)
📝 packages/server/src/services/agents/agent-pipeline.ts (+1 -1)
📝 packages/server/src/services/tools/tool-executor.ts (+53 -0)
📝 packages/shared/src/types/agent.ts (+24 -0)
➕ start_dev_client.bat (+1 -0)
➕ start_dev_server.bat (+1 -0)

📄 Description

Why this change

Problem Statement:
Currently, the generation pipeline in the Marinara Engine contains a hardcoded restriction that only allows the
spotify agent to utilize function calling (tools). For all other agents—built-in or custom—the server ignores their
enabledTools configuration and sends them into the pipeline as "batchable" (text-only) agents. This prevents users
from extending the engine's capabilities via custom agent-tool interactions.

Solution:
This PR generalizes the tool resolution logic within the generate route. It relocates tool definition and Spotify
credential resolution to the beginning of the pipeline and iterates through all agents. For every agent with tools
enabled in its settings, the server now correctly attaches a toolContext, enabling true function-calling capabilities
for both custom and built-in agents.

What changed

Key Changes

  • generate.routes.ts:
    • Relocated tool resolution and custom tool fetching to the start of the generation flow.
    • Decoupled tool assignment from the hardcoded spotify type check.
    • Implemented a universal loop that builds and attaches toolContext to any resolvedAgent with active tools.
    • Optimized Spotify token refresh to occur early in the request lifecycle.
  • agent-executor.ts:
    • Added real-time terminal logging for tool calls and results using the [agent-tools] tag, following existing
      project conventions.

Validation

  • pnpm check
  • Manual verification completed (describe below)

Manual verification notes

Verification Performed

  • Prerequisites: Test Agent enabled with instruction to invoke roll_dice tool (tool enabled in test agent configuration), tools enabled in chat.

  • Orchestration Test: Confirmed via server logs that a custom agent now reports as 1 tool-using instead of 1
    batchable.

  • Functionality Test: Verified that a custom agent can successfully call the roll_dice tool, receive the JSON result,
    and incorporate it into its final response.

  • Logging Test: Verified that the new [agent-tools] logs correctly display the function name, arguments, and raw
    results in the terminal.

  • Cross-check: compared before / after change server logging with the same test to demonstrate tool calling behaviour (before:[agent-pipeline] executeGroup: 1 batchable, 0 tool-using [ 'custom-test-agent' ]. after: [agent-pipeline] executeGroup: 0 batchable, 1 tool-using []

  • OUT of scope of validation: existing tools. The only agent that seem to be enabled for tools and has tools in its prompt is "Spotify DJ", the others either don't have tools enabled and/or do not mention tools in their prompt.

  • Merge recommendations: regression testing on Spotify DJ agent, as I was unable to test it.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (Not applicable)

Additional Information

Logging Information

Prior to the change:
[generate] chatId=Dwlm4t37BhFxx6sPbpB31, chatMode=roleplay
[timing] Embedding: 2ms
[generate] Resolved 1 agents for chat Dwlm4t37BhFxx6sPbpB31 (enableAgents=true, perChatList=true, activeIds=[custom-test-agent]) [ 'custom-test-agent(pre_generation)' ]
[timing] Prompt assembly + context: 49ms
[agent-pipeline] Phase "pre_generation": 1 agents → 1 group(s) [ '[custom-test-agent] (model: z-ai/glm-5.1)' ]
[agent-pipeline] executeGroup: 1 batchable, 0 tool-using [ 'custom-test-agent' ]
[agent-batch] Only 1 agent (custom-test-agent), running individually
[agent] custom-test-agent (Test Agent) — z-ai/glm-5.1
[OpenAI chatComplete()] stream=true model=z-ai/glm-5.1 onToken=true
[agent] custom-test-agent done (14 chars, 1565ms)
[timing] Pre-gen agents: 1567ms

After the change:
[generate] chatId=Dwlm4t37BhFxx6sPbpB31, chatMode=roleplay
[timing] Embedding: 3ms
[generate] Resolved 1 agents for chat Dwlm4t37BhFxx6sPbpB31 (enableAgents=true, perChatList=true, activeIds=[custom-test-agent]) [ 'custom-test-agent(pre_generation)' ]
[timing] Prompt assembly + context: 49ms
[agent-pipeline] Phase "pre_generation": 1 agents → 1 group(s) [ '[custom-test-agent] (model: z-ai/glm-5.1)' ]
[agent-pipeline] executeGroup: 0 batchable, 1 tool-using [ 'custom-test-agent' ]
[OpenAI chatComplete()] stream=true model=z-ai/glm-5.1 onToken=false
[agent-tools] custom-test-agent calling: roll_dice({"notation": "1d6", "reason": "General dice roll"})
[agent-tools] custom-test-agent result: {"notation":"1d6","rolls":[5],"sum":5,"modifier":0,"total":5,"reason":"General dice roll","display":"🎲 1d6 (General dice roll): [5] = 5"}
[OpenAI chatComplete()] stream=true model=z-ai/glm-5.1 onToken=false

Detailed Code Changes

  1. Key changes
  • The logic that queries the database for custom tools and builds the toolDefs array has been moved up to run before
    the agent pipeline is created.
  • The server now iterates through all resolvedAgents active in the chat.
  • For each agent, it checks if they have enabledTools specified in their settings (or fallback defaults).
  • If they do, the server filters the global list of tools down to just the ones the agent is allowed to use, and
    attaches a toolContext (containing those definitions and an executeToolCall function) directly to the agent.
  • Result: Any agent (built-in or custom) can now functionally use AI tools/functions to fetch data or take actions
    during their turn, rather than just generating static text.
  1. Preserving Existing Spotify Token Logic
    While generalizing the tool context, the specific token refresh logic required for the Spotify integration was
    carefully decoupled. It still runs early to refresh the access token if needed, and injects those spotifyCreds into
    the execution context so the Spotify tool can still authenticate properly when called by an agent.

  2. Enhanced Pipeline Debugging & Visibility
    The changes introduce specific console.log statements in the agent logic to improve observability:

  • agent-pipeline.ts: When executing a group of agents, the pipeline now explicitly logs which agents are running as
    "batchable" (text only) vs which are "tool-using" (capable of multi-step function calling).
  • agent-executor.ts: When a tool-using agent decides to call a function, it now logs the exact function name and
    arguments [agent-tools] <agent_type> calling: <func_name>().
  • It also logs a truncated version of the result returned by the tool before feeding it back into the LLM.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/188 **Author:** [@adunato](https://github.com/adunato) **Created:** 4/22/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `upstream-main` --- ### 📝 Commits (10+) - [`75f5196`](https://github.com/Pasta-Devs/Marinara-Engine/commit/75f51961b1d73e05d553e3253cab530f814acc57) docs: init HLD and implementation plan for CR001 chat_summary_tools - [`aa058ab`](https://github.com/Pasta-Devs/Marinara-Engine/commit/aa058abfbe9ea0a0d2e686d9d51f88e4619bb0ba) feat: implement read_chat_summary and append_chat_summary built-in tools - [`8bb7add`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8bb7add0fea12ba904875c74685e5ecfb28f4d71) docs: init HLD and implementation plan for CR002 universal_agent_tool_support - [`2576ef2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2576ef29299f02edb8959cf906d32180fdce1e6f) feat: relocate and generalize agent tool resolution - [`b20a9a0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b20a9a01e9c7b5243077b9a389f8b95e2cb7aafa) feat: add debug logging for agent tool calls and results - [`bc57719`](https://github.com/Pasta-Devs/Marinara-Engine/commit/bc577193b191565f568179c941bdf644b73640e3) feat: improve agent-pipeline logging to show both batchable and tool-using agent names - [`01eca24`](https://github.com/Pasta-Devs/Marinara-Engine/commit/01eca2408e698d36fb3c3f4ceeacba9a79fd1d28) local: update gitignore for dev scripts - [`4044ccc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4044ccc669e843f01d84edbd841e9247fb9dd99c) Merge branch 'change/CR001-chat_summary_tools' - [`75629e6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/75629e6ae1de6f2dddbfd5af00ac817c8aa7a97c) Merge branch 'change/CR002-universal_agent_tool_support' - [`c551479`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c551479be6e929c6091b6d7140803e116c612ccd) local: clean up draft design file ### 📊 Changes **9 files changed** (+293 additions, -62 deletions) <details> <summary>View changed files</summary> ➕ `change_requests/CR001_chat_summary_tools/HLD.md` (+15 -0) ➕ `change_requests/CR001_chat_summary_tools/IMPLEMENTATION_PLAN.md` (+25 -0) 📝 `packages/server/src/routes/generate.routes.ts` (+171 -61) 📝 `packages/server/src/services/agents/agent-executor.ts` (+2 -0) 📝 `packages/server/src/services/agents/agent-pipeline.ts` (+1 -1) 📝 `packages/server/src/services/tools/tool-executor.ts` (+53 -0) 📝 `packages/shared/src/types/agent.ts` (+24 -0) ➕ `start_dev_client.bat` (+1 -0) ➕ `start_dev_server.bat` (+1 -0) </details> ### 📄 Description ## Why this change Problem Statement: Currently, the generation pipeline in the Marinara Engine contains a hardcoded restriction that only allows the spotify agent to utilize function calling (tools). For all other agents—built-in or custom—the server ignores their enabledTools configuration and sends them into the pipeline as "batchable" (text-only) agents. This prevents users from extending the engine's capabilities via custom agent-tool interactions. Solution: This PR generalizes the tool resolution logic within the generate route. It relocates tool definition and Spotify credential resolution to the beginning of the pipeline and iterates through all agents. For every agent with tools enabled in its settings, the server now correctly attaches a toolContext, enabling true function-calling capabilities for both custom and built-in agents. ## What changed Key Changes - generate.routes.ts: - Relocated tool resolution and custom tool fetching to the start of the generation flow. - Decoupled tool assignment from the hardcoded spotify type check. - Implemented a universal loop that builds and attaches toolContext to any resolvedAgent with active tools. - Optimized Spotify token refresh to occur early in the request lifecycle. - agent-executor.ts: - Added real-time terminal logging for tool calls and results using the [agent-tools] tag, following existing project conventions. ## Validation - [x] `pnpm check` - [x] Manual verification completed (describe below) ### Manual verification notes Verification Performed - Prerequisites: Test Agent enabled with instruction to invoke roll_dice tool (tool enabled in test agent configuration), tools enabled in chat. - Orchestration Test: Confirmed via server logs that a custom agent now reports as 1 tool-using instead of 1 batchable. - Functionality Test: Verified that a custom agent can successfully call the roll_dice tool, receive the JSON result, and incorporate it into its final response. - Logging Test: Verified that the new [agent-tools] logs correctly display the function name, arguments, and raw results in the terminal. - Cross-check: compared before / after change server logging with the same test to demonstrate tool calling behaviour (before:[agent-pipeline] executeGroup: 1 batchable, 0 tool-using [ 'custom-test-agent' ]. after: [agent-pipeline] executeGroup: 0 batchable, 1 tool-using [] - OUT of scope of validation: existing tools. The only agent that seem to be enabled for tools and has tools in its prompt is "Spotify DJ", the others either don't have tools enabled and/or do not mention tools in their prompt. - **Merge recommendations: regression testing on Spotify DJ agent, as I was unable to test it.** - ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (Not applicable) ## Additional Information Logging Information Prior to the change: [generate] chatId=Dwlm4t37BhFxx6sPbpB31, chatMode=roleplay [timing] Embedding: 2ms [generate] Resolved 1 agents for chat Dwlm4t37BhFxx6sPbpB31 (enableAgents=true, perChatList=true, activeIds=[custom-test-agent]) [ 'custom-test-agent(pre_generation)' ] [timing] Prompt assembly + context: 49ms [agent-pipeline] Phase "pre_generation": 1 agents → 1 group(s) [ '[custom-test-agent] (model: z-ai/glm-5.1)' ] [agent-pipeline] executeGroup: 1 batchable, **0 tool-using [ 'custom-test-agent' ]** [agent-batch] Only 1 agent (custom-test-agent), running individually [agent] custom-test-agent (Test Agent) — z-ai/glm-5.1 [OpenAI chatComplete()] stream=true model=z-ai/glm-5.1 onToken=true [agent] custom-test-agent done (14 chars, 1565ms) [timing] Pre-gen agents: 1567ms After the change: [generate] chatId=Dwlm4t37BhFxx6sPbpB31, chatMode=roleplay [timing] Embedding: 3ms [generate] Resolved 1 agents for chat Dwlm4t37BhFxx6sPbpB31 (enableAgents=true, perChatList=true, activeIds=[custom-test-agent]) [ 'custom-test-agent(pre_generation)' ] [timing] Prompt assembly + context: 49ms [agent-pipeline] Phase "pre_generation": 1 agents → 1 group(s) [ '[custom-test-agent] (model: z-ai/glm-5.1)' ] [agent-pipeline] executeGroup: 0 batchable, **1 tool-using [ 'custom-test-agent' ]** [OpenAI chatComplete()] stream=true model=z-ai/glm-5.1 onToken=false [agent-tools] custom-test-agent calling: roll_dice({"notation": "1d6", "reason": "General dice roll"}) [agent-tools] custom-test-agent result: {"notation":"1d6","rolls":[5],"sum":5,"modifier":0,"total":5,"reason":"General dice roll","display":"🎲 1d6 (General dice roll): [5] = **5**"} [OpenAI chatComplete()] stream=true model=z-ai/glm-5.1 onToken=false Detailed Code Changes 1. Key changes * The logic that queries the database for custom tools and builds the toolDefs array has been moved up to run before the agent pipeline is created. * The server now iterates through all resolvedAgents active in the chat. * For each agent, it checks if they have enabledTools specified in their settings (or fallback defaults). * If they do, the server filters the global list of tools down to just the ones the agent is allowed to use, and attaches a toolContext (containing those definitions and an executeToolCall function) directly to the agent. * Result: Any agent (built-in or custom) can now functionally use AI tools/functions to fetch data or take actions during their turn, rather than just generating static text. 2. Preserving Existing Spotify Token Logic While generalizing the tool context, the specific token refresh logic required for the Spotify integration was carefully decoupled. It still runs early to refresh the access token if needed, and injects those spotifyCreds into the execution context so the Spotify tool can still authenticate properly when called by an agent. 3. Enhanced Pipeline Debugging & Visibility The changes introduce specific console.log statements in the agent logic to improve observability: * agent-pipeline.ts: When executing a group of agents, the pipeline now explicitly logs which agents are running as "batchable" (text only) vs which are "tool-using" (capable of multi-step function calling). * agent-executor.ts: When a tool-using agent decides to call a function, it now logs the exact function name and arguments [agent-tools] <agent_type> calling: <func_name>(<args>). * It also logs a truncated version of the result returned by the tool before feeding it back into the LLM. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:08:09 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4498
No description provided.