@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.
Files changed (327) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2321 -0
  3. package/dist/agent.d.ts +52 -0
  4. package/dist/agent.d.ts.map +1 -0
  5. package/dist/agent.js +2122 -0
  6. package/dist/agent.js.map +1 -0
  7. package/dist/backend.d.ts +378 -0
  8. package/dist/backend.d.ts.map +1 -0
  9. package/dist/backend.js +71 -0
  10. package/dist/backend.js.map +1 -0
  11. package/dist/backends/composite.d.ts +258 -0
  12. package/dist/backends/composite.d.ts.map +1 -0
  13. package/dist/backends/composite.js +437 -0
  14. package/dist/backends/composite.js.map +1 -0
  15. package/dist/backends/filesystem.d.ts +268 -0
  16. package/dist/backends/filesystem.d.ts.map +1 -0
  17. package/dist/backends/filesystem.js +623 -0
  18. package/dist/backends/filesystem.js.map +1 -0
  19. package/dist/backends/index.d.ts +14 -0
  20. package/dist/backends/index.d.ts.map +1 -0
  21. package/dist/backends/index.js +14 -0
  22. package/dist/backends/index.js.map +1 -0
  23. package/dist/backends/persistent.d.ts +312 -0
  24. package/dist/backends/persistent.d.ts.map +1 -0
  25. package/dist/backends/persistent.js +519 -0
  26. package/dist/backends/persistent.js.map +1 -0
  27. package/dist/backends/sandbox.d.ts +315 -0
  28. package/dist/backends/sandbox.d.ts.map +1 -0
  29. package/dist/backends/sandbox.js +490 -0
  30. package/dist/backends/sandbox.js.map +1 -0
  31. package/dist/backends/state.d.ts +225 -0
  32. package/dist/backends/state.d.ts.map +1 -0
  33. package/dist/backends/state.js +396 -0
  34. package/dist/backends/state.js.map +1 -0
  35. package/dist/checkpointer/file-saver.d.ts +182 -0
  36. package/dist/checkpointer/file-saver.d.ts.map +1 -0
  37. package/dist/checkpointer/file-saver.js +298 -0
  38. package/dist/checkpointer/file-saver.js.map +1 -0
  39. package/dist/checkpointer/index.d.ts +40 -0
  40. package/dist/checkpointer/index.d.ts.map +1 -0
  41. package/dist/checkpointer/index.js +40 -0
  42. package/dist/checkpointer/index.js.map +1 -0
  43. package/dist/checkpointer/kv-saver.d.ts +142 -0
  44. package/dist/checkpointer/kv-saver.d.ts.map +1 -0
  45. package/dist/checkpointer/kv-saver.js +176 -0
  46. package/dist/checkpointer/kv-saver.js.map +1 -0
  47. package/dist/checkpointer/memory-saver.d.ts +158 -0
  48. package/dist/checkpointer/memory-saver.d.ts.map +1 -0
  49. package/dist/checkpointer/memory-saver.js +222 -0
  50. package/dist/checkpointer/memory-saver.js.map +1 -0
  51. package/dist/checkpointer/types.d.ts +353 -0
  52. package/dist/checkpointer/types.d.ts.map +1 -0
  53. package/dist/checkpointer/types.js +159 -0
  54. package/dist/checkpointer/types.js.map +1 -0
  55. package/dist/context-manager.d.ts +627 -0
  56. package/dist/context-manager.d.ts.map +1 -0
  57. package/dist/context-manager.js +1039 -0
  58. package/dist/context-manager.js.map +1 -0
  59. package/dist/context.d.ts +57 -0
  60. package/dist/context.d.ts.map +1 -0
  61. package/dist/context.js +76 -0
  62. package/dist/context.js.map +1 -0
  63. package/dist/errors/index.d.ts +611 -0
  64. package/dist/errors/index.d.ts.map +1 -0
  65. package/dist/errors/index.js +1023 -0
  66. package/dist/errors/index.js.map +1 -0
  67. package/dist/generation-helpers.d.ts +126 -0
  68. package/dist/generation-helpers.d.ts.map +1 -0
  69. package/dist/generation-helpers.js +181 -0
  70. package/dist/generation-helpers.js.map +1 -0
  71. package/dist/hooks/audit.d.ts +210 -0
  72. package/dist/hooks/audit.d.ts.map +1 -0
  73. package/dist/hooks/audit.js +305 -0
  74. package/dist/hooks/audit.js.map +1 -0
  75. package/dist/hooks/cache.d.ts +180 -0
  76. package/dist/hooks/cache.d.ts.map +1 -0
  77. package/dist/hooks/cache.js +273 -0
  78. package/dist/hooks/cache.js.map +1 -0
  79. package/dist/hooks/guardrails.d.ts +145 -0
  80. package/dist/hooks/guardrails.d.ts.map +1 -0
  81. package/dist/hooks/guardrails.js +326 -0
  82. package/dist/hooks/guardrails.js.map +1 -0
  83. package/dist/hooks/index.d.ts +18 -0
  84. package/dist/hooks/index.d.ts.map +1 -0
  85. package/dist/hooks/index.js +32 -0
  86. package/dist/hooks/index.js.map +1 -0
  87. package/dist/hooks/logging.d.ts +193 -0
  88. package/dist/hooks/logging.d.ts.map +1 -0
  89. package/dist/hooks/logging.js +345 -0
  90. package/dist/hooks/logging.js.map +1 -0
  91. package/dist/hooks/parallel-guardrails.d.ts +268 -0
  92. package/dist/hooks/parallel-guardrails.d.ts.map +1 -0
  93. package/dist/hooks/parallel-guardrails.js +416 -0
  94. package/dist/hooks/parallel-guardrails.js.map +1 -0
  95. package/dist/hooks/rate-limit.d.ts +305 -0
  96. package/dist/hooks/rate-limit.d.ts.map +1 -0
  97. package/dist/hooks/rate-limit.js +372 -0
  98. package/dist/hooks/rate-limit.js.map +1 -0
  99. package/dist/hooks/retry.d.ts +144 -0
  100. package/dist/hooks/retry.d.ts.map +1 -0
  101. package/dist/hooks/retry.js +210 -0
  102. package/dist/hooks/retry.js.map +1 -0
  103. package/dist/hooks/secrets.d.ts +174 -0
  104. package/dist/hooks/secrets.d.ts.map +1 -0
  105. package/dist/hooks/secrets.js +306 -0
  106. package/dist/hooks/secrets.js.map +1 -0
  107. package/dist/hooks.d.ts +229 -0
  108. package/dist/hooks.d.ts.map +1 -0
  109. package/dist/hooks.js +352 -0
  110. package/dist/hooks.js.map +1 -0
  111. package/dist/index.d.ts +97 -0
  112. package/dist/index.d.ts.map +1 -0
  113. package/dist/index.js +182 -0
  114. package/dist/index.js.map +1 -0
  115. package/dist/mcp/env.d.ts +25 -0
  116. package/dist/mcp/env.d.ts.map +1 -0
  117. package/dist/mcp/env.js +18 -0
  118. package/dist/mcp/env.js.map +1 -0
  119. package/dist/mcp/index.d.ts +16 -0
  120. package/dist/mcp/index.d.ts.map +1 -0
  121. package/dist/mcp/index.js +17 -0
  122. package/dist/mcp/index.js.map +1 -0
  123. package/dist/mcp/manager.d.ts +184 -0
  124. package/dist/mcp/manager.d.ts.map +1 -0
  125. package/dist/mcp/manager.js +446 -0
  126. package/dist/mcp/manager.js.map +1 -0
  127. package/dist/mcp/types.d.ts +58 -0
  128. package/dist/mcp/types.d.ts.map +1 -0
  129. package/dist/mcp/types.js +7 -0
  130. package/dist/mcp/types.js.map +1 -0
  131. package/dist/mcp/validation.d.ts +119 -0
  132. package/dist/mcp/validation.d.ts.map +1 -0
  133. package/dist/mcp/validation.js +407 -0
  134. package/dist/mcp/validation.js.map +1 -0
  135. package/dist/mcp/virtual-server.d.ts +78 -0
  136. package/dist/mcp/virtual-server.d.ts.map +1 -0
  137. package/dist/mcp/virtual-server.js +137 -0
  138. package/dist/mcp/virtual-server.js.map +1 -0
  139. package/dist/memory/filesystem-store.d.ts +217 -0
  140. package/dist/memory/filesystem-store.d.ts.map +1 -0
  141. package/dist/memory/filesystem-store.js +343 -0
  142. package/dist/memory/filesystem-store.js.map +1 -0
  143. package/dist/memory/index.d.ts +46 -0
  144. package/dist/memory/index.d.ts.map +1 -0
  145. package/dist/memory/index.js +46 -0
  146. package/dist/memory/index.js.map +1 -0
  147. package/dist/memory/loader.d.ts +396 -0
  148. package/dist/memory/loader.d.ts.map +1 -0
  149. package/dist/memory/loader.js +419 -0
  150. package/dist/memory/loader.js.map +1 -0
  151. package/dist/memory/permissions.d.ts +282 -0
  152. package/dist/memory/permissions.d.ts.map +1 -0
  153. package/dist/memory/permissions.js +297 -0
  154. package/dist/memory/permissions.js.map +1 -0
  155. package/dist/memory/rules.d.ts +249 -0
  156. package/dist/memory/rules.d.ts.map +1 -0
  157. package/dist/memory/rules.js +362 -0
  158. package/dist/memory/rules.js.map +1 -0
  159. package/dist/memory/store.d.ts +286 -0
  160. package/dist/memory/store.d.ts.map +1 -0
  161. package/dist/memory/store.js +263 -0
  162. package/dist/memory/store.js.map +1 -0
  163. package/dist/middleware/apply.d.ts +73 -0
  164. package/dist/middleware/apply.d.ts.map +1 -0
  165. package/dist/middleware/apply.js +219 -0
  166. package/dist/middleware/apply.js.map +1 -0
  167. package/dist/middleware/context.d.ts +33 -0
  168. package/dist/middleware/context.d.ts.map +1 -0
  169. package/dist/middleware/context.js +176 -0
  170. package/dist/middleware/context.js.map +1 -0
  171. package/dist/middleware/index.d.ts +31 -0
  172. package/dist/middleware/index.d.ts.map +1 -0
  173. package/dist/middleware/index.js +32 -0
  174. package/dist/middleware/index.js.map +1 -0
  175. package/dist/middleware/logging.d.ts +137 -0
  176. package/dist/middleware/logging.d.ts.map +1 -0
  177. package/dist/middleware/logging.js +374 -0
  178. package/dist/middleware/logging.js.map +1 -0
  179. package/dist/middleware/types.d.ts +183 -0
  180. package/dist/middleware/types.d.ts.map +1 -0
  181. package/dist/middleware/types.js +11 -0
  182. package/dist/middleware/types.js.map +1 -0
  183. package/dist/observability/events.d.ts +183 -0
  184. package/dist/observability/events.d.ts.map +1 -0
  185. package/dist/observability/events.js +305 -0
  186. package/dist/observability/events.js.map +1 -0
  187. package/dist/observability/index.d.ts +55 -0
  188. package/dist/observability/index.d.ts.map +1 -0
  189. package/dist/observability/index.js +87 -0
  190. package/dist/observability/index.js.map +1 -0
  191. package/dist/observability/logger.d.ts +318 -0
  192. package/dist/observability/logger.d.ts.map +1 -0
  193. package/dist/observability/logger.js +436 -0
  194. package/dist/observability/logger.js.map +1 -0
  195. package/dist/observability/metrics.d.ts +341 -0
  196. package/dist/observability/metrics.d.ts.map +1 -0
  197. package/dist/observability/metrics.js +490 -0
  198. package/dist/observability/metrics.js.map +1 -0
  199. package/dist/observability/preset.d.ts +161 -0
  200. package/dist/observability/preset.d.ts.map +1 -0
  201. package/dist/observability/preset.js +133 -0
  202. package/dist/observability/preset.js.map +1 -0
  203. package/dist/observability/streaming.d.ts +113 -0
  204. package/dist/observability/streaming.d.ts.map +1 -0
  205. package/dist/observability/streaming.js +114 -0
  206. package/dist/observability/streaming.js.map +1 -0
  207. package/dist/observability/tracing.d.ts +378 -0
  208. package/dist/observability/tracing.d.ts.map +1 -0
  209. package/dist/observability/tracing.js +539 -0
  210. package/dist/observability/tracing.js.map +1 -0
  211. package/dist/plugins.d.ts +55 -0
  212. package/dist/plugins.d.ts.map +1 -0
  213. package/dist/plugins.js +63 -0
  214. package/dist/plugins.js.map +1 -0
  215. package/dist/presets/index.d.ts +7 -0
  216. package/dist/presets/index.d.ts.map +1 -0
  217. package/dist/presets/index.js +7 -0
  218. package/dist/presets/index.js.map +1 -0
  219. package/dist/presets/production.d.ts +262 -0
  220. package/dist/presets/production.d.ts.map +1 -0
  221. package/dist/presets/production.js +295 -0
  222. package/dist/presets/production.js.map +1 -0
  223. package/dist/security/index.d.ts +179 -0
  224. package/dist/security/index.d.ts.map +1 -0
  225. package/dist/security/index.js +323 -0
  226. package/dist/security/index.js.map +1 -0
  227. package/dist/subagents/advanced.d.ts +413 -0
  228. package/dist/subagents/advanced.d.ts.map +1 -0
  229. package/dist/subagents/advanced.js +396 -0
  230. package/dist/subagents/advanced.js.map +1 -0
  231. package/dist/subagents/index.d.ts +14 -0
  232. package/dist/subagents/index.d.ts.map +1 -0
  233. package/dist/subagents/index.js +15 -0
  234. package/dist/subagents/index.js.map +1 -0
  235. package/dist/subagents.d.ts +73 -0
  236. package/dist/subagents.d.ts.map +1 -0
  237. package/dist/subagents.js +213 -0
  238. package/dist/subagents.js.map +1 -0
  239. package/dist/task-store/file-store.d.ts +76 -0
  240. package/dist/task-store/file-store.d.ts.map +1 -0
  241. package/dist/task-store/file-store.js +190 -0
  242. package/dist/task-store/file-store.js.map +1 -0
  243. package/dist/task-store/index.d.ts +11 -0
  244. package/dist/task-store/index.d.ts.map +1 -0
  245. package/dist/task-store/index.js +10 -0
  246. package/dist/task-store/index.js.map +1 -0
  247. package/dist/task-store/kv-store.d.ts +140 -0
  248. package/dist/task-store/kv-store.d.ts.map +1 -0
  249. package/dist/task-store/kv-store.js +169 -0
  250. package/dist/task-store/kv-store.js.map +1 -0
  251. package/dist/task-store/memory-store.d.ts +66 -0
  252. package/dist/task-store/memory-store.d.ts.map +1 -0
  253. package/dist/task-store/memory-store.js +125 -0
  254. package/dist/task-store/memory-store.js.map +1 -0
  255. package/dist/task-store/types.d.ts +235 -0
  256. package/dist/task-store/types.d.ts.map +1 -0
  257. package/dist/task-store/types.js +110 -0
  258. package/dist/task-store/types.js.map +1 -0
  259. package/dist/testing/assertions.d.ts +401 -0
  260. package/dist/testing/assertions.d.ts.map +1 -0
  261. package/dist/testing/assertions.js +630 -0
  262. package/dist/testing/assertions.js.map +1 -0
  263. package/dist/testing/index.d.ts +343 -0
  264. package/dist/testing/index.d.ts.map +1 -0
  265. package/dist/testing/index.js +360 -0
  266. package/dist/testing/index.js.map +1 -0
  267. package/dist/testing/mock-agent.d.ts +214 -0
  268. package/dist/testing/mock-agent.d.ts.map +1 -0
  269. package/dist/testing/mock-agent.js +448 -0
  270. package/dist/testing/mock-agent.js.map +1 -0
  271. package/dist/testing/recorder.d.ts +288 -0
  272. package/dist/testing/recorder.d.ts.map +1 -0
  273. package/dist/testing/recorder.js +499 -0
  274. package/dist/testing/recorder.js.map +1 -0
  275. package/dist/tools/execute.d.ts +104 -0
  276. package/dist/tools/execute.d.ts.map +1 -0
  277. package/dist/tools/execute.js +191 -0
  278. package/dist/tools/execute.js.map +1 -0
  279. package/dist/tools/factory.d.ts +260 -0
  280. package/dist/tools/factory.d.ts.map +1 -0
  281. package/dist/tools/factory.js +241 -0
  282. package/dist/tools/factory.js.map +1 -0
  283. package/dist/tools/filesystem.d.ts +215 -0
  284. package/dist/tools/filesystem.d.ts.map +1 -0
  285. package/dist/tools/filesystem.js +311 -0
  286. package/dist/tools/filesystem.js.map +1 -0
  287. package/dist/tools/index.d.ts +33 -0
  288. package/dist/tools/index.d.ts.map +1 -0
  289. package/dist/tools/index.js +33 -0
  290. package/dist/tools/index.js.map +1 -0
  291. package/dist/tools/search.d.ts +59 -0
  292. package/dist/tools/search.d.ts.map +1 -0
  293. package/dist/tools/search.js +94 -0
  294. package/dist/tools/search.js.map +1 -0
  295. package/dist/tools/skills.d.ts +354 -0
  296. package/dist/tools/skills.d.ts.map +1 -0
  297. package/dist/tools/skills.js +413 -0
  298. package/dist/tools/skills.js.map +1 -0
  299. package/dist/tools/task.d.ts +272 -0
  300. package/dist/tools/task.d.ts.map +1 -0
  301. package/dist/tools/task.js +521 -0
  302. package/dist/tools/task.js.map +1 -0
  303. package/dist/tools/todos.d.ts +131 -0
  304. package/dist/tools/todos.d.ts.map +1 -0
  305. package/dist/tools/todos.js +120 -0
  306. package/dist/tools/todos.js.map +1 -0
  307. package/dist/tools/tool-registry.d.ts +424 -0
  308. package/dist/tools/tool-registry.d.ts.map +1 -0
  309. package/dist/tools/tool-registry.js +607 -0
  310. package/dist/tools/tool-registry.js.map +1 -0
  311. package/dist/tools/user-interaction.d.ts +116 -0
  312. package/dist/tools/user-interaction.d.ts.map +1 -0
  313. package/dist/tools/user-interaction.js +147 -0
  314. package/dist/tools/user-interaction.js.map +1 -0
  315. package/dist/tools/utils.d.ts +124 -0
  316. package/dist/tools/utils.d.ts.map +1 -0
  317. package/dist/tools/utils.js +189 -0
  318. package/dist/tools/utils.js.map +1 -0
  319. package/dist/tools.d.ts +74 -0
  320. package/dist/tools.d.ts.map +1 -0
  321. package/dist/tools.js +73 -0
  322. package/dist/tools.js.map +1 -0
  323. package/dist/types.d.ts +2421 -0
  324. package/dist/types.d.ts.map +1 -0
  325. package/dist/types.js +55 -0
  326. package/dist/types.js.map +1 -0
  327. package/package.json +81 -0
