@agent-smith/agent 0.2.3 → 0.3.1

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 +63 -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,44 @@ 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
+ }
213
+ let thinking = "";
214
+ let final = "";
215
+ if (tpl?.tags?.think) {
216
+ const { think, finalAnswer } = splitThinking(res.text, tpl.tags.think.start, tpl.tags.think.end);
217
+ thinking = think;
218
+ final = finalAnswer;
219
+ }
186
220
  if (it == 1) {
187
- this.history.push({
221
+ const t = {
188
222
  user: prompt,
189
- assistant: res.text,
190
223
  tools: toolResults,
191
- });
224
+ };
225
+ if (assistant) {
226
+ t.assistant = final;
227
+ }
228
+ if (thinking.length > 0) {
229
+ t.think = thinking;
230
+ }
231
+ this.history.push(t);
192
232
  }
193
233
  else {
194
- this.history.push({
195
- assistant: res.text,
234
+ const t = {
196
235
  tools: toolResults,
197
- });
236
+ };
237
+ if (assistant) {
238
+ t.assistant = final;
239
+ }
240
+ if (thinking.length > 0) {
241
+ t.think = thinking;
242
+ }
243
+ this.history.push(t);
198
244
  }
199
245
  if (options?.isToolsRouter && isToolCall) {
200
246
  const fres = {
@@ -205,6 +251,9 @@ class Agent {
205
251
  };
206
252
  return fres;
207
253
  }
254
+ if (options?.onTurnEnd) {
255
+ options.onTurnEnd(this.history[this.history.length - 1]);
256
+ }
208
257
  return await this.runAgentWithTemplate(it + 1, prompt, params, options, tpl);
209
258
  }
210
259
  else {
@@ -232,6 +281,9 @@ class Agent {
232
281
  turn.assistant = final;
233
282
  this.history.push(turn);
234
283
  }
284
+ if (options?.onTurnEnd) {
285
+ options.onTurnEnd(this.history[this.history.length - 1]);
286
+ }
235
287
  return res;
236
288
  }
237
289
  }
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.1",
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.24.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"