-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathqoder-wrapper.js
More file actions
254 lines (213 loc) · 7.94 KB
/
Copy pathqoder-wrapper.js
File metadata and controls
254 lines (213 loc) · 7.94 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
const { spawn } = require('child_process');
const fs = require('fs');
const readline = require('readline');
const path = require('path');
// ANSI colors
const COLORS = {
CYAN: '\x1b[36m',
DIM: '\x1b[2m',
RESET: '\x1b[0m',
BOLD: '\x1b[1m'
};
// Helper functions for GitHub Actions logging
function printGroupStart(title) {
process.stdout.write(`::group::${title}\n`);
}
function printGroupEnd() {
process.stdout.write(`::endgroup::\n`);
}
function maskSensitiveData(obj) {
if (!obj || typeof obj !== 'object') return obj;
const sensitiveKeys = ['token', 'password', 'secret', 'key', 'authorization', 'auth', 'credential', 'private', 'cert', 'access_key'];
const maskedObj = Array.isArray(obj) ? [...obj] : { ...obj };
for (const key in maskedObj) {
if (Object.prototype.hasOwnProperty.call(maskedObj, key)) {
const lowerKey = key.toLowerCase();
// Check if key contains sensitive words
if (sensitiveKeys.some(s => lowerKey.includes(s))) {
maskedObj[key] = '******';
} else if (typeof maskedObj[key] === 'object') {
maskedObj[key] = maskSensitiveData(maskedObj[key]);
}
}
}
return maskedObj;
}
// --- 1. Environment & Arguments Preparation ---
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
const prompt = process.env.INPUT_PROMPT || '';
const flagsInput = process.env.INPUT_FLAGS || '';
const githubOutput = process.env.GITHUB_OUTPUT;
// Generate unique file paths
const timestamp = Math.floor(Date.now() / 1000);
const outputFile = `/tmp/qoder-output-${timestamp}.log`;
const errorFile = `/tmp/qoder-error-${timestamp}.log`;
const outputStream = fs.createWriteStream(outputFile, { flags: 'a' });
const errorStream = fs.createWriteStream(errorFile, { flags: 'a' });
// Parse flags using regex logic (migrated from shell script)
const args = ['-w', workspace];
// Add prompt if exists
if (prompt) {
args.push('-p', prompt);
}
// Parse additional flags
if (flagsInput) {
const lines = flagsInput.split('\n');
// Match non-whitespace OR double-quoted content OR single-quoted content
const regex = /[^\s"']+|"([^"]*)"|'([^']*)'/g;
for (const rawLine of lines) {
const line = rawLine.trim();
if (!line) continue;
let match;
while ((match = regex.exec(line)) !== null) {
if (match[1] !== undefined) {
args.push(match[1]);
} else if (match[2] !== undefined) {
args.push(match[2]);
} else {
args.push(match[0]);
}
}
}
}
// Force output format to be stream-json, overriding user input if necessary
const formatIndex = args.findIndex(arg => arg === '-f' || arg === '--output-format');
if (formatIndex !== -1) {
// Remove existing format flag and its value
args.splice(formatIndex, 2);
}
args.push('-f', 'stream-json');
// Print Arguments Group
printGroupStart('Arguments for qodercli');
for (let i = 0; i < args.length; i++) {
console.log(` ${args[i]}`);
}
printGroupEnd();
// --- 2. Execution & Stream Processing ---
const child = spawn('qodercli', args, {
stdio: ['inherit', 'pipe', 'pipe'], // Capture stdout and stderr
shell: false,
env: process.env
});
// Handle stdout (Main output stream)
const rlOut = readline.createInterface({
input: child.stdout,
terminal: false
});
let lastThinking = '';
const processedToolIds = new Set();
let sessionIdPrinted = false;
let capturedSessionId = null;
rlOut.on('line', (line) => {
outputStream.write(line + '\n');
if (!line.trim()) return;
try {
const data = JSON.parse(line);
if (data.type === 'system' && data.subtype === 'init' && data.session_id) {
capturedSessionId = data.session_id;
if (!sessionIdPrinted) {
process.stdout.write(`${COLORS.BOLD}Session ID:${COLORS.RESET} ${data.session_id}\n`);
sessionIdPrinted = true;
}
}
// Stream Content
if (data.type === 'assistant' && data.subtype === 'message') {
if (data.message && Array.isArray(data.message.content)) {
data.message.content.forEach(part => {
// Text
if (part.type === 'text' && part.text) {
const summary = part.text.substring(0, 60).replace(/\r?\n/g, ' ') + (part.text.length > 60 ? '...' : '');
printGroupStart(`${COLORS.CYAN}Assistant${COLORS.RESET} ${summary}`);
process.stdout.write(part.text + '\n');
printGroupEnd();
}
// Thinking
else if (part.thinking) {
if (part.thinking === lastThinking) return;
lastThinking = part.thinking;
const summary = part.thinking.substring(0, 60).replace(/\r?\n/g, ' ') + (part.thinking.length > 60 ? '...' : '');
printGroupStart(`${COLORS.CYAN}[Thinking]${COLORS.RESET} ${summary}`);
process.stdout.write(part.thinking + '\n');
printGroupEnd();
}
// Tool Calls
else if (part.type === 'function' && part.id && part.name) {
if (processedToolIds.has(part.id)) return;
processedToolIds.add(part.id);
let displayStr = part.input;
try {
const inputObj = JSON.parse(part.input);
const maskedObj = maskSensitiveData(inputObj);
displayStr = JSON.stringify(maskedObj, null, 2);
} catch (e) {}
const argsSummary = displayStr.replace(/\s+/g, ' ').substring(0, 50) + (displayStr.length > 50 ? '...' : '');
printGroupStart(`${COLORS.CYAN}[Tool Call]${COLORS.RESET} ${part.name} ${argsSummary}`);
if (process.env.ACTIONS_STEP_DEBUG === 'true') {
process.stdout.write(displayStr + '\n');
} else {
process.stdout.write('(Detailed arguments hidden. Enable Actions Debug logging to view)\n');
}
printGroupEnd();
}
});
}
}
} catch (e) {
console.log(line);
}
});
// Handle stderr (Error logging)
child.stderr.on('data', (chunk) => {
errorStream.write(chunk);
process.stderr.write(chunk); // Also print to console stderr
});
// --- 3. Cleanup & Output ---
child.on('close', (code) => {
outputStream.end();
errorStream.end(() => {
// After streams close, write to GITHUB_OUTPUT
if (githubOutput) {
try {
fs.appendFileSync(githubOutput, `output_file=${outputFile}\n`);
// Check if error file has content
const errorStats = fs.statSync(errorFile);
if (errorStats.size > 0) {
const errorContent = fs.readFileSync(errorFile, 'utf8');
fs.appendFileSync(githubOutput, `error<<QODER_ERROR_EOF\n${errorContent}\nQODER_ERROR_EOF\n`);
} else {
fs.appendFileSync(githubOutput, `error=\n`);
}
} catch (err) {
console.error('Failed to write to GITHUB_OUTPUT:', err);
}
}
if (code !== 0) {
let errorMessage = `qodercli failed with exit code ${code}`;
if (capturedSessionId) {
errorMessage += ` (Session ID: ${capturedSessionId})`;
}
// Try to read the last few lines of the error file to provide more context
try {
if (fs.existsSync(errorFile)) {
const errorContent = fs.readFileSync(errorFile, 'utf8').trim();
if (errorContent) {
// Get the last 5 lines or 500 chars to avoid spamming
const lines = errorContent.split('\n');
const lastLines = lines.slice(-10).join('\n');
errorMessage += `\n\nError Details:\n${lastLines}`;
}
}
} catch (e) {
// Ignore file read errors
}
console.log(`::error::${errorMessage.replace(/\n/g, '%0A')}`); // Escape newlines for GitHub Actions
} else {
console.log('✓ qodercli executed successfully');
}
process.exit(code);
});
});
child.on('error', (err) => {
console.error(`Failed to start qodercli: ${err}`);
process.exit(1);
});