@arki-moe/agent-ts 2.2.3 → 4.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 +6 -21
- package/dist/index.d.ts +3 -4
- package/dist/index.js +4 -11
- package/dist/types.d.ts +8 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ const getTimeTool: Tool = {
|
|
|
13
13
|
name: "get_time",
|
|
14
14
|
description: "Get the current time in ISO format",
|
|
15
15
|
parameters: { type: "object", properties: {} },
|
|
16
|
-
execute: () => new Date().toISOString(),
|
|
16
|
+
execute: (_args, _agent) => new Date().toISOString(),
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
const agent = new Agent("openai", {
|
|
@@ -23,11 +23,12 @@ const agent = new Agent("openai", {
|
|
|
23
23
|
onStream: (textDelta) => {
|
|
24
24
|
process.stdout.write(textDelta);
|
|
25
25
|
},
|
|
26
|
-
onToolCall: (message, args) => {
|
|
26
|
+
onToolCall: (message, args, agent) => {
|
|
27
27
|
console.log("tool call:", message);
|
|
28
28
|
console.log("tool args:", args);
|
|
29
|
+
console.log("agent context length:", agent.context.length);
|
|
29
30
|
},
|
|
30
|
-
onToolResult: (msg) => console.log("tool result:", msg),
|
|
31
|
+
onToolResult: (msg, agent) => console.log("tool result:", msg, agent.context.length),
|
|
31
32
|
});
|
|
32
33
|
agent.registerTool(getTimeTool);
|
|
33
34
|
|
|
@@ -54,7 +55,6 @@ When `apiKey` is not provided in config, adapters read from the corresponding en
|
|
|
54
55
|
- `agent.context` - Public property, complete conversation history
|
|
55
56
|
- `agent.registerTool(tool)` - Register tool
|
|
56
57
|
- `agent.run(message, options?)` - Execute tool chain automatically, returns all new `Message[]`
|
|
57
|
-
- `agent.fork()` - Create a new agent with a copied context
|
|
58
58
|
|
|
59
59
|
### Config
|
|
60
60
|
|
|
@@ -65,28 +65,13 @@ When `apiKey` is not provided in config, adapters read from the corresponding en
|
|
|
65
65
|
| `system` | `string` | Optional system prompt |
|
|
66
66
|
| `endCondition` | `(context, last) => boolean` | Stop condition for `run`. Defaults to `last.role === Role.Ai` |
|
|
67
67
|
| `onStream` | `(textDelta: string) => void \| Promise<void>` | Stream hook for AI text only. When provided, adapters use SSE streaming and still return the final `Message[]`. |
|
|
68
|
-
| `onToolCall` | `(message, args) => boolean \| void \| Promise<boolean \| void>` | Called before each tool execution; return `false` to skip tool execution and `onToolResult` |
|
|
69
|
-
| `onToolResult` | `(message) => void \| Promise<void>` | Called after each tool execution (`message.role === Role.ToolResult`) |
|
|
68
|
+
| `onToolCall` | `(message, args, agent) => boolean \| void \| Promise<boolean \| void>` | Called before each tool execution; return `false` to skip tool execution and `onToolResult` |
|
|
69
|
+
| `onToolResult` | `(message, agent) => void \| Promise<void>` | Called after each tool execution (`message.role === Role.ToolResult`) |
|
|
70
70
|
|
|
71
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
|
|
|
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
75
|
## Scripts
|
|
91
76
|
|
|
92
77
|
| Command | Description |
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { AgentConfig, Context, Message, RunOptions, Tool } from "./types";
|
|
1
|
+
import type { AgentConfig, AgentLike, 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, RunOptions, Tool } from "./types";
|
|
4
|
+
export type { Adapter, AgentConfig, AgentLike, Context, Message, RunOptions, Tool } from "./types";
|
|
5
5
|
export { Role } from "./types";
|
|
6
|
-
export declare class Agent {
|
|
6
|
+
export declare class Agent implements AgentLike {
|
|
7
7
|
context: Context;
|
|
8
8
|
private adapter;
|
|
9
9
|
private adapterName;
|
|
@@ -15,5 +15,4 @@ export declare class Agent {
|
|
|
15
15
|
constructor(adapterName: string, config: AgentConfig);
|
|
16
16
|
registerTool(tool: Tool): void;
|
|
17
17
|
run(message: string, options?: RunOptions): Promise<Message[]>;
|
|
18
|
-
fork(): Agent;
|
|
19
18
|
}
|
package/dist/index.js
CHANGED
|
@@ -73,18 +73,18 @@ class Agent {
|
|
|
73
73
|
isError: true,
|
|
74
74
|
};
|
|
75
75
|
if (this.onToolResult)
|
|
76
|
-
await Promise.resolve(this.onToolResult(result));
|
|
76
|
+
await Promise.resolve(this.onToolResult(result, this));
|
|
77
77
|
return result;
|
|
78
78
|
}
|
|
79
79
|
if (this.onToolCall) {
|
|
80
|
-
const shouldRun = await Promise.resolve(this.onToolCall(m, args));
|
|
80
|
+
const shouldRun = await Promise.resolve(this.onToolCall(m, args, this));
|
|
81
81
|
if (shouldRun === false)
|
|
82
82
|
return null;
|
|
83
83
|
}
|
|
84
84
|
let content;
|
|
85
85
|
let isError = false;
|
|
86
86
|
try {
|
|
87
|
-
const out = await Promise.resolve(tool.execute(args));
|
|
87
|
+
const out = await Promise.resolve(tool.execute(args, this));
|
|
88
88
|
content = typeof out === "string" ? out : JSON.stringify(out);
|
|
89
89
|
}
|
|
90
90
|
catch (err) {
|
|
@@ -93,7 +93,7 @@ class Agent {
|
|
|
93
93
|
}
|
|
94
94
|
const result = { role: types_1.Role.ToolResult, callId: m.callId, content, isError };
|
|
95
95
|
if (this.onToolResult)
|
|
96
|
-
await Promise.resolve(this.onToolResult(result));
|
|
96
|
+
await Promise.resolve(this.onToolResult(result, this));
|
|
97
97
|
return result;
|
|
98
98
|
}));
|
|
99
99
|
const filteredResults = results.filter((result) => result !== null);
|
|
@@ -103,12 +103,5 @@ class Agent {
|
|
|
103
103
|
msgs = await runAdapter();
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
|
-
fork() {
|
|
107
|
-
const agent = new Agent(this.adapterName, this.config);
|
|
108
|
-
agent.adapter = this.adapter;
|
|
109
|
-
agent.tools = [...this.tools];
|
|
110
|
-
agent.context = [...this.context];
|
|
111
|
-
return agent;
|
|
112
|
-
}
|
|
113
106
|
}
|
|
114
107
|
exports.Agent = Agent;
|
package/dist/types.d.ts
CHANGED
|
@@ -26,21 +26,26 @@ export type Message = {
|
|
|
26
26
|
isError?: boolean;
|
|
27
27
|
};
|
|
28
28
|
export type Context = Message[];
|
|
29
|
+
export interface AgentLike {
|
|
30
|
+
context: Context;
|
|
31
|
+
registerTool: (tool: Tool) => void;
|
|
32
|
+
run: (message: string, options?: RunOptions) => Promise<Message[]>;
|
|
33
|
+
}
|
|
29
34
|
export type Tool = {
|
|
30
35
|
name: string;
|
|
31
36
|
description: string;
|
|
32
37
|
parameters: unknown;
|
|
33
|
-
execute: (args: unknown) => Promise<unknown> | unknown;
|
|
38
|
+
execute: (args: unknown, agent: AgentLike) => Promise<unknown> | unknown;
|
|
34
39
|
};
|
|
35
40
|
export type AgentConfig = {
|
|
36
41
|
endCondition?: (context: Message[], last: Message) => boolean;
|
|
37
42
|
onStream?: (textDelta: string) => void | Promise<void>;
|
|
38
43
|
onToolCall?: (message: Extract<Message, {
|
|
39
44
|
role: Role.ToolCall;
|
|
40
|
-
}>, args: unknown) => boolean | void | Promise<boolean | void>;
|
|
45
|
+
}>, args: unknown, agent: AgentLike) => boolean | void | Promise<boolean | void>;
|
|
41
46
|
onToolResult?: (message: Extract<Message, {
|
|
42
47
|
role: Role.ToolResult;
|
|
43
|
-
}
|
|
48
|
+
}>, agent: AgentLike) => void | Promise<void>;
|
|
44
49
|
[key: string]: unknown;
|
|
45
50
|
};
|
|
46
51
|
export type RunOptions = {
|