@arki-moe/agent-ts 3.0.0 → 5.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 +11 -7
- package/dist/index.d.ts +4 -4
- package/dist/index.js +6 -8
- package/dist/types.d.ts +9 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,8 @@ 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
|
-
|
|
16
|
+
data: { category: "time" },
|
|
17
|
+
execute: (_args, _agent) => new Date().toISOString(),
|
|
17
18
|
};
|
|
18
19
|
|
|
19
20
|
const agent = new Agent("openai", {
|
|
@@ -23,11 +24,12 @@ const agent = new Agent("openai", {
|
|
|
23
24
|
onStream: (textDelta) => {
|
|
24
25
|
process.stdout.write(textDelta);
|
|
25
26
|
},
|
|
26
|
-
onToolCall: (message, args) => {
|
|
27
|
+
onToolCall: (message, args, agent) => {
|
|
27
28
|
console.log("tool call:", message);
|
|
28
29
|
console.log("tool args:", args);
|
|
30
|
+
console.log("agent context length:", agent.context.length);
|
|
29
31
|
},
|
|
30
|
-
onToolResult: (msg) => console.log("tool result:", msg),
|
|
32
|
+
onToolResult: (msg, agent) => console.log("tool result:", msg, agent.context.length),
|
|
31
33
|
});
|
|
32
34
|
agent.registerTool(getTimeTool);
|
|
33
35
|
|
|
@@ -53,7 +55,7 @@ When `apiKey` is not provided in config, adapters read from the corresponding en
|
|
|
53
55
|
- `Agent(adapterName, config)` - Create Agent
|
|
54
56
|
- `agent.context` - Public property, complete conversation history
|
|
55
57
|
- `agent.registerTool(tool)` - Register tool
|
|
56
|
-
- `agent.run(message
|
|
58
|
+
- `agent.run(message)` - Execute tool chain automatically, returns all new `Message[]`
|
|
57
59
|
|
|
58
60
|
### Config
|
|
59
61
|
|
|
@@ -64,13 +66,15 @@ When `apiKey` is not provided in config, adapters read from the corresponding en
|
|
|
64
66
|
| `system` | `string` | Optional system prompt |
|
|
65
67
|
| `endCondition` | `(context, last) => boolean` | Stop condition for `run`. Defaults to `last.role === Role.Ai` |
|
|
66
68
|
| `onStream` | `(textDelta: string) => void \| Promise<void>` | Stream hook for AI text only. When provided, adapters use SSE streaming and still return the final `Message[]`. |
|
|
67
|
-
| `onToolCall` | `(message, args) => boolean \| void \| Promise<boolean \| void>` | Called before each tool execution; return `false` to skip tool execution and `onToolResult` |
|
|
68
|
-
| `onToolResult` | `(message) => void \| Promise<void>` | Called after each tool execution (`message.role === Role.ToolResult`) |
|
|
69
|
+
| `onToolCall` | `(message, args, agent) => boolean \| void \| Promise<boolean \| void>` | Called before each tool execution; return `false` to skip tool execution and `onToolResult` |
|
|
70
|
+
| `onToolResult` | `(message, agent) => void \| Promise<void>` | Called after each tool execution (`message.role === Role.ToolResult`) |
|
|
69
71
|
|
|
70
|
-
`agent.run` always appends new messages to `agent.context`.
|
|
72
|
+
`agent.run` always appends new messages to `agent.context`. Multiple tool calls in a single model response are executed in parallel.
|
|
71
73
|
|
|
72
74
|
`onToolCall` receives parsed JSON args and can mutate them before execution. Returning `false` skips the tool call and does not emit a `ToolResult` message.
|
|
73
75
|
|
|
76
|
+
`tool.data` is a local JSON metadata bag for your own use and is not sent to adapters.
|
|
77
|
+
|
|
74
78
|
## Scripts
|
|
75
79
|
|
|
76
80
|
| Command | Description |
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { AgentConfig, Context, Message,
|
|
1
|
+
import type { AgentConfig, AgentLike, Context, Message, Tool } from "./types";
|
|
2
2
|
export { openaiAdapter } from "./adapter/openai";
|
|
3
3
|
export { openrouterAdapter } from "./adapter/openrouter";
|
|
4
|
-
export type { Adapter, AgentConfig, Context, Message,
|
|
4
|
+
export type { Adapter, AgentConfig, AgentLike, Context, Message, 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;
|
|
@@ -14,5 +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
|
|
17
|
+
run(message: string): Promise<Message[]>;
|
|
18
18
|
}
|
package/dist/index.js
CHANGED
|
@@ -28,8 +28,7 @@ class Agent {
|
|
|
28
28
|
registerTool(tool) {
|
|
29
29
|
this.tools.push(tool);
|
|
30
30
|
}
|
|
31
|
-
async run(message
|
|
32
|
-
const once = options.once ?? false;
|
|
31
|
+
async run(message) {
|
|
33
32
|
const all = [];
|
|
34
33
|
const sessionContext = [...this.context];
|
|
35
34
|
const persistToContext = (msgs) => {
|
|
@@ -40,8 +39,7 @@ class Agent {
|
|
|
40
39
|
};
|
|
41
40
|
const userMessage = { role: types_1.Role.User, content: message };
|
|
42
41
|
pushToSession([userMessage]);
|
|
43
|
-
|
|
44
|
-
persistToContext([userMessage]);
|
|
42
|
+
persistToContext([userMessage]);
|
|
45
43
|
const runAdapter = async () => {
|
|
46
44
|
const msgs = await this.adapter(this.config, sessionContext, this.tools);
|
|
47
45
|
pushToSession(msgs);
|
|
@@ -73,18 +71,18 @@ class Agent {
|
|
|
73
71
|
isError: true,
|
|
74
72
|
};
|
|
75
73
|
if (this.onToolResult)
|
|
76
|
-
await Promise.resolve(this.onToolResult(result));
|
|
74
|
+
await Promise.resolve(this.onToolResult(result, this));
|
|
77
75
|
return result;
|
|
78
76
|
}
|
|
79
77
|
if (this.onToolCall) {
|
|
80
|
-
const shouldRun = await Promise.resolve(this.onToolCall(m, args));
|
|
78
|
+
const shouldRun = await Promise.resolve(this.onToolCall(m, args, this));
|
|
81
79
|
if (shouldRun === false)
|
|
82
80
|
return null;
|
|
83
81
|
}
|
|
84
82
|
let content;
|
|
85
83
|
let isError = false;
|
|
86
84
|
try {
|
|
87
|
-
const out = await Promise.resolve(tool.execute(args));
|
|
85
|
+
const out = await Promise.resolve(tool.execute(args, this));
|
|
88
86
|
content = typeof out === "string" ? out : JSON.stringify(out);
|
|
89
87
|
}
|
|
90
88
|
catch (err) {
|
|
@@ -93,7 +91,7 @@ class Agent {
|
|
|
93
91
|
}
|
|
94
92
|
const result = { role: types_1.Role.ToolResult, callId: m.callId, content, isError };
|
|
95
93
|
if (this.onToolResult)
|
|
96
|
-
await Promise.resolve(this.onToolResult(result));
|
|
94
|
+
await Promise.resolve(this.onToolResult(result, this));
|
|
97
95
|
return result;
|
|
98
96
|
}));
|
|
99
97
|
const filteredResults = results.filter((result) => result !== null);
|
package/dist/types.d.ts
CHANGED
|
@@ -26,24 +26,27 @@ 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) => Promise<Message[]>;
|
|
33
|
+
}
|
|
29
34
|
export type Tool = {
|
|
30
35
|
name: string;
|
|
31
36
|
description: string;
|
|
32
37
|
parameters: unknown;
|
|
33
|
-
|
|
38
|
+
data?: Record<string, unknown>;
|
|
39
|
+
execute: (args: unknown, agent: AgentLike) => Promise<unknown> | unknown;
|
|
34
40
|
};
|
|
35
41
|
export type AgentConfig = {
|
|
36
42
|
endCondition?: (context: Message[], last: Message) => boolean;
|
|
37
43
|
onStream?: (textDelta: string) => void | Promise<void>;
|
|
38
44
|
onToolCall?: (message: Extract<Message, {
|
|
39
45
|
role: Role.ToolCall;
|
|
40
|
-
}>, args: unknown) => boolean | void | Promise<boolean | void>;
|
|
46
|
+
}>, args: unknown, agent: AgentLike) => boolean | void | Promise<boolean | void>;
|
|
41
47
|
onToolResult?: (message: Extract<Message, {
|
|
42
48
|
role: Role.ToolResult;
|
|
43
|
-
}
|
|
49
|
+
}>, agent: AgentLike) => void | Promise<void>;
|
|
44
50
|
[key: string]: unknown;
|
|
45
51
|
};
|
|
46
|
-
export type RunOptions = {
|
|
47
|
-
once?: boolean;
|
|
48
|
-
};
|
|
49
52
|
export type Adapter = (config: Record<string, unknown>, context: Message[], tools: Tool[]) => Promise<Message[]>;
|