@zqagent/core 0.3.13 → 0.3.22

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.
Files changed (37) hide show
  1. package/dist/{TurnEngine-BZjUGic_.d.ts → TurnEngine-C8qqbilC.d.ts} +18 -3
  2. package/dist/{TurnEngine-CMGdcSdp.d.cts → TurnEngine-DKSczYaA.d.cts} +18 -3
  3. package/dist/{conflictGuard-BBOOIxxK.d.cts → conflictGuard-BuICnWEI.d.cts} +8 -0
  4. package/dist/{conflictGuard-BBOOIxxK.d.ts → conflictGuard-BuICnWEI.d.ts} +8 -0
  5. package/dist/index.cjs +3451 -1129
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.cts +242 -6
  8. package/dist/index.d.ts +242 -6
  9. package/dist/index.js +3500 -1178
  10. package/dist/index.js.map +1 -1
  11. package/dist/internal/engine/FileLogHook.d.cts +1 -1
  12. package/dist/internal/engine/FileLogHook.d.ts +1 -1
  13. package/dist/internal/update/controller.cjs +1 -1
  14. package/dist/internal/update/controller.js +1 -1
  15. package/dist/internal/update/platform.cjs +1 -1
  16. package/dist/internal/update/platform.js +1 -1
  17. package/dist/tools/diff.d.cts +1 -1
  18. package/dist/tools/diff.d.ts +1 -1
  19. package/dist/tools/explore.cjs +504 -127
  20. package/dist/tools/explore.cjs.map +1 -1
  21. package/dist/tools/explore.d.cts +1 -1
  22. package/dist/tools/explore.d.ts +1 -1
  23. package/dist/tools/explore.js +504 -127
  24. package/dist/tools/explore.js.map +1 -1
  25. package/dist/tools/files.cjs +267 -47
  26. package/dist/tools/files.cjs.map +1 -1
  27. package/dist/tools/files.d.cts +1 -1
  28. package/dist/tools/files.d.ts +1 -1
  29. package/dist/tools/files.js +267 -47
  30. package/dist/tools/files.js.map +1 -1
  31. package/dist/tools/search.cjs +103 -24
  32. package/dist/tools/search.cjs.map +1 -1
  33. package/dist/tools/search.d.cts +8 -2
  34. package/dist/tools/search.d.ts +8 -2
  35. package/dist/tools/search.js +103 -24
  36. package/dist/tools/search.js.map +1 -1
  37. package/package.json +6 -5
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { M as Mode, S as ShellPolicy, E as EngineHook, C as CompactionStartInfo, a as CompactionDoneInfo, b as EventType, c as EngineErrorInfo, P as ProviderClient, T as TurnEngine } from './TurnEngine-CMGdcSdp.cjs';
2
- export { A as ApprovalOutcome, d as Approver, e as CompactionTrigger, D as Decision, O as OpenAIProvider, f as PermissionEngine, g as PermissionRequest, h as ShellSandboxStatus, i as TurnEngineOpts, W as WsEvent } from './TurnEngine-CMGdcSdp.cjs';
1
+ import { M as Mode, S as ShellPolicy, E as EngineHook, C as CompactionStartInfo, a as CompactionDoneInfo, b as EventType, c as EngineErrorInfo, T as TurnEngine, P as ProviderClient } from './TurnEngine-DKSczYaA.cjs';
2
+ export { A as ApprovalOutcome, d as Approver, e as CompactionTrigger, D as Decision, O as OpenAIProvider, f as PermissionEngine, g as PermissionRequest, h as ShellSandboxStatus, i as TurnActor, j as TurnEngineOpts, W as WsEvent } from './TurnEngine-DKSczYaA.cjs';
3
3
  import { T as TokenUsage, M as Message, R as ReasoningEffort } from './types-BYyXWWPW.cjs';
4
4
  export { A as ATTACHMENT_DIR, d as AssistantTurn, e as AttachmentError, f as MAX_ATTACHMENTS, g as MAX_ATTACHMENT_BYTES, b as MessageAttachment, c as StoredAttachment, S as StreamChunk, h as ToolCall, a as ToolSchema, U as UPLOAD_DIR, W as WorkspaceFileRef, i as attachmentBytes, j as buildWorkspaceFileNotice, k as hasNonImageAttachments, n as normalizeAttachments, r as readStoredAttachment, s as saveAttachments, l as saveAttachmentsToWorkspace, m as splitByCapability, o as stripAttachments } from './types-BYyXWWPW.cjs';
5
5
  import { T as ToolRegistry } from './ToolRegistry-DouDvn9E.cjs';
@@ -12,7 +12,7 @@ import { a as McpServerConfig, M as McpBuiltinContext } from './McpClient-CEQBUP
12
12
  export { b as McpClient, c as McpClientOptions, d as McpToolInfo, e as McpTransportType, O as OcrLlmConfig, f as OcrRuntimeOverrides, m as mcpToolKey, s as sanitizeToolName } from './McpClient-CEQBUPuC.cjs';
13
13
  export { BUILTIN_MCP_CONFIGS, McpConfigNormalizeOptions, McpConfigNormalizeResult, defaultMcpConfigPath, loadMcpConfigFile, normalizeMcpConfig, normalizeMcpConfigs, readMcpConfigFile, saveMcpConfigFile, splitCommandLine, withBuiltinMcpConfigs } from './internal/mcp/config.cjs';
14
14
  export { registerFileTools, registerReadOnlyFileTools } from './tools/files.cjs';
15
- export { C as ConflictGuard, W as WriteConflict, c as contentDigest } from './conflictGuard-BBOOIxxK.cjs';
15
+ export { C as ConflictGuard, W as WriteConflict, c as contentDigest } from './conflictGuard-BuICnWEI.cjs';
16
16
  export { getShellCwd, registerShellTool } from './internal/tools/shell.cjs';
17
17
  export { registerSearchTool } from './tools/search.cjs';
18
18
  export { registerWebTools } from './tools/web.cjs';
