@agent-smith/agent 0.0.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.
- package/dist/agent.d.ts +16 -0
- package/dist/agent.js +143 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +2 -0
- package/package.json +39 -0
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Lm } from "@locallm/api";
|
|
2
|
+
import { InferenceParams, ToolSpec, HistoryTurn, InferenceOptions, InferenceResult } from "@locallm/types";
|
|
3
|
+
import { PromptTemplate } from 'modprompt';
|
|
4
|
+
declare class Agent {
|
|
5
|
+
lm: Lm;
|
|
6
|
+
tools: Record<string, ToolSpec>;
|
|
7
|
+
history: Array<HistoryTurn>;
|
|
8
|
+
constructor(lm: Lm);
|
|
9
|
+
run(prompt: string, params: InferenceParams, options?: InferenceOptions, template?: PromptTemplate): Promise<InferenceResult>;
|
|
10
|
+
runAgentNoTemplate(it: number, prompt: string, params: InferenceParams, options?: InferenceOptions): Promise<InferenceResult>;
|
|
11
|
+
runAgentWithTemplate(it: number, prompt: string, params: InferenceParams, options: InferenceOptions | undefined, tpl: PromptTemplate): Promise<{
|
|
12
|
+
inferenceResult: InferenceResult;
|
|
13
|
+
template: PromptTemplate;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
export { Agent, };
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
class Agent {
|
|
2
|
+
lm;
|
|
3
|
+
tools = {};
|
|
4
|
+
history = [];
|
|
5
|
+
constructor(lm) {
|
|
6
|
+
this.lm = lm;
|
|
7
|
+
}
|
|
8
|
+
async run(prompt, params, options = {}, template) {
|
|
9
|
+
this.tools = {};
|
|
10
|
+
if (options?.tools) {
|
|
11
|
+
options.tools.forEach(t => {
|
|
12
|
+
this.tools[t.name] = t;
|
|
13
|
+
if (template) {
|
|
14
|
+
template = template.addTool(t);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
if (this.lm.providerType == "openai") {
|
|
19
|
+
return await this.runAgentNoTemplate(1, prompt, params, options);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
if (!template) {
|
|
23
|
+
throw new Error(`A template is required for provider ${this.lm.provider.name}`);
|
|
24
|
+
}
|
|
25
|
+
return (await this.runAgentWithTemplate(1, prompt, params, options, template)).inferenceResult;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async runAgentNoTemplate(it, prompt, params, options = {}) {
|
|
29
|
+
const res = await this.lm.infer(prompt, params, options);
|
|
30
|
+
this.history.push({ user: prompt });
|
|
31
|
+
let _res = res;
|
|
32
|
+
if (res?.toolCalls) {
|
|
33
|
+
const toolsResults = new Array();
|
|
34
|
+
const toolNames = Object.keys(this.tools);
|
|
35
|
+
for (const tc of res.toolCalls) {
|
|
36
|
+
if (!toolNames.includes(tc.name)) {
|
|
37
|
+
throw new Error(`Inexistant tool ${tc.name} called (available tools: ${toolNames})`);
|
|
38
|
+
}
|
|
39
|
+
const tool = this.tools[tc.name];
|
|
40
|
+
let canRun = true;
|
|
41
|
+
if (tool?.canRun) {
|
|
42
|
+
canRun = await tool.canRun(tool);
|
|
43
|
+
}
|
|
44
|
+
if (canRun) {
|
|
45
|
+
const toolCallResult = await tool.execute(tc.arguments);
|
|
46
|
+
if (options?.debug || options?.verbose) {
|
|
47
|
+
console.log("[x] Executed tool", tool.name + ":", toolCallResult);
|
|
48
|
+
}
|
|
49
|
+
toolsResults.push({ call: tc, response: toolCallResult });
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
if (options?.debug || options?.verbose) {
|
|
53
|
+
console.log("[-] Tool", tool.name, "execution refused");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
this.history.push({ tools: toolsResults });
|
|
58
|
+
if (options?.tools) {
|
|
59
|
+
options.tools = Object.values(this.tools);
|
|
60
|
+
}
|
|
61
|
+
const nit = it + 1;
|
|
62
|
+
if (nit > 1 && options?.debug) {
|
|
63
|
+
options.debug = false;
|
|
64
|
+
options.verbose = true;
|
|
65
|
+
}
|
|
66
|
+
options.history = this.history;
|
|
67
|
+
_res = await this.runAgentNoTemplate(nit, " ", params, options);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
this.history.push({ assistant: res.text });
|
|
71
|
+
}
|
|
72
|
+
return _res;
|
|
73
|
+
}
|
|
74
|
+
async runAgentWithTemplate(it, prompt, params, options = {}, tpl) {
|
|
75
|
+
let res = await this.lm.infer(tpl.prompt(prompt), params, options);
|
|
76
|
+
const toolResponseStart = tpl.toolsDef?.response.split("{tools_response}")[0];
|
|
77
|
+
const toolCallStart = tpl.toolsDef?.response.split("{tool}")[0];
|
|
78
|
+
if (!toolCallStart || !toolResponseStart) {
|
|
79
|
+
throw new Error(`Tool definition malformed in template ${tpl.name}`);
|
|
80
|
+
}
|
|
81
|
+
const { isToolCall, toolsCall, error } = tpl.processAnswer(res.text);
|
|
82
|
+
if (error) {
|
|
83
|
+
throw new Error(`error processing tool call answer:\n, ${error}`);
|
|
84
|
+
}
|
|
85
|
+
console.log("\nProcessed answer", isToolCall, toolsCall, error);
|
|
86
|
+
const toolResults = new Array();
|
|
87
|
+
if (isToolCall) {
|
|
88
|
+
for (const toolCall of toolsCall) {
|
|
89
|
+
if (!("name" in toolCall)) {
|
|
90
|
+
throw new Error(`tool call does not includes a name in it's response:\n${toolCall}`);
|
|
91
|
+
}
|
|
92
|
+
if (!("arguments" in toolCall)) {
|
|
93
|
+
toolCall.arguments = {};
|
|
94
|
+
}
|
|
95
|
+
const tool = toolCall.name in this.tools ? this.tools[toolCall.name] : null;
|
|
96
|
+
if (!tool) {
|
|
97
|
+
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, " "));
|
|
98
|
+
throw new Error(buf.join("\n"));
|
|
99
|
+
}
|
|
100
|
+
if (options?.debug === true) {
|
|
101
|
+
console.log("\n* Calling tool", tool.name + ":", toolCall.arguments);
|
|
102
|
+
}
|
|
103
|
+
const toolResp = await tool.execute(toolCall.arguments);
|
|
104
|
+
if (options?.debug || options?.verbose) {
|
|
105
|
+
console.log("[x] Executed tool", tool.name + ":", toolResp);
|
|
106
|
+
}
|
|
107
|
+
toolResults.push({
|
|
108
|
+
call: toolCall,
|
|
109
|
+
response: toolResp
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
if (it == 1) {
|
|
113
|
+
tpl.pushToHistory({
|
|
114
|
+
user: prompt.replace("{prompt}", prompt),
|
|
115
|
+
assistant: res.text,
|
|
116
|
+
tools: toolResults,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
tpl.pushToHistory({
|
|
121
|
+
assistant: res.text,
|
|
122
|
+
tools: toolResults,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return await this.runAgentWithTemplate(it + 1, prompt, params, options, tpl);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
if (it == 1) {
|
|
129
|
+
tpl.pushToHistory({
|
|
130
|
+
user: prompt.replace("{prompt}", prompt),
|
|
131
|
+
assistant: res.text,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
tpl.pushToHistory({
|
|
136
|
+
assistant: res.text,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return { inferenceResult: res, template: tpl };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
export { Agent, };
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agent-smith/agent",
|
|
3
|
+
"description": "Agent Smith: agent runtime",
|
|
4
|
+
"repository": "https://github.com/synw/agent-smith",
|
|
5
|
+
"version": "0.0.1",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"buildrl": "rm -rf dist/* && rollup -c",
|
|
8
|
+
"build": "rm -rf dist/* && tsc"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@locallm/api": "^0.4.2",
|
|
12
|
+
"modprompt": "^0.12.0"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@locallm/types": "^0.4.2",
|
|
16
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
17
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
18
|
+
"@types/node": "^24.3.1",
|
|
19
|
+
"rollup": "^4.50.1",
|
|
20
|
+
"ts-node": "^10.9.2",
|
|
21
|
+
"tslib": "2.8.1",
|
|
22
|
+
"typescript": "^5.9.2"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/main.js",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": "./dist/main.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public",
|
|
36
|
+
"registry": "https://registry.npmjs.org/"
|
|
37
|
+
},
|
|
38
|
+
"license": "MIT"
|
|
39
|
+
}
|