Khala Phase 1: D1 schema & data access layer #125

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

Parent: #119

Objective

Create D1 migration and type-safe data access functions.

Tasks

1.1 D1 Migration

Create packages/khala/migrations/0001_initial.sql with tables:

  • agents (id TEXT PK, token_hash TEXT, created_at TEXT)
  • threads (id TEXT PK, workflow TEXT, status TEXT, initiator TEXT, result TEXT, created_at, updated_at)
  • messages (id INTEGER PK AUTOINCREMENT, thread_id, role, content, meta, step INTEGER, agent_id, created_at) + index on (thread_id, step)
  • tasks (id TEXT PK, thread_id, role, instruction, status TEXT default open, claim_id, claimed_by, claimed_at, timeout_seconds INTEGER default 300, created_at) + index on (status, created_at)

1.2 Types & Data Access

  • Create packages/khala/src/types.ts — row types for all tables
  • Create packages/khala/src/db.ts — query functions:
    • createThread(db, workflow, initiator)
    • appendMessage(db, threadId, role, content, meta, step, agentId)
    • createTask(db, threadId, role, instruction, timeoutSeconds)
    • claimTask(db, taskId, agentId){ ok, claimId } (atomic UPDATE WHERE status=open)
    • completeTask(db, taskId, claimId) → boolean
    • expireTimedOutTasks(db) → count
    • getOpenTasks(db, limit)
    • getThreadMessages(db, threadId, opts?) (filter by role, since, step, last)

Use ulidx for ID generation. Add to package.json deps.

Rules

  • type not interface, function not class, no ?: (use T | null)
  • See CLAUDE.md for full conventions

References

  • Plan: docs/plans/2026-04-25-khala-mvp.md (Task 1.1, 1.2)
Parent: #119 ## Objective Create D1 migration and type-safe data access functions. ## Tasks ### 1.1 D1 Migration Create `packages/khala/migrations/0001_initial.sql` with tables: - `agents` (id TEXT PK, token_hash TEXT, created_at TEXT) - `threads` (id TEXT PK, workflow TEXT, status TEXT, initiator TEXT, result TEXT, created_at, updated_at) - `messages` (id INTEGER PK AUTOINCREMENT, thread_id, role, content, meta, step INTEGER, agent_id, created_at) + index on (thread_id, step) - `tasks` (id TEXT PK, thread_id, role, instruction, status TEXT default open, claim_id, claimed_by, claimed_at, timeout_seconds INTEGER default 300, created_at) + index on (status, created_at) ### 1.2 Types & Data Access - Create `packages/khala/src/types.ts` — row types for all tables - Create `packages/khala/src/db.ts` — query functions: - `createThread(db, workflow, initiator)` - `appendMessage(db, threadId, role, content, meta, step, agentId)` - `createTask(db, threadId, role, instruction, timeoutSeconds)` - `claimTask(db, taskId, agentId)` → `{ ok, claimId }` (atomic UPDATE WHERE status=open) - `completeTask(db, taskId, claimId)` → boolean - `expireTimedOutTasks(db)` → count - `getOpenTasks(db, limit)` - `getThreadMessages(db, threadId, opts?)` (filter by role, since, step, last) Use `ulidx` for ID generation. Add to package.json deps. ## Rules - `type` not `interface`, `function` not `class`, no `?:` (use `T | null`) - See `CLAUDE.md` for full conventions ## References - Plan: `docs/plans/2026-04-25-khala-mvp.md` (Task 1.1, 1.2)
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#125