@twinsearth/dsh-master 0.0.0-stage → 3.2.8

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 (95) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +89 -2
  3. package/client.js +263 -0
  4. package/cordis.patch.yml +71 -0
  5. package/dist-host/contract/client.js +82 -0
  6. package/dist-host/contract/constants.js +70 -0
  7. package/dist-host/contract/envelope.js +84 -0
  8. package/dist-host/contract/index.js +17 -0
  9. package/dist-host/contract/negotiate.js +87 -0
  10. package/dist-host/contract/server.js +71 -0
  11. package/dist-host/contract/transports.js +360 -0
  12. package/dist-host/index.js +260 -0
  13. package/dist-host/kernel.js +186 -0
  14. package/dist-host/plugins/cost-optimizer.js +147 -0
  15. package/dist-host/plugins/dual-manager.js +251 -0
  16. package/dist-host/plugins/superagent-router.js +499 -0
  17. package/dist-host/plugins/superagent-worker.js +73 -0
  18. package/dist-host/superagent/bridge-tokens.js +236 -0
  19. package/dist-host/superagent/dispatch.js +198 -0
  20. package/dist-host/superagent/feishu.js +115 -0
  21. package/dist-host/superagent/github.js +119 -0
  22. package/dist-host/superagent/pr-delivery.js +351 -0
  23. package/dist-host/superagent/router.js +219 -0
  24. package/dist-host/superagent/stream-adapter.js +238 -0
  25. package/dist-host/superagent/stream-tokens.js +141 -0
  26. package/dist-host/superagent/ticket-store.js +108 -0
  27. package/dist-host/superagent/trust.js +145 -0
  28. package/dist-host/superagent/types.js +144 -0
  29. package/dist-host/superagent/worker.js +95 -0
  30. package/dist-host/superagent/writeback.js +162 -0
  31. package/dist-worker/agent-loop.js +493 -0
  32. package/dist-worker/errors.js +41 -0
  33. package/dist-worker/github-pr.js +44 -0
  34. package/dist-worker/index.js +323 -0
  35. package/dist-worker/llm-http.js +351 -0
  36. package/dist-worker/main.js +45 -0
  37. package/dist-worker/prompt.js +35 -0
  38. package/dist-worker/sink-http.js +135 -0
  39. package/dist-worker/tools.js +542 -0
  40. package/dist-worker/types.js +14 -0
  41. package/docs/ARCHITECTURE.md +114 -0
  42. package/docs/CHANGELOG.md +377 -0
  43. package/docs/INTEROP.md +60 -0
  44. package/docs/REVIEW-v3.2.7.md +121 -0
  45. package/docs/ROADMAP.md +106 -0
  46. package/docs/SECURITY.md +153 -0
  47. package/docs/SMOKE-3.2.5.md +77 -0
  48. package/docs/SMOKE-3.2.7.md +73 -0
  49. package/docs/SUPERAGENT.md +141 -0
  50. package/docs/WORKER-AGENT-LOOP.md +139 -0
  51. package/icon.svg +9 -0
  52. package/locale/en.json +6 -0
  53. package/locale/zh.json +6 -0
  54. package/package.json +79 -4
  55. package/src/contract/client.js +86 -0
  56. package/src/contract/constants.js +79 -0
  57. package/src/contract/envelope.js +83 -0
  58. package/src/contract/index.js +19 -0
  59. package/src/contract/negotiate.js +106 -0
  60. package/src/contract/server.js +77 -0
  61. package/src/contract/transports.js +319 -0
  62. package/src/index.ts +265 -0
  63. package/src/kernel.ts +190 -0
  64. package/src/plugins/cost-optimizer.ts +155 -0
  65. package/src/plugins/dual-manager.ts +252 -0
  66. package/src/plugins/superagent-router.ts +465 -0
  67. package/src/plugins/superagent-worker.ts +85 -0
  68. package/src/superagent/bridge-tokens.ts +236 -0
  69. package/src/superagent/dispatch.ts +197 -0
  70. package/src/superagent/feishu.ts +104 -0
  71. package/src/superagent/github.ts +111 -0
  72. package/src/superagent/pr-delivery.ts +308 -0
  73. package/src/superagent/router.ts +201 -0
  74. package/src/superagent/stream-adapter.ts +184 -0
  75. package/src/superagent/stream-tokens.ts +142 -0
  76. package/src/superagent/ticket-store.ts +104 -0
  77. package/src/superagent/trust.ts +139 -0
  78. package/src/superagent/types.ts +135 -0
  79. package/src/superagent/worker.ts +100 -0
  80. package/src/superagent/writeback.ts +154 -0
  81. package/tsconfig.host.build.json +12 -0
  82. package/tsconfig.host.json +21 -0
  83. package/tsconfig.worker.build.json +13 -0
  84. package/tsconfig.worker.json +17 -0
  85. package/types/superagent.d.ts +99 -0
  86. package/worker/agent-loop.ts +524 -0
  87. package/worker/errors.ts +47 -0
  88. package/worker/github-pr.ts +65 -0
  89. package/worker/index.ts +334 -0
  90. package/worker/llm-http.ts +359 -0
  91. package/worker/main.ts +45 -0
  92. package/worker/prompt.ts +39 -0
  93. package/worker/sink-http.ts +159 -0
  94. package/worker/tools.ts +531 -0
  95. package/worker/types.ts +247 -0
