@mondaydotcomorg/atp-runtime 0.17.14
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/README.md +389 -0
- package/dist/approval/handler.d.ts +12 -0
- package/dist/approval/handler.d.ts.map +1 -0
- package/dist/approval/handler.js +17 -0
- package/dist/approval/handler.js.map +1 -0
- package/dist/approval/index.d.ts +17 -0
- package/dist/approval/index.d.ts.map +1 -0
- package/dist/approval/index.js +94 -0
- package/dist/approval/index.js.map +1 -0
- package/dist/approval/types.d.ts +21 -0
- package/dist/approval/types.d.ts.map +1 -0
- package/dist/approval/types.js +5 -0
- package/dist/approval/types.js.map +1 -0
- package/dist/cache/backends.d.ts +39 -0
- package/dist/cache/backends.d.ts.map +1 -0
- package/dist/cache/backends.js +167 -0
- package/dist/cache/backends.js.map +1 -0
- package/dist/cache/index.d.ts +32 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +103 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/types.d.ts +20 -0
- package/dist/cache/types.d.ts.map +1 -0
- package/dist/cache/types.js +2 -0
- package/dist/cache/types.js.map +1 -0
- package/dist/embedding/index.d.ts +39 -0
- package/dist/embedding/index.d.ts.map +1 -0
- package/dist/embedding/index.js +162 -0
- package/dist/embedding/index.js.map +1 -0
- package/dist/embedding/types.d.ts +28 -0
- package/dist/embedding/types.d.ts.map +1 -0
- package/dist/embedding/types.js +5 -0
- package/dist/embedding/types.js.map +1 -0
- package/dist/embedding/utils.d.ts +11 -0
- package/dist/embedding/utils.d.ts.map +1 -0
- package/dist/embedding/utils.js +30 -0
- package/dist/embedding/utils.js.map +1 -0
- package/dist/embedding/vector-store.d.ts +64 -0
- package/dist/embedding/vector-store.d.ts.map +1 -0
- package/dist/embedding/vector-store.js +142 -0
- package/dist/embedding/vector-store.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/callback.d.ts +13 -0
- package/dist/llm/callback.d.ts.map +1 -0
- package/dist/llm/callback.js +19 -0
- package/dist/llm/callback.js.map +1 -0
- package/dist/llm/index.d.ts +29 -0
- package/dist/llm/index.d.ts.map +1 -0
- package/dist/llm/index.js +118 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/replay.d.ts +47 -0
- package/dist/llm/replay.d.ts.map +1 -0
- package/dist/llm/replay.js +114 -0
- package/dist/llm/replay.js.map +1 -0
- package/dist/llm/types.d.ts +24 -0
- package/dist/llm/types.d.ts.map +1 -0
- package/dist/llm/types.js +2 -0
- package/dist/llm/types.js.map +1 -0
- package/dist/log/index.d.ts +12 -0
- package/dist/log/index.d.ts.map +1 -0
- package/dist/log/index.js +166 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/types.d.ts +19 -0
- package/dist/log/types.d.ts.map +1 -0
- package/dist/log/types.js +5 -0
- package/dist/log/types.js.map +1 -0
- package/dist/metadata/decorators.d.ts +27 -0
- package/dist/metadata/decorators.d.ts.map +1 -0
- package/dist/metadata/decorators.js +38 -0
- package/dist/metadata/decorators.js.map +1 -0
- package/dist/metadata/generated.d.ts +18 -0
- package/dist/metadata/generated.d.ts.map +1 -0
- package/dist/metadata/generated.js +290 -0
- package/dist/metadata/generated.js.map +1 -0
- package/dist/metadata/index.d.ts +11 -0
- package/dist/metadata/index.d.ts.map +1 -0
- package/dist/metadata/index.js +45 -0
- package/dist/metadata/index.js.map +1 -0
- package/dist/metadata/types.d.ts +22 -0
- package/dist/metadata/types.d.ts.map +1 -0
- package/dist/metadata/types.js +6 -0
- package/dist/metadata/types.js.map +1 -0
- package/dist/pause/index.d.ts +11 -0
- package/dist/pause/index.d.ts.map +1 -0
- package/dist/pause/index.js +15 -0
- package/dist/pause/index.js.map +1 -0
- package/dist/pause/types.d.ts +46 -0
- package/dist/pause/types.d.ts.map +1 -0
- package/dist/pause/types.js +57 -0
- package/dist/pause/types.js.map +1 -0
- package/dist/progress/index.d.ts +19 -0
- package/dist/progress/index.d.ts.map +1 -0
- package/dist/progress/index.js +61 -0
- package/dist/progress/index.js.map +1 -0
- package/dist/progress/types.d.ts +7 -0
- package/dist/progress/types.d.ts.map +1 -0
- package/dist/progress/types.js +2 -0
- package/dist/progress/types.js.map +1 -0
- package/dist/registry.d.ts +16 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +16 -0
- package/dist/registry.js.map +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +31 -0
- package/dist/utils.js.map +1 -0
- package/package.json +51 -0
- package/src/approval/handler.ts +26 -0
- package/src/approval/index.ts +95 -0
- package/src/approval/types.ts +23 -0
- package/src/cache/backends.ts +196 -0
- package/src/cache/index.ts +74 -0
- package/src/cache/types.ts +20 -0
- package/src/embedding/index.ts +153 -0
- package/src/embedding/types.ts +31 -0
- package/src/embedding/utils.ts +34 -0
- package/src/embedding/vector-store.ts +164 -0
- package/src/index.ts +35 -0
- package/src/llm/callback.ts +24 -0
- package/src/llm/index.ts +121 -0
- package/src/llm/replay.ts +141 -0
- package/src/llm/types.ts +29 -0
- package/src/log/index.ts +167 -0
- package/src/log/types.ts +21 -0
- package/src/metadata/decorators.ts +44 -0
- package/src/metadata/generated.ts +293 -0
- package/src/metadata/index.ts +61 -0
- package/src/metadata/types.ts +24 -0
- package/src/pause/index.ts +28 -0
- package/src/pause/types.ts +57 -0
- package/src/progress/index.ts +50 -0
- package/src/progress/types.ts +6 -0
- package/src/registry.ts +22 -0
- package/src/utils.ts +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
# @mondaydotcomorg/atp-runtime
|
|
2
|
+
|
|
3
|
+
Runtime APIs available to agents during code execution (LLM, embedding, approval, cache, logging, progress).
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides the `atp.*` runtime APIs that agents can use when executing code on ATP servers. These APIs enable LLM calls, embeddings, approvals, caching, logging, and progress reporting.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @mondaydotcomorg/atp-runtime
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Architecture
|
|
16
|
+
|
|
17
|
+
```mermaid
|
|
18
|
+
graph TB
|
|
19
|
+
Runtime[Runtime Registry] --> LLM[atp.llm.*]
|
|
20
|
+
Runtime --> Embedding[atp.embedding.*]
|
|
21
|
+
Runtime --> Approval[atp.approval.*]
|
|
22
|
+
Runtime --> Cache[atp.cache.*]
|
|
23
|
+
Runtime --> Log[atp.log.*]
|
|
24
|
+
Runtime --> Progress[atp.progress.*]
|
|
25
|
+
|
|
26
|
+
LLM --> Pause[Pause Mechanism]
|
|
27
|
+
Approval --> Pause
|
|
28
|
+
Embedding --> Pause
|
|
29
|
+
|
|
30
|
+
Cache --> Provider[CacheProvider]
|
|
31
|
+
Log --> Logger[Logger]
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Runtime APIs
|
|
35
|
+
|
|
36
|
+
### atp.llm.\*
|
|
37
|
+
|
|
38
|
+
LLM operations that pause execution and route to client-provided LLM.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
// Simple LLM call
|
|
42
|
+
const response = await atp.llm.call({
|
|
43
|
+
prompt: 'What is the capital of France?',
|
|
44
|
+
model: 'gpt-4',
|
|
45
|
+
temperature: 0.7,
|
|
46
|
+
systemPrompt: 'You are a helpful assistant',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Extract structured data
|
|
50
|
+
const user = await atp.llm.extract({
|
|
51
|
+
prompt: 'Extract user info: John Doe, john@example.com',
|
|
52
|
+
schema: {
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {
|
|
55
|
+
name: { type: 'string' },
|
|
56
|
+
email: { type: 'string' },
|
|
57
|
+
},
|
|
58
|
+
required: ['name', 'email'],
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
// Returns: { name: 'John Doe', email: 'john@example.com' }
|
|
62
|
+
|
|
63
|
+
// Classify text
|
|
64
|
+
const category = await atp.llm.classify({
|
|
65
|
+
text: 'This product is amazing!',
|
|
66
|
+
categories: ['positive', 'negative', 'neutral'],
|
|
67
|
+
});
|
|
68
|
+
// Returns: 'positive'
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### atp.embedding.\*
|
|
72
|
+
|
|
73
|
+
Embedding operations for semantic search.
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
// Generate and store embedding
|
|
77
|
+
const embeddingId = await atp.embedding.embed('Important document content');
|
|
78
|
+
|
|
79
|
+
// Search by similarity
|
|
80
|
+
const results = await atp.embedding.search('find similar documents', {
|
|
81
|
+
topK: 5,
|
|
82
|
+
minSimilarity: 0.7,
|
|
83
|
+
});
|
|
84
|
+
// Returns: [{ id: string, text: string, similarity: number }, ...]
|
|
85
|
+
|
|
86
|
+
// Calculate similarity between two embeddings
|
|
87
|
+
const similarity = await atp.embedding.similarity(vec1, vec2);
|
|
88
|
+
|
|
89
|
+
// Get all embeddings
|
|
90
|
+
const all = await atp.embedding.getAll();
|
|
91
|
+
|
|
92
|
+
// Count embeddings
|
|
93
|
+
const count = await atp.embedding.count();
|
|
94
|
+
|
|
95
|
+
// Clear all embeddings
|
|
96
|
+
await atp.embedding.clear();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### atp.approval.\*
|
|
100
|
+
|
|
101
|
+
Request human approval during execution.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
// Request approval
|
|
105
|
+
const result = await atp.approval.request('Delete all user data?', {
|
|
106
|
+
critical: true,
|
|
107
|
+
affectedUsers: 150,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
if (result.approved) {
|
|
111
|
+
// User approved, proceed
|
|
112
|
+
await deleteData();
|
|
113
|
+
} else {
|
|
114
|
+
// User denied
|
|
115
|
+
return { cancelled: true, reason: result.response };
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### atp.cache.\*
|
|
120
|
+
|
|
121
|
+
Cache data with TTL support.
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
// Set cache value
|
|
125
|
+
await atp.cache.set('user:123', userData, 3600); // TTL in seconds
|
|
126
|
+
|
|
127
|
+
// Get cache value
|
|
128
|
+
const cached = await atp.cache.get('user:123');
|
|
129
|
+
|
|
130
|
+
// Check if key exists
|
|
131
|
+
const exists = await atp.cache.has('user:123');
|
|
132
|
+
|
|
133
|
+
// Delete cache key
|
|
134
|
+
await atp.cache.delete('user:123');
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### atp.log.\*
|
|
138
|
+
|
|
139
|
+
Structured logging with multiple levels.
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
// Log levels
|
|
143
|
+
atp.log.trace('Detailed trace', { requestId: '123' });
|
|
144
|
+
atp.log.debug('Debug info', { state });
|
|
145
|
+
atp.log.info('User logged in', { userId: '456' });
|
|
146
|
+
atp.log.warn('Deprecated API used', { api: 'v1' });
|
|
147
|
+
atp.log.error('Failed to connect', { error, retries: 3 });
|
|
148
|
+
atp.log.fatal('System crash', { reason });
|
|
149
|
+
|
|
150
|
+
// Logs include context automatically:
|
|
151
|
+
// - executionId
|
|
152
|
+
// - timestamp
|
|
153
|
+
// - level
|
|
154
|
+
// - message
|
|
155
|
+
// - metadata
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### atp.progress.\*
|
|
159
|
+
|
|
160
|
+
Report progress for long-running operations.
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
// Report progress
|
|
164
|
+
atp.progress.report({
|
|
165
|
+
current: 5,
|
|
166
|
+
total: 10,
|
|
167
|
+
message: 'Processing items...',
|
|
168
|
+
metadata: {
|
|
169
|
+
itemsPerSecond: 2.5,
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// Progress updates are sent to client in real-time
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Usage in Agent Code
|
|
177
|
+
|
|
178
|
+
When agents execute code on ATP server, these APIs are automatically available:
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
// Agent-generated code
|
|
182
|
+
const items = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
|
|
183
|
+
const results = [];
|
|
184
|
+
|
|
185
|
+
atp.log.info('Starting fruit analysis', { count: items.length });
|
|
186
|
+
|
|
187
|
+
for (let i = 0; i < items.length; i++) {
|
|
188
|
+
atp.progress.report({
|
|
189
|
+
current: i + 1,
|
|
190
|
+
total: items.length,
|
|
191
|
+
message: `Processing ${items[i]}`,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Call LLM for each item
|
|
195
|
+
const analysis = await atp.llm.call({
|
|
196
|
+
prompt: `Analyze this fruit: ${items[i]}`,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// Store embedding for semantic search
|
|
200
|
+
const embeddingId = await atp.embedding.embed(analysis);
|
|
201
|
+
|
|
202
|
+
// Cache result
|
|
203
|
+
await atp.cache.set(`analysis:${items[i]}`, analysis, 3600);
|
|
204
|
+
|
|
205
|
+
results.push({ fruit: items[i], analysis, embeddingId });
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Request approval before returning
|
|
209
|
+
const approval = await atp.approval.request('Analysis complete. Proceed with storage?', {
|
|
210
|
+
resultCount: results.length,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
if (!approval.approved) {
|
|
214
|
+
atp.log.warn('User rejected storage');
|
|
215
|
+
return { cancelled: true };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
atp.log.info('Analysis complete', { results: results.length });
|
|
219
|
+
return results;
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Pause/Resume Mechanism
|
|
223
|
+
|
|
224
|
+
```mermaid
|
|
225
|
+
sequenceDiagram
|
|
226
|
+
participant Code
|
|
227
|
+
participant Runtime
|
|
228
|
+
participant Server
|
|
229
|
+
participant Client
|
|
230
|
+
|
|
231
|
+
Code->>Runtime: atp.llm.call()
|
|
232
|
+
Runtime->>Server: Pause execution
|
|
233
|
+
Server->>Client: Request LLM callback
|
|
234
|
+
Client->>LLM: Call LLM API
|
|
235
|
+
LLM-->>Client: Response
|
|
236
|
+
Client->>Server: Resume with result
|
|
237
|
+
Server->>Runtime: Restore state
|
|
238
|
+
Runtime-->>Code: Return LLM result
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Initialization
|
|
242
|
+
|
|
243
|
+
Runtime APIs are automatically initialized by the ATP server. For standalone use:
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import {
|
|
247
|
+
setClientLLMCallback,
|
|
248
|
+
initializeCache,
|
|
249
|
+
initializeApproval,
|
|
250
|
+
initializeVectorStore,
|
|
251
|
+
initializeLogger,
|
|
252
|
+
} from '@mondaydotcomorg/atp-runtime';
|
|
253
|
+
|
|
254
|
+
// Setup LLM callback
|
|
255
|
+
setClientLLMCallback({
|
|
256
|
+
call: async (prompt, options) => {
|
|
257
|
+
// Implement LLM call
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// Setup cache
|
|
262
|
+
initializeCache(cacheProvider);
|
|
263
|
+
|
|
264
|
+
// Setup approval
|
|
265
|
+
initializeApproval({
|
|
266
|
+
request: async (message, context) => {
|
|
267
|
+
// Implement approval
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// Setup embeddings
|
|
272
|
+
initializeVectorStore(embeddingHandler);
|
|
273
|
+
|
|
274
|
+
// Setup logger
|
|
275
|
+
initializeLogger({
|
|
276
|
+
level: 'info',
|
|
277
|
+
pretty: true,
|
|
278
|
+
});
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Replay Mode
|
|
282
|
+
|
|
283
|
+
For deterministic execution and testing:
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
import { setReplayMode } from '@mondaydotcomorg/atp-runtime';
|
|
287
|
+
|
|
288
|
+
// Enable replay mode with cached results
|
|
289
|
+
setReplayMode(true);
|
|
290
|
+
|
|
291
|
+
// LLM calls will return cached results instead of pausing
|
|
292
|
+
const result = await atp.llm.call({ prompt: 'Hello' });
|
|
293
|
+
// Returns cached response from previous execution
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## Type Definitions
|
|
297
|
+
|
|
298
|
+
The runtime exports TypeScript definitions for all APIs:
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import type {
|
|
302
|
+
LLMCallOptions,
|
|
303
|
+
LLMExtractOptions,
|
|
304
|
+
LLMClassifyOptions,
|
|
305
|
+
EmbeddingSearchOptions,
|
|
306
|
+
EmbeddingSearchResult,
|
|
307
|
+
ApprovalRequest,
|
|
308
|
+
ApprovalResponse,
|
|
309
|
+
ProgressUpdate,
|
|
310
|
+
} from '@mondaydotcomorg/atp-runtime';
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
## Metadata Generation
|
|
314
|
+
|
|
315
|
+
Runtime APIs are decorated with metadata for automatic discovery:
|
|
316
|
+
|
|
317
|
+
```typescript
|
|
318
|
+
import { GENERATED_METADATA } from '@mondaydotcomorg/atp-runtime';
|
|
319
|
+
|
|
320
|
+
// Metadata includes:
|
|
321
|
+
// - API names and descriptions
|
|
322
|
+
// - Method signatures
|
|
323
|
+
// - Parameter types
|
|
324
|
+
// - Return types
|
|
325
|
+
|
|
326
|
+
console.log(GENERATED_METADATA);
|
|
327
|
+
// {
|
|
328
|
+
// apis: [
|
|
329
|
+
// {
|
|
330
|
+
// name: 'llm',
|
|
331
|
+
// description: 'LLM API - Large Language Model calls...',
|
|
332
|
+
// methods: [...]
|
|
333
|
+
// },
|
|
334
|
+
// ...
|
|
335
|
+
// ]
|
|
336
|
+
// }
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
## Error Handling
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
import { PauseExecutionError, isPauseError } from '@mondaydotcomorg/atp-runtime';
|
|
343
|
+
|
|
344
|
+
try {
|
|
345
|
+
const result = await atp.llm.call({ prompt: 'Hello' });
|
|
346
|
+
} catch (error) {
|
|
347
|
+
if (isPauseError(error)) {
|
|
348
|
+
// Execution paused for callback
|
|
349
|
+
console.log('Paused for:', error.callbackType);
|
|
350
|
+
} else {
|
|
351
|
+
// Other error
|
|
352
|
+
throw error;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
## Advanced Features
|
|
358
|
+
|
|
359
|
+
### Sequence Numbers
|
|
360
|
+
|
|
361
|
+
Track call order for replay:
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
import { getCallSequenceNumber } from '@mondaydotcomorg/atp-runtime';
|
|
365
|
+
|
|
366
|
+
const seq = getCallSequenceNumber();
|
|
367
|
+
console.log('Current sequence:', seq);
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
### Execution Context
|
|
371
|
+
|
|
372
|
+
Run code in specific execution context:
|
|
373
|
+
|
|
374
|
+
```typescript
|
|
375
|
+
import { runInExecutionContext } from '@mondaydotcomorg/atp-runtime';
|
|
376
|
+
|
|
377
|
+
runInExecutionContext('exec-123', () => {
|
|
378
|
+
// All atp.* calls use this execution ID
|
|
379
|
+
const result = await atp.llm.call({ prompt: 'Hello' });
|
|
380
|
+
});
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
## TypeScript Support
|
|
384
|
+
|
|
385
|
+
Full TypeScript definitions with strict typing.
|
|
386
|
+
|
|
387
|
+
## License
|
|
388
|
+
|
|
389
|
+
MIT
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*/
|
|
3
|
+
import type { ApprovalRequest, ApprovalResponse } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Initialize the approval system with a custom handler
|
|
6
|
+
*/
|
|
7
|
+
export declare function initializeApproval(handler: (request: ApprovalRequest) => Promise<ApprovalResponse>): void;
|
|
8
|
+
/**
|
|
9
|
+
* Get the current approval handler
|
|
10
|
+
*/
|
|
11
|
+
export declare function getApprovalHandler(): ((request: ApprovalRequest) => Promise<ApprovalResponse>) | null;
|
|
12
|
+
//# sourceMappingURL=handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/approval/handler.ts"],"names":[],"mappings":"AAAA;GACG;AACH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAOjE;;GAEG;AACH,wBAAgB,kBAAkB,CACjC,OAAO,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,OAAO,CAAC,gBAAgB,CAAC,GAC9D,IAAI,CAEN;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAC/B,CAAC,CAAC,OAAO,EAAE,eAAe,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC,GACzD,IAAI,CAEN"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global approval handler that must be set by the execution context
|
|
3
|
+
*/
|
|
4
|
+
let approvalHandler = null;
|
|
5
|
+
/**
|
|
6
|
+
* Initialize the approval system with a custom handler
|
|
7
|
+
*/
|
|
8
|
+
export function initializeApproval(handler) {
|
|
9
|
+
approvalHandler = handler;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Get the current approval handler
|
|
13
|
+
*/
|
|
14
|
+
export function getApprovalHandler() {
|
|
15
|
+
return approvalHandler;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/approval/handler.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,IAAI,eAAe,GAAqE,IAAI,CAAC;AAE7F;;GAEG;AACH,MAAM,UAAU,kBAAkB,CACjC,OAAgE;IAEhE,eAAe,GAAG,OAAO,CAAC;AAC3B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB;IAGjC,OAAO,eAAe,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ApprovalResponse } from './types';
|
|
2
|
+
export type { ApprovalRequest, ApprovalResponse, ApprovalSchema } from './types';
|
|
3
|
+
export { initializeApproval } from './handler.js';
|
|
4
|
+
/**
|
|
5
|
+
* Approval Runtime API
|
|
6
|
+
*
|
|
7
|
+
* Allows agents to request explicit human approval before proceeding with sensitive operations.
|
|
8
|
+
* This integrates with MCP's elicitation feature to request structured input from users.
|
|
9
|
+
*/
|
|
10
|
+
declare class ApprovalAPI {
|
|
11
|
+
/**
|
|
12
|
+
* Request approval from a human
|
|
13
|
+
*/
|
|
14
|
+
request(message: string, context?: Record<string, unknown>): Promise<ApprovalResponse>;
|
|
15
|
+
}
|
|
16
|
+
export declare const approval: ApprovalAPI;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/approval/index.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAmB,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAGjE,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD;;;;;GAKG;AACH,cACM,WAAW;IAChB;;OAEG;IAWG,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAsD5F;AAED,eAAO,MAAM,QAAQ,aAAoB,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Approval API - Clean refactored version with decorators and extracted modules
|
|
12
|
+
*
|
|
13
|
+
* Benefits:
|
|
14
|
+
* - No duplication between implementation and metadata
|
|
15
|
+
* - Types auto-detected from TypeScript signatures
|
|
16
|
+
* - Clean separation of concerns (handler, API)
|
|
17
|
+
*/
|
|
18
|
+
import { RuntimeAPI, RuntimeMethod } from '../metadata/decorators.js';
|
|
19
|
+
import { getApprovalHandler } from './handler.js';
|
|
20
|
+
import { pauseForCallback, CallbackType, ApprovalOperation } from '../pause/index.js';
|
|
21
|
+
import { nextSequenceNumber, getCachedResult, shouldPauseForClient } from '../llm/replay.js';
|
|
22
|
+
export { initializeApproval } from './handler.js';
|
|
23
|
+
/**
|
|
24
|
+
* Approval Runtime API
|
|
25
|
+
*
|
|
26
|
+
* Allows agents to request explicit human approval before proceeding with sensitive operations.
|
|
27
|
+
* This integrates with MCP's elicitation feature to request structured input from users.
|
|
28
|
+
*/
|
|
29
|
+
let ApprovalAPI = class ApprovalAPI {
|
|
30
|
+
/**
|
|
31
|
+
* Request approval from a human
|
|
32
|
+
*/
|
|
33
|
+
async request(message, context) {
|
|
34
|
+
const currentSequence = nextSequenceNumber();
|
|
35
|
+
const cachedResult = getCachedResult(currentSequence);
|
|
36
|
+
if (cachedResult !== undefined) {
|
|
37
|
+
return cachedResult;
|
|
38
|
+
}
|
|
39
|
+
if (shouldPauseForClient()) {
|
|
40
|
+
pauseForCallback(CallbackType.APPROVAL, ApprovalOperation.REQUEST, {
|
|
41
|
+
message,
|
|
42
|
+
context,
|
|
43
|
+
sequenceNumber: currentSequence,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const handler = getApprovalHandler();
|
|
47
|
+
if (!handler) {
|
|
48
|
+
throw new Error('Approval handler not configured. Human approval is required but no handler is set.');
|
|
49
|
+
}
|
|
50
|
+
const approvalRequest = {
|
|
51
|
+
message,
|
|
52
|
+
context,
|
|
53
|
+
timeout: 300000,
|
|
54
|
+
};
|
|
55
|
+
let timeoutId = null;
|
|
56
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
57
|
+
timeoutId = setTimeout(() => reject(new Error('Approval request timed out')), approvalRequest.timeout);
|
|
58
|
+
});
|
|
59
|
+
try {
|
|
60
|
+
const response = await Promise.race([handler(approvalRequest), timeoutPromise]);
|
|
61
|
+
if (timeoutId)
|
|
62
|
+
clearTimeout(timeoutId);
|
|
63
|
+
return {
|
|
64
|
+
...response,
|
|
65
|
+
timestamp: Date.now(),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (timeoutId)
|
|
70
|
+
clearTimeout(timeoutId);
|
|
71
|
+
throw new Error(`Approval request failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
__decorate([
|
|
76
|
+
RuntimeMethod('Request approval from a human', {
|
|
77
|
+
message: {
|
|
78
|
+
description: 'The message to display to the user',
|
|
79
|
+
},
|
|
80
|
+
context: {
|
|
81
|
+
description: 'Optional context information about what needs approval',
|
|
82
|
+
optional: true,
|
|
83
|
+
type: 'Record<string, unknown>',
|
|
84
|
+
},
|
|
85
|
+
}),
|
|
86
|
+
__metadata("design:type", Function),
|
|
87
|
+
__metadata("design:paramtypes", [String, Object]),
|
|
88
|
+
__metadata("design:returntype", Promise)
|
|
89
|
+
], ApprovalAPI.prototype, "request", null);
|
|
90
|
+
ApprovalAPI = __decorate([
|
|
91
|
+
RuntimeAPI('approval', 'Approval API - Request explicit human approval for sensitive operations')
|
|
92
|
+
], ApprovalAPI);
|
|
93
|
+
export const approval = new ApprovalAPI();
|
|
94
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/approval/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtF,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAG7F,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD;;;;;GAKG;AAEH,IAAM,WAAW,GAAjB,MAAM,WAAW;IAChB;;OAEG;IAWG,AAAN,KAAK,CAAC,OAAO,CAAC,OAAe,EAAE,OAAiC;QAC/D,MAAM,eAAe,GAAG,kBAAkB,EAAE,CAAC;QAE7C,MAAM,YAAY,GAAG,eAAe,CAAC,eAAe,CAAC,CAAC;QACtD,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;YAChC,OAAO,YAAgC,CAAC;QACzC,CAAC;QAED,IAAI,oBAAoB,EAAE,EAAE,CAAC;YAC5B,gBAAgB,CAAC,YAAY,CAAC,QAAQ,EAAE,iBAAiB,CAAC,OAAO,EAAE;gBAClE,OAAO;gBACP,OAAO;gBACP,cAAc,EAAE,eAAe;aAC/B,CAAC,CAAC;QACJ,CAAC;QAED,MAAM,OAAO,GAAG,kBAAkB,EAAE,CAAC;QAErC,IAAI,CAAC,OAAO,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CACd,oFAAoF,CACpF,CAAC;QACH,CAAC;QAED,MAAM,eAAe,GAAoB;YACxC,OAAO;YACP,OAAO;YACP,OAAO,EAAE,MAAM;SACf,CAAC;QAEF,IAAI,SAAS,GAA0B,IAAI,CAAC;QAC5C,MAAM,cAAc,GAAG,IAAI,OAAO,CAAmB,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE;YAClE,SAAS,GAAG,UAAU,CACrB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC,EACrD,eAAe,CAAC,OAAQ,CACxB,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC;YACJ,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,eAAe,CAAC,EAAE,cAAc,CAAC,CAAC,CAAC;YAEhF,IAAI,SAAS;gBAAE,YAAY,CAAC,SAAS,CAAC,CAAC;YAEvC,OAAO;gBACN,GAAG,QAAQ;gBACX,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;aACrB,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,SAAS;gBAAE,YAAY,CAAC,SAAS,CAAC,CAAC;YACvC,MAAM,IAAI,KAAK,CACd,4BAA4B,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,EAAE,CACtF,CAAC;QACH,CAAC;IACF,CAAC;CACD,CAAA;AAtDM;IAVL,aAAa,CAAC,+BAA+B,EAAE;QAC/C,OAAO,EAAE;YACR,WAAW,EAAE,oCAAoC;SACjD;QACD,OAAO,EAAE;YACR,WAAW,EAAE,wDAAwD;YACrE,QAAQ,EAAE,IAAI;YACd,IAAI,EAAE,yBAAyB;SAC/B;KACD,CAAC;;;;0CAsDD;AAnEI,WAAW;IADhB,UAAU,CAAC,UAAU,EAAE,yEAAyE,CAAC;GAC5F,WAAW,CAoEhB;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,IAAI,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Human Approval API Types
|
|
3
|
+
*/
|
|
4
|
+
export interface ApprovalRequest {
|
|
5
|
+
message: string;
|
|
6
|
+
context?: Record<string, unknown>;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
schema?: ApprovalSchema;
|
|
9
|
+
}
|
|
10
|
+
export interface ApprovalSchema {
|
|
11
|
+
type: 'boolean' | 'text' | 'choice' | 'structured';
|
|
12
|
+
choices?: string[];
|
|
13
|
+
structuredSchema?: Record<string, unknown>;
|
|
14
|
+
required?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface ApprovalResponse<T = unknown> {
|
|
17
|
+
approved: boolean;
|
|
18
|
+
response?: T;
|
|
19
|
+
timestamp: number;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/approval/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,eAAe;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,cAAc,CAAC;CACxB;AAED,MAAM,WAAW,cAAc;IAC9B,IAAI,EAAE,SAAS,GAAG,MAAM,GAAG,QAAQ,GAAG,YAAY,CAAC;IACnD,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC3C,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,OAAO;IAC5C,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,CAAC,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CAClB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/approval/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { CacheBackend, CacheConfig } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* In-memory cache implementation using node-cache
|
|
4
|
+
*/
|
|
5
|
+
export declare class MemoryCacheBackend implements CacheBackend {
|
|
6
|
+
private cache;
|
|
7
|
+
constructor(config?: {
|
|
8
|
+
maxKeys?: number;
|
|
9
|
+
defaultTTL?: number;
|
|
10
|
+
checkPeriod?: number;
|
|
11
|
+
});
|
|
12
|
+
get<T>(key: string): Promise<T | null>;
|
|
13
|
+
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
14
|
+
delete(key: string): Promise<void>;
|
|
15
|
+
has(key: string): Promise<boolean>;
|
|
16
|
+
clear(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Redis cache implementation (lazy-loaded only if configured)
|
|
20
|
+
*/
|
|
21
|
+
export declare class RedisCacheBackend implements CacheBackend {
|
|
22
|
+
private client;
|
|
23
|
+
private connected;
|
|
24
|
+
constructor(config: NonNullable<CacheConfig['redis']>);
|
|
25
|
+
get<T>(key: string): Promise<T | null>;
|
|
26
|
+
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
27
|
+
delete(key: string): Promise<void>;
|
|
28
|
+
has(key: string): Promise<boolean>;
|
|
29
|
+
clear(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Initializes the cache system with configuration
|
|
33
|
+
*/
|
|
34
|
+
export declare function initializeCache(config: CacheConfig): void;
|
|
35
|
+
/**
|
|
36
|
+
* Get the current cache backend
|
|
37
|
+
*/
|
|
38
|
+
export declare function getCacheBackend(): CacheBackend;
|
|
39
|
+
//# sourceMappingURL=backends.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backends.d.ts","sourceRoot":"","sources":["../../src/cache/backends.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEzD;;GAEG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IACtD,OAAO,CAAC,KAAK,CAAY;gBAEb,MAAM,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE;IAS9E,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAKtC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ7D,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAIlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG5B;AAED;;GAEG;AACH,qBAAa,iBAAkB,YAAW,YAAY;IACrD,OAAO,CAAC,MAAM,CAAM;IACpB,OAAO,CAAC,SAAS,CAAkB;gBAEvB,MAAM,EAAE,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IA+B/C,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAkBtC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAqB7D,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBlC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAkBlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAc5B;AAID;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,CAUzD;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,YAAY,CAE9C"}
|