@myrialabs/clopen 0.2.14 → 0.2.16
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/backend/engine/adapters/opencode/server.ts +72 -33
- package/backend/snapshot/helpers.ts +8 -0
- package/backend/tunnel/cloudflared/binary.ts +153 -0
- package/backend/tunnel/cloudflared/index.ts +21 -0
- package/backend/tunnel/cloudflared/tunnel.ts +389 -0
- package/backend/tunnel/global-tunnel-manager.ts +538 -152
- package/backend/tunnel/project-tunnel-manager.ts +6 -14
- package/backend/tunnel/tunnel-config.ts +234 -0
- package/backend/ws/tunnel/config.ts +108 -0
- package/backend/ws/tunnel/index.ts +3 -1
- package/backend/ws/tunnel/operations.ts +290 -43
- package/bin/clopen.ts +1 -1
- package/bun.lock +0 -4
- package/frontend/App.svelte +2 -1
- package/frontend/components/chat/ChatInterface.svelte +6 -1
- package/frontend/components/chat/formatters/Tools.svelte +3 -1
- package/frontend/components/chat/input/components/EngineModelPicker.svelte +12 -1
- package/frontend/components/chat/message/ChatMessage.svelte +0 -29
- package/frontend/components/chat/message/ChatMessages.svelte +2 -2
- package/frontend/components/chat/message/MessageBubble.svelte +0 -6
- package/frontend/components/chat/message/MessageHeader.svelte +0 -16
- package/frontend/components/chat/tools/SkillTool.svelte +21 -0
- package/frontend/components/chat/tools/index.ts +1 -0
- package/frontend/components/chat/widgets/ContextIndicator.svelte +186 -0
- package/frontend/components/history/HistoryModal.svelte +3 -3
- package/frontend/components/settings/SettingsModal.svelte +5 -0
- package/frontend/components/settings/SettingsView.svelte +4 -0
- package/frontend/components/settings/model/EngineModelPicker.svelte +9 -0
- package/frontend/components/settings/tunnel/TunnelSettings.svelte +1060 -0
- package/frontend/components/tunnel/TunnelActive.svelte +132 -71
- package/frontend/components/tunnel/TunnelButton.svelte +4 -4
- package/frontend/components/tunnel/TunnelInactive.svelte +38 -65
- package/frontend/components/tunnel/TunnelModal.svelte +55 -2
- package/frontend/components/workspace/PanelHeader.svelte +4 -0
- package/frontend/components/workspace/WorkspaceLayout.svelte +31 -1
- package/frontend/components/workspace/panels/ChatPanel.svelte +6 -0
- package/frontend/services/favicon.service.ts +103 -0
- package/frontend/services/terminal/project.service.ts +52 -0
- package/frontend/stores/features/tunnel-config.svelte.ts +205 -0
- package/frontend/stores/features/tunnel.svelte.ts +194 -80
- package/frontend/stores/ui/settings-modal.svelte.ts +7 -0
- package/frontend/utils/chat/message-grouper.ts +33 -5
- package/frontend/utils/chat/message-processor.ts +2 -1
- package/frontend/utils/chat/tool-handler.ts +38 -3
- package/frontend/utils/context-manager.ts +35 -237
- package/package.json +1 -2
- package/shared/types/messaging/index.ts +1 -0
- package/shared/types/messaging/tool.ts +24 -1
- package/shared/utils/ws-client.ts +23 -1
- package/frontend/components/chat/modal/TokenUsageModal.svelte +0 -124
- package/frontend/components/chat/widgets/TokenUsage.svelte +0 -78
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Tunnel Store
|
|
3
|
-
* Manages cloudflared tunnel state
|
|
3
|
+
* Manages cloudflared tunnel runtime state (active tunnels, progress, errors)
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { debug } from '$shared/utils/logger';
|
|
7
7
|
import ws from '$frontend/utils/ws';
|
|
8
8
|
|
|
9
|
+
type TunnelType = 'quick' | 'remote' | 'local';
|
|
10
|
+
|
|
9
11
|
type TunnelProgress =
|
|
10
12
|
| { stage: 'idle' }
|
|
11
13
|
| { stage: 'checking-binary' }
|
|
@@ -16,14 +18,23 @@ type TunnelProgress =
|
|
|
16
18
|
| { stage: 'connected' }
|
|
17
19
|
| { stage: 'failed'; error: string };
|
|
18
20
|
|
|
21
|
+
interface IngressInfo {
|
|
22
|
+
hostname?: string;
|
|
23
|
+
service: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
19
26
|
interface TunnelInfo {
|
|
20
27
|
port: number;
|
|
21
28
|
publicUrl: string;
|
|
22
29
|
startedAt: string;
|
|
23
30
|
autoStopMinutes: number;
|
|
31
|
+
type: TunnelType;
|
|
32
|
+
label?: string;
|
|
33
|
+
configId?: string;
|
|
34
|
+
ingress?: IngressInfo[];
|
|
24
35
|
}
|
|
25
36
|
|
|
26
|
-
interface
|
|
37
|
+
interface LoadingState {
|
|
27
38
|
isLoading: boolean;
|
|
28
39
|
error: string | null;
|
|
29
40
|
progress: TunnelProgress;
|
|
@@ -31,133 +42,236 @@ interface PortState {
|
|
|
31
42
|
|
|
32
43
|
interface TunnelState {
|
|
33
44
|
tunnels: TunnelInfo[];
|
|
34
|
-
|
|
45
|
+
loadingStates: Record<string, LoadingState>;
|
|
35
46
|
}
|
|
36
47
|
|
|
37
|
-
// Tunnel store state
|
|
38
48
|
const tunnelState = $state<TunnelState>({
|
|
39
49
|
tunnels: [],
|
|
40
|
-
|
|
50
|
+
loadingStates: {}
|
|
41
51
|
});
|
|
42
52
|
|
|
53
|
+
function setLoading(key: string): void {
|
|
54
|
+
tunnelState.loadingStates[key] = {
|
|
55
|
+
isLoading: true,
|
|
56
|
+
error: null,
|
|
57
|
+
progress: { stage: 'starting-tunnel' }
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function setConnected(key: string): void {
|
|
62
|
+
if (tunnelState.loadingStates[key]) {
|
|
63
|
+
tunnelState.loadingStates[key].progress = { stage: 'connected' };
|
|
64
|
+
tunnelState.loadingStates[key].isLoading = false;
|
|
65
|
+
}
|
|
66
|
+
setTimeout(() => {
|
|
67
|
+
if (tunnelState.loadingStates[key]) {
|
|
68
|
+
tunnelState.loadingStates[key].progress = { stage: 'idle' };
|
|
69
|
+
}
|
|
70
|
+
}, 1500);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function setFailed(key: string, error: string): void {
|
|
74
|
+
if (tunnelState.loadingStates[key]) {
|
|
75
|
+
tunnelState.loadingStates[key].error = error;
|
|
76
|
+
tunnelState.loadingStates[key].progress = { stage: 'failed', error };
|
|
77
|
+
tunnelState.loadingStates[key].isLoading = false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
43
81
|
export const tunnelStore = {
|
|
44
82
|
get tunnels() {
|
|
45
83
|
return tunnelState.tunnels;
|
|
46
84
|
},
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
85
|
+
|
|
86
|
+
get activeDomainCount() {
|
|
87
|
+
let count = 0;
|
|
88
|
+
for (const tunnel of tunnelState.tunnels) {
|
|
89
|
+
const hostRules = tunnel.ingress?.filter((r) => r.hostname) ?? [];
|
|
90
|
+
count += hostRules.length > 0 ? hostRules.length : (tunnel.publicUrl ? 1 : 0);
|
|
91
|
+
}
|
|
92
|
+
return count;
|
|
52
93
|
},
|
|
53
|
-
|
|
54
|
-
|
|
94
|
+
|
|
95
|
+
getLoadingState(key: string): LoadingState {
|
|
96
|
+
return tunnelState.loadingStates[key] ?? { isLoading: false, error: null, progress: { stage: 'idle' } };
|
|
55
97
|
},
|
|
98
|
+
|
|
56
99
|
getTunnel(port: number) {
|
|
57
|
-
return tunnelState.tunnels.find((t) => t.port === port)
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
* Start tunnel globally
|
|
62
|
-
* Note: First time may take 30-90 seconds (downloading binary + starting tunnel)
|
|
63
|
-
*/
|
|
64
|
-
async startTunnel(port: number, autoStopMinutes?: number) {
|
|
65
|
-
// Initialize port state
|
|
66
|
-
tunnelState.portStates[port] = {
|
|
67
|
-
isLoading: true,
|
|
68
|
-
error: null,
|
|
69
|
-
progress: { stage: 'starting-tunnel' }
|
|
70
|
-
};
|
|
100
|
+
return tunnelState.tunnels.find((t) => t.port === port && t.type === 'quick') ?? null;
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
// --- Quick tunnel ---
|
|
71
104
|
|
|
72
|
-
|
|
105
|
+
async startQuickTunnel(port: number, autoStopMinutes?: number) {
|
|
106
|
+
const key = String(port);
|
|
107
|
+
setLoading(key);
|
|
73
108
|
|
|
74
109
|
try {
|
|
75
|
-
|
|
76
|
-
const result = await ws.http('tunnel:start', { port, autoStopMinutes });
|
|
110
|
+
const result = await ws.http('tunnel:quick:start', { port, autoStopMinutes });
|
|
77
111
|
|
|
78
|
-
if (!result
|
|
79
|
-
throw new Error('No result received from server');
|
|
80
|
-
}
|
|
112
|
+
if (!result?.publicUrl) throw new Error('No result received from server');
|
|
81
113
|
|
|
82
|
-
// Add tunnel to the list
|
|
83
114
|
tunnelState.tunnels.push({
|
|
84
115
|
port,
|
|
85
116
|
publicUrl: result.publicUrl,
|
|
86
117
|
startedAt: new Date().toISOString(),
|
|
87
|
-
autoStopMinutes: autoStopMinutes
|
|
118
|
+
autoStopMinutes: autoStopMinutes ?? 60,
|
|
119
|
+
type: 'quick'
|
|
88
120
|
});
|
|
89
121
|
|
|
90
|
-
|
|
91
|
-
|
|
122
|
+
setConnected(key);
|
|
123
|
+
debug.log('tunnel', `Quick tunnel started on port ${port}: ${result.publicUrl}`);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
const msg = error instanceof Error ? error.message : 'Unknown error';
|
|
126
|
+
setFailed(key, msg);
|
|
127
|
+
debug.error('tunnel', 'Quick tunnel error:', msg);
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
},
|
|
92
131
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
132
|
+
async stopQuickTunnel(port: number) {
|
|
133
|
+
try {
|
|
134
|
+
const response = await ws.http('tunnel:quick:stop', { port });
|
|
135
|
+
if (!response.stopped) throw new Error('Failed to stop tunnel');
|
|
97
136
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
}, 1500);
|
|
137
|
+
tunnelState.tunnels = tunnelState.tunnels.filter(
|
|
138
|
+
(t) => !(t.port === port && t.type === 'quick')
|
|
139
|
+
);
|
|
140
|
+
delete tunnelState.loadingStates[String(port)];
|
|
104
141
|
} catch (error) {
|
|
105
|
-
|
|
106
|
-
tunnelState.portStates[port].error = error.message;
|
|
107
|
-
tunnelState.portStates[port].progress = { stage: 'failed', error: error.message };
|
|
108
|
-
debug.error('tunnel', '[Frontend] Error:', error.message);
|
|
109
|
-
} else {
|
|
110
|
-
const errorMsg = 'Unknown error';
|
|
111
|
-
tunnelState.portStates[port].error = errorMsg;
|
|
112
|
-
tunnelState.portStates[port].progress = { stage: 'failed', error: errorMsg };
|
|
113
|
-
debug.error('tunnel', '[Frontend] Unknown error:', error);
|
|
114
|
-
}
|
|
115
|
-
tunnelState.portStates[port].isLoading = false;
|
|
142
|
+
debug.error('tunnel', 'Failed to stop quick tunnel:', error);
|
|
116
143
|
throw error;
|
|
117
144
|
}
|
|
118
145
|
},
|
|
119
146
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
147
|
+
// --- Remote tunnel ---
|
|
148
|
+
|
|
149
|
+
async startRemoteTunnel(configId: string) {
|
|
150
|
+
setLoading(configId);
|
|
151
|
+
|
|
124
152
|
try {
|
|
125
|
-
|
|
153
|
+
await ws.http('tunnel:remote:start', { configId });
|
|
154
|
+
setConnected(configId);
|
|
155
|
+
debug.log('tunnel', `Remote tunnel started: ${configId}`);
|
|
156
|
+
} catch (error) {
|
|
157
|
+
const msg = error instanceof Error ? error.message : 'Unknown error';
|
|
158
|
+
setFailed(configId, msg);
|
|
159
|
+
debug.error('tunnel', 'Remote tunnel error:', msg);
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
},
|
|
126
163
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
164
|
+
async stopRemoteTunnel(configId: string) {
|
|
165
|
+
try {
|
|
166
|
+
const response = await ws.http('tunnel:remote:stop', { configId });
|
|
167
|
+
if (!response.stopped) throw new Error('Failed to stop tunnel');
|
|
130
168
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
169
|
+
tunnelState.tunnels = tunnelState.tunnels.filter(
|
|
170
|
+
(t) => !(t.configId === configId && t.type === 'remote')
|
|
171
|
+
);
|
|
172
|
+
delete tunnelState.loadingStates[configId];
|
|
173
|
+
} catch (error) {
|
|
174
|
+
debug.error('tunnel', 'Failed to stop remote tunnel:', error);
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
// --- Local tunnel ---
|
|
180
|
+
|
|
181
|
+
async startLocalTunnel(configId: string) {
|
|
182
|
+
setLoading(configId);
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
await ws.http('tunnel:local:start', { id: configId });
|
|
186
|
+
setConnected(configId);
|
|
187
|
+
debug.log('tunnel', `Local tunnel started: ${configId}`);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
const msg = error instanceof Error ? error.message : 'Unknown error';
|
|
190
|
+
setFailed(configId, msg);
|
|
191
|
+
debug.error('tunnel', 'Local tunnel error:', msg);
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
async stopLocalTunnel(configId: string) {
|
|
197
|
+
try {
|
|
198
|
+
const response = await ws.http('tunnel:local:stop', { id: configId });
|
|
199
|
+
if (!response.stopped) throw new Error('Failed to stop tunnel');
|
|
134
200
|
|
|
135
|
-
|
|
201
|
+
tunnelState.tunnels = tunnelState.tunnels.filter(
|
|
202
|
+
(t) => !(t.configId === configId && t.type === 'local')
|
|
203
|
+
);
|
|
204
|
+
delete tunnelState.loadingStates[configId];
|
|
136
205
|
} catch (error) {
|
|
137
|
-
debug.error('tunnel', 'Failed to stop tunnel:', error);
|
|
206
|
+
debug.error('tunnel', 'Failed to stop local tunnel:', error);
|
|
138
207
|
throw error;
|
|
139
208
|
}
|
|
140
209
|
},
|
|
141
210
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
211
|
+
// --- Local auth ---
|
|
212
|
+
|
|
213
|
+
async checkAuth(): Promise<{ authenticated: boolean; certPath: string; zone: string | null }> {
|
|
214
|
+
return await ws.http('tunnel:local:auth-status', {});
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
async logout(): Promise<void> {
|
|
218
|
+
await ws.http('tunnel:local:logout', {});
|
|
219
|
+
},
|
|
220
|
+
|
|
221
|
+
async setZone(zone: string): Promise<void> {
|
|
222
|
+
await ws.http('tunnel:local:set-zone', { zone });
|
|
223
|
+
},
|
|
224
|
+
|
|
225
|
+
startLogin() {
|
|
226
|
+
ws.emit('tunnel:local:login-start', {});
|
|
227
|
+
},
|
|
228
|
+
|
|
229
|
+
cancelLogin() {
|
|
230
|
+
ws.emit('tunnel:local:login-cancel', {});
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
// --- Status ---
|
|
234
|
+
|
|
145
235
|
async checkStatus() {
|
|
146
236
|
try {
|
|
147
237
|
const response = await ws.http('tunnel:status', {});
|
|
148
|
-
|
|
149
|
-
// Update tunnels list from server
|
|
150
|
-
tunnelState.tunnels = response.tunnels || [];
|
|
238
|
+
tunnelState.tunnels = response.tunnels ?? [];
|
|
151
239
|
} catch (error) {
|
|
152
240
|
debug.error('tunnel', 'Failed to check tunnel status:', error);
|
|
153
241
|
}
|
|
154
242
|
},
|
|
155
243
|
|
|
156
|
-
/**
|
|
157
|
-
* Reset tunnel state
|
|
158
|
-
*/
|
|
159
244
|
reset() {
|
|
160
245
|
tunnelState.tunnels = [];
|
|
161
|
-
tunnelState.
|
|
246
|
+
tunnelState.loadingStates = {};
|
|
247
|
+
},
|
|
248
|
+
|
|
249
|
+
/** Update ingress for a specific tunnel (used by remote ingress-update events) */
|
|
250
|
+
updateTunnelIngress(configId: string, ingress: IngressInfo[]) {
|
|
251
|
+
const tunnel = tunnelState.tunnels.find((t) => t.configId === configId);
|
|
252
|
+
if (tunnel) {
|
|
253
|
+
tunnel.ingress = ingress;
|
|
254
|
+
// Also update publicUrl from first hostname
|
|
255
|
+
const firstHostname = ingress.find((r) => r.hostname)?.hostname;
|
|
256
|
+
if (firstHostname) {
|
|
257
|
+
tunnel.publicUrl = `https://${firstHostname}`;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
|
|
262
|
+
/** Initialize realtime tunnel status listener. Call once on app mount. */
|
|
263
|
+
initRealtimeListener() {
|
|
264
|
+
const cleanupStatus = ws.on('tunnel:status-changed', (data: { tunnels: TunnelInfo[] }) => {
|
|
265
|
+
tunnelState.tunnels = data.tunnels;
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
const cleanupIngress = ws.on('tunnel:remote:ingress-update', (data: { configId: string; ingress: IngressInfo[] }) => {
|
|
269
|
+
tunnelStore.updateTunnelIngress(data.configId, data.ingress);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
return () => {
|
|
273
|
+
cleanupStatus();
|
|
274
|
+
cleanupIngress();
|
|
275
|
+
};
|
|
162
276
|
}
|
|
163
277
|
};
|
|
@@ -10,6 +10,7 @@ export type SettingsSection =
|
|
|
10
10
|
| 'engines'
|
|
11
11
|
| 'appearance'
|
|
12
12
|
| 'notifications'
|
|
13
|
+
| 'tunnel'
|
|
13
14
|
| 'account'
|
|
14
15
|
| 'team'
|
|
15
16
|
| 'security'
|
|
@@ -48,6 +49,12 @@ export const settingsSections: SettingsSectionMeta[] = [
|
|
|
48
49
|
icon: 'lucide:bell',
|
|
49
50
|
description: 'Sound and push notifications'
|
|
50
51
|
},
|
|
52
|
+
{
|
|
53
|
+
id: 'tunnel',
|
|
54
|
+
label: 'Tunnel',
|
|
55
|
+
icon: 'lucide:globe',
|
|
56
|
+
description: 'Cloudflare tunnel services'
|
|
57
|
+
},
|
|
51
58
|
{
|
|
52
59
|
id: 'account',
|
|
53
60
|
label: 'User Profile',
|
|
@@ -57,13 +57,18 @@ function getParentToolUseId(message: SDKMessageFormatter): string | null {
|
|
|
57
57
|
export function groupMessages(messages: SDKMessageFormatter[]): {
|
|
58
58
|
groups: ProcessedMessage[],
|
|
59
59
|
toolUseMap: Map<string, ToolGroup>,
|
|
60
|
-
subAgentMap: Map<string, SDKMessageFormatter[]
|
|
60
|
+
subAgentMap: Map<string, SDKMessageFormatter[]>,
|
|
61
|
+
skillPromptMap: Map<string, string>
|
|
61
62
|
} {
|
|
62
63
|
const groups: ProcessedMessage[] = [];
|
|
63
64
|
const toolUseMap = new Map<string, ToolGroup>();
|
|
64
65
|
const agentToolUseIds = new Set<string>();
|
|
65
66
|
const subAgentMap = new Map<string, SDKMessageFormatter[]>();
|
|
67
|
+
const skillToolUseIds = new Set<string>();
|
|
68
|
+
const skillPromptMap = new Map<string, string>();
|
|
66
69
|
let lastCompactBoundaryIdx = -1;
|
|
70
|
+
// Track last Skill tool_use ID for associating synthetic user messages
|
|
71
|
+
let lastSkillToolUseId: string | null = null;
|
|
67
72
|
|
|
68
73
|
// Rebuild compact summary map each call
|
|
69
74
|
_compactSummaryMap = new WeakMap<SDKMessageFormatter, string>();
|
|
@@ -91,6 +96,7 @@ export function groupMessages(messages: SDKMessageFormatter[]): {
|
|
|
91
96
|
// Handle compact boundary messages — track for synthetic user embedding
|
|
92
97
|
if (isCompactBoundaryMessage(message)) {
|
|
93
98
|
lastCompactBoundaryIdx = groups.length;
|
|
99
|
+
lastSkillToolUseId = null;
|
|
94
100
|
groups.push(message as ProcessedMessage);
|
|
95
101
|
return;
|
|
96
102
|
}
|
|
@@ -98,6 +104,7 @@ export function groupMessages(messages: SDKMessageFormatter[]): {
|
|
|
98
104
|
// Handle assistant messages with tool_use
|
|
99
105
|
if (message.type === 'assistant' && 'message' in message && message.message?.content) {
|
|
100
106
|
lastCompactBoundaryIdx = -1;
|
|
107
|
+
lastSkillToolUseId = null;
|
|
101
108
|
const toolUses = extractToolUses(message.message.content);
|
|
102
109
|
|
|
103
110
|
if (toolUses.length > 0) {
|
|
@@ -112,6 +119,11 @@ export function groupMessages(messages: SDKMessageFormatter[]): {
|
|
|
112
119
|
if (toolUse.name === 'Agent') {
|
|
113
120
|
agentToolUseIds.add(toolUse.id);
|
|
114
121
|
}
|
|
122
|
+
// Track Skill tool IDs for synthetic user message capture
|
|
123
|
+
if (toolUse.name === 'Skill') {
|
|
124
|
+
skillToolUseIds.add(toolUse.id);
|
|
125
|
+
lastSkillToolUseId = toolUse.id;
|
|
126
|
+
}
|
|
115
127
|
}
|
|
116
128
|
});
|
|
117
129
|
groups.push(message as ProcessedMessage);
|
|
@@ -134,11 +146,23 @@ export function groupMessages(messages: SDKMessageFormatter[]): {
|
|
|
134
146
|
return;
|
|
135
147
|
}
|
|
136
148
|
|
|
149
|
+
// Synthetic user messages after Skill tool: capture expanded skill prompt
|
|
150
|
+
if (isSyntheticUserMessage(message) && lastSkillToolUseId) {
|
|
151
|
+
const promptText = extractUserTextContent(message);
|
|
152
|
+
if (promptText) {
|
|
153
|
+
skillPromptMap.set(lastSkillToolUseId, promptText);
|
|
154
|
+
}
|
|
155
|
+
lastSkillToolUseId = null;
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
137
159
|
lastCompactBoundaryIdx = -1;
|
|
138
160
|
|
|
139
161
|
const toolResults = extractToolResults(message.message.content);
|
|
140
162
|
if (toolResults.length > 0) {
|
|
141
163
|
// Group tool_result with corresponding tool_use
|
|
164
|
+
// Note: don't reset lastSkillToolUseId here — tool_result may precede
|
|
165
|
+
// the synthetic skill prompt user message
|
|
142
166
|
toolResults.forEach((toolResult: any) => {
|
|
143
167
|
if (toolResult.tool_use_id && toolUseMap.has(toolResult.tool_use_id)) {
|
|
144
168
|
const group = toolUseMap.get(toolResult.tool_use_id);
|
|
@@ -149,7 +173,8 @@ export function groupMessages(messages: SDKMessageFormatter[]): {
|
|
|
149
173
|
});
|
|
150
174
|
// Don't add tool_result messages separately
|
|
151
175
|
} else {
|
|
152
|
-
// Regular user message
|
|
176
|
+
// Regular user message — reset skill tracking
|
|
177
|
+
lastSkillToolUseId = null;
|
|
153
178
|
groups.push(message as ProcessedMessage);
|
|
154
179
|
}
|
|
155
180
|
return;
|
|
@@ -157,17 +182,19 @@ export function groupMessages(messages: SDKMessageFormatter[]): {
|
|
|
157
182
|
|
|
158
183
|
// Include stream_event and other messages
|
|
159
184
|
lastCompactBoundaryIdx = -1;
|
|
185
|
+
lastSkillToolUseId = null;
|
|
160
186
|
groups.push(message as ProcessedMessage);
|
|
161
187
|
});
|
|
162
188
|
|
|
163
|
-
return { groups, toolUseMap, subAgentMap };
|
|
189
|
+
return { groups, toolUseMap, subAgentMap, skillPromptMap };
|
|
164
190
|
}
|
|
165
191
|
|
|
166
192
|
// Add tool results to messages
|
|
167
193
|
export function embedToolResults(
|
|
168
194
|
groups: ProcessedMessage[],
|
|
169
195
|
toolUseMap: Map<string, ToolGroup>,
|
|
170
|
-
subAgentMap: Map<string, SDKMessageFormatter[]
|
|
196
|
+
subAgentMap: Map<string, SDKMessageFormatter[]>,
|
|
197
|
+
skillPromptMap: Map<string, string>
|
|
171
198
|
): ProcessedMessage[] {
|
|
172
199
|
// Track background bash sessions
|
|
173
200
|
const backgroundBashMap = trackBackgroundBashSessions(groups, toolUseMap);
|
|
@@ -182,7 +209,8 @@ export function embedToolResults(
|
|
|
182
209
|
message,
|
|
183
210
|
toolUseMap,
|
|
184
211
|
backgroundBashMap,
|
|
185
|
-
subAgentMap
|
|
212
|
+
subAgentMap,
|
|
213
|
+
skillPromptMap
|
|
186
214
|
);
|
|
187
215
|
return processedMessage;
|
|
188
216
|
}
|
|
@@ -18,6 +18,7 @@ export interface ToolUseWithResult {
|
|
|
18
18
|
input: any;
|
|
19
19
|
$result?: any;
|
|
20
20
|
$subMessages?: SubAgentActivity[];
|
|
21
|
+
$skillPrompt?: string;
|
|
21
22
|
metadata?: Record<string, unknown>;
|
|
22
23
|
}
|
|
23
24
|
|
|
@@ -26,7 +27,8 @@ export function processToolMessage(
|
|
|
26
27
|
message: ProcessedMessage,
|
|
27
28
|
toolUseMap: Map<string, ToolGroup>,
|
|
28
29
|
backgroundBashMap: Map<string, BackgroundBashData>,
|
|
29
|
-
subAgentMap: Map<string, SDKMessageFormatter[]
|
|
30
|
+
subAgentMap: Map<string, SDKMessageFormatter[]>,
|
|
31
|
+
skillPromptMap: Map<string, string> = new Map()
|
|
30
32
|
): ProcessedMessage {
|
|
31
33
|
const messageAny = message as any;
|
|
32
34
|
const content = messageAny.message?.content ?
|
|
@@ -39,7 +41,7 @@ export function processToolMessage(
|
|
|
39
41
|
const modifiedContent = content
|
|
40
42
|
.map((item: any): any => {
|
|
41
43
|
if (typeof item === 'object' && item && 'type' in item && item.type === 'tool_use') {
|
|
42
|
-
const processed = processToolUse(item, toolUseMap, backgroundBashMap, subAgentMap);
|
|
44
|
+
const processed = processToolUse(item, toolUseMap, backgroundBashMap, subAgentMap, skillPromptMap);
|
|
43
45
|
// Propagate message-level interrupted flag to ALL tool_use blocks
|
|
44
46
|
if (processed && isInterrupted) {
|
|
45
47
|
return { ...processed, metadata: { ...processed.metadata, interrupted: true } };
|
|
@@ -65,7 +67,8 @@ function processToolUse(
|
|
|
65
67
|
item: any,
|
|
66
68
|
toolUseMap: Map<string, ToolGroup>,
|
|
67
69
|
backgroundBashMap: Map<string, BackgroundBashData>,
|
|
68
|
-
subAgentMap: Map<string, SDKMessageFormatter[]
|
|
70
|
+
subAgentMap: Map<string, SDKMessageFormatter[]>,
|
|
71
|
+
skillPromptMap: Map<string, string>
|
|
69
72
|
): ToolUseWithResult | null {
|
|
70
73
|
// Hide certain tools completely
|
|
71
74
|
if (shouldHideTool(item.name)) {
|
|
@@ -85,6 +88,11 @@ function processToolUse(
|
|
|
85
88
|
return handleAgentTool(item, toolUseMap, subAgentMap);
|
|
86
89
|
}
|
|
87
90
|
|
|
91
|
+
// Special handling for Skill tool — embed expanded skill prompt
|
|
92
|
+
if (item.name === 'Skill' && item.id && skillPromptMap.has(item.id)) {
|
|
93
|
+
return handleSkillTool(item, toolUseMap, skillPromptMap);
|
|
94
|
+
}
|
|
95
|
+
|
|
88
96
|
// Regular tool handling
|
|
89
97
|
if (item.id && item.name && shouldEmbedResult(item.name) && toolUseMap.has(item.id)) {
|
|
90
98
|
return handleRegularTool(item, toolUseMap);
|
|
@@ -165,6 +173,33 @@ function processSubAgentMessages(messages: SDKMessageFormatter[]): SubAgentActiv
|
|
|
165
173
|
return activities;
|
|
166
174
|
}
|
|
167
175
|
|
|
176
|
+
// Handle Skill tool — embed expanded skill prompt
|
|
177
|
+
function handleSkillTool(
|
|
178
|
+
item: any,
|
|
179
|
+
toolUseMap: Map<string, ToolGroup>,
|
|
180
|
+
skillPromptMap: Map<string, string>
|
|
181
|
+
): ToolUseWithResult {
|
|
182
|
+
const skillPrompt = skillPromptMap.get(item.id);
|
|
183
|
+
|
|
184
|
+
// Also embed the $result if available
|
|
185
|
+
let result: any = undefined;
|
|
186
|
+
if (item.id && toolUseMap.has(item.id)) {
|
|
187
|
+
const group = toolUseMap.get(item.id);
|
|
188
|
+
if (group?.toolResultMessage) {
|
|
189
|
+
const resultMessage = group.toolResultMessage as any;
|
|
190
|
+
const resultContent = resultMessage.message ?
|
|
191
|
+
(Array.isArray(resultMessage.message.content) ? resultMessage.message.content : [resultMessage.message.content]) : [];
|
|
192
|
+
result = findToolResult(resultContent, item.id);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
...item,
|
|
198
|
+
...(result ? { $result: result } : {}),
|
|
199
|
+
...(skillPrompt ? { $skillPrompt: skillPrompt } : {})
|
|
200
|
+
} as ToolUseWithResult;
|
|
201
|
+
}
|
|
202
|
+
|
|
168
203
|
// Handle background bash commands
|
|
169
204
|
function handleBackgroundBash(
|
|
170
205
|
item: any,
|