forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
379 lines (358 loc) · 10.4 KB
/
index.ts
File metadata and controls
379 lines (358 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* eslint-disable */
import { execSync, spawnSync } from "node:child_process"
import fs from "node:fs"
import { defineCommand } from "citty"
import { findAllCopilotLaunchdJobs } from "./macos"
import { installMacOS, startMacOS, statusMacOS, stopMacOS, uninstallMacOS } from "./macos"
import { findAllCopilotSystemdUnits } from "./linux"
import { installLinux, startLinux, statusLinux, stopLinux, uninstallLinux } from "./linux"
import { installWindows, startWindows, statusWindows, stopWindows, uninstallWindows, windowsLogFile } from "./windows"
import {
LOG_FILE,
assertSupported,
isMacOS,
isLinux,
isWindows,
} from "./shared"
import type { CopilotProcess, DaemonInstallArgs } from "./shared"
// ---------------------------------------------------------------------------
// --all: find all copilot-api related processes
// ---------------------------------------------------------------------------
function findAllCopilotProcesses(): CopilotProcess[] {
if (isWindows()) {
const result = spawnSync("powershell", [
"-NoProfile", "-Command",
`Get-CimInstance Win32_Process -Filter "CommandLine LIKE '%copilot-api%' AND ProcessId != $PID" | ForEach-Object { "$($_.ProcessId)|$($_.CommandLine)" }`,
], { encoding: "utf-8", timeout: 10000 })
if (result.status !== 0) return []
const procs: CopilotProcess[] = []
for (const line of result.stdout.trim().split("\n")) {
if (!line.trim()) continue
const sep = line.indexOf("|")
if (sep < 0) continue
procs.push({ pid: line.slice(0, sep).trim(), command: line.slice(sep + 1).trim() })
}
return procs
}
try {
const output = execSync(
"ps aux | grep -i copilot-api | grep -v grep",
{ encoding: "utf-8", timeout: 5000 },
)
const procs: CopilotProcess[] = []
for (const line of output.trim().split("\n")) {
if (!line) continue
const parts = line.trim().split(/\s+/)
if (parts.length < 11) continue
const pid = parts[1]
const command = parts.slice(10).join(" ")
procs.push({ pid, command })
}
return procs
} catch {
return []
}
}
function statusAll(): void {
if (isMacOS()) {
const jobs = findAllCopilotLaunchdJobs()
if (jobs.length > 0) {
console.log("=== LaunchAgent Jobs ===")
for (const job of jobs) {
console.log(`\n Label: ${job.label}`)
if (job.plistPath) console.log(` Plist: ${job.plistPath}`)
else console.log(` Plist: (not found)`)
console.log(` PID: ${job.pid ?? "not running"}`)
}
} else {
console.log("No copilot-api LaunchAgent jobs found.")
}
} else if (isWindows()) {
statusWindows()
} else {
const units = findAllCopilotSystemdUnits()
if (units.length > 0) {
console.log("=== Systemd Units ===")
for (const unit of units) {
const r = spawnSync(
"systemctl",
["--user", "status", unit, "--no-pager"],
{ encoding: "utf-8", timeout: 5000 },
)
console.log(`\n--- ${unit} ---`)
console.log(r.stdout.trim())
}
} else {
console.log("No copilot-api systemd units found.")
}
}
const procs = findAllCopilotProcesses()
if (procs.length > 0) {
console.log("\n=== Processes ===")
for (const proc of procs) {
console.log(` PID ${proc.pid}: ${proc.command}`)
}
}
}
function stopAll(): void {
let stopped = 0
if (isMacOS()) {
const jobs = findAllCopilotLaunchdJobs()
const domain = `gui/${process.getuid?.() ?? 501}`
for (const job of jobs) {
const jobTarget = `${domain}/${job.label}`
console.log(`Stopping LaunchAgent '${job.label}'...`)
spawnSync("launchctl", ["bootout", jobTarget], {
encoding: "utf-8",
timeout: 15000,
})
stopped++
}
} else if (!isWindows()) {
const units = findAllCopilotSystemdUnits()
for (const unit of units) {
console.log(`Stopping systemd unit '${unit}'...`)
spawnSync("systemctl", ["--user", "stop", unit], {
encoding: "utf-8",
timeout: 15000,
})
stopped++
}
}
// Kill remaining processes
const procs = findAllCopilotProcesses()
for (const proc of procs) {
console.log(`Killing PID ${proc.pid}: ${proc.command}`)
if (isWindows()) {
spawnSync("taskkill", ["/PID", proc.pid, "/F"], { encoding: "utf-8", timeout: 5000 })
stopped++
} else {
try {
process.kill(Number.parseInt(proc.pid, 10), "SIGTERM")
stopped++
} catch {
// already dead
}
}
}
if (stopped === 0) {
console.log("No copilot-api processes found.")
} else {
console.log(`\nStopped/killed ${stopped} item(s).`)
}
}
// ---------------------------------------------------------------------------
// CLI subcommands
// ---------------------------------------------------------------------------
const installCmd = defineCommand({
meta: {
name: "install",
description:
"Install xc-copilot-api daemon (launchd on macOS, systemd on Linux, Task Scheduler on Windows)",
},
args: {
npx: {
type: "boolean",
default: true,
description:
"Use npx to run (auto-updates on restart). Set --no-npx to use direct binary.",
},
port: {
alias: "p",
type: "string",
default: "4141",
description: "Port for the API server",
},
verbose: {
alias: "v",
type: "boolean",
default: false,
description: "Enable verbose logging",
},
"account-type": {
alias: "a",
type: "string",
default: "individual",
description: "Account type (individual, business, enterprise)",
},
"github-token": {
alias: "g",
type: "string",
description: "GitHub token to use",
},
"proxy-env": {
type: "boolean",
default: false,
description: "Initialize proxy from environment variables",
},
},
run({ args }) {
assertSupported()
const installArgs: DaemonInstallArgs = {
npx: args.npx,
port: args.port,
verbose: args.verbose,
accountType: args["account-type"],
githubToken: args["github-token"],
proxyEnv: args["proxy-env"],
}
if (isMacOS()) {
installMacOS(installArgs)
} else if (isWindows()) {
installWindows(installArgs)
} else {
installLinux(installArgs)
}
},
})
const uninstallCmd = defineCommand({
meta: {
name: "uninstall",
description: "Uninstall the daemon",
},
run() {
assertSupported()
if (isMacOS()) {
uninstallMacOS()
} else if (isWindows()) {
uninstallWindows()
} else {
uninstallLinux()
}
},
})
const statusCmd = defineCommand({
meta: {
name: "status",
description: "Show daemon status (use --all to show all copilot-api instances)",
},
args: {
all: {
type: "boolean",
default: false,
description:
"Show all copilot-api related daemons and processes",
},
},
run({ args }) {
assertSupported()
if (args.all) {
statusAll()
} else if (isMacOS()) {
statusMacOS()
} else if (isWindows()) {
statusWindows()
} else {
statusLinux()
}
},
})
const restartCmd = defineCommand({
meta: {
name: "restart",
description:
"Restart the daemon (npx mode fetches latest version automatically)",
},
run() {
assertSupported()
if (isMacOS()) {
if (!startMacOS()) process.exit(1)
} else if (isWindows()) {
stopWindows()
if (!startWindows()) process.exit(1)
} else {
stopLinux()
if (!startLinux()) process.exit(1)
}
},
})
const stopCmd = defineCommand({
meta: {
name: "stop",
description: "Stop the daemon (use --all to kill all copilot-api instances)",
},
args: {
all: {
type: "boolean",
default: false,
description:
"Stop all copilot-api related daemons and kill all processes",
},
},
run({ args }) {
assertSupported()
if (args.all) {
stopAll()
} else if (isMacOS()) {
if (!stopMacOS()) process.exit(1)
} else if (isWindows()) {
if (!stopWindows()) process.exit(1)
} else {
if (!stopLinux()) process.exit(1)
}
},
})
const logsCmd = defineCommand({
meta: {
name: "logs",
description: "Show recent daemon logs",
},
args: {
follow: {
alias: "f",
type: "boolean",
default: false,
description: "Follow log output (like tail -f)",
},
lines: {
alias: "n",
type: "string",
default: "50",
description: "Number of lines to show",
},
},
run({ args }) {
assertSupported()
const logPath = isWindows() ? windowsLogFile() : LOG_FILE
if (!fs.existsSync(logPath)) {
console.log("No log file found.")
return
}
if (isWindows()) {
// Windows: use powershell Get-Content
if (args.follow) {
const { status } = spawnSync("powershell", ["-Command", `Get-Content -Path '${logPath}' -Wait -Tail ${args.lines}`], { stdio: "inherit" })
process.exit(status ?? 0)
} else {
const { status } = spawnSync("powershell", ["-Command", `Get-Content -Path '${logPath}' -Tail ${args.lines}`], { stdio: "inherit" })
process.exit(status ?? 0)
}
} else {
if (args.follow) {
const { status } = spawnSync("tail", ["-f", logPath], { stdio: "inherit" })
process.exit(status ?? 0)
} else {
const { status } = spawnSync("tail", ["-n", args.lines, logPath], { stdio: "inherit" })
process.exit(status ?? 0)
}
}
},
})
// ---------------------------------------------------------------------------
// Main daemon command
// ---------------------------------------------------------------------------
export const daemon = defineCommand({
meta: {
name: "xc-copilot-api-daemon",
description:
"Manage xc-copilot-api daemon (install, status, restart, stop, uninstall, logs)",
},
subCommands: {
install: installCmd,
uninstall: uninstallCmd,
status: statusCmd,
restart: restartCmd,
stop: stopCmd,
logs: logsCmd,
},
})