@myrialabs/clopen 0.2.14 → 0.2.15
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/snapshot/helpers.ts +8 -0
- package/bin/clopen.ts +1 -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/model/EngineModelPicker.svelte +9 -0
- package/frontend/components/workspace/PanelHeader.svelte +4 -0
- package/frontend/components/workspace/panels/ChatPanel.svelte +6 -0
- package/frontend/services/terminal/project.service.ts +52 -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 -1
- 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
|
@@ -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,
|
|
@@ -1,257 +1,55 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Context Window
|
|
2
|
+
* Context Window Usage
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Calculates actual context window usage from assistant message token data.
|
|
5
|
+
* Each API call sends the full conversation, so input_tokens on the last
|
|
6
|
+
* assistant message represents total context consumed at that turn.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { SDKMessage } from '$shared/types/messaging';
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
const DISPLAY_MAX_TOKENS = 200000; // Display token limit
|
|
13
|
-
const WARNING_THRESHOLD = 0.8; // Warn when reaching 80% of max tokens
|
|
14
|
-
const TOKEN_ESTIMATE_RATIO = 3.5; // Estimate: 1 token ≈ 3.5 characters
|
|
11
|
+
const WARNING_THRESHOLD = 0.8;
|
|
15
12
|
|
|
16
|
-
export interface
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
tokenUsage: {
|
|
22
|
-
current: number;
|
|
23
|
-
max: number;
|
|
24
|
-
percentage: number;
|
|
25
|
-
nearLimit: boolean;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Estimate token count for a text string
|
|
31
|
-
* Uses a more sophisticated estimation that accounts for different text patterns
|
|
32
|
-
*/
|
|
33
|
-
export function estimateTokens(text: string): number {
|
|
34
|
-
if (!text || text.length === 0) return 0;
|
|
35
|
-
|
|
36
|
-
// More accurate estimation accounting for:
|
|
37
|
-
// - Whitespace and punctuation
|
|
38
|
-
// - Code blocks (different tokenization)
|
|
39
|
-
// - Common patterns
|
|
40
|
-
|
|
41
|
-
let adjustedLength = text.length;
|
|
42
|
-
|
|
43
|
-
// Code blocks tend to have more tokens per character
|
|
44
|
-
const codeBlockMatches = text.match(/```[\s\S]*?```/g);
|
|
45
|
-
if (codeBlockMatches) {
|
|
46
|
-
const codeLength = codeBlockMatches.reduce((sum, block) => sum + block.length, 0);
|
|
47
|
-
// Code has ~1 token per 2.5 characters
|
|
48
|
-
adjustedLength = adjustedLength - codeLength + (codeLength / 2.5);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Account for whitespace - multiple spaces/newlines count as fewer tokens
|
|
52
|
-
const whitespaceReduced = text.replace(/\s+/g, ' ').length;
|
|
53
|
-
adjustedLength = Math.min(adjustedLength, whitespaceReduced * 1.2);
|
|
54
|
-
|
|
55
|
-
// Apply base ratio with minimum of 1 token for non-empty text
|
|
56
|
-
return Math.max(1, Math.ceil(adjustedLength / TOKEN_ESTIMATE_RATIO));
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Token calculation cache to prevent recalculation
|
|
60
|
-
const tokenCache = new Map<string, number>();
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Calculate total tokens for a set of messages using only actual token usage data
|
|
64
|
-
* Returns 0 if no actual usage data is available (no estimates)
|
|
65
|
-
*/
|
|
66
|
-
export function calculateMessageTokens(messages: SDKMessage[]): number {
|
|
67
|
-
if (!messages || messages.length === 0) return 0;
|
|
68
|
-
|
|
69
|
-
// Only use actual token usage data from API responses
|
|
70
|
-
const messagesWithUsage = messages.filter(msg => {
|
|
71
|
-
if (msg.type === 'assistant' && 'message' in msg && msg.message.usage) {
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
return false;
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
if (messagesWithUsage.length === 0) {
|
|
78
|
-
// No actual usage data available - return 0 instead of estimates
|
|
79
|
-
return 0;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Calculate actual total tokens from API responses
|
|
83
|
-
return messagesWithUsage.reduce((total, msg) => {
|
|
84
|
-
if (msg.type === 'assistant' && 'message' in msg && msg.message.usage) {
|
|
85
|
-
const usage = msg.message.usage;
|
|
86
|
-
return total + (usage.input_tokens || 0) + (usage.output_tokens || 0);
|
|
87
|
-
}
|
|
88
|
-
return total;
|
|
89
|
-
}, 0);
|
|
13
|
+
export interface ContextUsage {
|
|
14
|
+
current: number;
|
|
15
|
+
max: number;
|
|
16
|
+
percentage: number;
|
|
17
|
+
nearLimit: boolean;
|
|
90
18
|
}
|
|
91
19
|
|
|
92
20
|
/**
|
|
93
|
-
*
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
} else if (msg.type === 'assistant' && 'message' in msg) {
|
|
109
|
-
// Extract key actions/responses from assistant
|
|
110
|
-
const content = Array.isArray(msg.message.content)
|
|
111
|
-
? msg.message.content.map(c => c.type === 'text' ? c.text : JSON.stringify(c)).join(' ')
|
|
112
|
-
: JSON.stringify(msg.message.content);
|
|
113
|
-
const keyActions = extractKeyActions(content);
|
|
114
|
-
if (keyActions) assistantMessages.push(keyActions);
|
|
21
|
+
* Get context window usage from the last assistant message.
|
|
22
|
+
*
|
|
23
|
+
* Cache tokens (creation + read) also count toward the context window.
|
|
24
|
+
*/
|
|
25
|
+
export function getContextUsage(messages: SDKMessage[], contextWindow: number): ContextUsage {
|
|
26
|
+
// Find the last assistant message with usage data (iterate backwards)
|
|
27
|
+
let lastUsage: { input_tokens: number; cache_creation_input_tokens?: number; cache_read_input_tokens?: number } | null = null;
|
|
28
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
29
|
+
const msg = messages[i];
|
|
30
|
+
if (msg.type === 'assistant' && 'message' in msg) {
|
|
31
|
+
const usage = (msg as any).message?.usage;
|
|
32
|
+
if (usage && typeof usage.input_tokens === 'number') {
|
|
33
|
+
lastUsage = usage;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
115
36
|
}
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
// Build summary
|
|
119
|
-
const summaryParts: string[] = [];
|
|
120
|
-
|
|
121
|
-
if (userMessages.length > 0) {
|
|
122
|
-
summaryParts.push(`Previous user requests: ${userMessages.join('; ')}`);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (assistantMessages.length > 0) {
|
|
126
|
-
summaryParts.push(`Previous assistant actions: ${assistantMessages.join('; ')}`);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return summaryParts.join('\n');
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Extract key points from user messages
|
|
134
|
-
*/
|
|
135
|
-
function extractKeyPoints(content: string): string {
|
|
136
|
-
// Handle both string and ContentBlock array formats
|
|
137
|
-
const textContent = content;
|
|
138
|
-
|
|
139
|
-
// Look for questions, commands, or important keywords
|
|
140
|
-
const lines = textContent.split('\n').filter(line => line.trim());
|
|
141
|
-
|
|
142
|
-
// Prioritize lines with questions or commands
|
|
143
|
-
const importantLines = lines.filter(line =>
|
|
144
|
-
line.includes('?') ||
|
|
145
|
-
line.includes('please') ||
|
|
146
|
-
line.includes('help') ||
|
|
147
|
-
line.includes('create') ||
|
|
148
|
-
line.includes('update') ||
|
|
149
|
-
line.includes('fix') ||
|
|
150
|
-
line.includes('implement') ||
|
|
151
|
-
line.includes('add') ||
|
|
152
|
-
line.includes('remove') ||
|
|
153
|
-
line.includes('show') ||
|
|
154
|
-
line.includes('explain')
|
|
155
|
-
);
|
|
156
|
-
|
|
157
|
-
if (importantLines.length > 0) {
|
|
158
|
-
// Take first important line as summary
|
|
159
|
-
return importantLines[0].substring(0, 100);
|
|
160
37
|
}
|
|
161
|
-
|
|
162
|
-
// Fallback to first line
|
|
163
|
-
return lines[0]?.substring(0, 100) || '';
|
|
164
|
-
}
|
|
165
38
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
*/
|
|
169
|
-
function extractKeyActions(content: string): string {
|
|
170
|
-
// Look for action indicators
|
|
171
|
-
const actionPatterns = [
|
|
172
|
-
/created (.+)/i,
|
|
173
|
-
/updated (.+)/i,
|
|
174
|
-
/implemented (.+)/i,
|
|
175
|
-
/fixed (.+)/i,
|
|
176
|
-
/added (.+)/i,
|
|
177
|
-
/removed (.+)/i,
|
|
178
|
-
/modified (.+)/i,
|
|
179
|
-
/explained (.+)/i,
|
|
180
|
-
/I will (.+)/i,
|
|
181
|
-
/I have (.+)/i,
|
|
182
|
-
/I've (.+)/i
|
|
183
|
-
];
|
|
184
|
-
|
|
185
|
-
for (const pattern of actionPatterns) {
|
|
186
|
-
const match = content.match(pattern);
|
|
187
|
-
if (match) {
|
|
188
|
-
return match[0].substring(0, 100);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// Look for code blocks as actions
|
|
193
|
-
if (content.includes('```')) {
|
|
194
|
-
return 'provided code implementation';
|
|
39
|
+
if (!lastUsage) {
|
|
40
|
+
return { current: 0, max: contextWindow, percentage: 0, nearLimit: false };
|
|
195
41
|
}
|
|
196
|
-
|
|
197
|
-
// Fallback to first line
|
|
198
|
-
const firstLine = content.split('\n')[0];
|
|
199
|
-
return firstLine?.substring(0, 100) || '';
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Calculate token usage statistics with smoothing
|
|
204
|
-
*/
|
|
205
|
-
function calculateTokenUsage(currentTokens: number): ManagedContext['tokenUsage'] {
|
|
206
|
-
// Ensure non-negative values
|
|
207
|
-
const safeTokens = Math.max(0, currentTokens);
|
|
208
|
-
const percentage = Math.min(100, (safeTokens / DISPLAY_MAX_TOKENS) * 100);
|
|
209
|
-
|
|
210
|
-
return {
|
|
211
|
-
current: safeTokens,
|
|
212
|
-
max: DISPLAY_MAX_TOKENS,
|
|
213
|
-
percentage: Math.round(percentage * 10) / 10, // Round to 1 decimal place for smoother transitions
|
|
214
|
-
nearLimit: safeTokens >= (DISPLAY_MAX_TOKENS * WARNING_THRESHOLD)
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
42
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
*/
|
|
222
|
-
export function manageConversationContext(messages: SDKMessage[]): ManagedContext {
|
|
223
|
-
if (messages.length === 0) {
|
|
224
|
-
return {
|
|
225
|
-
messages: [],
|
|
226
|
-
totalTokens: 0,
|
|
227
|
-
isCompressed: false,
|
|
228
|
-
tokenUsage: calculateTokenUsage(0)
|
|
229
|
-
};
|
|
230
|
-
}
|
|
43
|
+
const current = (lastUsage.input_tokens || 0)
|
|
44
|
+
+ (lastUsage.cache_creation_input_tokens || 0)
|
|
45
|
+
+ (lastUsage.cache_read_input_tokens || 0);
|
|
231
46
|
|
|
232
|
-
|
|
233
|
-
const totalTokens = calculateMessageTokens(messages);
|
|
47
|
+
const percentage = Math.min(100, Math.round((current / contextWindow) * 1000) / 10);
|
|
234
48
|
|
|
235
|
-
// SDK handles context internally, we just return info for UI
|
|
236
49
|
return {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
tokenUsage: calculateTokenUsage(totalTokens)
|
|
50
|
+
current,
|
|
51
|
+
max: contextWindow,
|
|
52
|
+
percentage,
|
|
53
|
+
nearLimit: current >= contextWindow * WARNING_THRESHOLD
|
|
242
54
|
};
|
|
243
55
|
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Build conversation prompt
|
|
247
|
-
* @deprecated SDK handles prompt building internally with resume feature
|
|
248
|
-
*/
|
|
249
|
-
export function buildConversationPrompt(
|
|
250
|
-
messages: SDKMessage[],
|
|
251
|
-
currentPrompt: string,
|
|
252
|
-
summary?: string
|
|
253
|
-
): string {
|
|
254
|
-
// SDK handles this internally now, just return the prompt
|
|
255
|
-
// This function is kept for backward compatibility
|
|
256
|
-
return currentPrompt;
|
|
257
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@myrialabs/clopen",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.15",
|
|
4
4
|
"description": "All-in-one web workspace for Claude Code & OpenCode — chat, terminal, git, browser preview, checkpoints, and real-time collaboration",
|
|
5
5
|
"author": "Myria Labs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -323,6 +323,28 @@ export interface EnterPlanModeToolInput {
|
|
|
323
323
|
metadata?: ToolMetadata;
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
+
/**
|
|
327
|
+
* Skill tool input — manually defined (not from SDK).
|
|
328
|
+
*
|
|
329
|
+
* Neither @anthropic-ai/claude-agent-sdk nor @opencode-ai/sdk export a
|
|
330
|
+
* SkillInput type. The shape is inferred from observed tool_use payloads
|
|
331
|
+
* where `input.skill` is the slash-command name and `input.args` is an
|
|
332
|
+
* optional argument string.
|
|
333
|
+
*/
|
|
334
|
+
export interface SkillToolInput {
|
|
335
|
+
type: 'tool_use';
|
|
336
|
+
id: string;
|
|
337
|
+
name: 'Skill';
|
|
338
|
+
input: {
|
|
339
|
+
skill: string;
|
|
340
|
+
args?: string;
|
|
341
|
+
};
|
|
342
|
+
$result?: ToolResult;
|
|
343
|
+
/** Expanded skill prompt (from synthetic user message) */
|
|
344
|
+
$skillPrompt?: string;
|
|
345
|
+
metadata?: ToolMetadata;
|
|
346
|
+
}
|
|
347
|
+
|
|
326
348
|
// ============================================================
|
|
327
349
|
// Union of All Tool Input Types
|
|
328
350
|
// ============================================================
|
|
@@ -348,4 +370,5 @@ export type ToolInput =
|
|
|
348
370
|
| ConfigToolInput
|
|
349
371
|
| EnterWorktreeToolInput
|
|
350
372
|
| AgentToolInput
|
|
351
|
-
| EnterPlanModeToolInput
|
|
373
|
+
| EnterPlanModeToolInput
|
|
374
|
+
| SkillToolInput;
|
|
@@ -584,11 +584,33 @@ export class WSClient<TAPI extends { client: any; server: any }> {
|
|
|
584
584
|
|
|
585
585
|
/**
|
|
586
586
|
* Manual reconnect
|
|
587
|
+
* Unlike disconnect(), this preserves all registered listeners
|
|
588
|
+
* so that services (chat, terminal, etc.) continue receiving events.
|
|
587
589
|
*/
|
|
588
590
|
reconnect(): void {
|
|
589
|
-
|
|
591
|
+
// Cancel any pending auto-reconnect
|
|
592
|
+
if (this.reconnectTimeout) {
|
|
593
|
+
clearTimeout(this.reconnectTimeout);
|
|
594
|
+
this.reconnectTimeout = null;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// Close existing socket without triggering onclose reconnect
|
|
598
|
+
if (this.ws) {
|
|
599
|
+
try {
|
|
600
|
+
this.ws.onclose = null;
|
|
601
|
+
this.ws.onerror = null;
|
|
602
|
+
this.ws.onmessage = null;
|
|
603
|
+
this.ws.close();
|
|
604
|
+
} catch {
|
|
605
|
+
// Ignore close errors on stale socket
|
|
606
|
+
}
|
|
607
|
+
this.ws = null;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
this.isConnected = false;
|
|
590
611
|
this.shouldReconnect = true;
|
|
591
612
|
this.reconnectAttempts = 0;
|
|
613
|
+
// NOTE: listeners and messageQueue are intentionally preserved
|
|
592
614
|
this.connect();
|
|
593
615
|
}
|
|
594
616
|
|