@@ -413,6 +413,27 @@ interface PromptSet {
413
413
  summarySystemPrompt: string;
414
414
  /** 压缩后续接的指令(作为最后一条用户消息注入) */
415
415
  continuationContract: string;
416
+ /**
417
+ * 压缩块(`<compacted-history>`)的结构标题。
418
+ *
419
+ * 早先这些是硬编码英文,而压缩块**每轮都随上下文重发**:中文用户聊到触发
420
+ * 压缩(软阈值 60% 窗口)之后,上下文里就混进大量英文结构标题,模型的回复
421
+ * 语言随之漂移成英文 —— 表现为「我明明用中文聊,AI 却回英文」,且只在长
422
+ * 会话出现。因此这几条必须跟随界面语言。
423
+ */
424
+ compactedIntro: string;
425
+ /** 压缩块里「用户消息原文」小节标题 */
426
+ compactedUserMessagesHeader: string;
427
+ /** 压缩块里「更早的用户消息被省略」说明({n} = 条数) */
428
+ compactedOmittedNote: (n: number) => string;
429
+ /** 摘要不可用时的降级说明(旧轮次只做了裁剪) */
430
+ compactedTrimmedNote: string;
431
+ /** 机械提取的工作状态块标题 */
432
+ workingStateHeader: string;
433
+ workingStateFiles: string;
434
+ workingStateCommands: string;
435
+ /** {list} = 工具名列表 */
436
+ workingStateTools: (list: string) => string;
416
437
  /** Git 提交说明生成提示词 */
417
438
  genCommitMessageInstructions: string;
418
439
  }
