@agent-smith/agent 0.3.3 → 0.4.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 CHANGED
@@ -8,7 +8,7 @@ declare class Agent {
8
8
  history: Array<HistoryTurn>;
9
9
  constructor(lm: Lm, name?: string);
10
10
  run(prompt: string, params: InferenceParams, options?: InferenceOptions, template?: PromptTemplate | string): Promise<InferenceResult>;
11
- runAgentNoTemplate(it: number, prompt: string, params: InferenceParams, options?: InferenceOptions): Promise<InferenceResult>;
11
+ runAgentNoTemplate(it: number, prompt: string, params: InferenceParams, options: InferenceOptions | undefined, tpl: PromptTemplate): Promise<InferenceResult>;
12
12
  runAgentWithTemplate(it: number, prompt: string, params: InferenceParams, options: InferenceOptions | undefined, tpl: PromptTemplate): Promise<InferenceResult>;
13
13
  abort(): Promise<void>;
14
14
  }
package/dist/agent.js CHANGED
@@ -21,6 +21,22 @@ class Agent {
21
21
  this.history = options.history;
22
22
  options.history = undefined;
23
23
  }
24
+ if (!template) {
25
+ if (params?.template) {
26
+ tpl = new PromptTemplate(params.template);
27
+ }
28
+ else {
29
+ throw new Error(`A template is required for provider ${this.lm.provider.name}`);
30
+ }
31
+ }
32
+ else {
33
+ if (typeof template == "string") {
34
+ tpl = new PromptTemplate(template);
35
+ }
36
+ else {
37
+ tpl = template;
38
+ }
39
+ }
24
40
  if (this.lm.providerType == "openai") {
25
41
  this.tools = {};
26
42
  if (options?.tools) {
@@ -28,26 +44,10 @@ class Agent {
28
44
  this.tools[t.name] = t;
29
45
  });
30
46
  }
31
- const res = await this.runAgentNoTemplate(1, prompt, params, options);
47
+ const res = await this.runAgentNoTemplate(1, prompt, params, options, tpl);
32
48
  return res;
33
49
  }
34
50
  else {
35
- if (!template) {
36
- if (params?.template) {
37
- tpl = new PromptTemplate(params.template);
38
- }
39
- else {
40
- throw new Error(`A template is required for provider ${this.lm.provider.name}`);
41
- }
42
- }
43
- else {
44
- if (typeof template == "string") {
45
- tpl = new PromptTemplate(template);
46
- }
47
- else {
48
- tpl = template;
49
- }
50
- }
51
51
  this.tools = {};
52
52
  if (options?.tools) {
53
53
  options.tools.forEach(t => {
@@ -59,13 +59,33 @@ class Agent {
59
59
  return res;
60
60
  }
61
61
  }
62
- async runAgentNoTemplate(it, prompt, params, options = {}) {
62
+ async runAgentNoTemplate(it, prompt, params, options = {}, tpl) {
63
63
  options.history = this.history;
64
64
  const res = await this.lm.infer(prompt, params, options);
65
65
  if (it == 1) {
66
66
  this.history.push({ user: prompt });
67
67
  }
68
68
  let _res = res;
69
+ if (_res.text) {
70
+ if (tpl.tags?.think?.start) {
71
+ const { think, finalAnswer } = splitThinking(res.text, tpl.tags.think.start, tpl.tags.think.end);
72
+ if (think.length > 0) {
73
+ if (options?.onThink) {
74
+ options.onThink(think);
75
+ }
76
+ }
77
+ if (finalAnswer) {
78
+ if (options?.onAssistant) {
79
+ options.onAssistant(finalAnswer);
80
+ }
81
+ }
82
+ }
83
+ else {
84
+ if (options?.onAssistant) {
85
+ options.onAssistant(res.text);
86
+ }
87
+ }
88
+ }
69
89
  if (res?.toolCalls) {
70
90
  if (options?.onToolsTurnStart) {
71
91
  options.onToolsTurnStart(res.toolCalls);
@@ -120,7 +140,7 @@ class Agent {
120
140
  if (options?.onTurnEnd) {
121
141
  options.onTurnEnd(this.history[this.history.length - 1]);
122
142
  }
123
- _res = await this.runAgentNoTemplate(nit, "", params, options);
143
+ _res = await this.runAgentNoTemplate(nit, "", params, options, tpl);
124
144
  }
125
145
  else {
126
146
  this.history.push({ assistant: res.text });
package/dist/utils.js CHANGED
@@ -24,13 +24,14 @@ function extractBetweenTags(text, startTag, endTag) {
24
24
  function splitThinking(text, startTag, endTag) {
25
25
  let think = "";
26
26
  let answer = "";
27
- const st = text.split(endTag);
28
- if (st.length > 1) {
29
- think = extractBetweenTags(text, startTag, endTag).trim();
30
- answer = st[1].trim();
27
+ text = text.trim();
28
+ if (!text.includes(startTag)) {
29
+ return { think: "", finalAnswer: text };
31
30
  }
32
- else {
33
- answer = text;
31
+ think = extractBetweenTags(text, startTag, endTag).trim();
32
+ if (!text.endsWith(endTag)) {
33
+ const st = text.split(endTag);
34
+ answer = st[1].trim();
34
35
  }
35
36
  return { think: think, finalAnswer: answer };
36
37
  }
package/package.json CHANGED
@@ -5,26 +5,26 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/synw/agent-smith.git"
7
7
  },
8
- "version": "0.3.3",
8
+ "version": "0.4.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
- "@locallm/api": "^0.7.3",
15
- "modprompt": "^0.14.2",
14
+ "@locallm/api": "^0.8.0",
15
+ "modprompt": "^0.14.3",
16
16
  "restmix": "^0.6.1"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@locallm/types": "^0.7.1",
20
20
  "@rollup/plugin-node-resolve": "^16.0.3",
21
21
  "@rollup/plugin-typescript": "^12.3.0",
22
- "@types/node": "^25.3.0",
23
- "openai": "^6.24.0",
24
- "rollup": "^4.59.0",
22
+ "@types/node": "^25.5.2",
23
+ "openai": "^6.33.0",
24
+ "rollup": "^4.60.1",
25
25
  "ts-node": "^10.9.2",
26
26
  "tslib": "2.8.1",
27
- "typescript": "^5.9.3"
27
+ "typescript": "^6.0.2"
28
28
  },
29
29
  "type": "module",
30
30
  "main": "./dist/main.js",