@atgs/tapeworm 0.1.4 → 0.1.6

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.d.ts CHANGED
@@ -6,3 +6,4 @@ export { default as ToolCall, ToolCallBuilder } from "./tool/toolCall";
6
6
  export { default as ToolSchema, ToolSchemaBuilder, Parameter, ParameterBuilder, } from "./tool/toolschema";
7
7
  export { default as OllamaModel } from "./model/OllamaModel";
8
8
  export { default as Message, MessageBuilder, MessageComponentType, MessageComponent, Content, Thinking, ToolResult, } from "./conversation/message";
9
+ export { ToolName, ToolDescription, ToolParameter, ToolOutput, } from "./tool/toolDecorators";
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class v{messages;manager;constructor(){this.messages=[],this.manager=new p}append(e){this.messages.push(e),this.messages=this.manager.compact(this.messages)}}class y{compact(e){throw new _("No implementation for conversation manager!")}configure(e){throw new _("No implementation for conversation manager!")}}class p extends y{compact(e){return e}configure(e){}}class _ extends Error{constructor(e){super(e),this.name="ConversationManagerNotImplemented"}}class h{role;content;constructor(e,t){this.role=e,this.content=t}static builder(){return new M}filter(e){return this.content.filter(t=>t.getMessageComponentType()==e)}}class M{_role;_content;role(e){return this._role=e,this}init(){this._content==null&&(this._content=[])}toolCall(e){return e==null?this:(this.init(),this._content.push(e),this)}toolCalls(e){if(e==null)return this;this.init();for(const t of e)this._content.push(t);return this}toolResult(e){return e==null?this:(this.init(),this._content.push(e),this)}thinking(e){return e==null?this:(this.init(),this._content.push(C.of(e)),this)}content(e){return e==null?this:(this.init(),this._content.push(w.of(e)),this)}build(){if(this._content==null||this._content.length==0)throw new Error("Role-only messages are not supported by Tapeworm.");return new h(this._role,this._content)}}const a={Content:"content",Thinking:"thinking",ToolCall:"toolcall",ToolResult:"toolresult"};class u{getMessageComponentType(){throw new Error("Message components that do not have a message component type are not allowed.")}}class w extends u{text;constructor(e){super(),this.text=e}getMessageComponentType(){return a.Content}get(){return this.text}static of(e){return new this(e)}}class C extends u{thought;constructor(e){super(),this.thought=e}getMessageComponentType(){return a.Thinking}get(){return this.thought}static of(e){return new this(e)}}class m extends u{id;toolName;toolResult;constructor(e,t,o){super(),this.id=e,this.toolName=t,this.toolResult=o}getMessageComponentType(){return a.ToolResult}static of(e,t){return new this(e.id,e.name,t)}}class b{async invoke(e){throw new T("The invoke function for this model was not correctly implemented.")}tokenLimit(){throw new T("The tokenLimit function for this model was not correctly implemented.")}}class N{messages;tools;constructor(e,t){this.messages=e,this.tools=t}static builder(){return new g}}class g{_messages;_tools;messages(e){return this._messages=e,this}tools(e){return this._tools=e,this}build(){if(this._tools==null&&(this._tools=[]),this._messages==null)throw new j("Requests to the model should include content.");return new N(this._messages,this._tools)}}class T extends Error{constructor(e){super(e),this.name="ModelNotImplementedError"}}class j extends Error{constructor(e){super(e),this.name="MessagesNotDefinedError"}}class f extends u{sequence;name;parameters;type;id;getMessageComponentType(){return a.ToolCall}constructor(e,t,o,s,i){super(),this.sequence=e,this.name=t,this.parameters=o,this.type=s,this.id=i}static builder(){return new x}}class x{_sequence;_name;_parameters;_type;_id;sequence(e){return this._sequence=e,this}name(e){return this._name=e,this}parameters(e){return this._parameters=e,this}type(e){return this._type=e,this}id(e){return e!=null&&(this._id=e),this}build(){return this._sequence==null&&(this._sequence=0),this._id==null&&(this._id=(Math.random()+1).toString(36).slice(2,7)),new f(this._sequence,this._name,this._parameters,this._type,this._id)}}class D extends Error{constructor(e){super(e),this.name="ToolNotFoundError"}}class k{name;systemPrompt;tools;model;conversation;conversationManager;toolNameToIndexMap;callback;constructor(e,t,o,s,i){this.name=e,this.model=o,this.conversationManager=s,this.tools=t,this.callback=i,this.conversationManager.configure(o)}async invoke(e){this.conversation==null&&(this.conversation=new v,this.conversationManager!=null&&(this.conversation.manager=this.conversationManager),this.systemPrompt!=null&&this.conversation.append(h.builder().role("system").content(this.systemPrompt).build())),this.conversation.append(h.builder().role("user").content(e).build());let t=!1;for(;!t;){let o=await this._runQuery();this.callback(o),this.conversation.append(o),t=!0;const s=o.filter(a.ToolCall);if(s!=null&&s.length!=0){t=!1,s.sort((i,l)=>(i.sequence??0)<(l.sequence??0)?-1:1);for(let i of s)await this._runTool(i)}}}async _runQuery(){return await this.model.invoke(new g().messages(this.conversation?.messages).tools(this.tools).build())}async _runTool(e){if(this.generateToolNameToIndexMap(),!(e.name in this.toolNameToIndexMap)){this.conversation.append(h.builder().role("tool").toolResult(m.of(e,new D("Agent does not have a tool with this name."))).build());return}let t=this.tools[this.toolNameToIndexMap[e.name]];try{let o=await t.execute(e.parameters);this.conversation.append(h.builder().role("tool").toolResult(m.of(e,o)).build())}catch(o){this.conversation.append(h.builder().role("tool").toolResult(m.of(e,JSON.stringify(o))).build())}}generateToolNameToIndexMap(){if(this.toolNameToIndexMap==null){this.toolNameToIndexMap={};for(let e=0;e<this.tools.length;e++)this.toolNameToIndexMap[this.tools[e].getName()]=e}}static builder(){return new A}}class A{_name;_systemPrompt;_tools;_model;_conversation;_conversationManager=new p;_toolNameToIndexMap;_callback=e=>J(e);name(e){return this._name=e,this}systemPrompt(e){return this._systemPrompt=e,this}tools(e){return this._tools=e,this}addTool(e){return this._tools==null&&(this._tools=[]),this._tools.push(e),this}model(e){return this._model=e,this}conversationManager(e){return this._conversationManager=e,this}callback(e){return this._callback=e,this}build(){let e=new k(this._name,this._tools,this._model,this._conversationManager,this._callback);return this._conversation!=null&&(e.conversation=this._conversation),this._systemPrompt!=null&&(e.systemPrompt=this._systemPrompt),e}}function J(n){for(const e of n.filter(a.Thinking))console.log("\x1B[90m"+e.get()+"\x1B[0m");for(const e of n.filter(a.Content))console.log(e.get());for(const e of n.filter(a.ToolCall))console.log("\x1B[32mCalling Tool: "+e.name+"\x1B[0m")}class d extends Error{constructor(e){super(e),this.name="ToolNotDefinedError"}}class F{name;description;tool_schema;constructor(){this.name=this.getName(),this.description=this.getDescription(),this.tool_schema=this.getToolSchema()}getName(){throw new d("Tool name not defined.")}getDescription(){throw new d("Tool description not defined.")}getToolSchema(){throw new d("Tool parameter schema not defined.")}execute(e){return null}}class q{parameters;output;constructor(e,t){this.parameters=e,this.output=t}static builder(){return new R}}class R{_parameters;_output;addParameter(e){return this._parameters==null&&(this._parameters=[]),this._parameters.push(e),this}output(e){return this._output=e,this}build(){return this._parameters==null&&(this._parameters=[]),new q(this._parameters,this._output)}}class E{name;description;type;required;constructor(e,t,o,s){this.name=e,this.description=t,this.type=o,this.required=s,this.assertValidType()}assertValidType(){}static builder(){return new P}}class P{_name;_description;_type;_required;name(e){return this._name=e,this}description(e){return this._description=e,this}type(e){return this._type=e,this}required(e){return this._required=e,this}build(){return this._required==null&&(this._required=!1),new E(this._name,this._description,this._type,this._required)}}class L extends b{endpoint;model;options;constructor(e,t,o){super(),this.endpoint=e,this.model=t,this.options=o}async invoke(e){let t={model:this.model,messages:this._formatMessages(e),tools:this._formatTools(e),...this.options},o=await fetch(this.endpoint+"/api/chat",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});if(!o.ok)throw new Error(`HTTP error! status: ${o.status}`);let s=await o.json(),i=[];if(s.message.tool_calls)for(let l in s.message.tool_calls){const r=s.message.tool_calls[l];let c="function",S=r[c]?.name,B=r[c]?.index,O=r.id??void 0,I=typeof r[c]?.arguments=="string"?JSON.parse(r[c]?.arguments):r[c]?.arguments;i.push(f.builder().name(S).type(c).parameters(I).sequence(B).id(O).build())}return h.builder().toolCalls(i).role(s.message.role).content(s.message.content).thinking(s.message.thinking).build()}_formatTools(e){let t=[];for(let o of e.tools){let s={},i=[];for(let r of o.getToolSchema().parameters)s[r.name]={},s[r.name].type=r.type,s[r.name].description=r.description,r.required&&i.push(r.name);let l={type:"function",function:{name:o.getName(),description:o.getDescription(),parameters:{type:"object",properties:s,required:i}}};t.push(l)}return t}_formatMessages(e){let t=[];for(let o of e.messages){if(o.role=="assistant"||o.role=="system"||o.role=="user"){t.push(this._formatSingleMessage(o));continue}if(o.role=="tool"){for(const s of o.content)if(s.getMessageComponentType()==a.ToolResult){const i=s;t.push({role:o.role,name:i.toolName,content:JSON.stringify(i.toolResult)})}}}return t}_formatSingleMessage(e){let t={role:e.role},o,s,i;for(const l of e.content){if(l.getMessageComponentType()==a.Content){const r=l;o==null&&(o=""),o+=r.get()}if(l.getMessageComponentType()==a.Thinking){const r=l;i==null&&(i=""),i+=r.get()}if(l.getMessageComponentType()==a.ToolCall){const r=l;s==null&&(s=[]),s.push(this._formatToolCall(r))}}return o!=null&&(t.content=o),s!=null&&(t.tool_calls=s),i!=null&&(t.thinking=i),t}_formatToolCall(e){return{function:{name:e.name,arguments:e.parameters}}}}exports.Agent=k;exports.Content=w;exports.Conversation=v;exports.ConversationManager=y;exports.DefaultConversationManager=p;exports.Message=h;exports.MessageBuilder=M;exports.MessageComponent=u;exports.MessageComponentType=a;exports.Model=b;exports.ModelRequest=N;exports.ModelRequestBuilder=g;exports.OllamaModel=L;exports.Parameter=E;exports.ParameterBuilder=P;exports.Thinking=C;exports.Tool=F;exports.ToolCall=f;exports.ToolCallBuilder=x;exports.ToolResult=m;exports.ToolSchema=q;exports.ToolSchemaBuilder=R;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class w{messages;manager;constructor(){this.messages=[],this.manager=new g}append(e){this.messages.push(e),this.messages=this.manager.compact(this.messages)}}class M{compact(e){throw new y("No implementation for conversation manager!")}configure(e){throw new y("No implementation for conversation manager!")}}class g extends M{compact(e){return e}configure(e){}}class y extends Error{constructor(e){super(e),this.name="ConversationManagerNotImplemented"}}class u{role;content;constructor(e,o){this.role=e,this.content=o}static builder(){return new b}filter(e){return this.content.filter(o=>o.getMessageComponentType()==e)}}class b{_role;_content;role(e){return this._role=e,this}init(){this._content==null&&(this._content=[])}toolCall(e){return e==null?this:(this.init(),this._content.push(e),this)}toolCalls(e){if(e==null)return this;this.init();for(const o of e)this._content.push(o);return this}toolResult(e){return e==null?this:(this.init(),this._content.push(e),this)}thinking(e){return e==null?this:(this.init(),this._content.push(C.of(e)),this)}content(e){return e==null?this:(this.init(),this._content.push(N.of(e)),this)}build(){if(this._content==null||this._content.length==0)throw new Error("Role-only messages are not supported by Tapeworm.");return new u(this._role,this._content)}}const a={Content:"content",Thinking:"thinking",ToolCall:"toolcall",ToolResult:"toolresult"};class m{getMessageComponentType(){throw new Error("Message components that do not have a message component type are not allowed.")}}class N extends m{text;constructor(e){super(),this.text=e}getMessageComponentType(){return a.Content}get(){return this.text}static of(e){return new this(e)}}class C extends m{thought;constructor(e){super(),this.thought=e}getMessageComponentType(){return a.Thinking}get(){return this.thought}static of(e){return new this(e)}}class c extends m{id;toolName;toolResult;constructor(e,o,s){super(),this.id=e,this.toolName=o,this.toolResult=s}getMessageComponentType(){return a.ToolResult}static of(e,o){return new this(e.id,e.name,o)}}class P{async invoke(e){throw new v("The invoke function for this model was not correctly implemented.")}tokenLimit(){throw new v("The tokenLimit function for this model was not correctly implemented.")}}class q{messages;tools;constructor(e,o){this.messages=e,this.tools=o}static builder(){return new f}}class f{_messages;_tools;messages(e){return this._messages=e,this}tools(e){return this._tools=e,this}build(){if(this._tools==null&&(this._tools=[]),this._messages==null)throw new j("Requests to the model should include content.");return new q(this._messages,this._tools)}}class v extends Error{constructor(e){super(e),this.name="ModelNotImplementedError"}}class j extends Error{constructor(e){super(e),this.name="MessagesNotDefinedError"}}class _ extends m{sequence;name;parameters;type;id;getMessageComponentType(){return a.ToolCall}constructor(e,o,s,n,r){super(),this.sequence=e,this.name=o,this.parameters=s,this.type=n,this.id=r}static builder(){return new x}}class x{_sequence;_name;_parameters;_type;_id;sequence(e){return this._sequence=e,this}name(e){return this._name=e,this}parameters(e){return this._parameters=e,this}type(e){return this._type=e,this}id(e){return e!=null&&(this._id=e),this}build(){return this._sequence==null&&(this._sequence=0),this._id==null&&(this._id=(Math.random()+1).toString(36).slice(2,7)),new _(this._sequence,this._name,this._parameters,this._type,this._id)}}class D extends Error{constructor(e){super(e),this.name="ToolNotFoundError"}}class k{name;systemPrompt;tools;model;conversation;conversationManager;toolNameToIndexMap;callback;constructor(e,o,s,n,r){this.name=e,this.model=s,this.conversationManager=n,this.tools=o,this.callback=r,this.conversationManager.configure(s)}async invoke(e){this.conversation==null&&(this.conversation=new w,this.conversationManager!=null&&(this.conversation.manager=this.conversationManager),this.systemPrompt!=null&&this.conversation.append(u.builder().role("system").content(this.systemPrompt).build())),this.conversation.append(u.builder().role("user").content(e).build());let o=!1;for(;!o;){let s=await this._runQuery();this.callback(s),this.conversation.append(s),o=!0;const n=s.filter(a.ToolCall);if(n!=null&&n.length!=0){o=!1,n.sort((r,l)=>(r.sequence??0)<(l.sequence??0)?-1:1);for(let r of n)await this._runTool(r)}}}async _runQuery(){return await this.model.invoke(new f().messages(this.conversation?.messages).tools(this.tools).build())}async _runTool(e){if(this.generateToolNameToIndexMap(),!(e.name in this.toolNameToIndexMap)){this.conversation.append(u.builder().role("tool").toolResult(c.of(e,new D("Agent does not have a tool with this name."))).build());return}let o=this.tools[this.toolNameToIndexMap[e.name]];try{let s=await o.execute(e.parameters);this.conversation.append(u.builder().role("tool").toolResult(c.of(e,s)).build())}catch(s){this.conversation.append(u.builder().role("tool").toolResult(c.of(e,JSON.stringify(s))).build())}}generateToolNameToIndexMap(){if(this.toolNameToIndexMap==null){this.toolNameToIndexMap={};for(let e=0;e<this.tools.length;e++)this.toolNameToIndexMap[this.tools[e].getName()]=e}}static builder(){return new A}}class A{_name;_systemPrompt;_tools;_model;_conversation;_conversationManager=new g;_toolNameToIndexMap;_callback=e=>J(e);name(e){return this._name=e,this}systemPrompt(e){return this._systemPrompt=e,this}tools(e){return this._tools=e,this}addTool(e){return this._tools==null&&(this._tools=[]),this._tools.push(e),this}model(e){return this._model=e,this}conversationManager(e){return this._conversationManager=e,this}callback(e){return this._callback=e,this}build(){let e=new k(this._name,this._tools,this._model,this._conversationManager,this._callback);return this._conversation!=null&&(e.conversation=this._conversation),this._systemPrompt!=null&&(e.systemPrompt=this._systemPrompt),e}}function J(t){for(const e of t.filter(a.Thinking))console.log("\x1B[90m"+e.get()+"\x1B[0m");for(const e of t.filter(a.Content))console.log(e.get());for(const e of t.filter(a.ToolCall))console.log("\x1B[32mCalling Tool: "+e.name+"\x1B[0m")}class d extends Error{constructor(e){super(e),this.name="ToolNotDefinedError"}}class F{name;description;tool_schema;constructor(){this.name=this.getName(),this.description=this.getDescription(),this.tool_schema=this.getToolSchema()}getName(){throw new d("Tool name not defined.")}getDescription(){throw new d("Tool description not defined.")}getToolSchema(){throw new d("Tool parameter schema not defined.")}execute(e){return null}}class p{parameters;output;constructor(e,o){this.parameters=e,this.output=o}static builder(){return new O}}class O{_parameters;_output;addParameter(e){return this._parameters==null&&(this._parameters=[]),this._parameters.push(e),this}output(e){return this._output=e,this}build(){return this._parameters==null&&(this._parameters=[]),new p(this._parameters,this._output)}}class T{name;description;type;required;constructor(e,o,s,n){this.name=e,this.description=o,this.type=s,this.required=n,this.assertValidType()}assertValidType(){}static builder(){return new S}}class S{_name;_description;_type;_required;name(e){return this._name=e,this}description(e){return this._description=e,this}type(e){return this._type=e,this}required(e){return this._required=e,this}build(){return this._required==null&&(this._required=!1),new T(this._name,this._description,this._type,this._required)}}class L extends P{endpoint;model;options;constructor(e,o,s){super(),this.endpoint=e,this.model=o,this.options=s}async invoke(e){let o={model:this.model,messages:this._formatMessages(e),tools:this._formatTools(e),...this.options},s=await fetch(this.endpoint+"/api/chat",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});if(!s.ok)throw new Error(`HTTP error! status: ${s.status}`);let n=await s.json(),r=[];if(n.message.tool_calls)for(let l in n.message.tool_calls){const i=n.message.tool_calls[l];let h="function",R=i[h]?.name,E=i[h]?.index,B=i.id??void 0,I=typeof i[h]?.arguments=="string"?JSON.parse(i[h]?.arguments):i[h]?.arguments;r.push(_.builder().name(R).type(h).parameters(I).sequence(E).id(B).build())}return u.builder().toolCalls(r).role(n.message.role).content(n.message.content).thinking(n.message.thinking).build()}_formatTools(e){let o=[];for(let s of e.tools){let n={},r=[];for(let i of s.getToolSchema().parameters)n[i.name]={},n[i.name].type=i.type,n[i.name].description=i.description,i.required&&r.push(i.name);let l={type:"function",function:{name:s.getName(),description:s.getDescription(),parameters:{type:"object",properties:n,required:r}}};o.push(l)}return o}_formatMessages(e){let o=[];for(let s of e.messages){if(s.role=="assistant"||s.role=="system"||s.role=="user"){o.push(this._formatSingleMessage(s));continue}if(s.role=="tool"){for(const n of s.content)if(n.getMessageComponentType()==a.ToolResult){const r=n;o.push({role:s.role,name:r.toolName,content:JSON.stringify(r.toolResult)})}}}return o}_formatSingleMessage(e){let o={role:e.role},s,n,r;for(const l of e.content){if(l.getMessageComponentType()==a.Content){const i=l;s==null&&(s=""),s+=i.get()}if(l.getMessageComponentType()==a.Thinking){const i=l;r==null&&(r=""),r+=i.get()}if(l.getMessageComponentType()==a.ToolCall){const i=l;n==null&&(n=[]),n.push(this._formatToolCall(i))}}return s!=null&&(o.content=s),n!=null&&(o.tool_calls=n),r!=null&&(o.thinking=r),o}_formatToolCall(e){return{function:{name:e.name,arguments:e.parameters}}}}function Q(t){return e=>{e.prototype.getName=()=>t}}function V(t){return e=>{Object.defineProperty(e.prototype,"getDescription",{value:function(){return t},enumerable:!0,configurable:!0,writable:!0})}}function H(t){return e=>{e.prototype.tapewormParams==null&&(e.prototype.tapewormParams=[]),e.prototype.tapewormParamsOutput==null&&(e.prototype.tapewormParamsOutput=""),e.prototype.tapewormParams.push(T.builder().name(t.name).description(t.description).required(t.required).type(t.type).build()),e.prototype.getToolSchema=()=>new p(e.prototype.tapewormParams,e.prototype.tapewormParamsOutput)}}function K(t){return e=>{e.prototype.tapewormParams==null&&(e.prototype.tapewormParams=[]),e.prototype.tapewormParamsOutput==null&&(e.prototype.tapewormParamsOutput=""),e.prototype.tapewormParamsOutput=t,e.prototype.getToolSchema=()=>new p(e.prototype.tapewormParams,e.prototype.tapewormParamsOutput)}}exports.Agent=k;exports.Content=N;exports.Conversation=w;exports.ConversationManager=M;exports.DefaultConversationManager=g;exports.Message=u;exports.MessageBuilder=b;exports.MessageComponent=m;exports.MessageComponentType=a;exports.Model=P;exports.ModelRequest=q;exports.ModelRequestBuilder=f;exports.OllamaModel=L;exports.Parameter=T;exports.ParameterBuilder=S;exports.Thinking=C;exports.Tool=F;exports.ToolCall=_;exports.ToolCallBuilder=x;exports.ToolDescription=V;exports.ToolName=Q;exports.ToolOutput=K;exports.ToolParameter=H;exports.ToolResult=c;exports.ToolSchema=p;exports.ToolSchemaBuilder=O;
@@ -1,11 +1,11 @@
1
- class b {
1
+ class x {
2
2
  messages;
3
3
  manager;
4
4
  /**
5
5
  * Initialize an empty conversation using the default manager.
6
6
  */
7
7
  constructor() {
8
- this.messages = [], this.manager = new f();
8
+ this.messages = [], this.manager = new _();
9
9
  }
10
10
  /**
11
11
  * Append a message to the history and apply compaction rules.
@@ -15,14 +15,14 @@ class b {
15
15
  this.messages.push(e), this.messages = this.manager.compact(this.messages);
16
16
  }
17
17
  }
18
- class C {
18
+ class P {
19
19
  /**
20
20
  * Compact or adjust a conversation according to a strategy.
21
21
  * @param conversation Full list of messages so far.
22
22
  * @returns Adjusted message list to keep.
23
23
  */
24
24
  compact(e) {
25
- throw new p(
25
+ throw new g(
26
26
  "No implementation for conversation manager!"
27
27
  );
28
28
  }
@@ -31,12 +31,12 @@ class C {
31
31
  * @param model Model being used for this conversation.
32
32
  */
33
33
  configure(e) {
34
- throw new p(
34
+ throw new g(
35
35
  "No implementation for conversation manager!"
36
36
  );
37
37
  }
38
38
  }
39
- class f extends C {
39
+ class _ extends P {
40
40
  /**
41
41
  * Default strategy simply returns the conversation untouched.
42
42
  */
@@ -49,7 +49,7 @@ class f extends C {
49
49
  configure(e) {
50
50
  }
51
51
  }
52
- class p extends Error {
52
+ class g extends Error {
53
53
  constructor(e) {
54
54
  super(e), this.name = "ConversationManagerNotImplemented";
55
55
  }
@@ -62,24 +62,24 @@ class h {
62
62
  * @param role Source of the message (e.g., user, assistant, tool).
63
63
  * @param content Ordered list of message components that make up the message body.
64
64
  */
65
- constructor(e, t) {
66
- this.role = e, this.content = t;
65
+ constructor(e, o) {
66
+ this.role = e, this.content = o;
67
67
  }
68
68
  /**
69
69
  * Create a new message builder for ergonomic construction.
70
70
  * @returns A MessageBuilder instance.
71
71
  */
72
72
  static builder() {
73
- return new N();
73
+ return new k();
74
74
  }
75
75
  /**
76
76
  * Filters the message contents based on the message type.
77
77
  */
78
78
  filter(e) {
79
- return this.content.filter((t) => t.getMessageComponentType() == e);
79
+ return this.content.filter((o) => o.getMessageComponentType() == e);
80
80
  }
81
81
  }
82
- class N {
82
+ class k {
83
83
  _role;
84
84
  _content;
85
85
  /**
@@ -113,8 +113,8 @@ class N {
113
113
  if (e == null)
114
114
  return this;
115
115
  this.init();
116
- for (const t of e)
117
- this._content.push(t);
116
+ for (const o of e)
117
+ this._content.push(o);
118
118
  return this;
119
119
  }
120
120
  /**
@@ -131,7 +131,7 @@ class N {
131
131
  * @returns This builder for chaining.
132
132
  */
133
133
  thinking(e) {
134
- return e == null ? this : (this.init(), this._content.push(k.of(e)), this);
134
+ return e == null ? this : (this.init(), this._content.push(C.of(e)), this);
135
135
  }
136
136
  /**
137
137
  * Attach content to the message if provided.
@@ -139,7 +139,7 @@ class N {
139
139
  * @returns This builder for chaining.
140
140
  */
141
141
  content(e) {
142
- return e == null ? this : (this.init(), this._content.push(x.of(e)), this);
142
+ return e == null ? this : (this.init(), this._content.push(q.of(e)), this);
143
143
  }
144
144
  /**
145
145
  * Construct the Message with the accumulated values.
@@ -157,7 +157,7 @@ const l = {
157
157
  ToolCall: "toolcall",
158
158
  ToolResult: "toolresult"
159
159
  };
160
- class u {
160
+ class c {
161
161
  /**
162
162
  * Get the type of this message component, as defined in MessageComponentType
163
163
  */
@@ -167,7 +167,7 @@ class u {
167
167
  );
168
168
  }
169
169
  }
170
- class x extends u {
170
+ class q extends c {
171
171
  text;
172
172
  /**
173
173
  * Create a content component with the provided text.
@@ -199,7 +199,7 @@ class x extends u {
199
199
  return new this(e);
200
200
  }
201
201
  }
202
- class k extends u {
202
+ class C extends c {
203
203
  thought;
204
204
  /**
205
205
  * Create a thinking component with the provided thought text.
@@ -231,7 +231,7 @@ class k extends u {
231
231
  return new this(e);
232
232
  }
233
233
  }
234
- class m extends u {
234
+ class m extends c {
235
235
  id;
236
236
  toolName;
237
237
  toolResult;
@@ -241,8 +241,8 @@ class m extends u {
241
241
  * @param toolName Name of the tool that produced the result.
242
242
  * @param toolResult Output returned by the tool.
243
243
  */
244
- constructor(e, t, o) {
245
- super(), this.id = e, this.toolName = t, this.toolResult = o;
244
+ constructor(e, o, s) {
245
+ super(), this.id = e, this.toolName = o, this.toolResult = s;
246
246
  }
247
247
  /**
248
248
  * Get the type of this message component, as defined in MessageComponentType.
@@ -257,17 +257,17 @@ class m extends u {
257
257
  * @param toolResult Output to attach to the tool result component.
258
258
  * @returns a new ToolResult component mirroring the provided tool call.
259
259
  */
260
- static of(e, t) {
261
- return new this(e.id, e.name, t);
260
+ static of(e, o) {
261
+ return new this(e.id, e.name, o);
262
262
  }
263
263
  }
264
- class q {
264
+ class O {
265
265
  /**
266
266
  * Execute the model against the provided request.
267
267
  * @param request Aggregated messages and tools to send to the model.
268
268
  */
269
269
  async invoke(e) {
270
- throw new g(
270
+ throw new f(
271
271
  "The invoke function for this model was not correctly implemented."
272
272
  );
273
273
  }
@@ -275,7 +275,7 @@ class q {
275
275
  * Return the maximum token budget supported by this model.
276
276
  */
277
277
  tokenLimit() {
278
- throw new g(
278
+ throw new f(
279
279
  "The tokenLimit function for this model was not correctly implemented."
280
280
  );
281
281
  }
@@ -288,18 +288,18 @@ class E {
288
288
  * @param messages Conversation history to send to the model.
289
289
  * @param tools Tool definitions available for function calling.
290
290
  */
291
- constructor(e, t) {
292
- this.messages = e, this.tools = t;
291
+ constructor(e, o) {
292
+ this.messages = e, this.tools = o;
293
293
  }
294
294
  /**
295
295
  * Create a builder for composing a model request.
296
296
  * @returns A ModelRequestBuilder instance.
297
297
  */
298
298
  static builder() {
299
- return new _();
299
+ return new y();
300
300
  }
301
301
  }
302
- class _ {
302
+ class y {
303
303
  _messages;
304
304
  _tools;
305
305
  /**
@@ -328,7 +328,7 @@ class _ {
328
328
  return new E(this._messages, this._tools);
329
329
  }
330
330
  }
331
- class g extends Error {
331
+ class f extends Error {
332
332
  constructor(e) {
333
333
  super(e), this.name = "ModelNotImplementedError";
334
334
  }
@@ -338,7 +338,7 @@ class R extends Error {
338
338
  super(e), this.name = "MessagesNotDefinedError";
339
339
  }
340
340
  }
341
- class v extends u {
341
+ class v extends c {
342
342
  sequence;
343
343
  name;
344
344
  parameters;
@@ -359,18 +359,18 @@ class v extends u {
359
359
  * @param type Type of tool call (e.g., function).
360
360
  * @param id Provider-generated identifier for correlating results.
361
361
  */
362
- constructor(e, t, o, s, i) {
363
- super(), this.sequence = e, this.name = t, this.parameters = o, this.type = s, this.id = i;
362
+ constructor(e, o, s, n, r) {
363
+ super(), this.sequence = e, this.name = o, this.parameters = s, this.type = n, this.id = r;
364
364
  }
365
365
  /**
366
366
  * Convenience factory for a ToolCallBuilder.
367
367
  * @returns A new ToolCallBuilder instance.
368
368
  */
369
369
  static builder() {
370
- return new P();
370
+ return new S();
371
371
  }
372
372
  }
373
- class P {
373
+ class S {
374
374
  _sequence;
375
375
  _name;
376
376
  _parameters;
@@ -435,7 +435,7 @@ class I extends Error {
435
435
  super(e), this.name = "ToolNotFoundError";
436
436
  }
437
437
  }
438
- class O {
438
+ class j {
439
439
  name;
440
440
  systemPrompt;
441
441
  tools;
@@ -444,8 +444,8 @@ class O {
444
444
  conversationManager;
445
445
  toolNameToIndexMap;
446
446
  callback;
447
- constructor(e, t, o, s, i) {
448
- this.name = e, this.model = o, this.conversationManager = s, this.tools = t, this.callback = i, this.conversationManager.configure(o);
447
+ constructor(e, o, s, n, r) {
448
+ this.name = e, this.model = s, this.conversationManager = n, this.tools = o, this.callback = r, this.conversationManager.configure(s);
449
449
  }
450
450
  /**
451
451
  * Run the full agent loop for a user query: seed the conversation, invoke the model,
@@ -453,22 +453,22 @@ class O {
453
453
  * @param query User-provided input to hand to the agent.
454
454
  */
455
455
  async invoke(e) {
456
- this.conversation == null && (this.conversation = new b(), this.conversationManager != null && (this.conversation.manager = this.conversationManager), this.systemPrompt != null && this.conversation.append(
456
+ this.conversation == null && (this.conversation = new x(), this.conversationManager != null && (this.conversation.manager = this.conversationManager), this.systemPrompt != null && this.conversation.append(
457
457
  h.builder().role("system").content(this.systemPrompt).build()
458
458
  )), this.conversation.append(
459
459
  h.builder().role("user").content(e).build()
460
460
  );
461
- let t = !1;
462
- for (; !t; ) {
463
- let o = await this._runQuery();
464
- this.callback(o), this.conversation.append(o), t = !0;
465
- const s = o.filter(
461
+ let o = !1;
462
+ for (; !o; ) {
463
+ let s = await this._runQuery();
464
+ this.callback(s), this.conversation.append(s), o = !0;
465
+ const n = s.filter(
466
466
  l.ToolCall
467
467
  );
468
- if (s != null && s.length != 0) {
469
- t = !1, s.sort((i, a) => (i.sequence ?? 0) < (a.sequence ?? 0) ? -1 : 1);
470
- for (let i of s)
471
- await this._runTool(i);
468
+ if (n != null && n.length != 0) {
469
+ o = !1, n.sort((r, a) => (r.sequence ?? 0) < (a.sequence ?? 0) ? -1 : 1);
470
+ for (let r of n)
471
+ await this._runTool(r);
472
472
  }
473
473
  }
474
474
  }
@@ -478,7 +478,7 @@ class O {
478
478
  */
479
479
  async _runQuery() {
480
480
  return await this.model.invoke(
481
- new _().messages(this.conversation?.messages).tools(this.tools).build()
481
+ new y().messages(this.conversation?.messages).tools(this.tools).build()
482
482
  );
483
483
  }
484
484
  /**
@@ -500,15 +500,15 @@ class O {
500
500
  );
501
501
  return;
502
502
  }
503
- let t = this.tools[this.toolNameToIndexMap[e.name]];
503
+ let o = this.tools[this.toolNameToIndexMap[e.name]];
504
504
  try {
505
- let o = await t.execute(e.parameters);
505
+ let s = await o.execute(e.parameters);
506
506
  this.conversation.append(
507
- h.builder().role("tool").toolResult(m.of(e, o)).build()
507
+ h.builder().role("tool").toolResult(m.of(e, s)).build()
508
508
  );
509
- } catch (o) {
509
+ } catch (s) {
510
510
  this.conversation.append(
511
- h.builder().role("tool").toolResult(m.of(e, JSON.stringify(o))).build()
511
+ h.builder().role("tool").toolResult(m.of(e, JSON.stringify(s))).build()
512
512
  );
513
513
  }
514
514
  }
@@ -523,18 +523,18 @@ class O {
523
523
  }
524
524
  }
525
525
  static builder() {
526
- return new S();
526
+ return new B();
527
527
  }
528
528
  }
529
- class S {
529
+ class B {
530
530
  _name;
531
531
  _systemPrompt;
532
532
  _tools;
533
533
  _model;
534
534
  _conversation;
535
- _conversationManager = new f();
535
+ _conversationManager = new _();
536
536
  _toolNameToIndexMap;
537
- _callback = (e) => j(e);
537
+ _callback = (e) => D(e);
538
538
  name(e) {
539
539
  return this._name = e, this;
540
540
  }
@@ -557,7 +557,7 @@ class S {
557
557
  return this._callback = e, this;
558
558
  }
559
559
  build() {
560
- let e = new O(
560
+ let e = new j(
561
561
  this._name,
562
562
  this._tools,
563
563
  this._model,
@@ -567,15 +567,15 @@ class S {
567
567
  return this._conversation != null && (e.conversation = this._conversation), this._systemPrompt != null && (e.systemPrompt = this._systemPrompt), e;
568
568
  }
569
569
  }
570
- function j(n) {
571
- for (const e of n.filter(l.Thinking))
570
+ function D(t) {
571
+ for (const e of t.filter(l.Thinking))
572
572
  console.log("\x1B[90m" + e.get() + "\x1B[0m");
573
- for (const e of n.filter(l.Content))
573
+ for (const e of t.filter(l.Content))
574
574
  console.log(e.get());
575
- for (const e of n.filter(l.ToolCall))
575
+ for (const e of t.filter(l.ToolCall))
576
576
  console.log("\x1B[32mCalling Tool: " + e.name + "\x1B[0m");
577
577
  }
578
- class d extends Error {
578
+ class p extends Error {
579
579
  constructor(e) {
580
580
  super(e), this.name = "ToolNotDefinedError";
581
581
  }
@@ -595,21 +595,21 @@ class F {
595
595
  * @returns A short, stable identifier string.
596
596
  */
597
597
  getName() {
598
- throw new d("Tool name not defined.");
598
+ throw new p("Tool name not defined.");
599
599
  }
600
600
  /**
601
601
  * Human-readable description of what the tool does.
602
602
  * @returns Description string shown to the model.
603
603
  */
604
604
  getDescription() {
605
- throw new d("Tool description not defined.");
605
+ throw new p("Tool description not defined.");
606
606
  }
607
607
  /**
608
608
  * Structured schema describing tool parameters and output.
609
609
  * @returns ToolSchema instance.
610
610
  */
611
611
  getToolSchema() {
612
- throw new d("Tool parameter schema not defined.");
612
+ throw new p("Tool parameter schema not defined.");
613
613
  }
614
614
  // @ts-ignore
615
615
  /**
@@ -621,7 +621,7 @@ class F {
621
621
  return null;
622
622
  }
623
623
  }
624
- class B {
624
+ class d {
625
625
  parameters;
626
626
  output;
627
627
  /**
@@ -629,18 +629,18 @@ class B {
629
629
  * @param parameters Ordered list of input parameters the tool accepts.
630
630
  * @param output Human-readable description of the tool output.
631
631
  */
632
- constructor(e, t) {
633
- this.parameters = e, this.output = t;
632
+ constructor(e, o) {
633
+ this.parameters = e, this.output = o;
634
634
  }
635
635
  /**
636
636
  * Return a tool schema builder, so you can properly construct tools instead of dealing with the constructor
637
637
  * @returns a tool schema builder.
638
638
  */
639
639
  static builder() {
640
- return new D();
640
+ return new J();
641
641
  }
642
642
  }
643
- class D {
643
+ class J {
644
644
  _parameters;
645
645
  _output;
646
646
  /**
@@ -664,10 +664,10 @@ class D {
664
664
  * @returns Constructed ToolSchema describing parameters and output.
665
665
  */
666
666
  build() {
667
- return this._parameters == null && (this._parameters = []), new B(this._parameters, this._output);
667
+ return this._parameters == null && (this._parameters = []), new d(this._parameters, this._output);
668
668
  }
669
669
  }
670
- class J {
670
+ class T {
671
671
  name;
672
672
  description;
673
673
  type;
@@ -679,8 +679,8 @@ class J {
679
679
  * @param type JSON-serializable type name (e.g., string, number).
680
680
  * @param required Whether the parameter must be provided.
681
681
  */
682
- constructor(e, t, o, s) {
683
- this.name = e, this.description = t, this.type = o, this.required = s, this.assertValidType();
682
+ constructor(e, o, s, n) {
683
+ this.name = e, this.description = o, this.type = s, this.required = n, this.assertValidType();
684
684
  }
685
685
  /**
686
686
  * Placeholder for validating supported parameter types.
@@ -733,7 +733,7 @@ class A {
733
733
  * @returns Constructed Parameter definition.
734
734
  */
735
735
  build() {
736
- return this._required == null && (this._required = !1), new J(
736
+ return this._required == null && (this._required = !1), new T(
737
737
  this._name,
738
738
  this._description,
739
739
  this._type,
@@ -741,7 +741,7 @@ class A {
741
741
  );
742
742
  }
743
743
  }
744
- class L extends q {
744
+ class L extends O {
745
745
  endpoint;
746
746
  model;
747
747
  options;
@@ -751,38 +751,38 @@ class L extends q {
751
751
  * @param model Model name/tag to use for inference.
752
752
  * @param options Additional Ollama options passed through to the API.
753
753
  */
754
- constructor(e, t, o) {
755
- super(), this.endpoint = e, this.model = t, this.options = o;
754
+ constructor(e, o, s) {
755
+ super(), this.endpoint = e, this.model = o, this.options = s;
756
756
  }
757
757
  /**
758
758
  * Call the Ollama chat API with the provided conversation and tools.
759
759
  * Formats the request, performs the HTTP POST, and translates the response into a ModelResponse.
760
760
  */
761
761
  async invoke(e) {
762
- let t = {
762
+ let o = {
763
763
  model: this.model,
764
764
  messages: this._formatMessages(e),
765
765
  tools: this._formatTools(e),
766
766
  ...this.options
767
- }, o = await fetch(this.endpoint + "/api/chat", {
767
+ }, s = await fetch(this.endpoint + "/api/chat", {
768
768
  method: "POST",
769
769
  headers: {
770
770
  "Content-Type": "application/json"
771
771
  },
772
- body: JSON.stringify(t)
772
+ body: JSON.stringify(o)
773
773
  });
774
- if (!o.ok)
775
- throw new Error(`HTTP error! status: ${o.status}`);
776
- let s = await o.json(), i = [];
777
- if (s.message.tool_calls)
778
- for (let a in s.message.tool_calls) {
779
- const r = s.message.tool_calls[a];
780
- let c = "function", T = r[c]?.name, y = r[c]?.index, w = r.id ?? void 0, M = typeof r[c]?.arguments == "string" ? JSON.parse(r[c]?.arguments) : r[c]?.arguments;
781
- i.push(
782
- v.builder().name(T).type(c).parameters(M).sequence(y).id(w).build()
774
+ if (!s.ok)
775
+ throw new Error(`HTTP error! status: ${s.status}`);
776
+ let n = await s.json(), r = [];
777
+ if (n.message.tool_calls)
778
+ for (let a in n.message.tool_calls) {
779
+ const i = n.message.tool_calls[a];
780
+ let u = "function", w = i[u]?.name, b = i[u]?.index, M = i.id ?? void 0, N = typeof i[u]?.arguments == "string" ? JSON.parse(i[u]?.arguments) : i[u]?.arguments;
781
+ r.push(
782
+ v.builder().name(w).type(u).parameters(N).sequence(b).id(M).build()
783
783
  );
784
784
  }
785
- return h.builder().toolCalls(i).role(s.message.role).content(s.message.content).thinking(s.message.thinking).build();
785
+ return h.builder().toolCalls(r).role(n.message.role).content(n.message.content).thinking(n.message.thinking).build();
786
786
  }
787
787
  /**
788
788
  * Convert internal tool definitions into the JSON schema format expected by Ollama.
@@ -790,26 +790,26 @@ class L extends q {
790
790
  * @returns Array of tool schema objects formatted for the Ollama API.
791
791
  */
792
792
  _formatTools(e) {
793
- let t = [];
794
- for (let o of e.tools) {
795
- let s = {}, i = [];
796
- for (let r of o.getToolSchema().parameters)
797
- s[r.name] = {}, s[r.name].type = r.type, s[r.name].description = r.description, r.required && i.push(r.name);
793
+ let o = [];
794
+ for (let s of e.tools) {
795
+ let n = {}, r = [];
796
+ for (let i of s.getToolSchema().parameters)
797
+ n[i.name] = {}, n[i.name].type = i.type, n[i.name].description = i.description, i.required && r.push(i.name);
798
798
  let a = {
799
799
  type: "function",
800
800
  function: {
801
- name: o.getName(),
802
- description: o.getDescription(),
801
+ name: s.getName(),
802
+ description: s.getDescription(),
803
803
  parameters: {
804
804
  type: "object",
805
- properties: s,
806
- required: i
805
+ properties: n,
806
+ required: r
807
807
  }
808
808
  }
809
809
  };
810
- t.push(a);
810
+ o.push(a);
811
811
  }
812
- return t;
812
+ return o;
813
813
  }
814
814
  /**
815
815
  * Convert internal message objects into Ollama's chat message shape.
@@ -818,25 +818,25 @@ class L extends q {
818
818
  * @returns Array of serialized messages ready for Ollama.
819
819
  */
820
820
  _formatMessages(e) {
821
- let t = [];
822
- for (let o of e.messages) {
823
- if (o.role == "assistant" || o.role == "system" || o.role == "user") {
824
- t.push(this._formatSingleMessage(o));
821
+ let o = [];
822
+ for (let s of e.messages) {
823
+ if (s.role == "assistant" || s.role == "system" || s.role == "user") {
824
+ o.push(this._formatSingleMessage(s));
825
825
  continue;
826
826
  }
827
- if (o.role == "tool") {
828
- for (const s of o.content)
829
- if (s.getMessageComponentType() == l.ToolResult) {
830
- const i = s;
831
- t.push({
832
- role: o.role,
833
- name: i.toolName,
834
- content: JSON.stringify(i.toolResult)
827
+ if (s.role == "tool") {
828
+ for (const n of s.content)
829
+ if (n.getMessageComponentType() == l.ToolResult) {
830
+ const r = n;
831
+ o.push({
832
+ role: s.role,
833
+ name: r.toolName,
834
+ content: JSON.stringify(r.toolResult)
835
835
  });
836
836
  }
837
837
  }
838
838
  }
839
- return t;
839
+ return o;
840
840
  }
841
841
  /**
842
842
  * Format everything sent by a user or LLM (no tools)
@@ -844,24 +844,24 @@ class L extends q {
844
844
  * @returns Serialized message payload.
845
845
  */
846
846
  _formatSingleMessage(e) {
847
- let t = {
847
+ let o = {
848
848
  role: e.role
849
- }, o, s, i;
849
+ }, s, n, r;
850
850
  for (const a of e.content) {
851
851
  if (a.getMessageComponentType() == l.Content) {
852
- const r = a;
853
- o == null && (o = ""), o += r.get();
852
+ const i = a;
853
+ s == null && (s = ""), s += i.get();
854
854
  }
855
855
  if (a.getMessageComponentType() == l.Thinking) {
856
- const r = a;
857
- i == null && (i = ""), i += r.get();
856
+ const i = a;
857
+ r == null && (r = ""), r += i.get();
858
858
  }
859
859
  if (a.getMessageComponentType() == l.ToolCall) {
860
- const r = a;
861
- s == null && (s = []), s.push(this._formatToolCall(r));
860
+ const i = a;
861
+ n == null && (n = []), n.push(this._formatToolCall(i));
862
862
  }
863
863
  }
864
- return o != null && (t.content = o), s != null && (t.tool_calls = s), i != null && (t.thinking = i), t;
864
+ return s != null && (o.content = s), n != null && (o.tool_calls = n), r != null && (o.thinking = r), o;
865
865
  }
866
866
  /**
867
867
  * Convert a ToolCall into the structure Ollama expects.
@@ -877,27 +877,66 @@ class L extends q {
877
877
  };
878
878
  }
879
879
  }
880
+ function Q(t) {
881
+ return (e) => {
882
+ e.prototype.getName = () => t;
883
+ };
884
+ }
885
+ function V(t) {
886
+ return (e) => {
887
+ Object.defineProperty(e.prototype, "getDescription", {
888
+ value: function() {
889
+ return t;
890
+ },
891
+ enumerable: !0,
892
+ configurable: !0,
893
+ writable: !0
894
+ });
895
+ };
896
+ }
897
+ function H(t) {
898
+ return (e) => {
899
+ e.prototype.tapewormParams == null && (e.prototype.tapewormParams = []), e.prototype.tapewormParamsOutput == null && (e.prototype.tapewormParamsOutput = ""), e.prototype.tapewormParams.push(
900
+ T.builder().name(t.name).description(t.description).required(t.required).type(t.type).build()
901
+ ), e.prototype.getToolSchema = () => new d(
902
+ e.prototype.tapewormParams,
903
+ e.prototype.tapewormParamsOutput
904
+ );
905
+ };
906
+ }
907
+ function K(t) {
908
+ return (e) => {
909
+ e.prototype.tapewormParams == null && (e.prototype.tapewormParams = []), e.prototype.tapewormParamsOutput == null && (e.prototype.tapewormParamsOutput = ""), e.prototype.tapewormParamsOutput = t, e.prototype.getToolSchema = () => new d(
910
+ e.prototype.tapewormParams,
911
+ e.prototype.tapewormParamsOutput
912
+ );
913
+ };
914
+ }
880
915
  export {
881
- O as Agent,
882
- x as Content,
883
- b as Conversation,
884
- C as ConversationManager,
885
- f as DefaultConversationManager,
916
+ j as Agent,
917
+ q as Content,
918
+ x as Conversation,
919
+ P as ConversationManager,
920
+ _ as DefaultConversationManager,
886
921
  h as Message,
887
- N as MessageBuilder,
888
- u as MessageComponent,
922
+ k as MessageBuilder,
923
+ c as MessageComponent,
889
924
  l as MessageComponentType,
890
- q as Model,
925
+ O as Model,
891
926
  E as ModelRequest,
892
- _ as ModelRequestBuilder,
927
+ y as ModelRequestBuilder,
893
928
  L as OllamaModel,
894
- J as Parameter,
929
+ T as Parameter,
895
930
  A as ParameterBuilder,
896
- k as Thinking,
931
+ C as Thinking,
897
932
  F as Tool,
898
933
  v as ToolCall,
899
- P as ToolCallBuilder,
934
+ S as ToolCallBuilder,
935
+ V as ToolDescription,
936
+ Q as ToolName,
937
+ K as ToolOutput,
938
+ H as ToolParameter,
900
939
  m as ToolResult,
901
- B as ToolSchema,
902
- D as ToolSchemaBuilder
940
+ d as ToolSchema,
941
+ J as ToolSchemaBuilder
903
942
  };
@@ -1 +1 @@
1
- (function(i,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(i=typeof globalThis<"u"?globalThis:i||self,u(i.Tapeworm={}))})(this,(function(i){"use strict";class u{messages;manager;constructor(){this.messages=[],this.manager=new g}append(e){this.messages.push(e),this.messages=this.manager.compact(this.messages)}}class v{compact(e){throw new y("No implementation for conversation manager!")}configure(e){throw new y("No implementation for conversation manager!")}}class g extends v{compact(e){return e}configure(e){}}class y extends Error{constructor(e){super(e),this.name="ConversationManagerNotImplemented"}}class c{role;content;constructor(e,t){this.role=e,this.content=t}static builder(){return new M}filter(e){return this.content.filter(t=>t.getMessageComponentType()==e)}}class M{_role;_content;role(e){return this._role=e,this}init(){this._content==null&&(this._content=[])}toolCall(e){return e==null?this:(this.init(),this._content.push(e),this)}toolCalls(e){if(e==null)return this;this.init();for(const t of e)this._content.push(t);return this}toolResult(e){return e==null?this:(this.init(),this._content.push(e),this)}thinking(e){return e==null?this:(this.init(),this._content.push(b.of(e)),this)}content(e){return e==null?this:(this.init(),this._content.push(w.of(e)),this)}build(){if(this._content==null||this._content.length==0)throw new Error("Role-only messages are not supported by Tapeworm.");return new c(this._role,this._content)}}const l={Content:"content",Thinking:"thinking",ToolCall:"toolcall",ToolResult:"toolresult"};class d{getMessageComponentType(){throw new Error("Message components that do not have a message component type are not allowed.")}}class w extends d{text;constructor(e){super(),this.text=e}getMessageComponentType(){return l.Content}get(){return this.text}static of(e){return new this(e)}}class b extends d{thought;constructor(e){super(),this.thought=e}getMessageComponentType(){return l.Thinking}get(){return this.thought}static of(e){return new this(e)}}class p extends d{id;toolName;toolResult;constructor(e,t,o){super(),this.id=e,this.toolName=t,this.toolResult=o}getMessageComponentType(){return l.ToolResult}static of(e,t){return new this(e.id,e.name,t)}}class C{async invoke(e){throw new k("The invoke function for this model was not correctly implemented.")}tokenLimit(){throw new k("The tokenLimit function for this model was not correctly implemented.")}}class N{messages;tools;constructor(e,t){this.messages=e,this.tools=t}static builder(){return new f}}class f{_messages;_tools;messages(e){return this._messages=e,this}tools(e){return this._tools=e,this}build(){if(this._tools==null&&(this._tools=[]),this._messages==null)throw new O("Requests to the model should include content.");return new N(this._messages,this._tools)}}class k extends Error{constructor(e){super(e),this.name="ModelNotImplementedError"}}class O extends Error{constructor(e){super(e),this.name="MessagesNotDefinedError"}}class _ extends d{sequence;name;parameters;type;id;getMessageComponentType(){return l.ToolCall}constructor(e,t,o,s,r){super(),this.sequence=e,this.name=t,this.parameters=o,this.type=s,this.id=r}static builder(){return new q}}class q{_sequence;_name;_parameters;_type;_id;sequence(e){return this._sequence=e,this}name(e){return this._name=e,this}parameters(e){return this._parameters=e,this}type(e){return this._type=e,this}id(e){return e!=null&&(this._id=e),this}build(){return this._sequence==null&&(this._sequence=0),this._id==null&&(this._id=(Math.random()+1).toString(36).slice(2,7)),new _(this._sequence,this._name,this._parameters,this._type,this._id)}}class I extends Error{constructor(e){super(e),this.name="ToolNotFoundError"}}class R{name;systemPrompt;tools;model;conversation;conversationManager;toolNameToIndexMap;callback;constructor(e,t,o,s,r){this.name=e,this.model=o,this.conversationManager=s,this.tools=t,this.callback=r,this.conversationManager.configure(o)}async invoke(e){this.conversation==null&&(this.conversation=new u,this.conversationManager!=null&&(this.conversation.manager=this.conversationManager),this.systemPrompt!=null&&this.conversation.append(c.builder().role("system").content(this.systemPrompt).build())),this.conversation.append(c.builder().role("user").content(e).build());let t=!1;for(;!t;){let o=await this._runQuery();this.callback(o),this.conversation.append(o),t=!0;const s=o.filter(l.ToolCall);if(s!=null&&s.length!=0){t=!1,s.sort((r,h)=>(r.sequence??0)<(h.sequence??0)?-1:1);for(let r of s)await this._runTool(r)}}}async _runQuery(){return await this.model.invoke(new f().messages(this.conversation?.messages).tools(this.tools).build())}async _runTool(e){if(this.generateToolNameToIndexMap(),!(e.name in this.toolNameToIndexMap)){this.conversation.append(c.builder().role("tool").toolResult(p.of(e,new I("Agent does not have a tool with this name."))).build());return}let t=this.tools[this.toolNameToIndexMap[e.name]];try{let o=await t.execute(e.parameters);this.conversation.append(c.builder().role("tool").toolResult(p.of(e,o)).build())}catch(o){this.conversation.append(c.builder().role("tool").toolResult(p.of(e,JSON.stringify(o))).build())}}generateToolNameToIndexMap(){if(this.toolNameToIndexMap==null){this.toolNameToIndexMap={};for(let e=0;e<this.tools.length;e++)this.toolNameToIndexMap[this.tools[e].getName()]=e}}static builder(){return new j}}class j{_name;_systemPrompt;_tools;_model;_conversation;_conversationManager=new g;_toolNameToIndexMap;_callback=e=>x(e);name(e){return this._name=e,this}systemPrompt(e){return this._systemPrompt=e,this}tools(e){return this._tools=e,this}addTool(e){return this._tools==null&&(this._tools=[]),this._tools.push(e),this}model(e){return this._model=e,this}conversationManager(e){return this._conversationManager=e,this}callback(e){return this._callback=e,this}build(){let e=new R(this._name,this._tools,this._model,this._conversationManager,this._callback);return this._conversation!=null&&(e.conversation=this._conversation),this._systemPrompt!=null&&(e.systemPrompt=this._systemPrompt),e}}function x(n){for(const e of n.filter(l.Thinking))console.log("\x1B[90m"+e.get()+"\x1B[0m");for(const e of n.filter(l.Content))console.log(e.get());for(const e of n.filter(l.ToolCall))console.log("\x1B[32mCalling Tool: "+e.name+"\x1B[0m")}class T extends Error{constructor(e){super(e),this.name="ToolNotDefinedError"}}class D{name;description;tool_schema;constructor(){this.name=this.getName(),this.description=this.getDescription(),this.tool_schema=this.getToolSchema()}getName(){throw new T("Tool name not defined.")}getDescription(){throw new T("Tool description not defined.")}getToolSchema(){throw new T("Tool parameter schema not defined.")}execute(e){return null}}class E{parameters;output;constructor(e,t){this.parameters=e,this.output=t}static builder(){return new P}}class P{_parameters;_output;addParameter(e){return this._parameters==null&&(this._parameters=[]),this._parameters.push(e),this}output(e){return this._output=e,this}build(){return this._parameters==null&&(this._parameters=[]),new E(this._parameters,this._output)}}class S{name;description;type;required;constructor(e,t,o,s){this.name=e,this.description=t,this.type=o,this.required=s,this.assertValidType()}assertValidType(){}static builder(){return new B}}class B{_name;_description;_type;_required;name(e){return this._name=e,this}description(e){return this._description=e,this}type(e){return this._type=e,this}required(e){return this._required=e,this}build(){return this._required==null&&(this._required=!1),new S(this._name,this._description,this._type,this._required)}}class A extends C{endpoint;model;options;constructor(e,t,o){super(),this.endpoint=e,this.model=t,this.options=o}async invoke(e){let t={model:this.model,messages:this._formatMessages(e),tools:this._formatTools(e),...this.options},o=await fetch(this.endpoint+"/api/chat",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});if(!o.ok)throw new Error(`HTTP error! status: ${o.status}`);let s=await o.json(),r=[];if(s.message.tool_calls)for(let h in s.message.tool_calls){const a=s.message.tool_calls[h];let m="function",J=a[m]?.name,F=a[m]?.index,L=a.id??void 0,Q=typeof a[m]?.arguments=="string"?JSON.parse(a[m]?.arguments):a[m]?.arguments;r.push(_.builder().name(J).type(m).parameters(Q).sequence(F).id(L).build())}return c.builder().toolCalls(r).role(s.message.role).content(s.message.content).thinking(s.message.thinking).build()}_formatTools(e){let t=[];for(let o of e.tools){let s={},r=[];for(let a of o.getToolSchema().parameters)s[a.name]={},s[a.name].type=a.type,s[a.name].description=a.description,a.required&&r.push(a.name);let h={type:"function",function:{name:o.getName(),description:o.getDescription(),parameters:{type:"object",properties:s,required:r}}};t.push(h)}return t}_formatMessages(e){let t=[];for(let o of e.messages){if(o.role=="assistant"||o.role=="system"||o.role=="user"){t.push(this._formatSingleMessage(o));continue}if(o.role=="tool"){for(const s of o.content)if(s.getMessageComponentType()==l.ToolResult){const r=s;t.push({role:o.role,name:r.toolName,content:JSON.stringify(r.toolResult)})}}}return t}_formatSingleMessage(e){let t={role:e.role},o,s,r;for(const h of e.content){if(h.getMessageComponentType()==l.Content){const a=h;o==null&&(o=""),o+=a.get()}if(h.getMessageComponentType()==l.Thinking){const a=h;r==null&&(r=""),r+=a.get()}if(h.getMessageComponentType()==l.ToolCall){const a=h;s==null&&(s=[]),s.push(this._formatToolCall(a))}}return o!=null&&(t.content=o),s!=null&&(t.tool_calls=s),r!=null&&(t.thinking=r),t}_formatToolCall(e){return{function:{name:e.name,arguments:e.parameters}}}}i.Agent=R,i.Content=w,i.Conversation=u,i.ConversationManager=v,i.DefaultConversationManager=g,i.Message=c,i.MessageBuilder=M,i.MessageComponent=d,i.MessageComponentType=l,i.Model=C,i.ModelRequest=N,i.ModelRequestBuilder=f,i.OllamaModel=A,i.Parameter=S,i.ParameterBuilder=B,i.Thinking=b,i.Tool=D,i.ToolCall=_,i.ToolCallBuilder=q,i.ToolResult=p,i.ToolSchema=E,i.ToolSchemaBuilder=P,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(n,m){typeof exports=="object"&&typeof module<"u"?m(exports):typeof define=="function"&&define.amd?define(["exports"],m):(n=typeof globalThis<"u"?globalThis:n||self,m(n.Tapeworm={}))})(this,(function(n){"use strict";class m{messages;manager;constructor(){this.messages=[],this.manager=new f}append(e){this.messages.push(e),this.messages=this.manager.compact(this.messages)}}class w{compact(e){throw new M("No implementation for conversation manager!")}configure(e){throw new M("No implementation for conversation manager!")}}class f extends w{compact(e){return e}configure(e){}}class M extends Error{constructor(e){super(e),this.name="ConversationManagerNotImplemented"}}class h{role;content;constructor(e,o){this.role=e,this.content=o}static builder(){return new b}filter(e){return this.content.filter(o=>o.getMessageComponentType()==e)}}class b{_role;_content;role(e){return this._role=e,this}init(){this._content==null&&(this._content=[])}toolCall(e){return e==null?this:(this.init(),this._content.push(e),this)}toolCalls(e){if(e==null)return this;this.init();for(const o of e)this._content.push(o);return this}toolResult(e){return e==null?this:(this.init(),this._content.push(e),this)}thinking(e){return e==null?this:(this.init(),this._content.push(C.of(e)),this)}content(e){return e==null?this:(this.init(),this._content.push(N.of(e)),this)}build(){if(this._content==null||this._content.length==0)throw new Error("Role-only messages are not supported by Tapeworm.");return new h(this._role,this._content)}}const l={Content:"content",Thinking:"thinking",ToolCall:"toolcall",ToolResult:"toolresult"};class p{getMessageComponentType(){throw new Error("Message components that do not have a message component type are not allowed.")}}class N extends p{text;constructor(e){super(),this.text=e}getMessageComponentType(){return l.Content}get(){return this.text}static of(e){return new this(e)}}class C extends p{thought;constructor(e){super(),this.thought=e}getMessageComponentType(){return l.Thinking}get(){return this.thought}static of(e){return new this(e)}}class d extends p{id;toolName;toolResult;constructor(e,o,s){super(),this.id=e,this.toolName=o,this.toolResult=s}getMessageComponentType(){return l.ToolResult}static of(e,o){return new this(e.id,e.name,o)}}class P{async invoke(e){throw new k("The invoke function for this model was not correctly implemented.")}tokenLimit(){throw new k("The tokenLimit function for this model was not correctly implemented.")}}class q{messages;tools;constructor(e,o){this.messages=e,this.tools=o}static builder(){return new _}}class _{_messages;_tools;messages(e){return this._messages=e,this}tools(e){return this._tools=e,this}build(){if(this._tools==null&&(this._tools=[]),this._messages==null)throw new B("Requests to the model should include content.");return new q(this._messages,this._tools)}}class k extends Error{constructor(e){super(e),this.name="ModelNotImplementedError"}}class B extends Error{constructor(e){super(e),this.name="MessagesNotDefinedError"}}class T extends p{sequence;name;parameters;type;id;getMessageComponentType(){return l.ToolCall}constructor(e,o,s,r,i){super(),this.sequence=e,this.name=o,this.parameters=s,this.type=r,this.id=i}static builder(){return new O}}class O{_sequence;_name;_parameters;_type;_id;sequence(e){return this._sequence=e,this}name(e){return this._name=e,this}parameters(e){return this._parameters=e,this}type(e){return this._type=e,this}id(e){return e!=null&&(this._id=e),this}build(){return this._sequence==null&&(this._sequence=0),this._id==null&&(this._id=(Math.random()+1).toString(36).slice(2,7)),new T(this._sequence,this._name,this._parameters,this._type,this._id)}}class j extends Error{constructor(e){super(e),this.name="ToolNotFoundError"}}class S{name;systemPrompt;tools;model;conversation;conversationManager;toolNameToIndexMap;callback;constructor(e,o,s,r,i){this.name=e,this.model=s,this.conversationManager=r,this.tools=o,this.callback=i,this.conversationManager.configure(s)}async invoke(e){this.conversation==null&&(this.conversation=new m,this.conversationManager!=null&&(this.conversation.manager=this.conversationManager),this.systemPrompt!=null&&this.conversation.append(h.builder().role("system").content(this.systemPrompt).build())),this.conversation.append(h.builder().role("user").content(e).build());let o=!1;for(;!o;){let s=await this._runQuery();this.callback(s),this.conversation.append(s),o=!0;const r=s.filter(l.ToolCall);if(r!=null&&r.length!=0){o=!1,r.sort((i,u)=>(i.sequence??0)<(u.sequence??0)?-1:1);for(let i of r)await this._runTool(i)}}}async _runQuery(){return await this.model.invoke(new _().messages(this.conversation?.messages).tools(this.tools).build())}async _runTool(e){if(this.generateToolNameToIndexMap(),!(e.name in this.toolNameToIndexMap)){this.conversation.append(h.builder().role("tool").toolResult(d.of(e,new j("Agent does not have a tool with this name."))).build());return}let o=this.tools[this.toolNameToIndexMap[e.name]];try{let s=await o.execute(e.parameters);this.conversation.append(h.builder().role("tool").toolResult(d.of(e,s)).build())}catch(s){this.conversation.append(h.builder().role("tool").toolResult(d.of(e,JSON.stringify(s))).build())}}generateToolNameToIndexMap(){if(this.toolNameToIndexMap==null){this.toolNameToIndexMap={};for(let e=0;e<this.tools.length;e++)this.toolNameToIndexMap[this.tools[e].getName()]=e}}static builder(){return new I}}class I{_name;_systemPrompt;_tools;_model;_conversation;_conversationManager=new f;_toolNameToIndexMap;_callback=e=>D(e);name(e){return this._name=e,this}systemPrompt(e){return this._systemPrompt=e,this}tools(e){return this._tools=e,this}addTool(e){return this._tools==null&&(this._tools=[]),this._tools.push(e),this}model(e){return this._model=e,this}conversationManager(e){return this._conversationManager=e,this}callback(e){return this._callback=e,this}build(){let e=new S(this._name,this._tools,this._model,this._conversationManager,this._callback);return this._conversation!=null&&(e.conversation=this._conversation),this._systemPrompt!=null&&(e.systemPrompt=this._systemPrompt),e}}function D(t){for(const e of t.filter(l.Thinking))console.log("\x1B[90m"+e.get()+"\x1B[0m");for(const e of t.filter(l.Content))console.log(e.get());for(const e of t.filter(l.ToolCall))console.log("\x1B[32mCalling Tool: "+e.name+"\x1B[0m")}class y extends Error{constructor(e){super(e),this.name="ToolNotDefinedError"}}class x{name;description;tool_schema;constructor(){this.name=this.getName(),this.description=this.getDescription(),this.tool_schema=this.getToolSchema()}getName(){throw new y("Tool name not defined.")}getDescription(){throw new y("Tool description not defined.")}getToolSchema(){throw new y("Tool parameter schema not defined.")}execute(e){return null}}class g{parameters;output;constructor(e,o){this.parameters=e,this.output=o}static builder(){return new R}}class R{_parameters;_output;addParameter(e){return this._parameters==null&&(this._parameters=[]),this._parameters.push(e),this}output(e){return this._output=e,this}build(){return this._parameters==null&&(this._parameters=[]),new g(this._parameters,this._output)}}class v{name;description;type;required;constructor(e,o,s,r){this.name=e,this.description=o,this.type=s,this.required=r,this.assertValidType()}assertValidType(){}static builder(){return new E}}class E{_name;_description;_type;_required;name(e){return this._name=e,this}description(e){return this._description=e,this}type(e){return this._type=e,this}required(e){return this._required=e,this}build(){return this._required==null&&(this._required=!1),new v(this._name,this._description,this._type,this._required)}}class A extends P{endpoint;model;options;constructor(e,o,s){super(),this.endpoint=e,this.model=o,this.options=s}async invoke(e){let o={model:this.model,messages:this._formatMessages(e),tools:this._formatTools(e),...this.options},s=await fetch(this.endpoint+"/api/chat",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});if(!s.ok)throw new Error(`HTTP error! status: ${s.status}`);let r=await s.json(),i=[];if(r.message.tool_calls)for(let u in r.message.tool_calls){const a=r.message.tool_calls[u];let c="function",V=a[c]?.name,H=a[c]?.index,K=a.id??void 0,W=typeof a[c]?.arguments=="string"?JSON.parse(a[c]?.arguments):a[c]?.arguments;i.push(T.builder().name(V).type(c).parameters(W).sequence(H).id(K).build())}return h.builder().toolCalls(i).role(r.message.role).content(r.message.content).thinking(r.message.thinking).build()}_formatTools(e){let o=[];for(let s of e.tools){let r={},i=[];for(let a of s.getToolSchema().parameters)r[a.name]={},r[a.name].type=a.type,r[a.name].description=a.description,a.required&&i.push(a.name);let u={type:"function",function:{name:s.getName(),description:s.getDescription(),parameters:{type:"object",properties:r,required:i}}};o.push(u)}return o}_formatMessages(e){let o=[];for(let s of e.messages){if(s.role=="assistant"||s.role=="system"||s.role=="user"){o.push(this._formatSingleMessage(s));continue}if(s.role=="tool"){for(const r of s.content)if(r.getMessageComponentType()==l.ToolResult){const i=r;o.push({role:s.role,name:i.toolName,content:JSON.stringify(i.toolResult)})}}}return o}_formatSingleMessage(e){let o={role:e.role},s,r,i;for(const u of e.content){if(u.getMessageComponentType()==l.Content){const a=u;s==null&&(s=""),s+=a.get()}if(u.getMessageComponentType()==l.Thinking){const a=u;i==null&&(i=""),i+=a.get()}if(u.getMessageComponentType()==l.ToolCall){const a=u;r==null&&(r=[]),r.push(this._formatToolCall(a))}}return s!=null&&(o.content=s),r!=null&&(o.tool_calls=r),i!=null&&(o.thinking=i),o}_formatToolCall(e){return{function:{name:e.name,arguments:e.parameters}}}}function J(t){return e=>{e.prototype.getName=()=>t}}function F(t){return e=>{Object.defineProperty(e.prototype,"getDescription",{value:function(){return t},enumerable:!0,configurable:!0,writable:!0})}}function L(t){return e=>{e.prototype.tapewormParams==null&&(e.prototype.tapewormParams=[]),e.prototype.tapewormParamsOutput==null&&(e.prototype.tapewormParamsOutput=""),e.prototype.tapewormParams.push(v.builder().name(t.name).description(t.description).required(t.required).type(t.type).build()),e.prototype.getToolSchema=()=>new g(e.prototype.tapewormParams,e.prototype.tapewormParamsOutput)}}function Q(t){return e=>{e.prototype.tapewormParams==null&&(e.prototype.tapewormParams=[]),e.prototype.tapewormParamsOutput==null&&(e.prototype.tapewormParamsOutput=""),e.prototype.tapewormParamsOutput=t,e.prototype.getToolSchema=()=>new g(e.prototype.tapewormParams,e.prototype.tapewormParamsOutput)}}n.Agent=S,n.Content=N,n.Conversation=m,n.ConversationManager=w,n.DefaultConversationManager=f,n.Message=h,n.MessageBuilder=b,n.MessageComponent=p,n.MessageComponentType=l,n.Model=P,n.ModelRequest=q,n.ModelRequestBuilder=_,n.OllamaModel=A,n.Parameter=v,n.ParameterBuilder=E,n.Thinking=C,n.Tool=x,n.ToolCall=T,n.ToolCallBuilder=O,n.ToolDescription=F,n.ToolName=J,n.ToolOutput=Q,n.ToolParameter=L,n.ToolResult=d,n.ToolSchema=g,n.ToolSchemaBuilder=R,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atgs/tapeworm",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "main": "dist/tapeworm.cjs.js",
6
6
  "module": "dist/tapeworm.es.js",