@@ -481,9 +502,205 @@ declare function buildExplorerInstructions(maxIterations: number, lang?: string
481
502
  * @param workspace 工作区路径(注入到环境信息块中)
482
503
  * @param today 当前日期(ISO 格式,注入到环境信息块中)
483
504
  * @param lang 界面语言(缺省简中)
505
+ * @param repoMapBlock 代码结构图(可选)。由 repomap 渲染,**放在最后**:
506
+ * 它是系统提示词里唯一会随工作区变化的内容,紧跟其后没有别的内容,
507
+ * 这样它变化时不会击穿前面稳定前缀的服务端缓存。
484
508
  * @returns 拼接后的完整系统提示词(代理提示词 + 叙述规则 + MCP/OCR/语言引导 + 环境信息)
485
509
  */
486
- declare function buildInstructions(agentType: "code" | "cowork", workspace: string, today: string, lang?: string | null): string;
510
+ declare function buildInstructions(agentType: "code" | "cowork", workspace: string, today: string, lang?: string | null, repoMapBlock?: string): string;
511
+
512
+ /** 项目规模档:决定注入形态(文件级 / 模块级 / 只给骨架) */
513
+ type Scale = "small" | "medium" | "large";
514
+ /** 符号种类(跨语言归一化;只用于渲染分组,不追求某语言的精确分类) */
515
+ type SymbolKind = "class" | "interface" | "struct" | "enum" | "record" | "trait" | "impl" | "type"
516
+ /** 模块级常量(`export const FOO = 1`) */
517
+ | "const" | "function" | "method" | "module" | "namespace";
518
+ /** 一个符号 */
519
+ interface SymbolNode {
520
+ name: string;
521
+ kind: SymbolKind;
522
+ /**
523
+ * 声明起始行(1 起始)。渲染成 `Name:12`,模型可直接 `read_file start_line=12`,
524
+ * 省掉先 grep 一行号的那一轮 —— 一轮工具调用要重发整个上下文,比这几个字符贵得多。
525
+ */
526
+ line?: number;
527
+ /** 声明结束行;与 {@link line} 相同时渲染层只写一个行号 */
528
+ endLine?: number;
529
+ }
530
+ /** 单文件的索引结果 */
531
+ interface FileSymbols {
532
+ /** mtime(毫秒):与 size 一起构成失效判据 */
533
+ mtimeMs: number;
534
+ size: number;
535
+ /** 提取出的符号(已截断到单文件上限) */
536
+ symbols: SymbolNode[];
537
+ /**
538
+ * 包名 / 命名空间(Java `package`、C# `namespace`、Go `package`)。
539
+ * 比目录结构可靠,用于渲染分组;取不到则缺省。
540
+ */
541
+ pkg?: string;
542
+ /**
543
+ * 再导出语句数(`export * from "./x"` / `export { a } from "./x"`)。
544
+ *
545
+ * barrel 文件(`index.ts` 只有再导出)提取不到任何符号,但它往往是**全仓
546
+ * 被 import 最多的文件**,是「该从哪个包入口开始」的最佳答案。没有这个计数,
547
+ * 索引器会把它连同其它 0 符号文件一起丢掉,包入口在结构图里彻底不可见。
548
+ */
549
+ reexports?: number;
550
+ /** 该文件由哪个提取器处理(诊断用) */
551
+ via: "tree-sitter" | "regex" | "path-only";
552
+ }
553
+ /** 缓存文件内容 */
554
+ interface RepoMapCache {
555
+ version: number;
556
+ /** 工作区绝对路径(缓存自检用,避免 hash 碰撞时用错缓存) */
557
+ root: string;
558
+ builtAt: number;
559
+ scale: Scale;
560
+ /** 枚举与索引的统计,用于诊断与注入文案 */
561
+ scan: {
562
+ /** 枚举到的文件总数(可能远大于 indexed) */
563
+ files: number;
564
+ /** 进地图的文件数(有声明 / 纯 barrel / 入口;受 MAX_FILES 封顶) */
565
+ indexed: number;
566
+ /** 是否因上限被截断 */
567
+ truncated: boolean;
568
+ elapsedMs: number;
569
+ };
570
+ /** 相对工作区(POSIX 分隔符)→ 索引结果 */
571
+ files: Record<string, FileSymbols>;
572
+ /**
573
+ * 项目形状(探测 + 学习得到)。存在时后续会话跳过探测。
574
+ */
575
+ shape?: ProjectShape;
576
+ }
577
+ /** 构建文件暴露出的项目形状 */
578
+ interface ProjectShape {
579
+ /** 构建体系类型 */
580
+ kinds: string[];
581
+ /** 源码根(相对工作区,POSIX),如 `src/main/java` */
582
+ sourceRoots: string[];
583
+ /** 模块划分 */
584
+ modules: Array<{
585
+ /** 相对工作区的模块目录;根模块为 "." */
586
+ path: string;
587
+ /** 该模块的构建文件名 */
588
+ buildFile?: string;
589
+ /** 识别出的框架(spring-boot / aspnet / cmake / cargo / npm-workspace …) */
590
+ framework?: string;
591
+ /** 入口文件(相对工作区) */
592
+ entry?: string;
593
+ }>;
594
+ /** 连续观察到同一形状的次数;达到阈值才固化 */
595
+ confirmations: number;
596
+ learnedAt: number;
597
+ }
598
+ /** 排序后的候选文件 */
599
+ interface RankedFile {
600
+ /** 相对工作区的 POSIX 路径 */
601
+ path: string;
602
+ score: number;
603
+ }
604
+ /** 对外 API 返回的仓库地图 */
605
+ interface RepoMap {
606
+ root: string;
607
+ scale: Scale;
608
+ scan: RepoMapCache["scan"];
609
+ /** 已排序的文件(score 降序),与 {@link files} 的键集完全一致 */
610
+ ranked: RankedFile[];
611
+ /**
612
+ * path → 索引结果,**只含进地图的文件**:有顶层声明、纯 barrel、或模块入口。
613
+ *
614
+ * 解析成功但三者都不满足的文件(例如只有顶层调用的脚本)不在这里 —— 它们在
615
+ * 结构图里只能占一行而不提供任何定位信息。索引磁盘缓存仍保留它们以支持增量。
616
+ */
617
+ files: Record<string, FileSymbols>;
618
+ shape?: ProjectShape;
619
+ }
620
+
621
+ /**
622
+ * 渲染:把索引结果压成能塞进系统提示词的一小段文本
623
+ *
624
+ * 三个档位(由项目规模决定),核心矛盾是同一个:
625
+ * **大项目里文件太多,列不完也没意义**。5 万个文件里列 250 个类名,对模型
626
+ * 定位代码毫无帮助;而「payment-service 里有 gateway 层」这种模块级信息
627
+ * 才能真正指路。所以大项目只给骨架,细节交给 `repo_map` 工具按需下钻。
628
+ *
629
+ * token 上的取舍(都按"能否帮模型少走一轮"来判断):
630
+ * - 目录路径只写一次(分组标题),不在每行重复
631
+ * - 符号写「名字 + 行号」,不写 kind:行号能省掉先 grep 定位的那一轮,
632
+ * kind 省不了任何一次工具调用
633
+ * - 不画树形符号(`├──` 每行要花 4-8 个 token)
634
+ */
635
+
636
+ /**
637
+ * 渲染 repo map 为可注入提示词的文本。
638
+ *
639
+ * @param map 仓库地图
640
+ * @returns 空字符串表示没有可渲染内容(调用方应跳过注入)
641
+ */
642
+ declare function renderRepoMap(map: RepoMap): string;
643
+
644
+ /**
645
+ * 确保运行时已初始化。
646
+ *
647
+ * @returns true = 可用;false = 不可用(调用方降级到正则提取)
648
+ */
649
+ declare function ensureRuntime(): Promise<boolean>;
650
+
651
+ /**
652
+ * 取得某工作区的结构图(带进程内复用与 TTL 刷新)。
653
+ *
654
+ * @param workspace 工作区绝对路径
655
+ * @returns 永不为 null;失败时是空地图(有旧图时退回旧图)
656
+ */
657
+ declare function loadRepoMap(workspace: string): Promise<RepoMap>;
658
+ /**
659
+ * 作废某工作区的缓存(写文件后调用)。
660
+ *
661
+ * 只丢缓存不重建:真正的重建发生在下一次取用时,且是增量的 —— 连续写 10 个
662
+ * 文件也只会在下一次真正需要结构图时重建一次。
663
+ */
664
+ declare function invalidateRepoMap(workspace: string): void;
665
+ /** 清空进程内缓存(测试 / 工作区内容大幅变化时) */
666
+ declare function resetRepoMapMemo(): void;
667
+
668
+ /**
669
+ * 把代码结构图接进一个已经建好的引擎。
670
+ *
671
+ * `createDefaultEngine` 用它做默认装配;CLI / server 这类自己拼装系统提示词的
672
+ * 调用方也用它,否则 `!instructions` 那道判断会让结构图整段失效 —— 它们都传了
673
+ * 自定义提示词(按当前 agent / 界面语言组装),属于设计上必须传的场景。
674
+ *
675
+ * 注入方式是**回调**而不是一段固定字符串:提示词的内容由调用方决定(agent
676
+ * 人设、语言),这里只负责把渲染好的结构图块交给它。这样 `attach.ts` 不需要
677
+ * 知道 AgentRegistry 或 PromptLang 的存在。
678
+ *
679
+ * 失败一律静默:结构图是纯增益,索引失败时表现为「没有结构图」,而不是会话报错。
680
+ */
681
+
682
+ interface AttachRepoMapOptions {
683
+ /** 工作区根目录 */
684
+ workspace: string;
685
+ /**
686
+ * 用最新的结构图块(可能为空串)重建系统提示词。
687
+ *
688
+ * 调用方在这里拼自己的提示词(`AgentRegistry.buildSystemPrompt` /
689
+ * `buildInstructions`),再 `engine.setSystemPrompt(...)`。
690
+ */
691
+ buildPrompt: (repoMapBlock: string) => string;
692
+ /** 设 false 可完全关闭(默认 true) */
693
+ enabled?: boolean;
694
+ }
695
+ /**
696
+ * 立即挂上「写文件后失效 + 合并刷新」的钩子,并在后台补一次结构图。
697
+ *
698
+ * 首轮渲染不阻塞调用方:索引大仓库要几百毫秒到几秒,挡在装配流程里会让启动
699
+ * 明显变慢;而首轮没有结构图也能正常跑,只是少了点方向感。
700
+ *
701
+ * @returns 清理函数(引擎被判废时调用,避免定时器挂在废弃的引擎上)
702
+ */
703
+ declare function attachRepoMap(engine: TurnEngine, opts: AttachRepoMapOptions): () => void;
487
704
 
488
705
  /**
489
706
  * Agent 注册表(AgentRegistry)
@@ -566,8 +783,11 @@ declare class AgentRegistry {
566
783
  * @param workspace 工作区路径(注入环境信息块)
567
784
  * @param today 当前日期(ISO 格式,注入环境信息块)
568
785
  * @param lang 界面语言(缺省简体中文)
786
+ * @param repoMapBlock 代码结构图(可选)。由 repomap 渲染,**放在最后**:
787
+ * 它是系统提示词里唯一随工作区变化的内容,紧跟其后没有别的内容,
788
+ * 这样它变化时不会击穿前面稳定前缀的服务端缓存。
569
789
  */
570
- buildSystemPrompt(name: string, workspace: string, today: string, lang?: string | null): string;
790
+ buildSystemPrompt(name: string, workspace: string, today: string, lang?: string | null, repoMapBlock?: string): string;
571
791
  }
