@pmate/cli 0.44.10 → 0.45.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.
|
@@ -2,15 +2,15 @@ const { execFile } = require('node:child_process')
|
|
|
2
2
|
const { homedir } = require('node:os')
|
|
3
3
|
const path = require('node:path')
|
|
4
4
|
const {modelCatalog}=require('./model-catalog.cjs')
|
|
5
|
-
const {
|
|
6
|
-
const methods = ['capabilities','workspaces.list','agents.list','agents.create','agents.update','plugins.list','plugins.configure','
|
|
5
|
+
const {createLocalExecutor}=require('./local-exec.cjs')
|
|
6
|
+
const methods = ['capabilities','workspaces.list','agents.list','agents.create','agents.update','plugins.list','plugins.configure','local.info','local.exec','runtime.inspect','scripts.run']
|
|
7
7
|
function createAgentRuntime({ platform = process.platform, home = homedir(), execute, fetcher = fetch, environment = process.env } = {}) {
|
|
8
8
|
let identityEpoch=0
|
|
9
9
|
let identity
|
|
10
|
-
const run = execute || ((file, args, cwd, timeout = 600000) => new Promise((resolve, reject) => {
|
|
11
|
-
execFile(file, args, { cwd, timeout, 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()))
|
|
10
|
+
const run = execute || ((file, args, cwd, timeout = 600000, credentials = {}) => new Promise((resolve, reject) => {
|
|
11
|
+
execFile(file, args, { cwd, timeout, maxBuffer: 4 * 1024 * 1024, env: { ...environment, ...credentials, 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
12
|
}))
|
|
13
|
-
const
|
|
13
|
+
const localExec=createLocalExecutor({home,environment})
|
|
14
14
|
const api = async (route, method='GET', body) => {
|
|
15
15
|
if (!identity) throw new Error('请先登录梅丽号')
|
|
16
16
|
const response = await fetcher(`https://issue-api.pmate.chat${route}`, { method, headers: { authorization:`Bearer ${identity.token}`, 'x-pmate-app':identity.appId, 'content-type':'application/json' }, ...(body===undefined?{}:{body:JSON.stringify(body)}), signal:AbortSignal.timeout(30000) })
|
|
@@ -21,14 +21,27 @@ function createAgentRuntime({ platform = process.platform, home = homedir(), exe
|
|
|
21
21
|
const invoke = async (method,input) => {
|
|
22
22
|
if(!methods.includes(method))throw new Error('ELECTRON_METHOD_UNAVAILABLE')
|
|
23
23
|
if(method==='capabilities')return methods
|
|
24
|
-
if(method==='
|
|
25
|
-
if(method==='
|
|
24
|
+
if(method==='local.info')return {platform,arch:process.arch,home}
|
|
25
|
+
if(method==='local.exec'){
|
|
26
|
+
if(!identity)throw new Error('请先登录梅丽号')
|
|
27
|
+
const epoch=identityEpoch,result=await localExec(input)
|
|
28
|
+
if(epoch!==identityEpoch||!identity)throw new Error('登录身份已改变,请重新执行')
|
|
29
|
+
return result
|
|
30
|
+
}
|
|
26
31
|
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(':')}})
|
|
27
32
|
if(method==='scripts.run')throw new Error('未知脚本操作')
|
|
28
33
|
if(method==='workspaces.list')return api('/v2/workspaces')
|
|
29
34
|
const {scope,id,config,expectedRevision}=input||{}
|
|
30
35
|
if(!scope||!['namespace','topic'].every(k=>typeof scope[k]==='string'&&/^[a-z0-9][a-z0-9_-]{0,127}$/.test(scope[k])))throw new Error('无效工作区')
|
|
31
36
|
const base=`/v2/topics/${encodeURIComponent(scope.namespace)}/${encodeURIComponent(scope.topic)}`
|
|
37
|
+
if(method==='runtime.inspect'){
|
|
38
|
+
if(!identity)throw new Error('请先登录梅丽号')
|
|
39
|
+
if(!['runs','run','run-logs'].includes(input.kind)||Object.keys(input).some(k=>!['scope','kind','agentId','runId','status'].includes(k)))throw new Error('无效的 Run 查询')
|
|
40
|
+
const epoch=identityEpoch
|
|
41
|
+
const output=await run('merry-runtime',['inspect','--request',JSON.stringify(input)],undefined,600000,{PCS_AGENT_API_TOKEN:identity.token,PCS_ISSUES_API_URL:'https://issue-api.pmate.chat',PCS_AGENT_ID:undefined,PCS_AGENT_RUN_ID:undefined,VEGA_RUN_TOKEN:undefined,VEGA_INPUT_ID:undefined})
|
|
42
|
+
if(epoch!==identityEpoch||!identity)throw new Error('登录身份已改变,请重新打开页面')
|
|
43
|
+
return JSON.parse(output)
|
|
44
|
+
}
|
|
32
45
|
if(method==='agents.list')return api(base+'/vega-agents')
|
|
33
46
|
if(method==='plugins.list')return api(base+'/runtime-plugins')
|
|
34
47
|
if(typeof id!=='string'||!(method==='plugins.configure'?/^(?:[a-z][a-z0-9-]*:)?[a-z][a-z0-9-]*$/:/^0x[a-f0-9]{64}$/).test(id)||!config||typeof config!=='object'||Array.isArray(config))throw new Error('无效配置')
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const {execFile}=require('node:child_process')
|
|
2
|
+
const path=require('node:path')
|
|
3
|
+
/** Stable local process capability. Command selection and business rules belong to Web/bridge. */
|
|
4
|
+
function createLocalExecutor({home,environment,execute=execFile}){
|
|
5
|
+
const 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(':')}
|
|
6
|
+
return input=>{
|
|
7
|
+
if(!input||typeof input.command!=='string'||!input.command||input.command.includes('\0')||!Array.isArray(input.args)||input.args.some(v=>typeof v!=='string'||v.includes('\0'))||input.args.length>256||input.args.some(v=>v.length>131072))throw Error('Invalid local command')
|
|
8
|
+
if(input.cwd!==undefined&&(typeof input.cwd!=='string'||!path.isAbsolute(input.cwd)||input.cwd.includes('\0')))throw Error('Invalid working directory')
|
|
9
|
+
if(input.env!==undefined&&(!input.env||typeof input.env!=='object'||Array.isArray(input.env)||Object.entries(input.env).some(([k,v])=>!k||k.includes('=')||k.includes('\0')||typeof v!=='string'||v.includes('\0'))))throw Error('Invalid process environment')
|
|
10
|
+
const timeout=input.timeoutMs??10000
|
|
11
|
+
if(!Number.isSafeInteger(timeout)||timeout<100||timeout>120000)throw Error('Invalid command timeout')
|
|
12
|
+
return new Promise(resolve=>execute(input.command,input.args,{cwd:input.cwd,env:{...env,...input.env},shell:false,timeout,maxBuffer:1024*1024,windowsHide:true},(error,stdout,stderr)=>resolve({stdout:String(stdout??''),stderr:String(stderr??''),exitCode:error?(typeof error.code==='number'?error.code:null):0,error:error?(error.code==='ENOENT'?'NOT_FOUND':error.code==='ERR_CHILD_PROCESS_STDIO_MAXBUFFER'?'OUTPUT_LIMIT':error.killed?'TIMEOUT':'PROCESS_FAILED'):undefined})))
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
module.exports={createLocalExecutor}
|
package/package.json
CHANGED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
const {existsSync,readFileSync,realpathSync}=require('node:fs')
|
|
2
|
-
const path=require('node:path')
|
|
3
|
-
const SHA=/^[a-f0-9]{40}$/
|
|
4
|
-
const SEMVER=/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/
|
|
5
|
-
const versionAtLeast=(local,latest)=>{const a=local.split('.').map(Number),b=latest.split('.').map(Number);for(let i=0;i<3;i++)if(a[i]!==b[i])return a[i]>b[i];return true}
|
|
6
|
-
const row=(id,value)=>({id,name:id,required:true,ready:false,...value})
|
|
7
|
-
|
|
8
|
-
/** Read-only dependency gate. Each recheck observes remote versions again; never installs. */
|
|
9
|
-
async function checkEnvironment({platform,home,run,fetcher}){
|
|
10
|
-
const command=(file,args,cwd)=>run(file,args,cwd,10000)
|
|
11
|
-
const tools=await Promise.all(['git','node','bun','pnpm'].map(async id=>{
|
|
12
|
-
try{const version=await command(id,['--version']);const ready=id!=='node'||/^v?(\d+)\./.test(version)&&Number(version.replace(/^v/,'').split('.')[0])>=24;return row(id,{version,ready,status:ready?'ready':'outdated',...(ready?{}:{detail:'需要 Node.js 24 或更新版本'})})}
|
|
13
|
-
catch{return row(id,{status:'missing'})}
|
|
14
|
-
}))
|
|
15
|
-
async function sourceCheck(id,packageName,repository,load){
|
|
16
|
-
let info
|
|
17
|
-
try{info=await load()}
|
|
18
|
-
catch(error){return row(id,{...(error.installedVersion?{version:error.installedVersion,status:'broken',detail:'本地安装缺少可用的版本检查信息,请按 Skill 更新并重新构建'}:{status:'missing',detail:'未找到可用的本地安装;请按安装 Skill 安装或更新'})})}
|
|
19
|
-
if(!info||typeof info!=='object'||info.package!==packageName||!SEMVER.test(info.version)||!SHA.test(info.revision)||typeof info.root!=='string'||!path.isAbsolute(info.root))return row(id,{status:'broken',detail:'安装信息不完整,请按 Skill 更新并重新构建'})
|
|
20
|
-
const local={version:info.version,revision:info.revision}
|
|
21
|
-
try{
|
|
22
|
-
const root=await command('git',['rev-parse','--show-toplevel'],info.root)
|
|
23
|
-
const remote=await command('git',['remote','get-url','origin'],root)
|
|
24
|
-
// Do not compare a copied build or an unrelated repository to an apparent version.
|
|
25
|
-
if(!new RegExp(`(?:/|:)parrot-mate/${repository}(?:\\.git)?$`).test(remote))return row(id,{...local,status:'broken',detail:'安装来源不是预期的源码仓库'})
|
|
26
|
-
if(id==='merry-runtime'&&realpathSync(root)!==realpathSync(path.join(home,'.pmate/merry-runtime')))return row(id,{...local,status:'broken',detail:'Runtime 必须安装到 ~/.pmate/merry-runtime'})
|
|
27
|
-
const latest=(await command('git',['ls-remote','origin','refs/heads/main'],root)).split(/\s+/)[0]
|
|
28
|
-
if(!SHA.test(latest))throw Error('Invalid remote revision')
|
|
29
|
-
const target={...local,latestVersion:`main@${latest.slice(0,12)}`,latestRevision:latest}
|
|
30
|
-
if(await command('git',['status','--porcelain','--untracked-files=no'],root))return row(id,{...target,status:'modified',detail:'本地源码有未提交改动,请保留并检查后再更新'})
|
|
31
|
-
if(info.revision!==latest)return row(id,{...target,status:'outdated',detail:'本地执行版本与远端 main 不一致,请按 Skill 更新;CLI 更新源码后还需重新构建'})
|
|
32
|
-
return row(id,{...target,ready:true,status:'ready'})
|
|
33
|
-
}catch{return row(id,{...local,status:'unknown',detail:'无法确认远端 main;请检查 GitHub 访问权限和网络后重新检查'})}
|
|
34
|
-
}
|
|
35
|
-
const runtime=sourceCheck('merry-runtime','@pmate/merry-runtime','pmate-agents',async()=>{
|
|
36
|
-
const root=path.join(home,'.pmate/merry-runtime')
|
|
37
|
-
if(!existsSync(path.join(root,'packages/merry-runtime/package.json')))throw Error('missing')
|
|
38
|
-
const pkg=JSON.parse(readFileSync(path.join(root,'packages/merry-runtime/package.json'),'utf8'))
|
|
39
|
-
try{const info=JSON.parse(await command('bun',['packages/merry-runtime/src/runtime-cli.ts','--build-info'],root));await command('bun',['packages/merry-runtime/src/plugin.ts','--help'],root);return info}
|
|
40
|
-
catch{throw Object.assign(Error('Runtime verification failed'),{installedVersion:pkg.version})}
|
|
41
|
-
})
|
|
42
|
-
const merry=sourceCheck('merry-cli','@pmate/merry-cli','pmate-merry',async()=>{const version=await command('merry',['--version']);try{return JSON.parse(await command('merry',['--build-info']))}catch{throw Object.assign(Error('CLI verification failed'),{installedVersion:version})}})
|
|
43
|
-
const pmate=(async()=>{
|
|
44
|
-
let version,latestVersion
|
|
45
|
-
try{const output=await command('pmate',['--version']);version=output.trim();if(!SEMVER.test(version))return row('pmate-cli',{status:'broken',detail:'无法识别 pmate --version 的输出'})}
|
|
46
|
-
catch{return row('pmate-cli',{status:'missing'})}
|
|
47
|
-
try{
|
|
48
|
-
const response=await fetcher('https://registry.npmjs.org/@pmate%2fcli/latest',{signal:AbortSignal.timeout(10000),headers:{'cache-control':'no-cache'}})
|
|
49
|
-
if(!response.ok)throw Error('registry unavailable')
|
|
50
|
-
const pkg=await response.json();latestVersion=pkg.version
|
|
51
|
-
if(pkg.name!=='@pmate/cli'||!SEMVER.test(latestVersion))throw Error('invalid registry version')
|
|
52
|
-
const ready=versionAtLeast(version,latestVersion)
|
|
53
|
-
return row('pmate-cli',{version,latestVersion,ready,status:ready?'ready':'outdated'})
|
|
54
|
-
}catch{return row('pmate-cli',{version,status:'unknown',detail:'无法确认 npm 最新版本;请检查网络后重新检查'})}
|
|
55
|
-
})()
|
|
56
|
-
const libraries=await Promise.all([runtime,merry,pmate])
|
|
57
|
-
const checks=[row('macos',{name:'macOS',ready:platform==='darwin',status:platform==='darwin'?'ready':'unsupported'}),...tools,...libraries]
|
|
58
|
-
const ready=checks.every(c=>c.ready)
|
|
59
|
-
return {schemaVersion:2,supported:platform==='darwin',installed:ready,ready,checks}
|
|
60
|
-
}
|
|
61
|
-
module.exports={checkEnvironment}
|