@agent-smith/agent 0.1.5 → 0.1.7

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 +54 -24
  2. package/package.json +7 -7
package/dist/agent.js CHANGED
@@ -13,6 +13,10 @@ class Agent {
13
13
  console.log("Agent inference params:", params);
14
14
  console.log("Agent options:", options);
15
15
  }
16
+ if (options?.history) {
17
+ this.history = options.history;
18
+ options.history = undefined;
19
+ }
16
20
  if (this.lm.providerType == "openai") {
17
21
  this.tools = {};
18
22
  if (options?.tools) {
@@ -52,8 +56,11 @@ class Agent {
52
56
  }
53
57
  }
54
58
  async runAgentNoTemplate(it, prompt, params, options = {}) {
59
+ options.history = this.history;
55
60
  const res = await this.lm.infer(prompt, params, options);
56
- this.history.push({ user: prompt });
61
+ if (it == 1) {
62
+ this.history.push({ user: prompt });
63
+ }
57
64
  let _res = res;
58
65
  if (res?.toolCalls) {
59
66
  const toolsResults = new Array();
@@ -68,11 +75,17 @@ class Agent {
68
75
  canRun = await tool.canRun(tool);
69
76
  }
70
77
  if (canRun) {
78
+ if (options?.onToolCall) {
79
+ options.onToolCall(tc);
80
+ }
71
81
  const toolCallResult = await tool.execute(tc.arguments);
72
82
  if (options?.debug || options?.verbose) {
73
83
  console.log("[x] Executed tool", tool.name + ":", toolCallResult);
74
84
  }
75
- toolsResults.push({ call: tc, response: toolCallResult });
85
+ toolsResults.push({ call: tc, response: JSON.stringify(toolCallResult) });
86
+ if (options?.onToolCallEnd) {
87
+ options.onToolCallEnd(toolCallResult);
88
+ }
76
89
  }
77
90
  else {
78
91
  if (options?.debug || options?.verbose) {
@@ -85,12 +98,7 @@ class Agent {
85
98
  options.tools = Object.values(this.tools);
86
99
  }
87
100
  const nit = it + 1;
88
- if (nit > 1 && options?.debug) {
89
- options.debug = false;
90
- options.verbose = true;
91
- }
92
- options.history = this.history;
93
- _res = await this.runAgentNoTemplate(nit, " ", params, options);
101
+ _res = await this.runAgentNoTemplate(nit, "", params, options);
94
102
  }
95
103
  else {
96
104
  this.history.push({ assistant: res.text });
@@ -104,7 +112,9 @@ class Agent {
104
112
  }
105
113
  await this.lm.loadModel(params.model.name, params.model.ctx);
106
114
  }
107
- let res = await this.lm.infer(tpl.prompt(prompt), params, options);
115
+ tpl.history = this.history;
116
+ const pr = tpl.prompt(prompt);
117
+ let res = await this.lm.infer(pr, params, options);
108
118
  if (typeof params?.model == "string") {
109
119
  params.model = { name: params.model };
110
120
  }
@@ -126,27 +136,48 @@ class Agent {
126
136
  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, " "));
127
137
  throw new Error(buf.join("\n"));
128
138
  }
129
- if (options?.debug === true) {
130
- console.log("\n* Calling tool", tool.name + ":", toolCall.arguments);
139
+ let canRun = true;
140
+ if (tool?.canRun) {
141
+ canRun = await tool.canRun(tool);
142
+ }
143
+ if (!canRun) {
144
+ if (options?.debug || options?.verbose) {
145
+ console.log("[-] Tool", tool.name, "execution refused");
146
+ }
147
+ toolResults.push({
148
+ call: toolCall,
149
+ response: "tool execution denied"
150
+ });
131
151
  }
132
- const toolResp = await tool.execute(toolCall.arguments);
133
- if (options?.debug || options?.verbose) {
134
- console.log("[x] Executed tool", tool.name + ":", toolResp);
152
+ else {
153
+ if (options?.onToolCall) {
154
+ options.onToolCall(toolCall);
155
+ }
156
+ if (options?.debug === true) {
157
+ console.log("\n=> Calling tool", tool.name + ":", toolCall.arguments);
158
+ }
159
+ const toolResp = await tool.execute(toolCall.arguments);
160
+ if (options?.debug) {
161
+ console.log("[x] Executed tool", tool.name + ":", toolResp);
162
+ }
163
+ toolResults.push({
164
+ call: toolCall,
165
+ response: toolResp
166
+ });
167
+ if (options?.onToolCallEnd) {
168
+ options.onToolCallEnd(toolResp);
169
+ }
135
170
  }
136
- toolResults.push({
137
- call: toolCall,
138
- response: toolResp
139
- });
140
171
  }
141
172
  if (it == 1) {
142
- tpl.pushToHistory({
143
- user: prompt.replace("{prompt}", prompt),
173
+ this.history.push({
174
+ user: prompt,
144
175
  assistant: res.text,
145
176
  tools: toolResults,
146
177
  });
147
178
  }
148
179
  else {
149
- tpl.pushToHistory({
180
+ this.history.push({
150
181
  assistant: res.text,
151
182
  tools: toolResults,
152
183
  });
@@ -168,7 +199,7 @@ class Agent {
168
199
  }
169
200
  turn.user = prompt.replace("{prompt}", prompt);
170
201
  turn.assistant = final;
171
- tpl.pushToHistory(turn);
202
+ this.history.push(turn);
172
203
  }
173
204
  else {
174
205
  const turn = {};
@@ -176,9 +207,8 @@ class Agent {
176
207
  turn.think = thinking;
177
208
  }
178
209
  turn.assistant = final;
179
- tpl.pushToHistory(turn);
210
+ this.history.push(turn);
180
211
  }
181
- this.history = tpl.history;
182
212
  return res;
183
213
  }
184
214
  }
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.1.5",
8
+ "version": "0.1.7",
9
9
  "scripts": {
10
10
  "buildrl": "rm -rf dist/* && rollup -c",
11
11
  "build": "rm -rf dist/* && tsc"
12
12
  },
13
13
  "dependencies": {
14
- "@locallm/api": "^0.7.2",
14
+ "@locallm/api": "^0.7.3",
15
15
  "modprompt": "^0.12.6",
16
16
  "restmix": "^0.6.1"
17
17
  },
18
18
  "devDependencies": {
19
- "@locallm/types": "^0.6.4",
19
+ "@locallm/types": "file:/home/ggg/dev/js/locallm/packages/types",
20
20
  "@rollup/plugin-node-resolve": "^16.0.3",
21
21
  "@rollup/plugin-typescript": "^12.3.0",
22
- "@types/node": "^25.0.1",
23
- "openai": "^6.10.0",
24
- "rollup": "^4.53.3",
22
+ "@types/node": "^25.0.3",
23
+ "openai": "^6.15.0",
24
+ "rollup": "^4.54.0",
25
25
  "ts-node": "^10.9.2",
26
26
  "tslib": "2.8.1",
27
27
  "typescript": "^5.9.3"
@@ -41,4 +41,4 @@
41
41
  "registry": "https://registry.npmjs.org/"
42
42
  },
43
43
  "license": "MIT"
44
- }
44
+ }