572
792
 
573
793
  /**
@@ -917,6 +1137,9 @@ declare function gitOk(r: GitResult): boolean;
917
1137
  /** 取 stderr/stdout 的首行,用于错误提示 */
918
1138
  declare function gitMessage(r: GitResult): string;
919
1139
 
1140
+ /** 注册 repo_map 工具(低风险只读) */
1141
+ declare function registerRepoMapTool(registry: ToolRegistry, workspace: string): void;
1142
+
920
1143
  /**
921
1144
  * 终端工具(控制台「终端」面板的长驻会话 → Agent 的只读上下文)
922
1145
  *
@@ -1174,6 +1397,19 @@ interface CreateEngineOpts {
1174
1397
  * 仅建议在隔离环境或完全可信的输入下使用)。
1175
1398
  */
1176
1399
  defaultApproval?: "error" | "deny" | "allow";
1400
+ /**
1401
+ * 代码结构图(repo map)。
1402
+ *
1403
+ * 开启后会在**后台**索引工作区,构建完把结构摘要补进系统提示词,用于让模型
1404
+ * 在读代码前就知道大致结构,减少「grep 摸底 → 读错文件 → 换词重搜」的轮次。
1405
+ *
1406
+ * 只在调用方没有显式传 `instructions` 时生效(自定义提示词的集成自己决定)。
1407
+ * 任何失败都静默跳过,不影响会话。
1408
+ */
1409
+ repomap?: {
1410
+ /** 默认 true;设 false 可完全关闭 */
1411
+ enabled?: boolean;
1412
+ };
1177
1413
  }
1178
1414
  /**
1179
1415
  * 一键装配完整的 TurnEngine
@@ -1190,4 +1426,4 @@ interface CreateEngineOpts {
1190
1426
  */
1191
1427
  declare function createDefaultEngine(opts: CreateEngineOpts): TurnEngine;
1192
1428
 
1193
- export { type AgentDef, AgentRegistry, BUILTIN_AGENTS, CODE_INSTRUCTIONS, COWORK_INSTRUCTIONS, CompactionDoneInfo, CompactionStartInfo, CompositeHook, type ConfinementResult, ConsoleHook, type ConsoleHookOptions, type CreateEngineOpts, DEFAULT_ALLOWED_COMMANDS, DISCUSS_MODE_CONTEXT, type DownloadResult, EngineErrorInfo, EngineHook, EventType, type GitResult, MCP_CALL_TOOL, MCP_GUIDANCE, MCP_SEARCH_TOOL, MarkdownRenderer, McpBuiltinContext, type McpConnectResult, type McpLazyOptions, McpManager, type McpManagerOptions, McpServerConfig, Message, Mode, OCR_GUIDANCE, PLAN_MODE_CONTEXT, PROMPT_LANGS, type PermissionConfig, type PromptLang, type PromptSet, ProviderClient, ReasoningEffort, type RepairResult, type ResolveVcsOptions, type ResolvedBin, type ResolvedMedia, type RestoreResult, SHADOW_DIR, SandboxMode, SandboxOptions, SessionVcs, ShellPolicy, SubagentHook, type TerminalMetaView, type TerminalRecordView, type TerminalSource, TokenUsage, ToolRegistry, type TurnCommit, TurnEngine, type VcsConfigMode, VcsHook, type VcsMode, type VcsPlan, agentRef, assessCommand, buildEnvBlock, buildExplorerInstructions, buildInstructions, createDefaultEngine, downloadFfmpeg, getPrompts, gitMessage, gitOk, installHints, isGitRepo, loadPermissionConfig, matchPromptLang, normalizePromptLang, promptSets, registerTerminalTools, repairToolCallHistory, repairToolCallHistoryDetailed, resetFfmpegCache, resolveMedia, resolveVcsPlan, runGit, sessionIndexPath };
1429
+ export { type AgentDef, AgentRegistry, type AttachRepoMapOptions, BUILTIN_AGENTS, CODE_INSTRUCTIONS, COWORK_INSTRUCTIONS, CompactionDoneInfo, CompactionStartInfo, CompositeHook, type ConfinementResult, ConsoleHook, type ConsoleHookOptions, type CreateEngineOpts, DEFAULT_ALLOWED_COMMANDS, DISCUSS_MODE_CONTEXT, type DownloadResult, EngineErrorInfo, EngineHook, EventType, type GitResult, MCP_CALL_TOOL, MCP_GUIDANCE, MCP_SEARCH_TOOL, MarkdownRenderer, McpBuiltinContext, type McpConnectResult, type McpLazyOptions, McpManager, type McpManagerOptions, McpServerConfig, Message, Mode, OCR_GUIDANCE, PLAN_MODE_CONTEXT, PROMPT_LANGS, type PermissionConfig, type PromptLang, type PromptSet, ProviderClient, ReasoningEffort, type RepairResult, type RepoMap, type ResolveVcsOptions, type ResolvedBin, type ResolvedMedia, type RestoreResult, SHADOW_DIR, SandboxMode, SandboxOptions, type Scale, SessionVcs, ShellPolicy, SubagentHook, type TerminalMetaView, type TerminalRecordView, type TerminalSource, TokenUsage, ToolRegistry, type TurnCommit, TurnEngine, type VcsConfigMode, VcsHook, type VcsMode, type VcsPlan, agentRef, assessCommand, attachRepoMap, buildEnvBlock, buildExplorerInstructions, buildInstructions, createDefaultEngine, downloadFfmpeg, ensureRuntime, getPrompts, gitMessage, gitOk, installHints, invalidateRepoMap, isGitRepo, loadPermissionConfig, loadRepoMap, matchPromptLang, normalizePromptLang, promptSets, registerRepoMapTool, registerTerminalTools, renderRepoMap, repairToolCallHistory, repairToolCallHistoryDetailed, resetFfmpegCache, resetRepoMapMemo, resolveMedia, resolveVcsPlan, runGit, sessionIndexPath };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { M as Mode, S as ShellPolicy, E as EngineHook, C as CompactionStartInfo, a as CompactionDoneInfo, b as EventType, c as EngineErrorInfo, P as ProviderClient, T as TurnEngine } from './TurnEngine-BZjUGic_.js';
2
- export { A as ApprovalOutcome, d as Approver, e as CompactionTrigger, D as Decision, O as OpenAIProvider, f as PermissionEngine, g as PermissionRequest, h as ShellSandboxStatus, i as TurnEngineOpts, W as WsEvent } from './TurnEngine-BZjUGic_.js';
1
+ import { M as Mode, S as ShellPolicy, E as EngineHook, C as CompactionStartInfo, a as CompactionDoneInfo, b as EventType, c as EngineErrorInfo, T as TurnEngine, P as ProviderClient } from './TurnEngine-C8qqbilC.js';
2
+ export { A as ApprovalOutcome, d as Approver, e as CompactionTrigger, D as Decision, O as OpenAIProvider, f as PermissionEngine, g as PermissionRequest, h as ShellSandboxStatus, i as TurnActor, j as TurnEngineOpts, W as WsEvent } from './TurnEngine-C8qqbilC.js';
3
3
  import { T as TokenUsage, M as Message, R as ReasoningEffort } from './types-BYyXWWPW.js';
