feat: integrate DSH with audited isolated Git repairs
This commit is contained in:
1 parent
7104b6745c
commit
e2b3e66a5b
29 files changed
+381
-50
No files matched your search
@@ -3,11 +3,7 @@ const path=require('node:path'),crypto=require('node:crypto'),fs=require('node:f
|
||||
const fail=(message,status=400)=>Object.assign(new Error(message),{status}),now=()=>new Date().toISOString();
|
||||
const {ConversationHistory}=require('./conversation-history.cjs');
|
||||
const day=()=>new Intl.DateTimeFormat('en-CA',{timeZone:'Asia/Shanghai'}).format(new Date());
|
||||
function installedRuntime(dir){try{
|
||||
const root=path.join(path.dirname(dir),'zcode-runtime'),manifest=JSON.parse(fs.readFileSync(path.join(root,'manifest.json'),'utf8')),entry=path.join(root,'3.14.3','package','resources','glm','zcode.cjs');
|
||||
if(manifest.releaseVersion!=='3.14.3'||manifest.cliVersion!=='0.16.9'||path.resolve(manifest.runtimePath)!==path.resolve(entry))return '';
|
||||
const digest='sha256:'+crypto.createHash('sha256').update(fs.readFileSync(entry)).digest('hex');return digest===manifest.runtimeSha256?entry:'';
|
||||
}catch{return '';}}
|
||||
function installedRuntime(){return require('./dsh-runtime.cjs').installedRuntime();}
|
||||
class AssistantStore{
|
||||
constructor(dir){
|
||||
try {
|
||||
@@ -27,21 +23,24 @@ class AssistantStore{
|
||||
transaction(work){this.db.exec('BEGIN IMMEDIATE');try{const result=work();this.db.exec('COMMIT');return result;}catch(e){this.db.exec('ROLLBACK');throw e;}}
|
||||
encode(id,value){return this.vault.encode('assistant:'+id,value);}
|
||||
decode(id,value){return this.vault.decode('assistant:'+id,value);}
|
||||
secretSettings(){const row=this.db.prepare('SELECT * FROM config WHERE id=1').get(),saved=row?this.decode('config',row.payload):null;return row?{...saved,runtimePath:saved.runtimePath||this.defaultRuntime,version:row.version}:{version:1,enabled:false,runtimePath:this.defaultRuntime,apiBase:'',model:'',apiKey:'',timeoutSeconds:90,dailyLimit:100,allowActions:false};}
|
||||
secretSettings(){const row=this.db.prepare('SELECT * FROM config WHERE id=1').get(),saved=row?this.decode('config',row.payload):null;return row?{allowCodeRepair:false,repairRepository:'',...saved,runtimePath:saved.engine==='dsh'?saved.runtimePath||this.defaultRuntime:this.defaultRuntime,version:row.version}:{version:1,enabled:false,runtimePath:this.defaultRuntime,apiBase:'https://api.deepseek.com/anthropic',model:'deepseek-flash',apiKey:'',timeoutSeconds:90,dailyLimit:100,allowActions:false,allowCodeRepair:false,repairRepository:''};}
|
||||
settings(){const {apiKey,...settings}=this.secretSettings();return {...settings,hasApiKey:!!apiKey};}
|
||||
saveSettings(input){return this.transaction(()=>{
|
||||
const old=this.secretSettings(),keys=['version','enabled','runtimePath','apiBase','model','apiKey','hasApiKey','timeoutSeconds','dailyLimit','allowActions'];
|
||||
const old=this.secretSettings(),keys=['version','enabled','runtimePath','apiBase','model','apiKey','hasApiKey','timeoutSeconds','dailyLimit','allowActions','allowCodeRepair','repairRepository','engine'];
|
||||
if(!input||Array.isArray(input)||Object.keys(input).some(k=>!keys.includes(k)))throw fail('助手配置格式不正确。');
|
||||
if(input.version!==old.version)throw fail('助手配置已更新,请刷新后重试。',409);
|
||||
for(const key of ['enabled','allowActions'])if(typeof input[key]!=='boolean')throw fail('请选择是否启用助手及恢复操作。');
|
||||
if(!Number.isInteger(input.timeoutSeconds)||input.timeoutSeconds<15||input.timeoutSeconds>180||!Number.isInteger(input.dailyLimit)||input.dailyLimit<1||input.dailyLimit>1000)throw fail('超时须为 15–180 秒,每日调用上限须为 1–1000。');
|
||||
for(const key of ['runtimePath','apiBase','model'])if(typeof input[key]!=='string'||input[key].length>1000||/[\x00-\x1f]/.test(input[key]))throw fail('运行包、模型地址或名称格式不正确。');
|
||||
const runtimePath=input.runtimePath.trim(),apiBase=input.apiBase.trim().replace(/\/+$/,''),model=input.model.trim();
|
||||
if(runtimePath&&(!path.isAbsolute(runtimePath)||!['.exe','.cjs','.mjs','.js'].includes(path.extname(runtimePath).toLowerCase())))throw fail('请选择本机官方 ZCode CLI 的绝对路径(exe/cjs/mjs/js)。');
|
||||
if(runtimePath&&(!path.isAbsolute(runtimePath)||!['.exe','.cjs','.mjs','.js'].includes(path.extname(runtimePath).toLowerCase())))throw fail('请选择本机官方 DSH CLI 的绝对路径(exe/cjs/mjs/js)。');
|
||||
if(apiBase){let u;try{u=new URL(apiBase);}catch{throw fail('模型 API 地址无效。');}if(u.username||u.password||u.search||u.hash||!(u.protocol==='https:'||(u.protocol==='http:'&&['127.0.0.1','localhost','[::1]'].includes(u.hostname))))throw fail('模型 API 请使用 HTTPS,或本机 HTTP 地址。');}
|
||||
if(typeof input.apiKey!=='string'||input.apiKey.length>4096||/[\r\n]/.test(input.apiKey))throw fail('请输入有效模型密钥。');
|
||||
if(!input.apiKey&&old.apiKey&&(apiBase!==old.apiBase||model!==old.model))throw fail('更换模型地址或名称后请重新填写密钥。');
|
||||
const saved={enabled:input.enabled,runtimePath,apiBase,model,apiKey:input.apiKey||old.apiKey,timeoutSeconds:input.timeoutSeconds,dailyLimit:input.dailyLimit,allowActions:input.allowActions};
|
||||
const allowCodeRepair=input.allowCodeRepair===undefined?old.allowCodeRepair:input.allowCodeRepair,repairRepository=input.repairRepository===undefined?old.repairRepository:input.repairRepository;
|
||||
if(typeof allowCodeRepair!=='boolean'||typeof repairRepository!=='string'||repairRepository.length>1000||repairRepository&&(!path.isAbsolute(repairRepository)||/[\x00-\x1f]/.test(repairRepository)))throw fail('代码修复权限或仓库路径无效。');
|
||||
if(allowCodeRepair&&!repairRepository)throw fail('开放代码修复前请选择源码仓库。');
|
||||
const saved={engine:'dsh',allowCodeRepair,repairRepository,enabled:input.enabled,runtimePath,apiBase,model,apiKey:input.apiKey||old.apiKey,timeoutSeconds:input.timeoutSeconds,dailyLimit:input.dailyLimit,allowActions:input.allowActions};
|
||||
if(saved.enabled&&(!runtimePath||!apiBase||!model||!saved.apiKey))throw fail('启用前请填写运行包路径、模型 API 地址、模型名称和密钥。');
|
||||
this.db.prepare('INSERT INTO config VALUES(1,?,?) ON CONFLICT(id) DO UPDATE SET version=excluded.version,payload=excluded.payload').run(old.version+1,this.encode('config',saved));return this.settings();
|
||||
});}
|
||||
|
||||
Reference in new issue
Block a user