@njuptlzf/agent-chat-server 0.1.1 → 0.2.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/dist/audit.d.ts +54 -0
- package/dist/audit.js +133 -0
- package/dist/authz.d.ts +134 -0
- package/dist/authz.js +238 -0
- package/dist/events.d.ts +33 -0
- package/dist/events.js +169 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +4 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +126 -0
- package/dist/manifest.d.ts +147 -0
- package/dist/manifest.js +491 -0
- package/dist/mcp-config.d.ts +87 -0
- package/dist/mcp-config.js +353 -0
- package/dist/model-config.d.ts +123 -0
- package/dist/model-config.js +371 -0
- package/dist/model-failover.d.ts +42 -0
- package/dist/model-failover.js +82 -0
- package/dist/models-api.d.ts +98 -0
- package/dist/models-api.js +326 -0
- package/dist/persona-config.d.ts +112 -0
- package/dist/persona-config.js +368 -0
- package/dist/persona-trial.d.ts +50 -0
- package/dist/persona-trial.js +77 -0
- package/dist/product-routes.d.ts +35 -0
- package/dist/product-routes.js +92 -0
- package/dist/server.d.ts +162 -0
- package/dist/server.js +2572 -0
- package/dist/session.d.ts +218 -0
- package/dist/session.js +545 -0
- package/dist/sqlite-stores.d.ts +173 -0
- package/dist/sqlite-stores.js +921 -0
- package/dist/stores.d.ts +506 -0
- package/dist/stores.js +614 -0
- package/dist/testing.d.ts +42 -0
- package/dist/testing.js +47 -0
- package/dist/trial-grant.d.ts +40 -0
- package/dist/trial-grant.js +24 -0
- package/dist/usage-report.d.ts +23 -0
- package/dist/usage-report.js +68 -0
- package/dist/util.d.ts +3 -0
- package/dist/util.js +5 -0
- package/package.json +64 -58
- package/scripts/agentchat-integrate.mjs +607 -601
- package/src/audit.ts +169 -169
- package/src/authz.ts +307 -307
- package/src/events.ts +206 -206
- package/src/index.ts +26 -26
- package/src/main.ts +142 -142
- package/src/manifest.ts +562 -562
- package/src/mcp-config.ts +385 -385
- package/src/model-config.ts +424 -424
- package/src/model-failover.ts +95 -95
- package/src/models-api.ts +360 -360
- package/src/persona-config.ts +436 -436
- package/src/persona-trial.ts +121 -121
- package/src/product-routes.ts +102 -102
- package/src/server.ts +2647 -2647
- package/src/session.ts +688 -688
- package/src/sqlite-stores.ts +1040 -1040
- package/src/stores.ts +899 -899
- package/src/testing.ts +80 -80
- package/src/trial-grant.ts +62 -62
- package/src/usage-report.ts +86 -86
- package/src/util.ts +6 -6
package/src/events.ts
CHANGED
|
@@ -1,206 +1,206 @@
|
|
|
1
|
-
import type { AgentSessionEvent } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { ChatEvent, TokenUsage, ToolResultView } from "@njuptlzf/agent-chat-protocol";
|
|
3
|
-
|
|
4
|
-
type PiUsage = {
|
|
5
|
-
input: number;
|
|
6
|
-
output: number;
|
|
7
|
-
cacheRead: number;
|
|
8
|
-
cacheWrite: number;
|
|
9
|
-
totalTokens: number;
|
|
10
|
-
cost: { total: number };
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export function emptyUsage(): TokenUsage {
|
|
14
|
-
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function toTokenUsage(u: PiUsage): TokenUsage {
|
|
18
|
-
return {
|
|
19
|
-
input: u.input,
|
|
20
|
-
output: u.output,
|
|
21
|
-
cacheRead: u.cacheRead,
|
|
22
|
-
cacheWrite: u.cacheWrite,
|
|
23
|
-
total: u.totalTokens,
|
|
24
|
-
costUsd: u.cost.total,
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function addUsage(a: TokenUsage, b: TokenUsage): TokenUsage {
|
|
29
|
-
return {
|
|
30
|
-
input: a.input + b.input,
|
|
31
|
-
output: a.output + b.output,
|
|
32
|
-
cacheRead: a.cacheRead + b.cacheRead,
|
|
33
|
-
cacheWrite: a.cacheWrite + b.cacheWrite,
|
|
34
|
-
total: a.total + b.total,
|
|
35
|
-
costUsd: a.costUsd + b.costUsd,
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 卡片数据:内联工具直接给 `details`;MCP 工具走 `structuredContent`
|
|
41
|
-
* (pi 的 convertMcpResult 把它设成整个 CallToolResult,业务负载在内层
|
|
42
|
-
* `structuredContent`,`details` 只剩 {server, tool})。前端只认负载,不认实现。
|
|
43
|
-
*/
|
|
44
|
-
function cardOf(r: { details?: unknown; structuredContent?: unknown }): unknown {
|
|
45
|
-
const sc = r.structuredContent;
|
|
46
|
-
if (sc && typeof sc === "object") {
|
|
47
|
-
const inner = (sc as { structuredContent?: unknown }).structuredContent;
|
|
48
|
-
return inner && typeof inner === "object" ? inner : sc;
|
|
49
|
-
}
|
|
50
|
-
return r.details;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Flatten pi tool-result content into a text view the UI can show. */
|
|
54
|
-
function toResultView(result: unknown): ToolResultView {
|
|
55
|
-
const r = (result ?? {}) as {
|
|
56
|
-
content?: Array<{ type: string; text?: string }>;
|
|
57
|
-
details?: unknown;
|
|
58
|
-
structuredContent?: unknown;
|
|
59
|
-
};
|
|
60
|
-
const text = (r.content ?? [])
|
|
61
|
-
.filter((c) => c.type === "text" && typeof c.text === "string")
|
|
62
|
-
.map((c) => c.text as string)
|
|
63
|
-
.join("\n");
|
|
64
|
-
return { text, details: cardOf(r) };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function assistantText(content: unknown): string {
|
|
68
|
-
if (!Array.isArray(content)) return "";
|
|
69
|
-
return content
|
|
70
|
-
.filter((c: { type?: string }) => c?.type === "text")
|
|
71
|
-
.map((c: { text?: string }) => c.text ?? "")
|
|
72
|
-
.join("");
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* 模型报错的用户文案(2026-11-04):pi 的 errorMessage 是原始串(如
|
|
77
|
-
* `429: {"message":"inference exceeds tpm/rpm limit",...}`),直接下发会把
|
|
78
|
-
* 上游 JSON 露进气泡。这里折叠成一句人话 + 保留状态码数字便于口头报障。
|
|
79
|
-
* **failover 匹配不读这条文案**(见 server.ts 的 rawModelError)——它直接从
|
|
80
|
-
* pi 原始事件取 errorMessage 做正则,用户文案怎么改都不影响切换逻辑。
|
|
81
|
-
*/
|
|
82
|
-
export function friendlyModelError(raw: string | undefined): string {
|
|
83
|
-
const msg = (raw ?? "").slice(0, 400);
|
|
84
|
-
const status = /\b(4\d\d|5\d\d)\b/.exec(msg)?.[1];
|
|
85
|
-
if (status === "429") return `模型限流中,请稍后再试,或在「管理 → 模型」调整档位(429)`;
|
|
86
|
-
if (status === "401" || status === "403") return `模型服务鉴权失败,请检查模型配置或联系管理员(${status})`;
|
|
87
|
-
if (status === "402") return "模型服务额度不足,请联系管理员(402)";
|
|
88
|
-
if (status) return `模型服务暂时不可用,请稍后重试(${status})`;
|
|
89
|
-
if (/timeout|timed out|ETIMEDOUT|ECONNRESET|ECONNREFUSED|socket hang up|fetch failed|network|ENOTFOUND|EAI_AGAIN/i.test(msg))
|
|
90
|
-
return "连接模型服务的网络异常,请稍后重试";
|
|
91
|
-
return "模型请求失败,请稍后重试";
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export interface EventMapper {
|
|
95
|
-
map(event: AgentSessionEvent): ChatEvent[];
|
|
96
|
-
/** Usage accumulated across every assistant message seen so far. */
|
|
97
|
-
totalUsage(): TokenUsage;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Translate pi session events into the stable wire protocol.
|
|
102
|
-
* Stateful (message ids, usage totals), so create one mapper per run.
|
|
103
|
-
*/
|
|
104
|
-
export function createEventMapper(): EventMapper {
|
|
105
|
-
let counter = 0;
|
|
106
|
-
let currentMessageId = "";
|
|
107
|
-
let total = emptyUsage();
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
totalUsage: () => total,
|
|
111
|
-
map(event) {
|
|
112
|
-
switch (event.type) {
|
|
113
|
-
case "message_start": {
|
|
114
|
-
if ((event.message as { role?: string }).role === "assistant") {
|
|
115
|
-
currentMessageId = `m${++counter}`;
|
|
116
|
-
}
|
|
117
|
-
return [];
|
|
118
|
-
}
|
|
119
|
-
case "message_update": {
|
|
120
|
-
const ev = event.assistantMessageEvent;
|
|
121
|
-
if (ev.type === "text_delta") {
|
|
122
|
-
return [{ type: "text_delta", messageId: currentMessageId, delta: ev.delta }];
|
|
123
|
-
}
|
|
124
|
-
if (ev.type === "thinking_delta") {
|
|
125
|
-
return [{ type: "thinking_delta", messageId: currentMessageId, delta: ev.delta }];
|
|
126
|
-
}
|
|
127
|
-
return [];
|
|
128
|
-
}
|
|
129
|
-
case "message_end": {
|
|
130
|
-
const msg = event.message as {
|
|
131
|
-
role?: string;
|
|
132
|
-
content?: unknown;
|
|
133
|
-
usage?: PiUsage;
|
|
134
|
-
stopReason?: string;
|
|
135
|
-
errorMessage?: string;
|
|
136
|
-
};
|
|
137
|
-
if (msg.role !== "assistant") return [];
|
|
138
|
-
const out: ChatEvent[] = [];
|
|
139
|
-
const usage = msg.usage ? toTokenUsage(msg.usage) : undefined;
|
|
140
|
-
if (usage) total = addUsage(total, usage);
|
|
141
|
-
out.push({
|
|
142
|
-
type: "message_end",
|
|
143
|
-
messageId: currentMessageId,
|
|
144
|
-
text: assistantText(msg.content),
|
|
145
|
-
usage,
|
|
146
|
-
});
|
|
147
|
-
if (msg.stopReason === "error") {
|
|
148
|
-
out.push({
|
|
149
|
-
type: "error",
|
|
150
|
-
code: "model_error",
|
|
151
|
-
message: friendlyModelError(msg.errorMessage),
|
|
152
|
-
retryable: true,
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
return out;
|
|
156
|
-
}
|
|
157
|
-
case "tool_execution_start":
|
|
158
|
-
return [{ type: "tool_start", toolCallId: event.toolCallId, name: event.toolName, args: event.args }];
|
|
159
|
-
case "tool_execution_update":
|
|
160
|
-
return [{ type: "tool_update", toolCallId: event.toolCallId, partial: toResultView(event.partialResult) }];
|
|
161
|
-
case "tool_execution_end":
|
|
162
|
-
return [
|
|
163
|
-
{
|
|
164
|
-
type: "tool_end",
|
|
165
|
-
toolCallId: event.toolCallId,
|
|
166
|
-
name: event.toolName,
|
|
167
|
-
ok: !event.isError,
|
|
168
|
-
result: toResultView(event.result),
|
|
169
|
-
durationMs: event.durationMs,
|
|
170
|
-
},
|
|
171
|
-
];
|
|
172
|
-
case "agent_settled":
|
|
173
|
-
return [{ type: "done", aborted: event.aborted, usage: total }];
|
|
174
|
-
default:
|
|
175
|
-
return [];
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export interface RunAccounting {
|
|
182
|
-
/** Feed each outgoing protocol event; counts completed tool rounds. */
|
|
183
|
-
observe(ev: ChatEvent): void;
|
|
184
|
-
toolRounds(): number;
|
|
185
|
-
/** True once a completed tool round hit the configured cap. */
|
|
186
|
-
limitHit(): boolean;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Bounds one run's agentic loop: pi has no native turn cap, so the server
|
|
191
|
-
* counts tool rounds on the wire and aborts when the cap is hit.
|
|
192
|
-
*/
|
|
193
|
-
export function createRunAccounting(maxToolRounds: number | undefined): RunAccounting {
|
|
194
|
-
let rounds = 0;
|
|
195
|
-
let hit = false;
|
|
196
|
-
return {
|
|
197
|
-
observe(ev) {
|
|
198
|
-
if (ev.type === "tool_end") {
|
|
199
|
-
rounds++;
|
|
200
|
-
if (maxToolRounds !== undefined && rounds >= maxToolRounds) hit = true;
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
toolRounds: () => rounds,
|
|
204
|
-
limitHit: () => hit,
|
|
205
|
-
};
|
|
206
|
-
}
|
|
1
|
+
import type { AgentSessionEvent } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { ChatEvent, TokenUsage, ToolResultView } from "@njuptlzf/agent-chat-protocol";
|
|
3
|
+
|
|
4
|
+
type PiUsage = {
|
|
5
|
+
input: number;
|
|
6
|
+
output: number;
|
|
7
|
+
cacheRead: number;
|
|
8
|
+
cacheWrite: number;
|
|
9
|
+
totalTokens: number;
|
|
10
|
+
cost: { total: number };
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function emptyUsage(): TokenUsage {
|
|
14
|
+
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function toTokenUsage(u: PiUsage): TokenUsage {
|
|
18
|
+
return {
|
|
19
|
+
input: u.input,
|
|
20
|
+
output: u.output,
|
|
21
|
+
cacheRead: u.cacheRead,
|
|
22
|
+
cacheWrite: u.cacheWrite,
|
|
23
|
+
total: u.totalTokens,
|
|
24
|
+
costUsd: u.cost.total,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function addUsage(a: TokenUsage, b: TokenUsage): TokenUsage {
|
|
29
|
+
return {
|
|
30
|
+
input: a.input + b.input,
|
|
31
|
+
output: a.output + b.output,
|
|
32
|
+
cacheRead: a.cacheRead + b.cacheRead,
|
|
33
|
+
cacheWrite: a.cacheWrite + b.cacheWrite,
|
|
34
|
+
total: a.total + b.total,
|
|
35
|
+
costUsd: a.costUsd + b.costUsd,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 卡片数据:内联工具直接给 `details`;MCP 工具走 `structuredContent`
|
|
41
|
+
* (pi 的 convertMcpResult 把它设成整个 CallToolResult,业务负载在内层
|
|
42
|
+
* `structuredContent`,`details` 只剩 {server, tool})。前端只认负载,不认实现。
|
|
43
|
+
*/
|
|
44
|
+
function cardOf(r: { details?: unknown; structuredContent?: unknown }): unknown {
|
|
45
|
+
const sc = r.structuredContent;
|
|
46
|
+
if (sc && typeof sc === "object") {
|
|
47
|
+
const inner = (sc as { structuredContent?: unknown }).structuredContent;
|
|
48
|
+
return inner && typeof inner === "object" ? inner : sc;
|
|
49
|
+
}
|
|
50
|
+
return r.details;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Flatten pi tool-result content into a text view the UI can show. */
|
|
54
|
+
function toResultView(result: unknown): ToolResultView {
|
|
55
|
+
const r = (result ?? {}) as {
|
|
56
|
+
content?: Array<{ type: string; text?: string }>;
|
|
57
|
+
details?: unknown;
|
|
58
|
+
structuredContent?: unknown;
|
|
59
|
+
};
|
|
60
|
+
const text = (r.content ?? [])
|
|
61
|
+
.filter((c) => c.type === "text" && typeof c.text === "string")
|
|
62
|
+
.map((c) => c.text as string)
|
|
63
|
+
.join("\n");
|
|
64
|
+
return { text, details: cardOf(r) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function assistantText(content: unknown): string {
|
|
68
|
+
if (!Array.isArray(content)) return "";
|
|
69
|
+
return content
|
|
70
|
+
.filter((c: { type?: string }) => c?.type === "text")
|
|
71
|
+
.map((c: { text?: string }) => c.text ?? "")
|
|
72
|
+
.join("");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 模型报错的用户文案(2026-11-04):pi 的 errorMessage 是原始串(如
|
|
77
|
+
* `429: {"message":"inference exceeds tpm/rpm limit",...}`),直接下发会把
|
|
78
|
+
* 上游 JSON 露进气泡。这里折叠成一句人话 + 保留状态码数字便于口头报障。
|
|
79
|
+
* **failover 匹配不读这条文案**(见 server.ts 的 rawModelError)——它直接从
|
|
80
|
+
* pi 原始事件取 errorMessage 做正则,用户文案怎么改都不影响切换逻辑。
|
|
81
|
+
*/
|
|
82
|
+
export function friendlyModelError(raw: string | undefined): string {
|
|
83
|
+
const msg = (raw ?? "").slice(0, 400);
|
|
84
|
+
const status = /\b(4\d\d|5\d\d)\b/.exec(msg)?.[1];
|
|
85
|
+
if (status === "429") return `模型限流中,请稍后再试,或在「管理 → 模型」调整档位(429)`;
|
|
86
|
+
if (status === "401" || status === "403") return `模型服务鉴权失败,请检查模型配置或联系管理员(${status})`;
|
|
87
|
+
if (status === "402") return "模型服务额度不足,请联系管理员(402)";
|
|
88
|
+
if (status) return `模型服务暂时不可用,请稍后重试(${status})`;
|
|
89
|
+
if (/timeout|timed out|ETIMEDOUT|ECONNRESET|ECONNREFUSED|socket hang up|fetch failed|network|ENOTFOUND|EAI_AGAIN/i.test(msg))
|
|
90
|
+
return "连接模型服务的网络异常,请稍后重试";
|
|
91
|
+
return "模型请求失败,请稍后重试";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface EventMapper {
|
|
95
|
+
map(event: AgentSessionEvent): ChatEvent[];
|
|
96
|
+
/** Usage accumulated across every assistant message seen so far. */
|
|
97
|
+
totalUsage(): TokenUsage;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Translate pi session events into the stable wire protocol.
|
|
102
|
+
* Stateful (message ids, usage totals), so create one mapper per run.
|
|
103
|
+
*/
|
|
104
|
+
export function createEventMapper(): EventMapper {
|
|
105
|
+
let counter = 0;
|
|
106
|
+
let currentMessageId = "";
|
|
107
|
+
let total = emptyUsage();
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
totalUsage: () => total,
|
|
111
|
+
map(event) {
|
|
112
|
+
switch (event.type) {
|
|
113
|
+
case "message_start": {
|
|
114
|
+
if ((event.message as { role?: string }).role === "assistant") {
|
|
115
|
+
currentMessageId = `m${++counter}`;
|
|
116
|
+
}
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
case "message_update": {
|
|
120
|
+
const ev = event.assistantMessageEvent;
|
|
121
|
+
if (ev.type === "text_delta") {
|
|
122
|
+
return [{ type: "text_delta", messageId: currentMessageId, delta: ev.delta }];
|
|
123
|
+
}
|
|
124
|
+
if (ev.type === "thinking_delta") {
|
|
125
|
+
return [{ type: "thinking_delta", messageId: currentMessageId, delta: ev.delta }];
|
|
126
|
+
}
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
case "message_end": {
|
|
130
|
+
const msg = event.message as {
|
|
131
|
+
role?: string;
|
|
132
|
+
content?: unknown;
|
|
133
|
+
usage?: PiUsage;
|
|
134
|
+
stopReason?: string;
|
|
135
|
+
errorMessage?: string;
|
|
136
|
+
};
|
|
137
|
+
if (msg.role !== "assistant") return [];
|
|
138
|
+
const out: ChatEvent[] = [];
|
|
139
|
+
const usage = msg.usage ? toTokenUsage(msg.usage) : undefined;
|
|
140
|
+
if (usage) total = addUsage(total, usage);
|
|
141
|
+
out.push({
|
|
142
|
+
type: "message_end",
|
|
143
|
+
messageId: currentMessageId,
|
|
144
|
+
text: assistantText(msg.content),
|
|
145
|
+
usage,
|
|
146
|
+
});
|
|
147
|
+
if (msg.stopReason === "error") {
|
|
148
|
+
out.push({
|
|
149
|
+
type: "error",
|
|
150
|
+
code: "model_error",
|
|
151
|
+
message: friendlyModelError(msg.errorMessage),
|
|
152
|
+
retryable: true,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
case "tool_execution_start":
|
|
158
|
+
return [{ type: "tool_start", toolCallId: event.toolCallId, name: event.toolName, args: event.args }];
|
|
159
|
+
case "tool_execution_update":
|
|
160
|
+
return [{ type: "tool_update", toolCallId: event.toolCallId, partial: toResultView(event.partialResult) }];
|
|
161
|
+
case "tool_execution_end":
|
|
162
|
+
return [
|
|
163
|
+
{
|
|
164
|
+
type: "tool_end",
|
|
165
|
+
toolCallId: event.toolCallId,
|
|
166
|
+
name: event.toolName,
|
|
167
|
+
ok: !event.isError,
|
|
168
|
+
result: toResultView(event.result),
|
|
169
|
+
durationMs: event.durationMs,
|
|
170
|
+
},
|
|
171
|
+
];
|
|
172
|
+
case "agent_settled":
|
|
173
|
+
return [{ type: "done", aborted: event.aborted, usage: total }];
|
|
174
|
+
default:
|
|
175
|
+
return [];
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface RunAccounting {
|
|
182
|
+
/** Feed each outgoing protocol event; counts completed tool rounds. */
|
|
183
|
+
observe(ev: ChatEvent): void;
|
|
184
|
+
toolRounds(): number;
|
|
185
|
+
/** True once a completed tool round hit the configured cap. */
|
|
186
|
+
limitHit(): boolean;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Bounds one run's agentic loop: pi has no native turn cap, so the server
|
|
191
|
+
* counts tool rounds on the wire and aborts when the cap is hit.
|
|
192
|
+
*/
|
|
193
|
+
export function createRunAccounting(maxToolRounds: number | undefined): RunAccounting {
|
|
194
|
+
let rounds = 0;
|
|
195
|
+
let hit = false;
|
|
196
|
+
return {
|
|
197
|
+
observe(ev) {
|
|
198
|
+
if (ev.type === "tool_end") {
|
|
199
|
+
rounds++;
|
|
200
|
+
if (maxToolRounds !== undefined && rounds >= maxToolRounds) hit = true;
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
toolRounds: () => rounds,
|
|
204
|
+
limitHit: () => hit,
|
|
205
|
+
};
|
|
206
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `@njuptlzf/agent-chat-server` 的公共入口。
|
|
3
|
-
*
|
|
4
|
-
* 只导出《[PUBLIC_API](../../../docs/design/PUBLIC_API.md)》登记的 18 项
|
|
5
|
-
* 「被产品侧具名 import」的符号。产品仓通过 `@njuptlzf/agent-chat-server` 消费平台,
|
|
6
|
-
* 不再走 `packages/server/src/*` 深路径;平台其余导出都是内部实现,随时可改。
|
|
7
|
-
*
|
|
8
|
-
* 增删导出必须同批改:PUBLIC_API.md、package.json 的 `exports`、
|
|
9
|
-
* `packages/server/test/public-api.test.ts`(那条测试会数条数)。
|
|
10
|
-
*/
|
|
11
|
-
export type { LoadedProduct, ToolContext } from "./manifest.ts";
|
|
12
|
-
export { personaIdsOf } from "./manifest.ts";
|
|
13
|
-
export type { ProductAdminRoute, ProductRoute, ServerDeps } from "./server.ts";
|
|
14
|
-
export {
|
|
15
|
-
auditDeny,
|
|
16
|
-
auditedWrite,
|
|
17
|
-
DRAFT_ID,
|
|
18
|
-
deny401,
|
|
19
|
-
MAX_ESSAY_CHARS,
|
|
20
|
-
readJson,
|
|
21
|
-
sendError,
|
|
22
|
-
sendJson,
|
|
23
|
-
SESSION_ID,
|
|
24
|
-
} from "./server.ts";
|
|
25
|
-
export { openDatabase, SqliteAccountState } from "./sqlite-stores.ts";
|
|
26
|
-
export { localDay } from "./util.ts";
|
|
1
|
+
/**
|
|
2
|
+
* `@njuptlzf/agent-chat-server` 的公共入口。
|
|
3
|
+
*
|
|
4
|
+
* 只导出《[PUBLIC_API](../../../docs/design/PUBLIC_API.md)》登记的 18 项
|
|
5
|
+
* 「被产品侧具名 import」的符号。产品仓通过 `@njuptlzf/agent-chat-server` 消费平台,
|
|
6
|
+
* 不再走 `packages/server/src/*` 深路径;平台其余导出都是内部实现,随时可改。
|
|
7
|
+
*
|
|
8
|
+
* 增删导出必须同批改:PUBLIC_API.md、package.json 的 `exports`、
|
|
9
|
+
* `packages/server/test/public-api.test.ts`(那条测试会数条数)。
|
|
10
|
+
*/
|
|
11
|
+
export type { LoadedProduct, ToolContext } from "./manifest.ts";
|
|
12
|
+
export { personaIdsOf } from "./manifest.ts";
|
|
13
|
+
export type { ProductAdminRoute, ProductRoute, ServerDeps } from "./server.ts";
|
|
14
|
+
export {
|
|
15
|
+
auditDeny,
|
|
16
|
+
auditedWrite,
|
|
17
|
+
DRAFT_ID,
|
|
18
|
+
deny401,
|
|
19
|
+
MAX_ESSAY_CHARS,
|
|
20
|
+
readJson,
|
|
21
|
+
sendError,
|
|
22
|
+
sendJson,
|
|
23
|
+
SESSION_ID,
|
|
24
|
+
} from "./server.ts";
|
|
25
|
+
export { openDatabase, SqliteAccountState } from "./sqlite-stores.ts";
|
|
26
|
+
export { localDay } from "./util.ts";
|