4
4
  export { A as ATTACHMENT_DIR, d as AssistantTurn, e as AttachmentError, f as MAX_ATTACHMENTS, g as MAX_ATTACHMENT_BYTES, b as MessageAttachment, c as StoredAttachment, S as StreamChunk, h as ToolCall, a as ToolSchema, U as UPLOAD_DIR, W as WorkspaceFileRef, i as attachmentBytes, j as buildWorkspaceFileNotice, k as hasNonImageAttachments, n as normalizeAttachments, r as readStoredAttachment, s as saveAttachments, l as saveAttachmentsToWorkspace, m as splitByCapability, o as stripAttachments } from './types-BYyXWWPW.js';
5
5
  import { T as ToolRegistry } from './ToolRegistry-Cy0fQxrb.js';
@@ -12,7 +12,7 @@ import { a as McpServerConfig, M as McpBuiltinContext } from './McpClient-CEQBUP
12
12
  export { b as McpClient, c as McpClientOptions, d as McpToolInfo, e as McpTransportType, O as OcrLlmConfig, f as OcrRuntimeOverrides, m as mcpToolKey, s as sanitizeToolName } from './McpClient-CEQBUPuC.js';
13
13
  export { BUILTIN_MCP_CONFIGS, McpConfigNormalizeOptions, McpConfigNormalizeResult, defaultMcpConfigPath, loadMcpConfigFile, normalizeMcpConfig, normalizeMcpConfigs, readMcpConfigFile, saveMcpConfigFile, splitCommandLine, withBuiltinMcpConfigs } from './internal/mcp/config.js';
14
14
  export { registerFileTools, registerReadOnlyFileTools } from './tools/files.js';
15
- export { C as ConflictGuard, W as WriteConflict, c as contentDigest } from './conflictGuard-BBOOIxxK.js';
15
+ export { C as ConflictGuard, W as WriteConflict, c as contentDigest } from './conflictGuard-BuICnWEI.js';
16
16
  export { getShellCwd, registerShellTool } from './internal/tools/shell.js';
17
17
  export { registerSearchTool } from './tools/search.js';
18
18
  export { registerWebTools } from './tools/web.js';
@@ -413,6 +413,27 @@ interface PromptSet {
413
413
  summarySystemPrompt: string;
414
414
  /** 压缩后续接的指令(作为最后一条用户消息注入) */
415
415
  continuationContract: string;
416
+ /**
417
+ * 压缩块(`<compacted-history>`)的结构标题。
418
+ *
419
+ * 早先这些是硬编码英文,而压缩块**每轮都随上下文重发**:中文用户聊到触发
420
+ * 压缩(软阈值 60% 窗口)之后,上下文里就混进大量英文结构标题,模型的回复
421
+ * 语言随之漂移成英文 —— 表现为「我明明用中文聊,AI 却回英文」,且只在长
422
+ * 会话出现。因此这几条必须跟随界面语言。
423
+ */
424
+ compactedIntro: string;
425
+ /** 压缩块里「用户消息原文」小节标题 */
426
+ compactedUserMessagesHeader: string;
427
+ /** 压缩块里「更早的用户消息被省略」说明({n} = 条数) */
428
+ compactedOmittedNote: (n: number) => string;
429
+ /** 摘要不可用时的降级说明(旧轮次只做了裁剪) */
430
+ compactedTrimmedNote: string;
431
+ /** 机械提取的工作状态块标题 */
432
+ workingStateHeader: string;
433
+ workingStateFiles: string;
434
+ workingStateCommands: string;
435
+ /** {list} = 工具名列表 */
436
+ workingStateTools: (list: string) => string;
416
437
  /** Git 提交说明生成提示词 */
417
438
  genCommitMessageInstructions: string;
418
439
  }
