@pmate/cli 0.44.7 → 0.44.9
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.
|
@@ -2,6 +2,7 @@ const { execFile } = require('node:child_process')
|
|
|
2
2
|
const { existsSync, mkdirSync } = require('node:fs')
|
|
3
3
|
const { homedir } = require('node:os')
|
|
4
4
|
const path = require('node:path')
|
|
5
|
+
const {modelCatalog}=require('./model-catalog.cjs')
|
|
5
6
|
const methods = ['capabilities','workspaces.list','agents.list','agents.create','agents.update','plugins.list','plugins.configure','runtime.checkEnvironment','runtime.install','runtime.status','scripts.run']
|
|
6
7
|
function createAgentRuntime({ platform = process.platform, home = homedir(), execute, fetcher = fetch, environment = process.env } = {}) {
|
|
7
8
|
let identityEpoch=0
|
|
@@ -11,8 +12,8 @@ function createAgentRuntime({ platform = process.platform, home = homedir(), exe
|
|
|
11
12
|
execFile(file, args, { cwd, timeout: 600000, maxBuffer: 1024 * 1024, env: { ...environment, PATH: [path.join(home,'.local/bin'),path.join(home,'.bun/bin'),environment.PNPM_HOME,path.join(home,'Library/pnpm'),path.join(home,'.local/share/pnpm'),'/opt/homebrew/bin','/usr/local/bin',environment.PATH].filter(Boolean).join(':'), GIT_TERMINAL_PROMPT: '0' } }, (error, stdout) => error ? reject(new Error(`${path.basename(file)} 执行失败,请检查本地安装和仓库访问权限`)) : resolve(stdout.trim()))
|
|
12
13
|
}))
|
|
13
14
|
const installed = async () => {
|
|
14
|
-
if(existsSync(path.join(root,'packages/
|
|
15
|
-
try{await run('
|
|
15
|
+
if(existsSync(path.join(root,'packages/merry-runtime/src/runtime-cli.ts'))){try{await run('bun',['packages/merry-runtime/src/runtime-cli.ts','--help'],root);await run('bun',['packages/merry-runtime/src/plugin.ts','--help'],root);return true}catch{return false}}
|
|
16
|
+
try{await run('merry-runtime',['--help']);await run('merry-plugin',['--help']);return true}catch{return false}
|
|
16
17
|
}
|
|
17
18
|
const check = async () => {
|
|
18
19
|
const checks = [{ id:'macos', name:'macOS', ready:platform==='darwin', required:true }]
|
|
@@ -35,12 +36,15 @@ function createAgentRuntime({ platform = process.platform, home = homedir(), exe
|
|
|
35
36
|
const missing=environment.checks.filter(c=>!c.ready).map(c=>c.name)
|
|
36
37
|
if(missing.length)throw new Error(`请先安装 ${missing.join('、')},然后重新检测`)
|
|
37
38
|
if(environment.installed&&!existsSync(root)){progress={phase:'ready',message:'已检测到现有 Runtime'};return progress}
|
|
38
|
-
if(existsSync(root)&&!existsSync(path.join(root,'packages/
|
|
39
|
+
if(existsSync(root)&&!existsSync(path.join(root,'packages/merry-runtime/src/runtime-cli.ts')))throw new Error('安装目录不是当前 Runtime 源码,请先保留本地改动并更新仓库后重试;不会覆盖现有文件')
|
|
39
40
|
if(!existsSync(root)){mkdirSync(path.dirname(root),{recursive:true,mode:0o700});progress={phase:'downloading',message:'正在获取 Runtime 源码'};await run('git',['clone','https://github.com/parrot-mate/pmate-agents.git',root])}
|
|
40
41
|
progress={phase:'installing',message:'正在安装依赖'}
|
|
41
42
|
await run('pnpm',['install','--frozen-lockfile','--ignore-scripts'],root)
|
|
42
43
|
progress={phase:'verifying',message:'正在验证 Runtime'}
|
|
43
|
-
await run('bun',['packages/
|
|
44
|
+
await run('bun',['packages/merry-runtime/src/runtime-cli.ts','--help'],root)
|
|
45
|
+
await run('bun',['packages/merry-runtime/src/plugin.ts','--help'],root)
|
|
46
|
+
await run('bun',['packages/merry-runtime/src/setup.ts'],root)
|
|
47
|
+
await run('bun',['packages/merry-runtime/src/plugin.ts','install'],root)
|
|
44
48
|
progress={phase:'ready',message:'Runtime 已安装'}
|
|
45
49
|
return progress
|
|
46
50
|
}catch(error){progress={phase:'error',error:error.message};throw error}finally{installation=undefined}
|
|
@@ -53,6 +57,7 @@ function createAgentRuntime({ platform = process.platform, home = homedir(), exe
|
|
|
53
57
|
if(method==='runtime.checkEnvironment')return check()
|
|
54
58
|
if(method==='runtime.status')return {...progress,installed:await installed()}
|
|
55
59
|
if(method==='runtime.install')return install()
|
|
60
|
+
if(method==='scripts.run'&&input?.id==='runtime.models'&&input.args&&Object.keys(input.args).length===0)return modelCatalog({environment:{...environment,PATH:[path.join(home,'.local/bin'),path.join(home,'.bun/bin'),'/opt/homebrew/bin','/usr/local/bin',environment.PATH].filter(Boolean).join(':')}})
|
|
56
61
|
if(method==='scripts.run') {if(input?.id!=='runtime.repair'||!input.args||Object.keys(input.args).length)throw new Error('未知安装操作');return install()}
|
|
57
62
|
if(method==='workspaces.list')return api('/v2/workspaces')
|
|
58
63
|
const {scope,id,config,expectedRevision}=input||{}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const {spawn}=require('node:child_process')
|
|
2
|
+
const {createInterface}=require('node:readline')
|
|
3
|
+
const {tmpdir}=require('node:os')
|
|
4
|
+
const EFFORTS=['none','minimal','low','medium','high','xhigh','max','ultra']
|
|
5
|
+
function normalize(engine,rows){
|
|
6
|
+
if(!Array.isArray(rows))throw Error('invalid_catalog')
|
|
7
|
+
const seen=new Set(),models=[]
|
|
8
|
+
for(const row of rows){
|
|
9
|
+
const id=engine==='codex'?row.model:(row.resolvedModel||row.value)
|
|
10
|
+
if(typeof id!=='string'||!/^[-a-zA-Z0-9_.:/[\]]{1,160}$/.test(id)||seen.has(id))continue
|
|
11
|
+
seen.add(id)
|
|
12
|
+
const efforts=(engine==='codex'?row.supportedReasoningEfforts?.map(x=>x.reasoningEffort):row.supportedEffortLevels)??[]
|
|
13
|
+
if(!Array.isArray(efforts))continue
|
|
14
|
+
models.push({id,label:typeof row.displayName==='string'?row.displayName.slice(0,120):id,efforts:[...new Set(efforts.filter(e=>EFFORTS.includes(e)))],...(engine==='codex'&&efforts.includes(row.defaultReasoningEffort)?{defaultEffort:row.defaultReasoningEffort}:{})})
|
|
15
|
+
}
|
|
16
|
+
if(!models.length)throw Error('empty_catalog')
|
|
17
|
+
return models
|
|
18
|
+
}
|
|
19
|
+
function queryModels(engine,{environment=process.env,spawnProcess=spawn,timeoutMs=8000}={}){
|
|
20
|
+
return new Promise(resolve=>{
|
|
21
|
+
let child,reader,timer,settled=false,bytes=0,models=[],pages=0
|
|
22
|
+
const finish=(result)=>{if(settled)return;settled=true;clearTimeout(timer);reader?.close();child?.stdin?.end();child?.kill();resolve(result)}
|
|
23
|
+
const fail=()=>finish({source:'fallback',models:[],reason:'catalog_unavailable'})
|
|
24
|
+
try{
|
|
25
|
+
const env={...environment};delete env.CLAUDECODE
|
|
26
|
+
const args=engine==='codex'?['app-server','--stdio']:['--print','--input-format','stream-json','--output-format','stream-json','--verbose','--no-session-persistence','--safe-mode','--tools','','--strict-mcp-config']
|
|
27
|
+
child=spawnProcess(engine==='codex'?'codex':'claude',args,{cwd:tmpdir(),env,stdio:['pipe','pipe','pipe']})
|
|
28
|
+
const send=v=>child.stdin.write(JSON.stringify(v)+'\n')
|
|
29
|
+
timer=setTimeout(fail,timeoutMs)
|
|
30
|
+
child.on('error',fail);child.on('exit',()=>{if(!settled)fail()});child.stdin.on('error',fail)
|
|
31
|
+
child.stderr.on('data',()=>{})
|
|
32
|
+
reader=createInterface({input:child.stdout})
|
|
33
|
+
child.stdout.on('data',b=>{bytes+=b.length;if(bytes>1024*1024)fail()})
|
|
34
|
+
reader.on('line',line=>{if(settled)return;try{
|
|
35
|
+
const v=JSON.parse(line)
|
|
36
|
+
if(engine==='codex'){
|
|
37
|
+
if(v.id===1){if(v.error)return fail();send({method:'initialized',params:{}});send({id:2,method:'model/list',params:{limit:100}})}
|
|
38
|
+
if(v.id===2){if(v.error)return fail();models.push(...normalize(engine,v.result?.data));if(v.result.nextCursor){if(++pages>10)return fail();send({id:2,method:'model/list',params:{limit:100,cursor:v.result.nextCursor}})}else finish({source:'live',models,checkedAt:new Date().toISOString()})}
|
|
39
|
+
}else if(v.type==='control_response'&&v.response?.request_id==='models'){
|
|
40
|
+
if(v.response.subtype!=='success')return fail();finish({source:'live',models:normalize(engine,v.response.response?.models),checkedAt:new Date().toISOString()})
|
|
41
|
+
}
|
|
42
|
+
}catch{fail()}})
|
|
43
|
+
send(engine==='codex'?{id:1,method:'initialize',params:{clientInfo:{name:'merry_model_picker',version:'1'}}}:{type:'control_request',request_id:'models',request:{subtype:'initialize'}})
|
|
44
|
+
}catch{fail()}
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
async function modelCatalog(options){const [codex,claude]=await Promise.all([queryModels('codex',options),queryModels('claude',options)]);return{codex,claude}}
|
|
48
|
+
module.exports={modelCatalog,queryModels,normalize}
|