groundswell 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 (120) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/.claude/system_prompts/task-breakdown.md +100 -0
  3. package/PRPs/001-hierarchical-workflow-engine.md +2438 -0
  4. package/PRPs/PRDs/001-hierarchical-workflow-engine.md +543 -0
  5. package/PRPs/PRDs/002-agent-prompt.md +390 -0
  6. package/PRPs/PRDs/003-agent-prompt.md +943 -0
  7. package/PRPs/PRDs/004-agent-prompt.md +1136 -0
  8. package/PRPs/PRDs/tasks-001.json +492 -0
  9. package/PRPs/README.md +83 -0
  10. package/PRPs/templates/prp_base.md +222 -0
  11. package/README.md +218 -0
  12. package/docs/agent.md +422 -0
  13. package/docs/prompt.md +419 -0
  14. package/docs/workflow.md +600 -0
  15. package/examples/README.md +244 -0
  16. package/examples/examples/01-basic-workflow.ts +100 -0
  17. package/examples/examples/02-decorator-options.ts +217 -0
  18. package/examples/examples/03-parent-child.ts +241 -0
  19. package/examples/examples/04-observers-debugger.ts +340 -0
  20. package/examples/examples/05-error-handling.ts +387 -0
  21. package/examples/examples/06-concurrent-tasks.ts +352 -0
  22. package/examples/examples/07-agent-loops.ts +432 -0
  23. package/examples/examples/08-sdk-features.ts +667 -0
  24. package/examples/examples/09-reflection.ts +573 -0
  25. package/examples/examples/10-introspection.ts +550 -0
  26. package/examples/index.ts +143 -0
  27. package/examples/utils/helpers.ts +57 -0
  28. package/llms_full.txt +5890 -0
  29. package/package.json +63 -0
  30. package/plan/P1P2/PRP.md +527 -0
  31. package/plan/P1P2/research/LRU_CACHE_BEST_PRACTICES.md +1929 -0
  32. package/plan/P1P2/research/LRU_CACHE_CODE_PATTERNS.md +857 -0
  33. package/plan/P1P2/research/LRU_CACHE_INTEGRATION_GUIDE.md +738 -0
  34. package/plan/P1P2/research/LRU_CACHE_RESEARCH_INDEX.md +424 -0
  35. package/plan/P1P2/research/REFLECTION_INDEX.md +291 -0
  36. package/plan/P1P2/research/REFLECTION_RESEARCH_REPORT.md +1342 -0
  37. package/plan/P1P2/research/RESEARCH_SUMMARY.md +342 -0
  38. package/plan/P1P2/research/anthropic-sdk.md +174 -0
  39. package/plan/P1P2/research/async-local-storage.md +200 -0
  40. package/plan/P1P2/research/reflection-code-patterns.md +1205 -0
  41. package/plan/P1P2/research/reflection-decision-matrix.md +421 -0
  42. package/plan/P1P2/research/reflection-implementation-guide.md +1341 -0
  43. package/plan/P1P2/research/reflection-integration-guide.md +834 -0
  44. package/plan/P1P2/research/reflection-patterns.md +1468 -0
  45. package/plan/P1P2/research/reflection-quick-reference.md +558 -0
  46. package/plan/P1P2/research/zod-schema.md +152 -0
  47. package/plan/P3P4/PRP.md +1388 -0
  48. package/plan/P3P4/research/caching-lru.md +116 -0
  49. package/plan/P3P4/research/introspection-tools.md +177 -0
  50. package/plan/P3P4/research/reflection-patterns.md +117 -0
  51. package/plan/P4P5/PRP.md +1136 -0
  52. package/plan/P4P5/research/RESEARCH_SUMMARY.md +151 -0
  53. package/plan/architecture/external_deps.md +358 -0
  54. package/plan/architecture/system_context.md +242 -0
  55. package/plan/backlog.json +867 -0
  56. package/plan/research/INTROSPECTION_RESEARCH_SUMMARY.md +378 -0
  57. package/plan/research/README-INTROSPECTION.md +352 -0
  58. package/plan/research/agent-introspection-patterns.md +1085 -0
  59. package/plan/research/introspection-security-guide.md +928 -0
  60. package/plan/research/introspection-tool-examples.md +875 -0
  61. package/scripts/generate-llms-full.ts +206 -0
  62. package/src/__tests__/integration/agent-workflow.test.ts +256 -0
  63. package/src/__tests__/integration/tree-mirroring.test.ts +114 -0
  64. package/src/__tests__/unit/agent.test.ts +169 -0
  65. package/src/__tests__/unit/cache-key.test.ts +182 -0
  66. package/src/__tests__/unit/cache.test.ts +172 -0
  67. package/src/__tests__/unit/context.test.ts +138 -0
  68. package/src/__tests__/unit/decorators.test.ts +100 -0
  69. package/src/__tests__/unit/introspection-tools.test.ts +277 -0
  70. package/src/__tests__/unit/prompt.test.ts +135 -0
  71. package/src/__tests__/unit/reflection.test.ts +210 -0
  72. package/src/__tests__/unit/tree-debugger.test.ts +85 -0
  73. package/src/__tests__/unit/workflow.test.ts +81 -0
  74. package/src/cache/cache-key.ts +244 -0
  75. package/src/cache/cache.ts +236 -0
  76. package/src/cache/index.ts +8 -0
  77. package/src/core/agent.ts +573 -0
  78. package/src/core/context.ts +119 -0
  79. package/src/core/event-tree.ts +260 -0
  80. package/src/core/factory.ts +123 -0
  81. package/src/core/index.ts +17 -0
  82. package/src/core/logger.ts +87 -0
  83. package/src/core/mcp-handler.ts +184 -0
  84. package/src/core/prompt.ts +150 -0
  85. package/src/core/workflow-context.ts +349 -0
  86. package/src/core/workflow.ts +302 -0
  87. package/src/debugger/index.ts +1 -0
  88. package/src/debugger/tree-debugger.ts +210 -0
  89. package/src/decorators/index.ts +3 -0
  90. package/src/decorators/observed-state.ts +95 -0
  91. package/src/decorators/step.ts +139 -0
  92. package/src/decorators/task.ts +96 -0
  93. package/src/examples/index.ts +2 -0
  94. package/src/examples/tdd-orchestrator.ts +65 -0
  95. package/src/examples/test-cycle-workflow.ts +64 -0
  96. package/src/index.ts +140 -0
  97. package/src/reflection/index.ts +5 -0
  98. package/src/reflection/reflection.ts +407 -0
  99. package/src/tools/index.ts +36 -0
  100. package/src/tools/introspection.ts +464 -0
  101. package/src/types/agent.ts +90 -0
  102. package/src/types/decorators.ts +25 -0
  103. package/src/types/error-strategy.ts +13 -0
  104. package/src/types/error.ts +20 -0
  105. package/src/types/events.ts +74 -0
  106. package/src/types/index.ts +55 -0
  107. package/src/types/logging.ts +24 -0
  108. package/src/types/observer.ts +18 -0
  109. package/src/types/prompt.ts +40 -0
  110. package/src/types/reflection.ts +117 -0
  111. package/src/types/sdk-primitives.ts +128 -0
  112. package/src/types/snapshot.ts +14 -0
  113. package/src/types/workflow-context.ts +163 -0
  114. package/src/types/workflow.ts +37 -0
  115. package/src/utils/id.ts +11 -0
  116. package/src/utils/index.ts +3 -0
  117. package/src/utils/observable.ts +77 -0
  118. package/tasks.json +0 -0
  119. package/tsconfig.json +22 -0
  120. package/vitest.config.ts +16 -0
