-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·322 lines (298 loc) · 11.2 KB
/
Copy pathindex.js
File metadata and controls
executable file
·322 lines (298 loc) · 11.2 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
#!/usr/bin/env node
const os = require("os");
const path = require("path");
const { Command } = require("commander");
const { spawn } = require("child_process");
// Handle both CommonJS and ESM distributions of inquirer (v8 vs v9+)
let inquirer = require("inquirer");
if (inquirer.default) {
// Inquirer v9 (ESM-only) returns the module under the `default` key when required
inquirer = inquirer.default;
}
// Use the built-in global fetch in newer Node versions (>=18). For older versions, fallback to dynamically importing node-fetch (v3, ESM-only).
let fetch = global.fetch;
if (!fetch) {
fetch = (...args) =>
import("node-fetch").then(({ default: fn }) => fn(...args));
}
// Use puppeteer-extra with the stealth plugin to reduce detection surface
const puppeteer = require("puppeteer-extra");
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
const AnonymizeUA = require("puppeteer-extra-plugin-anonymize-ua");
puppeteer.use(StealthPlugin());
puppeteer.use(AnonymizeUA({ stripHeadless: true, makeWindows: false }));
// Expect Linear API key from env variable
const linearToken = process.env.LINEAR_API_KEY;
if (!linearToken) {
console.error(
"Error: Please set the LINEAR_API_KEY environment variable to your Linear API token.",
);
process.exit(1);
}
const program = new Command();
program
.name("linear-codex")
.description("CLI to fetch Linear tickets and send to ChatGPT Codex")
.version("1.0.0");
program
.command("code <ticketId>")
.description("Fetch a Linear ticket by ID and send it to ChatGPT Codex")
.action(async (ticketId) => {
try {
// const user = await getCurrentUser()
// console.log({ user });
// 1. Fetch the Linear ticket (name and description) via GraphQL API
// Determine if ticketId is in format TEAM-123 or a raw ID
let issueData;
if (ticketId.includes("-")) {
// Use the official "issue(identifier)" query which accepts the human-readable key (e.g. "ABC-123").
const query = `
query ($id: String!) {
issue(id: $id) {
id
title
description
comments(last: 50) {
nodes {
body
}
}
children {
nodes {
title
description
}
}
}
}
`;
const variables = { id: ticketId };
const resp = await fetch("https://api.linear.app/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `${linearToken}`,
},
body: JSON.stringify({ query, variables }),
});
const result = await resp.json();
if (result.errors || !result.data.issue) {
throw new Error(
`Linear issue ${ticketId} not found or access denied.`,
);
}
issueData = result.data.issue;
} else {
// If a raw issue UUID is provided (not typical), use direct query by id
const query = `
query ($id: String!) {
issue(id: $id) {
id
title
description
children {
nodes { title description }
}
}
}
`;
const variables = { id: ticketId };
const resp = await fetch("https://api.linear.app/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${linearToken}`,
},
body: JSON.stringify({ query, variables }),
});
const result = await resp.json();
if (result.errors || !result.data.issue) {
throw new Error(
`Linear issue ${ticketId} not found or access denied.`,
);
}
issueData = result.data.issue;
}
// 2. Prompt to include sub-issues (child issues) if any exist
let includeSubIssues = false;
let subIssuesText = "";
const children = issueData.children?.nodes || [];
if (children.length > 0) {
const { includeSubs } = await inquirer.prompt([
{
type: "confirm",
name: "includeSubs",
message: `Include ${children.length} sub-issue(s) in the context?`,
default: false,
},
]);
includeSubIssues = includeSubs;
if (includeSubIssues) {
// Prepare sub-issues text (title and description for each sub-issue)
subIssuesText = "Sub-issues:\n";
for (const sub of children) {
subIssuesText += `- ${sub.title}: ${sub.description}\n`;
}
}
}
// 3. Prompt for monorepo project context if applicable
let projectContext = "";
const { isMonorepo } = await inquirer.prompt([
{
type: "confirm",
name: "isMonorepo",
message: "Is this codebase a monorepo?",
default: false,
},
]);
if (isMonorepo) {
const { projectName } = await inquirer.prompt([
{
type: "input",
name: "projectName",
message:
"Enter the specific project name or folder within the monorepo relevant to this issue:",
},
]);
if (projectName && projectName.trim() !== "") {
projectContext = `Project context: (Monorepo) focus on the "${projectName.trim()}" project.\n`;
}
}
// 4. Summarize/compile the content to send
const issueTitle = issueData.title;
const issueDescription = issueData.description || "";
let combinedContent = `**Issue:** ${issueTitle}\n**Description:** ${issueDescription}\n`;
if (includeSubIssues && subIssuesText) {
combinedContent += subIssuesText;
}
if (projectContext) {
combinedContent += projectContext;
}
// Display summary to user for confirmation
console.log("\n=== Combined Content Preview ===");
console.log(combinedContent);
const { confirmSend } = await inquirer.prompt([
{
type: "confirm",
name: "confirmSend",
message: "Proceed to open Codex with this content?",
default: true,
},
]);
if (!confirmSend) {
console.log("Aborted. The content was not sent to Codex.");
process.exit(0);
}
// 5. Launch (or connect to) a Chrome instance running with remote debugging enabled.
const remoteDebugPort = 9222;
const remoteUserDataDir = "/tmp/chrome-remote-profile"; // persistent session directory
const remoteEndpointURL = `http://localhost:${remoteDebugPort}`;
async function tryConnect() {
try {
return await puppeteer.connect({
browserURL: remoteEndpointURL,
headless: true,
});
} catch (_) {
return null;
}
}
let browser = await tryConnect();
if (!browser) {
console.log(
`No Chrome instance detected on ${remoteEndpointURL}. Launching a new one...`,
);
const chromePath =
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
const chromeArgs = [
`--remote-debugging-port=${remoteDebugPort}`,
`--user-data-dir=${remoteUserDataDir}`,
];
// Spawn Chrome detached so it continues running independently.
const chromeProcess = spawn(chromePath, chromeArgs, {
detached: true,
stdio: "ignore",
});
chromeProcess.unref();
// Poll until the debugging endpoint is ready (max ~10 seconds)
const maxAttempts = 20;
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
let attempt = 0;
while (attempt < maxAttempts) {
browser = await tryConnect();
if (browser) break;
await delay(500);
attempt += 1;
}
if (!browser) {
throw new Error(
"Failed to start or connect to Chrome with remote debugging.",
);
}
}
const randomViewport = () => ({
width: Math.floor(1280 + Math.random() * 400),
height: Math.floor(720 + Math.random() * 360),
deviceScaleFactor: 1,
});
const page = await browser.newPage();
// Randomize viewport size to avoid fixed fingerprint
await page.setViewport(randomViewport());
// Attempt to emulate the system timezone for additional realism
try {
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (tz) {
await page.emulateTimezone(tz);
}
} catch (_) {
/* ignore if not supported */
}
console.log("Opening ChatGPT Codex page...");
await page.goto("https://chatgpt.com/codex", {
waitUntil: "networkidle2",
});
// If not logged in, the page may redirect to a login page.
// (User should log in manually if needed, then re-run the command.)
// Wait for the Codex input textbox to be present
console.log(
"Waiting for ChatGPT Codex editor to be fully loaded (please log in if prompted)...",
);
// Wait indefinitely until the Codex textarea becomes available so the user has time to authenticate.
await page.waitForSelector("textarea", { timeout: 0 });
// Populate the Codex input textbox with the combined content
await page.focus("textarea");
await page.keyboard.type(combinedContent, { delay: 0 });
// Ask the user which action to trigger within Codex
const { codexAction } = await inquirer.prompt([
{
type: "list",
name: "codexAction",
message: "Which Codex action should be triggered?",
choices: [
{ name: "Code – have ChatGPT generate code", value: "code" },
{ name: "Ask – send as a normal question", value: "ask" },
{ name: "None – I will click manually", value: "none" },
],
default: "code",
},
]);
if (codexAction !== "none") {
// Click the chosen button (case-insensitive match on innerText)
const buttons = await page.$$("button");
for (const btn of buttons) {
const label = await page.evaluate((el) => el.innerText || "", btn);
if (label && label.trim().toLowerCase() === codexAction) {
console.log(`Triggering Codex "${label.trim()}" action...`);
await btn.click();
break;
}
}
}
console.log("✅ Done! Check the browser for the Codex response.");
// (Browser remains open for user to review the output)
} catch (err) {
console.error("Error:", err.message);
process.exit(1);
}
});
program.parse(process.argv);