@myrialabs/clopen 0.2.24 → 0.3.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/README.md +1 -1
- package/backend/chat/helpers.ts +2 -20
- package/backend/chat/stream-manager.ts +556 -547
- package/backend/database/migrations/029_migrate_messages_to_unified.ts +1108 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/engine-queries.ts +195 -36
- package/backend/database/queries/index.ts +1 -2
- package/backend/database/queries/message-queries.ts +84 -139
- package/backend/database/queries/session-queries.ts +167 -21
- package/backend/database/queries/snapshot-queries.ts +5 -4
- package/backend/database/queries/utils-queries.ts +2 -2
- package/backend/engine/adapters/claude/environment.ts +4 -5
- package/backend/engine/adapters/claude/message-converter.ts +552 -0
- package/backend/engine/adapters/claude/stream.ts +59 -47
- package/backend/engine/adapters/opencode/config.ts +7 -7
- package/backend/engine/adapters/opencode/message-converter.ts +328 -352
- package/backend/engine/adapters/opencode/stream.ts +84 -101
- package/backend/engine/types.ts +17 -11
- package/backend/project/status-manager.ts +10 -13
- package/backend/snapshot/helpers.ts +46 -90
- package/backend/snapshot/snapshot-service.ts +1 -1
- package/backend/utils/paths.ts +22 -1
- package/backend/ws/chat/index.ts +1 -11
- package/backend/ws/chat/stream.ts +101 -61
- package/backend/ws/engine/claude/accounts.ts +27 -7
- package/backend/ws/engine/claude/status.ts +3 -2
- package/backend/ws/engine/opencode/providers.ts +25 -24
- package/backend/ws/git/commit-message.ts +6 -8
- package/backend/ws/messages/crud.ts +4 -59
- package/backend/ws/sessions/crud.ts +78 -130
- package/backend/ws/sessions/index.ts +1 -9
- package/backend/ws/settings/crud.ts +14 -11
- package/backend/ws/snapshot/restore.ts +23 -26
- package/backend/ws/snapshot/timeline.ts +5 -5
- package/dist/assets/{index-w7Uwi2Jz.js → index-BVWBZlgD.js} +2 -2
- package/dist/assets/main-Cgbs6dR7.js +246 -0
- package/dist/assets/main-TEa2MRnh.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +4 -4
- package/shared/constants/engines.ts +141 -56
- package/shared/types/database/schema.ts +39 -40
- package/shared/types/stores/settings.ts +8 -4
- package/shared/types/unified/common.ts +21 -0
- package/shared/types/unified/display.ts +25 -0
- package/shared/types/unified/engine.ts +61 -0
- package/shared/types/unified/index.ts +130 -0
- package/shared/types/unified/message.ts +151 -0
- package/shared/types/unified/stream.ts +157 -0
- package/shared/types/unified/tool.ts +409 -0
- package/shared/utils/message-formatter.ts +9 -73
- package/shared/utils/path.ts +3 -2
- package/tsconfig.json +1 -1
- package/backend/database/queries/opencode-provider-queries.ts +0 -190
- package/backend/engine/adapters/claude/path-utils.ts +0 -38
- package/backend/ws/chat/background.ts +0 -201
- package/dist/assets/main-B4q8wX9B.js +0 -257
- package/dist/assets/main-Bfz5Urp3.css +0 -1
- package/shared/types/engine/index.ts +0 -45
- package/shared/types/messaging/index.ts +0 -242
- package/shared/types/messaging/tool.ts +0 -374
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UnifiedMessage — the discriminated union of all persistent message types.
|
|
3
|
+
*
|
|
4
|
+
* All messages extend MessageBase which carries identity, session,
|
|
5
|
+
* and context fields at the root level.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { TokenUsage, StopReason } from './common';
|
|
9
|
+
import type { ToolUseBlock, ToolResult } from './tool';
|
|
10
|
+
|
|
11
|
+
// ============================================================
|
|
12
|
+
// Message Base
|
|
13
|
+
// ============================================================
|
|
14
|
+
|
|
15
|
+
/** Sender identity attached to every message */
|
|
16
|
+
export interface MessageSender {
|
|
17
|
+
id: string;
|
|
18
|
+
name: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Engine account identity attached to every message */
|
|
22
|
+
export interface MessageAccount {
|
|
23
|
+
id: number;
|
|
24
|
+
name: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Parent references carried by every message */
|
|
28
|
+
export interface MessageParent {
|
|
29
|
+
messageId: string | null;
|
|
30
|
+
sessionId: string | null;
|
|
31
|
+
toolUseId: string | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Model identity attached to every message */
|
|
35
|
+
export interface MessageModel {
|
|
36
|
+
id: string;
|
|
37
|
+
name: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Engine context attached to every message */
|
|
41
|
+
export interface MessageEngine {
|
|
42
|
+
type: 'claude-code' | 'opencode';
|
|
43
|
+
provider: string;
|
|
44
|
+
model: MessageModel;
|
|
45
|
+
account: MessageAccount;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Common fields present on every message */
|
|
49
|
+
export interface MessageBase {
|
|
50
|
+
createdAt: string;
|
|
51
|
+
messageId: string;
|
|
52
|
+
/** SDK-issued session ID. Null for messages that have no SDK session context
|
|
53
|
+
* (e.g. user messages from the frontend, or messages where the SDK did not provide one). */
|
|
54
|
+
sessionId: string | null;
|
|
55
|
+
parent: MessageParent;
|
|
56
|
+
engine: MessageEngine;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ============================================================
|
|
60
|
+
// Content Blocks
|
|
61
|
+
// ============================================================
|
|
62
|
+
|
|
63
|
+
export interface TextBlock {
|
|
64
|
+
type: 'text';
|
|
65
|
+
text: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ImageBlock {
|
|
69
|
+
type: 'image';
|
|
70
|
+
mediaType: string;
|
|
71
|
+
data: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface DocumentBlock {
|
|
75
|
+
type: 'document';
|
|
76
|
+
mediaType: string;
|
|
77
|
+
data: string;
|
|
78
|
+
title: string | null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type UserContentBlock =
|
|
82
|
+
| TextBlock
|
|
83
|
+
| ToolResult
|
|
84
|
+
| ImageBlock
|
|
85
|
+
| DocumentBlock;
|
|
86
|
+
|
|
87
|
+
export type AssistantContentBlock =
|
|
88
|
+
| TextBlock
|
|
89
|
+
| ToolUseBlock;
|
|
90
|
+
|
|
91
|
+
// ============================================================
|
|
92
|
+
// Message Types
|
|
93
|
+
// ============================================================
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* User-submitted message.
|
|
97
|
+
* Contains text, file attachments, or tool execution results.
|
|
98
|
+
*/
|
|
99
|
+
export interface UserMessage extends MessageBase {
|
|
100
|
+
type: 'user';
|
|
101
|
+
sender: MessageSender;
|
|
102
|
+
content: UserContentBlock[];
|
|
103
|
+
synthetic: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* AI assistant response.
|
|
108
|
+
* Contains text and/or tool invocations.
|
|
109
|
+
*/
|
|
110
|
+
export interface AssistantMessage extends MessageBase {
|
|
111
|
+
type: 'assistant';
|
|
112
|
+
content: AssistantContentBlock[];
|
|
113
|
+
stopReason: StopReason | null;
|
|
114
|
+
usage: TokenUsage | null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Reasoning/thinking content from the model.
|
|
119
|
+
* Displayed as a separate collapsible bubble in the UI.
|
|
120
|
+
*/
|
|
121
|
+
export interface ReasoningMessage extends MessageBase {
|
|
122
|
+
type: 'reasoning';
|
|
123
|
+
text: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Context compaction boundary marker.
|
|
128
|
+
* Rendered as a visual separator when the engine compacts context.
|
|
129
|
+
*/
|
|
130
|
+
export interface CompactBoundaryMessage extends MessageBase {
|
|
131
|
+
type: 'compact_boundary';
|
|
132
|
+
trigger: 'manual' | 'auto';
|
|
133
|
+
preTokens: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ============================================================
|
|
137
|
+
// UnifiedMessage
|
|
138
|
+
// ============================================================
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Discriminated union of all persistent message types.
|
|
142
|
+
*
|
|
143
|
+
* This is the canonical format for DB storage, WebSocket transport,
|
|
144
|
+
* and frontend rendering. Engine adapters convert SDK-specific
|
|
145
|
+
* formats to this type.
|
|
146
|
+
*/
|
|
147
|
+
export type UnifiedMessage =
|
|
148
|
+
| UserMessage
|
|
149
|
+
| AssistantMessage
|
|
150
|
+
| ReasoningMessage
|
|
151
|
+
| CompactBoundaryMessage;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Engine output and streaming event types.
|
|
3
|
+
*
|
|
4
|
+
* Defines all events yielded by engine adapters during query execution.
|
|
5
|
+
* The stream-manager routes each event type:
|
|
6
|
+
* - Messages → save to DB + emit to WebSocket
|
|
7
|
+
* - Stream events → forward to frontend for live typing
|
|
8
|
+
* - System/result events → extract metadata, emit notifications
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { TokenUsage } from './common';
|
|
12
|
+
import type {
|
|
13
|
+
UserMessage,
|
|
14
|
+
AssistantMessage,
|
|
15
|
+
ReasoningMessage,
|
|
16
|
+
CompactBoundaryMessage,
|
|
17
|
+
UnifiedMessage,
|
|
18
|
+
MessageEngine,
|
|
19
|
+
MessageSender,
|
|
20
|
+
} from './message';
|
|
21
|
+
|
|
22
|
+
// ============================================================
|
|
23
|
+
// Stream Events (transient, not persisted)
|
|
24
|
+
// ============================================================
|
|
25
|
+
|
|
26
|
+
export interface TextDeltaEvent {
|
|
27
|
+
type: 'stream_event';
|
|
28
|
+
event: 'delta';
|
|
29
|
+
sessionId: string;
|
|
30
|
+
text: string;
|
|
31
|
+
reasoning: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface StreamLifecycleEvent {
|
|
35
|
+
type: 'stream_event';
|
|
36
|
+
event: 'start' | 'stop';
|
|
37
|
+
sessionId: string;
|
|
38
|
+
reasoning: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type StreamEvent =
|
|
42
|
+
| TextDeltaEvent
|
|
43
|
+
| StreamLifecycleEvent;
|
|
44
|
+
|
|
45
|
+
// ============================================================
|
|
46
|
+
// Result Events (transient, completion info)
|
|
47
|
+
// ============================================================
|
|
48
|
+
|
|
49
|
+
export interface SuccessResultEvent {
|
|
50
|
+
type: 'result';
|
|
51
|
+
subtype: 'success';
|
|
52
|
+
sessionId: string;
|
|
53
|
+
numTurns: number;
|
|
54
|
+
totalCostUsd: number;
|
|
55
|
+
usage: TokenUsage;
|
|
56
|
+
stopReason: string | null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ErrorResultEvent {
|
|
60
|
+
type: 'result';
|
|
61
|
+
subtype: 'error_max_turns' | 'error_during_execution' | 'error_max_budget';
|
|
62
|
+
sessionId: string;
|
|
63
|
+
errors: string[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type ResultEvent =
|
|
67
|
+
| SuccessResultEvent
|
|
68
|
+
| ErrorResultEvent;
|
|
69
|
+
|
|
70
|
+
// ============================================================
|
|
71
|
+
// System Events (transient)
|
|
72
|
+
// ============================================================
|
|
73
|
+
|
|
74
|
+
export interface SystemInitEvent {
|
|
75
|
+
type: 'system_init';
|
|
76
|
+
sessionId: string;
|
|
77
|
+
model: string;
|
|
78
|
+
engine: string;
|
|
79
|
+
tools: string[];
|
|
80
|
+
mcpServers: McpServerStatus[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface McpServerStatus {
|
|
84
|
+
name: string;
|
|
85
|
+
status: 'connected' | 'disconnected' | 'error';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface RateLimitEvent {
|
|
89
|
+
type: 'rate_limit';
|
|
90
|
+
sessionId: string;
|
|
91
|
+
status: 'rejected' | 'allowed_warning';
|
|
92
|
+
utilization: number;
|
|
93
|
+
resetsAt: number | null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ============================================================
|
|
97
|
+
// Engine Output
|
|
98
|
+
// ============================================================
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Union of all events yielded by engine adapters via AsyncGenerator.
|
|
102
|
+
* Stream-manager discriminates on `type` to route each event.
|
|
103
|
+
*/
|
|
104
|
+
export type EngineOutput =
|
|
105
|
+
| UserMessage
|
|
106
|
+
| AssistantMessage
|
|
107
|
+
| ReasoningMessage
|
|
108
|
+
| CompactBoundaryMessage
|
|
109
|
+
| StreamEvent
|
|
110
|
+
| ResultEvent
|
|
111
|
+
| SystemInitEvent
|
|
112
|
+
| RateLimitEvent;
|
|
113
|
+
|
|
114
|
+
// ============================================================
|
|
115
|
+
// Stream Transport (WebSocket layer)
|
|
116
|
+
// ============================================================
|
|
117
|
+
|
|
118
|
+
export type StreamEventType =
|
|
119
|
+
| 'connection'
|
|
120
|
+
| 'message'
|
|
121
|
+
| 'partial'
|
|
122
|
+
| 'notification'
|
|
123
|
+
| 'complete'
|
|
124
|
+
| 'error'
|
|
125
|
+
| 'cancelled';
|
|
126
|
+
|
|
127
|
+
export type StreamStatus = 'active' | 'completed' | 'error' | 'cancelled';
|
|
128
|
+
|
|
129
|
+
export interface StreamNotification {
|
|
130
|
+
type: 'info' | 'warning' | 'error';
|
|
131
|
+
title: string;
|
|
132
|
+
message: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface PartialMessageData {
|
|
136
|
+
processId: string;
|
|
137
|
+
eventType: 'start' | 'update' | 'end';
|
|
138
|
+
partialText: string;
|
|
139
|
+
deltaText: string;
|
|
140
|
+
reasoning: boolean;
|
|
141
|
+
timestamp: string;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface MessageTransportData {
|
|
145
|
+
processId: string;
|
|
146
|
+
message: UnifiedMessage;
|
|
147
|
+
usage?: TokenUsage;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface StreamRequest {
|
|
151
|
+
projectPath: string;
|
|
152
|
+
projectId: string;
|
|
153
|
+
chatSessionId: string;
|
|
154
|
+
prompt: UserMessage;
|
|
155
|
+
engine: MessageEngine;
|
|
156
|
+
sender: MessageSender;
|
|
157
|
+
}
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool types, tool input definitions, and ToolUseBlock.
|
|
3
|
+
*
|
|
4
|
+
* ToolUseBlock is the single canonical type for tool invocations —
|
|
5
|
+
* it includes both the invocation data and display enrichment fields
|
|
6
|
+
* (result, subActivities). When first created, enrichment fields are null/empty.
|
|
7
|
+
* The message grouper populates them for UI rendering.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// ============================================================
|
|
11
|
+
// Tool Input Types
|
|
12
|
+
// ============================================================
|
|
13
|
+
|
|
14
|
+
export interface BashInput {
|
|
15
|
+
command: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
timeout?: number;
|
|
18
|
+
runInBackground?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ReadInput {
|
|
22
|
+
filePath: string;
|
|
23
|
+
offset?: number;
|
|
24
|
+
limit?: number;
|
|
25
|
+
pages?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface EditInput {
|
|
29
|
+
filePath: string;
|
|
30
|
+
oldString: string;
|
|
31
|
+
newString: string;
|
|
32
|
+
replaceAll?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface WriteInput {
|
|
36
|
+
filePath: string;
|
|
37
|
+
content: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface GlobInput {
|
|
41
|
+
pattern: string;
|
|
42
|
+
path?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface GrepInput {
|
|
46
|
+
pattern: string;
|
|
47
|
+
path?: string;
|
|
48
|
+
glob?: string;
|
|
49
|
+
outputMode?: 'content' | 'files_with_matches' | 'count';
|
|
50
|
+
type?: string;
|
|
51
|
+
headLimit?: number;
|
|
52
|
+
offset?: number;
|
|
53
|
+
multiline?: boolean;
|
|
54
|
+
caseInsensitive?: boolean;
|
|
55
|
+
beforeContext?: number;
|
|
56
|
+
afterContext?: number;
|
|
57
|
+
context?: number;
|
|
58
|
+
lineNumbers?: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface WebFetchInput {
|
|
62
|
+
url: string;
|
|
63
|
+
prompt: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface WebSearchInput {
|
|
67
|
+
query: string;
|
|
68
|
+
allowedDomains?: string[];
|
|
69
|
+
blockedDomains?: string[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface AskUserQuestionInput {
|
|
73
|
+
questions: AskUserQuestion[];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface AskUserQuestion {
|
|
77
|
+
question: string;
|
|
78
|
+
header: string;
|
|
79
|
+
options: AskUserQuestionOption[];
|
|
80
|
+
multiSelect: boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface AskUserQuestionOption {
|
|
84
|
+
label: string;
|
|
85
|
+
description: string;
|
|
86
|
+
markdown?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface TodoWriteInput {
|
|
90
|
+
todos: TodoItem[];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface TodoItem {
|
|
94
|
+
content: string;
|
|
95
|
+
status: 'pending' | 'in_progress' | 'completed';
|
|
96
|
+
activeForm: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface AgentInput {
|
|
100
|
+
prompt: string;
|
|
101
|
+
description: string;
|
|
102
|
+
subagentType: string;
|
|
103
|
+
model?: string;
|
|
104
|
+
maxTurns?: number;
|
|
105
|
+
isolation?: 'worktree';
|
|
106
|
+
runInBackground?: boolean;
|
|
107
|
+
resume?: string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface TaskOutputInput {
|
|
111
|
+
taskId: string;
|
|
112
|
+
block?: boolean;
|
|
113
|
+
timeout?: number;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface TaskStopInput {
|
|
117
|
+
taskId: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface NotebookEditInput {
|
|
121
|
+
notebookPath: string;
|
|
122
|
+
newSource: string;
|
|
123
|
+
cellId?: string;
|
|
124
|
+
cellType?: 'code' | 'markdown';
|
|
125
|
+
editMode?: 'replace' | 'insert' | 'delete';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface ListMcpResourcesInput {
|
|
129
|
+
server?: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface ReadMcpResourceInput {
|
|
133
|
+
server: string;
|
|
134
|
+
uri: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface ConfigInput {
|
|
138
|
+
key: string;
|
|
139
|
+
value?: string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface EnterWorktreeInput {
|
|
143
|
+
name?: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface EnterPlanModeInput {}
|
|
147
|
+
|
|
148
|
+
export interface ExitPlanModeInput {
|
|
149
|
+
allowedPrompts?: ExitPlanModePrompt[];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface ExitPlanModePrompt {
|
|
153
|
+
tool: string;
|
|
154
|
+
prompt: string;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface SkillInput {
|
|
158
|
+
skill: string;
|
|
159
|
+
args?: string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface ExitWorktreeInput {
|
|
163
|
+
keep?: boolean;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface ToolSearchInput {
|
|
167
|
+
query: string;
|
|
168
|
+
maxResults?: number;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface ScheduleWakeupInput {
|
|
172
|
+
delaySeconds: number;
|
|
173
|
+
reason: string;
|
|
174
|
+
prompt: string;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface MonitorInput {
|
|
178
|
+
bashId?: string;
|
|
179
|
+
processId?: string;
|
|
180
|
+
source?: string;
|
|
181
|
+
until?: string;
|
|
182
|
+
timeout?: number;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface PushNotificationInput {
|
|
186
|
+
title: string;
|
|
187
|
+
message: string;
|
|
188
|
+
url?: string;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface RemoteTriggerInput {
|
|
192
|
+
name?: string;
|
|
193
|
+
payload?: Record<string, unknown>;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface CronCreateInput {
|
|
197
|
+
name: string;
|
|
198
|
+
schedule: string;
|
|
199
|
+
prompt: string;
|
|
200
|
+
description?: string;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export interface CronDeleteInput {
|
|
204
|
+
id: string;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface CronListInput {
|
|
208
|
+
filter?: string;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface PatchInput {
|
|
212
|
+
filePath: string;
|
|
213
|
+
patch: string;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface ListInput {
|
|
217
|
+
path?: string;
|
|
218
|
+
ignore?: string[];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface LspInput {
|
|
222
|
+
operation: string;
|
|
223
|
+
filePath?: string;
|
|
224
|
+
line?: number;
|
|
225
|
+
column?: number;
|
|
226
|
+
symbol?: string;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ============================================================
|
|
230
|
+
// Tool Input Map
|
|
231
|
+
// ============================================================
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Single source of truth for every canonical tool name accepted by the
|
|
235
|
+
* chat UI. Any tool emitted by an engine adapter MUST normalize to one
|
|
236
|
+
* of these names — otherwise the backend emits an `Unknown:*` marker
|
|
237
|
+
* and the UI renders it as an error (no silent fallback).
|
|
238
|
+
*/
|
|
239
|
+
export interface ToolInputMap {
|
|
240
|
+
// File operations
|
|
241
|
+
Bash: BashInput;
|
|
242
|
+
Read: ReadInput;
|
|
243
|
+
Edit: EditInput;
|
|
244
|
+
Write: WriteInput;
|
|
245
|
+
Patch: PatchInput;
|
|
246
|
+
NotebookEdit: NotebookEditInput;
|
|
247
|
+
// Discovery
|
|
248
|
+
Glob: GlobInput;
|
|
249
|
+
Grep: GrepInput;
|
|
250
|
+
List: ListInput;
|
|
251
|
+
// Web
|
|
252
|
+
WebFetch: WebFetchInput;
|
|
253
|
+
WebSearch: WebSearchInput;
|
|
254
|
+
// Planning & questions
|
|
255
|
+
TodoWrite: TodoWriteInput;
|
|
256
|
+
AskUserQuestion: AskUserQuestionInput;
|
|
257
|
+
EnterPlanMode: EnterPlanModeInput;
|
|
258
|
+
ExitPlanMode: ExitPlanModeInput;
|
|
259
|
+
// Sub-agents & tasks
|
|
260
|
+
Agent: AgentInput;
|
|
261
|
+
TaskOutput: TaskOutputInput;
|
|
262
|
+
TaskStop: TaskStopInput;
|
|
263
|
+
// MCP
|
|
264
|
+
ListMcpResources: ListMcpResourcesInput;
|
|
265
|
+
ReadMcpResource: ReadMcpResourceInput;
|
|
266
|
+
// Harness / workspace
|
|
267
|
+
Config: ConfigInput;
|
|
268
|
+
EnterWorktree: EnterWorktreeInput;
|
|
269
|
+
ExitWorktree: ExitWorktreeInput;
|
|
270
|
+
Skill: SkillInput;
|
|
271
|
+
ToolSearch: ToolSearchInput;
|
|
272
|
+
Lsp: LspInput;
|
|
273
|
+
// Automation & notifications
|
|
274
|
+
ScheduleWakeup: ScheduleWakeupInput;
|
|
275
|
+
Monitor: MonitorInput;
|
|
276
|
+
PushNotification: PushNotificationInput;
|
|
277
|
+
RemoteTrigger: RemoteTriggerInput;
|
|
278
|
+
CronCreate: CronCreateInput;
|
|
279
|
+
CronDelete: CronDeleteInput;
|
|
280
|
+
CronList: CronListInput;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export type KnownToolName = keyof ToolInputMap;
|
|
284
|
+
export type McpToolName = `mcp__${string}`;
|
|
285
|
+
/**
|
|
286
|
+
* Placeholder name for a tool emitted by an engine adapter that does not
|
|
287
|
+
* map to any canonical name. Adapters MUST wrap the original name (e.g.
|
|
288
|
+
* `Unknown:weird_tool`) so the UI can render a visible error instead of
|
|
289
|
+
* silently falling back to a generic box.
|
|
290
|
+
*/
|
|
291
|
+
export type UnknownToolName = `Unknown:${string}`;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Runtime set of every canonical tool name. Kept in sync with `ToolInputMap`.
|
|
295
|
+
* Use this in engine adapters to decide whether to emit the tool name as-is
|
|
296
|
+
* or wrap it with the `Unknown:` prefix.
|
|
297
|
+
*/
|
|
298
|
+
export const CANONICAL_TOOL_NAMES = new Set<KnownToolName>([
|
|
299
|
+
'Bash', 'Read', 'Edit', 'Write', 'Patch', 'NotebookEdit',
|
|
300
|
+
'Glob', 'Grep', 'List',
|
|
301
|
+
'WebFetch', 'WebSearch',
|
|
302
|
+
'TodoWrite', 'AskUserQuestion', 'EnterPlanMode', 'ExitPlanMode',
|
|
303
|
+
'Agent', 'TaskOutput', 'TaskStop',
|
|
304
|
+
'ListMcpResources', 'ReadMcpResource',
|
|
305
|
+
'Config', 'EnterWorktree', 'ExitWorktree', 'Skill', 'ToolSearch', 'Lsp',
|
|
306
|
+
'ScheduleWakeup', 'Monitor', 'PushNotification', 'RemoteTrigger',
|
|
307
|
+
'CronCreate', 'CronDelete', 'CronList',
|
|
308
|
+
]);
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Normalize an arbitrary tool name emitted by an engine into a shape the UI
|
|
312
|
+
* is guaranteed to handle:
|
|
313
|
+
* - canonical names pass through unchanged;
|
|
314
|
+
* - `mcp__*` names pass through unchanged;
|
|
315
|
+
* - anything else is wrapped as `Unknown:<original>` so the UI renders a
|
|
316
|
+
* visible error with a pointer to the fix (add to the registry).
|
|
317
|
+
*/
|
|
318
|
+
export function toCanonicalToolName(rawName: string): KnownToolName | McpToolName | UnknownToolName {
|
|
319
|
+
if (rawName.startsWith('mcp__')) return rawName as McpToolName;
|
|
320
|
+
if (CANONICAL_TOOL_NAMES.has(rawName as KnownToolName)) return rawName as KnownToolName;
|
|
321
|
+
return `Unknown:${rawName}` as UnknownToolName;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// ============================================================
|
|
325
|
+
// Sub-Agent Activity (populated in Agent tool blocks)
|
|
326
|
+
// ============================================================
|
|
327
|
+
|
|
328
|
+
export interface SubAgentToolActivity {
|
|
329
|
+
type: 'tool_use';
|
|
330
|
+
name: string;
|
|
331
|
+
input: Record<string, unknown>;
|
|
332
|
+
result: ToolResult | null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export interface SubAgentTextActivity {
|
|
336
|
+
type: 'text';
|
|
337
|
+
text: string;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export type SubAgentActivity =
|
|
341
|
+
| SubAgentToolActivity
|
|
342
|
+
| SubAgentTextActivity;
|
|
343
|
+
|
|
344
|
+
// ============================================================
|
|
345
|
+
// Tool Result
|
|
346
|
+
// ============================================================
|
|
347
|
+
|
|
348
|
+
/** Result of a tool execution, matched by toolUseId */
|
|
349
|
+
export interface ToolResult {
|
|
350
|
+
type: 'tool_result';
|
|
351
|
+
toolUseId: string;
|
|
352
|
+
content: string;
|
|
353
|
+
isError: boolean;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ============================================================
|
|
357
|
+
// Tool Use Block
|
|
358
|
+
// ============================================================
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* ToolUseBlock is the unified type for tool invocations in assistant messages.
|
|
362
|
+
*
|
|
363
|
+
* It includes enrichment fields (result, subActivities, etc.) directly.
|
|
364
|
+
* These are null/empty when the message is first created from the engine,
|
|
365
|
+
* and get populated by the message grouper when preparing for display.
|
|
366
|
+
*
|
|
367
|
+
* Discriminate on `name` to narrow the `input` type:
|
|
368
|
+
* ```ts
|
|
369
|
+
* if (block.name === 'Bash') {
|
|
370
|
+
* block.input.command; // BashInput
|
|
371
|
+
* }
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
type KnownToolUseBlock = {
|
|
375
|
+
[N in KnownToolName]: {
|
|
376
|
+
type: 'tool_use';
|
|
377
|
+
id: string;
|
|
378
|
+
name: N;
|
|
379
|
+
input: ToolInputMap[N];
|
|
380
|
+
result: ToolResult | null;
|
|
381
|
+
subActivities: SubAgentActivity[];
|
|
382
|
+
skillPrompt: string | null;
|
|
383
|
+
interrupted: boolean;
|
|
384
|
+
};
|
|
385
|
+
}[KnownToolName];
|
|
386
|
+
|
|
387
|
+
export interface McpToolUseBlock {
|
|
388
|
+
type: 'tool_use';
|
|
389
|
+
id: string;
|
|
390
|
+
name: McpToolName;
|
|
391
|
+
input: Record<string, unknown>;
|
|
392
|
+
result: ToolResult | null;
|
|
393
|
+
subActivities: SubAgentActivity[];
|
|
394
|
+
skillPrompt: string | null;
|
|
395
|
+
interrupted: boolean;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
export interface UnknownToolUseBlock {
|
|
399
|
+
type: 'tool_use';
|
|
400
|
+
id: string;
|
|
401
|
+
name: UnknownToolName;
|
|
402
|
+
input: Record<string, unknown>;
|
|
403
|
+
result: ToolResult | null;
|
|
404
|
+
subActivities: SubAgentActivity[];
|
|
405
|
+
skillPrompt: string | null;
|
|
406
|
+
interrupted: boolean;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export type ToolUseBlock = KnownToolUseBlock | McpToolUseBlock | UnknownToolUseBlock;
|