-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudAgenticChat.js
More file actions
327 lines (287 loc) · 11.7 KB
/
cloudAgenticChat.js
File metadata and controls
327 lines (287 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
'use strict';
const {
parseToolCalls,
repairToolCalls,
stripToolCallText,
looksLikeToolAttempt,
suggestClosestToolName,
} = require('./tools/toolParser');
const { buildCloudSystemPrompt, buildAgentSystemPromptLayers } = require('./chatEngine');
const {
formatToolResultForInject,
buildToolResultsUserMessage,
sanitizeCloudConversationHistory,
} = require('./tools/toolResultInjection');
const { createCloudStreamFilters } = require('./tools/streamingToolFilter');
const {
resolveAgentMode,
filterToolDefinitions,
filterPlanModeToolCalls,
shouldStreamFileContentForAgent,
} = require('./agentModeResolver');
const streamTrace = require('./streamTrace');
const CLOUD_CONTINUE_PROMPT = 'Continue from the tool results above. Call more tools if needed, or give a concise final answer.';
const PLAN_BLOCKED_TOOLS_MSG =
'[System: Plan mode — update_todo cannot mark items done/in-progress or edit non-plan files until Build. Use write_todos for planning; write_file/edit_file only for .guide/plans/*.plan.md. Do not repeat blocked tool JSON in your reply.]';
const FILE_WRITE_OPS = new Set(['write_file', 'create_file', 'append_to_file']);
const FILE_EDIT_OPS = new Set(['edit_file', 'replace_in_file']);
/**
* Agentic cloud chat: same tool catalog and mode rules as local (via agentModeResolver).
*/
async function runCloudAgenticChat({
cloudLLM,
mcpToolServer,
ChatEngine,
userMessage,
cloudProvider,
cloudModel,
settings,
conversationHistory: initialHistory,
images,
executeToolFn,
onToken,
onThinkingToken,
onStreamEvent,
getCancelled,
getActiveTodos,
}) {
const enableSubAgents = !!(settings.enableSubAgents);
const toolsEnabled = settings.toolsEnabled !== false;
const mode = resolveAgentMode({
askOnly: settings.askOnly,
planMode: settings.planMode,
chatMode: settings.chatMode,
agentPhase: settings.agentPhase || 'planning',
toolsEnabled,
planReady: !!settings.planReady,
planFileExists: !!settings.planFileExists,
});
mcpToolServer.setAgentContext({ planMode: mode.planMode, agentPhase: mode.agentPhase });
const allDefs = mcpToolServer.getToolDefinitions();
const filteredDefs = filterToolDefinitions(allDefs, mode.allowedTools);
const toolPromptOpts = { planning: mode.planning };
let toolPrompt = mode.toolsActive ? mcpToolServer.getToolPromptForTools(filteredDefs, toolPromptOpts) : '';
const compactToolParts = mode.toolsActive
? mcpToolServer.getCompactToolHint('default', { toolDefs: filteredDefs, planning: mode.planning })
: [];
let compactToolPrompt = compactToolParts.join('');
if (enableSubAgents && toolPrompt) {
const subAgentTool = '\n- **spawn_subagent** — Delegate a focused sub-task to an isolated sub-agent (local model only; unavailable in cloud mode).';
toolPrompt += subAgentTool;
compactToolPrompt += '\n- spawn_subagent: not available in cloud mode\n';
}
let systemPrompt = buildCloudSystemPrompt({
userSystemPrompt: settings.systemPrompt,
baseSystemPrompt: mode.baseSystemPrompt,
customInstructions: settings.customInstructions,
toolPrompt,
});
systemPrompt += buildAgentSystemPromptLayers({
projectPath: settings.projectPath,
guideInstructionsPath: settings.guideInstructionsPath,
editorContext: settings.editorContext,
editorDiagnostics: settings.editorDiagnostics,
});
if (mode.systemPromptAdditions) {
systemPrompt += mode.systemPromptAdditions;
}
const conversationHistory = sanitizeCloudConversationHistory(
Array.isArray(initialHistory) ? initialHistory : [],
{ parseToolCalls, stripToolCallText }
).map((m) => ({ role: m.role, content: String(m.content || '') }));
console.log(
`[CloudAgentic] history: ${initialHistory?.length || 0} raw → ${conversationHistory.length} sanitized; mode=${mode.planning ? 'plan' : mode.askOnly ? 'ask' : 'agent'}`
);
const maxIter = settings.maxIterations > 0 ? settings.maxIterations : 25;
let fullResponse = '';
let displayResponse = '';
let totalToolCalls = 0;
let nextUserPrompt = userMessage;
const contextTokens = settings.maxResponseTokens > 0 ? settings.maxResponseTokens : 8192;
const streamFilters = createCloudStreamFilters({ onToken, onThinkingToken });
const genBase = {
provider: cloudProvider,
model: cloudModel,
systemPrompt,
temperature: settings.temperature,
maxTokens: settings.maxResponseTokens || -1,
topP: settings.topP,
images,
stream: true,
};
for (let iter = 0; iter < maxIter; iter++) {
if (getCancelled?.()) {
console.log('[CloudAgentic] cancelled');
break;
}
streamFilters.resetRound();
const result = await cloudLLM.generate(nextUserPrompt, {
...genBase,
conversationHistory,
images: iter === 0 ? images : [],
onToken: (token) => {
streamTrace.trace('stream', 'cloud-token', { token, iter });
streamFilters.processContentChunk(token);
},
onThinkingToken: (token) => {
streamTrace.trace('stream', 'cloud-thinking-token', { token, iter });
streamFilters.processThinkingChunk(token);
},
});
streamFilters.flush();
if (result?.isQuotaError) {
return { isQuotaError: true, error: '__QUOTA_EXCEEDED__', text: displayResponse || fullResponse, toolCallCount: totalToolCalls };
}
const roundTextContent = result?.text || '';
const roundRawCombined = streamFilters.getCombinedRawBuffer() || roundTextContent;
fullResponse += roundRawCombined;
const roundCleanProse = streamFilters.getProseCleanText() || stripToolCallText(roundTextContent);
displayResponse += roundCleanProse;
if (mode.askOnly || !mode.toolsActive || !executeToolFn) {
break;
}
let parsedCalls = parseToolCalls(roundRawCombined);
if (!parsedCalls.length) {
if (looksLikeToolAttempt(roundRawCombined)) {
const closestHint = suggestClosestToolName(roundRawCombined);
conversationHistory.push({
role: 'assistant',
content: roundCleanProse.trim() || '(tool calls)',
});
conversationHistory.push({
role: 'user',
content: `[System: Tool call could not be parsed. Retry with valid JSON: {"tool":"<name>","params":{...}}.${closestHint ? ` ${closestHint}` : ''}]`,
});
nextUserPrompt = CLOUD_CONTINUE_PROMPT;
continue;
}
break;
}
const { repaired, issues } = repairToolCalls(parsedCalls, roundRawCombined);
parsedCalls = repaired.filter((c) => c.tool !== 'spawn_subagent');
let planBlockedAll = false;
if (mode.planning && parsedCalls.length > 0) {
const { calls: planCalls, blocked } = filterPlanModeToolCalls(parsedCalls);
if (blocked.length) {
console.log(`[CloudAgentic] Plan mode blocked: ${blocked.map((c) => c.tool).join(', ')}`);
}
if (parsedCalls.length > 0 && planCalls.length === 0 && blocked.length > 0) {
planBlockedAll = true;
}
parsedCalls = planCalls;
}
if (!parsedCalls.length) {
if (planBlockedAll) {
conversationHistory.push({
role: 'assistant',
content: roundCleanProse.trim() || '(tool calls)',
});
conversationHistory.push({ role: 'user', content: PLAN_BLOCKED_TOOLS_MSG });
nextUserPrompt = CLOUD_CONTINUE_PROMPT;
continue;
}
if (issues?.length) {
conversationHistory.push({
role: 'assistant',
content: roundCleanProse.trim() || '(tool calls)',
});
conversationHistory.push({
role: 'user',
content: `[System: Tool Validation Failed]\n${issues.join('\n')}\n\nRetry with valid tool parameters.`,
});
nextUserPrompt = CLOUD_CONTINUE_PROMPT;
continue;
}
if (looksLikeToolAttempt(roundRawCombined)) {
const closestHint = suggestClosestToolName(roundRawCombined);
conversationHistory.push({
role: 'assistant',
content: roundCleanProse.trim() || '(tool calls)',
});
conversationHistory.push({
role: 'user',
content: `[System: Tool call could not be parsed. Retry with valid JSON: {"tool":"<name>","params":{...}}.${closestHint ? ` ${closestHint}` : ''}]`,
});
nextUserPrompt = CLOUD_CONTINUE_PROMPT;
continue;
}
break;
}
const visibleAssistant = roundCleanProse.trim();
conversationHistory.push({ role: 'assistant', content: visibleAssistant || '(tool calls)' });
const toolResultLines = [];
for (const call of parsedCalls) {
if (getCancelled?.()) break;
const toolName = call.tool;
const toolParams = call.params || {};
totalToolCalls++;
if (onStreamEvent) {
onStreamEvent('tool-generating', { tool: toolName });
onStreamEvent('tool-executing', [{ tool: toolName, params: toolParams }]);
}
if (
FILE_WRITE_OPS.has(toolName)
&& call.params?.content
&& onStreamEvent
&& shouldStreamFileContentForAgent(settings, call.params.filePath || call.params.path || '')
) {
const filePath = call.params.filePath || call.params.path || '';
const fileName = filePath.split(/[\\/]/).pop() || filePath;
const ext = fileName.includes('.') ? fileName.split('.').pop().toLowerCase() : '';
onStreamEvent('file-content-block-complete', {
filePath,
fileName,
language: ext,
fileKey: filePath,
content: String(call.params.content),
op: 'write',
});
}
if (
FILE_EDIT_OPS.has(toolName)
&& call.params?.newText != null
&& onStreamEvent
&& shouldStreamFileContentForAgent(settings, call.params.filePath || call.params.path || '')
) {
const filePath = call.params.filePath || call.params.path || '';
const fileName = filePath.split(/[\\/]/).pop() || filePath;
const ext = fileName.includes('.') ? fileName.split('.').pop().toLowerCase() : '';
onStreamEvent('file-content-block-complete', {
filePath,
fileName,
language: ext,
fileKey: filePath,
content: String(call.params.newText),
op: 'edit',
oldText: call.params.oldText != null ? String(call.params.oldText) : '',
newText: String(call.params.newText),
});
}
let toolResult;
try {
toolResult = await executeToolFn(toolName, toolParams);
} catch (e) {
toolResult = { success: false, error: e.message };
}
if (onStreamEvent) {
onStreamEvent('mcp-tool-results', [{ tool: toolName, result: toolResult }]);
}
const injectResult = formatToolResultForInject(toolName, toolResult, { contextTokens });
toolResultLines.push(`${toolName}: ${injectResult}`);
console.log(`[CloudAgentic] tool ${toolName} done (${injectResult.length} chars inject)`);
}
if (!toolResultLines.length) break;
let todoListPrefix = '';
const activeTodos = typeof getActiveTodos === 'function' ? getActiveTodos() : [];
if (Array.isArray(activeTodos) && activeTodos.length > 0 && activeTodos.some((t) => t.status === 'in-progress')) {
todoListPrefix = '[Active todo list: mark completed items with update_todo(id, "done").]\n\n';
}
const injectText = buildToolResultsUserMessage(toolResultLines, { interruptPrefix: todoListPrefix });
conversationHistory.push({ role: 'user', content: injectText });
console.log(`[CloudAgentic] ─── TOOL RESULTS → MODEL ─── ${toolResultLines.length} result(s)`);
nextUserPrompt = CLOUD_CONTINUE_PROMPT;
}
const finalText = displayResponse || stripToolCallText(fullResponse);
return { text: finalText, toolCallCount: totalToolCalls };
}
module.exports = { runCloudAgenticChat };