@agent-smith/agent 0.2.3 → 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 +50 -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}`);
@@ -158,10 +178,14 @@ class Agent {
158
178
  if (options?.debug || options?.verbose) {
159
179
  console.log("[-] Tool", tool.name, "execution refused");
160
180
  }
181
+ const toolResp = "tool execution denied";
161
182
  toolResults.push({
162
183
  call: toolCall,
163
- response: "tool execution denied"
184
+ response: toolResp,
164
185
  });
186
+ if (options?.onToolCallEnd) {
187
+ options.onToolCallEnd(toolCall.id, toolResp);
188
+ }
165
189
  }
166
190
  else {
167
191
  if (options?.onToolCall) {
@@ -179,22 +203,31 @@ class Agent {
179
203
  response: toolResp
180
204
  });
181
205
  if (options?.onToolCallEnd) {
182
- options.onToolCallEnd(toolResp);
206
+ options.onToolCallEnd(toolCall.id, toolResp);
183
207
  }
184
208
  }
185
209
  }
210
+ if (options?.onToolsTurnEnd) {
211
+ options.onToolsTurnEnd(toolResults);
212
+ }
186
213
  if (it == 1) {
187
- this.history.push({
214
+ const t = {
188
215
  user: prompt,
189
- assistant: res.text,
190
216
  tools: toolResults,
191
- });
217
+ };
218
+ if (assistant) {
219
+ t.assistant = assistant;
220
+ }
221
+ this.history.push(t);
192
222
  }
193
223
  else {
194
- this.history.push({
195
- assistant: res.text,
224
+ const t = {
196
225
  tools: toolResults,
197
- });
226
+ };
227
+ if (assistant) {
228
+ t.assistant = assistant;
229
+ }
230
+ this.history.push(t);
198
231
  }
199
232
  if (options?.isToolsRouter && isToolCall) {
200
233
  const fres = {
@@ -205,6 +238,9 @@ class Agent {
205
238
  };
206
239
  return fres;
207
240
  }
241
+ if (options?.onTurnEnd) {
242
+ options.onTurnEnd(this.history[this.history.length - 1]);
243
+ }
208
244
  return await this.runAgentWithTemplate(it + 1, prompt, params, options, tpl);
209
245
  }
210
246
  else {
@@ -232,6 +268,9 @@ class Agent {
232
268
  turn.assistant = final;
233
269
  this.history.push(turn);
234
270
  }
271
+ if (options?.onTurnEnd) {
272
+ options.onTurnEnd(this.history[this.history.length - 1]);
273
+ }
235
274
  return res;
236
275
  }
237
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.3",
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"