dsh-topic-split 0.0.0-stage → 0.12.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/CHANGELOG.md +385 -0
- package/LICENSE +674 -0
- package/README.md +417 -2
- package/SECURITY.md +68 -0
- package/client/index.js +985 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +6 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +66 -4
- package/src/config.js +141 -0
- package/src/cut.js +54 -0
- package/src/diagnostics.js +71 -0
- package/src/errors.js +43 -0
- package/src/exporter.js +149 -0
- package/src/http-auth.js +183 -0
- package/src/index.js +122 -0
- package/src/judge.js +350 -0
- package/src/log.js +39 -0
- package/src/registry.js +219 -0
- package/src/repair.js +75 -0
- package/src/routes.js +349 -0
- package/src/selection.js +154 -0
- package/src/semantic.js +45 -0
- package/src/store.js +272 -0
- package/src/tools.js +392 -0
- package/src/topics.js +108 -0
- package/src/zstd.js +82 -0
package/src/http-auth.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/** HTTP 面板路由的安全层:令牌鉴权、Host/Origin 校验、限流、令牌注入。
|
|
2
|
+
*
|
|
3
|
+
* 背景:插件向 DSH webServer 注册的路由不经过宿主自身的 token 鉴权,
|
|
4
|
+
* 因此必须自己把关,否则任何本机进程(以及可能的 DNS rebinding)都能读取
|
|
5
|
+
* 会话元数据或触发导出。
|
|
6
|
+
*/
|
|
7
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto'
|
|
8
|
+
|
|
9
|
+
const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '::1'])
|
|
10
|
+
/** 客户端携带令牌的请求头。 */
|
|
11
|
+
export const TOKEN_HEADER = 'x-dsh-split-token'
|
|
12
|
+
/** 服务端注入页面、客户端读取令牌的 meta 名。 */
|
|
13
|
+
export const TOKEN_META_NAME = 'dsh-split-token'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 判断连接对端地址是否为本机(IPv4/IPv6/映射地址)。
|
|
17
|
+
* @param {unknown} address socket.remoteAddress
|
|
18
|
+
* @returns {boolean}
|
|
19
|
+
*/
|
|
20
|
+
export function isLoopbackAddress(address) {
|
|
21
|
+
const raw = String(address ?? '').trim().toLowerCase()
|
|
22
|
+
if (!raw) return false
|
|
23
|
+
if (raw === '::1' || raw === '127.0.0.1') return true
|
|
24
|
+
if (raw.startsWith('::ffff:')) return raw.slice('::ffff:'.length) === '127.0.0.1'
|
|
25
|
+
return false
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 判断 Host 头是否为本机地址(允许带端口、IPv6 方括号)。
|
|
30
|
+
* @param {unknown} hostHeader 请求的 Host 头
|
|
31
|
+
* @returns {boolean}
|
|
32
|
+
*/
|
|
33
|
+
export function isLoopbackHost(hostHeader) {
|
|
34
|
+
const raw = String(hostHeader ?? '').trim().toLowerCase()
|
|
35
|
+
if (!raw) return false
|
|
36
|
+
let host = raw
|
|
37
|
+
if (raw.startsWith('[')) {
|
|
38
|
+
const end = raw.indexOf(']')
|
|
39
|
+
if (end === -1) return false
|
|
40
|
+
host = raw.slice(0, end + 1)
|
|
41
|
+
} else if ((raw.match(/:/g) ?? []).length > 1) {
|
|
42
|
+
// 裸 IPv6(理论少见,Host 头按 RFC 应带方括号)
|
|
43
|
+
host = raw
|
|
44
|
+
} else if (raw.includes(':')) {
|
|
45
|
+
host = raw.slice(0, raw.indexOf(':'))
|
|
46
|
+
}
|
|
47
|
+
return LOOPBACK_HOSTS.has(host.replace(/^\[|\]$/g, ''))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 判断 Origin 是否为本机来源;缺省 Origin(同源 GET)视为通过。
|
|
52
|
+
* @param {unknown} origin 请求的 Origin 头
|
|
53
|
+
* @returns {boolean}
|
|
54
|
+
*/
|
|
55
|
+
export function isLoopbackOrigin(origin) {
|
|
56
|
+
const raw = String(origin ?? '').trim()
|
|
57
|
+
if (!raw) return true
|
|
58
|
+
try {
|
|
59
|
+
const url = new URL(raw)
|
|
60
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
|
|
61
|
+
return isLoopbackHost(url.host)
|
|
62
|
+
} catch {
|
|
63
|
+
return false
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 常量时间比较,避免逐字符泄露。 */
|
|
68
|
+
export function safeEqual(a, b) {
|
|
69
|
+
const left = Buffer.from(String(a ?? ''), 'utf8')
|
|
70
|
+
const right = Buffer.from(String(b ?? ''), 'utf8')
|
|
71
|
+
if (left.length !== right.length || left.length === 0) return false
|
|
72
|
+
return timingSafeEqual(left, right)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 从请求头读取令牌:优先自定义头,其次 Bearer。
|
|
77
|
+
* @param {{headers?: Record<string, unknown>}} req
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
export function readToken(req) {
|
|
81
|
+
const header = req?.headers?.[TOKEN_HEADER]
|
|
82
|
+
if (typeof header === 'string' && header.trim()) return header.trim()
|
|
83
|
+
const auth = req?.headers?.authorization
|
|
84
|
+
if (typeof auth === 'string' && auth.toLowerCase().startsWith('bearer ')) return auth.slice(7).trim()
|
|
85
|
+
return ''
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 解析令牌:配置 > 环境变量 > 随机生成(随机生成只活在当前进程)。
|
|
90
|
+
* @param {{httpToken?: string}} cfg
|
|
91
|
+
* @param {{warn: Function}} [log]
|
|
92
|
+
* @returns {{token: string, ephemeral: boolean}}
|
|
93
|
+
*/
|
|
94
|
+
export function resolveHttpToken(cfg, log) {
|
|
95
|
+
const fromConfig = String(cfg?.httpToken ?? '').trim()
|
|
96
|
+
if (fromConfig) return { token: fromConfig, ephemeral: false }
|
|
97
|
+
const fromEnv = String(process.env.DSH_SESSION_SPLIT_HTTP_TOKEN ?? '').trim()
|
|
98
|
+
if (fromEnv) return { token: fromEnv, ephemeral: false }
|
|
99
|
+
const token = randomBytes(24).toString('hex')
|
|
100
|
+
log?.warn('未配置 httpToken,已生成进程内随机令牌(客户端通过页面 meta 自动获取)')
|
|
101
|
+
return { token, ephemeral: true }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 滑动窗口限流器。
|
|
106
|
+
* @param {number} perMin 每分钟允许次数
|
|
107
|
+
* @param {() => number} [now] 时钟(测试可注入)
|
|
108
|
+
* @returns {(key: string) => boolean} 是否放行
|
|
109
|
+
*/
|
|
110
|
+
export function createRateLimiter(perMin, now = Date.now) {
|
|
111
|
+
const hits = new Map()
|
|
112
|
+
return function allow(key) {
|
|
113
|
+
const at = now()
|
|
114
|
+
const cutoff = at - 60_000
|
|
115
|
+
const list = (hits.get(key) ?? []).filter((ts) => ts > cutoff)
|
|
116
|
+
if (list.length >= perMin) {
|
|
117
|
+
hits.set(key, list)
|
|
118
|
+
return false
|
|
119
|
+
}
|
|
120
|
+
list.push(at)
|
|
121
|
+
hits.set(key, list)
|
|
122
|
+
if (hits.size > 512) {
|
|
123
|
+
for (const [k, v] of hits) {
|
|
124
|
+
if (v.every((ts) => ts <= cutoff)) hits.delete(k)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return true
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 创建请求守卫。
|
|
133
|
+
* @param {{cfg: object, log: object}} input
|
|
134
|
+
*/
|
|
135
|
+
export function createHttpGuard({ cfg, log }) {
|
|
136
|
+
const { token, ephemeral } = resolveHttpToken(cfg, log)
|
|
137
|
+
const general = createRateLimiter(cfg.httpRateLimitPerMin)
|
|
138
|
+
const exporter = createRateLimiter(cfg.httpExportPerMin)
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {any} req node 请求
|
|
142
|
+
* @param {'general'|'export'} kind 限流档位
|
|
143
|
+
* @returns {{ok: true} | {ok: false, status: number, code: string, message: string}}
|
|
144
|
+
*/
|
|
145
|
+
function check(req, kind) {
|
|
146
|
+
if (!isLoopbackHost(req?.headers?.host)) {
|
|
147
|
+
return { ok: false, status: 403, code: 'HOST_NOT_ALLOWED', message: '只允许本机访问' }
|
|
148
|
+
}
|
|
149
|
+
const peerAddress = req?.socket?.remoteAddress
|
|
150
|
+
if (peerAddress && !isLoopbackAddress(peerAddress)) {
|
|
151
|
+
return { ok: false, status: 403, code: 'PEER_NOT_ALLOWED', message: '只允许本机连接' }
|
|
152
|
+
}
|
|
153
|
+
if (!isLoopbackOrigin(req?.headers?.origin)) {
|
|
154
|
+
return { ok: false, status: 403, code: 'ORIGIN_NOT_ALLOWED', message: '来源不被允许' }
|
|
155
|
+
}
|
|
156
|
+
const site = String(req?.headers?.['sec-fetch-site'] ?? '').toLowerCase()
|
|
157
|
+
if (site === 'cross-site' || site === 'same-site') {
|
|
158
|
+
return { ok: false, status: 403, code: 'CROSS_SITE', message: '跨站请求被拒绝' }
|
|
159
|
+
}
|
|
160
|
+
if (!safeEqual(readToken(req), token)) {
|
|
161
|
+
return { ok: false, status: 401, code: 'UNAUTHORIZED', message: '缺少或错误的访问令牌' }
|
|
162
|
+
}
|
|
163
|
+
const peer = String(peerAddress ?? 'local')
|
|
164
|
+
const limiter = kind === 'export' ? exporter : general
|
|
165
|
+
if (!limiter(`${peer}|${kind}`)) {
|
|
166
|
+
return { ok: false, status: 429, code: 'RATE_LIMITED', message: '请求过于频繁,请稍后再试' }
|
|
167
|
+
}
|
|
168
|
+
return { ok: true }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
token,
|
|
173
|
+
ephemeral,
|
|
174
|
+
check,
|
|
175
|
+
describe: () => (ephemeral ? '进程内随机令牌' : '配置令牌'),
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 注入 index.html 的 meta 标签(令牌为十六进制或用户字符串,已转义)。 */
|
|
180
|
+
export function tokenMetaTag(token) {
|
|
181
|
+
const escaped = String(token).replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<')
|
|
182
|
+
return `<meta name="${TOKEN_META_NAME}" content="${escaped}">`
|
|
183
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/** 插件入口:配置校验 → 注册工具。任何失败都不影响宿主启动。 */
|
|
2
|
+
import { resolveConfig } from './config.js'
|
|
3
|
+
import { SessionSplitError } from './errors.js'
|
|
4
|
+
import { createJudge } from './judge.js'
|
|
5
|
+
import { createLogger } from './log.js'
|
|
6
|
+
import { createRegistrar } from './registry.js'
|
|
7
|
+
import { repairArchivedDirs } from './repair.js'
|
|
8
|
+
import { registerRoutes } from './routes.js'
|
|
9
|
+
import { createSessionReader, resolveSession } from './store.js'
|
|
10
|
+
import { __setResolver, createTools } from './tools.js'
|
|
11
|
+
|
|
12
|
+
/** 插件名(cordis 用它标识插件)。 */
|
|
13
|
+
export const name = 'dsh-topic-split'
|
|
14
|
+
/** 依赖的内核服务:只要求 tools;webServer 可选(面板 HTTP 路由默认关闭)。 */
|
|
15
|
+
export const inject = ['tools']
|
|
16
|
+
|
|
17
|
+
const log = createLogger('dsh-topic-split')
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {any} ctx cordis 上下文
|
|
21
|
+
* @param {Record<string, unknown>} [config] 用户配置(可选,会与默认值合并校验)
|
|
22
|
+
*/
|
|
23
|
+
export function apply(ctx, config) {
|
|
24
|
+
if (String(process.env.DSH_SESSION_SPLIT_DISABLE ?? '') === '1') {
|
|
25
|
+
log.warn('已被 DSH_SESSION_SPLIT_DISABLE=1 禁用,跳过注册')
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
let cfg
|
|
29
|
+
try {
|
|
30
|
+
cfg = resolveConfig(config)
|
|
31
|
+
} catch (err) {
|
|
32
|
+
log.error('配置非法,插件不注册:', err instanceof SessionSplitError ? `${err.code} ${err.message}` : err)
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
const toolsService = ctx?.tools
|
|
36
|
+
if (!toolsService || typeof toolsService.register !== 'function') {
|
|
37
|
+
log.warn('没有 tools 服务,跳过注册')
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
// 先自愈历史遗留的「改名归档」目录:它们会让 DSH 的会话列表整份读取失败
|
|
41
|
+
try {
|
|
42
|
+
const repaired = repairArchivedDirs({ log })
|
|
43
|
+
if (repaired.length > 0) {
|
|
44
|
+
log.warn(`已修复 ${repaired.length} 个被改名的归档目录;这些会话会重新出现在列表里`)
|
|
45
|
+
}
|
|
46
|
+
} catch (err) {
|
|
47
|
+
log.warn('归档目录自愈失败:', err && err.message ? err.message : err)
|
|
48
|
+
}
|
|
49
|
+
// 必须绑定 this:直接取方法引用会丢上下文(this.layers 未定义)
|
|
50
|
+
const register = toolsService.register.bind(toolsService)
|
|
51
|
+
/** 收集所有可释放的注册,插件卸载时统一清理。 */
|
|
52
|
+
const disposers = []
|
|
53
|
+
const hold = (disposer) => {
|
|
54
|
+
if (typeof disposer === 'function') disposers.push(disposer)
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
__setResolver(resolveSession)
|
|
58
|
+
const readSession = createSessionReader(cfg)
|
|
59
|
+
let judge = null
|
|
60
|
+
try {
|
|
61
|
+
judge = createJudge({ cfg, log, ctx })
|
|
62
|
+
} catch (err) {
|
|
63
|
+
log.warn('判断模型初始化失败,回退关键词规则:', err)
|
|
64
|
+
}
|
|
65
|
+
let toolNames = []
|
|
66
|
+
// workspaceRegistry 是可选依赖:动态注入拿到实例后再用服务登记
|
|
67
|
+
let registryService = null
|
|
68
|
+
if (typeof ctx.inject === 'function') {
|
|
69
|
+
try {
|
|
70
|
+
hold(
|
|
71
|
+
ctx.inject(['workspaceRegistry'], (injected) => {
|
|
72
|
+
registryService = injected.workspaceRegistry
|
|
73
|
+
}),
|
|
74
|
+
)
|
|
75
|
+
} catch (err) {
|
|
76
|
+
log.warn('workspaceRegistry 动态注入失败:', err && err.message ? err.message : err)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const registrar = createRegistrar({ ctx, log, getService: () => registryService })
|
|
80
|
+
const tools = createTools({ cfg, readSession, log, judge, registrar, getToolNames: () => toolNames })
|
|
81
|
+
toolNames = tools.map((t) => t.name)
|
|
82
|
+
for (const tool of tools) hold(register(tool))
|
|
83
|
+
log.info(`已注册工具:${toolNames.join(', ')}`)
|
|
84
|
+
log.info(`判断模型:${judge ? judge.describe() : '不可用(关键词规则)'}`)
|
|
85
|
+
// HTTP 面板路由:webServer 是可选依赖,用动态注入等它就绪(工具不受影响)
|
|
86
|
+
const startRoutes = (routeCtx) => {
|
|
87
|
+
try {
|
|
88
|
+
hold(
|
|
89
|
+
registerRoutes(routeCtx, { cfg, readSession, log, getToolNames: () => toolNames, judge, registrar }),
|
|
90
|
+
)
|
|
91
|
+
} catch (err) {
|
|
92
|
+
log.warn('HTTP 路由注册失败(工具仍可用):', err && err.message ? err.message : err)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (cfg.httpRoutes === 'token' && typeof ctx.inject === 'function') {
|
|
96
|
+
try {
|
|
97
|
+
hold(ctx.inject(['webServer'], startRoutes))
|
|
98
|
+
} catch (err) {
|
|
99
|
+
log.warn('webServer 动态注入失败,尝试直接注册:', err && err.message ? err.message : err)
|
|
100
|
+
startRoutes(ctx)
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
startRoutes(ctx)
|
|
104
|
+
}
|
|
105
|
+
if (typeof ctx.effect === 'function') {
|
|
106
|
+
ctx.effect(
|
|
107
|
+
() => () => {
|
|
108
|
+
for (const dispose of disposers.splice(0)) {
|
|
109
|
+
try {
|
|
110
|
+
dispose()
|
|
111
|
+
} catch (err) {
|
|
112
|
+
log.warn('释放注册失败:', err && err.message ? err.message : err)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
'dsh-topic-split: tools + routes',
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
} catch (err) {
|
|
120
|
+
log.error('注册失败(宿主继续运行):', err instanceof SessionSplitError ? `${err.code} ${err.message}` : err)
|
|
121
|
+
}
|
|
122
|
+
}
|
package/src/judge.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/** 判断模型适配层:把「按主题分段」交给 LLM,失败由调用方回退关键词规则。
|
|
2
|
+
*
|
|
3
|
+
* 设计要点:
|
|
4
|
+
* - 只喂每轮摘要(用户首句 + 助手首句),不喂全文,成本降低一个数量级;
|
|
5
|
+
* - 输出必须是 JSON,且在代码里硬校验「连续、不重叠、全覆盖」;
|
|
6
|
+
* - 校验失败重试一次,仍失败则抛 JUDGE_BAD_OUTPUT,由上层决定回退。
|
|
7
|
+
*/
|
|
8
|
+
import { randomUUID } from 'node:crypto'
|
|
9
|
+
|
|
10
|
+
import { ERR, SessionSplitError, asSplitError } from './errors.js'
|
|
11
|
+
|
|
12
|
+
/** 判断模型的系统提示:约束输出格式与覆盖规则。 */
|
|
13
|
+
export const JUDGE_SYSTEM = [
|
|
14
|
+
'你是会话主题分段器。输入是按轮次编号的对话摘要,每行形如:',
|
|
15
|
+
'#12 用户:…… | 助手:……',
|
|
16
|
+
'请把相邻轮次按主题合并成若干连续段,只输出 JSON,不要解释。',
|
|
17
|
+
'输出格式:{"segments":[{"label":"简短主题名","from":0,"to":51},{"label":"另一个主题","from":52,"to":96}]}',
|
|
18
|
+
'硬性要求:',
|
|
19
|
+
'1. from/to 都是轮次编号,从 0 开始,段与段首尾相接、不重叠、不遗漏,最后一段的 to 必须等于最后一轮编号。',
|
|
20
|
+
'2. 每段至少 3 轮;主题名不超过 12 个汉字,概括该段实际内容。',
|
|
21
|
+
'3. 只有确实无法归类时才用「其他」。',
|
|
22
|
+
'4. 只输出 JSON 对象,不要 Markdown 代码块,不要多余文字。',
|
|
23
|
+
].join('\n')
|
|
24
|
+
|
|
25
|
+
/** 摘要超长时按预算压缩每轮字符数(保证所有轮次都进提示词)。 */
|
|
26
|
+
function perTurnBudget(totalTurns, cfg) {
|
|
27
|
+
const sem = cfg.semantic
|
|
28
|
+
const budget = Math.floor((sem.maxTurnsInPrompt * sem.summaryChars) / Math.max(totalTurns, 1))
|
|
29
|
+
return Math.max(24, Math.min(sem.summaryChars, budget))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function firstLine(text, limit) {
|
|
33
|
+
const flat = String(text ?? '').replace(/\s+/g, ' ').trim()
|
|
34
|
+
return flat.length <= limit ? flat : flat.slice(0, limit) + '…'
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 生成每轮摘要行。
|
|
39
|
+
* @param {Array<{turn:number, user:string, assistant:string}>} items
|
|
40
|
+
* @param {{semantic:{summaryChars:number, maxTurnsInPrompt:number}}} cfg
|
|
41
|
+
* @returns {string[]}
|
|
42
|
+
*/
|
|
43
|
+
export function summarizeTurns(items, cfg) {
|
|
44
|
+
const budget = perTurnBudget(items.length, cfg)
|
|
45
|
+
return items.map((item) => {
|
|
46
|
+
const user = firstLine(item.user, budget)
|
|
47
|
+
const assistant = firstLine(item.assistant, Math.floor(budget / 2))
|
|
48
|
+
return `#${item.turn} 用户:${user} | 助手:${assistant}`
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 构造判断模型的用户输入。
|
|
54
|
+
* @param {Array<{turn:number, user:string, assistant:string}>} items
|
|
55
|
+
* @param {object} cfg
|
|
56
|
+
* @returns {string}
|
|
57
|
+
*/
|
|
58
|
+
export function buildPrompt(items, cfg) {
|
|
59
|
+
const lines = summarizeTurns(items, cfg)
|
|
60
|
+
return [
|
|
61
|
+
`共有 ${items.length} 轮对话(编号 0..${items.length - 1})。`,
|
|
62
|
+
'摘要如下:',
|
|
63
|
+
...lines,
|
|
64
|
+
'',
|
|
65
|
+
'请输出 JSON。',
|
|
66
|
+
].join('\n')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 从可能带代码块的回复里取出 JSON 文本。 */
|
|
70
|
+
function extractJson(text) {
|
|
71
|
+
const raw = String(text ?? '').trim()
|
|
72
|
+
const fenced = /```(?:json)?\s*([\s\S]*?)```/i.exec(raw)
|
|
73
|
+
const body = fenced ? fenced[1].trim() : raw
|
|
74
|
+
const start = body.indexOf('{')
|
|
75
|
+
const end = body.lastIndexOf('}')
|
|
76
|
+
if (start === -1 || end === -1 || end <= start) return null
|
|
77
|
+
try {
|
|
78
|
+
return JSON.parse(body.slice(start, end + 1))
|
|
79
|
+
} catch {
|
|
80
|
+
return null
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 校验并规范化模型给的分段:必须连续覆盖 0..totalTurns-1。
|
|
86
|
+
* @param {unknown} payload 解析后的 JSON
|
|
87
|
+
* @param {number} totalTurns
|
|
88
|
+
* @param {{maxRanges:number}} cfg
|
|
89
|
+
* @returns {Array<{label:string, from:number, to:number}>}
|
|
90
|
+
*/
|
|
91
|
+
export function validateSegments(payload, totalTurns, cfg) {
|
|
92
|
+
const list = payload && typeof payload === 'object' ? payload.segments : null
|
|
93
|
+
if (!Array.isArray(list) || list.length === 0) {
|
|
94
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, '模型输出缺少 segments 数组')
|
|
95
|
+
}
|
|
96
|
+
if (list.length > cfg.maxRanges) {
|
|
97
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, `模型给了 ${list.length} 段,超过上限 ${cfg.maxRanges}`)
|
|
98
|
+
}
|
|
99
|
+
const out = []
|
|
100
|
+
let cursor = 0
|
|
101
|
+
for (const [index, entry] of list.entries()) {
|
|
102
|
+
const from = Number(entry?.from)
|
|
103
|
+
const to = Number(entry?.to)
|
|
104
|
+
const label = String(entry?.label ?? '').trim()
|
|
105
|
+
if (!Number.isInteger(from) || !Number.isInteger(to)) {
|
|
106
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, `第 ${index + 1} 段的 from/to 不是整数`)
|
|
107
|
+
}
|
|
108
|
+
if (from !== cursor) {
|
|
109
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, `第 ${index + 1} 段应从 ${cursor} 开始,实际 ${from}`)
|
|
110
|
+
}
|
|
111
|
+
if (to < from) {
|
|
112
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, `第 ${index + 1} 段终点小于起点`)
|
|
113
|
+
}
|
|
114
|
+
if (to >= totalTurns) {
|
|
115
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, `第 ${index + 1} 段超出范围(共 ${totalTurns} 轮)`)
|
|
116
|
+
}
|
|
117
|
+
if (!label) throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, `第 ${index + 1} 段没有主题名`)
|
|
118
|
+
out.push({ label: label.slice(0, 24), from, to })
|
|
119
|
+
cursor = to + 1
|
|
120
|
+
}
|
|
121
|
+
if (cursor !== totalTurns) {
|
|
122
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, `只覆盖到 ${cursor} 轮,共 ${totalTurns} 轮`)
|
|
123
|
+
}
|
|
124
|
+
return out
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 解析模型回复。
|
|
129
|
+
* @param {string} text 模型原始回复
|
|
130
|
+
* @param {number} totalTurns
|
|
131
|
+
* @param {{maxRanges:number}} cfg
|
|
132
|
+
*/
|
|
133
|
+
export function parseJudgeOutput(text, totalTurns, cfg) {
|
|
134
|
+
const payload = extractJson(text)
|
|
135
|
+
if (!payload) throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, '模型回复不是合法 JSON')
|
|
136
|
+
return validateSegments(payload, totalTurns, cfg)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 组合父信号与超时,得到子信号。 */
|
|
140
|
+
function withDeadline(parent, ms) {
|
|
141
|
+
const ctrl = new AbortController()
|
|
142
|
+
const timer = setTimeout(() => {
|
|
143
|
+
ctrl.abort(new SessionSplitError(ERR.JUDGE_TIMEOUT, `判断模型超时(${ms}ms)`))
|
|
144
|
+
}, ms)
|
|
145
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
146
|
+
const onAbort = () => ctrl.abort(parent?.reason)
|
|
147
|
+
if (parent) {
|
|
148
|
+
if (parent.aborted) onAbort()
|
|
149
|
+
else parent.addEventListener('abort', onAbort, { once: true })
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
signal: ctrl.signal,
|
|
153
|
+
dispose() {
|
|
154
|
+
clearTimeout(timer)
|
|
155
|
+
if (parent) parent.removeEventListener('abort', onAbort)
|
|
156
|
+
},
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 把任意异常规整成带错误码的 SessionSplitError。 */
|
|
161
|
+
function judgeError(err) {
|
|
162
|
+
if (err instanceof SessionSplitError) return err
|
|
163
|
+
const code = err?.name === 'AbortError' ? ERR.JUDGE_TIMEOUT : ERR.IO_FAILED
|
|
164
|
+
return new SessionSplitError(code, err?.message ?? String(err))
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** 解析宿主 llm 服务:属性直取与 ctx.get 双路径,取不到返回 null。 */
|
|
168
|
+
function resolveLlm(ctx) {
|
|
169
|
+
if (!ctx) return null
|
|
170
|
+
try {
|
|
171
|
+
const direct = ctx.llm
|
|
172
|
+
if (direct && typeof direct.prepareCall === 'function') return direct
|
|
173
|
+
} catch {
|
|
174
|
+
/* 服务未注册时某些版本会抛错,继续尝试 ctx.get */
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
const got = typeof ctx.get === 'function' ? ctx.get('llm') : null
|
|
178
|
+
if (got && typeof got.prepareCall === 'function') return got
|
|
179
|
+
} catch {
|
|
180
|
+
/* 忽略,按不可用处理 */
|
|
181
|
+
}
|
|
182
|
+
return null
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** 走宿主内核 LLM 服务(不需要额外密钥)。 */
|
|
186
|
+
function createKernelJudge({ ctx, cfg, log }) {
|
|
187
|
+
const sem = cfg.semantic
|
|
188
|
+
const llm = resolveLlm(ctx)
|
|
189
|
+
if (!llm) {
|
|
190
|
+
log?.warn('kernel 判断后端不可用:宿主没有 llm 服务')
|
|
191
|
+
return null
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
kind: 'kernel',
|
|
195
|
+
describe: () => `内核模型 ${sem.provider}/${sem.model}`,
|
|
196
|
+
async complete(prompt, signal) {
|
|
197
|
+
try {
|
|
198
|
+
const call = await llm.prepareCall(
|
|
199
|
+
{ provider: sem.provider, model: sem.model, maxTokens: sem.maxOutputTokens },
|
|
200
|
+
signal,
|
|
201
|
+
)
|
|
202
|
+
const messages = [
|
|
203
|
+
{
|
|
204
|
+
role: 'user',
|
|
205
|
+
content: [{ type: 'text', text: prompt }],
|
|
206
|
+
source: { kind: 'dsh-topic-split' },
|
|
207
|
+
id: randomUUID(),
|
|
208
|
+
},
|
|
209
|
+
]
|
|
210
|
+
let streamed = ''
|
|
211
|
+
let blocks = ''
|
|
212
|
+
let finish = null
|
|
213
|
+
for await (const chunk of call.stream({ ...call.config, messages, system: JUDGE_SYSTEM, signal })) {
|
|
214
|
+
if (!chunk) continue
|
|
215
|
+
if (chunk.type === 'text-delta' && typeof chunk.text === 'string') {
|
|
216
|
+
streamed += chunk.text
|
|
217
|
+
} else if (chunk.type === 'block-end') {
|
|
218
|
+
// 部分适配器不流式发 delta,只在块结束时给完整文本
|
|
219
|
+
const block = chunk.block
|
|
220
|
+
if (block && block.type === 'text' && typeof block.text === 'string') blocks += block.text
|
|
221
|
+
} else if (chunk.type === 'finish') {
|
|
222
|
+
finish = chunk.reason
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const text = streamed || blocks
|
|
226
|
+
if (!text.trim()) {
|
|
227
|
+
if (finish && finish.kind === 'error') {
|
|
228
|
+
throw new SessionSplitError(
|
|
229
|
+
ERR.JUDGE_UNAVAILABLE,
|
|
230
|
+
`内核模型失败:${finish.failure?.message ?? '未知错误'}`,
|
|
231
|
+
)
|
|
232
|
+
}
|
|
233
|
+
if (finish && finish.kind === 'aborted') {
|
|
234
|
+
throw new SessionSplitError(ERR.ABORTED, `内核模型中止:${finish.failure?.message ?? '无详情'}`)
|
|
235
|
+
}
|
|
236
|
+
if (finish && finish.kind === 'max-tokens') {
|
|
237
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, '内核模型输出被 maxTokens 截断且无文本')
|
|
238
|
+
}
|
|
239
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, '内核模型没有返回文本')
|
|
240
|
+
}
|
|
241
|
+
return text
|
|
242
|
+
} catch (err) {
|
|
243
|
+
throw judgeError(err)
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** 走 OpenAI 兼容端点(需要用户自备密钥,密钥只在环境变量里读)。 */
|
|
250
|
+
function createHttpJudge({ cfg, log }) {
|
|
251
|
+
const sem = cfg.semantic
|
|
252
|
+
if (!sem.httpBaseUrl) {
|
|
253
|
+
log?.warn('http 判断后端不可用:没有配置 httpBaseUrl')
|
|
254
|
+
return null
|
|
255
|
+
}
|
|
256
|
+
const base = sem.httpBaseUrl.replace(/\/+$/, '')
|
|
257
|
+
const url = base.endsWith('/chat/completions') ? base : `${base}/chat/completions`
|
|
258
|
+
const model = sem.httpModel || 'deepseek-chat'
|
|
259
|
+
return {
|
|
260
|
+
kind: 'http',
|
|
261
|
+
describe: () => `HTTP 端点 ${model}`,
|
|
262
|
+
async complete(prompt, signal) {
|
|
263
|
+
const key = String(process.env[sem.httpKeyEnv] ?? '').trim()
|
|
264
|
+
const headers = { 'content-type': 'application/json' }
|
|
265
|
+
if (key) headers.authorization = `Bearer ${key}`
|
|
266
|
+
let resp
|
|
267
|
+
try {
|
|
268
|
+
resp = await fetch(url, {
|
|
269
|
+
method: 'POST',
|
|
270
|
+
headers,
|
|
271
|
+
body: JSON.stringify({
|
|
272
|
+
model,
|
|
273
|
+
messages: [
|
|
274
|
+
{ role: 'system', content: JUDGE_SYSTEM },
|
|
275
|
+
{ role: 'user', content: prompt },
|
|
276
|
+
],
|
|
277
|
+
temperature: 0,
|
|
278
|
+
max_tokens: sem.maxOutputTokens,
|
|
279
|
+
response_format: { type: 'json_object' },
|
|
280
|
+
}),
|
|
281
|
+
signal,
|
|
282
|
+
})
|
|
283
|
+
} catch (err) {
|
|
284
|
+
throw judgeError(err)
|
|
285
|
+
}
|
|
286
|
+
if (!resp.ok) {
|
|
287
|
+
throw new SessionSplitError(ERR.JUDGE_UNAVAILABLE, `判断端点返回 HTTP ${resp.status}`)
|
|
288
|
+
}
|
|
289
|
+
let data
|
|
290
|
+
try {
|
|
291
|
+
data = await resp.json()
|
|
292
|
+
} catch (err) {
|
|
293
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, `判断端点返回的不是 JSON:${err.message}`)
|
|
294
|
+
}
|
|
295
|
+
const text = data?.choices?.[0]?.message?.content
|
|
296
|
+
if (typeof text !== 'string' || !text.trim()) {
|
|
297
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, '判断端点没有返回文本')
|
|
298
|
+
}
|
|
299
|
+
return text
|
|
300
|
+
},
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 按配置创建判断器;不可用时返回 null(调用方回退关键词规则)。
|
|
306
|
+
* @param {{cfg:object, log?:object, ctx?:any}} input
|
|
307
|
+
* @returns {{kind:string, describe:()=>string, complete:(prompt:string, signal?:AbortSignal)=>Promise<string>}|null}
|
|
308
|
+
*/
|
|
309
|
+
export function createJudge({ cfg, log, ctx }) {
|
|
310
|
+
const sem = cfg.semantic
|
|
311
|
+
const backend = sem.backend
|
|
312
|
+
if (backend === 'off') return null
|
|
313
|
+
if (backend === 'kernel') return createKernelJudge({ ctx, cfg, log })
|
|
314
|
+
if (backend === 'http') return createHttpJudge({ cfg, log })
|
|
315
|
+
// auto:优先内核(无需密钥),其次 HTTP 端点
|
|
316
|
+
const kernel = createKernelJudge({ ctx, cfg, log })
|
|
317
|
+
if (kernel) return kernel
|
|
318
|
+
return createHttpJudge({ cfg, log })
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* 调一次判断模型并解析,带超时与重试。
|
|
323
|
+
* @param {object} judge
|
|
324
|
+
* @param {Array<{turn:number, user:string, assistant:string}>} items
|
|
325
|
+
* @param {object} cfg
|
|
326
|
+
* @param {AbortSignal} [signal]
|
|
327
|
+
* @returns {Promise<Array<{label:string, from:number, to:number}>>}
|
|
328
|
+
*/
|
|
329
|
+
export async function segmentByJudge(judge, items, cfg, signal) {
|
|
330
|
+
const sem = cfg.semantic
|
|
331
|
+
if (items.length < 2) {
|
|
332
|
+
throw new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, '轮次太少,无法做主题分段')
|
|
333
|
+
}
|
|
334
|
+
const prompt = buildPrompt(items, cfg)
|
|
335
|
+
let lastError = null
|
|
336
|
+
for (let attempt = 0; attempt <= sem.retries; attempt++) {
|
|
337
|
+
if (signal?.aborted) throw new SessionSplitError(ERR.ABORTED, '调用已取消')
|
|
338
|
+
const call = withDeadline(signal, sem.timeoutMs)
|
|
339
|
+
try {
|
|
340
|
+
const text = await judge.complete(prompt, call.signal)
|
|
341
|
+
return parseJudgeOutput(text, items.length, cfg)
|
|
342
|
+
} catch (err) {
|
|
343
|
+
lastError = asSplitError(err)
|
|
344
|
+
if (lastError.code === ERR.ABORTED) throw lastError
|
|
345
|
+
} finally {
|
|
346
|
+
call.dispose()
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
throw lastError ?? new SessionSplitError(ERR.JUDGE_BAD_OUTPUT, '判断模型失败')
|
|
350
|
+
}
|
package/src/log.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** 分级日志:默认只输出 info 及以上;DSH_SESSION_SPLIT_DEBUG=1 时输出 debug。 */
|
|
2
|
+
|
|
3
|
+
const LEVELS = { debug: 10, info: 20, warn: 30, error: 40 }
|
|
4
|
+
|
|
5
|
+
function enabledAt() {
|
|
6
|
+
const raw = String(process.env.DSH_SESSION_SPLIT_LOG ?? '').toLowerCase()
|
|
7
|
+
if (raw in LEVELS) return LEVELS[raw]
|
|
8
|
+
return String(process.env.DSH_SESSION_SPLIT_DEBUG ?? '') === '1' ? LEVELS.debug : LEVELS.info
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 创建带前缀与级别的日志器。
|
|
13
|
+
* @param {string} tag 日志前缀
|
|
14
|
+
*/
|
|
15
|
+
export function createLogger(tag) {
|
|
16
|
+
const threshold = enabledAt()
|
|
17
|
+
const emit = (level, args) => {
|
|
18
|
+
if (LEVELS[level] < threshold) return
|
|
19
|
+
const fn =
|
|
20
|
+
level === 'debug'
|
|
21
|
+
? console.debug
|
|
22
|
+
: level === 'warn'
|
|
23
|
+
? console.warn
|
|
24
|
+
: level === 'error'
|
|
25
|
+
? console.error
|
|
26
|
+
: console.log
|
|
27
|
+
try {
|
|
28
|
+
fn.call(console, `[${tag}]`, ...args)
|
|
29
|
+
} catch {
|
|
30
|
+
/* 日志本身失败不应影响主流程 */
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
debug: (...a) => emit('debug', a),
|
|
35
|
+
info: (...a) => emit('info', a),
|
|
36
|
+
warn: (...a) => emit('warn', a),
|
|
37
|
+
error: (...a) => emit('error', a),
|
|
38
|
+
}
|
|
39
|
+
}
|