UNPKG

@mondaydotcomorg/atp-runtime

Version:

Runtime SDK injected into sandbox for Agent Tool Protocol

162 lines 6.73 kB
var __decorate = (this && this.__decorate) || function (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; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { pauseForCallback, CallbackType, EmbeddingOperation } from '../pause/index.js'; import { RuntimeAPI, RuntimeMethod } from '../metadata/decorators.js'; import { getVectorStore } from './vector-store.js'; import { cosineSimilarity, generateEmbeddingId } from './utils.js'; import { nextSequenceNumber, getCachedResult, shouldPauseForClient } from '../llm/replay.js'; export { VectorStore, initializeVectorStore, clearVectorStore, getVectorStore, setVectorStoreExecutionId, clearVectorStoreExecutionId, } from './vector-store.js'; export { cosineSimilarity, generateEmbeddingId } from './utils.js'; /** * Embedding Runtime API * * Decorators automatically: * - Extract parameter names and types * - Generate metadata for type definitions * - Maintain single source of truth */ let EmbeddingAPI = class 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 !== undefined && cachedResult !== null) { const vectorStore = getVectorStore(); const embedding = cachedResult; for (let i = 0; i < texts.length; i++) { vectorStore.store(ids[i], texts[i], embedding, metadata); } return isBatch ? ids : ids[0]; } if (shouldPauseForClient()) { pauseForCallback(CallbackType.EMBEDDING, 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 !== undefined && 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(CallbackType.EMBEDDING, 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(); } }; __decorate([ 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>', }, }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], EmbeddingAPI.prototype, "embed", null); __decorate([ 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', }, }), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Object]), __metadata("design:returntype", Promise) ], EmbeddingAPI.prototype, "search", null); __decorate([ RuntimeMethod('Calculate cosine similarity between two embedding vectors', { embedding1: { description: 'First embedding vector', type: 'number[]' }, embedding2: { description: 'Second embedding vector', type: 'number[]' }, }), __metadata("design:type", Function), __metadata("design:paramtypes", [Array, Array]), __metadata("design:returntype", Number) ], EmbeddingAPI.prototype, "similarity", null); __decorate([ RuntimeMethod('Get all stored embeddings'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Array) ], EmbeddingAPI.prototype, "getAll", null); __decorate([ RuntimeMethod('Get count of stored embeddings'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Number) ], EmbeddingAPI.prototype, "count", null); EmbeddingAPI = __decorate([ RuntimeAPI('embedding', 'Embedding API - Client-side embedding with server-side vector storage') ], EmbeddingAPI); export const embedding = new EmbeddingAPI(); //# sourceMappingURL=index.js.map