@mondaydotcomorg/atp-runtime
Version:
Runtime SDK injected into sandbox for Agent Tool Protocol
1,700 lines (1,679 loc) • 54.4 kB
JavaScript
'use strict';
var async_hooks = require('async_hooks');
var pino = require('pino');
var NodeCache = require('node-cache');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var pino__default = /*#__PURE__*/_interopDefault(pino);
var NodeCache__default = /*#__PURE__*/_interopDefault(NodeCache);
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/pause/types.ts
exports.CallbackType = void 0;
(function(CallbackType2) {
CallbackType2["LLM"] = "llm";
CallbackType2["APPROVAL"] = "approval";
CallbackType2["EMBEDDING"] = "embedding";
CallbackType2["TOOL"] = "tool";
})(exports.CallbackType || (exports.CallbackType = {}));
exports.LLMOperation = void 0;
(function(LLMOperation2) {
LLMOperation2["CALL"] = "call";
LLMOperation2["EXTRACT"] = "extract";
LLMOperation2["CLASSIFY"] = "classify";
})(exports.LLMOperation || (exports.LLMOperation = {}));
exports.EmbeddingOperation = void 0;
(function(EmbeddingOperation2) {
EmbeddingOperation2["EMBED"] = "embed";
EmbeddingOperation2["SEARCH"] = "search";
})(exports.EmbeddingOperation || (exports.EmbeddingOperation = {}));
exports.ApprovalOperation = void 0;
(function(ApprovalOperation2) {
ApprovalOperation2["REQUEST"] = "request";
})(exports.ApprovalOperation || (exports.ApprovalOperation = {}));
exports.ToolOperation = void 0;
(function(ToolOperation2) {
ToolOperation2["CALL"] = "call";
})(exports.ToolOperation || (exports.ToolOperation = {}));
var PauseExecutionError = class extends Error {
static {
__name(this, "PauseExecutionError");
}
type;
operation;
payload;
constructor(type, operation, payload) {
super(`Execution paused: waiting for ${type}.${operation}`);
this.name = "PauseExecutionError";
this.type = type;
this.operation = operation;
this.payload = payload;
}
};
// src/pause/index.ts
function pauseForCallback(type, operation, payload) {
throw new PauseExecutionError(type, operation, payload);
}
__name(pauseForCallback, "pauseForCallback");
function isPauseError(error) {
return error instanceof PauseExecutionError;
}
__name(isPauseError, "isPauseError");
// src/metadata/decorators.ts
function RuntimeAPI(name, description) {
return function(constructor) {
constructor.API_NAME = name;
constructor.API_DESCRIPTION = description;
return constructor;
};
}
__name(RuntimeAPI, "RuntimeAPI");
function RuntimeMethod(description, paramDescriptions) {
return function(target, propertyKey, descriptor) {
if (!target.constructor.__methods) {
target.constructor.__methods = {};
}
target.constructor.__methods[propertyKey] = {
description,
paramDescriptions: paramDescriptions || {}
};
return descriptor;
};
}
__name(RuntimeMethod, "RuntimeMethod");
var logger = null;
function initializeLogger(config) {
const pinoLevel = config?.level === "none" ? "silent" : config?.level ?? "info";
const options = {
level: pinoLevel,
timestamp: pino__default.default.stdTimeFunctions.isoTime,
formatters: {
level: /* @__PURE__ */ __name((label) => {
return {
level: label
};
}, "level")
},
redact: {
paths: config?.redact ?? [
"apiKey",
"password",
"*.apiKey",
"*.password",
"authorization"
],
censor: "[REDACTED]"
}
};
if (config?.pretty) {
logger = pino__default.default({
...options,
transport: {
target: "pino-pretty",
options: {
colorize: true,
translateTime: "SYS:standard",
ignore: "pid,hostname"
}
}
});
} else if (config?.destination && config.destination !== "stdout") {
logger = pino__default.default(options, pino__default.default.destination(config.destination));
} else {
logger = pino__default.default(options);
}
}
__name(initializeLogger, "initializeLogger");
function getLogger() {
if (!logger) {
initializeLogger({
level: "info",
pretty: false
});
}
return logger;
}
__name(getLogger, "getLogger");
var log = {
/**
* Logs an informational message
*/
info(message, data) {
const l = getLogger();
if (data) {
l.info(data, message);
} else {
l.info(message);
}
},
/**
* Logs a warning message
*/
warn(message, data) {
const l = getLogger();
if (data) {
l.warn(data, message);
} else {
l.warn(message);
}
},
/**
* Logs an error message
*/
error(message, data) {
const l = getLogger();
if (data) {
l.error(data, message);
} else {
l.error(message);
}
},
/**
* Logs a debug message
*/
debug(message, data) {
const l = getLogger();
if (data) {
l.debug(data, message);
} else {
l.debug(message);
}
},
/**
* Logs a fatal error message
*/
fatal(message, data) {
const l = getLogger();
if (data) {
l.fatal(data, message);
} else {
l.fatal(message);
}
},
/**
* Creates a child logger with additional context
*/
child(bindings) {
const childLogger = getLogger().child(bindings);
return {
info: /* @__PURE__ */ __name((message, data) => {
if (data) {
childLogger.info(data, message);
} else {
childLogger.info(message);
}
}, "info"),
warn: /* @__PURE__ */ __name((message, data) => {
if (data) {
childLogger.warn(data, message);
} else {
childLogger.warn(message);
}
}, "warn"),
error: /* @__PURE__ */ __name((message, data) => {
if (data) {
childLogger.error(data, message);
} else {
childLogger.error(message);
}
}, "error"),
debug: /* @__PURE__ */ __name((message, data) => {
if (data) {
childLogger.debug(data, message);
} else {
childLogger.debug(message);
}
}, "debug"),
fatal: /* @__PURE__ */ __name((message, data) => {
if (data) {
childLogger.fatal(data, message);
} else {
childLogger.fatal(message);
}
}, "fatal"),
child: log.child
};
}
};
function shutdownLogger() {
logger = null;
}
__name(shutdownLogger, "shutdownLogger");
// src/llm/replay.ts
var executionStates = /* @__PURE__ */ new Map();
var MAX_EXECUTION_STATES = 100;
var operationCounter = 0;
var CLEANUP_CHECK_INTERVAL = 50;
var executionContext = new async_hooks.AsyncLocalStorage();
var currentExecutionId = null;
function setCurrentExecutionId(executionId) {
currentExecutionId = executionId;
}
__name(setCurrentExecutionId, "setCurrentExecutionId");
function clearCurrentExecutionId() {
currentExecutionId = null;
}
__name(clearCurrentExecutionId, "clearCurrentExecutionId");
function getCurrentState() {
let executionId = currentExecutionId;
if (!executionId) {
executionId = executionContext.getStore() || null;
}
if (!executionId) {
throw new Error("No execution context set. Executor must call setCurrentExecutionId() before runtime API calls.");
}
operationCounter++;
if (operationCounter >= CLEANUP_CHECK_INTERVAL) {
operationCounter = 0;
autoCleanup();
}
let state = executionStates.get(executionId);
if (!state) {
log.warn("State not initialized, creating with default. This should not happen.", {
executionId
});
state = {
shouldPauseForClient: false,
replayResults: void 0,
callSequenceNumber: 0,
apiCallResults: [],
apiResultCache: void 0,
createdAt: Date.now()
};
executionStates.set(executionId, state);
}
return state;
}
__name(getCurrentState, "getCurrentState");
function initializeExecutionState(shouldPause) {
const executionId = currentExecutionId || executionContext.getStore();
if (!executionId) {
throw new Error("No execution context set. Executor must call setCurrentExecutionId() before initializeExecutionState().");
}
const existingState = executionStates.get(executionId);
if (existingState) {
existingState.shouldPauseForClient = shouldPause;
if (!existingState.apiCallResults) {
existingState.apiCallResults = [];
}
if (!existingState.apiResultCache) {
existingState.apiResultCache = void 0;
}
return;
}
const state = {
shouldPauseForClient: shouldPause,
replayResults: void 0,
callSequenceNumber: 0,
apiCallResults: [],
apiResultCache: void 0,
createdAt: Date.now()
};
executionStates.set(executionId, state);
}
__name(initializeExecutionState, "initializeExecutionState");
function runInExecutionContext(executionId, fn) {
return executionContext.run(executionId, fn);
}
__name(runInExecutionContext, "runInExecutionContext");
function setPauseForClient(pause) {
const executionId = currentExecutionId || executionContext.getStore();
if (!executionId) {
throw new Error("No execution context set. Executor must call setCurrentExecutionId() before setPauseForClient().");
}
const state = executionStates.get(executionId);
if (!state) {
throw new Error("Execution state not initialized. Call initializeExecutionState() first.");
}
state.shouldPauseForClient = pause;
}
__name(setPauseForClient, "setPauseForClient");
function shouldPauseForClient() {
const state = getCurrentState();
return state.shouldPauseForClient;
}
__name(shouldPauseForClient, "shouldPauseForClient");
function setReplayMode(results) {
const state = getCurrentState();
state.replayResults = results;
state.callSequenceNumber = 0;
}
__name(setReplayMode, "setReplayMode");
function getCallSequenceNumber() {
const state = getCurrentState();
return state.callSequenceNumber;
}
__name(getCallSequenceNumber, "getCallSequenceNumber");
function nextSequenceNumber() {
const state = getCurrentState();
const current = state.callSequenceNumber;
state.callSequenceNumber++;
return current;
}
__name(nextSequenceNumber, "nextSequenceNumber");
function getCachedResult(sequenceNumber) {
const state = getCurrentState();
if (state.replayResults && state.replayResults.has(sequenceNumber)) {
return state.replayResults.get(sequenceNumber);
}
return void 0;
}
__name(getCachedResult, "getCachedResult");
function isReplayMode() {
return getCurrentState().replayResults !== void 0;
}
__name(isReplayMode, "isReplayMode");
function storeAPICallResult(record) {
const state = getCurrentState();
state.apiCallResults.push(record);
}
__name(storeAPICallResult, "storeAPICallResult");
function getAPICallResults() {
const state = getCurrentState();
return state.apiCallResults;
}
__name(getAPICallResults, "getAPICallResults");
function clearAPICallResults() {
const state = getCurrentState();
state.apiCallResults = [];
}
__name(clearAPICallResults, "clearAPICallResults");
function setAPIResultCache(cache2) {
const state = getCurrentState();
state.apiResultCache = cache2;
}
__name(setAPIResultCache, "setAPIResultCache");
function getAPIResultFromCache(operation) {
const state = getCurrentState();
return state.apiResultCache?.get(operation);
}
__name(getAPIResultFromCache, "getAPIResultFromCache");
function storeAPIResultInCache(operation, result) {
const state = getCurrentState();
if (!state.apiResultCache) {
state.apiResultCache = /* @__PURE__ */ new Map();
}
state.apiResultCache.set(operation, result);
}
__name(storeAPIResultInCache, "storeAPIResultInCache");
function cleanupExecutionState(executionId) {
executionStates.delete(executionId);
if (currentExecutionId === executionId) {
currentExecutionId = null;
}
}
__name(cleanupExecutionState, "cleanupExecutionState");
function autoCleanup() {
if (executionStates.size <= MAX_EXECUTION_STATES) {
return;
}
const entries = Array.from(executionStates.entries()).sort((a, b) => a[1].createdAt - b[1].createdAt);
const toRemove = executionStates.size - MAX_EXECUTION_STATES;
for (let i = 0; i < toRemove; i++) {
const entry = entries[i];
if (entry) {
executionStates.delete(entry[0]);
}
}
}
__name(autoCleanup, "autoCleanup");
function cleanupOldExecutionStates(maxAgeMs = 36e5) {
const now = Date.now();
let cleaned = 0;
for (const [executionId, state] of executionStates.entries()) {
const age = now - state.createdAt;
if (age > maxAgeMs) {
executionStates.delete(executionId);
cleaned++;
}
}
return cleaned;
}
__name(cleanupOldExecutionStates, "cleanupOldExecutionStates");
function resetAllExecutionState() {
executionStates.clear();
currentExecutionId = null;
}
__name(resetAllExecutionState, "resetAllExecutionState");
function getExecutionStateStats() {
const now = Date.now();
const executionIds = Array.from(executionStates.keys());
let oldestAge = null;
let newestAge = null;
for (const state of executionStates.values()) {
const age = now - state.createdAt;
if (oldestAge === null || age > oldestAge) {
oldestAge = age;
}
if (newestAge === null || age < newestAge) {
newestAge = age;
}
}
return {
totalStates: executionStates.size,
oldestStateAge: oldestAge,
newestStateAge: newestAge,
executionIds
};
}
__name(getExecutionStateStats, "getExecutionStateStats");
// src/llm/callback.ts
var clientLLMCallback;
function setClientLLMCallback(callback) {
clientLLMCallback = callback;
}
__name(setClientLLMCallback, "setClientLLMCallback");
function getClientLLMCallback() {
return clientLLMCallback;
}
__name(getClientLLMCallback, "getClientLLMCallback");
// src/llm/index.ts
function _ts_decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
__name(_ts_decorate, "_ts_decorate");
function _ts_metadata(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
__name(_ts_metadata, "_ts_metadata");
var LLMAPI = class LLMAPI2 {
static {
__name(this, "LLMAPI");
}
/**
* Makes a standard LLM call
* Always pauses execution and routes to client-provided LLM
*/
async call(options) {
const currentSequence = nextSequenceNumber();
const cachedResult = getCachedResult(currentSequence);
if (cachedResult !== void 0) {
return cachedResult;
}
pauseForCallback(exports.CallbackType.LLM, exports.LLMOperation.CALL, {
prompt: options.prompt,
options,
sequenceNumber: currentSequence
});
}
/**
* Extracts structured data using LLM
* Always pauses execution and routes to client-provided LLM
*/
async extract(options) {
const currentSequence = nextSequenceNumber();
const cachedResult = getCachedResult(currentSequence);
if (cachedResult !== void 0) {
return cachedResult;
}
pauseForCallback(exports.CallbackType.LLM, exports.LLMOperation.EXTRACT, {
prompt: options.prompt,
schema: options.schema,
options,
sequenceNumber: currentSequence
});
}
/**
* Classifies text into one of the provided categories
* Always pauses execution and routes to client-provided LLM
*/
async classify(options) {
const currentSequence = nextSequenceNumber();
const cachedResult = getCachedResult(currentSequence);
if (cachedResult !== void 0) {
return cachedResult;
}
pauseForCallback(exports.CallbackType.LLM, exports.LLMOperation.CLASSIFY, {
text: options.text,
categories: options.categories,
options,
sequenceNumber: currentSequence
});
}
};
_ts_decorate([
RuntimeMethod("Make an LLM call with a prompt", {
options: {
description: "LLM call options including prompt",
type: "LLMCallOptions"
}
}),
_ts_metadata("design:type", Function),
_ts_metadata("design:paramtypes", [
typeof LLMCallOptions === "undefined" ? Object : LLMCallOptions
]),
_ts_metadata("design:returntype", Promise)
], LLMAPI.prototype, "call", null);
_ts_decorate([
RuntimeMethod("Extract structured data from text using an LLM", {
options: {
description: "Extraction options with JSON schema",
type: "LLMExtractOptions"
}
}),
_ts_metadata("design:type", Function),
_ts_metadata("design:paramtypes", [
typeof LLMExtractOptions === "undefined" ? Object : LLMExtractOptions
]),
_ts_metadata("design:returntype", Promise)
], LLMAPI.prototype, "extract", null);
_ts_decorate([
RuntimeMethod("Classify text into one of the provided categories", {
options: {
description: "Classification options with categories",
type: "LLMClassifyOptions"
}
}),
_ts_metadata("design:type", Function),
_ts_metadata("design:paramtypes", [
typeof LLMClassifyOptions === "undefined" ? Object : LLMClassifyOptions
]),
_ts_metadata("design:returntype", Promise)
], LLMAPI.prototype, "classify", null);
LLMAPI = _ts_decorate([
RuntimeAPI("llm", "LLM API - Large Language Model calls using client-provided LLM (requires client.provideLLM())")
], LLMAPI);
var llm = new LLMAPI();
// src/progress/index.ts
function _ts_decorate2(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
__name(_ts_decorate2, "_ts_decorate");
function _ts_metadata2(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
__name(_ts_metadata2, "_ts_metadata");
var progressCallback = null;
function setProgressCallback(callback) {
progressCallback = callback;
}
__name(setProgressCallback, "setProgressCallback");
var ProgressAPI = class ProgressAPI2 {
static {
__name(this, "ProgressAPI");
}
/**
* Report progress with message and completion fraction
*/
report(message, fraction) {
if (progressCallback) {
try {
progressCallback(message, fraction);
} catch (error) {
log.error("Progress callback error", {
error
});
}
}
}
};
_ts_decorate2([
RuntimeMethod("Report progress with message and completion fraction", {
message: {
description: "Progress message"
},
fraction: {
description: "Completion fraction (0-1)"
}
}),
_ts_metadata2("design:type", Function),
_ts_metadata2("design:paramtypes", [
String,
Number
]),
_ts_metadata2("design:returntype", void 0)
], ProgressAPI.prototype, "report", null);
ProgressAPI = _ts_decorate2([
RuntimeAPI("progress", "Progress API - Report execution progress to clients")
], ProgressAPI);
var progress = new ProgressAPI();
var MemoryCacheBackend = class {
static {
__name(this, "MemoryCacheBackend");
}
cache;
constructor(config) {
this.cache = new NodeCache__default.default({
stdTTL: config?.defaultTTL ?? 600,
checkperiod: config?.checkPeriod ?? 120,
maxKeys: config?.maxKeys ?? 1e3,
useClones: false
});
}
async get(key) {
const value = this.cache.get(key);
return value ?? null;
}
async set(key, value, ttl) {
if (ttl) {
this.cache.set(key, value, ttl);
} else {
this.cache.set(key, value);
}
}
async delete(key) {
this.cache.del(key);
}
async has(key) {
return this.cache.has(key);
}
async clear() {
this.cache.flushAll();
}
};
var RedisCacheBackend = class {
static {
__name(this, "RedisCacheBackend");
}
client;
connected = false;
constructor(config) {
import('ioredis').then((Redis) => {
this.client = new Redis.default({
host: config.host,
port: config.port,
password: config.password,
db: config.db ?? 0,
retryStrategy: /* @__PURE__ */ __name((times) => {
if (times > 3) {
return null;
}
return Math.min(times * 100, 2e3);
}, "retryStrategy"),
lazyConnect: true
});
this.client.connect().then(() => {
this.connected = true;
}).catch(() => {
this.connected = false;
});
}).catch(() => {
throw new Error("ioredis package not installed. Install it with: yarn add ioredis");
});
}
async get(key) {
if (!this.connected) {
log.warn("Redis Cache not connected, cannot get key", {
key
});
return null;
}
try {
const value = await this.client.get(key);
return value ? JSON.parse(value) : null;
} catch (error) {
log.error("Redis Cache failed to get key", {
key,
error: error instanceof Error ? error.message : error
});
return null;
}
}
async set(key, value, ttl) {
if (!this.connected) {
log.warn("Redis Cache not connected, cannot set key", {
key
});
return;
}
try {
const serialized = JSON.stringify(value);
if (ttl) {
await this.client.setex(key, ttl, serialized);
} else {
await this.client.set(key, serialized);
}
} catch (error) {
log.error("Redis Cache failed to set key", {
key,
error: error instanceof Error ? error.message : error
});
}
}
async delete(key) {
if (!this.connected) {
log.warn("Redis Cache not connected, cannot delete key", {
key
});
return;
}
try {
await this.client.del(key);
} catch (error) {
log.error("Redis Cache failed to delete key", {
key,
error: error instanceof Error ? error.message : error
});
}
}
async has(key) {
if (!this.connected) {
log.warn("Redis Cache not connected, cannot check key", {
key
});
return false;
}
try {
const exists = await this.client.exists(key);
return exists === 1;
} catch (error) {
log.error("Redis Cache failed to check key", {
key,
error: error instanceof Error ? error.message : error
});
return false;
}
}
async clear() {
if (!this.connected) {
log.warn("Redis Cache not connected, cannot clear cache");
return;
}
try {
await this.client.flushdb();
} catch (error) {
log.error("Redis Cache failed to clear cache", {
error: error instanceof Error ? error.message : error
});
}
}
};
var cacheBackend = new MemoryCacheBackend();
function initializeCache(config) {
if (config.type === "redis" && config.redis) {
cacheBackend = new RedisCacheBackend(config.redis);
} else {
cacheBackend = new MemoryCacheBackend({
maxKeys: config.maxKeys,
defaultTTL: config.defaultTTL,
checkPeriod: config.checkPeriod
});
}
}
__name(initializeCache, "initializeCache");
function getCacheBackend() {
return cacheBackend;
}
__name(getCacheBackend, "getCacheBackend");
// src/cache/index.ts
function _ts_decorate3(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
__name(_ts_decorate3, "_ts_decorate");
function _ts_metadata3(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
__name(_ts_metadata3, "_ts_metadata");
var CacheAPI = class CacheAPI2 {
static {
__name(this, "CacheAPI");
}
/**
* Gets a value from cache
*/
async get(key) {
return getCacheBackend().get(key);
}
/**
* Sets a value in cache with optional TTL in seconds
*/
async set(key, value, ttl) {
return getCacheBackend().set(key, value, ttl);
}
/**
* Deletes a value from cache
*/
async delete(key) {
return getCacheBackend().delete(key);
}
/**
* Checks if a key exists in cache
*/
async has(key) {
return getCacheBackend().has(key);
}
/**
* Clears all cache entries
*/
async clear() {
return getCacheBackend().clear();
}
};
_ts_decorate3([
RuntimeMethod("Get a value from cache by key", {
key: {
description: "Cache key"
}
}),
_ts_metadata3("design:type", Function),
_ts_metadata3("design:paramtypes", [
String
]),
_ts_metadata3("design:returntype", Promise)
], CacheAPI.prototype, "get", null);
_ts_decorate3([
RuntimeMethod("Set a value in cache with optional TTL", {
key: {
description: "Cache key"
},
value: {
description: "Value to cache",
type: "unknown"
},
ttl: {
description: "Time to live in seconds",
optional: true
}
}),
_ts_metadata3("design:type", Function),
_ts_metadata3("design:paramtypes", [
String,
Object,
Number
]),
_ts_metadata3("design:returntype", Promise)
], CacheAPI.prototype, "set", null);
_ts_decorate3([
RuntimeMethod("Delete a value from cache", {
key: {
description: "Cache key to delete"
}
}),
_ts_metadata3("design:type", Function),
_ts_metadata3("design:paramtypes", [
String
]),
_ts_metadata3("design:returntype", Promise)
], CacheAPI.prototype, "delete", null);
_ts_decorate3([
RuntimeMethod("Check if a key exists in cache", {
key: {
description: "Cache key to check"
}
}),
_ts_metadata3("design:type", Function),
_ts_metadata3("design:paramtypes", [
String
]),
_ts_metadata3("design:returntype", Promise)
], CacheAPI.prototype, "has", null);
_ts_decorate3([
RuntimeMethod("Clear all cache entries"),
_ts_metadata3("design:type", Function),
_ts_metadata3("design:paramtypes", []),
_ts_metadata3("design:returntype", Promise)
], CacheAPI.prototype, "clear", null);
CacheAPI = _ts_decorate3([
RuntimeAPI("cache", "Cache API - Store and retrieve data with optional TTL")
], CacheAPI);
var cache = new CacheAPI();
// src/utils.ts
var utils = {
async sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
},
async retry(fn, options) {
let lastError;
for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error;
if (attempt < options.maxAttempts) {
await this.sleep(options.delayMs);
}
}
}
throw lastError;
},
async parallel(tasks) {
return Promise.all(tasks.map((task) => task()));
},
async sequence(tasks) {
const results = [];
for (const task of tasks) {
results.push(await task());
}
return results;
}
};
// src/approval/handler.ts
var approvalHandler = null;
function initializeApproval(handler) {
approvalHandler = handler;
}
__name(initializeApproval, "initializeApproval");
function getApprovalHandler() {
return approvalHandler;
}
__name(getApprovalHandler, "getApprovalHandler");
// src/approval/index.ts
function _ts_decorate4(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
__name(_ts_decorate4, "_ts_decorate");
function _ts_metadata4(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
__name(_ts_metadata4, "_ts_metadata");
var ApprovalAPI = class ApprovalAPI2 {
static {
__name(this, "ApprovalAPI");
}
/**
* Request approval from a human
*/
async request(message, context) {
const currentSequence = nextSequenceNumber();
const cachedResult = getCachedResult(currentSequence);
if (cachedResult !== void 0) {
return cachedResult;
}
const shouldPause = shouldPauseForClient();
if (shouldPause) {
pauseForCallback(exports.CallbackType.APPROVAL, exports.ApprovalOperation.REQUEST, {
message,
context,
sequenceNumber: currentSequence
});
}
const handler = getApprovalHandler();
if (!handler) {
throw new Error("Approval handler not configured. Human approval is required but no handler is set.");
}
const approvalRequest = {
message,
context,
timeout: 3e5
};
let timeoutId = null;
const timeoutPromise = new Promise((_, reject) => {
timeoutId = setTimeout(() => reject(new Error("Approval request timed out")), approvalRequest.timeout);
});
try {
const response = await Promise.race([
handler(approvalRequest),
timeoutPromise
]);
if (timeoutId) clearTimeout(timeoutId);
return {
...response,
timestamp: Date.now()
};
} catch (error) {
if (timeoutId) clearTimeout(timeoutId);
throw new Error(`Approval request failed: ${error instanceof Error ? error.message : "Unknown error"}`);
}
}
};
_ts_decorate4([
RuntimeMethod("Request approval from a human", {
message: {
description: "The message to display to the user"
},
context: {
description: "Optional context information about what needs approval",
optional: true,
type: "Record<string, unknown>"
}
}),
_ts_metadata4("design:type", Function),
_ts_metadata4("design:paramtypes", [
String,
typeof Record === "undefined" ? Object : Record
]),
_ts_metadata4("design:returntype", Promise)
], ApprovalAPI.prototype, "request", null);
ApprovalAPI = _ts_decorate4([
RuntimeAPI("approval", "Approval API - Request explicit human approval for sensitive operations")
], ApprovalAPI);
var approval = new ApprovalAPI();
// src/embedding/utils.ts
function cosineSimilarity(vec1, vec2) {
if (vec1.length !== vec2.length) {
throw new Error(`Vectors must have the same length (${vec1.length} vs ${vec2.length})`);
}
let dotProduct = 0;
let norm1 = 0;
let norm2 = 0;
for (let i = 0; i < vec1.length; i++) {
dotProduct += vec1[i] * vec2[i];
norm1 += vec1[i] * vec1[i];
norm2 += vec2[i] * vec2[i];
}
const denominator = Math.sqrt(norm1) * Math.sqrt(norm2);
if (denominator === 0) {
return 0;
}
return dotProduct / denominator;
}
__name(cosineSimilarity, "cosineSimilarity");
function generateEmbeddingId(index = 0) {
return `emb_${Date.now()}_${index}_${Math.random().toString(36).slice(2)}`;
}
__name(generateEmbeddingId, "generateEmbeddingId");
// src/embedding/vector-store.ts
var VectorStore = class {
static {
__name(this, "VectorStore");
}
records = /* @__PURE__ */ new Map();
queryEmbedding = null;
/**
* Store embeddings from client response
*/
store(id, text, embedding2, metadata) {
this.records.set(id, {
id,
text,
embedding: embedding2,
metadata
});
}
/**
* Store multiple embeddings
*/
storeBatch(records) {
for (const record of records) {
this.records.set(record.id, record);
}
}
/**
* Set the query embedding for search
*/
setQueryEmbedding(embedding2) {
this.queryEmbedding = embedding2;
}
/**
* Search stored embeddings by similarity to query
*/
search(options) {
if (!this.queryEmbedding) {
throw new Error("No query embedding set. Call embed() with query first.");
}
const topK = options.topK ?? 5;
const minSimilarity = options.minSimilarity ?? 0;
const results = [];
for (const record of this.records.values()) {
if (options.filter && record.metadata) {
let matches = true;
for (const [key, value] of Object.entries(options.filter)) {
if (record.metadata[key] !== value) {
matches = false;
break;
}
}
if (!matches) continue;
}
const similarity = cosineSimilarity(this.queryEmbedding, record.embedding);
if (similarity >= minSimilarity) {
results.push({
id: record.id,
text: record.text,
similarity,
metadata: record.metadata
});
}
}
results.sort((a, b) => b.similarity - a.similarity);
return results.slice(0, topK);
}
/**
* Get all stored embeddings
*/
getAll() {
return Array.from(this.records.values());
}
/**
* Get embedding by ID
*/
get(id) {
return this.records.get(id);
}
/**
* Clear all stored embeddings
*/
clear() {
this.records.clear();
this.queryEmbedding = null;
}
/**
* Get count of stored embeddings
*/
count() {
return this.records.size;
}
};
var vectorStores = /* @__PURE__ */ new Map();
var currentVectorStoreExecutionId = null;
function setVectorStoreExecutionId(executionId) {
currentVectorStoreExecutionId = executionId;
}
__name(setVectorStoreExecutionId, "setVectorStoreExecutionId");
function clearVectorStoreExecutionId() {
currentVectorStoreExecutionId = null;
}
__name(clearVectorStoreExecutionId, "clearVectorStoreExecutionId");
function initializeVectorStore(executionId) {
const id = executionId || currentVectorStoreExecutionId;
if (!id) {
throw new Error("No execution ID set for vector store");
}
vectorStores.set(id, new VectorStore());
}
__name(initializeVectorStore, "initializeVectorStore");
function clearVectorStore(executionId) {
const id = executionId || currentVectorStoreExecutionId;
if (!id) return;
const store = vectorStores.get(id);
if (store) {
store.clear();
vectorStores.delete(id);
}
}
__name(clearVectorStore, "clearVectorStore");
function getVectorStore(executionId) {
const id = executionId || currentVectorStoreExecutionId;
if (!id) {
throw new Error("No execution ID set for vector store");
}
let store = vectorStores.get(id);
if (!store) {
store = new VectorStore();
vectorStores.set(id, store);
}
return store;
}
__name(getVectorStore, "getVectorStore");
// src/embedding/index.ts
function _ts_decorate5(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
__name(_ts_decorate5, "_ts_decorate");
function _ts_metadata5(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
__name(_ts_metadata5, "_ts_metadata");
var EmbeddingAPI = class EmbeddingAPI2 {
static {
__name(this, "EmbeddingAPI");
}
/**
* Request client to generate embedding and store it
* For batch inputs, returns array of IDs for stored embeddings
*/
async embed(input, metadata) {
const isBatch = Array.isArray(input);
const texts = isBatch ? input : [
input
];
const ids = texts.map((_, i) => generateEmbeddingId(i));
const currentSequence = nextSequenceNumber();
const cachedResult = getCachedResult(currentSequence);
if (cachedResult !== void 0 && cachedResult !== null) {
const vectorStore = getVectorStore();
const embedding2 = cachedResult;
for (let i = 0; i < texts.length; i++) {
vectorStore.store(ids[i], texts[i], embedding2, metadata);
}
return isBatch ? ids : ids[0];
}
if (shouldPauseForClient()) {
pauseForCallback(exports.CallbackType.EMBEDDING, exports.EmbeddingOperation.EMBED, {
text: isBatch ? texts.join("\n") : texts[0],
input,
ids,
metadata,
sequenceNumber: currentSequence
});
}
throw new Error("Embedding service not provided by client");
}
/**
* Search stored embeddings by similarity
* Query must be embedded first via embed()
*/
async search(query, options) {
const currentSequence = nextSequenceNumber();
const vectorStore = getVectorStore();
const cachedQueryEmbedding = getCachedResult(currentSequence);
if (cachedQueryEmbedding !== void 0 && cachedQueryEmbedding !== null) {
vectorStore.setQueryEmbedding(cachedQueryEmbedding);
const searchOptions = {
...options,
query
};
if (options?.collection) {
searchOptions.filter = {
...searchOptions.filter,
collection: options.collection
};
}
return vectorStore.search(searchOptions);
}
if (shouldPauseForClient()) {
pauseForCallback(exports.CallbackType.EMBEDDING, exports.EmbeddingOperation.SEARCH, {
query,
options: {
...options,
query
},
sequenceNumber: currentSequence
});
}
throw new Error("Embedding service not provided by client");
}
/**
* Calculate cosine similarity between two embedding vectors
* This is a utility function that doesn't require client interaction
*/
similarity(embedding1, embedding2) {
return cosineSimilarity(embedding1, embedding2);
}
/**
* Get all stored embeddings (useful for debugging)
*/
getAll() {
return getVectorStore().getAll();
}
/**
* Get count of stored embeddings
*/
count() {
return getVectorStore().count();
}
};
_ts_decorate5([
RuntimeMethod("Request client to generate and store embeddings", {
input: {
description: "Text(s) to embed",
type: "string | string[]"
},
metadata: {
description: "Optional metadata to store with embeddings",
optional: true,
type: "Record<string, unknown>"
}
}),
_ts_metadata5("design:type", Function),
_ts_metadata5("design:paramtypes", [
Object,
typeof Record === "undefined" ? Object : Record
]),
_ts_metadata5("design:returntype", Promise)
], EmbeddingAPI.prototype, "embed", null);
_ts_decorate5([
RuntimeMethod("Search stored embeddings by similarity", {
query: {
description: "Search query text (will be embedded by client)"
},
options: {
description: "Search options (topK, minSimilarity, filter)",
optional: true,
type: "SearchOptions"
}
}),
_ts_metadata5("design:type", Function),
_ts_metadata5("design:paramtypes", [
String,
typeof Omit === "undefined" ? Object : Omit
]),
_ts_metadata5("design:returntype", Promise)
], EmbeddingAPI.prototype, "search", null);
_ts_decorate5([
RuntimeMethod("Calculate cosine similarity between two embedding vectors", {
embedding1: {
description: "First embedding vector",
type: "number[]"
},
embedding2: {
description: "Second embedding vector",
type: "number[]"
}
}),
_ts_metadata5("design:type", Function),
_ts_metadata5("design:paramtypes", [
Array,
Array
]),
_ts_metadata5("design:returntype", Number)
], EmbeddingAPI.prototype, "similarity", null);
_ts_decorate5([
RuntimeMethod("Get all stored embeddings"),
_ts_metadata5("design:type", Function),
_ts_metadata5("design:paramtypes", []),
_ts_metadata5("design:returntype", Array)
], EmbeddingAPI.prototype, "getAll", null);
_ts_decorate5([
RuntimeMethod("Get count of stored embeddings"),
_ts_metadata5("design:type", Function),
_ts_metadata5("design:paramtypes", []),
_ts_metadata5("design:returntype", Number)
], EmbeddingAPI.prototype, "count", null);
EmbeddingAPI = _ts_decorate5([
RuntimeAPI("embedding", "Embedding API - Client-side embedding with server-side vector storage")
], EmbeddingAPI);
var embedding = new EmbeddingAPI();
// src/metadata/generated.ts
var GENERATED_METADATA = [
{
"name": "approval",
"description": "Approval API - Request explicit human approval for sensitive operations",
"methods": [
{
"name": "request",
"description": "Request approval from a human",
"params": [
{
"name": "message",
"type": "string",
"description": "The message to display to the user",
"optional": false
},
{
"name": "context",
"type": "Record<string, unknown>",
"description": "Optional context information about what needs approval",
"optional": true
}
],
"returns": "Promise<ApprovalResponse>"
}
]
},
{
"name": "cache",
"description": "Cache API - Store and retrieve data with optional TTL",
"methods": [
{
"name": "get",
"description": "Get a value from cache by key",
"params": [
{
"name": "key",
"type": "string",
"description": "Cache key",
"optional": false
}
],
"returns": "Promise<T | null>"
},
{
"name": "set",
"description": "Set a value in cache with optional TTL",
"params": [
{
"name": "key",
"type": "string",
"description": "Cache key",
"optional": false
},
{
"name": "value",
"type": "unknown",
"description": "Value to cache",
"optional": false
},
{
"name": "ttl",
"type": "number",
"description": "Time to live in seconds",
"optional": true
}
],
"returns": "Promise<void>"
},
{
"name": "delete",
"description": "Delete a value from cache",
"params": [
{
"name": "key",
"type": "string",
"description": "Cache key to delete",
"optional": false
}
],
"returns": "Promise<void>"
},
{
"name": "has",
"description": "Check if a key exists in cache",
"params": [
{
"name": "key",
"type": "string",
"description": "Cache key to check",
"optional": false
}
],
"returns": "Promise<boolean>"
},
{
"name": "clear",
"description": "Clear all cache entries",
"params": [],
"returns": "Promise<void>"
}
]
},
{
"name": "embedding",
"description": "Embedding API - Client-side embedding with server-side vector storage",
"methods": [
{
"name": "embed",
"description": "Request client to generate and store embeddings",
"params": [
{
"name": "input",
"type": "string | string[]",
"description": "Text(s) to embed",
"optional": false
},
{
"name": "metadata",
"type": "Record<string, unknown>",
"description": "Optional metadata to store with embeddings",
"optional": true
}
],
"returns": "Promise<string | string[]>"
},
{
"name": "search",
"description": "Search stored embeddings by similarity",
"params": [
{
"name": "query",
"type": "string",
"description": "Search query text (will be embedded by client)",
"optional": false
},
{
"name": "options",
"type": "Omit<SearchOptions, 'query'>",
"description": "Search options (topK, minSimilarity, filter)",
"optional": true
}
],
"returns": "Promise<SearchResult[]>"
},
{
"name": "similarity",
"description": "Calculate cosine similarity between two embedding vectors",
"params": [
{
"name": "embedding1",
"type": "number[]",
"description": "First embedding vector",
"optional": false
},
{
"name": "embedding2",
"type": "number[]",
"description": "Second embedding vector",
"optional": false
}
],
"returns": "number"
},
{
"name": "getAll",
"description": "Get all stored embeddings",
"params": [],
"returns": "EmbeddingRecord[]"
},
{
"name": "count",
"description": "Get count of stored embeddings",
"params": [],
"returns": "number"
}
]
},
{
"name": "llm",
"description": "LLM API - Large Language Model calls using client-provided LLM (requires client.provideLLM())",
"methods": [
{
"name": "call",
"description": "Make an LLM call with a prompt",
"params": [
{
"name": "options",
"type": "LLMCallOptions",
"description": "LLM call options including prompt",
"optional": false
}
],
"returns": "Promise<string>"
},
{
"name": "extract",
"description": "Extract structured data from text using an LLM",
"params": [
{
"name": "options",
"type": "LLMExtractOptions",
"description": "Extraction options with JSON schema",
"optional": false
}
],
"returns": "Promise<T>"
},
{
"name": "classify",
"description": "Classify text into one of the provided categories",
"params": [
{
"name": "options",
"type": "LLMClassifyOptions",
"description": "Classification options with categories",
"optional": false
}
],
"returns": "Promise<string>"
}
]
},
{
"name": "progress",
"description": "Progress API - Report execution progress to clients",
"methods": [
{
"name": "report",
"description": "Report progress with message and completion fraction",
"params": [
{
"name": "message",
"type": "string",
"description": "Progress message",
"optional": false
},
{
"name": "fraction",
"type": "number",
"description": "Completion fraction (0-1)",
"optional": false
}
],
"returns": "void"
}
]
}
];
var TYPE_REGISTRY = [
{
"name": "ApprovalResponse",
"definition": "export interface ApprovalResponse<T = unknown> {\n approved: boolean;\n response?: T;\n timestamp: number;\n}"
},
{
"name": "SearchOptions",
"definition": "interface SearchOptions {\n query: string;\n topK?: number;\n minSimilarity?: number;\n filter?: Record<string, unknown>;\n}"
},
{
"name": "SearchResult",
"definition": "interface SearchResult {\n id: string;\n text: string;\n similarity: number;\n metadata?: Record<string, unknown>;\n}"
},
{
"name": "EmbeddingRecord",
"definition": "interface EmbeddingRecord {\n id: string;\n text: string;\n embedding: number[];\n metadata?: Record<string, unknown>;\n}"
},
{
"name": "LLMCallOptions",
"definition": "interface LLMCallOptions {\n prompt: string;\n context?: Record<string, unknown>;\n model?: string;\n maxTokens?: number;\n temperature?: number;\n systemPrompt?: string;\n}"
},
{
"name": "LLMExtractOptions",
"definition": "interface LLMExtractOptions {\n prompt: string;\n context?: Record<string, unknown>;\n schema: unknown;\n}"
},
{
"name": "LLMClassifyOptions",
"definition": "interface LLMClassifyOptions {\n text: string;\n categories: string[];\n context?: Record<string, unknown>;\n}"
}
];
// src/registry.ts
function getAllAPIs() {
return GENERATED_METADATA;
}
__name(getAllAPIs, "getAllAPIs");
function getAPI(name) {
return GENERATED_METADATA.find((api) => api.name === name);
}
__name(getAPI, "getAPI");
// src/metadata/index.ts
function generateRuntimeTypes(apis, options) {
let filteredApis = apis;
if (options?.requestedApis && options.requestedApis.length > 0) {
const requestedApis = options.requestedApis.map((api) => apis.find((a) => a.name === api));
filteredApis = requestedApis.filter((api) => api !== void 0);
} else if (options?.clientServices) {
filteredApis = apis.filter((api) => {
if (api.name === "llm" && !options.clientServices.hasLLM) return false;
if (api.name === "approval" && !options.clientServices.hasApproval) return false;
if (api.name === "embedding" && !options.clientServices.hasEmbedding) return false;
if (api.name === "progress") return false;
return true;
});
} else {
filteredApis = apis.filter((api) => api.name === "cache");
}
let typescript = "// Runtime SDK Type Definitions\n\n";
const usedTypes = /* @__PURE__ */ new Set();
for (const api of filteredApis) {
for (con