@lovelymai/agent 1.0.6 → 1.0.7
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/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.7",
|
|
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
|