@@ -481,9 +502,205 @@ declare function buildExplorerInstructions(maxIterations: number, lang?: string
481
502
  * @param workspace 工作区路径(注入到环境信息块中)
482
503
  * @param today 当前日期(ISO 格式,注入到环境信息块中)
483
504
  * @param lang 界面语言(缺省简中)
505
+ * @param repoMapBlock 代码结构图(可选)。由 repomap 渲染,**放在最后**:
506
+ * 它是系统提示词里唯一会随工作区变化的内容,紧跟其后没有别的内容,
507
+ * 这样它变化时不会击穿前面稳定前缀的服务端缓存。
484
508
  * @returns 拼接后的完整系统提示词(代理提示词 + 叙述规则 + MCP/OCR/语言引导 + 环境信息)
485
509
  */
486
- declare function buildInstructions(agentType: "code" | "cowork", workspace: string, today: string, lang?: string | null): string;
510
+ declare function buildInstructions(agentType: "code" | "cowork", workspace: string, today: string, lang?: string | null, repoMapBlock?: string): string;
511
+
512
+ /** 项目规模档:决定注入形态(文件级 / 模块级 / 只给骨架) */
513
+ type Scale = "small" | "medium" | "large";
514
+ /** 符号种类(跨语言归一化;只用于渲染分组,不追求某语言的精确分类) */
515
+ type SymbolKind = "class" | "interface" | "struct" | "enum" | "record" | "trait" | "impl" | "type"
516
+ /** 模块级常量(`export const FOO = 1`) */
517
+ | "const" | "function" | "method" | "module" | "namespace";
518
+ /** 一个符号 */
519
+ interface SymbolNode {
520
+ name: string;
521
+ kind: SymbolKind;
522
+ /**
523
+ * 声明起始行(1 起始)。渲染成 `Name:12`,模型可直接 `read_file start_line=12`,
524
+ * 省掉先 grep 一行号的那一轮 —— 一轮工具调用要重发整个上下文,比这几个字符贵得多。
525
+ */
526
+ line?: number;
527
+ /** 声明结束行;与 {@link line} 相同时渲染层只写一个行号 */
528
+ endLine?: number;
529
+ }
530
+ /** 单文件的索引结果 */
531
+ interface FileSymbols {
532
+ /** mtime(毫秒):与 size 一起构成失效判据 */
533
+ mtimeMs: number;
534
+ size: number;
535
+ /** 提取出的符号(已截断到单文件上限) */
536
+ symbols: SymbolNode[];
537
+ /**
538
+ * 包名 / 命名空间(Java `package`、C# `namespace`、Go `package`)。
539
+ * 比目录结构可靠,用于渲染分组;取不到则缺省。
540
+ */
541
+ pkg?: string;
542
+ /**
543
+ * 再导出语句数(`export * from "./x"` / `export { a } from "./x"`)。
544
+ *
545
+ * barrel 文件(`index.ts` 只有再导出)提取不到任何符号,但它往往是**全仓
546
+ * 被 import 最多的文件**,是「该从哪个包入口开始」的最佳答案。没有这个计数,
547
+ * 索引器会把它连同其它 0 符号文件一起丢掉,包入口在结构图里彻底不可见。
548
+ */
549
+ reexports?: number;
550
+ /** 该文件由哪个提取器处理(诊断用) */
551
+ via: "tree-sitter" | "regex" | "path-only";
552
+ }
553
+ /** 缓存文件内容 */
554
+ interface RepoMapCache {
555
+ version: number;
556
+ /** 工作区绝对路径(缓存自检用,避免 hash 碰撞时用错缓存) */
557
+ root: string;
558
+ builtAt: number;
559
+ scale: Scale;
560
+ /** 枚举与索引的统计,用于诊断与注入文案 */
561
+ scan: {
562
+ /** 枚举到的文件总数(可能远大于 indexed) */
563
+ files: number;
564
+ /** 进地图的文件数(有声明 / 纯 barrel / 入口;受 MAX_FILES 封顶) */
565
+ indexed: number;
566
+ /** 是否因上限被截断 */
567
+ truncated: boolean;
568
+ elapsedMs: number;
569
+ };
570
+ /** 相对工作区(POSIX 分隔符)→ 索引结果 */
571
+ files: Record<string, FileSymbols>;
572
+ /**
573
+ * 项目形状(探测 + 学习得到)。存在时后续会话跳过探测。
574
+ */
575
+ shape?: ProjectShape;
576
+ }
577
+ /** 构建文件暴露出的项目形状 */
578
+ interface ProjectShape {
579
+ /** 构建体系类型 */
580
+ kinds: string[];
581
+ /** 源码根(相对工作区,POSIX),如 `src/main/java` */
582
+ sourceRoots: string[];
583
+ /** 模块划分 */
584
+ modules: Array<{
585
+ /** 相对工作区的模块目录;根模块为 "." */
586
+ path: string;
587
+ /** 该模块的构建文件名 */
588
+ buildFile?: string;
589
+ /** 识别出的框架(spring-boot / aspnet / cmake / cargo / npm-workspace …) */
590
+ framework?: string;
591
+ /** 入口文件(相对工作区) */
592
+ entry?: string;
593
+ }>;
594
+ /** 连续观察到同一形状的次数;达到阈值才固化 */
595
+ confirmations: number;
596
+ learnedAt: number;
597
+ }
598
+ /** 排序后的候选文件 */
599
+ interface RankedFile {
600
+ /** 相对工作区的 POSIX 路径 */
601
+ path: string;
602
+ score: number;
603
+ }
604
+ /** 对外 API 返回的仓库地图 */
605
+ interface RepoMap {
606
+ root: string;
607
+ scale: Scale;
608
+ scan: RepoMapCache["scan"];
609
+ /** 已排序的文件(score 降序),与 {@link files} 的键集完全一致 */
610
+ ranked: RankedFile[];
611
+ /**
612
+ * path → 索引结果,**只含进地图的文件**:有顶层声明、纯 barrel、或模块入口。
613
+ *
614
+ * 解析成功但三者都不满足的文件(例如只有顶层调用的脚本)不在这里 —— 它们在
615
+ * 结构图里只能占一行而不提供任何定位信息。索引磁盘缓存仍保留它们以支持增量。
616
+ */
617
+ files: Record<string, FileSymbols>;
618
+ shape?: ProjectShape;
619
+ }
620
+
621
+ /**
622
+ * 渲染:把索引结果压成能塞进系统提示词的一小段文本
623
+ *
624
+ * 三个档位(由项目规模决定),核心矛盾是同一个:
625
+ * **大项目里文件太多,列不完也没意义**。5 万个文件里列 250 个类名,对模型
626
+ * 定位代码毫无帮助;而「payment-service 里有 gateway 层」这种模块级信息
627
+ * 才能真正指路。所以大项目只给骨架,细节交给 `repo_map` 工具按需下钻。
628
+ *
629
+ * token 上的取舍(都按"能否帮模型少走一轮"来判断):
630
+ * - 目录路径只写一次(分组标题),不在每行重复
631
+ * - 符号写「名字 + 行号」,不写 kind:行号能省掉先 grep 定位的那一轮,
632
+ * kind 省不了任何一次工具调用
633
+ * - 不画树形符号(`├──` 每行要花 4-8 个 token)
634
+ */
635
+
636
+ /**
637
+ * 渲染 repo map 为可注入提示词的文本。
638
+ *
639
+ * @param map 仓库地图
640
+ * @returns 空字符串表示没有可渲染内容(调用方应跳过注入)
641
+ */
642
+ declare function renderRepoMap(map: RepoMap): string;
643
+
644
+ /**
645
+ * 确保运行时已初始化。
646
+ *
647
+ * @returns true = 可用;false = 不可用(调用方降级到正则提取)
648
+ */
649
+ declare function ensureRuntime(): Promise<boolean>;
650
+
651
+ /**
652
+ * 取得某工作区的结构图(带进程内复用与 TTL 刷新)。
653
+ *
654
+ * @param workspace 工作区绝对路径
655
+ * @returns 永不为 null;失败时是空地图(有旧图时退回旧图)
656
+ */
657
+ declare function loadRepoMap(workspace: string): Promise<RepoMap>;
658
+ /**
659
+ * 作废某工作区的缓存(写文件后调用)。
660
+ *
661
+ * 只丢缓存不重建:真正的重建发生在下一次取用时,且是增量的 —— 连续写 10 个
662
+ * 文件也只会在下一次真正需要结构图时重建一次。
663
+ */
664
+ declare function invalidateRepoMap(workspace: string): void;
665
+ /** 清空进程内缓存(测试 / 工作区内容大幅变化时) */
666
+ declare function resetRepoMapMemo(): void;
667
+
668
+ /**
669
+ * 把代码结构图接进一个已经建好的引擎。
670
+ *
671
+ * `createDefaultEngine` 用它做默认装配;CLI / server 这类自己拼装系统提示词的
672
+ * 调用方也用它,否则 `!instructions` 那道判断会让结构图整段失效 —— 它们都传了
673
+ * 自定义提示词(按当前 agent / 界面语言组装),属于设计上必须传的场景。
674
+ *
675
+ * 注入方式是**回调**而不是一段固定字符串:提示词的内容由调用方决定(agent
676
+ * 人设、语言),这里只负责把渲染好的结构图块交给它。这样 `attach.ts` 不需要
677
+ * 知道 AgentRegistry 或 PromptLang 的存在。
678
+ *
679
+ * 失败一律静默:结构图是纯增益,索引失败时表现为「没有结构图」,而不是会话报错。
680
+ */
681
+
682
+ interface AttachRepoMapOptions {
683
+ /** 工作区根目录 */
684
+ workspace: string;
685
+ /**
686
+ * 用最新的结构图块(可能为空串)重建系统提示词。
687
+ *
688
+ * 调用方在这里拼自己的提示词(`AgentRegistry.buildSystemPrompt` /
689
+ * `buildInstructions`),再 `engine.setSystemPrompt(...)`。
690
+ */
691
+ buildPrompt: (repoMapBlock: string) => string;
692
+ /** 设 false 可完全关闭(默认 true) */
693
+ enabled?: boolean;
694
+ }
695
+ /**
696
+ * 立即挂上「写文件后失效 + 合并刷新」的钩子,并在后台补一次结构图。
697
+ *
698
+ * 首轮渲染不阻塞调用方:索引大仓库要几百毫秒到几秒,挡在装配流程里会让启动
699
+ * 明显变慢;而首轮没有结构图也能正常跑,只是少了点方向感。
700
+ *
701
+ * @returns 清理函数(引擎被判废时调用,避免定时器挂在废弃的引擎上)
702
+ */
703
+ declare function attachRepoMap(engine: TurnEngine, opts: AttachRepoMapOptions): () => void;
487
704
 
488
705
  /**
489
706
  * Agent 注册表(AgentRegistry)
@@ -566,8 +783,11 @@ declare class AgentRegistry {
566
783
  * @param workspace 工作区路径(注入环境信息块)
567
784
  * @param today 当前日期(ISO 格式,注入环境信息块)
568
785
  * @param lang 界面语言(缺省简体中文)
786
+ * @param repoMapBlock 代码结构图(可选)。由 repomap 渲染,**放在最后**:
787
+ * 它是系统提示词里唯一随工作区变化的内容,紧跟其后没有别的内容,
788
+ * 这样它变化时不会击穿前面稳定前缀的服务端缓存。
569
789
  */
570
- buildSystemPrompt(name: string, workspace: string, today: string, lang?: string | null): string;
790
+ buildSystemPrompt(name: string, workspace: string, today: string, lang?: string | null, repoMapBlock?: string): string;
571
791
  }
