Skip to content

Conversation

christian-bromann
Copy link
Member

@christian-bromann christian-bromann commented Sep 23, 2025

Overview

This PR migrates the Deep Agents library from a custom LangGraph-based setup to the new langchain createAgent API with a middleware-driven architecture. It simplifies state management, standardizes agent creation, and aligns the project with current LangChain best practices.

Why

  • Reduce bespoke state/schema plumbing in favor of built-in agent capabilities.
  • Adopt a cleaner, composable middleware model for filesystem and todo operations.
  • Align with the latest langchain alpha APIs to enable future enhancements.

What Changed

  • Architecture

    • Replaced createReactAgent with createAgent.
    • Introduced middleware-based design (fsMiddleware, todoMiddleware).
    • Removed custom state scaffolding in favor of runtime middleware state.
  • Code

    • Deleted: src/state.ts, src/model.ts, src/interrupt.ts.
    • Moved tools to middleware: src/tools.tssrc/middleware/fs.ts; added src/middleware/todo.ts; new src/middleware/index.ts.
    • Refactored src/subAgent.ts:
      • Uses createAgent and middleware.
      • model can be a string or LanguageModelLike, defaulting to "openai:gpt-4o-mini".
      • Removed internal hardcoded/built-in tools map; resolution now relies solely on provided tools.
      • When subagent.tools is unspecified, all provided tools are used.
    • Updated src/types.ts:
      • Added SubAgentSchema (Zod) and SubAgent type based on it.
      • Simplified CreateDeepAgentParams; interruptConfig now uses HumanInTheLoopMiddlewareConfig["toolConfigs"].
      • Simplified CreateTaskToolParams generic to z.ZodObject.
    • Config and examples:
      • tsconfig.json: fix extends path to @tsconfig/recommended/tsconfig.json.
      • examples/research/research-agent.ts: updated to new API.
  • Dependencies

    • Bumped ESLint/TypeScript-related deps.
    • Upgraded to langchain alpha and latest related packages (@langchain/langgraph@next, @langchain/anthropic@alpha).
    • Updated lockfile accordingly.

Breaking Changes

  • Removal of DeepAgentState and custom reducers (src/state.ts).
  • Removal of built-in tools registry inside subAgent.ts:
    • Callers must pass tools explicitly (by name via subagent.tools mapping into the provided tools object).
  • createTaskTool signature changes:
    • stateSchema no longer required.
    • model accepts either a model string or a LanguageModelLike.
  • Interrupts/state handling now flows through middleware; any prior usage of interrupt.ts patterns must be updated.

Migration Guide

  • Replace any createReactAgent usage with createAgent.
  • Pass tools explicitly via tools: Record<string, StructuredTool>; if a subagent specifies no tools, all provided tools will be used.
  • Remove dependencies on DeepAgentState; move stateful behaviors into middleware or rely on the runtime state the agent manages.
  • Update types/imports to use SubAgentSchema / SubAgent and the simplified CreateDeepAgentParams.
  • Ensure your model is provided as a string (e.g., "openai:gpt-4o-mini") or as a LanguageModelLike.

BREAKING CHANGE: Major refactor from custom LangGraph implementation to new langchain createAgent API

**Architecture Migration:**
- Replace createReactAgent with createAgent API from langchain package
- Migrate from custom state management to middleware-based architecture
- Remove custom DeepAgentState and model configuration in favor of built-in patterns

**Package Updates:**
- Upgrade to @langchain/anthropic@alpha, @langchain/langgraph@next
- Add langchain package as main dependency with createAgent support
- Update @langchain/core to local development version

**Code Reorganization:**
- Move tools from src/tools.ts to modular middleware pattern (src/middleware/)
- Simplify type definitions in src/types.ts with Zod schema validation
- Refactor src/subAgent.ts to use createAgent with middleware integration
- Update src/graph.ts and src/index.ts for new API patterns

**Removed Files:**
- src/state.ts: Custom state management no longer needed
- src/model.ts: Default model configuration handled by createAgent
- src/interrupt.ts: Interrupt handling integrated into middleware

**New Middleware System:**
- src/middleware/fs.ts: File system operations middleware
- src/middleware/todo.ts: Todo management middleware
- src/middleware/index.ts: Middleware exports and configuration

**Configuration:**
- Fix tsconfig.json extends path for @tsconfig/recommended
- Update examples/research/research-agent.ts for new API
- Upgrade TypeScript ESLint and other dev dependencies

This migration provides a more maintainable architecture with better separation of concerns
and aligns with the latest LangChain patterns for agent development.
@christian-bromann christian-bromann marked this pull request as ready for review September 26, 2025 22:39
wuchangming and others added 2 commits September 29, 2025 20:43
@christian-bromann
Copy link
Member Author

Unfortunately this is blocked again until we do another alpha release.

@amitchaudharyc
Copy link

Hey @christian-bromann , glad to see the changes. But can you help me understand why the content doesn't get streamed token-by-token? It always generate one-off responses.

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.

3 participants