@xagent-ai/cli 1.2.0 → 1.2.2

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.
Files changed (80) hide show
  1. package/README.md +1 -1
  2. package/README_CN.md +1 -1
  3. package/dist/agents.js +164 -164
  4. package/dist/agents.js.map +1 -1
  5. package/dist/ai-client.d.ts +4 -6
  6. package/dist/ai-client.d.ts.map +1 -1
  7. package/dist/ai-client.js +137 -115
  8. package/dist/ai-client.js.map +1 -1
  9. package/dist/auth.js +4 -4
  10. package/dist/auth.js.map +1 -1
  11. package/dist/cli.js +184 -1
  12. package/dist/cli.js.map +1 -1
  13. package/dist/config.js +3 -3
  14. package/dist/config.js.map +1 -1
  15. package/dist/context-compressor.d.ts.map +1 -1
  16. package/dist/context-compressor.js +65 -81
  17. package/dist/context-compressor.js.map +1 -1
  18. package/dist/conversation.d.ts +1 -1
  19. package/dist/conversation.d.ts.map +1 -1
  20. package/dist/conversation.js +5 -31
  21. package/dist/conversation.js.map +1 -1
  22. package/dist/memory.d.ts +5 -1
  23. package/dist/memory.d.ts.map +1 -1
  24. package/dist/memory.js +77 -37
  25. package/dist/memory.js.map +1 -1
  26. package/dist/remote-ai-client.d.ts +1 -8
  27. package/dist/remote-ai-client.d.ts.map +1 -1
  28. package/dist/remote-ai-client.js +55 -65
  29. package/dist/remote-ai-client.js.map +1 -1
  30. package/dist/retry.d.ts +35 -0
  31. package/dist/retry.d.ts.map +1 -0
  32. package/dist/retry.js +166 -0
  33. package/dist/retry.js.map +1 -0
  34. package/dist/session.d.ts +0 -5
  35. package/dist/session.d.ts.map +1 -1
  36. package/dist/session.js +243 -312
  37. package/dist/session.js.map +1 -1
  38. package/dist/slash-commands.d.ts +1 -0
  39. package/dist/slash-commands.d.ts.map +1 -1
  40. package/dist/slash-commands.js +91 -9
  41. package/dist/slash-commands.js.map +1 -1
  42. package/dist/smart-approval.d.ts.map +1 -1
  43. package/dist/smart-approval.js +18 -17
  44. package/dist/smart-approval.js.map +1 -1
  45. package/dist/system-prompt-generator.d.ts.map +1 -1
  46. package/dist/system-prompt-generator.js +149 -139
  47. package/dist/system-prompt-generator.js.map +1 -1
  48. package/dist/theme.d.ts +48 -0
  49. package/dist/theme.d.ts.map +1 -1
  50. package/dist/theme.js +254 -0
  51. package/dist/theme.js.map +1 -1
  52. package/dist/tools/edit-diff.d.ts +32 -0
  53. package/dist/tools/edit-diff.d.ts.map +1 -0
  54. package/dist/tools/edit-diff.js +185 -0
  55. package/dist/tools/edit-diff.js.map +1 -0
  56. package/dist/tools/edit.d.ts +11 -0
  57. package/dist/tools/edit.d.ts.map +1 -0
  58. package/dist/tools/edit.js +129 -0
  59. package/dist/tools/edit.js.map +1 -0
  60. package/dist/tools.d.ts +19 -5
  61. package/dist/tools.d.ts.map +1 -1
  62. package/dist/tools.js +979 -631
  63. package/dist/tools.js.map +1 -1
  64. package/dist/types.d.ts +6 -31
  65. package/dist/types.d.ts.map +1 -1
  66. package/package.json +3 -2
  67. package/src/agents.ts +504 -504
  68. package/src/ai-client.ts +1559 -1458
  69. package/src/auth.ts +4 -4
  70. package/src/cli.ts +195 -1
  71. package/src/config.ts +3 -3
  72. package/src/memory.ts +55 -14
  73. package/src/remote-ai-client.ts +663 -683
  74. package/src/retry.ts +217 -0
  75. package/src/session.ts +1736 -1840
  76. package/src/slash-commands.ts +98 -9
  77. package/src/smart-approval.ts +626 -625
  78. package/src/system-prompt-generator.ts +853 -843
  79. package/src/theme.ts +284 -0
  80. package/src/tools.ts +390 -70
