@agent-smith/agent 0.2.2 → 0.3.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.
Files changed (2) hide show
  1. package/dist/agent.js +51 -11
  2. package/package.json +6 -6
package/dist/agent.js CHANGED
@@ -67,6 +67,9 @@ class Agent {
67
67
  }
68
68
  let _res = res;
69
69
  if (res?.toolCalls) {
70
+ if (options?.onToolsTurnStart) {
71
+ options.onToolsTurnStart(res.toolCalls);
72
+ }
70
73
  const toolsResults = new Array();
71
74
  const toolNames = Object.keys(this.tools);
72
75
  for (const tc of res.toolCalls) {
@@ -88,7 +91,7 @@ class Agent {
88
91
  }
89
92
  toolsResults.push({ call: tc, response: JSON.stringify(toolCallResult) });
90
93
  if (options?.onToolCallEnd) {
91
- options.onToolCallEnd(toolCallResult);
94
+ options.onToolCallEnd(tc.id, toolCallResult);
92
95
  }
93
96
  }
94
97
  else {
@@ -97,6 +100,9 @@ class Agent {
97
100
  }
98
101
  }
99
102
  }
103
+ if (options?.onToolsTurnEnd) {
104
+ options.onToolsTurnEnd(toolsResults);
105
+ }
100
106
  this.history.push({ tools: toolsResults });
101
107
  if (options?.isToolsRouter) {
102
108
  const fres = {
@@ -111,10 +117,16 @@ class Agent {
111
117
  if (options?.tools) {
112
118
  options.tools = Object.values(this.tools);
113
119
  }
120
+ if (options?.onTurnEnd) {
121
+ options.onTurnEnd(this.history[this.history.length - 1]);
122
+ }
114
123
  _res = await this.runAgentNoTemplate(nit, "", params, options);
115
124
  }
116
125
  else {
117
126
  this.history.push({ assistant: res.text });
127
+ if (options?.onTurnEnd) {
128
+ options.onTurnEnd(this.history[this.history.length - 1]);
129
+ }
118
130
  }
119
131
  return _res;
120
132
  }
@@ -131,12 +143,20 @@ class Agent {
131
143
  if (typeof params?.model == "string") {
132
144
  params.model = { name: params.model };
133
145
  }
134
- const { isToolCall, toolsCall, error } = tpl.processAnswer(res.text);
146
+ const { isToolCall, toolsCall, assistant, error } = tpl.processAnswer(res.text);
135
147
  if (error) {
136
- throw new Error(`error processing tool call answer:\n, ${error}`);
148
+ throw new Error(`error processing model answer:\n, ${error}`);
149
+ }
150
+ if (assistant) {
151
+ if (options?.onAssistant) {
152
+ options.onAssistant(assistant);
153
+ }
137
154
  }
138
155
  const toolResults = new Array();
139
156
  if (isToolCall) {
157
+ if (options?.onToolsTurnStart) {
158
+ options.onToolsTurnStart(toolsCall);
159
+ }
140
160
  for (const toolCall of toolsCall) {
141
161
  if (!("name" in toolCall)) {
142
162
  throw new Error(`tool call does not includes a name in it's response:\n${toolCall}`);
@@ -144,6 +164,7 @@ class Agent {
144
164
  if (!("arguments" in toolCall)) {
145
165
  toolCall.arguments = {};
146
166
  }
167
+ toolCall.name = toolCall.name.trim();
147
168
  const tool = toolCall.name in this.tools ? this.tools[toolCall.name] : null;
148
169
  if (!tool) {
149
170
  const buf = new Array(`wrong tool call ${toolCall.name} from the model:`, JSON.stringify(toolCall, null, " "), `It does not exist in the tools list:. Available tools:`, JSON.stringify(Object.keys(this.tools), null, " "));
@@ -157,10 +178,14 @@ class Agent {
157
178
  if (options?.debug || options?.verbose) {
158
179
  console.log("[-] Tool", tool.name, "execution refused");
159
180
  }
181
+ const toolResp = "tool execution denied";
160
182
  toolResults.push({
161
183
  call: toolCall,
162
- response: "tool execution denied"
184
+ response: toolResp,
163
185
  });
186
+ if (options?.onToolCallEnd) {
187
+ options.onToolCallEnd(toolCall.id, toolResp);
188
+ }
164
189
  }
165
190
  else {
166
191
  if (options?.onToolCall) {
@@ -178,22 +203,31 @@ class Agent {
178
203
  response: toolResp
179
204
  });
180
205
  if (options?.onToolCallEnd) {
181
- options.onToolCallEnd(toolResp);
206
+ options.onToolCallEnd(toolCall.id, toolResp);
182
207
  }
183
208
  }
184
209
  }
210
+ if (options?.onToolsTurnEnd) {
211
+ options.onToolsTurnEnd(toolResults);
212
+ }
185
213
  if (it == 1) {
186
- this.history.push({
214
+ const t = {
187
215
  user: prompt,
188
- assistant: res.text,
189
216
  tools: toolResults,
190
- });
217
+ };
218
+ if (assistant) {
219
+ t.assistant = assistant;
220
+ }
221
+ this.history.push(t);
191
222
  }
192
223
  else {
193
- this.history.push({
194
- assistant: res.text,
224
+ const t = {
195
225
  tools: toolResults,
196
- });
226
+ };
227
+ if (assistant) {
228
+ t.assistant = assistant;
229
+ }
230
+ this.history.push(t);
197
231
  }
198
232
  if (options?.isToolsRouter && isToolCall) {
199
233
  const fres = {
@@ -204,6 +238,9 @@ class Agent {
204
238
  };
205
239
  return fres;
206
240
  }
241
+ if (options?.onTurnEnd) {
242
+ options.onTurnEnd(this.history[this.history.length - 1]);
243
+ }
207
244
  return await this.runAgentWithTemplate(it + 1, prompt, params, options, tpl);
208
245
  }
209
246
  else {
@@ -231,6 +268,9 @@ class Agent {
231
268
  turn.assistant = final;
232
269
  this.history.push(turn);
233
270
  }
271
+ if (options?.onTurnEnd) {
272
+ options.onTurnEnd(this.history[this.history.length - 1]);
273
+ }
234
274
  return res;
235
275
  }
236
276
  }
package/package.json CHANGED
@@ -5,23 +5,23 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/synw/agent-smith.git"
7
7
  },
8
- "version": "0.2.2",
8
+ "version": "0.3.0",
9
9
  "scripts": {
10
10
  "buildrl": "rm -rf dist/* && rollup -c",
11
11
  "build": "rm -rf dist/* && tsc"
12
12
  },
13
13
  "dependencies": {
14
14
  "@locallm/api": "^0.7.3",
15
- "modprompt": "0.13.1",
15
+ "modprompt": "^0.14.0",
16
16
  "restmix": "^0.6.1"
17
17
  },
18
18
  "devDependencies": {
19
- "@locallm/types": "^0.6.7",
19
+ "@locallm/types": "^0.7.0",
20
20
  "@rollup/plugin-node-resolve": "^16.0.3",
21
21
  "@rollup/plugin-typescript": "^12.3.0",
22
- "@types/node": "^25.0.10",
23
- "openai": "^6.16.0",
24
- "rollup": "^4.56.0",
22
+ "@types/node": "^25.3.0",
23
+ "openai": "^6.22.0",
24
+ "rollup": "^4.59.0",
25
25
  "ts-node": "^10.9.2",
26
26
  "tslib": "2.8.1",
27
27
  "typescript": "^5.9.3"