@@ -0,0 +1,247 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (c) 2026 dsh-Master contributors — see LICENSE-CORE.
3
+
4
+ /**
5
+ * dsh-superagent-worker preset 的类型定义(v3.2.2)。
6
+ *
7
+ * 运行在分身 VM 内;Router 通过 `DSH_MASTER_TICKET` 环境变量传入 Ticket。
8
+ * 本文件只用可擦除类型语法(无 enum / namespace / parameter properties),
9
+ * 因此 Node 22.6+ 的类型擦除与 tsc 都能直接消费。
10
+ */
11
+
12
+ export type WorkerPhase =
13
+ | 'initializing'
14
+ | 'thinking'
15
+ | 'executing_tool'
16
+ | 'reporting'
17
+ | 'done'
18
+ | 'failed'
19
+
20
+ export type WorkerStepType =
21
+ | 'thought'
22
+ | 'tool_call'
23
+ | 'tool_result'
24
+ | 'final_answer'
25
+ | 'error'
26
+ | 'phase'
27
+
28
+ export type MandateStrength = 'explicit' | 'inferred'
29
+
30
+ /** 授权范围(不可变 Ticket 的一部分)。 */
31
+ export interface TicketMandate {
32
+ /** 允许修改的文件范围(通常为 workspace 内路径);Worker 工具据此做路径 jail。 */
33
+ scope: string
34
+ /** 是否允许执行 Shell(默认 false,最小权限)。 */
35
+ allowShell: boolean
36
+ policyVersion?: string
37
+ strength?: MandateStrength
38
+ }
39
+
40
+ /** Worker 实际消费的工单负载。 */
41
+ export interface TicketPayload {
42
+ ticketId: string
43
+ source: string
44
+ taskDescription: string
45
+ mandate: TicketMandate
46
+ }
47
+
48
+ /** 兼容 v3.2.1 Router 的 Ticket 形状(id/sourceId/payload)。 */
49
+ export interface RouterTicketLike {
50
+ id?: string
51
+ sourceId?: string
52
+ requester?: string
53
+ payload?: string
54
+ eventType?: string
55
+ mandate?: {
56
+ scope?: string
57
+ allowShell?: boolean
58
+ policyVersion?: string
59
+ strength?: MandateStrength
60
+ }
61
+ }
62
+
63
+ export interface ToolCall {
64
+ id: string
65
+ type?: 'function'
66
+ function: {
67
+ name: string
68
+ /** JSON 字符串(模型返回的原始参数)。 */
69
+ arguments: string
70
+ }
71
+ }
72
+
73
+ export interface ToolSchema {
74
+ name: string
75
+ description: string
76
+ parameters: Record<string, unknown>
77
+ }
78
+
79
+ export interface ChatMessage {
80
+ role: 'system' | 'user' | 'assistant' | 'tool'
81
+ content?: string | null
82
+ tool_calls?: ToolCall[]
83
+ tool_call_id?: string
84
+ name?: string
85
+ }
86
+
87
+ export interface LLMUsage {
88
+ promptTokens?: number
89
+ completionTokens?: number
90
+ totalTokens?: number
91
+ }
92
+
93
+ export interface LLMResponse {
94
+ message: ChatMessage
95
+ usage?: LLMUsage
96
+ finishReason?: string
97
+ }
98
+
99
+ export interface LLMRequest {
100
+ model?: string
101
+ messages: ChatMessage[]
102
+ tools?: ToolSchema[]
103
+ signal?: AbortSignal
104
+ /** OpenAI 兼容的可选 JSON 模式;Worker 默认不强制,避免破坏 tool_calls。 */
105
+ responseFormat?: { type: 'json_object' } | { type: 'text' }
106
+ }
107
+
108
+ /** 流式片段:content 是增量 token 文本,toolName 表示进入某个工具调用。 */
109
+ export interface LLMStreamDelta {
110
+ content?: string
111
+ toolName?: string
112
+ }
113
+
114
+ /** LLM 适配器接口:由 HTTP/宿主服务/测试脚本提供。 */
115
+ export interface LLMClient {
116
+ chat(request: LLMRequest): Promise<LLMResponse>
117
+ /**
118
+ * 真实流式调用(provider 支持 SSE delta 时实现)。
119
+ * 返回聚合后的完整响应;delta 在生成过程中实时回调,宿主可直接转发给 UI。
120
+ * 未实现时 Agent Loop 自动退回 chat() + 分块模拟。
121
+ */
122
+ chatStream?(request: LLMRequest, onDelta: (delta: LLMStreamDelta) => void): Promise<LLMResponse>
123
+ }
124
+
125
+ export interface ToolExecutionContext {
126
+ workspace: string
127
+ signal: AbortSignal
128
+ timeoutMs: number
129
+ mandate: TicketMandate
130
+ }
131
+
132
+ export interface WorkerTool {
133
+ name: string
134
+ description: string
135
+ parameters: Record<string, unknown>
136
+ execute(args: Record<string, unknown>, context: ToolExecutionContext): Promise<string>
137
+ }
138
+
139
+ export interface WorkerStep {
140
+ stepIndex: number
141
+ type: WorkerStepType
142
+ content: string
143
+ toolName?: string
144
+ toolArgs?: Record<string, unknown>
145
+ timestamp: number
146
+ tokenUsage?: number
147
+ }
148
+
149
+ export interface WorkerLimits {
150
+ /** 硬上限:LLM 轮次。超过即 max-steps。 */
151
+ maxSteps: number
152
+ /** 硬上限:累计 token(来自 usage;provider 不返回时按 0 计,另受 context 字符限制)。 */
153
+ maxTokens: number
154
+ /** 硬上限:墙钟时间。 */
155
+ maxWallClockMs: number
156
+ /** 单次 LLM 调用超时。 */
157
+ llmTimeoutMs: number
158
+ /** 单个工具执行超时。 */
159
+ toolTimeoutMs: number
160
+ /** 单轮最多并行/顺序工具调用数。 */
161
+ maxToolCallsPerStep: number
162
+ /** 连续相同工具调用(同名+同参)允许次数;超过判定死循环。 */
163
+ maxIdenticalToolRepeats: number
164
+ /** 工具结果回灌模型的截断长度。 */
165
+ maxToolResultChars: number
166
+ /** 上下文(messages 序列化)最大字符数,防 OOM / 超长请求。 */
167
+ maxContextChars: number
168
+ }
169
+
170
+ export interface WorkerSink {
171
+ /** 人类可读日志(可转发到宿主/日志文件)。 */
172
+ log?(message: string): void
173
+ /** 每一步的结构化审计记录(崩溃后可恢复现场)。 */
174
+ step?(step: WorkerStep): void
175
+ /** 状态机迁移。 */
176
+ phase?(phase: WorkerPhase): void
177
+ /** 流式片段(无真实 streaming 时按块模拟;宿主可转发到 UI)。 */
178
+ stream?(delta: { content?: string; toolName?: string }): void
179
+ }
180
+
181
+ export interface WorkerDeps {
182
+ llm: LLMClient
183
+ tools: WorkerTool[]
184
+ workspace: string
185
+ mandate: TicketMandate
186
+ logger?: {
187
+ info?(message: string): void
188
+ warn?(message: string): void
189
+ error?(message: string): void
190
+ }
191
+ sink?: WorkerSink
192
+ limits?: Partial<WorkerLimits>
193
+ /** 可注入时钟,便于测试;默认 Date.now。 */
194
+ clock?: () => number
195
+ /** 测试用:LLM 请求的 model 覆盖。 */
196
+ model?: string
197
+ /** 需要从日志/步骤中抹掉的具体 secret 值(如 LLM key);正则脱敏之外的兜底。 */
198
+ redactValues?: string[]
199
+ }
200
+
201
+ export interface WorkerFailure {
202
+ code: string
203
+ message: string
204
+ fatal: boolean
205
+ }
206
+
207
+ export interface WorkerResult {
208
+ ok: boolean
209
+ ticketId: string
210
+ phase: WorkerPhase
211
+ finalAnswer: string
212
+ steps: WorkerStep[]
213
+ tokenUsage: number
214
+ elapsedMs: number
215
+ error?: WorkerFailure
216
+ /** 可选的 PR 交付元数据;分支/文件由 Worker 准备,凭据与开 PR 仅在 Router 宿主侧。 */
217
+ pr?: {
218
+ branch: string
219
+ title?: string
220
+ body?: string
221
+ base?: string
222
+ draft?: boolean
223
+ files?: Array<{ path: string; content: string }>
224
+ }
225
+ }
226
+
227
+ export interface WorkerSnapshot {
228
+ ticketId: string
229
+ phase: WorkerPhase
230
+ steps: WorkerStep[]
231
+ tokenUsage: number
232
+ elapsedMs: number
233
+ finalAnswer: string
234
+ }
235
+
236
+ /** 默认限制(可被 config/构造参数覆盖,但不可关闭硬上限)。 */
237
+ export const DEFAULT_LIMITS: WorkerLimits = {
238
+ maxSteps: 15,
239
+ maxTokens: 50_000,
240
+ maxWallClockMs: 15 * 60_000,
241
+ llmTimeoutMs: 120_000,
242
+ toolTimeoutMs: 120_000,
243
+ maxToolCallsPerStep: 4,
244
+ maxIdenticalToolRepeats: 3,
245
+ maxToolResultChars: 8_000,
246
+ maxContextChars: 400_000,
247
+ }