@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,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache hook utilities.
|
|
3
|
+
*
|
|
4
|
+
* Provides cache hooks that replace the cache middleware functionality
|
|
5
|
+
* using the unified hook system.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Default in-memory cache store implementation with LRU eviction.
|
|
11
|
+
*
|
|
12
|
+
* @category Hooks
|
|
13
|
+
*/
|
|
14
|
+
export class InMemoryCacheStore {
|
|
15
|
+
cache = new Map();
|
|
16
|
+
maxSize;
|
|
17
|
+
constructor(maxSize = 100) {
|
|
18
|
+
this.maxSize = maxSize;
|
|
19
|
+
}
|
|
20
|
+
get(key) {
|
|
21
|
+
return this.cache.get(key);
|
|
22
|
+
}
|
|
23
|
+
set(key, value) {
|
|
24
|
+
// LRU eviction: remove oldest entry if at capacity
|
|
25
|
+
if (this.cache.size >= this.maxSize) {
|
|
26
|
+
const firstKey = this.cache.keys().next().value;
|
|
27
|
+
if (firstKey !== undefined) {
|
|
28
|
+
this.cache.delete(firstKey);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
this.cache.set(key, value);
|
|
32
|
+
}
|
|
33
|
+
delete(key) {
|
|
34
|
+
return this.cache.delete(key);
|
|
35
|
+
}
|
|
36
|
+
clear() {
|
|
37
|
+
this.cache.clear();
|
|
38
|
+
}
|
|
39
|
+
/** Get current cache size */
|
|
40
|
+
get size() {
|
|
41
|
+
return this.cache.size;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Default cache key generator.
|
|
46
|
+
*
|
|
47
|
+
* Creates a stable key from generation options including model identifier.
|
|
48
|
+
* Addresses the cache key issue mentioned in CODE_REVIEW.md.
|
|
49
|
+
*/
|
|
50
|
+
function defaultKeyGenerator(input, context) {
|
|
51
|
+
const opts = input.options;
|
|
52
|
+
// Extract model identifier from agent context
|
|
53
|
+
// The AI SDK LanguageModel may have modelId as a property or be a string
|
|
54
|
+
const model = context?.agent?.options?.model;
|
|
55
|
+
let modelId = "unknown";
|
|
56
|
+
if (model) {
|
|
57
|
+
if (typeof model === "string") {
|
|
58
|
+
modelId = model;
|
|
59
|
+
}
|
|
60
|
+
else if ("modelId" in model && typeof model.modelId === "string") {
|
|
61
|
+
modelId = model.modelId;
|
|
62
|
+
}
|
|
63
|
+
else if ("specificationVersion" in model) {
|
|
64
|
+
// For LanguageModel objects, try to extract identifier from provider
|
|
65
|
+
modelId = String(model);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const keyData = {
|
|
69
|
+
// Include model to differentiate between different models
|
|
70
|
+
model: modelId,
|
|
71
|
+
// Include session to differentiate conversations
|
|
72
|
+
session: input.session_id,
|
|
73
|
+
messages: opts.messages,
|
|
74
|
+
temperature: opts.temperature,
|
|
75
|
+
maxTokens: opts.maxTokens,
|
|
76
|
+
};
|
|
77
|
+
return JSON.stringify(keyData);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Creates cache hooks for PreGenerate and PostGenerate events.
|
|
81
|
+
*
|
|
82
|
+
* The PreGenerate hook checks for cached results and returns them via
|
|
83
|
+
* `respondWith` to short-circuit generation. The PostGenerate hook stores
|
|
84
|
+
* results in the cache.
|
|
85
|
+
*
|
|
86
|
+
* This replaces the cache middleware with hook-based caching that works
|
|
87
|
+
* correctly with streaming (hooks fire at lifecycle boundaries).
|
|
88
|
+
*
|
|
89
|
+
* @param options - Configuration options
|
|
90
|
+
* @returns Array of two hooks: [PreGenerate cache check, PostGenerate cache store]
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const [cacheCheck, cacheStore] = createCacheHooks({
|
|
95
|
+
* ttl: 60000, // 1 minute
|
|
96
|
+
* maxSize: 50,
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* const agent = createAgent({
|
|
100
|
+
* model,
|
|
101
|
+
* hooks: {
|
|
102
|
+
* PreGenerate: [{ hooks: [cacheCheck] }],
|
|
103
|
+
* PostGenerate: [{ hooks: [cacheStore] }],
|
|
104
|
+
* },
|
|
105
|
+
* });
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* // Custom cache key based on last message
|
|
111
|
+
* const hooks = createCacheHooks({
|
|
112
|
+
* keyGenerator: (input) => {
|
|
113
|
+
* const lastMsg = input.options.messages?.[input.options.messages.length - 1];
|
|
114
|
+
* return JSON.stringify(lastMsg);
|
|
115
|
+
* },
|
|
116
|
+
* });
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // Conditional caching (only cache when no tools)
|
|
122
|
+
* const hooks = createCacheHooks({
|
|
123
|
+
* shouldCache: (input) => !input.options.tools || Object.keys(input.options.tools).length === 0,
|
|
124
|
+
* ttl: 300000,
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @category Hooks
|
|
129
|
+
*/
|
|
130
|
+
export function createCacheHooks(options = {}) {
|
|
131
|
+
const { ttl = 300000, // 5 minutes
|
|
132
|
+
maxSize = 100, keyGenerator = defaultKeyGenerator, store = new InMemoryCacheStore(maxSize), shouldCache = () => true, } = options;
|
|
133
|
+
// PreGenerate: Check cache and short-circuit if hit
|
|
134
|
+
const cacheCheck = async (input, _toolUseId, context) => {
|
|
135
|
+
if (input.hook_event_name !== "PreGenerate")
|
|
136
|
+
return {};
|
|
137
|
+
const preGenInput = input;
|
|
138
|
+
// Check if this request should be cached
|
|
139
|
+
if (!shouldCache(preGenInput)) {
|
|
140
|
+
return {};
|
|
141
|
+
}
|
|
142
|
+
const key = keyGenerator(preGenInput, context);
|
|
143
|
+
// Check cache
|
|
144
|
+
const cached = await store.get(key);
|
|
145
|
+
if (cached && Date.now() - cached.timestamp < ttl) {
|
|
146
|
+
// Cache hit - return cached result via respondWith
|
|
147
|
+
return {
|
|
148
|
+
hookSpecificOutput: {
|
|
149
|
+
hookEventName: "PreGenerate",
|
|
150
|
+
respondWith: cached.result,
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
// Cache miss - continue with generation
|
|
155
|
+
return {};
|
|
156
|
+
};
|
|
157
|
+
// PostGenerate: Store result in cache
|
|
158
|
+
const cacheStore = async (input, _toolUseId, context) => {
|
|
159
|
+
if (input.hook_event_name !== "PostGenerate")
|
|
160
|
+
return {};
|
|
161
|
+
const postGenInput = input;
|
|
162
|
+
// Check if this request should be cached
|
|
163
|
+
// Convert PostGenerateInput to PreGenerateInput format for key generation
|
|
164
|
+
const preGenFormat = {
|
|
165
|
+
hook_event_name: "PreGenerate",
|
|
166
|
+
session_id: postGenInput.session_id,
|
|
167
|
+
cwd: postGenInput.cwd,
|
|
168
|
+
options: postGenInput.options,
|
|
169
|
+
};
|
|
170
|
+
if (!shouldCache(preGenFormat)) {
|
|
171
|
+
return {};
|
|
172
|
+
}
|
|
173
|
+
const key = keyGenerator(preGenFormat, context);
|
|
174
|
+
// Store result in cache
|
|
175
|
+
await store.set(key, {
|
|
176
|
+
result: postGenInput.result,
|
|
177
|
+
timestamp: Date.now(),
|
|
178
|
+
});
|
|
179
|
+
return {};
|
|
180
|
+
};
|
|
181
|
+
return [cacheCheck, cacheStore];
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Creates managed cache hooks with programmatic cache control.
|
|
185
|
+
*
|
|
186
|
+
* Returns hooks along with functions to clear cache, delete entries,
|
|
187
|
+
* and get statistics.
|
|
188
|
+
*
|
|
189
|
+
* @param options - Configuration options
|
|
190
|
+
* @returns Object with hooks and cache control functions
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* const { hooks, clearCache, getStats } = createManagedCacheHooks();
|
|
195
|
+
*
|
|
196
|
+
* const agent = createAgent({
|
|
197
|
+
* model,
|
|
198
|
+
* hooks: {
|
|
199
|
+
* PreGenerate: [{ hooks: [hooks[0]] }],
|
|
200
|
+
* PostGenerate: [{ hooks: [hooks[1]] }],
|
|
201
|
+
* },
|
|
202
|
+
* });
|
|
203
|
+
*
|
|
204
|
+
* // Clear cache when needed
|
|
205
|
+
* await clearCache();
|
|
206
|
+
*
|
|
207
|
+
* // Get cache statistics
|
|
208
|
+
* const { size, hits, misses } = getStats();
|
|
209
|
+
* console.log(`Cache hit rate: ${hits / (hits + misses)}`);
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @category Hooks
|
|
213
|
+
*/
|
|
214
|
+
export function createManagedCacheHooks(options = {}) {
|
|
215
|
+
const { ttl = 300000, maxSize = 100, keyGenerator = defaultKeyGenerator, store = new InMemoryCacheStore(maxSize), shouldCache = () => true, } = options;
|
|
216
|
+
let hits = 0;
|
|
217
|
+
let misses = 0;
|
|
218
|
+
// PreGenerate: Check cache with stats tracking
|
|
219
|
+
const cacheCheck = async (input, _toolUseId, context) => {
|
|
220
|
+
if (input.hook_event_name !== "PreGenerate")
|
|
221
|
+
return {};
|
|
222
|
+
const preGenInput = input;
|
|
223
|
+
if (!shouldCache(preGenInput)) {
|
|
224
|
+
return {};
|
|
225
|
+
}
|
|
226
|
+
const key = keyGenerator(preGenInput, context);
|
|
227
|
+
const cached = await store.get(key);
|
|
228
|
+
if (cached && Date.now() - cached.timestamp < ttl) {
|
|
229
|
+
hits++;
|
|
230
|
+
return {
|
|
231
|
+
hookSpecificOutput: {
|
|
232
|
+
hookEventName: "PreGenerate",
|
|
233
|
+
respondWith: cached.result,
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
misses++;
|
|
238
|
+
return {};
|
|
239
|
+
};
|
|
240
|
+
// PostGenerate: Store result
|
|
241
|
+
const cacheStore = async (input, _toolUseId, context) => {
|
|
242
|
+
if (input.hook_event_name !== "PostGenerate")
|
|
243
|
+
return {};
|
|
244
|
+
const postGenInput = input;
|
|
245
|
+
// Convert PostGenerateInput to PreGenerateInput format for key generation
|
|
246
|
+
const preGenFormat = {
|
|
247
|
+
hook_event_name: "PreGenerate",
|
|
248
|
+
session_id: postGenInput.session_id,
|
|
249
|
+
cwd: postGenInput.cwd,
|
|
250
|
+
options: postGenInput.options,
|
|
251
|
+
};
|
|
252
|
+
if (!shouldCache(preGenFormat)) {
|
|
253
|
+
return {};
|
|
254
|
+
}
|
|
255
|
+
const key = keyGenerator(preGenFormat, context);
|
|
256
|
+
await store.set(key, {
|
|
257
|
+
result: postGenInput.result,
|
|
258
|
+
timestamp: Date.now(),
|
|
259
|
+
});
|
|
260
|
+
return {};
|
|
261
|
+
};
|
|
262
|
+
return {
|
|
263
|
+
hooks: [cacheCheck, cacheStore],
|
|
264
|
+
clearCache: () => store.clear(),
|
|
265
|
+
deleteEntry: (key) => store.delete(key),
|
|
266
|
+
getStats: () => ({
|
|
267
|
+
size: store instanceof InMemoryCacheStore ? store.size : -1,
|
|
268
|
+
hits,
|
|
269
|
+
misses,
|
|
270
|
+
}),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../src/hooks/cache.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAwCH;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;IACrB,KAAK,GAAG,IAAI,GAAG,EAAsB,CAAC;IACtC,OAAO,CAAS;IAExB,YAAY,OAAO,GAAG,GAAG;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,GAAG,CAAC,GAAW;QACb,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC7B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Ae,EAAE,aAAa;YAC9B,UAAU,EAAE,YAAY,CAAC,UAAU;YACnC,GAAG,EAAE,YAAY,CAAC,GAAG;YACrB,OAAO,EAAE,YAAY,CAAC,OAAO;SAC9B,CAAC;QAEF,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,EAAE,CAAC;YAC/B,OAAO,EAAE,CAAC;QACZ,CAAC;QAED,MAAM,GAAG,GAAG,YAAY,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;QAChD,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;YACnB,MAAM,EAAE,YAAY,CAAC,MAAM;YAC3B,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC,CAAC;QAEH,OAAO,EAAE,CAAC;IACZ,CAAC,CAAC;IAEF,OAAO;QACL,KAAK,EAAE,CAAC,UAAU,EAAE,UAAU,CAAC;QAC/B,UAAU,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE;QAC/B,WAAW,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC;QAC/C,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAC;YACf,IAAI,EAAE,KAAK,YAAY,kBAAkB,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;YAC3D,IAAI;YACJ,MAAM;SACP,CAAC;KACH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Guardrails hook utilities.
|
|
3
|
+
*
|
|
4
|
+
* Provides content filtering and safety hooks using the unified hook system.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import type { HookCallback, PostGenerateInput, PreGenerateInput } from "../types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Options for creating guardrails hooks.
|
|
11
|
+
*
|
|
12
|
+
* @category Hooks
|
|
13
|
+
*/
|
|
14
|
+
export interface GuardrailsHooksOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Regex patterns to block in input (user messages).
|
|
17
|
+
* If matched, generation is denied.
|
|
18
|
+
*/
|
|
19
|
+
blockedInputPatterns?: RegExp[];
|
|
20
|
+
/**
|
|
21
|
+
* Regex patterns to filter in output (model responses).
|
|
22
|
+
* If matched, output is replaced with filterMessage.
|
|
23
|
+
*/
|
|
24
|
+
blockedOutputPatterns?: RegExp[];
|
|
25
|
+
/**
|
|
26
|
+
* Message to show when input is blocked.
|
|
27
|
+
* @defaultValue "Request blocked by content policy"
|
|
28
|
+
*/
|
|
29
|
+
blockedInputMessage?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Message to replace filtered output with.
|
|
32
|
+
* @defaultValue "[Content filtered]"
|
|
33
|
+
*/
|
|
34
|
+
filteredOutputMessage?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Custom input validation function.
|
|
37
|
+
* Throw error to block, or return options to allow/transform.
|
|
38
|
+
*/
|
|
39
|
+
checkInput?: (input: PreGenerateInput) => PreGenerateInput | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Custom output validation function.
|
|
42
|
+
* Return modified result to filter/transform output.
|
|
43
|
+
*/
|
|
44
|
+
checkOutput?: (input: PostGenerateInput) => PostGenerateInput["result"] | undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Creates guardrails hooks for input and output content filtering.
|
|
48
|
+
*
|
|
49
|
+
* The PreGenerate hook blocks requests matching input patterns.
|
|
50
|
+
* The PostGenerate hook filters output matching output patterns.
|
|
51
|
+
*
|
|
52
|
+
* This replaces guardrails middleware with hook-based filtering that
|
|
53
|
+
* works correctly with the unified hook system.
|
|
54
|
+
*
|
|
55
|
+
* @param options - Configuration options
|
|
56
|
+
* @returns Array of two hooks: [PreGenerate input filter, PostGenerate output filter]
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const [inputFilter, outputFilter] = createGuardrailsHooks({
|
|
61
|
+
* blockedInputPatterns: [
|
|
62
|
+
* /password/i,
|
|
63
|
+
* /api.?key/i,
|
|
64
|
+
* /secret/i,
|
|
65
|
+
* ],
|
|
66
|
+
* blockedOutputPatterns: [
|
|
67
|
+
* /\b\d{16}\b/, // Credit card numbers
|
|
68
|
+
* /\b\d{3}-\d{2}-\d{4}\b/, // SSN
|
|
69
|
+
* ],
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* const agent = createAgent({
|
|
73
|
+
* model,
|
|
74
|
+
* hooks: {
|
|
75
|
+
* PreGenerate: [{ hooks: [inputFilter] }],
|
|
76
|
+
* PostGenerate: [{ hooks: [outputFilter] }],
|
|
77
|
+
* },
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // Custom validation with transformation
|
|
84
|
+
* const hooks = createGuardrailsHooks({
|
|
85
|
+
* checkInput: (input) => {
|
|
86
|
+
* // Allow but transform: remove PII from messages
|
|
87
|
+
* const cleaned = cleanPII(input.options.messages);
|
|
88
|
+
* return {
|
|
89
|
+
* ...input,
|
|
90
|
+
* options: { ...input.options, messages: cleaned },
|
|
91
|
+
* };
|
|
92
|
+
* },
|
|
93
|
+
* checkOutput: (input) => {
|
|
94
|
+
* // Filter harmful content
|
|
95
|
+
* if (isHarmful(input.result.text)) {
|
|
96
|
+
* return { ...input.result, text: "[Content filtered for safety]" };
|
|
97
|
+
* }
|
|
98
|
+
* },
|
|
99
|
+
* });
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @category Hooks
|
|
103
|
+
*/
|
|
104
|
+
export declare function createGuardrailsHooks(options?: GuardrailsHooksOptions): [HookCallback, HookCallback];
|
|
105
|
+
/**
|
|
106
|
+
* Creates guardrails hooks with statistics tracking.
|
|
107
|
+
*
|
|
108
|
+
* Returns hooks along with functions to get filtering statistics.
|
|
109
|
+
*
|
|
110
|
+
* @param options - Configuration options
|
|
111
|
+
* @returns Object with hooks and statistics getter
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const { hooks, getStats } = createManagedGuardrailsHooks({
|
|
116
|
+
* blockedInputPatterns: [/password/i, /secret/i],
|
|
117
|
+
* blockedOutputPatterns: [/\b\d{16}\b/],
|
|
118
|
+
* });
|
|
119
|
+
*
|
|
120
|
+
* const agent = createAgent({
|
|
121
|
+
* model,
|
|
122
|
+
* hooks: {
|
|
123
|
+
* PreGenerate: [{ hooks: [hooks[0]] }],
|
|
124
|
+
* PostGenerate: [{ hooks: [hooks[1]] }],
|
|
125
|
+
* },
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* // Check statistics
|
|
129
|
+
* const stats = getStats();
|
|
130
|
+
* console.log(`Blocked inputs: ${stats.blockedInputs}`);
|
|
131
|
+
* console.log(`Filtered outputs: ${stats.filteredOutputs}`);
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* @category Hooks
|
|
135
|
+
*/
|
|
136
|
+
export declare function createManagedGuardrailsHooks(options?: GuardrailsHooksOptions): {
|
|
137
|
+
hooks: [HookCallback, HookCallback];
|
|
138
|
+
getStats: () => {
|
|
139
|
+
blockedInputs: number;
|
|
140
|
+
filteredOutputs: number;
|
|
141
|
+
totalInputs: number;
|
|
142
|
+
totalOutputs: number;
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
//# sourceMappingURL=guardrails.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guardrails.d.ts","sourceRoot":"","sources":["../../src/hooks/guardrails.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAErF;;;;GAIG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,EA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