UNPKG

magnitude-core

Version:
153 lines (149 loc) 8.29 kB
/************************************************************************************************* Welcome to Baml! To use this generated code, please run one of the following: $ npm install @boundaryml/baml $ yarn add @boundaryml/baml $ pnpm add @boundaryml/baml *************************************************************************************************/ import { toBamlError, BamlStream } from "@boundaryml/baml"; import { AsyncHttpRequest, AsyncHttpStreamRequest } from "./async_request"; import { LlmResponseParser, LlmStreamParser } from "./parser"; import { DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME } from "./globals"; export class BamlAsyncClient { runtime; ctxManager; streamClient; httpRequest; httpStreamRequest; llmResponseParser; llmStreamParser; bamlOptions; constructor(runtime, ctxManager, bamlOptions) { this.runtime = runtime; this.ctxManager = ctxManager; this.streamClient = new BamlStreamClient(runtime, ctxManager, bamlOptions); this.httpRequest = new AsyncHttpRequest(runtime, ctxManager); this.httpStreamRequest = new AsyncHttpStreamRequest(runtime, ctxManager); this.llmResponseParser = new LlmResponseParser(runtime, ctxManager); this.llmStreamParser = new LlmStreamParser(runtime, ctxManager); this.bamlOptions = bamlOptions || {}; } withOptions(bamlOptions) { return new BamlAsyncClient(this.runtime, this.ctxManager, bamlOptions); } get stream() { return this.streamClient; } get request() { return this.httpRequest; } get streamRequest() { return this.httpStreamRequest; } get parse() { return this.llmResponseParser; } get parseStream() { return this.llmStreamParser; } async CreatePartialRecipe(memory, task, taskData, includeClaudeSpoof, __baml_options__) { try { const options = { ...this.bamlOptions, ...(__baml_options__ || {}) }; const collector = options.collector ? (Array.isArray(options.collector) ? options.collector : [options.collector]) : []; const rawEnv = __baml_options__?.env ? { ...process.env, ...__baml_options__.env } : { ...process.env }; const env = Object.fromEntries(Object.entries(rawEnv).filter(([_, value]) => value !== undefined)); const raw = await this.runtime.callFunction("CreatePartialRecipe", { "memory": memory, "task": task, "taskData": taskData, "includeClaudeSpoof": includeClaudeSpoof }, this.ctxManager.cloneContext(), options.tb?.__tb(), options.clientRegistry, collector, env); return raw.parsed(false); } catch (error) { throw toBamlError(error); } } async ExtractData(instructions, screenshot, domContent, includeClaudeSpoof, __baml_options__) { try { const options = { ...this.bamlOptions, ...(__baml_options__ || {}) }; const collector = options.collector ? (Array.isArray(options.collector) ? options.collector : [options.collector]) : []; const rawEnv = __baml_options__?.env ? { ...process.env, ...__baml_options__.env } : { ...process.env }; const env = Object.fromEntries(Object.entries(rawEnv).filter(([_, value]) => value !== undefined)); const raw = await this.runtime.callFunction("ExtractData", { "instructions": instructions, "screenshot": screenshot, "domContent": domContent, "includeClaudeSpoof": includeClaudeSpoof }, this.ctxManager.cloneContext(), options.tb?.__tb(), options.clientRegistry, collector, env); return raw.parsed(false); } catch (error) { throw toBamlError(error); } } async QueryMemory(memory, query, includeClaudeSpoof, __baml_options__) { try { const options = { ...this.bamlOptions, ...(__baml_options__ || {}) }; const collector = options.collector ? (Array.isArray(options.collector) ? options.collector : [options.collector]) : []; const rawEnv = __baml_options__?.env ? { ...process.env, ...__baml_options__.env } : { ...process.env }; const env = Object.fromEntries(Object.entries(rawEnv).filter(([_, value]) => value !== undefined)); const raw = await this.runtime.callFunction("QueryMemory", { "memory": memory, "query": query, "includeClaudeSpoof": includeClaudeSpoof }, this.ctxManager.cloneContext(), options.tb?.__tb(), options.clientRegistry, collector, env); return raw.parsed(false); } catch (error) { throw toBamlError(error); } } } class BamlStreamClient { runtime; ctxManager; bamlOptions; constructor(runtime, ctxManager, bamlOptions) { this.runtime = runtime; this.ctxManager = ctxManager; this.bamlOptions = bamlOptions || {}; } CreatePartialRecipe(memory, task, taskData, includeClaudeSpoof, __baml_options__) { try { const options = { ...this.bamlOptions, ...(__baml_options__ || {}) }; const collector = options.collector ? (Array.isArray(options.collector) ? options.collector : [options.collector]) : []; const rawEnv = __baml_options__?.env ? { ...process.env, ...__baml_options__.env } : { ...process.env }; const env = Object.fromEntries(Object.entries(rawEnv).filter(([_, value]) => value !== undefined)); const raw = this.runtime.streamFunction("CreatePartialRecipe", { "memory": memory, "task": task, "taskData": taskData, "includeClaudeSpoof": includeClaudeSpoof }, undefined, this.ctxManager.cloneContext(), options.tb?.__tb(), options.clientRegistry, collector, env); return new BamlStream(raw, (a) => a, (a) => a, this.ctxManager.cloneContext()); } catch (error) { throw toBamlError(error); } } ExtractData(instructions, screenshot, domContent, includeClaudeSpoof, __baml_options__) { try { const options = { ...this.bamlOptions, ...(__baml_options__ || {}) }; const collector = options.collector ? (Array.isArray(options.collector) ? options.collector : [options.collector]) : []; const rawEnv = __baml_options__?.env ? { ...process.env, ...__baml_options__.env } : { ...process.env }; const env = Object.fromEntries(Object.entries(rawEnv).filter(([_, value]) => value !== undefined)); const raw = this.runtime.streamFunction("ExtractData", { "instructions": instructions, "screenshot": screenshot, "domContent": domContent, "includeClaudeSpoof": includeClaudeSpoof }, undefined, this.ctxManager.cloneContext(), options.tb?.__tb(), options.clientRegistry, collector, env); return new BamlStream(raw, (a) => a, (a) => a, this.ctxManager.cloneContext()); } catch (error) { throw toBamlError(error); } } QueryMemory(memory, query, includeClaudeSpoof, __baml_options__) { try { const options = { ...this.bamlOptions, ...(__baml_options__ || {}) }; const collector = options.collector ? (Array.isArray(options.collector) ? options.collector : [options.collector]) : []; const rawEnv = __baml_options__?.env ? { ...process.env, ...__baml_options__.env } : { ...process.env }; const env = Object.fromEntries(Object.entries(rawEnv).filter(([_, value]) => value !== undefined)); const raw = this.runtime.streamFunction("QueryMemory", { "memory": memory, "query": query, "includeClaudeSpoof": includeClaudeSpoof }, undefined, this.ctxManager.cloneContext(), options.tb?.__tb(), options.clientRegistry, collector, env); return new BamlStream(raw, (a) => a, (a) => a, this.ctxManager.cloneContext()); } catch (error) { throw toBamlError(error); } } } export const b = new BamlAsyncClient(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX);