@pixelbyte-software/pixcode 1.33.7 → 1.33.8

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 (34) hide show
  1. package/dist/assets/{index-BQizjYZ6.js → index-JU38YIxa.js} +138 -138
  2. package/dist/favicon.svg +8 -8
  3. package/dist/icons/icon-128x128.svg +9 -9
  4. package/dist/icons/icon-144x144.svg +9 -9
  5. package/dist/icons/icon-152x152.svg +9 -9
  6. package/dist/icons/icon-192x192.svg +9 -9
  7. package/dist/icons/icon-384x384.svg +9 -9
  8. package/dist/icons/icon-512x512.svg +9 -9
  9. package/dist/icons/icon-72x72.svg +9 -9
  10. package/dist/icons/icon-96x96.svg +9 -9
  11. package/dist/icons/icon-template.svg +9 -9
  12. package/dist/index.html +1 -1
  13. package/dist/logo.svg +12 -12
  14. package/package.json +1 -1
  15. package/server/database/db.js +794 -794
  16. package/server/database/json-store.js +194 -194
  17. package/server/modules/providers/list/opencode/opencode-auth.provider.ts +130 -130
  18. package/server/modules/providers/list/opencode/opencode-mcp.provider.ts +126 -126
  19. package/server/modules/providers/list/opencode/opencode-sessions.provider.ts +193 -193
  20. package/server/modules/providers/list/opencode/opencode.provider.ts +29 -29
  21. package/server/modules/providers/list/qwen/qwen-auth.provider.ts +145 -145
  22. package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -114
  23. package/server/modules/providers/list/qwen/qwen-sessions.provider.ts +218 -218
  24. package/server/modules/providers/list/qwen/qwen.provider.ts +21 -21
  25. package/server/modules/providers/shared/provider-configs.ts +142 -142
  26. package/server/qwen-code-cli.js +395 -395
  27. package/server/qwen-response-handler.js +73 -73
  28. package/server/routes/qwen.js +27 -27
  29. package/server/services/external-access.js +171 -171
  30. package/server/services/provider-credentials.js +155 -155
  31. package/server/services/provider-models.js +381 -381
  32. package/server/services/telegram/telegram-http-client.js +130 -130
  33. package/server/services/vapid-keys.js +36 -36
  34. package/server/utils/port-access.js +209 -209
