moi-computer 0.0.1
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 +95 -0
- package/README.md +71 -0
- package/bin/moi.mjs +25 -0
- package/bunfig.toml +3 -0
- package/client/CLAUDE.md +12 -0
- package/client/api/workspaces.ts +158 -0
- package/client/assets/claude.svg +3 -0
- package/client/assets/favicon.png +0 -0
- package/client/assets/hermes-nous.png +0 -0
- package/client/assets/mcp/.gitkeep +0 -0
- package/client/assets/mcp/atlassian.png +0 -0
- package/client/assets/mcp/discord.png +0 -0
- package/client/assets/mcp/figma.png +0 -0
- package/client/assets/mcp/github.png +0 -0
- package/client/assets/mcp/gitlab.png +0 -0
- package/client/assets/mcp/granola.png +0 -0
- package/client/assets/mcp/hubspot.png +0 -0
- package/client/assets/mcp/linear.png +0 -0
- package/client/assets/mcp/neon.png +0 -0
- package/client/assets/mcp/notion.png +0 -0
- package/client/assets/mcp/sentry.png +0 -0
- package/client/assets/mcp/slack.png +0 -0
- package/client/assets/mcp/stripe.png +0 -0
- package/client/assets/mcp/supabase.png +0 -0
- package/client/assets/mcp/vercel.png +0 -0
- package/client/assets/moi-logo.svg +11 -0
- package/client/assets/openclaw.svg +22 -0
- package/client/components/App.tsx +35 -0
- package/client/components/BottomPanel.tsx +33 -0
- package/client/components/ChatInput.tsx +62 -0
- package/client/components/ChatPanel.tsx +206 -0
- package/client/components/ChatPopup.tsx +60 -0
- package/client/components/CustomizePanel.tsx +117 -0
- package/client/components/HiddenPanel.tsx +79 -0
- package/client/components/HomePage.tsx +17 -0
- package/client/components/MarkdownContent.tsx +41 -0
- package/client/components/ThreadSelector.tsx +88 -0
- package/client/components/TurnView.tsx +618 -0
- package/client/components/WidgetErrorBoundary.tsx +46 -0
- package/client/components/WidgetFrame.tsx +66 -0
- package/client/components/WidgetGrid.tsx +113 -0
- package/client/components/WidgetShell.tsx +44 -0
- package/client/components/Widgets.tsx +132 -0
- package/client/components/WorkspacePreview.tsx +48 -0
- package/client/components/WorkspacesPage.tsx +237 -0
- package/client/components/layout/SidebarLayout.tsx +260 -0
- package/client/components/playground/LedLogo.tsx +181 -0
- package/client/components/playground/PixelPainter.tsx +105 -0
- package/client/components/playground/PlaygroundPage.tsx +71 -0
- package/client/components/routes/workspace/[id].tsx +282 -0
- package/client/components/ui/button.tsx +48 -0
- package/client/components/ui/dropdown-menu.tsx +257 -0
- package/client/components/ui/input.tsx +21 -0
- package/client/components/ui/popover.tsx +79 -0
- package/client/components/ui/textarea.tsx +18 -0
- package/client/externalize-react.ts +29 -0
- package/client/hooks/useChat.ts +88 -0
- package/client/hooks/useFitsSidebar.ts +34 -0
- package/client/hooks/useGridReconcile.ts +73 -0
- package/client/hooks/useMeiEvents.ts +66 -0
- package/client/hooks/useScrollFade.ts +38 -0
- package/client/hooks/useWidget.ts +72 -0
- package/client/hooks/useWidgetList.ts +30 -0
- package/client/hooks/useWorkspaceTheme.ts +76 -0
- package/client/index.css +182 -0
- package/client/index.html +43 -0
- package/client/index.tsx +19 -0
- package/client/lib/WorkspaceContext.tsx +24 -0
- package/client/lib/WorkspaceLayoutContext.tsx +92 -0
- package/client/lib/cn.ts +6 -0
- package/client/lib/grid-pack.ts +35 -0
- package/client/lib/group-turns.ts +44 -0
- package/client/lib/mcp-icons.ts +53 -0
- package/client/lib/ws.ts +129 -0
- package/client/main.tsx +23 -0
- package/client/store/chat.tsx +162 -0
- package/client/store/ui.ts +21 -0
- package/client/theme.css +66 -0
- package/lib/claude-adapter.ts +602 -0
- package/lib/format.ts +212 -0
- package/lib/themes.ts +89 -0
- package/lib/types.ts +144 -0
- package/package.json +117 -0
- package/server/CLAUDE.md +6 -0
- package/server/agent.ts +132 -0
- package/server/build-widget.ts +343 -0
- package/server/cli.ts +632 -0
- package/server/constants.ts +3 -0
- package/server/control.ts +98 -0
- package/server/functions-worker.ts +89 -0
- package/server/functions.ts +171 -0
- package/server/layout.ts +83 -0
- package/server/openclaw-adapter.ts +242 -0
- package/server/openclaw-gateway.ts +255 -0
- package/server/openclaw-session.ts +485 -0
- package/server/openclaw-strip.ts +134 -0
- package/server/openclaw.ts +206 -0
- package/server/registry.ts +150 -0
- package/server/skills-template.ts +21 -0
- package/server/state.ts +87 -0
- package/server/theme.ts +55 -0
- package/server/web.ts +321 -0
- package/server/widgets.ts +254 -0
- package/tsconfig.json +21 -0
- package/workspace/.claude/skills/widgets/DESIGN.md +630 -0
- package/workspace/.claude/skills/widgets/SKILL.md +176 -0
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Part,
|
|
3
|
+
ResultSummary,
|
|
4
|
+
SessionSnapshot,
|
|
5
|
+
StreamEvent,
|
|
6
|
+
SubagentRecord,
|
|
7
|
+
SystemNotice,
|
|
8
|
+
ToolCall,
|
|
9
|
+
ToolCaller,
|
|
10
|
+
Turn,
|
|
11
|
+
TurnOrigin
|
|
12
|
+
} from './format'
|
|
13
|
+
|
|
14
|
+
// Loose SDK message shape — we don't pull in the full SDK type tree; the
|
|
15
|
+
// adapter tolerates missing fields. Reference: dev/sdk-message-spec.md.
|
|
16
|
+
type ContentBlock = {
|
|
17
|
+
type: string
|
|
18
|
+
text?: string
|
|
19
|
+
thinking?: string
|
|
20
|
+
signature?: string
|
|
21
|
+
data?: string
|
|
22
|
+
name?: string
|
|
23
|
+
id?: string
|
|
24
|
+
input?: Record<string, unknown>
|
|
25
|
+
tool_use_id?: string
|
|
26
|
+
content?: unknown
|
|
27
|
+
is_error?: boolean
|
|
28
|
+
server_name?: string
|
|
29
|
+
url?: string
|
|
30
|
+
title?: string
|
|
31
|
+
media_type?: string
|
|
32
|
+
filename?: string
|
|
33
|
+
source_id?: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type SdkMessage = {
|
|
37
|
+
type: string
|
|
38
|
+
subtype?: string
|
|
39
|
+
message?: { role?: string; content?: unknown }
|
|
40
|
+
parent_tool_use_id?: string | null
|
|
41
|
+
tool_use_id?: string
|
|
42
|
+
isSynthetic?: boolean
|
|
43
|
+
isReplay?: boolean
|
|
44
|
+
tool_use_result?: unknown
|
|
45
|
+
uuid?: string
|
|
46
|
+
session_id?: string
|
|
47
|
+
timestamp?: string
|
|
48
|
+
tools?: string[]
|
|
49
|
+
mcp_servers?: { name: string; status: string }[]
|
|
50
|
+
model?: string
|
|
51
|
+
permissionMode?: string
|
|
52
|
+
cwd?: string
|
|
53
|
+
plugins?: { name: string; path: string }[]
|
|
54
|
+
skills?: string[]
|
|
55
|
+
slash_commands?: string[]
|
|
56
|
+
agents?: string[]
|
|
57
|
+
task_id?: string
|
|
58
|
+
description?: string
|
|
59
|
+
status?: string
|
|
60
|
+
usage?: { total_tokens?: number; tool_uses?: number; duration_ms?: number }
|
|
61
|
+
hook_id?: string
|
|
62
|
+
hook_name?: string
|
|
63
|
+
hook_event?: string
|
|
64
|
+
stdout?: string
|
|
65
|
+
stderr?: string
|
|
66
|
+
output?: string
|
|
67
|
+
exit_code?: number
|
|
68
|
+
outcome?: string
|
|
69
|
+
attempt?: number
|
|
70
|
+
max_retries?: number
|
|
71
|
+
retry_delay_ms?: number
|
|
72
|
+
error?: unknown
|
|
73
|
+
compact_metadata?: unknown
|
|
74
|
+
files?: { filename: string; file_id?: string; error?: string }[]
|
|
75
|
+
failed?: { filename: string; error: string }[]
|
|
76
|
+
mcp_server_name?: string
|
|
77
|
+
elicitation_id?: string
|
|
78
|
+
rate_limit_info?: unknown
|
|
79
|
+
duration_ms?: number
|
|
80
|
+
num_turns?: number
|
|
81
|
+
total_cost_usd?: number
|
|
82
|
+
state?: string
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function contentAsArray(content: unknown): ContentBlock[] {
|
|
86
|
+
if (Array.isArray(content)) return content as ContentBlock[]
|
|
87
|
+
if (typeof content === 'string') return [{ type: 'text', text: content }]
|
|
88
|
+
return []
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function blockOutputText(block: ContentBlock, max = 4000): string {
|
|
92
|
+
if (typeof block.content === 'string') return block.content.slice(0, max)
|
|
93
|
+
if (Array.isArray(block.content)) {
|
|
94
|
+
return (block.content as ContentBlock[])
|
|
95
|
+
.filter(b => b.type === 'text')
|
|
96
|
+
.map(b => b.text ?? '')
|
|
97
|
+
.join('\n')
|
|
98
|
+
.slice(0, max)
|
|
99
|
+
}
|
|
100
|
+
return ''
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function callerFromBlockType(type: string, name: string): ToolCaller {
|
|
104
|
+
if (name === 'Agent') return 'subagent'
|
|
105
|
+
if (type === 'server_tool_use') return 'server-tool'
|
|
106
|
+
if (type === 'mcp_tool_use') return 'mcp'
|
|
107
|
+
return 'model'
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function classifySyntheticReason(
|
|
111
|
+
firstText: string
|
|
112
|
+
): Exclude<Extract<TurnOrigin, { kind: 'synthetic' }>['reason'], never> {
|
|
113
|
+
if (/<system-reminder>/i.test(firstText)) return 'system-reminder'
|
|
114
|
+
return 'other'
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let _noticeSeq = 0
|
|
118
|
+
function noticeId(prefix: string): string {
|
|
119
|
+
_noticeSeq++
|
|
120
|
+
return `${prefix}:${Date.now()}:${_noticeSeq}`
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function turnId(msg: SdkMessage): string {
|
|
124
|
+
if (msg.uuid) return msg.uuid
|
|
125
|
+
return `fallback:${Math.random().toString(36).slice(2)}`
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export class ClaudeAdapter {
|
|
129
|
+
private turns: Turn[] = []
|
|
130
|
+
// toolCallId → the Turn that contains the tool call + its position
|
|
131
|
+
private toolIndex = new Map<string, { turn: Turn; partIndex: number }>()
|
|
132
|
+
// toolCallId of an Agent (subagent) tool_use → top-level Turn that owns it
|
|
133
|
+
private subagentOwners = new Map<string, Turn>()
|
|
134
|
+
private snapshot?: SessionSnapshot
|
|
135
|
+
// Client-chosen id to use for the next matching user-input echo, so the
|
|
136
|
+
// optimistic turn the client already rendered gets upserted instead of
|
|
137
|
+
// duplicated. Cleared once consumed.
|
|
138
|
+
private pendingUserEcho: { id: string; text: string } | null = null
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Tell the adapter that a user input with this text has already been
|
|
142
|
+
* emitted optimistically by the client under `id`. The next time the SDK
|
|
143
|
+
* echoes a matching plain user message, the adapter will emit it with
|
|
144
|
+
* `id` instead of the SDK's own uuid.
|
|
145
|
+
*/
|
|
146
|
+
expectUserEcho(id: string, text: string) {
|
|
147
|
+
this.pendingUserEcho = { id, text: text.trim() }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
ingest(raw: unknown): StreamEvent[] {
|
|
151
|
+
const msg = raw as SdkMessage
|
|
152
|
+
const events: StreamEvent[] = []
|
|
153
|
+
|
|
154
|
+
// --- system/init → SessionSnapshot ------------------------------------
|
|
155
|
+
if (msg.type === 'system' && msg.subtype === 'init') {
|
|
156
|
+
this.snapshot = {
|
|
157
|
+
sessionId: msg.session_id ?? '',
|
|
158
|
+
model: msg.model,
|
|
159
|
+
cwd: msg.cwd,
|
|
160
|
+
permissionMode: msg.permissionMode,
|
|
161
|
+
tools: msg.tools ?? [],
|
|
162
|
+
mcpServers: msg.mcp_servers ?? [],
|
|
163
|
+
plugins: msg.plugins ?? [],
|
|
164
|
+
skills: msg.skills ?? [],
|
|
165
|
+
slashCommands: msg.slash_commands ?? [],
|
|
166
|
+
agents: msg.agents ?? [],
|
|
167
|
+
updatedAt: new Date().toISOString()
|
|
168
|
+
}
|
|
169
|
+
events.push({ kind: 'snapshot', snapshot: this.snapshot })
|
|
170
|
+
return events
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// --- task_* → SubagentRecord mutations --------------------------------
|
|
174
|
+
if (msg.type === 'system' && msg.subtype === 'task_started') {
|
|
175
|
+
return this.onTaskStarted(msg)
|
|
176
|
+
}
|
|
177
|
+
if (msg.type === 'system' && msg.subtype === 'task_progress') {
|
|
178
|
+
return this.onTaskProgress(msg)
|
|
179
|
+
}
|
|
180
|
+
if (msg.type === 'system' && msg.subtype === 'task_notification') {
|
|
181
|
+
return this.onTaskNotification(msg)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// --- other system subtypes → SystemNotice -----------------------------
|
|
185
|
+
if (msg.type === 'system') {
|
|
186
|
+
const notice = this.buildSystemNotice(msg)
|
|
187
|
+
if (notice) events.push({ kind: 'notice', notice })
|
|
188
|
+
return events
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (msg.type === 'rate_limit_event') {
|
|
192
|
+
events.push({
|
|
193
|
+
kind: 'notice',
|
|
194
|
+
notice: {
|
|
195
|
+
id: noticeId('rate-limit'),
|
|
196
|
+
kind: 'rate-limit',
|
|
197
|
+
at: new Date().toISOString(),
|
|
198
|
+
info: msg.rate_limit_info
|
|
199
|
+
}
|
|
200
|
+
})
|
|
201
|
+
return events
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// --- assistant / user → Turn (+ tool-result merges) -------------------
|
|
205
|
+
if (msg.type === 'assistant' || msg.type === 'user') {
|
|
206
|
+
return this.ingestConversationMessage(msg)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// --- result -----------------------------------------------------------
|
|
210
|
+
if (msg.type === 'result') {
|
|
211
|
+
const subtype = (msg.subtype ?? 'success') as ResultSummary['subtype']
|
|
212
|
+
events.push({
|
|
213
|
+
kind: 'result',
|
|
214
|
+
result: {
|
|
215
|
+
subtype,
|
|
216
|
+
cost: msg.total_cost_usd,
|
|
217
|
+
turns: msg.num_turns,
|
|
218
|
+
durationMs: msg.duration_ms
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
return events
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// stream_event, tool_progress, tool_use_summary, auth_status,
|
|
225
|
+
// prompt_suggestion — dropped for display-only mode.
|
|
226
|
+
return events
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Returns the current snapshot + turns + notices + result as events — used
|
|
230
|
+
// when a new client connects and needs to rebuild the view from scratch.
|
|
231
|
+
hello(): StreamEvent[] {
|
|
232
|
+
const out: StreamEvent[] = []
|
|
233
|
+
if (this.snapshot) out.push({ kind: 'snapshot', snapshot: this.snapshot })
|
|
234
|
+
for (const t of this.turns) out.push({ kind: 'turn', turn: t })
|
|
235
|
+
return out
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// -----------------------------------------------------------------------
|
|
239
|
+
// Conversation messages (assistant/user)
|
|
240
|
+
// -----------------------------------------------------------------------
|
|
241
|
+
|
|
242
|
+
private ingestConversationMessage(msg: SdkMessage): StreamEvent[] {
|
|
243
|
+
const events: StreamEvent[] = []
|
|
244
|
+
const blocks = contentAsArray(msg.message?.content)
|
|
245
|
+
if (blocks.length === 0) return events
|
|
246
|
+
|
|
247
|
+
// Step 1: split the blocks. tool_result blocks merge into existing calls
|
|
248
|
+
// rather than producing a new turn.
|
|
249
|
+
const toolReturns: { toolCallId: string; output: string; isError: boolean }[] = []
|
|
250
|
+
const turnBlocks: ContentBlock[] = []
|
|
251
|
+
for (const b of blocks) {
|
|
252
|
+
if (b.type === 'tool_result' && b.tool_use_id) {
|
|
253
|
+
toolReturns.push({
|
|
254
|
+
toolCallId: b.tool_use_id,
|
|
255
|
+
output: blockOutputText(b),
|
|
256
|
+
isError: !!b.is_error
|
|
257
|
+
})
|
|
258
|
+
} else {
|
|
259
|
+
turnBlocks.push(b)
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Apply tool returns. Tracks which owner Turns changed so we can re-emit.
|
|
264
|
+
const affected = new Set<Turn>()
|
|
265
|
+
for (const tr of toolReturns) {
|
|
266
|
+
const owner = this.applyToolReturn(tr.toolCallId, tr.output, tr.isError)
|
|
267
|
+
if (owner) affected.add(owner)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Step 2: build a Turn from the remaining blocks (text / thinking /
|
|
271
|
+
// tool_use / files). If nothing's left, we only have tool returns.
|
|
272
|
+
const turn = this.buildTurn(msg, turnBlocks)
|
|
273
|
+
|
|
274
|
+
const parentId = msg.parent_tool_use_id ?? undefined
|
|
275
|
+
|
|
276
|
+
if (turn) {
|
|
277
|
+
if (parentId && this.subagentOwners.has(parentId)) {
|
|
278
|
+
// Nested turn inside a subagent transcript
|
|
279
|
+
const owner = this.subagentOwners.get(parentId)!
|
|
280
|
+
const sub = findSubagentCall(owner, parentId)?.subagent
|
|
281
|
+
if (sub) {
|
|
282
|
+
sub.transcript.push(turn)
|
|
283
|
+
this.registerToolCalls(turn)
|
|
284
|
+
affected.add(owner)
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
// Top-level turn
|
|
288
|
+
this.registerToolCalls(turn)
|
|
289
|
+
this.turns.push(turn)
|
|
290
|
+
affected.add(turn)
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
for (const t of affected) events.push({ kind: 'turn', turn: t })
|
|
295
|
+
return events
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private buildTurn(msg: SdkMessage, blocks: ContentBlock[]): Turn | undefined {
|
|
299
|
+
const parts: Part[] = []
|
|
300
|
+
let firstText = ''
|
|
301
|
+
|
|
302
|
+
for (const b of blocks) {
|
|
303
|
+
switch (b.type) {
|
|
304
|
+
case 'text':
|
|
305
|
+
if (b.text) {
|
|
306
|
+
parts.push({ type: 'text', text: b.text })
|
|
307
|
+
if (!firstText) firstText = b.text
|
|
308
|
+
}
|
|
309
|
+
break
|
|
310
|
+
case 'thinking':
|
|
311
|
+
if (b.thinking)
|
|
312
|
+
parts.push({ type: 'reasoning', text: b.thinking, signature: b.signature })
|
|
313
|
+
break
|
|
314
|
+
case 'redacted_thinking':
|
|
315
|
+
parts.push({ type: 'reasoning', text: '', redacted: true, signature: b.signature })
|
|
316
|
+
break
|
|
317
|
+
case 'tool_use':
|
|
318
|
+
case 'server_tool_use':
|
|
319
|
+
case 'mcp_tool_use': {
|
|
320
|
+
if (!b.id || !b.name) break
|
|
321
|
+
const call: ToolCall = {
|
|
322
|
+
toolCallId: b.id,
|
|
323
|
+
name: b.name,
|
|
324
|
+
caller: callerFromBlockType(b.type, b.name),
|
|
325
|
+
provider: 'claude-code',
|
|
326
|
+
mcpServer: b.type === 'mcp_tool_use' ? b.server_name : undefined,
|
|
327
|
+
state: 'running',
|
|
328
|
+
input: b.input ?? {}
|
|
329
|
+
}
|
|
330
|
+
if (b.name === 'Skill') {
|
|
331
|
+
const input = (b.input ?? {}) as { skill?: string }
|
|
332
|
+
call.skill = { skillName: input.skill ?? '' }
|
|
333
|
+
}
|
|
334
|
+
parts.push({ type: 'tool-call', call })
|
|
335
|
+
break
|
|
336
|
+
}
|
|
337
|
+
case 'file':
|
|
338
|
+
case 'image':
|
|
339
|
+
case 'document':
|
|
340
|
+
if (b.url) {
|
|
341
|
+
parts.push({
|
|
342
|
+
type: 'file',
|
|
343
|
+
mediaType: b.media_type ?? b.type,
|
|
344
|
+
url: b.url,
|
|
345
|
+
filename: b.filename
|
|
346
|
+
})
|
|
347
|
+
}
|
|
348
|
+
break
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (parts.length === 0) return undefined
|
|
353
|
+
|
|
354
|
+
const role: 'user' | 'assistant' = msg.type === 'user' ? 'user' : 'assistant'
|
|
355
|
+
let origin: TurnOrigin
|
|
356
|
+
if (role === 'assistant') {
|
|
357
|
+
origin = { kind: 'user-input' }
|
|
358
|
+
} else if (msg.isReplay) {
|
|
359
|
+
origin = { kind: 'replay' }
|
|
360
|
+
} else if (msg.isSynthetic) {
|
|
361
|
+
origin = { kind: 'synthetic', reason: classifySyntheticReason(firstText) }
|
|
362
|
+
} else if (msg.parent_tool_use_id) {
|
|
363
|
+
origin = { kind: 'subagent-prompt', parentToolCallId: msg.parent_tool_use_id }
|
|
364
|
+
} else {
|
|
365
|
+
origin = { kind: 'user-input' }
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// If a skill-invoking tool call and this is the synthetic SKILL.md body
|
|
369
|
+
// that followed, stash it on the owning Skill tool call rather than
|
|
370
|
+
// surfacing a separate synthetic turn in the scroll.
|
|
371
|
+
if (role === 'user' && msg.isSynthetic && firstText) {
|
|
372
|
+
const skillOwner = this.findRecentSkillCall()
|
|
373
|
+
if (skillOwner && !skillOwner.skill?.body) {
|
|
374
|
+
skillOwner.skill = { ...(skillOwner.skill ?? { skillName: '' }), body: firstText }
|
|
375
|
+
// Don't emit a standalone turn — return undefined so the caller
|
|
376
|
+
// simply re-emits the owning turn.
|
|
377
|
+
return undefined
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// If this is the user echo of an optimistic send, reuse the client-chosen
|
|
382
|
+
// id so the optimistic bubble upserts in place.
|
|
383
|
+
let id = turnId(msg)
|
|
384
|
+
if (
|
|
385
|
+
role === 'user' &&
|
|
386
|
+
origin.kind === 'user-input' &&
|
|
387
|
+
this.pendingUserEcho &&
|
|
388
|
+
firstText.trim() === this.pendingUserEcho.text
|
|
389
|
+
) {
|
|
390
|
+
id = this.pendingUserEcho.id
|
|
391
|
+
this.pendingUserEcho = null
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return {
|
|
395
|
+
id,
|
|
396
|
+
role,
|
|
397
|
+
origin,
|
|
398
|
+
parentTaskId: msg.parent_tool_use_id ?? undefined,
|
|
399
|
+
parts,
|
|
400
|
+
timestamp: msg.timestamp
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
private findRecentSkillCall(): ToolCall | undefined {
|
|
405
|
+
for (let i = this.turns.length - 1; i >= 0; i--) {
|
|
406
|
+
const t = this.turns[i]
|
|
407
|
+
for (let j = t.parts.length - 1; j >= 0; j--) {
|
|
408
|
+
const p = t.parts[j]
|
|
409
|
+
if (p.type === 'tool-call' && p.call.name === 'Skill' && p.call.skill) return p.call
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return undefined
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
private applyToolReturn(toolCallId: string, output: string, isError: boolean): Turn | undefined {
|
|
416
|
+
const loc = this.toolIndex.get(toolCallId)
|
|
417
|
+
if (!loc) return undefined
|
|
418
|
+
const part = loc.turn.parts[loc.partIndex]
|
|
419
|
+
if (part.type !== 'tool-call') return undefined
|
|
420
|
+
part.call.state = isError ? 'error' : 'success'
|
|
421
|
+
if (isError) part.call.errorText = output
|
|
422
|
+
else part.call.output = output
|
|
423
|
+
// Walk up from nested transcripts to the top-level owning turn.
|
|
424
|
+
return this.topLevelOwnerOf(loc.turn)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
private topLevelOwnerOf(turn: Turn): Turn {
|
|
428
|
+
if (this.turns.includes(turn)) return turn
|
|
429
|
+
for (const [, owner] of this.subagentOwners) {
|
|
430
|
+
for (const p of owner.parts) {
|
|
431
|
+
if (
|
|
432
|
+
p.type === 'tool-call' &&
|
|
433
|
+
p.call.subagent?.transcript.some(t => deepContains(t, turn))
|
|
434
|
+
) {
|
|
435
|
+
return owner
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return turn
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
private registerToolCalls(turn: Turn) {
|
|
443
|
+
for (let i = 0; i < turn.parts.length; i++) {
|
|
444
|
+
const p = turn.parts[i]
|
|
445
|
+
if (p.type !== 'tool-call') continue
|
|
446
|
+
this.toolIndex.set(p.call.toolCallId, { turn, partIndex: i })
|
|
447
|
+
if (p.call.caller === 'subagent') this.subagentOwners.set(p.call.toolCallId, turn)
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// -----------------------------------------------------------------------
|
|
452
|
+
// Task (subagent) events
|
|
453
|
+
// -----------------------------------------------------------------------
|
|
454
|
+
|
|
455
|
+
private onTaskStarted(msg: SdkMessage): StreamEvent[] {
|
|
456
|
+
const toolCallId = msg.tool_use_id
|
|
457
|
+
if (!toolCallId) return []
|
|
458
|
+
const owner = this.subagentOwners.get(toolCallId)
|
|
459
|
+
if (!owner) return []
|
|
460
|
+
const call = findToolCall(owner, toolCallId)
|
|
461
|
+
if (!call) return []
|
|
462
|
+
call.subagent = {
|
|
463
|
+
taskId: msg.task_id ?? '',
|
|
464
|
+
description: msg.description ?? '',
|
|
465
|
+
progress: [],
|
|
466
|
+
status: 'running',
|
|
467
|
+
transcript: []
|
|
468
|
+
}
|
|
469
|
+
call.state = 'running'
|
|
470
|
+
return [{ kind: 'turn', turn: owner }]
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
private onTaskProgress(msg: SdkMessage): StreamEvent[] {
|
|
474
|
+
const toolCallId = msg.tool_use_id
|
|
475
|
+
if (!toolCallId) return []
|
|
476
|
+
const owner = this.subagentOwners.get(toolCallId)
|
|
477
|
+
if (!owner) return []
|
|
478
|
+
const call = findToolCall(owner, toolCallId)
|
|
479
|
+
if (!call?.subagent) return []
|
|
480
|
+
if (msg.description) call.subagent.progress.push(msg.description)
|
|
481
|
+
if (msg.usage) {
|
|
482
|
+
call.subagent.usage = {
|
|
483
|
+
totalTokens: msg.usage.total_tokens,
|
|
484
|
+
toolUses: msg.usage.tool_uses,
|
|
485
|
+
durationMs: msg.usage.duration_ms
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return [{ kind: 'turn', turn: owner }]
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
private onTaskNotification(msg: SdkMessage): StreamEvent[] {
|
|
492
|
+
const toolCallId = msg.tool_use_id
|
|
493
|
+
if (!toolCallId) return []
|
|
494
|
+
const owner = this.subagentOwners.get(toolCallId)
|
|
495
|
+
if (!owner) return []
|
|
496
|
+
const call = findToolCall(owner, toolCallId)
|
|
497
|
+
if (!call?.subagent) return []
|
|
498
|
+
const status = msg.status as SubagentRecord['status']
|
|
499
|
+
if (status) call.subagent.status = status
|
|
500
|
+
return [{ kind: 'turn', turn: owner }]
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// -----------------------------------------------------------------------
|
|
504
|
+
// Notices
|
|
505
|
+
// -----------------------------------------------------------------------
|
|
506
|
+
|
|
507
|
+
private buildSystemNotice(msg: SdkMessage): SystemNotice | undefined {
|
|
508
|
+
const at = new Date().toISOString()
|
|
509
|
+
switch (msg.subtype) {
|
|
510
|
+
case 'session_state_changed':
|
|
511
|
+
return {
|
|
512
|
+
id: noticeId('session-state'),
|
|
513
|
+
kind: 'session-state',
|
|
514
|
+
at,
|
|
515
|
+
state: (msg.state as 'idle' | 'running' | 'requires-action') ?? 'idle'
|
|
516
|
+
}
|
|
517
|
+
case 'compact_boundary':
|
|
518
|
+
return { id: noticeId('compact'), kind: 'compact', at, metadata: msg.compact_metadata }
|
|
519
|
+
case 'api_retry':
|
|
520
|
+
return {
|
|
521
|
+
id: noticeId('api-retry'),
|
|
522
|
+
kind: 'api-retry',
|
|
523
|
+
at,
|
|
524
|
+
attempt: msg.attempt ?? 0,
|
|
525
|
+
maxRetries: msg.max_retries ?? 0,
|
|
526
|
+
delayMs: msg.retry_delay_ms ?? 0,
|
|
527
|
+
error: typeof msg.error === 'string' ? msg.error : undefined
|
|
528
|
+
}
|
|
529
|
+
case 'hook_started':
|
|
530
|
+
case 'hook_progress':
|
|
531
|
+
case 'hook_response':
|
|
532
|
+
return {
|
|
533
|
+
id: noticeId('hook'),
|
|
534
|
+
kind: 'hook',
|
|
535
|
+
at,
|
|
536
|
+
hookId: msg.hook_id ?? '',
|
|
537
|
+
hookName: msg.hook_name ?? '',
|
|
538
|
+
event: msg.hook_event ?? '',
|
|
539
|
+
status:
|
|
540
|
+
msg.subtype === 'hook_started'
|
|
541
|
+
? 'started'
|
|
542
|
+
: msg.subtype === 'hook_progress'
|
|
543
|
+
? 'progress'
|
|
544
|
+
: 'response',
|
|
545
|
+
output: msg.output ?? msg.stdout,
|
|
546
|
+
exitCode: msg.exit_code,
|
|
547
|
+
outcome: msg.outcome as 'success' | 'error' | 'cancelled' | undefined
|
|
548
|
+
}
|
|
549
|
+
case 'files_persisted':
|
|
550
|
+
return {
|
|
551
|
+
id: noticeId('files-persisted'),
|
|
552
|
+
kind: 'files-persisted',
|
|
553
|
+
at,
|
|
554
|
+
files: (msg.files ?? []).map(f => f.filename),
|
|
555
|
+
failed: msg.failed ?? []
|
|
556
|
+
}
|
|
557
|
+
case 'elicitation_complete':
|
|
558
|
+
return {
|
|
559
|
+
id: noticeId('elicitation'),
|
|
560
|
+
kind: 'elicitation',
|
|
561
|
+
at,
|
|
562
|
+
server: msg.mcp_server_name ?? '',
|
|
563
|
+
elicitationId: msg.elicitation_id ?? ''
|
|
564
|
+
}
|
|
565
|
+
default:
|
|
566
|
+
return undefined
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
function findToolCall(turn: Turn, toolCallId: string): ToolCall | undefined {
|
|
572
|
+
for (const p of turn.parts) {
|
|
573
|
+
if (p.type !== 'tool-call') continue
|
|
574
|
+
if (p.call.toolCallId === toolCallId) return p.call
|
|
575
|
+
if (p.call.subagent) {
|
|
576
|
+
for (const t of p.call.subagent.transcript) {
|
|
577
|
+
const nested = findToolCall(t, toolCallId)
|
|
578
|
+
if (nested) return nested
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return undefined
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
function findSubagentCall(owner: Turn, toolCallId: string): ToolCall | undefined {
|
|
586
|
+
for (const p of owner.parts) {
|
|
587
|
+
if (p.type === 'tool-call' && p.call.toolCallId === toolCallId) return p.call
|
|
588
|
+
}
|
|
589
|
+
return undefined
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function deepContains(haystack: Turn, needle: Turn): boolean {
|
|
593
|
+
if (haystack === needle) return true
|
|
594
|
+
for (const p of haystack.parts) {
|
|
595
|
+
if (p.type === 'tool-call' && p.call.subagent) {
|
|
596
|
+
for (const t of p.call.subagent.transcript) {
|
|
597
|
+
if (deepContains(t, needle)) return true
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
return false
|
|
602
|
+
}
|