@jayyuen66/dsh-dir-prep-organize 0.1.2 → 0.1.4
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/README.md +26 -29
- package/README.zh-CN.md +29 -32
- package/client.js +215 -108
- package/cordis.patch.yml +12 -16
- package/host.js +270 -358
- package/package.json +49 -43
- package/types/host.d.ts +105 -74
- package/types/src/client-entry.d.ts +5 -1
- package/types/src/client-timeouts.d.ts +1 -1
- package/types/src/host-messages.d.ts +0 -2
- package/types/src/remote.d.ts +27 -0
- package/types/src/templates.d.ts +4 -4
- package/types/src/ui-messages.d.ts +3 -3
package/host.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
// GENERATED by build-host.mjs — edit host.ts / src/*.ts, then run `node build-host.mjs`.
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
3
2
|
import path from "node:path";
|
|
4
3
|
import Schema from "@deepseek-ai/schemastery";
|
|
5
4
|
import { brandString } from "@deepseek-ai/dsh-brand";
|
|
6
|
-
import {
|
|
7
|
-
import { guardTrust, isNonLoopbackBindHost } from "@jayyuen66/dsh-plugin-shared/lib/trust";
|
|
5
|
+
import { Remote, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
|
|
8
6
|
import { LOCALE_SETTINGS_NAMESPACE, messagesFor, resolveLocalePreference } from "@jayyuen66/dsh-plugin-shared/lib/locale";
|
|
9
7
|
import { isRecord } from "@jayyuen66/dsh-plugin-shared/lib/record";
|
|
10
8
|
import { errorText } from "@jayyuen66/dsh-plugin-shared/lib/errors";
|
|
@@ -50,7 +48,6 @@ const HOST_MESSAGES = {
|
|
|
50
48
|
emptyPrompt: "用户要求为空,无法整理",
|
|
51
49
|
noModelSelected: "当前无可用模型选择",
|
|
52
50
|
emptyOrganizeResult: "模型整理结果为空",
|
|
53
|
-
invalidJsonBody: "请求体不是合法 JSON",
|
|
54
51
|
fsUnavailable: "fs 服务不可用",
|
|
55
52
|
cwdUnknown: "无法确定会话 cwd(sessionId 缺失或会话不存在)",
|
|
56
53
|
modelServiceUnavailable: "agentDefaultModel 服务不可用",
|
|
@@ -98,7 +95,6 @@ const HOST_MESSAGES = {
|
|
|
98
95
|
emptyPrompt: "The request is empty: nothing to refine",
|
|
99
96
|
noModelSelected: "No model selection is available",
|
|
100
97
|
emptyOrganizeResult: "The model returned an empty result",
|
|
101
|
-
invalidJsonBody: "Request body is not valid JSON",
|
|
102
98
|
fsUnavailable: "fs service unavailable",
|
|
103
99
|
cwdUnknown: "Cannot determine the session cwd (sessionId missing or session unknown)",
|
|
104
100
|
modelServiceUnavailable: "agentDefaultModel service unavailable",
|
|
@@ -144,8 +140,8 @@ const AGENT_DIR_GROUP_LABELS = {
|
|
|
144
140
|
* scripts/gen-defaults-lib.mjs,键序 = 下拉分组展示序),本文件不再同名 re-export:
|
|
145
141
|
* 实测同名两处导出正是 fallow `duplicate_export`(severity=error)报出来的那一条,
|
|
146
142
|
* 而"两份名字保持同步"的纪律并不由 re-export 保证。表体仍是生成物;host 半引本文件的
|
|
147
|
-
* groupLabelOf/parseAgentFrontmatter
|
|
148
|
-
*
|
|
143
|
+
* groupLabelOf/parseAgentFrontmatter,精选集(生成物)也由 host 半引走、经
|
|
144
|
+
* `dirPrepOrganize.defaults` 方法按需下发,client 半不打包它。 */
|
|
149
145
|
/** 仓库目录名 → 分组标签(groupLabelOf(engineering) === "工程";未收录回落目录名)。
|
|
150
146
|
* 必须只认**自有属性**:`AGENT_DIR_GROUP_LABELS["constructor"]` 会命中原型上的
|
|
151
147
|
* function(`?? dirName` 不触发)→ TemplateEntry.group 拿到函数、JSON.stringify
|
|
@@ -259,15 +255,15 @@ function condenseRoleBody(body) {
|
|
|
259
255
|
//#endregion
|
|
260
256
|
//#region host.ts
|
|
261
257
|
/** 本包送进 `llm.stream({ messages })` 的整理请求帧是 **request-only 输入**:它从不落
|
|
262
|
-
* 任何持久消息位(`runOrganize` 只回 `{ content }
|
|
258
|
+
* 任何持久消息位(`runOrganize` 只回 `{ content }`,读面也不把帧身份回投给卡片),所以
|
|
263
259
|
* 按 0.1.7 它既不需要 `id` 也不带 `source`——证据三条:
|
|
264
|
-
* 1. installed `@deepseek-ai/dsh-llm/lib/types/types.d.ts
|
|
260
|
+
* 1. installed `@deepseek-ai/dsh-llm/lib/types/types.d.ts` 的 `RequestUserInput`
|
|
265
261
|
* 把两面都标成 `?: never`(`readonly id?: never` / `readonly source?: never`),
|
|
266
262
|
* `GenerateOptions.messages` 收的是 `RequestMessage[] = Message | RequestUserInput`
|
|
267
|
-
*
|
|
263
|
+
* (`GenerateOptions` 的字段注记 "a hand-built one-shot may include identity-free user
|
|
268
264
|
* inputs");
|
|
269
265
|
* 2. 持久那半边 `Message` 要的 `source.kind` 必须是**已声明**进 `MessageSourceMap` 的
|
|
270
|
-
* kind(installed `dsh-llm/lib/types/message.d.ts
|
|
266
|
+
* kind(installed `dsh-llm/lib/types/message.d.ts`),本包的
|
|
271
267
|
* `plugin:dir-prep-organize` 不在其中——旧代码同时带上 `id` 与该 kind,造的正是宿主
|
|
272
268
|
* 没有的第三种形状;
|
|
273
269
|
* 3. 宿主的持久化归属策略是「request-only 输入不需要持久身份」:把一次性提示构造成
|
|
@@ -277,12 +273,7 @@ function condenseRoleBody(body) {
|
|
|
277
273
|
* 故旧的 `ORGANIZE_SOURCE_KIND` 常量(连同只被它和帧 id 用到的 `PLUGIN_NAME`)一并撤下。
|
|
278
274
|
* 本包没有读回侧需要它:`extractRecentTurns` 只认真人 `kind === 'user'` 与模型
|
|
279
275
|
* `kind === 'model'`,从不按本包 kind 去重或断链,所以删除它不牵动任何 dedupe 配对。
|
|
280
|
-
* 同款 request-only 先例:harness `packages/compaction/compaction-basic/src/summarizer.ts
|
|
281
|
-
const CONTEXT_PATH = "/_dsh/dir-prep/context";
|
|
282
|
-
const MODEL_PATH = "/_dsh/dir-prep/model";
|
|
283
|
-
const ORGANIZE_PATH = "/_dsh/dir-prep/organize";
|
|
284
|
-
const IMPORT_PATH = "/_dsh/dir-prep/import";
|
|
285
|
-
const DEFAULTS_PATH = "/_dsh/dir-prep/default-templates";
|
|
276
|
+
* 同款 request-only 先例:harness `packages/compaction/compaction-basic/src/summarizer.ts`。 */
|
|
286
277
|
/** 插件自带角色库目录(agency-agents-zh 部门目录移入)。导入路径留空 = 全量导入此目录。 */
|
|
287
278
|
const AGENTS_DIR = path.join(import.meta.dirname, "agents");
|
|
288
279
|
/**
|
|
@@ -303,7 +294,7 @@ let defaultTemplatesPromise;
|
|
|
303
294
|
/**
|
|
304
295
|
* 产物导出值的守卫:只认「是数组」这一条。
|
|
305
296
|
*
|
|
306
|
-
*
|
|
297
|
+
* 元素级校验刻意不在这一层做——读面把这张表原样 JSON 序列化下发,渲染侧的兜底在 client
|
|
307
298
|
* 半;在这里逐条断言字段只会让守卫和生成脚本的字段集耦在一起,改一个字段要动两处。
|
|
308
299
|
* `Array.isArray` 本身会把 unknown 收窄成 `any[]`,经类型谓词再收一次,返回值才不是
|
|
309
300
|
* 不安全返回(no-unsafe-return)。
|
|
@@ -332,10 +323,10 @@ async function importDefaultTemplates() {
|
|
|
332
323
|
return defaultTemplatesFrom(await import(DEFAULT_TEMPLATES_URL.href));
|
|
333
324
|
}
|
|
334
325
|
/**
|
|
335
|
-
*
|
|
326
|
+
* 内置精选模板表:按需载入,永不静态导入。
|
|
336
327
|
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
328
|
+
* 静态导入会让整张精选表随宿主载入插件一起进内存,而它们只有一个消费者——default-templates
|
|
329
|
+
* 读面,而客户端本来就是首次打开下拉时才拉这一发。两边都按需,才是同一件事的同一口径。
|
|
339
330
|
*
|
|
340
331
|
* @returns 内置精选模板表
|
|
341
332
|
*/
|
|
@@ -380,10 +371,6 @@ const SECRET_PATTERN = /(?:^|[^a-z0-9])(?:credentials?|secrets?|id_rsa|pem|p12|p
|
|
|
380
371
|
/** LLM 整理超时。 */
|
|
381
372
|
/** schema .default() 的单源(host 消费走 config 字段,不再直读常量)。 */
|
|
382
373
|
const ORGANIZE_TIMEOUT_MS = 12e4;
|
|
383
|
-
/** organize/import POST body 物理上限的 schema .default()(内存防爆纵深防御,非功能
|
|
384
|
-
* 截断:两条路由提交的都是整理草稿与模板产物,不是大文件本体;超大输入产物由模型
|
|
385
|
-
* context 报错反馈用户自行处理)。部署间需要更宽的走 `importBodyMaxBytes` 配置字段。 */
|
|
386
|
-
const DEFAULT_IMPORT_BODY_MAX_BYTES = 8388608;
|
|
387
374
|
/** 单个关键文件的摘要字节上限。harness 的 readWholeText 无上限,超限文件整份
|
|
388
375
|
* 跳过(不截半份——半份摘要会以"全量"的名义喂给模型,比没有更误导),
|
|
389
376
|
* 并计入 skipped.tooLarge 回报。 */
|
|
@@ -391,13 +378,12 @@ const MAX_SNIPPET_BYTES = 262144;
|
|
|
391
378
|
/** 单次整理带进模型的全部摘要的**总量**上限。
|
|
392
379
|
*
|
|
393
380
|
* 为什么需要总量而单文件上限不够:`maxSnippetBytes` 只管每个文件,而条目数不设上界
|
|
394
|
-
* (用户拍板)。于是 client 把所有摘要拼成 `entriesSummary` 原样回投 organize
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
* 「organize 必然失败」。留足余量:JSON 转义与 client 侧的拼接还会再放大一截。 */
|
|
381
|
+
* (用户拍板)。于是 client 把所有摘要拼成 `entriesSummary` 原样回投 organize,请求
|
|
382
|
+
* 仍要过宿主 /api 网关的请求体上限,摘要一多就顶得过它,结果是 context 读面成功、
|
|
383
|
+
* UI 正常列出文件树,用户点「整理」才在网关处失败。这里给总量封顶,让「context 成功」
|
|
384
|
+
* 不再意味着「organize 必然失败」。留足余量:JSON 转义与 client 侧的拼接还会再放大一截。 */
|
|
399
385
|
const MAX_CONTEXT_TOTAL_BYTES = 2097152;
|
|
400
|
-
/** 文件读取并发上限:Promise.all
|
|
386
|
+
/** 文件读取并发上限:Promise.all 一次铺开会在导入内置角色库(整棵 agents/)时打满
|
|
401
387
|
* fd,EMFILE 抛错被单文件 catch 吞掉 → 条目静默丢失而仍报 ok:true。 */
|
|
402
388
|
const READ_CONCURRENCY = 8;
|
|
403
389
|
/** `readConcurrency` 的上界。再往上只是更快撞上 fd 上限——而那正是这个字段存在的
|
|
@@ -423,8 +409,6 @@ function clampInt(value, min, max, fallback) {
|
|
|
423
409
|
if (!Number.isFinite(value)) return fallback;
|
|
424
410
|
return Math.min(Math.max(Math.trunc(value), min), max);
|
|
425
411
|
}
|
|
426
|
-
/** organize 回填的 CSRF 头名(与 client-entry 侧一致)。 */
|
|
427
|
-
const CSRF_HEADER = "x-dir-prep-csrf";
|
|
428
412
|
/** 会话 id 的官方品牌构造:把边界上读来的裸 string 送进 `SessionStore.get`。 */
|
|
429
413
|
const sessionIdOf = (value) => brandString(value);
|
|
430
414
|
/** 服务形状守卫:value 是对象且具名成员是函数(绕开 `as Service` 断言)。 */
|
|
@@ -435,9 +419,6 @@ function hasFnMember(value, member) {
|
|
|
435
419
|
function isSettingsService(value) {
|
|
436
420
|
return hasFnMember(value, "describe");
|
|
437
421
|
}
|
|
438
|
-
function isWebServerService(value) {
|
|
439
|
-
return hasFnMember(value, "register") && hasFnMember(value, "registerFallback");
|
|
440
|
-
}
|
|
441
422
|
function isSessionsService(value) {
|
|
442
423
|
return hasFnMember(value, "get");
|
|
443
424
|
}
|
|
@@ -462,7 +443,7 @@ function fillTemplate(template, tokens) {
|
|
|
462
443
|
return text;
|
|
463
444
|
}
|
|
464
445
|
/** 有界并发的 map:批内并行、批间串行等待,从而把同时在打开的文件数压在 limit 以内。
|
|
465
|
-
* 不用 `Promise.all(items.map(...))`
|
|
446
|
+
* 不用 `Promise.all(items.map(...))` 一次铺开——导入内置库(整棵 agents/ 的 .md)会把
|
|
466
447
|
* 进程 fd 打满,EMFILE 抛错被单文件 catch 吞掉 → 条目静默丢失而仍报 ok:true。
|
|
467
448
|
* 分片用**递归**而非 for+await:批间等待是并发上限的实现手段,而 await-in-loop
|
|
468
449
|
* 在本包严格档里是 error(不用 disable 注释换取绿灯)。
|
|
@@ -571,14 +552,13 @@ async function readSnippetBounded(fs, entry, maxBytes) {
|
|
|
571
552
|
const DEFAULT_LIMITS = {
|
|
572
553
|
organizeTimeoutMs: ORGANIZE_TIMEOUT_MS,
|
|
573
554
|
maxSnippetBytes: MAX_SNIPPET_BYTES,
|
|
574
|
-
readConcurrency: READ_CONCURRENCY
|
|
575
|
-
importBodyMaxBytes: DEFAULT_IMPORT_BODY_MAX_BYTES
|
|
555
|
+
readConcurrency: READ_CONCURRENCY
|
|
576
556
|
};
|
|
577
557
|
/**
|
|
578
|
-
*
|
|
558
|
+
* 部署值的唯一收口:把会致命的几个数夹进安全区间,再交给读面。
|
|
579
559
|
*
|
|
580
|
-
* 为什么放在这里而不是各消费点:`limitsOf()`
|
|
581
|
-
*
|
|
560
|
+
* 为什么放在这里而不是各消费点:`limitsOf()` 是 context/organize/importTemplates
|
|
561
|
+
* 三个读面读 limits 的**唯一入口**,一处收口就不会漏掉将来新增的读面;而散在
|
|
582
562
|
* 各消费点夹,哪天新增一个调用点就等于新增一个漏洞。
|
|
583
563
|
*
|
|
584
564
|
* 夹哪两个、为什么:
|
|
@@ -588,9 +568,9 @@ const DEFAULT_LIMITS = {
|
|
|
588
568
|
* - `organizeTimeoutMs` → `[1, 2^31-1]`。上界超了会被 `setTimeout` 静默夹成 1 ms,
|
|
589
569
|
* 「调大超时」变成「立刻失败」。
|
|
590
570
|
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
571
|
+
* 剩下 `maxSnippetBytes` **故意不夹**:schema 已经挡住 NaN/±Infinity/负数/小数,
|
|
572
|
+
* 唯一还能配出来的越界值 `0` 有确定且已记账的行为(全跳过并计 tooLarge),那是用户
|
|
573
|
+
* 的显式意图,不该被悄悄改写。
|
|
594
574
|
*
|
|
595
575
|
* @param limits - 未经夹位的部署值
|
|
596
576
|
* @returns 两项关键值已夹位、其余原样透传的新对象
|
|
@@ -657,7 +637,7 @@ async function collectDirContext(fs, cwd, messages, limits) {
|
|
|
657
637
|
}
|
|
658
638
|
}
|
|
659
639
|
/**
|
|
660
|
-
*
|
|
640
|
+
* 导入路径安全策略(只读读面,无副作用:不调 LLM、不写盘,结果回给设置卡)。
|
|
661
641
|
* 规则名:「cwd 内 + 内置角色目录 allowlist」。
|
|
662
642
|
*
|
|
663
643
|
* 允许根只有三类(除此之外**一律拒绝**,含所有绝对路径):
|
|
@@ -666,7 +646,7 @@ async function collectDirContext(fs, cwd, messages, limits) {
|
|
|
666
646
|
* - 用户在设置 `importAllowRoots` 里逐条登记的目录。
|
|
667
647
|
*
|
|
668
648
|
* 拒绝规则(确定性,测试锁死):
|
|
669
|
-
* - 空串/纯空白不是路径 →
|
|
649
|
+
* - 空串/纯空白不是路径 → 拒(读面把「精确空串」当作"导入内置库"的哨兵并在调用前
|
|
670
650
|
* 拦掉,走到这里说明调用方没做归一,绝不静默降级成"全量导入");
|
|
671
651
|
* - **先查原始串的段**再查解析结果:path.resolve 会把 `..` 归一掉,只查解析后的
|
|
672
652
|
* 绝对路径等于放行 `../../etc/passwd.md`(审计复现);原始串只判 `.` / `..`,
|
|
@@ -684,30 +664,35 @@ async function collectDirContext(fs, cwd, messages, limits) {
|
|
|
684
664
|
* 门禁后仍要求每个文件能解析出非空 frontmatter name(parseAgentFrontmatter),
|
|
685
665
|
* 否则跳过并计数——绝不返回任意文件原文,只产出模板条目形状。
|
|
686
666
|
*/
|
|
667
|
+
/** 路径拆段(**逐段**比较而非字符串前缀:`/home/dev/project-two/x.md` 会骗过
|
|
668
|
+
* `startsWith("/home/dev/project")`,前缀式包含判断等于放行兄弟目录)。
|
|
669
|
+
* 两种分隔符都拆:win32 的 `path.resolve`/`path.relative` 产出 `\`,只按 `/` 拆会
|
|
670
|
+
* 把整条路径当成一段,于是合法子路径被判成越界、`..\` 也漏过逐段校验。 */
|
|
671
|
+
function pathSegments(absolute) {
|
|
672
|
+
return absolute.split(/[\\/]/u).filter((token) => token.length > 0);
|
|
673
|
+
}
|
|
687
674
|
/** 逐段校验:段为 "." / ".." 一律拒(归一前必须先拦,否则 path.resolve 吃掉 `..`);
|
|
688
675
|
* checkHidden=true 时额外拒隐藏段(凭据/敏感目录,如 `.ssh`)。 */
|
|
689
676
|
function pathTokensError(absolute, checkHidden, messages) {
|
|
690
677
|
let errorMessage;
|
|
691
|
-
for (const token of absolute
|
|
678
|
+
for (const token of pathSegments(absolute)) if (token === "." || token === ".." || checkHidden && token.startsWith(".")) {
|
|
692
679
|
errorMessage = messages.illegalPathSegment;
|
|
693
680
|
break;
|
|
694
681
|
}
|
|
695
682
|
return errorMessage;
|
|
696
683
|
}
|
|
697
|
-
/**
|
|
698
|
-
* `
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
return
|
|
684
|
+
/** child 是否落在 root 之内(含"child 就是 root 本身"= 导入根目录)。
|
|
685
|
+
* 包含关系交给 `path.relative`:win32 的盘符大小写由它的平台语义收口
|
|
686
|
+
* (实测 `path.win32.relative("C:\\Users\\Me\\Roles", "c:\\users\\me\\roles\\x.md")`
|
|
687
|
+
* 给出 `x.md`),而兄弟目录仍以 `..` 开头被拒。 */
|
|
688
|
+
function isWithinRoot(root, child) {
|
|
689
|
+
const relative = path.relative(root, child);
|
|
690
|
+
if (relative === "") return true;
|
|
691
|
+
return !path.isAbsolute(relative) && pathSegments(relative)[0] !== "..";
|
|
705
692
|
}
|
|
706
693
|
function toImportRoot(rawPath, label) {
|
|
707
|
-
const absolute = path.resolve(rawPath);
|
|
708
694
|
return {
|
|
709
|
-
absolute,
|
|
710
|
-
segments: pathSegments(absolute),
|
|
695
|
+
absolute: path.resolve(rawPath),
|
|
711
696
|
label
|
|
712
697
|
};
|
|
713
698
|
}
|
|
@@ -740,8 +725,7 @@ function importPathError(requested, cwd, allowRoots, messages) {
|
|
|
740
725
|
if (!path.isAbsolute(trimmed) && cwd === void 0) return messages.relativeNeedsCwd;
|
|
741
726
|
const absolute = path.resolve(cwd ?? "", trimmed);
|
|
742
727
|
const roots = importRootsOf(cwd, allowRoots, messages);
|
|
743
|
-
const
|
|
744
|
-
const matched = roots.find((root) => withinRoot(child, root.segments));
|
|
728
|
+
const matched = roots.find((root) => isWithinRoot(root.absolute, absolute));
|
|
745
729
|
if (matched === void 0) return outsideRootsError(absolute, roots, messages);
|
|
746
730
|
return pathTokensError(path.relative(matched.absolute, absolute), true, messages);
|
|
747
731
|
}
|
|
@@ -751,8 +735,12 @@ function agentEntryFromFile(slug, dirName, info, collided) {
|
|
|
751
735
|
const text = condenseRoleBody(info.body);
|
|
752
736
|
let result;
|
|
753
737
|
if (text.length > 0) {
|
|
738
|
+
let suffix = collided.size;
|
|
754
739
|
let id = slug;
|
|
755
|
-
while (collided.has(id))
|
|
740
|
+
while (collided.has(id)) {
|
|
741
|
+
suffix += 1;
|
|
742
|
+
id = `${slug}-${String(suffix)}`;
|
|
743
|
+
}
|
|
756
744
|
collided.add(id);
|
|
757
745
|
result = {
|
|
758
746
|
id,
|
|
@@ -946,7 +934,7 @@ async function importFromDirectory(fs, absolute, target, limits, skipped) {
|
|
|
946
934
|
* truncated 反映"确实有东西没进来",不再硬编码 false。
|
|
947
935
|
* skipPolicy 用于内置角色库自检(AGENTS_DIR 是策略里的允许根,用户输入路径正常
|
|
948
936
|
* 不需要它;保留这个开关只为让"内置全量导入"不依赖安装位置是否命中隐藏段判断)。
|
|
949
|
-
* allowRoots = 设置 importAllowRoots
|
|
937
|
+
* allowRoots = 设置 importAllowRoots 登记的额外允许目录(由读面注入)。 */
|
|
950
938
|
async function collectAgentTemplates(fs, requested, cwd, messages, opts) {
|
|
951
939
|
const limits = opts?.limits ?? DEFAULT_LIMITS;
|
|
952
940
|
const allowRoots = opts?.allowRoots ?? [];
|
|
@@ -1100,7 +1088,7 @@ async function drainStreamToText(stream, messages) {
|
|
|
1100
1088
|
}
|
|
1101
1089
|
return text.trim();
|
|
1102
1090
|
}
|
|
1103
|
-
/**
|
|
1091
|
+
/** 整理入口(读面调用;导出供测试注入假 stream)。失败文案与 system 提示都来自
|
|
1104
1092
|
* 注入的字典,本函数不读设置。 */
|
|
1105
1093
|
async function runOrganize(llm, input, messages, timeoutMs = ORGANIZE_TIMEOUT_MS, callerSignal) {
|
|
1106
1094
|
const { prompt } = input;
|
|
@@ -1159,10 +1147,10 @@ const TemplateEntrySchema = Schema.object({
|
|
|
1159
1147
|
/** settings 命名空间与 loader 行 config 共用同一 schema(单源,防漂移)。
|
|
1160
1148
|
* 值名退避为 configSchema:避免与同名 interface Config 触发 no-redeclare;
|
|
1161
1149
|
* 外部仍以 `Config` 名导入(export as),公开 API 不变。
|
|
1162
|
-
* 写入校验由 dsh-settings 在持久化前按此 schema
|
|
1150
|
+
* 写入校验由 dsh-settings 在持久化前按此 schema 完成;凡是要出现在卡上的字段都必须 `.volatile()`
|
|
1163
1151
|
* ——没有任何 volatile 字段的条目会被宿主 describe() 整条跳过
|
|
1164
|
-
* (packages/settings/settings/src/index.ts
|
|
1165
|
-
* `has no volatile fields
|
|
1152
|
+
* (packages/settings/settings/src/index.ts),设置卡读不到、写入则抛
|
|
1153
|
+
* `has no volatile fields`。
|
|
1166
1154
|
*
|
|
1167
1155
|
* 旧 `settings.register(ns, Config, { base })` 的 base 底座取消,逐字段落到 `.default(...)`:
|
|
1168
1156
|
* - importAllowRoots:`.default([])`。**空表是策略不是缺省占位**——登记目录是用户的
|
|
@@ -1170,14 +1158,14 @@ const TemplateEntrySchema = Schema.object({
|
|
|
1170
1158
|
* 见 importPathError);
|
|
1171
1159
|
* - templates:**标 volatile、不给默认**。内置精选集(scripts/gen-defaults.mjs 从
|
|
1172
1160
|
* agents/ 角色库生成的 src/default-templates.generated.ts,~640 KB)由 **host 半**
|
|
1173
|
-
* 持有、经
|
|
1161
|
+
* 持有、经 `dirPrepOrganize.defaults` 方法按需下发,client 拉到后作回落:⚠ 不给默认**不等于**读回 undefined——schemastery 对 array 字段把缺失
|
|
1174
1162
|
* 值 cast 成 `[]`(实测 `configSchema["~standard"].validate({})` 之后 `templates` 引用
|
|
1175
|
-
* `.get() === []`),宿主 describe() 又只丢 undefined
|
|
1163
|
+
* `.get() === []`),宿主 describe() 又只丢 undefined 位(`SettingsForms.projectForm`),
|
|
1176
1164
|
* 所以「未设置」递到客户端看到的就是一张空表。回落判据因此落在 client 半
|
|
1177
1165
|
* (client-entry.ts 的 `fromSettings`:空表按未设置处理),设置卡「恢复默认」=
|
|
1178
1166
|
* `unset("templates")` 且复读空值按已回落处理,故「缺失」「空表」「内置」三者同义。
|
|
1179
|
-
* 写成 `.default(...)` 会让宿主每次 describe() 都重新序列化这 640 KB(index.ts
|
|
1180
|
-
* `JSON.stringify([uid, schema.toJSON(), entry.options.config])` 取指纹,schema.ts
|
|
1167
|
+
* 写成 `.default(...)` 会让宿主每次 describe() 都重新序列化这 640 KB(index.ts 对
|
|
1168
|
+
* `JSON.stringify([uid, schema.toJSON(), entry.options.config])` 取指纹,schema.ts
|
|
1181
1169
|
* 的 plainSchema 保留 meta.default),所以内置集不进 schema、由 host 半端点按需下发;而既然空表就是
|
|
1182
1170
|
* 「未设置」的运行时形状,回落判据仍由 client 半出。 */
|
|
1183
1171
|
const configShape = {
|
|
@@ -1185,8 +1173,7 @@ const configShape = {
|
|
|
1185
1173
|
importAllowRoots: Schema.array(Schema.string()).default([]).volatile(),
|
|
1186
1174
|
organizeTimeoutMs: Schema.natural().default(ORGANIZE_TIMEOUT_MS).volatile(),
|
|
1187
1175
|
maxSnippetBytes: Schema.natural().default(MAX_SNIPPET_BYTES),
|
|
1188
|
-
readConcurrency: Schema.natural().default(READ_CONCURRENCY)
|
|
1189
|
-
importBodyMaxBytes: Schema.natural().default(DEFAULT_IMPORT_BODY_MAX_BYTES)
|
|
1176
|
+
readConcurrency: Schema.natural().default(READ_CONCURRENCY)
|
|
1190
1177
|
};
|
|
1191
1178
|
const configSchema = Schema.object(configShape);
|
|
1192
1179
|
/** volatile 引用读出的值 → importAllowRoots 字符串列表(纯函数,测试锁死)。
|
|
@@ -1199,29 +1186,13 @@ function importAllowRootsOf(raw) {
|
|
|
1199
1186
|
}
|
|
1200
1187
|
return roots;
|
|
1201
1188
|
}
|
|
1202
|
-
/** 注入的服务面:
|
|
1189
|
+
/** 注入的服务面:settings 为硬依赖(cordis 等它就绪才 apply)。
|
|
1203
1190
|
* 0.1.7 起命名空间是隐式的(= 本条目在 cordis.patch.yml 里的裸 id,可编辑字段取自
|
|
1204
1191
|
* 导出的 Config),所以 settings 不再是「注册的前提」而是「页面策略(configure)与
|
|
1205
1192
|
* 跨命名空间读(describe)」的载体;设置页只派发「host 已 serve 的命名空间 ∩
|
|
1206
1193
|
* settings.plugin.item 卡」(dsh-client-ui-settings-plugins 源码语义),configure 必须
|
|
1207
1194
|
* 发生在 apply 期,故仍硬注入。sessions/fs/llm/agentDefaultModel 仍可选读。 */
|
|
1208
1195
|
const inject = ["settings"];
|
|
1209
|
-
/** 405 回执(四个端点同形,抽出来避免重复)。 */
|
|
1210
|
-
function methodNotAllowed(res, allow) {
|
|
1211
|
-
res.setHeader("Allow", allow);
|
|
1212
|
-
sendJson(res, 405, {
|
|
1213
|
-
ok: false,
|
|
1214
|
-
error: `${allow} only`
|
|
1215
|
-
});
|
|
1216
|
-
}
|
|
1217
|
-
/** JSON.parse 失败回执(读 body 与解析分离:超限/坏流由 guardBody 回 413/400,
|
|
1218
|
-
* 这里只对"确实读到了但不是 JSON"回 400)。文案取自字典,故由调用方现读后传入。 */
|
|
1219
|
-
function sendBadRequestJson(res, error) {
|
|
1220
|
-
sendJson(res, 400, {
|
|
1221
|
-
ok: false,
|
|
1222
|
-
error
|
|
1223
|
-
});
|
|
1224
|
-
}
|
|
1225
1196
|
/** 当前默认模型解析(缺服务/抛错 → undefined)。 */
|
|
1226
1197
|
function resolveModel(deps) {
|
|
1227
1198
|
const current = deps.agentDefaultModel?.();
|
|
@@ -1271,42 +1242,42 @@ function importRequestOf(parsed, messages) {
|
|
|
1271
1242
|
sessionId: typeof sessionId === "string" ? sessionId : null
|
|
1272
1243
|
};
|
|
1273
1244
|
}
|
|
1274
|
-
/**
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1245
|
+
/** 内置精选模板表的读面。两头都按需:表在独立产物 templates.data.mjs 里,第一次被
|
|
1246
|
+
* 调用时才 import;client 首次打开「模板」下拉时才拉这一发。 */
|
|
1247
|
+
async function defaultsPayload(scope) {
|
|
1248
|
+
try {
|
|
1249
|
+
return {
|
|
1250
|
+
ok: true,
|
|
1251
|
+
templates: await (scope.deps.loadDefaultTemplates ?? loadDefaultTemplates)()
|
|
1252
|
+
};
|
|
1253
|
+
} catch (error) {
|
|
1254
|
+
return {
|
|
1255
|
+
ok: false,
|
|
1256
|
+
error: errorText(error)
|
|
1257
|
+
};
|
|
1280
1258
|
}
|
|
1259
|
+
}
|
|
1260
|
+
/** 目录上下文:缺 fs / 缺 cwd / 扫描报错各回一句,成功才下发条目表。 */
|
|
1261
|
+
async function contextPayload(scope, sessionId) {
|
|
1262
|
+
const messages = scope.messagesOf();
|
|
1281
1263
|
try {
|
|
1282
1264
|
const fs = scope.deps.fs?.();
|
|
1283
|
-
const sessionId = queryParam(req, "sessionId");
|
|
1284
1265
|
const cwd = cwdOfSession(scope.deps.sessions?.(), sessionId);
|
|
1285
|
-
if (fs === void 0) {
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
sendJson(res, 200, {
|
|
1294
|
-
ok: false,
|
|
1295
|
-
error: messages.cwdUnknown
|
|
1296
|
-
});
|
|
1297
|
-
return;
|
|
1298
|
-
}
|
|
1266
|
+
if (fs === void 0) return {
|
|
1267
|
+
ok: false,
|
|
1268
|
+
error: messages.fsUnavailable
|
|
1269
|
+
};
|
|
1270
|
+
if (cwd === void 0) return {
|
|
1271
|
+
ok: false,
|
|
1272
|
+
error: messages.cwdUnknown
|
|
1273
|
+
};
|
|
1299
1274
|
const result = await collectDirContext(fs, cwd, messages, scope.limitsOf());
|
|
1300
|
-
if (result.error !== void 0) {
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
return;
|
|
1306
|
-
}
|
|
1307
|
-
sendJson(res, 200, {
|
|
1275
|
+
if (result.error !== void 0) return {
|
|
1276
|
+
ok: false,
|
|
1277
|
+
error: result.error
|
|
1278
|
+
};
|
|
1279
|
+
return {
|
|
1308
1280
|
ok: true,
|
|
1309
|
-
csrf: scope.csrf,
|
|
1310
1281
|
cwd: result.cwd,
|
|
1311
1282
|
truncated: result.truncated,
|
|
1312
1283
|
skipped: result.skipped,
|
|
@@ -1316,46 +1287,38 @@ async function sendContextResponse(scope, req, res) {
|
|
|
1316
1287
|
sizeBytes: entry.sizeBytes,
|
|
1317
1288
|
snippet: entry.snippet
|
|
1318
1289
|
}))
|
|
1319
|
-
}
|
|
1290
|
+
};
|
|
1320
1291
|
} catch (error) {
|
|
1321
|
-
|
|
1292
|
+
return {
|
|
1322
1293
|
ok: false,
|
|
1323
1294
|
error: fillTemplate(messages.collectFailed, [["{reason}", errorText(error)]])
|
|
1324
|
-
}
|
|
1295
|
+
};
|
|
1325
1296
|
}
|
|
1326
1297
|
}
|
|
1327
|
-
/**
|
|
1328
|
-
function
|
|
1298
|
+
/** 当前默认模型(provider/model/effort)。 */
|
|
1299
|
+
function modelPayload(scope) {
|
|
1329
1300
|
const messages = scope.messagesOf();
|
|
1330
|
-
if (req.method !== "GET") {
|
|
1331
|
-
methodNotAllowed(res, "GET");
|
|
1332
|
-
return;
|
|
1333
|
-
}
|
|
1334
1301
|
const agentDefaultModel = scope.deps.agentDefaultModel?.();
|
|
1335
|
-
if (agentDefaultModel === void 0) {
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
});
|
|
1340
|
-
return;
|
|
1341
|
-
}
|
|
1302
|
+
if (agentDefaultModel === void 0) return {
|
|
1303
|
+
ok: false,
|
|
1304
|
+
error: messages.modelServiceUnavailable
|
|
1305
|
+
};
|
|
1342
1306
|
try {
|
|
1343
1307
|
const sel = agentDefaultModel.currentSelection();
|
|
1344
|
-
|
|
1308
|
+
return {
|
|
1345
1309
|
ok: true,
|
|
1346
|
-
csrf: scope.csrf,
|
|
1347
1310
|
provider: sel.provider,
|
|
1348
1311
|
model: sel.model,
|
|
1349
1312
|
...sel.reasoningEffort === void 0 ? {} : { reasoningEffort: sel.reasoningEffort }
|
|
1350
|
-
}
|
|
1313
|
+
};
|
|
1351
1314
|
} catch (error) {
|
|
1352
|
-
|
|
1315
|
+
return {
|
|
1353
1316
|
ok: false,
|
|
1354
1317
|
error: fillTemplate(messages.readModelFailed, [["{reason}", errorText(error)]])
|
|
1355
|
-
}
|
|
1318
|
+
};
|
|
1356
1319
|
}
|
|
1357
1320
|
}
|
|
1358
|
-
/** organize 请求体的输入面:JSON
|
|
1321
|
+
/** organize 请求体的输入面:JSON 已解析的 unknown → 整理入参(非字符串一律按空值处理)。 */
|
|
1359
1322
|
function organizeRequestOf(body) {
|
|
1360
1323
|
const { prompt, sessionId, entriesSummary, roleText } = body;
|
|
1361
1324
|
return {
|
|
@@ -1365,206 +1328,191 @@ function organizeRequestOf(body) {
|
|
|
1365
1328
|
sessionId: typeof sessionId === "string" ? sessionId : null
|
|
1366
1329
|
};
|
|
1367
1330
|
}
|
|
1368
|
-
/**
|
|
1369
|
-
*
|
|
1370
|
-
|
|
1371
|
-
|
|
1331
|
+
/** 整理:LLM 调用与两层错误回执。内层 catch 是「整理过程本身失败」(回裸 errorText),
|
|
1332
|
+
* 外层是「装配入参时炸」。`messages` 每次调用现读一次,不在此处二次现读。 */
|
|
1333
|
+
async function organizePayload(scope, parsed) {
|
|
1334
|
+
const messages = scope.messagesOf();
|
|
1372
1335
|
try {
|
|
1373
1336
|
const llm = scope.deps.llm?.();
|
|
1374
|
-
if (llm === void 0) {
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
});
|
|
1379
|
-
return;
|
|
1380
|
-
}
|
|
1337
|
+
if (llm === void 0) return {
|
|
1338
|
+
ok: false,
|
|
1339
|
+
error: messages.llmUnavailable
|
|
1340
|
+
};
|
|
1381
1341
|
const { prompt, entriesSummary, roleText: roleTextRaw, sessionId: sessionIdText } = organizeRequestOf(isRecord(parsed) ? parsed : {});
|
|
1382
1342
|
const sessions = scope.deps.sessions?.();
|
|
1383
1343
|
const recentTurns = resolveRecentTurns(sessions, sessionIdText);
|
|
1384
1344
|
const cwd = cwdOfSession(sessions, sessionIdText);
|
|
1385
|
-
if (cwd === void 0) {
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
});
|
|
1390
|
-
return;
|
|
1391
|
-
}
|
|
1345
|
+
if (cwd === void 0) return {
|
|
1346
|
+
ok: false,
|
|
1347
|
+
error: messages.cwdUnknown
|
|
1348
|
+
};
|
|
1392
1349
|
const model = resolveModel(scope.deps);
|
|
1393
1350
|
try {
|
|
1394
|
-
|
|
1395
|
-
prompt,
|
|
1396
|
-
cwd,
|
|
1397
|
-
entriesSummary,
|
|
1398
|
-
recentTurns,
|
|
1399
|
-
...roleTextRaw.length === 0 ? {} : { roleText: roleTextRaw },
|
|
1400
|
-
...model === void 0 ? {} : { model }
|
|
1401
|
-
}, messages, scope.limitsOf().organizeTimeoutMs, callerSignal);
|
|
1402
|
-
sendJson(res, 200, {
|
|
1351
|
+
return {
|
|
1403
1352
|
ok: true,
|
|
1404
|
-
content:
|
|
1405
|
-
|
|
1353
|
+
content: (await runOrganize(llm, {
|
|
1354
|
+
prompt,
|
|
1355
|
+
cwd,
|
|
1356
|
+
entriesSummary,
|
|
1357
|
+
recentTurns,
|
|
1358
|
+
...roleTextRaw.length === 0 ? {} : { roleText: roleTextRaw },
|
|
1359
|
+
...model === void 0 ? {} : { model }
|
|
1360
|
+
}, messages, scope.limitsOf().organizeTimeoutMs)).content
|
|
1361
|
+
};
|
|
1406
1362
|
} catch (error) {
|
|
1407
|
-
|
|
1363
|
+
return {
|
|
1408
1364
|
ok: false,
|
|
1409
1365
|
error: errorText(error)
|
|
1410
|
-
}
|
|
1366
|
+
};
|
|
1411
1367
|
}
|
|
1412
1368
|
} catch (error) {
|
|
1413
|
-
|
|
1369
|
+
return {
|
|
1414
1370
|
ok: false,
|
|
1415
1371
|
error: fillTemplate(messages.organizeFailed, [["{reason}", errorText(error)]])
|
|
1416
|
-
}
|
|
1372
|
+
};
|
|
1417
1373
|
}
|
|
1418
1374
|
}
|
|
1419
|
-
/**
|
|
1420
|
-
*
|
|
1421
|
-
|
|
1422
|
-
|
|
1375
|
+
/** 导入:路径策略拒绝单独回 4xx,导入过程本身失败回 ok:false(界面读同一句原因)。
|
|
1376
|
+
* `messages` 同 organize:字典取表一次调用一次。 */
|
|
1377
|
+
async function importPayload(scope, parsed) {
|
|
1378
|
+
const messages = scope.messagesOf();
|
|
1423
1379
|
try {
|
|
1424
1380
|
const fs = scope.deps.fs?.();
|
|
1425
|
-
if (fs === void 0) {
|
|
1426
|
-
|
|
1381
|
+
if (fs === void 0) return {
|
|
1382
|
+
status: 200,
|
|
1383
|
+
body: {
|
|
1427
1384
|
ok: false,
|
|
1428
1385
|
error: messages.fsUnavailable
|
|
1429
|
-
}
|
|
1430
|
-
|
|
1431
|
-
}
|
|
1386
|
+
}
|
|
1387
|
+
};
|
|
1432
1388
|
const request = importRequestOf(parsed, messages);
|
|
1433
|
-
if (request.rejected) {
|
|
1434
|
-
|
|
1389
|
+
if (request.rejected) return {
|
|
1390
|
+
status: request.status,
|
|
1391
|
+
body: {
|
|
1435
1392
|
ok: false,
|
|
1436
1393
|
error: request.error
|
|
1437
|
-
}
|
|
1438
|
-
|
|
1439
|
-
}
|
|
1394
|
+
}
|
|
1395
|
+
};
|
|
1440
1396
|
const allowRoots = scope.deps.importAllowRoots?.() ?? [];
|
|
1441
1397
|
const cwd = cwdOfSession(scope.deps.sessions?.(), request.sessionId);
|
|
1442
1398
|
if (!request.isBuiltin) {
|
|
1443
1399
|
const policyError = importPathError(request.requested, cwd, allowRoots, messages);
|
|
1444
|
-
if (policyError !== void 0) {
|
|
1445
|
-
|
|
1400
|
+
if (policyError !== void 0) return {
|
|
1401
|
+
status: 403,
|
|
1402
|
+
body: {
|
|
1446
1403
|
ok: false,
|
|
1447
1404
|
error: policyError
|
|
1448
|
-
}
|
|
1449
|
-
|
|
1450
|
-
}
|
|
1405
|
+
}
|
|
1406
|
+
};
|
|
1451
1407
|
}
|
|
1452
1408
|
const result = await collectAgentTemplates(fs, request.isBuiltin ? AGENTS_DIR : request.requested, cwd, messages, {
|
|
1453
1409
|
skipPolicy: request.isBuiltin,
|
|
1454
1410
|
allowRoots,
|
|
1455
1411
|
limits: scope.limitsOf()
|
|
1456
1412
|
});
|
|
1457
|
-
if (result.error !== void 0) {
|
|
1458
|
-
|
|
1413
|
+
if (result.error !== void 0) return {
|
|
1414
|
+
status: 200,
|
|
1415
|
+
body: {
|
|
1459
1416
|
ok: false,
|
|
1460
1417
|
error: result.error
|
|
1461
|
-
}
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1418
|
+
}
|
|
1419
|
+
};
|
|
1420
|
+
return {
|
|
1421
|
+
status: 200,
|
|
1422
|
+
body: {
|
|
1423
|
+
ok: true,
|
|
1424
|
+
count: result.entries.length,
|
|
1425
|
+
truncated: result.truncated,
|
|
1426
|
+
skipped: result.skipped,
|
|
1427
|
+
entries: result.entries
|
|
1428
|
+
}
|
|
1429
|
+
};
|
|
1471
1430
|
} catch (error) {
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1431
|
+
return {
|
|
1432
|
+
status: 200,
|
|
1433
|
+
body: {
|
|
1434
|
+
ok: false,
|
|
1435
|
+
error: fillTemplate(messages.importException, [["{reason}", errorText(error)]])
|
|
1436
|
+
}
|
|
1437
|
+
};
|
|
1476
1438
|
}
|
|
1477
1439
|
}
|
|
1478
|
-
|
|
1479
|
-
|
|
1440
|
+
/** 五个读面的纯载荷入口(服务与测试共用):不含任何传输层判定。 */
|
|
1441
|
+
function createRemotePayloads(deps, messagesOf) {
|
|
1480
1442
|
const scope = {
|
|
1481
1443
|
deps,
|
|
1482
|
-
csrf,
|
|
1483
1444
|
messagesOf,
|
|
1484
|
-
limitsOf
|
|
1445
|
+
limitsOf: () => sanitizeLimits(deps.limits?.() ?? DEFAULT_LIMITS)
|
|
1485
1446
|
};
|
|
1486
1447
|
return {
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
sendBadRequestJson(res, messages.invalidJsonBody);
|
|
1537
|
-
return;
|
|
1538
|
-
}
|
|
1539
|
-
const disconnected = new AbortController();
|
|
1540
|
-
req.on("close", () => {
|
|
1541
|
-
disconnected.abort();
|
|
1542
|
-
});
|
|
1543
|
-
await sendOrganizeResponse(scope, res, messages, parsed, disconnected.signal);
|
|
1544
|
-
},
|
|
1545
|
-
async importAgent(req, res) {
|
|
1546
|
-
if (!guardTrust(req, res, { servingNonLoopback })) return;
|
|
1547
|
-
const messages = messagesOf();
|
|
1548
|
-
if (req.method !== "POST") {
|
|
1549
|
-
methodNotAllowed(res, "POST");
|
|
1550
|
-
return;
|
|
1551
|
-
}
|
|
1552
|
-
const raw = await guardBody(req, res, { maxBytes: scope.limitsOf().importBodyMaxBytes });
|
|
1553
|
-
if (raw === null) return;
|
|
1554
|
-
let parsed;
|
|
1555
|
-
try {
|
|
1556
|
-
parsed = JSON.parse(raw);
|
|
1557
|
-
} catch {
|
|
1558
|
-
sendBadRequestJson(res, messages.invalidJsonBody);
|
|
1559
|
-
return;
|
|
1560
|
-
}
|
|
1561
|
-
await sendImportResponse(scope, res, messages, parsed);
|
|
1448
|
+
defaults: () => defaultsPayload(scope),
|
|
1449
|
+
context: (sessionId) => contextPayload(scope, sessionId),
|
|
1450
|
+
model: () => modelPayload(scope),
|
|
1451
|
+
organize: (parsed) => organizePayload(scope, parsed),
|
|
1452
|
+
importTemplates: (parsed) => importPayload(scope, parsed)
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
/** 官方 Typert Remote 的服务键(= 线上命名空间)。 */
|
|
1456
|
+
const DIR_PREP_REMOTE_NAMESPACE = "dirPrepOrganize";
|
|
1457
|
+
/**
|
|
1458
|
+
* 设置卡的宿主读面(官方 Typert Remote):目录上下文、当前模型、整理、导入与内置模板。
|
|
1459
|
+
* 取代原先的五条 HTTP 端点(旧数据面已删除)。
|
|
1460
|
+
*/
|
|
1461
|
+
var DirPrepRemoteService = class extends TypertRemoteService {
|
|
1462
|
+
scope;
|
|
1463
|
+
constructor(ctx, scope) {
|
|
1464
|
+
super(ctx, DIR_PREP_REMOTE_NAMESPACE);
|
|
1465
|
+
this.scope = scope;
|
|
1466
|
+
}
|
|
1467
|
+
async defaults() {
|
|
1468
|
+
return defaultsPayload(this.scope);
|
|
1469
|
+
}
|
|
1470
|
+
async context(request) {
|
|
1471
|
+
const sessionId = isRecord(request) && typeof request.sessionId === "string" ? request.sessionId : null;
|
|
1472
|
+
return contextPayload(this.scope, sessionId);
|
|
1473
|
+
}
|
|
1474
|
+
model(request) {
|
|
1475
|
+
return modelPayload(this.scope);
|
|
1476
|
+
}
|
|
1477
|
+
async organize(request) {
|
|
1478
|
+
return organizePayload(this.scope, request);
|
|
1479
|
+
}
|
|
1480
|
+
async importTemplates(request) {
|
|
1481
|
+
const result = await importPayload(this.scope, request);
|
|
1482
|
+
return result.status === 200 ? result.body : {
|
|
1483
|
+
...result.body,
|
|
1484
|
+
status: result.status
|
|
1485
|
+
};
|
|
1486
|
+
}
|
|
1487
|
+
};
|
|
1488
|
+
/** 标准装饰器的程序化调用:标记落到真正的原型上,源码不引装饰器语法。 */
|
|
1489
|
+
function markRemote(prototype, methodName) {
|
|
1490
|
+
const context = {
|
|
1491
|
+
kind: "method",
|
|
1492
|
+
name: methodName,
|
|
1493
|
+
static: false,
|
|
1494
|
+
private: false,
|
|
1495
|
+
addInitializer: (initializer) => {
|
|
1496
|
+
Reflect.apply(initializer, Object.create(prototype), []);
|
|
1562
1497
|
}
|
|
1563
1498
|
};
|
|
1499
|
+
const decorate = Remote(methodName);
|
|
1500
|
+
Reflect.apply(decorate, void 0, [Reflect.get(prototype, methodName), context]);
|
|
1501
|
+
}
|
|
1502
|
+
markRemote(DirPrepRemoteService.prototype, "defaults");
|
|
1503
|
+
markRemote(DirPrepRemoteService.prototype, "context");
|
|
1504
|
+
markRemote(DirPrepRemoteService.prototype, "model");
|
|
1505
|
+
markRemote(DirPrepRemoteService.prototype, "organize");
|
|
1506
|
+
markRemote(DirPrepRemoteService.prototype, "importTemplates");
|
|
1507
|
+
/** 注册读面(构造即注册);替身上下文没有注册口时只少掉这一栏。 */
|
|
1508
|
+
function installDirPrepRemote(ctx, scope) {
|
|
1509
|
+
if (!isRecord(ctx) || !isRecord(ctx.reflect) || typeof ctx.reflect.provide !== "function") return false;
|
|
1510
|
+
new DirPrepRemoteService(ctx, scope);
|
|
1511
|
+
return true;
|
|
1564
1512
|
}
|
|
1565
1513
|
/** settings 注册的占位清理(无额外拆卸动作,注册随插件生命周期存续)。 */
|
|
1566
1514
|
function noopDisposer() {}
|
|
1567
|
-
/** host apply:登记设置页策略 +
|
|
1515
|
+
/** host apply:登记设置页策略 + 注册官方 Remote 读面(注入子上下文,卸载即撤)。
|
|
1568
1516
|
* 0.1.7 起模板设置是**隐式注册**的(导出的 Config + 字段上的 .volatile()),本函数
|
|
1569
1517
|
* 不再向 settings 注册任何东西;配置值由 cordis 校验并填过默认后按 volatile 引用交进来
|
|
1570
1518
|
* (第二参数 config),读当前值一律 `.get()`。 */
|
|
@@ -1595,74 +1543,38 @@ function apply(ctx, config) {
|
|
|
1595
1543
|
return settings.configure({ auto: false }, ctx.fiber);
|
|
1596
1544
|
}, "dir-prep-organize: settings presentation");
|
|
1597
1545
|
});
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
}, csrf, localeMessages, servingNonLoopback);
|
|
1630
|
-
const disposeDefaults = webServer.register({
|
|
1631
|
-
kind: "exact",
|
|
1632
|
-
path: DEFAULTS_PATH,
|
|
1633
|
-
handler: (req, res) => handlers.defaults(req, res)
|
|
1634
|
-
});
|
|
1635
|
-
const disposeContext = webServer.register({
|
|
1636
|
-
kind: "exact",
|
|
1637
|
-
path: CONTEXT_PATH,
|
|
1638
|
-
handler: (req, res) => handlers.context(req, res)
|
|
1639
|
-
});
|
|
1640
|
-
const disposeModel = webServer.register({
|
|
1641
|
-
kind: "exact",
|
|
1642
|
-
path: MODEL_PATH,
|
|
1643
|
-
handler: (req, res) => {
|
|
1644
|
-
handlers.model(req, res);
|
|
1645
|
-
}
|
|
1646
|
-
});
|
|
1647
|
-
const disposeOrganize = webServer.register({
|
|
1648
|
-
kind: "exact",
|
|
1649
|
-
path: ORGANIZE_PATH,
|
|
1650
|
-
handler: (req, res) => handlers.organize(req, res)
|
|
1651
|
-
});
|
|
1652
|
-
const disposeImport = webServer.register({
|
|
1653
|
-
kind: "exact",
|
|
1654
|
-
path: IMPORT_PATH,
|
|
1655
|
-
handler: (req, res) => handlers.importAgent(req, res)
|
|
1656
|
-
});
|
|
1657
|
-
return () => {
|
|
1658
|
-
disposeDefaults();
|
|
1659
|
-
disposeContext();
|
|
1660
|
-
disposeModel();
|
|
1661
|
-
disposeOrganize();
|
|
1662
|
-
disposeImport();
|
|
1663
|
-
};
|
|
1664
|
-
}, "dir-prep-organize: webServer routes");
|
|
1546
|
+
const limitsOfConfig = () => ({
|
|
1547
|
+
organizeTimeoutMs: config.organizeTimeoutMs.get(),
|
|
1548
|
+
maxSnippetBytes: config.maxSnippetBytes,
|
|
1549
|
+
readConcurrency: config.readConcurrency
|
|
1550
|
+
});
|
|
1551
|
+
const scope = {
|
|
1552
|
+
deps: {
|
|
1553
|
+
sessions: () => {
|
|
1554
|
+
const service = getService("sessions");
|
|
1555
|
+
return isSessionsService(service) ? service : void 0;
|
|
1556
|
+
},
|
|
1557
|
+
fs: () => {
|
|
1558
|
+
const service = getService("fs");
|
|
1559
|
+
return isFsService(service) ? service : void 0;
|
|
1560
|
+
},
|
|
1561
|
+
llm: () => {
|
|
1562
|
+
const service = getService("llm");
|
|
1563
|
+
return isLlmService(service) ? service : void 0;
|
|
1564
|
+
},
|
|
1565
|
+
agentDefaultModel: () => {
|
|
1566
|
+
const service = getService("agentDefaultModel");
|
|
1567
|
+
return isAgentDefaultModel(service) ? service : void 0;
|
|
1568
|
+
},
|
|
1569
|
+
importAllowRoots: () => importAllowRootsOf(config.importAllowRoots.get()),
|
|
1570
|
+
limits: limitsOfConfig
|
|
1571
|
+
},
|
|
1572
|
+
messagesOf: localeMessages,
|
|
1573
|
+
limitsOf: () => sanitizeLimits(limitsOfConfig())
|
|
1574
|
+
};
|
|
1575
|
+
ctx.inject([], (child) => {
|
|
1576
|
+
installDirPrepRemote(child, scope);
|
|
1665
1577
|
});
|
|
1666
1578
|
}
|
|
1667
1579
|
//#endregion
|
|
1668
|
-
export { configSchema as Config, apply, buildOrganizeSystem, buildOrganizeUser, clampInt, collectAgentTemplates, collectDirContext,
|
|
1580
|
+
export { configSchema as Config, DIR_PREP_REMOTE_NAMESPACE, DirPrepRemoteService, apply, buildOrganizeSystem, buildOrganizeUser, clampInt, collectAgentTemplates, collectDirContext, createRemotePayloads, defaultTemplatesFrom, drainStreamToText, extractRecentTurns, importAllowRootsOf, importPathError, inject, installDirPrepRemote, loadDefaultTemplates, runOrganize, sanitizeLimits };
|