@@ -1,395 +1,395 @@
1
- /**
2
- * Qwen Code CLI adapter.
3
- *
4
- * Qwen Code (https://github.com/QwenLM/qwen-code) is Alibaba's fork of Google's
5
- * Gemini CLI. The command-line surface, stream-json output, session layout
6
- * (~/.qwen/tmp/<project>/...), and approval flags all mirror Gemini's. This
7
- * adapter is therefore a structural copy of gemini-cli.js — kept as its own
8
- * file so future Qwen-specific divergence (different auth flow, different
9
- * model list) has a clean place to land without touching Gemini's code path.
10
- */
11
- import { spawn } from 'child_process';
12
- import crossSpawn from 'cross-spawn';
13
- import { promises as fs } from 'fs';
14
- import path from 'path';
15
- import os from 'os';
16
-
17
- import sessionManager from './sessionManager.js';
18
- import QwenResponseHandler from './qwen-response-handler.js';
19
- import { notifyRunFailed, notifyRunStopped } from './services/notification-orchestrator.js';
20
- import { buildSpawnEnv } from './services/provider-credentials.js';
21
- import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
22
- import { createNormalizedMessage } from './shared/utils.js';
23
-
24
- // Use cross-spawn on Windows so `qwen.cmd` resolves correctly.
25
- const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
26
-
27
- const activeQwenProcesses = new Map();
28
-
29
- async function spawnQwen(command, options = {}, ws) {
30
- const { sessionId, projectPath, cwd, toolsSettings, permissionMode, images, sessionSummary } = options;
31
- let capturedSessionId = sessionId;
32
- let sessionCreatedSent = false;
33
- let assistantBlocks = [];
34
-
35
- const settings = toolsSettings || { allowedTools: [], disallowedTools: [], skipPermissions: false };
36
-
37
- const args = [];
38
- if (command && command.trim()) {
39
- args.push('--prompt', command);
40
- }
41
-
42
- if (sessionId) {
43
- const session = sessionManager.getSession(sessionId);
44
- if (session && session.cliSessionId) {
45
- args.push('--resume', session.cliSessionId);
46
- }
47
- }
48
-
49
- const cleanPath = (cwd || projectPath || process.cwd()).replace(/[^\x20-\x7E]/g, '').trim();
50
- const workingDir = cleanPath;
51
-
52
- const tempImagePaths = [];
53
- let tempDir = null;
54
- if (images && images.length > 0) {
55
- try {
56
- tempDir = path.join(workingDir, '.tmp', 'images', Date.now().toString());
57
- await fs.mkdir(tempDir, { recursive: true });
58
-
59
- for (const [index, image] of images.entries()) {
60
- const matches = image.data.match(/^data:([^;]+);base64,(.+)$/);
61
- if (!matches) continue;
62
- const [, mimeType, base64Data] = matches;
63
- const extension = mimeType.split('/')[1] || 'png';
64
- const filename = `image_${index}.${extension}`;
65
- const filepath = path.join(tempDir, filename);
66
- await fs.writeFile(filepath, Buffer.from(base64Data, 'base64'));
67
- tempImagePaths.push(filepath);
68
- }
69
-
70
- if (tempImagePaths.length > 0 && command && command.trim()) {
71
- const imageNote = `\n\n[Images given: ${tempImagePaths.length} images are located at the following paths:]\n${tempImagePaths.map((p, i) => `${i + 1}. ${p}`).join('\n')}`;
72
- const modifiedCommand = command + imageNote;
73
- const promptIndex = args.indexOf('--prompt');
74
- if (promptIndex !== -1 && args[promptIndex + 1] === command) {
75
- args[promptIndex + 1] = modifiedCommand;
76
- } else if (promptIndex !== -1) {
77
- args[promptIndex + 1] = args[promptIndex + 1] + imageNote;
78
- }
79
- }
80
- } catch (error) {
81
- console.error('Error processing images for Qwen Code:', error);
82
- }
83
- }
84
-
85
- if (options.debug) {
86
- args.push('--debug');
87
- }
88
-
89
- // Qwen's MCP config mirrors Gemini's — per-user settings.json plus optional
90
- // project override. Pixcode writes to the user-scope file via the provider
91
- // MCP module, and Qwen Code auto-loads it, so we don't pass --mcp-config
92
- // explicitly. Left intentionally minimal to avoid double-loading.
93
-
94
- const modelToUse = options.model || 'qwen3-coder-plus';
95
- args.push('--model', modelToUse);
96
- args.push('--output-format', 'stream-json');
97
-
98
- if (settings.skipPermissions || options.skipPermissions || permissionMode === 'yolo') {
99
- args.push('--yolo');
100
- } else if (permissionMode === 'auto_edit') {
101
- args.push('--approval-mode', 'auto_edit');
102
- } else if (permissionMode === 'plan') {
103
- args.push('--approval-mode', 'plan');
104
- }
105
-
106
- if (settings.allowedTools && settings.allowedTools.length > 0) {
107
- args.push('--allowed-tools', settings.allowedTools.join(','));
108
- }
109
-
110
- const qwenPath = process.env.QWEN_PATH || 'qwen';
111
- console.log('Spawning Qwen Code CLI:', qwenPath, args.join(' '));
112
- console.log('Working directory:', workingDir);
113
-
114
- let spawnCmd = qwenPath;
115
- let spawnArgs = args;
116
-
117
- if (os.platform() !== 'win32') {
118
- spawnCmd = 'sh';
119
- spawnArgs = ['-c', 'exec "$0" "$@"', qwenPath, ...args];
120
- }
121
-
122
- // Credentials stored in ~/.pixcode/provider-credentials.json take
123
- // precedence over the host shell env, so an API key saved via the
124
- // Pixcode UI reaches the Qwen subprocess even when the user never
125
- // exported it in their login shell.
126
- const spawnEnv = await buildSpawnEnv('qwen');
127
-
128
- return new Promise((resolve, reject) => {
129
- const qwenProcess = spawnFunction(spawnCmd, spawnArgs, {
130
- cwd: workingDir,
131
- stdio: ['pipe', 'pipe', 'pipe'],
132
- env: spawnEnv,
133
- });
134
-
135
- let terminalNotificationSent = false;
136
- let terminalFailureReason = null;
137
-
138
- const notifyTerminalState = ({ code = null, error = null } = {}) => {
139
- if (terminalNotificationSent) return;
140
- terminalNotificationSent = true;
141
-
142
- const finalSessionId = capturedSessionId || sessionId || processKey;
143
- if (code === 0 && !error) {
144
- notifyRunStopped({
145
- userId: ws?.userId || null,
146
- provider: 'qwen',
147
- sessionId: finalSessionId,
148
- sessionName: sessionSummary,
149
- stopReason: 'completed',
150
- });
151
- return;
152
- }
153
-
154
- notifyRunFailed({
155
- userId: ws?.userId || null,
156
- provider: 'qwen',
157
- sessionId: finalSessionId,
158
- sessionName: sessionSummary,
159
- error: error || terminalFailureReason || `Qwen Code CLI exited with code ${code}`,
160
- });
161
- };
162
-
163
- qwenProcess.tempImagePaths = tempImagePaths;
164
- qwenProcess.tempDir = tempDir;
165
-
166
- const processKey = capturedSessionId || sessionId || Date.now().toString();
167
- activeQwenProcesses.set(processKey, qwenProcess);
168
- qwenProcess.sessionId = processKey;
169
-
170
- qwenProcess.stdin.end();
171
-
172
- const timeoutMs = 120000;
173
- let timeout;
174
- const startTimeout = () => {
175
- if (timeout) clearTimeout(timeout);
176
- timeout = setTimeout(() => {
177
- const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : (capturedSessionId || sessionId || processKey);
178
- terminalFailureReason = `Qwen Code CLI timeout - no response received for ${timeoutMs / 1000} seconds`;
179
- ws.send(createNormalizedMessage({ kind: 'error', content: terminalFailureReason, sessionId: socketSessionId, provider: 'qwen' }));
180
- try { qwenProcess.kill('SIGTERM'); } catch { /* noop */ }
181
- }, timeoutMs);
182
- };
183
- startTimeout();
184
-
185
- if (command && capturedSessionId) {
186
- sessionManager.addMessage(capturedSessionId, 'user', command);
187
- }
188
-
189
- let responseHandler;
190
- if (ws) {
191
- responseHandler = new QwenResponseHandler(ws, {
192
- onContentFragment: (content) => {
193
- if (assistantBlocks.length > 0 && assistantBlocks[assistantBlocks.length - 1].type === 'text') {
194
- assistantBlocks[assistantBlocks.length - 1].text += content;
195
- } else {
196
- assistantBlocks.push({ type: 'text', text: content });
197
- }
198
- },
199
- onToolUse: (event) => {
200
- assistantBlocks.push({
201
- type: 'tool_use',
202
- id: event.tool_id,
203
- name: event.tool_name,
204
- input: event.parameters,
205
- });
206
- },
207
- onToolResult: (event) => {
208
- if (capturedSessionId) {
209
- if (assistantBlocks.length > 0) {
210
- sessionManager.addMessage(capturedSessionId, 'assistant', [...assistantBlocks]);
211
- assistantBlocks = [];
212
- }
213
- sessionManager.addMessage(capturedSessionId, 'user', [{
214
- type: 'tool_result',
215
- tool_use_id: event.tool_id,
216
- content: event.output === undefined ? null : event.output,
217
- is_error: event.status === 'error',
218
- }]);
219
- }
220
- },
221
- onInit: (event) => {
222
- if (capturedSessionId) {
223
- const sess = sessionManager.getSession(capturedSessionId);
224
- if (sess && !sess.cliSessionId) {
225
- sess.cliSessionId = event.session_id;
226
- sessionManager.saveSession(capturedSessionId);
227
- }
228
- }
229
- },
230
- });
231
- }
232
-
233
- qwenProcess.stdout.on('data', (data) => {
234
- const rawOutput = data.toString();
235
- startTimeout();
236
-
237
- if (!sessionId && !sessionCreatedSent && !capturedSessionId) {
238
- capturedSessionId = `qwen_${Date.now()}`;
239
- sessionCreatedSent = true;
240
-
241
- sessionManager.createSession(capturedSessionId, cwd || process.cwd());
242
- if (command) {
243
- sessionManager.addMessage(capturedSessionId, 'user', command);
244
- }
245
- if (processKey !== capturedSessionId) {
246
- activeQwenProcesses.delete(processKey);
247
- activeQwenProcesses.set(capturedSessionId, qwenProcess);
248
- }
249
-
250
- ws.setSessionId && typeof ws.setSessionId === 'function' && ws.setSessionId(capturedSessionId);
251
- ws.send(createNormalizedMessage({ kind: 'session_created', newSessionId: capturedSessionId, sessionId: capturedSessionId, provider: 'qwen' }));
252
- }
253
-
254
- if (responseHandler) {
255
- responseHandler.processData(rawOutput);
256
- } else if (rawOutput) {
257
- if (assistantBlocks.length > 0 && assistantBlocks[assistantBlocks.length - 1].type === 'text') {
258
- assistantBlocks[assistantBlocks.length - 1].text += rawOutput;
259
- } else {
260
- assistantBlocks.push({ type: 'text', text: rawOutput });
261
- }
262
- const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : (capturedSessionId || sessionId);
263
- ws.send(createNormalizedMessage({ kind: 'stream_delta', content: rawOutput, sessionId: socketSessionId, provider: 'qwen' }));
264
- }
265
- });
266
-
267
- qwenProcess.stderr.on('data', (data) => {
268
- const errorMsg = data.toString();
269
- if (errorMsg.includes('[DEP0040]') ||
270
- errorMsg.includes('DeprecationWarning') ||
271
- errorMsg.includes('--trace-deprecation') ||
272
- errorMsg.includes('Loaded cached credentials')) {
273
- return;
274
- }
275
-
276
- const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : (capturedSessionId || sessionId);
277
- ws.send(createNormalizedMessage({ kind: 'error', content: errorMsg, sessionId: socketSessionId, provider: 'qwen' }));
278
- });
279
-
280
- qwenProcess.on('close', async (code) => {
281
- clearTimeout(timeout);
282
-
283
- if (responseHandler) {
284
- responseHandler.forceFlush();
285
- responseHandler.destroy();
286
- }
287
-
288
- const finalSessionId = capturedSessionId || sessionId || processKey;
289
- activeQwenProcesses.delete(finalSessionId);
290
-
291
- if (finalSessionId && assistantBlocks.length > 0) {
292
- sessionManager.addMessage(finalSessionId, 'assistant', assistantBlocks);
293
- }
294
-
295
- ws.send(createNormalizedMessage({ kind: 'complete', exitCode: code, isNewSession: !sessionId && !!command, sessionId: finalSessionId, provider: 'qwen' }));
296
-
297
- if (qwenProcess.tempImagePaths && qwenProcess.tempImagePaths.length > 0) {
298
- for (const imagePath of qwenProcess.tempImagePaths) {
299
- await fs.unlink(imagePath).catch(() => { /* noop */ });
300
- }
301
- if (qwenProcess.tempDir) {
302
- await fs.rm(qwenProcess.tempDir, { recursive: true, force: true }).catch(() => { /* noop */ });
303
- }
304
- }
305
-
306
- if (code === 0) {
307
- notifyTerminalState({ code });
308
- resolve();
309
- } else {
310
- if (code === 127) {
311
- const installed = await providerAuthService.isProviderInstalled('qwen');
312
- if (!installed) {
313
- const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : finalSessionId;
314
- ws.send(createNormalizedMessage({
315
- kind: 'error',
316
- content: 'Qwen Code CLI is not installed. Install it first: npm install -g @qwen-code/qwen-code',
317
- sessionId: socketSessionId,
318
- provider: 'qwen',
319
- }));
320
- }
321
- }
322
-
323
- notifyTerminalState({
324
- code,
325
- error: code === null ? 'Qwen Code CLI process was terminated or timed out' : null,
326
- });
327
- reject(new Error(code === null ? 'Qwen Code CLI process was terminated or timed out' : `Qwen Code CLI exited with code ${code}`));
328
- }
329
- });
330
-
331
- qwenProcess.on('error', async (error) => {
332
- const finalSessionId = capturedSessionId || sessionId || processKey;
333
- activeQwenProcesses.delete(finalSessionId);
334
-
335
- const installed = await providerAuthService.isProviderInstalled('qwen');
336
- const errorContent = !installed
337
- ? 'Qwen Code CLI is not installed. Install it first: npm install -g @qwen-code/qwen-code'
338
- : (error?.message || String(error));
339
-
340
- const errorSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : finalSessionId;
341
- ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: errorSessionId, provider: 'qwen' }));
342
- // Always emit `complete` so the UI's "Processing..." state clears
343
- // even when spawn fails (ENOENT, EACCES) and `close` never fires.
344
- ws.send(createNormalizedMessage({ kind: 'complete', exitCode: 1, isNewSession: !sessionId && !!command, sessionId: errorSessionId, provider: 'qwen' }));
345
- notifyTerminalState({ error });
346
-
347
- reject(error);
348
- });
349
- });
350
- }
351
-
352
- function abortQwenSession(sessionId) {
353
- let qwenProc = activeQwenProcesses.get(sessionId);
354
- let processKey = sessionId;
355
-
356
- if (!qwenProc) {
357
- for (const [key, proc] of activeQwenProcesses.entries()) {
358
- if (proc.sessionId === sessionId) {
359
- qwenProc = proc;
360
- processKey = key;
361
- break;
362
- }
363
- }
364
- }
365
-
366
- if (qwenProc) {
367
- try {
368
- qwenProc.kill('SIGTERM');
369
- setTimeout(() => {
370
- if (activeQwenProcesses.has(processKey)) {
371
- try { qwenProc.kill('SIGKILL'); } catch { /* noop */ }
372
- }
373
- }, 2000);
374
- return true;
375
- } catch {
376
- return false;
377
- }
378
- }
379
- return false;
380
- }
381
-
382
- function isQwenSessionActive(sessionId) {
383
- return activeQwenProcesses.has(sessionId);
384
- }
385
-
386
- function getActiveQwenSessions() {
387
- return Array.from(activeQwenProcesses.keys());
388
- }
389
-
390
- export {
391
- spawnQwen,
392
- abortQwenSession,
393
- isQwenSessionActive,
394
- getActiveQwenSessions,
395
- };
1
+ /**
2
+ * Qwen Code CLI adapter.
3
+ *
4
+ * Qwen Code (https://github.com/QwenLM/qwen-code) is Alibaba's fork of Google's
5
+ * Gemini CLI. The command-line surface, stream-json output, session layout
6
+ * (~/.qwen/tmp/<project>/...), and approval flags all mirror Gemini's. This
7
+ * adapter is therefore a structural copy of gemini-cli.js — kept as its own
8
+ * file so future Qwen-specific divergence (different auth flow, different
9
+ * model list) has a clean place to land without touching Gemini's code path.
10
+ */
11
+ import { spawn } from 'child_process';
12
+ import crossSpawn from 'cross-spawn';
13
+ import { promises as fs } from 'fs';
14
+ import path from 'path';
15
+ import os from 'os';
16
+
17
+ import sessionManager from './sessionManager.js';
18
+ import QwenResponseHandler from './qwen-response-handler.js';
19
+ import { notifyRunFailed, notifyRunStopped } from './services/notification-orchestrator.js';
20
+ import { buildSpawnEnv } from './services/provider-credentials.js';
21
+ import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
22
+ import { createNormalizedMessage } from './shared/utils.js';
23
+
24
+ // Use cross-spawn on Windows so `qwen.cmd` resolves correctly.
25
+ const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
26
+
27
+ const activeQwenProcesses = new Map();
28
+
29
+ async function spawnQwen(command, options = {}, ws) {
30
+ const { sessionId, projectPath, cwd, toolsSettings, permissionMode, images, sessionSummary } = options;
31
+ let capturedSessionId = sessionId;
32
+ let sessionCreatedSent = false;
33
+ let assistantBlocks = [];
34
+
35
+ const settings = toolsSettings || { allowedTools: [], disallowedTools: [], skipPermissions: false };
36
+
37
+ const args = [];
38
+ if (command && command.trim()) {
39
+ args.push('--prompt', command);
40
+ }
41
+
42
+ if (sessionId) {
43
+ const session = sessionManager.getSession(sessionId);
44
+ if (session && session.cliSessionId) {
45
+ args.push('--resume', session.cliSessionId);
46
+ }
47
+ }
48
+
49
+ const cleanPath = (cwd || projectPath || process.cwd()).replace(/[^\x20-\x7E]/g, '').trim();
50
+ const workingDir = cleanPath;
51
+
52
+ const tempImagePaths = [];
53
+ let tempDir = null;
54
+ if (images && images.length > 0) {
55
+ try {
56
+ tempDir = path.join(workingDir, '.tmp', 'images', Date.now().toString());
57
+ await fs.mkdir(tempDir, { recursive: true });
58
+
59
+ for (const [index, image] of images.entries()) {
60
+ const matches = image.data.match(/^data:([^;]+);base64,(.+)$/);
61
+ if (!matches) continue;
62
+ const [, mimeType, base64Data] = matches;
63
+ const extension = mimeType.split('/')[1] || 'png';
64
+ const filename = `image_${index}.${extension}`;
65
+ const filepath = path.join(tempDir, filename);
66
+ await fs.writeFile(filepath, Buffer.from(base64Data, 'base64'));
67
+ tempImagePaths.push(filepath);
68
+ }
69
+
70
+ if (tempImagePaths.length > 0 && command && command.trim()) {
71
+ const imageNote = `\n\n[Images given: ${tempImagePaths.length} images are located at the following paths:]\n${tempImagePaths.map((p, i) => `${i + 1}. ${p}`).join('\n')}`;
72
+ const modifiedCommand = command + imageNote;
73
+ const promptIndex = args.indexOf('--prompt');
74
+ if (promptIndex !== -1 && args[promptIndex + 1] === command) {
75
+ args[promptIndex + 1] = modifiedCommand;
76
+ } else if (promptIndex !== -1) {
77
+ args[promptIndex + 1] = args[promptIndex + 1] + imageNote;
78
+ }
79
+ }
80
+ } catch (error) {
81
+ console.error('Error processing images for Qwen Code:', error);
82
+ }
83
+ }
84
+
85
+ if (options.debug) {
86
+ args.push('--debug');
87
+ }
88
+
89
+ // Qwen's MCP config mirrors Gemini's — per-user settings.json plus optional
90
+ // project override. Pixcode writes to the user-scope file via the provider
91
+ // MCP module, and Qwen Code auto-loads it, so we don't pass --mcp-config
92
+ // explicitly. Left intentionally minimal to avoid double-loading.
93
+
94
+ const modelToUse = options.model || 'qwen3-coder-plus';
95
+ args.push('--model', modelToUse);
96
+ args.push('--output-format', 'stream-json');
97
+
98
+ if (settings.skipPermissions || options.skipPermissions || permissionMode === 'yolo') {
99
+ args.push('--yolo');
100
+ } else if (permissionMode === 'auto_edit') {
101
+ args.push('--approval-mode', 'auto_edit');
102
+ } else if (permissionMode === 'plan') {
103
+ args.push('--approval-mode', 'plan');
104
+ }
105
+
106
+ if (settings.allowedTools && settings.allowedTools.length > 0) {
107
+ args.push('--allowed-tools', settings.allowedTools.join(','));
108
+ }
109
+
110
+ const qwenPath = process.env.QWEN_PATH || 'qwen';
111
+ console.log('Spawning Qwen Code CLI:', qwenPath, args.join(' '));
112
+ console.log('Working directory:', workingDir);
113
+
114
+ let spawnCmd = qwenPath;
115
+ let spawnArgs = args;
116
+
117
+ if (os.platform() !== 'win32') {
118
+ spawnCmd = 'sh';
119
+ spawnArgs = ['-c', 'exec "$0" "$@"', qwenPath, ...args];
120
+ }
121
+
122
+ // Credentials stored in ~/.pixcode/provider-credentials.json take
123
+ // precedence over the host shell env, so an API key saved via the
124
+ // Pixcode UI reaches the Qwen subprocess even when the user never
125
+ // exported it in their login shell.
126
+ const spawnEnv = await buildSpawnEnv('qwen');
127
+
128
+ return new Promise((resolve, reject) => {
129
+ const qwenProcess = spawnFunction(spawnCmd, spawnArgs, {
130
+ cwd: workingDir,
131
+ stdio: ['pipe', 'pipe', 'pipe'],
132
+ env: spawnEnv,
133
+ });
134
+
135
+ let terminalNotificationSent = false;
136
+ let terminalFailureReason = null;
137
+
138
+ const notifyTerminalState = ({ code = null, error = null } = {}) => {
139
+ if (terminalNotificationSent) return;
140
+ terminalNotificationSent = true;
141
+
142
+ const finalSessionId = capturedSessionId || sessionId || processKey;
143
+ if (code === 0 && !error) {
144
+ notifyRunStopped({
145
+ userId: ws?.userId || null,
146
+ provider: 'qwen',
147
+ sessionId: finalSessionId,
148
+ sessionName: sessionSummary,
149
+ stopReason: 'completed',
150
+ });
151
+ return;
152
+ }
153
+
154
+ notifyRunFailed({
155
+ userId: ws?.userId || null,
156
+ provider: 'qwen',
157
+ sessionId: finalSessionId,
158
+ sessionName: sessionSummary,
159
+ error: error || terminalFailureReason || `Qwen Code CLI exited with code ${code}`,
160
+ });
161
+ };
162
+
163
+ qwenProcess.tempImagePaths = tempImagePaths;
164
+ qwenProcess.tempDir = tempDir;
165
+
166
+ const processKey = capturedSessionId || sessionId || Date.now().toString();
167
+ activeQwenProcesses.set(processKey, qwenProcess);
168
+ qwenProcess.sessionId = processKey;
169
+
170
+ qwenProcess.stdin.end();
171
+
172
+ const timeoutMs = 120000;
173
+ let timeout;
174
+ const startTimeout = () => {
175
+ if (timeout) clearTimeout(timeout);
176
+ timeout = setTimeout(() => {
177
+ const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : (capturedSessionId || sessionId || processKey);
178
+ terminalFailureReason = `Qwen Code CLI timeout - no response received for ${timeoutMs / 1000} seconds`;
179
+ ws.send(createNormalizedMessage({ kind: 'error', content: terminalFailureReason, sessionId: socketSessionId, provider: 'qwen' }));
180
+ try { qwenProcess.kill('SIGTERM'); } catch { /* noop */ }
181
+ }, timeoutMs);
182
+ };
183
+ startTimeout();
184
+
185
+ if (command && capturedSessionId) {
186
+ sessionManager.addMessage(capturedSessionId, 'user', command);
187
+ }
188
+
189
+ let responseHandler;
190
+ if (ws) {
191
+ responseHandler = new QwenResponseHandler(ws, {
192
+ onContentFragment: (content) => {
193
+ if (assistantBlocks.length > 0 && assistantBlocks[assistantBlocks.length - 1].type === 'text') {
194
+ assistantBlocks[assistantBlocks.length - 1].text += content;
195
+ } else {
196
+ assistantBlocks.push({ type: 'text', text: content });
197
+ }
198
+ },
199
+ onToolUse: (event) => {
200
+ assistantBlocks.push({
201
+ type: 'tool_use',
202
+ id: event.tool_id,
203
+ name: event.tool_name,
204
+ input: event.parameters,
205
+ });
206
+ },
207
+ onToolResult: (event) => {
208
+ if (capturedSessionId) {
209
+ if (assistantBlocks.length > 0) {
210
+ sessionManager.addMessage(capturedSessionId, 'assistant', [...assistantBlocks]);
211
+ assistantBlocks = [];
212
+ }
213
+ sessionManager.addMessage(capturedSessionId, 'user', [{
214
+ type: 'tool_result',
215
+ tool_use_id: event.tool_id,
216
+ content: event.output === undefined ? null : event.output,
217
+ is_error: event.status === 'error',
218
+ }]);
219
+ }
220
+ },
221
+ onInit: (event) => {
222
+ if (capturedSessionId) {
223
+ const sess = sessionManager.getSession(capturedSessionId);
224
+ if (sess && !sess.cliSessionId) {
225
+ sess.cliSessionId = event.session_id;
226
+ sessionManager.saveSession(capturedSessionId);
227
+ }
228
+ }
229
+ },
230
+ });
231
+ }
232
+
233
+ qwenProcess.stdout.on('data', (data) => {
234
+ const rawOutput = data.toString();
235
+ startTimeout();
236
+
237
+ if (!sessionId && !sessionCreatedSent && !capturedSessionId) {
238
+ capturedSessionId = `qwen_${Date.now()}`;
239
+ sessionCreatedSent = true;
240
+
241
+ sessionManager.createSession(capturedSessionId, cwd || process.cwd());
242
+ if (command) {
243
+ sessionManager.addMessage(capturedSessionId, 'user', command);
244
+ }
245
+ if (processKey !== capturedSessionId) {
246
+ activeQwenProcesses.delete(processKey);
247
+ activeQwenProcesses.set(capturedSessionId, qwenProcess);
248
+ }
249
+
250
+ ws.setSessionId && typeof ws.setSessionId === 'function' && ws.setSessionId(capturedSessionId);
251
+ ws.send(createNormalizedMessage({ kind: 'session_created', newSessionId: capturedSessionId, sessionId: capturedSessionId, provider: 'qwen' }));
252
+ }
253
+
254
+ if (responseHandler) {
255
+ responseHandler.processData(rawOutput);
256
+ } else if (rawOutput) {
257
+ if (assistantBlocks.length > 0 && assistantBlocks[assistantBlocks.length - 1].type === 'text') {
258
+ assistantBlocks[assistantBlocks.length - 1].text += rawOutput;
259
+ } else {
260
+ assistantBlocks.push({ type: 'text', text: rawOutput });
261
+ }
262
+ const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : (capturedSessionId || sessionId);
263
+ ws.send(createNormalizedMessage({ kind: 'stream_delta', content: rawOutput, sessionId: socketSessionId, provider: 'qwen' }));
264
+ }
265
+ });
266
+
267
+ qwenProcess.stderr.on('data', (data) => {
268
+ const errorMsg = data.toString();
269
+ if (errorMsg.includes('[DEP0040]') ||
270
+ errorMsg.includes('DeprecationWarning') ||
271
+ errorMsg.includes('--trace-deprecation') ||
272
+ errorMsg.includes('Loaded cached credentials')) {
273
+ return;
274
+ }
275
+
276
+ const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : (capturedSessionId || sessionId);
277
+ ws.send(createNormalizedMessage({ kind: 'error', content: errorMsg, sessionId: socketSessionId, provider: 'qwen' }));
278
+ });
279
+
280
+ qwenProcess.on('close', async (code) => {
281
+ clearTimeout(timeout);
282
+
283
+ if (responseHandler) {
284
+ responseHandler.forceFlush();
285
+ responseHandler.destroy();
286
+ }
287
+
288
+ const finalSessionId = capturedSessionId || sessionId || processKey;
289
+ activeQwenProcesses.delete(finalSessionId);
290
+
291
+ if (finalSessionId && assistantBlocks.length > 0) {
292
+ sessionManager.addMessage(finalSessionId, 'assistant', assistantBlocks);
293
+ }
294
+
295
+ ws.send(createNormalizedMessage({ kind: 'complete', exitCode: code, isNewSession: !sessionId && !!command, sessionId: finalSessionId, provider: 'qwen' }));
296
+
297
+ if (qwenProcess.tempImagePaths && qwenProcess.tempImagePaths.length > 0) {
298
+ for (const imagePath of qwenProcess.tempImagePaths) {
299
+ await fs.unlink(imagePath).catch(() => { /* noop */ });
300
+ }
301
+ if (qwenProcess.tempDir) {
302
+ await fs.rm(qwenProcess.tempDir, { recursive: true, force: true }).catch(() => { /* noop */ });
303
+ }
304
+ }
305
+
306
+ if (code === 0) {
307
+ notifyTerminalState({ code });
308
+ resolve();
309
+ } else {
310
+ if (code === 127) {
311
+ const installed = await providerAuthService.isProviderInstalled('qwen');
312
+ if (!installed) {
313
+ const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : finalSessionId;
314
+ ws.send(createNormalizedMessage({
315
+ kind: 'error',
316
+ content: 'Qwen Code CLI is not installed. Install it first: npm install -g @qwen-code/qwen-code',
317
+ sessionId: socketSessionId,
318
+ provider: 'qwen',
319
+ }));
320
+ }
321
+ }
322
+
323
+ notifyTerminalState({
324
+ code,
325
+ error: code === null ? 'Qwen Code CLI process was terminated or timed out' : null,
326
+ });
327
+ reject(new Error(code === null ? 'Qwen Code CLI process was terminated or timed out' : `Qwen Code CLI exited with code ${code}`));
328
+ }
329
+ });
330
+
331
+ qwenProcess.on('error', async (error) => {
332
+ const finalSessionId = capturedSessionId || sessionId || processKey;
333
+ activeQwenProcesses.delete(finalSessionId);
334
+
335
+ const installed = await providerAuthService.isProviderInstalled('qwen');
336
+ const errorContent = !installed
337
+ ? 'Qwen Code CLI is not installed. Install it first: npm install -g @qwen-code/qwen-code'
338
+ : (error?.message || String(error));
339
+
340
+ const errorSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : finalSessionId;
341
+ ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: errorSessionId, provider: 'qwen' }));
342
+ // Always emit `complete` so the UI's "Processing..." state clears
343
+ // even when spawn fails (ENOENT, EACCES) and `close` never fires.
344
+ ws.send(createNormalizedMessage({ kind: 'complete', exitCode: 1, isNewSession: !sessionId && !!command, sessionId: errorSessionId, provider: 'qwen' }));
345
+ notifyTerminalState({ error });
346
+
347
+ reject(error);
348
+ });
349
+ });
350
+ }
351
+
352
+ function abortQwenSession(sessionId) {
353
+ let qwenProc = activeQwenProcesses.get(sessionId);
354
+ let processKey = sessionId;
355
+
356
+ if (!qwenProc) {
357
+ for (const [key, proc] of activeQwenProcesses.entries()) {
358
+ if (proc.sessionId === sessionId) {
359
+ qwenProc = proc;
360
+ processKey = key;
361
+ break;
362
+ }
363
+ }
364
+ }
365
+
366
+ if (qwenProc) {
367
+ try {
368
+ qwenProc.kill('SIGTERM');
369
+ setTimeout(() => {
370
+ if (activeQwenProcesses.has(processKey)) {
371
+ try { qwenProc.kill('SIGKILL'); } catch { /* noop */ }
372
+ }
373
+ }, 2000);
374
+ return true;
375
+ } catch {
376
+ return false;
377
+ }
378
+ }
379
+ return false;
380
+ }
381
+
382
+ function isQwenSessionActive(sessionId) {
383
+ return activeQwenProcesses.has(sessionId);
384
+ }
385
+
386
+ function getActiveQwenSessions() {
387
+ return Array.from(activeQwenProcesses.keys());
388
+ }
389
+
390
+ export {
391
+ spawnQwen,
392
+ abortQwenSession,
393
+ isQwenSessionActive,
394
+ getActiveQwenSessions,
395
+ };