import { Stacklink, type WorkflowDefinitionInput } from '@stacklink/sdk'; const stacklink = new Stacklink({ apiKey: process.env.STACKLINK_API_KEY!, baseUrl: process.env.STACKLINK_API_URL ?? 'https://api.stacklink.in', }); const definition: WorkflowDefinitionInput = { nodes: [ { id: 'start', name: 'Ticket received', type: 'start', config: {}, position: { x: 0, y: 0 } }, { id: 'result', name: 'Return ticket ID', type: 'return', config: { value: { kind: 'input', path: ['ticketId'] } }, position: { x: 340, y: 0 } }, ], edges: [{ id: 'start-result', source: 'start', target: 'result' }], inputSchema: { type: 'object', properties: { ticketId: { type: 'string' } }, required: ['ticketId'] }, }; async function observe(workflowId: string, runId: string) { for (let attempt = 0; attempt < 30; attempt++) { const run = await stacklink.workflows.getRun(workflowId, runId); if (!['queued', 'running', 'retrying'].includes(run.status)) return run; await new Promise(resolve => setTimeout(resolve, 1000)); } return stacklink.workflows.getRun(workflowId, runId); } async function main() { const checked = await stacklink.workflows.validate(definition); if (!checked.valid) throw new Error(JSON.stringify(checked.issues)); const workflow = process.env.STACKLINK_WORKFLOW_ID ? await stacklink.workflows.get(process.env.STACKLINK_WORKFLOW_ID) : await stacklink.workflows.create({ name: 'Docs ยท Ticket intake', source: 'visual', definition, }); // Demo identities. Derive these from your authenticated user in a real app. const session = await stacklink.create({ tenantId: 'demo-company', userId: 'demo-user' }); console.log({ workflowId: workflow.id, sessionId: session.id }); const test = await stacklink.workflows.run(workflow.id, { sessionId: session.id, jobKey: `docs-test-${workflow.revision}`, revision: workflow.revision, purpose: 'test', mode: 'simulation', input: { ticketId: 'ticket-42' }, }); const tested = await observe(workflow.id, test.id); console.log({ testRunId: test.id, status: tested.status, output: tested.output }); if (tested.status !== 'completed') { throw new Error('Test has not completed. Inspect its saved run before publishing.'); } if (!process.argv.includes('--publish')) { console.log('Draft kept. Review it in Workflows before running with --publish.'); return; } await stacklink.workflows.publish(workflow.id, workflow.revision); const run = await stacklink.workflows.run(workflow.id, { sessionId: session.id, jobKey: 'docs-ticket-42', purpose: 'operational', input: { ticketId: 'ticket-42' }, }); const result = await observe(workflow.id, run.id); console.log({ runId: run.id, status: result.status, output: result.output }); // Waiting or still running is not failure. Keep the IDs and inspect later. } main().catch(error => { console.error(error); process.exitCode = 1; });