50 lines
5.3 KiB
JavaScript
50 lines
5.3 KiB
JavaScript
import {createHash} from 'node:crypto';
|
|
import {defineTool} from '@deepseek-ai/dsh-tools';
|
|
|
|
export const lifecycleNames=['dx_task_catalog','dx_task_preflight','dx_task_manage','dx_task_execute'];
|
|
|
|
export function registerLifecycle(ctx){
|
|
const pending=new Map();
|
|
ctx.on('dx/task-lifecycle-result',value=>pending.get(value.request_id)?.resolve(value));
|
|
ctx.effect(()=>()=>{for(const request of pending.values())request.reject(Error('Task center disconnected'));},'dx-task: lifecycle requests');
|
|
async function request(capability,spec,exec){
|
|
if(process.env.DX_DSH_PARENT_PIPE!=='1')throw Error('This capability requires the task-center desktop.');
|
|
if(!spec||typeof spec!=='object'||JSON.stringify(spec).length>16000)throw Error('Invalid task request');
|
|
const request_id=createHash('sha256').update((exec.agent?.id||'')+':'+exec.callId).digest('hex').slice(0,32);
|
|
if(pending.has(request_id))return pending.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_LIFECYCLE_CANCEL '+JSON.stringify({request_id})+'\n');
|
|
reject(Error('Conversation wait cancelled. Confirmed configuration writes may still finish. If execution started, a safe pause was requested; inspect actual task receipts.'));
|
|
};
|
|
pending.set(request_id,{promise,resolve,reject});
|
|
try{
|
|
exec.signal.throwIfAborted();exec.signal.addEventListener('abort',cancel,{once:true});
|
|
timer=setTimeout(cancel,capability==='execute'&&spec.operation==='start'?86400000:300000);
|
|
process.stdout.write('DX_DSH_LIFECYCLE_REQUEST '+JSON.stringify({request_id,capability,spec})+'\n');
|
|
return await promise;
|
|
}finally{clearTimeout(timer);exec.signal.removeEventListener('abort',cancel);pending.delete(request_id);}
|
|
}
|
|
const output={schema:{type:'object',additionalProperties:true},render:(_args,value)=>[{type:'text',text:JSON.stringify(value)}]};
|
|
const ids={type:'array',required:true,items:{type:'string'},description:'1 to 20 distinct real task IDs obtained from current records, in execution order. Never infer IDs.'};
|
|
const definitions=[
|
|
{name:'dx_task_catalog',capability:'catalog',parameters:{task_id:{type:'string',description:'Optional real task ID to also read its saved rule mappings, Value policy and execution phase.'}},
|
|
description:'List real tasks and their IDs, names, groups, statuses and notes. Optionally read one task in detail, including existing rule mappings. Use to resolve natural-language task references before changing, copying or executing a task. Reads saved records only. Interactive conversations only.'},
|
|
{name:'dx_task_preflight',capability:'preflight',parameters:{task_ids:ids},
|
|
description:'Check the selected saved tasks before execution. Reports project availability, enabled rules/targets, existing execution evidence, unresolved failures and resource blockers. No CAD connection or engineering writes. Returns concrete blockers; does not guarantee a successful CAD run. Interactive conversations only.'},
|
|
{name:'dx_task_manage',capability:'manage',parameters:{
|
|
operation:{type:'string',required:true,enum:['select','copy','update','archive']},task_id:{type:'string',required:true},
|
|
patch:{type:'object',additionalProperties:false,description:'Only for update. Omit fields the user did not request. Rule edits propagate to the existing task batch and are shown in the native preview.',properties:{
|
|
name:{type:'string'},note:{type:'string'},copy_part_number_to_value:{type:'boolean'},
|
|
rules:{type:'array',items:{type:'object',additionalProperties:false,properties:{old_part_number:{type:'string',required:true},
|
|
new_part_number:{type:'string'},target_cell:{type:'string'},custom_value:{type:'string'},custom_value_enabled:{type:'boolean'},sync_nc:{type:'boolean'},enabled:{type:'boolean'}}}}}}},
|
|
description:'Manage an existing real task. select changes the task-center selection. update previews explicit metadata and existing rule changes, then waits for native user confirmation. archive moves only that task to the recycle bin, preserving evidence. copy copies mapping configuration and opens the native selector for NEW engineering scope; never duplicates execution evidence or executes CAD. Returns committed IDs/results or cancellation. Native user confirmation cannot be supplied by model arguments. Interactive conversations only.'},
|
|
{name:'dx_task_execute',capability:'execute',parameters:{operation:{type:'string',required:true,enum:['start','pause']},task_ids:ids},
|
|
description:'Start or continue the existing serial execution queue for an explicit ordered list of real task IDs. Prechecks and native confirmation are required; actual execution can replace and save engineering files using existing backup, interruption and verification gates. Waits for real results including saved, failed and no-match counts. For pause, provide exactly the active run scope; requests safe stopping after the current item and returns pause_requested, not a claim that stopping has completed. No force-stop, no new CAD scripting or hidden retry. Interactive conversations only.'},
|
|
];
|
|
for(const {capability,...definition} of definitions){
|
|
ctx.tools.register(defineTool({...definition,output,execute:(spec,exec)=>request(capability,spec,exec)}));
|
|
}
|
|
}
|