572
792
 
573
793
  /**
@@ -917,6 +1137,9 @@ declare function gitOk(r: GitResult): boolean;
917
1137
  /** 取 stderr/stdout 的首行,用于错误提示 */
918
1138
  declare function gitMessage(r: GitResult): string;
919
1139
 
1140
+ /** 注册 repo_map 工具(低风险只读) */
1141
+ declare function registerRepoMapTool(registry: ToolRegistry, workspace: string): void;
1142
+
920
1143
  /**
921
1144
  * 终端工具(控制台「终端」面板的长驻会话 → Agent 的只读上下文)
922
1145
  *
@@ -1174,6 +1397,19 @@ interface CreateEngineOpts {
1174
1397
  * 仅建议在隔离环境或完全可信的输入下使用)。
1175
1398
  */
1176
1399
  defaultApproval?: "error" | "deny" | "allow";
1400
+ /**
1401
+ * 代码结构图(repo map)。
1402
+ *
1403
+ * 开启后会在**后台**索引工作区,构建完把结构摘要补进系统提示词,用于让模型
1404
+ * 在读代码前就知道大致结构,减少「grep 摸底 → 读错文件 → 换词重搜」的轮次。
1405
+ *
1406
+ * 只在调用方没有显式传 `instructions` 时生效(自定义提示词的集成自己决定)。
1407
+ * 任何失败都静默跳过,不影响会话。
1408
+ */
1409
+ repomap?: {
1410
+ /** 默认 true;设 false 可完全关闭 */
1411
+ enabled?: boolean;
1412
+ };
1177
1413
  }
