@mondaydotcomorg/atp-runtime 0.19.6 → 0.19.8
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/dist/index.cjs +1824 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +1763 -16
- package/dist/index.js.map +1 -1
- package/dist/metadata/generated.d.ts.map +1 -1
- package/dist/metadata/generated.js +189 -189
- package/dist/metadata/generated.js.map +1 -1
- package/package.json +8 -4
- package/src/metadata/generated.ts +275 -283
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1824 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var async_hooks = require('async_hooks');
|
|
4
|
+
var pino = require('pino');
|
|
5
|
+
var NodeCache = require('node-cache');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var pino__default = /*#__PURE__*/_interopDefault(pino);
|
|
10
|
+
var NodeCache__default = /*#__PURE__*/_interopDefault(NodeCache);
|
|
11
|
+
|
|
12
|
+
var __defProp = Object.defineProperty;
|
|
13
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
14
|
+
|
|
15
|
+
// src/pause/types.ts
|
|
16
|
+
exports.CallbackType = void 0;
|
|
17
|
+
(function(CallbackType2) {
|
|
18
|
+
CallbackType2["LLM"] = "llm";
|
|
19
|
+
CallbackType2["APPROVAL"] = "approval";
|
|
20
|
+
CallbackType2["EMBEDDING"] = "embedding";
|
|
21
|
+
CallbackType2["TOOL"] = "tool";
|
|
22
|
+
})(exports.CallbackType || (exports.CallbackType = {}));
|
|
23
|
+
exports.LLMOperation = void 0;
|
|
24
|
+
(function(LLMOperation2) {
|
|
25
|
+
LLMOperation2["CALL"] = "call";
|
|
26
|
+
LLMOperation2["EXTRACT"] = "extract";
|
|
27
|
+
LLMOperation2["CLASSIFY"] = "classify";
|
|
28
|
+
})(exports.LLMOperation || (exports.LLMOperation = {}));
|
|
29
|
+
exports.EmbeddingOperation = void 0;
|
|
30
|
+
(function(EmbeddingOperation2) {
|
|
31
|
+
EmbeddingOperation2["EMBED"] = "embed";
|
|
32
|
+
EmbeddingOperation2["SEARCH"] = "search";
|
|
33
|
+
})(exports.EmbeddingOperation || (exports.EmbeddingOperation = {}));
|
|
34
|
+
exports.ApprovalOperation = void 0;
|
|
35
|
+
(function(ApprovalOperation2) {
|
|
36
|
+
ApprovalOperation2["REQUEST"] = "request";
|
|
37
|
+
})(exports.ApprovalOperation || (exports.ApprovalOperation = {}));
|
|
38
|
+
exports.ToolOperation = void 0;
|
|
39
|
+
(function(ToolOperation2) {
|
|
40
|
+
ToolOperation2["CALL"] = "call";
|
|
41
|
+
})(exports.ToolOperation || (exports.ToolOperation = {}));
|
|
42
|
+
var PauseExecutionError = class extends Error {
|
|
43
|
+
static {
|
|
44
|
+
__name(this, "PauseExecutionError");
|
|
45
|
+
}
|
|
46
|
+
type;
|
|
47
|
+
operation;
|
|
48
|
+
payload;
|
|
49
|
+
constructor(type, operation, payload) {
|
|
50
|
+
super(`Execution paused: waiting for ${type}.${operation}`);
|
|
51
|
+
this.name = "PauseExecutionError";
|
|
52
|
+
this.type = type;
|
|
53
|
+
this.operation = operation;
|
|
54
|
+
this.payload = payload;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/pause/index.ts
|
|
59
|
+
function pauseForCallback(type, operation, payload) {
|
|
60
|
+
throw new PauseExecutionError(type, operation, payload);
|
|
61
|
+
}
|
|
62
|
+
__name(pauseForCallback, "pauseForCallback");
|
|
63
|
+
function isPauseError(error) {
|
|
64
|
+
return error instanceof PauseExecutionError;
|
|
65
|
+
}
|
|
66
|
+
__name(isPauseError, "isPauseError");
|
|
67
|
+
|
|
68
|
+
// src/metadata/decorators.ts
|
|
69
|
+
function RuntimeAPI(name, description) {
|
|
70
|
+
return function(constructor) {
|
|
71
|
+
constructor.API_NAME = name;
|
|
72
|
+
constructor.API_DESCRIPTION = description;
|
|
73
|
+
return constructor;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
__name(RuntimeAPI, "RuntimeAPI");
|
|
77
|
+
function RuntimeMethod(description, paramDescriptions) {
|
|
78
|
+
return function(target, propertyKey, descriptor) {
|
|
79
|
+
if (!target.constructor.__methods) {
|
|
80
|
+
target.constructor.__methods = {};
|
|
81
|
+
}
|
|
82
|
+
target.constructor.__methods[propertyKey] = {
|
|
83
|
+
description,
|
|
84
|
+
paramDescriptions: paramDescriptions || {}
|
|
85
|
+
};
|
|
86
|
+
return descriptor;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
__name(RuntimeMethod, "RuntimeMethod");
|
|
90
|
+
var logger = null;
|
|
91
|
+
function initializeLogger(config) {
|
|
92
|
+
const pinoLevel = config?.level === "none" ? "silent" : config?.level ?? "info";
|
|
93
|
+
const options = {
|
|
94
|
+
level: pinoLevel,
|
|
95
|
+
timestamp: pino__default.default.stdTimeFunctions.isoTime,
|
|
96
|
+
formatters: {
|
|
97
|
+
level: /* @__PURE__ */ __name((label) => {
|
|
98
|
+
return {
|
|
99
|
+
level: label
|
|
100
|
+
};
|
|
101
|
+
}, "level")
|
|
102
|
+
},
|
|
103
|
+
redact: {
|
|
104
|
+
paths: config?.redact ?? [
|
|
105
|
+
"apiKey",
|
|
106
|
+
"password",
|
|
107
|
+
"*.apiKey",
|
|
108
|
+
"*.password",
|
|
109
|
+
"authorization"
|
|
110
|
+
],
|
|
111
|
+
censor: "[REDACTED]"
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
if (config?.pretty) {
|
|
115
|
+
logger = pino__default.default({
|
|
116
|
+
...options,
|
|
117
|
+
transport: {
|
|
118
|
+
target: "pino-pretty",
|
|
119
|
+
options: {
|
|
120
|
+
colorize: true,
|
|
121
|
+
translateTime: "SYS:standard",
|
|
122
|
+
ignore: "pid,hostname"
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
} else if (config?.destination && config.destination !== "stdout") {
|
|
127
|
+
logger = pino__default.default(options, pino__default.default.destination(config.destination));
|
|
128
|
+
} else {
|
|
129
|
+
logger = pino__default.default(options);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
__name(initializeLogger, "initializeLogger");
|
|
133
|
+
function getLogger() {
|
|
134
|
+
if (!logger) {
|
|
135
|
+
initializeLogger({
|
|
136
|
+
level: "info",
|
|
137
|
+
pretty: false
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return logger;
|
|
141
|
+
}
|
|
142
|
+
__name(getLogger, "getLogger");
|
|
143
|
+
var log = {
|
|
144
|
+
/**
|
|
145
|
+
* Logs an informational message
|
|
146
|
+
*/
|
|
147
|
+
info(message, data) {
|
|
148
|
+
const l = getLogger();
|
|
149
|
+
if (data) {
|
|
150
|
+
l.info(data, message);
|
|
151
|
+
} else {
|
|
152
|
+
l.info(message);
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
/**
|
|
156
|
+
* Logs a warning message
|
|
157
|
+
*/
|
|
158
|
+
warn(message, data) {
|
|
159
|
+
const l = getLogger();
|
|
160
|
+
if (data) {
|
|
161
|
+
l.warn(data, message);
|
|
162
|
+
} else {
|
|
163
|
+
l.warn(message);
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
/**
|
|
167
|
+
* Logs an error message
|
|
168
|
+
*/
|
|
169
|
+
error(message, data) {
|
|
170
|
+
const l = getLogger();
|
|
171
|
+
if (data) {
|
|
172
|
+
l.error(data, message);
|
|
173
|
+
} else {
|
|
174
|
+
l.error(message);
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
/**
|
|
178
|
+
* Logs a debug message
|
|
179
|
+
*/
|
|
180
|
+
debug(message, data) {
|
|
181
|
+
const l = getLogger();
|
|
182
|
+
if (data) {
|
|
183
|
+
l.debug(data, message);
|
|
184
|
+
} else {
|
|
185
|
+
l.debug(message);
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
/**
|
|
189
|
+
* Logs a fatal error message
|
|
190
|
+
*/
|
|
191
|
+
fatal(message, data) {
|
|
192
|
+
const l = getLogger();
|
|
193
|
+
if (data) {
|
|
194
|
+
l.fatal(data, message);
|
|
195
|
+
} else {
|
|
196
|
+
l.fatal(message);
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
/**
|
|
200
|
+
* Creates a child logger with additional context
|
|
201
|
+
*/
|
|
202
|
+
child(bindings) {
|
|
203
|
+
const childLogger = getLogger().child(bindings);
|
|
204
|
+
return {
|
|
205
|
+
info: /* @__PURE__ */ __name((message, data) => {
|
|
206
|
+
if (data) {
|
|
207
|
+
childLogger.info(data, message);
|
|
208
|
+
} else {
|
|
209
|
+
childLogger.info(message);
|
|
210
|
+
}
|
|
211
|
+
}, "info"),
|
|
212
|
+
warn: /* @__PURE__ */ __name((message, data) => {
|
|
213
|
+
if (data) {
|
|
214
|
+
childLogger.warn(data, message);
|
|
215
|
+
} else {
|
|
216
|
+
childLogger.warn(message);
|
|
217
|
+
}
|
|
218
|
+
}, "warn"),
|
|
219
|
+
error: /* @__PURE__ */ __name((message, data) => {
|
|
220
|
+
if (data) {
|
|
221
|
+
childLogger.error(data, message);
|
|
222
|
+
} else {
|
|
223
|
+
childLogger.error(message);
|
|
224
|
+
}
|
|
225
|
+
}, "error"),
|
|
226
|
+
debug: /* @__PURE__ */ __name((message, data) => {
|
|
227
|
+
if (data) {
|
|
228
|
+
childLogger.debug(data, message);
|
|
229
|
+
} else {
|
|
230
|
+
childLogger.debug(message);
|
|
231
|
+
}
|
|
232
|
+
}, "debug"),
|
|
233
|
+
fatal: /* @__PURE__ */ __name((message, data) => {
|
|
234
|
+
if (data) {
|
|
235
|
+
childLogger.fatal(data, message);
|
|
236
|
+
} else {
|
|
237
|
+
childLogger.fatal(message);
|
|
238
|
+
}
|
|
239
|
+
}, "fatal"),
|
|
240
|
+
child: log.child
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
function shutdownLogger() {
|
|
245
|
+
logger = null;
|
|
246
|
+
}
|
|
247
|
+
__name(shutdownLogger, "shutdownLogger");
|
|
248
|
+
|
|
249
|
+
// src/llm/replay.ts
|
|
250
|
+
var executionStates = /* @__PURE__ */ new Map();
|
|
251
|
+
var MAX_EXECUTION_STATES = 100;
|
|
252
|
+
var operationCounter = 0;
|
|
253
|
+
var CLEANUP_CHECK_INTERVAL = 50;
|
|
254
|
+
var executionContext = new async_hooks.AsyncLocalStorage();
|
|
255
|
+
var currentExecutionId = null;
|
|
256
|
+
function setCurrentExecutionId(executionId) {
|
|
257
|
+
currentExecutionId = executionId;
|
|
258
|
+
}
|
|
259
|
+
__name(setCurrentExecutionId, "setCurrentExecutionId");
|
|
260
|
+
function clearCurrentExecutionId() {
|
|
261
|
+
currentExecutionId = null;
|
|
262
|
+
}
|
|
263
|
+
__name(clearCurrentExecutionId, "clearCurrentExecutionId");
|
|
264
|
+
function getCurrentState() {
|
|
265
|
+
let executionId = currentExecutionId;
|
|
266
|
+
if (!executionId) {
|
|
267
|
+
executionId = executionContext.getStore() || null;
|
|
268
|
+
}
|
|
269
|
+
if (!executionId) {
|
|
270
|
+
throw new Error("No execution context set. Executor must call setCurrentExecutionId() before runtime API calls.");
|
|
271
|
+
}
|
|
272
|
+
operationCounter++;
|
|
273
|
+
if (operationCounter >= CLEANUP_CHECK_INTERVAL) {
|
|
274
|
+
operationCounter = 0;
|
|
275
|
+
autoCleanup();
|
|
276
|
+
}
|
|
277
|
+
let state = executionStates.get(executionId);
|
|
278
|
+
if (!state) {
|
|
279
|
+
log.warn("State not initialized, creating with default. This should not happen.", {
|
|
280
|
+
executionId
|
|
281
|
+
});
|
|
282
|
+
state = {
|
|
283
|
+
shouldPauseForClient: false,
|
|
284
|
+
replayResults: void 0,
|
|
285
|
+
callSequenceNumber: 0,
|
|
286
|
+
apiCallResults: [],
|
|
287
|
+
apiResultCache: void 0,
|
|
288
|
+
createdAt: Date.now()
|
|
289
|
+
};
|
|
290
|
+
executionStates.set(executionId, state);
|
|
291
|
+
}
|
|
292
|
+
return state;
|
|
293
|
+
}
|
|
294
|
+
__name(getCurrentState, "getCurrentState");
|
|
295
|
+
function initializeExecutionState(shouldPause) {
|
|
296
|
+
const executionId = currentExecutionId || executionContext.getStore();
|
|
297
|
+
if (!executionId) {
|
|
298
|
+
throw new Error("No execution context set. Executor must call setCurrentExecutionId() before initializeExecutionState().");
|
|
299
|
+
}
|
|
300
|
+
const existingState = executionStates.get(executionId);
|
|
301
|
+
if (existingState) {
|
|
302
|
+
existingState.shouldPauseForClient = shouldPause;
|
|
303
|
+
if (!existingState.apiCallResults) {
|
|
304
|
+
existingState.apiCallResults = [];
|
|
305
|
+
}
|
|
306
|
+
if (!existingState.apiResultCache) {
|
|
307
|
+
existingState.apiResultCache = void 0;
|
|
308
|
+
}
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const state = {
|
|
312
|
+
shouldPauseForClient: shouldPause,
|
|
313
|
+
replayResults: void 0,
|
|
314
|
+
callSequenceNumber: 0,
|
|
315
|
+
apiCallResults: [],
|
|
316
|
+
apiResultCache: void 0,
|
|
317
|
+
createdAt: Date.now()
|
|
318
|
+
};
|
|
319
|
+
executionStates.set(executionId, state);
|
|
320
|
+
}
|
|
321
|
+
__name(initializeExecutionState, "initializeExecutionState");
|
|
322
|
+
function runInExecutionContext(executionId, fn) {
|
|
323
|
+
return executionContext.run(executionId, fn);
|
|
324
|
+
}
|
|
325
|
+
__name(runInExecutionContext, "runInExecutionContext");
|
|
326
|
+
function setPauseForClient(pause) {
|
|
327
|
+
const executionId = currentExecutionId || executionContext.getStore();
|
|
328
|
+
if (!executionId) {
|
|
329
|
+
throw new Error("No execution context set. Executor must call setCurrentExecutionId() before setPauseForClient().");
|
|
330
|
+
}
|
|
331
|
+
const state = executionStates.get(executionId);
|
|
332
|
+
if (!state) {
|
|
333
|
+
throw new Error("Execution state not initialized. Call initializeExecutionState() first.");
|
|
334
|
+
}
|
|
335
|
+
state.shouldPauseForClient = pause;
|
|
336
|
+
}
|
|
337
|
+
__name(setPauseForClient, "setPauseForClient");
|
|
338
|
+
function shouldPauseForClient() {
|
|
339
|
+
const state = getCurrentState();
|
|
340
|
+
return state.shouldPauseForClient;
|
|
341
|
+
}
|
|
342
|
+
__name(shouldPauseForClient, "shouldPauseForClient");
|
|
343
|
+
function setReplayMode(results) {
|
|
344
|
+
const state = getCurrentState();
|
|
345
|
+
state.replayResults = results;
|
|
346
|
+
state.callSequenceNumber = 0;
|
|
347
|
+
}
|
|
348
|
+
__name(setReplayMode, "setReplayMode");
|
|
349
|
+
function getCallSequenceNumber() {
|
|
350
|
+
const state = getCurrentState();
|
|
351
|
+
return state.callSequenceNumber;
|
|
352
|
+
}
|
|
353
|
+
__name(getCallSequenceNumber, "getCallSequenceNumber");
|
|
354
|
+
function nextSequenceNumber() {
|
|
355
|
+
const state = getCurrentState();
|
|
356
|
+
const current = state.callSequenceNumber;
|
|
357
|
+
state.callSequenceNumber++;
|
|
358
|
+
return current;
|
|
359
|
+
}
|
|
360
|
+
__name(nextSequenceNumber, "nextSequenceNumber");
|
|
361
|
+
function getCachedResult(sequenceNumber) {
|
|
362
|
+
const state = getCurrentState();
|
|
363
|
+
if (state.replayResults && state.replayResults.has(sequenceNumber)) {
|
|
364
|
+
return state.replayResults.get(sequenceNumber);
|
|
365
|
+
}
|
|
366
|
+
return void 0;
|
|
367
|
+
}
|
|
368
|
+
__name(getCachedResult, "getCachedResult");
|
|
369
|
+
function isReplayMode() {
|
|
370
|
+
return getCurrentState().replayResults !== void 0;
|
|
371
|
+
}
|
|
372
|
+
__name(isReplayMode, "isReplayMode");
|
|
373
|
+
function storeAPICallResult(record) {
|
|
374
|
+
const state = getCurrentState();
|
|
375
|
+
state.apiCallResults.push(record);
|
|
376
|
+
}
|
|
377
|
+
__name(storeAPICallResult, "storeAPICallResult");
|
|
378
|
+
function getAPICallResults() {
|
|
379
|
+
const state = getCurrentState();
|
|
380
|
+
return state.apiCallResults;
|
|
381
|
+
}
|
|
382
|
+
__name(getAPICallResults, "getAPICallResults");
|
|
383
|
+
function clearAPICallResults() {
|
|
384
|
+
const state = getCurrentState();
|
|
385
|
+
state.apiCallResults = [];
|
|
386
|
+
}
|
|
387
|
+
__name(clearAPICallResults, "clearAPICallResults");
|
|
388
|
+
function setAPIResultCache(cache2) {
|
|
389
|
+
const state = getCurrentState();
|
|
390
|
+
state.apiResultCache = cache2;
|
|
391
|
+
}
|
|
392
|
+
__name(setAPIResultCache, "setAPIResultCache");
|
|
393
|
+
function getAPIResultFromCache(operation) {
|
|
394
|
+
const state = getCurrentState();
|
|
395
|
+
return state.apiResultCache?.get(operation);
|
|
396
|
+
}
|
|
397
|
+
__name(getAPIResultFromCache, "getAPIResultFromCache");
|
|
398
|
+
function storeAPIResultInCache(operation, result) {
|
|
399
|
+
const state = getCurrentState();
|
|
400
|
+
if (!state.apiResultCache) {
|
|
401
|
+
state.apiResultCache = /* @__PURE__ */ new Map();
|
|
402
|
+
}
|
|
403
|
+
state.apiResultCache.set(operation, result);
|
|
404
|
+
}
|
|
405
|
+
__name(storeAPIResultInCache, "storeAPIResultInCache");
|
|
406
|
+
function cleanupExecutionState(executionId) {
|
|
407
|
+
executionStates.delete(executionId);
|
|
408
|
+
if (currentExecutionId === executionId) {
|
|
409
|
+
currentExecutionId = null;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
__name(cleanupExecutionState, "cleanupExecutionState");
|
|
413
|
+
function autoCleanup() {
|
|
414
|
+
if (executionStates.size <= MAX_EXECUTION_STATES) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
const entries = Array.from(executionStates.entries()).sort((a, b) => a[1].createdAt - b[1].createdAt);
|
|
418
|
+
const toRemove = executionStates.size - MAX_EXECUTION_STATES;
|
|
419
|
+
for (let i = 0; i < toRemove; i++) {
|
|
420
|
+
const entry = entries[i];
|
|
421
|
+
if (entry) {
|
|
422
|
+
executionStates.delete(entry[0]);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
__name(autoCleanup, "autoCleanup");
|
|
427
|
+
function cleanupOldExecutionStates(maxAgeMs = 36e5) {
|
|
428
|
+
const now = Date.now();
|
|
429
|
+
let cleaned = 0;
|
|
430
|
+
for (const [executionId, state] of executionStates.entries()) {
|
|
431
|
+
const age = now - state.createdAt;
|
|
432
|
+
if (age > maxAgeMs) {
|
|
433
|
+
executionStates.delete(executionId);
|
|
434
|
+
cleaned++;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return cleaned;
|
|
438
|
+
}
|
|
439
|
+
__name(cleanupOldExecutionStates, "cleanupOldExecutionStates");
|
|
440
|
+
function resetAllExecutionState() {
|
|
441
|
+
executionStates.clear();
|
|
442
|
+
currentExecutionId = null;
|
|
443
|
+
}
|
|
444
|
+
__name(resetAllExecutionState, "resetAllExecutionState");
|
|
445
|
+
function getExecutionStateStats() {
|
|
446
|
+
const now = Date.now();
|
|
447
|
+
const executionIds = Array.from(executionStates.keys());
|
|
448
|
+
let oldestAge = null;
|
|
449
|
+
let newestAge = null;
|
|
450
|
+
for (const state of executionStates.values()) {
|
|
451
|
+
const age = now - state.createdAt;
|
|
452
|
+
if (oldestAge === null || age > oldestAge) {
|
|
453
|
+
oldestAge = age;
|
|
454
|
+
}
|
|
455
|
+
if (newestAge === null || age < newestAge) {
|
|
456
|
+
newestAge = age;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return {
|
|
460
|
+
totalStates: executionStates.size,
|
|
461
|
+
oldestStateAge: oldestAge,
|
|
462
|
+
newestStateAge: newestAge,
|
|
463
|
+
executionIds
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
__name(getExecutionStateStats, "getExecutionStateStats");
|
|
467
|
+
|
|
468
|
+
// src/llm/callback.ts
|
|
469
|
+
var clientLLMCallback;
|
|
470
|
+
function setClientLLMCallback(callback) {
|
|
471
|
+
clientLLMCallback = callback;
|
|
472
|
+
}
|
|
473
|
+
__name(setClientLLMCallback, "setClientLLMCallback");
|
|
474
|
+
function getClientLLMCallback() {
|
|
475
|
+
return clientLLMCallback;
|
|
476
|
+
}
|
|
477
|
+
__name(getClientLLMCallback, "getClientLLMCallback");
|
|
478
|
+
|
|
479
|
+
// src/llm/index.ts
|
|
480
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
481
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
482
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
483
|
+
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;
|
|
484
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
485
|
+
}
|
|
486
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
487
|
+
function _ts_metadata(k, v) {
|
|
488
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
489
|
+
}
|
|
490
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
491
|
+
var LLMAPI = class LLMAPI2 {
|
|
492
|
+
static {
|
|
493
|
+
__name(this, "LLMAPI");
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Makes a standard LLM call
|
|
497
|
+
* Always pauses execution and routes to client-provided LLM
|
|
498
|
+
*/
|
|
499
|
+
async call(options) {
|
|
500
|
+
const currentSequence = nextSequenceNumber();
|
|
501
|
+
const cachedResult = getCachedResult(currentSequence);
|
|
502
|
+
if (cachedResult !== void 0) {
|
|
503
|
+
return cachedResult;
|
|
504
|
+
}
|
|
505
|
+
pauseForCallback(exports.CallbackType.LLM, exports.LLMOperation.CALL, {
|
|
506
|
+
prompt: options.prompt,
|
|
507
|
+
options,
|
|
508
|
+
sequenceNumber: currentSequence
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Extracts structured data using LLM
|
|
513
|
+
* Always pauses execution and routes to client-provided LLM
|
|
514
|
+
*/
|
|
515
|
+
async extract(options) {
|
|
516
|
+
const currentSequence = nextSequenceNumber();
|
|
517
|
+
const cachedResult = getCachedResult(currentSequence);
|
|
518
|
+
if (cachedResult !== void 0) {
|
|
519
|
+
return cachedResult;
|
|
520
|
+
}
|
|
521
|
+
pauseForCallback(exports.CallbackType.LLM, exports.LLMOperation.EXTRACT, {
|
|
522
|
+
prompt: options.prompt,
|
|
523
|
+
schema: options.schema,
|
|
524
|
+
options,
|
|
525
|
+
sequenceNumber: currentSequence
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Classifies text into one of the provided categories
|
|
530
|
+
* Always pauses execution and routes to client-provided LLM
|
|
531
|
+
*/
|
|
532
|
+
async classify(options) {
|
|
533
|
+
const currentSequence = nextSequenceNumber();
|
|
534
|
+
const cachedResult = getCachedResult(currentSequence);
|
|
535
|
+
if (cachedResult !== void 0) {
|
|
536
|
+
return cachedResult;
|
|
537
|
+
}
|
|
538
|
+
pauseForCallback(exports.CallbackType.LLM, exports.LLMOperation.CLASSIFY, {
|
|
539
|
+
text: options.text,
|
|
540
|
+
categories: options.categories,
|
|
541
|
+
options,
|
|
542
|
+
sequenceNumber: currentSequence
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
_ts_decorate([
|
|
547
|
+
RuntimeMethod("Make an LLM call with a prompt", {
|
|
548
|
+
options: {
|
|
549
|
+
description: "LLM call options including prompt",
|
|
550
|
+
type: "LLMCallOptions"
|
|
551
|
+
}
|
|
552
|
+
}),
|
|
553
|
+
_ts_metadata("design:type", Function),
|
|
554
|
+
_ts_metadata("design:paramtypes", [
|
|
555
|
+
typeof LLMCallOptions === "undefined" ? Object : LLMCallOptions
|
|
556
|
+
]),
|
|
557
|
+
_ts_metadata("design:returntype", Promise)
|
|
558
|
+
], LLMAPI.prototype, "call", null);
|
|
559
|
+
_ts_decorate([
|
|
560
|
+
RuntimeMethod("Extract structured data from text using an LLM", {
|
|
561
|
+
options: {
|
|
562
|
+
description: "Extraction options with JSON schema",
|
|
563
|
+
type: "LLMExtractOptions"
|
|
564
|
+
}
|
|
565
|
+
}),
|
|
566
|
+
_ts_metadata("design:type", Function),
|
|
567
|
+
_ts_metadata("design:paramtypes", [
|
|
568
|
+
typeof LLMExtractOptions === "undefined" ? Object : LLMExtractOptions
|
|
569
|
+
]),
|
|
570
|
+
_ts_metadata("design:returntype", Promise)
|
|
571
|
+
], LLMAPI.prototype, "extract", null);
|
|
572
|
+
_ts_decorate([
|
|
573
|
+
RuntimeMethod("Classify text into one of the provided categories", {
|
|
574
|
+
options: {
|
|
575
|
+
description: "Classification options with categories",
|
|
576
|
+
type: "LLMClassifyOptions"
|
|
577
|
+
}
|
|
578
|
+
}),
|
|
579
|
+
_ts_metadata("design:type", Function),
|
|
580
|
+
_ts_metadata("design:paramtypes", [
|
|
581
|
+
typeof LLMClassifyOptions === "undefined" ? Object : LLMClassifyOptions
|
|
582
|
+
]),
|
|
583
|
+
_ts_metadata("design:returntype", Promise)
|
|
584
|
+
], LLMAPI.prototype, "classify", null);
|
|
585
|
+
LLMAPI = _ts_decorate([
|
|
586
|
+
RuntimeAPI("llm", "LLM API - Large Language Model calls using client-provided LLM (requires client.provideLLM())")
|
|
587
|
+
], LLMAPI);
|
|
588
|
+
var llm = new LLMAPI();
|
|
589
|
+
|
|
590
|
+
// src/progress/index.ts
|
|
591
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
592
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
593
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
594
|
+
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;
|
|
595
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
596
|
+
}
|
|
597
|
+
__name(_ts_decorate2, "_ts_decorate");
|
|
598
|
+
function _ts_metadata2(k, v) {
|
|
599
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
600
|
+
}
|
|
601
|
+
__name(_ts_metadata2, "_ts_metadata");
|
|
602
|
+
var progressCallback = null;
|
|
603
|
+
function setProgressCallback(callback) {
|
|
604
|
+
progressCallback = callback;
|
|
605
|
+
}
|
|
606
|
+
__name(setProgressCallback, "setProgressCallback");
|
|
607
|
+
var ProgressAPI = class ProgressAPI2 {
|
|
608
|
+
static {
|
|
609
|
+
__name(this, "ProgressAPI");
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Report progress with message and completion fraction
|
|
613
|
+
*/
|
|
614
|
+
report(message, fraction) {
|
|
615
|
+
if (progressCallback) {
|
|
616
|
+
try {
|
|
617
|
+
progressCallback(message, fraction);
|
|
618
|
+
} catch (error) {
|
|
619
|
+
log.error("Progress callback error", {
|
|
620
|
+
error
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
_ts_decorate2([
|
|
627
|
+
RuntimeMethod("Report progress with message and completion fraction", {
|
|
628
|
+
message: {
|
|
629
|
+
description: "Progress message"
|
|
630
|
+
},
|
|
631
|
+
fraction: {
|
|
632
|
+
description: "Completion fraction (0-1)"
|
|
633
|
+
}
|
|
634
|
+
}),
|
|
635
|
+
_ts_metadata2("design:type", Function),
|
|
636
|
+
_ts_metadata2("design:paramtypes", [
|
|
637
|
+
String,
|
|
638
|
+
Number
|
|
639
|
+
]),
|
|
640
|
+
_ts_metadata2("design:returntype", void 0)
|
|
641
|
+
], ProgressAPI.prototype, "report", null);
|
|
642
|
+
ProgressAPI = _ts_decorate2([
|
|
643
|
+
RuntimeAPI("progress", "Progress API - Report execution progress to clients")
|
|
644
|
+
], ProgressAPI);
|
|
645
|
+
var progress = new ProgressAPI();
|
|
646
|
+
var MemoryCacheBackend = class {
|
|
647
|
+
static {
|
|
648
|
+
__name(this, "MemoryCacheBackend");
|
|
649
|
+
}
|
|
650
|
+
cache;
|
|
651
|
+
constructor(config) {
|
|
652
|
+
this.cache = new NodeCache__default.default({
|
|
653
|
+
stdTTL: config?.defaultTTL ?? 600,
|
|
654
|
+
checkperiod: config?.checkPeriod ?? 120,
|
|
655
|
+
maxKeys: config?.maxKeys ?? 1e3,
|
|
656
|
+
useClones: false
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
async get(key) {
|
|
660
|
+
const value = this.cache.get(key);
|
|
661
|
+
return value ?? null;
|
|
662
|
+
}
|
|
663
|
+
async set(key, value, ttl) {
|
|
664
|
+
if (ttl) {
|
|
665
|
+
this.cache.set(key, value, ttl);
|
|
666
|
+
} else {
|
|
667
|
+
this.cache.set(key, value);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
async delete(key) {
|
|
671
|
+
this.cache.del(key);
|
|
672
|
+
}
|
|
673
|
+
async has(key) {
|
|
674
|
+
return this.cache.has(key);
|
|
675
|
+
}
|
|
676
|
+
async clear() {
|
|
677
|
+
this.cache.flushAll();
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
var RedisCacheBackend = class {
|
|
681
|
+
static {
|
|
682
|
+
__name(this, "RedisCacheBackend");
|
|
683
|
+
}
|
|
684
|
+
client;
|
|
685
|
+
connected = false;
|
|
686
|
+
constructor(config) {
|
|
687
|
+
import('ioredis').then((Redis) => {
|
|
688
|
+
this.client = new Redis.default({
|
|
689
|
+
host: config.host,
|
|
690
|
+
port: config.port,
|
|
691
|
+
password: config.password,
|
|
692
|
+
db: config.db ?? 0,
|
|
693
|
+
retryStrategy: /* @__PURE__ */ __name((times) => {
|
|
694
|
+
if (times > 3) {
|
|
695
|
+
return null;
|
|
696
|
+
}
|
|
697
|
+
return Math.min(times * 100, 2e3);
|
|
698
|
+
}, "retryStrategy"),
|
|
699
|
+
lazyConnect: true
|
|
700
|
+
});
|
|
701
|
+
this.client.connect().then(() => {
|
|
702
|
+
this.connected = true;
|
|
703
|
+
}).catch(() => {
|
|
704
|
+
this.connected = false;
|
|
705
|
+
});
|
|
706
|
+
}).catch(() => {
|
|
707
|
+
throw new Error("ioredis package not installed. Install it with: yarn add ioredis");
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
async get(key) {
|
|
711
|
+
if (!this.connected) {
|
|
712
|
+
log.warn("Redis Cache not connected, cannot get key", {
|
|
713
|
+
key
|
|
714
|
+
});
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
try {
|
|
718
|
+
const value = await this.client.get(key);
|
|
719
|
+
return value ? JSON.parse(value) : null;
|
|
720
|
+
} catch (error) {
|
|
721
|
+
log.error("Redis Cache failed to get key", {
|
|
722
|
+
key,
|
|
723
|
+
error: error instanceof Error ? error.message : error
|
|
724
|
+
});
|
|
725
|
+
return null;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
async set(key, value, ttl) {
|
|
729
|
+
if (!this.connected) {
|
|
730
|
+
log.warn("Redis Cache not connected, cannot set key", {
|
|
731
|
+
key
|
|
732
|
+
});
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
try {
|
|
736
|
+
const serialized = JSON.stringify(value);
|
|
737
|
+
if (ttl) {
|
|
738
|
+
await this.client.setex(key, ttl, serialized);
|
|
739
|
+
} else {
|
|
740
|
+
await this.client.set(key, serialized);
|
|
741
|
+
}
|
|
742
|
+
} catch (error) {
|
|
743
|
+
log.error("Redis Cache failed to set key", {
|
|
744
|
+
key,
|
|
745
|
+
error: error instanceof Error ? error.message : error
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
async delete(key) {
|
|
750
|
+
if (!this.connected) {
|
|
751
|
+
log.warn("Redis Cache not connected, cannot delete key", {
|
|
752
|
+
key
|
|
753
|
+
});
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
try {
|
|
757
|
+
await this.client.del(key);
|
|
758
|
+
} catch (error) {
|
|
759
|
+
log.error("Redis Cache failed to delete key", {
|
|
760
|
+
key,
|
|
761
|
+
error: error instanceof Error ? error.message : error
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
async has(key) {
|
|
766
|
+
if (!this.connected) {
|
|
767
|
+
log.warn("Redis Cache not connected, cannot check key", {
|
|
768
|
+
key
|
|
769
|
+
});
|
|
770
|
+
return false;
|
|
771
|
+
}
|
|
772
|
+
try {
|
|
773
|
+
const exists = await this.client.exists(key);
|
|
774
|
+
return exists === 1;
|
|
775
|
+
} catch (error) {
|
|
776
|
+
log.error("Redis Cache failed to check key", {
|
|
777
|
+
key,
|
|
778
|
+
error: error instanceof Error ? error.message : error
|
|
779
|
+
});
|
|
780
|
+
return false;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
async clear() {
|
|
784
|
+
if (!this.connected) {
|
|
785
|
+
log.warn("Redis Cache not connected, cannot clear cache");
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
try {
|
|
789
|
+
await this.client.flushdb();
|
|
790
|
+
} catch (error) {
|
|
791
|
+
log.error("Redis Cache failed to clear cache", {
|
|
792
|
+
error: error instanceof Error ? error.message : error
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
var cacheBackend = new MemoryCacheBackend();
|
|
798
|
+
function initializeCache(config) {
|
|
799
|
+
if (config.type === "redis" && config.redis) {
|
|
800
|
+
cacheBackend = new RedisCacheBackend(config.redis);
|
|
801
|
+
} else {
|
|
802
|
+
cacheBackend = new MemoryCacheBackend({
|
|
803
|
+
maxKeys: config.maxKeys,
|
|
804
|
+
defaultTTL: config.defaultTTL,
|
|
805
|
+
checkPeriod: config.checkPeriod
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
__name(initializeCache, "initializeCache");
|
|
810
|
+
function getCacheBackend() {
|
|
811
|
+
return cacheBackend;
|
|
812
|
+
}
|
|
813
|
+
__name(getCacheBackend, "getCacheBackend");
|
|
814
|
+
|
|
815
|
+
// src/cache/index.ts
|
|
816
|
+
function _ts_decorate3(decorators, target, key, desc) {
|
|
817
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
818
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
819
|
+
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;
|
|
820
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
821
|
+
}
|
|
822
|
+
__name(_ts_decorate3, "_ts_decorate");
|
|
823
|
+
function _ts_metadata3(k, v) {
|
|
824
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
825
|
+
}
|
|
826
|
+
__name(_ts_metadata3, "_ts_metadata");
|
|
827
|
+
var CacheAPI = class CacheAPI2 {
|
|
828
|
+
static {
|
|
829
|
+
__name(this, "CacheAPI");
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Gets a value from cache
|
|
833
|
+
*/
|
|
834
|
+
async get(key) {
|
|
835
|
+
return getCacheBackend().get(key);
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Sets a value in cache with optional TTL in seconds
|
|
839
|
+
*/
|
|
840
|
+
async set(key, value, ttl) {
|
|
841
|
+
return getCacheBackend().set(key, value, ttl);
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Deletes a value from cache
|
|
845
|
+
*/
|
|
846
|
+
async delete(key) {
|
|
847
|
+
return getCacheBackend().delete(key);
|
|
848
|
+
}
|
|
849
|
+
/**
|
|
850
|
+
* Checks if a key exists in cache
|
|
851
|
+
*/
|
|
852
|
+
async has(key) {
|
|
853
|
+
return getCacheBackend().has(key);
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* Clears all cache entries
|
|
857
|
+
*/
|
|
858
|
+
async clear() {
|
|
859
|
+
return getCacheBackend().clear();
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
_ts_decorate3([
|
|
863
|
+
RuntimeMethod("Get a value from cache by key", {
|
|
864
|
+
key: {
|
|
865
|
+
description: "Cache key"
|
|
866
|
+
}
|
|
867
|
+
}),
|
|
868
|
+
_ts_metadata3("design:type", Function),
|
|
869
|
+
_ts_metadata3("design:paramtypes", [
|
|
870
|
+
String
|
|
871
|
+
]),
|
|
872
|
+
_ts_metadata3("design:returntype", Promise)
|
|
873
|
+
], CacheAPI.prototype, "get", null);
|
|
874
|
+
_ts_decorate3([
|
|
875
|
+
RuntimeMethod("Set a value in cache with optional TTL", {
|
|
876
|
+
key: {
|
|
877
|
+
description: "Cache key"
|
|
878
|
+
},
|
|
879
|
+
value: {
|
|
880
|
+
description: "Value to cache",
|
|
881
|
+
type: "unknown"
|
|
882
|
+
},
|
|
883
|
+
ttl: {
|
|
884
|
+
description: "Time to live in seconds",
|
|
885
|
+
optional: true
|
|
886
|
+
}
|
|
887
|
+
}),
|
|
888
|
+
_ts_metadata3("design:type", Function),
|
|
889
|
+
_ts_metadata3("design:paramtypes", [
|
|
890
|
+
String,
|
|
891
|
+
Object,
|
|
892
|
+
Number
|
|
893
|
+
]),
|
|
894
|
+
_ts_metadata3("design:returntype", Promise)
|
|
895
|
+
], CacheAPI.prototype, "set", null);
|
|
896
|
+
_ts_decorate3([
|
|
897
|
+
RuntimeMethod("Delete a value from cache", {
|
|
898
|
+
key: {
|
|
899
|
+
description: "Cache key to delete"
|
|
900
|
+
}
|
|
901
|
+
}),
|
|
902
|
+
_ts_metadata3("design:type", Function),
|
|
903
|
+
_ts_metadata3("design:paramtypes", [
|
|
904
|
+
String
|
|
905
|
+
]),
|
|
906
|
+
_ts_metadata3("design:returntype", Promise)
|
|
907
|
+
], CacheAPI.prototype, "delete", null);
|
|
908
|
+
_ts_decorate3([
|
|
909
|
+
RuntimeMethod("Check if a key exists in cache", {
|
|
910
|
+
key: {
|
|
911
|
+
description: "Cache key to check"
|
|
912
|
+
}
|
|
913
|
+
}),
|
|
914
|
+
_ts_metadata3("design:type", Function),
|
|
915
|
+
_ts_metadata3("design:paramtypes", [
|
|
916
|
+
String
|
|
917
|
+
]),
|
|
918
|
+
_ts_metadata3("design:returntype", Promise)
|
|
919
|
+
], CacheAPI.prototype, "has", null);
|
|
920
|
+
_ts_decorate3([
|
|
921
|
+
RuntimeMethod("Clear all cache entries"),
|
|
922
|
+
_ts_metadata3("design:type", Function),
|
|
923
|
+
_ts_metadata3("design:paramtypes", []),
|
|
924
|
+
_ts_metadata3("design:returntype", Promise)
|
|
925
|
+
], CacheAPI.prototype, "clear", null);
|
|
926
|
+
CacheAPI = _ts_decorate3([
|
|
927
|
+
RuntimeAPI("cache", "Cache API - Store and retrieve data with optional TTL")
|
|
928
|
+
], CacheAPI);
|
|
929
|
+
var cache = new CacheAPI();
|
|
930
|
+
|
|
931
|
+
// src/utils.ts
|
|
932
|
+
var utils = {
|
|
933
|
+
async sleep(ms) {
|
|
934
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
935
|
+
},
|
|
936
|
+
async retry(fn, options) {
|
|
937
|
+
let lastError;
|
|
938
|
+
for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
|
|
939
|
+
try {
|
|
940
|
+
return await fn();
|
|
941
|
+
} catch (error) {
|
|
942
|
+
lastError = error;
|
|
943
|
+
if (attempt < options.maxAttempts) {
|
|
944
|
+
await this.sleep(options.delayMs);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
throw lastError;
|
|
949
|
+
},
|
|
950
|
+
async parallel(tasks) {
|
|
951
|
+
return Promise.all(tasks.map((task) => task()));
|
|
952
|
+
},
|
|
953
|
+
async sequence(tasks) {
|
|
954
|
+
const results = [];
|
|
955
|
+
for (const task of tasks) {
|
|
956
|
+
results.push(await task());
|
|
957
|
+
}
|
|
958
|
+
return results;
|
|
959
|
+
}
|
|
960
|
+
};
|
|
961
|
+
|
|
962
|
+
// src/approval/handler.ts
|
|
963
|
+
var approvalHandler = null;
|
|
964
|
+
function initializeApproval(handler) {
|
|
965
|
+
approvalHandler = handler;
|
|
966
|
+
}
|
|
967
|
+
__name(initializeApproval, "initializeApproval");
|
|
968
|
+
function getApprovalHandler() {
|
|
969
|
+
return approvalHandler;
|
|
970
|
+
}
|
|
971
|
+
__name(getApprovalHandler, "getApprovalHandler");
|
|
972
|
+
|
|
973
|
+
// src/approval/index.ts
|
|
974
|
+
function _ts_decorate4(decorators, target, key, desc) {
|
|
975
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
976
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
977
|
+
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;
|
|
978
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
979
|
+
}
|
|
980
|
+
__name(_ts_decorate4, "_ts_decorate");
|
|
981
|
+
function _ts_metadata4(k, v) {
|
|
982
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
983
|
+
}
|
|
984
|
+
__name(_ts_metadata4, "_ts_metadata");
|
|
985
|
+
var ApprovalAPI = class ApprovalAPI2 {
|
|
986
|
+
static {
|
|
987
|
+
__name(this, "ApprovalAPI");
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Request approval from a human
|
|
991
|
+
*/
|
|
992
|
+
async request(message, context) {
|
|
993
|
+
const currentSequence = nextSequenceNumber();
|
|
994
|
+
const cachedResult = getCachedResult(currentSequence);
|
|
995
|
+
if (cachedResult !== void 0) {
|
|
996
|
+
return cachedResult;
|
|
997
|
+
}
|
|
998
|
+
const shouldPause = shouldPauseForClient();
|
|
999
|
+
if (shouldPause) {
|
|
1000
|
+
pauseForCallback(exports.CallbackType.APPROVAL, exports.ApprovalOperation.REQUEST, {
|
|
1001
|
+
message,
|
|
1002
|
+
context,
|
|
1003
|
+
sequenceNumber: currentSequence
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
const handler = getApprovalHandler();
|
|
1007
|
+
if (!handler) {
|
|
1008
|
+
throw new Error("Approval handler not configured. Human approval is required but no handler is set.");
|
|
1009
|
+
}
|
|
1010
|
+
const approvalRequest = {
|
|
1011
|
+
message,
|
|
1012
|
+
context,
|
|
1013
|
+
timeout: 3e5
|
|
1014
|
+
};
|
|
1015
|
+
let timeoutId = null;
|
|
1016
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
1017
|
+
timeoutId = setTimeout(() => reject(new Error("Approval request timed out")), approvalRequest.timeout);
|
|
1018
|
+
});
|
|
1019
|
+
try {
|
|
1020
|
+
const response = await Promise.race([
|
|
1021
|
+
handler(approvalRequest),
|
|
1022
|
+
timeoutPromise
|
|
1023
|
+
]);
|
|
1024
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
1025
|
+
return {
|
|
1026
|
+
...response,
|
|
1027
|
+
timestamp: Date.now()
|
|
1028
|
+
};
|
|
1029
|
+
} catch (error) {
|
|
1030
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
1031
|
+
throw new Error(`Approval request failed: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
_ts_decorate4([
|
|
1036
|
+
RuntimeMethod("Request approval from a human", {
|
|
1037
|
+
message: {
|
|
1038
|
+
description: "The message to display to the user"
|
|
1039
|
+
},
|
|
1040
|
+
context: {
|
|
1041
|
+
description: "Optional context information about what needs approval",
|
|
1042
|
+
optional: true,
|
|
1043
|
+
type: "Record<string, unknown>"
|
|
1044
|
+
}
|
|
1045
|
+
}),
|
|
1046
|
+
_ts_metadata4("design:type", Function),
|
|
1047
|
+
_ts_metadata4("design:paramtypes", [
|
|
1048
|
+
String,
|
|
1049
|
+
typeof Record === "undefined" ? Object : Record
|
|
1050
|
+
]),
|
|
1051
|
+
_ts_metadata4("design:returntype", Promise)
|
|
1052
|
+
], ApprovalAPI.prototype, "request", null);
|
|
1053
|
+
ApprovalAPI = _ts_decorate4([
|
|
1054
|
+
RuntimeAPI("approval", "Approval API - Request explicit human approval for sensitive operations")
|
|
1055
|
+
], ApprovalAPI);
|
|
1056
|
+
var approval = new ApprovalAPI();
|
|
1057
|
+
|
|
1058
|
+
// src/embedding/utils.ts
|
|
1059
|
+
function cosineSimilarity(vec1, vec2) {
|
|
1060
|
+
if (vec1.length !== vec2.length) {
|
|
1061
|
+
throw new Error(`Vectors must have the same length (${vec1.length} vs ${vec2.length})`);
|
|
1062
|
+
}
|
|
1063
|
+
let dotProduct = 0;
|
|
1064
|
+
let norm1 = 0;
|
|
1065
|
+
let norm2 = 0;
|
|
1066
|
+
for (let i = 0; i < vec1.length; i++) {
|
|
1067
|
+
dotProduct += vec1[i] * vec2[i];
|
|
1068
|
+
norm1 += vec1[i] * vec1[i];
|
|
1069
|
+
norm2 += vec2[i] * vec2[i];
|
|
1070
|
+
}
|
|
1071
|
+
const denominator = Math.sqrt(norm1) * Math.sqrt(norm2);
|
|
1072
|
+
if (denominator === 0) {
|
|
1073
|
+
return 0;
|
|
1074
|
+
}
|
|
1075
|
+
return dotProduct / denominator;
|
|
1076
|
+
}
|
|
1077
|
+
__name(cosineSimilarity, "cosineSimilarity");
|
|
1078
|
+
function generateEmbeddingId(index = 0) {
|
|
1079
|
+
return `emb_${Date.now()}_${index}_${Math.random().toString(36).slice(2)}`;
|
|
1080
|
+
}
|
|
1081
|
+
__name(generateEmbeddingId, "generateEmbeddingId");
|
|
1082
|
+
|
|
1083
|
+
// src/embedding/vector-store.ts
|
|
1084
|
+
var VectorStore = class {
|
|
1085
|
+
static {
|
|
1086
|
+
__name(this, "VectorStore");
|
|
1087
|
+
}
|
|
1088
|
+
records = /* @__PURE__ */ new Map();
|
|
1089
|
+
queryEmbedding = null;
|
|
1090
|
+
/**
|
|
1091
|
+
* Store embeddings from client response
|
|
1092
|
+
*/
|
|
1093
|
+
store(id, text, embedding2, metadata) {
|
|
1094
|
+
this.records.set(id, {
|
|
1095
|
+
id,
|
|
1096
|
+
text,
|
|
1097
|
+
embedding: embedding2,
|
|
1098
|
+
metadata
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Store multiple embeddings
|
|
1103
|
+
*/
|
|
1104
|
+
storeBatch(records) {
|
|
1105
|
+
for (const record of records) {
|
|
1106
|
+
this.records.set(record.id, record);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
* Set the query embedding for search
|
|
1111
|
+
*/
|
|
1112
|
+
setQueryEmbedding(embedding2) {
|
|
1113
|
+
this.queryEmbedding = embedding2;
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Search stored embeddings by similarity to query
|
|
1117
|
+
*/
|
|
1118
|
+
search(options) {
|
|
1119
|
+
if (!this.queryEmbedding) {
|
|
1120
|
+
throw new Error("No query embedding set. Call embed() with query first.");
|
|
1121
|
+
}
|
|
1122
|
+
const topK = options.topK ?? 5;
|
|
1123
|
+
const minSimilarity = options.minSimilarity ?? 0;
|
|
1124
|
+
const results = [];
|
|
1125
|
+
for (const record of this.records.values()) {
|
|
1126
|
+
if (options.filter && record.metadata) {
|
|
1127
|
+
let matches = true;
|
|
1128
|
+
for (const [key, value] of Object.entries(options.filter)) {
|
|
1129
|
+
if (record.metadata[key] !== value) {
|
|
1130
|
+
matches = false;
|
|
1131
|
+
break;
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
if (!matches) continue;
|
|
1135
|
+
}
|
|
1136
|
+
const similarity = cosineSimilarity(this.queryEmbedding, record.embedding);
|
|
1137
|
+
if (similarity >= minSimilarity) {
|
|
1138
|
+
results.push({
|
|
1139
|
+
id: record.id,
|
|
1140
|
+
text: record.text,
|
|
1141
|
+
similarity,
|
|
1142
|
+
metadata: record.metadata
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
results.sort((a, b) => b.similarity - a.similarity);
|
|
1147
|
+
return results.slice(0, topK);
|
|
1148
|
+
}
|
|
1149
|
+
/**
|
|
1150
|
+
* Get all stored embeddings
|
|
1151
|
+
*/
|
|
1152
|
+
getAll() {
|
|
1153
|
+
return Array.from(this.records.values());
|
|
1154
|
+
}
|
|
1155
|
+
/**
|
|
1156
|
+
* Get embedding by ID
|
|
1157
|
+
*/
|
|
1158
|
+
get(id) {
|
|
1159
|
+
return this.records.get(id);
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Clear all stored embeddings
|
|
1163
|
+
*/
|
|
1164
|
+
clear() {
|
|
1165
|
+
this.records.clear();
|
|
1166
|
+
this.queryEmbedding = null;
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Get count of stored embeddings
|
|
1170
|
+
*/
|
|
1171
|
+
count() {
|
|
1172
|
+
return this.records.size;
|
|
1173
|
+
}
|
|
1174
|
+
};
|
|
1175
|
+
var vectorStores = /* @__PURE__ */ new Map();
|
|
1176
|
+
var currentVectorStoreExecutionId = null;
|
|
1177
|
+
function setVectorStoreExecutionId(executionId) {
|
|
1178
|
+
currentVectorStoreExecutionId = executionId;
|
|
1179
|
+
}
|
|
1180
|
+
__name(setVectorStoreExecutionId, "setVectorStoreExecutionId");
|
|
1181
|
+
function clearVectorStoreExecutionId() {
|
|
1182
|
+
currentVectorStoreExecutionId = null;
|
|
1183
|
+
}
|
|
1184
|
+
__name(clearVectorStoreExecutionId, "clearVectorStoreExecutionId");
|
|
1185
|
+
function initializeVectorStore(executionId) {
|
|
1186
|
+
const id = executionId || currentVectorStoreExecutionId;
|
|
1187
|
+
if (!id) {
|
|
1188
|
+
throw new Error("No execution ID set for vector store");
|
|
1189
|
+
}
|
|
1190
|
+
vectorStores.set(id, new VectorStore());
|
|
1191
|
+
}
|
|
1192
|
+
__name(initializeVectorStore, "initializeVectorStore");
|
|
1193
|
+
function clearVectorStore(executionId) {
|
|
1194
|
+
const id = executionId || currentVectorStoreExecutionId;
|
|
1195
|
+
if (!id) return;
|
|
1196
|
+
const store = vectorStores.get(id);
|
|
1197
|
+
if (store) {
|
|
1198
|
+
store.clear();
|
|
1199
|
+
vectorStores.delete(id);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
__name(clearVectorStore, "clearVectorStore");
|
|
1203
|
+
function getVectorStore(executionId) {
|
|
1204
|
+
const id = executionId || currentVectorStoreExecutionId;
|
|
1205
|
+
if (!id) {
|
|
1206
|
+
throw new Error("No execution ID set for vector store");
|
|
1207
|
+
}
|
|
1208
|
+
let store = vectorStores.get(id);
|
|
1209
|
+
if (!store) {
|
|
1210
|
+
store = new VectorStore();
|
|
1211
|
+
vectorStores.set(id, store);
|
|
1212
|
+
}
|
|
1213
|
+
return store;
|
|
1214
|
+
}
|
|
1215
|
+
__name(getVectorStore, "getVectorStore");
|
|
1216
|
+
|
|
1217
|
+
// src/embedding/index.ts
|
|
1218
|
+
function _ts_decorate5(decorators, target, key, desc) {
|
|
1219
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1220
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1221
|
+
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;
|
|
1222
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1223
|
+
}
|
|
1224
|
+
__name(_ts_decorate5, "_ts_decorate");
|
|
1225
|
+
function _ts_metadata5(k, v) {
|
|
1226
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
1227
|
+
}
|
|
1228
|
+
__name(_ts_metadata5, "_ts_metadata");
|
|
1229
|
+
var EmbeddingAPI = class EmbeddingAPI2 {
|
|
1230
|
+
static {
|
|
1231
|
+
__name(this, "EmbeddingAPI");
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Request client to generate embedding and store it
|
|
1235
|
+
* For batch inputs, returns array of IDs for stored embeddings
|
|
1236
|
+
*/
|
|
1237
|
+
async embed(input, metadata) {
|
|
1238
|
+
const isBatch = Array.isArray(input);
|
|
1239
|
+
const texts = isBatch ? input : [
|
|
1240
|
+
input
|
|
1241
|
+
];
|
|
1242
|
+
const ids = texts.map((_, i) => generateEmbeddingId(i));
|
|
1243
|
+
const currentSequence = nextSequenceNumber();
|
|
1244
|
+
const cachedResult = getCachedResult(currentSequence);
|
|
1245
|
+
if (cachedResult !== void 0 && cachedResult !== null) {
|
|
1246
|
+
const vectorStore = getVectorStore();
|
|
1247
|
+
const embedding2 = cachedResult;
|
|
1248
|
+
for (let i = 0; i < texts.length; i++) {
|
|
1249
|
+
vectorStore.store(ids[i], texts[i], embedding2, metadata);
|
|
1250
|
+
}
|
|
1251
|
+
return isBatch ? ids : ids[0];
|
|
1252
|
+
}
|
|
1253
|
+
if (shouldPauseForClient()) {
|
|
1254
|
+
pauseForCallback(exports.CallbackType.EMBEDDING, exports.EmbeddingOperation.EMBED, {
|
|
1255
|
+
text: isBatch ? texts.join("\n") : texts[0],
|
|
1256
|
+
input,
|
|
1257
|
+
ids,
|
|
1258
|
+
metadata,
|
|
1259
|
+
sequenceNumber: currentSequence
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1262
|
+
throw new Error("Embedding service not provided by client");
|
|
1263
|
+
}
|
|
1264
|
+
/**
|
|
1265
|
+
* Search stored embeddings by similarity
|
|
1266
|
+
* Query must be embedded first via embed()
|
|
1267
|
+
*/
|
|
1268
|
+
async search(query, options) {
|
|
1269
|
+
const currentSequence = nextSequenceNumber();
|
|
1270
|
+
const vectorStore = getVectorStore();
|
|
1271
|
+
const cachedQueryEmbedding = getCachedResult(currentSequence);
|
|
1272
|
+
if (cachedQueryEmbedding !== void 0 && cachedQueryEmbedding !== null) {
|
|
1273
|
+
vectorStore.setQueryEmbedding(cachedQueryEmbedding);
|
|
1274
|
+
const searchOptions = {
|
|
1275
|
+
...options,
|
|
1276
|
+
query
|
|
1277
|
+
};
|
|
1278
|
+
if (options?.collection) {
|
|
1279
|
+
searchOptions.filter = {
|
|
1280
|
+
...searchOptions.filter,
|
|
1281
|
+
collection: options.collection
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
return vectorStore.search(searchOptions);
|
|
1285
|
+
}
|
|
1286
|
+
if (shouldPauseForClient()) {
|
|
1287
|
+
pauseForCallback(exports.CallbackType.EMBEDDING, exports.EmbeddingOperation.SEARCH, {
|
|
1288
|
+
query,
|
|
1289
|
+
options: {
|
|
1290
|
+
...options,
|
|
1291
|
+
query
|
|
1292
|
+
},
|
|
1293
|
+
sequenceNumber: currentSequence
|
|
1294
|
+
});
|
|
1295
|
+
}
|
|
1296
|
+
throw new Error("Embedding service not provided by client");
|
|
1297
|
+
}
|
|
1298
|
+
/**
|
|
1299
|
+
* Calculate cosine similarity between two embedding vectors
|
|
1300
|
+
* This is a utility function that doesn't require client interaction
|
|
1301
|
+
*/
|
|
1302
|
+
similarity(embedding1, embedding2) {
|
|
1303
|
+
return cosineSimilarity(embedding1, embedding2);
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* Get all stored embeddings (useful for debugging)
|
|
1307
|
+
*/
|
|
1308
|
+
getAll() {
|
|
1309
|
+
return getVectorStore().getAll();
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Get count of stored embeddings
|
|
1313
|
+
*/
|
|
1314
|
+
count() {
|
|
1315
|
+
return getVectorStore().count();
|
|
1316
|
+
}
|
|
1317
|
+
};
|
|
1318
|
+
_ts_decorate5([
|
|
1319
|
+
RuntimeMethod("Request client to generate and store embeddings", {
|
|
1320
|
+
input: {
|
|
1321
|
+
description: "Text(s) to embed",
|
|
1322
|
+
type: "string | string[]"
|
|
1323
|
+
},
|
|
1324
|
+
metadata: {
|
|
1325
|
+
description: "Optional metadata to store with embeddings",
|
|
1326
|
+
optional: true,
|
|
1327
|
+
type: "Record<string, unknown>"
|
|
1328
|
+
}
|
|
1329
|
+
}),
|
|
1330
|
+
_ts_metadata5("design:type", Function),
|
|
1331
|
+
_ts_metadata5("design:paramtypes", [
|
|
1332
|
+
Object,
|
|
1333
|
+
typeof Record === "undefined" ? Object : Record
|
|
1334
|
+
]),
|
|
1335
|
+
_ts_metadata5("design:returntype", Promise)
|
|
1336
|
+
], EmbeddingAPI.prototype, "embed", null);
|
|
1337
|
+
_ts_decorate5([
|
|
1338
|
+
RuntimeMethod("Search stored embeddings by similarity", {
|
|
1339
|
+
query: {
|
|
1340
|
+
description: "Search query text (will be embedded by client)"
|
|
1341
|
+
},
|
|
1342
|
+
options: {
|
|
1343
|
+
description: "Search options (topK, minSimilarity, filter)",
|
|
1344
|
+
optional: true,
|
|
1345
|
+
type: "SearchOptions"
|
|
1346
|
+
}
|
|
1347
|
+
}),
|
|
1348
|
+
_ts_metadata5("design:type", Function),
|
|
1349
|
+
_ts_metadata5("design:paramtypes", [
|
|
1350
|
+
String,
|
|
1351
|
+
typeof Omit === "undefined" ? Object : Omit
|
|
1352
|
+
]),
|
|
1353
|
+
_ts_metadata5("design:returntype", Promise)
|
|
1354
|
+
], EmbeddingAPI.prototype, "search", null);
|
|
1355
|
+
_ts_decorate5([
|
|
1356
|
+
RuntimeMethod("Calculate cosine similarity between two embedding vectors", {
|
|
1357
|
+
embedding1: {
|
|
1358
|
+
description: "First embedding vector",
|
|
1359
|
+
type: "number[]"
|
|
1360
|
+
},
|
|
1361
|
+
embedding2: {
|
|
1362
|
+
description: "Second embedding vector",
|
|
1363
|
+
type: "number[]"
|
|
1364
|
+
}
|
|
1365
|
+
}),
|
|
1366
|
+
_ts_metadata5("design:type", Function),
|
|
1367
|
+
_ts_metadata5("design:paramtypes", [
|
|
1368
|
+
Array,
|
|
1369
|
+
Array
|
|
1370
|
+
]),
|
|
1371
|
+
_ts_metadata5("design:returntype", Number)
|
|
1372
|
+
], EmbeddingAPI.prototype, "similarity", null);
|
|
1373
|
+
_ts_decorate5([
|
|
1374
|
+
RuntimeMethod("Get all stored embeddings"),
|
|
1375
|
+
_ts_metadata5("design:type", Function),
|
|
1376
|
+
_ts_metadata5("design:paramtypes", []),
|
|
1377
|
+
_ts_metadata5("design:returntype", Array)
|
|
1378
|
+
], EmbeddingAPI.prototype, "getAll", null);
|
|
1379
|
+
_ts_decorate5([
|
|
1380
|
+
RuntimeMethod("Get count of stored embeddings"),
|
|
1381
|
+
_ts_metadata5("design:type", Function),
|
|
1382
|
+
_ts_metadata5("design:paramtypes", []),
|
|
1383
|
+
_ts_metadata5("design:returntype", Number)
|
|
1384
|
+
], EmbeddingAPI.prototype, "count", null);
|
|
1385
|
+
EmbeddingAPI = _ts_decorate5([
|
|
1386
|
+
RuntimeAPI("embedding", "Embedding API - Client-side embedding with server-side vector storage")
|
|
1387
|
+
], EmbeddingAPI);
|
|
1388
|
+
var embedding = new EmbeddingAPI();
|
|
1389
|
+
|
|
1390
|
+
// src/metadata/generated.ts
|
|
1391
|
+
var GENERATED_METADATA = [
|
|
1392
|
+
{
|
|
1393
|
+
"name": "approval",
|
|
1394
|
+
"description": "Approval API - Request explicit human approval for sensitive operations",
|
|
1395
|
+
"methods": [
|
|
1396
|
+
{
|
|
1397
|
+
"name": "request",
|
|
1398
|
+
"description": "Request approval from a human",
|
|
1399
|
+
"params": [
|
|
1400
|
+
{
|
|
1401
|
+
"name": "message",
|
|
1402
|
+
"type": "string",
|
|
1403
|
+
"description": "The message to display to the user",
|
|
1404
|
+
"optional": false
|
|
1405
|
+
},
|
|
1406
|
+
{
|
|
1407
|
+
"name": "context",
|
|
1408
|
+
"type": "Record<string, unknown>",
|
|
1409
|
+
"description": "Optional context information about what needs approval",
|
|
1410
|
+
"optional": true
|
|
1411
|
+
}
|
|
1412
|
+
],
|
|
1413
|
+
"returns": "Promise<ApprovalResponse>"
|
|
1414
|
+
}
|
|
1415
|
+
]
|
|
1416
|
+
},
|
|
1417
|
+
{
|
|
1418
|
+
"name": "cache",
|
|
1419
|
+
"description": "Cache API - Store and retrieve data with optional TTL",
|
|
1420
|
+
"methods": [
|
|
1421
|
+
{
|
|
1422
|
+
"name": "get",
|
|
1423
|
+
"description": "Get a value from cache by key",
|
|
1424
|
+
"params": [
|
|
1425
|
+
{
|
|
1426
|
+
"name": "key",
|
|
1427
|
+
"type": "string",
|
|
1428
|
+
"description": "Cache key",
|
|
1429
|
+
"optional": false
|
|
1430
|
+
}
|
|
1431
|
+
],
|
|
1432
|
+
"returns": "Promise<T | null>"
|
|
1433
|
+
},
|
|
1434
|
+
{
|
|
1435
|
+
"name": "set",
|
|
1436
|
+
"description": "Set a value in cache with optional TTL",
|
|
1437
|
+
"params": [
|
|
1438
|
+
{
|
|
1439
|
+
"name": "key",
|
|
1440
|
+
"type": "string",
|
|
1441
|
+
"description": "Cache key",
|
|
1442
|
+
"optional": false
|
|
1443
|
+
},
|
|
1444
|
+
{
|
|
1445
|
+
"name": "value",
|
|
1446
|
+
"type": "unknown",
|
|
1447
|
+
"description": "Value to cache",
|
|
1448
|
+
"optional": false
|
|
1449
|
+
},
|
|
1450
|
+
{
|
|
1451
|
+
"name": "ttl",
|
|
1452
|
+
"type": "number",
|
|
1453
|
+
"description": "Time to live in seconds",
|
|
1454
|
+
"optional": true
|
|
1455
|
+
}
|
|
1456
|
+
],
|
|
1457
|
+
"returns": "Promise<void>"
|
|
1458
|
+
},
|
|
1459
|
+
{
|
|
1460
|
+
"name": "delete",
|
|
1461
|
+
"description": "Delete a value from cache",
|
|
1462
|
+
"params": [
|
|
1463
|
+
{
|
|
1464
|
+
"name": "key",
|
|
1465
|
+
"type": "string",
|
|
1466
|
+
"description": "Cache key to delete",
|
|
1467
|
+
"optional": false
|
|
1468
|
+
}
|
|
1469
|
+
],
|
|
1470
|
+
"returns": "Promise<void>"
|
|
1471
|
+
},
|
|
1472
|
+
{
|
|
1473
|
+
"name": "has",
|
|
1474
|
+
"description": "Check if a key exists in cache",
|
|
1475
|
+
"params": [
|
|
1476
|
+
{
|
|
1477
|
+
"name": "key",
|
|
1478
|
+
"type": "string",
|
|
1479
|
+
"description": "Cache key to check",
|
|
1480
|
+
"optional": false
|
|
1481
|
+
}
|
|
1482
|
+
],
|
|
1483
|
+
"returns": "Promise<boolean>"
|
|
1484
|
+
},
|
|
1485
|
+
{
|
|
1486
|
+
"name": "clear",
|
|
1487
|
+
"description": "Clear all cache entries",
|
|
1488
|
+
"params": [],
|
|
1489
|
+
"returns": "Promise<void>"
|
|
1490
|
+
}
|
|
1491
|
+
]
|
|
1492
|
+
},
|
|
1493
|
+
{
|
|
1494
|
+
"name": "embedding",
|
|
1495
|
+
"description": "Embedding API - Client-side embedding with server-side vector storage",
|
|
1496
|
+
"methods": [
|
|
1497
|
+
{
|
|
1498
|
+
"name": "embed",
|
|
1499
|
+
"description": "Request client to generate and store embeddings",
|
|
1500
|
+
"params": [
|
|
1501
|
+
{
|
|
1502
|
+
"name": "input",
|
|
1503
|
+
"type": "string | string[]",
|
|
1504
|
+
"description": "Text(s) to embed",
|
|
1505
|
+
"optional": false
|
|
1506
|
+
},
|
|
1507
|
+
{
|
|
1508
|
+
"name": "metadata",
|
|
1509
|
+
"type": "Record<string, unknown>",
|
|
1510
|
+
"description": "Optional metadata to store with embeddings",
|
|
1511
|
+
"optional": true
|
|
1512
|
+
}
|
|
1513
|
+
],
|
|
1514
|
+
"returns": "Promise<string | string[]>"
|
|
1515
|
+
},
|
|
1516
|
+
{
|
|
1517
|
+
"name": "search",
|
|
1518
|
+
"description": "Search stored embeddings by similarity",
|
|
1519
|
+
"params": [
|
|
1520
|
+
{
|
|
1521
|
+
"name": "query",
|
|
1522
|
+
"type": "string",
|
|
1523
|
+
"description": "Search query text (will be embedded by client)",
|
|
1524
|
+
"optional": false
|
|
1525
|
+
},
|
|
1526
|
+
{
|
|
1527
|
+
"name": "options",
|
|
1528
|
+
"type": "Omit<SearchOptions, 'query'>",
|
|
1529
|
+
"description": "Search options (topK, minSimilarity, filter)",
|
|
1530
|
+
"optional": true
|
|
1531
|
+
}
|
|
1532
|
+
],
|
|
1533
|
+
"returns": "Promise<SearchResult[]>"
|
|
1534
|
+
},
|
|
1535
|
+
{
|
|
1536
|
+
"name": "similarity",
|
|
1537
|
+
"description": "Calculate cosine similarity between two embedding vectors",
|
|
1538
|
+
"params": [
|
|
1539
|
+
{
|
|
1540
|
+
"name": "embedding1",
|
|
1541
|
+
"type": "number[]",
|
|
1542
|
+
"description": "First embedding vector",
|
|
1543
|
+
"optional": false
|
|
1544
|
+
},
|
|
1545
|
+
{
|
|
1546
|
+
"name": "embedding2",
|
|
1547
|
+
"type": "number[]",
|
|
1548
|
+
"description": "Second embedding vector",
|
|
1549
|
+
"optional": false
|
|
1550
|
+
}
|
|
1551
|
+
],
|
|
1552
|
+
"returns": "number"
|
|
1553
|
+
},
|
|
1554
|
+
{
|
|
1555
|
+
"name": "getAll",
|
|
1556
|
+
"description": "Get all stored embeddings",
|
|
1557
|
+
"params": [],
|
|
1558
|
+
"returns": "EmbeddingRecord[]"
|
|
1559
|
+
},
|
|
1560
|
+
{
|
|
1561
|
+
"name": "count",
|
|
1562
|
+
"description": "Get count of stored embeddings",
|
|
1563
|
+
"params": [],
|
|
1564
|
+
"returns": "number"
|
|
1565
|
+
}
|
|
1566
|
+
]
|
|
1567
|
+
},
|
|
1568
|
+
{
|
|
1569
|
+
"name": "llm",
|
|
1570
|
+
"description": "LLM API - Large Language Model calls using client-provided LLM (requires client.provideLLM())",
|
|
1571
|
+
"methods": [
|
|
1572
|
+
{
|
|
1573
|
+
"name": "call",
|
|
1574
|
+
"description": "Make an LLM call with a prompt",
|
|
1575
|
+
"params": [
|
|
1576
|
+
{
|
|
1577
|
+
"name": "options",
|
|
1578
|
+
"type": "LLMCallOptions",
|
|
1579
|
+
"description": "LLM call options including prompt",
|
|
1580
|
+
"optional": false
|
|
1581
|
+
}
|
|
1582
|
+
],
|
|
1583
|
+
"returns": "Promise<string>"
|
|
1584
|
+
},
|
|
1585
|
+
{
|
|
1586
|
+
"name": "extract",
|
|
1587
|
+
"description": "Extract structured data from text using an LLM",
|
|
1588
|
+
"params": [
|
|
1589
|
+
{
|
|
1590
|
+
"name": "options",
|
|
1591
|
+
"type": "LLMExtractOptions",
|
|
1592
|
+
"description": "Extraction options with JSON schema",
|
|
1593
|
+
"optional": false
|
|
1594
|
+
}
|
|
1595
|
+
],
|
|
1596
|
+
"returns": "Promise<T>"
|
|
1597
|
+
},
|
|
1598
|
+
{
|
|
1599
|
+
"name": "classify",
|
|
1600
|
+
"description": "Classify text into one of the provided categories",
|
|
1601
|
+
"params": [
|
|
1602
|
+
{
|
|
1603
|
+
"name": "options",
|
|
1604
|
+
"type": "LLMClassifyOptions",
|
|
1605
|
+
"description": "Classification options with categories",
|
|
1606
|
+
"optional": false
|
|
1607
|
+
}
|
|
1608
|
+
],
|
|
1609
|
+
"returns": "Promise<string>"
|
|
1610
|
+
}
|
|
1611
|
+
]
|
|
1612
|
+
},
|
|
1613
|
+
{
|
|
1614
|
+
"name": "progress",
|
|
1615
|
+
"description": "Progress API - Report execution progress to clients",
|
|
1616
|
+
"methods": [
|
|
1617
|
+
{
|
|
1618
|
+
"name": "report",
|
|
1619
|
+
"description": "Report progress with message and completion fraction",
|
|
1620
|
+
"params": [
|
|
1621
|
+
{
|
|
1622
|
+
"name": "message",
|
|
1623
|
+
"type": "string",
|
|
1624
|
+
"description": "Progress message",
|
|
1625
|
+
"optional": false
|
|
1626
|
+
},
|
|
1627
|
+
{
|
|
1628
|
+
"name": "fraction",
|
|
1629
|
+
"type": "number",
|
|
1630
|
+
"description": "Completion fraction (0-1)",
|
|
1631
|
+
"optional": false
|
|
1632
|
+
}
|
|
1633
|
+
],
|
|
1634
|
+
"returns": "void"
|
|
1635
|
+
}
|
|
1636
|
+
]
|
|
1637
|
+
}
|
|
1638
|
+
];
|
|
1639
|
+
var TYPE_REGISTRY = [
|
|
1640
|
+
{
|
|
1641
|
+
"name": "ApprovalResponse",
|
|
1642
|
+
"definition": "export interface ApprovalResponse<T = unknown> {\n approved: boolean;\n response?: T;\n timestamp: number;\n}"
|
|
1643
|
+
},
|
|
1644
|
+
{
|
|
1645
|
+
"name": "SearchOptions",
|
|
1646
|
+
"definition": "interface SearchOptions {\n query: string;\n topK?: number;\n minSimilarity?: number;\n filter?: Record<string, unknown>;\n}"
|
|
1647
|
+
},
|
|
1648
|
+
{
|
|
1649
|
+
"name": "SearchResult",
|
|
1650
|
+
"definition": "interface SearchResult {\n id: string;\n text: string;\n similarity: number;\n metadata?: Record<string, unknown>;\n}"
|
|
1651
|
+
},
|
|
1652
|
+
{
|
|
1653
|
+
"name": "EmbeddingRecord",
|
|
1654
|
+
"definition": "interface EmbeddingRecord {\n id: string;\n text: string;\n embedding: number[];\n metadata?: Record<string, unknown>;\n}"
|
|
1655
|
+
},
|
|
1656
|
+
{
|
|
1657
|
+
"name": "LLMCallOptions",
|
|
1658
|
+
"definition": "interface LLMCallOptions {\n prompt: string;\n context?: Record<string, unknown>;\n model?: string;\n maxTokens?: number;\n temperature?: number;\n systemPrompt?: string;\n}"
|
|
1659
|
+
},
|
|
1660
|
+
{
|
|
1661
|
+
"name": "LLMExtractOptions",
|
|
1662
|
+
"definition": "interface LLMExtractOptions {\n prompt: string;\n context?: Record<string, unknown>;\n schema: unknown;\n}"
|
|
1663
|
+
},
|
|
1664
|
+
{
|
|
1665
|
+
"name": "LLMClassifyOptions",
|
|
1666
|
+
"definition": "interface LLMClassifyOptions {\n text: string;\n categories: string[];\n context?: Record<string, unknown>;\n}"
|
|
1667
|
+
}
|
|
1668
|
+
];
|
|
1669
|
+
|
|
1670
|
+
// src/registry.ts
|
|
1671
|
+
function getAllAPIs() {
|
|
1672
|
+
return GENERATED_METADATA;
|
|
1673
|
+
}
|
|
1674
|
+
__name(getAllAPIs, "getAllAPIs");
|
|
1675
|
+
function getAPI(name) {
|
|
1676
|
+
return GENERATED_METADATA.find((api) => api.name === name);
|
|
1677
|
+
}
|
|
1678
|
+
__name(getAPI, "getAPI");
|
|
1679
|
+
|
|
1680
|
+
// src/metadata/index.ts
|
|
1681
|
+
function generateRuntimeTypes(apis, options) {
|
|
1682
|
+
let filteredApis = apis;
|
|
1683
|
+
if (options?.requestedApis && options.requestedApis.length > 0) {
|
|
1684
|
+
const requestedApis = options.requestedApis.map((api) => apis.find((a) => a.name === api));
|
|
1685
|
+
filteredApis = requestedApis.filter((api) => api !== void 0);
|
|
1686
|
+
} else if (options?.clientServices) {
|
|
1687
|
+
filteredApis = apis.filter((api) => {
|
|
1688
|
+
if (api.name === "llm" && !options.clientServices.hasLLM) return false;
|
|
1689
|
+
if (api.name === "approval" && !options.clientServices.hasApproval) return false;
|
|
1690
|
+
if (api.name === "embedding" && !options.clientServices.hasEmbedding) return false;
|
|
1691
|
+
if (api.name === "progress") return false;
|
|
1692
|
+
return true;
|
|
1693
|
+
});
|
|
1694
|
+
} else {
|
|
1695
|
+
filteredApis = apis.filter((api) => api.name === "cache");
|
|
1696
|
+
}
|
|
1697
|
+
let typescript = "// Runtime SDK Type Definitions\n\n";
|
|
1698
|
+
const usedTypes = /* @__PURE__ */ new Set();
|
|
1699
|
+
for (const api of filteredApis) {
|
|
1700
|
+
for (const method of api.methods) {
|
|
1701
|
+
const allTypes = [
|
|
1702
|
+
method.returns,
|
|
1703
|
+
...method.params.map((p) => p.type)
|
|
1704
|
+
].join(" ");
|
|
1705
|
+
const typeMatches = allTypes.match(/\b[A-Z][a-zA-Z]+\b/g);
|
|
1706
|
+
if (typeMatches) {
|
|
1707
|
+
typeMatches.forEach((t) => usedTypes.add(t));
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
for (const type of TYPE_REGISTRY) {
|
|
1712
|
+
const typeNameMatch = type.definition.match(/(?:interface|type)\s+([A-Z][a-zA-Z]+)/);
|
|
1713
|
+
const typeName = typeNameMatch?.[1];
|
|
1714
|
+
if (typeName && usedTypes.has(typeName)) {
|
|
1715
|
+
typescript += `${type.definition}
|
|
1716
|
+
|
|
1717
|
+
`;
|
|
1718
|
+
}
|
|
1719
|
+
}
|
|
1720
|
+
typescript += "// Runtime SDK\ndeclare const atp: {\n";
|
|
1721
|
+
for (const api of filteredApis) {
|
|
1722
|
+
typescript += ` /**
|
|
1723
|
+
`;
|
|
1724
|
+
for (const line of api.description.split("\n")) {
|
|
1725
|
+
typescript += ` * ${line}
|
|
1726
|
+
`;
|
|
1727
|
+
}
|
|
1728
|
+
typescript += ` */
|
|
1729
|
+
`;
|
|
1730
|
+
typescript += ` ${api.name}: {
|
|
1731
|
+
`;
|
|
1732
|
+
for (const method of api.methods) {
|
|
1733
|
+
typescript += ` /**
|
|
1734
|
+
`;
|
|
1735
|
+
typescript += ` * ${method.description}
|
|
1736
|
+
`;
|
|
1737
|
+
for (const param of method.params) {
|
|
1738
|
+
if (param.description) {
|
|
1739
|
+
typescript += ` * @param ${param.name} - ${param.description}
|
|
1740
|
+
`;
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
if (method.returns !== "void") {
|
|
1744
|
+
const returnDesc = method.returns.startsWith("Promise") ? "Promise resolving to result" : "Result value";
|
|
1745
|
+
typescript += ` * @returns ${returnDesc}
|
|
1746
|
+
`;
|
|
1747
|
+
}
|
|
1748
|
+
typescript += ` */
|
|
1749
|
+
`;
|
|
1750
|
+
const paramStrings = method.params.map((p) => {
|
|
1751
|
+
const optional = p.optional ? "?" : "";
|
|
1752
|
+
const type = p.type.includes("\n") ? p.type.replace(/\n/g, "\n ") : p.type;
|
|
1753
|
+
return `${p.name}${optional}: ${type}`;
|
|
1754
|
+
});
|
|
1755
|
+
typescript += ` ${method.name}(${paramStrings.join(", ")}): ${method.returns};
|
|
1756
|
+
`;
|
|
1757
|
+
typescript += `
|
|
1758
|
+
`;
|
|
1759
|
+
}
|
|
1760
|
+
typescript += ` };
|
|
1761
|
+
|
|
1762
|
+
`;
|
|
1763
|
+
}
|
|
1764
|
+
typescript += "};\n\n";
|
|
1765
|
+
return typescript;
|
|
1766
|
+
}
|
|
1767
|
+
__name(generateRuntimeTypes, "generateRuntimeTypes");
|
|
1768
|
+
|
|
1769
|
+
exports.GENERATED_METADATA = GENERATED_METADATA;
|
|
1770
|
+
exports.MemoryCacheBackend = MemoryCacheBackend;
|
|
1771
|
+
exports.PauseExecutionError = PauseExecutionError;
|
|
1772
|
+
exports.RedisCacheBackend = RedisCacheBackend;
|
|
1773
|
+
exports.RuntimeAPI = RuntimeAPI;
|
|
1774
|
+
exports.RuntimeMethod = RuntimeMethod;
|
|
1775
|
+
exports.VectorStore = VectorStore;
|
|
1776
|
+
exports.approval = approval;
|
|
1777
|
+
exports.cache = cache;
|
|
1778
|
+
exports.cleanupExecutionState = cleanupExecutionState;
|
|
1779
|
+
exports.cleanupOldExecutionStates = cleanupOldExecutionStates;
|
|
1780
|
+
exports.clearAPICallResults = clearAPICallResults;
|
|
1781
|
+
exports.clearCurrentExecutionId = clearCurrentExecutionId;
|
|
1782
|
+
exports.clearVectorStore = clearVectorStore;
|
|
1783
|
+
exports.clearVectorStoreExecutionId = clearVectorStoreExecutionId;
|
|
1784
|
+
exports.cosineSimilarity = cosineSimilarity;
|
|
1785
|
+
exports.embedding = embedding;
|
|
1786
|
+
exports.generateEmbeddingId = generateEmbeddingId;
|
|
1787
|
+
exports.generateRuntimeTypes = generateRuntimeTypes;
|
|
1788
|
+
exports.getAPI = getAPI;
|
|
1789
|
+
exports.getAPICallResults = getAPICallResults;
|
|
1790
|
+
exports.getAPIResultFromCache = getAPIResultFromCache;
|
|
1791
|
+
exports.getAllAPIs = getAllAPIs;
|
|
1792
|
+
exports.getCachedResult = getCachedResult;
|
|
1793
|
+
exports.getCallSequenceNumber = getCallSequenceNumber;
|
|
1794
|
+
exports.getClientLLMCallback = getClientLLMCallback;
|
|
1795
|
+
exports.getExecutionStateStats = getExecutionStateStats;
|
|
1796
|
+
exports.getVectorStore = getVectorStore;
|
|
1797
|
+
exports.initializeApproval = initializeApproval;
|
|
1798
|
+
exports.initializeCache = initializeCache;
|
|
1799
|
+
exports.initializeExecutionState = initializeExecutionState;
|
|
1800
|
+
exports.initializeLogger = initializeLogger;
|
|
1801
|
+
exports.initializeVectorStore = initializeVectorStore;
|
|
1802
|
+
exports.isPauseError = isPauseError;
|
|
1803
|
+
exports.isReplayMode = isReplayMode;
|
|
1804
|
+
exports.llm = llm;
|
|
1805
|
+
exports.log = log;
|
|
1806
|
+
exports.nextSequenceNumber = nextSequenceNumber;
|
|
1807
|
+
exports.pauseForCallback = pauseForCallback;
|
|
1808
|
+
exports.progress = progress;
|
|
1809
|
+
exports.resetAllExecutionState = resetAllExecutionState;
|
|
1810
|
+
exports.runInExecutionContext = runInExecutionContext;
|
|
1811
|
+
exports.setAPIResultCache = setAPIResultCache;
|
|
1812
|
+
exports.setClientLLMCallback = setClientLLMCallback;
|
|
1813
|
+
exports.setCurrentExecutionId = setCurrentExecutionId;
|
|
1814
|
+
exports.setPauseForClient = setPauseForClient;
|
|
1815
|
+
exports.setProgressCallback = setProgressCallback;
|
|
1816
|
+
exports.setReplayMode = setReplayMode;
|
|
1817
|
+
exports.setVectorStoreExecutionId = setVectorStoreExecutionId;
|
|
1818
|
+
exports.shouldPauseForClient = shouldPauseForClient;
|
|
1819
|
+
exports.shutdownLogger = shutdownLogger;
|
|
1820
|
+
exports.storeAPICallResult = storeAPICallResult;
|
|
1821
|
+
exports.storeAPIResultInCache = storeAPIResultInCache;
|
|
1822
|
+
exports.utils = utils;
|
|
1823
|
+
//# sourceMappingURL=index.cjs.map
|
|
1824
|
+
//# sourceMappingURL=index.cjs.map
|