@arki-moe/agent-ts 2.2.2 → 2.2.3

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/README.md CHANGED
@@ -53,7 +53,7 @@ When `apiKey` is not provided in config, adapters read from the corresponding en
53
53
  - `Agent(adapterName, config)` - Create Agent
54
54
  - `agent.context` - Public property, complete conversation history
55
55
  - `agent.registerTool(tool)` - Register tool
56
- - `agent.run(message)` - Execute tool chain automatically, returns all new `Message[]`
56
+ - `agent.run(message, options?)` - Execute tool chain automatically, returns all new `Message[]`
57
57
  - `agent.fork()` - Create a new agent with a copied context
58
58
 
59
59
  ### Config
@@ -68,7 +68,7 @@ When `apiKey` is not provided in config, adapters read from the corresponding en
68
68
  | `onToolCall` | `(message, args) => boolean \| void \| Promise<boolean \| void>` | Called before each tool execution; return `false` to skip tool execution and `onToolResult` |
69
69
  | `onToolResult` | `(message) => void \| Promise<void>` | Called after each tool execution (`message.role === Role.ToolResult`) |
70
70
 
71
- `agent.run` always appends new messages to `agent.context`. Multiple tool calls in a single model response are executed in parallel.
71
+ `agent.run` always appends new messages to `agent.context`. Set `options.once = true` to avoid persisting the user message (useful for one-shot hints). Multiple tool calls in a single model response are executed in parallel.
72
72
 
73
73
  `onToolCall` receives parsed JSON args and can mutate them before execution. Returning `false` skips the tool call and does not emit a `ToolResult` message.
74
74
 
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import type { AgentConfig, Context, Message, Tool } from "./types";
1
+ import type { AgentConfig, Context, Message, RunOptions, Tool } from "./types";
2
2
  export { openaiAdapter } from "./adapter/openai";
3
3
  export { openrouterAdapter } from "./adapter/openrouter";
4
- export type { Adapter, AgentConfig, Context, Message, Tool } from "./types";
4
+ export type { Adapter, AgentConfig, Context, Message, RunOptions, Tool } from "./types";
5
5
  export { Role } from "./types";
6
6
  export declare class Agent {
7
7
  context: Context;
@@ -14,6 +14,6 @@ export declare class Agent {
14
14
  private onToolResult?;
15
15
  constructor(adapterName: string, config: AgentConfig);
16
16
  registerTool(tool: Tool): void;
17
- run(message: string): Promise<Message[]>;
17
+ run(message: string, options?: RunOptions): Promise<Message[]>;
18
18
  fork(): Agent;
19
19
  }
package/dist/index.js CHANGED
@@ -28,15 +28,31 @@ class Agent {
28
28
  registerTool(tool) {
29
29
  this.tools.push(tool);
30
30
  }
31
- async run(message) {
32
- this.context.push({ role: types_1.Role.User, content: message });
31
+ async run(message, options = {}) {
32
+ const once = options.once ?? false;
33
33
  const all = [];
34
- let msgs = await this.adapter(this.config, this.context, this.tools);
35
- this.context.push(...msgs);
36
- all.push(...msgs);
34
+ const sessionContext = [...this.context];
35
+ const persistToContext = (msgs) => {
36
+ this.context.push(...msgs);
37
+ };
38
+ const pushToSession = (msgs) => {
39
+ sessionContext.push(...msgs);
40
+ };
41
+ const userMessage = { role: types_1.Role.User, content: message };
42
+ pushToSession([userMessage]);
43
+ if (!once)
44
+ persistToContext([userMessage]);
45
+ const runAdapter = async () => {
46
+ const msgs = await this.adapter(this.config, sessionContext, this.tools);
47
+ pushToSession(msgs);
48
+ persistToContext(msgs);
49
+ all.push(...msgs);
50
+ return msgs;
51
+ };
52
+ let msgs = await runAdapter();
37
53
  for (;;) {
38
54
  const last = msgs[msgs.length - 1];
39
- if (this.endCondition(this.context, last))
55
+ if (this.endCondition(sessionContext, last))
40
56
  return all;
41
57
  const toolCalls = msgs.filter((m) => m.role === types_1.Role.ToolCall);
42
58
  if (toolCalls.length === 0)
@@ -81,11 +97,10 @@ class Agent {
81
97
  return result;
82
98
  }));
83
99
  const filteredResults = results.filter((result) => result !== null);
84
- this.context.push(...filteredResults);
100
+ pushToSession(filteredResults);
101
+ persistToContext(filteredResults);
85
102
  all.push(...filteredResults);
86
- msgs = await this.adapter(this.config, this.context, this.tools);
87
- this.context.push(...msgs);
88
- all.push(...msgs);
103
+ msgs = await runAdapter();
89
104
  }
90
105
  }
91
106
  fork() {
package/dist/types.d.ts CHANGED
@@ -43,4 +43,7 @@ export type AgentConfig = {
43
43
  }>) => void | Promise<void>;
44
44
  [key: string]: unknown;
45
45
  };
46
+ export type RunOptions = {
47
+ once?: boolean;
48
+ };
46
49
  export type Adapter = (config: Record<string, unknown>, context: Message[], tools: Tool[]) => Promise<Message[]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arki-moe/agent-ts",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "Minimal Agent library, zero dependencies",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",