@njuptlzf/agent-chat-server 0.7.0 → 0.8.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/session.d.ts +10 -1
- package/dist/session.js +53 -0
- package/package.json +2 -2
- package/scripts/agentchat-integrate.mjs +5 -5
- package/src/session.ts +64 -1
package/dist/session.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ModelRuntime, type AgentSession, type FileEntry, type McpServerConfig, type Skill } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { McpServerDecl } from "@njuptlzf/agent-chat-protocol";
|
|
2
|
+
import type { McpServerDecl, MemoryCompletion } from "@njuptlzf/agent-chat-protocol";
|
|
3
3
|
import { type McpConfigStore } from "./mcp-config.ts";
|
|
4
4
|
import type { LoadedProduct, ResolvedTier, ToolContext } from "./manifest.ts";
|
|
5
5
|
import { type PersonaConfigStore, type PersonaOverride } from "./persona-config.ts";
|
|
@@ -203,6 +203,15 @@ export declare function compileSystemPrompt(product: LoadedProduct, qual: string
|
|
|
203
203
|
allowTools?: string[];
|
|
204
204
|
disabledTools: string[];
|
|
205
205
|
};
|
|
206
|
+
/**
|
|
207
|
+
* 把宿主的 `modelRuntime` 适配成注入 provider 的 `MemoryCompletion`。
|
|
208
|
+
*
|
|
209
|
+
* 复用当前会话已解析的 model(与聊天同一物理模型),但**不进用户会话**:
|
|
210
|
+
* 一次 `completeSimple` 调用,独立历史,失败以 `ok:false` 结构化返回,
|
|
211
|
+
* 不参与聊天主链的 failover,也不向 provider 抛异常。超时双保险:signal
|
|
212
|
+
* 传给 provider 请求,Promise.race 兜底不遵守 signal 的底层实现。
|
|
213
|
+
*/
|
|
214
|
+
export declare function createMemoryCompletion(modelRuntime: ModelRuntime, model: NonNullable<ReturnType<ModelRuntime["getModel"]>>): MemoryCompletion;
|
|
206
215
|
/**
|
|
207
216
|
* 产品依赖跨会话画像,但选中的 provider 没声明该能力时给宿主的可读告警(C6)。
|
|
208
217
|
* 导出为纯函数,便于契约测试钉住文案;返回 null 表示无需告警。
|
package/dist/session.js
CHANGED
|
@@ -358,6 +358,56 @@ export function compileSystemPrompt(product, qual, persona, override) {
|
|
|
358
358
|
* session also means a crashed session cannot leak a handle into the next one.
|
|
359
359
|
*/
|
|
360
360
|
const memorySessionsByAgent = new WeakMap();
|
|
361
|
+
/** 后台记忆补全的硬边界:超时与 token 都由宿主定,provider 只能通过请求收紧、不能放宽。 */
|
|
362
|
+
const MEMORY_COMPLETION_TIMEOUT_MS = 15_000;
|
|
363
|
+
const MEMORY_COMPLETION_MAX_TOKENS = 512;
|
|
364
|
+
/**
|
|
365
|
+
* 把宿主的 `modelRuntime` 适配成注入 provider 的 `MemoryCompletion`。
|
|
366
|
+
*
|
|
367
|
+
* 复用当前会话已解析的 model(与聊天同一物理模型),但**不进用户会话**:
|
|
368
|
+
* 一次 `completeSimple` 调用,独立历史,失败以 `ok:false` 结构化返回,
|
|
369
|
+
* 不参与聊天主链的 failover,也不向 provider 抛异常。超时双保险:signal
|
|
370
|
+
* 传给 provider 请求,Promise.race 兜底不遵守 signal 的底层实现。
|
|
371
|
+
*/
|
|
372
|
+
export function createMemoryCompletion(modelRuntime, model) {
|
|
373
|
+
return async (req) => {
|
|
374
|
+
const controller = new AbortController();
|
|
375
|
+
// 调用方 signal(例如会话销毁)要能取消这次后台补全。
|
|
376
|
+
const onAbort = () => controller.abort();
|
|
377
|
+
req.signal?.addEventListener("abort", onAbort, { once: true });
|
|
378
|
+
let timer;
|
|
379
|
+
try {
|
|
380
|
+
const timeout = new Promise((_, reject) => {
|
|
381
|
+
timer = setTimeout(() => {
|
|
382
|
+
controller.abort();
|
|
383
|
+
reject(new Error("memory completion timeout"));
|
|
384
|
+
}, MEMORY_COMPLETION_TIMEOUT_MS);
|
|
385
|
+
});
|
|
386
|
+
const reply = await Promise.race([
|
|
387
|
+
modelRuntime.completeSimple(model, { systemPrompt: req.systemPrompt, messages: [{ role: "user", content: req.userPrompt, timestamp: Date.now() }] }, {
|
|
388
|
+
signal: controller.signal,
|
|
389
|
+
maxTokens: Math.min(req.maxTokens ?? MEMORY_COMPLETION_MAX_TOKENS, MEMORY_COMPLETION_MAX_TOKENS),
|
|
390
|
+
}),
|
|
391
|
+
timeout,
|
|
392
|
+
]);
|
|
393
|
+
if (req.signal?.aborted)
|
|
394
|
+
return { text: "", ok: false, error: "aborted" };
|
|
395
|
+
const text = reply.content
|
|
396
|
+
.filter((c) => c.type === "text")
|
|
397
|
+
.map((c) => c.text)
|
|
398
|
+
.join("\n");
|
|
399
|
+
return { text, ok: true };
|
|
400
|
+
}
|
|
401
|
+
catch (err) {
|
|
402
|
+
return { text: "", ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
403
|
+
}
|
|
404
|
+
finally {
|
|
405
|
+
if (timer !== undefined)
|
|
406
|
+
clearTimeout(timer);
|
|
407
|
+
req.signal?.removeEventListener("abort", onAbort);
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
}
|
|
361
411
|
/**
|
|
362
412
|
* 产品依赖跨会话画像,但选中的 provider 没声明该能力时给宿主的可读告警(C6)。
|
|
363
413
|
* 导出为纯函数,便于契约测试钉住文案;返回 null 表示无需告警。
|
|
@@ -518,6 +568,9 @@ export async function createProductSession(opts) {
|
|
|
518
568
|
sharedRoot,
|
|
519
569
|
agentDir: opts.dataDir,
|
|
520
570
|
scope: { userId: opts.userId, productId: product.manifest.id, qual: opts.qual || "" },
|
|
571
|
+
// 后台整理(巩固/review/flush)用的补全能力。复用本会话已解析的 model,
|
|
572
|
+
// 不进用户会话历史;provider 拿不到凭据,超时/预算由这里统一控。
|
|
573
|
+
complete: createMemoryCompletion(modelRuntime, model),
|
|
521
574
|
})
|
|
522
575
|
: null;
|
|
523
576
|
const memoryExtensions = memorySession ? [memorySession.factory] : [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@njuptlzf/agent-chat-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "agentChat platform kernel: sessions, authz, billing, audit, model/MCP/persona config planes, and the product-directory loader.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@earendil-works/pi-coding-agent": "1.5.2",
|
|
58
|
-
"@njuptlzf/agent-chat-protocol": "^0.
|
|
58
|
+
"@njuptlzf/agent-chat-protocol": "^0.8.0",
|
|
59
59
|
"@njuptlzf/pi-hermes-memory": "0.11.0-scoped.5",
|
|
60
60
|
"casbin": "^5.51.1",
|
|
61
61
|
"taskflow-core": "1.0.0",
|
|
@@ -20,7 +20,7 @@ import { createRequire } from "node:module";
|
|
|
20
20
|
import { spawnSync } from "node:child_process";
|
|
21
21
|
import process from "node:process";
|
|
22
22
|
|
|
23
|
-
const CLI_VERSION = "0.
|
|
23
|
+
const CLI_VERSION = "0.8.0";
|
|
24
24
|
const MIN_SERVER = "0.2.0";
|
|
25
25
|
const PKG = {
|
|
26
26
|
protocol: "@njuptlzf/agent-chat-protocol",
|
|
@@ -215,9 +215,9 @@ function packageTemplate(o) {
|
|
|
215
215
|
private: true,
|
|
216
216
|
type: "module",
|
|
217
217
|
dependencies: {
|
|
218
|
-
[PKG.protocol]: "^0.
|
|
219
|
-
[PKG.server]: "^0.
|
|
220
|
-
[PKG.web]: "^0.
|
|
218
|
+
[PKG.protocol]: "^0.8.0",
|
|
219
|
+
[PKG.server]: "^0.8.0",
|
|
220
|
+
[PKG.web]: "^0.8.0",
|
|
221
221
|
},
|
|
222
222
|
devDependencies: {
|
|
223
223
|
tsx: "^4.20.0",
|
|
@@ -408,7 +408,7 @@ function checkDeps(add, dir) {
|
|
|
408
408
|
const all = { ...(pj.dependencies ?? {}), ...(pj.devDependencies ?? {}) };
|
|
409
409
|
const missing = Object.values(PKG).filter((n) => !(n in all));
|
|
410
410
|
if (missing.length) add("FAIL", "deps", `未声明平台依赖:${missing.join("、")}`,
|
|
411
|
-
`npm i ${PKG.protocol}@^0.
|
|
411
|
+
`npm i ${PKG.protocol}@^0.8.0 ${PKG.server}@^0.8.0 ${PKG.web}@^0.8.0`);
|
|
412
412
|
else add("OK", "deps", "三个平台包依赖已声明");
|
|
413
413
|
|
|
414
414
|
try {
|
package/src/session.ts
CHANGED
|
@@ -23,7 +23,12 @@ import {
|
|
|
23
23
|
type Theme,
|
|
24
24
|
type ToolDefinition,
|
|
25
25
|
} from "@earendil-works/pi-coding-agent";
|
|
26
|
-
import type {
|
|
26
|
+
import type {
|
|
27
|
+
McpServerDecl,
|
|
28
|
+
MemoryCompletion,
|
|
29
|
+
MemoryCompletionResult,
|
|
30
|
+
MemoryProviderSession,
|
|
31
|
+
} from "@njuptlzf/agent-chat-protocol";
|
|
27
32
|
import { toProxySpawn, type McpConfigStore } from "./mcp-config.ts";
|
|
28
33
|
import { ensureDefaultMemoryProvider, loadMemoryProviderModule, providerSupportsSharedProfile, resolveMemoryProvider } from "./memory-providers.ts";
|
|
29
34
|
import type { LoadedProduct, ResolvedTier, ToolContext } from "./manifest.ts";
|
|
@@ -497,6 +502,61 @@ export function compileSystemPrompt(
|
|
|
497
502
|
*/
|
|
498
503
|
const memorySessionsByAgent = new WeakMap<AgentSession, MemoryProviderSession<ExtensionAPI>>();
|
|
499
504
|
|
|
505
|
+
/** 后台记忆补全的硬边界:超时与 token 都由宿主定,provider 只能通过请求收紧、不能放宽。 */
|
|
506
|
+
const MEMORY_COMPLETION_TIMEOUT_MS = 15_000;
|
|
507
|
+
const MEMORY_COMPLETION_MAX_TOKENS = 512;
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* 把宿主的 `modelRuntime` 适配成注入 provider 的 `MemoryCompletion`。
|
|
511
|
+
*
|
|
512
|
+
* 复用当前会话已解析的 model(与聊天同一物理模型),但**不进用户会话**:
|
|
513
|
+
* 一次 `completeSimple` 调用,独立历史,失败以 `ok:false` 结构化返回,
|
|
514
|
+
* 不参与聊天主链的 failover,也不向 provider 抛异常。超时双保险:signal
|
|
515
|
+
* 传给 provider 请求,Promise.race 兜底不遵守 signal 的底层实现。
|
|
516
|
+
*/
|
|
517
|
+
export function createMemoryCompletion(
|
|
518
|
+
modelRuntime: ModelRuntime,
|
|
519
|
+
model: NonNullable<ReturnType<ModelRuntime["getModel"]>>,
|
|
520
|
+
): MemoryCompletion {
|
|
521
|
+
return async (req): Promise<MemoryCompletionResult> => {
|
|
522
|
+
const controller = new AbortController();
|
|
523
|
+
// 调用方 signal(例如会话销毁)要能取消这次后台补全。
|
|
524
|
+
const onAbort = () => controller.abort();
|
|
525
|
+
req.signal?.addEventListener("abort", onAbort, { once: true });
|
|
526
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
527
|
+
try {
|
|
528
|
+
const timeout = new Promise<never>((_, reject) => {
|
|
529
|
+
timer = setTimeout(() => {
|
|
530
|
+
controller.abort();
|
|
531
|
+
reject(new Error("memory completion timeout"));
|
|
532
|
+
}, MEMORY_COMPLETION_TIMEOUT_MS);
|
|
533
|
+
});
|
|
534
|
+
const reply = await Promise.race([
|
|
535
|
+
modelRuntime.completeSimple(
|
|
536
|
+
model,
|
|
537
|
+
{ systemPrompt: req.systemPrompt, messages: [{ role: "user", content: req.userPrompt, timestamp: Date.now() }] },
|
|
538
|
+
{
|
|
539
|
+
signal: controller.signal,
|
|
540
|
+
maxTokens: Math.min(req.maxTokens ?? MEMORY_COMPLETION_MAX_TOKENS, MEMORY_COMPLETION_MAX_TOKENS),
|
|
541
|
+
},
|
|
542
|
+
),
|
|
543
|
+
timeout,
|
|
544
|
+
]);
|
|
545
|
+
if (req.signal?.aborted) return { text: "", ok: false, error: "aborted" };
|
|
546
|
+
const text = reply.content
|
|
547
|
+
.filter((c) => c.type === "text")
|
|
548
|
+
.map((c) => (c as { type: "text"; text: string }).text)
|
|
549
|
+
.join("\n");
|
|
550
|
+
return { text, ok: true };
|
|
551
|
+
} catch (err) {
|
|
552
|
+
return { text: "", ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
553
|
+
} finally {
|
|
554
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
555
|
+
req.signal?.removeEventListener("abort", onAbort);
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
500
560
|
/**
|
|
501
561
|
* 产品依赖跨会话画像,但选中的 provider 没声明该能力时给宿主的可读告警(C6)。
|
|
502
562
|
* 导出为纯函数,便于契约测试钉住文案;返回 null 表示无需告警。
|
|
@@ -668,6 +728,9 @@ export async function createProductSession(opts: CreateProductSessionOptions): P
|
|
|
668
728
|
sharedRoot,
|
|
669
729
|
agentDir: opts.dataDir,
|
|
670
730
|
scope: { userId: opts.userId, productId: product.manifest.id, qual: opts.qual || "" },
|
|
731
|
+
// 后台整理(巩固/review/flush)用的补全能力。复用本会话已解析的 model,
|
|
732
|
+
// 不进用户会话历史;provider 拿不到凭据,超时/预算由这里统一控。
|
|
733
|
+
complete: createMemoryCompletion(modelRuntime, model),
|
|
671
734
|
})
|
|
672
735
|
: null;
|
|
673
736
|
const memoryExtensions = memorySession ? [memorySession.factory] : [];
|