@lovelymai/agent 1.0.6 → 1.0.8
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
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @lovelymai/agent
|
|
2
|
+
|
|
3
|
+
一个纯净极简易于使用的 Agent 框架。
|
|
4
|
+
|
|
5
|
+
## 第一步:安装依赖
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lovelymai/agent openai
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`openai` 是 peer dependency,需要在项目中自行安装。
|
|
12
|
+
|
|
13
|
+
## 第二步:创建 Agent 实例
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import OpenAI from 'openai'
|
|
17
|
+
import { createAgentManager } from '@lovelymai/agent'
|
|
18
|
+
|
|
19
|
+
const client = new OpenAI()
|
|
20
|
+
const agent = createAgentManager(client)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 第三步:配置模型与消息
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
agent.config = { model: 'deepseek-v4-flash', thinking: { type: 'disabled' } }
|
|
27
|
+
agent.messages.push({ role: 'user', content: '你好' })
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## 第四步:注册工具(可选)
|
|
31
|
+
|
|
32
|
+
定义工具并在 Agent 中注册,Agent 会在对话中自动调用:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
agent.updateTools([
|
|
36
|
+
{
|
|
37
|
+
name: 'get_weather',
|
|
38
|
+
description: '查询指定城市的天气',
|
|
39
|
+
properties: {
|
|
40
|
+
city: { type: 'string', description: '城市名称', required: true },
|
|
41
|
+
},
|
|
42
|
+
function: async ({ city }) => {
|
|
43
|
+
return `${city}今天晴,25°C`
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
])
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 第五步:监听事件
|
|
50
|
+
|
|
51
|
+
通过 `onEvent` 回调获取 Agent 运行过程中的各种事件:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
agent.onEvent = (event) => {
|
|
55
|
+
if (event.type === 'message_update' && 'content' in event.text) {
|
|
56
|
+
process.stdout.write(event.text.content ?? '')
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
支持的事件类型:
|
|
62
|
+
|
|
63
|
+
| 事件 | 说明 |
|
|
64
|
+
| ---------------- | --------------------------------------------------- |
|
|
65
|
+
| `agent_start` | Agent 开始运行 |
|
|
66
|
+
| `turn_start` | 每轮对话开始 |
|
|
67
|
+
| `message_update` | 流式输出更新 |
|
|
68
|
+
| `tool_start` | 工具开始执行 |
|
|
69
|
+
| `tool_end` | 工具执行完成 |
|
|
70
|
+
| `agent_end` | Agent 运行结束 |
|
|
71
|
+
| `agent_error` | Agent 运行出错 |
|
|
72
|
+
|
|
73
|
+
## 第六步:启动与停止
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
// 启动
|
|
77
|
+
await agent.start()
|
|
78
|
+
|
|
79
|
+
// 手动停止
|
|
80
|
+
agent.stop()
|
|
81
|
+
```
|
package/dist/agent.js
CHANGED
|
@@ -533,12 +533,18 @@ var Oe = class {
|
|
|
533
533
|
let i = await e.chat.completions.create({
|
|
534
534
|
...t,
|
|
535
535
|
stream: !0,
|
|
536
|
-
tool_choice: "auto"
|
|
536
|
+
tool_choice: "auto",
|
|
537
|
+
stream_options: { include_usage: !0 }
|
|
537
538
|
}), a = {
|
|
538
539
|
role: "assistant",
|
|
539
540
|
content: "",
|
|
540
541
|
reasoning_content: "",
|
|
541
|
-
tool_calls: []
|
|
542
|
+
tool_calls: [],
|
|
543
|
+
usage: {
|
|
544
|
+
prompt_tokens: 0,
|
|
545
|
+
completion_tokens: 0,
|
|
546
|
+
total_tokens: 0
|
|
547
|
+
}
|
|
542
548
|
};
|
|
543
549
|
for await (let e of i) {
|
|
544
550
|
if (!r.value) throw Error("主动停止");
|
|
@@ -555,6 +561,7 @@ var Oe = class {
|
|
|
555
561
|
let t = a.tool_calls[e.index];
|
|
556
562
|
e.id && (t.id = e.id), e.function?.name && (t.function.name = e.function.name), e.function?.arguments && (t.function.arguments += e.function.arguments);
|
|
557
563
|
}
|
|
564
|
+
e.usage && (a.usage = e.usage);
|
|
558
565
|
}
|
|
559
566
|
return a.tool_calls?.length === 0 && delete a.tool_calls, a.content || delete a.content, a.reasoning_content || delete a.reasoning_content, a;
|
|
560
567
|
}, Ae = (e) => {
|
|
@@ -580,13 +587,14 @@ var Oe = class {
|
|
|
580
587
|
toolExecutors: n
|
|
581
588
|
};
|
|
582
589
|
}, je = (e) => {
|
|
583
|
-
let t = /* @__PURE__ */ $({ model: "" }), n = /* @__PURE__ */ $([]), r = /* @__PURE__ */ $([]), i = /* @__PURE__ */ $({}), a = /* @__PURE__ */ $(10), o = /* @__PURE__ */ Ee({}), s = /* @__PURE__ */ $(), c = /* @__PURE__ */ $(!1);
|
|
590
|
+
let t = /* @__PURE__ */ $({ model: "" }), n = /* @__PURE__ */ $([]), r = /* @__PURE__ */ $([]), i = /* @__PURE__ */ $({}), a = /* @__PURE__ */ $(10), o = /* @__PURE__ */ Ee({}), s = /* @__PURE__ */ $(), c = /* @__PURE__ */ $(!1), l = /* @__PURE__ */ $(0);
|
|
584
591
|
return /* @__PURE__ */ G({
|
|
585
592
|
config: t,
|
|
586
593
|
messages: n,
|
|
587
594
|
maxIteration: a,
|
|
588
595
|
environment: o,
|
|
589
596
|
onEvent: s,
|
|
597
|
+
usage: l,
|
|
590
598
|
updateTools: (e) => {
|
|
591
599
|
let t = Ae(e);
|
|
592
600
|
r.value = t.toolDefinitions, i.value = t.toolExecutors;
|
|
@@ -594,18 +602,18 @@ var Oe = class {
|
|
|
594
602
|
start: async () => {
|
|
595
603
|
c.value = !0, s.value?.({ type: "agent_start" });
|
|
596
604
|
try {
|
|
597
|
-
for (let
|
|
605
|
+
for (let u = 0; u < a.value; u++) {
|
|
598
606
|
if (!c.value) return;
|
|
599
607
|
s.value?.({
|
|
600
608
|
type: "turn_start",
|
|
601
|
-
turnCount:
|
|
609
|
+
turnCount: u
|
|
602
610
|
});
|
|
603
611
|
let a = n.value.filter((e) => [
|
|
604
612
|
"system",
|
|
605
613
|
"user",
|
|
606
614
|
"assistant",
|
|
607
615
|
"tool"
|
|
608
|
-
].includes(e.role)),
|
|
616
|
+
].includes(e.role)), d = await ke(e, {
|
|
609
617
|
...t.value,
|
|
610
618
|
messages: a,
|
|
611
619
|
tools: r.value
|
|
@@ -614,15 +622,15 @@ var Oe = class {
|
|
|
614
622
|
type: "message_update",
|
|
615
623
|
text: e
|
|
616
624
|
});
|
|
617
|
-
}, c);
|
|
618
|
-
if (n.value.push(
|
|
625
|
+
}, c), { usage: { total_tokens: f }, ...ee } = d;
|
|
626
|
+
if (l.value = f, n.value.push(ee), !d.tool_calls) {
|
|
619
627
|
s.value?.({
|
|
620
628
|
type: "agent_end",
|
|
621
|
-
turnCount:
|
|
629
|
+
turnCount: u
|
|
622
630
|
});
|
|
623
631
|
return;
|
|
624
632
|
}
|
|
625
|
-
for (let e of
|
|
633
|
+
for (let e of d.tool_calls) {
|
|
626
634
|
if (!c.value) throw Error("主动停止");
|
|
627
635
|
let t = i.value[e.function.name];
|
|
628
636
|
if (!t) continue;
|
|
@@ -636,9 +644,9 @@ var Oe = class {
|
|
|
636
644
|
} catch (e) {
|
|
637
645
|
a = e instanceof Error ? e.message : e;
|
|
638
646
|
}
|
|
639
|
-
n.value.push({
|
|
647
|
+
a = typeof a == "string" ? a : JSON.stringify(a), n.value.push({
|
|
640
648
|
role: "tool",
|
|
641
|
-
content:
|
|
649
|
+
content: a,
|
|
642
650
|
tool_call_id: e.id
|
|
643
651
|
}), s.value?.({
|
|
644
652
|
type: "tool_end",
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export type
|
|
2
|
-
export { createAgentManager, type AgentManager, type Event } from './services/loop.ts';
|
|
1
|
+
export { createAgentManager, type AgentManager, type Tool, type Event } from './modules/index.ts';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type OpenAI from 'openai';
|
|
2
2
|
import type { ChatCompletionMessageParam } from 'openai/resources/chat/completions';
|
|
3
|
-
import { type Tool } from './tool.ts';
|
|
3
|
+
import { type Tool } from './modules/tool.ts';
|
|
4
4
|
export type AgentManager = {
|
|
5
5
|
/** 模型配置 */
|
|
6
6
|
config: {
|
|
@@ -15,6 +15,8 @@ export type AgentManager = {
|
|
|
15
15
|
maxIteration: number;
|
|
16
16
|
/** 环境参数对象 */
|
|
17
17
|
environment: Record<string, any>;
|
|
18
|
+
/** token 总量 */
|
|
19
|
+
readonly usage: number;
|
|
18
20
|
/** 事件回调 */
|
|
19
21
|
onEvent: ((event: Event) => void) | undefined;
|
|
20
22
|
/** 更新工具 */
|
|
@@ -24,6 +26,7 @@ export type AgentManager = {
|
|
|
24
26
|
/** 结束 */
|
|
25
27
|
readonly stop: () => void;
|
|
26
28
|
};
|
|
29
|
+
export type { Tool };
|
|
27
30
|
export type Event = {
|
|
28
31
|
type: 'agent_start';
|
|
29
32
|
} | {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Ref } from '@vue/reactivity';
|
|
2
2
|
import type OpenAI from 'openai';
|
|
3
3
|
import type { ChatCompletionMessageParam, ChatCompletionTool, ChatCompletionMessageFunctionToolCall } from 'openai/resources/chat/completions';
|
|
4
|
-
|
|
4
|
+
type Config = {
|
|
5
5
|
/** 模型 */
|
|
6
6
|
model: string;
|
|
7
7
|
/** 消息 */
|
|
@@ -10,14 +10,21 @@ export type Config = {
|
|
|
10
10
|
tools: ChatCompletionTool[];
|
|
11
11
|
[key: string]: any;
|
|
12
12
|
};
|
|
13
|
+
type Usage = {
|
|
14
|
+
prompt_tokens: number;
|
|
15
|
+
completion_tokens: number;
|
|
16
|
+
total_tokens: number;
|
|
17
|
+
};
|
|
13
18
|
type Accumulated = {
|
|
14
19
|
role: 'assistant';
|
|
15
20
|
content?: string;
|
|
16
21
|
reasoning_content?: string;
|
|
17
22
|
tool_calls?: ChatCompletionMessageFunctionToolCall[];
|
|
23
|
+
usage: Usage;
|
|
18
24
|
};
|
|
19
25
|
export declare const streamOut: (client: OpenAI, config: Config, onChunk: (text: {
|
|
20
|
-
content
|
|
21
|
-
|
|
26
|
+
content: string;
|
|
27
|
+
} | {
|
|
28
|
+
reasoning_content: string;
|
|
22
29
|
}) => void, isRunning: Ref<boolean>) => Promise<Accumulated>;
|
|
23
30
|
export {};
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lovelymai/agent",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "一个纯净极简易于使用的 Agent 框架",
|
|
5
|
-
"keywords": [
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agent"
|
|
7
|
+
],
|
|
6
8
|
"license": "MIT",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
7
12
|
"type": "module",
|
|
8
|
-
"files": ["dist"],
|
|
9
13
|
"exports": {
|
|
10
14
|
".": {
|
|
11
15
|
"types": "./dist/index.d.ts",
|
|
@@ -13,13 +17,16 @@
|
|
|
13
17
|
}
|
|
14
18
|
},
|
|
15
19
|
"scripts": {
|
|
16
|
-
"
|
|
20
|
+
"start": "tsx ./test/src/index.ts",
|
|
21
|
+
"build": "vite build && tsc -p tsconfig.build.json"
|
|
17
22
|
},
|
|
18
23
|
"dependencies": {
|
|
19
24
|
"@vue/reactivity": "^3.5.41"
|
|
20
25
|
},
|
|
21
26
|
"devDependencies": {
|
|
27
|
+
"@types/node": "^26.4.0",
|
|
22
28
|
"openai": "^7.7.0",
|
|
29
|
+
"tsx": "^4.23.13",
|
|
23
30
|
"typescript": "^7.0.2",
|
|
24
31
|
"vite": "^8.0.12"
|
|
25
32
|
},
|
|
File without changes
|