@compilr-dev/agents 0.0.1
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/README.md +1277 -0
- package/dist/agent.d.ts +1272 -0
- package/dist/agent.js +1912 -0
- package/dist/anchors/builtin.d.ts +24 -0
- package/dist/anchors/builtin.js +61 -0
- package/dist/anchors/index.d.ts +6 -0
- package/dist/anchors/index.js +5 -0
- package/dist/anchors/manager.d.ts +115 -0
- package/dist/anchors/manager.js +412 -0
- package/dist/anchors/types.d.ts +168 -0
- package/dist/anchors/types.js +10 -0
- package/dist/context/index.d.ts +12 -0
- package/dist/context/index.js +10 -0
- package/dist/context/manager.d.ts +224 -0
- package/dist/context/manager.js +770 -0
- package/dist/context/types.d.ts +377 -0
- package/dist/context/types.js +7 -0
- package/dist/costs/index.d.ts +8 -0
- package/dist/costs/index.js +7 -0
- package/dist/costs/tracker.d.ts +121 -0
- package/dist/costs/tracker.js +295 -0
- package/dist/costs/types.d.ts +157 -0
- package/dist/costs/types.js +8 -0
- package/dist/errors.d.ts +178 -0
- package/dist/errors.js +249 -0
- package/dist/guardrails/builtin.d.ts +27 -0
- package/dist/guardrails/builtin.js +223 -0
- package/dist/guardrails/index.d.ts +6 -0
- package/dist/guardrails/index.js +5 -0
- package/dist/guardrails/manager.d.ts +117 -0
- package/dist/guardrails/manager.js +288 -0
- package/dist/guardrails/types.d.ts +159 -0
- package/dist/guardrails/types.js +7 -0
- package/dist/hooks/index.d.ts +31 -0
- package/dist/hooks/index.js +29 -0
- package/dist/hooks/manager.d.ts +147 -0
- package/dist/hooks/manager.js +600 -0
- package/dist/hooks/types.d.ts +368 -0
- package/dist/hooks/types.js +12 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +73 -0
- package/dist/mcp/client.d.ts +93 -0
- package/dist/mcp/client.js +287 -0
- package/dist/mcp/errors.d.ts +60 -0
- package/dist/mcp/errors.js +78 -0
- package/dist/mcp/index.d.ts +43 -0
- package/dist/mcp/index.js +45 -0
- package/dist/mcp/manager.d.ts +120 -0
- package/dist/mcp/manager.js +276 -0
- package/dist/mcp/tools.d.ts +54 -0
- package/dist/mcp/tools.js +99 -0
- package/dist/mcp/types.d.ts +150 -0
- package/dist/mcp/types.js +40 -0
- package/dist/memory/index.d.ts +8 -0
- package/dist/memory/index.js +7 -0
- package/dist/memory/loader.d.ts +114 -0
- package/dist/memory/loader.js +463 -0
- package/dist/memory/types.d.ts +182 -0
- package/dist/memory/types.js +8 -0
- package/dist/messages/index.d.ts +82 -0
- package/dist/messages/index.js +155 -0
- package/dist/permissions/index.d.ts +5 -0
- package/dist/permissions/index.js +4 -0
- package/dist/permissions/manager.d.ts +125 -0
- package/dist/permissions/manager.js +379 -0
- package/dist/permissions/types.d.ts +162 -0
- package/dist/permissions/types.js +7 -0
- package/dist/providers/claude.d.ts +90 -0
- package/dist/providers/claude.js +348 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.js +11 -0
- package/dist/providers/mock.d.ts +133 -0
- package/dist/providers/mock.js +204 -0
- package/dist/providers/types.d.ts +168 -0
- package/dist/providers/types.js +4 -0
- package/dist/rate-limit/index.d.ts +45 -0
- package/dist/rate-limit/index.js +47 -0
- package/dist/rate-limit/limiter.d.ts +104 -0
- package/dist/rate-limit/limiter.js +326 -0
- package/dist/rate-limit/provider-wrapper.d.ts +112 -0
- package/dist/rate-limit/provider-wrapper.js +201 -0
- package/dist/rate-limit/retry.d.ts +108 -0
- package/dist/rate-limit/retry.js +287 -0
- package/dist/rate-limit/types.d.ts +181 -0
- package/dist/rate-limit/types.js +22 -0
- package/dist/rehearsal/file-analyzer.d.ts +22 -0
- package/dist/rehearsal/file-analyzer.js +351 -0
- package/dist/rehearsal/git-analyzer.d.ts +22 -0
- package/dist/rehearsal/git-analyzer.js +472 -0
- package/dist/rehearsal/index.d.ts +35 -0
- package/dist/rehearsal/index.js +36 -0
- package/dist/rehearsal/manager.d.ts +100 -0
- package/dist/rehearsal/manager.js +290 -0
- package/dist/rehearsal/types.d.ts +235 -0
- package/dist/rehearsal/types.js +8 -0
- package/dist/skills/index.d.ts +160 -0
- package/dist/skills/index.js +282 -0
- package/dist/state/agent-state.d.ts +41 -0
- package/dist/state/agent-state.js +88 -0
- package/dist/state/checkpointer.d.ts +110 -0
- package/dist/state/checkpointer.js +362 -0
- package/dist/state/errors.d.ts +66 -0
- package/dist/state/errors.js +88 -0
- package/dist/state/index.d.ts +35 -0
- package/dist/state/index.js +37 -0
- package/dist/state/serializer.d.ts +55 -0
- package/dist/state/serializer.js +172 -0
- package/dist/state/types.d.ts +312 -0
- package/dist/state/types.js +14 -0
- package/dist/tools/builtin/bash-output.d.ts +61 -0
- package/dist/tools/builtin/bash-output.js +90 -0
- package/dist/tools/builtin/bash.d.ts +150 -0
- package/dist/tools/builtin/bash.js +354 -0
- package/dist/tools/builtin/edit.d.ts +50 -0
- package/dist/tools/builtin/edit.js +215 -0
- package/dist/tools/builtin/glob.d.ts +62 -0
- package/dist/tools/builtin/glob.js +244 -0
- package/dist/tools/builtin/grep.d.ts +74 -0
- package/dist/tools/builtin/grep.js +363 -0
- package/dist/tools/builtin/index.d.ts +44 -0
- package/dist/tools/builtin/index.js +69 -0
- package/dist/tools/builtin/kill-shell.d.ts +44 -0
- package/dist/tools/builtin/kill-shell.js +80 -0
- package/dist/tools/builtin/read-file.d.ts +57 -0
- package/dist/tools/builtin/read-file.js +184 -0
- package/dist/tools/builtin/shell-manager.d.ts +176 -0
- package/dist/tools/builtin/shell-manager.js +337 -0
- package/dist/tools/builtin/task.d.ts +202 -0
- package/dist/tools/builtin/task.js +350 -0
- package/dist/tools/builtin/todo.d.ts +207 -0
- package/dist/tools/builtin/todo.js +453 -0
- package/dist/tools/builtin/utils.d.ts +27 -0
- package/dist/tools/builtin/utils.js +70 -0
- package/dist/tools/builtin/web-fetch.d.ts +96 -0
- package/dist/tools/builtin/web-fetch.js +290 -0
- package/dist/tools/builtin/write-file.d.ts +54 -0
- package/dist/tools/builtin/write-file.js +147 -0
- package/dist/tools/define.d.ts +60 -0
- package/dist/tools/define.js +65 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +37 -0
- package/dist/tools/registry.d.ts +79 -0
- package/dist/tools/registry.js +151 -0
- package/dist/tools/types.d.ts +59 -0
- package/dist/tools/types.js +4 -0
- package/dist/tracing/hooks.d.ts +58 -0
- package/dist/tracing/hooks.js +377 -0
- package/dist/tracing/index.d.ts +51 -0
- package/dist/tracing/index.js +55 -0
- package/dist/tracing/logging.d.ts +78 -0
- package/dist/tracing/logging.js +310 -0
- package/dist/tracing/manager.d.ts +160 -0
- package/dist/tracing/manager.js +468 -0
- package/dist/tracing/otel.d.ts +102 -0
- package/dist/tracing/otel.js +246 -0
- package/dist/tracing/types.d.ts +346 -0
- package/dist/tracing/types.js +38 -0
- package/dist/utils/index.d.ts +23 -0
- package/dist/utils/index.js +44 -0
- package/package.json +79 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skills System - Prompt expansions for specialized agent behavior
|
|
3
|
+
*
|
|
4
|
+
* Skills are reusable prompt templates that can be invoked to provide
|
|
5
|
+
* specialized capabilities to agents. They allow you to define
|
|
6
|
+
* domain-specific instructions that can be dynamically added to conversations.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Skill Registry - Manages skill registration and invocation
|
|
10
|
+
*/
|
|
11
|
+
export class SkillRegistry {
|
|
12
|
+
skills = new Map();
|
|
13
|
+
/**
|
|
14
|
+
* Register a new skill
|
|
15
|
+
*/
|
|
16
|
+
register(skill) {
|
|
17
|
+
if (!skill.name || skill.name.trim() === '') {
|
|
18
|
+
throw new Error('Skill name is required');
|
|
19
|
+
}
|
|
20
|
+
if (!skill.prompt || skill.prompt.trim() === '') {
|
|
21
|
+
throw new Error('Skill prompt is required');
|
|
22
|
+
}
|
|
23
|
+
this.skills.set(skill.name, {
|
|
24
|
+
...skill,
|
|
25
|
+
enabled: skill.enabled ?? true,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Register multiple skills at once
|
|
30
|
+
*/
|
|
31
|
+
registerAll(skills) {
|
|
32
|
+
for (const skill of skills) {
|
|
33
|
+
this.register(skill);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get a skill by name
|
|
38
|
+
*/
|
|
39
|
+
get(name) {
|
|
40
|
+
return this.skills.get(name);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Check if a skill exists
|
|
44
|
+
*/
|
|
45
|
+
has(name) {
|
|
46
|
+
return this.skills.has(name);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get all registered skills
|
|
50
|
+
*/
|
|
51
|
+
getAll() {
|
|
52
|
+
return Array.from(this.skills.values());
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get all enabled skills
|
|
56
|
+
*/
|
|
57
|
+
getEnabled() {
|
|
58
|
+
return this.getAll().filter((s) => s.enabled !== false);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get skills by tag
|
|
62
|
+
*/
|
|
63
|
+
getByTag(tag) {
|
|
64
|
+
return this.getAll().filter((s) => s.tags?.includes(tag));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get skill names
|
|
68
|
+
*/
|
|
69
|
+
getNames() {
|
|
70
|
+
return Array.from(this.skills.keys());
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Remove a skill
|
|
74
|
+
*/
|
|
75
|
+
remove(name) {
|
|
76
|
+
return this.skills.delete(name);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Clear all skills
|
|
80
|
+
*/
|
|
81
|
+
clear() {
|
|
82
|
+
this.skills.clear();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Enable a skill
|
|
86
|
+
*/
|
|
87
|
+
enable(name) {
|
|
88
|
+
const skill = this.skills.get(name);
|
|
89
|
+
if (!skill)
|
|
90
|
+
return false;
|
|
91
|
+
skill.enabled = true;
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Disable a skill
|
|
96
|
+
*/
|
|
97
|
+
disable(name) {
|
|
98
|
+
const skill = this.skills.get(name);
|
|
99
|
+
if (!skill)
|
|
100
|
+
return false;
|
|
101
|
+
skill.enabled = false;
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Invoke a skill by name
|
|
106
|
+
*/
|
|
107
|
+
invoke(name, options) {
|
|
108
|
+
const skill = this.skills.get(name);
|
|
109
|
+
if (!skill) {
|
|
110
|
+
return {
|
|
111
|
+
skill: { name, description: '', prompt: '' },
|
|
112
|
+
prompt: '',
|
|
113
|
+
success: false,
|
|
114
|
+
error: `Skill not found: ${name}`,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (skill.enabled === false) {
|
|
118
|
+
return {
|
|
119
|
+
skill,
|
|
120
|
+
prompt: '',
|
|
121
|
+
success: false,
|
|
122
|
+
error: `Skill is disabled: ${name}`,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
let prompt = skill.prompt;
|
|
126
|
+
// Interpolate variables
|
|
127
|
+
if (options?.variables) {
|
|
128
|
+
for (const [key, value] of Object.entries(options.variables)) {
|
|
129
|
+
prompt = prompt.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// Add context if provided
|
|
133
|
+
if (options?.context) {
|
|
134
|
+
prompt = `${options.context}\n\n${prompt}`;
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
skill,
|
|
138
|
+
prompt,
|
|
139
|
+
success: true,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Get skill count
|
|
144
|
+
*/
|
|
145
|
+
get size() {
|
|
146
|
+
return this.skills.size;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Helper function to define a skill
|
|
151
|
+
*/
|
|
152
|
+
export function defineSkill(options) {
|
|
153
|
+
return {
|
|
154
|
+
name: options.name,
|
|
155
|
+
description: options.description,
|
|
156
|
+
prompt: options.prompt,
|
|
157
|
+
tags: options.tags,
|
|
158
|
+
version: options.version,
|
|
159
|
+
enabled: options.enabled ?? true,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Create a new skill registry
|
|
164
|
+
*/
|
|
165
|
+
export function createSkillRegistry() {
|
|
166
|
+
return new SkillRegistry();
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Built-in skills that are commonly useful
|
|
170
|
+
*/
|
|
171
|
+
export const builtinSkills = [
|
|
172
|
+
defineSkill({
|
|
173
|
+
name: 'code-review',
|
|
174
|
+
description: 'Perform a thorough code review',
|
|
175
|
+
prompt: `You are now in code review mode. When reviewing code:
|
|
176
|
+
|
|
177
|
+
1. **Security**: Check for vulnerabilities (injection, XSS, auth issues)
|
|
178
|
+
2. **Performance**: Identify bottlenecks and optimization opportunities
|
|
179
|
+
3. **Maintainability**: Evaluate code clarity, naming, and structure
|
|
180
|
+
4. **Best Practices**: Check for anti-patterns and code smells
|
|
181
|
+
5. **Testing**: Assess test coverage and edge cases
|
|
182
|
+
|
|
183
|
+
Provide specific, actionable feedback with line references.
|
|
184
|
+
Format: List issues by severity (Critical, High, Medium, Low).`,
|
|
185
|
+
tags: ['development', 'review'],
|
|
186
|
+
}),
|
|
187
|
+
defineSkill({
|
|
188
|
+
name: 'debug',
|
|
189
|
+
description: 'Systematic debugging approach',
|
|
190
|
+
prompt: `You are now in debugging mode. Follow this systematic approach:
|
|
191
|
+
|
|
192
|
+
1. **Reproduce**: Ensure the issue can be consistently reproduced
|
|
193
|
+
2. **Isolate**: Narrow down the scope to specific components
|
|
194
|
+
3. **Analyze**: Examine logs, stack traces, and state
|
|
195
|
+
4. **Hypothesize**: Form theories about root cause
|
|
196
|
+
5. **Test**: Verify hypotheses with targeted tests
|
|
197
|
+
6. **Fix**: Implement and validate the fix
|
|
198
|
+
7. **Document**: Note the issue and solution for future reference
|
|
199
|
+
|
|
200
|
+
Be methodical and avoid jumping to conclusions.`,
|
|
201
|
+
tags: ['development', 'troubleshooting'],
|
|
202
|
+
}),
|
|
203
|
+
defineSkill({
|
|
204
|
+
name: 'explain',
|
|
205
|
+
description: 'Explain code or concepts clearly',
|
|
206
|
+
prompt: `You are now in explanation mode. When explaining:
|
|
207
|
+
|
|
208
|
+
1. Start with a high-level overview
|
|
209
|
+
2. Break down complex concepts into smaller parts
|
|
210
|
+
3. Use analogies and examples where helpful
|
|
211
|
+
4. Highlight key points and takeaways
|
|
212
|
+
5. Provide context for why things work the way they do
|
|
213
|
+
6. Tailor complexity to the audience level
|
|
214
|
+
|
|
215
|
+
Be clear, concise, and avoid unnecessary jargon.`,
|
|
216
|
+
tags: ['education', 'documentation'],
|
|
217
|
+
}),
|
|
218
|
+
defineSkill({
|
|
219
|
+
name: 'refactor',
|
|
220
|
+
description: 'Guide code refactoring',
|
|
221
|
+
prompt: `You are now in refactoring mode. When refactoring:
|
|
222
|
+
|
|
223
|
+
1. **Understand**: Fully understand existing behavior before changing
|
|
224
|
+
2. **Test First**: Ensure tests exist to catch regressions
|
|
225
|
+
3. **Small Steps**: Make incremental, reversible changes
|
|
226
|
+
4. **Single Purpose**: Each refactor should have one clear goal
|
|
227
|
+
5. **Preserve Behavior**: No functional changes during refactoring
|
|
228
|
+
6. **Clean Up**: Remove dead code, simplify complexity
|
|
229
|
+
|
|
230
|
+
Follow established patterns and maintain consistency with the codebase.`,
|
|
231
|
+
tags: ['development', 'improvement'],
|
|
232
|
+
}),
|
|
233
|
+
defineSkill({
|
|
234
|
+
name: 'planning',
|
|
235
|
+
description: 'Help plan and structure work',
|
|
236
|
+
prompt: `You are now in planning mode. When planning:
|
|
237
|
+
|
|
238
|
+
1. **Clarify Goals**: Understand the desired outcome
|
|
239
|
+
2. **Break Down**: Divide into manageable tasks
|
|
240
|
+
3. **Dependencies**: Identify task dependencies and order
|
|
241
|
+
4. **Risks**: Anticipate potential blockers
|
|
242
|
+
5. **Milestones**: Define checkpoints to measure progress
|
|
243
|
+
6. **Resources**: Identify what's needed
|
|
244
|
+
|
|
245
|
+
Provide actionable plans with clear next steps.`,
|
|
246
|
+
tags: ['project-management', 'organization'],
|
|
247
|
+
}),
|
|
248
|
+
defineSkill({
|
|
249
|
+
name: 'security-review',
|
|
250
|
+
description: 'Focus on security aspects',
|
|
251
|
+
prompt: `You are now in security review mode. Focus on:
|
|
252
|
+
|
|
253
|
+
1. **Authentication**: Verify proper auth mechanisms
|
|
254
|
+
2. **Authorization**: Check access control at every level
|
|
255
|
+
3. **Input Validation**: Look for injection vulnerabilities
|
|
256
|
+
4. **Data Protection**: Ensure sensitive data is protected
|
|
257
|
+
5. **Dependencies**: Check for known vulnerabilities
|
|
258
|
+
6. **Configuration**: Review security settings
|
|
259
|
+
7. **Logging**: Verify security events are logged
|
|
260
|
+
|
|
261
|
+
Apply OWASP guidelines and security best practices.`,
|
|
262
|
+
tags: ['security', 'review'],
|
|
263
|
+
}),
|
|
264
|
+
];
|
|
265
|
+
/**
|
|
266
|
+
* Default skill registry with built-in skills
|
|
267
|
+
*/
|
|
268
|
+
const defaultRegistry = new SkillRegistry();
|
|
269
|
+
defaultRegistry.registerAll(builtinSkills);
|
|
270
|
+
/**
|
|
271
|
+
* Get the default skill registry
|
|
272
|
+
*/
|
|
273
|
+
export function getDefaultSkillRegistry() {
|
|
274
|
+
return defaultRegistry;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Reset the default registry to built-in skills only
|
|
278
|
+
*/
|
|
279
|
+
export function resetDefaultSkillRegistry() {
|
|
280
|
+
defaultRegistry.clear();
|
|
281
|
+
defaultRegistry.registerAll(builtinSkills);
|
|
282
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent State Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helper functions for serializing and restoring agent state.
|
|
5
|
+
*/
|
|
6
|
+
import type { Message } from '../providers/types.js';
|
|
7
|
+
import type { TodoItem } from '../tools/builtin/todo.js';
|
|
8
|
+
import type { AgentState } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Generate a new session ID
|
|
11
|
+
*/
|
|
12
|
+
export declare function generateSessionId(): string;
|
|
13
|
+
/**
|
|
14
|
+
* Create an empty agent state
|
|
15
|
+
*/
|
|
16
|
+
export declare function createEmptyState(sessionId: string, systemPrompt: string): AgentState;
|
|
17
|
+
/**
|
|
18
|
+
* Create agent state from current agent state
|
|
19
|
+
*/
|
|
20
|
+
export declare function createAgentState(options: {
|
|
21
|
+
sessionId: string;
|
|
22
|
+
systemPrompt: string;
|
|
23
|
+
model?: string;
|
|
24
|
+
messages: Message[];
|
|
25
|
+
todos: TodoItem[];
|
|
26
|
+
currentIteration: number;
|
|
27
|
+
totalTokensUsed: number;
|
|
28
|
+
createdAt?: string;
|
|
29
|
+
}): AgentState;
|
|
30
|
+
/**
|
|
31
|
+
* Serialize todos (convert Date to ISO string)
|
|
32
|
+
*/
|
|
33
|
+
export declare function serializeTodos(todos: TodoItem[]): TodoItem[];
|
|
34
|
+
/**
|
|
35
|
+
* Deserialize todos (convert ISO string back to Date)
|
|
36
|
+
*/
|
|
37
|
+
export declare function deserializeTodos(todos: TodoItem[]): TodoItem[];
|
|
38
|
+
/**
|
|
39
|
+
* Update state's updatedAt timestamp
|
|
40
|
+
*/
|
|
41
|
+
export declare function touchState(state: AgentState): AgentState;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent State Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helper functions for serializing and restoring agent state.
|
|
5
|
+
*/
|
|
6
|
+
import { CURRENT_STATE_VERSION } from './types.js';
|
|
7
|
+
import { generateId } from '../utils/index.js';
|
|
8
|
+
/**
|
|
9
|
+
* Generate a new session ID
|
|
10
|
+
*/
|
|
11
|
+
export function generateSessionId() {
|
|
12
|
+
return `session_${generateId()}`;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Create an empty agent state
|
|
16
|
+
*/
|
|
17
|
+
export function createEmptyState(sessionId, systemPrompt) {
|
|
18
|
+
const now = new Date().toISOString();
|
|
19
|
+
return {
|
|
20
|
+
sessionId,
|
|
21
|
+
messages: [],
|
|
22
|
+
systemPrompt,
|
|
23
|
+
todos: [],
|
|
24
|
+
currentIteration: 0,
|
|
25
|
+
totalTokensUsed: 0,
|
|
26
|
+
createdAt: now,
|
|
27
|
+
updatedAt: now,
|
|
28
|
+
version: CURRENT_STATE_VERSION,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create agent state from current agent state
|
|
33
|
+
*/
|
|
34
|
+
export function createAgentState(options) {
|
|
35
|
+
const now = new Date().toISOString();
|
|
36
|
+
return {
|
|
37
|
+
sessionId: options.sessionId,
|
|
38
|
+
messages: options.messages,
|
|
39
|
+
systemPrompt: options.systemPrompt,
|
|
40
|
+
model: options.model,
|
|
41
|
+
todos: serializeTodos(options.todos),
|
|
42
|
+
currentIteration: options.currentIteration,
|
|
43
|
+
totalTokensUsed: options.totalTokensUsed,
|
|
44
|
+
createdAt: options.createdAt || now,
|
|
45
|
+
updatedAt: now,
|
|
46
|
+
version: CURRENT_STATE_VERSION,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Serialize todos (convert Date to ISO string)
|
|
51
|
+
*/
|
|
52
|
+
export function serializeTodos(todos) {
|
|
53
|
+
return todos.map((todo) => ({
|
|
54
|
+
...todo,
|
|
55
|
+
// Convert Date objects to ISO strings for JSON serialization
|
|
56
|
+
createdAt: todo.createdAt instanceof Date
|
|
57
|
+
? todo.createdAt
|
|
58
|
+
: new Date(todo.createdAt),
|
|
59
|
+
updatedAt: todo.updatedAt instanceof Date
|
|
60
|
+
? todo.updatedAt
|
|
61
|
+
: new Date(todo.updatedAt),
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Deserialize todos (convert ISO string back to Date)
|
|
66
|
+
*/
|
|
67
|
+
export function deserializeTodos(todos) {
|
|
68
|
+
return todos.map((todo) => {
|
|
69
|
+
const createdAt = new Date(todo.createdAt);
|
|
70
|
+
// updatedAt might be undefined in stored state
|
|
71
|
+
const rawUpdatedAt = todo.updatedAt;
|
|
72
|
+
const updatedAt = rawUpdatedAt ? new Date(rawUpdatedAt) : createdAt; // Default to createdAt if undefined
|
|
73
|
+
return {
|
|
74
|
+
...todo,
|
|
75
|
+
createdAt,
|
|
76
|
+
updatedAt,
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Update state's updatedAt timestamp
|
|
82
|
+
*/
|
|
83
|
+
export function touchState(state) {
|
|
84
|
+
return {
|
|
85
|
+
...state,
|
|
86
|
+
updatedAt: new Date().toISOString(),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checkpointer Implementations
|
|
3
|
+
*
|
|
4
|
+
* Built-in checkpointers for state persistence.
|
|
5
|
+
*/
|
|
6
|
+
import type { AgentState, Checkpointer, SessionMetadata, SessionInfo, ListSessionsOptions, StateSerializer } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* In-memory checkpointer for development and testing.
|
|
9
|
+
* State is lost when the process exits.
|
|
10
|
+
*/
|
|
11
|
+
export declare class MemoryCheckpointer implements Checkpointer {
|
|
12
|
+
private readonly sessions;
|
|
13
|
+
/**
|
|
14
|
+
* Save state to memory
|
|
15
|
+
*
|
|
16
|
+
* Pre-validates state before saving to prevent corrupted checkpoints.
|
|
17
|
+
* This catches issues like invalid data types that would fail on reload.
|
|
18
|
+
*/
|
|
19
|
+
save(sessionId: string, state: AgentState, metadataOverrides?: Partial<SessionMetadata>): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Load state from memory
|
|
22
|
+
*/
|
|
23
|
+
load(sessionId: string): Promise<AgentState | null>;
|
|
24
|
+
/**
|
|
25
|
+
* List all sessions
|
|
26
|
+
*/
|
|
27
|
+
list(options?: ListSessionsOptions): Promise<SessionInfo[]>;
|
|
28
|
+
/**
|
|
29
|
+
* Delete a session
|
|
30
|
+
*/
|
|
31
|
+
delete(sessionId: string): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Check if session exists
|
|
34
|
+
*/
|
|
35
|
+
exists(sessionId: string): Promise<boolean>;
|
|
36
|
+
/**
|
|
37
|
+
* Get session metadata
|
|
38
|
+
*/
|
|
39
|
+
getMetadata(sessionId: string): Promise<SessionMetadata | null>;
|
|
40
|
+
/**
|
|
41
|
+
* Clear all sessions (useful for testing)
|
|
42
|
+
*/
|
|
43
|
+
clear(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Get current session count
|
|
46
|
+
*/
|
|
47
|
+
get size(): number;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Options for FileCheckpointer
|
|
51
|
+
*/
|
|
52
|
+
export interface FileCheckpointerOptions {
|
|
53
|
+
/**
|
|
54
|
+
* Custom serializer (defaults to JsonSerializer)
|
|
55
|
+
*/
|
|
56
|
+
serializer?: StateSerializer;
|
|
57
|
+
/**
|
|
58
|
+
* File extension (defaults to '.json')
|
|
59
|
+
*/
|
|
60
|
+
extension?: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* File-based checkpointer for simple persistence.
|
|
64
|
+
* Stores each session as a separate JSON file.
|
|
65
|
+
*/
|
|
66
|
+
export declare class FileCheckpointer implements Checkpointer {
|
|
67
|
+
private readonly baseDir;
|
|
68
|
+
private readonly serializer;
|
|
69
|
+
private readonly extension;
|
|
70
|
+
constructor(baseDir: string, options?: FileCheckpointerOptions);
|
|
71
|
+
/**
|
|
72
|
+
* Get file path for a session
|
|
73
|
+
*/
|
|
74
|
+
private getStatePath;
|
|
75
|
+
/**
|
|
76
|
+
* Get metadata file path for a session
|
|
77
|
+
*/
|
|
78
|
+
private getMetadataPath;
|
|
79
|
+
/**
|
|
80
|
+
* Ensure base directory exists
|
|
81
|
+
*/
|
|
82
|
+
private ensureDir;
|
|
83
|
+
/**
|
|
84
|
+
* Save state to file
|
|
85
|
+
*
|
|
86
|
+
* Pre-validates state before saving to prevent corrupted checkpoints.
|
|
87
|
+
* This catches issues like invalid data types that would fail on reload.
|
|
88
|
+
*/
|
|
89
|
+
save(sessionId: string, state: AgentState, metadataOverrides?: Partial<SessionMetadata>): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Load state from file
|
|
92
|
+
*/
|
|
93
|
+
load(sessionId: string): Promise<AgentState | null>;
|
|
94
|
+
/**
|
|
95
|
+
* List all sessions
|
|
96
|
+
*/
|
|
97
|
+
list(options?: ListSessionsOptions): Promise<SessionInfo[]>;
|
|
98
|
+
/**
|
|
99
|
+
* Delete a session
|
|
100
|
+
*/
|
|
101
|
+
delete(sessionId: string): Promise<boolean>;
|
|
102
|
+
/**
|
|
103
|
+
* Check if session exists
|
|
104
|
+
*/
|
|
105
|
+
exists(sessionId: string): Promise<boolean>;
|
|
106
|
+
/**
|
|
107
|
+
* Get session metadata
|
|
108
|
+
*/
|
|
109
|
+
getMetadata(sessionId: string): Promise<SessionMetadata | null>;
|
|
110
|
+
}
|