@njuptlzf/agent-chat-server 0.2.1 → 0.3.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/branding-ui.d.ts +16 -0
- package/dist/branding-ui.js +119 -0
- package/dist/manifest.js +13 -0
- package/dist/persona-config.js +12 -0
- package/dist/server.d.ts +6 -0
- package/dist/server.js +52 -0
- package/dist/session.js +2 -1
- package/dist/suggestions.d.ts +60 -0
- package/dist/suggestions.js +107 -0
- package/package.json +64 -64
- package/scripts/agentchat-integrate.mjs +6 -6
- package/src/branding-ui.ts +126 -0
- package/src/manifest.ts +13 -0
- package/src/persona-config.ts +12 -1
- package/src/server.ts +58 -0
- package/src/session.ts +689 -688
- package/src/suggestions.ts +146 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「下一步建议」的生成层:一轮对话结束后,把最近的**文本**消息拍平交给产品
|
|
3
|
+
* 默认模型,产出 N 条用户可能发出的简短追问。
|
|
4
|
+
*
|
|
5
|
+
* 定位与 `persona-trial` 不同:这是 run 结束后的附属产物——不建会话、不走 SSE、
|
|
6
|
+
* 不接配额与计费、不写 runLog。调用方(server.ts 路由)负责按人格复核生效模式,
|
|
7
|
+
* 并在**任何失败**时回落 `branding.ui.followUp.static`,绝不 4xx/5xx 冒泡。
|
|
8
|
+
*
|
|
9
|
+
* 与 `@assistant-ui/react` 的接法:前端实现自定义 `SuggestionAdapter.generate`
|
|
10
|
+
* 直接发结构化 messages(库自带的 `createSuggestionAdapter` 会把消息拍成单个
|
|
11
|
+
* prompt 字符串,服务端无法按 role 分离、无法强制中文指令,裁剪也不可测)。
|
|
12
|
+
*/
|
|
13
|
+
import type { ModelRuntime } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { resolveTier, type LoadedProduct } from "./manifest.ts";
|
|
15
|
+
|
|
16
|
+
/** 裁剪后的单条对话消息:只有文本,图片/附件/工具细节从不离开前端。 */
|
|
17
|
+
export interface ClippedMessage {
|
|
18
|
+
role: "user" | "assistant";
|
|
19
|
+
text: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 生成层的输入。`personaId` 由路由用于复核生效模式,生成层本身不用它。 */
|
|
23
|
+
export interface SuggestionsInput {
|
|
24
|
+
messages: ClippedMessage[];
|
|
25
|
+
count: number;
|
|
26
|
+
/** 生成失败时的回落内容(`branding.ui.followUp.static`)。 */
|
|
27
|
+
staticFallback: string[];
|
|
28
|
+
/** 覆盖内置生成指令(`branding.ui.followUp.systemPrompt`)。 */
|
|
29
|
+
systemPrompt?: string;
|
|
30
|
+
timeoutMs: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 生成层的依赖。真实现读 `deps.modelRuntime`;测试注入替身避免真花钱。 */
|
|
34
|
+
export interface SuggestionsEnv {
|
|
35
|
+
product: LoadedProduct;
|
|
36
|
+
modelRuntime: ModelRuntime;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type SuggestionsRunner = (env: SuggestionsEnv, input: SuggestionsInput) => Promise<string[]>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 缺省值与硬上限。前端 `followUpConfigOf` 各持一份同名缺省(两个包不共享模块,
|
|
43
|
+
* 单测钉住两边一致)——改这里要同步那边。
|
|
44
|
+
*/
|
|
45
|
+
export const SUGGESTIONS_DEFAULTS = {
|
|
46
|
+
count: 3,
|
|
47
|
+
maxMessages: 10,
|
|
48
|
+
timeoutMs: 8000,
|
|
49
|
+
maxTokens: 200,
|
|
50
|
+
/** 单条消息文本上限(超出截断)。 */
|
|
51
|
+
messageChars: 500,
|
|
52
|
+
/** 整段对话字符上限(从最新往回收)。 */
|
|
53
|
+
totalChars: 4000,
|
|
54
|
+
/** 单条建议字符上限。 */
|
|
55
|
+
suggestionChars: 80,
|
|
56
|
+
} as const;
|
|
57
|
+
|
|
58
|
+
/** 内置生成指令:中性、产品无关;`followUp.systemPrompt` 可整体覆盖。 */
|
|
59
|
+
function defaultSystemPrompt(count: number): string {
|
|
60
|
+
return (
|
|
61
|
+
`根据下面这段用户与助手的对话,推测用户接下来最可能发出的 ${count} 条简短追问。` +
|
|
62
|
+
"每行一条:不要编号、不要任何解释或前后缀,不要复述对话内容,每条不超过 30 个字。"
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const NUMBERED = /^\s*(?:\d{1,3}[.)、]|[•*·-])\s*/;
|
|
67
|
+
|
|
68
|
+
/** 模型回复 → 建议列表:按行拆、去编号前缀、去空行、截到 count 条。 */
|
|
69
|
+
export function parseSuggestions(text: string, count: number): string[] {
|
|
70
|
+
const cap = Math.max(1, Math.min(count, 20));
|
|
71
|
+
const out: string[] = [];
|
|
72
|
+
for (const line of text.split(/\r?\n/)) {
|
|
73
|
+
const s = line.replace(NUMBERED, "").trim();
|
|
74
|
+
if (!s) continue;
|
|
75
|
+
out.push(s.length > SUGGESTIONS_DEFAULTS.suggestionChars ? s.slice(0, SUGGESTIONS_DEFAULTS.suggestionChars) : s);
|
|
76
|
+
if (out.length >= cap) break;
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 服务端强制的输入裁剪——前端已裁一道,这里是权威层:
|
|
83
|
+
* 只留 user/assistant 的非空文本、单条截断、取最近 maxMessages 条、整段不超 totalChars。
|
|
84
|
+
*/
|
|
85
|
+
export function clipMessages(raw: unknown, maxMessages: number): ClippedMessage[] {
|
|
86
|
+
if (!Array.isArray(raw)) return [];
|
|
87
|
+
const kept: ClippedMessage[] = [];
|
|
88
|
+
for (const entry of raw) {
|
|
89
|
+
if (!entry || typeof entry !== "object") continue;
|
|
90
|
+
const role = (entry as { role?: unknown }).role;
|
|
91
|
+
const text = (entry as { text?: unknown }).text;
|
|
92
|
+
if ((role !== "user" && role !== "assistant") || typeof text !== "string") continue;
|
|
93
|
+
const trimmed = text.trim();
|
|
94
|
+
if (!trimmed) continue;
|
|
95
|
+
kept.push({ role, text: trimmed.slice(0, SUGGESTIONS_DEFAULTS.messageChars) });
|
|
96
|
+
}
|
|
97
|
+
const window = kept.slice(-Math.max(1, maxMessages));
|
|
98
|
+
let total = 0;
|
|
99
|
+
const out: ClippedMessage[] = [];
|
|
100
|
+
for (let i = window.length - 1; i >= 0; i--) {
|
|
101
|
+
total += window[i]!.text.length;
|
|
102
|
+
if (total > SUGGESTIONS_DEFAULTS.totalChars) break;
|
|
103
|
+
out.unshift(window[i]!);
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 真调模型。任何失败都抛给路由(统一 catch 后回落 static)。 */
|
|
109
|
+
export async function runSuggestions(env: SuggestionsEnv, input: SuggestionsInput): Promise<string[]> {
|
|
110
|
+
// 模型来源与建会话同源(manifest `agent.model`);model-config 的档位链是聊天主链
|
|
111
|
+
// 的东西,这条旁路不参与——失败静默,不值得为它引一整套 failover。
|
|
112
|
+
const tier = resolveTier(env.product);
|
|
113
|
+
if (!tier) throw new Error("product has no default model");
|
|
114
|
+
const model = env.modelRuntime.getModel(tier.provider, tier.model);
|
|
115
|
+
if (!model) throw new Error(`unknown model ${tier.provider}/${tier.model}`);
|
|
116
|
+
|
|
117
|
+
const dialogue = input.messages.map((m) => `${m.role}: ${m.text}`).join("\n");
|
|
118
|
+
const systemPrompt = input.systemPrompt?.trim() || defaultSystemPrompt(input.count);
|
|
119
|
+
const controller = new AbortController();
|
|
120
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
121
|
+
try {
|
|
122
|
+
// 双保险:signal 传进 provider 请求;不遵守 signal 的底层由 Promise.race 兜底超时。
|
|
123
|
+
const timeout = new Promise<never>((_, reject) => {
|
|
124
|
+
timer = setTimeout(() => {
|
|
125
|
+
controller.abort();
|
|
126
|
+
reject(new Error("suggestions timeout"));
|
|
127
|
+
}, Math.max(1, input.timeoutMs));
|
|
128
|
+
});
|
|
129
|
+
// 只构造一条 UserMessage 把对话拍平——AssistantMessage 必填字段太多,别手工造。
|
|
130
|
+
const reply = await Promise.race([
|
|
131
|
+
env.modelRuntime.completeSimple(
|
|
132
|
+
model,
|
|
133
|
+
{ systemPrompt, messages: [{ role: "user", content: `最近一段对话:\n${dialogue}`, timestamp: Date.now() }] },
|
|
134
|
+
{ signal: controller.signal, maxTokens: SUGGESTIONS_DEFAULTS.maxTokens },
|
|
135
|
+
),
|
|
136
|
+
timeout,
|
|
137
|
+
]);
|
|
138
|
+
const parts: string[] = [];
|
|
139
|
+
for (const c of reply.content) {
|
|
140
|
+
if (c.type === "text") parts.push(c.text);
|
|
141
|
+
}
|
|
142
|
+
return parseSuggestions(parts.join("\n"), input.count);
|
|
143
|
+
} finally {
|
|
144
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
145
|
+
}
|
|
146
|
+
}
|