@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,9 @@
1
+ export interface FileSink {
2
+ writeThreadFile(projectRoot: string, threadId: string, relativePath: string, content: string): Promise<string>;
3
+ }
4
+ export declare function assertSafeThreadId(threadId: string): void;
5
+ export declare function threadDir(projectRoot: string, threadId: string): string;
6
+ export declare function isInsideThreadDir(projectRoot: string, threadId: string, target: string): boolean;
7
+ export declare function summaryRelPath(): string;
8
+ export declare const memorySink: () => FileSink;
9
+ export declare function normalizePath(path: string): string;
@@ -0,0 +1,45 @@
1
+ /** threadId 会拼进目录:只允许生成器使用的安全字符,挡住 `../` 穿越。 */
2
+ const THREAD_ID_PATTERN = /^[A-Za-z0-9_-]{1,80}$/;
3
+ export function assertSafeThreadId(threadId) {
4
+ // status 让宿主路由把参数错误翻成 400,而不是 500。
5
+ if (!THREAD_ID_PATTERN.test(threadId))
6
+ throw Object.assign(new Error('denied: invalid threadId'), { status: 400 });
7
+ }
8
+ export function threadDir(projectRoot, threadId) {
9
+ assertSafeThreadId(threadId);
10
+ return normalizePath(`${projectRoot}/.dsh-im/${threadId}`);
11
+ }
12
+ export function isInsideThreadDir(projectRoot, threadId, target) {
13
+ const root = threadDir(projectRoot, threadId).replace(/\\/g, '/');
14
+ const resolved = normalizePath(target).replace(/\\/g, '/');
15
+ return resolved === root || resolved.startsWith(`${root}/`);
16
+ }
17
+ export function summaryRelPath() {
18
+ return '本轮汇总.md';
19
+ }
20
+ export const memorySink = () => {
21
+ const files = new Map();
22
+ return {
23
+ async writeThreadFile(projectRoot, threadId, relativePath, content) {
24
+ const dest = `${threadDir(projectRoot, threadId)}/${relativePath}`;
25
+ if (!isInsideThreadDir(projectRoot, threadId, dest)) {
26
+ throw new Error('write denied: path escapes .dsh-im thread directory');
27
+ }
28
+ files.set(dest, content);
29
+ return dest;
30
+ },
31
+ };
32
+ };
33
+ export function normalizePath(path) {
34
+ const parts = [];
35
+ for (const part of path.replace(/\\/g, '/').split('/')) {
36
+ if (part === '' || part === '.')
37
+ continue;
38
+ if (part === '..')
39
+ parts.pop();
40
+ else
41
+ parts.push(part);
42
+ }
43
+ const prefix = path.startsWith('/') ? '/' : '';
44
+ return prefix + parts.join('/');
45
+ }
@@ -0,0 +1,279 @@
1
+ import type { NativeFrame } from '../core/types.ts';
2
+ import { type ToolGroupDecl } from '../core/tools.ts';
3
+ import { type TurnToolCache } from './tool-cache.ts';
4
+ import type { TurnStepTrace } from '../core/types.ts';
5
+ export interface NativeMessageBlock {
6
+ type: string;
7
+ text?: string;
8
+ }
9
+ export interface NativeMessage {
10
+ role?: string;
11
+ content?: unknown;
12
+ }
13
+ /**
14
+ * 一次 followup 必须给“完整”的 user Message(id + role + content + source)。
15
+ * DSH 的 `agent.followup` 只把传入对象原样塞进 inbox,下一轮再原样写成
16
+ * `user/message`,**不会补 role/id**。缺 role 时 `session.deriveMessages()`
17
+ * 会返回无角色消息,deepseek-account 适配器 serialize 里
18
+ * `previous?.role === wireRole`(两边都是 undefined)成立后读 `previous.content`
19
+ * 抛 TypeError,被适配器包成 `{code:'TRANSPORT'}`,llm-retry 再退避重试 5 次
20
+ * (约 15s),最后表现为 NATIVE_EMPTY。参考 DSH 自带调用方:全部用
21
+ * `createUserMessage(...)` 的完整消息,不是文档示例里的 {content, source} 简写。
22
+ */
23
+ export interface NativeUserMessage {
24
+ id: string;
25
+ role: 'user';
26
+ content: NativeMessageBlock[];
27
+ source: {
28
+ kind: 'user';
29
+ };
30
+ }
31
+ export interface NativeAgent {
32
+ followup(message: NativeUserMessage): void;
33
+ whenIdle(): Promise<void>;
34
+ cancel?(cause?: unknown): void;
35
+ session: {
36
+ id: string;
37
+ deriveMessages(): NativeMessage[];
38
+ };
39
+ status?: string;
40
+ }
41
+ export interface NativeAgentHandle {
42
+ agent: NativeAgent;
43
+ dispose(): Promise<void>;
44
+ }
45
+ export interface NativeAgentCreateOptions {
46
+ sessionId: string;
47
+ meta?: {
48
+ cwd?: string;
49
+ };
50
+ agentOptions?: {
51
+ provider?: string;
52
+ model?: string;
53
+ maxTokens?: number;
54
+ reasoningEffort?: string;
55
+ };
56
+ setup?: (agentCtx: unknown, agent: NativeAgent) => unknown;
57
+ }
58
+ export interface NativeAgentsApi {
59
+ create(options: NativeAgentCreateOptions): Promise<NativeAgentHandle>;
60
+ /** 进程重启后会话 id 还在时,加载持久化会话而不是再 create。 */
61
+ resume?(options: NativeAgentCreateOptions): Promise<NativeAgentHandle>;
62
+ }
63
+ export interface NativeWorkspaceApi {
64
+ archiveSession?: (sessionId: string, options?: {
65
+ stopActivity?: boolean;
66
+ }) => Promise<unknown>;
67
+ }
68
+ export interface NativeBridgeInput {
69
+ /** 会话归属:同一 key 复用同一个原生 session。 */
70
+ sessionKey: string;
71
+ provider: string;
72
+ model: string;
73
+ cwd?: string;
74
+ /** 静态前缀(现有 buildSystem 输出)。有 systemPrompt 时只在 setup 注册一次。 */
75
+ system?: string;
76
+ /** 回退用;有 frame 且 systemPrompt 可用时不再整段投喂。 */
77
+ prompt: string;
78
+ /** 增量帧。没有时保持旧行为:整段投喂 system + prompt。 */
79
+ frame?: NativeFrame;
80
+ /** 角色勾选的能力集,映射成宿主只读工具白名单。 */
81
+ toolAllow?: string[];
82
+ /** 当前角色名。自己的发言不投喂,避免和会话历史里的 assistant 重复。 */
83
+ personaName?: string;
84
+ /** 诊断:off 不跑 setup;sections 只注册段;restrict 只收窄;bcdp/mcp 只留一个工具。 */
85
+ setupMode?: 'all' | 'off' | 'sections' | 'restrict' | 'bcdp' | 'mcp';
86
+ /** 诊断:替换 im- 前缀的 sessionId。 */
87
+ sessionIdOverride?: string;
88
+ maxTokens?: number;
89
+ reasoningEffort?: string;
90
+ /** 这条会话对应的群。工具按这个群的目录护栏执行。 */
91
+ threadId?: string;
92
+ /** 本轮群级预算(热区 windowChars):记忆工具的多跳展开按它的 15% 限总字数。 */
93
+ memoryBudgetChars?: number;
94
+ /** 单轮工具调用上限(默认 12,`DSH_IM_MAX_TOOL_CALLS` 可调)。 */
95
+ maxToolCalls?: number;
96
+ }
97
+ /** 原生一轮的工具调用统计(回执/metrics 用)。 */
98
+ export interface NativeToolStats {
99
+ toolCalls: number;
100
+ toolExecuted: number;
101
+ toolBudgetExhausted: boolean;
102
+ toolCallLimit: number;
103
+ }
104
+ export interface NativeChecks {
105
+ promptSection: boolean;
106
+ toolRestricted: boolean;
107
+ /** agent 作用域的 pre-execute 硬白名单已装上(P0 的越权边界)。 */
108
+ guard: boolean;
109
+ allow: string[];
110
+ }
111
+ /** 宿主 tools 服务的探测面:只判断能力是否可覆盖,不在这里调用业务方法。 */
112
+ export interface HostToolsProbe {
113
+ register?: unknown;
114
+ guard?: unknown;
115
+ restrict?: unknown;
116
+ schemas?: unknown;
117
+ }
118
+ export interface NativeBridge {
119
+ status(): {
120
+ enabled: boolean;
121
+ reason?: string;
122
+ checks?: NativeChecks;
123
+ /** read/search/write/check 由插件工具覆盖;宿主来源组数量由 toolGroups 反映。 */
124
+ capabilities?: {
125
+ read: boolean;
126
+ search: boolean;
127
+ write: boolean;
128
+ check: boolean;
129
+ };
130
+ hostToolGroups?: number;
131
+ };
132
+ complete(input: NativeBridgeInput): Promise<{
133
+ text: string;
134
+ trace?: TurnStepTrace[];
135
+ toolStats?: NativeToolStats;
136
+ }>;
137
+ debug?(): NativeDebugInfo;
138
+ }
139
+ export interface NativeAgentSessionsOptions {
140
+ agents: () => NativeAgentsApi | undefined;
141
+ workspace?: () => NativeWorkspaceApi | undefined;
142
+ logger?: {
143
+ warn?: (message: string) => void;
144
+ };
145
+ /** 空闲多久后释放原生会话。不传则读 DSH_IM_NATIVE_TTL_MS,再默认 30 分钟。 */
146
+ ttlMs?: number;
147
+ now?: () => number;
148
+ /** 提供之后,setup 会把插件工具 register 进 agent,不再把勾选当成能力缺口。 */
149
+ executeTool?: (call: {
150
+ name: string;
151
+ args: Record<string, unknown>;
152
+ projectRoot: string;
153
+ threadId: string;
154
+ /** 谁在调用(记忆可见性用;原生会话 setup 时已知)。 */
155
+ personaId?: string;
156
+ /** 本轮群级预算(热区 windowChars):记忆工具多跳展开按它的 15% 限总字数。 */
157
+ budgetChars?: number;
158
+ cache: TurnToolCache;
159
+ }) => Promise<string>;
160
+ /** 宿主 tools 服务探测:缺 register/guard 时原生模式整体不可用(诚实回退,不半残运行)。 */
161
+ hostTools?: () => HostToolsProbe | undefined;
162
+ /** 工具来源组声明(配置覆盖/追加出厂声明)。 */
163
+ toolGroups?: ToolGroupDecl[];
164
+ }
165
+ export interface NativeDebugSession {
166
+ sessionKey: string;
167
+ sessionId: string;
168
+ lastError?: string;
169
+ roles: string[];
170
+ lastText: string;
171
+ setupCalls: number;
172
+ lastDeliveredSeq: number;
173
+ feedChars: number[];
174
+ allow: string[];
175
+ schemas: string[];
176
+ /** 当前授权下真正生效的宿主工具(来源组/单工具)。 */
177
+ hostGranted?: string[];
178
+ /** 发现到的来源组 id(浏览器/MCP server/其他)。 */
179
+ toolGroups?: string[];
180
+ toolAllow?: string[];
181
+ }
182
+ export interface NativeDebugInfo {
183
+ sessions: NativeDebugSession[];
184
+ }
185
+ /** 读 DSH_IM_NATIVE_TTL_MS;非法或未设置时用 30 分钟。 */
186
+ export declare function nativeTtlMs(raw?: string | undefined): number;
187
+ /**
188
+ * 角色能力集不再映射成宿主工具名。
189
+ * 读/搜索/写/跑检查要等插件工具注册进 agent 之后才走原生;在那之前客户端直接回退。
190
+ * 空数组表示不调用 restrict。越权拦截靠 guard。
191
+ * DSH_IM_NATIVE_TOOL_ALLOW 非空时整表覆盖,只给排查用;guard 仍会拒绝名单以外的工具。
192
+ */
193
+ export declare function resolveNativeToolAllow(toolAllow?: string[]): string[];
194
+ export declare function nativeToolAllowDenial(name: string, toolAllow?: string[]): string | undefined;
195
+ /**
196
+ * 角色能力集里,宿主上真实存在的同名工具。
197
+ * 对不上就返回空,不偷偷换成浏览器或 MCP 工具。
198
+ * 空数组表示不调用 restrict。越权拦截靠 guard,不靠这份名单。
199
+ */
200
+ export declare function pickNativeAllow(wanted: string[], schemas: string[]): string[];
201
+ export declare function nativeSessionId(sessionKey: string): string;
202
+ export declare function dumpNativeDiag(line: string): void;
203
+ export declare function describeFailure(error: unknown, depth?: number): unknown;
204
+ /** 取会话里最后一条 assistant 文本(原生会话的回复来源)。 */
205
+ export declare function lastAssistantText(messages: readonly NativeMessage[]): string;
206
+ /** 这一轮里宿主自己执行的工具(浏览器、画图)。插件工具已经在 execute 里记过,这里跳过。 */
207
+ export declare function hostToolSteps(messages: readonly NativeMessage[]): TurnStepTrace[];
208
+ export declare class NativeAgentSessions {
209
+ private readonly options;
210
+ private readonly sessions;
211
+ private readonly ttlMs;
212
+ private readonly now;
213
+ private lastChecks;
214
+ constructor(options: NativeAgentSessionsOptions);
215
+ status(): {
216
+ enabled: boolean;
217
+ reason?: string;
218
+ checks?: NativeChecks;
219
+ capabilities?: {
220
+ read: boolean;
221
+ search: boolean;
222
+ write: boolean;
223
+ check: boolean;
224
+ };
225
+ hostToolGroups?: number;
226
+ };
227
+ private declaredGroups;
228
+ /** 宿主全局工具名(host-plane)。来源组发现、会话重建判定共用。 */
229
+ private hostToolNames;
230
+ private discoverHostGroups;
231
+ /** 当前授权下应当生效的宿主工具 + 签名(签名变化 → 重建会话)。 */
232
+ private desiredHostGrant;
233
+ /** 事实投影变了就换掉 dsh-im:facts-l2。没变不碰,避免每轮重注册。 */
234
+ private refreshFacts;
235
+ /** 释放空闲超时的原生会话(惰性清扫,避免定时器常驻)。 */
236
+ sweep(): void;
237
+ complete(input: NativeBridgeInput): Promise<{
238
+ text: string;
239
+ trace?: TurnStepTrace[];
240
+ toolStats?: NativeToolStats;
241
+ }>;
242
+ /** 诊断:当前原生会话的派生消息角色与最近文本。 */
243
+ debug(): NativeDebugInfo;
244
+ /** 按当前授权注册插件工具,并卸掉这次没勾的。宿主工具按来源组授权收窄可见性,guard 再兜一层。 */
245
+ private syncPersonaTools;
246
+ /**
247
+ * 按来源组授权收窄宿主工具可见性:
248
+ * - 被授予的组/单工具保留;
249
+ * - 其余全部 restrict deny(restrict 只挡继承工具,own-scope 注入的工具由 guard 兜);
250
+ * - 同时记录 hostGranted 签名,授权变化时由 complete() 重建会话。
251
+ */
252
+ private hideHostTools;
253
+ /** 有 frame 且系统提示词已注册时只喂增量;否则整段投喂(旧调用 / 宿主没有 systemPrompt)。 */
254
+ private feedText;
255
+ private createEntry;
256
+ /** 插件卸载/重载时释放全部原生会话。释放时才归档,避免活会话被归档门禁挡住。 */
257
+ disposeAll(): Promise<void>;
258
+ private hideFromSidebar;
259
+ /** 测试/调试:当前活着的原生会话。 */
260
+ liveSessionIds(): string[];
261
+ }
262
+ /** host 侧组装:从 ctx 里拿 agents/workspaceRegistry;未启用或宿主缺 agents 时返回 undefined。 */
263
+ export declare function createNativeRuntime(options: {
264
+ agents: () => NativeAgentsApi | undefined;
265
+ workspace?: () => NativeWorkspaceApi | undefined;
266
+ logger?: {
267
+ warn?: (message: string) => void;
268
+ };
269
+ enabled: boolean;
270
+ ttlMs?: number;
271
+ now?: () => number;
272
+ /** 宿主 tools 服务探测(能力契约保证不了就整体回退)。 */
273
+ hostTools?: () => HostToolsProbe | undefined;
274
+ /** 工具来源组声明(配置追加/覆盖出厂声明)。 */
275
+ toolGroups?: ToolGroupDecl[];
276
+ }): {
277
+ bridge?: NativeBridge;
278
+ disposeAll: () => Promise<void>;
279
+ };