@arki-moe/agent-ts 3.0.0 → 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 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
 
@@ -64,8 +65,8 @@ When `apiKey` is not provided in config, adapters read from the corresponding en
64
65
  | `system` | `string` | Optional system prompt |
65
66
  | `endCondition` | `(context, last) => boolean` | Stop condition for `run`. Defaults to `last.role === Role.Ai` |
66
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[]`. |
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`) |
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`) |
69
70
 
70
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.
71
72
 
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;
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);
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
- }>) => void | Promise<void>;
48
+ }>, agent: AgentLike) => void | Promise<void>;
44
49
  [key: string]: unknown;
45
50
  };
46
51
  export type RunOptions = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arki-moe/agent-ts",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "Minimal Agent library, zero dependencies",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",