@dabuliu_wsy/dsh-im 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +204 -3
- package/cordis.patch.yml +4 -0
- package/dist/core/agent-state.d.ts +38 -0
- package/dist/core/agent-state.js +59 -0
- package/dist/core/arbiter-bridge.d.ts +25 -0
- package/dist/core/arbiter-bridge.js +20 -0
- package/dist/core/arbiter.d.ts +29 -0
- package/dist/core/arbiter.js +59 -0
- package/dist/core/avatar-brands.d.ts +23 -0
- package/dist/core/avatar-brands.js +75 -0
- package/dist/core/avatar-presets.d.ts +21 -0
- package/dist/core/avatar-presets.js +38 -0
- package/dist/core/avatar.d.ts +1 -0
- package/dist/core/avatar.js +18 -0
- package/dist/core/blocked.d.ts +12 -0
- package/dist/core/blocked.js +23 -0
- package/dist/core/budget-plan.d.ts +41 -0
- package/dist/core/budget-plan.js +69 -0
- package/dist/core/calibration-samples.d.ts +77 -0
- package/dist/core/calibration-samples.js +168 -0
- package/dist/core/calibrator.d.ts +57 -0
- package/dist/core/calibrator.js +125 -0
- package/dist/core/chapter-tree.d.ts +128 -0
- package/dist/core/chapter-tree.js +400 -0
- package/dist/core/consensus.d.ts +135 -0
- package/dist/core/consensus.js +394 -0
- package/dist/core/context-budget.d.ts +153 -0
- package/dist/core/context-budget.js +183 -0
- package/dist/core/curator.d.ts +28 -0
- package/dist/core/curator.js +190 -0
- package/dist/core/dual-window.d.ts +86 -0
- package/dist/core/dual-window.js +175 -0
- package/dist/core/events.d.ts +110 -0
- package/dist/core/events.js +117 -0
- package/dist/core/facts.d.ts +114 -0
- package/dist/core/facts.js +400 -0
- package/dist/core/fence.d.ts +34 -0
- package/dist/core/fence.js +129 -0
- package/dist/core/floor.d.ts +18 -0
- package/dist/core/floor.js +80 -0
- package/dist/core/ids.d.ts +11 -0
- package/dist/core/ids.js +29 -0
- package/dist/core/im.d.ts +213 -0
- package/dist/core/im.js +1640 -0
- package/dist/core/index.d.ts +34 -0
- package/dist/core/index.js +34 -0
- package/dist/core/log.d.ts +21 -0
- package/dist/core/log.js +63 -0
- package/dist/core/memory-claims.d.ts +67 -0
- package/dist/core/memory-claims.js +164 -0
- package/dist/core/memory-context.d.ts +121 -0
- package/dist/core/memory-context.js +303 -0
- package/dist/core/memory-visibility.d.ts +58 -0
- package/dist/core/memory-visibility.js +36 -0
- package/dist/core/memory.d.ts +12 -0
- package/dist/core/memory.js +73 -0
- package/dist/core/metrics.d.ts +49 -0
- package/dist/core/metrics.js +89 -0
- package/dist/core/multi-hop.d.ts +116 -0
- package/dist/core/multi-hop.js +278 -0
- package/dist/core/persist.d.ts +21 -0
- package/dist/core/persist.js +94 -0
- package/dist/core/proactive.d.ts +54 -0
- package/dist/core/proactive.js +108 -0
- package/dist/core/prompt.d.ts +82 -0
- package/dist/core/prompt.js +164 -0
- package/dist/core/receipt.d.ts +107 -0
- package/dist/core/receipt.js +86 -0
- package/dist/core/retrieval.d.ts +69 -0
- package/dist/core/retrieval.js +260 -0
- package/dist/core/role-state.d.ts +16 -0
- package/dist/core/role-state.js +65 -0
- package/dist/core/runtime.d.ts +137 -0
- package/dist/core/runtime.js +574 -0
- package/dist/core/summary.d.ts +19 -0
- package/dist/core/summary.js +96 -0
- package/dist/core/text.d.ts +10 -0
- package/dist/core/text.js +45 -0
- package/dist/core/time.d.ts +22 -0
- package/dist/core/time.js +84 -0
- package/dist/core/tool-schema.d.ts +12 -0
- package/dist/core/tool-schema.js +92 -0
- package/dist/core/tools.d.ts +137 -0
- package/dist/core/tools.js +348 -0
- package/dist/core/trash.d.ts +110 -0
- package/dist/core/trash.js +56 -0
- package/dist/core/types.d.ts +400 -0
- package/dist/core/types.js +1 -0
- package/dist/core/window.d.ts +6 -0
- package/dist/core/window.js +7 -0
- package/dist/core/workspace-node.d.ts +28 -0
- package/dist/core/workspace-node.js +232 -0
- package/dist/core/workspace.d.ts +9 -0
- package/dist/core/workspace.js +45 -0
- package/dist/host/agent-session.d.ts +296 -0
- package/dist/host/agent-session.js +964 -0
- package/dist/host/attachment-gc.d.ts +37 -0
- package/dist/host/attachment-gc.js +97 -0
- package/dist/host/calibration-samples-store.d.ts +5 -0
- package/dist/host/calibration-samples-store.js +67 -0
- package/dist/host/capabilities.d.ts +45 -0
- package/dist/host/capabilities.js +67 -0
- package/dist/host/derived-store.d.ts +36 -0
- package/dist/host/derived-store.js +199 -0
- package/dist/host/dsh-llm.d.ts +143 -0
- package/dist/host/dsh-llm.js +395 -0
- package/dist/host/event-store.d.ts +47 -0
- package/dist/host/event-store.js +154 -0
- package/dist/host/fs-atomic.d.ts +1 -0
- package/dist/host/fs-atomic.js +26 -0
- package/dist/host/group-files.d.ts +26 -0
- package/dist/host/group-files.js +142 -0
- package/dist/host/im-api.d.ts +408 -0
- package/dist/host/im-api.js +1188 -0
- package/dist/host/index.d.ts +103 -0
- package/dist/host/index.js +509 -0
- package/dist/host/media-type.d.ts +10 -0
- package/dist/host/media-type.js +39 -0
- package/dist/host/memory-index.d.ts +65 -0
- package/dist/host/memory-index.js +181 -0
- package/dist/host/metrics-store.d.ts +7 -0
- package/dist/host/metrics-store.js +104 -0
- package/dist/host/models.d.ts +39 -0
- package/dist/host/models.js +67 -0
- package/dist/host/persona-tools.d.ts +57 -0
- package/dist/host/persona-tools.js +226 -0
- package/dist/host/preflight.d.ts +31 -0
- package/dist/host/preflight.js +29 -0
- package/dist/host/receipt-store.d.ts +26 -0
- package/dist/host/receipt-store.js +68 -0
- package/dist/host/run-check.d.ts +51 -0
- package/dist/host/run-check.js +190 -0
- package/dist/host/state-roots.d.ts +50 -0
- package/dist/host/state-roots.js +163 -0
- package/dist/host/tool-cache.d.ts +25 -0
- package/dist/host/tool-cache.js +90 -0
- package/dist/host/tool-call-budget.d.ts +29 -0
- package/dist/host/tool-call-budget.js +38 -0
- package/dist/host/trash-store.d.ts +32 -0
- package/dist/host/trash-store.js +316 -0
- package/dsh-plugin.json +25 -0
- package/lib/client.inner.cjs +3933 -0
- package/lib/client.js +3941 -0
- package/package.json +86 -6
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 附件垃圾回收(v1,默认 dryRun):
|
|
3
|
+
* 扫 `groups/<threadId>/attachments/`,把 state 里所有消息 attachments[].path 收成引用集,
|
|
4
|
+
* 未引用且 mtime 早于宽限期(默认 24h)的算孤儿。默认只报告,不删除;
|
|
5
|
+
* 宽限期用于避免把“上传成功但还没发出的草稿附件”当孤儿。
|
|
6
|
+
*
|
|
7
|
+
* 未做:内容 sha256 去重/.index.json、按 workspace id 的 URL 反查;留待后续。
|
|
8
|
+
*/
|
|
9
|
+
import { stat, unlink } from 'node:fs/promises';
|
|
10
|
+
/** 服务端硬下限:宽限期不允许低于 1h(客户端传再小也按 1h)。 */
|
|
11
|
+
export declare const MIN_GC_AGE_MS: number;
|
|
12
|
+
export interface GcOptions {
|
|
13
|
+
dryRun?: boolean;
|
|
14
|
+
minAgeMs?: number;
|
|
15
|
+
now?: () => number;
|
|
16
|
+
/** 测试注入:默认 node:fs/promises 的 stat。 */
|
|
17
|
+
statFile?: typeof stat;
|
|
18
|
+
/** 测试注入:默认 node:fs/promises 的 unlink。 */
|
|
19
|
+
unlinkFile?: typeof unlink;
|
|
20
|
+
}
|
|
21
|
+
export interface GcResult {
|
|
22
|
+
scanned: number;
|
|
23
|
+
referenced: number;
|
|
24
|
+
orphans: string[];
|
|
25
|
+
freed: number;
|
|
26
|
+
/** dryRun 时预计可释放的字节数(freed 只在真删成功后累加)。 */
|
|
27
|
+
wouldFree: number;
|
|
28
|
+
/** unlink 失败的个数(删除阶段)。 */
|
|
29
|
+
failed: number;
|
|
30
|
+
/** stat 失败的个数(扫描阶段:文件被删/权限不足)。 */
|
|
31
|
+
statFailed: number;
|
|
32
|
+
/** 读目录失败的个数(读不了不能当成“很干净”;ENOENT 单列 noDir,不算故障)。 */
|
|
33
|
+
errored: number;
|
|
34
|
+
noDir: number;
|
|
35
|
+
dryRun: boolean;
|
|
36
|
+
}
|
|
37
|
+
export declare function gcAttachments(projectRoot: string, threadId: string, options?: GcOptions): Promise<GcResult>;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 附件垃圾回收(v1,默认 dryRun):
|
|
3
|
+
* 扫 `groups/<threadId>/attachments/`,把 state 里所有消息 attachments[].path 收成引用集,
|
|
4
|
+
* 未引用且 mtime 早于宽限期(默认 24h)的算孤儿。默认只报告,不删除;
|
|
5
|
+
* 宽限期用于避免把“上传成功但还没发出的草稿附件”当孤儿。
|
|
6
|
+
*
|
|
7
|
+
* 未做:内容 sha256 去重/.index.json、按 workspace id 的 URL 反查;留待后续。
|
|
8
|
+
*/
|
|
9
|
+
import { readdir, stat, unlink } from 'node:fs/promises';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
import { assertSafeThreadId } from '../core/workspace.js';
|
|
12
|
+
import { loadState } from '../core/workspace-node.js';
|
|
13
|
+
import { groupDir } from './event-store.js';
|
|
14
|
+
/** 服务端硬下限:宽限期不允许低于 1h(客户端传再小也按 1h)。 */
|
|
15
|
+
export const MIN_GC_AGE_MS = 60 * 60 * 1000;
|
|
16
|
+
export async function gcAttachments(projectRoot, threadId, options = {}) {
|
|
17
|
+
assertSafeThreadId(threadId);
|
|
18
|
+
const dryRun = options.dryRun ?? true;
|
|
19
|
+
const minAgeMs = Math.max(MIN_GC_AGE_MS, options.minAgeMs ?? 24 * 60 * 60 * 1000);
|
|
20
|
+
const now = options.now?.() ?? Date.now();
|
|
21
|
+
const state = await loadState(projectRoot).catch(() => null);
|
|
22
|
+
if (!state) {
|
|
23
|
+
throw Object.assign(new Error('GC_ABORT:读不到 state.json,拒绝在“零引用”假设下清理附件'), { code: 'GC_ABORT' });
|
|
24
|
+
}
|
|
25
|
+
const thread = state.threads.find((item) => item.id === threadId);
|
|
26
|
+
if (!thread) {
|
|
27
|
+
throw Object.assign(new Error(`GC_ABORT:本工作区没有群 ${threadId},拒绝清理附件`), { code: 'GC_ABORT' });
|
|
28
|
+
}
|
|
29
|
+
const referenced = new Set();
|
|
30
|
+
for (const message of thread.messages ?? []) {
|
|
31
|
+
for (const attachment of message.attachments ?? []) {
|
|
32
|
+
if (attachment.path)
|
|
33
|
+
referenced.add(attachment.path.replaceAll('\\', '/'));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const dir = join(groupDir(projectRoot, threadId), 'attachments');
|
|
37
|
+
// 递归收集(v1 只有一层,但按日期/子目录存时引用集仍然对得上)。
|
|
38
|
+
const files = [];
|
|
39
|
+
let errored = 0;
|
|
40
|
+
let noDir = 0;
|
|
41
|
+
async function walk(current, prefix) {
|
|
42
|
+
let entries;
|
|
43
|
+
try {
|
|
44
|
+
entries = await readdir(current, { withFileTypes: true });
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error.code === 'ENOENT')
|
|
48
|
+
noDir += 1;
|
|
49
|
+
else
|
|
50
|
+
errored += 1;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
for (const entry of entries) {
|
|
54
|
+
if (entry.isDirectory()) {
|
|
55
|
+
await walk(join(current, entry.name), `${prefix}${entry.name}/`);
|
|
56
|
+
}
|
|
57
|
+
else if (entry.isFile()) {
|
|
58
|
+
files.push({ rel: `attachments/${prefix}${entry.name}`, full: join(current, entry.name) });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
await walk(dir, '');
|
|
63
|
+
const statFile = options.statFile ?? stat;
|
|
64
|
+
const unlinkFile = options.unlinkFile ?? unlink;
|
|
65
|
+
let scanned = 0;
|
|
66
|
+
let freed = 0;
|
|
67
|
+
let wouldFree = 0;
|
|
68
|
+
let failed = 0;
|
|
69
|
+
let statFailed = 0;
|
|
70
|
+
const orphans = [];
|
|
71
|
+
for (const file of files) {
|
|
72
|
+
scanned += 1;
|
|
73
|
+
if (referenced.has(file.rel))
|
|
74
|
+
continue;
|
|
75
|
+
const info = await statFile(file.full).catch(() => null);
|
|
76
|
+
if (!info) {
|
|
77
|
+
statFailed += 1;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (now - info.mtimeMs < minAgeMs)
|
|
81
|
+
continue;
|
|
82
|
+
orphans.push(file.rel);
|
|
83
|
+
if (dryRun) {
|
|
84
|
+
wouldFree += info.size;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
await unlinkFile(file.full);
|
|
89
|
+
freed += info.size;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// 删除失败不计入 freed,单独计数。
|
|
93
|
+
failed += 1;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return { scanned, referenced: referenced.size, orphans, freed, wouldFree, failed, statFailed, errored, noDir, dryRun };
|
|
97
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type CalibrationSampleRecord } from '../core/calibration-samples.ts';
|
|
2
|
+
export declare function calibrationSamplesPath(root: string): string;
|
|
3
|
+
export declare function readCalibrationSamples(root: string, limit?: number): Promise<CalibrationSampleRecord[]>;
|
|
4
|
+
/** 追加一条样本;滚动裁剪用「读-改-写 + rename」(文件小,500 条约几百 KB)。 */
|
|
5
|
+
export declare function appendCalibrationSample(root: string, sample: CalibrationSampleRecord): Promise<void>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 校准样本落盘(L1):`<root>/.dsh-im/calibration-samples.jsonl`,只追加、滚动保留最近 N 条。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不塞 metrics.jsonl:指标文件会被撑爆,而且"成本观测"和"校准样本"混在一起后就无法离线复算。
|
|
5
|
+
* 坏行跳过;读取时按 schema 过滤;写串行 + 原子写。
|
|
6
|
+
*/
|
|
7
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
8
|
+
import { dirname, join } from 'node:path';
|
|
9
|
+
import { CALIBRATION_SAMPLE_SCHEMA, MAX_CALIBRATION_SAMPLES, } from '../core/calibration-samples.js';
|
|
10
|
+
import { logWarn } from '../core/log.js';
|
|
11
|
+
export function calibrationSamplesPath(root) {
|
|
12
|
+
return join(root, '.dsh-im', 'calibration-samples.jsonl');
|
|
13
|
+
}
|
|
14
|
+
const chains = new Map();
|
|
15
|
+
function enqueue(key, task) {
|
|
16
|
+
const previous = chains.get(key) ?? Promise.resolve();
|
|
17
|
+
const next = previous.then(task, task);
|
|
18
|
+
chains.set(key, next.then(() => undefined, () => undefined));
|
|
19
|
+
return next;
|
|
20
|
+
}
|
|
21
|
+
export async function readCalibrationSamples(root, limit = MAX_CALIBRATION_SAMPLES) {
|
|
22
|
+
let raw;
|
|
23
|
+
try {
|
|
24
|
+
raw = await readFile(calibrationSamplesPath(root), 'utf8');
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (error.code === 'ENOENT')
|
|
28
|
+
return [];
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
const out = [];
|
|
32
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
33
|
+
const trimmed = line.trim();
|
|
34
|
+
if (!trimmed)
|
|
35
|
+
continue;
|
|
36
|
+
try {
|
|
37
|
+
const parsed = JSON.parse(trimmed);
|
|
38
|
+
if (parsed?.schema !== CALIBRATION_SAMPLE_SCHEMA)
|
|
39
|
+
continue;
|
|
40
|
+
if (typeof parsed.provider !== 'string' || typeof parsed.promptChars !== 'number')
|
|
41
|
+
continue;
|
|
42
|
+
out.push(parsed);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return limit > 0 ? out.slice(-limit) : out;
|
|
49
|
+
}
|
|
50
|
+
/** 追加一条样本;滚动裁剪用「读-改-写 + rename」(文件小,500 条约几百 KB)。 */
|
|
51
|
+
export function appendCalibrationSample(root, sample) {
|
|
52
|
+
const path = calibrationSamplesPath(root);
|
|
53
|
+
return enqueue(path, async () => {
|
|
54
|
+
try {
|
|
55
|
+
await mkdir(dirname(path), { recursive: true });
|
|
56
|
+
const existing = await readCalibrationSamples(root, 0);
|
|
57
|
+
existing.push(sample);
|
|
58
|
+
const kept = existing.slice(-MAX_CALIBRATION_SAMPLES);
|
|
59
|
+
const tmp = `${path}.tmp-${process.pid}-${Date.now().toString(36)}`;
|
|
60
|
+
await writeFile(tmp, kept.map((record) => JSON.stringify(record)).join('\n') + '\n', 'utf8');
|
|
61
|
+
await rename(tmp, path);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
logWarn('[dsh-im] 写校准样本失败(忽略)', { root, reason: String(error?.message ?? error) });
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 宿主能力探测(Phase 0 · 可寻址记忆层的前置裁决项)。
|
|
3
|
+
*
|
|
4
|
+
* 背景:`tokenizer 预算` / `usage 回执校准` / `embedding 排序` 这三件事都被写进过路线图,
|
|
5
|
+
* 但它们的**前提都是宿主真的暴露了对应能力**。`LlmService` 现在只有
|
|
6
|
+
* `listProviders / listModels / stream / resolveModelInfo`(`host/dsh-llm.ts`),
|
|
7
|
+
* 所以必须先探测、再决定 Phase 1 用「字符预算 + 保守系数」还是「真实 token 预算」。
|
|
8
|
+
*
|
|
9
|
+
* 探测分两类:
|
|
10
|
+
* 1. **形状探测**(同步、零成本):服务对象上有没有 `tokenize / embed / ...`;
|
|
11
|
+
* 2. **观测探测**(需要一次真实流式调用):`stream` 的 chunk 里有没有 usage 字段。
|
|
12
|
+
* 观测结果由调用方(`/api/im/llm-probe`)通过 `observations` 传进来,本模块不主动发请求。
|
|
13
|
+
*/
|
|
14
|
+
export type CapabilityState = 'available' | 'absent' | 'unknown';
|
|
15
|
+
export interface HostCapabilityReport {
|
|
16
|
+
schema: 1;
|
|
17
|
+
/** 模型目录可列(listModels)。 */
|
|
18
|
+
models: CapabilityState;
|
|
19
|
+
/** 流式调用可用(stream)。 */
|
|
20
|
+
stream: CapabilityState;
|
|
21
|
+
/** 上下文窗口可查(resolveModelInfo → context.contextWindow)。 */
|
|
22
|
+
contextWindow: CapabilityState;
|
|
23
|
+
/** 宿主是否有 tokenizer(决定能不能做真实 token 预算)。 */
|
|
24
|
+
tokenizer: CapabilityState;
|
|
25
|
+
/** 宿主是否有 embedding(决定 Phase 3 能不能做语义重排)。 */
|
|
26
|
+
embeddings: CapabilityState;
|
|
27
|
+
/** 流里是否带 usage(决定能不能做字符→token 比例校准)。 */
|
|
28
|
+
usage: CapabilityState;
|
|
29
|
+
/** 命中的方法名 / 字段路径,便于人工复核。 */
|
|
30
|
+
evidence: Record<string, string[]>;
|
|
31
|
+
/** 一句话结论,给面板/日志用。 */
|
|
32
|
+
summary: string;
|
|
33
|
+
}
|
|
34
|
+
export interface CapabilityObservations {
|
|
35
|
+
/** 真实流式调用中观察到的 usage 字段路径(如 `usage.promptTokens`)。 */
|
|
36
|
+
usageKeys?: readonly string[];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 从流式 chunk 里收集 usage 字段路径(含 `reason.usage` 这类嵌套位置)。
|
|
40
|
+
* 纯函数:不改 chunk,只报告"这条流里出现了哪些用量字段"。
|
|
41
|
+
*/
|
|
42
|
+
export declare function usageKeysInChunk(chunk: unknown, prefix?: string): string[];
|
|
43
|
+
export declare function detectHostCapabilities(llm: unknown, observations?: CapabilityObservations): HostCapabilityReport;
|
|
44
|
+
/** 面板/日志用:把报告压成一行,避免把 evidence 全打出去。 */
|
|
45
|
+
export declare function summarizeCapabilities(report: HostCapabilityReport): string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const TOKENIZER_KEYS = ['tokenize', 'tokenizer', 'countTokens', 'tokenCount', 'encode', 'encodeTokens'];
|
|
2
|
+
const EMBEDDING_KEYS = ['embed', 'embedding', 'embeddings', 'embedMany', 'embedText', 'embedBatch'];
|
|
3
|
+
const USAGE_FIELDS = ['promptTokens', 'completionTokens', 'totalTokens', 'inputTokens', 'outputTokens', 'input_tokens', 'output_tokens'];
|
|
4
|
+
function hits(target, keys) {
|
|
5
|
+
if (!target || (typeof target !== 'object' && typeof target !== 'function'))
|
|
6
|
+
return [];
|
|
7
|
+
return keys.filter((key) => typeof target[key] === 'function');
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 从流式 chunk 里收集 usage 字段路径(含 `reason.usage` 这类嵌套位置)。
|
|
11
|
+
* 纯函数:不改 chunk,只报告"这条流里出现了哪些用量字段"。
|
|
12
|
+
*/
|
|
13
|
+
export function usageKeysInChunk(chunk, prefix = '') {
|
|
14
|
+
if (!chunk || typeof chunk !== 'object')
|
|
15
|
+
return [];
|
|
16
|
+
const found = [];
|
|
17
|
+
const record = chunk;
|
|
18
|
+
for (const field of USAGE_FIELDS) {
|
|
19
|
+
const value = record[field];
|
|
20
|
+
if (value !== undefined && value !== null && typeof value !== 'function')
|
|
21
|
+
found.push(prefix ? `${prefix}.${field}` : field);
|
|
22
|
+
}
|
|
23
|
+
for (const nested of ['reason', 'usage', 'meta', 'stats']) {
|
|
24
|
+
const value = record[nested];
|
|
25
|
+
if (value && typeof value === 'object')
|
|
26
|
+
found.push(...usageKeysInChunk(value, prefix ? `${prefix}.${nested}` : nested));
|
|
27
|
+
}
|
|
28
|
+
return [...new Set(found)];
|
|
29
|
+
}
|
|
30
|
+
export function detectHostCapabilities(llm, observations = {}) {
|
|
31
|
+
const evidence = {};
|
|
32
|
+
const tokenizer = hits(llm, TOKENIZER_KEYS);
|
|
33
|
+
const embeddings = hits(llm, EMBEDDING_KEYS);
|
|
34
|
+
if (tokenizer.length)
|
|
35
|
+
evidence.tokenizer = tokenizer;
|
|
36
|
+
if (embeddings.length)
|
|
37
|
+
evidence.embeddings = embeddings;
|
|
38
|
+
const usageKeys = [...new Set(observations.usageKeys ?? [])];
|
|
39
|
+
if (usageKeys.length)
|
|
40
|
+
evidence.usage = usageKeys;
|
|
41
|
+
const state = (value) => (typeof value === 'function' ? 'available' : 'absent');
|
|
42
|
+
const report = {
|
|
43
|
+
schema: 1,
|
|
44
|
+
models: state(llm?.listModels),
|
|
45
|
+
stream: state(llm?.stream),
|
|
46
|
+
contextWindow: state(llm?.resolveModelInfo),
|
|
47
|
+
tokenizer: tokenizer.length ? 'available' : 'absent',
|
|
48
|
+
embeddings: embeddings.length ? 'available' : 'absent',
|
|
49
|
+
// usage 只能靠真实调用观测:没观测过就是 unknown(不是 absent)。
|
|
50
|
+
usage: usageKeys.length ? 'available' : 'unknown',
|
|
51
|
+
evidence,
|
|
52
|
+
summary: '',
|
|
53
|
+
};
|
|
54
|
+
report.summary = [
|
|
55
|
+
`models=${report.models}`,
|
|
56
|
+
`stream=${report.stream}`,
|
|
57
|
+
`contextWindow=${report.contextWindow}`,
|
|
58
|
+
`tokenizer=${report.tokenizer}`,
|
|
59
|
+
`usage=${report.usage}`,
|
|
60
|
+
`embeddings=${report.embeddings}`,
|
|
61
|
+
].join(' / ');
|
|
62
|
+
return report;
|
|
63
|
+
}
|
|
64
|
+
/** 面板/日志用:把报告压成一行,避免把 evidence 全打出去。 */
|
|
65
|
+
export function summarizeCapabilities(report) {
|
|
66
|
+
return report.summary;
|
|
67
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ChatEvent } from '../core/events.ts';
|
|
2
|
+
import type { Fact, FloorDecision, Message, PendingFactOp } from '../core/types.ts';
|
|
3
|
+
export declare function factsPath(root: string, threadId: string): string;
|
|
4
|
+
export interface FactsFile {
|
|
5
|
+
version: string;
|
|
6
|
+
facts: Fact[];
|
|
7
|
+
updatedAt: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 消息 → fact 事件(只追加不投影)。
|
|
11
|
+
*
|
|
12
|
+
* 幂等规则:
|
|
13
|
+
* - 同一 factId 已经是同样或更高优先级的有效事实 → 不重复写;
|
|
14
|
+
* - 同一个 factId 已被**更新**的事实撤回(retract 事件比声明消息更晚)→ 不复活;
|
|
15
|
+
* 用户再说一次(新消息 seq 更大)会重新产生 assert。
|
|
16
|
+
*/
|
|
17
|
+
export declare function appendDerivedFactEvents(root: string, threadId: string, messages: readonly Message[]): Promise<number>;
|
|
18
|
+
/** 事件流 → facts.json 物化投影(唯一写 projections 的地方)。 */
|
|
19
|
+
export declare function projectAndWriteFacts(root: string, threadId: string): Promise<FactsFile>;
|
|
20
|
+
/** 消息 → fact 事件 + 投影(独立入口,测试/工具用)。 */
|
|
21
|
+
export declare function syncThreadFacts(root: string, threadId: string, messages: readonly Message[]): Promise<FactsFile>;
|
|
22
|
+
export declare function readFacts(root: string, threadId: string): Promise<FactsFile | null>;
|
|
23
|
+
/**
|
|
24
|
+
* 客户端产生的事实操作(异步抽取 / 任务板归档)→ `fact` 事件(幂等)。
|
|
25
|
+
*
|
|
26
|
+
* 保存后 state.json 就不需要再带这些操作(重新投影即可)。
|
|
27
|
+
*/
|
|
28
|
+
export declare function syncPendingFactOps(root: string, threadId: string, ops: readonly PendingFactOp[]): Promise<number>;
|
|
29
|
+
/**
|
|
30
|
+
* 本轮仲裁决策 → `floor_decision` 事件(幂等)。
|
|
31
|
+
*
|
|
32
|
+
* `thread.floorDecisions` 每轮会被清空,但事件日志保留全部历史,可按 seq 回放。
|
|
33
|
+
*/
|
|
34
|
+
export declare function syncThreadFloorDecisions(root: string, threadId: string, decisions: readonly FloorDecision[]): Promise<number>;
|
|
35
|
+
/** 读回某个群的全部仲裁决策(回放/调试用)。 */
|
|
36
|
+
export declare function readFloorDecisions(root: string, threadId: string): Promise<ChatEvent[]>;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 由消息/仲裁记录推导出来的事件(facts / floor_decision)落盘与物化投影。
|
|
3
|
+
*
|
|
4
|
+
* 过渡期事实来自确定性规则抽取(`core/facts.ts`);同一事实的 factId 由内容 hash 派生,
|
|
5
|
+
* 所以重复保存不会重复写事件(幂等)。以后接 LLM 抽取时只新增 supersede/retract 事件,
|
|
6
|
+
* 这里不用改。
|
|
7
|
+
*/
|
|
8
|
+
import { appendFile, mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
9
|
+
import { join } from 'node:path';
|
|
10
|
+
import { factEventsFromMessages, projectFacts, SOURCE_PRIORITY } from '../core/facts.js';
|
|
11
|
+
import { appendEvent } from '../core/events.js';
|
|
12
|
+
import { eventsPath, groupDir, readEvents } from './event-store.js';
|
|
13
|
+
export function factsPath(root, threadId) {
|
|
14
|
+
return join(groupDir(root, threadId), 'facts.json');
|
|
15
|
+
}
|
|
16
|
+
/** 每个 factId 最后一条事件(判断“已被归档”/“已是最新声明”)。 */
|
|
17
|
+
function lastFactEvents(events) {
|
|
18
|
+
const last = new Map();
|
|
19
|
+
for (const event of events) {
|
|
20
|
+
if (event.kind !== 'fact' || !event.fact)
|
|
21
|
+
continue;
|
|
22
|
+
const previous = last.get(event.fact.factId);
|
|
23
|
+
if (!previous || event.seq > previous.seq)
|
|
24
|
+
last.set(event.fact.factId, event);
|
|
25
|
+
}
|
|
26
|
+
return last;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 消息 → fact 事件(只追加不投影)。
|
|
30
|
+
*
|
|
31
|
+
* 幂等规则:
|
|
32
|
+
* - 同一 factId 已经是同样或更高优先级的有效事实 → 不重复写;
|
|
33
|
+
* - 同一个 factId 已被**更新**的事实撤回(retract 事件比声明消息更晚)→ 不复活;
|
|
34
|
+
* 用户再说一次(新消息 seq 更大)会重新产生 assert。
|
|
35
|
+
*/
|
|
36
|
+
export async function appendDerivedFactEvents(root, threadId, messages) {
|
|
37
|
+
const existing = await readEvents(root, threadId);
|
|
38
|
+
const projection = projectFacts(existing);
|
|
39
|
+
const activeById = new Map(projection.active.map((fact) => [fact.factId, fact]));
|
|
40
|
+
const lastById = lastFactEvents(existing);
|
|
41
|
+
let events = existing;
|
|
42
|
+
const lines = [];
|
|
43
|
+
for (const item of factEventsFromMessages(messages)) {
|
|
44
|
+
const factId = item.fact.factId;
|
|
45
|
+
const last = lastById.get(factId);
|
|
46
|
+
// 撤回发生在这条声明之后:说明事实已被用户归档,不要重新拉起来(新消息声明会更晚,能恢复)。
|
|
47
|
+
if (last?.fact?.op === 'retract'
|
|
48
|
+
&& SOURCE_PRIORITY[last.fact.source] >= SOURCE_PRIORITY[item.fact.source]
|
|
49
|
+
&& last.at > item.at) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const active = activeById.get(factId);
|
|
53
|
+
if (active && SOURCE_PRIORITY[active.source] >= SOURCE_PRIORITY[item.fact.source])
|
|
54
|
+
continue;
|
|
55
|
+
const event = appendEvent(events, {
|
|
56
|
+
at: item.at,
|
|
57
|
+
kind: 'fact',
|
|
58
|
+
actorName: item.fact.owner ?? '群',
|
|
59
|
+
text: item.text,
|
|
60
|
+
producer: 'plugin',
|
|
61
|
+
fact: item.fact,
|
|
62
|
+
});
|
|
63
|
+
events = [...events, event];
|
|
64
|
+
lines.push(JSON.stringify(event));
|
|
65
|
+
}
|
|
66
|
+
if (lines.length === 0)
|
|
67
|
+
return 0;
|
|
68
|
+
await mkdir(groupDir(root, threadId), { recursive: true });
|
|
69
|
+
await appendFile(eventsPath(root, threadId), lines.join('\n') + '\n', 'utf8');
|
|
70
|
+
return lines.length;
|
|
71
|
+
}
|
|
72
|
+
/** 事件流 → facts.json 物化投影(唯一写 projections 的地方)。 */
|
|
73
|
+
export async function projectAndWriteFacts(root, threadId) {
|
|
74
|
+
const events = await readEvents(root, threadId);
|
|
75
|
+
const projection = projectFacts(events);
|
|
76
|
+
const file = { version: projection.version, facts: projection.active, updatedAt: Date.now() };
|
|
77
|
+
await mkdir(groupDir(root, threadId), { recursive: true });
|
|
78
|
+
await writeFile(factsPath(root, threadId), JSON.stringify(file, null, 2), 'utf8');
|
|
79
|
+
return file;
|
|
80
|
+
}
|
|
81
|
+
/** 消息 → fact 事件 + 投影(独立入口,测试/工具用)。 */
|
|
82
|
+
export async function syncThreadFacts(root, threadId, messages) {
|
|
83
|
+
await appendDerivedFactEvents(root, threadId, messages);
|
|
84
|
+
return projectAndWriteFacts(root, threadId);
|
|
85
|
+
}
|
|
86
|
+
export async function readFacts(root, threadId) {
|
|
87
|
+
try {
|
|
88
|
+
const parsed = JSON.parse(await readFile(factsPath(root, threadId), 'utf8'));
|
|
89
|
+
return Array.isArray(parsed.facts) ? parsed : null;
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (error.code === 'ENOENT')
|
|
93
|
+
return null;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function decisionKey(decision) {
|
|
98
|
+
return [decision.personaId, decision.action, decision.reason, decision.at].join('|');
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* 客户端产生的事实操作(异步抽取 / 任务板归档)→ `fact` 事件(幂等)。
|
|
102
|
+
*
|
|
103
|
+
* 保存后 state.json 就不需要再带这些操作(重新投影即可)。
|
|
104
|
+
*/
|
|
105
|
+
export async function syncPendingFactOps(root, threadId, ops) {
|
|
106
|
+
if (ops.length === 0)
|
|
107
|
+
return 0;
|
|
108
|
+
const existing = await readEvents(root, threadId);
|
|
109
|
+
// 幂等键带 at:同一次用户操作重复保存不会重复写;但同一事实被再次归档(新时间)会真的生效。
|
|
110
|
+
const seen = new Set(existing
|
|
111
|
+
.filter((event) => event.kind === 'fact' && event.fact)
|
|
112
|
+
.map((event) => [event.fact?.op, event.fact?.factId, event.text, event.at].join('|')));
|
|
113
|
+
const lastById = lastFactEvents(existing);
|
|
114
|
+
let events = existing;
|
|
115
|
+
const lines = [];
|
|
116
|
+
for (const op of ops) {
|
|
117
|
+
const key = [op.op, op.factId, op.text, op.at].join('|');
|
|
118
|
+
if (seen.has(key))
|
|
119
|
+
continue;
|
|
120
|
+
// 断言/替换:如果该事实已被同等或更高优先级来源归档,就不要复活它。
|
|
121
|
+
if (op.op !== 'retract') {
|
|
122
|
+
const last = lastById.get(op.factId);
|
|
123
|
+
if (last?.fact?.op === 'retract' && SOURCE_PRIORITY[last.fact.source] >= SOURCE_PRIORITY[op.source])
|
|
124
|
+
continue;
|
|
125
|
+
const active = last?.fact?.op !== 'retract' ? projectFacts(events).active.find((fact) => fact.factId === op.factId) : undefined;
|
|
126
|
+
if (active && SOURCE_PRIORITY[active.source] >= SOURCE_PRIORITY[op.source])
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const input = {
|
|
130
|
+
at: op.at,
|
|
131
|
+
kind: 'fact',
|
|
132
|
+
actorName: op.owner ?? '群',
|
|
133
|
+
text: op.text,
|
|
134
|
+
producer: 'plugin',
|
|
135
|
+
fact: {
|
|
136
|
+
op: op.op,
|
|
137
|
+
factId: op.factId,
|
|
138
|
+
factKind: op.kind,
|
|
139
|
+
source: op.source,
|
|
140
|
+
confidence: op.source === 'user' ? 1 : op.source === 'persona' ? 0.8 : 0.5,
|
|
141
|
+
...(op.owner === undefined ? {} : { owner: op.owner }),
|
|
142
|
+
...(op.supersedes === undefined ? {} : { supersedes: op.supersedes }),
|
|
143
|
+
...(op.sourceMessageId === undefined ? {} : { sourceMessageId: op.sourceMessageId }),
|
|
144
|
+
...(op.sourceMessageSeq === undefined ? {} : { sourceMessageSeq: op.sourceMessageSeq }),
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
seen.add(key);
|
|
148
|
+
const event = appendEvent(events, input);
|
|
149
|
+
events = [...events, event];
|
|
150
|
+
lastById.set(op.factId, event);
|
|
151
|
+
lines.push(JSON.stringify(event));
|
|
152
|
+
}
|
|
153
|
+
if (lines.length === 0)
|
|
154
|
+
return 0;
|
|
155
|
+
await mkdir(groupDir(root, threadId), { recursive: true });
|
|
156
|
+
await appendFile(eventsPath(root, threadId), lines.join('\n') + '\n', 'utf8');
|
|
157
|
+
return lines.length;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* 本轮仲裁决策 → `floor_decision` 事件(幂等)。
|
|
161
|
+
*
|
|
162
|
+
* `thread.floorDecisions` 每轮会被清空,但事件日志保留全部历史,可按 seq 回放。
|
|
163
|
+
*/
|
|
164
|
+
export async function syncThreadFloorDecisions(root, threadId, decisions) {
|
|
165
|
+
if (decisions.length === 0)
|
|
166
|
+
return 0;
|
|
167
|
+
const existing = await readEvents(root, threadId);
|
|
168
|
+
const seen = new Set(existing
|
|
169
|
+
.filter((event) => event.kind === 'floor_decision' && event.floorDecision)
|
|
170
|
+
.map((event) => [event.floorDecision?.personaId, event.floorDecision?.action, event.text, event.at].join('|')));
|
|
171
|
+
let events = existing;
|
|
172
|
+
const lines = [];
|
|
173
|
+
for (const decision of decisions) {
|
|
174
|
+
const key = decisionKey(decision);
|
|
175
|
+
if (seen.has(key))
|
|
176
|
+
continue;
|
|
177
|
+
seen.add(key);
|
|
178
|
+
const payload = { personaId: decision.personaId, action: decision.action };
|
|
179
|
+
const event = appendEvent(events, {
|
|
180
|
+
at: decision.at,
|
|
181
|
+
kind: 'floor_decision',
|
|
182
|
+
actorName: decision.personaId || '系统主持',
|
|
183
|
+
text: decision.reason,
|
|
184
|
+
producer: 'plugin',
|
|
185
|
+
floorDecision: payload,
|
|
186
|
+
});
|
|
187
|
+
events = [...events, event];
|
|
188
|
+
lines.push(JSON.stringify(event));
|
|
189
|
+
}
|
|
190
|
+
if (!lines.length)
|
|
191
|
+
return 0;
|
|
192
|
+
await mkdir(groupDir(root, threadId), { recursive: true });
|
|
193
|
+
await appendFile(eventsPath(root, threadId), lines.join('\n') + '\n', 'utf8');
|
|
194
|
+
return lines.length;
|
|
195
|
+
}
|
|
196
|
+
/** 读回某个群的全部仲裁决策(回放/调试用)。 */
|
|
197
|
+
export async function readFloorDecisions(root, threadId) {
|
|
198
|
+
return (await readEvents(root, threadId)).filter((event) => event.kind === 'floor_decision');
|
|
199
|
+
}
|