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.
- package/.claude/settings.local.json +9 -0
- package/.claude/system_prompts/task-breakdown.md +100 -0
- package/PRPs/001-hierarchical-workflow-engine.md +2438 -0
- package/PRPs/PRDs/001-hierarchical-workflow-engine.md +543 -0
- package/PRPs/PRDs/002-agent-prompt.md +390 -0
- package/PRPs/PRDs/003-agent-prompt.md +943 -0
- package/PRPs/PRDs/004-agent-prompt.md +1136 -0
- package/PRPs/PRDs/tasks-001.json +492 -0
- package/PRPs/README.md +83 -0
- package/PRPs/templates/prp_base.md +222 -0
- package/README.md +218 -0
- package/docs/agent.md +422 -0
- package/docs/prompt.md +419 -0
- package/docs/workflow.md +600 -0
- package/examples/README.md +244 -0
- package/examples/examples/01-basic-workflow.ts +100 -0
- package/examples/examples/02-decorator-options.ts +217 -0
- package/examples/examples/03-parent-child.ts +241 -0
- package/examples/examples/04-observers-debugger.ts +340 -0
- package/examples/examples/05-error-handling.ts +387 -0
- package/examples/examples/06-concurrent-tasks.ts +352 -0
- package/examples/examples/07-agent-loops.ts +432 -0
- package/examples/examples/08-sdk-features.ts +667 -0
- package/examples/examples/09-reflection.ts +573 -0
- package/examples/examples/10-introspection.ts +550 -0
- package/examples/index.ts +143 -0
- package/examples/utils/helpers.ts +57 -0
- package/llms_full.txt +5890 -0
- package/package.json +63 -0
- package/plan/P1P2/PRP.md +527 -0
- package/plan/P1P2/research/LRU_CACHE_BEST_PRACTICES.md +1929 -0
- package/plan/P1P2/research/LRU_CACHE_CODE_PATTERNS.md +857 -0
- package/plan/P1P2/research/LRU_CACHE_INTEGRATION_GUIDE.md +738 -0
- package/plan/P1P2/research/LRU_CACHE_RESEARCH_INDEX.md +424 -0
- package/plan/P1P2/research/REFLECTION_INDEX.md +291 -0
- package/plan/P1P2/research/REFLECTION_RESEARCH_REPORT.md +1342 -0
- package/plan/P1P2/research/RESEARCH_SUMMARY.md +342 -0
- package/plan/P1P2/research/anthropic-sdk.md +174 -0
- package/plan/P1P2/research/async-local-storage.md +200 -0
- package/plan/P1P2/research/reflection-code-patterns.md +1205 -0
- package/plan/P1P2/research/reflection-decision-matrix.md +421 -0
- package/plan/P1P2/research/reflection-implementation-guide.md +1341 -0
- package/plan/P1P2/research/reflection-integration-guide.md +834 -0
- package/plan/P1P2/research/reflection-patterns.md +1468 -0
- package/plan/P1P2/research/reflection-quick-reference.md +558 -0
- package/plan/P1P2/research/zod-schema.md +152 -0
- package/plan/P3P4/PRP.md +1388 -0
- package/plan/P3P4/research/caching-lru.md +116 -0
- package/plan/P3P4/research/introspection-tools.md +177 -0
- package/plan/P3P4/research/reflection-patterns.md +117 -0
- package/plan/P4P5/PRP.md +1136 -0
- package/plan/P4P5/research/RESEARCH_SUMMARY.md +151 -0
- package/plan/architecture/external_deps.md +358 -0
- package/plan/architecture/system_context.md +242 -0
- package/plan/backlog.json +867 -0
- package/plan/research/INTROSPECTION_RESEARCH_SUMMARY.md +378 -0
- package/plan/research/README-INTROSPECTION.md +352 -0
- package/plan/research/agent-introspection-patterns.md +1085 -0
- package/plan/research/introspection-security-guide.md +928 -0
- package/plan/research/introspection-tool-examples.md +875 -0
- package/scripts/generate-llms-full.ts +206 -0
- package/src/__tests__/integration/agent-workflow.test.ts +256 -0
- package/src/__tests__/integration/tree-mirroring.test.ts +114 -0
- package/src/__tests__/unit/agent.test.ts +169 -0
- package/src/__tests__/unit/cache-key.test.ts +182 -0
- package/src/__tests__/unit/cache.test.ts +172 -0
- package/src/__tests__/unit/context.test.ts +138 -0
- package/src/__tests__/unit/decorators.test.ts +100 -0
- package/src/__tests__/unit/introspection-tools.test.ts +277 -0
- package/src/__tests__/unit/prompt.test.ts +135 -0
- package/src/__tests__/unit/reflection.test.ts +210 -0
- package/src/__tests__/unit/tree-debugger.test.ts +85 -0
- package/src/__tests__/unit/workflow.test.ts +81 -0
- package/src/cache/cache-key.ts +244 -0
- package/src/cache/cache.ts +236 -0
- package/src/cache/index.ts +8 -0
- package/src/core/agent.ts +573 -0
- package/src/core/context.ts +119 -0
- package/src/core/event-tree.ts +260 -0
- package/src/core/factory.ts +123 -0
- package/src/core/index.ts +17 -0
- package/src/core/logger.ts +87 -0
- package/src/core/mcp-handler.ts +184 -0
- package/src/core/prompt.ts +150 -0
- package/src/core/workflow-context.ts +349 -0
- package/src/core/workflow.ts +302 -0
- package/src/debugger/index.ts +1 -0
- package/src/debugger/tree-debugger.ts +210 -0
- package/src/decorators/index.ts +3 -0
- package/src/decorators/observed-state.ts +95 -0
- package/src/decorators/step.ts +139 -0
- package/src/decorators/task.ts +96 -0
- package/src/examples/index.ts +2 -0
- package/src/examples/tdd-orchestrator.ts +65 -0
- package/src/examples/test-cycle-workflow.ts +64 -0
- package/src/index.ts +140 -0
- package/src/reflection/index.ts +5 -0
- package/src/reflection/reflection.ts +407 -0
- package/src/tools/index.ts +36 -0
- package/src/tools/introspection.ts +464 -0
- package/src/types/agent.ts +90 -0
- package/src/types/decorators.ts +25 -0
- package/src/types/error-strategy.ts +13 -0
- package/src/types/error.ts +20 -0
- package/src/types/events.ts +74 -0
- package/src/types/index.ts +55 -0
- package/src/types/logging.ts +24 -0
- package/src/types/observer.ts +18 -0
- package/src/types/prompt.ts +40 -0
- package/src/types/reflection.ts +117 -0
- package/src/types/sdk-primitives.ts +128 -0
- package/src/types/snapshot.ts +14 -0
- package/src/types/workflow-context.ts +163 -0
- package/src/types/workflow.ts +37 -0
- package/src/utils/id.ts +11 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/observable.ts +77 -0
- package/tasks.json +0 -0
- package/tsconfig.json +22 -0
- package/vitest.config.ts +16 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLMCache - LRU cache for LLM responses with size and TTL limits
|
|
3
|
+
*
|
|
4
|
+
* Wraps the lru-cache package to provide:
|
|
5
|
+
* - Item count limits
|
|
6
|
+
* - Memory size limits
|
|
7
|
+
* - Time-based expiration (TTL)
|
|
8
|
+
* - Prefix-based cache busting for related entries
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { LRUCache } from 'lru-cache';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Cache configuration options
|
|
15
|
+
*/
|
|
16
|
+
export interface CacheConfig {
|
|
17
|
+
/** Maximum number of items in cache (default: 1000) */
|
|
18
|
+
maxItems?: number;
|
|
19
|
+
/** Maximum cache size in bytes (default: 50MB) */
|
|
20
|
+
maxSizeBytes?: number;
|
|
21
|
+
/** Default TTL in milliseconds (default: 1 hour) */
|
|
22
|
+
defaultTTLMs?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Cache performance metrics
|
|
27
|
+
*/
|
|
28
|
+
export interface CacheMetrics {
|
|
29
|
+
/** Number of cache hits */
|
|
30
|
+
hits: number;
|
|
31
|
+
/** Number of cache misses */
|
|
32
|
+
misses: number;
|
|
33
|
+
/** Current number of items in cache */
|
|
34
|
+
size: number;
|
|
35
|
+
/** Current cache size in bytes (estimated) */
|
|
36
|
+
sizeBytes: number;
|
|
37
|
+
/** Hit rate percentage */
|
|
38
|
+
hitRate: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Internal cache entry wrapper
|
|
43
|
+
*/
|
|
44
|
+
interface CacheEntry<T> {
|
|
45
|
+
value: T;
|
|
46
|
+
prefix?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* LLMCache - High-performance LRU cache for LLM responses
|
|
51
|
+
*/
|
|
52
|
+
export class LLMCache<T = unknown> {
|
|
53
|
+
private readonly cache: LRUCache<string, CacheEntry<T>>;
|
|
54
|
+
private readonly prefixIndex: Map<string, Set<string>>;
|
|
55
|
+
private readonly config: Required<CacheConfig>;
|
|
56
|
+
|
|
57
|
+
private hitCount = 0;
|
|
58
|
+
private missCount = 0;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Create a new LLMCache instance
|
|
62
|
+
* @param config Cache configuration
|
|
63
|
+
*/
|
|
64
|
+
constructor(config: CacheConfig = {}) {
|
|
65
|
+
this.config = {
|
|
66
|
+
maxItems: config.maxItems ?? 1000,
|
|
67
|
+
maxSizeBytes: config.maxSizeBytes ?? 52_428_800, // 50MB
|
|
68
|
+
defaultTTLMs: config.defaultTTLMs ?? 3_600_000, // 1 hour
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
this.prefixIndex = new Map();
|
|
72
|
+
|
|
73
|
+
this.cache = new LRUCache<string, CacheEntry<T>>({
|
|
74
|
+
max: this.config.maxItems,
|
|
75
|
+
maxSize: this.config.maxSizeBytes,
|
|
76
|
+
sizeCalculation: (entry) => {
|
|
77
|
+
// Estimate size based on JSON serialization
|
|
78
|
+
try {
|
|
79
|
+
return JSON.stringify(entry.value).length;
|
|
80
|
+
} catch {
|
|
81
|
+
// Fallback for non-serializable values
|
|
82
|
+
return 1000;
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
ttl: this.config.defaultTTLMs,
|
|
86
|
+
updateAgeOnGet: true,
|
|
87
|
+
allowStale: false,
|
|
88
|
+
dispose: (entry, key) => {
|
|
89
|
+
// Clean up prefix index on eviction
|
|
90
|
+
if (entry.prefix) {
|
|
91
|
+
const keys = this.prefixIndex.get(entry.prefix);
|
|
92
|
+
if (keys) {
|
|
93
|
+
keys.delete(key);
|
|
94
|
+
if (keys.size === 0) {
|
|
95
|
+
this.prefixIndex.delete(entry.prefix);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Get a value from the cache
|
|
105
|
+
* @param key Cache key
|
|
106
|
+
* @returns Cached value or undefined if not found
|
|
107
|
+
*/
|
|
108
|
+
async get(key: string): Promise<T | undefined> {
|
|
109
|
+
const entry = this.cache.get(key);
|
|
110
|
+
if (entry) {
|
|
111
|
+
this.hitCount++;
|
|
112
|
+
return entry.value;
|
|
113
|
+
}
|
|
114
|
+
this.missCount++;
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Set a value in the cache
|
|
120
|
+
* @param key Cache key
|
|
121
|
+
* @param value Value to cache
|
|
122
|
+
* @param options Optional TTL override and prefix for grouping
|
|
123
|
+
*/
|
|
124
|
+
async set(
|
|
125
|
+
key: string,
|
|
126
|
+
value: T,
|
|
127
|
+
options?: { ttl?: number; prefix?: string }
|
|
128
|
+
): Promise<void> {
|
|
129
|
+
const entry: CacheEntry<T> = {
|
|
130
|
+
value,
|
|
131
|
+
prefix: options?.prefix,
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const cacheOptions: { ttl?: number } = {};
|
|
135
|
+
if (options?.ttl !== undefined) {
|
|
136
|
+
cacheOptions.ttl = options.ttl;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
this.cache.set(key, entry, cacheOptions);
|
|
140
|
+
|
|
141
|
+
// Track prefix for bulk invalidation
|
|
142
|
+
if (options?.prefix) {
|
|
143
|
+
let keys = this.prefixIndex.get(options.prefix);
|
|
144
|
+
if (!keys) {
|
|
145
|
+
keys = new Set();
|
|
146
|
+
this.prefixIndex.set(options.prefix, keys);
|
|
147
|
+
}
|
|
148
|
+
keys.add(key);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Check if a key exists in the cache (without affecting LRU order)
|
|
154
|
+
* @param key Cache key
|
|
155
|
+
* @returns True if key exists
|
|
156
|
+
*/
|
|
157
|
+
has(key: string): boolean {
|
|
158
|
+
return this.cache.has(key);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Remove a specific key from the cache
|
|
163
|
+
* @param key Cache key to remove
|
|
164
|
+
*/
|
|
165
|
+
async bust(key: string): Promise<void> {
|
|
166
|
+
const entry = this.cache.peek(key);
|
|
167
|
+
if (entry?.prefix) {
|
|
168
|
+
const keys = this.prefixIndex.get(entry.prefix);
|
|
169
|
+
if (keys) {
|
|
170
|
+
keys.delete(key);
|
|
171
|
+
if (keys.size === 0) {
|
|
172
|
+
this.prefixIndex.delete(entry.prefix);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
this.cache.delete(key);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Remove all keys with a given prefix
|
|
181
|
+
* @param prefix Prefix to match
|
|
182
|
+
*/
|
|
183
|
+
async bustPrefix(prefix: string): Promise<void> {
|
|
184
|
+
const keys = this.prefixIndex.get(prefix);
|
|
185
|
+
if (keys) {
|
|
186
|
+
for (const key of keys) {
|
|
187
|
+
this.cache.delete(key);
|
|
188
|
+
}
|
|
189
|
+
this.prefixIndex.delete(prefix);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Clear the entire cache
|
|
195
|
+
*/
|
|
196
|
+
async clear(): Promise<void> {
|
|
197
|
+
this.cache.clear();
|
|
198
|
+
this.prefixIndex.clear();
|
|
199
|
+
this.hitCount = 0;
|
|
200
|
+
this.missCount = 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Get cache performance metrics
|
|
205
|
+
* @returns Current cache metrics
|
|
206
|
+
*/
|
|
207
|
+
metrics(): CacheMetrics {
|
|
208
|
+
const total = this.hitCount + this.missCount;
|
|
209
|
+
return {
|
|
210
|
+
hits: this.hitCount,
|
|
211
|
+
misses: this.missCount,
|
|
212
|
+
size: this.cache.size,
|
|
213
|
+
sizeBytes: this.cache.calculatedSize ?? 0,
|
|
214
|
+
hitRate: total > 0 ? (this.hitCount / total) * 100 : 0,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Get the number of items in the cache
|
|
220
|
+
*/
|
|
221
|
+
get size(): number {
|
|
222
|
+
return this.cache.size;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Get all keys in the cache
|
|
227
|
+
*/
|
|
228
|
+
keys(): IterableIterator<string> {
|
|
229
|
+
return this.cache.keys();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Default singleton cache instance
|
|
235
|
+
*/
|
|
236
|
+
export const defaultCache = new LLMCache();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache module exports
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export { LLMCache, defaultCache } from './cache.js';
|
|
6
|
+
export type { CacheConfig, CacheMetrics } from './cache.js';
|
|
7
|
+
export { generateCacheKey, deterministicStringify, getSchemaHash } from './cache-key.js';
|
|
8
|
+
export type { CacheKeyInputs } from './cache-key.js';
|