deepagents 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +151 -0
- package/dist/graph.d.ts +46 -0
- package/dist/graph.js +44 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +15 -0
- package/dist/model.d.ts +4 -0
- package/dist/model.js +25 -0
- package/dist/prompts.d.ts +5 -0
- package/dist/prompts.js +102 -0
- package/dist/state.d.ts +15 -0
- package/dist/state.js +31 -0
- package/dist/subAgent.d.ts +30 -0
- package/dist/subAgent.js +72 -0
- package/dist/tools.d.ts +105 -0
- package/dist/tools.js +167 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Oleksandr Shevtsov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
## This is an unofficial port of the [hwchase17/deepagents](https://github.com/hwchase17/deepagents) Python package to TypeScript
|
|
2
|
+
|
|
3
|
+
# 🧠🤖Deep Agents
|
|
4
|
+
|
|
5
|
+
Using an LLM to call tools in a loop is the simplest form of an agent.
|
|
6
|
+
This architecture, however, can yield agents that are “shallow” and fail to plan and act over longer, more complex tasks.
|
|
7
|
+
Applications like “Deep Research”, "Manus", and “Claude Code” have gotten around this limitation by implementing a combination of four things:
|
|
8
|
+
a **planning tool**, **sub agents**, access to a **file system**, and a **detailed prompt**.
|
|
9
|
+
|
|
10
|
+
<img src="deep_agents.png" alt="deep agent" width="600"/>
|
|
11
|
+
|
|
12
|
+
`deepagents` is a Typescript package that implements these in a general purpose way so that you can easily create a Deep Agent for your application.
|
|
13
|
+
|
|
14
|
+
**Acknowledgements: This project was primarily inspired by Claude Code, and initially was largely an attempt to see what made Claude Code general purpose, and make it even more so.**
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install deepagents
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
> NB! This part of documentation is not fully migrated
|
|
25
|
+
|
|
26
|
+
See [src/examples/research/agent.ts](src/examples/research/agent.ts) for a more complex example.
|
|
27
|
+
|
|
28
|
+
The agent created with `createDeepAgent` is just a LangGraph graph - so you can interact with it (streaming, human-in-the-loop, memory, studio)
|
|
29
|
+
in the same way you would any LangGraph agent.
|
|
30
|
+
|
|
31
|
+
## Creating a custom deep agent
|
|
32
|
+
|
|
33
|
+
There is an option argument with three properties you can pass to `createDeepAgent` to create your own custom deep agent.
|
|
34
|
+
|
|
35
|
+
### `tools` (Required)
|
|
36
|
+
|
|
37
|
+
This should be a list of functions or LangChain `@tool` objects.
|
|
38
|
+
The agent (and any subagents) will have access to these tools.
|
|
39
|
+
|
|
40
|
+
### `instructions` (Required)
|
|
41
|
+
|
|
42
|
+
This will serve as part of the prompt of the deep agent.
|
|
43
|
+
Note that there is a [built in system prompt](#built-in-prompt) as well, so this is not the _entire_ prompt the agent will see.
|
|
44
|
+
|
|
45
|
+
### `subagents` (Optional)
|
|
46
|
+
|
|
47
|
+
This can be used to specify any custom subagents this deep agent will have access to.
|
|
48
|
+
You can read more about why you would want to use subagents [here](#sub-agents)
|
|
49
|
+
`subagents` should be a list of dictionaries, where each dictionary follow this schema:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
export interface SubAgent {
|
|
53
|
+
name: string;
|
|
54
|
+
description: string;
|
|
55
|
+
prompt: string;
|
|
56
|
+
tools?: string[];
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
- **name**: This is the name of the subagent, and how the main agent will call the subagent
|
|
61
|
+
- **description**: This is the description of the subagent that is shown to the main agent
|
|
62
|
+
- **prompt**: This is the prompt used for the subagent
|
|
63
|
+
- **tools**: This is the list of tools that the subagent has access to. By default will have access to all tools passed in, as well as all built-in tools.
|
|
64
|
+
|
|
65
|
+
To use it looks like:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import { createDeepAgent, SubAgent } from "deepagents";
|
|
69
|
+
|
|
70
|
+
const researchSubAgent: SubAgent = {
|
|
71
|
+
name: "research-agent",
|
|
72
|
+
description: "Used to research more in depth questions...",
|
|
73
|
+
prompt: subResearchPrompt,
|
|
74
|
+
tools: ["internet_search"],
|
|
75
|
+
};
|
|
76
|
+
const agent = createDeepAgent({
|
|
77
|
+
instructions: researchInstructions,
|
|
78
|
+
subagents: [researchSubAgent],
|
|
79
|
+
tools: [internetSearch],
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### `model` (Optional)
|
|
84
|
+
|
|
85
|
+
By default, `deepagents` will use local LM Studio. If you want to use a different model,
|
|
86
|
+
you can pass a [LangChain model object](https://js.langchain.com/docs/integrations/chat/).
|
|
87
|
+
|
|
88
|
+
## Deep Agent Details
|
|
89
|
+
|
|
90
|
+
The below components are built into `deepagents` and helps make it work for deep tasks off-the-shelf.
|
|
91
|
+
|
|
92
|
+
### System Prompt
|
|
93
|
+
|
|
94
|
+
`deepagents` comes with a [built-in system prompt](src/prompts.ts). This is relatively detailed prompt that is heavily based on and inspired by [attempts](https://github.com/kn1026/cc/blob/main/claudecode.md) to [replicate](https://github.com/asgeirtj/system_prompts_leaks/blob/main/Anthropic/claude-code.md)
|
|
95
|
+
Claude Code's system prompt. It was made more general purpose than Claude Code's system prompt.
|
|
96
|
+
This contains detailed instructions for how to use the built-in planning tool, file system tools, and sub agents.
|
|
97
|
+
Note that part of this system prompt [can be customized](#promptprefix--required-)
|
|
98
|
+
|
|
99
|
+
Without this default system prompt - the agent would not be nearly as successful at going as it is.
|
|
100
|
+
The importance of prompting for creating a "deep" agent cannot be understated.
|
|
101
|
+
|
|
102
|
+
### Planing Tool
|
|
103
|
+
|
|
104
|
+
`deepagents` comes with a built-in planning tool. This planning tool is very simple and is based on ClaudeCode's TodoWrite tool.
|
|
105
|
+
This tool doesn't actually do anything - it is just a way for the agent to come up with a plan, and then have that in the context to help keep it on track.
|
|
106
|
+
|
|
107
|
+
### File System Tools
|
|
108
|
+
|
|
109
|
+
`deepagents` comes with four built-in file system tools: `ls`, `edit_file`, `read_file`, `write_file`.
|
|
110
|
+
These do not actually use a file system - rather, they mock out a file system using LangGraph's State object.
|
|
111
|
+
This means you can easily run many of these agents on the same machine without worrying that they will edit the same underlying files.
|
|
112
|
+
|
|
113
|
+
Right now the "file system" will only be one level deep (no sub directories).
|
|
114
|
+
|
|
115
|
+
These files can be passed in (and also retrieved) by using the `files` key in the LangGraph State object.
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
import { createDeepAgent } from "deepagents";
|
|
119
|
+
|
|
120
|
+
const agent = createDeepAgent({
|
|
121
|
+
instructions: researchInstructions,
|
|
122
|
+
subagents: [critiqueSubAgent, researchSubAgent],
|
|
123
|
+
tools: [internetSearch],
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const result = await agent.invoke({
|
|
127
|
+
messages: [new HumanMessage("what is langchain?")],
|
|
128
|
+
files: [{"README.md": "# Deep Agents\n\nThis is a README file for the deep agents example."}],
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
# Access any files afterwards like this
|
|
132
|
+
result.files
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Sub Agents
|
|
136
|
+
|
|
137
|
+
`deepagents` comes with the built-in ability to call sub agents (based on Claude Code).
|
|
138
|
+
It has access to a `general-purpose` subagent at all times - this is a subagent with the same instructions as the main agent and all the tools that is has access to.
|
|
139
|
+
You can also specify [custom sub agents](#subagents--optional-) with their own instructions and tools.
|
|
140
|
+
|
|
141
|
+
Sub agents are useful for ["context quarantine"](https://www.dbreunig.com/2025/06/26/how-to-fix-your-context.html#context-quarantine) (to help not pollute the overall context of the main agent)
|
|
142
|
+
as well as custom instructions.
|
|
143
|
+
|
|
144
|
+
## Roadmap
|
|
145
|
+
|
|
146
|
+
- [ ] Allow users to customize full system prompt
|
|
147
|
+
- [ ] Code cleanliness (type hinting, docstrings, formating)
|
|
148
|
+
- [ ] Allow for more of a robust virtual filesystem
|
|
149
|
+
- [ ] Create an example of a deep coding agent built on top of this
|
|
150
|
+
- [ ] Benchmark the example of [deep research agent](src/examples/research/agent.ts)
|
|
151
|
+
- [ ] Add human-in-the-loop support for tools
|
package/dist/graph.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { StructuredTool } from "@langchain/core/tools";
|
|
2
|
+
import { LanguageModelLike } from "@langchain/core/language_models/base";
|
|
3
|
+
import { SubAgent } from "./subAgent.js";
|
|
4
|
+
import { DeepAgentState } from "./state.js";
|
|
5
|
+
export interface CreateDeepAgentOptions {
|
|
6
|
+
tools: StructuredTool[];
|
|
7
|
+
instructions: string;
|
|
8
|
+
model?: string | LanguageModelLike;
|
|
9
|
+
subagents?: SubAgent[];
|
|
10
|
+
stateSchema?: typeof DeepAgentState;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Create a deep agent.
|
|
14
|
+
*
|
|
15
|
+
* This agent will by default have access to a tool to write todos (write_todos),
|
|
16
|
+
* and then four file editing tools: write_file, ls, read_file, edit_file.
|
|
17
|
+
*
|
|
18
|
+
* @param options Configuration options for the deep agent
|
|
19
|
+
* @returns A LangGraph agent
|
|
20
|
+
*/
|
|
21
|
+
export declare function createDeepAgent(options: CreateDeepAgentOptions): import("@langchain/langgraph").CompiledStateGraph<import("@langchain/langgraph").StateType<{
|
|
22
|
+
messages: import("@langchain/langgraph").BinaryOperatorAggregate<import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[], import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[]>;
|
|
23
|
+
todos: import("@langchain/langgraph").BinaryOperatorAggregate<import("./state.js").Todo[], import("./state.js").Todo[]>;
|
|
24
|
+
files: import("@langchain/langgraph").BinaryOperatorAggregate<Record<string, string>, Record<string, string>>;
|
|
25
|
+
}>, import("@langchain/langgraph").UpdateType<{
|
|
26
|
+
messages: import("@langchain/langgraph").BinaryOperatorAggregate<import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[], import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[]>;
|
|
27
|
+
todos: import("@langchain/langgraph").BinaryOperatorAggregate<import("./state.js").Todo[], import("./state.js").Todo[]>;
|
|
28
|
+
files: import("@langchain/langgraph").BinaryOperatorAggregate<Record<string, string>, Record<string, string>>;
|
|
29
|
+
}>, any, {
|
|
30
|
+
messages: import("@langchain/langgraph").BinaryOperatorAggregate<import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[], import("@langchain/langgraph").Messages>;
|
|
31
|
+
} & {
|
|
32
|
+
messages: import("@langchain/langgraph").BinaryOperatorAggregate<import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[], import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[]>;
|
|
33
|
+
todos: import("@langchain/langgraph").BinaryOperatorAggregate<import("./state.js").Todo[], import("./state.js").Todo[]>;
|
|
34
|
+
files: import("@langchain/langgraph").BinaryOperatorAggregate<Record<string, string>, Record<string, string>>;
|
|
35
|
+
}, {
|
|
36
|
+
messages: import("@langchain/langgraph").BinaryOperatorAggregate<import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[], import("@langchain/langgraph").Messages>;
|
|
37
|
+
structuredResponse: {
|
|
38
|
+
(): import("@langchain/langgraph").LastValue<Record<string, any>>;
|
|
39
|
+
(annotation: import("@langchain/langgraph").SingleReducer<Record<string, any>, Record<string, any>>): import("@langchain/langgraph").BinaryOperatorAggregate<Record<string, any>, Record<string, any>>;
|
|
40
|
+
Root: <S extends import("@langchain/langgraph").StateDefinition>(sd: S) => import("@langchain/langgraph").AnnotationRoot<S>;
|
|
41
|
+
};
|
|
42
|
+
} & {
|
|
43
|
+
messages: import("@langchain/langgraph").BinaryOperatorAggregate<import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[], import("@langchain/core/messages", { with: { "resolution-mode": "import" } }).BaseMessage[]>;
|
|
44
|
+
todos: import("@langchain/langgraph").BinaryOperatorAggregate<import("./state.js").Todo[], import("./state.js").Todo[]>;
|
|
45
|
+
files: import("@langchain/langgraph").BinaryOperatorAggregate<Record<string, string>, Record<string, string>>;
|
|
46
|
+
}, import("@langchain/langgraph").StateDefinition>;
|
package/dist/graph.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDeepAgent = createDeepAgent;
|
|
4
|
+
const prebuilt_1 = require("@langchain/langgraph/prebuilt");
|
|
5
|
+
const subAgent_js_1 = require("./subAgent.js");
|
|
6
|
+
const state_js_1 = require("./state.js");
|
|
7
|
+
const model_js_1 = require("./model.js");
|
|
8
|
+
const tools_js_1 = require("./tools.js");
|
|
9
|
+
const BASE_PROMPT = `You have access to a number of standard tools
|
|
10
|
+
|
|
11
|
+
## \`write_todos\`
|
|
12
|
+
|
|
13
|
+
You have access to the \`write_todos\` tools to help you manage and plan tasks. Use these tools VERY frequently to ensure that you are tracking your tasks and giving the user visibility into your progress.
|
|
14
|
+
These tools are also EXTREMELY helpful for planning tasks, and for breaking down larger complex tasks into smaller steps. If you do not use this tool when planning, you may forget to do important tasks - and that is unacceptable.
|
|
15
|
+
|
|
16
|
+
It is critical that you mark todos as completed as soon as you are done with a task. Do not batch up multiple tasks before marking them as completed.
|
|
17
|
+
|
|
18
|
+
## \`task\`
|
|
19
|
+
|
|
20
|
+
- When doing web search, prefer to use the \`task\` tool in order to reduce context usage.`;
|
|
21
|
+
/**
|
|
22
|
+
* Create a deep agent.
|
|
23
|
+
*
|
|
24
|
+
* This agent will by default have access to a tool to write todos (write_todos),
|
|
25
|
+
* and then four file editing tools: write_file, ls, read_file, edit_file.
|
|
26
|
+
*
|
|
27
|
+
* @param options Configuration options for the deep agent
|
|
28
|
+
* @returns A LangGraph agent
|
|
29
|
+
*/
|
|
30
|
+
function createDeepAgent(options) {
|
|
31
|
+
const { tools, instructions, model, subagents = [], stateSchema = state_js_1.DeepAgentState, } = options;
|
|
32
|
+
const prompt = instructions + BASE_PROMPT;
|
|
33
|
+
const builtInTools = [tools_js_1.writeTodos, tools_js_1.writeFile, tools_js_1.readFile, tools_js_1.ls, tools_js_1.editFile];
|
|
34
|
+
//TODO: lookup for model that has better tool calling capabilities
|
|
35
|
+
const actualModel = model || (0, model_js_1.getLocalLMStudio)() || (0, model_js_1.getDefaultModel)();
|
|
36
|
+
const taskTool = (0, subAgent_js_1.createTaskTool)([...tools, ...builtInTools], instructions, subagents, actualModel, stateSchema);
|
|
37
|
+
const allTools = [taskTool, ...builtInTools, ...tools];
|
|
38
|
+
return (0, prebuilt_1.createReactAgent)({
|
|
39
|
+
llm: actualModel,
|
|
40
|
+
tools: allTools,
|
|
41
|
+
messageModifier: prompt,
|
|
42
|
+
stateSchema,
|
|
43
|
+
});
|
|
44
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createDeepAgent } from "./graph.js";
|
|
2
|
+
export { DeepAgentState, DeepAgentStateType, Todo, Files } from "./state.js";
|
|
3
|
+
export { SubAgent } from "./subAgent.js";
|
|
4
|
+
export { getDefaultModel } from "./model.js";
|
|
5
|
+
export { writeTodos, writeFile, readFile, ls, editFile } from "./tools.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.editFile = exports.ls = exports.readFile = exports.writeFile = exports.writeTodos = exports.getDefaultModel = exports.DeepAgentState = exports.createDeepAgent = void 0;
|
|
4
|
+
var graph_js_1 = require("./graph.js");
|
|
5
|
+
Object.defineProperty(exports, "createDeepAgent", { enumerable: true, get: function () { return graph_js_1.createDeepAgent; } });
|
|
6
|
+
var state_js_1 = require("./state.js");
|
|
7
|
+
Object.defineProperty(exports, "DeepAgentState", { enumerable: true, get: function () { return state_js_1.DeepAgentState; } });
|
|
8
|
+
var model_js_1 = require("./model.js");
|
|
9
|
+
Object.defineProperty(exports, "getDefaultModel", { enumerable: true, get: function () { return model_js_1.getDefaultModel; } });
|
|
10
|
+
var tools_js_1 = require("./tools.js");
|
|
11
|
+
Object.defineProperty(exports, "writeTodos", { enumerable: true, get: function () { return tools_js_1.writeTodos; } });
|
|
12
|
+
Object.defineProperty(exports, "writeFile", { enumerable: true, get: function () { return tools_js_1.writeFile; } });
|
|
13
|
+
Object.defineProperty(exports, "readFile", { enumerable: true, get: function () { return tools_js_1.readFile; } });
|
|
14
|
+
Object.defineProperty(exports, "ls", { enumerable: true, get: function () { return tools_js_1.ls; } });
|
|
15
|
+
Object.defineProperty(exports, "editFile", { enumerable: true, get: function () { return tools_js_1.editFile; } });
|
package/dist/model.d.ts
ADDED
package/dist/model.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDefaultModel = getDefaultModel;
|
|
4
|
+
exports.getLocalLMStudio = getLocalLMStudio;
|
|
5
|
+
const anthropic_1 = require("@langchain/anthropic");
|
|
6
|
+
const openai_1 = require("@langchain/openai");
|
|
7
|
+
function getDefaultModel() {
|
|
8
|
+
return new anthropic_1.ChatAnthropic({
|
|
9
|
+
modelName: "claude-3-5-sonnet-20241022",
|
|
10
|
+
maxTokens: 64000,
|
|
11
|
+
temperature: 0,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function getLocalLMStudio() {
|
|
15
|
+
process.env.OPENAI_API_KEY = "lm-studio";
|
|
16
|
+
return new openai_1.ChatOpenAI({
|
|
17
|
+
modelName: "mlx-community/llama-3.2-3b-instruct:2",
|
|
18
|
+
openAIApiKey: "lm-studio",
|
|
19
|
+
configuration: {
|
|
20
|
+
baseURL: "http://localhost:1234/v1",
|
|
21
|
+
},
|
|
22
|
+
maxTokens: 512,
|
|
23
|
+
temperature: 0.3,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const WRITE_TODOS_DESCRIPTION = "Use this tool to create and manage a structured task list for your current work session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.\nIt also helps the user understand the progress of the task and overall progress of their requests.\n\n## When to Use This Tool\nUse this tool proactively in these scenarios:\n\n1. Complex multi-step tasks - When a task requires 3 or more distinct steps or actions\n2. Non-trivial and complex tasks - Tasks that require careful planning or multiple operations\n3. User explicitly requests todo list - When the user directly asks you to use the todo list\n4. User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)\n5. After receiving new instructions - Immediately capture user requirements as todos\n6. When you start working on a task - Mark it as in_progress BEFORE beginning work. Ideally you should only have one todo as in_progress at a time\n7. After completing a task - Mark it as completed and add any new follow-up tasks discovered during implementation\n\n## When NOT to Use This Tool\n\nSkip using this tool when:\n1. There is only a single, straightforward task\n2. The task is trivial and tracking it provides no organizational benefit\n3. The task can be completed in less than 3 trivial steps\n4. The task is purely conversational or informational\n\nNOTE that you should not use this tool if there is only one trivial task to do. In this case you are better off just doing the task directly.\n\n## Task States and Management\n\n1. **Task States**: Use these states to track progress:\n - pending: Task not yet started\n - in_progress: Currently working on (limit to ONE task at a time)\n - completed: Task finished successfully\n\n2. **Task Management**:\n - Update task status in real-time as you work\n - Mark tasks complete IMMEDIATELY after finishing (don't batch completions)\n - Only have ONE task in_progress at any time\n - Complete current tasks before starting new ones\n - Remove tasks that are no longer relevant from the list entirely\n\n3. **Task Completion Requirements**:\n - ONLY mark a task as completed when you have FULLY accomplished it\n - If you encounter errors, blockers, or cannot finish, keep the task as in_progress\n - When blocked, create a new task describing what needs to be resolved\n - Never mark a task as completed if:\n - There are unresolved issues or errors\n - Work is partial or incomplete\n - You encountered blockers that prevent completion\n - You couldn't find necessary resources or dependencies\n - Quality standards haven't been met\n\n4. **Task Breakdown**:\n - Create specific, actionable items\n - Break complex tasks into smaller, manageable steps\n - Use clear, descriptive task names\n\nWhen in doubt, use this tool. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully.";
|
|
2
|
+
export declare const TASK_DESCRIPTION_PREFIX = "Launch a new agent to handle complex, multi-step tasks autonomously. \n\nAvailable agent types and the tools they have access to:\n- general-purpose: General-purpose agent for researching complex questions, searching for files and content, and executing multi-step tasks. When you are searching for a keyword or file and are not confident that you will find the right match in the first few tries use this agent to perform the search for you. (Tools: *)\n{other_agents}\n";
|
|
3
|
+
export declare const TASK_DESCRIPTION_SUFFIX = "When using the Task tool, you must specify a subagent_type parameter to select which agent type to use.\n\nWhen to use the Agent tool:\n- When you are instructed to execute custom slash commands. Use the Agent tool with the slash command invocation as the entire prompt. The slash command can take arguments. For example: Task(description=\"Check the file\", prompt=\"/check-file path/to/file.py\")\n\nWhen NOT to use the Agent tool:\n- If you want to read a specific file path, use the Read or Glob tool instead of the Agent tool, to find the match more quickly\n- If you are searching for a specific term or definition within a known location, use the Glob tool instead, to find the match more quickly\n- If you are searching for content within a specific file or set of 2-3 files, use the Read tool instead of the Agent tool, to find the match more quickly\n- Other tasks that are not related to the agent descriptions above\n\nUsage notes:\n1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses\n2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.\n3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.\n4. The agent's outputs should generally be trusted\n5. Clearly tell the agent whether you expect it to create content, perform analysis, or just do research (search, file reads, web fetches, etc.), since it is not aware of the user's intent\n6. If the agent description mentions that it should be used proactively, then you should try your best to use it without the user having to ask for it first. Use your judgement.";
|
|
4
|
+
export declare const EDIT_DESCRIPTION = "Performs exact string replacements in files. \n\nUsage:\n- You must use your Read tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file. \n- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the old_string or new_string.\n- ALWAYS prefer editing existing files. NEVER write new files unless explicitly required.\n- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.\n- The edit will FAIL if old_string is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use replace_all to change every instance of old_string. \n- Use replace_all for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.";
|
|
5
|
+
export declare const TOOL_DESCRIPTION = "Reads a file from the local filesystem. You can access any file directly by using this tool.\nAssume this tool is able to read all files on the machine. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.\n\nUsage:\n- The file_path parameter must be an absolute path, not a relative path\n- By default, it reads up to 2000 lines starting from the beginning of the file\n- You can optionally specify a line offset and limit (especially handy for long files), but it's recommended to read the whole file by not providing these parameters\n- Any lines longer than 2000 characters will be truncated\n- Results are returned using cat -n format, with line numbers starting at 1\n- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful. \n- If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents.";
|
package/dist/prompts.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TOOL_DESCRIPTION = exports.EDIT_DESCRIPTION = exports.TASK_DESCRIPTION_SUFFIX = exports.TASK_DESCRIPTION_PREFIX = exports.WRITE_TODOS_DESCRIPTION = void 0;
|
|
4
|
+
exports.WRITE_TODOS_DESCRIPTION = `Use this tool to create and manage a structured task list for your current work session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
|
|
5
|
+
It also helps the user understand the progress of the task and overall progress of their requests.
|
|
6
|
+
|
|
7
|
+
## When to Use This Tool
|
|
8
|
+
Use this tool proactively in these scenarios:
|
|
9
|
+
|
|
10
|
+
1. Complex multi-step tasks - When a task requires 3 or more distinct steps or actions
|
|
11
|
+
2. Non-trivial and complex tasks - Tasks that require careful planning or multiple operations
|
|
12
|
+
3. User explicitly requests todo list - When the user directly asks you to use the todo list
|
|
13
|
+
4. User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)
|
|
14
|
+
5. After receiving new instructions - Immediately capture user requirements as todos
|
|
15
|
+
6. When you start working on a task - Mark it as in_progress BEFORE beginning work. Ideally you should only have one todo as in_progress at a time
|
|
16
|
+
7. After completing a task - Mark it as completed and add any new follow-up tasks discovered during implementation
|
|
17
|
+
|
|
18
|
+
## When NOT to Use This Tool
|
|
19
|
+
|
|
20
|
+
Skip using this tool when:
|
|
21
|
+
1. There is only a single, straightforward task
|
|
22
|
+
2. The task is trivial and tracking it provides no organizational benefit
|
|
23
|
+
3. The task can be completed in less than 3 trivial steps
|
|
24
|
+
4. The task is purely conversational or informational
|
|
25
|
+
|
|
26
|
+
NOTE that you should not use this tool if there is only one trivial task to do. In this case you are better off just doing the task directly.
|
|
27
|
+
|
|
28
|
+
## Task States and Management
|
|
29
|
+
|
|
30
|
+
1. **Task States**: Use these states to track progress:
|
|
31
|
+
- pending: Task not yet started
|
|
32
|
+
- in_progress: Currently working on (limit to ONE task at a time)
|
|
33
|
+
- completed: Task finished successfully
|
|
34
|
+
|
|
35
|
+
2. **Task Management**:
|
|
36
|
+
- Update task status in real-time as you work
|
|
37
|
+
- Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
|
|
38
|
+
- Only have ONE task in_progress at any time
|
|
39
|
+
- Complete current tasks before starting new ones
|
|
40
|
+
- Remove tasks that are no longer relevant from the list entirely
|
|
41
|
+
|
|
42
|
+
3. **Task Completion Requirements**:
|
|
43
|
+
- ONLY mark a task as completed when you have FULLY accomplished it
|
|
44
|
+
- If you encounter errors, blockers, or cannot finish, keep the task as in_progress
|
|
45
|
+
- When blocked, create a new task describing what needs to be resolved
|
|
46
|
+
- Never mark a task as completed if:
|
|
47
|
+
- There are unresolved issues or errors
|
|
48
|
+
- Work is partial or incomplete
|
|
49
|
+
- You encountered blockers that prevent completion
|
|
50
|
+
- You couldn't find necessary resources or dependencies
|
|
51
|
+
- Quality standards haven't been met
|
|
52
|
+
|
|
53
|
+
4. **Task Breakdown**:
|
|
54
|
+
- Create specific, actionable items
|
|
55
|
+
- Break complex tasks into smaller, manageable steps
|
|
56
|
+
- Use clear, descriptive task names
|
|
57
|
+
|
|
58
|
+
When in doubt, use this tool. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully.`;
|
|
59
|
+
exports.TASK_DESCRIPTION_PREFIX = `Launch a new agent to handle complex, multi-step tasks autonomously.
|
|
60
|
+
|
|
61
|
+
Available agent types and the tools they have access to:
|
|
62
|
+
- general-purpose: General-purpose agent for researching complex questions, searching for files and content, and executing multi-step tasks. When you are searching for a keyword or file and are not confident that you will find the right match in the first few tries use this agent to perform the search for you. (Tools: *)
|
|
63
|
+
{other_agents}
|
|
64
|
+
`;
|
|
65
|
+
exports.TASK_DESCRIPTION_SUFFIX = `When using the Task tool, you must specify a subagent_type parameter to select which agent type to use.
|
|
66
|
+
|
|
67
|
+
When to use the Agent tool:
|
|
68
|
+
- When you are instructed to execute custom slash commands. Use the Agent tool with the slash command invocation as the entire prompt. The slash command can take arguments. For example: Task(description="Check the file", prompt="/check-file path/to/file.py")
|
|
69
|
+
|
|
70
|
+
When NOT to use the Agent tool:
|
|
71
|
+
- If you want to read a specific file path, use the Read or Glob tool instead of the Agent tool, to find the match more quickly
|
|
72
|
+
- If you are searching for a specific term or definition within a known location, use the Glob tool instead, to find the match more quickly
|
|
73
|
+
- If you are searching for content within a specific file or set of 2-3 files, use the Read tool instead of the Agent tool, to find the match more quickly
|
|
74
|
+
- Other tasks that are not related to the agent descriptions above
|
|
75
|
+
|
|
76
|
+
Usage notes:
|
|
77
|
+
1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses
|
|
78
|
+
2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.
|
|
79
|
+
3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.
|
|
80
|
+
4. The agent's outputs should generally be trusted
|
|
81
|
+
5. Clearly tell the agent whether you expect it to create content, perform analysis, or just do research (search, file reads, web fetches, etc.), since it is not aware of the user's intent
|
|
82
|
+
6. If the agent description mentions that it should be used proactively, then you should try your best to use it without the user having to ask for it first. Use your judgement.`;
|
|
83
|
+
exports.EDIT_DESCRIPTION = `Performs exact string replacements in files.
|
|
84
|
+
|
|
85
|
+
Usage:
|
|
86
|
+
- You must use your Read tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file.
|
|
87
|
+
- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the old_string or new_string.
|
|
88
|
+
- ALWAYS prefer editing existing files. NEVER write new files unless explicitly required.
|
|
89
|
+
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
|
|
90
|
+
- The edit will FAIL if old_string is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use replace_all to change every instance of old_string.
|
|
91
|
+
- Use replace_all for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.`;
|
|
92
|
+
exports.TOOL_DESCRIPTION = `Reads a file from the local filesystem. You can access any file directly by using this tool.
|
|
93
|
+
Assume this tool is able to read all files on the machine. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.
|
|
94
|
+
|
|
95
|
+
Usage:
|
|
96
|
+
- The file_path parameter must be an absolute path, not a relative path
|
|
97
|
+
- By default, it reads up to 2000 lines starting from the beginning of the file
|
|
98
|
+
- You can optionally specify a line offset and limit (especially handy for long files), but it's recommended to read the whole file by not providing these parameters
|
|
99
|
+
- Any lines longer than 2000 characters will be truncated
|
|
100
|
+
- Results are returned using cat -n format, with line numbers starting at 1
|
|
101
|
+
- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful.
|
|
102
|
+
- If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents.`;
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BaseMessage } from "@langchain/core/messages";
|
|
2
|
+
export interface Todo {
|
|
3
|
+
content: string;
|
|
4
|
+
status: "pending" | "in_progress" | "completed";
|
|
5
|
+
}
|
|
6
|
+
export interface Files {
|
|
7
|
+
[fileName: string]: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function fileReducer(left: Files | null, right: Files | null): Files | null;
|
|
10
|
+
export declare const DeepAgentState: import("@langchain/langgraph").AnnotationRoot<{
|
|
11
|
+
messages: import("@langchain/langgraph").BinaryOperatorAggregate<BaseMessage[], BaseMessage[]>;
|
|
12
|
+
todos: import("@langchain/langgraph").BinaryOperatorAggregate<Todo[], Todo[]>;
|
|
13
|
+
files: import("@langchain/langgraph").BinaryOperatorAggregate<Record<string, string>, Record<string, string>>;
|
|
14
|
+
}>;
|
|
15
|
+
export type DeepAgentStateType = typeof DeepAgentState.State;
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeepAgentState = void 0;
|
|
4
|
+
exports.fileReducer = fileReducer;
|
|
5
|
+
const langgraph_1 = require("@langchain/langgraph");
|
|
6
|
+
function fileReducer(left, right) {
|
|
7
|
+
if (!left) {
|
|
8
|
+
return right;
|
|
9
|
+
}
|
|
10
|
+
if (!right) {
|
|
11
|
+
return left;
|
|
12
|
+
}
|
|
13
|
+
return { ...left, ...right };
|
|
14
|
+
}
|
|
15
|
+
exports.DeepAgentState = langgraph_1.Annotation.Root({
|
|
16
|
+
messages: (0, langgraph_1.Annotation)({
|
|
17
|
+
reducer: (currentState, updateValue) => currentState.concat(updateValue),
|
|
18
|
+
default: () => [],
|
|
19
|
+
}),
|
|
20
|
+
todos: (0, langgraph_1.Annotation)({
|
|
21
|
+
reducer: (currentState, updateValue) => updateValue || currentState,
|
|
22
|
+
default: () => [],
|
|
23
|
+
}),
|
|
24
|
+
files: (0, langgraph_1.Annotation)({
|
|
25
|
+
reducer: (currentState, updateValue) => ({
|
|
26
|
+
...currentState,
|
|
27
|
+
...updateValue,
|
|
28
|
+
}),
|
|
29
|
+
default: () => ({}),
|
|
30
|
+
}),
|
|
31
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type StructuredTool } from "@langchain/core/tools";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { DeepAgentState } from "./state.js";
|
|
4
|
+
import { ToolMessage } from "@langchain/core/messages";
|
|
5
|
+
import { LanguageModelLike } from "@langchain/core/language_models/base";
|
|
6
|
+
export interface SubAgent {
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
prompt: string;
|
|
10
|
+
tools?: string[];
|
|
11
|
+
}
|
|
12
|
+
export declare function createTaskTool(tools: StructuredTool[], instructions: string, subagents: SubAgent[], model: LanguageModelLike, stateAnnotation?: typeof DeepAgentState): import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{
|
|
13
|
+
description: z.ZodString;
|
|
14
|
+
subagent_type: z.ZodString;
|
|
15
|
+
}, "strip", z.ZodTypeAny, {
|
|
16
|
+
description: string;
|
|
17
|
+
subagent_type: string;
|
|
18
|
+
}, {
|
|
19
|
+
description: string;
|
|
20
|
+
subagent_type: string;
|
|
21
|
+
}>, {
|
|
22
|
+
description: string;
|
|
23
|
+
subagent_type: string;
|
|
24
|
+
}, {
|
|
25
|
+
description: string;
|
|
26
|
+
subagent_type: string;
|
|
27
|
+
}, string | {
|
|
28
|
+
files: any;
|
|
29
|
+
messages: ToolMessage[];
|
|
30
|
+
}>;
|
package/dist/subAgent.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTaskTool = createTaskTool;
|
|
4
|
+
const tools_1 = require("@langchain/core/tools");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const prebuilt_1 = require("@langchain/langgraph/prebuilt");
|
|
7
|
+
const messages_1 = require("@langchain/core/messages");
|
|
8
|
+
const prompts_js_1 = require("./prompts.js");
|
|
9
|
+
function createTaskTool(tools, instructions, subagents, model, stateAnnotation) {
|
|
10
|
+
const agents = {
|
|
11
|
+
"general-purpose": (0, prebuilt_1.createReactAgent)({
|
|
12
|
+
llm: model,
|
|
13
|
+
tools,
|
|
14
|
+
messageModifier: instructions,
|
|
15
|
+
stateSchema: stateAnnotation,
|
|
16
|
+
}),
|
|
17
|
+
};
|
|
18
|
+
const toolsByName = tools.reduce((tools, tool) => {
|
|
19
|
+
const toolName = tool.name ?? tool.description ?? "unknown_tool";
|
|
20
|
+
tools[toolName] = tool;
|
|
21
|
+
return tools;
|
|
22
|
+
}, {});
|
|
23
|
+
for (const agent of subagents) {
|
|
24
|
+
let agentTools;
|
|
25
|
+
if (agent.tools) {
|
|
26
|
+
agentTools = agent.tools.map((t) => toolsByName[t]);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
agentTools = tools;
|
|
30
|
+
}
|
|
31
|
+
agents[agent.name] = (0, prebuilt_1.createReactAgent)({
|
|
32
|
+
llm: model,
|
|
33
|
+
tools: agentTools,
|
|
34
|
+
messageModifier: agent.prompt,
|
|
35
|
+
stateSchema: stateAnnotation,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const otherAgentsString = subagents
|
|
39
|
+
.map((agent) => `- ${agent.name}: ${agent.description}`)
|
|
40
|
+
.join("\\n");
|
|
41
|
+
return (0, tools_1.tool)(async (input, config) => {
|
|
42
|
+
const { description, subagent_type } = input;
|
|
43
|
+
const state = config?.configurable?.state;
|
|
44
|
+
if (!(subagent_type in agents)) {
|
|
45
|
+
return `Error: invoked agent of type ${subagent_type}, the only allowed types are [${Object.keys(agents)
|
|
46
|
+
.map((k) => `\`${k}\``)
|
|
47
|
+
.join(", ")}]`;
|
|
48
|
+
}
|
|
49
|
+
const subAgent = agents[subagent_type];
|
|
50
|
+
const newState = {
|
|
51
|
+
...state,
|
|
52
|
+
messages: [new messages_1.HumanMessage({ content: description })],
|
|
53
|
+
};
|
|
54
|
+
const result = await subAgent.invoke(newState);
|
|
55
|
+
return {
|
|
56
|
+
files: result.files || {},
|
|
57
|
+
messages: [
|
|
58
|
+
new messages_1.ToolMessage({
|
|
59
|
+
content: result.messages[result.messages.length - 1].content,
|
|
60
|
+
tool_call_id: config?.configurable?.tool_call_id || "",
|
|
61
|
+
}),
|
|
62
|
+
],
|
|
63
|
+
};
|
|
64
|
+
}, {
|
|
65
|
+
name: "task",
|
|
66
|
+
description: prompts_js_1.TASK_DESCRIPTION_PREFIX.replace("{other_agents}", otherAgentsString) + prompts_js_1.TASK_DESCRIPTION_SUFFIX,
|
|
67
|
+
schema: zod_1.z.object({
|
|
68
|
+
description: zod_1.z.string(),
|
|
69
|
+
subagent_type: zod_1.z.string(),
|
|
70
|
+
}),
|
|
71
|
+
});
|
|
72
|
+
}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Todo } from "./state.js";
|
|
3
|
+
import { ToolMessage } from "@langchain/core/messages";
|
|
4
|
+
export declare const writeTodos: import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{
|
|
5
|
+
todos: z.ZodArray<z.ZodObject<{
|
|
6
|
+
content: z.ZodString;
|
|
7
|
+
status: z.ZodEnum<["pending", "in_progress", "completed"]>;
|
|
8
|
+
}, "strip", z.ZodTypeAny, {
|
|
9
|
+
status: "pending" | "in_progress" | "completed";
|
|
10
|
+
content: string;
|
|
11
|
+
}, {
|
|
12
|
+
status: "pending" | "in_progress" | "completed";
|
|
13
|
+
content: string;
|
|
14
|
+
}>, "many">;
|
|
15
|
+
}, "strip", z.ZodTypeAny, {
|
|
16
|
+
todos: {
|
|
17
|
+
status: "pending" | "in_progress" | "completed";
|
|
18
|
+
content: string;
|
|
19
|
+
}[];
|
|
20
|
+
}, {
|
|
21
|
+
todos: {
|
|
22
|
+
status: "pending" | "in_progress" | "completed";
|
|
23
|
+
content: string;
|
|
24
|
+
}[];
|
|
25
|
+
}>, {
|
|
26
|
+
todos: Todo[];
|
|
27
|
+
}, {
|
|
28
|
+
todos: {
|
|
29
|
+
status: "pending" | "in_progress" | "completed";
|
|
30
|
+
content: string;
|
|
31
|
+
}[];
|
|
32
|
+
}, {
|
|
33
|
+
todos: Todo[];
|
|
34
|
+
messages: ToolMessage[];
|
|
35
|
+
}>;
|
|
36
|
+
export declare const ls: import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>, {}, {}, string[]>;
|
|
37
|
+
export declare const readFile: import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{
|
|
38
|
+
file_path: z.ZodString;
|
|
39
|
+
offset: z.ZodOptional<z.ZodNumber>;
|
|
40
|
+
limit: z.ZodOptional<z.ZodNumber>;
|
|
41
|
+
}, "strip", z.ZodTypeAny, {
|
|
42
|
+
file_path: string;
|
|
43
|
+
offset?: number | undefined;
|
|
44
|
+
limit?: number | undefined;
|
|
45
|
+
}, {
|
|
46
|
+
file_path: string;
|
|
47
|
+
offset?: number | undefined;
|
|
48
|
+
limit?: number | undefined;
|
|
49
|
+
}>, {
|
|
50
|
+
file_path: string;
|
|
51
|
+
offset?: number;
|
|
52
|
+
limit?: number;
|
|
53
|
+
}, {
|
|
54
|
+
file_path: string;
|
|
55
|
+
offset?: number | undefined;
|
|
56
|
+
limit?: number | undefined;
|
|
57
|
+
}, string>;
|
|
58
|
+
export declare const writeFile: import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{
|
|
59
|
+
file_path: z.ZodString;
|
|
60
|
+
content: z.ZodString;
|
|
61
|
+
}, "strip", z.ZodTypeAny, {
|
|
62
|
+
content: string;
|
|
63
|
+
file_path: string;
|
|
64
|
+
}, {
|
|
65
|
+
content: string;
|
|
66
|
+
file_path: string;
|
|
67
|
+
}>, {
|
|
68
|
+
file_path: string;
|
|
69
|
+
content: string;
|
|
70
|
+
}, {
|
|
71
|
+
content: string;
|
|
72
|
+
file_path: string;
|
|
73
|
+
}, {
|
|
74
|
+
files: Record<string, string>;
|
|
75
|
+
messages: ToolMessage[];
|
|
76
|
+
}>;
|
|
77
|
+
export declare const editFile: import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{
|
|
78
|
+
file_path: z.ZodString;
|
|
79
|
+
old_string: z.ZodString;
|
|
80
|
+
new_string: z.ZodString;
|
|
81
|
+
replace_all: z.ZodOptional<z.ZodBoolean>;
|
|
82
|
+
}, "strip", z.ZodTypeAny, {
|
|
83
|
+
file_path: string;
|
|
84
|
+
old_string: string;
|
|
85
|
+
new_string: string;
|
|
86
|
+
replace_all?: boolean | undefined;
|
|
87
|
+
}, {
|
|
88
|
+
file_path: string;
|
|
89
|
+
old_string: string;
|
|
90
|
+
new_string: string;
|
|
91
|
+
replace_all?: boolean | undefined;
|
|
92
|
+
}>, {
|
|
93
|
+
file_path: string;
|
|
94
|
+
old_string: string;
|
|
95
|
+
new_string: string;
|
|
96
|
+
replace_all?: boolean;
|
|
97
|
+
}, {
|
|
98
|
+
file_path: string;
|
|
99
|
+
old_string: string;
|
|
100
|
+
new_string: string;
|
|
101
|
+
replace_all?: boolean | undefined;
|
|
102
|
+
}, string | {
|
|
103
|
+
files: Record<string, string>;
|
|
104
|
+
messages: ToolMessage[];
|
|
105
|
+
}>;
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.editFile = exports.writeFile = exports.readFile = exports.ls = exports.writeTodos = void 0;
|
|
4
|
+
const tools_1 = require("@langchain/core/tools");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const messages_1 = require("@langchain/core/messages");
|
|
7
|
+
const prompts_js_1 = require("./prompts.js");
|
|
8
|
+
exports.writeTodos = (0, tools_1.tool)(async (input, config) => {
|
|
9
|
+
const state = config?.configurable?.state;
|
|
10
|
+
if (!state) {
|
|
11
|
+
throw new Error("State not available in tool execution");
|
|
12
|
+
}
|
|
13
|
+
state.todos = input.todos;
|
|
14
|
+
return {
|
|
15
|
+
todos: input.todos,
|
|
16
|
+
messages: [
|
|
17
|
+
new messages_1.ToolMessage({
|
|
18
|
+
content: `Updated todo list to ${JSON.stringify(input.todos)}`,
|
|
19
|
+
tool_call_id: config?.configurable?.tool_call_id || "",
|
|
20
|
+
}),
|
|
21
|
+
],
|
|
22
|
+
};
|
|
23
|
+
}, {
|
|
24
|
+
name: "write_todos",
|
|
25
|
+
description: prompts_js_1.WRITE_TODOS_DESCRIPTION,
|
|
26
|
+
schema: zod_1.z.object({
|
|
27
|
+
todos: zod_1.z.array(zod_1.z.object({
|
|
28
|
+
content: zod_1.z.string(),
|
|
29
|
+
status: zod_1.z.enum(["pending", "in_progress", "completed"]),
|
|
30
|
+
})),
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
exports.ls = (0, tools_1.tool)(async (input, config) => {
|
|
34
|
+
const state = config?.configurable?.state;
|
|
35
|
+
const files = state?.files || {};
|
|
36
|
+
return Object.keys(files);
|
|
37
|
+
}, {
|
|
38
|
+
name: "ls",
|
|
39
|
+
description: "List all files",
|
|
40
|
+
schema: zod_1.z.object({}),
|
|
41
|
+
});
|
|
42
|
+
exports.readFile = (0, tools_1.tool)(async (input, config) => {
|
|
43
|
+
const state = config?.configurable?.state;
|
|
44
|
+
const mockFilesystem = state?.files || {};
|
|
45
|
+
const { file_path, offset = 0, limit = 2000 } = input;
|
|
46
|
+
if (!(file_path in mockFilesystem)) {
|
|
47
|
+
return `Error: File '${file_path}' not found`;
|
|
48
|
+
}
|
|
49
|
+
const content = mockFilesystem[file_path];
|
|
50
|
+
// Handle empty file
|
|
51
|
+
if (!content || content.trim() === "") {
|
|
52
|
+
return "System reminder: File exists but has empty contents";
|
|
53
|
+
}
|
|
54
|
+
// Split content into lines
|
|
55
|
+
const lines = content.split("\n");
|
|
56
|
+
// Apply line offset and limit
|
|
57
|
+
const startIdx = offset;
|
|
58
|
+
const endIdx = Math.min(startIdx + limit, lines.length);
|
|
59
|
+
// Handle case where offset is beyond file length
|
|
60
|
+
if (startIdx >= lines.length) {
|
|
61
|
+
return `Error: Line offset ${offset} exceeds file length (${lines.length} lines)`;
|
|
62
|
+
}
|
|
63
|
+
// Format output with line numbers (cat -n format)
|
|
64
|
+
const resultLines = [];
|
|
65
|
+
for (let i = startIdx; i < endIdx; i++) {
|
|
66
|
+
let lineContent = lines[i];
|
|
67
|
+
// Truncate lines longer than 2000 characters
|
|
68
|
+
if (lineContent.length > 2000) {
|
|
69
|
+
lineContent = lineContent.substring(0, 2000);
|
|
70
|
+
}
|
|
71
|
+
// Line numbers start at 1, so add 1 to the index
|
|
72
|
+
const lineNumber = i + 1;
|
|
73
|
+
resultLines.push(`${lineNumber.toString().padStart(6)}\\t${lineContent}`);
|
|
74
|
+
}
|
|
75
|
+
return resultLines.join("\\n");
|
|
76
|
+
}, {
|
|
77
|
+
name: "read_file",
|
|
78
|
+
description: prompts_js_1.TOOL_DESCRIPTION,
|
|
79
|
+
schema: zod_1.z.object({
|
|
80
|
+
file_path: zod_1.z.string(),
|
|
81
|
+
offset: zod_1.z.number().optional(),
|
|
82
|
+
limit: zod_1.z.number().optional(),
|
|
83
|
+
}),
|
|
84
|
+
});
|
|
85
|
+
exports.writeFile = (0, tools_1.tool)(async (input, config) => {
|
|
86
|
+
const state = config?.configurable?.state;
|
|
87
|
+
const files = state?.files || {};
|
|
88
|
+
files[input.file_path] = input.content;
|
|
89
|
+
return {
|
|
90
|
+
files,
|
|
91
|
+
messages: [
|
|
92
|
+
new messages_1.ToolMessage({
|
|
93
|
+
content: `Updated file ${input.file_path}`,
|
|
94
|
+
tool_call_id: config?.configurable?.tool_call_id || "",
|
|
95
|
+
}),
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
}, {
|
|
99
|
+
name: "write_file",
|
|
100
|
+
description: "Write to a file.",
|
|
101
|
+
schema: zod_1.z.object({
|
|
102
|
+
file_path: zod_1.z.string(),
|
|
103
|
+
content: zod_1.z.string(),
|
|
104
|
+
}),
|
|
105
|
+
});
|
|
106
|
+
exports.editFile = (0, tools_1.tool)(async (input, config) => {
|
|
107
|
+
const state = config?.configurable?.state;
|
|
108
|
+
const mockFilesystem = state?.files || {};
|
|
109
|
+
const { file_path, old_string, new_string, replace_all = false, } = input;
|
|
110
|
+
// Check if file exists in mock filesystem
|
|
111
|
+
if (!(file_path in mockFilesystem)) {
|
|
112
|
+
return `Error: File '${file_path}' not found`;
|
|
113
|
+
}
|
|
114
|
+
// Get current file content
|
|
115
|
+
const content = mockFilesystem[file_path];
|
|
116
|
+
// Check if old_string exists in the file
|
|
117
|
+
if (!content.includes(old_string)) {
|
|
118
|
+
return `Error: String not found in file: '${old_string}'`;
|
|
119
|
+
}
|
|
120
|
+
// If not replace_all, check for uniqueness
|
|
121
|
+
if (!replace_all) {
|
|
122
|
+
const occurrences = (content.match(new RegExp(old_string.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&"), "g")) || []).length;
|
|
123
|
+
if (occurrences > 1) {
|
|
124
|
+
return `Error: String '${old_string}' appears ${occurrences} times in file. Use replace_all=true to replace all instances, or provide a more specific string with surrounding context.`;
|
|
125
|
+
}
|
|
126
|
+
else if (occurrences === 0) {
|
|
127
|
+
return `Error: String not found in file: '${old_string}'`;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const newContent = replace({
|
|
131
|
+
replaceAll: replace_all,
|
|
132
|
+
content,
|
|
133
|
+
current: old_string,
|
|
134
|
+
next: new_string,
|
|
135
|
+
filePath: file_path,
|
|
136
|
+
});
|
|
137
|
+
// Update the mock filesystem
|
|
138
|
+
mockFilesystem[file_path] = newContent;
|
|
139
|
+
return {
|
|
140
|
+
files: mockFilesystem,
|
|
141
|
+
messages: [
|
|
142
|
+
new messages_1.ToolMessage({
|
|
143
|
+
content: `Updated file ${file_path}`,
|
|
144
|
+
tool_call_id: config?.configurable?.tool_call_id || "",
|
|
145
|
+
}),
|
|
146
|
+
],
|
|
147
|
+
};
|
|
148
|
+
}, {
|
|
149
|
+
name: "edit_file",
|
|
150
|
+
description: prompts_js_1.EDIT_DESCRIPTION,
|
|
151
|
+
schema: zod_1.z.object({
|
|
152
|
+
file_path: zod_1.z.string(),
|
|
153
|
+
old_string: zod_1.z.string(),
|
|
154
|
+
new_string: zod_1.z.string(),
|
|
155
|
+
replace_all: zod_1.z.boolean().optional(),
|
|
156
|
+
}),
|
|
157
|
+
});
|
|
158
|
+
const replace = (options) => {
|
|
159
|
+
if (!options.replaceAll) {
|
|
160
|
+
console.log(`Replacing string in '${options.filePath}'`);
|
|
161
|
+
return options.content.replace(options.current, options.next);
|
|
162
|
+
}
|
|
163
|
+
const newContent = options.content.replaceAll(options.current, options.next);
|
|
164
|
+
const replacementCount = (options.content.match(new RegExp(options.current.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&"), "g")) || []).length;
|
|
165
|
+
console.log(`Successfully replaced ${replacementCount} instance(s) of the string in '${options.filePath}'`);
|
|
166
|
+
return newContent;
|
|
167
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "deepagents",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "General purpose 'deep agent' with sub-agent spawning, todo list capabilities, and mock file system",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/**/*"
|
|
9
|
+
],
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git@github.com:Shelex/deepagents-ts.git"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"dev": "node --import tsx src/examples/research/agent.ts",
|
|
20
|
+
"test": "jest"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"langchain",
|
|
24
|
+
"langgraph",
|
|
25
|
+
"agents",
|
|
26
|
+
"llm",
|
|
27
|
+
"ai",
|
|
28
|
+
"typescript"
|
|
29
|
+
],
|
|
30
|
+
"author": "Oleksandr Shevtsov",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@langchain/anthropic": "^0.3.25",
|
|
34
|
+
"@langchain/community": "^0.3.49",
|
|
35
|
+
"@langchain/core": "^0.3.66",
|
|
36
|
+
"@langchain/langgraph": "^0.4.2",
|
|
37
|
+
"@lmstudio/sdk": "^1.4.0",
|
|
38
|
+
"zod": "^3.25.76"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
42
|
+
"@semantic-release/git": "^10.0.1",
|
|
43
|
+
"@semantic-release/npm": "^12.0.1",
|
|
44
|
+
"@types/node": "^22.17.0",
|
|
45
|
+
"jest": "^29.7.0",
|
|
46
|
+
"semantic-release": "^24.0.0",
|
|
47
|
+
"ts-node": "^10.9.2",
|
|
48
|
+
"tsx": "^4.20.3",
|
|
49
|
+
"typescript": "^5.0.0"
|
|
50
|
+
},
|
|
51
|
+
"release": {
|
|
52
|
+
"branches": [
|
|
53
|
+
"master",
|
|
54
|
+
"main"
|
|
55
|
+
],
|
|
56
|
+
"plugins": [
|
|
57
|
+
"@semantic-release/commit-analyzer",
|
|
58
|
+
"@semantic-release/release-notes-generator",
|
|
59
|
+
"@semantic-release/changelog",
|
|
60
|
+
"@semantic-release/npm",
|
|
61
|
+
"@semantic-release/git",
|
|
62
|
+
"@semantic-release/github"
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
}
|