deepagentsdk 0.9.2
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 +159 -0
- package/package.json +95 -0
- package/src/agent.ts +1230 -0
- package/src/backends/composite.ts +273 -0
- package/src/backends/filesystem.ts +692 -0
- package/src/backends/index.ts +22 -0
- package/src/backends/local-sandbox.ts +175 -0
- package/src/backends/persistent.ts +593 -0
- package/src/backends/sandbox.ts +510 -0
- package/src/backends/state.ts +244 -0
- package/src/backends/utils.ts +287 -0
- package/src/checkpointer/file-saver.ts +98 -0
- package/src/checkpointer/index.ts +5 -0
- package/src/checkpointer/kv-saver.ts +82 -0
- package/src/checkpointer/memory-saver.ts +82 -0
- package/src/checkpointer/types.ts +125 -0
- package/src/cli/components/ApiKeyInput.tsx +300 -0
- package/src/cli/components/FilePreview.tsx +237 -0
- package/src/cli/components/Input.tsx +277 -0
- package/src/cli/components/Message.tsx +93 -0
- package/src/cli/components/ModelSelection.tsx +338 -0
- package/src/cli/components/SlashMenu.tsx +101 -0
- package/src/cli/components/StatusBar.tsx +89 -0
- package/src/cli/components/Subagent.tsx +91 -0
- package/src/cli/components/TodoList.tsx +133 -0
- package/src/cli/components/ToolApproval.tsx +70 -0
- package/src/cli/components/ToolCall.tsx +144 -0
- package/src/cli/components/ToolCallSummary.tsx +175 -0
- package/src/cli/components/Welcome.tsx +75 -0
- package/src/cli/components/index.ts +24 -0
- package/src/cli/hooks/index.ts +12 -0
- package/src/cli/hooks/useAgent.ts +933 -0
- package/src/cli/index.tsx +1066 -0
- package/src/cli/theme.ts +205 -0
- package/src/cli/utils/model-list.ts +365 -0
- package/src/constants/errors.ts +29 -0
- package/src/constants/limits.ts +195 -0
- package/src/index.ts +176 -0
- package/src/middleware/agent-memory.ts +330 -0
- package/src/prompts.ts +196 -0
- package/src/skills/index.ts +2 -0
- package/src/skills/load.ts +191 -0
- package/src/skills/types.ts +53 -0
- package/src/tools/execute.ts +167 -0
- package/src/tools/filesystem.ts +418 -0
- package/src/tools/index.ts +39 -0
- package/src/tools/subagent.ts +443 -0
- package/src/tools/todos.ts +101 -0
- package/src/tools/web.ts +567 -0
- package/src/types/backend.ts +177 -0
- package/src/types/core.ts +220 -0
- package/src/types/events.ts +429 -0
- package/src/types/index.ts +94 -0
- package/src/types/structured-output.ts +43 -0
- package/src/types/subagent.ts +96 -0
- package/src/types.ts +22 -0
- package/src/utils/approval.ts +213 -0
- package/src/utils/events.ts +416 -0
- package/src/utils/eviction.ts +181 -0
- package/src/utils/index.ts +34 -0
- package/src/utils/model-parser.ts +38 -0
- package/src/utils/patch-tool-calls.ts +233 -0
- package/src/utils/project-detection.ts +32 -0
- package/src/utils/summarization.ts +254 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backend protocol and filesystem types.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { TodoItem } from "./core.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* File data structure used by backends.
|
|
9
|
+
*/
|
|
10
|
+
export interface FileData {
|
|
11
|
+
/** Lines of text content */
|
|
12
|
+
content: string[];
|
|
13
|
+
/** ISO format timestamp of creation */
|
|
14
|
+
created_at: string;
|
|
15
|
+
/** ISO format timestamp of last modification */
|
|
16
|
+
modified_at: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Structured file listing info.
|
|
21
|
+
*/
|
|
22
|
+
export interface FileInfo {
|
|
23
|
+
/** File path */
|
|
24
|
+
path: string;
|
|
25
|
+
/** Whether this is a directory */
|
|
26
|
+
is_dir?: boolean;
|
|
27
|
+
/** File size in bytes (approximate) */
|
|
28
|
+
size?: number;
|
|
29
|
+
/** ISO 8601 timestamp of last modification */
|
|
30
|
+
modified_at?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Structured grep match entry.
|
|
35
|
+
*/
|
|
36
|
+
export interface GrepMatch {
|
|
37
|
+
/** File path where match was found */
|
|
38
|
+
path: string;
|
|
39
|
+
/** Line number (1-indexed) */
|
|
40
|
+
line: number;
|
|
41
|
+
/** The matching line text */
|
|
42
|
+
text: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Result from backend write operations.
|
|
47
|
+
*/
|
|
48
|
+
export interface WriteResult {
|
|
49
|
+
/** Whether the write operation succeeded */
|
|
50
|
+
success: boolean;
|
|
51
|
+
/** Error message on failure, undefined on success */
|
|
52
|
+
error?: string;
|
|
53
|
+
/** File path of written file, undefined on failure */
|
|
54
|
+
path?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Result from backend edit operations.
|
|
59
|
+
*/
|
|
60
|
+
export interface EditResult {
|
|
61
|
+
/** Whether the edit operation succeeded */
|
|
62
|
+
success: boolean;
|
|
63
|
+
/** Error message on failure, undefined on success */
|
|
64
|
+
error?: string;
|
|
65
|
+
/** File path of edited file, undefined on failure */
|
|
66
|
+
path?: string;
|
|
67
|
+
/** Number of replacements made, undefined on failure */
|
|
68
|
+
occurrences?: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Shared state for deep agent operations.
|
|
73
|
+
* This is passed to tools and modified during execution.
|
|
74
|
+
*/
|
|
75
|
+
export interface DeepAgentState {
|
|
76
|
+
/** Current todo list */
|
|
77
|
+
todos: TodoItem[];
|
|
78
|
+
/** Virtual filesystem (for StateBackend) */
|
|
79
|
+
files: Record<string, FileData>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Protocol for pluggable memory backends.
|
|
84
|
+
*/
|
|
85
|
+
export interface BackendProtocol {
|
|
86
|
+
/**
|
|
87
|
+
* Structured listing with file metadata.
|
|
88
|
+
*/
|
|
89
|
+
lsInfo(path: string): FileInfo[] | Promise<FileInfo[]>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Read file content with line numbers or an error string.
|
|
93
|
+
*/
|
|
94
|
+
read(
|
|
95
|
+
filePath: string,
|
|
96
|
+
offset?: number,
|
|
97
|
+
limit?: number
|
|
98
|
+
): string | Promise<string>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Read file content as raw FileData.
|
|
102
|
+
*/
|
|
103
|
+
readRaw(filePath: string): FileData | Promise<FileData>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Structured search results or error string for invalid input.
|
|
107
|
+
*/
|
|
108
|
+
grepRaw(
|
|
109
|
+
pattern: string,
|
|
110
|
+
path?: string | null,
|
|
111
|
+
glob?: string | null
|
|
112
|
+
): GrepMatch[] | string | Promise<GrepMatch[] | string>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Structured glob matching returning FileInfo objects.
|
|
116
|
+
*/
|
|
117
|
+
globInfo(pattern: string, path?: string): FileInfo[] | Promise<FileInfo[]>;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Create a new file or overwrite existing file.
|
|
121
|
+
*/
|
|
122
|
+
write(filePath: string, content: string): WriteResult | Promise<WriteResult>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Edit a file by replacing string occurrences.
|
|
126
|
+
*/
|
|
127
|
+
edit(
|
|
128
|
+
filePath: string,
|
|
129
|
+
oldString: string,
|
|
130
|
+
newString: string,
|
|
131
|
+
replaceAll?: boolean
|
|
132
|
+
): EditResult | Promise<EditResult>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Factory function type for creating backend instances from agent state.
|
|
137
|
+
*/
|
|
138
|
+
export type BackendFactory = (state: DeepAgentState) => BackendProtocol;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Result of command execution in a sandbox.
|
|
142
|
+
*/
|
|
143
|
+
export interface ExecuteResponse {
|
|
144
|
+
/** Combined stdout and stderr output of the executed command */
|
|
145
|
+
output: string;
|
|
146
|
+
/** Exit code (0 = success, non-zero = failure, null = unknown/timeout) */
|
|
147
|
+
exitCode: number | null;
|
|
148
|
+
/** Whether the output was truncated due to size limits */
|
|
149
|
+
truncated: boolean;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Protocol for sandbox backends with command execution capability.
|
|
154
|
+
*/
|
|
155
|
+
export interface SandboxBackendProtocol extends BackendProtocol {
|
|
156
|
+
/**
|
|
157
|
+
* Execute a shell command in the sandbox.
|
|
158
|
+
*/
|
|
159
|
+
execute(command: string): Promise<ExecuteResponse>;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Unique identifier for this sandbox instance.
|
|
163
|
+
*/
|
|
164
|
+
readonly id: string;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Type guard to check if a backend is a SandboxBackendProtocol.
|
|
169
|
+
*/
|
|
170
|
+
export function isSandboxBackend(
|
|
171
|
+
backend: BackendProtocol
|
|
172
|
+
): backend is SandboxBackendProtocol {
|
|
173
|
+
return (
|
|
174
|
+
typeof (backend as SandboxBackendProtocol).execute === "function" &&
|
|
175
|
+
typeof (backend as SandboxBackendProtocol).id === "string"
|
|
176
|
+
);
|
|
177
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core agent configuration, state, and control types.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
ToolSet,
|
|
7
|
+
LanguageModel,
|
|
8
|
+
LanguageModelMiddleware,
|
|
9
|
+
StopCondition,
|
|
10
|
+
ToolLoopAgentSettings,
|
|
11
|
+
} from "ai";
|
|
12
|
+
import type { z } from "zod";
|
|
13
|
+
import type { BaseCheckpointSaver } from "../checkpointer/types.js";
|
|
14
|
+
import type { BackendProtocol, BackendFactory } from "./backend.js";
|
|
15
|
+
import type { SubAgent, InterruptOnConfig } from "./subagent.js";
|
|
16
|
+
|
|
17
|
+
// Re-export LanguageModel for convenience
|
|
18
|
+
export type { LanguageModel };
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Configuration options for agent memory middleware.
|
|
22
|
+
*/
|
|
23
|
+
export interface AgentMemoryOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Unique identifier for the agent (e.g., "code-architect", "research-agent").
|
|
26
|
+
* Used to locate agent-specific memory at ~/.deepagents/{agentId}/agent.md
|
|
27
|
+
*/
|
|
28
|
+
agentId: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Optional working directory for project-level memory detection.
|
|
32
|
+
* Defaults to process.cwd().
|
|
33
|
+
*/
|
|
34
|
+
workingDirectory?: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Optional custom path for user-level .deepagents directory.
|
|
38
|
+
* Defaults to os.homedir() + '/.deepagents'.
|
|
39
|
+
*/
|
|
40
|
+
userDeepagentsDir?: string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Optional callback to request user approval for creating project-level .deepagents/ directory.
|
|
44
|
+
*/
|
|
45
|
+
requestProjectApproval?: (projectPath: string) => Promise<boolean>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Todo item for task planning and tracking.
|
|
50
|
+
*/
|
|
51
|
+
export interface TodoItem {
|
|
52
|
+
/** Unique identifier for the todo */
|
|
53
|
+
id: string;
|
|
54
|
+
/** Description of the task */
|
|
55
|
+
content: string;
|
|
56
|
+
/** Current status */
|
|
57
|
+
status: "pending" | "in_progress" | "completed" | "cancelled";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Tool choice type for prepareStep.
|
|
62
|
+
*/
|
|
63
|
+
export type DeepAgentToolChoice =
|
|
64
|
+
| "auto"
|
|
65
|
+
| "none"
|
|
66
|
+
| "required"
|
|
67
|
+
| { type: "tool"; toolName: string };
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Result from prepareStep callback.
|
|
71
|
+
*/
|
|
72
|
+
export interface PrepareStepResult {
|
|
73
|
+
toolChoice?: DeepAgentToolChoice;
|
|
74
|
+
model?: LanguageModel;
|
|
75
|
+
[key: string]: unknown;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Arguments passed to prepareStep callback.
|
|
80
|
+
*/
|
|
81
|
+
export interface PrepareStepArgs {
|
|
82
|
+
stepNumber: number;
|
|
83
|
+
steps: unknown[];
|
|
84
|
+
model: LanguageModel;
|
|
85
|
+
messages: import("ai").ModelMessage[];
|
|
86
|
+
experimental_context?: unknown;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Prepare step function type with permissive tool choice typing.
|
|
91
|
+
*/
|
|
92
|
+
export type PrepareStepFunction = (
|
|
93
|
+
args: PrepareStepArgs
|
|
94
|
+
) => PrepareStepResult | PromiseLike<PrepareStepResult>;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Loop control callbacks for agent iteration.
|
|
98
|
+
*/
|
|
99
|
+
export interface LoopControlOptions {
|
|
100
|
+
/**
|
|
101
|
+
* Called before each step to dynamically adjust settings.
|
|
102
|
+
*/
|
|
103
|
+
prepareStep?: PrepareStepFunction;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Called after each step finishes.
|
|
107
|
+
*/
|
|
108
|
+
onStepFinish?: ToolLoopAgentSettings["onStepFinish"];
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Called when all steps are finished.
|
|
112
|
+
*/
|
|
113
|
+
onFinish?: ToolLoopAgentSettings["onFinish"];
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Custom stop conditions (composed with maxSteps safety limit).
|
|
117
|
+
*/
|
|
118
|
+
stopWhen?: StopCondition<ToolSet> | StopCondition<ToolSet>[];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Sampling and generation parameters for model calls.
|
|
123
|
+
*/
|
|
124
|
+
export interface GenerationOptions {
|
|
125
|
+
/** Sampling temperature (0-2). */
|
|
126
|
+
temperature?: number;
|
|
127
|
+
/** Nucleus sampling (0-1). */
|
|
128
|
+
topP?: number;
|
|
129
|
+
/** Top-K sampling. */
|
|
130
|
+
topK?: number;
|
|
131
|
+
/** Maximum number of tokens to generate per response. */
|
|
132
|
+
maxOutputTokens?: number;
|
|
133
|
+
/** Presence penalty (-1 to 1). */
|
|
134
|
+
presencePenalty?: number;
|
|
135
|
+
/** Frequency penalty (-1 to 1). */
|
|
136
|
+
frequencyPenalty?: number;
|
|
137
|
+
/** Random seed for deterministic generation. */
|
|
138
|
+
seed?: number;
|
|
139
|
+
/** Sequences that stop generation when encountered. */
|
|
140
|
+
stopSequences?: string[];
|
|
141
|
+
/** Maximum number of retries for failed API calls. */
|
|
142
|
+
maxRetries?: number;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Advanced options for power users.
|
|
147
|
+
*/
|
|
148
|
+
export interface AdvancedAgentOptions {
|
|
149
|
+
/** OpenTelemetry configuration for observability. */
|
|
150
|
+
experimental_telemetry?: ToolLoopAgentSettings["experimental_telemetry"];
|
|
151
|
+
/** Provider-specific options passed through to the model provider. */
|
|
152
|
+
providerOptions?: ToolLoopAgentSettings["providerOptions"];
|
|
153
|
+
/** Custom context passed to tool executions. */
|
|
154
|
+
experimental_context?: ToolLoopAgentSettings["experimental_context"];
|
|
155
|
+
/** Control which tool the model should call. */
|
|
156
|
+
toolChoice?: ToolLoopAgentSettings["toolChoice"];
|
|
157
|
+
/** Limit which tools are available for the model to call. */
|
|
158
|
+
activeTools?: string[];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Summarization configuration options.
|
|
163
|
+
*/
|
|
164
|
+
export interface SummarizationConfig {
|
|
165
|
+
/** Enable automatic summarization when approaching token limits. */
|
|
166
|
+
enabled: boolean;
|
|
167
|
+
/** Token threshold to trigger summarization (default: 170000). */
|
|
168
|
+
tokenThreshold?: number;
|
|
169
|
+
/** Number of recent messages to keep intact without summarization (default: 6). */
|
|
170
|
+
keepMessages?: number;
|
|
171
|
+
/** Model to use for summarization. */
|
|
172
|
+
model?: LanguageModel;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Configuration parameters for creating a Deep Agent.
|
|
177
|
+
*/
|
|
178
|
+
export interface CreateDeepAgentParams {
|
|
179
|
+
/** The AI SDK LanguageModel instance to use. */
|
|
180
|
+
model: LanguageModel;
|
|
181
|
+
/** Optional middleware to wrap the model. */
|
|
182
|
+
middleware?: LanguageModelMiddleware | LanguageModelMiddleware[];
|
|
183
|
+
/** Optional custom tools the agent should have access to. */
|
|
184
|
+
tools?: ToolSet;
|
|
185
|
+
/** Optional custom system prompt for the agent. */
|
|
186
|
+
systemPrompt?: string;
|
|
187
|
+
/** Optional list of subagent specifications for task delegation. */
|
|
188
|
+
subagents?: SubAgent[];
|
|
189
|
+
/** Optional backend for filesystem operations. */
|
|
190
|
+
backend?: BackendProtocol | BackendFactory;
|
|
191
|
+
/** Optional maximum number of steps for the agent loop (default: 100). */
|
|
192
|
+
maxSteps?: number;
|
|
193
|
+
/** Optional flag to include the general-purpose subagent (default: true). */
|
|
194
|
+
includeGeneralPurposeAgent?: boolean;
|
|
195
|
+
/** Optional token limit before evicting large tool results to filesystem. */
|
|
196
|
+
toolResultEvictionLimit?: number;
|
|
197
|
+
/** Optional flag to enable prompt caching (Anthropic only). */
|
|
198
|
+
enablePromptCaching?: boolean;
|
|
199
|
+
/** Optional summarization configuration. */
|
|
200
|
+
summarization?: SummarizationConfig;
|
|
201
|
+
/** Configuration for human-in-the-loop tool approval. */
|
|
202
|
+
interruptOn?: InterruptOnConfig;
|
|
203
|
+
/** Optional checkpointer for persisting agent state. */
|
|
204
|
+
checkpointer?: BaseCheckpointSaver;
|
|
205
|
+
/** @deprecated Use `agentId` instead. */
|
|
206
|
+
skillsDir?: string;
|
|
207
|
+
/** Optional agent identifier for loading agent-specific memory and skills. */
|
|
208
|
+
agentId?: string;
|
|
209
|
+
/** Optional configuration for structured output parsing. */
|
|
210
|
+
output?: {
|
|
211
|
+
schema: z.ZodType<any>;
|
|
212
|
+
description?: string;
|
|
213
|
+
};
|
|
214
|
+
/** Advanced loop control options. */
|
|
215
|
+
loopControl?: LoopControlOptions;
|
|
216
|
+
/** Advanced generation options. */
|
|
217
|
+
generationOptions?: GenerationOptions;
|
|
218
|
+
/** Advanced agent options. */
|
|
219
|
+
advancedOptions?: AdvancedAgentOptions;
|
|
220
|
+
}
|