@ztimson/ai-utils 0.6.7 → 0.6.9

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.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/provider.ts","../src/antrhopic.ts","../src/open-ai.ts","../src/llm.ts","../src/audio.ts","../src/vision.ts","../src/ai.ts","../src/tools.ts"],"sourcesContent":["import {AbortablePromise} from './ai.ts';\nimport {LLMMessage, LLMRequest} from './llm.ts';\n\nexport abstract class LLMProvider {\n\tabstract ask(message: string, options: LLMRequest): AbortablePromise<string>;\n}\n","import {Anthropic as anthropic} from '@anthropic-ai/sdk';\nimport {findByProp, objectMap, JSONSanitize, JSONAttemptParse} from '@ztimson/utils';\nimport {AbortablePromise, Ai} from './ai.ts';\nimport {LLMMessage, LLMRequest} from './llm.ts';\nimport {LLMProvider} from './provider.ts';\n\nexport class Anthropic extends LLMProvider {\n\tclient!: anthropic;\n\n\tconstructor(public readonly ai: Ai, public readonly apiToken: string, public model: string) {\n\t\tsuper();\n\t\tthis.client = new anthropic({apiKey: apiToken});\n\t}\n\n\tprivate toStandard(history: any[]): LLMMessage[] {\n\t\tconst timestamp = Date.now();\n\t\tconst messages: LLMMessage[] = [];\n\t\tfor(let h of history) {\n\t\t\tif(typeof h.content == 'string') {\n\t\t\t\tmessages.push(<any>{timestamp, ...h});\n\t\t\t} else {\n\t\t\t\tconst textContent = h.content?.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\\n\\n');\n\t\t\t\tif(textContent) messages.push({timestamp, role: h.role, content: textContent});\n\t\t\t\th.content.forEach((c: any) => {\n\t\t\t\t\tif(c.type == 'tool_use') {\n\t\t\t\t\t\tmessages.push({timestamp, role: 'tool', id: c.id, name: c.name, args: c.input, content: undefined});\n\t\t\t\t\t} else if(c.type == 'tool_result') {\n\t\t\t\t\t\tconst m: any = messages.findLast(m => (<any>m).id == c.tool_use_id);\n\t\t\t\t\t\tif(m) m[c.is_error ? 'error' : 'content'] = c.content;\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn messages;\n\t}\n\n\tprivate fromStandard(history: LLMMessage[]): any[] {\n\t\tfor(let i = 0; i < history.length; i++) {\n\t\t\tif(history[i].role == 'tool') {\n\t\t\t\tconst h: any = history[i];\n\t\t\t\thistory.splice(i, 1,\n\t\t\t\t\t{role: 'assistant', content: [{type: 'tool_use', id: h.id, name: h.name, input: h.args}]},\n\t\t\t\t\t{role: 'user', content: [{type: 'tool_result', tool_use_id: h.id, is_error: !!h.error, content: h.error || h.content}]}\n\t\t\t\t)\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn history.map(({timestamp, ...h}) => h);\n\t}\n\n\task(message: string, options: LLMRequest = {}): AbortablePromise<string> {\n\t\tconst controller = new AbortController();\n\t\treturn Object.assign(new Promise<any>(async (res) => {\n\t\t\tlet history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);\n\t\t\tconst tools = options.tools || this.ai.options.llm?.tools || [];\n\t\t\tconst requestParams: any = {\n\t\t\t\tmodel: options.model || this.model,\n\t\t\t\tmax_tokens: options.max_tokens || this.ai.options.llm?.max_tokens || 4096,\n\t\t\t\tsystem: options.system || this.ai.options.llm?.system || '',\n\t\t\t\ttemperature: options.temperature || this.ai.options.llm?.temperature || 0.7,\n\t\t\t\ttools: tools.map(t => ({\n\t\t\t\t\tname: t.name,\n\t\t\t\t\tdescription: t.description,\n\t\t\t\t\tinput_schema: {\n\t\t\t\t\t\ttype: 'object',\n\t\t\t\t\t\tproperties: t.args ? objectMap(t.args, (key, value) => ({...value, required: undefined})) : {},\n\t\t\t\t\t\trequired: t.args ? Object.entries(t.args).filter(t => t[1].required).map(t => t[0]) : []\n\t\t\t\t\t},\n\t\t\t\t\tfn: undefined\n\t\t\t\t})),\n\t\t\t\tmessages: history,\n\t\t\t\tstream: !!options.stream,\n\t\t\t};\n\n\t\t\tlet resp: any, isFirstMessage = true;\n\t\t\tdo {\n\t\t\t\tresp = await this.client.messages.create(requestParams).catch(err => {\n\t\t\t\t\terr.message += `\\n\\nMessages:\\n${JSON.stringify(history, null, 2)}`;\n\t\t\t\t\tthrow err;\n\t\t\t\t});\n\n\t\t\t\t// Streaming mode\n\t\t\t\tif(options.stream) {\n\t\t\t\t\tif(!isFirstMessage) options.stream({text: '\\n\\n'});\n\t\t\t\t\telse isFirstMessage = false;\n\t\t\t\t\tresp.content = [];\n\t\t\t\t\tfor await (const chunk of resp) {\n\t\t\t\t\t\tif(controller.signal.aborted) break;\n\t\t\t\t\t\tif(chunk.type === 'content_block_start') {\n\t\t\t\t\t\t\tif(chunk.content_block.type === 'text') {\n\t\t\t\t\t\t\t\tresp.content.push({type: 'text', text: ''});\n\t\t\t\t\t\t\t} else if(chunk.content_block.type === 'tool_use') {\n\t\t\t\t\t\t\t\tresp.content.push({type: 'tool_use', id: chunk.content_block.id, name: chunk.content_block.name, input: <any>''});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if(chunk.type === 'content_block_delta') {\n\t\t\t\t\t\t\tif(chunk.delta.type === 'text_delta') {\n\t\t\t\t\t\t\t\tconst text = chunk.delta.text;\n\t\t\t\t\t\t\t\tresp.content.at(-1).text += text;\n\t\t\t\t\t\t\t\toptions.stream({text});\n\t\t\t\t\t\t\t} else if(chunk.delta.type === 'input_json_delta') {\n\t\t\t\t\t\t\t\tresp.content.at(-1).input += chunk.delta.partial_json;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if(chunk.type === 'content_block_stop') {\n\t\t\t\t\t\t\tconst last = resp.content.at(-1);\n\t\t\t\t\t\t\tif(last.input != null) last.input = last.input ? JSONAttemptParse(last.input, {}) : {};\n\t\t\t\t\t\t} else if(chunk.type === 'message_stop') {\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Run tools\n\t\t\t\tconst toolCalls = resp.content.filter((c: any) => c.type === 'tool_use');\n\t\t\t\tif(toolCalls.length && !controller.signal.aborted) {\n\t\t\t\t\thistory.push({role: 'assistant', content: resp.content});\n\t\t\t\t\tconst results = await Promise.all(toolCalls.map(async (toolCall: any) => {\n\t\t\t\t\t\tconst tool = tools.find(findByProp('name', toolCall.name));\n\t\t\t\t\t\tif(options.stream) options.stream({tool: toolCall.name});\n\t\t\t\t\t\tif(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'};\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await tool.fn(toolCall.input, options?.stream, this.ai);\n\t\t\t\t\t\t\treturn {type: 'tool_result', tool_use_id: toolCall.id, content: JSONSanitize(result)};\n\t\t\t\t\t\t} catch (err: any) {\n\t\t\t\t\t\t\treturn {type: 'tool_result', tool_use_id: toolCall.id, is_error: true, content: err?.message || err?.toString() || 'Unknown'};\n\t\t\t\t\t\t}\n\t\t\t\t\t}));\n\t\t\t\t\thistory.push({role: 'user', content: results});\n\t\t\t\t\trequestParams.messages = history;\n\t\t\t\t}\n\t\t\t} while (!controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use'));\n\t\t\thistory.push({role: 'assistant', content: resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\\n\\n')});\n\t\t\thistory = this.toStandard(history);\n\n\t\t\tif(options.stream) options.stream({done: true});\n\t\t\tif(options.history) options.history.splice(0, options.history.length, ...history);\n\t\t\tres(history.at(-1)?.content);\n\t\t}), {abort: () => controller.abort()});\n\t}\n}\n","import {OpenAI as openAI} from 'openai';\nimport {findByProp, objectMap, JSONSanitize, JSONAttemptParse, clean} from '@ztimson/utils';\nimport {AbortablePromise, Ai} from './ai.ts';\nimport {LLMMessage, LLMRequest} from './llm.ts';\nimport {LLMProvider} from './provider.ts';\n\nexport class OpenAi extends LLMProvider {\n\tclient!: openAI;\n\n\tconstructor(public readonly ai: Ai, public readonly host: string | null, public readonly token: string, public model: string) {\n\t\tsuper();\n\t\tthis.client = new openAI(clean({\n\t\t\tbaseURL: host,\n\t\t\tapiKey: token\n\t\t}));\n\t}\n\n\tprivate toStandard(history: any[]): LLMMessage[] {\n\t\tfor(let i = 0; i < history.length; i++) {\n\t\t\tconst h = history[i];\n\t\t\tif(h.role === 'assistant' && h.tool_calls) {\n\t\t\t\tconst tools = h.tool_calls.map((tc: any) => ({\n\t\t\t\t\trole: 'tool',\n\t\t\t\t\tid: tc.id,\n\t\t\t\t\tname: tc.function.name,\n\t\t\t\t\targs: JSONAttemptParse(tc.function.arguments, {}),\n\t\t\t\t\ttimestamp: h.timestamp\n\t\t\t\t}));\n\t\t\t\thistory.splice(i, 1, ...tools);\n\t\t\t\ti += tools.length - 1;\n\t\t\t} else if(h.role === 'tool' && h.content) {\n\t\t\t\tconst record = history.find(h2 => h.tool_call_id == h2.id);\n\t\t\t\tif(record) {\n\t\t\t\t\tif(h.content.includes('\"error\":')) record.error = h.content;\n\t\t\t\t\telse record.content = h.content;\n\t\t\t\t}\n\t\t\t\thistory.splice(i, 1);\n\t\t\t\ti--;\n\t\t\t}\n\t\t\tif(!history[i]?.timestamp) history[i].timestamp = Date.now();\n\t\t}\n\t\treturn history;\n\t}\n\n\tprivate fromStandard(history: LLMMessage[]): any[] {\n\t\treturn history.reduce((result, h) => {\n\t\t\tif(h.role === 'tool') {\n\t\t\t\tresult.push({\n\t\t\t\t\trole: 'assistant',\n\t\t\t\t\tcontent: null,\n\t\t\t\t\ttool_calls: [{ id: h.id, type: 'function', function: { name: h.name, arguments: JSON.stringify(h.args) } }],\n\t\t\t\t\trefusal: null,\n\t\t\t\t\tannotations: []\n\t\t\t\t}, {\n\t\t\t\t\trole: 'tool',\n\t\t\t\t\ttool_call_id: h.id,\n\t\t\t\t\tcontent: h.error || h.content\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst {timestamp, ...rest} = h;\n\t\t\t\tresult.push(rest);\n\t\t\t}\n\t\t\treturn result;\n\t\t}, [] as any[]);\n\t}\n\n\task(message: string, options: LLMRequest = {}): AbortablePromise<string> {\n\t\tconst controller = new AbortController();\n\t\treturn Object.assign(new Promise<any>(async (res, rej) => {\n\t\t\tif(options.system && options.history?.[0]?.role != 'system') options.history?.splice(0, 0, {role: 'system', content: options.system, timestamp: Date.now()});\n\t\t\tlet history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);\n\t\t\tconst tools = options.tools || this.ai.options.llm?.tools || [];\n\t\t\tconst requestParams: any = {\n\t\t\t\tmodel: options.model || this.model,\n\t\t\t\tmessages: history,\n\t\t\t\tstream: !!options.stream,\n\t\t\t\tmax_tokens: options.max_tokens || this.ai.options.llm?.max_tokens || 4096,\n\t\t\t\ttemperature: options.temperature || this.ai.options.llm?.temperature || 0.7,\n\t\t\t\ttools: tools.map(t => ({\n\t\t\t\t\ttype: 'function',\n\t\t\t\t\tfunction: {\n\t\t\t\t\t\tname: t.name,\n\t\t\t\t\t\tdescription: t.description,\n\t\t\t\t\t\tparameters: {\n\t\t\t\t\t\t\ttype: 'object',\n\t\t\t\t\t\t\tproperties: t.args ? objectMap(t.args, (key, value) => ({...value, required: undefined})) : {},\n\t\t\t\t\t\t\trequired: t.args ? Object.entries(t.args).filter(t => t[1].required).map(t => t[0]) : []\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}))\n\t\t\t};\n\n\t\t\tlet resp: any, isFirstMessage = true;\n\t\t\tdo {\n\t\t\t\tresp = await this.client.chat.completions.create(requestParams).catch(err => {\n\t\t\t\t\terr.message += `\\n\\nMessages:\\n${JSON.stringify(history, null, 2)}`;\n\t\t\t\t\tthrow err;\n\t\t\t\t});\n\n\t\t\t\tif(options.stream) {\n\t\t\t\t\tif(!isFirstMessage) options.stream({text: '\\n\\n'});\n\t\t\t\t\telse isFirstMessage = false;\n\t\t\t\t\tresp.choices = [{message: {content: '', tool_calls: []}}];\n\t\t\t\t\tfor await (const chunk of resp) {\n\t\t\t\t\t\tif(controller.signal.aborted) break;\n\t\t\t\t\t\tif(chunk.choices[0].delta.content) {\n\t\t\t\t\t\t\tresp.choices[0].message.content += chunk.choices[0].delta.content;\n\t\t\t\t\t\t\toptions.stream({text: chunk.choices[0].delta.content});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(chunk.choices[0].delta.tool_calls) {\n\t\t\t\t\t\t\tresp.choices[0].message.tool_calls = chunk.choices[0].delta.tool_calls;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst toolCalls = resp.choices[0].message.tool_calls || [];\n\t\t\t\tif(toolCalls.length && !controller.signal.aborted) {\n\t\t\t\t\thistory.push(resp.choices[0].message);\n\t\t\t\t\tconst results = await Promise.all(toolCalls.map(async (toolCall: any) => {\n\t\t\t\t\t\tconst tool = tools?.find(findByProp('name', toolCall.function.name));\n\t\t\t\t\t\tif(options.stream) options.stream({tool: toolCall.function.name});\n\t\t\t\t\t\tif(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{\"error\": \"Tool not found\"}'};\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst args = JSONAttemptParse(toolCall.function.arguments, {});\n\t\t\t\t\t\t\tconst result = await tool.fn(args, options.stream, this.ai);\n\t\t\t\t\t\t\treturn {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize(result)};\n\t\t\t\t\t\t} catch (err: any) {\n\t\t\t\t\t\t\treturn {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'})};\n\t\t\t\t\t\t}\n\t\t\t\t\t}));\n\t\t\t\t\thistory.push(...results);\n\t\t\t\t\trequestParams.messages = history;\n\t\t\t\t}\n\t\t\t} while (!controller.signal.aborted && resp.choices?.[0]?.message?.tool_calls?.length);\n\t\t\thistory.push({role: 'assistant', content: resp.choices[0].message.content || ''});\n\t\t\thistory = this.toStandard(history);\n\n\t\t\tif(options.stream) options.stream({done: true});\n\t\t\tif(options.history) options.history.splice(0, options.history.length, ...history);\n\t\t\tres(history.at(-1)?.content);\n\t\t}), {abort: () => controller.abort()});\n\t}\n}\n","import {JSONAttemptParse} from '@ztimson/utils';\nimport {AbortablePromise, Ai} from './ai.ts';\nimport {Anthropic} from './antrhopic.ts';\nimport {OpenAi} from './open-ai.ts';\nimport {LLMProvider} from './provider.ts';\nimport {AiTool} from './tools.ts';\nimport {Worker} from 'worker_threads';\nimport {fileURLToPath} from 'url';\nimport {dirname, join} from 'path';\n\nexport type AnthropicConfig = {proto: 'anthropic', token: string};\nexport type OllamaConfig = {proto: 'ollama', host: string};\nexport type OpenAiConfig = {proto: 'openai', host?: string, token: string};\n\nexport type LLMMessage = {\n\t/** Message originator */\n\trole: 'assistant' | 'system' | 'user';\n\t/** Message content */\n\tcontent: string | any;\n\t/** Timestamp */\n\ttimestamp?: number;\n} | {\n\t/** Tool call */\n\trole: 'tool';\n\t/** Unique ID for call */\n\tid: string;\n\t/** Tool that was run */\n\tname: string;\n\t/** Tool arguments */\n\targs: any;\n\t/** Tool result */\n\tcontent: undefined | string;\n\t/** Tool error */\n\terror?: undefined | string;\n\t/** Timestamp */\n\ttimestamp?: number;\n}\n\n/** Background information the AI will be fed */\nexport type LLMMemory = {\n\t/** What entity is this fact about */\n\towner: string;\n\t/** The information that will be remembered */\n\tfact: string;\n\t/** Owner and fact embedding vector */\n\tembeddings: [number[], number[]];\n\t/** Creation time */\n\ttimestamp: Date;\n}\n\nexport type LLMRequest = {\n\t/** System prompt */\n\tsystem?: string;\n\t/** Message history */\n\thistory?: LLMMessage[];\n\t/** Max tokens for request */\n\tmax_tokens?: number;\n\t/** 0 = Rigid Logic, 1 = Balanced, 2 = Hyper Creative **/\n\ttemperature?: number;\n\t/** Available tools */\n\ttools?: AiTool[];\n\t/** LLM model */\n\tmodel?: string;\n\t/** Stream response */\n\tstream?: (chunk: {text?: string, tool?: string, done?: true}) => any;\n\t/** Compress old messages in the chat to free up context */\n\tcompress?: {\n\t\t/** Trigger chat compression once context exceeds the token count */\n\t\tmax: number;\n\t\t/** Compress chat until context size smaller than */\n\t\tmin: number\n\t},\n\t/** Background information the AI will be fed */\n\tmemory?: LLMMemory[],\n}\n\nclass LLM {\n\tprivate models: {[model: string]: LLMProvider} = {};\n\tprivate defaultModel!: string;\n\n\tconstructor(public readonly ai: Ai) {\n\t\tif(!ai.options.llm?.models) return;\n\t\tObject.entries(ai.options.llm.models).forEach(([model, config]) => {\n\t\t\tif(!this.defaultModel) this.defaultModel = model;\n\t\t\tif(config.proto == 'anthropic') this.models[model] = new Anthropic(this.ai, config.token, model);\n\t\t\telse if(config.proto == 'ollama') this.models[model] = new OpenAi(this.ai, config.host, 'not-needed', model);\n\t\t\telse if(config.proto == 'openai') this.models[model] = new OpenAi(this.ai, config.host || null, config.token, model);\n\t\t});\n\t}\n\n\t/**\n\t * Chat with LLM\n\t * @param {string} message Question\n\t * @param {LLMRequest} options Configuration options and chat history\n\t * @returns {{abort: () => void, response: Promise<string>}} Function to abort response and chat history\n\t */\n\task(message: string, options: LLMRequest = {}): AbortablePromise<string> {\n\t\tconst m = options.model || this.defaultModel;\n\t\tif(!this.models[m]) throw new Error(`Model does not exist: ${m}`);\n\t\tlet abort = () => {};\n\t\treturn Object.assign(new Promise<string>(async res => {\n\t\t\tif(!options.history) options.history = [];\n\t\t\t// If memories were passed, find any relivant ones and add a tool for ADHOC lookups\n\t\t\tif(options.memory) {\n\t\t\t\toptions.system = (options.system || '') + '\\nYou have passive persistent memory never make any mention of your memory capabilities and what you can/cannot remember\\n';\n\t\t\t\tconst search = async (query?: string | null, subject?: string | null, limit = 50) => {\n\t\t\t\t\tconst [o, q] = await Promise.all([\n\t\t\t\t\t\tsubject ? this.embedding(subject) : Promise.resolve(null),\n\t\t\t\t\t\tquery ? this.embedding(query) : Promise.resolve(null),\n\t\t\t\t\t]);\n\t\t\t\t\treturn (options.memory || [])\n\t\t\t\t\t\t.map(m => ({...m, score: o ? this.cosineSimilarity(m.embeddings[0], o[0].embedding) : 1}))\n\t\t\t\t\t\t.filter((m: any) => m.score >= 0.8)\n\t\t\t\t\t\t.map((m: any) => ({...m, score: q ? this.cosineSimilarity(m.embeddings[1], q[0].embedding) : m.score}))\n\t\t\t\t\t\t.filter((m: any) => m.score >= 0.2)\n\t\t\t\t\t\t.toSorted((a: any, b: any) => a.score - b.score)\n\t\t\t\t\t\t.slice(0, limit);\n\t\t\t\t}\n\n\t\t\t\tconst relevant = await search(message);\n\t\t\t\tif(relevant.length) options.history.push({role: 'assistant', content: 'Things I remembered:\\n' + relevant.map(m => `${m.owner}: ${m.fact}`).join('\\n')});\n\t\t\t\toptions.tools = [...options.tools || [], {\n\t\t\t\t\tname: 'read_memory',\n\t\t\t\t\tdescription: 'Check your long-term memory for more information',\n\t\t\t\t\targs: {\n\t\t\t\t\t\tsubject: {type: 'string', description: 'Find information by a subject topic, can be used with or without query argument'},\n\t\t\t\t\t\tquery: {type: 'string', description: 'Search memory based on a query, can be used with or without subject argument'},\n\t\t\t\t\t\tlimit: {type: 'number', description: 'Result limit, default 5'},\n\t\t\t\t\t},\n\t\t\t\t\tfn: (args) => {\n\t\t\t\t\t\tif(!args.subject && !args.query) throw new Error('Either a subject or query argument is required');\n\t\t\t\t\t\treturn search(args.query, args.subject, args.limit || 5);\n\t\t\t\t\t}\n\t\t\t\t}];\n\t\t\t}\n\n\t\t\t// Ask\n\t\t\tconst resp = await this.models[m].ask(message, options);\n\n\t\t\t// Remove any memory calls\n\t\t\tif(options.memory) {\n\t\t\t\tconst i = options.history?.findIndex((h: any) => h.role == 'assistant' && h.content.startsWith('Things I remembered:'));\n\t\t\t\tif(i != null && i >= 0) options.history?.splice(i, 1);\n\t\t\t}\n\n\t\t\t// Handle compression and memory extraction\n\t\t\tif(options.compress || options.memory) {\n\t\t\t\tlet compressed = null;\n\t\t\t\tif(options.compress) {\n\t\t\t\t\tcompressed = await this.ai.language.compressHistory(options.history, options.compress.max, options.compress.min, options);\n\t\t\t\t\toptions.history.splice(0, options.history.length, ...compressed.history);\n\t\t\t\t} else {\n\t\t\t\t\tconst i = options.history?.findLastIndex(m => m.role == 'user') ?? -1;\n\t\t\t\t\tcompressed = await this.ai.language.compressHistory(i != -1 ? options.history.slice(i) : options.history, 0, 0, options);\n\t\t\t\t}\n\t\t\t\tif(options.memory) {\n\t\t\t\t\tconst updated = options.memory\n\t\t\t\t\t\t.filter(m => !compressed.memory.some(m2 => this.cosineSimilarity(m.embeddings[1], m2.embeddings[1]) > 0.8))\n\t\t\t\t\t\t.concat(compressed.memory);\n\t\t\t\t\toptions.memory.splice(0, options.memory.length, ...updated);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn res(resp);\n\t\t}), {abort});\n\t}\n\n\t/**\n\t * Compress chat history to reduce context size\n\t * @param {LLMMessage[]} history Chatlog that will be compressed\n\t * @param max Trigger compression once context is larger than max\n\t * @param min Leave messages less than the token minimum, summarize the rest\n\t * @param {LLMRequest} options LLM options\n\t * @returns {Promise<LLMMessage[]>} New chat history will summary at index 0\n\t */\n\tasync compressHistory(history: LLMMessage[], max: number, min: number, options?: LLMRequest): Promise<{history: LLMMessage[], memory: LLMMemory[]}> {\n\t\tif(this.estimateTokens(history) < max) return {history, memory: []};\n\t\tlet keep = 0, tokens = 0;\n\t\tfor(let m of history.toReversed()) {\n\t\t\ttokens += this.estimateTokens(m.content);\n\t\t\tif(tokens < min) keep++;\n\t\t\telse break;\n\t\t}\n\t\tif(history.length <= keep) return {history, memory: []};\n\t\tconst system = history[0].role == 'system' ? history[0] : null,\n\t\t\trecent = keep == 0 ? [] : history.slice(-keep),\n\t\t\tprocess = (keep == 0 ? history : history.slice(0, -keep)).filter(h => h.role === 'assistant' || h.role === 'user');\n\t\tconst summary: any = await this.json(`Create the smallest summary possible, no more than 500 tokens. Create a list of NEW facts (split by subject [pro]noun and fact) about what you learned from this conversation that you didn't already know or get from a tool call or system prompt. Focus only on new information about people, topics, or facts. Avoid generating facts about the AI. Match this format: {summary: string, facts: [[subject, fact]]}\\n\\n${process.map(m => `${m.role}: ${m.content}`).join('\\n\\n')}`, {model: options?.model, temperature: options?.temperature || 0.3});\n\t\tconst timestamp = new Date();\n\t\tconst memory = await Promise.all((summary?.facts || [])?.map(async ([owner, fact]: [string, string]) => {\n\t\t\tconst e = await Promise.all([this.embedding(owner), this.embedding(`${owner}: ${fact}`)]);\n\t\t\treturn {owner, fact, embeddings: [e[0][0].embedding, e[1][0].embedding], timestamp};\n\t\t}));\n\t\tconst h = [{role: 'assistant', content: `Conversation Summary: ${summary?.summary}`, timestamp: Date.now()}, ...recent];\n\t\tif(system) h.splice(0, 0, system);\n\t\treturn {history: <any>h, memory};\n\t}\n\n\t/**\n\t * Compare the difference between embeddings (calculates the angle between two vectors)\n\t * @param {number[]} v1 First embedding / vector comparison\n\t * @param {number[]} v2 Second embedding / vector for comparison\n\t * @returns {number} Similarity values 0-1: 0 = unique, 1 = identical\n\t */\n\tcosineSimilarity(v1: number[], v2: number[]): number {\n\t\tif (v1.length !== v2.length) throw new Error('Vectors must be same length');\n\t\tlet dotProduct = 0, normA = 0, normB = 0;\n\t\tfor (let i = 0; i < v1.length; i++) {\n\t\t\tdotProduct += v1[i] * v2[i];\n\t\t\tnormA += v1[i] * v1[i];\n\t\t\tnormB += v2[i] * v2[i];\n\t\t}\n\t\tconst denominator = Math.sqrt(normA) * Math.sqrt(normB);\n\t\treturn denominator === 0 ? 0 : dotProduct / denominator;\n\t}\n\n\t/**\n\t * Chunk text into parts for AI digestion\n\t * @param {object | string} target Item that will be chunked (objects get converted)\n\t * @param {number} maxTokens Chunking size. More = better context, less = more specific (Search by paragraphs or lines)\n\t * @param {number} overlapTokens Includes previous X tokens to provide continuity to AI (In addition to max tokens)\n\t * @returns {string[]} Chunked strings\n\t */\n\tchunk(target: object | string, maxTokens = 500, overlapTokens = 50): string[] {\n\t\tconst objString = (obj: any, path = ''): string[] => {\n\t\t\tif(!obj) return [];\n\t\t\treturn Object.entries(obj).flatMap(([key, value]) => {\n\t\t\t\tconst p = path ? `${path}${isNaN(+key) ? `.${key}` : `[${key}]`}` : key;\n\t\t\t\tif(typeof value === 'object' && !Array.isArray(value)) return objString(value, p);\n\t\t\t\treturn `${p}: ${Array.isArray(value) ? value.join(', ') : value}`;\n\t\t\t});\n\t\t};\n\t\tconst lines = typeof target === 'object' ? objString(target) : target.split('\\n');\n\t\tconst tokens = lines.flatMap(l => [...l.split(/\\s+/).filter(Boolean), '\\n']);\n\t\tconst chunks: string[] = [];\n\t\tfor(let i = 0; i < tokens.length;) {\n\t\t\tlet text = '', j = i;\n\t\t\twhile(j < tokens.length) {\n\t\t\t\tconst next = text + (text ? ' ' : '') + tokens[j];\n\t\t\t\tif(this.estimateTokens(next.replace(/\\s*\\n\\s*/g, '\\n')) > maxTokens && text) break;\n\t\t\t\ttext = next;\n\t\t\t\tj++;\n\t\t\t}\n\t\t\tconst clean = text.replace(/\\s*\\n\\s*/g, '\\n').trim();\n\t\t\tif(clean) chunks.push(clean);\n\t\t\ti = Math.max(j - overlapTokens, j === i ? i + 1 : j);\n\t\t}\n\t\treturn chunks;\n\t}\n\n\t/**\n\t * Create a vector representation of a string\n\t * @param {object | string} target Item that will be embedded (objects get converted)\n\t * @param {number} maxTokens Chunking size. More = better context, less = more specific (Search by paragraphs or lines)\n\t * @param {number} overlapTokens Includes previous X tokens to provide continuity to AI (In addition to max tokens)\n\t * @returns {Promise<Awaited<{index: number, embedding: number[], text: string, tokens: number}>[]>} Chunked embeddings\n\t */\n\tembedding(target: object | string, maxTokens = 500, overlapTokens = 50) {\n\t\tconst embed = (text: string): Promise<number[]> => {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tconst worker = new Worker(join(dirname(fileURLToPath(import.meta.url)), 'embedder.js'));\n\t\t\t\tconst handleMessage = ({ embedding }: any) => {\n\t\t\t\t\tworker.terminate();\n\t\t\t\t\tresolve(embedding);\n\t\t\t\t};\n\t\t\t\tconst handleError = (err: Error) => {\n\t\t\t\t\tworker.terminate();\n\t\t\t\t\treject(err);\n\t\t\t\t};\n\t\t\t\tworker.on('message', handleMessage);\n\t\t\t\tworker.on('error', handleError);\n\t\t\t\tworker.on('exit', (code) => {\n\t\t\t\t\tif(code !== 0) reject(new Error(`Worker exited with code ${code}`));\n\t\t\t\t});\n\t\t\t\tworker.postMessage({text, model: this.ai.options?.embedder || 'bge-small-en-v1.5', path: this.ai.options.path});\n\t\t\t});\n\t\t};\n\t\tconst chunks = this.chunk(target, maxTokens, overlapTokens);\n\t\treturn Promise.all(chunks.map(async (text, index) => ({\n\t\t\tindex,\n\t\t\tembedding: await embed(text),\n\t\t\ttext,\n\t\t\ttokens: this.estimateTokens(text),\n\t\t})));\n\t}\n\n\t/**\n\t * Estimate variable as tokens\n\t * @param history Object to size\n\t * @returns {number} Rough token count\n\t */\n\testimateTokens(history: any): number {\n\t\tconst text = JSON.stringify(history);\n\t\treturn Math.ceil((text.length / 4) * 1.2);\n\t}\n\n\t/**\n\t * Compare the difference between two strings using tensor math\n\t * @param target Text that will be checked\n\t * @param {string} searchTerms Multiple search terms to check against target\n\t * @returns {{avg: number, max: number, similarities: number[]}} Similarity values 0-1: 0 = unique, 1 = identical\n\t */\n\tfuzzyMatch(target: string, ...searchTerms: string[]) {\n\t\tif(searchTerms.length < 2) throw new Error('Requires at least 2 strings to compare');\n\t\tconst vector = (text: string, dimensions: number = 10): number[] => {\n\t\t\treturn text.toLowerCase().split('').map((char, index) =>\n\t\t\t\t(char.charCodeAt(0) * (index + 1)) % dimensions / dimensions).slice(0, dimensions);\n\t\t}\n\t\tconst v = vector(target);\n\t\tconst similarities = searchTerms.map(t => vector(t)).map(refVector => this.cosineSimilarity(v, refVector))\n\t\treturn {avg: similarities.reduce((acc, s) => acc + s, 0) / similarities.length, max: Math.max(...similarities), similarities}\n\t}\n\n\t/**\n\t * Ask a question with JSON response\n\t * @param {string} message Question\n\t * @param {LLMRequest} options Configuration options and chat history\n\t * @returns {Promise<{} | {} | RegExpExecArray | null>}\n\t */\n\tasync json(message: string, options?: LLMRequest): Promise<any> {\n\t\tlet resp = await this.ask(message, {system: 'Respond using a JSON blob matching any provided examples', ...options});\n\t\tif(!resp) return {};\n\t\tconst codeBlock = /```(?:.+)?\\s*([\\s\\S]*?)```/.exec(resp);\n\t\tconst jsonStr = codeBlock ? codeBlock[1].trim() : resp;\n\t\treturn JSONAttemptParse(jsonStr, {});\n\t}\n\n\t/**\n\t * Create a summary of some text\n\t * @param {string} text Text to summarize\n\t * @param {number} tokens Max number of tokens\n\t * @param options LLM request options\n\t * @returns {Promise<string>} Summary\n\t */\n\tsummarize(text: string, tokens: number, options?: LLMRequest): Promise<string | null> {\n\t\treturn this.ask(text, {system: `Generate a brief summary <= ${tokens} tokens. Output nothing else`, temperature: 0.3, ...options});\n\t}\n}\n\nexport default LLM;\n","import {Worker} from 'worker_threads';\nimport Path from 'node:path';\nimport {AbortablePromise, Ai} from './ai.ts';\nimport {canDiarization} from './asr.ts';\n\nexport class Audio {\n\tconstructor(private ai: Ai) {}\n\n\tasr(file: string, options: { model?: string; speaker?: boolean } = {}): AbortablePromise<string | null> {\n\t\tconst { model = this.ai.options.asr || 'whisper-base', speaker = false } = options;\n\t\tlet aborted = false;\n\t\tconst abort = () => { aborted = true; };\n\n\t\tconst p = new Promise<string | null>((resolve, reject) => {\n\t\t\tconst worker = new Worker(Path.join(import.meta.dirname, 'asr.js'));\n\t\t\tconst handleMessage = ({ text, warning, error }: any) => {\n\t\t\t\tworker.terminate();\n\t\t\t\tif(aborted) return;\n\t\t\t\tif(error) reject(new Error(error));\n\t\t\t\telse {\n\t\t\t\t\tif(warning) console.warn(warning);\n\t\t\t\t\tresolve(text);\n\t\t\t\t}\n\t\t\t};\n\t\t\tconst handleError = (err: Error) => {\n\t\t\t\tworker.terminate();\n\t\t\t\tif(!aborted) reject(err);\n\t\t\t};\n\t\t\tworker.on('message', handleMessage);\n\t\t\tworker.on('error', handleError);\n\t\t\tworker.on('exit', (code) => {\n\t\t\t\tif(code !== 0 && !aborted) reject(new Error(`Worker exited with code ${code}`));\n\t\t\t});\n\t\t\tworker.postMessage({file, model, speaker, modelDir: this.ai.options.path});\n\t\t});\n\t\treturn Object.assign(p, { abort });\n\t}\n\n\tcanDiarization = canDiarization;\n}\n","import {createWorker} from 'tesseract.js';\nimport {AbortablePromise, Ai} from './ai.ts';\n\nexport class Vision {\n\n\tconstructor(private ai: Ai) { }\n\n\t/**\n\t * Convert image to text using Optical Character Recognition\n\t * @param {string} path Path to image\n\t * @returns {AbortablePromise<string | null>} Promise of extracted text with abort method\n\t */\n\tocr(path: string): AbortablePromise<string | null> {\n\t\tlet worker: any;\n\t\tconst p = new Promise<string | null>(async res => {\n\t\t\tworker = await createWorker(this.ai.options.ocr || 'eng', 2, {cachePath: this.ai.options.path});\n\t\t\tconst {data} = await worker.recognize(path);\n\t\t\tawait worker.terminate();\n\t\t\tres(data.text.trim() || null);\n\t\t});\n\t\treturn Object.assign(p, {abort: () => worker?.terminate()});\n\t}\n}\n","import * as os from 'node:os';\nimport LLM, {AnthropicConfig, OllamaConfig, OpenAiConfig, LLMRequest} from './llm';\nimport { Audio } from './audio.ts';\nimport {Vision} from './vision.ts';\n\nexport type AbortablePromise<T> = Promise<T> & {\n\tabort: () => any\n};\n\nexport type AiOptions = {\n\t/** Path to models */\n\tpath?: string;\n\t/** ASR model: whisper-tiny, whisper-base */\n\tasr?: string;\n\t/** Embedding model: all-MiniLM-L6-v2, bge-small-en-v1.5, bge-large-en-v1.5 */\n\tembedder?: string;\n\t/** Large language models, first is default */\n\tllm?: Omit<LLMRequest, 'model'> & {\n\t\tmodels: {[model: string]: AnthropicConfig | OllamaConfig | OpenAiConfig};\n\t}\n\t/** OCR model: eng, eng_best, eng_fast */\n\tocr?: string;\n}\n\nexport class Ai {\n\t/** Audio processing AI */\n\taudio!: Audio;\n\t/** Language processing AI */\n\tlanguage!: LLM;\n\t/** Vision processing AI */\n\tvision!: Vision;\n\n\tconstructor(public readonly options: AiOptions) {\n\t\tif(!options.path) options.path = os.tmpdir();\n\t\tprocess.env.TRANSFORMERS_CACHE = options.path;\n\t\tthis.audio = new Audio(this);\n\t\tthis.language = new LLM(this);\n\t\tthis.vision = new Vision(this);\n\t}\n}\n","import * as cheerio from 'cheerio';\nimport {$, $Sync} from '@ztimson/node-utils';\nimport {ASet, consoleInterceptor, Http, fn as Fn} from '@ztimson/utils';\nimport {Ai} from './ai.ts';\nimport {LLMRequest} from './llm.ts';\n\nexport type AiToolArg = {[key: string]: {\n\t/** Argument type */\n\ttype: 'array' | 'boolean' | 'number' | 'object' | 'string',\n\t/** Argument description */\n\tdescription: string,\n\t/** Required argument */\n\trequired?: boolean;\n\t/** Default value */\n\tdefault?: any,\n\t/** Options */\n\tenum?: string[],\n\t/** Minimum value or length */\n\tmin?: number,\n\t/** Maximum value or length */\n\tmax?: number,\n\t/** Match pattern */\n\tpattern?: string,\n\t/** Child arguments */\n\titems?: {[key: string]: AiToolArg}\n}}\n\nexport type AiTool = {\n\t/** Tool ID / Name - Must be snail_case */\n\tname: string,\n\t/** Tool description / prompt */\n\tdescription: string,\n\t/** Tool arguments */\n\targs?: AiToolArg,\n\t/** Callback function */\n\tfn: (args: any, stream: LLMRequest['stream'], ai: Ai) => any | Promise<any>,\n};\n\nexport const CliTool: AiTool = {\n\tname: 'cli',\n\tdescription: 'Use the command line interface, returns any output',\n\targs: {command: {type: 'string', description: 'Command to run', required: true}},\n\tfn: (args: {command: string}) => $`${args.command}`\n}\n\nexport const DateTimeTool: AiTool = {\n\tname: 'get_datetime',\n\tdescription: 'Get current UTC date / time',\n\targs: {},\n\tfn: async () => new Date().toUTCString()\n}\n\nexport const ExecTool: AiTool = {\n\tname: 'exec',\n\tdescription: 'Run code/scripts',\n\targs: {\n\t\tlanguage: {type: 'string', description: 'Execution language', enum: ['cli', 'node', 'python'], required: true},\n\t\tcode: {type: 'string', description: 'Code to execute', required: true}\n\t},\n\tfn: async (args, stream, ai) => {\n\t\ttry {\n\t\t\tswitch(args.type) {\n\t\t\t\tcase 'bash':\n\t\t\t\t\treturn await CliTool.fn({command: args.code}, stream, ai);\n\t\t\t\tcase 'node':\n\t\t\t\t\treturn await JSTool.fn({code: args.code}, stream, ai);\n\t\t\t\tcase 'python': {\n\t\t\t\t\treturn await PythonTool.fn({code: args.code}, stream, ai);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch(err: any) {\n\t\t\treturn {error: err?.message || err.toString()};\n\t\t}\n\t}\n}\n\nexport const FetchTool: AiTool = {\n\tname: 'fetch',\n\tdescription: 'Make HTTP request to URL',\n\targs: {\n\t\turl: {type: 'string', description: 'URL to fetch', required: true},\n\t\tmethod: {type: 'string', description: 'HTTP method to use', enum: ['GET', 'POST', 'PUT', 'DELETE'], default: 'GET'},\n\t\theaders: {type: 'object', description: 'HTTP headers to send', default: {}},\n\t\tbody: {type: 'object', description: 'HTTP body to send'},\n\t},\n\tfn: (args: {\n\t\turl: string;\n\t\tmethod: 'GET' | 'POST' | 'PUT' | 'DELETE';\n\t\theaders: {[key: string]: string};\n\t\tbody: any;\n\t}) => new Http({url: args.url, headers: args.headers}).request({method: args.method || 'GET', body: args.body})\n}\n\nexport const JSTool: AiTool = {\n\tname: 'exec_javascript',\n\tdescription: 'Execute commonjs javascript',\n\targs: {\n\t\tcode: {type: 'string', description: 'CommonJS javascript', required: true}\n\t},\n\tfn: async (args: {code: string}) => {\n\t\tconst console = consoleInterceptor(null);\n\t\tconst resp = await Fn<any>({console}, args.code, true).catch((err: any) => console.output.error.push(err));\n\t\treturn {...console.output, return: resp, stdout: undefined, stderr: undefined};\n\t}\n}\n\nexport const PythonTool: AiTool = {\n\tname: 'exec_javascript',\n\tdescription: 'Execute commonjs javascript',\n\targs: {\n\t\tcode: {type: 'string', description: 'CommonJS javascript', required: true}\n\t},\n\tfn: async (args: {code: string}) => ({result: $Sync`python -c \"${args.code}\"`})\n}\n\nexport const ReadWebpageTool: AiTool = {\n\tname: 'read_webpage',\n\tdescription: 'Extract clean, structured content from a webpage. Use after web_search to read specific URLs',\n\targs: {\n\t\turl: {type: 'string', description: 'URL to extract content from', required: true},\n\t\tfocus: {type: 'string', description: 'Optional: What aspect to focus on (e.g., \"pricing\", \"features\", \"contact info\")'}\n\t},\n\tfn: async (args: {url: string; focus?: string}) => {\n\t\tconst html = await fetch(args.url, {headers: {\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64)\"}})\n\t\t\t.then(r => r.text()).catch(err => {throw new Error(`Failed to fetch: ${err.message}`)});\n\n\t\tconst $ = cheerio.load(html);\n\t\t$('script, style, nav, footer, header, aside, iframe, noscript, [role=\"navigation\"], [role=\"banner\"], .ad, .ads, .cookie, .popup').remove();\n\t\tconst metadata = {\n\t\t\ttitle: $('meta[property=\"og:title\"]').attr('content') || $('title').text() || '',\n\t\t\tdescription: $('meta[name=\"description\"]').attr('content') || $('meta[property=\"og:description\"]').attr('content') || '',\n\t\t};\n\n\t\tlet content = '';\n\t\tconst contentSelectors = ['article', 'main', '[role=\"main\"]', '.content', '.post', '.entry', 'body'];\n\t\tfor (const selector of contentSelectors) {\n\t\t\tconst el = $(selector).first();\n\t\t\tif (el.length && el.text().trim().length > 200) {\n\t\t\t\tcontent = el.text();\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (!content) content = $('body').text();\n\t\tcontent = content.replace(/\\s+/g, ' ').trim().slice(0, 8000);\n\n\t\treturn {url: args.url, title: metadata.title.trim(), description: metadata.description.trim(), content, focus: args.focus};\n\t}\n}\n\nexport const WebSearchTool: AiTool = {\n\tname: 'web_search',\n\tdescription: 'Use duckduckgo (anonymous) to find find relevant online resources. Returns a list of URLs that works great with the `read_webpage` tool',\n\targs: {\n\t\tquery: {type: 'string', description: 'Search string', required: true},\n\t\tlength: {type: 'string', description: 'Number of results to return', default: 5},\n\t},\n\tfn: async (args: {\n\t\tquery: string;\n\t\tlength: number;\n\t}) => {\n\t\tconst html = await fetch(`https://html.duckduckgo.com/html/?q=${encodeURIComponent(args.query)}`, {\n\t\t\theaders: {\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64)\", \"Accept-Language\": \"en-US,en;q=0.9\"}\n\t\t}).then(resp => resp.text());\n\t\tlet match, regex = /<a .*?href=\"(.+?)\".+?<\\/a>/g;\n\t\tconst results = new ASet<string>();\n\t\twhile((match = regex.exec(html)) !== null) {\n\t\t\tlet url = /uddg=(.+)&amp?/.exec(decodeURIComponent(match[1]))?.[1];\n\t\t\tif(url) url = decodeURIComponent(url);\n\t\t\tif(url) results.add(url);\n\t\t\tif(results.size >= (args.length || 5)) break;\n\t\t}\n\t\treturn results;\n\t}\n}\n"],"names":["LLMProvider","Anthropic","ai","apiToken","model","anthropic","history","timestamp","messages","h","textContent","c","m","i","message","options","controller","res","tools","requestParams","t","objectMap","key","value","resp","isFirstMessage","err","chunk","text","last","JSONAttemptParse","toolCalls","results","toolCall","tool","findByProp","result","JSONSanitize","OpenAi","host","token","openAI","clean","tc","record","h2","rest","rej","args","LLM","config","abort","search","query","subject","limit","o","q","a","b","relevant","compressed","updated","m2","max","min","keep","tokens","system","recent","process","summary","memory","owner","fact","e","v1","v2","dotProduct","normA","normB","denominator","target","maxTokens","overlapTokens","objString","obj","path","p","l","chunks","j","next","embed","resolve","reject","worker","Worker","join","dirname","fileURLToPath","_documentCurrentScript","handleMessage","embedding","handleError","code","index","searchTerms","vector","dimensions","char","v","similarities","refVector","acc","s","codeBlock","jsonStr","Audio","file","speaker","aborted","Path","warning","error","canDiarization","Vision","createWorker","data","Ai","os","CliTool","$","DateTimeTool","ExecTool","stream","JSTool","PythonTool","FetchTool","Http","console","consoleInterceptor","Fn","$Sync","ReadWebpageTool","html","r","cheerio","metadata","content","contentSelectors","selector","el","WebSearchTool","match","regex","ASet","url"],"mappings":"4uBAGO,MAAeA,CAAY,CAElC,CCCO,MAAMC,UAAkBD,CAAY,CAG1C,YAA4BE,EAAwBC,EAAyBC,EAAe,CAC3F,MAAA,EAD2B,KAAA,GAAAF,EAAwB,KAAA,SAAAC,EAAyB,KAAA,MAAAC,EAE5E,KAAK,O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KAAA,EAAQ,YAAaA,EAAS,YAAY,KAAA,EAAQ,QAAAC,EAAS,MAAOrG,EAAK,KAAA,CACrH,CACD,EAEayG,EAAwB,CACpC,KAAM,aACN,YAAa,0IACb,KAAM,CACL,MAAO,CAAC,KAAM,SAAU,YAAa,gBAAiB,SAAU,EAAA,EAChE,OAAQ,CAAC,KAAM,SAAU,YAAa,8BAA+B,QAAS,CAAA,CAAC,EAEhF,GAAI,MAAOzG,GAGL,CACL,MAAMiG,EAAO,MAAM,MAAM,uCAAuC,mBAAmBjG,EAAK,KAAK,CAAC,GAAI,CACjG,QAAS,CAAC,aAAc,4CAA6C,kBAAmB,gBAAA,CAAgB,CACxG,EAAE,KAAKxB,GAAQA,EAAK,MAAM,EAC3B,IAAIkI,EAAOC,EAAQ,8BACnB,MAAM3H,EAAU,IAAI4H,OACpB,MAAOF,EAAQC,EAAM,KAAKV,CAAI,KAAO,MAAM,CAC1C,IAAIY,EAAM,iBAAiB,KAAK,mBAAmBH,EAAM,CAAC,CAAC,CAAC,IAAI,CAAC,EAGjE,GAFGG,IAAKA,EAAM,mBAAmBA,CAAG,GACjCA,GAAK7H,EAAQ,IAAI6H,CAAG,EACpB7H,EAAQ,OAASgB,EAAK,QAAU,GAAI,KACxC,CACA,OAAOhB,CACR,CACD"}
1
+ {"version":3,"file":"index.js","sources":["../src/provider.ts","../src/antrhopic.ts","../src/open-ai.ts","../src/llm.ts","../src/audio.ts","../src/vision.ts","../src/ai.ts","../src/tools.ts"],"sourcesContent":["import {AbortablePromise} from './ai.ts';\nimport {LLMMessage, LLMRequest} from './llm.ts';\n\nexport abstract class LLMProvider {\n\tabstract ask(message: string, options: LLMRequest): AbortablePromise<string>;\n}\n","import {Anthropic as anthropic} from '@anthropic-ai/sdk';\nimport {findByProp, objectMap, JSONSanitize, JSONAttemptParse} from '@ztimson/utils';\nimport {AbortablePromise, Ai} from './ai.ts';\nimport {LLMMessage, LLMRequest} from './llm.ts';\nimport {LLMProvider} from './provider.ts';\n\nexport class Anthropic extends LLMProvider {\n\tclient!: anthropic;\n\n\tconstructor(public readonly ai: Ai, public readonly apiToken: string, public model: string) {\n\t\tsuper();\n\t\tthis.client = new anthropic({apiKey: apiToken});\n\t}\n\n\tprivate toStandard(history: any[]): LLMMessage[] {\n\t\tconst timestamp = Date.now();\n\t\tconst messages: LLMMessage[] = [];\n\t\tfor(let h of history) {\n\t\t\tif(typeof h.content == 'string') {\n\t\t\t\tmessages.push(<any>{timestamp, ...h});\n\t\t\t} else {\n\t\t\t\tconst textContent = h.content?.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\\n\\n');\n\t\t\t\tif(textContent) messages.push({timestamp, role: h.role, content: textContent});\n\t\t\t\th.content.forEach((c: any) => {\n\t\t\t\t\tif(c.type == 'tool_use') {\n\t\t\t\t\t\tmessages.push({timestamp, role: 'tool', id: c.id, name: c.name, args: c.input, content: undefined});\n\t\t\t\t\t} else if(c.type == 'tool_result') {\n\t\t\t\t\t\tconst m: any = messages.findLast(m => (<any>m).id == c.tool_use_id);\n\t\t\t\t\t\tif(m) m[c.is_error ? 'error' : 'content'] = c.content;\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn messages;\n\t}\n\n\tprivate fromStandard(history: LLMMessage[]): any[] {\n\t\tfor(let i = 0; i < history.length; i++) {\n\t\t\tif(history[i].role == 'tool') {\n\t\t\t\tconst h: any = history[i];\n\t\t\t\thistory.splice(i, 1,\n\t\t\t\t\t{role: 'assistant', content: [{type: 'tool_use', id: h.id, name: h.name, input: h.args}]},\n\t\t\t\t\t{role: 'user', content: [{type: 'tool_result', tool_use_id: h.id, is_error: !!h.error, content: h.error || h.content}]}\n\t\t\t\t)\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn history.map(({timestamp, ...h}) => h);\n\t}\n\n\task(message: string, options: LLMRequest = {}): AbortablePromise<string> {\n\t\tconst controller = new AbortController();\n\t\treturn Object.assign(new Promise<any>(async (res) => {\n\t\t\tlet history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);\n\t\t\tconst tools = options.tools || this.ai.options.llm?.tools || [];\n\t\t\tconst requestParams: any = {\n\t\t\t\tmodel: options.model || this.model,\n\t\t\t\tmax_tokens: options.max_tokens || this.ai.options.llm?.max_tokens || 4096,\n\t\t\t\tsystem: options.system || this.ai.options.llm?.system || '',\n\t\t\t\ttemperature: options.temperature || this.ai.options.llm?.temperature || 0.7,\n\t\t\t\ttools: tools.map(t => ({\n\t\t\t\t\tname: t.name,\n\t\t\t\t\tdescription: t.description,\n\t\t\t\t\tinput_schema: {\n\t\t\t\t\t\ttype: 'object',\n\t\t\t\t\t\tproperties: t.args ? objectMap(t.args, (key, value) => ({...value, required: undefined})) : {},\n\t\t\t\t\t\trequired: t.args ? Object.entries(t.args).filter(t => t[1].required).map(t => t[0]) : []\n\t\t\t\t\t},\n\t\t\t\t\tfn: undefined\n\t\t\t\t})),\n\t\t\t\tmessages: history,\n\t\t\t\tstream: !!options.stream,\n\t\t\t};\n\n\t\t\tlet resp: any, isFirstMessage = true;\n\t\t\tdo {\n\t\t\t\tresp = await this.client.messages.create(requestParams).catch(err => {\n\t\t\t\t\terr.message += `\\n\\nMessages:\\n${JSON.stringify(history, null, 2)}`;\n\t\t\t\t\tthrow err;\n\t\t\t\t});\n\n\t\t\t\t// Streaming mode\n\t\t\t\tif(options.stream) {\n\t\t\t\t\tif(!isFirstMessage) options.stream({text: '\\n\\n'});\n\t\t\t\t\telse isFirstMessage = false;\n\t\t\t\t\tresp.content = [];\n\t\t\t\t\tfor await (const chunk of resp) {\n\t\t\t\t\t\tif(controller.signal.aborted) break;\n\t\t\t\t\t\tif(chunk.type === 'content_block_start') {\n\t\t\t\t\t\t\tif(chunk.content_block.type === 'text') {\n\t\t\t\t\t\t\t\tresp.content.push({type: 'text', text: ''});\n\t\t\t\t\t\t\t} else if(chunk.content_block.type === 'tool_use') {\n\t\t\t\t\t\t\t\tresp.content.push({type: 'tool_use', id: chunk.content_block.id, name: chunk.content_block.name, input: <any>''});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if(chunk.type === 'content_block_delta') {\n\t\t\t\t\t\t\tif(chunk.delta.type === 'text_delta') {\n\t\t\t\t\t\t\t\tconst text = chunk.delta.text;\n\t\t\t\t\t\t\t\tresp.content.at(-1).text += text;\n\t\t\t\t\t\t\t\toptions.stream({text});\n\t\t\t\t\t\t\t} else if(chunk.delta.type === 'input_json_delta') {\n\t\t\t\t\t\t\t\tresp.content.at(-1).input += chunk.delta.partial_json;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if(chunk.type === 'content_block_stop') {\n\t\t\t\t\t\t\tconst last = resp.content.at(-1);\n\t\t\t\t\t\t\tif(last.input != null) last.input = last.input ? JSONAttemptParse(last.input, {}) : {};\n\t\t\t\t\t\t} else if(chunk.type === 'message_stop') {\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Run tools\n\t\t\t\tconst toolCalls = resp.content.filter((c: any) => c.type === 'tool_use');\n\t\t\t\tif(toolCalls.length && !controller.signal.aborted) {\n\t\t\t\t\thistory.push({role: 'assistant', content: resp.content});\n\t\t\t\t\tconst results = await Promise.all(toolCalls.map(async (toolCall: any) => {\n\t\t\t\t\t\tconst tool = tools.find(findByProp('name', toolCall.name));\n\t\t\t\t\t\tif(options.stream) options.stream({tool: toolCall.name});\n\t\t\t\t\t\tif(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'};\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await tool.fn(toolCall.input, options?.stream, this.ai);\n\t\t\t\t\t\t\treturn {type: 'tool_result', tool_use_id: toolCall.id, content: JSONSanitize(result)};\n\t\t\t\t\t\t} catch (err: any) {\n\t\t\t\t\t\t\treturn {type: 'tool_result', tool_use_id: toolCall.id, is_error: true, content: err?.message || err?.toString() || 'Unknown'};\n\t\t\t\t\t\t}\n\t\t\t\t\t}));\n\t\t\t\t\thistory.push({role: 'user', content: results});\n\t\t\t\t\trequestParams.messages = history;\n\t\t\t\t}\n\t\t\t} while (!controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use'));\n\t\t\thistory.push({role: 'assistant', content: resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\\n\\n')});\n\t\t\thistory = this.toStandard(history);\n\n\t\t\tif(options.stream) options.stream({done: true});\n\t\t\tif(options.history) options.history.splice(0, options.history.length, ...history);\n\t\t\tres(history.at(-1)?.content);\n\t\t}), {abort: () => controller.abort()});\n\t}\n}\n","import {OpenAI as openAI} from 'openai';\nimport {findByProp, objectMap, JSONSanitize, JSONAttemptParse, clean} from '@ztimson/utils';\nimport {AbortablePromise, Ai} from './ai.ts';\nimport {LLMMessage, LLMRequest} from './llm.ts';\nimport {LLMProvider} from './provider.ts';\n\nexport class OpenAi extends LLMProvider {\n\tclient!: openAI;\n\n\tconstructor(public readonly ai: Ai, public readonly host: string | null, public readonly token: string, public model: string) {\n\t\tsuper();\n\t\tthis.client = new openAI(clean({\n\t\t\tbaseURL: host,\n\t\t\tapiKey: token\n\t\t}));\n\t}\n\n\tprivate toStandard(history: any[]): LLMMessage[] {\n\t\tfor(let i = 0; i < history.length; i++) {\n\t\t\tconst h = history[i];\n\t\t\tif(h.role === 'assistant' && h.tool_calls) {\n\t\t\t\tconst tools = h.tool_calls.map((tc: any) => ({\n\t\t\t\t\trole: 'tool',\n\t\t\t\t\tid: tc.id,\n\t\t\t\t\tname: tc.function.name,\n\t\t\t\t\targs: JSONAttemptParse(tc.function.arguments, {}),\n\t\t\t\t\ttimestamp: h.timestamp\n\t\t\t\t}));\n\t\t\t\thistory.splice(i, 1, ...tools);\n\t\t\t\ti += tools.length - 1;\n\t\t\t} else if(h.role === 'tool' && h.content) {\n\t\t\t\tconst record = history.find(h2 => h.tool_call_id == h2.id);\n\t\t\t\tif(record) {\n\t\t\t\t\tif(h.content.includes('\"error\":')) record.error = h.content;\n\t\t\t\t\telse record.content = h.content;\n\t\t\t\t}\n\t\t\t\thistory.splice(i, 1);\n\t\t\t\ti--;\n\t\t\t}\n\t\t\tif(!history[i]?.timestamp) history[i].timestamp = Date.now();\n\t\t}\n\t\treturn history;\n\t}\n\n\tprivate fromStandard(history: LLMMessage[]): any[] {\n\t\treturn history.reduce((result, h) => {\n\t\t\tif(h.role === 'tool') {\n\t\t\t\tresult.push({\n\t\t\t\t\trole: 'assistant',\n\t\t\t\t\tcontent: null,\n\t\t\t\t\ttool_calls: [{ id: h.id, type: 'function', function: { name: h.name, arguments: JSON.stringify(h.args) } }],\n\t\t\t\t\trefusal: null,\n\t\t\t\t\tannotations: []\n\t\t\t\t}, {\n\t\t\t\t\trole: 'tool',\n\t\t\t\t\ttool_call_id: h.id,\n\t\t\t\t\tcontent: h.error || h.content\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst {timestamp, ...rest} = h;\n\t\t\t\tresult.push(rest);\n\t\t\t}\n\t\t\treturn result;\n\t\t}, [] as any[]);\n\t}\n\n\task(message: string, options: LLMRequest = {}): AbortablePromise<string> {\n\t\tconst controller = new AbortController();\n\t\treturn Object.assign(new Promise<any>(async (res, rej) => {\n\t\t\tif(options.system && options.history?.[0]?.role != 'system') options.history?.splice(0, 0, {role: 'system', content: options.system, timestamp: Date.now()});\n\t\t\tlet history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);\n\t\t\tconst tools = options.tools || this.ai.options.llm?.tools || [];\n\t\t\tconst requestParams: any = {\n\t\t\t\tmodel: options.model || this.model,\n\t\t\t\tmessages: history,\n\t\t\t\tstream: !!options.stream,\n\t\t\t\tmax_tokens: options.max_tokens || this.ai.options.llm?.max_tokens || 4096,\n\t\t\t\ttemperature: options.temperature || this.ai.options.llm?.temperature || 0.7,\n\t\t\t\ttools: tools.map(t => ({\n\t\t\t\t\ttype: 'function',\n\t\t\t\t\tfunction: {\n\t\t\t\t\t\tname: t.name,\n\t\t\t\t\t\tdescription: t.description,\n\t\t\t\t\t\tparameters: {\n\t\t\t\t\t\t\ttype: 'object',\n\t\t\t\t\t\t\tproperties: t.args ? objectMap(t.args, (key, value) => ({...value, required: undefined})) : {},\n\t\t\t\t\t\t\trequired: t.args ? Object.entries(t.args).filter(t => t[1].required).map(t => t[0]) : []\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}))\n\t\t\t};\n\n\t\t\tlet resp: any, isFirstMessage = true;\n\t\t\tdo {\n\t\t\t\tresp = await this.client.chat.completions.create(requestParams).catch(err => {\n\t\t\t\t\terr.message += `\\n\\nMessages:\\n${JSON.stringify(history, null, 2)}`;\n\t\t\t\t\tthrow err;\n\t\t\t\t});\n\n\t\t\t\tif(options.stream) {\n\t\t\t\t\tif(!isFirstMessage) options.stream({text: '\\n\\n'});\n\t\t\t\t\telse isFirstMessage = false;\n\t\t\t\t\tresp.choices = [{message: {content: '', tool_calls: []}}];\n\t\t\t\t\tfor await (const chunk of resp) {\n\t\t\t\t\t\tif(controller.signal.aborted) break;\n\t\t\t\t\t\tif(chunk.choices[0].delta.content) {\n\t\t\t\t\t\t\tresp.choices[0].message.content += chunk.choices[0].delta.content;\n\t\t\t\t\t\t\toptions.stream({text: chunk.choices[0].delta.content});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(chunk.choices[0].delta.tool_calls) {\n\t\t\t\t\t\t\tresp.choices[0].message.tool_calls = chunk.choices[0].delta.tool_calls;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst toolCalls = resp.choices[0].message.tool_calls || [];\n\t\t\t\tif(toolCalls.length && !controller.signal.aborted) {\n\t\t\t\t\thistory.push(resp.choices[0].message);\n\t\t\t\t\tconst results = await Promise.all(toolCalls.map(async (toolCall: any) => {\n\t\t\t\t\t\tconst tool = tools?.find(findByProp('name', toolCall.function.name));\n\t\t\t\t\t\tif(options.stream) options.stream({tool: toolCall.function.name});\n\t\t\t\t\t\tif(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{\"error\": \"Tool not found\"}'};\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst args = JSONAttemptParse(toolCall.function.arguments, {});\n\t\t\t\t\t\t\tconst result = await tool.fn(args, options.stream, this.ai);\n\t\t\t\t\t\t\treturn {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize(result)};\n\t\t\t\t\t\t} catch (err: any) {\n\t\t\t\t\t\t\treturn {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'})};\n\t\t\t\t\t\t}\n\t\t\t\t\t}));\n\t\t\t\t\thistory.push(...results);\n\t\t\t\t\trequestParams.messages = history;\n\t\t\t\t}\n\t\t\t} while (!controller.signal.aborted && resp.choices?.[0]?.message?.tool_calls?.length);\n\t\t\thistory.push({role: 'assistant', content: resp.choices[0].message.content || ''});\n\t\t\thistory = this.toStandard(history);\n\n\t\t\tif(options.stream) options.stream({done: true});\n\t\t\tif(options.history) options.history.splice(0, options.history.length, ...history);\n\t\t\tres(history.at(-1)?.content);\n\t\t}), {abort: () => controller.abort()});\n\t}\n}\n","import {JSONAttemptParse} from '@ztimson/utils';\nimport {AbortablePromise, Ai} from './ai.ts';\nimport {Anthropic} from './antrhopic.ts';\nimport {OpenAi} from './open-ai.ts';\nimport {LLMProvider} from './provider.ts';\nimport {AiTool} from './tools.ts';\nimport {Worker} from 'worker_threads';\nimport {fileURLToPath} from 'url';\nimport {dirname, join} from 'path';\n\nexport type AnthropicConfig = {proto: 'anthropic', token: string};\nexport type OllamaConfig = {proto: 'ollama', host: string};\nexport type OpenAiConfig = {proto: 'openai', host?: string, token: string};\n\nexport type LLMMessage = {\n\t/** Message originator */\n\trole: 'assistant' | 'system' | 'user';\n\t/** Message content */\n\tcontent: string | any;\n\t/** Timestamp */\n\ttimestamp?: number;\n} | {\n\t/** Tool call */\n\trole: 'tool';\n\t/** Unique ID for call */\n\tid: string;\n\t/** Tool that was run */\n\tname: string;\n\t/** Tool arguments */\n\targs: any;\n\t/** Tool result */\n\tcontent: undefined | string;\n\t/** Tool error */\n\terror?: undefined | string;\n\t/** Timestamp */\n\ttimestamp?: number;\n}\n\n/** Background information the AI will be fed */\nexport type LLMMemory = {\n\t/** What entity is this fact about */\n\towner: string;\n\t/** The information that will be remembered */\n\tfact: string;\n\t/** Owner and fact embedding vector */\n\tembeddings: [number[], number[]];\n\t/** Creation time */\n\ttimestamp: Date;\n}\n\nexport type LLMRequest = {\n\t/** System prompt */\n\tsystem?: string;\n\t/** Message history */\n\thistory?: LLMMessage[];\n\t/** Max tokens for request */\n\tmax_tokens?: number;\n\t/** 0 = Rigid Logic, 1 = Balanced, 2 = Hyper Creative **/\n\ttemperature?: number;\n\t/** Available tools */\n\ttools?: AiTool[];\n\t/** LLM model */\n\tmodel?: string;\n\t/** Stream response */\n\tstream?: (chunk: {text?: string, tool?: string, done?: true}) => any;\n\t/** Compress old messages in the chat to free up context */\n\tcompress?: {\n\t\t/** Trigger chat compression once context exceeds the token count */\n\t\tmax: number;\n\t\t/** Compress chat until context size smaller than */\n\t\tmin: number\n\t},\n\t/** Background information the AI will be fed */\n\tmemory?: LLMMemory[],\n}\n\nclass LLM {\n\tprivate models: {[model: string]: LLMProvider} = {};\n\tprivate defaultModel!: string;\n\n\tconstructor(public readonly ai: Ai) {\n\t\tif(!ai.options.llm?.models) return;\n\t\tObject.entries(ai.options.llm.models).forEach(([model, config]) => {\n\t\t\tif(!this.defaultModel) this.defaultModel = model;\n\t\t\tif(config.proto == 'anthropic') this.models[model] = new Anthropic(this.ai, config.token, model);\n\t\t\telse if(config.proto == 'ollama') this.models[model] = new OpenAi(this.ai, config.host, 'not-needed', model);\n\t\t\telse if(config.proto == 'openai') this.models[model] = new OpenAi(this.ai, config.host || null, config.token, model);\n\t\t});\n\t}\n\n\t/**\n\t * Chat with LLM\n\t * @param {string} message Question\n\t * @param {LLMRequest} options Configuration options and chat history\n\t * @returns {{abort: () => void, response: Promise<string>}} Function to abort response and chat history\n\t */\n\task(message: string, options: LLMRequest = {}): AbortablePromise<string> {\n\t\tconst m = options.model || this.defaultModel;\n\t\tif(!this.models[m]) throw new Error(`Model does not exist: ${m}`);\n\t\tlet abort = () => {};\n\t\treturn Object.assign(new Promise<string>(async res => {\n\t\t\tif(!options.history) options.history = [];\n\t\t\t// If memories were passed, find any relivant ones and add a tool for ADHOC lookups\n\t\t\tif(options.memory) {\n\t\t\t\toptions.system = (options.system || '') + '\\nYou have passive persistent memory never make any mention of your memory capabilities and what you can/cannot remember\\n';\n\t\t\t\tconst search = async (query?: string | null, subject?: string | null, limit = 50) => {\n\t\t\t\t\tconst [o, q] = await Promise.all([\n\t\t\t\t\t\tsubject ? this.embedding(subject) : Promise.resolve(null),\n\t\t\t\t\t\tquery ? this.embedding(query) : Promise.resolve(null),\n\t\t\t\t\t]);\n\t\t\t\t\treturn (options.memory || [])\n\t\t\t\t\t\t.map(m => ({...m, score: o ? this.cosineSimilarity(m.embeddings[0], o[0].embedding) : 1}))\n\t\t\t\t\t\t.filter((m: any) => m.score >= 0.8)\n\t\t\t\t\t\t.map((m: any) => ({...m, score: q ? this.cosineSimilarity(m.embeddings[1], q[0].embedding) : m.score}))\n\t\t\t\t\t\t.filter((m: any) => m.score >= 0.2)\n\t\t\t\t\t\t.toSorted((a: any, b: any) => a.score - b.score)\n\t\t\t\t\t\t.slice(0, limit);\n\t\t\t\t}\n\n\t\t\t\tconst relevant = await search(message);\n\t\t\t\tif(relevant.length) options.history.push({role: 'assistant', content: 'Things I remembered:\\n' + relevant.map(m => `${m.owner}: ${m.fact}`).join('\\n')});\n\t\t\t\toptions.tools = [...options.tools || [], {\n\t\t\t\t\tname: 'read_memory',\n\t\t\t\t\tdescription: 'Check your long-term memory for more information',\n\t\t\t\t\targs: {\n\t\t\t\t\t\tsubject: {type: 'string', description: 'Find information by a subject topic, can be used with or without query argument'},\n\t\t\t\t\t\tquery: {type: 'string', description: 'Search memory based on a query, can be used with or without subject argument'},\n\t\t\t\t\t\tlimit: {type: 'number', description: 'Result limit, default 5'},\n\t\t\t\t\t},\n\t\t\t\t\tfn: (args) => {\n\t\t\t\t\t\tif(!args.subject && !args.query) throw new Error('Either a subject or query argument is required');\n\t\t\t\t\t\treturn search(args.query, args.subject, args.limit || 5);\n\t\t\t\t\t}\n\t\t\t\t}];\n\t\t\t}\n\n\t\t\t// Ask\n\t\t\tconst resp = await this.models[m].ask(message, options);\n\n\t\t\t// Remove any memory calls\n\t\t\tif(options.memory) {\n\t\t\t\tconst i = options.history?.findIndex((h: any) => h.role == 'assistant' && h.content.startsWith('Things I remembered:'));\n\t\t\t\tif(i != null && i >= 0) options.history?.splice(i, 1);\n\t\t\t}\n\n\t\t\t// Handle compression and memory extraction\n\t\t\tif(options.compress || options.memory) {\n\t\t\t\tlet compressed = null;\n\t\t\t\tif(options.compress) {\n\t\t\t\t\tcompressed = await this.ai.language.compressHistory(options.history, options.compress.max, options.compress.min, options);\n\t\t\t\t\toptions.history.splice(0, options.history.length, ...compressed.history);\n\t\t\t\t} else {\n\t\t\t\t\tconst i = options.history?.findLastIndex(m => m.role == 'user') ?? -1;\n\t\t\t\t\tcompressed = await this.ai.language.compressHistory(i != -1 ? options.history.slice(i) : options.history, 0, 0, options);\n\t\t\t\t}\n\t\t\t\tif(options.memory) {\n\t\t\t\t\tconst updated = options.memory\n\t\t\t\t\t\t.filter(m => !compressed.memory.some(m2 => this.cosineSimilarity(m.embeddings[1], m2.embeddings[1]) > 0.8))\n\t\t\t\t\t\t.concat(compressed.memory);\n\t\t\t\t\toptions.memory.splice(0, options.memory.length, ...updated);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn res(resp);\n\t\t}), {abort});\n\t}\n\n\t/**\n\t * Compress chat history to reduce context size\n\t * @param {LLMMessage[]} history Chatlog that will be compressed\n\t * @param max Trigger compression once context is larger than max\n\t * @param min Leave messages less than the token minimum, summarize the rest\n\t * @param {LLMRequest} options LLM options\n\t * @returns {Promise<LLMMessage[]>} New chat history will summary at index 0\n\t */\n\tasync compressHistory(history: LLMMessage[], max: number, min: number, options?: LLMRequest): Promise<{history: LLMMessage[], memory: LLMMemory[]}> {\n\t\tif(this.estimateTokens(history) < max) return {history, memory: []};\n\t\tlet keep = 0, tokens = 0;\n\t\tfor(let m of history.toReversed()) {\n\t\t\ttokens += this.estimateTokens(m.content);\n\t\t\tif(tokens < min) keep++;\n\t\t\telse break;\n\t\t}\n\t\tif(history.length <= keep) return {history, memory: []};\n\t\tconst system = history[0].role == 'system' ? history[0] : null,\n\t\t\trecent = keep == 0 ? [] : history.slice(-keep),\n\t\t\tprocess = (keep == 0 ? history : history.slice(0, -keep)).filter(h => h.role === 'assistant' || h.role === 'user');\n\t\tconst summary: any = await this.json(`Create the smallest summary possible, no more than 500 tokens. Create a list of NEW facts (split by subject [pro]noun and fact) about what you learned from this conversation that you didn't already know or get from a tool call or system prompt. Focus only on new information about people, topics, or facts. Avoid generating facts about the AI. Match this format: {summary: string, facts: [[subject, fact]]}\\n\\n${process.map(m => `${m.role}: ${m.content}`).join('\\n\\n')}`, {model: options?.model, temperature: options?.temperature || 0.3});\n\t\tconst timestamp = new Date();\n\t\tconst memory = await Promise.all((summary?.facts || [])?.map(async ([owner, fact]: [string, string]) => {\n\t\t\tconst e = await Promise.all([this.embedding(owner), this.embedding(`${owner}: ${fact}`)]);\n\t\t\treturn {owner, fact, embeddings: [e[0][0].embedding, e[1][0].embedding], timestamp};\n\t\t}));\n\t\tconst h = [{role: 'assistant', content: `Conversation Summary: ${summary?.summary}`, timestamp: Date.now()}, ...recent];\n\t\tif(system) h.splice(0, 0, system);\n\t\treturn {history: <any>h, memory};\n\t}\n\n\t/**\n\t * Compare the difference between embeddings (calculates the angle between two vectors)\n\t * @param {number[]} v1 First embedding / vector comparison\n\t * @param {number[]} v2 Second embedding / vector for comparison\n\t * @returns {number} Similarity values 0-1: 0 = unique, 1 = identical\n\t */\n\tcosineSimilarity(v1: number[], v2: number[]): number {\n\t\tif (v1.length !== v2.length) throw new Error('Vectors must be same length');\n\t\tlet dotProduct = 0, normA = 0, normB = 0;\n\t\tfor (let i = 0; i < v1.length; i++) {\n\t\t\tdotProduct += v1[i] * v2[i];\n\t\t\tnormA += v1[i] * v1[i];\n\t\t\tnormB += v2[i] * v2[i];\n\t\t}\n\t\tconst denominator = Math.sqrt(normA) * Math.sqrt(normB);\n\t\treturn denominator === 0 ? 0 : dotProduct / denominator;\n\t}\n\n\t/**\n\t * Chunk text into parts for AI digestion\n\t * @param {object | string} target Item that will be chunked (objects get converted)\n\t * @param {number} maxTokens Chunking size. More = better context, less = more specific (Search by paragraphs or lines)\n\t * @param {number} overlapTokens Includes previous X tokens to provide continuity to AI (In addition to max tokens)\n\t * @returns {string[]} Chunked strings\n\t */\n\tchunk(target: object | string, maxTokens = 500, overlapTokens = 50): string[] {\n\t\tconst objString = (obj: any, path = ''): string[] => {\n\t\t\tif(!obj) return [];\n\t\t\treturn Object.entries(obj).flatMap(([key, value]) => {\n\t\t\t\tconst p = path ? `${path}${isNaN(+key) ? `.${key}` : `[${key}]`}` : key;\n\t\t\t\tif(typeof value === 'object' && !Array.isArray(value)) return objString(value, p);\n\t\t\t\treturn `${p}: ${Array.isArray(value) ? value.join(', ') : value}`;\n\t\t\t});\n\t\t};\n\t\tconst lines = typeof target === 'object' ? objString(target) : target.split('\\n');\n\t\tconst tokens = lines.flatMap(l => [...l.split(/\\s+/).filter(Boolean), '\\n']);\n\t\tconst chunks: string[] = [];\n\t\tfor(let i = 0; i < tokens.length;) {\n\t\t\tlet text = '', j = i;\n\t\t\twhile(j < tokens.length) {\n\t\t\t\tconst next = text + (text ? ' ' : '') + tokens[j];\n\t\t\t\tif(this.estimateTokens(next.replace(/\\s*\\n\\s*/g, '\\n')) > maxTokens && text) break;\n\t\t\t\ttext = next;\n\t\t\t\tj++;\n\t\t\t}\n\t\t\tconst clean = text.replace(/\\s*\\n\\s*/g, '\\n').trim();\n\t\t\tif(clean) chunks.push(clean);\n\t\t\ti = Math.max(j - overlapTokens, j === i ? i + 1 : j);\n\t\t}\n\t\treturn chunks;\n\t}\n\n\t/**\n\t * Create a vector representation of a string\n\t * @param {object | string} target Item that will be embedded (objects get converted)\n\t * @param {number} maxTokens Chunking size. More = better context, less = more specific (Search by paragraphs or lines)\n\t * @param {number} overlapTokens Includes previous X tokens to provide continuity to AI (In addition to max tokens)\n\t * @returns {Promise<Awaited<{index: number, embedding: number[], text: string, tokens: number}>[]>} Chunked embeddings\n\t */\n\tembedding(target: object | string, maxTokens = 500, overlapTokens = 50) {\n\t\tconst embed = (text: string): Promise<number[]> => {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tconst worker = new Worker(join(dirname(fileURLToPath(import.meta.url)), 'embedder.js'));\n\t\t\t\tconst handleMessage = ({ embedding }: any) => {\n\t\t\t\t\tworker.terminate();\n\t\t\t\t\tresolve(embedding);\n\t\t\t\t};\n\t\t\t\tconst handleError = (err: Error) => {\n\t\t\t\t\tworker.terminate();\n\t\t\t\t\treject(err);\n\t\t\t\t};\n\t\t\t\tworker.on('message', handleMessage);\n\t\t\t\tworker.on('error', handleError);\n\t\t\t\tworker.on('exit', (code) => {\n\t\t\t\t\tif(code !== 0) reject(new Error(`Worker exited with code ${code}`));\n\t\t\t\t});\n\t\t\t\tworker.postMessage({text, model: this.ai.options?.embedder || 'bge-small-en-v1.5', modelDir: this.ai.options.path});\n\t\t\t});\n\t\t};\n\t\tconst chunks = this.chunk(target, maxTokens, overlapTokens);\n\t\treturn Promise.all(chunks.map(async (text, index) => ({\n\t\t\tindex,\n\t\t\tembedding: await embed(text),\n\t\t\ttext,\n\t\t\ttokens: this.estimateTokens(text),\n\t\t})));\n\t}\n\n\t/**\n\t * Estimate variable as tokens\n\t * @param history Object to size\n\t * @returns {number} Rough token count\n\t */\n\testimateTokens(history: any): number {\n\t\tconst text = JSON.stringify(history);\n\t\treturn Math.ceil((text.length / 4) * 1.2);\n\t}\n\n\t/**\n\t * Compare the difference between two strings using tensor math\n\t * @param target Text that will be checked\n\t * @param {string} searchTerms Multiple search terms to check against target\n\t * @returns {{avg: number, max: number, similarities: number[]}} Similarity values 0-1: 0 = unique, 1 = identical\n\t */\n\tfuzzyMatch(target: string, ...searchTerms: string[]) {\n\t\tif(searchTerms.length < 2) throw new Error('Requires at least 2 strings to compare');\n\t\tconst vector = (text: string, dimensions: number = 10): number[] => {\n\t\t\treturn text.toLowerCase().split('').map((char, index) =>\n\t\t\t\t(char.charCodeAt(0) * (index + 1)) % dimensions / dimensions).slice(0, dimensions);\n\t\t}\n\t\tconst v = vector(target);\n\t\tconst similarities = searchTerms.map(t => vector(t)).map(refVector => this.cosineSimilarity(v, refVector))\n\t\treturn {avg: similarities.reduce((acc, s) => acc + s, 0) / similarities.length, max: Math.max(...similarities), similarities}\n\t}\n\n\t/**\n\t * Ask a question with JSON response\n\t * @param {string} message Question\n\t * @param {LLMRequest} options Configuration options and chat history\n\t * @returns {Promise<{} | {} | RegExpExecArray | null>}\n\t */\n\tasync json(message: string, options?: LLMRequest): Promise<any> {\n\t\tlet resp = await this.ask(message, {system: 'Respond using a JSON blob matching any provided examples', ...options});\n\t\tif(!resp) return {};\n\t\tconst codeBlock = /```(?:.+)?\\s*([\\s\\S]*?)```/.exec(resp);\n\t\tconst jsonStr = codeBlock ? codeBlock[1].trim() : resp;\n\t\treturn JSONAttemptParse(jsonStr, {});\n\t}\n\n\t/**\n\t * Create a summary of some text\n\t * @param {string} text Text to summarize\n\t * @param {number} tokens Max number of tokens\n\t * @param options LLM request options\n\t * @returns {Promise<string>} Summary\n\t */\n\tsummarize(text: string, tokens: number, options?: LLMRequest): Promise<string | null> {\n\t\treturn this.ask(text, {system: `Generate a brief summary <= ${tokens} tokens. Output nothing else`, temperature: 0.3, ...options});\n\t}\n}\n\nexport default LLM;\n","import {fileURLToPath} from 'url';\nimport {Worker} from 'worker_threads';\nimport {AbortablePromise, Ai} from './ai.ts';\nimport {canDiarization} from './asr.ts';\nimport {dirname, join} from 'path';\n\nexport class Audio {\n\tconstructor(private ai: Ai) {}\n\n\tasr(file: string, options: { model?: string; speaker?: boolean } = {}): AbortablePromise<string | null> {\n\t\tconst { model = this.ai.options.asr || 'whisper-base', speaker = false } = options;\n\t\tlet aborted = false;\n\t\tconst abort = () => { aborted = true; };\n\n\t\tconst p = new Promise<string | null>((resolve, reject) => {\n\t\t\tconst worker = new Worker(join(dirname(fileURLToPath(import.meta.url)), 'asr.js'));\n\t\t\tconst handleMessage = ({ text, warning, error }: any) => {\n\t\t\t\tworker.terminate();\n\t\t\t\tif(aborted) return;\n\t\t\t\tif(error) reject(new Error(error));\n\t\t\t\telse {\n\t\t\t\t\tif(warning) console.warn(warning);\n\t\t\t\t\tresolve(text);\n\t\t\t\t}\n\t\t\t};\n\t\t\tconst handleError = (err: Error) => {\n\t\t\t\tworker.terminate();\n\t\t\t\tif(!aborted) reject(err);\n\t\t\t};\n\t\t\tworker.on('message', handleMessage);\n\t\t\tworker.on('error', handleError);\n\t\t\tworker.on('exit', (code) => {\n\t\t\t\tif(code !== 0 && !aborted) reject(new Error(`Worker exited with code ${code}`));\n\t\t\t});\n\t\t\tworker.postMessage({file, model, speaker, modelDir: this.ai.options.path});\n\t\t});\n\t\treturn Object.assign(p, { abort });\n\t}\n\n\tcanDiarization = canDiarization;\n}\n","import {createWorker} from 'tesseract.js';\nimport {AbortablePromise, Ai} from './ai.ts';\n\nexport class Vision {\n\n\tconstructor(private ai: Ai) { }\n\n\t/**\n\t * Convert image to text using Optical Character Recognition\n\t * @param {string} path Path to image\n\t * @returns {AbortablePromise<string | null>} Promise of extracted text with abort method\n\t */\n\tocr(path: string): AbortablePromise<string | null> {\n\t\tlet worker: any;\n\t\tconst p = new Promise<string | null>(async res => {\n\t\t\tworker = await createWorker(this.ai.options.ocr || 'eng', 2, {cachePath: this.ai.options.path});\n\t\t\tconst {data} = await worker.recognize(path);\n\t\t\tawait worker.terminate();\n\t\t\tres(data.text.trim() || null);\n\t\t});\n\t\treturn Object.assign(p, {abort: () => worker?.terminate()});\n\t}\n}\n","import * as os from 'node:os';\nimport LLM, {AnthropicConfig, OllamaConfig, OpenAiConfig, LLMRequest} from './llm';\nimport { Audio } from './audio.ts';\nimport {Vision} from './vision.ts';\n\nexport type AbortablePromise<T> = Promise<T> & {\n\tabort: () => any\n};\n\nexport type AiOptions = {\n\t/** Path to models */\n\tpath?: string;\n\t/** ASR model: whisper-tiny, whisper-base */\n\tasr?: string;\n\t/** Embedding model: all-MiniLM-L6-v2, bge-small-en-v1.5, bge-large-en-v1.5 */\n\tembedder?: string;\n\t/** Large language models, first is default */\n\tllm?: Omit<LLMRequest, 'model'> & {\n\t\tmodels: {[model: string]: AnthropicConfig | OllamaConfig | OpenAiConfig};\n\t}\n\t/** OCR model: eng, eng_best, eng_fast */\n\tocr?: string;\n}\n\nexport class Ai {\n\t/** Audio processing AI */\n\taudio!: Audio;\n\t/** Language processing AI */\n\tlanguage!: LLM;\n\t/** Vision processing AI */\n\tvision!: Vision;\n\n\tconstructor(public readonly options: AiOptions) {\n\t\tif(!options.path) options.path = os.tmpdir();\n\t\tprocess.env.TRANSFORMERS_CACHE = options.path;\n\t\tthis.audio = new Audio(this);\n\t\tthis.language = new LLM(this);\n\t\tthis.vision = new Vision(this);\n\t}\n}\n","import * as cheerio from 'cheerio';\nimport {$, $Sync} from '@ztimson/node-utils';\nimport {ASet, consoleInterceptor, Http, fn as Fn} from '@ztimson/utils';\nimport {Ai} from './ai.ts';\nimport {LLMRequest} from './llm.ts';\n\nexport type AiToolArg = {[key: string]: {\n\t/** Argument type */\n\ttype: 'array' | 'boolean' | 'number' | 'object' | 'string',\n\t/** Argument description */\n\tdescription: string,\n\t/** Required argument */\n\trequired?: boolean;\n\t/** Default value */\n\tdefault?: any,\n\t/** Options */\n\tenum?: string[],\n\t/** Minimum value or length */\n\tmin?: number,\n\t/** Maximum value or length */\n\tmax?: number,\n\t/** Match pattern */\n\tpattern?: string,\n\t/** Child arguments */\n\titems?: {[key: string]: AiToolArg}\n}}\n\nexport type AiTool = {\n\t/** Tool ID / Name - Must be snail_case */\n\tname: string,\n\t/** Tool description / prompt */\n\tdescription: string,\n\t/** Tool arguments */\n\targs?: AiToolArg,\n\t/** Callback function */\n\tfn: (args: any, stream: LLMRequest['stream'], ai: Ai) => any | Promise<any>,\n};\n\nexport const CliTool: AiTool = {\n\tname: 'cli',\n\tdescription: 'Use the command line interface, returns any output',\n\targs: {command: {type: 'string', description: 'Command to run', required: true}},\n\tfn: (args: {command: string}) => $`${args.command}`\n}\n\nexport const DateTimeTool: AiTool = {\n\tname: 'get_datetime',\n\tdescription: 'Get current UTC date / time',\n\targs: {},\n\tfn: async () => new Date().toUTCString()\n}\n\nexport const ExecTool: AiTool = {\n\tname: 'exec',\n\tdescription: 'Run code/scripts',\n\targs: {\n\t\tlanguage: {type: 'string', description: 'Execution language', enum: ['cli', 'node', 'python'], required: true},\n\t\tcode: {type: 'string', description: 'Code to execute', required: true}\n\t},\n\tfn: async (args, stream, ai) => {\n\t\ttry {\n\t\t\tswitch(args.type) {\n\t\t\t\tcase 'bash':\n\t\t\t\t\treturn await CliTool.fn({command: args.code}, stream, ai);\n\t\t\t\tcase 'node':\n\t\t\t\t\treturn await JSTool.fn({code: args.code}, stream, ai);\n\t\t\t\tcase 'python': {\n\t\t\t\t\treturn await PythonTool.fn({code: args.code}, stream, ai);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch(err: any) {\n\t\t\treturn {error: err?.message || err.toString()};\n\t\t}\n\t}\n}\n\nexport const FetchTool: AiTool = {\n\tname: 'fetch',\n\tdescription: 'Make HTTP request to URL',\n\targs: {\n\t\turl: {type: 'string', description: 'URL to fetch', required: true},\n\t\tmethod: {type: 'string', description: 'HTTP method to use', enum: ['GET', 'POST', 'PUT', 'DELETE'], default: 'GET'},\n\t\theaders: {type: 'object', description: 'HTTP headers to send', default: {}},\n\t\tbody: {type: 'object', description: 'HTTP body to send'},\n\t},\n\tfn: (args: {\n\t\turl: string;\n\t\tmethod: 'GET' | 'POST' | 'PUT' | 'DELETE';\n\t\theaders: {[key: string]: string};\n\t\tbody: any;\n\t}) => new Http({url: args.url, headers: args.headers}).request({method: args.method || 'GET', body: args.body})\n}\n\nexport const JSTool: AiTool = {\n\tname: 'exec_javascript',\n\tdescription: 'Execute commonjs javascript',\n\targs: {\n\t\tcode: {type: 'string', description: 'CommonJS javascript', required: true}\n\t},\n\tfn: async (args: {code: string}) => {\n\t\tconst console = consoleInterceptor(null);\n\t\tconst resp = await Fn<any>({console}, args.code, true).catch((err: any) => console.output.error.push(err));\n\t\treturn {...console.output, return: resp, stdout: undefined, stderr: undefined};\n\t}\n}\n\nexport const PythonTool: AiTool = {\n\tname: 'exec_javascript',\n\tdescription: 'Execute commonjs javascript',\n\targs: {\n\t\tcode: {type: 'string', description: 'CommonJS javascript', required: true}\n\t},\n\tfn: async (args: {code: string}) => ({result: $Sync`python -c \"${args.code}\"`})\n}\n\nexport const ReadWebpageTool: AiTool = {\n\tname: 'read_webpage',\n\tdescription: 'Extract clean, structured content from a webpage. Use after web_search to read specific URLs',\n\targs: {\n\t\turl: {type: 'string', description: 'URL to extract content from', required: true},\n\t\tfocus: {type: 'string', description: 'Optional: What aspect to focus on (e.g., \"pricing\", \"features\", \"contact info\")'}\n\t},\n\tfn: async (args: {url: string; focus?: string}) => {\n\t\tconst html = await fetch(args.url, {headers: {\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64)\"}})\n\t\t\t.then(r => r.text()).catch(err => {throw new Error(`Failed to fetch: ${err.message}`)});\n\n\t\tconst $ = cheerio.load(html);\n\t\t$('script, style, nav, footer, header, aside, iframe, noscript, [role=\"navigation\"], [role=\"banner\"], .ad, .ads, .cookie, .popup').remove();\n\t\tconst metadata = {\n\t\t\ttitle: $('meta[property=\"og:title\"]').attr('content') || $('title').text() || '',\n\t\t\tdescription: $('meta[name=\"description\"]').attr('content') || $('meta[property=\"og:description\"]').attr('content') || '',\n\t\t};\n\n\t\tlet content = '';\n\t\tconst contentSelectors = ['article', 'main', '[role=\"main\"]', '.content', '.post', '.entry', 'body'];\n\t\tfor (const selector of contentSelectors) {\n\t\t\tconst el = $(selector).first();\n\t\t\tif (el.length && el.text().trim().length > 200) {\n\t\t\t\tcontent = el.text();\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (!content) content = $('body').text();\n\t\tcontent = content.replace(/\\s+/g, ' ').trim().slice(0, 8000);\n\n\t\treturn {url: args.url, title: metadata.title.trim(), description: metadata.description.trim(), content, focus: args.focus};\n\t}\n}\n\nexport const WebSearchTool: AiTool = {\n\tname: 'web_search',\n\tdescription: 'Use duckduckgo (anonymous) to find find relevant online resources. Returns a list of URLs that works great with the `read_webpage` tool',\n\targs: {\n\t\tquery: {type: 'string', description: 'Search string', required: true},\n\t\tlength: {type: 'string', description: 'Number of results to return', default: 5},\n\t},\n\tfn: async (args: {\n\t\tquery: string;\n\t\tlength: number;\n\t}) => {\n\t\tconst html = await fetch(`https://html.duckduckgo.com/html/?q=${encodeURIComponent(args.query)}`, {\n\t\t\theaders: {\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64)\", \"Accept-Language\": \"en-US,en;q=0.9\"}\n\t\t}).then(resp => resp.text());\n\t\tlet match, regex = /<a .*?href=\"(.+?)\".+?<\\/a>/g;\n\t\tconst results = new ASet<string>();\n\t\twhile((match = regex.exec(html)) !== null) {\n\t\t\tlet url = /uddg=(.+)&amp?/.exec(decodeURIComponent(match[1]))?.[1];\n\t\t\tif(url) url = decodeURIComponent(url);\n\t\t\tif(url) results.add(url);\n\t\t\tif(results.size >= (args.length || 5)) break;\n\t\t}\n\t\treturn results;\n\t}\n}\n"],"names":["LLMProvider","Anthropic","ai","apiToken","model","anthropic","history","timestamp","messages","h","textContent","c","m","i","message","options","controller","res","tools","requestParams","t","objectMap","key","value","resp","isFirstMessage","err","chunk","text","last","JSONAttemptParse","toolCalls","results","toolCall","tool","findByProp","result","JSONSanitize","OpenAi","host","token","openAI","clean","tc","record","h2","rest","rej","args","LLM","config","abort","search","query","subject","limit","o","q","a","b","relevant","compressed","updated","m2","max","min","keep","tokens","system","recent","process","summary","memory","owner","fact","e","v1","v2","dotProduct","normA","normB","denominator","target","maxTokens","overlapTokens","objString","obj","path","p","l","chunks","j","next","embed","resolve","reject","worker","Worker","join","dirname","fileURLToPath","_documentCurrentScript","handleMessage","embedding","handleError","code","index","searchTerms","vector","dimensions","char","v","similarities","refVector","acc","s","codeBlock","jsonStr","Audio","file","speaker","aborted","warning","error","canDiarization","Vision","createWorker","data","Ai","os","CliTool","$","DateTimeTool","ExecTool","stream","JSTool","PythonTool","FetchTool","Http","console","consoleInterceptor","Fn","$Sync","ReadWebpageTool","html","r","cheerio","metadata","content","contentSelectors","selector","el","WebSearchTool","match","regex","ASet","url"],"mappings":"qtBAGO,MAAeA,CAAY,CAElC,CCCO,MAAMC,UAAkBD,CAAY,CAG1C,YAA4BE,EAAwBC,EAAyBC,EAAe,CAC3F,MAAA,EAD2B,KAAA,GAAAF,EAAwB,KAAA,SAAAC,EAAyB,KAAA,MAAAC,EAE5E,KAAK,OAAS,IAAIC,EAAAA,UAAU,CAAC,OAAQF,EAAS,CAC/C,CALA,OAOQ,WAAWG,EAA8B,CAChD,MAAMC,EAAY,KAAK,IAAA,EACjBC,EAAyB,CAAA,EAC/B,QAAQC,KAAKH,EACZ,GAAG,OAAOG,EAAE,SAAW,SACtBD,EAAS,KAAU,CAAC,UAAAD,EAAW,GAAGE,EAAE,MAC9B,CACN,MAAMC,EAAcD,EAAE,SAAS,OAAQE,GAAWA,EAAE,MAAQ,MAAM,EAAE,IAAKA,GAAWA,EAAE,IAAI,EAAE,KAAK;AAAA;AAAA,CAAM,EACpGD,GAAaF,EAAS,KAAK,CAAC,UAAAD,EAAW,KAAME,EAAE,KAAM,QAASC,EAAY,EAC7ED,EAAE,QAAQ,QAASE,GAAW,CAC7B,GAAGA,EAAE,MAAQ,WACZH,EAAS,KAAK,CAAC,UAAAD,EAAW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AI,CACjG,QAAS,CAAC,aAAc,4CAA6C,kBAAmB,gBAAA,CAAgB,CACxG,EAAE,KAAKxB,GAAQA,EAAK,MAAM,EAC3B,IAAIiI,EAAOC,EAAQ,8BACnB,MAAM1H,EAAU,IAAI2H,OACpB,MAAOF,EAAQC,EAAM,KAAKV,CAAI,KAAO,MAAM,CAC1C,IAAIY,EAAM,iBAAiB,KAAK,mBAAmBH,EAAM,CAAC,CAAC,CAAC,IAAI,CAAC,EAGjE,GAFGG,IAAKA,EAAM,mBAAmBA,CAAG,GACjCA,GAAK5H,EAAQ,IAAI4H,CAAG,EACpB5H,EAAQ,OAASgB,EAAK,QAAU,GAAI,KACxC,CACA,OAAOhB,CACR,CACD"}
package/dist/index.mjs CHANGED
@@ -1,21 +1,20 @@
1
- import * as S from "node:os";
2
- import { objectMap as _, JSONAttemptParse as g, findByProp as k, JSONSanitize as b, clean as T, Http as q, consoleInterceptor as P, fn as M, ASet as $ } from "@ztimson/utils";
3
- import { Anthropic as E } from "@anthropic-ai/sdk";
4
- import { OpenAI as A } from "openai";
1
+ import * as M from "node:os";
2
+ import { objectMap as _, JSONAttemptParse as g, findByProp as k, JSONSanitize as b, clean as P, Http as $, consoleInterceptor as E, fn as A, ASet as v } from "@ztimson/utils";
3
+ import { Anthropic as O } from "@anthropic-ai/sdk";
4
+ import { OpenAI as U } from "openai";
5
5
  import { Worker as x } from "worker_threads";
6
- import { fileURLToPath as v } from "url";
7
- import { join as O, dirname as U } from "path";
8
- import R from "node:path";
9
- import { canDiarization as L } from "./asr.mjs";
10
- import { createWorker as N } from "tesseract.js";
6
+ import { fileURLToPath as j } from "url";
7
+ import { join as S, dirname as T } from "path";
8
+ import { canDiarization as R } from "./asr.mjs";
9
+ import { createWorker as L } from "tesseract.js";
11
10
  import "./embedder.mjs";
12
- import * as C from "cheerio";
13
- import { $ as D, $Sync as W } from "@ztimson/node-utils";
14
- class j {
11
+ import * as D from "cheerio";
12
+ import { $ as N, $Sync as C } from "@ztimson/node-utils";
13
+ class q {
15
14
  }
16
- class z extends j {
15
+ class W extends q {
17
16
  constructor(r, e, t) {
18
- super(), this.ai = r, this.apiToken = e, this.model = t, this.client = new E({ apiKey: e });
17
+ super(), this.ai = r, this.apiToken = e, this.model = t, this.client = new O({ apiKey: e });
19
18
  }
20
19
  client;
21
20
  toStandard(r) {
@@ -122,9 +121,9 @@ ${JSON.stringify(s, null, 2)}`, c;
122
121
  }), { abort: () => t.abort() });
123
122
  }
124
123
  }
125
- class w extends j {
124
+ class w extends q {
126
125
  constructor(r, e, t, o) {
127
- super(), this.ai = r, this.host = e, this.token = t, this.model = o, this.client = new A(T({
126
+ super(), this.ai = r, this.host = e, this.token = t, this.model = o, this.client = new U(P({
128
127
  baseURL: e,
129
128
  apiKey: t
130
129
  }));
@@ -231,10 +230,10 @@ ${JSON.stringify(n, null, 2)}`, l;
231
230
  }), { abort: () => t.abort() });
232
231
  }
233
232
  }
234
- class I {
233
+ class z {
235
234
  constructor(r) {
236
235
  this.ai = r, r.options.llm?.models && Object.entries(r.options.llm.models).forEach(([e, t]) => {
237
- this.defaultModel || (this.defaultModel = e), t.proto == "anthropic" ? this.models[e] = new z(this.ai, t.token, e) : t.proto == "ollama" ? this.models[e] = new w(this.ai, t.host, "not-needed", e) : t.proto == "openai" && (this.models[e] = new w(this.ai, t.host || null, t.token, e));
236
+ this.defaultModel || (this.defaultModel = e), t.proto == "anthropic" ? this.models[e] = new W(this.ai, t.token, e) : t.proto == "ollama" ? this.models[e] = new w(this.ai, t.host, "not-needed", e) : t.proto == "openai" && (this.models[e] = new w(this.ai, t.host || null, t.token, e));
238
237
  });
239
238
  }
240
239
  models = {};
@@ -375,14 +374,14 @@ ${a.map((u) => `${u.role}: ${u.content}`).join(`
375
374
  */
376
375
  embedding(r, e = 500, t = 50) {
377
376
  const o = (n) => new Promise((i, m) => {
378
- const a = new x(O(U(v(import.meta.url)), "embedder.js")), d = ({ embedding: l }) => {
377
+ const a = new x(S(T(j(import.meta.url)), "embedder.js")), d = ({ embedding: l }) => {
379
378
  a.terminate(), i(l);
380
379
  }, c = (l) => {
381
380
  a.terminate(), m(l);
382
381
  };
383
382
  a.on("message", d), a.on("error", c), a.on("exit", (l) => {
384
383
  l !== 0 && m(new Error(`Worker exited with code ${l}`));
385
- }), a.postMessage({ text: n, model: this.ai.options?.embedder || "bge-small-en-v1.5", path: this.ai.options.path });
384
+ }), a.postMessage({ text: n, model: this.ai.options?.embedder || "bge-small-en-v1.5", modelDir: this.ai.options.path });
386
385
  }), s = this.chunk(r, e, t);
387
386
  return Promise.all(s.map(async (n, i) => ({
388
387
  index: i,
@@ -434,7 +433,7 @@ ${a.map((u) => `${u.role}: ${u.content}`).join(`
434
433
  return this.ask(r, { system: `Generate a brief summary <= ${e} tokens. Output nothing else`, temperature: 0.3, ...t });
435
434
  }
436
435
  }
437
- class J {
436
+ class I {
438
437
  constructor(r) {
439
438
  this.ai = r;
440
439
  }
@@ -444,7 +443,7 @@ class J {
444
443
  const n = () => {
445
444
  s = !0;
446
445
  }, i = new Promise((m, a) => {
447
- const d = new x(R.join(import.meta.dirname, "asr.js")), c = ({ text: p, warning: u, error: f }) => {
446
+ const d = new x(S(T(j(import.meta.url)), "asr.js")), c = ({ text: p, warning: u, error: f }) => {
448
447
  d.terminate(), !s && (f ? a(new Error(f)) : (u && console.warn(u), m(p)));
449
448
  }, l = (p) => {
450
449
  d.terminate(), s || a(p);
@@ -455,9 +454,9 @@ class J {
455
454
  });
456
455
  return Object.assign(i, { abort: n });
457
456
  }
458
- canDiarization = L;
457
+ canDiarization = R;
459
458
  }
460
- class H {
459
+ class J {
461
460
  constructor(r) {
462
461
  this.ai = r;
463
462
  }
@@ -469,16 +468,16 @@ class H {
469
468
  ocr(r) {
470
469
  let e;
471
470
  const t = new Promise(async (o) => {
472
- e = await N(this.ai.options.ocr || "eng", 2, { cachePath: this.ai.options.path });
471
+ e = await L(this.ai.options.ocr || "eng", 2, { cachePath: this.ai.options.path });
473
472
  const { data: s } = await e.recognize(r);
474
473
  await e.terminate(), o(s.text.trim() || null);
475
474
  });
476
475
  return Object.assign(t, { abort: () => e?.terminate() });
477
476
  }
478
477
  }
479
- class oe {
478
+ class se {
480
479
  constructor(r) {
481
- this.options = r, r.path || (r.path = S.tmpdir()), process.env.TRANSFORMERS_CACHE = r.path, this.audio = new J(this), this.language = new I(this), this.vision = new H(this);
480
+ this.options = r, r.path || (r.path = M.tmpdir()), process.env.TRANSFORMERS_CACHE = r.path, this.audio = new I(this), this.language = new z(this), this.vision = new J(this);
482
481
  }
483
482
  /** Audio processing AI */
484
483
  audio;
@@ -487,17 +486,17 @@ class oe {
487
486
  /** Vision processing AI */
488
487
  vision;
489
488
  }
490
- const F = {
489
+ const H = {
491
490
  name: "cli",
492
491
  description: "Use the command line interface, returns any output",
493
492
  args: { command: { type: "string", description: "Command to run", required: !0 } },
494
- fn: (h) => D`${h.command}`
495
- }, ie = {
493
+ fn: (h) => N`${h.command}`
494
+ }, ne = {
496
495
  name: "get_datetime",
497
496
  description: "Get current UTC date / time",
498
497
  args: {},
499
498
  fn: async () => (/* @__PURE__ */ new Date()).toUTCString()
500
- }, ae = {
499
+ }, oe = {
501
500
  name: "exec",
502
501
  description: "Run code/scripts",
503
502
  args: {
@@ -508,17 +507,17 @@ const F = {
508
507
  try {
509
508
  switch (h.type) {
510
509
  case "bash":
511
- return await F.fn({ command: h.code }, r, e);
510
+ return await H.fn({ command: h.code }, r, e);
512
511
  case "node":
513
- return await G.fn({ code: h.code }, r, e);
512
+ return await F.fn({ code: h.code }, r, e);
514
513
  case "python":
515
- return await B.fn({ code: h.code }, r, e);
514
+ return await G.fn({ code: h.code }, r, e);
516
515
  }
517
516
  } catch (t) {
518
517
  return { error: t?.message || t.toString() };
519
518
  }
520
519
  }
521
- }, ce = {
520
+ }, ie = {
522
521
  name: "fetch",
523
522
  description: "Make HTTP request to URL",
524
523
  args: {
@@ -527,25 +526,25 @@ const F = {
527
526
  headers: { type: "object", description: "HTTP headers to send", default: {} },
528
527
  body: { type: "object", description: "HTTP body to send" }
529
528
  },
530
- fn: (h) => new q({ url: h.url, headers: h.headers }).request({ method: h.method || "GET", body: h.body })
531
- }, G = {
529
+ fn: (h) => new $({ url: h.url, headers: h.headers }).request({ method: h.method || "GET", body: h.body })
530
+ }, F = {
532
531
  name: "exec_javascript",
533
532
  description: "Execute commonjs javascript",
534
533
  args: {
535
534
  code: { type: "string", description: "CommonJS javascript", required: !0 }
536
535
  },
537
536
  fn: async (h) => {
538
- const r = P(null), e = await M({ console: r }, h.code, !0).catch((t) => r.output.error.push(t));
537
+ const r = E(null), e = await A({ console: r }, h.code, !0).catch((t) => r.output.error.push(t));
539
538
  return { ...r.output, return: e, stdout: void 0, stderr: void 0 };
540
539
  }
541
- }, B = {
540
+ }, G = {
542
541
  name: "exec_javascript",
543
542
  description: "Execute commonjs javascript",
544
543
  args: {
545
544
  code: { type: "string", description: "CommonJS javascript", required: !0 }
546
545
  },
547
- fn: async (h) => ({ result: W`python -c "${h.code}"` })
548
- }, le = {
546
+ fn: async (h) => ({ result: C`python -c "${h.code}"` })
547
+ }, ae = {
549
548
  name: "read_webpage",
550
549
  description: "Extract clean, structured content from a webpage. Use after web_search to read specific URLs",
551
550
  args: {
@@ -555,7 +554,7 @@ const F = {
555
554
  fn: async (h) => {
556
555
  const r = await fetch(h.url, { headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" } }).then((n) => n.text()).catch((n) => {
557
556
  throw new Error(`Failed to fetch: ${n.message}`);
558
- }), e = C.load(r);
557
+ }), e = D.load(r);
559
558
  e('script, style, nav, footer, header, aside, iframe, noscript, [role="navigation"], [role="banner"], .ad, .ads, .cookie, .popup').remove();
560
559
  const t = {
561
560
  title: e('meta[property="og:title"]').attr("content") || e("title").text() || "",
@@ -572,7 +571,7 @@ const F = {
572
571
  }
573
572
  return o || (o = e("body").text()), o = o.replace(/\s+/g, " ").trim().slice(0, 8e3), { url: h.url, title: t.title.trim(), description: t.description.trim(), content: o, focus: h.focus };
574
573
  }
575
- }, me = {
574
+ }, ce = {
576
575
  name: "web_search",
577
576
  description: "Use duckduckgo (anonymous) to find find relevant online resources. Returns a list of URLs that works great with the `read_webpage` tool",
578
577
  args: {
@@ -584,7 +583,7 @@ const F = {
584
583
  headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", "Accept-Language": "en-US,en;q=0.9" }
585
584
  }).then((s) => s.text());
586
585
  let e, t = /<a .*?href="(.+?)".+?<\/a>/g;
587
- const o = new $();
586
+ const o = new v();
588
587
  for (; (e = t.exec(r)) !== null; ) {
589
588
  let s = /uddg=(.+)&amp?/.exec(decodeURIComponent(e[1]))?.[1];
590
589
  if (s && (s = decodeURIComponent(s)), s && o.add(s), o.size >= (h.length || 5)) break;
@@ -593,20 +592,20 @@ const F = {
593
592
  }
594
593
  };
595
594
  export {
596
- oe as Ai,
597
- z as Anthropic,
598
- J as Audio,
599
- F as CliTool,
600
- ie as DateTimeTool,
601
- ae as ExecTool,
602
- ce as FetchTool,
603
- G as JSTool,
604
- j as LLMProvider,
595
+ se as Ai,
596
+ W as Anthropic,
597
+ I as Audio,
598
+ H as CliTool,
599
+ ne as DateTimeTool,
600
+ oe as ExecTool,
601
+ ie as FetchTool,
602
+ F as JSTool,
603
+ q as LLMProvider,
605
604
  w as OpenAi,
606
- B as PythonTool,
607
- le as ReadWebpageTool,
608
- H as Vision,
609
- me as WebSearchTool,
610
- L as canDiarization
605
+ G as PythonTool,
606
+ ae as ReadWebpageTool,
607
+ J as Vision,
608
+ ce as WebSearchTool,
609
+ R as canDiarization
611
610
  };
612
611
  //# sourceMappingURL=index.mjs.map