@@ -0,0 +1,213 @@
1
+ /**
2
+ * Subagent creation and task delegation.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ import { createAgent } from "./agent.js";
7
+ /**
8
+ * Creates a subagent that inherits configuration from a parent agent.
9
+ *
10
+ * Subagents are specialized agents that can be spawned by a parent agent
11
+ * to handle specific tasks. They can inherit the parent's model or use
12
+ * their own, and can have their own tools, plugins, and configuration.
13
+ *
14
+ * **Hook Inheritance**:
15
+ * By default, subagents inherit all hooks from their parent agent. You can control
16
+ * this behavior with the `inheritHooks` option:
17
+ * - `true` (default): Inherit all parent hooks
18
+ * - `false`: No inheritance, use only subagent's own hooks
19
+ * - `string[]`: Inherit only specific hook events
20
+ *
21
+ * **Tool Filtering Security**:
22
+ * When `allowedTools` is specified, subagent gets access to only those tools.
23
+ * A warning is logged if dangerous tools (bash, write, edit, rm, etc.) are included
24
+ * without explicit opt-in via `inheritHooks: false` or specific hook controls.
25
+ *
26
+ * @param parentAgent - The parent agent to inherit configuration from
27
+ * @param options - Configuration options for the subagent
28
+ * @returns A new agent instance configured as a subagent
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * import { createAgent, createSubagent } from "@lleverage-ai/agent-sdk";
33
+ * import { anthropic } from "@ai-sdk/anthropic";
34
+ * import { tool } from "ai";
35
+ * import { z } from "zod";
36
+ *
37
+ * const mainAgent = createAgent({
38
+ * model: anthropic("claude-sonnet-4-20250514"),
39
+ * systemPrompt: "You are a helpful assistant.",
40
+ * hooks: {
41
+ * PreToolUse: [{
42
+ * callback: async (input) => {
43
+ * console.log("Parent hook:", input.tool_name);
44
+ * return {};
45
+ * },
46
+ * }],
47
+ * },
48
+ * });
49
+ *
50
+ * // Create a specialized subagent for code review
51
+ * const reviewerAgent = createSubagent(mainAgent, {
52
+ * name: "code-reviewer",
53
+ * description: "Reviews code for quality and best practices",
54
+ * systemPrompt: "You are an expert code reviewer.",
55
+ * inheritHooks: true, // Inherits parent's PreToolUse hook
56
+ * tools: {
57
+ * analyze: tool({
58
+ * description: "Analyze code",
59
+ * inputSchema: z.object({ code: z.string() }),
60
+ * execute: async ({ code }) => analyzeCode(code),
61
+ * }),
62
+ * },
63
+ * });
64
+ *
65
+ * const result = await reviewerAgent.generate({
66
+ * prompt: "Review this function: function add(a, b) { return a + b; }",
67
+ * });
68
+ * ```
69
+ *
70
+ * @category Subagents
71
+ */
72
+ export function createSubagent(parentAgent, options) {
73
+ // Determine hook inheritance
74
+ const inheritHooks = options.inheritHooks ?? true; // Default to inheriting
75
+ let mergedHooks = options.hooks;
76
+ if (inheritHooks && parentAgent.options.hooks) {
77
+ // Inherit hooks from parent
78
+ if (inheritHooks === true) {
79
+ // Inherit all hooks
80
+ mergedHooks = mergeHooks(parentAgent.options.hooks, options.hooks);
81
+ }
82
+ else if (Array.isArray(inheritHooks)) {
83
+ // Inherit only specific hook events
84
+ const filteredParentHooks = filterHookEvents(parentAgent.options.hooks, inheritHooks);
85
+ mergedHooks = mergeHooks(filteredParentHooks, options.hooks);
86
+ }
87
+ }
88
+ // Check for dangerous tools without explicit security controls
89
+ if (options.allowedTools) {
90
+ checkDangerousToolAccess(options.allowedTools, mergedHooks, options.name, inheritHooks);
91
+ }
92
+ return createAgent({
93
+ model: options.model ?? parentAgent.options.model,
94
+ systemPrompt: options.systemPrompt,
95
+ maxSteps: options.maxSteps,
96
+ plugins: options.plugins,
97
+ tools: options.tools,
98
+ skills: options.skills,
99
+ hooks: mergedHooks,
100
+ allowedTools: options.allowedTools,
101
+ disabledCoreTools: options.disabledCoreTools,
102
+ permissionMode: options.permissionMode,
103
+ canUseTool: options.canUseTool,
104
+ disallowedTools: options.disallowedTools,
105
+ });
106
+ }
107
+ /**
108
+ * Dangerous tools that should trigger warnings if accessible without explicit security.
109
+ * @internal
110
+ */
111
+ const DANGEROUS_TOOLS = new Set([
112
+ "bash",
113
+ "write",
114
+ "edit",
115
+ "rm",
116
+ "mv",
117
+ "cp",
118
+ "chmod",
119
+ "exec",
120
+ "shell",
121
+ "execute",
122
+ ]);
123
+ /**
124
+ * Merges parent and subagent hooks.
125
+ * Subagent hooks are added after parent hooks (subagent hooks fire last).
126
+ * @internal
127
+ */
128
+ function mergeHooks(parentHooks, subagentHooks) {
129
+ if (!subagentHooks) {
130
+ return parentHooks;
131
+ }
132
+ const merged = {};
133
+ // Merge tool lifecycle hooks (HookMatcher[])
134
+ const toolEvents = ["PreToolUse", "PostToolUse", "PostToolUseFailure"];
135
+ for (const eventType of toolEvents) {
136
+ const parentMatchers = parentHooks[eventType];
137
+ const subagentMatchers = subagentHooks[eventType];
138
+ if (parentMatchers || subagentMatchers) {
139
+ merged[eventType] = [...(parentMatchers ?? []), ...(subagentMatchers ?? [])];
140
+ }
141
+ }
142
+ // Merge generation lifecycle hooks (HookCallback[])
143
+ const genEvents = ["PreGenerate", "PostGenerate", "PostGenerateFailure"];
144
+ for (const eventType of genEvents) {
145
+ const parentCallbacks = parentHooks[eventType];
146
+ const subagentCallbacks = subagentHooks[eventType];
147
+ if (parentCallbacks || subagentCallbacks) {
148
+ merged[eventType] = [...(parentCallbacks ?? []), ...(subagentCallbacks ?? [])];
149
+ }
150
+ }
151
+ return merged;
152
+ }
153
+ /**
154
+ * Filters parent hooks to only include specific events.
155
+ * @internal
156
+ */
157
+ function filterHookEvents(parentHooks, events) {
158
+ const filtered = {};
159
+ // Filter tool lifecycle hooks (HookMatcher[])
160
+ const toolEvents = ["PreToolUse", "PostToolUse", "PostToolUseFailure"];
161
+ for (const event of toolEvents) {
162
+ if (events.includes(event) && parentHooks[event]) {
163
+ filtered[event] = parentHooks[event];
164
+ }
165
+ }
166
+ // Filter generation lifecycle hooks (HookCallback[])
167
+ const genEvents = ["PreGenerate", "PostGenerate", "PostGenerateFailure"];
168
+ for (const event of genEvents) {
169
+ if (events.includes(event) && parentHooks[event]) {
170
+ filtered[event] = parentHooks[event];
171
+ }
172
+ }
173
+ // Filter session lifecycle hooks (HookCallback[])
174
+ const sessionEvents = ["SessionStart", "SessionEnd"];
175
+ for (const event of sessionEvents) {
176
+ if (events.includes(event) && parentHooks[event]) {
177
+ filtered[event] = parentHooks[event];
178
+ }
179
+ }
180
+ // Filter subagent lifecycle hooks (HookCallback[])
181
+ const subagentEvents = ["SubagentStart", "SubagentStop"];
182
+ for (const event of subagentEvents) {
183
+ if (events.includes(event) && parentHooks[event]) {
184
+ filtered[event] = parentHooks[event];
185
+ }
186
+ }
187
+ return filtered;
188
+ }
189
+ /**
190
+ * Checks if dangerous tools are accessible without explicit security controls.
191
+ * Logs a warning if found.
192
+ * @internal
193
+ */
194
+ function checkDangerousToolAccess(allowedTools, hooks, subagentName, inheritHooks) {
195
+ const dangerousToolsFound = allowedTools.filter((tool) => DANGEROUS_TOOLS.has(tool));
196
+ if (dangerousToolsFound.length === 0) {
197
+ return; // No dangerous tools, all good
198
+ }
199
+ // Check if there are security controls in place
200
+ const hasToolHooks = hooks?.PreToolUse !== undefined && hooks.PreToolUse.length > 0;
201
+ const explicitNoInheritance = inheritHooks === false;
202
+ if (!hasToolHooks && !explicitNoInheritance) {
203
+ console.warn(`[Agent SDK] Subagent "${subagentName}" has access to dangerous tools [${dangerousToolsFound.join(", ")}] without explicit security controls. ` +
204
+ `Consider:\n` +
205
+ ` - Setting inheritHooks: false to isolate from parent permissions\n` +
206
+ ` - Adding PreToolUse hooks for permission checking\n` +
207
+ ` - Using permissionMode: "plan" to prevent execution\n` +
208
+ ` - Using canUseTool callback for runtime approval`);
209
+ }
210
+ }
211
+ // Note: createTaskTool has been moved to ./tools/task.ts with enhanced functionality
212
+ // Use the one from ./tools/index.js instead
213
+ //# sourceMappingURL=subagents.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subagents.js","sourceRoot":"","sources":["../src/subagents.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgEG;AACH,MAAM,UAAU,cAAc,CAAC,WAAkB,EAAE,OAAwB;IACzE,6BAA6B;IAC7B,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,IAAI,CAAC,CAAC,wBAAwB;IAC3E,IAAI,WAAW,GAAiC,OAAO,CAAC,KAAK,CAAC;IAE9D,IAAI,YAAY,IAAI,WAAW,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QAC9C,4BAA4B;QAC5B,IAAI,YAAY,KAAK,IAAI,EAA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AAC,CAAC;QACvC,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;GAIG;AACH,SAAS,wBAAwB,CAC/B,YAAsB,EACtB,KAAmC,EACnC,YAAoB,EACpB,YAAgC;IAEhC,MAAM,mBAAmB,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IAErF,IAAI,mBAAmB,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrC,OAAO,CAAC,+BAA+B;IACzC,CAAC;IAED,gDAAgD;IAChD,MAAM,YAAY,GAAG,KAAK,EAAE,UAAU,KAAK,SAAS,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC;IACpF,MAAM,qBAAqB,GAAG,YAAY,KAAK,KAAK,CAAC;IAErD,IAAI,CAAC,YAAY,IAAI,CAAC,qBAAqB,EAAE,CAAC;QAC5C,OAAO,CAAC,IAAI,CACV,yBAAyB,YAAY,oCAAoC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,wCAAwC;YAC7I,aAAa;YACb,sEAAsE;YACtE,uDAAuD;YACvD,yDAAyD;YACzD,oDAAoD,CACvD,CAAC;IACJ,CAAC;AACH,CAAC;AAED,qFAAqF;AACrF,4CAA4C"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * File-based task store implementation.
3
+ *
4
+ * Stores tasks as JSON files in a directory.
5
+ * Provides persistence across process restarts.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ import type { BackgroundTask, BackgroundTaskStatus, BaseTaskStore, TaskStoreOptions } from "./types.js";
10
+ /**
11
+ * File-based implementation of the task store.
12
+ *
13
+ * Stores each task as a JSON file in the specified directory.
14
+ * Tasks persist across process restarts.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const store = new FileTaskStore({
19
+ * directory: "./task-data",
20
+ * });
21
+ *
22
+ * await store.save({
23
+ * id: "task-123",
24
+ * subagentType: "researcher",
25
+ * description: "Research topic",
26
+ * status: "running",
27
+ * createdAt: new Date().toISOString(),
28
+ * updatedAt: new Date().toISOString(),
29
+ * });
30
+ *
31
+ * const task = await store.load("task-123");
32
+ * ```
33
+ *
34
+ * @category TaskStore
35
+ */
36
+ export declare class FileTaskStore implements BaseTaskStore {
37
+ private directory;
38
+ private namespace?;
39
+ private expirationMs;
40
+ /**
41
+ * Create a new file-based task store.
42
+ *
43
+ * @param options - Configuration options including directory path
44
+ */
45
+ constructor(options: TaskStoreOptions & {
46
+ /** Directory to store task files */
47
+ directory: string;
48
+ });
49
+ /**
50
+ * Get the file path for a task ID.
51
+ * @internal
52
+ */
53
+ private getFilePath;
54
+ /**
55
+ * Get the task ID from a file name.
56
+ * @internal
57
+ */
58
+ private getTaskIdFromFileName;
59
+ /**
60
+ * Ensure the storage directory exists.
61
+ * @internal
62
+ */
63
+ private ensureDirectory;
64
+ save(task: BackgroundTask): Promise<void>;
65
+ load(taskId: string): Promise<BackgroundTask | undefined>;
66
+ list(filter?: {
67
+ status?: BackgroundTaskStatus | BackgroundTaskStatus[];
68
+ }): Promise<string[]>;
69
+ listTasks(filter?: {
70
+ status?: BackgroundTaskStatus | BackgroundTaskStatus[];
71
+ }): Promise<BackgroundTask[]>;
72
+ delete(taskId: string): Promise<boolean>;
73
+ exists(taskId: string): Promise<boolean>;
74
+ cleanup(): Promise<number>;
75
+ }
76
+ //# sourceMappingURL=file-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-store.d.ts","sourceRoot":"","sources":["../../src/task-store/file-store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAGpB;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,aAAc,YAAW,aAAa;IACjD,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,SAAS,CAAC,CAAS;IAC3B,OAAO,CAAC,YAAY,CAAS;IAE7B;;;;OAIG;gBAED,OAAO,EAAE,gBAAgB,GAAG;QAC1B,oCAAoC;QACpC,SAAS,EAAE,MAAM,CAAC;KACnB;IAOH;;;OAGG;IACH,OAAO,CAAC,WAAW;IAKnB;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAkB7B;;;OAGG;YACW,eAAe;IAWvB,IAAI,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAOzC,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC;IAqBzD,IAAI,CAAC,MAAM,CAAC,EAAE;QAClB,MAAM,CAAC,EAAE,oBAAoB,GAAG,oBAAoB,EAAE,CAAC;KACxD,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAKf,SAAS,CAAC,MAAM,CAAC,EAAE;QACvB,MAAM,CAAC,EAAE,oBAAoB,GAAG,oBAAoB,EAAE,CAAC;KACxD,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IAsCvB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAcxC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAWxC,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;CAejC"}
@@ -0,0 +1,190 @@
1
+ /**
2
+ * File-based task store implementation.
3
+ *
4
+ * Stores tasks as JSON files in a directory.
5
+ * Provides persistence across process restarts.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ import { promises as fs } from "node:fs";
10
+ import * as path from "node:path";
11
+ import { isBackgroundTask, shouldExpireTask } from "./types.js";
12
+ /**
13
+ * File-based implementation of the task store.
14
+ *
15
+ * Stores each task as a JSON file in the specified directory.
16
+ * Tasks persist across process restarts.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const store = new FileTaskStore({
21
+ * directory: "./task-data",
22
+ * });
23
+ *
24
+ * await store.save({
25
+ * id: "task-123",
26
+ * subagentType: "researcher",
27
+ * description: "Research topic",
28
+ * status: "running",
29
+ * createdAt: new Date().toISOString(),
30
+ * updatedAt: new Date().toISOString(),
31
+ * });
32
+ *
33
+ * const task = await store.load("task-123");
34
+ * ```
35
+ *
36
+ * @category TaskStore
37
+ */
38
+ export class FileTaskStore {
39
+ directory;
40
+ namespace;
41
+ expirationMs;
42
+ /**
43
+ * Create a new file-based task store.
44
+ *
45
+ * @param options - Configuration options including directory path
46
+ */
47
+ constructor(options) {
48
+ this.directory = options.directory;
49
+ this.namespace = options.namespace;
50
+ this.expirationMs = options.expirationMs ?? 86400000; // 24 hours default
51
+ }
52
+ /**
53
+ * Get the file path for a task ID.
54
+ * @internal
55
+ */
56
+ getFilePath(taskId) {
57
+ const fileName = this.namespace ? `${this.namespace}-${taskId}.json` : `${taskId}.json`;
58
+ return path.join(this.directory, fileName);
59
+ }
60
+ /**
61
+ * Get the task ID from a file name.
62
+ * @internal
63
+ */
64
+ getTaskIdFromFileName(fileName) {
65
+ if (!fileName.endsWith(".json")) {
66
+ return null;
67
+ }
68
+ const baseName = fileName.slice(0, -5); // Remove .json
69
+ if (this.namespace) {
70
+ const prefix = `${this.namespace}-`;
71
+ if (baseName.startsWith(prefix)) {
72
+ return baseName.slice(prefix.length);
73
+ }
74
+ return null; // Not in our namespace
75
+ }
76
+ return baseName;
77
+ }
78
+ /**
79
+ * Ensure the storage directory exists.
80
+ * @internal
81
+ */
82
+ async ensureDirectory() {
83
+ try {
84
+ await fs.mkdir(this.directory, { recursive: true });
85
+ }
86
+ catch (error) {
87
+ // Ignore if directory already exists
88
+ if (error.code !== "EEXIST") {
89
+ throw error;
90
+ }
91
+ }
92
+ }
93
+ async save(task) {
94
+ await this.ensureDirectory();
95
+ const filePath = this.getFilePath(task.id);
96
+ const data = JSON.stringify(task, null, 2);
97
+ await fs.writeFile(filePath, data, "utf-8");
98
+ }
99
+ async load(taskId) {
100
+ const filePath = this.getFilePath(taskId);
101
+ try {
102
+ const data = await fs.readFile(filePath, "utf-8");
103
+ const task = JSON.parse(data);
104
+ if (!isBackgroundTask(task)) {
105
+ console.warn(`Invalid task data in ${filePath}, skipping`);
106
+ return undefined;
107
+ }
108
+ return task;
109
+ }
110
+ catch (error) {
111
+ if (error.code === "ENOENT") {
112
+ return undefined; // File not found
113
+ }
114
+ throw error;
115
+ }
116
+ }
117
+ async list(filter) {
118
+ const tasks = await this.listTasks(filter);
119
+ return tasks.map((t) => t.id);
120
+ }
121
+ async listTasks(filter) {
122
+ await this.ensureDirectory();
123
+ try {
124
+ const files = await fs.readdir(this.directory);
125
+ const tasks = [];
126
+ for (const file of files) {
127
+ const taskId = this.getTaskIdFromFileName(file);
128
+ if (!taskId) {
129
+ continue; // Not a task file or not in our namespace
130
+ }
131
+ const task = await this.load(taskId);
132
+ if (!task) {
133
+ continue; // Invalid or missing task
134
+ }
135
+ // Apply status filter if provided
136
+ if (filter?.status) {
137
+ const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
138
+ if (!statuses.includes(task.status)) {
139
+ continue;
140
+ }
141
+ }
142
+ tasks.push(task);
143
+ }
144
+ return tasks;
145
+ }
146
+ catch (error) {
147
+ if (error.code === "ENOENT") {
148
+ return []; // Directory doesn't exist yet
149
+ }
150
+ throw error;
151
+ }
152
+ }
153
+ async delete(taskId) {
154
+ const filePath = this.getFilePath(taskId);
155
+ try {
156
+ await fs.unlink(filePath);
157
+ return true;
158
+ }
159
+ catch (error) {
160
+ if (error.code === "ENOENT") {
161
+ return false; // File not found
162
+ }
163
+ throw error;
164
+ }
165
+ }
166
+ async exists(taskId) {
167
+ const filePath = this.getFilePath(taskId);
168
+ try {
169
+ await fs.access(filePath);
170
+ return true;
171
+ }
172
+ catch {
173
+ return false;
174
+ }
175
+ }
176
+ async cleanup() {
177
+ const allTasks = await this.listTasks({
178
+ status: ["completed", "failed"],
179
+ });
180
+ let cleaned = 0;
181
+ for (const task of allTasks) {
182
+ if (shouldExpireTask(task, this.expirationMs)) {
183
+ await this.delete(task.id);
184
+ cleaned++;
185
+ }
186
+ }
187
+ return cleaned;
188
+ }
189
+ }
190
+ //# sourceMappingURL=file-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-store.js","sourceRoot":"","sources":["../../src/task-store/file-store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAOlC,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,OAAO,aAAa;IAChB,SAAS,CAAS;IAClB,SAAS,CAAU;IACnB,YAAY,CAAS;IAE7B;;;;OAIG;IACH,YACE,OAGC;QAED,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,QAAQ,CAAC,CAAC,mBAAmB;IAC3E,CAAC;IAED;;;OAGG;IACK,WAAW,CAAC,MAAc;QAChC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,SAAS,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,OAAO,CAAC;QACxF,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAC7C,CAAC;IAED;;;OAGG;IACK,qBAAqB,CAAC,QAAgB;QAC5C,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YAChC,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,QAAQ,GAAG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C1B,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,KAAK,CAAC,OAAO;QACX,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC;YACpC,MAAM,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;SAChC,CAAC,CAAC;QAEH,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;YAC5B,IAAI,gBAAgB,CAAC,IAAI,EAAE,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;gBAC9C,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBAC3B,OAAO,EAAE,CAAC;YACZ,CAAC;QACH,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Task store system for background task persistence.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ export { FileTaskStore } from "./file-store.js";
7
+ export { type KeyValueStore, KVTaskStore } from "./kv-store.js";
8
+ export { MemoryTaskStore } from "./memory-store.js";
9
+ export type { BackgroundTask, BackgroundTaskStatus, BaseTaskStore, TaskStoreOptions, } from "./types.js";
10
+ export { createBackgroundTask, isBackgroundTask, shouldExpireTask, updateBackgroundTask, } from "./types.js";
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/task-store/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,KAAK,aAAa,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEhE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,YAAY,EACV,cAAc,EACd,oBAAoB,EACpB,aAAa,EACb,gBAAgB,GACjB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,YAAY,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Task store system for background task persistence.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ export { FileTaskStore } from "./file-store.js";
7
+ export { KVTaskStore } from "./kv-store.js";
8
+ export { MemoryTaskStore } from "./memory-store.js";
9
+ export { createBackgroundTask, isBackgroundTask, shouldExpireTask, updateBackgroundTask, } from "./types.js";
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/task-store/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAsB,WAAW,EAAE,MAAM,eAAe,CAAC;AAEhE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAOpD,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,YAAY,CAAC"}
@@ -0,0 +1,140 @@
1
+ /**
2
+ * Key-value store task implementation.
3
+ *
4
+ * Stores tasks in a key-value store (Redis, DynamoDB, etc.).
5
+ * Provides persistence with fast access.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ import type { BackgroundTask, BackgroundTaskStatus, BaseTaskStore, TaskStoreOptions } from "./types.js";
10
+ /**
11
+ * Abstract key-value store interface.
12
+ *
13
+ * Implement this interface to connect to your KV store backend.
14
+ *
15
+ * @category TaskStore
16
+ */
17
+ export interface KeyValueStore {
18
+ /**
19
+ * Get a value by key.
20
+ *
21
+ * @param key - The key to get
22
+ * @returns The value if found, null otherwise
23
+ */
24
+ get(key: string): Promise<string | null>;
25
+ /**
26
+ * Set a value for a key.
27
+ *
28
+ * @param key - The key to set
29
+ * @param value - The value to store
30
+ * @param options - Optional TTL in seconds
31
+ */
32
+ set(key: string, value: string, options?: {
33
+ ttl?: number;
34
+ }): Promise<void>;
35
+ /**
36
+ * Delete a key.
37
+ *
38
+ * @param key - The key to delete
39
+ * @returns True if deleted, false if not found
40
+ */
41
+ delete(key: string): Promise<boolean>;
42
+ /**
43
+ * Check if a key exists.
44
+ *
45
+ * @param key - The key to check
46
+ * @returns True if exists
47
+ */
48
+ exists(key: string): Promise<boolean>;
49
+ /**
50
+ * List all keys matching a pattern.
51
+ *
52
+ * @param pattern - Glob pattern (e.g., "tasks:*")
53
+ * @returns Array of matching keys
54
+ */
55
+ keys(pattern: string): Promise<string[]>;
56
+ }
57
+ /**
58
+ * Key-value store implementation of the task store.
59
+ *
60
+ * Stores tasks in a key-value store (Redis, DynamoDB, etc.).
61
+ * Requires a KeyValueStore implementation.
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * // Using Redis
66
+ * import { createClient } from "redis";
67
+ *
68
+ * const redisClient = createClient();
69
+ * await redisClient.connect();
70
+ *
71
+ * const kvStore: KeyValueStore = {
72
+ * async get(key) {
73
+ * return redisClient.get(key);
74
+ * },
75
+ * async set(key, value, options) {
76
+ * if (options?.ttl) {
77
+ * await redisClient.setEx(key, options.ttl, value);
78
+ * } else {
79
+ * await redisClient.set(key, value);
80
+ * }
81
+ * },
82
+ * async delete(key) {
83
+ * const result = await redisClient.del(key);
84
+ * return result > 0;
85
+ * },
86
+ * async exists(key) {
87
+ * const result = await redisClient.exists(key);
88
+ * return result > 0;
89
+ * },
90
+ * async keys(pattern) {
91
+ * return redisClient.keys(pattern);
92
+ * },
93
+ * };
94
+ *
95
+ * const store = new KVTaskStore(kvStore, {
96
+ * namespace: "myapp",
97
+ * });
98
+ * ```
99
+ *
100
+ * @category TaskStore
101
+ */
102
+ export declare class KVTaskStore implements BaseTaskStore {
103
+ private kv;
104
+ private namespace?;
105
+ private expirationMs;
106
+ /**
107
+ * Create a new KV-based task store.
108
+ *
109
+ * @param kv - The key-value store implementation
110
+ * @param options - Configuration options
111
+ */
112
+ constructor(kv: KeyValueStore, options?: TaskStoreOptions);
113
+ /**
114
+ * Get the storage key for a task ID.
115
+ * @internal
116
+ */
117
+ private getKey;
118
+ /**
119
+ * Get the task ID from a storage key.
120
+ * @internal
121
+ */
122
+ private getTaskIdFromKey;
123
+ /**
124
+ * Get the key pattern for listing tasks.
125
+ * @internal
126
+ */
127
+ private getKeyPattern;
128
+ save(task: BackgroundTask): Promise<void>;
129
+ load(taskId: string): Promise<BackgroundTask | undefined>;
130
+ list(filter?: {
131
+ status?: BackgroundTaskStatus | BackgroundTaskStatus[];
132
+ }): Promise<string[]>;
133
+ listTasks(filter?: {
134
+ status?: BackgroundTaskStatus | BackgroundTaskStatus[];
135
+ }): Promise<BackgroundTask[]>;
136
+ delete(taskId: string): Promise<boolean>;
137
+ exists(taskId: string): Promise<boolean>;
138
+ cleanup(): Promise<number>;
139
+ }
140
+ //# sourceMappingURL=kv-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kv-store.d.ts","sourceRoot":"","sources":["../../src/task-store/kv-store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAGpB;;;;;;GAMG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;OAKG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAEzC;;;;;;OAMG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE3E;;;;;OAKG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC;;;;;OA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