@lleverage-ai/agent-sdk 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/LICENSE +21 -0
- package/README.md +2321 -0
- package/dist/agent.d.ts +52 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +2122 -0
- package/dist/agent.js.map +1 -0
- package/dist/backend.d.ts +378 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.js +71 -0
- package/dist/backend.js.map +1 -0
- package/dist/backends/composite.d.ts +258 -0
- package/dist/backends/composite.d.ts.map +1 -0
- package/dist/backends/composite.js +437 -0
- package/dist/backends/composite.js.map +1 -0
- package/dist/backends/filesystem.d.ts +268 -0
- package/dist/backends/filesystem.d.ts.map +1 -0
- package/dist/backends/filesystem.js +623 -0
- package/dist/backends/filesystem.js.map +1 -0
- package/dist/backends/index.d.ts +14 -0
- package/dist/backends/index.d.ts.map +1 -0
- package/dist/backends/index.js +14 -0
- package/dist/backends/index.js.map +1 -0
- package/dist/backends/persistent.d.ts +312 -0
- package/dist/backends/persistent.d.ts.map +1 -0
- package/dist/backends/persistent.js +519 -0
- package/dist/backends/persistent.js.map +1 -0
- package/dist/backends/sandbox.d.ts +315 -0
- package/dist/backends/sandbox.d.ts.map +1 -0
- package/dist/backends/sandbox.js +490 -0
- package/dist/backends/sandbox.js.map +1 -0
- package/dist/backends/state.d.ts +225 -0
- package/dist/backends/state.d.ts.map +1 -0
- package/dist/backends/state.js +396 -0
- package/dist/backends/state.js.map +1 -0
- package/dist/checkpointer/file-saver.d.ts +182 -0
- package/dist/checkpointer/file-saver.d.ts.map +1 -0
- package/dist/checkpointer/file-saver.js +298 -0
- package/dist/checkpointer/file-saver.js.map +1 -0
- package/dist/checkpointer/index.d.ts +40 -0
- package/dist/checkpointer/index.d.ts.map +1 -0
- package/dist/checkpointer/index.js +40 -0
- package/dist/checkpointer/index.js.map +1 -0
- package/dist/checkpointer/kv-saver.d.ts +142 -0
- package/dist/checkpointer/kv-saver.d.ts.map +1 -0
- package/dist/checkpointer/kv-saver.js +176 -0
- package/dist/checkpointer/kv-saver.js.map +1 -0
- package/dist/checkpointer/memory-saver.d.ts +158 -0
- package/dist/checkpointer/memory-saver.d.ts.map +1 -0
- package/dist/checkpointer/memory-saver.js +222 -0
- package/dist/checkpointer/memory-saver.js.map +1 -0
- package/dist/checkpointer/types.d.ts +353 -0
- package/dist/checkpointer/types.d.ts.map +1 -0
- package/dist/checkpointer/types.js +159 -0
- package/dist/checkpointer/types.js.map +1 -0
- package/dist/context-manager.d.ts +627 -0
- package/dist/context-manager.d.ts.map +1 -0
- package/dist/context-manager.js +1039 -0
- package/dist/context-manager.js.map +1 -0
- package/dist/context.d.ts +57 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +76 -0
- package/dist/context.js.map +1 -0
- package/dist/errors/index.d.ts +611 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +1023 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/generation-helpers.d.ts +126 -0
- package/dist/generation-helpers.d.ts.map +1 -0
- package/dist/generation-helpers.js +181 -0
- package/dist/generation-helpers.js.map +1 -0
- package/dist/hooks/audit.d.ts +210 -0
- package/dist/hooks/audit.d.ts.map +1 -0
- package/dist/hooks/audit.js +305 -0
- package/dist/hooks/audit.js.map +1 -0
- package/dist/hooks/cache.d.ts +180 -0
- package/dist/hooks/cache.d.ts.map +1 -0
- package/dist/hooks/cache.js +273 -0
- package/dist/hooks/cache.js.map +1 -0
- package/dist/hooks/guardrails.d.ts +145 -0
- package/dist/hooks/guardrails.d.ts.map +1 -0
- package/dist/hooks/guardrails.js +326 -0
- package/dist/hooks/guardrails.js.map +1 -0
- package/dist/hooks/index.d.ts +18 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +32 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/logging.d.ts +193 -0
- package/dist/hooks/logging.d.ts.map +1 -0
- package/dist/hooks/logging.js +345 -0
- package/dist/hooks/logging.js.map +1 -0
- package/dist/hooks/parallel-guardrails.d.ts +268 -0
- package/dist/hooks/parallel-guardrails.d.ts.map +1 -0
- package/dist/hooks/parallel-guardrails.js +416 -0
- package/dist/hooks/parallel-guardrails.js.map +1 -0
- package/dist/hooks/rate-limit.d.ts +305 -0
- package/dist/hooks/rate-limit.d.ts.map +1 -0
- package/dist/hooks/rate-limit.js +372 -0
- package/dist/hooks/rate-limit.js.map +1 -0
- package/dist/hooks/retry.d.ts +144 -0
- package/dist/hooks/retry.d.ts.map +1 -0
- package/dist/hooks/retry.js +210 -0
- package/dist/hooks/retry.js.map +1 -0
- package/dist/hooks/secrets.d.ts +174 -0
- package/dist/hooks/secrets.d.ts.map +1 -0
- package/dist/hooks/secrets.js +306 -0
- package/dist/hooks/secrets.js.map +1 -0
- package/dist/hooks.d.ts +229 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +352 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +97 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +182 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/env.d.ts +25 -0
- package/dist/mcp/env.d.ts.map +1 -0
- package/dist/mcp/env.js +18 -0
- package/dist/mcp/env.js.map +1 -0
- package/dist/mcp/index.d.ts +16 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +17 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/manager.d.ts +184 -0
- package/dist/mcp/manager.d.ts.map +1 -0
- package/dist/mcp/manager.js +446 -0
- package/dist/mcp/manager.js.map +1 -0
- package/dist/mcp/types.d.ts +58 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +7 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/mcp/validation.d.ts +119 -0
- package/dist/mcp/validation.d.ts.map +1 -0
- package/dist/mcp/validation.js +407 -0
- package/dist/mcp/validation.js.map +1 -0
- package/dist/mcp/virtual-server.d.ts +78 -0
- package/dist/mcp/virtual-server.d.ts.map +1 -0
- package/dist/mcp/virtual-server.js +137 -0
- package/dist/mcp/virtual-server.js.map +1 -0
- package/dist/memory/filesystem-store.d.ts +217 -0
- package/dist/memory/filesystem-store.d.ts.map +1 -0
- package/dist/memory/filesystem-store.js +343 -0
- package/dist/memory/filesystem-store.js.map +1 -0
- package/dist/memory/index.d.ts +46 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +46 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/loader.d.ts +396 -0
- package/dist/memory/loader.d.ts.map +1 -0
- package/dist/memory/loader.js +419 -0
- package/dist/memory/loader.js.map +1 -0
- package/dist/memory/permissions.d.ts +282 -0
- package/dist/memory/permissions.d.ts.map +1 -0
- package/dist/memory/permissions.js +297 -0
- package/dist/memory/permissions.js.map +1 -0
- package/dist/memory/rules.d.ts +249 -0
- package/dist/memory/rules.d.ts.map +1 -0
- package/dist/memory/rules.js +362 -0
- package/dist/memory/rules.js.map +1 -0
- package/dist/memory/store.d.ts +286 -0
- package/dist/memory/store.d.ts.map +1 -0
- package/dist/memory/store.js +263 -0
- package/dist/memory/store.js.map +1 -0
- package/dist/middleware/apply.d.ts +73 -0
- package/dist/middleware/apply.d.ts.map +1 -0
- package/dist/middleware/apply.js +219 -0
- package/dist/middleware/apply.js.map +1 -0
- package/dist/middleware/context.d.ts +33 -0
- package/dist/middleware/context.d.ts.map +1 -0
- package/dist/middleware/context.js +176 -0
- package/dist/middleware/context.js.map +1 -0
- package/dist/middleware/index.d.ts +31 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +32 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/logging.d.ts +137 -0
- package/dist/middleware/logging.d.ts.map +1 -0
- package/dist/middleware/logging.js +374 -0
- package/dist/middleware/logging.js.map +1 -0
- package/dist/middleware/types.d.ts +183 -0
- package/dist/middleware/types.d.ts.map +1 -0
- package/dist/middleware/types.js +11 -0
- package/dist/middleware/types.js.map +1 -0
- package/dist/observability/events.d.ts +183 -0
- package/dist/observability/events.d.ts.map +1 -0
- package/dist/observability/events.js +305 -0
- package/dist/observability/events.js.map +1 -0
- package/dist/observability/index.d.ts +55 -0
- package/dist/observability/index.d.ts.map +1 -0
- package/dist/observability/index.js +87 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/observability/logger.d.ts +318 -0
- package/dist/observability/logger.d.ts.map +1 -0
- package/dist/observability/logger.js +436 -0
- package/dist/observability/logger.js.map +1 -0
- package/dist/observability/metrics.d.ts +341 -0
- package/dist/observability/metrics.d.ts.map +1 -0
- package/dist/observability/metrics.js +490 -0
- package/dist/observability/metrics.js.map +1 -0
- package/dist/observability/preset.d.ts +161 -0
- package/dist/observability/preset.d.ts.map +1 -0
- package/dist/observability/preset.js +133 -0
- package/dist/observability/preset.js.map +1 -0
- package/dist/observability/streaming.d.ts +113 -0
- package/dist/observability/streaming.d.ts.map +1 -0
- package/dist/observability/streaming.js +114 -0
- package/dist/observability/streaming.js.map +1 -0
- package/dist/observability/tracing.d.ts +378 -0
- package/dist/observability/tracing.d.ts.map +1 -0
- package/dist/observability/tracing.js +539 -0
- package/dist/observability/tracing.js.map +1 -0
- package/dist/plugins.d.ts +55 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +63 -0
- package/dist/plugins.js.map +1 -0
- package/dist/presets/index.d.ts +7 -0
- package/dist/presets/index.d.ts.map +1 -0
- package/dist/presets/index.js +7 -0
- package/dist/presets/index.js.map +1 -0
- package/dist/presets/production.d.ts +262 -0
- package/dist/presets/production.d.ts.map +1 -0
- package/dist/presets/production.js +295 -0
- package/dist/presets/production.js.map +1 -0
- package/dist/security/index.d.ts +179 -0
- package/dist/security/index.d.ts.map +1 -0
- package/dist/security/index.js +323 -0
- package/dist/security/index.js.map +1 -0
- package/dist/subagents/advanced.d.ts +413 -0
- package/dist/subagents/advanced.d.ts.map +1 -0
- package/dist/subagents/advanced.js +396 -0
- package/dist/subagents/advanced.js.map +1 -0
- package/dist/subagents/index.d.ts +14 -0
- package/dist/subagents/index.d.ts.map +1 -0
- package/dist/subagents/index.js +15 -0
- package/dist/subagents/index.js.map +1 -0
- package/dist/subagents.d.ts +73 -0
- package/dist/subagents.d.ts.map +1 -0
- package/dist/subagents.js +213 -0
- package/dist/subagents.js.map +1 -0
- package/dist/task-store/file-store.d.ts +76 -0
- package/dist/task-store/file-store.d.ts.map +1 -0
- package/dist/task-store/file-store.js +190 -0
- package/dist/task-store/file-store.js.map +1 -0
- package/dist/task-store/index.d.ts +11 -0
- package/dist/task-store/index.d.ts.map +1 -0
- package/dist/task-store/index.js +10 -0
- package/dist/task-store/index.js.map +1 -0
- package/dist/task-store/kv-store.d.ts +140 -0
- package/dist/task-store/kv-store.d.ts.map +1 -0
- package/dist/task-store/kv-store.js +169 -0
- package/dist/task-store/kv-store.js.map +1 -0
- package/dist/task-store/memory-store.d.ts +66 -0
- package/dist/task-store/memory-store.d.ts.map +1 -0
- package/dist/task-store/memory-store.js +125 -0
- package/dist/task-store/memory-store.js.map +1 -0
- package/dist/task-store/types.d.ts +235 -0
- package/dist/task-store/types.d.ts.map +1 -0
- package/dist/task-store/types.js +110 -0
- package/dist/task-store/types.js.map +1 -0
- package/dist/testing/assertions.d.ts +401 -0
- package/dist/testing/assertions.d.ts.map +1 -0
- package/dist/testing/assertions.js +630 -0
- package/dist/testing/assertions.js.map +1 -0
- package/dist/testing/index.d.ts +343 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +360 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/mock-agent.d.ts +214 -0
- package/dist/testing/mock-agent.d.ts.map +1 -0
- package/dist/testing/mock-agent.js +448 -0
- package/dist/testing/mock-agent.js.map +1 -0
- package/dist/testing/recorder.d.ts +288 -0
- package/dist/testing/recorder.d.ts.map +1 -0
- package/dist/testing/recorder.js +499 -0
- package/dist/testing/recorder.js.map +1 -0
- package/dist/tools/execute.d.ts +104 -0
- package/dist/tools/execute.d.ts.map +1 -0
- package/dist/tools/execute.js +191 -0
- package/dist/tools/execute.js.map +1 -0
- package/dist/tools/factory.d.ts +260 -0
- package/dist/tools/factory.d.ts.map +1 -0
- package/dist/tools/factory.js +241 -0
- package/dist/tools/factory.js.map +1 -0
- package/dist/tools/filesystem.d.ts +215 -0
- package/dist/tools/filesystem.d.ts.map +1 -0
- package/dist/tools/filesystem.js +311 -0
- package/dist/tools/filesystem.js.map +1 -0
- package/dist/tools/index.d.ts +33 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +33 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/search.d.ts +59 -0
- package/dist/tools/search.d.ts.map +1 -0
- package/dist/tools/search.js +94 -0
- package/dist/tools/search.js.map +1 -0
- package/dist/tools/skills.d.ts +354 -0
- package/dist/tools/skills.d.ts.map +1 -0
- package/dist/tools/skills.js +413 -0
- package/dist/tools/skills.js.map +1 -0
- package/dist/tools/task.d.ts +272 -0
- package/dist/tools/task.d.ts.map +1 -0
- package/dist/tools/task.js +521 -0
- package/dist/tools/task.js.map +1 -0
- package/dist/tools/todos.d.ts +131 -0
- package/dist/tools/todos.d.ts.map +1 -0
- package/dist/tools/todos.js +120 -0
- package/dist/tools/todos.js.map +1 -0
- package/dist/tools/tool-registry.d.ts +424 -0
- package/dist/tools/tool-registry.d.ts.map +1 -0
- package/dist/tools/tool-registry.js +607 -0
- package/dist/tools/tool-registry.js.map +1 -0
- package/dist/tools/user-interaction.d.ts +116 -0
- package/dist/tools/user-interaction.d.ts.map +1 -0
- package/dist/tools/user-interaction.js +147 -0
- package/dist/tools/user-interaction.js.map +1 -0
- package/dist/tools/utils.d.ts +124 -0
- package/dist/tools/utils.d.ts.map +1 -0
- package/dist/tools/utils.js +189 -0
- package/dist/tools/utils.js.map +1 -0
- package/dist/tools.d.ts +74 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +73 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +2421 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +55 -0
- package/dist/types.js.map +1 -0
- package/package.json +81 -0
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skill tool for progressive disclosure of capabilities.
|
|
3
|
+
*
|
|
4
|
+
* The skill tool allows agents to load skills (tools + prompts) on-demand
|
|
5
|
+
* based on conversation context. This keeps the initial context small and
|
|
6
|
+
* focused, expanding capabilities only as needed.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
import { tool } from "ai";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
// =============================================================================
|
|
13
|
+
// Skill Registry
|
|
14
|
+
// =============================================================================
|
|
15
|
+
/**
|
|
16
|
+
* Registry for managing loadable skills.
|
|
17
|
+
*
|
|
18
|
+
* The registry tracks available skills and which ones have been loaded.
|
|
19
|
+
* Skills are loaded on-demand by the agent using the skill tool.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const registry = new SkillRegistry({
|
|
24
|
+
* skills: [gitSkill, dockerSkill],
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Register more skills later
|
|
28
|
+
* registry.register(kubernetesSkill);
|
|
29
|
+
*
|
|
30
|
+
* // Check available skills
|
|
31
|
+
* const available = registry.listAvailable();
|
|
32
|
+
*
|
|
33
|
+
* // Load a skill
|
|
34
|
+
* const result = registry.load("git");
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @category Tools
|
|
38
|
+
*/
|
|
39
|
+
export class SkillRegistry {
|
|
40
|
+
/** All registered skills */
|
|
41
|
+
skills = new Map();
|
|
42
|
+
/** Currently loaded skills */
|
|
43
|
+
loadedSkills = new Set();
|
|
44
|
+
/** Callback for skill load events */
|
|
45
|
+
onSkillLoaded;
|
|
46
|
+
/** MCP manager for loading MCP tools */
|
|
47
|
+
mcpManager;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new skill registry.
|
|
50
|
+
*
|
|
51
|
+
* @param options - Configuration options
|
|
52
|
+
*/
|
|
53
|
+
constructor(options = {}) {
|
|
54
|
+
this.onSkillLoaded = options.onSkillLoaded;
|
|
55
|
+
this.mcpManager = options.mcpManager;
|
|
56
|
+
if (options.skills) {
|
|
57
|
+
for (const skill of options.skills) {
|
|
58
|
+
this.register(skill);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Register a skill with the registry.
|
|
64
|
+
*
|
|
65
|
+
* @param skill - The skill definition to register
|
|
66
|
+
* @throws Error if a skill with the same name is already registered
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* registry.register({
|
|
71
|
+
* name: "aws",
|
|
72
|
+
* description: "AWS cloud operations",
|
|
73
|
+
* tools: { ... },
|
|
74
|
+
* prompt: "You now have access to AWS tools.",
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
register(skill) {
|
|
79
|
+
if (this.skills.has(skill.name)) {
|
|
80
|
+
throw new Error(`Skill '${skill.name}' is already registered`);
|
|
81
|
+
}
|
|
82
|
+
this.skills.set(skill.name, skill);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Unregister a skill from the registry.
|
|
86
|
+
*
|
|
87
|
+
* @param name - The name of the skill to unregister
|
|
88
|
+
* @returns True if the skill was found and removed
|
|
89
|
+
*/
|
|
90
|
+
unregister(name) {
|
|
91
|
+
this.loadedSkills.delete(name);
|
|
92
|
+
return this.skills.delete(name);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if a skill is registered.
|
|
96
|
+
*
|
|
97
|
+
* @param name - The name of the skill to check
|
|
98
|
+
* @returns True if the skill is registered
|
|
99
|
+
*/
|
|
100
|
+
has(name) {
|
|
101
|
+
return this.skills.has(name);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Check if a skill is currently loaded.
|
|
105
|
+
*
|
|
106
|
+
* @param name - The name of the skill to check
|
|
107
|
+
* @returns True if the skill is loaded
|
|
108
|
+
*/
|
|
109
|
+
isLoaded(name) {
|
|
110
|
+
return this.loadedSkills.has(name);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get a registered skill definition.
|
|
114
|
+
*
|
|
115
|
+
* @param name - The name of the skill
|
|
116
|
+
* @returns The skill definition or undefined if not found
|
|
117
|
+
*/
|
|
118
|
+
get(name) {
|
|
119
|
+
return this.skills.get(name);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Load a skill, making its tools and prompt available.
|
|
123
|
+
*
|
|
124
|
+
* This method handles dependencies, loading them first if specified.
|
|
125
|
+
* Already-loaded skills are skipped (no duplicate loading).
|
|
126
|
+
*
|
|
127
|
+
* @param name - The name of the skill to load
|
|
128
|
+
* @param args - Optional arguments to pass to the skill's prompt function
|
|
129
|
+
* @returns The load result with tools, prompt, and status
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* const result = registry.load("git");
|
|
134
|
+
* if (result.success) {
|
|
135
|
+
* // Inject result.tools into agent
|
|
136
|
+
* // Inject result.prompt into context
|
|
137
|
+
* }
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
load(name, args) {
|
|
141
|
+
// Check if already loaded
|
|
142
|
+
if (this.loadedSkills.has(name)) {
|
|
143
|
+
return {
|
|
144
|
+
success: true,
|
|
145
|
+
tools: {},
|
|
146
|
+
prompt: "",
|
|
147
|
+
error: `Skill '${name}' is already loaded`,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
// Check if skill exists
|
|
151
|
+
const skill = this.skills.get(name);
|
|
152
|
+
if (!skill) {
|
|
153
|
+
return {
|
|
154
|
+
success: false,
|
|
155
|
+
tools: {},
|
|
156
|
+
prompt: "",
|
|
157
|
+
error: `Skill '${name}' not found. Available: ${this.listAvailable()
|
|
158
|
+
.map((s) => s.name)
|
|
159
|
+
.join(", ")}`,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
// Load dependencies first
|
|
163
|
+
const loadedDependencies = [];
|
|
164
|
+
const aggregatedTools = {};
|
|
165
|
+
let aggregatedPrompt = "";
|
|
166
|
+
if (skill.dependencies && skill.dependencies.length > 0) {
|
|
167
|
+
for (const depName of skill.dependencies) {
|
|
168
|
+
if (this.loadedSkills.has(depName)) {
|
|
169
|
+
continue; // Skip already loaded dependencies
|
|
170
|
+
}
|
|
171
|
+
const depResult = this.load(depName);
|
|
172
|
+
if (!depResult.success) {
|
|
173
|
+
return {
|
|
174
|
+
success: false,
|
|
175
|
+
tools: {},
|
|
176
|
+
prompt: "",
|
|
177
|
+
error: `Failed to load dependency '${depName}': ${depResult.error}`,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
// Aggregate dependency tools and prompts
|
|
181
|
+
Object.assign(aggregatedTools, depResult.tools);
|
|
182
|
+
if (depResult.prompt) {
|
|
183
|
+
aggregatedPrompt += `${depResult.prompt}\n\n`;
|
|
184
|
+
}
|
|
185
|
+
loadedDependencies.push(depName);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// Mark as loaded
|
|
189
|
+
this.loadedSkills.add(name);
|
|
190
|
+
// Track MCP tool loading results
|
|
191
|
+
let loadedMcpTools;
|
|
192
|
+
let notFoundMcpTools;
|
|
193
|
+
// Load MCP tools if specified
|
|
194
|
+
if (skill.mcpTools && skill.mcpTools.length > 0 && this.mcpManager) {
|
|
195
|
+
const mcpLoadResult = this.mcpManager.loadTools(skill.mcpTools);
|
|
196
|
+
if (mcpLoadResult.loaded.length > 0 || mcpLoadResult.alreadyLoaded.length > 0) {
|
|
197
|
+
loadedMcpTools = [...mcpLoadResult.loaded, ...mcpLoadResult.alreadyLoaded];
|
|
198
|
+
}
|
|
199
|
+
if (mcpLoadResult.notFound.length > 0) {
|
|
200
|
+
notFoundMcpTools = mcpLoadResult.notFound;
|
|
201
|
+
// Log warning but continue - some MCP tools may be unavailable
|
|
202
|
+
console.warn(`Skill '${name}': Some MCP tools not found: ${mcpLoadResult.notFound.join(", ")}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Get the prompt
|
|
206
|
+
const prompt = typeof skill.prompt === "function" ? skill.prompt(args) : skill.prompt;
|
|
207
|
+
// Build result - MCP tools are loaded via MCPManager, not returned directly
|
|
208
|
+
const result = {
|
|
209
|
+
success: true,
|
|
210
|
+
tools: { ...aggregatedTools, ...skill.tools },
|
|
211
|
+
prompt: aggregatedPrompt + prompt,
|
|
212
|
+
loadedDependencies: loadedDependencies.length > 0 ? loadedDependencies : undefined,
|
|
213
|
+
loadedMcpTools,
|
|
214
|
+
notFoundMcpTools,
|
|
215
|
+
};
|
|
216
|
+
// Notify callback
|
|
217
|
+
if (this.onSkillLoaded) {
|
|
218
|
+
this.onSkillLoaded(name, result);
|
|
219
|
+
}
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* List skills that are available but not yet loaded.
|
|
224
|
+
*
|
|
225
|
+
* @returns Array of skill summaries (name and description)
|
|
226
|
+
*/
|
|
227
|
+
listAvailable() {
|
|
228
|
+
const available = [];
|
|
229
|
+
for (const [name, skill] of this.skills) {
|
|
230
|
+
if (!this.loadedSkills.has(name)) {
|
|
231
|
+
available.push({
|
|
232
|
+
name,
|
|
233
|
+
description: skill.description,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return available;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* List all loaded skills.
|
|
241
|
+
*
|
|
242
|
+
* @returns Array of loaded skill names
|
|
243
|
+
*/
|
|
244
|
+
listLoaded() {
|
|
245
|
+
return Array.from(this.loadedSkills);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* List all registered skills (loaded and available).
|
|
249
|
+
*
|
|
250
|
+
* @returns Array of all skill summaries
|
|
251
|
+
*/
|
|
252
|
+
listAll() {
|
|
253
|
+
const all = [];
|
|
254
|
+
for (const [name, skill] of this.skills) {
|
|
255
|
+
all.push({
|
|
256
|
+
name,
|
|
257
|
+
description: skill.description,
|
|
258
|
+
loaded: this.loadedSkills.has(name),
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
return all;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Reset the registry, marking all skills as unloaded.
|
|
265
|
+
*
|
|
266
|
+
* This does not unregister skills, only resets the loaded state.
|
|
267
|
+
*/
|
|
268
|
+
reset() {
|
|
269
|
+
this.loadedSkills.clear();
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Get the number of registered skills.
|
|
273
|
+
*/
|
|
274
|
+
get size() {
|
|
275
|
+
return this.skills.size;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Get the number of loaded skills.
|
|
279
|
+
*/
|
|
280
|
+
get loadedCount() {
|
|
281
|
+
return this.loadedSkills.size;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Creates a tool that allows agents to load skills on-demand.
|
|
286
|
+
*
|
|
287
|
+
* The tool's description dynamically lists available (not yet loaded) skills,
|
|
288
|
+
* so the agent can decide which skill to load based on the conversation.
|
|
289
|
+
*
|
|
290
|
+
* @param options - Configuration options
|
|
291
|
+
* @returns An AI SDK compatible tool for loading skills
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```typescript
|
|
295
|
+
* import { createSkillTool, SkillRegistry } from "@lleverage-ai/agent-sdk";
|
|
296
|
+
*
|
|
297
|
+
* const registry = new SkillRegistry({
|
|
298
|
+
* skills: [gitSkill, dockerSkill],
|
|
299
|
+
* });
|
|
300
|
+
*
|
|
301
|
+
* const skillTool = createSkillTool({ registry });
|
|
302
|
+
*
|
|
303
|
+
* const agent = createAgent({
|
|
304
|
+
* model,
|
|
305
|
+
* tools: { load_skill: skillTool },
|
|
306
|
+
* });
|
|
307
|
+
*
|
|
308
|
+
* // Agent can now invoke load_skill to gain new capabilities
|
|
309
|
+
* ```
|
|
310
|
+
*
|
|
311
|
+
* @category Tools
|
|
312
|
+
*/
|
|
313
|
+
export function createSkillTool(options) {
|
|
314
|
+
const { registry, descriptionPrefix } = options;
|
|
315
|
+
// Build dynamic description based on available skills
|
|
316
|
+
const buildDescription = () => {
|
|
317
|
+
const available = registry.listAvailable();
|
|
318
|
+
if (available.length === 0) {
|
|
319
|
+
return "No skills available to load.";
|
|
320
|
+
}
|
|
321
|
+
const prefix = descriptionPrefix ??
|
|
322
|
+
"Load a skill to gain additional capabilities. After loading, new tools and instructions become available.";
|
|
323
|
+
const skillList = available.map((s) => `- ${s.name}: ${s.description}`).join("\n");
|
|
324
|
+
return `${prefix}\n\nAvailable skills:\n${skillList}`;
|
|
325
|
+
};
|
|
326
|
+
return tool({
|
|
327
|
+
description: buildDescription(),
|
|
328
|
+
inputSchema: z.object({
|
|
329
|
+
skill_name: z.string().describe("Name of the skill to load"),
|
|
330
|
+
args: z.string().optional().describe("Optional arguments to pass to the skill"),
|
|
331
|
+
}),
|
|
332
|
+
execute: async ({ skill_name, args }) => {
|
|
333
|
+
const result = registry.load(skill_name, args);
|
|
334
|
+
if (!result.success) {
|
|
335
|
+
return {
|
|
336
|
+
success: false,
|
|
337
|
+
error: result.error,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
// Format the response
|
|
341
|
+
const toolNames = Object.keys(result.tools);
|
|
342
|
+
const response = {
|
|
343
|
+
success: true,
|
|
344
|
+
skill: skill_name,
|
|
345
|
+
newTools: toolNames,
|
|
346
|
+
instructions: result.prompt,
|
|
347
|
+
};
|
|
348
|
+
if (result.loadedDependencies && result.loadedDependencies.length > 0) {
|
|
349
|
+
response.dependencies = result.loadedDependencies;
|
|
350
|
+
}
|
|
351
|
+
if (toolNames.length === 0) {
|
|
352
|
+
response.message = `Loaded skill '${skill_name}' (provides instructions only, no new tools)`;
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
response.message = `Loaded skill '${skill_name}'. New tools available: ${toolNames.join(", ")}`;
|
|
356
|
+
}
|
|
357
|
+
return response;
|
|
358
|
+
},
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
// =============================================================================
|
|
362
|
+
// Factory Functions
|
|
363
|
+
// =============================================================================
|
|
364
|
+
/**
|
|
365
|
+
* Creates a new skill registry with the given skills.
|
|
366
|
+
*
|
|
367
|
+
* This is a convenience function for creating a SkillRegistry instance.
|
|
368
|
+
*
|
|
369
|
+
* @param skills - Initial skills to register
|
|
370
|
+
* @param options - Additional options
|
|
371
|
+
* @returns A new SkillRegistry instance
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* ```typescript
|
|
375
|
+
* const registry = createSkillRegistry([gitSkill, dockerSkill]);
|
|
376
|
+
* ```
|
|
377
|
+
*
|
|
378
|
+
* @category Tools
|
|
379
|
+
*/
|
|
380
|
+
export function createSkillRegistry(skills, options) {
|
|
381
|
+
return new SkillRegistry({
|
|
382
|
+
...options,
|
|
383
|
+
skills,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Creates a skill definition.
|
|
388
|
+
*
|
|
389
|
+
* This is a helper function for creating LoadableSkillDefinition objects
|
|
390
|
+
* with proper typing.
|
|
391
|
+
*
|
|
392
|
+
* @param options - Skill configuration
|
|
393
|
+
* @returns A LoadableSkillDefinition object
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* ```typescript
|
|
397
|
+
* const gitSkill = defineLoadableSkill({
|
|
398
|
+
* name: "git",
|
|
399
|
+
* description: "Git version control operations",
|
|
400
|
+
* tools: {
|
|
401
|
+
* git_status: tool({ ... }),
|
|
402
|
+
* git_commit: tool({ ... }),
|
|
403
|
+
* },
|
|
404
|
+
* prompt: "You now have access to Git tools.",
|
|
405
|
+
* });
|
|
406
|
+
* ```
|
|
407
|
+
*
|
|
408
|
+
* @category Tools
|
|
409
|
+
*/
|
|
410
|
+
export function defineLoadableSkill(options) {
|
|
411
|
+
return options;
|
|
412
|
+
}
|
|
413
|
+
//# sourceMappingURL=skills.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"skills.js","sourceRoot":"","sources":["../../src/tools/skills.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAC1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AA+HxB,gFAAgF;AAChF,iBAAiB;AACjB,gFAAgF;AAEhF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,aAAa;IACxB,4BAA4B;IACpB,MAAM,GAAG,IAAI,GAAG,EAAmC,CAAC;IAE5D,8BAA8B;IACtB,YAAY,GAAG,IAAI,GAAG,EAAU,CAAC;IAEzC,qCAAqC;IAC7B,aAAa,CAAwD;IAE7E,wCAAwC;IAChC,UAAU,CAAc;IAEhC;;;;OAIG;IACH,YAAY,UAAgC,EAAE;QAC5C,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;QAC3C,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QAErC,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YACnB,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;gBACnC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;IACH,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,QAAQ,CAAC,KAA8B;QACrC,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YAChC,MAAM,IAAI,KAAK,CAAC,UAAU,KAAK,CAAC,IAAI,yBAAyB,CAAC,CA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AAC;YAED,sBAAsB;YACtB,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC5C,MAAM,QAAQ,GAA4B;gBACxC,OAAO,EAAE,IAAI;gBACb,KAAK,EAAE,UAAU;gBACjB,QAAQ,EAAE,SAAS;gBACnB,YAAY,EAAE,MAAM,CAAC,MAAM;aAC5B,CAAC;YAEF,IAAI,MAAM,CAAC,kBAAkB,IAAI,MAAM,CAAC,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACtE,QAAQ,CAAC,YAAY,GAAG,MAAM,CAAC,kBAAkB,CAAC;YACpD,CAAC;YAED,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC3B,QAAQ,CAAC,OAAO,GAAG,iBAAiB,UAAU,8CAA8C,CAAC;YAC/F,CAAC;iBAAM,CAAC;gBACN,QAAQ,CAAC,OAAO,GAAG,iBAAiB,UAAU,2BAA2B,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YAClG,CAAC;YAED,OAAO,QAAQ,CAAC;QAClB,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED,gFAAgF;AAChF,oBAAoB;AACpB,gFAAgF;AAEhF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,mBAAmB,CACjC,MAAiC,EACjC,OAA8C;IAE9C,OAAO,IAAI,aAAa,CAAC;QACvB,GAAG,OAAO;QACV,MAAM;KACP,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAgC;IAClE,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Task tool for delegating work to specialized subagents.
|
|
3
|
+
*
|
|
4
|
+
* Provides a single `task` tool for spawning subagents, similar to Claude Code's
|
|
5
|
+
* Task tool. Supports both foreground and background execution, as well as
|
|
6
|
+
* streaming subagents that can write to the parent's data stream.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
import type { LanguageModel, Tool } from "ai";
|
|
11
|
+
import type { BackgroundTask, BaseTaskStore } from "../task-store/index.js";
|
|
12
|
+
import type { Agent, StreamingContext, SubagentDefinition } from "../types.js";
|
|
13
|
+
/**
|
|
14
|
+
* Status of a background task.
|
|
15
|
+
*
|
|
16
|
+
* @category Subagents
|
|
17
|
+
*/
|
|
18
|
+
export type TaskStatus = "pending" | "running" | "completed" | "failed";
|
|
19
|
+
/**
|
|
20
|
+
* Options for creating the task tool.
|
|
21
|
+
*
|
|
22
|
+
* @category Subagents
|
|
23
|
+
*/
|
|
24
|
+
export interface TaskToolOptions {
|
|
25
|
+
/** Available subagent definitions */
|
|
26
|
+
subagents: SubagentDefinition[];
|
|
27
|
+
/** Default model for subagents that don't specify one */
|
|
28
|
+
defaultModel: LanguageModel;
|
|
29
|
+
/** Parent agent for creating subagents */
|
|
30
|
+
parentAgent: Agent;
|
|
31
|
+
/** Custom tool description */
|
|
32
|
+
description?: string;
|
|
33
|
+
/** Default max turns for subagents */
|
|
34
|
+
defaultMaxTurns?: number;
|
|
35
|
+
/** Include a general-purpose subagent automatically */
|
|
36
|
+
includeGeneralPurpose?: boolean;
|
|
37
|
+
/** Model to use for general-purpose subagent */
|
|
38
|
+
generalPurposeModel?: LanguageModel;
|
|
39
|
+
/** System prompt for general-purpose subagent */
|
|
40
|
+
generalPurposePrompt?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Streaming context from the parent agent.
|
|
43
|
+
*
|
|
44
|
+
* When provided, streaming subagents (those with `streaming: true`) can
|
|
45
|
+
* write custom data directly to the parent's data stream. This enables
|
|
46
|
+
* progressive rendering, real-time updates, and structured data streaming.
|
|
47
|
+
*
|
|
48
|
+
* Typically set when the parent agent is using `streamDataResponse()`.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* // In agent.ts streamDataResponse
|
|
53
|
+
* const streamingContext: StreamingContext = { writer };
|
|
54
|
+
* const task = createTaskTool({
|
|
55
|
+
* subagents,
|
|
56
|
+
* defaultModel,
|
|
57
|
+
* parentAgent,
|
|
58
|
+
* streamingContext, // Pass to enable streaming subagents
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
streamingContext?: StreamingContext;
|
|
63
|
+
/**
|
|
64
|
+
* Task store for persisting background task state.
|
|
65
|
+
*
|
|
66
|
+
* When provided, background tasks will be persisted and can be recovered
|
|
67
|
+
* across process restarts. Without a task store, tasks are kept in memory
|
|
68
|
+
* only and lost when the process exits.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* import { FileTaskStore } from "@lleverage-ai/agent-sdk/task-store";
|
|
73
|
+
*
|
|
74
|
+
* const taskStore = new FileTaskStore({
|
|
75
|
+
* directory: "./task-data",
|
|
76
|
+
* expirationMs: 86400000, // 24 hours
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* const task = createTaskTool({
|
|
80
|
+
* subagents,
|
|
81
|
+
* defaultModel,
|
|
82
|
+
* parentAgent,
|
|
83
|
+
* taskStore, // Tasks now persist across restarts
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
taskStore?: BaseTaskStore;
|
|
88
|
+
/**
|
|
89
|
+
* Parent span context for distributed tracing.
|
|
90
|
+
*
|
|
91
|
+
* When provided, subagents will create child spans linked to this parent
|
|
92
|
+
* span, enabling cross-agent trace correlation. This allows full request
|
|
93
|
+
* tracing across parent and child agents in distributed tracing systems.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* import { createTracer } from "@lleverage-ai/agent-sdk";
|
|
98
|
+
*
|
|
99
|
+
* const tracer = createTracer({ name: "parent-agent" });
|
|
100
|
+
* const span = tracer.startSpan("handle-request");
|
|
101
|
+
*
|
|
102
|
+
* const task = createTaskTool({
|
|
103
|
+
* subagents,
|
|
104
|
+
* defaultModel,
|
|
105
|
+
* parentAgent,
|
|
106
|
+
* parentSpanContext: {
|
|
107
|
+
* traceId: span.traceId,
|
|
108
|
+
* spanId: span.spanId,
|
|
109
|
+
* },
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
parentSpanContext?: import("../observability/tracing.js").SpanContext;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Get a background task by ID.
|
|
117
|
+
*
|
|
118
|
+
* @param taskId - The task ID
|
|
119
|
+
* @returns The tracked task or undefined
|
|
120
|
+
*
|
|
121
|
+
* @category Subagents
|
|
122
|
+
*/
|
|
123
|
+
export declare function getBackgroundTask(taskId: string): Promise<BackgroundTask | undefined>;
|
|
124
|
+
/**
|
|
125
|
+
* List all background tasks.
|
|
126
|
+
*
|
|
127
|
+
* @param filter - Optional filter by status
|
|
128
|
+
* @returns Array of all tracked tasks
|
|
129
|
+
*
|
|
130
|
+
* @category Subagents
|
|
131
|
+
*/
|
|
132
|
+
export declare function listBackgroundTasks(filter?: {
|
|
133
|
+
status?: TaskStatus | TaskStatus[];
|
|
134
|
+
}): Promise<BackgroundTask[]>;
|
|
135
|
+
/**
|
|
136
|
+
* Clear completed/failed background tasks.
|
|
137
|
+
*
|
|
138
|
+
* Removes tasks older than the expiration time (if using task store)
|
|
139
|
+
* or all completed/failed tasks (if using in-memory storage).
|
|
140
|
+
*
|
|
141
|
+
* @returns Number of tasks cleared
|
|
142
|
+
*
|
|
143
|
+
* @category Subagents
|
|
144
|
+
*/
|
|
145
|
+
export declare function clearCompletedTasks(): Promise<number>;
|
|
146
|
+
/**
|
|
147
|
+
* Recover running tasks on agent restart.
|
|
148
|
+
*
|
|
149
|
+
* When using a task store, this function loads all "running" tasks
|
|
150
|
+
* and marks them as "failed" since they were interrupted by the restart.
|
|
151
|
+
*
|
|
152
|
+
* Call this function when initializing your agent to handle crashed tasks.
|
|
153
|
+
*
|
|
154
|
+
* @param store - Optional task store to use. If not provided, uses the global task store.
|
|
155
|
+
* @returns Number of tasks recovered
|
|
156
|
+
*
|
|
157
|
+
* @category Subagents
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* // On agent startup
|
|
161
|
+
* const recovered = await recoverRunningTasks();
|
|
162
|
+
* console.log(`Recovered ${recovered} interrupted tasks`);
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
export declare function recoverRunningTasks(store?: BaseTaskStore): Promise<number>;
|
|
166
|
+
/**
|
|
167
|
+
* Recover failed tasks for retry.
|
|
168
|
+
*
|
|
169
|
+
* Loads all "failed" tasks from the store and returns them for inspection
|
|
170
|
+
* or automatic retry. Applications can decide which tasks to retry based
|
|
171
|
+
* on error type, retry count, or other criteria.
|
|
172
|
+
*
|
|
173
|
+
* To retry a task, update its status back to "pending" and process it
|
|
174
|
+
* through your task execution logic.
|
|
175
|
+
*
|
|
176
|
+
* @param store - The task store to query
|
|
177
|
+
* @param options - Optional filter options
|
|
178
|
+
* @returns Array of failed tasks
|
|
179
|
+
*
|
|
180
|
+
* @category Subagents
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* // Load failed tasks and retry those with transient errors
|
|
184
|
+
* const failedTasks = await recoverFailedTasks(taskStore);
|
|
185
|
+
*
|
|
186
|
+
* for (const task of failedTasks) {
|
|
187
|
+
* // Check if error is retryable
|
|
188
|
+
* if (task.error?.includes("timeout") || task.error?.includes("network")) {
|
|
189
|
+
* // Mark for retry
|
|
190
|
+
* const retryTask = updateBackgroundTask(task, {
|
|
191
|
+
* status: "pending",
|
|
192
|
+
* error: undefined,
|
|
193
|
+
* });
|
|
194
|
+
* await taskStore.save(retryTask);
|
|
195
|
+
* }
|
|
196
|
+
* }
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
export declare function recoverFailedTasks(store: BaseTaskStore, options?: {
|
|
200
|
+
/** Only return tasks with errors matching this pattern */
|
|
201
|
+
errorPattern?: RegExp;
|
|
202
|
+
/** Only return tasks newer than this timestamp */
|
|
203
|
+
minCreatedAt?: Date;
|
|
204
|
+
/** Only return tasks older than this timestamp */
|
|
205
|
+
maxCreatedAt?: Date;
|
|
206
|
+
}): Promise<BackgroundTask[]>;
|
|
207
|
+
/**
|
|
208
|
+
* Clean up stale tasks from the task store.
|
|
209
|
+
*
|
|
210
|
+
* Removes tasks that have been in a terminal state (completed or failed)
|
|
211
|
+
* for longer than the specified age. This prevents unbounded storage growth
|
|
212
|
+
* and maintains system health.
|
|
213
|
+
*
|
|
214
|
+
* @param store - The task store to clean
|
|
215
|
+
* @param maxAge - Maximum age in milliseconds for terminal tasks
|
|
216
|
+
* @returns Number of tasks cleaned up
|
|
217
|
+
*
|
|
218
|
+
* @category Subagents
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* // Clean up tasks older than 7 days
|
|
222
|
+
* const sevenDays = 7 * 24 * 60 * 60 * 1000;
|
|
223
|
+
* const cleaned = await cleanupStaleTasks(taskStore, sevenDays);
|
|
224
|
+
* console.log(`Cleaned up ${cleaned} stale tasks`);
|
|
225
|
+
*
|
|
226
|
+
* // Or use a shorter retention for testing
|
|
227
|
+
* const oneHour = 60 * 60 * 1000;
|
|
228
|
+
* await cleanupStaleTasks(taskStore, oneHour);
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
export declare function cleanupStaleTasks(store: BaseTaskStore, maxAge: number): Promise<number>;
|
|
232
|
+
/**
|
|
233
|
+
* Creates the task tool for delegating work to specialized subagents.
|
|
234
|
+
*
|
|
235
|
+
* This tool delegates tasks to subagents with isolated context. It supports
|
|
236
|
+
* both foreground (blocking) and background execution.
|
|
237
|
+
*
|
|
238
|
+
* @param options - Configuration options
|
|
239
|
+
* @returns An AI SDK tool for task delegation
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```typescript
|
|
243
|
+
* import { createTaskTool } from "@lleverage-ai/agent-sdk";
|
|
244
|
+
*
|
|
245
|
+
* const task = createTaskTool({
|
|
246
|
+
* subagents: [
|
|
247
|
+
* {
|
|
248
|
+
* type: "researcher",
|
|
249
|
+
* description: "Searches for information",
|
|
250
|
+
* create: () => createSubagent(parentAgent, { ... }),
|
|
251
|
+
* },
|
|
252
|
+
* {
|
|
253
|
+
* type: "coder",
|
|
254
|
+
* description: "Writes and modifies code",
|
|
255
|
+
* create: () => createSubagent(parentAgent, { ... }),
|
|
256
|
+
* },
|
|
257
|
+
* ],
|
|
258
|
+
* defaultModel: anthropic("claude-sonnet-4-20250514"),
|
|
259
|
+
* parentAgent,
|
|
260
|
+
* includeGeneralPurpose: true,
|
|
261
|
+
* });
|
|
262
|
+
*
|
|
263
|
+
* const agent = createAgent({
|
|
264
|
+
* model,
|
|
265
|
+
* tools: { task },
|
|
266
|
+
* });
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* @category Subagents
|
|
270
|
+
*/
|
|
271
|
+
export declare function createTaskTool(options: TaskToolOptions): Tool;
|
|
272
|
+
//# sourceMappingURL=task.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task.d.ts","sourceRoot":"","sources":["../../src/tools/task.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAI9C,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE5E,OAAO,KAAK,EACV,KAAK,EACL,gBAAgB,EAGhB,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAMrB;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG,SAAS,GAAG,SAAS,GAAG,WAAW,GAAG,QAAQ,CAAC;AAExE;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,qCAAqC;IACrC,SAA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