@dabuliu_wsy/dsh-im 0.0.0-stage → 0.2.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.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +197 -3
  3. package/cordis.patch.yml +4 -0
  4. package/dist/core/agent-state.d.ts +38 -0
  5. package/dist/core/agent-state.js +59 -0
  6. package/dist/core/arbiter-bridge.d.ts +25 -0
  7. package/dist/core/arbiter-bridge.js +20 -0
  8. package/dist/core/arbiter.d.ts +29 -0
  9. package/dist/core/arbiter.js +59 -0
  10. package/dist/core/avatar-brands.d.ts +23 -0
  11. package/dist/core/avatar-brands.js +75 -0
  12. package/dist/core/avatar-presets.d.ts +21 -0
  13. package/dist/core/avatar-presets.js +38 -0
  14. package/dist/core/avatar.d.ts +1 -0
  15. package/dist/core/avatar.js +18 -0
  16. package/dist/core/blocked.d.ts +12 -0
  17. package/dist/core/blocked.js +23 -0
  18. package/dist/core/budget-plan.d.ts +41 -0
  19. package/dist/core/budget-plan.js +69 -0
  20. package/dist/core/calibration-samples.d.ts +77 -0
  21. package/dist/core/calibration-samples.js +168 -0
  22. package/dist/core/calibrator.d.ts +57 -0
  23. package/dist/core/calibrator.js +125 -0
  24. package/dist/core/chapter-tree.d.ts +128 -0
  25. package/dist/core/chapter-tree.js +400 -0
  26. package/dist/core/consensus.d.ts +135 -0
  27. package/dist/core/consensus.js +394 -0
  28. package/dist/core/context-budget.d.ts +153 -0
  29. package/dist/core/context-budget.js +183 -0
  30. package/dist/core/curator.d.ts +28 -0
  31. package/dist/core/curator.js +190 -0
  32. package/dist/core/dual-window.d.ts +86 -0
  33. package/dist/core/dual-window.js +175 -0
  34. package/dist/core/events.d.ts +110 -0
  35. package/dist/core/events.js +117 -0
  36. package/dist/core/facts.d.ts +114 -0
  37. package/dist/core/facts.js +400 -0
  38. package/dist/core/fence.d.ts +34 -0
  39. package/dist/core/fence.js +129 -0
  40. package/dist/core/floor.d.ts +18 -0
  41. package/dist/core/floor.js +80 -0
  42. package/dist/core/ids.d.ts +11 -0
  43. package/dist/core/ids.js +29 -0
  44. package/dist/core/im.d.ts +167 -0
  45. package/dist/core/im.js +1307 -0
  46. package/dist/core/index.d.ts +33 -0
  47. package/dist/core/index.js +33 -0
  48. package/dist/core/log.d.ts +21 -0
  49. package/dist/core/log.js +63 -0
  50. package/dist/core/memory-claims.d.ts +67 -0
  51. package/dist/core/memory-claims.js +164 -0
  52. package/dist/core/memory-context.d.ts +121 -0
  53. package/dist/core/memory-context.js +303 -0
  54. package/dist/core/memory-visibility.d.ts +58 -0
  55. package/dist/core/memory-visibility.js +36 -0
  56. package/dist/core/memory.d.ts +12 -0
  57. package/dist/core/memory.js +73 -0
  58. package/dist/core/metrics.d.ts +49 -0
  59. package/dist/core/metrics.js +89 -0
  60. package/dist/core/multi-hop.d.ts +116 -0
  61. package/dist/core/multi-hop.js +278 -0
  62. package/dist/core/persist.d.ts +21 -0
  63. package/dist/core/persist.js +94 -0
  64. package/dist/core/proactive.d.ts +54 -0
  65. package/dist/core/proactive.js +108 -0
  66. package/dist/core/prompt.d.ts +82 -0
  67. package/dist/core/prompt.js +164 -0
  68. package/dist/core/receipt.d.ts +107 -0
  69. package/dist/core/receipt.js +86 -0
  70. package/dist/core/retrieval.d.ts +69 -0
  71. package/dist/core/retrieval.js +260 -0
  72. package/dist/core/role-state.d.ts +16 -0
  73. package/dist/core/role-state.js +65 -0
  74. package/dist/core/runtime.d.ts +137 -0
  75. package/dist/core/runtime.js +574 -0
  76. package/dist/core/summary.d.ts +19 -0
  77. package/dist/core/summary.js +96 -0
  78. package/dist/core/text.d.ts +10 -0
  79. package/dist/core/text.js +45 -0
  80. package/dist/core/time.d.ts +22 -0
  81. package/dist/core/time.js +84 -0
  82. package/dist/core/tool-schema.d.ts +12 -0
  83. package/dist/core/tool-schema.js +92 -0
  84. package/dist/core/tools.d.ts +137 -0
  85. package/dist/core/tools.js +348 -0
  86. package/dist/core/types.d.ts +400 -0
  87. package/dist/core/types.js +1 -0
  88. package/dist/core/window.d.ts +6 -0
  89. package/dist/core/window.js +7 -0
  90. package/dist/core/workspace-node.d.ts +28 -0
  91. package/dist/core/workspace-node.js +232 -0
  92. package/dist/core/workspace.d.ts +9 -0
  93. package/dist/core/workspace.js +45 -0
  94. package/dist/host/agent-session.d.ts +279 -0
  95. package/dist/host/agent-session.js +935 -0
  96. package/dist/host/attachment-gc.d.ts +37 -0
  97. package/dist/host/attachment-gc.js +97 -0
  98. package/dist/host/calibration-samples-store.d.ts +5 -0
  99. package/dist/host/calibration-samples-store.js +67 -0
  100. package/dist/host/capabilities.d.ts +45 -0
  101. package/dist/host/capabilities.js +67 -0
  102. package/dist/host/derived-store.d.ts +36 -0
  103. package/dist/host/derived-store.js +199 -0
  104. package/dist/host/dsh-llm.d.ts +143 -0
  105. package/dist/host/dsh-llm.js +395 -0
  106. package/dist/host/event-store.d.ts +47 -0
  107. package/dist/host/event-store.js +154 -0
  108. package/dist/host/fs-atomic.d.ts +1 -0
  109. package/dist/host/fs-atomic.js +26 -0
  110. package/dist/host/im-api.d.ts +352 -0
  111. package/dist/host/im-api.js +1137 -0
  112. package/dist/host/index.d.ts +103 -0
  113. package/dist/host/index.js +453 -0
  114. package/dist/host/memory-index.d.ts +65 -0
  115. package/dist/host/memory-index.js +181 -0
  116. package/dist/host/metrics-store.d.ts +7 -0
  117. package/dist/host/metrics-store.js +104 -0
  118. package/dist/host/models.d.ts +39 -0
  119. package/dist/host/models.js +67 -0
  120. package/dist/host/persona-tools.d.ts +57 -0
  121. package/dist/host/persona-tools.js +226 -0
  122. package/dist/host/preflight.d.ts +31 -0
  123. package/dist/host/preflight.js +29 -0
  124. package/dist/host/receipt-store.d.ts +26 -0
  125. package/dist/host/receipt-store.js +68 -0
  126. package/dist/host/run-check.d.ts +51 -0
  127. package/dist/host/run-check.js +190 -0
  128. package/dist/host/state-roots.d.ts +50 -0
  129. package/dist/host/state-roots.js +163 -0
  130. package/dist/host/tool-cache.d.ts +25 -0
  131. package/dist/host/tool-cache.js +90 -0
  132. package/dist/host/tool-call-budget.d.ts +29 -0
  133. package/dist/host/tool-call-budget.js +38 -0
  134. package/dsh-plugin.json +25 -0
  135. package/lib/client.inner.cjs +3346 -0
  136. package/lib/client.js +3354 -0
  137. package/package.json +86 -6
