@heyhru/auto-app-agent 0.0.0-stage → 0.1.0
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.
- package/README.md +23 -2
- package/cli.js +40 -0
- package/doctor.js +52 -0
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# auto-app 机器端
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
需要 Node.js 22 或更高版本。在目标 mini 上执行 `npm install -g @heyhru/auto-app-agent`。
|
|
4
|
+
|
|
5
|
+
在服务端生成机器配置。第一个参数使用项目绑定的 macminis 机器 ID,第二个参数使用 mini 可访问的 app-server HTTPS 地址(本机联调可用 http://127.0.0.1:8788)。
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
node packages/app-agent/cli.js pair MACHINE_ID https://server.example.com /private/tmp/mini.json
|
|
9
|
+
AUTO_APP_MACHINE_CONFIGS=/private/tmp/mini.json pnpm dev
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
配置文件包含机器凭据,通过私密渠道复制到对应 mini。多台机器的配置路径在 AUTO_APP_MACHINE_CONFIGS 中用逗号分隔。机器以日后构建 App 的 macOS 用户启动:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
auto-app start /path/to/mini.json
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
随后在项目的「检查环境」中点击「开始检查」。也可直接在 mini 上运行单次检查:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
auto-app doctor git@example.com:team/app.git main
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
默认运行 OpenCode 和 testwow 的版本检查。使用定制 Pi 的机器在启动 agent 或运行 doctor 时设置 `AUTO_APP_PROVIDER=pi` 和 `AUTO_APP_PI_BIN=/absolute/path/to/pi`。doctor 在全部检查通过时退出码为 0,任何检查失败时为 1。
|
package/cli.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { randomBytes } from 'node:crypto'
|
|
3
|
+
import { readFile, writeFile } from 'node:fs/promises'
|
|
4
|
+
import { setTimeout as sleep } from 'node:timers/promises'
|
|
5
|
+
import { doctor } from './doctor.js'
|
|
6
|
+
|
|
7
|
+
const [command, first, second] = process.argv.slice(2)
|
|
8
|
+
if (command === 'pair' && first && second && process.argv[5]) {
|
|
9
|
+
const config = {machine:first, server:new URL(second).origin, token:randomBytes(32).toString('hex')}
|
|
10
|
+
await writeFile(process.argv[5],JSON.stringify(config),{mode:0o600,flag:'wx'})
|
|
11
|
+
} else if (command === 'doctor' && first && second) {
|
|
12
|
+
const results = await doctor(first, second)
|
|
13
|
+
console.log(JSON.stringify(results,null,2))
|
|
14
|
+
process.exitCode = results.every(r => r.ok) ? 0 : 1
|
|
15
|
+
} else if (command === 'start' && first) {
|
|
16
|
+
const {server,token} = JSON.parse(await readFile(first,'utf8'))
|
|
17
|
+
const origin = new URL(server)
|
|
18
|
+
if (origin.protocol !== 'https:' && !['localhost','127.0.0.1','[::1]'].includes(origin.hostname)) throw new Error('远程服务需要 HTTPS')
|
|
19
|
+
async function post(path, body) {
|
|
20
|
+
const response = await fetch(new URL('/api/agent/'+path,origin), {
|
|
21
|
+
method:'POST', headers:{authorization:'Bearer '+token,'content-type':'application/json'},
|
|
22
|
+
body:JSON.stringify(body), signal:AbortSignal.timeout(15000), redirect:'error',
|
|
23
|
+
})
|
|
24
|
+
if (!response.ok) throw new Error('app-server HTTP '+response.status)
|
|
25
|
+
return response.json()
|
|
26
|
+
}
|
|
27
|
+
for (;;) {
|
|
28
|
+
try {
|
|
29
|
+
const job = await post('claim',{})
|
|
30
|
+
if (job.ticket) {
|
|
31
|
+
const results = await doctor(job.repoUrl,job.branch)
|
|
32
|
+
await post('finish',{ticket:job.ticket,results})
|
|
33
|
+
}
|
|
34
|
+
} catch (error) { console.error(error.message) }
|
|
35
|
+
await sleep(3000)
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
console.error('auto-app pair <machine-id> <server-url> <config-file> | auto-app doctor <repo-url> <branch> | auto-app start <machine-config.json>')
|
|
39
|
+
process.exitCode = 2
|
|
40
|
+
}
|
package/doctor.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process'
|
|
2
|
+
import { promisify } from 'node:util'
|
|
3
|
+
import { hostname } from 'node:os'
|
|
4
|
+
|
|
5
|
+
const exec = promisify(execFile)
|
|
6
|
+
async function run(file, args) {
|
|
7
|
+
try {
|
|
8
|
+
const { stdout } = await exec(file, args, {
|
|
9
|
+
timeout: 15000, maxBuffer: 4 * 1024 * 1024,
|
|
10
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: '0', GIT_SSH_COMMAND: 'ssh -o BatchMode=yes -o ConnectTimeout=8' },
|
|
11
|
+
})
|
|
12
|
+
return stdout.trim()
|
|
13
|
+
} catch (error) {
|
|
14
|
+
// Command errors may contain a credential-bearing Git URL.
|
|
15
|
+
throw new Error(error.code === 'ENOENT' ? '找不到 '+file : file+' 执行失败('+(error.signal || error.code)+')')
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function check(operation) {
|
|
19
|
+
try { return { ok: true, detail: await operation() } }
|
|
20
|
+
catch (error) { return { ok: false, detail: error.message } }
|
|
21
|
+
}
|
|
22
|
+
export async function doctor(repo, branch) {
|
|
23
|
+
const provider = process.env.AUTO_APP_PROVIDER || 'opencode'
|
|
24
|
+
const [git, xcode, tools] = await Promise.all([
|
|
25
|
+
check(async () => {
|
|
26
|
+
if (!/^(?:(?:https?|ssh):\/\/[^\s/]+\/[^\s]+|[\w.-]+@[\w.-]+:[^\s]+)$/.test(repo)) throw new Error('仓库地址无效')
|
|
27
|
+
await run('git', ['check-ref-format', 'refs/heads/'+branch])
|
|
28
|
+
const ref = 'refs/heads/'+branch
|
|
29
|
+
const output = await run('git', ['-c','protocol.allow=never','-c','protocol.https.allow=always','-c','protocol.http.allow=always','-c','protocol.ssh.allow=always','ls-remote','--heads','--',repo,ref])
|
|
30
|
+
if (!output.split('\n').some(line => line.split('\t')[1] === ref)) throw new Error('仓库可访问,目标分支不存在')
|
|
31
|
+
return '已找到 '+ref
|
|
32
|
+
}),
|
|
33
|
+
check(async () => {
|
|
34
|
+
const version = await run('xcodebuild',['-version'])
|
|
35
|
+
const {devices} = JSON.parse(await run('xcrun',['simctl','list','devices','available','--json']))
|
|
36
|
+
const simulators = Object.entries(devices).filter(([runtime]) => runtime.includes('.iOS-')).flatMap(([,items]) => items).filter(d => d.isAvailable)
|
|
37
|
+
if (!simulators.length) throw new Error(version+',无可用 iOS 模拟器')
|
|
38
|
+
return version+' · '+simulators[0].name
|
|
39
|
+
}),
|
|
40
|
+
(async () => {
|
|
41
|
+
const results = await Promise.all([provider, 'testwow'].map(async name => {
|
|
42
|
+
const binary = name === 'pi' ? process.env.AUTO_APP_PI_BIN || 'pi' : name
|
|
43
|
+
return check(async () => {
|
|
44
|
+
if (!['opencode','pi','testwow'].includes(name)) throw new Error('未知 provider')
|
|
45
|
+
return name+' '+(await run(binary,['--version'])).slice(0,160)
|
|
46
|
+
})
|
|
47
|
+
}))
|
|
48
|
+
return {ok: results.every(r => r.ok), detail: results.map(r => r.detail).join(' · ')}
|
|
49
|
+
})(),
|
|
50
|
+
])
|
|
51
|
+
return [{ok:true, detail:hostname()},git,xcode,tools]
|
|
52
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@heyhru/auto-app-agent",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
}
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"publishConfig": {"access": "public"},
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {"node": ">=22"},
|
|
7
|
+
"bin": {"auto-app": "./cli.js"},
|
|
8
|
+
"files": ["cli.js", "doctor.js"]
|
|
9
|
+
}
|