-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
234 lines (204 loc) · 9.82 KB
/
cli.ts
File metadata and controls
234 lines (204 loc) · 9.82 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
#!/usr/bin/env npx tsx
/**
* CLI for oauth3-skill. Designed for AI agents using shell exec tools.
*
* Two modes:
*
* FOREGROUND (blocks until done — use if your exec tool has long/no timeout):
* npx tsx cli.ts scope-and-execute --scope scope.json --code skill.ts [--timeout 300]
* npx tsx cli.ts execute --code skill.ts [--session ID] [--timeout 300]
* npx tsx cli.ts poll REQUEST_ID [--timeout 300]
* npx tsx cli.ts status REQUEST_ID
*
* BACKGROUND (for agents with short exec timeouts):
* npx tsx cli.ts scope-and-execute --bg --scope scope.json --code skill.ts
* → prints approval URLs + job file path, exits immediately
* → background process polls until completion, writes result to job file
*
* npx tsx cli.ts result JOB_FILE
* → reads job file, prints current status/result. Run after human approves.
*
* scope.json: { "description": "...", "constraints": [...], "secrets": [...], "networks": [...] }
*/
import { OAuth3 } from './index.ts'
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'
import { spawn } from 'node:child_process'
import { dirname, resolve } from 'node:path'
import { randomBytes } from 'node:crypto'
const args = process.argv.slice(2)
const cmd = args[0]
const hasFlag = (name: string) => args.includes(`--${name}`)
function flag(name: string): string | undefined {
const i = args.indexOf(`--${name}`)
return i >= 0 ? args[i + 1] : undefined
}
const timeout = parseInt(flag('timeout') || '300') * 1000
const HOME = process.env.HOME || '/tmp'
const JOBS_DIR = `${HOME}/.oauth3/jobs`
function writeJob(jobFile: string, data: any) {
mkdirSync(dirname(jobFile), { recursive: true })
writeFileSync(jobFile, JSON.stringify(data, null, 2) + '\n')
}
// --- Background launcher ---
if (hasFlag('bg') && cmd !== '__bg-worker') {
// Re-spawn ourselves as a detached background process
const jobId = randomBytes(4).toString('hex')
const jobFile = `${JOBS_DIR}/${jobId}.json`
writeJob(jobFile, { status: 'starting', job_id: jobId, created: new Date().toISOString() })
const worker = spawn(process.execPath, [...process.execArgv, resolve(import.meta.filename!), '__bg-worker', '--job', jobFile, ...args.filter(a => a !== '--bg')], {
detached: true, stdio: 'ignore',
})
worker.unref()
// Quick: submit the scope/execute to get approval URLs before exiting
const client = await OAuth3.create({ token: flag('key'), enclaveUrl: flag('enclave-url'), orchestratorUrl: flag('orchestrator-url') })
if (cmd === 'scope-and-execute') {
const scopeFile = flag('scope')
const codeFile = flag('code')
if (!scopeFile || !codeFile) { console.error('Usage: scope-and-execute --bg --scope FILE --code FILE'); process.exit(1) }
const scope = JSON.parse(readFileSync(scopeFile, 'utf-8'))
const code = readFileSync(codeFile, 'utf-8')
const scopeRes = await client.scope({ ...scope, skill_code: code })
const info = {
status: 'polling_scope', job_id: jobId, job_file: jobFile,
scope_request_id: scopeRes.request_id, session_id: scopeRes.session_id,
scope_approval_url: scopeRes.approval_url,
created: new Date().toISOString(),
}
writeJob(jobFile, info)
// Print for agent to capture
if (scopeRes.approval_url) console.log(`👉 Approve scope: ${scopeRes.approval_url}`)
console.log(`📁 Job file: ${jobFile}`)
console.log(`Run \`npx tsx cli.ts result ${jobFile}\` after approving to get the result.`)
process.exit(0)
} else if (cmd === 'execute') {
const codeFile = flag('code')
if (!codeFile) { console.error('Usage: execute --bg --code FILE'); process.exit(1) }
const code = readFileSync(codeFile, 'utf-8')
const skillId = flag('skill-id') || codeFile.replace(/\.ts$/, '').replace(/.*\//, '')
const sessionId = flag('session')
const execRes = await client.execute({ skill_id: skillId, skill_code: code, session_id: sessionId })
const info = {
status: execRes.status === 'completed' ? 'completed' : 'polling_exec',
job_id: jobId, job_file: jobFile,
exec_request_id: execRes.request_id,
exec_approval_url: execRes.approval_url,
result: execRes.status === 'completed' ? execRes : undefined,
created: new Date().toISOString(),
}
writeJob(jobFile, info)
if (execRes.approval_url) console.log(`👉 Approve: ${execRes.approval_url}`)
if (execRes.status === 'completed') console.log(JSON.stringify(execRes))
console.log(`📁 Job file: ${jobFile}`)
process.exit(execRes.status === 'completed' ? 0 : 0)
}
process.exit(0)
}
// --- Background worker (detached process) ---
if (cmd === '__bg-worker') {
const jobFile = flag('job')!
const innerCmd = args.find(a => a === 'scope-and-execute' || a === 'execute')!
try {
const client = await OAuth3.create({ token: flag('key'), enclaveUrl: flag('enclave-url'), orchestratorUrl: flag('orchestrator-url') })
const job = JSON.parse(readFileSync(jobFile, 'utf-8'))
if (innerCmd === 'scope-and-execute') {
// Poll scope
if (job.scope_request_id) {
const approved = await client.poll(job.scope_request_id, timeout)
if (approved.status !== 'completed') {
writeJob(jobFile, { ...job, status: 'scope_failed', result: approved })
process.exit(1)
}
writeJob(jobFile, { ...job, status: 'scope_approved' })
}
// Execute
const codeFile = flag('code')!
const code = readFileSync(codeFile, 'utf-8')
const skillId = flag('skill-id') || codeFile.replace(/\.ts$/, '').replace(/.*\//, '')
const execRes = await client.execute({ skill_id: skillId, skill_code: code, session_id: job.session_id })
writeJob(jobFile, {
...job, status: 'polling_exec',
exec_request_id: execRes.request_id,
exec_approval_url: execRes.approval_url,
})
if (['completed', 'failed', 'denied'].includes(execRes.status)) {
writeJob(jobFile, { ...job, status: execRes.status, result: execRes })
process.exit(0)
}
const result = await client.poll(execRes.request_id, timeout)
writeJob(jobFile, { ...job, status: result.status, exec_request_id: execRes.request_id, result })
process.exit(0)
} else if (innerCmd === 'execute') {
if (!job.exec_request_id) process.exit(1)
const result = await client.poll(job.exec_request_id, timeout)
writeJob(jobFile, { ...job, status: result.status, result })
process.exit(0)
}
} catch (e: any) {
const job = JSON.parse(readFileSync(jobFile, 'utf-8').toString())
writeJob(jobFile, { ...job, status: 'error', error: e.message })
process.exit(1)
}
process.exit(0)
}
// --- Foreground commands ---
async function main() {
const client = await OAuth3.create({ token: flag('key'), enclaveUrl: flag('enclave-url'), orchestratorUrl: flag('orchestrator-url') })
if (cmd === 'result') {
const jobFile = args[1]
if (!jobFile) { console.error('Usage: result JOB_FILE'); process.exit(1) }
const job = JSON.parse(readFileSync(jobFile, 'utf-8'))
console.log(JSON.stringify(job, null, 2))
process.exit(job.status === 'completed' ? 0 : 1)
} else if (cmd === 'scope-and-execute') {
const scopeFile = flag('scope')
const codeFile = flag('code')
if (!scopeFile || !codeFile) { console.error('Usage: scope-and-execute --scope FILE --code FILE'); process.exit(1) }
const scope = JSON.parse(readFileSync(scopeFile, 'utf-8'))
const code = readFileSync(codeFile, 'utf-8')
const skillId = flag('skill-id') || codeFile.replace(/\.ts$/, '').replace(/.*\//, '')
const scopeRes = await client.scope({ ...scope, skill_code: code })
if (scopeRes.approval_url) console.error(`👉 Approve scope: ${scopeRes.approval_url}`)
console.error(`⏳ Polling scope ${scopeRes.request_id}...`)
const approved = await client.poll(scopeRes.request_id, timeout)
if (approved.status !== 'completed') {
console.log(JSON.stringify(approved))
process.exit(1)
}
console.error(`✅ Scope approved, session: ${scopeRes.session_id}`)
const execRes = await client.execute({ skill_id: skillId, skill_code: code, session_id: scopeRes.session_id })
if (execRes.approval_url) console.error(`👉 Approve execution: ${execRes.approval_url}`)
if (['completed', 'failed', 'denied'].includes(execRes.status)) {
console.log(JSON.stringify(execRes))
process.exit(execRes.status === 'completed' ? 0 : 1)
}
console.error(`⏳ Polling execution ${execRes.request_id}...`)
const result = await client.poll(execRes.request_id, timeout)
console.log(JSON.stringify(result))
process.exit(result.status === 'completed' ? 0 : 1)
} else if (cmd === 'execute') {
const codeFile = flag('code')
if (!codeFile) { console.error('Usage: execute --code FILE'); process.exit(1) }
const code = readFileSync(codeFile, 'utf-8')
const skillId = flag('skill-id') || codeFile.replace(/\.ts$/, '').replace(/.*\//, '')
const sessionId = flag('session')
const result = await client.executeAndWait({ skill_id: skillId, skill_code: code, session_id: sessionId }, timeout)
console.log(JSON.stringify(result))
process.exit(result.status === 'completed' ? 0 : 1)
} else if (cmd === 'poll') {
const reqId = args[1]
if (!reqId) { console.error('Usage: poll REQUEST_ID'); process.exit(1) }
const result = await client.poll(reqId, timeout)
console.log(JSON.stringify(result))
process.exit(result.status === 'completed' ? 0 : 1)
} else if (cmd === 'status') {
const reqId = args[1]
if (!reqId) { console.error('Usage: status REQUEST_ID'); process.exit(1) }
const data = await client['get'](`/execute/${reqId}/status`)
console.log(JSON.stringify(data))
} else {
console.error(`Commands: scope-and-execute, execute, poll, status, result
Add --bg to scope-and-execute or execute for background mode.`)
process.exit(1)
}
}
main().catch(e => { console.error(e.message); process.exit(1) })