1178
1414
  /**
1179
1415
  * 一键装配完整的 TurnEngine
@@ -1190,4 +1426,4 @@ interface CreateEngineOpts {
1190
1426
  */
1191
1427
  declare function createDefaultEngine(opts: CreateEngineOpts): TurnEngine;
1192
1428
 
1193
- export { type AgentDef, AgentRegistry, BUILTIN_AGENTS, CODE_INSTRUCTIONS, COWORK_INSTRUCTIONS, CompactionDoneInfo, CompactionStartInfo, CompositeHook, type ConfinementResult, ConsoleHook, type ConsoleHookOptions, type CreateEngineOpts, DEFAULT_ALLOWED_COMMANDS, DISCUSS_MODE_CONTEXT, type DownloadResult, EngineErrorInfo, EngineHook, EventType, type GitResult, MCP_CALL_TOOL, MCP_GUIDANCE, MCP_SEARCH_TOOL, MarkdownRenderer, McpBuiltinContext, type McpConnectResult, type McpLazyOptions, McpManager, type McpManagerOptions, McpServerConfig, Message, Mode, OCR_GUIDANCE, PLAN_MODE_CONTEXT, PROMPT_LANGS, type PermissionConfig, type PromptLang, type PromptSet, ProviderClient, ReasoningEffort, type RepairResult, type ResolveVcsOptions, type ResolvedBin, type ResolvedMedia, type RestoreResult, SHADOW_DIR, SandboxMode, SandboxOptions, SessionVcs, ShellPolicy, SubagentHook, type TerminalMetaView, type TerminalRecordView, type TerminalSource, TokenUsage, ToolRegistry, type TurnCommit, TurnEngine, type VcsConfigMode, VcsHook, type VcsMode, type VcsPlan, agentRef, assessCommand, buildEnvBlock, buildExplorerInstructions, buildInstructions, createDefaultEngine, downloadFfmpeg, getPrompts, gitMessage, gitOk, installHints, isGitRepo, loadPermissionConfig, matchPromptLang, normalizePromptLang, promptSets, registerTerminalTools, repairToolCallHistory, repairToolCallHistoryDetailed, resetFfmpegCache, resolveMedia, resolveVcsPlan, runGit, sessionIndexPath };
1429
+ export { type AgentDef, AgentRegistry, type AttachRepoMapOptions, BUILTIN_AGENTS, CODE_INSTRUCTIONS, COWORK_INSTRUCTIONS, CompactionDoneInfo, CompactionStartInfo, CompositeHook, type ConfinementResult, ConsoleHook, type ConsoleHookOptions, type CreateEngineOpts, DEFAULT_ALLOWED_COMMANDS, DISCUSS_MODE_CONTEXT, type DownloadResult, EngineErrorInfo, EngineHook, EventType, type GitResult, MCP_CALL_TOOL, MCP_GUIDANCE, MCP_SEARCH_TOOL, MarkdownRenderer, McpBuiltinContext, type McpConnectResult, type McpLazyOptions, McpManager, type McpManagerOptions, McpServerConfig, Message, Mode, OCR_GUIDANCE, PLAN_MODE_CONTEXT, PROMPT_LANGS, type PermissionConfig, type PromptLang, type PromptSet, ProviderClient, ReasoningEffort, type RepairResult, type RepoMap, type ResolveVcsOptions, type ResolvedBin, type ResolvedMedia, type RestoreResult, SHADOW_DIR, SandboxMode, SandboxOptions, type Scale, SessionVcs, ShellPolicy, SubagentHook, type TerminalMetaView, type TerminalRecordView, type TerminalSource, TokenUsage, ToolRegistry, type TurnCommit, TurnEngine, type VcsConfigMode, VcsHook, type VcsMode, type VcsPlan, agentRef, assessCommand, attachRepoMap, buildEnvBlock, buildExplorerInstructions, buildInstructions, createDefaultEngine, downloadFfmpeg, ensureRuntime, getPrompts, gitMessage, gitOk, installHints, invalidateRepoMap, isGitRepo, loadPermissionConfig, loadRepoMap, matchPromptLang, normalizePromptLang, promptSets, registerRepoMapTool, registerTerminalTools, renderRepoMap, repairToolCallHistory, repairToolCallHistoryDetailed, resetFfmpegCache, resetRepoMapMemo, resolveMedia, resolveVcsPlan, runGit, sessionIndexPath };