package/docs/agent.md ADDED
@@ -0,0 +1,422 @@
1
+ # Agents
2
+
3
+ Agents are lightweight wrappers around the Anthropic SDK that execute prompts, manage tool invocation cycles, and integrate with caching and reflection systems.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Basic Usage](#basic-usage)
8
+ - [Configuration](#configuration)
9
+ - [Executing Prompts](#executing-prompts)
10
+ - [Reflection](#reflection)
11
+ - [Tools and MCP](#tools-and-mcp)
12
+ - [Hooks](#hooks)
13
+ - [Caching](#caching)
14
+ - [API Reference](#api-reference)
15
+
16
+ ## Basic Usage
17
+
18
+ ```typescript
19
+ import { createAgent, createPrompt } from 'groundswell';
20
+ import { z } from 'zod';
21
+
22
+ const agent = createAgent({
23
+ name: 'AnalysisAgent',
24
+ model: 'claude-sonnet-4-20250514',
25
+ enableCache: true,
26
+ });
27
+
28
+ const prompt = createPrompt({
29
+ user: 'Analyze this code for bugs',
30
+ data: { code: 'function foo() { return 42; }' },
31
+ responseFormat: z.object({
32
+ bugs: z.array(z.string()),
33
+ severity: z.enum(['low', 'medium', 'high']),
34
+ }),
35
+ });
36
+
37
+ const result = await agent.prompt(prompt);
38
+ // result is typed: { bugs: string[], severity: 'low' | 'medium' | 'high' }
39
+ ```
40
+
41
+ ## Configuration
42
+
43
+ ### AgentConfig
44
+
45
+ ```typescript
46
+ interface AgentConfig {
47
+ name?: string; // Human-readable name
48
+ system?: string; // System prompt
49
+ tools?: Tool[]; // Available tools
50
+ mcps?: MCPServer[]; // MCP servers to connect
51
+ skills?: Skill[]; // Skills to load
52
+ hooks?: AgentHooks; // Lifecycle hooks
53
+ env?: Record<string, string>; // Environment variables
54
+ enableReflection?: boolean; // Enable reflection capability
55
+ enableCache?: boolean; // Enable response caching
56
+ model?: string; // Model to use
57
+ maxTokens?: number; // Max tokens for responses
58
+ temperature?: number; // Response temperature
59
+ }
60
+ ```
61
+
62
+ ### Configuration Priority
63
+
64
+ Configuration follows a three-level override hierarchy:
65
+
66
+ 1. **Prompt-level** (highest priority)
67
+ 2. **Execution-level** (via `PromptOverrides`)
68
+ 3. **Agent-level** (lowest priority)
69
+
70
+ ```typescript
71
+ const agent = createAgent({
72
+ system: 'Default system prompt', // Agent-level
73
+ model: 'claude-sonnet-4-20250514',
74
+ });
75
+
76
+ const prompt = createPrompt({
77
+ user: 'Hello',
78
+ system: 'Override system prompt', // Prompt-level (wins)
79
+ responseFormat: z.object({ response: z.string() }),
80
+ });
81
+
82
+ // Or override at execution time
83
+ const result = await agent.prompt(prompt, {
84
+ model: 'claude-opus-4-5-20251101', // Execution-level override
85
+ });
86
+ ```
87
+
88
+ ## Executing Prompts
89
+
90
+ ### prompt()
91
+
92
+ Returns validated response data:
93
+
94
+ ```typescript
95
+ const result = await agent.prompt(prompt);
96
+ // result is T (the response type)
97
+ ```
98
+
99
+ ### promptWithMetadata()
100
+
101
+ Returns full execution metadata:
102
+
103
+ ```typescript
104
+ const result = await agent.promptWithMetadata(prompt);
105
+
106
+ console.log(result.data); // Validated response
107
+ console.log(result.usage); // { input_tokens, output_tokens }
108
+ console.log(result.duration); // Total time in ms
109
+ console.log(result.toolCalls); // Number of tool invocations
110
+ ```
111
+
112
+ ### PromptResult
113
+
114
+ ```typescript
115
+ interface PromptResult<T> {
116
+ data: T; // Validated response
117
+ usage: TokenUsage; // Token usage stats
118
+ duration: number; // Duration in milliseconds
119
+ toolCalls: number; // Number of tool calls
120
+ }
121
+
122
+ interface TokenUsage {
123
+ input_tokens: number;
124
+ output_tokens: number;
125
+ }
126
+ ```
127
+
128
+ ## Reflection
129
+
130
+ Enable self-correction with the `reflect()` method:
131
+
132
+ ```typescript
133
+ const agent = createAgent({
134
+ name: 'ReflectiveAgent',
135
+ enableReflection: true,
136
+ });
137
+
138
+ const result = await agent.reflect(prompt);
139
+ ```
140
+
141
+ The reflection system prepends a reflection prefix to the system prompt:
142
+
143
+ ```
144
+ Before answering, reflect on your reasoning step by step.
145
+ Consider alternative approaches and potential errors.
146
+ Then provide your final answer.
147
+ ```
148
+
149
+ ### Reflection Configuration
150
+
151
+ ```typescript
152
+ // Agent-level
153
+ const agent = createAgent({
154
+ enableReflection: true,
155
+ });
156
+
157
+ // Prompt-level
158
+ const prompt = createPrompt({
159
+ user: 'Complex question',
160
+ enableReflection: true,
161
+ responseFormat: schema,
162
+ });
163
+
164
+ // Execution-level
165
+ const result = await agent.prompt(prompt, {
166
+ enableReflection: true,
167
+ });
168
+ ```
169
+
170
+ ## Tools and MCP
171
+
172
+ ### Tool Definition
173
+
174
+ ```typescript
175
+ import type { Tool } from 'groundswell';
176
+
177
+ const calculatorTool: Tool = {
178
+ name: 'calculate',
179
+ description: 'Performs arithmetic operations',
180
+ input_schema: {
181
+ type: 'object',
182
+ properties: {
183
+ operation: { type: 'string', enum: ['add', 'subtract', 'multiply', 'divide'] },
184
+ a: { type: 'number' },
185
+ b: { type: 'number' },
186
+ },
187
+ required: ['operation', 'a', 'b'],
188
+ },
189
+ };
190
+ ```
191
+
192
+ ### MCP Handler
193
+
194
+ Register tools with an MCP handler:
195
+
196
+ ```typescript
197
+ import { MCPHandler } from 'groundswell';
198
+
199
+ const mcpHandler = new MCPHandler();
200
+
201
+ mcpHandler.registerServer({
202
+ name: 'demo',
203
+ transport: 'inprocess',
204
+ tools: [calculatorTool],
205
+ });
206
+
207
+ mcpHandler.registerToolExecutor('demo', 'calculate', async (input) => {
208
+ const { operation, a, b } = input;
209
+ switch (operation) {
210
+ case 'add': return { result: a + b };
211
+ case 'subtract': return { result: a - b };
212
+ case 'multiply': return { result: a * b };
213
+ case 'divide': return { result: a / b };
214
+ }
215
+ });
216
+
217
+ const agent = createAgent({
218
+ tools: mcpHandler.getTools(),
219
+ });
220
+ ```
221
+
222
+ ### Tool Execution Flow
223
+
224
+ 1. Agent sends prompt to API
225
+ 2. API requests tool use
226
+ 3. Agent executes tool via MCP handler
227
+ 4. Result sent back to API
228
+ 5. Loop continues until no more tool calls
229
+ 6. Final response validated and returned
230
+
231
+ ## Hooks
232
+
233
+ Lifecycle hooks enable logging, monitoring, and custom processing:
234
+
235
+ ```typescript
236
+ import type { AgentHooks, PreToolUseContext, PostToolUseContext } from 'groundswell';
237
+
238
+ const hooks: AgentHooks = {
239
+ preToolUse: [
240
+ async (ctx: PreToolUseContext) => {
241
+ console.log(`[PRE] Tool: ${ctx.toolName}`);
242
+ console.log(`Input:`, ctx.input);
243
+ }
244
+ ],
245
+ postToolUse: [
246
+ async (ctx: PostToolUseContext) => {
247
+ console.log(`[POST] Tool: ${ctx.toolName}`);
248
+ console.log(`Output:`, ctx.output);
249
+ console.log(`Duration: ${ctx.duration}ms`);
250
+ }
251
+ ],
252
+ sessionStart: [
253
+ async (ctx) => {
254
+ console.log(`Session started: ${ctx.agentName}`);
255
+ }
256
+ ],
257
+ sessionEnd: [
258
+ async (ctx) => {
259
+ console.log(`Session ended: ${ctx.totalDuration}ms`);
260
+ }
261
+ ],
262
+ };
263
+
264
+ const agent = createAgent({
265
+ hooks,
266
+ });
267
+ ```
268
+
269
+ ### Hook Types
270
+
271
+ | Hook | Trigger | Context |
272
+ |------|---------|---------|
273
+ | `preToolUse` | Before tool execution | `toolName`, `input` |
274
+ | `postToolUse` | After tool execution | `toolName`, `input`, `output`, `duration` |
275
+ | `sessionStart` | Before prompt execution | `agentName`, `promptId` |
276
+ | `sessionEnd` | After prompt execution | `agentName`, `totalDuration` |
277
+
278
+ ## Caching
279
+
280
+ ### Enable Caching
281
+
282
+ ```typescript
283
+ const agent = createAgent({
284
+ enableCache: true,
285
+ });
286
+
287
+ // First call: API request made, result cached
288
+ const result1 = await agent.prompt(prompt);
289
+
290
+ // Second call: cached result returned
291
+ const result2 = await agent.prompt(prompt);
292
+ ```
293
+
294
+ ### Cache Metrics
295
+
296
+ ```typescript
297
+ import { defaultCache } from 'groundswell';
298
+
299
+ const metrics = defaultCache.metrics();
300
+ console.log(`Hits: ${metrics.hits}`);
301
+ console.log(`Misses: ${metrics.misses}`);
302
+ console.log(`Hit rate: ${metrics.hitRate}%`);
303
+ console.log(`Size: ${metrics.size} items`);
304
+ console.log(`Size bytes: ${metrics.sizeBytes}`);
305
+ ```
306
+
307
+ ### Cache Key Generation
308
+
309
+ Cache keys are deterministic SHA-256 hashes of:
310
+ - User message
311
+ - Data
312
+ - System prompt
313
+ - Model
314
+ - Temperature
315
+ - Max tokens
316
+ - Tools
317
+ - MCP servers
318
+ - Skills
319
+ - Response format schema
320
+
321
+ Identical inputs always produce identical keys.
322
+
323
+ ### Disable Cache for Specific Call
324
+
325
+ ```typescript
326
+ const result = await agent.prompt(prompt, {
327
+ disableCache: true,
328
+ });
329
+ ```
330
+
331
+ ### Clear Cache
332
+
333
+ ```typescript
334
+ import { defaultCache } from 'groundswell';
335
+
336
+ await defaultCache.clear();
337
+
338
+ // Or bust by prefix (agent ID)
339
+ await defaultCache.bustPrefix(agent.id);
340
+ ```
341
+
342
+ ### Cache Configuration
343
+
344
+ ```typescript
345
+ import { LLMCache } from 'groundswell';
346
+
347
+ const customCache = new LLMCache({
348
+ maxItems: 500, // Default: 1000
349
+ maxSizeBytes: 25_000_000, // Default: 50MB
350
+ defaultTTLMs: 7_200_000, // Default: 1 hour
351
+ });
352
+ ```
353
+
354
+ ## API Reference
355
+
356
+ ### Agent Class
357
+
358
+ ```typescript
359
+ class Agent {
360
+ readonly id: string;
361
+ readonly name: string;
362
+
363
+ constructor(config?: AgentConfig);
364
+
365
+ prompt<T>(prompt: Prompt<T>, overrides?: PromptOverrides): Promise<T>;
366
+ promptWithMetadata<T>(prompt: Prompt<T>, overrides?: PromptOverrides): Promise<PromptResult<T>>;
367
+ reflect<T>(prompt: Prompt<T>, overrides?: PromptOverrides): Promise<T>;
368
+ }
369
+ ```
370
+
371
+ ### Factory Function
372
+
373
+ ```typescript
374
+ function createAgent(config?: AgentConfig): Agent;
375
+ ```
376
+
377
+ ### PromptOverrides
378
+
379
+ ```typescript
380
+ interface PromptOverrides {
381
+ system?: string;
382
+ model?: string;
383
+ temperature?: number;
384
+ maxTokens?: number;
385
+ enableReflection?: boolean;
386
+ disableCache?: boolean;
387
+ }
388
+ ```
389
+
390
+ ### Types
391
+
392
+ ```typescript
393
+ interface Tool {
394
+ name: string;
395
+ description: string;
396
+ input_schema: {
397
+ type: 'object';
398
+ properties: Record<string, unknown>;
399
+ required?: string[];
400
+ };
401
+ }
402
+
403
+ interface MCPServer {
404
+ name: string;
405
+ transport: 'inprocess' | 'stdio' | 'http';
406
+ tools?: Tool[];
407
+ }
408
+
409
+ interface Skill {
410
+ name: string;
411
+ path: string;
412
+ }
413
+
414
+ interface AgentHooks {
415
+ preToolUse?: Array<(ctx: PreToolUseContext) => Promise<void>>;
416
+ postToolUse?: Array<(ctx: PostToolUseContext) => Promise<void>>;
417
+ sessionStart?: Array<(ctx: SessionStartContext) => Promise<void>>;
418
+ sessionEnd?: Array<(ctx: SessionEndContext) => Promise<void>>;
419
+ }
420
+ ```
421
+
422
+ See [examples/08-sdk-features.ts](../examples/examples/08-sdk-features.ts) for tools and hooks usage.