Khala Phase 3: Workflow engine — ThreadDO + JSONata moderator #128

Closed
opened 2026-04-25 04:33:43 +00:00 by tuanzi · 0 comments
Owner

Parent: #119

Objective

Core workflow engine using Durable Objects + JSONata moderator.

Tasks

3.1 Workflow Registry

Create packages/khala/src/workflows.ts:

  • CloudRole type: { prompt: string }
  • CloudWorkflowDef type: { name, roles: Record<string, CloudRole>, moderator: string }
  • In-memory Map registry with registerWorkflow() and getWorkflow()

3.2 ThreadDO

Create packages/khala/src/thread-do.ts:

  • Durable Object class (exception to no-class rule — required by CF)
  • Handles:
    • POST /start — init thread, run moderator for first turn, create task
    • POST /response — validate claim_id, append message, run moderator → next task or END
    • GET /messages — query with filters (role, since, step, last)
  • Moderator: build context from messages, evaluate JSONata → { role } or { role: "__end__" }
  • On END: mark thread completed, set result

3.3 Wire into Worker

Modify src/index.ts:

  • Export ThreadDO class
  • Routes:
    • POST /workflows/:name/threads — create thread → DO
    • POST /threads/:id/response — forward to DO
    • GET /threads/:id/messages — query messages
    • GET /threads/:id — thread status

References

Parent: #119 ## Objective Core workflow engine using Durable Objects + JSONata moderator. ## Tasks ### 3.1 Workflow Registry Create `packages/khala/src/workflows.ts`: - `CloudRole` type: `{ prompt: string }` - `CloudWorkflowDef` type: `{ name, roles: Record<string, CloudRole>, moderator: string }` - In-memory Map registry with `registerWorkflow()` and `getWorkflow()` ### 3.2 ThreadDO Create `packages/khala/src/thread-do.ts`: - Durable Object class (exception to no-class rule — required by CF) - Handles: - `POST /start` — init thread, run moderator for first turn, create task - `POST /response` — validate claim_id, append message, run moderator → next task or END - `GET /messages` — query with filters (role, since, step, last) - Moderator: build context from messages, evaluate JSONata → `{ role }` or `{ role: "__end__" }` - On END: mark thread completed, set result ### 3.3 Wire into Worker Modify `src/index.ts`: - Export `ThreadDO` class - Routes: - `POST /workflows/:name/threads` — create thread → DO - `POST /threads/:id/response` — forward to DO - `GET /threads/:id/messages` — query messages - `GET /threads/:id` — thread status ## References - Plan: `docs/plans/2026-04-25-khala-mvp.md` (Task 3.1-3.3) - JSONata docs: https://jsonata.org/
This repo is archived. You cannot comment on issues.
No Label
1 Participants
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: uncaged/nerve#128