@myrialabs/clopen 0.2.24 → 0.3.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/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 +501 -0
- package/backend/engine/adapters/claude/stream.ts +52 -47
- package/backend/engine/adapters/opencode/config.ts +7 -7
- package/backend/engine/adapters/opencode/message-converter.ts +266 -326
- 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-B95VOHJY.js} +2 -2
- package/dist/assets/main-D1QNkcRL.js +257 -0
- package/dist/assets/main-rtLiuU_x.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 +108 -0
- package/shared/types/unified/message.ts +151 -0
- package/shared/types/unified/stream.ts +157 -0
- package/shared/types/unified/tool.ts +268 -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,268 @@
|
|
|
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
|
+
// ============================================================
|
|
163
|
+
// Tool Input Map
|
|
164
|
+
// ============================================================
|
|
165
|
+
|
|
166
|
+
/** Maps each known tool name to its strongly-typed input interface */
|
|
167
|
+
export interface ToolInputMap {
|
|
168
|
+
Bash: BashInput;
|
|
169
|
+
Read: ReadInput;
|
|
170
|
+
Edit: EditInput;
|
|
171
|
+
Write: WriteInput;
|
|
172
|
+
Glob: GlobInput;
|
|
173
|
+
Grep: GrepInput;
|
|
174
|
+
WebFetch: WebFetchInput;
|
|
175
|
+
WebSearch: WebSearchInput;
|
|
176
|
+
TodoWrite: TodoWriteInput;
|
|
177
|
+
Agent: AgentInput;
|
|
178
|
+
AskUserQuestion: AskUserQuestionInput;
|
|
179
|
+
TaskOutput: TaskOutputInput;
|
|
180
|
+
TaskStop: TaskStopInput;
|
|
181
|
+
NotebookEdit: NotebookEditInput;
|
|
182
|
+
ListMcpResources: ListMcpResourcesInput;
|
|
183
|
+
ReadMcpResource: ReadMcpResourceInput;
|
|
184
|
+
Config: ConfigInput;
|
|
185
|
+
EnterWorktree: EnterWorktreeInput;
|
|
186
|
+
EnterPlanMode: EnterPlanModeInput;
|
|
187
|
+
ExitPlanMode: ExitPlanModeInput;
|
|
188
|
+
Skill: SkillInput;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export type KnownToolName = keyof ToolInputMap;
|
|
192
|
+
export type McpToolName = `mcp__${string}`;
|
|
193
|
+
|
|
194
|
+
// ============================================================
|
|
195
|
+
// Sub-Agent Activity (populated in Agent tool blocks)
|
|
196
|
+
// ============================================================
|
|
197
|
+
|
|
198
|
+
export interface SubAgentToolActivity {
|
|
199
|
+
type: 'tool_use';
|
|
200
|
+
name: string;
|
|
201
|
+
input: Record<string, unknown>;
|
|
202
|
+
result: ToolResult | null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface SubAgentTextActivity {
|
|
206
|
+
type: 'text';
|
|
207
|
+
text: string;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export type SubAgentActivity =
|
|
211
|
+
| SubAgentToolActivity
|
|
212
|
+
| SubAgentTextActivity;
|
|
213
|
+
|
|
214
|
+
// ============================================================
|
|
215
|
+
// Tool Result
|
|
216
|
+
// ============================================================
|
|
217
|
+
|
|
218
|
+
/** Result of a tool execution, matched by toolUseId */
|
|
219
|
+
export interface ToolResult {
|
|
220
|
+
type: 'tool_result';
|
|
221
|
+
toolUseId: string;
|
|
222
|
+
content: string;
|
|
223
|
+
isError: boolean;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// ============================================================
|
|
227
|
+
// Tool Use Block
|
|
228
|
+
// ============================================================
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* ToolUseBlock is the unified type for tool invocations in assistant messages.
|
|
232
|
+
*
|
|
233
|
+
* It includes enrichment fields (result, subActivities, etc.) directly.
|
|
234
|
+
* These are null/empty when the message is first created from the engine,
|
|
235
|
+
* and get populated by the message grouper when preparing for display.
|
|
236
|
+
*
|
|
237
|
+
* Discriminate on `name` to narrow the `input` type:
|
|
238
|
+
* ```ts
|
|
239
|
+
* if (block.name === 'Bash') {
|
|
240
|
+
* block.input.command; // BashInput
|
|
241
|
+
* }
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
type KnownToolUseBlock = {
|
|
245
|
+
[N in KnownToolName]: {
|
|
246
|
+
type: 'tool_use';
|
|
247
|
+
id: string;
|
|
248
|
+
name: N;
|
|
249
|
+
input: ToolInputMap[N];
|
|
250
|
+
result: ToolResult | null;
|
|
251
|
+
subActivities: SubAgentActivity[];
|
|
252
|
+
skillPrompt: string | null;
|
|
253
|
+
interrupted: boolean;
|
|
254
|
+
};
|
|
255
|
+
}[KnownToolName];
|
|
256
|
+
|
|
257
|
+
export interface McpToolUseBlock {
|
|
258
|
+
type: 'tool_use';
|
|
259
|
+
id: string;
|
|
260
|
+
name: McpToolName;
|
|
261
|
+
input: Record<string, unknown>;
|
|
262
|
+
result: ToolResult | null;
|
|
263
|
+
subActivities: SubAgentActivity[];
|
|
264
|
+
skillPrompt: string | null;
|
|
265
|
+
interrupted: boolean;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export type ToolUseBlock = KnownToolUseBlock | McpToolUseBlock;
|
|
@@ -1,80 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Message Formatter
|
|
2
|
+
* Message Formatter
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* Used by ALL paths that return messages to the frontend:
|
|
6
|
-
* - Backend: message-queries.ts, messages/crud.ts
|
|
7
|
-
* - Frontend: chat.service.ts (stream transport → metadata)
|
|
4
|
+
* loadMessage() — DB row → UnifiedMessage
|
|
8
5
|
*
|
|
9
|
-
*
|
|
6
|
+
* After migration 029, the JSON blob in message is the single source
|
|
7
|
+
* of truth. This function is a simple JSON.parse wrapper.
|
|
10
8
|
*/
|
|
11
9
|
|
|
12
|
-
import type { DatabaseMessage
|
|
13
|
-
import type {
|
|
10
|
+
import type { DatabaseMessage } from '$shared/types/database/schema';
|
|
11
|
+
import type { UnifiedMessage } from '$shared/types/unified';
|
|
14
12
|
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*/
|
|
19
|
-
export function formatDatabaseMessage(
|
|
20
|
-
msg: DatabaseMessage,
|
|
21
|
-
overrides?: {
|
|
22
|
-
sender_id?: string | null;
|
|
23
|
-
sender_name?: string | null;
|
|
24
|
-
}
|
|
25
|
-
): SDKMessageFormatter {
|
|
26
|
-
const sdkMessage = JSON.parse(msg.sdk_message) as EngineSDKMessage;
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
...sdkMessage,
|
|
30
|
-
metadata: {
|
|
31
|
-
...buildMetadataFromDb(msg, overrides),
|
|
32
|
-
...(sdkMessage.metadata?.reasoning && { reasoning: true }),
|
|
33
|
-
...(sdkMessage.metadata?.interrupted && { interrupted: true }),
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Build metadata object from DatabaseMessage fields
|
|
40
|
-
* This is the SINGLE definition of what metadata contains from DB
|
|
41
|
-
*/
|
|
42
|
-
export function buildMetadataFromDb(
|
|
43
|
-
msg: DatabaseMessage,
|
|
44
|
-
overrides?: {
|
|
45
|
-
sender_id?: string | null;
|
|
46
|
-
sender_name?: string | null;
|
|
47
|
-
}
|
|
48
|
-
): NonNullable<SDKMessageFormatter['metadata']> {
|
|
49
|
-
return {
|
|
50
|
-
message_id: msg.id,
|
|
51
|
-
created_at: msg.timestamp,
|
|
52
|
-
parent_message_id: msg.parent_message_id || null,
|
|
53
|
-
sender_id: overrides?.sender_id ?? msg.sender_id ?? null,
|
|
54
|
-
sender_name: overrides?.sender_name ?? msg.sender_name ?? null,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Build metadata from stream transport fields (used by frontend during stream)
|
|
60
|
-
* Produces identical structure as buildMetadataFromDb
|
|
61
|
-
*/
|
|
62
|
-
export function buildMetadataFromTransport(transport: {
|
|
63
|
-
message_id?: string;
|
|
64
|
-
timestamp?: string;
|
|
65
|
-
parent_message_id?: string | null;
|
|
66
|
-
sender_id?: string | null;
|
|
67
|
-
sender_name?: string | null;
|
|
68
|
-
engine?: string;
|
|
69
|
-
reasoning?: boolean;
|
|
70
|
-
}): NonNullable<SDKMessageFormatter['metadata']> {
|
|
71
|
-
return {
|
|
72
|
-
message_id: transport.message_id || undefined,
|
|
73
|
-
created_at: transport.timestamp || new Date().toISOString(),
|
|
74
|
-
parent_message_id: transport.parent_message_id ?? null,
|
|
75
|
-
sender_id: transport.sender_id ?? null,
|
|
76
|
-
sender_name: transport.sender_name ?? null,
|
|
77
|
-
engine: transport.engine,
|
|
78
|
-
...(transport.reasoning && { reasoning: true }),
|
|
79
|
-
};
|
|
13
|
+
/** Parse a DatabaseMessage row into a UnifiedMessage. */
|
|
14
|
+
export function loadMessage(row: DatabaseMessage): UnifiedMessage {
|
|
15
|
+
return JSON.parse(row.data) as UnifiedMessage;
|
|
80
16
|
}
|
package/shared/utils/path.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shared path
|
|
3
|
-
*
|
|
2
|
+
* Shared path utilities (browser-safe, no Node imports).
|
|
3
|
+
* - normalizePath / pathsEqual / getRelativePath: forward-slash display/comparison
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -45,3 +45,4 @@ export function getRelativePath(fullPath: string, basePath: string): string {
|
|
|
45
45
|
|
|
46
46
|
return normalizePath(fullPath);
|
|
47
47
|
}
|
|
48
|
+
|