feat: add constrained ZCode chat assistant and local runtime setup

This commit is contained in:
阿白叔 committed 2026-10-18 00:36:41 +08:00
1 parent fff2a29ec0
commit 3363a88abf
23 files changed
+569 -14

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
// Verify and extract the pinned official release; never execute its desktop installer.
'use strict';
const fs=require('node:fs/promises'),native=require('node:fs'),path=require('node:path'),crypto=require('node:crypto');
const VERSION='3.14.3';
const {execFile}=require('node:child_process'),{promisify}=require('node:util'),execFileAsync=promisify(execFile);
async function sha(file){const hash=crypto.createHash('sha256');for await(const chunk of native.createReadStream(file))hash.update(chunk);return 'sha256:'+hash.digest('hex');}
async function extract(root,file,digest){
const sevenZip=path.join(process.env.ProgramFiles||'C:/Program Files','7-Zip','7z.exe');await fs.access(sevenZip).catch(()=>{throw Error('请先安装 7-Zip,再重新运行此脚本完成解包。');});
const versionDir=path.join(root,VERSION),installer=path.join(versionDir,'installer'),packageDir=path.join(versionDir,'package');await fs.mkdir(installer,{recursive:true});
await execFileAsync(sevenZip,['x','-y','-o'+installer,file],{windowsHide:true,maxBuffer:2*1024*1024});
await execFileAsync(sevenZip,['x','-y','-o'+packageDir,path.join(installer,'$PLUGINSDIR','app-64.7z')],{windowsHide:true,maxBuffer:2*1024*1024});
const runtimePath=path.join(packageDir,'resources','glm','zcode.cjs'),builtinPath=path.join(packageDir,'resources','config','provider','zcode-builtin.json');await fs.access(builtinPath);
const manifest={releaseVersion:VERSION,cliVersion:'0.16.9',runtimePath,runtimeSha256:await sha(runtimePath),installerSha256:digest,source:'https://github.com/zai-org/ZCode/releases/tag/v'+VERSION};
await fs.writeFile(path.join(root,'manifest.json'),JSON.stringify(manifest,null,2));console.log('官方运行包已解包:'+runtimePath);return manifest;
}
async function main(){
const root=path.join(process.env.LOCALAPPDATA,'OrderNotificationAutomation','zcode-runtime'),dir=path.join(root,'downloads');await fs.mkdir(dir,{recursive:true});
const response=await fetch('https://api.github.com/repos/zai-org/ZCode/releases/tags/v'+VERSION,{signal:AbortSignal.timeout(15000)});if(!response.ok)throw Error('无法读取官方 release:HTTP '+response.status);
const release=await response.json(),asset=release.assets.find(a=>a.name===`ZCode-${VERSION}-win-x64.exe`);if(!asset||!/^sha256:[a-f0-9]{64}$/.test(asset.digest||''))throw Error('官方 release 缺少可核对的 SHA-256,停止下载。');
const file=path.join(dir,asset.name);let cached=false;try{cached=(await fs.stat(file)).size===asset.size&&await sha(file)===asset.digest;}catch{}
if(cached){console.log('官方安装包已通过 SHA-256 校验。');return extract(root,file,asset.digest);}
const redirect=await fetch(asset.url,{headers:{Accept:'application/octet-stream'},signal:AbortSignal.timeout(15000)});if(!redirect.ok)throw Error('官方资源暂不可用。');const downloadURL=redirect.url;await redirect.body.cancel();
if(new URL(downloadURL).hostname!=='release-assets.githubusercontent.com')throw Error('官方资源跳转地址不符合预期。');
const handle=await fs.open(file,'w'),chunkSize=256*1024,total=Math.ceil(asset.size/chunkSize);let next=0,complete=0,failed;
try{await handle.truncate(asset.size);const workers=await Promise.allSettled(Array.from({length:24},async()=>{
try{for(;;){if(failed)return;const index=next++;if(index>=total)return;const start=index*chunkSize,end=Math.min(start+chunkSize,asset.size)-1;let bytes,last;
for(let attempt=0;attempt<3;attempt++){try{const r=await fetch(downloadURL,{headers:{Range:`bytes=${start}-${end}`},signal:AbortSignal.timeout(30000)});if(r.status!==206||r.headers.get('content-range')!==`bytes ${start}-${end}/${asset.size}`)throw Error('分段响应无效:HTTP '+r.status);bytes=Buffer.from(await r.arrayBuffer());if(bytes.length!==end-start+1)throw Error('下载分段不完整');last=null;break;}catch(e){last=e;}}
if(last)throw last;await handle.write(bytes,0,bytes.length,start);complete++;if(complete%50===0||complete===total)console.log('官方下载:'+Math.round(complete/total*100)+'%');
}}catch(error){failed=error;throw error;}
}));const error=workers.find(r=>r.status==='rejected');if(error)throw error.reason;}finally{await handle.close();}
if(await sha(file)!==asset.digest)throw Error('官方 SHA-256 不匹配,停止使用下载文件。');
await fs.writeFile(path.join(root,'download.json'),JSON.stringify({version:VERSION,file,sha256:asset.digest,source:'https://github.com/zai-org/ZCode/releases/tag/v'+VERSION},null,2));
console.log('官方安装包已下载并通过 SHA-256 校验:'+file);
return extract(root,file,asset.digest);
}
if(require.main===module)main().catch(e=>{console.error('ZCode 下载未完成:'+e.message);process.exitCode=1;});
module.exports={main};