feat: create task-center project tasks from native DSH conversations

This commit is contained in:
阿白叔 committed 2026-10-30 14:42:50 +08:00
1 parent e9b77186dc
commit ef40f339cd
23 files changed
+773 -31

No files matched your search

+44 -4
View File
@@ -1,6 +1,6 @@
import {readFile,appendFile} from 'node:fs/promises';
import path from 'node:path';
import {randomUUID} from 'node:crypto';
import {randomUUID,createHash} from 'node:crypto';
import {defineTool} from '@deepseek-ai/dsh-tools';
export const inject=['webServer','connection','tools','workspaceRegistry','sessionController','agents'];
@@ -13,11 +13,13 @@ export async function readContext(){
export async function apply(ctx){
const pending=new Set();
const incidentSnapshots=new Map(),incidentSessions=new Map();
const creations=new Map();
let auditTail=Promise.resolve();
ctx.on('tools/result',(exec,result)=>{
if(exec.name!=='dx_task_snapshot')return;
if(!['dx_task_snapshot','dx_task_create'].includes(exec.name))return;
const row={at:new Date().toISOString(),name:exec.name,call_id:exec.callId,is_error:result.isError,
task_id:result.value?.task_id||'',revision:result.value?.revision||''};
task_id:result.value?.task_id||'',revision:result.value?.revision||'',
...exec.name==='dx_task_create'?{status:result.value?.status,task_ids:result.value?.task_ids||[]}: {}};
auditTail=auditTail.then(()=>appendFile(path.join(process.env.DX_DSH_HOME,'task-tool-audit.jsonl'),JSON.stringify(row)+'\n'))
.catch(()=>{console.error('dx-task: could not persist tool audit');});
});
@@ -53,7 +55,45 @@ export async function apply(ctx){
description:'Read the current DxDesigner task status, failure code and diagnostic summary. No files or CAD are accessed.',
parameters:{},output:{schema:{type:'object',additionalProperties:true},render:(_args,value)=>[{type:'text',text:JSON.stringify(value)}]},
execute:async(_args,exec)=>incidentSnapshots.get(exec.agent?.id) || readContext()}));
ctx.tools.guard(exec=>exec.name==='dx_task_snapshot'?undefined:'Only task snapshot access is enabled.');
ctx.on('dx/task-creation-result',result=>creations.get(result.request_id)?.resolve(result));
ctx.effect(()=>()=>{
for(const request of creations.values())request.reject(Error('Task center disconnected'));
},'dx-task: settle task requests');
ctx.tools.register(defineTool({name:'dx_task_create',
description:'Create real DxDesigner project tasks with replacement rules. Opens the native task preview and project selector; the user confirms the project scope. Returns actual task IDs after creation or cancellation. Does not execute CAD. project_directory only prefills the native dialog and is never scanned automatically.',
parameters:{name:{type:'string',required:true,description:'Short task name'},note:{type:'string'},
project_directory:{type:'string',description:'Optional directory suggested by the user; only displayed for native confirmation'},
copy_part_number_to_value:{type:'boolean',description:'Copy the part number into Value. Default false; set true only when requested.'},
rules:{type:'array',items:{type:'object',additionalProperties:false,properties:{
old_part_number:{type:'string',required:true},new_part_number:{type:'string',required:true},
target_cell:{type:'string',description:'Optional explicit target package/cell, unrelated to Value'},
custom_value:{type:'string',description:'Explicit custom Value text; omit unless requested'},
sync_nc:{type:'boolean',description:'NC handling; default true. Omit unless the user explicitly requests a change.'}}}}},
output:{schema:{type:'object',additionalProperties:true},render:(_args,value)=>[{type:'text',text:JSON.stringify(value)}]},
execute:async(args,exec)=>{
if(process.env.DX_DSH_PARENT_PIPE!=='1')throw Error('Task creation requires the task-center desktop.');
if(!args||Object.keys(args).some(key=>!['name','note','project_directory','rules','copy_part_number_to_value'].includes(key))||
typeof args.name!=='string'||!args.name.trim()||args.name.length>120||JSON.stringify(args).length>16000||
(args.rules!==undefined&&(!Array.isArray(args.rules)||args.rules.length>30)))throw Error('Invalid task creation specification');
const request_id=createHash('sha256').update((exec.agent?.id||'')+':'+exec.callId).digest('hex').slice(0,32);
if(creations.has(request_id))return creations.get(request_id).promise;
let resolve,reject,timer;
const promise=new Promise((yes,no)=>{resolve=yes;reject=no;});
const cancel=()=>{
process.stdout.write('DX_DSH_CREATE_CANCEL '+JSON.stringify({request_id})+'\n');
reject(Error('Task creation cancelled or timed out'));
};
creations.set(request_id,{promise,resolve,reject});
try{
exec.signal.throwIfAborted();
exec.signal.addEventListener('abort',cancel,{once:true});
timer=setTimeout(cancel,300000);
process.stdout.write('DX_DSH_CREATE_REQUEST '+JSON.stringify({request_id,session_id:exec.agent?.id||'',spec:args})+'\n');
return await promise;
}finally{clearTimeout(timer);exec.signal.removeEventListener('abort',cancel);creations.delete(request_id);}
}}));
ctx.tools.guard(exec=>exec.name==='dx_task_snapshot'||(exec.name==='dx_task_create'&&!incidentSnapshots.has(exec.agent?.id))?
undefined:'This tool is not enabled for this session. Automatic incident sessions may only read their snapshot.');
const workspace=await ctx.workspaceRegistry.create(process.env.DX_DSH_WORKSPACE,'DxDesigner 任务中心');
ctx.on('dx/diagnose-request',request=>{
const emit=value=>process.stdout.write('DX_DSH_DIAGNOSIS '+JSON.stringify({request_id:request.request_id,...value})+'\n');