@arki-moe/agent-ts 2.2.2 → 3.0.0

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,8 +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[]`
57
- - `agent.fork()` - Create a new agent with a copied context
56
+ - `agent.run(message, options?)` - Execute tool chain automatically, returns all new `Message[]`
58
57
 
59
58
  ### Config
60
59
 
@@ -68,25 +67,10 @@ When `apiKey` is not provided in config, adapters read from the corresponding en
68
67
  | `onToolCall` | `(message, args) => boolean \| void \| Promise<boolean \| void>` | Called before each tool execution; return `false` to skip tool execution and `onToolResult` |
69
68
  | `onToolResult` | `(message) => void \| Promise<void>` | Called after each tool execution (`message.role === Role.ToolResult`) |
70
69
 
71
- `agent.run` always appends new messages to `agent.context`. Multiple tool calls in a single model response are executed in parallel.
70
+ `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
71
 
73
72
  `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
73
 
75
- `agent.fork()` shallow-copies the context array, but message objects are shared. This means:
76
- - Shallow copy: `forked.context !== agent.context`, so pushing new messages does not affect the other agent.
77
- - Shared messages: modifying a message object in one context will be visible in the other.
78
-
79
- Example:
80
-
81
- ```ts
82
- const forked = agent.fork();
83
- forked.context.push({ role: Role.User, content: "hi" });
84
- // agent.context length is unchanged
85
-
86
- forked.context[0].content = "changed";
87
- // agent.context[0].content is also "changed" because messages are shared
88
- ```
89
-
90
74
  ## Scripts
91
75
 
92
76
  | Command | Description |
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,5 @@ 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[]>;
18
- fork(): Agent;
17
+ run(message: string, options?: RunOptions): Promise<Message[]>;
19
18
  }
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,19 +97,11 @@ 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
- fork() {
92
- const agent = new Agent(this.adapterName, this.config);
93
- agent.adapter = this.adapter;
94
- agent.tools = [...this.tools];
95
- agent.context = [...this.context];
96
- return agent;
97
- }
98
106
  }
99
107
  exports.Agent = Agent;
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": "3.0.0",
4
4
  "description": "Minimal Agent library, zero dependencies",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",