Refactor agent loop and AI initialization for better lifecycle management#7
Open
Refactor agent loop and AI initialization for better lifecycle management#7
Conversation
Agent Loop: - Remove wasteful checkLoopFinish (extra LLM call per turn) - Use standard ReAct pattern: no tool calls + text = done - Add lifecycle hooks (onTurnStart, onTurnEnd, onToolCallStart, onToolCallEnd, onText, onError) - Add abort signal support via AbortController - Track turns with structured TurnInfo and return LoopResult - Properly preserve text alongside tool calls in messages Skill System: - Fix skill tool to use SkillRegistry singleton instead of re-scanning - Inject skill list into system prompt (as designed in docs) - Add slash command support (/skill-name args) in CLI - Add /skills command to list available skills - Add fuzzy matching fallback in skill tool - Lazy tool construction: buildTools() called after registry init Types: - Add ToolCallInfo, TurnInfo, LoopResult, LoopHooks, LoopOptions - Add abortSignal to Context https://claude.ai/code/session_01Q5TnLskGfMzuS7VZLLXRCS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the agent loop architecture and AI initialization to improve lifecycle management, add comprehensive instrumentation hooks, and simplify the tool execution flow. The main changes include:
initializeAI()call after skill registry is readyLoopResultobjects containing turn-by-turn telemetryLoopHooksfor observability (turn start/end, tool execution, text generation, errors)checkLoopFinish()function that made an extra LLM call every turn; now uses standard ReAct pattern (no tool calls = done)/skill-name), skill listing (/skills), and better formatted output with timing/metricsKey Changes
AI & Loop Architecture
ai.ts: Removed staticBuiltinToolsMapinitialization; addedinitializeAI(tools)function that must be called after skill registry setuploop.ts: Complete rewrite with structuredLoopResultreturn type,LoopHooksfor lifecycle events, and removal of the extracheckLoopFinish()LLM callcore.ts: Updated initialization sequence: registry → build tools → initialize AI; added slash command parsing and skill invocationgenerateObject()function (was unused and relied on tool-calling for structured output)Tools & Skills
tools/index.ts: IntroducedbuildTools()factory function; separated static tools from dynamic skill tooltools/skill.ts: Changed from static export tocreateSkillTool()factory; now reads from initialized registry with fuzzy search fallbackprompt/system.ts: AddedbuildSkillsSection()to dynamically include loaded skills in system promptType System
typings/index.ts: AddedLoopResult,TurnInfo,ToolCallInfo,LoopHooks, andLoopOptionsinterfaces for better type safety and observabilityabortSignaltoContextfor external loop cancellationCLI Improvements
/skill-name [args]invokes skills directly/skillscommand lists all loaded skillsImplementation Details
MAX_ERROR_COUNThttps://claude.ai/code/session_01Q5TnLskGfMzuS7VZLLXRCS