@ztimson/ai-utils 0.7.11 → 0.8.0

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/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  <br />
4
4
 
5
5
  <!-- Logo -->
6
- <img src="https://git.zakscode.com/repo-avatars/a90851ca730480ec37a5c0c2c4f1b4609eee5eadf806eaf16c83ac4cb7493aa9" alt="Logo" width="200" height="200">
6
+ <img alt="Logo" width="200" height="200" src="https://git.zakscode.com/repo-avatars/a82d423674763e7a0c1c945bdbb07e249b2bb786d3c9beae76d5b196a10f5c0f">
7
7
 
8
8
  <!-- Title -->
9
9
  ### @ztimson/ai-utils
@@ -53,13 +53,15 @@ A TypeScript library that provides a unified interface for working with multiple
53
53
  - **Provider Abstraction**: Switch between AI providers without changing your code
54
54
 
55
55
  ### Built With
56
- [![Anthropic](https://img.shields.io/badge/Anthropic-191919?style=for-the-badge&logo=anthropic&logoColor=white)](https://anthropic.com/)
57
- [![OpenAI](https://img.shields.io/badge/OpenAI-412991?style=for-the-badge&logo=openai&logoColor=white)](https://openai.com/)
58
- [![Ollama](https://img.shields.io/badge/Ollama-000000?style=for-the-badge&logo=ollama&logoColor=white)](https://ollama.com/)
59
- [![TensorFlow](https://img.shields.io/badge/TensorFlow-FF6F00?style=for-the-badge&logo=tensorflow&logoColor=white)](https://tensorflow.org/)
60
- [![Tesseract](https://img.shields.io/badge/Tesseract-3C8FC7?style=for-the-badge&logo=tesseract&logoColor=white)](https://tesseract-ocr.github.io/)
56
+ [![Anthropic](https://img.shields.io/badge/Anthropic-de7356?style=for-the-badge&logo=anthropic&logoColor=white)](https://anthropic.com/)
57
+ [![llama](https://img.shields.io/badge/llama.cpp-fff?style=for-the-badge&logo=ollama&logoColor=black)](https://github.com/ggml-org/llama.cpp)
58
+ [![OpenAI](https://img.shields.io/badge/OpenAI-000?style=for-the-badge&logo=openai-gym&logoColor=white)](https://openai.com/)
59
+ [![Pyannote](https://img.shields.io/badge/Pyannote-458864?style=for-the-badge&logo=python&logoColor=white)](https://github.com/pyannote)
60
+ [![TensorFlow](https://img.shields.io/badge/TensorFlow-fff?style=for-the-badge&logo=tensorflow&logoColor=ff6f00)](https://tensorflow.org/)
61
+ [![Tesseract](https://img.shields.io/badge/Tesseract-B874B2?style=for-the-badge&logo=hack-the-box&logoColor=white)](https://tesseract-ocr.github.io/)
62
+ [![Transformers.js](https://img.shields.io/badge/Transformers.js-000?style=for-the-badge&logo=hugging-face&logoColor=yellow)](https://huggingface.co/docs/transformers.js/en/index)
61
63
  [![TypeScript](https://img.shields.io/badge/TypeScript-3178C6?style=for-the-badge&logo=typescript&logoColor=white)](https://typescriptlang.org/)
62
- [![Whisper](https://img.shields.io/badge/Whisper-412991?style=for-the-badge&logo=openai&logoColor=white)](https://github.com/ggerganov/whisper.cpp)
64
+ [![Whisper](https://img.shields.io/badge/Whisper.cpp-000?style=for-the-badge&logo=openai-gym&logoColor=white)](https://github.com/ggerganov/whisper.cpp)
63
65
 
64
66
  ## Setup
65
67
 
@@ -88,6 +90,8 @@ A TypeScript library that provides a unified interface for working with multiple
88
90
 
89
91
  #### Prerequisites
90
92
  - [Node.js](https://nodejs.org/en/download)
93
+ - _[Whisper.cpp](https://github.com/ggml-org/whisper.cpp/releases/tag) (ASR)_
94
+ - _[Pyannote](https://github.com/pyannote) (ASR Diarization):_ `pip install pyannote.audio`
91
95
 
92
96
  #### Instructions
93
97
  1. Install the dependencies: `npm i`
package/dist/index.js CHANGED
@@ -1,32 +1,32 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("node:os"),y=require("@ztimson/utils"),N=require("@anthropic-ai/sdk"),R=require("openai"),U=require("url"),T=require("path"),b=require("node:child_process"),z=require("node:fs"),k=require("node:fs/promises"),w=require("node:path"),L=require("tesseract.js"),C=require("cheerio"),P=require("@ztimson/node-utils");var _=typeof document<"u"?document.currentScript:null;function q(p){const r=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(p){for(const e in p)if(e!=="default"){const t=Object.getOwnPropertyDescriptor(p,e);Object.defineProperty(r,e,t.get?t:{enumerable:!0,get:()=>p[e]})}}return r.default=p,Object.freeze(r)}const J=q(j),D=q(C);class x{}class O extends x{constructor(r,e,t){super(),this.ai=r,this.apiToken=e,this.model=t,this.client=new N.Anthropic({apiKey:e})}client;toStandard(r){const e=Date.now(),t=[];for(let m of r)if(typeof m.content=="string")t.push({timestamp:e,...m});else{const n=m.content?.filter(s=>s.type=="text").map(s=>s.text).join(`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const M=require("node:os"),g=require("@ztimson/utils"),C=require("@anthropic-ai/sdk"),J=require("openai"),W=require("url"),q=require("path"),k=require("node:child_process"),D=require("node:fs"),_=require("node:fs/promises"),S=require("node:path"),I=require("tesseract.js"),F=require("cheerio"),v=require("@ztimson/node-utils");var x=typeof document<"u"?document.currentScript:null;function T(h){const r=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(h){for(const e in h)if(e!=="default"){const t=Object.getOwnPropertyDescriptor(h,e);Object.defineProperty(r,e,t.get?t:{enumerable:!0,get:()=>h[e]})}}return r.default=h,Object.freeze(r)}const H=T(M),O=T(S),B=T(F);class E{}class A extends E{constructor(r,e,t){super(),this.ai=r,this.apiToken=e,this.model=t,this.client=new C.Anthropic({apiKey:e})}client;toStandard(r){const e=Date.now(),t=[];for(let c of r)if(typeof c.content=="string")t.push({timestamp:e,...c});else{const o=c.content?.filter(s=>s.type=="text").map(s=>s.text).join(`
2
2
 
3
- `);n&&t.push({timestamp:e,role:m.role,content:n}),m.content.forEach(s=>{if(s.type=="tool_use")t.push({timestamp:e,role:"tool",id:s.id,name:s.name,args:s.input,content:void 0});else if(s.type=="tool_result"){const i=t.findLast(l=>l.id==s.tool_use_id);i&&(i[s.is_error?"error":"content"]=s.content)}})}return t}fromStandard(r){for(let e=0;e<r.length;e++)if(r[e].role=="tool"){const t=r[e];r.splice(e,1,{role:"assistant",content:[{type:"tool_use",id:t.id,name:t.name,input:t.args}]},{role:"user",content:[{type:"tool_result",tool_use_id:t.id,is_error:!!t.error,content:t.error||t.content}]}),e++}return r.map(({timestamp:e,...t})=>t)}ask(r,e={}){const t=new AbortController;return Object.assign(new Promise(async m=>{let n=this.fromStandard([...e.history||[],{role:"user",content:r,timestamp:Date.now()}]);const s=e.tools||this.ai.options.llm?.tools||[],i={model:e.model||this.model,max_tokens:e.max_tokens||this.ai.options.llm?.max_tokens||4096,system:e.system||this.ai.options.llm?.system||"",temperature:e.temperature||this.ai.options.llm?.temperature||.7,tools:s.map(a=>({name:a.name,description:a.description,input_schema:{type:"object",properties:a.args?y.objectMap(a.args,(o,c)=>({...c,required:void 0})):{},required:a.args?Object.entries(a.args).filter(o=>o[1].required).map(o=>o[0]):[]},fn:void 0})),messages:n,stream:!!e.stream};let l,d=!0;do{if(l=await this.client.messages.create(i).catch(o=>{throw o.message+=`
3
+ `);o&&t.push({timestamp:e,role:c.role,content:o}),c.content.forEach(s=>{if(s.type=="tool_use")t.push({timestamp:e,role:"tool",id:s.id,name:s.name,args:s.input,content:void 0});else if(s.type=="tool_result"){const l=t.findLast(n=>n.id==s.tool_use_id);l&&(l[s.is_error?"error":"content"]=s.content)}})}return t}fromStandard(r){for(let e=0;e<r.length;e++)if(r[e].role=="tool"){const t=r[e];r.splice(e,1,{role:"assistant",content:[{type:"tool_use",id:t.id,name:t.name,input:t.args}]},{role:"user",content:[{type:"tool_result",tool_use_id:t.id,is_error:!!t.error,content:t.error||t.content}]}),e++}return r.map(({timestamp:e,...t})=>t)}ask(r,e={}){const t=new AbortController;return Object.assign(new Promise(async c=>{let o=this.fromStandard([...e.history||[],{role:"user",content:r,timestamp:Date.now()}]);const s=e.tools||this.ai.options.llm?.tools||[],l={model:e.model||this.model,max_tokens:e.max_tokens||this.ai.options.llm?.max_tokens||4096,system:e.system||this.ai.options.llm?.system||"",temperature:e.temperature||this.ai.options.llm?.temperature||.7,tools:s.map(d=>({name:d.name,description:d.description,input_schema:{type:"object",properties:d.args?g.objectMap(d.args,(i,m)=>({...m,required:void 0})):{},required:d.args?Object.entries(d.args).filter(i=>i[1].required).map(i=>i[0]):[]},fn:void 0})),messages:o,stream:!!e.stream};let n,a=!0;do{if(n=await this.client.messages.create(l).catch(i=>{throw i.message+=`
4
4
 
5
5
  Messages:
6
- ${JSON.stringify(n,null,2)}`,o}),e.stream){d?d=!1:e.stream({text:`
6
+ ${JSON.stringify(o,null,2)}`,i}),e.stream){a?a=!1:e.stream({text:`
7
7
 
8
- `}),l.content=[];for await(const o of l){if(t.signal.aborted)break;if(o.type==="content_block_start")o.content_block.type==="text"?l.content.push({type:"text",text:""}):o.content_block.type==="tool_use"&&l.content.push({type:"tool_use",id:o.content_block.id,name:o.content_block.name,input:""});else if(o.type==="content_block_delta")if(o.delta.type==="text_delta"){const c=o.delta.text;l.content.at(-1).text+=c,e.stream({text:c})}else o.delta.type==="input_json_delta"&&(l.content.at(-1).input+=o.delta.partial_json);else if(o.type==="content_block_stop"){const c=l.content.at(-1);c.input!=null&&(c.input=c.input?y.JSONAttemptParse(c.input,{}):{})}else if(o.type==="message_stop")break}}const a=l.content.filter(o=>o.type==="tool_use");if(a.length&&!t.signal.aborted){n.push({role:"assistant",content:l.content});const o=await Promise.all(a.map(async c=>{const h=s.find(y.findByProp("name",c.name));if(e.stream&&e.stream({tool:c.name}),!h)return{tool_use_id:c.id,is_error:!0,content:"Tool not found"};try{const u=await h.fn(c.input,e?.stream,this.ai);return{type:"tool_result",tool_use_id:c.id,content:y.JSONSanitize(u)}}catch(u){return{type:"tool_result",tool_use_id:c.id,is_error:!0,content:u?.message||u?.toString()||"Unknown"}}}));n.push({role:"user",content:o}),i.messages=n}}while(!t.signal.aborted&&l.content.some(a=>a.type==="tool_use"));n.push({role:"assistant",content:l.content.filter(a=>a.type=="text").map(a=>a.text).join(`
8
+ `}),n.content=[];for await(const i of n){if(t.signal.aborted)break;if(i.type==="content_block_start")i.content_block.type==="text"?n.content.push({type:"text",text:""}):i.content_block.type==="tool_use"&&n.content.push({type:"tool_use",id:i.content_block.id,name:i.content_block.name,input:""});else if(i.type==="content_block_delta")if(i.delta.type==="text_delta"){const m=i.delta.text;n.content.at(-1).text+=m,e.stream({text:m})}else i.delta.type==="input_json_delta"&&(n.content.at(-1).input+=i.delta.partial_json);else if(i.type==="content_block_stop"){const m=n.content.at(-1);m.input!=null&&(m.input=m.input?g.JSONAttemptParse(m.input,{}):{})}else if(i.type==="message_stop")break}}const d=n.content.filter(i=>i.type==="tool_use");if(d.length&&!t.signal.aborted){o.push({role:"assistant",content:n.content});const i=await Promise.all(d.map(async m=>{const p=s.find(g.findByProp("name",m.name));if(e.stream&&e.stream({tool:m.name}),!p)return{tool_use_id:m.id,is_error:!0,content:"Tool not found"};try{const u=await p.fn(m.input,e?.stream,this.ai);return{type:"tool_result",tool_use_id:m.id,content:g.JSONSanitize(u)}}catch(u){return{type:"tool_result",tool_use_id:m.id,is_error:!0,content:u?.message||u?.toString()||"Unknown"}}}));o.push({role:"user",content:i}),l.messages=o}}while(!t.signal.aborted&&n.content.some(d=>d.type==="tool_use"));o.push({role:"assistant",content:n.content.filter(d=>d.type=="text").map(d=>d.text).join(`
9
9
 
10
- `)}),n=this.toStandard(n),e.stream&&e.stream({done:!0}),e.history&&e.history.splice(0,e.history.length,...n),m(n.at(-1)?.content)}),{abort:()=>t.abort()})}}class S extends x{constructor(r,e,t,m){super(),this.ai=r,this.host=e,this.token=t,this.model=m,this.client=new R.OpenAI(y.clean({baseURL:e,apiKey:t}))}client;toStandard(r){for(let e=0;e<r.length;e++){const t=r[e];if(t.role==="assistant"&&t.tool_calls){const m=t.tool_calls.map(n=>({role:"tool",id:n.id,name:n.function.name,args:y.JSONAttemptParse(n.function.arguments,{}),timestamp:t.timestamp}));r.splice(e,1,...m),e+=m.length-1}else if(t.role==="tool"&&t.content){const m=r.find(n=>t.tool_call_id==n.id);m&&(t.content.includes('"error":')?m.error=t.content:m.content=t.content),r.splice(e,1),e--}r[e]?.timestamp||(r[e].timestamp=Date.now())}return r}fromStandard(r){return r.reduce((e,t)=>{if(t.role==="tool")e.push({role:"assistant",content:null,tool_calls:[{id:t.id,type:"function",function:{name:t.name,arguments:JSON.stringify(t.args)}}],refusal:null,annotations:[]},{role:"tool",tool_call_id:t.id,content:t.error||t.content});else{const{timestamp:m,...n}=t;e.push(n)}return e},[])}ask(r,e={}){const t=new AbortController;return Object.assign(new Promise(async(m,n)=>{e.system&&e.history?.[0]?.role!="system"&&e.history?.splice(0,0,{role:"system",content:e.system,timestamp:Date.now()});let s=this.fromStandard([...e.history||[],{role:"user",content:r,timestamp:Date.now()}]);const i=e.tools||this.ai.options.llm?.tools||[],l={model:e.model||this.model,messages:s,stream:!!e.stream,max_tokens:e.max_tokens||this.ai.options.llm?.max_tokens||4096,temperature:e.temperature||this.ai.options.llm?.temperature||.7,tools:i.map(o=>({type:"function",function:{name:o.name,description:o.description,parameters:{type:"object",properties:o.args?y.objectMap(o.args,(c,h)=>({...h,required:void 0})):{},required:o.args?Object.entries(o.args).filter(c=>c[1].required).map(c=>c[0]):[]}}}))};let d,a=!0;do{if(d=await this.client.chat.completions.create(l).catch(c=>{throw c.message+=`
10
+ `)}),o=this.toStandard(o),e.stream&&e.stream({done:!0}),e.history&&e.history.splice(0,e.history.length,...o),c(o.at(-1)?.content)}),{abort:()=>t.abort()})}}class j extends E{constructor(r,e,t,c){super(),this.ai=r,this.host=e,this.token=t,this.model=c,this.client=new J.OpenAI(g.clean({baseURL:e,apiKey:t}))}client;toStandard(r){for(let e=0;e<r.length;e++){const t=r[e];if(t.role==="assistant"&&t.tool_calls){const c=t.tool_calls.map(o=>({role:"tool",id:o.id,name:o.function.name,args:g.JSONAttemptParse(o.function.arguments,{}),timestamp:t.timestamp}));r.splice(e,1,...c),e+=c.length-1}else if(t.role==="tool"&&t.content){const c=r.find(o=>t.tool_call_id==o.id);c&&(t.content.includes('"error":')?c.error=t.content:c.content=t.content),r.splice(e,1),e--}r[e]?.timestamp||(r[e].timestamp=Date.now())}return r}fromStandard(r){return r.reduce((e,t)=>{if(t.role==="tool")e.push({role:"assistant",content:null,tool_calls:[{id:t.id,type:"function",function:{name:t.name,arguments:JSON.stringify(t.args)}}],refusal:null,annotations:[]},{role:"tool",tool_call_id:t.id,content:t.error||t.content});else{const{timestamp:c,...o}=t;e.push(o)}return e},[])}ask(r,e={}){const t=new AbortController;return Object.assign(new Promise(async(c,o)=>{e.system&&e.history?.[0]?.role!="system"&&e.history?.splice(0,0,{role:"system",content:e.system,timestamp:Date.now()});let s=this.fromStandard([...e.history||[],{role:"user",content:r,timestamp:Date.now()}]);const l=e.tools||this.ai.options.llm?.tools||[],n={model:e.model||this.model,messages:s,stream:!!e.stream,max_tokens:e.max_tokens||this.ai.options.llm?.max_tokens||4096,temperature:e.temperature||this.ai.options.llm?.temperature||.7,tools:l.map(i=>({type:"function",function:{name:i.name,description:i.description,parameters:{type:"object",properties:i.args?g.objectMap(i.args,(m,p)=>({...p,required:void 0})):{},required:i.args?Object.entries(i.args).filter(m=>m[1].required).map(m=>m[0]):[]}}}))};let a,d=!0;do{if(a=await this.client.chat.completions.create(n).catch(m=>{throw m.message+=`
11
11
 
12
12
  Messages:
13
- ${JSON.stringify(s,null,2)}`,c}),e.stream){a?a=!1:e.stream({text:`
13
+ ${JSON.stringify(s,null,2)}`,m}),e.stream){d?d=!1:e.stream({text:`
14
14
 
15
- `}),d.choices=[{message:{content:"",tool_calls:[]}}];for await(const c of d){if(t.signal.aborted)break;c.choices[0].delta.content&&(d.choices[0].message.content+=c.choices[0].delta.content,e.stream({text:c.choices[0].delta.content})),c.choices[0].delta.tool_calls&&(d.choices[0].message.tool_calls=c.choices[0].delta.tool_calls)}}const o=d.choices[0].message.tool_calls||[];if(o.length&&!t.signal.aborted){s.push(d.choices[0].message);const c=await Promise.all(o.map(async h=>{const u=i?.find(y.findByProp("name",h.function.name));if(e.stream&&e.stream({tool:h.function.name}),!u)return{role:"tool",tool_call_id:h.id,content:'{"error": "Tool not found"}'};try{const f=y.JSONAttemptParse(h.function.arguments,{}),g=await u.fn(f,e.stream,this.ai);return{role:"tool",tool_call_id:h.id,content:y.JSONSanitize(g)}}catch(f){return{role:"tool",tool_call_id:h.id,content:y.JSONSanitize({error:f?.message||f?.toString()||"Unknown"})}}}));s.push(...c),l.messages=s}}while(!t.signal.aborted&&d.choices?.[0]?.message?.tool_calls?.length);s.push({role:"assistant",content:d.choices[0].message.content||""}),s=this.toStandard(s),e.stream&&e.stream({done:!0}),e.history&&e.history.splice(0,e.history.length,...s),m(s.at(-1)?.content)}),{abort:()=>t.abort()})}}class W{constructor(r){this.ai=r,r.options.llm?.models&&Object.entries(r.options.llm.models).forEach(([e,t])=>{this.defaultModel||(this.defaultModel=e),t.proto=="anthropic"?this.models[e]=new O(this.ai,t.token,e):t.proto=="ollama"?this.models[e]=new S(this.ai,t.host,"not-needed",e):t.proto=="openai"&&(this.models[e]=new S(this.ai,t.host||null,t.token,e))})}defaultModel;models={};ask(r,e={}){const t=e.model||this.defaultModel;if(!this.models[t])throw new Error(`Model does not exist: ${t}`);let m=()=>{};return Object.assign(new Promise(async n=>{if(e.history||(e.history=[]),e.memory){e.system=(e.system||"")+`
15
+ `}),a.choices=[{message:{content:"",tool_calls:[]}}];for await(const m of a){if(t.signal.aborted)break;m.choices[0].delta.content&&(a.choices[0].message.content+=m.choices[0].delta.content,e.stream({text:m.choices[0].delta.content})),m.choices[0].delta.tool_calls&&(a.choices[0].message.tool_calls=m.choices[0].delta.tool_calls)}}const i=a.choices[0].message.tool_calls||[];if(i.length&&!t.signal.aborted){s.push(a.choices[0].message);const m=await Promise.all(i.map(async p=>{const u=l?.find(g.findByProp("name",p.function.name));if(e.stream&&e.stream({tool:p.function.name}),!u)return{role:"tool",tool_call_id:p.id,content:'{"error": "Tool not found"}'};try{const f=g.JSONAttemptParse(p.function.arguments,{}),y=await u.fn(f,e.stream,this.ai);return{role:"tool",tool_call_id:p.id,content:g.JSONSanitize(y)}}catch(f){return{role:"tool",tool_call_id:p.id,content:g.JSONSanitize({error:f?.message||f?.toString()||"Unknown"})}}}));s.push(...m),n.messages=s}}while(!t.signal.aborted&&a.choices?.[0]?.message?.tool_calls?.length);s.push({role:"assistant",content:a.choices[0].message.content||""}),s=this.toStandard(s),e.stream&&e.stream({done:!0}),e.history&&e.history.splice(0,e.history.length,...s),c(s.at(-1)?.content)}),{abort:()=>t.abort()})}}class G{constructor(r){this.ai=r,r.options.llm?.models&&Object.entries(r.options.llm.models).forEach(([e,t])=>{this.defaultModel||(this.defaultModel=e),t.proto=="anthropic"?this.models[e]=new A(this.ai,t.token,e):t.proto=="ollama"?this.models[e]=new j(this.ai,t.host,"not-needed",e):t.proto=="openai"&&(this.models[e]=new j(this.ai,t.host||null,t.token,e))})}defaultModel;models={};ask(r,e={}){const t=e.model||this.defaultModel;if(!this.models[t])throw new Error(`Model does not exist: ${t}`);let c=()=>{};return Object.assign(new Promise(async o=>{if(e.history||(e.history=[]),e.memory){e.system=(e.system||"")+`
16
16
  You have passive persistent memory never make any mention of your memory capabilities and what you can/cannot remember
17
- `;const i=async(d,a,o=50)=>{const[c,h]=await Promise.all([a?this.embedding(a):Promise.resolve(null),d?this.embedding(d):Promise.resolve(null)]);return(e.memory||[]).map(u=>({...u,score:c?this.cosineSimilarity(u.embeddings[0],c[0].embedding):1})).filter(u=>u.score>=.8).map(u=>({...u,score:h?this.cosineSimilarity(u.embeddings[1],h[0].embedding):u.score})).filter(u=>u.score>=.2).toSorted((u,f)=>u.score-f.score).slice(0,o)},l=await i(r);l.length&&e.history.push({role:"assistant",content:`Things I remembered:
18
- `+l.map(d=>`${d.owner}: ${d.fact}`).join(`
19
- `)}),e.tools=[...e.tools||[],{name:"read_memory",description:"Check your long-term memory for more information",args:{subject:{type:"string",description:"Find information by a subject topic, can be used with or without query argument"},query:{type:"string",description:"Search memory based on a query, can be used with or without subject argument"},limit:{type:"number",description:"Result limit, default 5"}},fn:d=>{if(!d.subject&&!d.query)throw new Error("Either a subject or query argument is required");return i(d.query,d.subject,d.limit||5)}}]}const s=await this.models[t].ask(r,e);if(e.memory){const i=e.history?.findIndex(l=>l.role=="assistant"&&l.content.startsWith("Things I remembered:"));i!=null&&i>=0&&e.history?.splice(i,1)}if(e.compress||e.memory){let i=null;if(e.compress)i=await this.ai.language.compressHistory(e.history,e.compress.max,e.compress.min,e),e.history.splice(0,e.history.length,...i.history);else{const l=e.history?.findLastIndex(d=>d.role=="user")??-1;i=await this.ai.language.compressHistory(l!=-1?e.history.slice(l):e.history,0,0,e)}if(e.memory){const l=e.memory.filter(d=>!i.memory.some(a=>this.cosineSimilarity(d.embeddings[1],a.embeddings[1])>.8)).concat(i.memory);e.memory.splice(0,e.memory.length,...l)}}return n(s)}),{abort:m})}async compressHistory(r,e,t,m){if(this.estimateTokens(r)<e)return{history:r,memory:[]};let n=0,s=0;for(let u of r.toReversed())if(s+=this.estimateTokens(u.content),s<t)n++;else break;if(r.length<=n)return{history:r,memory:[]};const i=r[0].role=="system"?r[0]:null,l=n==0?[]:r.slice(-n),d=(n==0?r:r.slice(0,-n)).filter(u=>u.role==="assistant"||u.role==="user"),a=await this.json(d.map(u=>`${u.role}: ${u.content}`).join(`
20
-
21
- `),"{summary: string, facts: [[subject, fact]]}",{system:"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.",model:m?.model,temperature:m?.temperature||.3}),o=new Date,c=await Promise.all((a?.facts||[])?.map(async([u,f])=>{const g=await Promise.all([this.embedding(u),this.embedding(`${u}: ${f}`)]);return{owner:u,fact:f,embeddings:[g[0][0].embedding,g[1][0].embedding],timestamp:o}})),h=[{role:"assistant",content:`Conversation Summary: ${a?.summary}`,timestamp:Date.now()},...l];return i&&h.splice(0,0,i),{history:h,memory:c}}cosineSimilarity(r,e){if(r.length!==e.length)throw new Error("Vectors must be same length");let t=0,m=0,n=0;for(let i=0;i<r.length;i++)t+=r[i]*e[i],m+=r[i]*r[i],n+=e[i]*e[i];const s=Math.sqrt(m)*Math.sqrt(n);return s===0?0:t/s}chunk(r,e=500,t=50){const m=(l,d="")=>l?Object.entries(l).flatMap(([a,o])=>{const c=d?`${d}${isNaN(+a)?`.${a}`:`[${a}]`}`:a;return typeof o=="object"&&!Array.isArray(o)?m(o,c):`${c}: ${Array.isArray(o)?o.join(", "):o}`}):[],s=(typeof r=="object"?m(r):r.split(`
22
- `)).flatMap(l=>[...l.split(/\s+/).filter(Boolean),`
23
- `]),i=[];for(let l=0;l<s.length;){let d="",a=l;for(;a<s.length;){const c=d+(d?" ":"")+s[a];if(this.estimateTokens(c.replace(/\s*\n\s*/g,`
24
- `))>e&&d)break;d=c,a++}const o=d.replace(/\s*\n\s*/g,`
25
- `).trim();o&&i.push(o),l=Math.max(a-t,a===l?l+1:a)}return i}embedding(r,e={}){let{maxTokens:t=500,overlapTokens:m=50}=e,n=!1;const s=()=>{n=!0},i=d=>new Promise((a,o)=>{if(n)return o(new Error("Aborted"));const c=[T.join(T.dirname(U.fileURLToPath(typeof document>"u"?require("url").pathToFileURL(__filename).href:_&&_.tagName.toUpperCase()==="SCRIPT"&&_.src||new URL("index.js",document.baseURI).href)),"embedder.js"),this.ai.options.path,this.ai.options?.embedder||"bge-small-en-v1.5"],h=b.spawn("node",c,{stdio:["pipe","pipe","ignore"]});h.stdin.write(d),h.stdin.end();let u="";h.stdout.on("data",f=>u+=f.toString()),h.on("close",f=>{if(n)return o(new Error("Aborted"));if(f===0)try{const g=JSON.parse(u);a(g.embedding)}catch{o(new Error("Failed to parse embedding output"))}else o(new Error(`Embedder process exited with code ${f}`))}),h.on("error",o)}),l=(async()=>{const d=this.chunk(r,t,m),a=[];for(let o=0;o<d.length&&!n;o++){const c=d[o],h=await i(c);a.push({index:o,embedding:h,text:c,tokens:this.estimateTokens(c)})}return a})();return Object.assign(l,{abort:s})}estimateTokens(r){const e=JSON.stringify(r);return Math.ceil(e.length/4*1.2)}fuzzyMatch(r,...e){if(e.length<2)throw new Error("Requires at least 2 strings to compare");const t=(s,i=10)=>s.toLowerCase().split("").map((l,d)=>l.charCodeAt(0)*(d+1)%i/i).slice(0,i),m=t(r),n=e.map(s=>t(s)).map(s=>this.cosineSimilarity(m,s));return{avg:n.reduce((s,i)=>s+i,0)/n.length,max:Math.max(...n),similarities:n}}async json(r,e,t){let m=await this.ask(r,{...t,system:(t?.system?`${t.system}
17
+ `;const l=async(a,d,i=50)=>{const[m,p]=await Promise.all([d?this.embedding(d):Promise.resolve(null),a?this.embedding(a):Promise.resolve(null)]);return(e.memory||[]).map(u=>({...u,score:m?this.cosineSimilarity(u.embeddings[0],m[0].embedding):1})).filter(u=>u.score>=.8).map(u=>({...u,score:p?this.cosineSimilarity(u.embeddings[1],p[0].embedding):u.score})).filter(u=>u.score>=.2).toSorted((u,f)=>u.score-f.score).slice(0,i)},n=await l(r);n.length&&e.history.push({role:"assistant",content:`Things I remembered:
18
+ `+n.map(a=>`${a.owner}: ${a.fact}`).join(`
19
+ `)}),e.tools=[...e.tools||[],{name:"read_memory",description:"Check your long-term memory for more information",args:{subject:{type:"string",description:"Find information by a subject topic, can be used with or without query argument"},query:{type:"string",description:"Search memory based on a query, can be used with or without subject argument"},limit:{type:"number",description:"Result limit, default 5"}},fn:a=>{if(!a.subject&&!a.query)throw new Error("Either a subject or query argument is required");return l(a.query,a.subject,a.limit||5)}}]}const s=await this.models[t].ask(r,e);if(e.memory){const l=e.history?.findIndex(n=>n.role=="assistant"&&n.content.startsWith("Things I remembered:"));l!=null&&l>=0&&e.history?.splice(l,1)}if(e.compress||e.memory){let l=null;if(e.compress)l=await this.ai.language.compressHistory(e.history,e.compress.max,e.compress.min,e),e.history.splice(0,e.history.length,...l.history);else{const n=e.history?.findLastIndex(a=>a.role=="user")??-1;l=await this.ai.language.compressHistory(n!=-1?e.history.slice(n):e.history,0,0,e)}if(e.memory){const n=e.memory.filter(a=>!l.memory.some(d=>this.cosineSimilarity(a.embeddings[1],d.embeddings[1])>.8)).concat(l.memory);e.memory.splice(0,e.memory.length,...n)}}return o(s)}),{abort:c})}async compressHistory(r,e,t,c){if(this.estimateTokens(r)<e)return{history:r,memory:[]};let o=0,s=0;for(let u of r.toReversed())if(s+=this.estimateTokens(u.content),s<t)o++;else break;if(r.length<=o)return{history:r,memory:[]};const l=r[0].role=="system"?r[0]:null,n=o==0?[]:r.slice(-o),a=(o==0?r:r.slice(0,-o)).filter(u=>u.role==="assistant"||u.role==="user"),d=await this.json(a.map(u=>`${u.role}: ${u.content}`).join(`
20
+
21
+ `),"{summary: string, facts: [[subject, fact]]}",{system:"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.",model:c?.model,temperature:c?.temperature||.3}),i=new Date,m=await Promise.all((d?.facts||[])?.map(async([u,f])=>{const y=await Promise.all([this.embedding(u),this.embedding(`${u}: ${f}`)]);return{owner:u,fact:f,embeddings:[y[0][0].embedding,y[1][0].embedding],timestamp:i}})),p=[{role:"assistant",content:`Conversation Summary: ${d?.summary}`,timestamp:Date.now()},...n];return l&&p.splice(0,0,l),{history:p,memory:m}}cosineSimilarity(r,e){if(r.length!==e.length)throw new Error("Vectors must be same length");let t=0,c=0,o=0;for(let l=0;l<r.length;l++)t+=r[l]*e[l],c+=r[l]*r[l],o+=e[l]*e[l];const s=Math.sqrt(c)*Math.sqrt(o);return s===0?0:t/s}chunk(r,e=500,t=50){const c=(n,a="")=>n?Object.entries(n).flatMap(([d,i])=>{const m=a?`${a}${isNaN(+d)?`.${d}`:`[${d}]`}`:d;return typeof i=="object"&&!Array.isArray(i)?c(i,m):`${m}: ${Array.isArray(i)?i.join(", "):i}`}):[],s=(typeof r=="object"?c(r):r.split(`
22
+ `)).flatMap(n=>[...n.split(/\s+/).filter(Boolean),`
23
+ `]),l=[];for(let n=0;n<s.length;){let a="",d=n;for(;d<s.length;){const m=a+(a?" ":"")+s[d];if(this.estimateTokens(m.replace(/\s*\n\s*/g,`
24
+ `))>e&&a)break;a=m,d++}const i=a.replace(/\s*\n\s*/g,`
25
+ `).trim();i&&l.push(i),n=Math.max(d-t,d===n?n+1:d)}return l}embedding(r,e={}){let{maxTokens:t=500,overlapTokens:c=50}=e,o=!1;const s=()=>{o=!0},l=a=>new Promise((d,i)=>{if(o)return i(new Error("Aborted"));const m=[q.join(q.dirname(W.fileURLToPath(typeof document>"u"?require("url").pathToFileURL(__filename).href:x&&x.tagName.toUpperCase()==="SCRIPT"&&x.src||new URL("index.js",document.baseURI).href)),"embedder.js"),this.ai.options.path,this.ai.options?.embedder||"bge-small-en-v1.5"],p=k.spawn("node",m,{stdio:["pipe","pipe","ignore"]});p.stdin.write(a),p.stdin.end();let u="";p.stdout.on("data",f=>u+=f.toString()),p.on("close",f=>{if(o)return i(new Error("Aborted"));if(f===0)try{const y=JSON.parse(u);d(y.embedding)}catch{i(new Error("Failed to parse embedding output"))}else i(new Error(`Embedder process exited with code ${f}`))}),p.on("error",i)}),n=(async()=>{const a=this.chunk(r,t,c),d=[];for(let i=0;i<a.length&&!o;i++){const m=a[i],p=await l(m);d.push({index:i,embedding:p,text:m,tokens:this.estimateTokens(m)})}return d})();return Object.assign(n,{abort:s})}estimateTokens(r){const e=JSON.stringify(r);return Math.ceil(e.length/4*1.2)}fuzzyMatch(r,...e){if(e.length<2)throw new Error("Requires at least 2 strings to compare");const t=(s,l=10)=>s.toLowerCase().split("").map((n,a)=>n.charCodeAt(0)*(a+1)%l/l).slice(0,l),c=t(r),o=e.map(s=>t(s)).map(s=>this.cosineSimilarity(c,s));return{avg:o.reduce((s,l)=>s+l,0)/o.length,max:Math.max(...o),similarities:o}}async json(r,e,t){let c=await this.ask(r,{...t,system:(t?.system?`${t.system}
26
26
  `:"")+`Only respond using a JSON code block matching this schema:
27
27
  \`\`\`json
28
28
  ${e}
29
- \`\`\``});if(!m)return{};const n=/```(?:.+)?\s*([\s\S]*?)```/.exec(m),s=n?n[1].trim():m;return y.JSONAttemptParse(s,{})}summarize(r,e,t){return this.ask(r,{system:`Generate a brief summary <= ${e} tokens. Output nothing else`,temperature:.3,...t})}}class E{constructor(r){this.ai=r,r.options.whisper&&(this.whisperModel=r.options.asr||"ggml-base.en.bin",this.downloadAsrModel()),this.pyannote=`
29
+ \`\`\``});if(!c)return{};const o=/```(?:.+)?\s*([\s\S]*?)```/.exec(c),s=o?o[1].trim():c;return g.JSONAttemptParse(s,{})}summarize(r,e,t){return this.ask(r,{system:`Generate a brief summary <= ${e} tokens. Output nothing else`,temperature:.3,...t})}}class ${constructor(r){this.ai=r,r.options.whisper&&(this.whisperModel=r.options.asr||"ggml-base.en.bin",this.downloadAsrModel()),this.pyannote=`
30
30
  import sys
31
31
  import json
32
32
  import os
@@ -41,7 +41,10 @@ for turn, speaker in output.speaker_diarization:
41
41
  segments.append({"start": turn.start, "end": turn.end, "speaker": speaker})
42
42
 
43
43
  print(json.dumps(segments))
44
- `}downloads={};pyannote;whisperModel;runAsr(r,e={}){let t;const m=new Promise((n,s)=>{this.downloadAsrModel(e.model).then(i=>{let l="";const d=[e.diarization?"-owts":"-nt","-m",i,"-f",r];t=b.spawn(this.ai.options.whisper,d,{stdio:["ignore","pipe","ignore"]}),t.on("error",a=>s(a)),t.stdout.on("data",a=>l+=a.toString()),t.on("close",a=>{if(a===0)if(e.diarization)try{n(JSON.parse(l))}catch{s(new Error("Failed to parse whisper JSON"))}else n(l.trim()||null);else s(new Error(`Exit code ${a}`))})})});return Object.assign(m,{abort:()=>t?.kill("SIGTERM")})}runDiarization(r){let e=!1,t=()=>{e=!0};const m=s=>new Promise(i=>{const l=b.spawn(s,["-c","import pyannote.audio"]);l.on("close",d=>i(d===0)),l.on("error",()=>i(!1))}),n=Promise.all([m("python"),m("python3")]).then((async([s,i])=>{if(e)return;if(!s&&!i)throw new Error("Pyannote is not installed: pip install pyannote.audio");const l=i?"python3":"python";return new Promise((d,a)=>{if(e)return;let o="";const c=b.spawn(l,["-c",this.pyannote,r]);c.stdout.on("data",h=>o+=h.toString()),c.stderr.on("data",h=>console.error(h.toString())),c.on("close",h=>{if(h===0)try{d(JSON.parse(o))}catch{a(new Error("Failed to parse diarization output"))}else a(new Error(`Python process exited with code ${h}`))}),c.on("error",a),t=()=>c.kill("SIGTERM")})}));return Object.assign(n,{abort:t})}combineSpeakerTranscript(r,e){const t=new Map;let m=0;e.forEach(l=>{t.has(l.speaker)||t.set(l.speaker,++m)});const n=[];let s=-1,i="";return r.transcription.forEach(l=>{const d=l.offsets.from/1e3,a=e.find(c=>d>=c.start&&d<=c.end),o=a?t.get(a.speaker):1;o!==s?(i&&n.push(`[Speaker ${s}]: ${i.trim()}`),s=o,i=l.text):i+=" "+l.text}),i&&n.push(`[Speaker ${s}]: ${i.trim()}`),n.join(`
45
- `)}asr(r,e={}){if(!this.ai.options.whisper)throw new Error("Whisper not configured");const t=w.join(z.mkdtempSync(w.join(j.tmpdir(),"audio-")),"converted.wav");b.execSync(`ffmpeg -i "${r}" -ar 16000 -ac 1 -f wav "${t}"`,{stdio:"ignore"});const m=()=>k.rm(w.dirname(t),{recursive:!0,force:!0}).catch(()=>{}),n=this.runAsr(t,{model:e.model,diarization:!!e.diarization}),s=e.diarization?this.runDiarization(t):Promise.resolve(null);let i=!1,l=()=>{i=!0,n.abort(),s?.abort?.(),m()};const d=Promise.all([n,s]).then(async([a,o])=>{if(i||!e.diarization)return a;if(a=this.combineSpeakerTranscript(a,o),!i&&e.diarization==="id"){if(!this.ai.language.defaultModel)throw new Error("Configure an LLM for advanced ASR speaker detection");let c=this.ai.language.chunk(a,500,0);c.length>4&&(c=[...c.slice(0,3),c.at(-1)]);const h=await this.ai.language.json(c.join(`
46
- `),'{1: "Detected Name", 2: "Second Name"}',{system:"Use the following transcript to identify speakers. Only identify speakers you are positive about, dont mention speakers you are unsure about in your response",temperature:.1});Object.entries(h).forEach(([u,f])=>a=a.replaceAll(`[Speaker ${u}]`,`[${f}]`))}return a}).finally(()=>m());return Object.assign(d,{abort:l})}async downloadAsrModel(r=this.whisperModel){if(!this.ai.options.whisper)throw new Error("Whisper not configured");r.endsWith(".bin")||(r+=".bin");const e=w.join(this.ai.options.path,r);return await k.stat(e).then(()=>!0).catch(()=>!1)?e:this.downloads[r]?this.downloads[r]:(this.downloads[r]=fetch(`https://huggingface.co/ggerganov/whisper.cpp/resolve/main/${r}`).then(t=>t.arrayBuffer()).then(t=>Buffer.from(t)).then(async t=>(await k.writeFile(e,t),delete this.downloads[r],e)),this.downloads[r])}}class M{constructor(r){this.ai=r}ocr(r){let e;const t=new Promise(async m=>{e=await L.createWorker(this.ai.options.ocr||"eng",2,{cachePath:this.ai.options.path});const{data:n}=await e.recognize(r);await e.terminate(),m(n.text.trim()||null)});return Object.assign(t,{abort:()=>e?.terminate()})}}class I{constructor(r){this.options=r,r.path||(r.path=J.tmpdir()),process.env.TRANSFORMERS_CACHE=r.path,this.audio=new E(this),this.language=new W(this),this.vision=new M(this)}audio;language;vision}const $={name:"cli",description:"Use the command line interface, returns any output",args:{command:{type:"string",description:"Command to run",required:!0}},fn:p=>P.$`${p.command}`},F={name:"get_datetime",description:"Get current UTC date / time",args:{},fn:async()=>new Date().toUTCString()},H={name:"exec",description:"Run code/scripts",args:{language:{type:"string",description:"Execution language",enum:["cli","node","python"],required:!0},code:{type:"string",description:"Code to execute",required:!0}},fn:async(p,r,e)=>{try{switch(p.type){case"bash":return await $.fn({command:p.code},r,e);case"node":return await A.fn({code:p.code},r,e);case"python":return await v.fn({code:p.code},r,e)}}catch(t){return{error:t?.message||t.toString()}}}},B={name:"fetch",description:"Make HTTP request to URL",args:{url:{type:"string",description:"URL to fetch",required:!0},method:{type:"string",description:"HTTP method to use",enum:["GET","POST","PUT","DELETE"],default:"GET"},headers:{type:"object",description:"HTTP headers to send",default:{}},body:{type:"object",description:"HTTP body to send"}},fn:p=>new y.Http({url:p.url,headers:p.headers}).request({method:p.method||"GET",body:p.body})},A={name:"exec_javascript",description:"Execute commonjs javascript",args:{code:{type:"string",description:"CommonJS javascript",required:!0}},fn:async p=>{const r=y.consoleInterceptor(null),e=await y.fn({console:r},p.code,!0).catch(t=>r.output.error.push(t));return{...r.output,return:e,stdout:void 0,stderr:void 0}}},v={name:"exec_javascript",description:"Execute commonjs javascript",args:{code:{type:"string",description:"CommonJS javascript",required:!0}},fn:async p=>({result:P.$Sync`python -c "${p.code}"`})},G={name:"read_webpage",description:"Extract clean, structured content from a webpage. Use after web_search to read specific URLs",args:{url:{type:"string",description:"URL to extract content from",required:!0},focus:{type:"string",description:'Optional: What aspect to focus on (e.g., "pricing", "features", "contact info")'}},fn:async p=>{const r=await fetch(p.url,{headers:{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}}).then(s=>s.text()).catch(s=>{throw new Error(`Failed to fetch: ${s.message}`)}),e=D.load(r);e('script, style, nav, footer, header, aside, iframe, noscript, [role="navigation"], [role="banner"], .ad, .ads, .cookie, .popup').remove();const t={title:e('meta[property="og:title"]').attr("content")||e("title").text()||"",description:e('meta[name="description"]').attr("content")||e('meta[property="og:description"]').attr("content")||""};let m="";const n=["article","main",'[role="main"]',".content",".post",".entry","body"];for(const s of n){const i=e(s).first();if(i.length&&i.text().trim().length>200){m=i.text();break}}return m||(m=e("body").text()),m=m.replace(/\s+/g," ").trim().slice(0,8e3),{url:p.url,title:t.title.trim(),description:t.description.trim(),content:m,focus:p.focus}}},V={name:"web_search",description:"Use duckduckgo (anonymous) to find find relevant online resources. Returns a list of URLs that works great with the `read_webpage` tool",args:{query:{type:"string",description:"Search string",required:!0},length:{type:"string",description:"Number of results to return",default:5}},fn:async p=>{const r=await fetch(`https://html.duckduckgo.com/html/?q=${encodeURIComponent(p.query)}`,{headers:{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)","Accept-Language":"en-US,en;q=0.9"}}).then(n=>n.text());let e,t=/<a .*?href="(.+?)".+?<\/a>/g;const m=new y.ASet;for(;(e=t.exec(r))!==null;){let n=/uddg=(.+)&amp?/.exec(decodeURIComponent(e[1]))?.[1];if(n&&(n=decodeURIComponent(n)),n&&m.add(n),m.size>=(p.length||5))break}return m}};exports.Ai=I;exports.Anthropic=O;exports.Audio=E;exports.CliTool=$;exports.DateTimeTool=F;exports.ExecTool=H;exports.FetchTool=B;exports.JSTool=A;exports.LLMProvider=x;exports.OpenAi=S;exports.PythonTool=v;exports.ReadWebpageTool=G;exports.Vision=M;exports.WebSearchTool=V;
44
+ `}downloads={};pyannote;whisperModel;runAsr(r,e={}){let t;const c=new Promise((o,s)=>{this.downloadAsrModel(e.model).then(l=>{if(e.diarization){let n=O.join(O.dirname(r),"transcript");t=k.spawn(this.ai.options.whisper,["-m",l,"-f",r,"-np","-ml","1","-oj","-of",n],{stdio:["ignore","ignore","pipe"]}),t.on("error",a=>s(a)),t.on("close",async a=>{if(a===0){n=await _.readFile(n+".json","utf-8"),_.rm(n+".json").catch(()=>{});try{o(JSON.parse(n))}catch{s(new Error("Failed to parse whisper JSON"))}}else s(new Error(`Exit code ${a}`))})}else{let n="";t=k.spawn(this.ai.options.whisper,["-m",l,"-f",r,"-np","-nt"]),t.on("error",a=>s(a)),t.stdout.on("data",a=>n+=a.toString()),t.on("close",async a=>{a===0?o(n.trim()||null):s(new Error(`Exit code ${a}`))})}})});return Object.assign(c,{abort:()=>t?.kill("SIGTERM")})}runDiarization(r){let e=!1,t=()=>{e=!0};const c=s=>new Promise(l=>{const n=k.spawn(s,["-W","ignore","-c","import pyannote.audio"]);n.on("close",a=>l(a===0)),n.on("error",()=>l(!1))}),o=Promise.all([c("python"),c("python3")]).then((async([s,l])=>{if(e)return;if(!s&&!l)throw new Error("Pyannote is not installed: pip install pyannote.audio");const n=l?"python3":"python";return new Promise((a,d)=>{if(e)return;let i="";const m=k.spawn(n,["-W","ignore","-c",this.pyannote,r]);m.stdout.on("data",p=>i+=p.toString()),m.stderr.on("data",p=>console.error(p.toString())),m.on("close",p=>{if(p===0)try{a(JSON.parse(i))}catch{d(new Error("Failed to parse diarization output"))}else d(new Error(`Python process exited with code ${p}`))}),m.on("error",d),t=()=>m.kill("SIGTERM")})}));return Object.assign(o,{abort:t})}async combineSpeakerTranscript(r,e,t){const c=new Map;let o=0;t.forEach(n=>{c.has(n.speaker)||c.set(n.speaker,++o)});const s=r.match(/[^.!?]+[.!?]+/g)||[r],l=[];return s.forEach(n=>{if(n=n.trim(),!n)return;const a=n.toLowerCase().replace(/[^\w\s]/g,"").split(/\s+/);let d=1/0;const i=[];if(e.transcription.forEach(f=>{const y=f.text.trim().toLowerCase();if(a.some(w=>y.includes(w))){const w=f.offsets.from/1e3,b=f.offsets.to/1e3;i.push({start:w,end:b}),w<d&&(d=w)}}),d===1/0)return;const m=new Map;i.forEach(f=>{t.forEach(y=>{const w=Math.max(0,Math.min(f.end,y.end)-Math.max(f.start,y.start)),b=f.end-f.start;if(b>0){const L=w/b,P=c.get(y.speaker);m.set(P,(m.get(P)||0)+L)}})});let p=1,u=0;m.forEach((f,y)=>{f>u&&(u=f,p=y)}),l.push(`[Speaker ${p}]: ${n}`)}),l.join(`
45
+ `).trim()}asr(r,e={}){if(!this.ai.options.whisper)throw new Error("Whisper not configured");const t=S.join(D.mkdtempSync(S.join(M.tmpdir(),"audio-")),"converted.wav");k.execSync(`ffmpeg -i "${r}" -ar 16000 -ac 1 -f wav "${t}"`,{stdio:"ignore"});const c=()=>_.rm(S.dirname(t),{recursive:!0,force:!0}).catch(()=>{}),o=this.runAsr(t,{model:e.model,diarization:!1}),s=e.diarization?this.runAsr(t,{model:e.model,diarization:!0}):Promise.resolve(null),l=e.diarization?this.runDiarization(t):Promise.resolve(null);let n=!1,a=()=>{n=!0,o.abort(),s?.abort?.(),l?.abort?.(),c()};const d=Promise.allSettled([o,s,l]).then(async([i,m,p])=>{if(i.status=="rejected")throw new Error(`Whisper.cpp punctuated:
46
+ `+i.reason);if(m.status=="rejected")throw new Error(`Whisper.cpp timestamps:
47
+ `+m.reason);if(p.status=="rejected")throw new Error(`Pyannote:
48
+ `+p.reason);if(n||!e.diarization)return i.value;let u=await this.combineSpeakerTranscript(i.value,m.value,p.value);if(!n&&e.diarization==="id"){if(!this.ai.language.defaultModel)throw new Error("Configure an LLM for advanced ASR speaker detection");let f=this.ai.language.chunk(u,500,0);f.length>4&&(f=[...f.slice(0,3),f.at(-1)]);const y=await this.ai.language.json(f.join(`
49
+ `),'{1: "Detected Name", 2: "Second Name"}',{system:"Use the following transcript to identify speakers. Only identify speakers you are positive about, dont mention speakers you are unsure about in your response",temperature:.1});Object.entries(y).forEach(([w,b])=>u=u.replaceAll(`[Speaker ${w}]`,`[${b}]`))}return u}).finally(()=>c());return Object.assign(d,{abort:a})}async downloadAsrModel(r=this.whisperModel){if(!this.ai.options.whisper)throw new Error("Whisper not configured");r.endsWith(".bin")||(r+=".bin");const e=S.join(this.ai.options.path,r);return await _.stat(e).then(()=>!0).catch(()=>!1)?e:this.downloads[r]?this.downloads[r]:(this.downloads[r]=fetch(`https://huggingface.co/ggerganov/whisper.cpp/resolve/main/${r}`).then(t=>t.arrayBuffer()).then(t=>Buffer.from(t)).then(async t=>(await _.writeFile(e,t),delete this.downloads[r],e)),this.downloads[r])}}class N{constructor(r){this.ai=r}ocr(r){let e;const t=new Promise(async c=>{e=await I.createWorker(this.ai.options.ocr||"eng",2,{cachePath:this.ai.options.path});const{data:o}=await e.recognize(r);await e.terminate(),c(o.text.trim()||null)});return Object.assign(t,{abort:()=>e?.terminate()})}}class V{constructor(r){this.options=r,r.path||(r.path=H.tmpdir()),process.env.TRANSFORMERS_CACHE=r.path,this.audio=new $(this),this.language=new G(this),this.vision=new N(this)}audio;language;vision}const R={name:"cli",description:"Use the command line interface, returns any output",args:{command:{type:"string",description:"Command to run",required:!0}},fn:h=>v.$`${h.command}`},K={name:"get_datetime",description:"Get current UTC date / time",args:{},fn:async()=>new Date().toUTCString()},Y={name:"exec",description:"Run code/scripts",args:{language:{type:"string",description:"Execution language",enum:["cli","node","python"],required:!0},code:{type:"string",description:"Code to execute",required:!0}},fn:async(h,r,e)=>{try{switch(h.type){case"bash":return await R.fn({command:h.code},r,e);case"node":return await U.fn({code:h.code},r,e);case"python":return await z.fn({code:h.code},r,e)}}catch(t){return{error:t?.message||t.toString()}}}},Q={name:"fetch",description:"Make HTTP request to URL",args:{url:{type:"string",description:"URL to fetch",required:!0},method:{type:"string",description:"HTTP method to use",enum:["GET","POST","PUT","DELETE"],default:"GET"},headers:{type:"object",description:"HTTP headers to send",default:{}},body:{type:"object",description:"HTTP body to send"}},fn:h=>new g.Http({url:h.url,headers:h.headers}).request({method:h.method||"GET",body:h.body})},U={name:"exec_javascript",description:"Execute commonjs javascript",args:{code:{type:"string",description:"CommonJS javascript",required:!0}},fn:async h=>{const r=g.consoleInterceptor(null),e=await g.fn({console:r},h.code,!0).catch(t=>r.output.error.push(t));return{...r.output,return:e,stdout:void 0,stderr:void 0}}},z={name:"exec_javascript",description:"Execute commonjs javascript",args:{code:{type:"string",description:"CommonJS javascript",required:!0}},fn:async h=>({result:v.$Sync`python -c "${h.code}"`})},X={name:"read_webpage",description:"Extract clean, structured content from a webpage. Use after web_search to read specific URLs",args:{url:{type:"string",description:"URL to extract content from",required:!0},focus:{type:"string",description:'Optional: What aspect to focus on (e.g., "pricing", "features", "contact info")'}},fn:async h=>{const r=await fetch(h.url,{headers:{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}}).then(s=>s.text()).catch(s=>{throw new Error(`Failed to fetch: ${s.message}`)}),e=B.load(r);e('script, style, nav, footer, header, aside, iframe, noscript, [role="navigation"], [role="banner"], .ad, .ads, .cookie, .popup').remove();const t={title:e('meta[property="og:title"]').attr("content")||e("title").text()||"",description:e('meta[name="description"]').attr("content")||e('meta[property="og:description"]').attr("content")||""};let c="";const o=["article","main",'[role="main"]',".content",".post",".entry","body"];for(const s of o){const l=e(s).first();if(l.length&&l.text().trim().length>200){c=l.text();break}}return c||(c=e("body").text()),c=c.replace(/\s+/g," ").trim().slice(0,8e3),{url:h.url,title:t.title.trim(),description:t.description.trim(),content:c,focus:h.focus}}},Z={name:"web_search",description:"Use duckduckgo (anonymous) to find find relevant online resources. Returns a list of URLs that works great with the `read_webpage` tool",args:{query:{type:"string",description:"Search string",required:!0},length:{type:"string",description:"Number of results to return",default:5}},fn:async h=>{const r=await fetch(`https://html.duckduckgo.com/html/?q=${encodeURIComponent(h.query)}`,{headers:{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)","Accept-Language":"en-US,en;q=0.9"}}).then(o=>o.text());let e,t=/<a .*?href="(.+?)".+?<\/a>/g;const c=new g.ASet;for(;(e=t.exec(r))!==null;){let o=/uddg=(.+)&amp?/.exec(decodeURIComponent(e[1]))?.[1];if(o&&(o=decodeURIComponent(o)),o&&c.add(o),c.size>=(h.length||5))break}return c}};exports.Ai=V;exports.Anthropic=A;exports.Audio=$;exports.CliTool=R;exports.DateTimeTool=K;exports.ExecTool=Y;exports.FetchTool=Q;exports.JSTool=U;exports.LLMProvider=E;exports.OpenAi=j;exports.PythonTool=z;exports.ReadWebpageTool=X;exports.Vision=N;exports.WebSearchTool=Z;
47
50
  //# sourceMappingURL=index.js.map