better-dsh-pet 0.1.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.
- package/LICENSE +21 -0
- package/README.en.md +93 -0
- package/README.md +103 -0
- package/assets/alarm.mp3 +0 -0
- package/assets/thumb//344/270/234/345/274/240/350/245/277/346/234/233.webm +0 -0
- package/assets/thumb//344/270/255/347/247/213/350/265/217/346/234/210/345/220/203/346/234/210/351/245/274.webm +0 -0
- package/assets/thumb//344/274/230/351/233/205/345/245/263/344/273/206/350/210/236.webm +0 -0
- package/assets/thumb//345/201/267/345/220/203/351/233/266/351/243/237/350/242/253/346/212/223/344/275/217.webm +0 -0
- package/assets/thumb//345/206/231/344/273/243/347/240/201.webm +0 -0
- package/assets/thumb//345/212/250/347/211/251/347/216/257/347/273/225.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/344/270/223/345/277/203/347/216/251/351/255/224/346/226/271.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/345/260/217/346/206/251/346/262/211/347/234/240.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/345/267/246/350/275/254/345/245/224/350/267/221.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/346/225/262/345/207/273/346/241/214/351/235/242/344/272/222/345/212/250.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/346/274/202/346/265/256/350/270/217/346/255/245.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/350/267/263/350/267/203/346/212/223/347/242/216/345/244/264/351/241/266/347/211/251/345/223/201.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/350/271/262/344/270/213/347/216/251/347/216/251/345/205/267/346/261/275/350/275/246.webm +0 -0
- package/assets/thumb//345/216/237/345/234/260/351/207/215/345/212/233/344/270/213/350/271/262/345/216/213/347/274/251.webm +0 -0
- package/assets/thumb//345/217/257/347/210/261/345/256/205/350/210/236.webm +0 -0
- package/assets/thumb//345/220/203Token.webm +0 -0
- package/assets/thumb//345/220/203/345/206/260/346/267/207/346/267/213/350/236/215/345/214/226.webm +0 -0
- package/assets/thumb//345/220/203/345/215/210/351/244/220.webm +0 -0
- package/assets/thumb//345/220/203/346/227/251/351/244/220.webm +0 -0
- package/assets/thumb//345/220/203/346/231/232/351/244/220.webm +0 -0
- package/assets/thumb//345/220/203/347/231/275/351/245/255.webm +0 -0
- package/assets/thumb//345/220/271/346/260/224/347/220/203.webm +0 -0
- package/assets/thumb//345/223/210/346/254/240/350/277/236/345/244/251.webm +0 -0
- package/assets/thumb//345/240/206/351/233/252/344/272/272.webm +0 -0
- package/assets/thumb//345/244/247/345/217/243/345/220/203/351/233/266/351/243/237.webm +0 -0
- package/assets/thumb//345/245/263/344/273/206/345/261/210/350/206/235/347/244/274/344/273/252.webm +0 -0
- package/assets/thumb//345/260/217/345/271/205/345/272/246/345/216/237/345/234/260 360 /345/272/246/346/227/213/350/275/254/345/261/225/347/244/272.webm +0 -0
- package/assets/thumb//345/260/217/346/217/220/347/220/264/346/274/224/345/245/217.webm +0 -0
- package/assets/thumb//345/276/205/346/234/272/345/221/274/345/220/270/344/274/221/351/227/262.webm +0 -0
- package/assets/thumb//346/202/240/351/227/262/345/223/274/346/255/214.webm +0 -0
- package/assets/thumb//346/211/223/347/236/214/347/235/241/350/242/253/346/203/212/351/206/222.webm +0 -0
- package/assets/thumb//346/221/207/346/211/207/347/272/263/345/207/211.webm +0 -0
- package/assets/thumb//346/224/276/351/243/216/347/255/235.webm +0 -0
- package/assets/thumb//346/225/264/344/275/223/346/215/242/350/243/205/350/257/225/350/211/262.webm +0 -0
- package/assets/thumb//346/267/261/345/272/246/346/200/235/350/200/203/347/242/216/347/242/216/345/277/265.webm +0 -0
- package/assets/thumb//347/202/271/345/207/273/345/233/236/345/272/224 - /345/202/262/345/250/207/347/224/237/346/260/224/357/274/210/344/276/247/350/272/253/345/261/225/347/244/272/357/274/211.webm +0 -0
- package/assets/thumb//347/202/271/345/207/273/345/233/236/345/272/224 - /345/256/263/347/276/236/346/203/212/350/256/266.webm +0 -0
- package/assets/thumb//347/202/271/345/207/273/345/233/236/345/272/224 - /345/274/200/345/277/203/350/267/203/345/212/250.webm +0 -0
- package/assets/thumb//347/205/247/351/225/234/345/255/220.webm +0 -0
- package/assets/thumb//347/216/251/346/260/264/346/236/252.webm +0 -0
- package/assets/thumb//347/216/251/346/270/270/346/210/217/346/260/224/346/200/245/350/264/245/345/235/217.webm +0 -0
- package/assets/thumb//347/224/250/351/262/270/351/261/274/345/260/276/345/267/264/346/213/215/346/211/223/345/234/260/351/235/242.webm +0 -0
- package/assets/thumb//350/223/235/351/262/270/347/216/260/344/270/226.webm +0 -0
- package/assets/thumb//350/236/203/350/237/271/350/265/260/350/267/257.webm +0 -0
- package/assets/thumb//350/242/253/345/220/223/344/270/200/350/267/263/357/274/210/347/202/270/346/257/233/357/274/211.webm +0 -0
- package/assets/thumb//350/242/253/350/220/275/345/217/266/346/267/271/346/262/241.webm +0 -0
- package/assets/thumb//350/242/253/351/274/240/346/240/207/346/213/226/346/213/275/346/202/254/347/251/272/345/217/215/351/246/210.webm +0 -0
- package/assets/thumb//350/266/205/345/244/247/344/274/270/346/207/222/350/205/260.webm +0 -0
- package/assets/thumb//350/275/273/345/277/253/346/221/207/346/221/206/350/210/236.webm +0 -0
- package/assets/thumb//350/275/273/345/277/253/350/256/260/345/275/225.webm +0 -0
- package/assets/thumb//351/262/270/351/261/274/345/220/220/346/263/241/346/263/241/347/211/271/346/225/210.webm +0 -0
- package/cordis.patch.yml +36 -0
- package/lib/client.js +238 -0
- package/lib/index.js +589 -0
- package/lib/index.js.bak-20260819-104008 +483 -0
- package/lib/pet-helper-process.js +133 -0
- package/lib/pet-protocol.js +64 -0
- package/lib/pet-reducer.js +527 -0
- package/lib/pet-status-copy.js +118 -0
- package/lib/types/client/index.d.ts +43 -0
- package/lib/types/index.d.ts +65 -0
- package/package.json +85 -0
- package/runtime/electron-helper/index.html +134 -0
- package/runtime/electron-helper/main.js +191 -0
- package/runtime/electron-helper/package.json +7 -0
- package/runtime/electron-helper/preload.js +39 -0
- package/runtime/electron-helper/renderer.js +903 -0
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CompanionMessageKind,
|
|
3
|
+
CompanionState,
|
|
4
|
+
createMessage,
|
|
5
|
+
} from './pet-protocol.js'
|
|
6
|
+
import {
|
|
7
|
+
activityCopy,
|
|
8
|
+
activityStage,
|
|
9
|
+
statusCopy,
|
|
10
|
+
taskCopy,
|
|
11
|
+
} from './pet-status-copy.js'
|
|
12
|
+
|
|
13
|
+
const statePriority = Object.freeze({
|
|
14
|
+
[CompanionState.WAITING]: 60,
|
|
15
|
+
[CompanionState.ERROR]: 50,
|
|
16
|
+
[CompanionState.WORKING]: 30,
|
|
17
|
+
[CompanionState.THINKING]: 20,
|
|
18
|
+
[CompanionState.IDLE]: 0,
|
|
19
|
+
[CompanionState.DISCONNECTED]: -1,
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
function toolActivity(name) {
|
|
23
|
+
const value = String(name || '').toLowerCase()
|
|
24
|
+
if (/search|grep|find|glob|web|read|fetch|open/.test(value)) return 'searching'
|
|
25
|
+
if (/write|edit|patch|replace|create|move|delete/.test(value)) return 'editing'
|
|
26
|
+
if (/test|check|lint|build|verify/.test(value)) return 'testing'
|
|
27
|
+
if (/shell|bash|exec|command|terminal|powershell/.test(value)) return 'commanding'
|
|
28
|
+
return 'using-tool'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function toolCallIdOf(event, fallback = '') {
|
|
32
|
+
const content = event?.data?.message?.content
|
|
33
|
+
const contentCallId = Array.isArray(content)
|
|
34
|
+
? content.find((item) => item?.toolCallId)?.toolCallId
|
|
35
|
+
: undefined
|
|
36
|
+
return String(event?.data?.message?.source?.callId
|
|
37
|
+
?? contentCallId
|
|
38
|
+
?? event?.data?.message?.toolCallId
|
|
39
|
+
?? event?.data?.message?.callId
|
|
40
|
+
?? event?.data?.callId
|
|
41
|
+
?? fallback)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isUserQuestionTool(name) {
|
|
45
|
+
const value = String(name || '').toLowerCase()
|
|
46
|
+
return /ask.*user.*question|request.*user.*input|user[-_/.:]?questions?/u.test(value)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 解析 tool/call 事件里的 arguments(可能是 JSON 字符串或对象)。 */
|
|
50
|
+
function parseToolArguments(event) {
|
|
51
|
+
try {
|
|
52
|
+
const raw = event.data?.arguments ?? event.data?.message?.arguments
|
|
53
|
+
if (typeof raw === 'string') return JSON.parse(raw)
|
|
54
|
+
if (raw && typeof raw === 'object') return raw
|
|
55
|
+
} catch {
|
|
56
|
+
/* 非 JSON 参数直接忽略 */
|
|
57
|
+
}
|
|
58
|
+
return undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 从 ask_user_question 工具参数里提取给用户看的文字提示。 */
|
|
62
|
+
function userQuestionPromptOf(event) {
|
|
63
|
+
const args = parseToolArguments(event)
|
|
64
|
+
const questions = Array.isArray(args?.questions) ? args.questions : []
|
|
65
|
+
if (questions.length === 0) return undefined
|
|
66
|
+
const first = questions[0]
|
|
67
|
+
const parts = []
|
|
68
|
+
if (first?.header) parts.push(`【${first.header}】`)
|
|
69
|
+
if (first?.question) parts.push(first.question)
|
|
70
|
+
if (Array.isArray(first?.options) && first.options.length > 0) {
|
|
71
|
+
const labels = first.options.map((option) => option?.label).filter(Boolean)
|
|
72
|
+
if (labels.length > 0) parts.push(`选项:${labels.join(' / ')}`)
|
|
73
|
+
}
|
|
74
|
+
return parts.join(' ') || undefined
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function sessionIdOf(session) {
|
|
78
|
+
return String(session?.header?.id ?? session?.id ?? 'unknown-session')
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function isSubagent(session) {
|
|
82
|
+
return session?.header?.origin === 'subagent'
|
|
83
|
+
|| Number(session?.header?.delegationDepth ?? 0) > 0
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function cleanProjectName(value) {
|
|
87
|
+
const text = String(value ?? '').trim()
|
|
88
|
+
if (!text) return undefined
|
|
89
|
+
const pathParts = text.split(/[\\/]/u).filter(Boolean)
|
|
90
|
+
const candidate = pathParts.length > 1 ? pathParts.at(-1) : text
|
|
91
|
+
return candidate.replace(/\s+/gu, ' ').slice(0, 40) || undefined
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function projectNameOf(session, event) {
|
|
95
|
+
const candidates = [
|
|
96
|
+
session?.header?.title,
|
|
97
|
+
session?.header?.name,
|
|
98
|
+
session?.title,
|
|
99
|
+
session?.name,
|
|
100
|
+
session?.header?.cwd,
|
|
101
|
+
session?.cwd,
|
|
102
|
+
session?.context?.cwd,
|
|
103
|
+
event?.data?.projectName,
|
|
104
|
+
event?.data?.cwd,
|
|
105
|
+
]
|
|
106
|
+
return candidates.map(cleanProjectName).find(Boolean)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function progressOf(todos) {
|
|
110
|
+
if (!Array.isArray(todos) || todos.length === 0) return undefined
|
|
111
|
+
const completed = todos.filter((todo) => ['completed', 'complete', 'done'].includes(todo?.status)).length
|
|
112
|
+
const currentIndex = todos.findIndex((todo) => todo?.status === 'in_progress')
|
|
113
|
+
return {
|
|
114
|
+
completed,
|
|
115
|
+
total: todos.length,
|
|
116
|
+
current: currentIndex >= 0 ? currentIndex + 1 : undefined,
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function detailFor(record, stage = record.payload.stage) {
|
|
121
|
+
const parts = []
|
|
122
|
+
if (record.project) parts.push(record.project)
|
|
123
|
+
if (record.progress?.total) parts.push(`已完成 ${record.progress.completed}/${record.progress.total} 步`)
|
|
124
|
+
if (record.task) parts.push(record.task)
|
|
125
|
+
else if (stage) parts.push(stage)
|
|
126
|
+
return parts.join(' · ') || stage || 'DSH 任务'
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export class CompanionReducer {
|
|
130
|
+
constructor({ includeSubagents = false } = {}) {
|
|
131
|
+
this.includeSubagents = includeSubagents
|
|
132
|
+
this.sessions = new Map()
|
|
133
|
+
this.clock = 0
|
|
134
|
+
this.selectedSessionId = undefined
|
|
135
|
+
this.outputSignature = undefined
|
|
136
|
+
this.tasksSignature = undefined
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
setIncludeSubagents(value) {
|
|
140
|
+
const includeSubagents = value === true
|
|
141
|
+
if (includeSubagents === this.includeSubagents) return []
|
|
142
|
+
this.includeSubagents = includeSubagents
|
|
143
|
+
if (!includeSubagents) {
|
|
144
|
+
for (const [sessionId, record] of this.sessions) {
|
|
145
|
+
if (record.subagent) this.sessions.delete(sessionId)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return this.#render()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
handle(session, event) {
|
|
152
|
+
if (!event || typeof event.type !== 'string') return []
|
|
153
|
+
const subagent = isSubagent(session)
|
|
154
|
+
if (!this.includeSubagents && subagent) return []
|
|
155
|
+
|
|
156
|
+
const sessionId = sessionIdOf(session)
|
|
157
|
+
const record = this.#record(sessionId)
|
|
158
|
+
record.subagent = subagent
|
|
159
|
+
record.lastSeq = Number(event.seq ?? record.lastSeq)
|
|
160
|
+
record.project = projectNameOf(session, event) ?? record.project
|
|
161
|
+
|
|
162
|
+
switch (event.type) {
|
|
163
|
+
case 'turn/start':
|
|
164
|
+
record.turnActive = true
|
|
165
|
+
record.openTools.clear()
|
|
166
|
+
record.waitingCallId = undefined
|
|
167
|
+
record.task = undefined
|
|
168
|
+
record.progress = undefined
|
|
169
|
+
this.#update(record, CompanionState.THINKING, {
|
|
170
|
+
phase: 'turn-start',
|
|
171
|
+
stage: '准备阶段',
|
|
172
|
+
message: statusCopy('preparing', event.seq),
|
|
173
|
+
})
|
|
174
|
+
return this.#render()
|
|
175
|
+
|
|
176
|
+
case 'step/start':
|
|
177
|
+
case 'assistant/chunk':
|
|
178
|
+
case 'assistant/message':
|
|
179
|
+
if (!record.turnActive || record.openTools.size > 0) return []
|
|
180
|
+
if (record.state === CompanionState.THINKING && record.payload.phase === 'thinking') return []
|
|
181
|
+
this.#update(record, CompanionState.THINKING, {
|
|
182
|
+
phase: 'thinking',
|
|
183
|
+
stage: '分析阶段',
|
|
184
|
+
message: statusCopy('thinking', event.seq),
|
|
185
|
+
})
|
|
186
|
+
return this.#render()
|
|
187
|
+
|
|
188
|
+
case 'tool/call': {
|
|
189
|
+
const callId = toolCallIdOf(event, `seq-${String(event.seq ?? 'unknown')}`)
|
|
190
|
+
const name = String(event.data?.name ?? event.data?.message?.name ?? 'tool')
|
|
191
|
+
record.openTools.set(callId, name)
|
|
192
|
+
if (isUserQuestionTool(name)) {
|
|
193
|
+
record.waitingCallId = callId
|
|
194
|
+
const prompt = userQuestionPromptOf(event)
|
|
195
|
+
this.#update(record, CompanionState.WAITING, {
|
|
196
|
+
phase: 'user-question',
|
|
197
|
+
stage: '等待确认',
|
|
198
|
+
toolName: name,
|
|
199
|
+
message: prompt ?? statusCopy('waiting', event.seq),
|
|
200
|
+
detail: prompt ? '请在 DSH 中回答' : undefined,
|
|
201
|
+
})
|
|
202
|
+
return this.#render()
|
|
203
|
+
}
|
|
204
|
+
const activity = toolActivity(name)
|
|
205
|
+
this.#update(record, CompanionState.WORKING, {
|
|
206
|
+
phase: 'tool-call',
|
|
207
|
+
activity,
|
|
208
|
+
stage: activityStage(activity),
|
|
209
|
+
toolName: name,
|
|
210
|
+
message: activityCopy(activity, event.seq),
|
|
211
|
+
})
|
|
212
|
+
return this.#render()
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
case 'tool/result':
|
|
216
|
+
return this.#toolResult(record, event)
|
|
217
|
+
|
|
218
|
+
case 'approval/asked':
|
|
219
|
+
record.waitingCallId = event.data?.callId
|
|
220
|
+
this.#update(record, CompanionState.WAITING, {
|
|
221
|
+
phase: 'approval',
|
|
222
|
+
stage: '等待批准',
|
|
223
|
+
toolName: event.data?.toolName,
|
|
224
|
+
message: event.data?.reason || statusCopy('waiting', event.seq),
|
|
225
|
+
detail: event.data?.toolName ? `需要批准操作:${event.data.toolName}` : undefined,
|
|
226
|
+
})
|
|
227
|
+
return this.#render()
|
|
228
|
+
|
|
229
|
+
case 'approval/decided':
|
|
230
|
+
if (record.waitingCallId) {
|
|
231
|
+
record.openTools.delete(record.waitingCallId)
|
|
232
|
+
record.waitingCallId = undefined
|
|
233
|
+
}
|
|
234
|
+
return this.#resumeAfterTool(record, event)
|
|
235
|
+
|
|
236
|
+
case 'user/message':
|
|
237
|
+
return this.#userMessage(record, event)
|
|
238
|
+
|
|
239
|
+
case 'todo/write':
|
|
240
|
+
return this.#todo(record, event)
|
|
241
|
+
|
|
242
|
+
case 'turn/end':
|
|
243
|
+
return this.#turnEnd(record, event)
|
|
244
|
+
|
|
245
|
+
default:
|
|
246
|
+
return []
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
disposeSession(session) {
|
|
251
|
+
const sessionId = sessionIdOf(session)
|
|
252
|
+
const existed = this.sessions.delete(sessionId)
|
|
253
|
+
if (!existed) return []
|
|
254
|
+
return this.#render()
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
#toolResult(record, event) {
|
|
258
|
+
const callId = toolCallIdOf(event)
|
|
259
|
+
if (callId) record.openTools.delete(callId)
|
|
260
|
+
if (callId && callId === record.waitingCallId) record.waitingCallId = undefined
|
|
261
|
+
return this.#resumeAfterTool(record, event)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#userMessage(record, event) {
|
|
265
|
+
if (!record.waitingCallId) return []
|
|
266
|
+
record.openTools.delete(record.waitingCallId)
|
|
267
|
+
record.waitingCallId = undefined
|
|
268
|
+
return this.#resumeAfterTool(record, event)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#resumeAfterTool(record, event) {
|
|
272
|
+
if (record.waitingCallId && record.openTools.has(record.waitingCallId)) {
|
|
273
|
+
return this.#render()
|
|
274
|
+
}
|
|
275
|
+
const next = record.openTools.size > 0 ? CompanionState.WORKING : CompanionState.THINKING
|
|
276
|
+
const nextPayload = {
|
|
277
|
+
phase: 'tool-result',
|
|
278
|
+
activity: next === CompanionState.WORKING
|
|
279
|
+
? toolActivity(record.openTools.values().next().value)
|
|
280
|
+
: undefined,
|
|
281
|
+
stage: next === CompanionState.WORKING
|
|
282
|
+
? activityStage(toolActivity(record.openTools.values().next().value))
|
|
283
|
+
: '整理阶段',
|
|
284
|
+
message: next === CompanionState.WORKING
|
|
285
|
+
? activityCopy(toolActivity(record.openTools.values().next().value), event.seq)
|
|
286
|
+
: statusCopy('result', event.seq),
|
|
287
|
+
}
|
|
288
|
+
this.#update(record, next, nextPayload)
|
|
289
|
+
if (!event.data?.error) return this.#render()
|
|
290
|
+
|
|
291
|
+
const selection = this.#select()
|
|
292
|
+
if (selection.record.state === CompanionState.WAITING || selection.record.state === CompanionState.ERROR) {
|
|
293
|
+
return this.#render(selection)
|
|
294
|
+
}
|
|
295
|
+
this.#remember(selection)
|
|
296
|
+
return this.#withTasks([createMessage(CompanionMessageKind.PULSE, {
|
|
297
|
+
sessionId: record.id,
|
|
298
|
+
sourceSeq: event.seq,
|
|
299
|
+
state: CompanionState.ERROR,
|
|
300
|
+
ttlMs: 1800,
|
|
301
|
+
resumeState: selection.record.state,
|
|
302
|
+
resumeActivity: selection.record.payload.activity,
|
|
303
|
+
resumeMessage: selection.record.payload.message,
|
|
304
|
+
resumeDetail: detailFor(selection.record),
|
|
305
|
+
message: statusCopy('toolError', event.seq),
|
|
306
|
+
detail: detailFor(record),
|
|
307
|
+
errorCode: event.data.error.code,
|
|
308
|
+
})])
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
#todo(record, event) {
|
|
312
|
+
const todos = Array.isArray(event.data?.todos) ? event.data.todos : []
|
|
313
|
+
const current = todos.find((todo) => todo?.status === 'in_progress')
|
|
314
|
+
?? todos.find((todo) => todo?.status === 'pending')
|
|
315
|
+
const progress = progressOf(todos)
|
|
316
|
+
if (!current?.content && !progress) return []
|
|
317
|
+
const nextTask = current?.content ? String(current.content) : record.task
|
|
318
|
+
const unchanged = nextTask === record.task
|
|
319
|
+
&& progress?.completed === record.progress?.completed
|
|
320
|
+
&& progress?.total === record.progress?.total
|
|
321
|
+
if (unchanged) return []
|
|
322
|
+
record.task = nextTask
|
|
323
|
+
record.progress = progress
|
|
324
|
+
record.updatedAt = ++this.clock
|
|
325
|
+
const selection = this.#select()
|
|
326
|
+
if (selection.record.id !== record.id) return this.#render(selection)
|
|
327
|
+
return this.#withTasks([createMessage(CompanionMessageKind.TASK, {
|
|
328
|
+
sessionId: record.id,
|
|
329
|
+
sourceSeq: event.seq,
|
|
330
|
+
task: record.task,
|
|
331
|
+
progress: record.progress,
|
|
332
|
+
project: record.project,
|
|
333
|
+
message: taskCopy(record.task),
|
|
334
|
+
detail: detailFor(record, '执行阶段'),
|
|
335
|
+
})])
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
#turnEnd(record, event) {
|
|
339
|
+
record.turnActive = false
|
|
340
|
+
record.openTools.clear()
|
|
341
|
+
record.waitingCallId = undefined
|
|
342
|
+
const kind = String(event.data?.reason?.kind ?? 'completed')
|
|
343
|
+
|
|
344
|
+
if (kind === 'blocked') {
|
|
345
|
+
this.#update(record, CompanionState.WAITING, {
|
|
346
|
+
phase: 'turn-end',
|
|
347
|
+
stage: '等待确认',
|
|
348
|
+
message: statusCopy('waiting', event.seq),
|
|
349
|
+
})
|
|
350
|
+
return this.#render()
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (kind === 'aborted') {
|
|
354
|
+
this.#update(record, CompanionState.IDLE, {
|
|
355
|
+
phase: 'turn-end',
|
|
356
|
+
stage: '已停止',
|
|
357
|
+
message: statusCopy('stopped', event.seq),
|
|
358
|
+
})
|
|
359
|
+
return this.#render()
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (kind !== 'completed') {
|
|
363
|
+
this.#update(record, CompanionState.ERROR, {
|
|
364
|
+
phase: 'turn-end',
|
|
365
|
+
stage: '需要处理',
|
|
366
|
+
reasonKind: kind,
|
|
367
|
+
message: kind === 'max-tokens'
|
|
368
|
+
? statusCopy('limit', event.seq)
|
|
369
|
+
: statusCopy('error', event.seq),
|
|
370
|
+
})
|
|
371
|
+
return this.#render()
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
this.#update(record, CompanionState.IDLE, {
|
|
375
|
+
phase: 'turn-end',
|
|
376
|
+
stage: '已完成',
|
|
377
|
+
message: statusCopy('idle', event.seq),
|
|
378
|
+
})
|
|
379
|
+
const selection = this.#select()
|
|
380
|
+
if ([CompanionState.WAITING, CompanionState.ERROR].includes(selection.record.state)) {
|
|
381
|
+
return this.#render(selection)
|
|
382
|
+
}
|
|
383
|
+
this.#remember(selection)
|
|
384
|
+
return this.#withTasks([createMessage(CompanionMessageKind.PULSE, {
|
|
385
|
+
sessionId: record.id,
|
|
386
|
+
sourceSeq: event.seq,
|
|
387
|
+
state: CompanionState.SUCCESS,
|
|
388
|
+
resumeState: selection.record.state,
|
|
389
|
+
resumeActivity: selection.record.payload.activity,
|
|
390
|
+
resumeMessage: selection.record.payload.message,
|
|
391
|
+
resumeDetail: detailFor(selection.record),
|
|
392
|
+
ttlMs: 2200,
|
|
393
|
+
phase: 'turn-end',
|
|
394
|
+
message: statusCopy('success', event.seq),
|
|
395
|
+
detail: detailFor(record, '本轮已完成'),
|
|
396
|
+
})])
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
#record(sessionId) {
|
|
400
|
+
let record = this.sessions.get(sessionId)
|
|
401
|
+
if (record) return record
|
|
402
|
+
record = {
|
|
403
|
+
id: sessionId,
|
|
404
|
+
state: CompanionState.IDLE,
|
|
405
|
+
payload: { phase: 'session-created', message: 'DSH 空闲中' },
|
|
406
|
+
turnActive: false,
|
|
407
|
+
openTools: new Map(),
|
|
408
|
+
waitingCallId: undefined,
|
|
409
|
+
task: undefined,
|
|
410
|
+
progress: undefined,
|
|
411
|
+
project: undefined,
|
|
412
|
+
subagent: false,
|
|
413
|
+
lastSeq: -1,
|
|
414
|
+
updatedAt: ++this.clock,
|
|
415
|
+
}
|
|
416
|
+
this.sessions.set(sessionId, record)
|
|
417
|
+
return record
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
#update(record, state, payload) {
|
|
421
|
+
record.state = state
|
|
422
|
+
record.payload = payload
|
|
423
|
+
record.updatedAt = ++this.clock
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
#select() {
|
|
427
|
+
const records = [...this.sessions.values()]
|
|
428
|
+
if (records.length === 0) {
|
|
429
|
+
return {
|
|
430
|
+
record: {
|
|
431
|
+
id: 'dsh-host',
|
|
432
|
+
state: CompanionState.IDLE,
|
|
433
|
+
payload: { phase: 'no-session', message: 'DSH 空闲中' },
|
|
434
|
+
updatedAt: ++this.clock,
|
|
435
|
+
},
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
records.sort((left, right) => {
|
|
439
|
+
const priority = (statePriority[right.state] ?? 0) - (statePriority[left.state] ?? 0)
|
|
440
|
+
return priority || right.updatedAt - left.updatedAt || left.id.localeCompare(right.id)
|
|
441
|
+
})
|
|
442
|
+
return { record: records[0] }
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
#render(selection = this.#select()) {
|
|
446
|
+
const messages = []
|
|
447
|
+
const signature = this.#signature(selection.record)
|
|
448
|
+
if (signature !== this.outputSignature) {
|
|
449
|
+
this.#remember(selection)
|
|
450
|
+
messages.push(createMessage(CompanionMessageKind.STATE, {
|
|
451
|
+
sessionId: selection.record.id,
|
|
452
|
+
state: selection.record.state,
|
|
453
|
+
...selection.record.payload,
|
|
454
|
+
task: selection.record.task,
|
|
455
|
+
progress: selection.record.progress,
|
|
456
|
+
project: selection.record.project,
|
|
457
|
+
detail: detailFor(selection.record),
|
|
458
|
+
}))
|
|
459
|
+
}
|
|
460
|
+
messages.push(...this.#taskMessages())
|
|
461
|
+
return messages
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
#taskMessages() {
|
|
465
|
+
const tasks = this.#activeTaskList()
|
|
466
|
+
if (tasks.length < 2) {
|
|
467
|
+
if (this.tasksSignature !== undefined) {
|
|
468
|
+
this.tasksSignature = undefined
|
|
469
|
+
return [createMessage(CompanionMessageKind.TASKS, { tasks: [] })]
|
|
470
|
+
}
|
|
471
|
+
return []
|
|
472
|
+
}
|
|
473
|
+
const signature = tasks.map((task) => [
|
|
474
|
+
task.sessionId,
|
|
475
|
+
task.state,
|
|
476
|
+
task.project ?? '',
|
|
477
|
+
task.task ?? '',
|
|
478
|
+
task.message ?? '',
|
|
479
|
+
task.detail ?? '',
|
|
480
|
+
].join('|')).join('~')
|
|
481
|
+
if (signature === this.tasksSignature) return []
|
|
482
|
+
this.tasksSignature = signature
|
|
483
|
+
return [createMessage(CompanionMessageKind.TASKS, { tasks })]
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
#activeTaskList() {
|
|
487
|
+
return [...this.sessions.values()]
|
|
488
|
+
.filter((record) => record.state !== CompanionState.IDLE && record.state !== CompanionState.DISCONNECTED)
|
|
489
|
+
.sort((left, right) => {
|
|
490
|
+
const priority = (statePriority[right.state] ?? 0) - (statePriority[left.state] ?? 0)
|
|
491
|
+
return priority || right.updatedAt - left.updatedAt || left.id.localeCompare(right.id)
|
|
492
|
+
})
|
|
493
|
+
.map((record) => ({
|
|
494
|
+
sessionId: record.id,
|
|
495
|
+
state: record.state,
|
|
496
|
+
project: record.project,
|
|
497
|
+
task: record.task,
|
|
498
|
+
message: record.payload.message,
|
|
499
|
+
detail: detailFor(record),
|
|
500
|
+
}))
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
#withTasks(messages) {
|
|
504
|
+
return [...messages, ...this.#taskMessages()]
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
#remember(selection) {
|
|
508
|
+
this.selectedSessionId = selection.record.id
|
|
509
|
+
this.outputSignature = this.#signature(selection.record)
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
#signature(record) {
|
|
513
|
+
return [
|
|
514
|
+
record.id,
|
|
515
|
+
record.state,
|
|
516
|
+
record.payload.activity ?? '',
|
|
517
|
+
record.payload.toolName ?? '',
|
|
518
|
+
record.payload.message ?? '',
|
|
519
|
+
record.project ?? '',
|
|
520
|
+
record.task ?? '',
|
|
521
|
+
record.progress?.completed ?? '',
|
|
522
|
+
record.progress?.total ?? '',
|
|
523
|
+
].join('|')
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export { isUserQuestionTool, statePriority, toolActivity, toolCallIdOf }
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const COPY = Object.freeze({
|
|
2
|
+
idle: [
|
|
3
|
+
'大肥鱼在这儿等新任务哦~',
|
|
4
|
+
'现在暂时没任务呢,先游两圈~',
|
|
5
|
+
'大肥鱼正在待命中,随时可以开工!',
|
|
6
|
+
],
|
|
7
|
+
preparing: [
|
|
8
|
+
'新任务正在梳理中哦~',
|
|
9
|
+
'让本鱼先看看项目呢',
|
|
10
|
+
'正在理清接下来要做什么呀',
|
|
11
|
+
],
|
|
12
|
+
thinking: [
|
|
13
|
+
'大肥鱼正在认真想下一步呢',
|
|
14
|
+
'正在梳理思路哦~',
|
|
15
|
+
'让本鱼整理一下刚才的结果呢',
|
|
16
|
+
],
|
|
17
|
+
searching: [
|
|
18
|
+
'正在帮你找相关内容呢',
|
|
19
|
+
'大肥鱼正在项目里仔细找找哦~',
|
|
20
|
+
'正在翻箱倒柜看相关文件呢',
|
|
21
|
+
],
|
|
22
|
+
editing: [
|
|
23
|
+
'这部分正在修改中哦',
|
|
24
|
+
'大肥鱼正在把改动写进去呢',
|
|
25
|
+
'正在认真调整实现呢',
|
|
26
|
+
],
|
|
27
|
+
testing: [
|
|
28
|
+
'正在认真检查结果呢',
|
|
29
|
+
'大肥鱼正在跑测试确认一下哦',
|
|
30
|
+
'正在验证改动有没有问题呢',
|
|
31
|
+
],
|
|
32
|
+
commanding: [
|
|
33
|
+
'正在执行项目命令呢',
|
|
34
|
+
'大肥鱼正在让项目跑起来哦',
|
|
35
|
+
'正在看看命令执行得怎么样呢',
|
|
36
|
+
],
|
|
37
|
+
working: [
|
|
38
|
+
'正在继续处理任务呢',
|
|
39
|
+
'这一步正在进行中哦',
|
|
40
|
+
'大肥鱼还在认真干活呢,别打扰我~',
|
|
41
|
+
],
|
|
42
|
+
result: [
|
|
43
|
+
'这一步处理好啦,继续看看哦~',
|
|
44
|
+
'搞定一步,大肥鱼接着看下一步呢',
|
|
45
|
+
'正在确认下一步怎么做呀',
|
|
46
|
+
],
|
|
47
|
+
waiting: [
|
|
48
|
+
'需要你确认一下后续呢~',
|
|
49
|
+
'这里要等你看一下哦',
|
|
50
|
+
'轮到你来决定下一步啦,大肥鱼等你~',
|
|
51
|
+
],
|
|
52
|
+
success: [
|
|
53
|
+
'这次的任务搞定啦~',
|
|
54
|
+
'这一轮顺利完成啦,大肥鱼真棒!',
|
|
55
|
+
'任务完成咯,可以摸摸鱼啦~',
|
|
56
|
+
],
|
|
57
|
+
toolError: [
|
|
58
|
+
'这一步好像没跑通呢~',
|
|
59
|
+
'刚才的操作遇到一点问题哦',
|
|
60
|
+
'这里卡了一下,大肥鱼再等等你呢',
|
|
61
|
+
],
|
|
62
|
+
error: [
|
|
63
|
+
'任务好像遇到一点问题呢~',
|
|
64
|
+
'这里需要回来看看啦',
|
|
65
|
+
'这次没有顺利跑完呢,大肥鱼有点委屈',
|
|
66
|
+
],
|
|
67
|
+
stopped: [
|
|
68
|
+
'任务已经停下来啦~',
|
|
69
|
+
'这次任务先停在这里哦',
|
|
70
|
+
],
|
|
71
|
+
limit: [
|
|
72
|
+
'内容有点多,到上限啦~',
|
|
73
|
+
'这次输出已经到上限咯,大肥鱼也尽力啦',
|
|
74
|
+
],
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
function seedNumber(seed) {
|
|
78
|
+
const number = Number(seed)
|
|
79
|
+
if (Number.isFinite(number)) return Math.abs(Math.trunc(number))
|
|
80
|
+
return [...String(seed ?? '')].reduce((total, character) => total + character.codePointAt(0), 0)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function statusCopy(group, seed = 0) {
|
|
84
|
+
const variants = COPY[group] ?? COPY.working
|
|
85
|
+
return variants[seedNumber(seed) % variants.length]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function activityCopy(activity, seed = 0) {
|
|
89
|
+
return statusCopy({
|
|
90
|
+
searching: 'searching',
|
|
91
|
+
editing: 'editing',
|
|
92
|
+
testing: 'testing',
|
|
93
|
+
commanding: 'commanding',
|
|
94
|
+
}[activity] ?? 'working', seed)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function activityStage(activity) {
|
|
98
|
+
return {
|
|
99
|
+
searching: '查找阶段',
|
|
100
|
+
editing: '实现阶段',
|
|
101
|
+
testing: '验证阶段',
|
|
102
|
+
commanding: '执行阶段',
|
|
103
|
+
}[activity] ?? '处理阶段'
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function taskCopy(task) {
|
|
107
|
+
const value = String(task ?? '').trim().replace(/[。!?.!?]+$/u, '')
|
|
108
|
+
if (!value) return statusCopy('working')
|
|
109
|
+
if (/^(正在|继续)/u.test(value)) {
|
|
110
|
+
return `${value}呢~`
|
|
111
|
+
}
|
|
112
|
+
if (/^(准备|检查|验证|修改|修复|测试|构建|整理|分析|梳理|查找|搜索|读取|实现)/u.test(value)) {
|
|
113
|
+
return `正在${value}呢~`
|
|
114
|
+
}
|
|
115
|
+
return `正在处理「${value}」呢~`
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export { COPY as statusCopyLibrary }
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ============================================================================
|
|
3
|
+
* better-dsh-pet 浏览器半侧的类型声明(TypeScript)
|
|
4
|
+
* ============================================================================
|
|
5
|
+
*
|
|
6
|
+
* 【用途】
|
|
7
|
+
* 给 lib/client.js(浏览器半侧)提供类型信息。纯类型文件,不影响运行时。
|
|
8
|
+
*
|
|
9
|
+
* 【对应实现】
|
|
10
|
+
* lib/client.js —— 不再渲染网页浮动宠物,只在 DSH 设置页注册“小宠物桌面伴侣”
|
|
11
|
+
* 配置卡片,通过 /plugins/better-dsh-pet/config 读写宿主配置。
|
|
12
|
+
*
|
|
13
|
+
* ============================================================================
|
|
14
|
+
* @module better-dsh-pet/client
|
|
15
|
+
*/
|
|
16
|
+
import type { Context } from '@deepseek-ai/dsh-client-runtime';
|
|
17
|
+
|
|
18
|
+
/** Cordis 插件名(loader 诊断用),与 lib/client.js 的 name 一致 */
|
|
19
|
+
export declare const name = 'pet';
|
|
20
|
+
/** 需要注入的服务列表(slots 槽位注册表),与 lib/client.js 的 inject 一致 */
|
|
21
|
+
export declare const inject: string[];
|
|
22
|
+
|
|
23
|
+
/** 客户端插件配置(目前只用于设置卡片展示,实际值由宿主 /config 提供) */
|
|
24
|
+
export interface Config {
|
|
25
|
+
enabled?: boolean;
|
|
26
|
+
scale?: number;
|
|
27
|
+
bubbleScale?: number;
|
|
28
|
+
activityLevel?: 'quiet' | 'normal' | 'lively';
|
|
29
|
+
reducedMotion?: boolean;
|
|
30
|
+
bubbleMode?: 'always' | 'hidden' | 'custom';
|
|
31
|
+
bubbleStates?: string[];
|
|
32
|
+
includeSubagents?: boolean;
|
|
33
|
+
workMinutes?: number;
|
|
34
|
+
breakMinutes?: number;
|
|
35
|
+
roastEnabled?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 客户端插件主体:注册设置卡片到 `settings.plugin.item`。
|
|
40
|
+
* @param ctx - 客户端根上下文(ctx.slots 提供槽位注册)
|
|
41
|
+
* @param config - 本行配置(来自 patch 树;当前实际为空对象)
|
|
42
|
+
*/
|
|
43
|
+
export declare function apply(ctx: Context, config: Config): void;
|