@@ -0,0 +1,29 @@
1
+ export function createId(prefix = 'id') {
2
+ return `${prefix}_${crypto.randomUUID()}`;
3
+ }
4
+ /** 光标前如果正在输入 @,返回 @ 的位置和已输入的筛选字。 */
5
+ export function activeMention(text, caret) {
6
+ const upto = text.slice(0, Math.max(0, caret));
7
+ const match = upto.match(/(?:^|\s)@([^\s@]*)$/);
8
+ if (!match || match.index === undefined)
9
+ return null;
10
+ const start = match[0].startsWith('@') ? match.index : match.index + 1;
11
+ return { start, query: match[1] };
12
+ }
13
+ export function mentionIds(text, personas) {
14
+ const ids = [];
15
+ for (const p of personas) {
16
+ if (text.includes(`@${p.name}`) || text.includes(`@${p.id}`))
17
+ ids.push(p.id);
18
+ }
19
+ if (/@全体|@all/i.test(text)) {
20
+ for (const p of personas) {
21
+ if (!ids.includes(p.id))
22
+ ids.push(p.id);
23
+ }
24
+ }
25
+ return ids;
26
+ }
27
+ export function wantsReview(text) {
28
+ return /开始评审/.test(text);
29
+ }
@@ -0,0 +1,167 @@
1
+ import { type AgentRuntimeState } from './agent-state.ts';
2
+ import { type ProactiveLimits } from './proactive.ts';
3
+ import { type GroupBudgetPolicy } from './context-budget.ts';
4
+ import type { Activity, ChairSelector, Fact, ImState, MemoryEntry, MessageAttachment, Persona, RetrievalMode, Speaker, StalePolicy, Thread } from './types.ts';
5
+ import { type FileSink } from './workspace.ts';
6
+ export declare class ImService {
7
+ private readonly speaker;
8
+ private readonly chair;
9
+ private readonly files;
10
+ readonly state: ImState;
11
+ constructor(speaker?: Speaker, chair?: ChairSelector, initial?: Partial<ImState>, files?: FileSink);
12
+ /** 角色运行时状态:只在内存、带 TTL,不落盘(崩溃留下的 thinking 自动过期)。 */
13
+ private readonly agentStates;
14
+ /** 正在生成共识的群:防止 UI 自动触发和轮末自动触发重复调用 curator。 */
15
+ private readonly consensusInFlight;
16
+ /** 正在跑事实抽取的群:界面据此显示“抽取中”,而不是误以为任务板空白。 */
17
+ private readonly extractionInFlight;
18
+ /** 异步事实抽取完成时通知界面(重新渲染 + 触发落盘)。 */
19
+ onAsyncChange?: () => void;
20
+ /** 生成期间出现新消息时的处理策略:重算一次(默认)/ 标注旧版本 / 丢弃。 */
21
+ stalePolicy: StalePolicy;
22
+ /** 按需检索档位:off / shadow(默认,只记录不进 prompt)/ on。 */
23
+ retrievalMode: RetrievalMode;
24
+ /** 引用句柄协议档位(Phase 1):off / shadow(默认,只写回执)/ on(注入)。 */
25
+ memoryContextMode: 'off' | 'shadow' | 'on';
26
+ /**
27
+ * 群级预算输入(A-1/A-2:客户端注入,prompt 组装与 L1 摘要生成用同一份)。
28
+ * 不注入时摘要预算回退到旧的静态口径(`DEFAULT_SUMMARY_TOTAL_CHARS`,digest 份额 = 旧 8000)。
29
+ */
30
+ groupBudgetPolicy?: GroupBudgetPolicy;
31
+ /** 章节目录占 summary 预算的比例(默认 40%)。 */
32
+ summaryChapterShare: number;
33
+ /**
34
+ * 共识状态开关(默认开):每轮事实抽取之后自动跑 curator,生成 thread.consensusState。
35
+ * 只有 `localStorage dsh-im:consensus='off'` 才关闭(回滚开关,不需要用户默认点)。
36
+ */
37
+ consensusEnabled: boolean;
38
+ /** 主动私聊的全局限制(档位数值在 proactive.ts 的预设里;这里可覆盖)。 */
39
+ proactiveLimits: ProactiveLimits;
40
+ snapshot(): ImState;
41
+ /** 当前群的有效事实(用户在任务板归档的已排除,异步抽取的也合并进来)。 */
42
+ factsFor(threadId: string): Fact[];
43
+ /** 任务板:把一条事实归档(写 retract,下一次保存落成事件)。 */
44
+ completeFact(threadId: string, factId: string): void;
45
+ /** 客户端可见的事实投影:host 投影 + 覆盖点之后的新消息派生事实 + 本地异步抽取,再去掉本地归档的。 */
46
+ private localFacts;
47
+ private queueFactOp;
48
+ private applyExtractedFacts;
49
+ /**
50
+ * 每轮结束后异步抽一次事实(有 LLM 通道才跑):不阻塞回复,失败静默。
51
+ * 短招呼(<6 字且非评审)不抽,避免每句都多一次调用。
52
+ */
53
+ private scheduleFactExtraction;
54
+ /** 事实抽取是否在跑(任务板显示“抽取中”)。 */
55
+ extractionPending(threadId: string): boolean;
56
+ /** 共识整理是否在跑(任务板显示“整理中”)。 */
57
+ consensusPending(threadId: string): boolean;
58
+ /**
59
+ * 每轮事实抽取之后跑一次 curator → consensus builder,产出 thread.consensusState。
60
+ * `force=true` 供任务板“生成本轮共识”按钮使用(开关关闭时不自动跑,显式点击才跑)。
61
+ *
62
+ * 能力/环境自述由本层先按确定性规则摘出来,直接进 D 栏归档,不交给模型判断:
63
+ * 这样即使 curator 犯错,也不会把“我没有 shell”写进 A/B/C 变成长期约束。
64
+ */
65
+ private scheduleConsensus;
66
+ /**
67
+ * 界面自动触发:当前群有事实、但没有共识状态(或共识落后于最新消息)时生成一次。
68
+ * 不需要用户点任何按钮;同一群并发去重,失败只留下 consensusError 供界面提示。
69
+ */
70
+ ensureConsensus(threadId: string): void;
71
+ /** 任务板按钮:手动生成/刷新本轮共识(显式动作,不受自动开关限制)。 */
72
+ refreshConsensus(threadId: string): void;
73
+ /** 只读一份运行时状态(界面/调试用;不参与落盘)。 */
74
+ runtimeStates(): AgentRuntimeState[];
75
+ runtimeState(personaId: string): AgentRuntimeState | undefined;
76
+ private stateOf;
77
+ private setAgentState;
78
+ private setAgentIdle;
79
+ /** 发言完成:本人回 idle 并记冷却;正在等他的角色解除等待。 */
80
+ private settleSpeak;
81
+ private recordFloorDecision;
82
+ /** 每轮结束刷新分层摘要投影:L1 旧消息压缩 + L2 事实/决策/待办,并推进窗口边界。 */
83
+ private refreshDigest;
84
+ private nameOf;
85
+ /** 当前最后一条消息 seq(没有 seq 的老消息按 0 算)。 */
86
+ private lastMessageSeq;
87
+ private recordMetric;
88
+ /** 每轮结束记一次地板指标(speak/wait/pass 计数)。 */
89
+ private recordFloorMetric;
90
+ createPersona(input: Omit<Persona, 'id' | 'origin' | 'model'> & {
91
+ id?: string;
92
+ origin?: Persona['origin'];
93
+ model: string;
94
+ }): Persona;
95
+ updatePersona(id: string, patch: Partial<Persona>): Persona;
96
+ listPersonas(): Persona[];
97
+ /** 用户明确说“私聊/小窗”且只点了一个人时,返回该角色。 */
98
+ private privateChatTarget;
99
+ /** 改群的“关联项目”目录(角色读/搜/run_check 的根目录)。 */
100
+ setThreadProjectRoot(threadId: string, projectRoot: string): Thread;
101
+ openDm(personaId: string, projectRoot: string): Thread;
102
+ createTriad(personaIds: [string, string], projectRoot: string): Thread;
103
+ /** 建群:群名由用户填,留空退回「新群」(UI 的占位也是这个,保证标题永不为空)。 */
104
+ createGroup(title: string, memberIds: string[], projectRoot: string): Thread;
105
+ /**
106
+ * 改会话名:群(名字本来就是用户起的)与三人小窗(默认标题由角色名派生,改名后置
107
+ * `titleCustom` 锁定,之后角色改名不再覆盖)。DM 标题永远从角色名派生,不允许手改。
108
+ */
109
+ setThreadTitle(threadId: string, title: string): Thread;
110
+ setActivity(threadId: string, personaId: string, activity: Activity): void;
111
+ /** 客户端 UI:自动打开私聊时切到那个小窗。 */
112
+ onDmOpened?: (threadId: string) => void;
113
+ sendUser(threadId: string, text: string, opts?: {
114
+ startReview?: boolean;
115
+ excludePersonaIds?: string[];
116
+ attachments?: MessageAttachment[];
117
+ }): Promise<Thread>;
118
+ confirmRecall(allow: boolean): Promise<Thread | null>;
119
+ pauseRound(threadId: string): void;
120
+ requestSummary(threadId: string): Promise<string>;
121
+ private createThread;
122
+ /**
123
+ * 发言权仲裁地板:
124
+ * 资格门(谁能进候选,含点名独占)→ 仲裁器决定 speak/wait/pass → 执行。
125
+ * 决策写进 `thread.floorDecisions`(随 state.json 落盘,事件类型下一步补)。
126
+ */
127
+ private advanceFloor;
128
+ /**
129
+ * 发言焦点仍由椅子按职责决定(人物选择已交给仲裁器,这里只算 focus)。
130
+ */
131
+ private focusFor;
132
+ /**
133
+ * 解除中断:skip = 本轮移除该角色继续;retry = 已修好,重试本轮。
134
+ */
135
+ /** 开始前体检不通过:把指定成员从本轮候选移除(只影响本轮,不踢出群)。 */
136
+ excludeFromRound(threadId: string, personaIds: string[]): void;
137
+ resolveBlocked(threadId: string, action: 'skip' | 'retry'): Promise<void>;
138
+ private noteRecall;
139
+ private blockForeignLeak;
140
+ private otherThreadBodies;
141
+ private allowedSpeech;
142
+ private visibleFor;
143
+ private memoryTexts;
144
+ /** 记录一轮执行轨迹(最多保留最近 40 轮,steps 截断)。 */
145
+ private recordTrace;
146
+ private maybeRemember;
147
+ private maybeProactive;
148
+ /**
149
+ * 客户端定时器调用:挑一个可触发的角色,生成一条主动私聊(一次 tick 最多一条)。
150
+ * 判定是纯函数(proactive.ts),这里只负责生成/投递/计数;失败静默,不影响主流程。
151
+ */
152
+ tickProactive(now?: number): Promise<{
153
+ fired: boolean;
154
+ personaId?: string;
155
+ reason: string;
156
+ }>;
157
+ private defaultProjectRoot;
158
+ private abortSpeaking;
159
+ private resetRound;
160
+ private buildSummary;
161
+ private pushMessage;
162
+ private personasIn;
163
+ private memberOf;
164
+ private requirePersona;
165
+ requireThread(id: string): Thread;
166
+ addMemory(entry: MemoryEntry): void;
167
+ }