@@ -1,684 +1,664 @@
1
- import { EventEmitter } from 'events';
2
- import https from 'https';
3
- import axios from 'axios';
4
- import { ChatMessage, SessionOutput, ToolCall } from './types.js';
5
- import { ChatCompletionResponse, ChatCompletionOptions, Message } from './ai-client.js';
6
- import { getLogger } from './logger.js';
7
-
8
- const logger = getLogger();
9
-
10
- /**
11
- * Token invalid error - thrown when the authentication token is no longer valid
12
- */
13
- export class TokenInvalidError extends Error {
14
- constructor(message: string = 'Authentication token is invalid or expired') {
15
- super(message);
16
- this.name = 'TokenInvalidError';
17
- }
18
- }
19
-
20
- export interface RemoteChatOptions {
21
- model?: string;
22
- taskId?: string;
23
- status?: 'begin' | 'continue' | 'end' | 'cancel';
24
- conversationId?: string;
25
- context?: {
26
- cwd?: string;
27
- workspace?: string;
28
- recentFiles?: string[];
29
- };
30
- toolResults?: Array<{
31
- toolCallId: string;
32
- toolName: string;
33
- result: any;
34
- }>;
35
- tools?: Array<{
36
- type: 'function';
37
- function: {
38
- name: string;
39
- description: string;
40
- parameters: {
41
- type: 'object';
42
- properties: Record<string, any>;
43
- required?: string[];
44
- };
45
- };
46
- }>;
47
- signal?: AbortSignal;
48
- }
49
-
50
- export interface RemoteChatResponse {
51
- content: string;
52
- reasoningContent?: string;
53
- toolCalls?: ToolCall[];
54
- conversationId: string;
55
- }
56
-
57
- export interface RemoteVLMResponse {
58
- content: string;
59
- }
60
-
61
- /**
62
- * Remote AI Client - communicates with xagent-web service
63
- */
64
- export class RemoteAIClient extends EventEmitter {
65
- private authToken: string;
66
- private webBaseUrl: string;
67
- private agentApi: string;
68
- private vlmApi: string;
69
- private showAIDebugInfo: boolean;
70
-
71
- constructor(authToken: string, webBaseUrl: string, showAIDebugInfo: boolean = false) {
72
- super();
73
- logger.debug(`[RemoteAIClient] Constructor called, authToken: ${authToken ? authToken.substring(0, 30) + '...' : 'empty'}`);
74
- this.authToken = authToken;
75
- this.webBaseUrl = webBaseUrl.replace(/\/$/, ''); // Remove trailing slash
76
- this.agentApi = `${this.webBaseUrl}/api/agent`;
77
- this.vlmApi = `${this.webBaseUrl}/api/agent/vlm`;
78
- this.showAIDebugInfo = showAIDebugInfo;
79
-
80
- if (this.showAIDebugInfo) {
81
- logger.debug('[RemoteAIClient] Initialization complete');
82
- logger.debug(`[RemoteAIClient] Web Base URL: ${this.webBaseUrl}`);
83
- logger.debug(`[RemoteAIClient] Agent API: ${this.agentApi}`);
84
- logger.debug(`[RemoteAIClient] VLM API: ${this.vlmApi}`);
85
- }
86
- }
87
-
88
- /**
89
- * Non-streaming chat - send messages and receive full response
90
- */
91
- async chat(
92
- messages: ChatMessage[],
93
- remoteChatOptions: RemoteChatOptions = {}
94
- ): Promise<SessionOutput> {
95
- // Pass complete messages array to backend, backend forwards directly to LLM
96
- const requestBody = {
97
- messages: messages, // Pass complete message history
98
- taskId: remoteChatOptions.taskId,
99
- status: remoteChatOptions.status || 'begin',
100
- conversationId: remoteChatOptions.conversationId,
101
- context: remoteChatOptions.context,
102
- options: {
103
- model: remoteChatOptions.model
104
- },
105
- toolResults: remoteChatOptions.toolResults,
106
- tools: remoteChatOptions.tools
107
- };
108
-
109
- const url = `${this.agentApi}/chat`;
110
- if (this.showAIDebugInfo) {
111
- logger.debug(`[RemoteAIClient] Sending request to: ${url}`);
112
- logger.debug(`[RemoteAIClient] Token prefix: ${this.authToken.substring(0, 20)}...`);
113
- logger.debug(`[RemoteAIClient] Message count: ${messages.length}`);
114
- if (remoteChatOptions.tools) {
115
- logger.debug(`[RemoteAIClient] Tool count: ${remoteChatOptions.tools.length}`);
116
- }
117
- }
118
-
119
- const httpsAgent = new https.Agent({ rejectUnauthorized: false });
120
-
121
- try {
122
- const response = await axios.post(url, requestBody, {
123
- headers: {
124
- 'Content-Type': 'application/json',
125
- 'Authorization': `Bearer ${this.authToken}`
126
- },
127
- httpsAgent,
128
- timeout: 120000
129
- });
130
-
131
- // Check for 401 and throw TokenInvalidError
132
- if (response.status === 401) {
133
- throw new TokenInvalidError('Authentication token is invalid or expired. Please log in again.');
134
- }
135
-
136
- const data = response.data;
137
- logger.debug('[RemoteAIClient] response received, status:', String(response.status));
138
- if (this.showAIDebugInfo) {
139
- console.log('[RemoteAIClient] Received response, content length:', data.content?.length || 0);
140
- console.log('[RemoteAIClient] toolCalls count:', data.toolCalls?.length || 0);
141
- }
142
-
143
- return {
144
- role: 'assistant',
145
- content: data.content || '',
146
- reasoningContent: data.reasoningContent || '',
147
- toolCalls: data.toolCalls,
148
- timestamp: Date.now()
149
- };
150
-
151
- } catch (error: any) {
152
- if (this.showAIDebugInfo) {
153
- console.log('[RemoteAIClient] Request exception:', error.message);
154
- }
155
-
156
- // Provide user-friendly error messages based on status code
157
- if (error.response) {
158
- const status = error.response.status;
159
- let errorMessage: string;
160
- let userFriendlyMessage: string;
161
-
162
- switch (status) {
163
- case 400:
164
- errorMessage = 'Bad Request';
165
- userFriendlyMessage = 'Invalid request parameters. Please check your input and try again.';
166
- break;
167
- case 401:
168
- throw new TokenInvalidError('Authentication token is invalid or expired. Please log in again.');
169
- case 413:
170
- errorMessage = 'Payload Too Large';
171
- userFriendlyMessage = 'Request data is too large. Please reduce input content or screenshot size and try again.';
172
- break;
173
- case 429:
174
- errorMessage = 'Too Many Requests';
175
- userFriendlyMessage = 'XAgent service rate limit exceeded. Please wait a moment and try again.';
176
- break;
177
- case 500:
178
- // Try to parse server's detailed error message
179
- try {
180
- const errorData = error.response.data || null;
181
- errorMessage = errorData?.error || 'Internal Server Error';
182
- if (errorData?.error && errorData?.errorType === 'AI_SERVICE_ERROR') {
183
- userFriendlyMessage = `${errorData.error}\n\nSuggestion: ${errorData.suggestion}`;
184
- } else {
185
- userFriendlyMessage = errorData?.error || 'Server error. Please try again later. If the problem persists, contact the administrator.';
186
- }
187
- } catch {
188
- errorMessage = 'Internal Server Error';
189
- userFriendlyMessage = 'Server error. Please try again later. If the problem persists, contact the administrator.';
190
- }
191
- break;
192
- case 502:
193
- errorMessage = 'Bad Gateway';
194
- userFriendlyMessage = 'Gateway error. Service temporarily unavailable. Please try again later.';
195
- break;
196
- case 503:
197
- errorMessage = 'Service Unavailable';
198
- userFriendlyMessage = 'AI service request timed out. Please try again.';
199
- break;
200
- case 504:
201
- errorMessage = 'Gateway Timeout';
202
- userFriendlyMessage = 'Gateway timeout. Please try again later.';
203
- break;
204
- default:
205
- try {
206
- errorMessage = error.response.data?.error || `HTTP ${status}`;
207
- } catch {
208
- errorMessage = `HTTP ${status}`;
209
- }
210
- userFriendlyMessage = `Request failed with status code: ${status}`;
211
- }
212
-
213
- // Print user-friendly error message
214
- console.error(`\n❌ Request failed (${status})`);
215
- console.error(` ${userFriendlyMessage}`);
216
- if (this.showAIDebugInfo) {
217
- console.error(` Original error: ${errorMessage}`);
218
- }
219
- throw new Error(userFriendlyMessage);
220
- }
221
-
222
- // Network error or other error
223
- throw error;
224
- }
225
- }
226
-
227
- /**
228
- * Mark task as completed
229
- * Call backend to update task status to 'end'
230
- */
231
- async completeTask(taskId: string): Promise<void> {
232
- if (!taskId) {
233
- logger.debug('[RemoteAIClient] completeTask called with empty taskId, skipping');
234
- return;
235
- }
236
-
237
- logger.debug(`[RemoteAIClient] completeTask called: taskId=${taskId}`);
238
-
239
- const url = `${this.agentApi}/chat`;
240
- const requestBody = {
241
- taskId,
242
- status: 'end',
243
- messages: [],
244
- options: {}
245
- };
246
-
247
- const httpsAgent = new https.Agent({ rejectUnauthorized: false });
248
-
249
- try {
250
- const response = await axios.post(url, requestBody, {
251
- headers: {
252
- 'Content-Type': 'application/json',
253
- 'Authorization': `Bearer ${this.authToken}`
254
- },
255
- httpsAgent
256
- });
257
- logger.debug(`[RemoteAIClient] completeTask response status: ${response.status}`);
258
- } catch (error) {
259
- console.error('[RemoteAIClient] Failed to mark task as completed:', error);
260
- }
261
- }
262
-
263
- /**
264
- * Mark task as cancelled
265
- * Call backend to update task status to 'cancel'
266
- */
267
- async cancelTask(taskId: string): Promise<void> {
268
- if (!taskId) return;
269
-
270
- const url = `${this.agentApi}/chat`;
271
- const requestBody = {
272
- taskId,
273
- status: 'cancel',
274
- messages: [],
275
- options: {}
276
- };
277
-
278
- const httpsAgent = new https.Agent({ rejectUnauthorized: false });
279
-
280
- try {
281
- await axios.post(url, requestBody, {
282
- headers: {
283
- 'Content-Type': 'application/json',
284
- 'Authorization': `Bearer ${this.authToken}`
285
- },
286
- httpsAgent
287
- });
288
- } catch (error) {
289
- console.error('[RemoteAIClient] Failed to mark task as cancelled:', error);
290
- }
291
- }
292
-
293
- /**
294
- * Unified LLM call interface - same return type as aiClient.chatCompletion
295
- * Implements transparency: caller doesn't need to know remote vs local mode
296
- */
297
- async chatCompletion(
298
- messages: ChatMessage[],
299
- options: ChatCompletionOptions = {}
300
- ): Promise<ChatCompletionResponse> {
301
- const model = options.model || 'remote-llm';
302
-
303
- // Debug output for request
304
- if (this.showAIDebugInfo) {
305
- console.log('\n╔══════════════════════════════════════════════════════════╗');
306
- console.log('║ AI REQUEST DEBUG (REMOTE) ║');
307
- console.log('╚══════════════════════════════════════════════════════════╝');
308
- console.log(`📦 Model: ${model}`);
309
- console.log(`🌐 Base URL: ${this.webBaseUrl}`);
310
- console.log(`💬 Total Messages: ${messages.length} items`);
311
- if (options.temperature !== undefined) console.log(`🌡️ Temperature: ${options.temperature}`);
312
- if (options.maxTokens) console.log(`📏 Max Tokens: ${options.maxTokens}`);
313
- if (options.tools?.length) console.log(`🔧 Tools: ${options.tools.length} items`);
314
- if (options.thinkingTokens) console.log(`🧠 Thinking Tokens: ${options.thinkingTokens}`);
315
- console.log('─'.repeat(60));
316
-
317
- // Display system messages separately
318
- const systemMsgs = messages.filter(m => m.role === 'system');
319
- const otherMsgs = messages.filter(m => m.role !== 'system');
320
-
321
- if (systemMsgs.length > 0) {
322
- const systemContent = typeof systemMsgs[0].content === 'string'
323
- ? systemMsgs[0].content
324
- : JSON.stringify(systemMsgs[0].content);
325
- console.log('\n┌─────────────────────────────────────────────────────────────┐');
326
- console.log('│ 🟫 SYSTEM │');
327
- console.log('├─────────────────────────────────────────────────────────────┤');
328
- console.log(this.renderMarkdown(systemContent).split('\n').map(l => '' + l).join('\n'));
329
- console.log('└─────────────────────────────────────────────────────────────┘');
330
- }
331
-
332
- // Display other messages
333
- this.displayMessages(otherMsgs);
334
-
335
- console.log('\n📤 Sending request to Remote API...\n');
336
- }
337
-
338
- // Call existing chat method
339
- const response = await this.chat(messages, {
340
- conversationId: undefined,
341
- tools: options.tools as any,
342
- toolResults: undefined,
343
- context: undefined,
344
- model: options.model,
345
- taskId: (options as any).taskId,
346
- status: 'begin' // Mark as beginning of task
347
- });
348
-
349
- // Debug output for response
350
- if (this.showAIDebugInfo) {
351
- console.log('\n╔══════════════════════════════════════════════════════════╗');
352
- console.log('║ AI RESPONSE DEBUG (REMOTE) ║');
353
- console.log('╚══════════════════════════════════════════════════════════╝');
354
- console.log(`🆔 ID: remote-${Date.now()}`);
355
- console.log(`🤖 Model: ${model}`);
356
- console.log(`🏁 Finish Reason: stop`);
357
-
358
- console.log('\n┌─────────────────────────────────────────────────────────────┐');
359
- console.log('│ 🤖 ASSISTANT │');
360
- console.log('├─────────────────────────────────────────────────────────────┤');
361
-
362
- // Display reasoning_content (if present)
363
- if (response.reasoningContent) {
364
- console.log('│ 🧠 REASONING:');
365
- console.log('│ ───────────────────────────────────────────────────────────');
366
- const reasoningLines = this.renderMarkdown(response.reasoningContent).split('\n');
367
- for (const line of reasoningLines.slice(0, 15)) {
368
- console.log('│ ' + line.slice(0, 62));
369
- }
370
- if (response.reasoningContent.length > 800) console.log('│ ... (truncated)');
371
- console.log('│ ───────────────────────────────────────────────────────────');
372
- }
373
-
374
- // Display content
375
- console.log('│ 💬 CONTENT:');
376
- console.log('│ ───────────────────────────────────────────────────────────');
377
- const lines = this.renderMarkdown(response.content).split('\n');
378
- for (const line of lines.slice(0, 40)) {
379
- console.log('│ ' + line.slice(0, 62));
380
- }
381
- if (lines.length > 40) {
382
- console.log(`│ ... (${lines.length - 40} more lines)`);
383
- }
384
- console.log('└─────────────────────────────────────────────────────────────┘');
385
-
386
- // Display tool calls if present
387
- if (response.toolCalls && response.toolCalls.length > 0) {
388
- console.log('\n┌─────────────────────────────────────────────────────────────┐');
389
- console.log('│ 🔧 TOOL CALLS │');
390
- console.log('├─────────────────────────────────────────────────────────────┤');
391
- for (let i = 0; i < response.toolCalls.length; i++) {
392
- const tc = response.toolCalls[i];
393
- console.log(`│ ${i + 1}. ${tc.function?.name || 'unknown'}`);
394
- if (tc.function?.arguments) {
395
- const args = typeof tc.function.arguments === 'string'
396
- ? JSON.parse(tc.function.arguments)
397
- : tc.function.arguments;
398
- const argsStr = JSON.stringify(args, null, 2).split('\n').slice(0, 5).join('\n');
399
- console.log('│ Args:', argsStr.slice(0, 50) + (argsStr.length > 50 ? '...' : ''));
400
- }
401
- }
402
- console.log('└─────────────────────────────────────────────────────────────┘');
403
- }
404
-
405
- console.log('\n╔══════════════════════════════════════════════════════════╗');
406
- console.log('║ RESPONSE ENDED ║');
407
- console.log('╚══════════════════════════════════════════════════════════╝\n');
408
- }
409
-
410
- // Convert to ChatCompletionResponse format (consistent with local mode)
411
- return {
412
- id: `remote-${Date.now()}`,
413
- object: 'chat.completion',
414
- created: Date.now(),
415
- model: options.model || 'remote-llm',
416
- choices: [
417
- {
418
- index: 0,
419
- message: {
420
- role: 'assistant',
421
- content: response.content,
422
- reasoning_content: response.reasoningContent || '',
423
- tool_calls: response.toolCalls
424
- },
425
- finish_reason: 'stop'
426
- }
427
- ],
428
- usage: undefined
429
- };
430
- }
431
-
432
- /**
433
- * Render markdown text (helper method for debug output)
434
- */
435
- private renderMarkdown(text: string): string {
436
- let result = text;
437
- // Code block rendering
438
- result = result.replace(/```(\w*)\n([\s\S]*?)```/g, (_, lang, code) => {
439
- return `\n┌─[${lang || 'code'}]\n${code.trim().split('\n').map((l: string) => '│ ' + l).join('\n')}\n└─\n`;
440
- });
441
- // Inline code rendering
442
- result = result.replace(/`([^`]+)`/g, '`$1`');
443
- // Bold rendering
444
- result = result.replace(/\*\*([^*]+)\*\*/g, '●$1○');
445
- // Italic rendering
446
- result = result.replace(/\*([^*]+)\*/g, '/$1/');
447
- // List rendering
448
- result = result.replace(/^- (.*$)/gm, '○ $1');
449
- result = result.replace(/^\d+\. (.*$)/gm, '• $1');
450
- // Heading rendering
451
- result = result.replace(/^### (.*$)/gm, '\n━━━ $1 ━━━\n');
452
- result = result.replace(/^## (.*$)/gm, '\n━━━━━ $1 ━━━━━\n');
453
- result = result.replace(/^# (.*$)/gm, '\n━━━━━━━ $1 ━━━━━━━\n');
454
- // Quote rendering
455
- result = result.replace(/^> (.*$)/gm, '│ │ $1');
456
- return result;
457
- }
458
-
459
- /**
460
- * Display messages by category (helper method for debug output)
461
- */
462
- private displayMessages(messages: ChatMessage[]): void {
463
- const roleColors: Record<string, string> = {
464
- system: '🟫 SYSTEM',
465
- user: '👤 USER',
466
- assistant: '🤖 ASSISTANT',
467
- tool: '🔧 TOOL'
468
- };
469
-
470
- for (let i = 0; i < messages.length; i++) {
471
- const msg = messages[i];
472
- const role = msg.role as string;
473
- const roleLabel = roleColors[role] || `● ${role.toUpperCase()}`;
474
-
475
- console.log(`\n┌─────────────────────────────────────────────────────────────┐`);
476
- console.log(`│ ${roleLabel} (${i + 1}/${messages.length}) │`);
477
- console.log('├─────────────────────────────────────────────────────────────┤');
478
-
479
- // Display reasoning_content (if present) - check both camelCase and snake_case
480
- const reasoningContent = (msg as any).reasoningContent || (msg as any).reasoning_content;
481
- if (reasoningContent) {
482
- console.log('│ 🧠 REASONING:');
483
- console.log('│ ───────────────────────────────────────────────────────────');
484
- const reasoningLines = this.renderMarkdown(reasoningContent).split('\n');
485
- for (const line of reasoningLines.slice(0, 20)) {
486
- console.log('│ ' + line.slice(0, 62));
487
- }
488
- if (reasoningContent.length > 1000) console.log('│ ... (truncated)');
489
- console.log('│ ───────────────────────────────────────────────────────────');
490
- }
491
-
492
- // Display main content
493
- const content = typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content);
494
-
495
- const lines = this.renderMarkdown(content).split('\n');
496
- for (const line of lines.slice(0, 50)) {
497
- console.log('│ ' + line.slice(0, 62));
498
- }
499
- if (lines.length > 50) {
500
- console.log('│ ... (' + (lines.length - 50) + ' more lines)');
501
- }
502
-
503
- console.log('└─────────────────────────────────────────────────────────────┘');
504
- }
505
- }
506
-
507
- /**
508
- * Invoke VLM for image understanding
509
- * @param messages - full messages array (consistent with local mode)
510
- * @param systemPrompt - system prompt (optional, for reference)
511
- * @param remoteChatOptions - other options including AbortSignal, taskId
512
- */
513
- async invokeVLM(
514
- messages: any[],
515
- _systemPrompt?: string,
516
- remoteChatOptions: RemoteChatOptions = {}
517
- ): Promise<string> {
518
- // Forward complete messages to backend (same format as local mode)
519
- const requestBody = {
520
- messages, // Pass complete messages array
521
- taskId: remoteChatOptions.taskId,
522
- status: remoteChatOptions.status || 'begin',
523
- context: remoteChatOptions.context,
524
- options: {
525
- model: remoteChatOptions.model
526
- }
527
- };
528
-
529
- if (this.showAIDebugInfo) {
530
- console.log('[RemoteAIClient] VLM sending request to:', this.vlmApi);
531
- }
532
-
533
- // Handle abort signal
534
- const controller = remoteChatOptions.signal ? new AbortController() : undefined;
535
- const abortSignal = remoteChatOptions.signal || controller?.signal;
536
-
537
- // If external signal is provided, listen to it
538
- if (remoteChatOptions.signal) {
539
- remoteChatOptions.signal.addEventListener?.('abort', () => controller?.abort());
540
- }
541
-
542
- try {
543
- const response = await axios.post(this.vlmApi, requestBody, {
544
- headers: {
545
- 'Content-Type': 'application/json',
546
- 'Authorization': `Bearer ${this.authToken}`
547
- },
548
- signal: abortSignal,
549
- httpsAgent: new https.Agent({ rejectUnauthorized: false }),
550
- timeout: 120000
551
- });
552
-
553
- if (this.showAIDebugInfo) {
554
- console.log('[RemoteAIClient] VLM response status:', response.status);
555
- }
556
-
557
- const data = response.data as RemoteVLMResponse;
558
- return data.content || '';
559
-
560
- } catch (error: any) {
561
- if (this.showAIDebugInfo) {
562
- console.log('[RemoteAIClient] VLM request exception:', error.message);
563
- }
564
- throw error;
565
- }
566
- }
567
-
568
- /**
569
- * Validate if the current token is still valid
570
- * Returns true if valid, false otherwise
571
- */
572
- async validateToken(): Promise<boolean> {
573
- try {
574
- const url = `${this.webBaseUrl}/api/auth/me`;
575
- const httpsAgent = new https.Agent({ rejectUnauthorized: false });
576
- const response = await axios.get(url, {
577
- httpsAgent,
578
- timeout: 10000
579
- });
580
- return response.status === 200;
581
- } catch {
582
- return false;
583
- }
584
- }
585
-
586
- async getConversations(): Promise<any[]> {
587
- const url = `${this.agentApi}/conversations`;
588
- if (this.showAIDebugInfo) {
589
- console.log('[RemoteAIClient] Getting conversation list:', url);
590
- }
591
-
592
- const httpsAgent = new https.Agent({ rejectUnauthorized: false });
593
-
594
- const response = await axios.get(url, {
595
- headers: {
596
- 'Authorization': `Bearer ${this.authToken}`
597
- },
598
- httpsAgent
599
- });
600
-
601
- if (response.status !== 200) {
602
- throw new Error('Failed to get conversation list');
603
- }
604
-
605
- const data = response.data as { conversations?: any[] };
606
- return data.conversations || [];
607
- }
608
-
609
- /**
610
- * Get conversation details
611
- */
612
- async getConversation(conversationId: string): Promise<any> {
613
- const url = `${this.agentApi}/conversations/${conversationId}`;
614
- if (this.showAIDebugInfo) {
615
- console.log('[RemoteAIClient] Getting conversation details:', url);
616
- }
617
-
618
- const httpsAgent = new https.Agent({ rejectUnauthorized: false });
619
-
620
- const response = await axios.get(url, {
621
- headers: {
622
- 'Authorization': `Bearer ${this.authToken}`
623
- },
624
- httpsAgent
625
- });
626
-
627
- if (response.status !== 200) {
628
- throw new Error('Failed to get conversation details');
629
- }
630
-
631
- const data = response.data as { conversation?: any };
632
- return data.conversation;
633
- }
634
-
635
- /**
636
- * Create new conversation
637
- */
638
- async createConversation(title?: string): Promise<any> {
639
- const url = `${this.agentApi}/conversations`;
640
- if (this.showAIDebugInfo) {
641
- console.log('[RemoteAIClient] Creating conversation:', url);
642
- }
643
-
644
- const httpsAgent = new https.Agent({ rejectUnauthorized: false });
645
-
646
- const response = await axios.post(url, { title }, {
647
- headers: {
648
- 'Content-Type': 'application/json',
649
- 'Authorization': `Bearer ${this.authToken}`
650
- },
651
- httpsAgent
652
- });
653
-
654
- if (response.status !== 200) {
655
- throw new Error('Failed to create conversation');
656
- }
657
-
658
- const data = response.data as { conversation?: any };
659
- return data.conversation;
660
- }
661
-
662
- /**
663
- * Delete conversation
664
- */
665
- async deleteConversation(conversationId: string): Promise<void> {
666
- const url = `${this.agentApi}/conversations/${conversationId}`;
667
- if (this.showAIDebugInfo) {
668
- console.log('[RemoteAIClient] Deleting conversation:', url);
669
- }
670
-
671
- const httpsAgent = new https.Agent({ rejectUnauthorized: false });
672
-
673
- const response = await axios.delete(url, {
674
- headers: {
675
- 'Authorization': `Bearer ${this.authToken}`
676
- },
677
- httpsAgent
678
- });
679
-
680
- if (!response.status.toString().startsWith('2')) {
681
- throw new Error('Failed to delete conversation');
682
- }
683
- }
1
+ import { EventEmitter } from 'events';
2
+ import https from 'https';
3
+ import axios from 'axios';
4
+ import { ChatMessage, SessionOutput, ToolCall } from './types.js';
5
+ import { ChatCompletionResponse, ChatCompletionOptions, Message, renderMarkdown, displayMessages } from './ai-client.js';
6
+ import { getLogger } from './logger.js';
7
+ import { withRetry, RetryConfig } from './retry.js';
8
+
9
+ const logger = getLogger();
10
+
11
+ /**
12
+ * Token invalid error - thrown when the authentication token is no longer valid
13
+ */
14
+ export class TokenInvalidError extends Error {
15
+ constructor(message: string = 'Authentication token is invalid or expired') {
16
+ super(message);
17
+ this.name = 'TokenInvalidError';
18
+ }
19
+ }
20
+
21
+ export interface RemoteChatOptions {
22
+ model?: string;
23
+ taskId?: string;
24
+ status?: 'begin' | 'continue' | 'end' | 'cancel';
25
+ conversationId?: string;
26
+ context?: {
27
+ cwd?: string;
28
+ workspace?: string;
29
+ recentFiles?: string[];
30
+ };
31
+ toolResults?: Array<{
32
+ toolCallId: string;
33
+ toolName: string;
34
+ result: any;
35
+ }>;
36
+ tools?: Array<{
37
+ type: 'function';
38
+ function: {
39
+ name: string;
40
+ description: string;
41
+ parameters: {
42
+ type: 'object';
43
+ properties: Record<string, any>;
44
+ required?: string[];
45
+ };
46
+ };
47
+ }>;
48
+ signal?: AbortSignal;
49
+ }
50
+
51
+ export interface RemoteChatResponse {
52
+ content: string;
53
+ reasoningContent?: string;
54
+ toolCalls?: ToolCall[];
55
+ conversationId: string;
56
+ }
57
+
58
+ export interface RemoteVLMResponse {
59
+ content: string;
60
+ }
61
+
62
+ /**
63
+ * Remote AI Client - communicates with xagent-web service
64
+ */
65
+ export class RemoteAIClient extends EventEmitter {
66
+ private authToken: string;
67
+ private webBaseUrl: string;
68
+ private agentApi: string;
69
+ private vlmApi: string;
70
+ private showAIDebugInfo: boolean;
71
+
72
+ constructor(authToken: string, webBaseUrl: string, showAIDebugInfo: boolean = false) {
73
+ super();
74
+ logger.debug(`[RemoteAIClient] Constructor called, authToken: ${authToken ? authToken.substring(0, 30) + '...' : 'empty'}`);
75
+ this.authToken = authToken;
76
+ this.webBaseUrl = webBaseUrl.replace(/\/$/, ''); // Remove trailing slash
77
+ this.agentApi = `${this.webBaseUrl}/api/agent`;
78
+ this.vlmApi = `${this.webBaseUrl}/api/agent/vlm`;
79
+ this.showAIDebugInfo = showAIDebugInfo;
80
+
81
+ if (this.showAIDebugInfo) {
82
+ logger.debug('[RemoteAIClient] Initialization complete');
83
+ logger.debug(`[RemoteAIClient] Web Base URL: ${this.webBaseUrl}`);
84
+ logger.debug(`[RemoteAIClient] Agent API: ${this.agentApi}`);
85
+ logger.debug(`[RemoteAIClient] VLM API: ${this.vlmApi}`);
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Non-streaming chat - send messages and receive full response
91
+ */
92
+ async chat(
93
+ messages: ChatMessage[],
94
+ remoteChatOptions: RemoteChatOptions = {}
95
+ ): Promise<SessionOutput> {
96
+ // Pass complete messages array to backend, backend forwards directly to LLM
97
+ const requestBody = {
98
+ messages: messages, // Pass complete message history
99
+ taskId: remoteChatOptions.taskId,
100
+ status: remoteChatOptions.status || 'begin',
101
+ conversationId: remoteChatOptions.conversationId,
102
+ context: remoteChatOptions.context,
103
+ options: {
104
+ model: remoteChatOptions.model
105
+ },
106
+ toolResults: remoteChatOptions.toolResults,
107
+ tools: remoteChatOptions.tools
108
+ };
109
+
110
+ const url = `${this.agentApi}/chat`;
111
+ if (this.showAIDebugInfo) {
112
+ logger.debug(`[RemoteAIClient] Sending request to: ${url}`);
113
+ logger.debug(`[RemoteAIClient] Token prefix: ${this.authToken.substring(0, 20)}...`);
114
+ logger.debug(`[RemoteAIClient] Message count: ${messages.length}`);
115
+ if (remoteChatOptions.tools) {
116
+ logger.debug(`[RemoteAIClient] Tool count: ${remoteChatOptions.tools.length}`);
117
+ }
118
+ }
119
+
120
+ const httpsAgent = new https.Agent({ rejectUnauthorized: false });
121
+
122
+ try {
123
+ const response = await axios.post(url, requestBody, {
124
+ headers: {
125
+ 'Content-Type': 'application/json',
126
+ 'Authorization': `Bearer ${this.authToken}`
127
+ },
128
+ httpsAgent,
129
+ timeout: 300000
130
+ });
131
+
132
+ // Check for 401 and throw TokenInvalidError
133
+ if (response.status === 401) {
134
+ throw new TokenInvalidError('Authentication token is invalid or expired. Please log in again.');
135
+ }
136
+
137
+ const data = response.data;
138
+ logger.debug('[RemoteAIClient] response received, status:', String(response.status));
139
+ if (this.showAIDebugInfo) {
140
+ console.log('[RemoteAIClient] Received response, content length:', data.content?.length || 0);
141
+ console.log('[RemoteAIClient] toolCalls count:', data.toolCalls?.length || 0);
142
+ }
143
+
144
+ return {
145
+ role: 'assistant',
146
+ content: data.content || '',
147
+ reasoningContent: data.reasoningContent || '',
148
+ toolCalls: data.toolCalls,
149
+ timestamp: Date.now()
150
+ };
151
+
152
+ } catch (error: any) {
153
+ if (this.showAIDebugInfo) {
154
+ console.log('[RemoteAIClient] Request exception:', error.message);
155
+ }
156
+
157
+ // Provide user-friendly error messages based on status code
158
+ if (error.response) {
159
+ const status = error.response.status;
160
+ let errorMessage: string;
161
+ let userFriendlyMessage: string;
162
+
163
+ switch (status) {
164
+ case 400:
165
+ errorMessage = 'Bad Request';
166
+ userFriendlyMessage = 'Invalid request parameters. Please check your input and try again.';
167
+ break;
168
+ case 401:
169
+ throw new TokenInvalidError('Authentication token is invalid or expired. Please log in again.');
170
+ case 413:
171
+ errorMessage = 'Payload Too Large';
172
+ userFriendlyMessage = 'Request data is too large. Please reduce input content or screenshot size and try again.';
173
+ break;
174
+ case 429:
175
+ errorMessage = 'Too Many Requests';
176
+ userFriendlyMessage = 'XAgent service rate limit exceeded. Please wait a moment and try again.';
177
+ break;
178
+ case 500:
179
+ // Try to parse server's detailed error message
180
+ try {
181
+ const errorData = error.response.data || null;
182
+ errorMessage = errorData?.error || 'Internal Server Error';
183
+ if (errorData?.error && errorData?.errorType === 'AI_SERVICE_ERROR') {
184
+ userFriendlyMessage = `${errorData.error}\n\nSuggestion: ${errorData.suggestion}`;
185
+ } else {
186
+ userFriendlyMessage = errorData?.error || 'Server error. Please try again later. If the problem persists, contact the administrator.';
187
+ }
188
+ } catch {
189
+ errorMessage = 'Internal Server Error';
190
+ userFriendlyMessage = 'Server error. Please try again later. If the problem persists, contact the administrator.';
191
+ }
192
+ break;
193
+ case 502:
194
+ errorMessage = 'Bad Gateway';
195
+ userFriendlyMessage = 'Gateway error. Service temporarily unavailable. Please try again later.';
196
+ break;
197
+ case 503:
198
+ errorMessage = 'Service Unavailable';
199
+ userFriendlyMessage = 'AI service request timed out. Please try again.';
200
+ break;
201
+ case 504:
202
+ errorMessage = 'Gateway Timeout';
203
+ userFriendlyMessage = 'Gateway timeout. Please try again later.';
204
+ break;
205
+ default:
206
+ try {
207
+ errorMessage = error.response.data?.error || `HTTP ${status}`;
208
+ } catch {
209
+ errorMessage = `HTTP ${status}`;
210
+ }
211
+ userFriendlyMessage = `Request failed with status code: ${status}`;
212
+ }
213
+
214
+ // Print user-friendly error message
215
+ console.error(`\n❌ Request failed (${status})`);
216
+ console.error(` ${userFriendlyMessage}`);
217
+ if (this.showAIDebugInfo) {
218
+ console.error(` Original error: ${errorMessage}`);
219
+ }
220
+ throw new Error(userFriendlyMessage);
221
+ }
222
+
223
+ // Network error or other error
224
+ // Check if error is retryable
225
+ const isRetryable = this.isRetryableError(error);
226
+ if (!isRetryable) {
227
+ throw error;
228
+ }
229
+
230
+ // Retry with exponential backoff
231
+ const retryResult = await withRetry(async () => {
232
+ const response = await axios.post(url, requestBody, {
233
+ headers: {
234
+ 'Content-Type': 'application/json',
235
+ 'Authorization': `Bearer ${this.authToken}`
236
+ },
237
+ httpsAgent,
238
+ timeout: 300000
239
+ });
240
+
241
+ if (response.status === 401) {
242
+ throw new TokenInvalidError('Authentication token is invalid or expired. Please log in again.');
243
+ }
244
+
245
+ return {
246
+ role: 'assistant' as const,
247
+ content: response.data.content || '',
248
+ reasoningContent: response.data.reasoningContent || '',
249
+ toolCalls: response.data.toolCalls,
250
+ timestamp: Date.now()
251
+ };
252
+ }, { maxRetries: 3, baseDelay: 1000, maxDelay: 10000, jitter: true });
253
+
254
+ if (!retryResult.success) {
255
+ throw retryResult.error || new Error('Retry failed');
256
+ }
257
+
258
+ if (!retryResult.data) {
259
+ throw new Error('Retry returned empty response');
260
+ }
261
+
262
+ return retryResult.data;
263
+ }
264
+ }
265
+
266
+ private isRetryableError(error: any): boolean {
267
+ // Timeout or network error (no response received)
268
+ if (error.code === 'ECONNABORTED' || !error.response) {
269
+ return true;
270
+ }
271
+ // 5xx server errors
272
+ if (error.response?.status && error.response.status >= 500) {
273
+ return true;
274
+ }
275
+ // 429 rate limit
276
+ if (error.response?.status === 429) {
277
+ return true;
278
+ }
279
+ return false;
280
+ }
281
+
282
+ /**
283
+ * Mark task as completed
284
+ * Call backend to update task status to 'end'
285
+ */
286
+ async completeTask(taskId: string): Promise<void> {
287
+ if (!taskId) {
288
+ logger.debug('[RemoteAIClient] completeTask called with empty taskId, skipping');
289
+ return;
290
+ }
291
+
292
+ logger.debug(`[RemoteAIClient] completeTask called: taskId=${taskId}`);
293
+
294
+ const url = `${this.agentApi}/chat`;
295
+ const requestBody = {
296
+ taskId,
297
+ status: 'end',
298
+ messages: [],
299
+ options: {}
300
+ };
301
+
302
+ const httpsAgent = new https.Agent({ rejectUnauthorized: false });
303
+
304
+ try {
305
+ const response = await axios.post(url, requestBody, {
306
+ headers: {
307
+ 'Content-Type': 'application/json',
308
+ 'Authorization': `Bearer ${this.authToken}`
309
+ },
310
+ httpsAgent
311
+ });
312
+ logger.debug(`[RemoteAIClient] completeTask response status: ${response.status}`);
313
+ } catch (error) {
314
+ console.error('[RemoteAIClient] Failed to mark task as completed:', error);
315
+ }
316
+ }
317
+
318
+ /**
319
+ * Mark task as cancelled
320
+ * Call backend to update task status to 'cancel'
321
+ */
322
+ async cancelTask(taskId: string): Promise<void> {
323
+ if (!taskId) return;
324
+
325
+ const url = `${this.agentApi}/chat`;
326
+ const requestBody = {
327
+ taskId,
328
+ status: 'cancel',
329
+ messages: [],
330
+ options: {}
331
+ };
332
+
333
+ const httpsAgent = new https.Agent({ rejectUnauthorized: false });
334
+
335
+ try {
336
+ await axios.post(url, requestBody, {
337
+ headers: {
338
+ 'Content-Type': 'application/json',
339
+ 'Authorization': `Bearer ${this.authToken}`
340
+ },
341
+ httpsAgent
342
+ });
343
+ } catch (error) {
344
+ console.error('[RemoteAIClient] Failed to mark task as cancelled:', error);
345
+ }
346
+ }
347
+
348
+ /**
349
+ * Unified LLM call interface - same return type as aiClient.chatCompletion
350
+ * Implements transparency: caller doesn't need to know remote vs local mode
351
+ */
352
+ async chatCompletion(
353
+ messages: ChatMessage[],
354
+ options: ChatCompletionOptions = {}
355
+ ): Promise<ChatCompletionResponse> {
356
+ const model = options.model || 'remote-llm';
357
+
358
+ // Debug output for request
359
+ if (this.showAIDebugInfo) {
360
+ console.log('\n╔══════════════════════════════════════════════════════════╗');
361
+ console.log('║ AI REQUEST DEBUG (REMOTE) ║');
362
+ console.log('╚══════════════════════════════════════════════════════════╝');
363
+ console.log(`📦 Model: ${model}`);
364
+ console.log(`🌐 Base URL: ${this.webBaseUrl}`);
365
+ console.log(`💬 Total Messages: ${messages.length} items`);
366
+ if (options.temperature !== undefined) console.log(`🌡️ Temperature: ${options.temperature}`);
367
+ if (options.maxTokens) console.log(`📏 Max Tokens: ${options.maxTokens}`);
368
+ if (options.tools?.length) console.log(`🔧 Tools: ${options.tools.length} items`);
369
+ if (options.thinkingTokens) console.log(`🧠 Thinking Tokens: ${options.thinkingTokens}`);
370
+ console.log('─'.repeat(60));
371
+
372
+ // Display system messages separately
373
+ const systemMsgs = messages.filter(m => m.role === 'system');
374
+ const otherMsgs = messages.filter(m => m.role !== 'system');
375
+
376
+ if (systemMsgs.length > 0) {
377
+ const systemContent = typeof systemMsgs[0].content === 'string'
378
+ ? systemMsgs[0].content
379
+ : JSON.stringify(systemMsgs[0].content);
380
+ console.log('\n┌─────────────────────────────────────────────────────────────┐');
381
+ console.log('│ 🟫 SYSTEM │');
382
+ console.log('├─────────────────────────────────────────────────────────────┤');
383
+ console.log(renderMarkdown(systemContent).split('\n').map(l => '│ ' + l).join('\n'));
384
+ console.log('└─────────────────────────────────────────────────────────────┘');
385
+ }
386
+
387
+ // Display other messages
388
+ displayMessages(otherMsgs);
389
+
390
+ console.log('\n📤 Sending request to Remote API...\n');
391
+ }
392
+
393
+ // Call existing chat method
394
+ const response = await this.chat(messages, {
395
+ conversationId: undefined,
396
+ tools: options.tools as any,
397
+ toolResults: undefined,
398
+ context: undefined,
399
+ model: options.model,
400
+ taskId: (options as any).taskId,
401
+ status: 'begin' // Mark as beginning of task
402
+ });
403
+
404
+ // Debug output for response
405
+ if (this.showAIDebugInfo) {
406
+ console.log('\n╔══════════════════════════════════════════════════════════╗');
407
+ console.log('║ AI RESPONSE DEBUG (REMOTE) ║');
408
+ console.log('╚══════════════════════════════════════════════════════════╝');
409
+ console.log(`🆔 ID: remote-${Date.now()}`);
410
+ console.log(`🤖 Model: ${model}`);
411
+ console.log(`🏁 Finish Reason: stop`);
412
+
413
+ console.log('\n┌─────────────────────────────────────────────────────────────┐');
414
+ console.log('│ 🤖 ASSISTANT │');
415
+ console.log('├─────────────────────────────────────────────────────────────┤');
416
+
417
+ // Display reasoning_content (if present)
418
+ if (response.reasoningContent) {
419
+ console.log('│ 🧠 REASONING:');
420
+ console.log('│ ───────────────────────────────────────────────────────────');
421
+ const reasoningLines = renderMarkdown(response.reasoningContent).split('\n');
422
+ for (const line of reasoningLines.slice(0, 15)) {
423
+ console.log('│ ' + line.slice(0, 62));
424
+ }
425
+ if (response.reasoningContent.length > 800) console.log('│ ... (truncated)');
426
+ console.log('│ ───────────────────────────────────────────────────────────');
427
+ }
428
+
429
+ // Display content
430
+ console.log('│ 💬 CONTENT:');
431
+ console.log('│ ───────────────────────────────────────────────────────────');
432
+ const lines = renderMarkdown(response.content).split('\n');
433
+ for (const line of lines.slice(0, 40)) {
434
+ console.log('│ ' + line.slice(0, 62));
435
+ }
436
+ if (lines.length > 40) {
437
+ console.log(`│ ... (${lines.length - 40} more lines)`);
438
+ }
439
+ console.log('└─────────────────────────────────────────────────────────────┘');
440
+
441
+ // Display tool calls if present
442
+ if (response.toolCalls && response.toolCalls.length > 0) {
443
+ console.log('\n┌─────────────────────────────────────────────────────────────┐');
444
+ console.log('│ 🔧 TOOL CALLS │');
445
+ console.log('├─────────────────────────────────────────────────────────────┤');
446
+ for (let i = 0; i < response.toolCalls.length; i++) {
447
+ const tc = response.toolCalls[i];
448
+ console.log(`│ ${i + 1}. ${tc.function?.name || 'unknown'}`);
449
+ if (tc.function?.arguments) {
450
+ const args = typeof tc.function.arguments === 'string'
451
+ ? JSON.parse(tc.function.arguments)
452
+ : tc.function.arguments;
453
+ const argsStr = JSON.stringify(args, null, 2).split('\n').slice(0, 5).join('\n');
454
+ console.log('│ Args:', argsStr.slice(0, 50) + (argsStr.length > 50 ? '...' : ''));
455
+ }
456
+ }
457
+ console.log('└─────────────────────────────────────────────────────────────┘');
458
+ }
459
+
460
+ console.log('\n╔══════════════════════════════════════════════════════════╗');
461
+ console.log('║ RESPONSE ENDED ║');
462
+ console.log('╚══════════════════════════════════════════════════════════╝\n');
463
+ }
464
+
465
+ // Convert to ChatCompletionResponse format (consistent with local mode)
466
+ return {
467
+ id: `remote-${Date.now()}`,
468
+ object: 'chat.completion',
469
+ created: Date.now(),
470
+ model: options.model || 'remote-llm',
471
+ choices: [
472
+ {
473
+ index: 0,
474
+ message: {
475
+ role: 'assistant',
476
+ content: response.content,
477
+ reasoning_content: response.reasoningContent || '',
478
+ tool_calls: response.toolCalls
479
+ },
480
+ finish_reason: 'stop'
481
+ }
482
+ ],
483
+ usage: undefined
484
+ };
485
+ }
486
+
487
+ /**
488
+ * Invoke VLM for image understanding
489
+ * @param messages - full messages array (consistent with local mode)
490
+ * @param systemPrompt - system prompt (optional, for reference)
491
+ * @param remoteChatOptions - other options including AbortSignal, taskId
492
+ */
493
+ async invokeVLM(
494
+ messages: any[],
495
+ _systemPrompt?: string,
496
+ remoteChatOptions: RemoteChatOptions = {}
497
+ ): Promise<string> {
498
+ // Forward complete messages to backend (same format as local mode)
499
+ const requestBody = {
500
+ messages, // Pass complete messages array
501
+ taskId: remoteChatOptions.taskId,
502
+ status: remoteChatOptions.status || 'begin',
503
+ context: remoteChatOptions.context,
504
+ options: {
505
+ model: remoteChatOptions.model
506
+ }
507
+ };
508
+
509
+ if (this.showAIDebugInfo) {
510
+ console.log('[RemoteAIClient] VLM sending request to:', this.vlmApi);
511
+ }
512
+
513
+ // Handle abort signal
514
+ const controller = remoteChatOptions.signal ? new AbortController() : undefined;
515
+ const abortSignal = remoteChatOptions.signal || controller?.signal;
516
+
517
+ // If external signal is provided, listen to it
518
+ if (remoteChatOptions.signal) {
519
+ remoteChatOptions.signal.addEventListener?.('abort', () => controller?.abort());
520
+ }
521
+
522
+ try {
523
+ const response = await axios.post(this.vlmApi, requestBody, {
524
+ headers: {
525
+ 'Content-Type': 'application/json',
526
+ 'Authorization': `Bearer ${this.authToken}`
527
+ },
528
+ signal: abortSignal,
529
+ httpsAgent: new https.Agent({ rejectUnauthorized: false }),
530
+ timeout: 120000
531
+ });
532
+
533
+ if (this.showAIDebugInfo) {
534
+ console.log('[RemoteAIClient] VLM response status:', response.status);
535
+ }
536
+
537
+ const data = response.data as RemoteVLMResponse;
538
+ return data.content || '';
539
+
540
+ } catch (error: any) {
541
+ if (this.showAIDebugInfo) {
542
+ console.log('[RemoteAIClient] VLM request exception:', error.message);
543
+ }
544
+ throw error;
545
+ }
546
+ }
547
+
548
+ /**
549
+ * Validate if the current token is still valid
550
+ * Returns true if valid, false otherwise
551
+ */
552
+ async validateToken(): Promise<boolean> {
553
+ try {
554
+ const url = `${this.webBaseUrl}/api/auth/me`;
555
+ const httpsAgent = new https.Agent({ rejectUnauthorized: false });
556
+ const response = await axios.get(url, {
557
+ httpsAgent,
558
+ timeout: 10000
559
+ });
560
+ return response.status === 200;
561
+ } catch {
562
+ return false;
563
+ }
564
+ }
565
+
566
+ async getConversations(): Promise<any[]> {
567
+ const url = `${this.agentApi}/conversations`;
568
+ if (this.showAIDebugInfo) {
569
+ console.log('[RemoteAIClient] Getting conversation list:', url);
570
+ }
571
+
572
+ const httpsAgent = new https.Agent({ rejectUnauthorized: false });
573
+
574
+ const response = await axios.get(url, {
575
+ headers: {
576
+ 'Authorization': `Bearer ${this.authToken}`
577
+ },
578
+ httpsAgent
579
+ });
580
+
581
+ if (response.status !== 200) {
582
+ throw new Error('Failed to get conversation list');
583
+ }
584
+
585
+ const data = response.data as { conversations?: any[] };
586
+ return data.conversations || [];
587
+ }
588
+
589
+ /**
590
+ * Get conversation details
591
+ */
592
+ async getConversation(conversationId: string): Promise<any> {
593
+ const url = `${this.agentApi}/conversations/${conversationId}`;
594
+ if (this.showAIDebugInfo) {
595
+ console.log('[RemoteAIClient] Getting conversation details:', url);
596
+ }
597
+
598
+ const httpsAgent = new https.Agent({ rejectUnauthorized: false });
599
+
600
+ const response = await axios.get(url, {
601
+ headers: {
602
+ 'Authorization': `Bearer ${this.authToken}`
603
+ },
604
+ httpsAgent
605
+ });
606
+
607
+ if (response.status !== 200) {
608
+ throw new Error('Failed to get conversation details');
609
+ }
610
+
611
+ const data = response.data as { conversation?: any };
612
+ return data.conversation;
613
+ }
614
+
615
+ /**
616
+ * Create new conversation
617
+ */
618
+ async createConversation(title?: string): Promise<any> {
619
+ const url = `${this.agentApi}/conversations`;
620
+ if (this.showAIDebugInfo) {
621
+ console.log('[RemoteAIClient] Creating conversation:', url);
622
+ }
623
+
624
+ const httpsAgent = new https.Agent({ rejectUnauthorized: false });
625
+
626
+ const response = await axios.post(url, { title }, {
627
+ headers: {
628
+ 'Content-Type': 'application/json',
629
+ 'Authorization': `Bearer ${this.authToken}`
630
+ },
631
+ httpsAgent
632
+ });
633
+
634
+ if (response.status !== 200) {
635
+ throw new Error('Failed to create conversation');
636
+ }
637
+
638
+ const data = response.data as { conversation?: any };
639
+ return data.conversation;
640
+ }
641
+
642
+ /**
643
+ * Delete conversation
644
+ */
645
+ async deleteConversation(conversationId: string): Promise<void> {
646
+ const url = `${this.agentApi}/conversations/${conversationId}`;
647
+ if (this.showAIDebugInfo) {
648
+ console.log('[RemoteAIClient] Deleting conversation:', url);
649
+ }
650
+
651
+ const httpsAgent = new https.Agent({ rejectUnauthorized: false });
652
+
653
+ const response = await axios.delete(url, {
654
+ headers: {
655
+ 'Authorization': `Bearer ${this.authToken}`
656
+ },
657
+ httpsAgent
658
+ });
659
+
660
+ if (!response.status.toString().startsWith('2')) {
661
+ throw new Error('Failed to delete conversation');
662
+ }
663
+ }
684
664
  }