Skip to content

feat(agent): implement multi-step agent loop with tool-calling support - #1

Merged
Jonnytoshen merged 5 commits into
mainfrom
feat/agent-loop
May 19, 2026
Merged

feat(agent): implement multi-step agent loop with tool-calling support#1
Jonnytoshen merged 5 commits into
mainfrom
feat/agent-loop

Conversation

@Jonnytoshen

Copy link
Copy Markdown
Owner

Overview

Upgrades the single-turn streamText call to a multi-step agent loop that lets the model call tools, inspect results, and continue reasoning — all within a single user turn.

Changes

New: src/tools/

Tool Description
WeatherTool Query weather by city name (mock data for now)
CalculatorTool Evaluate math expressions

Both tools are re-exported via a barrel index.ts.

New: src/agent-loop.ts

Implements agentLoop(model, tools, messages, system):

  • Iterates over fullStream events: text-delta, tool-call, tool-result
  • Merges each step's messages back into the history so the model can reason over tool results
  • Exits when no tool calls are made in a step; hard-capped at MAX_STEPS = 10 to prevent runaway loops

Modified: src/index.ts

  • Replaces the inline streamText call with agentLoop()
  • Registers WeatherTool and CalculatorTool in the tools map
  • Updates the system prompt to reflect tool-use capability

Other

  • src/version.ts: reads the version string from package.json, removing the hardcoded literal from the startup banner
  • tsconfig.json: enables skipLibCheck to suppress type declaration errors introduced by the JSON module import

Testing

pnpm dev
# Try the following to verify tool calls:
# "What's the weather in Beijing today?"
# "What is 2 to the power of 10?"

Follow-up

  • Connect WeatherTool to a real weather API
  • Add unit tests for agentLoop step logic and tool execute functions
  • Consider making MAX_STEPS configurable

- WeatherTool: mock weather query by city name
- CalculatorTool: evaluate math expressions via Function constructor
- barrel index re-exports both tools
agentLoop() streams model output step by step, handles text-delta /
tool-call / tool-result events from fullStream, and exits when no
further tool calls are made; hard-capped at MAX_STEPS = 10
- Replace inline streamText call with agentLoop()
- Register WeatherTool and CalculatorTool in the tools map
- Update system prompt to reflect tool-use capability
- Bump startup banner to v0.2.0 - Agent Loop
Add src/version.ts to re-export the version string from package.json,
replacing the hardcoded literal in the startup banner of index.ts
Importing package.json via @types/node triggers type declaration errors;
skipLibCheck suppresses them without affecting project type safety
Copilot AI review requested due to automatic review settings May 19, 2026 03:01
@Jonnytoshen
Jonnytoshen merged commit fc29796 into main May 19, 2026
1 check failed
@Jonnytoshen
Jonnytoshen deleted the feat/agent-loop branch May 19, 2026 03:02
@Jonnytoshen
Jonnytoshen removed the request for review from Copilot May 19, 2026 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant