Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 94c719870f | |||
| 5af2d54e0f | |||
| e01c08dacb | |||
| f9d3d38008 | |||
| 9e99e58405 | |||
| 6af3059fb4 | |||
| dfeba9d8fc | |||
| 0da1aabfab | |||
| bb3618cc42 | |||
| 2b21d981dd | |||
| ebfb99bf4c | |||
| 33f9425848 |
@@ -0,0 +1,35 @@
|
||||
# Uncaged Workflow Architecture
|
||||
|
||||
Uncaged Workflow is a monorepo implementing a workflow engine that executes single-file ESM bundles. Each workflow is identified by an XXH64 hash (Crockford Base32); execution state is stored in a content-addressable store (CAS) as immutable Merkle nodes. Agents are pluggable — the same workflow definition runs with Cursor, Hermes, a raw LLM, or a ReAct loop.
|
||||
|
||||
## Core Concepts
|
||||
|
||||
| Card | Description |
|
||||
|------|-------------|
|
||||
| [Bundle](./bundle.md) | A single-file `.esm.js` module with an XXH64 hash identity, stored in `~/.uncaged/workflow/bundles/` |
|
||||
| [Thread](./thread.md) | A single execution instance of a workflow, identified by a ULID, with CAS-linked state nodes |
|
||||
| [CAS](./cas.md) | The content-addressable store that holds all immutable blobs — content, start nodes, and state nodes |
|
||||
| [Registry](./registry.md) | `workflow.yaml` — maps workflow names to current and historical bundle hashes |
|
||||
|
||||
## Execution
|
||||
|
||||
| Card | Description |
|
||||
|------|-------------|
|
||||
| [Engine](./engine.md) | The three-phase loop that drives the workflow `AsyncGenerator` and writes each step to CAS |
|
||||
| [Role](./role.md) | A named actor defined as pure data (`RoleDefinition`) — description, system prompt, and Zod schema |
|
||||
| [Agent Binding](./agent-binding.md) | The runtime binding that connects a role to a concrete agent implementation via `AdapterFn` |
|
||||
| [Reactor](./reactor.md) | The ReAct loop abstraction for LLM function-calling, used by both the extract phase and agent adapters |
|
||||
|
||||
## Tooling
|
||||
|
||||
| Card | Description |
|
||||
|------|-------------|
|
||||
| [CLI](./cli.md) | The `uncaged-workflow` command-line tool for managing workflows, threads, and CAS |
|
||||
| [Dashboard](./dashboard.md) | A private React app for inspecting threads, workflows, and live execution via the gateway |
|
||||
| [Package Map](./package-map.md) | All packages in the monorepo with their layer positions and dependency graph |
|
||||
|
||||
## Authoring
|
||||
|
||||
| Card | Description |
|
||||
|------|-------------|
|
||||
| [Workflow Templates](./workflow-templates.md) | The `solve-issue` and `develop` reference templates and how to author custom workflows |
|
||||
@@ -0,0 +1,104 @@
|
||||
# Agent Binding
|
||||
|
||||
> The runtime connection between a workflow's role definitions and a concrete agent implementation, expressed as an `AdapterBinding` passed to `createWorkflow`.
|
||||
|
||||
## Overview
|
||||
|
||||
Agent binding is how a workflow author specifies which agent executes each role. Roles are pure data (see [Role](./role.md)); the binding supplies the execution strategy. The same `WorkflowDefinition` can be run with different agents by changing the `AdapterBinding` — useful for testing, cost optimization, or environment-specific deployment.
|
||||
|
||||
An `AdapterFn` receives a role's `systemPrompt` and Zod `schema`, and returns a `RoleFn` — a function that takes `ThreadContext` and `WorkflowRuntime` and returns `RoleResult<T>`. The adapter is responsible for producing typed structured output directly; there is no separate extract phase when using adapters.
|
||||
|
||||
## Key Types
|
||||
|
||||
```typescript
|
||||
// The core adapter interface
|
||||
type AdapterFn = <T>(prompt: string, schema: z.ZodType<T>) => RoleFn<T>;
|
||||
|
||||
type RoleFn<T> = (ctx: ThreadContext, runtime: WorkflowRuntime) => Promise<RoleResult<T>>;
|
||||
|
||||
type RoleResult<T> = { meta: T; childThread: string | null };
|
||||
|
||||
// The binding passed to createWorkflow
|
||||
type AdapterBinding = {
|
||||
adapter: AdapterFn;
|
||||
overrides: Partial<Record<string, AdapterFn>> | null;
|
||||
};
|
||||
```
|
||||
|
||||
`overrides` allows per-role adapters — for example, using Cursor for one role and an LLM for another within the same workflow.
|
||||
|
||||
## AgentFn (Legacy / Low-level)
|
||||
|
||||
Below the adapter layer, the original `AgentFn` type still exists for agent implementations that produce raw strings rather than structured output:
|
||||
|
||||
```typescript
|
||||
type AgentFn<Opt = void> = Opt extends void
|
||||
? (ctx: ThreadContext) => Promise<string>
|
||||
: (ctx: ThreadContext, options: Opt) => Promise<string>;
|
||||
```
|
||||
|
||||
The `createAgentAdapter` utility in `@uncaged/workflow-util-agent` wraps an `AgentFn` into an `AdapterFn` by composing it with extraction logic.
|
||||
|
||||
## Concrete Implementations
|
||||
|
||||
| Package | Export | Agent |
|
||||
|---------|--------|-------|
|
||||
| `@uncaged/workflow-agent-cursor` | `createCursorAgent` | Runs `cursor` CLI non-interactively in a workspace directory |
|
||||
| `@uncaged/workflow-agent-hermes` | `createHermesAgent` | Runs `hermes chat` with `--yolo --quiet` (Nerve-style argv) |
|
||||
| `@uncaged/workflow-agent-llm` | `createLlmAdapter` | Direct LLM completion via the OpenAI-compatible chat endpoint |
|
||||
| `@uncaged/workflow-agent-react` | `createReactAdapter` | ReAct loop with file and shell tools (read, write, patch, exec) |
|
||||
|
||||
All four return an `AdapterFn` suitable for use in `AdapterBinding.adapter`.
|
||||
|
||||
## workflow-util-agent
|
||||
|
||||
`@uncaged/workflow-util-agent` provides two helpers shared by adapter implementations:
|
||||
|
||||
- **`buildThreadInput(ctx)`** — constructs the user-message string from thread context (task, previous steps, tool hints). Used by all CLI-based agents.
|
||||
- **`spawnCli(command, args, opts)`** — spawns an external process (e.g., `cursor`, `hermes`) and captures stdout, with optional timeout.
|
||||
- **`createAgentAdapter(agentFn, optionsFn)`** — wraps an `AgentFn<Opt>` into an `AdapterFn`, handling the options extraction step.
|
||||
|
||||
## Cursor Agent
|
||||
|
||||
`createCursorAgent(config)` invokes the `cursor` CLI binary:
|
||||
|
||||
```
|
||||
cursor -p <fullPrompt> --model <model> --workspace <path> --output-format text --trust --force
|
||||
```
|
||||
|
||||
The workspace path is taken from `config.workspace` or extracted from the thread context via `runtime.extract`.
|
||||
|
||||
## Hermes Agent
|
||||
|
||||
`createHermesAgent(config)` invokes `hermes chat`:
|
||||
|
||||
```
|
||||
hermes chat -q <fullPrompt> --yolo --max-turns 90 --quiet [--model <model>]
|
||||
```
|
||||
|
||||
## LLM Adapter
|
||||
|
||||
`createLlmAdapter(provider)` calls the OpenAI-compatible chat completions endpoint directly. It builds a two-message conversation (system + user) from the role's `systemPrompt` and `buildThreadInput` output, then extracts structured output from the response.
|
||||
|
||||
## React Adapter
|
||||
|
||||
`createReactAdapter(config)` creates a ReAct loop agent with four default tools: `read_file`, `write_file`, `patch_file`, and `shell_exec`. The loop continues until the agent calls the structured extraction tool or until `maxRounds` is exceeded.
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-protocol` | `src/types.ts` | `AdapterFn`, `AdapterBinding`, `RoleFn`, `RoleResult`, `AgentFn` |
|
||||
| `@uncaged/workflow-runtime` | `src/create-workflow.ts` | `createWorkflow` — dispatches `adapterForRole` each iteration |
|
||||
| `@uncaged/workflow-util-agent` | `src/build-agent-prompt.ts` | `buildThreadInput`, `buildAgentPrompt` |
|
||||
| `@uncaged/workflow-util-agent` | `src/spawn-cli.ts` | `spawnCli` — subprocess runner with timeout |
|
||||
| `@uncaged/workflow-util-agent` | `src/create-agent-adapter.ts` | `createAgentAdapter` — wraps `AgentFn` into `AdapterFn` |
|
||||
| `@uncaged/workflow-agent-cursor` | `src/index.ts` | `createCursorAgent` |
|
||||
| `@uncaged/workflow-agent-hermes` | `src/index.ts` | `createHermesAgent` |
|
||||
| `@uncaged/workflow-agent-llm` | `src/create-llm-adapter.ts` | `createLlmAdapter` |
|
||||
| `@uncaged/workflow-agent-react` | `src/create-react-adapter.ts` | `createReactAdapter` |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Role](./role.md) — the pure data that the binding executes
|
||||
- [Engine](./engine.md) — the loop that invokes the bound adapter each step
|
||||
@@ -0,0 +1,83 @@
|
||||
# Bundle
|
||||
|
||||
> A self-contained single-file ESM module (`.esm.js`) that implements one workflow, identified by its XXH64 hash encoded as 13-char Crockford Base32.
|
||||
|
||||
## Overview
|
||||
|
||||
A bundle is the physical unit of workflow distribution. Workflow authors build their TypeScript source into a single ESM file using `bun build` with `@uncaged/*` packages as externals. The resulting `.esm.js` is the artifact that gets registered and executed.
|
||||
|
||||
Every bundle is immutable and content-addressed: its identity is the XXH64 hash of its bytes, encoded as 13 characters of Crockford Base32 (e.g., `3TNKQRJ7BM4XH`). Registering a bundle with a new version simply adds a new hash entry; old hashes stay in the registry history and remain valid.
|
||||
|
||||
Bundles are stored on disk at `~/.uncaged/workflow/bundles/<hash>/` after registration. The `cas/` and `threads.json` for that bundle's execution state live under the same directory.
|
||||
|
||||
## Exports
|
||||
|
||||
Every valid bundle must export exactly two named exports — no default export is permitted:
|
||||
|
||||
| Export | Type | Description |
|
||||
|--------|------|-------------|
|
||||
| `run` | `WorkflowFn` | The `AsyncGenerator` that drives the execution loop |
|
||||
| `descriptor` | `WorkflowDescriptor` | Serializable metadata: description, roles, and routing graph |
|
||||
|
||||
```typescript
|
||||
// Minimal bundle shape
|
||||
export const run: WorkflowFn = createWorkflow(def, binding);
|
||||
export const descriptor: WorkflowDescriptor = buildDescriptor(def);
|
||||
```
|
||||
|
||||
The validator in `@uncaged/workflow-register` enforces this contract before a bundle can be registered — see `extractBundleExports`.
|
||||
|
||||
## Hash Algorithm
|
||||
|
||||
The bundle hash is computed with **XXH64** (seed 0) over the raw bytes of the `.esm.js` file, then encoded as 13-char Crockford Base32 using `encodeUint64AsCrockford`:
|
||||
|
||||
```typescript
|
||||
// packages/workflow-cas/src/hash.ts
|
||||
export function hashWorkflowBundleBytes(data: Uint8Array): string {
|
||||
const buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
||||
const digest = XXH.h64(0).update(buf).digest();
|
||||
return encodeUint64AsCrockford(digestToUint64(digest));
|
||||
}
|
||||
```
|
||||
|
||||
The same algorithm hashes CAS blob content (`hashString`), so all IDs in the system are consistent Crockford Base32 strings.
|
||||
|
||||
## Build Process
|
||||
|
||||
Bundles are not distributed from the monorepo directly. The typical flow is:
|
||||
|
||||
1. Create a separate workspace (e.g., `my-workflows/`) with `@uncaged/workflow-runtime` as a dependency.
|
||||
2. Write a TypeScript workflow module that imports `createWorkflow` from `@uncaged/workflow-runtime`.
|
||||
3. Run `bun build --entrypoints src/my-workflow.ts --outfile dist/my-workflow.esm.js --format esm --external '@uncaged/*'`.
|
||||
4. Register with `uncaged-workflow workflow add <name> dist/my-workflow.esm.js`.
|
||||
|
||||
## Storage Layout
|
||||
|
||||
```
|
||||
~/.uncaged/workflow/
|
||||
workflow.yaml # registry (name → hash mapping)
|
||||
bundles/
|
||||
<hash>/
|
||||
threads.json # active thread index
|
||||
history/
|
||||
YYYY-MM-DD.jsonl # completed thread records
|
||||
cas/
|
||||
<hash>.txt # CAS blobs (all bundles share one global CAS)
|
||||
```
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-cas` | `src/hash.ts` | `hashWorkflowBundleBytes` and `hashString` — XXH64 + Crockford encoding |
|
||||
| `@uncaged/workflow-register` | `src/bundle/extract-bundle-exports.ts` | Loads a `.esm.js` bundle and validates `run` + `descriptor` |
|
||||
| `@uncaged/workflow-register` | `src/bundle/bundle-validator.ts` | Schema validation of bundle exports |
|
||||
| `@uncaged/workflow-runtime` | `src/create-workflow.ts` | `createWorkflow` — the primary bundle authoring function |
|
||||
| `@uncaged/workflow-util` | `src/base32.ts` | `encodeUint64AsCrockford` — Crockford Base32 encoding |
|
||||
| `@uncaged/workflow-util` | `src/storage-root.ts` | `getDefaultWorkflowStorageRoot` → `~/.uncaged/workflow` |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Registry](./registry.md) — how bundles are registered and named in `workflow.yaml`
|
||||
- [Thread](./thread.md) — how a bundle's `run` export is executed as a thread
|
||||
- [Engine](./engine.md) — the executor that drives the bundle's `AsyncGenerator`
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
# CAS (Content-Addressable Storage)
|
||||
|
||||
> An append-only store where every blob is identified by its XXH64 hash, used to persist all workflow thread state as immutable Merkle nodes.
|
||||
|
||||
## Overview
|
||||
|
||||
CAS is the persistence substrate for the entire workflow engine. Rather than mutating a database row, every piece of state — agent output, role metadata, thread start parameters — is serialized as a YAML blob and stored under its hash. Because content determines identity, the same content always maps to the same hash, and writes are idempotent.
|
||||
|
||||
The `CasStore` interface is intentionally simple: `put`, `get`, `delete`, `list`. The default filesystem implementation stores each blob as `<hash>.txt` under `~/.uncaged/workflow/cas/`. Writes use an atomic rename-from-tmp pattern to prevent partial writes.
|
||||
|
||||
## Hash Algorithm
|
||||
|
||||
All hashes in the system are **XXH64** (seed 0) over UTF-8 content, encoded as 13-char Crockford Base32. This applies to both CAS blob hashes and bundle file hashes. The encoding function `encodeUint64AsCrockford` lives in `@uncaged/workflow-util`.
|
||||
|
||||
## Node Types
|
||||
|
||||
The CAS holds three types of YAML nodes, all sharing the `{ type, payload, refs }` envelope:
|
||||
|
||||
### `content` node
|
||||
Stores the raw text output of an agent or the initial prompt. `refs` lists any artifact hashes the content references.
|
||||
|
||||
```yaml
|
||||
type: content
|
||||
payload: "The implementation is complete. Changed files: src/foo.ts"
|
||||
refs:
|
||||
- 3TNKQRJ7BM4XH # optional artifact refs
|
||||
```
|
||||
|
||||
### `start` node
|
||||
Written once when a thread begins. Anchors the thread to a specific workflow name, bundle hash, and depth level.
|
||||
|
||||
```yaml
|
||||
type: start
|
||||
payload:
|
||||
name: solve-issue
|
||||
hash: 3TNKQRJ7BM4XH
|
||||
depth: 0
|
||||
parentState: null
|
||||
refs:
|
||||
- <promptHash>
|
||||
```
|
||||
|
||||
### `state` node
|
||||
Written once per completed role step. Points back to the `start` node, the role's content node, and maintains an ancestor skip-list for traversal.
|
||||
|
||||
```yaml
|
||||
type: state
|
||||
payload:
|
||||
role: coder
|
||||
meta: { status: "done", completedPhase: "..." }
|
||||
start: <startHash>
|
||||
content: <contentHash>
|
||||
ancestors: [<prev_state>, ...]
|
||||
compact: null
|
||||
timestamp: 1716000000000
|
||||
childThread: null
|
||||
refs:
|
||||
- <contentHash>
|
||||
- <startHash>
|
||||
- <ancestor hashes>
|
||||
```
|
||||
|
||||
## Merkle Structure
|
||||
|
||||
The `ancestors` array in each `StateNode` implements a **skip-list** capped at 11 entries (1 direct parent + up to 10 skip-list ancestors). This allows `O(log n)` traversal of the chain without loading every node, while keeping each blob self-contained.
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
S[StartNode] --> C1[content₁]
|
||||
N1[StateNode₁] --> S
|
||||
N1 --> C1
|
||||
N2[StateNode₂] --> N1
|
||||
N2 --> S
|
||||
N2 --> C2[content₂]
|
||||
END[StateNode __end__] --> N2
|
||||
END --> S
|
||||
```
|
||||
|
||||
## CasStore Interface
|
||||
|
||||
```typescript
|
||||
type CasStore = {
|
||||
put(content: string): Promise<string>; // returns hash
|
||||
get(hash: string): Promise<string | null>;
|
||||
delete(hash: string): Promise<void>;
|
||||
list(): Promise<string[]>;
|
||||
};
|
||||
```
|
||||
|
||||
`put` normalizes raw strings into `content` Merkle nodes before hashing; pre-serialized RFC v3 nodes pass through unchanged.
|
||||
|
||||
## Garbage Collection
|
||||
|
||||
`cas gc` performs a mark-and-sweep over all CAS blobs. It seeds the reachable set from `head` and `start` hashes in every `threads.json` and `history/*.jsonl`, then traverses `refs` edges transitively. Unreachable blobs are deleted. The result reports `scannedThreads`, `activeRefs`, and `deletedEntries`.
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-protocol` | `src/types.ts` | `CasStore` interface definition |
|
||||
| `@uncaged/workflow-protocol` | `src/cas-types.ts` | `StartNode`, `StateNode`, `ContentMerkleNode` types |
|
||||
| `@uncaged/workflow-cas` | `src/cas.ts` | `createCasStore` — filesystem implementation |
|
||||
| `@uncaged/workflow-cas` | `src/hash.ts` | `hashString`, `hashWorkflowBundleBytes` — XXH64 + Crockford |
|
||||
| `@uncaged/workflow-cas` | `src/nodes.ts` | `putStartNode`, `putStateNode`, `putContentNodeWithRefs`, `parseCasThreadNode` |
|
||||
| `@uncaged/workflow-cas` | `src/merkle.ts` | `parseMerkleNode`, `serializeMerkleNode`, `getContentMerklePayload` |
|
||||
| `@uncaged/workflow-cas` | `src/reachable.ts` | Reachability traversal for GC |
|
||||
| `@uncaged/workflow-execute` | `src/engine/gc.ts` | GC orchestration |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Thread](./thread.md) — how thread execution state maps to CAS nodes
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
# CLI
|
||||
|
||||
> `uncaged-workflow` — the command-line tool for registering bundles, running threads, inspecting CAS, and connecting to the gateway.
|
||||
|
||||
## Overview
|
||||
|
||||
The CLI (`@uncaged/cli-workflow`) is the primary human interface to the workflow engine. It is a multi-level command dispatcher: top-level command groups (`workflow`, `thread`, `cas`, `init`, `setup`) each have a set of subcommands. Two shortcuts (`run`, `live`) alias frequently-used subcommands.
|
||||
|
||||
The storage root defaults to `~/.uncaged/workflow` and can be overridden with `WORKFLOW_STORAGE_ROOT` or `UNCAGED_WORKFLOW_STORAGE_ROOT` environment variables.
|
||||
|
||||
## Command Reference
|
||||
|
||||
### Workflow Registry (`workflow`)
|
||||
|
||||
| Subcommand | Args | Description |
|
||||
|-----------|------|-------------|
|
||||
| `workflow add` | `<name> <file.esm.js> [--types <path>]` | Register a workflow bundle in the registry |
|
||||
| `workflow list` | | List all registered workflows |
|
||||
| `workflow show` | `<name>` | Show bundle hash, timestamp, and descriptor |
|
||||
| `workflow rm` | `<name>` | Remove a workflow from the registry |
|
||||
| `workflow history` | `<name>` | Show version history for a workflow |
|
||||
| `workflow rollback` | `<name> [hash]` | Roll back to a previous version |
|
||||
|
||||
### Thread Execution (`thread`)
|
||||
|
||||
| Subcommand | Args | Description |
|
||||
|-----------|------|-------------|
|
||||
| `thread run` | `<name> [--prompt <text>]` | Start a new thread for a workflow; prints thread ID |
|
||||
| `thread list` | `[name]` | List threads, optionally filtered by workflow name |
|
||||
| `thread show` | `<id>` | Show thread steps and state from CAS |
|
||||
| `thread rm` | `<id>` | Remove a thread (from index and history) |
|
||||
| `thread fork` | `<thread-id> [--from-role <role>]` | Fork from an existing thread |
|
||||
| `thread ps` | | List running (active) threads |
|
||||
| `thread kill` | `<thread-id>` | Send kill signal to a running thread |
|
||||
| `thread live` | `<thread-id> \| --latest [--debug] [--role <name>]` | Attach and stream output live |
|
||||
| `thread pause` | `<thread-id>` | Pause a running thread |
|
||||
| `thread resume` | `<thread-id>` | Resume a paused thread |
|
||||
|
||||
### CAS Inspection (`cas`)
|
||||
|
||||
| Subcommand | Args | Description |
|
||||
|-----------|------|-------------|
|
||||
| `cas get` | `<hash>` | Print a CAS blob by hash |
|
||||
| `cas put` | `<content>` | Store content in CAS, print hash |
|
||||
| `cas list` | | List all hashes in CAS |
|
||||
| `cas rm` | `<hash>` | Remove a CAS entry |
|
||||
| `cas gc` | | Garbage-collect unreferenced entries |
|
||||
|
||||
### Other Commands
|
||||
|
||||
| Command | Args | Description |
|
||||
|---------|------|-------------|
|
||||
| `run <name> [...]` | | Shortcut for `thread run` |
|
||||
| `live <id> [...]` | | Shortcut for `thread live` |
|
||||
| `init` | | Scaffold a workflow workspace |
|
||||
| `setup` | | Configure LLM providers in `workflow.yaml` |
|
||||
| `connect [--name NAME] [--gateway URL]` | | Connect to gateway via WebSocket |
|
||||
| `skill [topic]` | | Print agent-consumable docs (`cli`, `develop`, `author`) |
|
||||
|
||||
## Common Usage Examples
|
||||
|
||||
```bash
|
||||
# Register a bundle
|
||||
uncaged-workflow workflow add solve-issue dist/solve-issue.esm.js
|
||||
|
||||
# Run a workflow (prints thread ID)
|
||||
uncaged-workflow run solve-issue --prompt "Fix the login bug in auth.ts"
|
||||
|
||||
# Watch live output
|
||||
uncaged-workflow live <thread-id>
|
||||
|
||||
# Inspect a CAS blob
|
||||
uncaged-workflow cas get 3TNKQRJ7BM4XH
|
||||
|
||||
# Show all running threads
|
||||
uncaged-workflow thread ps
|
||||
|
||||
# Garbage-collect
|
||||
uncaged-workflow cas gc
|
||||
|
||||
# Roll back to previous version
|
||||
uncaged-workflow workflow rollback solve-issue
|
||||
```
|
||||
|
||||
## Environment Variables
|
||||
|
||||
| Variable | Description |
|
||||
|----------|-------------|
|
||||
| `WORKFLOW_STORAGE_ROOT` | Override storage directory (default: `~/.uncaged/workflow`) |
|
||||
| `UNCAGED_WORKFLOW_STORAGE_ROOT` | Internal override; takes priority over `WORKFLOW_STORAGE_ROOT` |
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/cli-workflow` | `src/cli-dispatch.ts` | Top-level command router (`COMMAND_TABLE`) |
|
||||
| `@uncaged/cli-workflow` | `src/cli-usage.ts` | Usage text formatting |
|
||||
| `@uncaged/cli-workflow` | `src/commands/workflow/dispatch.ts` | `WORKFLOW_SUBCOMMAND_TABLE` |
|
||||
| `@uncaged/cli-workflow` | `src/commands/thread/dispatch.ts` | `THREAD_SUBCOMMAND_TABLE` |
|
||||
| `@uncaged/cli-workflow` | `src/commands/cas/dispatch.ts` | `CAS_SUBCOMMAND_TABLE` |
|
||||
| `@uncaged/cli-workflow` | `src/cli.ts` | CLI entry point |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Bundle](./bundle.md) — what `workflow add` registers
|
||||
- [Thread](./thread.md) — what `thread run` creates
|
||||
- [Registry](./registry.md) — the `workflow.yaml` that `workflow` commands manage
|
||||
@@ -0,0 +1,74 @@
|
||||
# Dashboard
|
||||
|
||||
> A private React single-page application for browsing workflows, inspecting thread execution records, and triggering runs via a connected gateway.
|
||||
|
||||
## Overview
|
||||
|
||||
The dashboard (`workflow-dashboard`) is a read-mostly web UI that surfaces thread history and workflow metadata. It is a private package (not published to npm) and is deployed separately from the CLI. It communicates with one or more remote workflow engine instances through the `workflow-gateway` WebSocket gateway, which proxies API calls back to each connected CLI client.
|
||||
|
||||
The dashboard is not required to use the workflow engine — it is an optional observability layer on top of the same data that the CLI exposes.
|
||||
|
||||
## Tech Stack
|
||||
|
||||
| Concern | Choice |
|
||||
|---------|--------|
|
||||
| Framework | React (functional components, hooks) |
|
||||
| Build | Vite |
|
||||
| Styling | CSS variables via Tailwind-compatible utility classes |
|
||||
| Charts/graphs | ReactFlow (workflow graph visualization) |
|
||||
| HTTP | Native `fetch` with Bearer token auth |
|
||||
| Transport | REST over HTTP (proxied through the gateway) |
|
||||
|
||||
## Data Sources
|
||||
|
||||
The dashboard consumes four REST endpoints per connected client (proxied by the gateway):
|
||||
|
||||
| Endpoint | Data |
|
||||
|----------|------|
|
||||
| `GET /workflows` | List of registered workflows with current hash and timestamp |
|
||||
| `GET /workflows/:name` | Full workflow detail including `WorkflowDescriptor` and version history |
|
||||
| `GET /threads` | All threads (active + completed) with summary fields |
|
||||
| `GET /threads/:id` | Thread records: `ThreadStartRecord`, `RoleRecord[]`, `WorkflowResultRecord` |
|
||||
|
||||
The gateway multiplexes multiple CLI clients; the sidebar allows switching between them.
|
||||
|
||||
## Views
|
||||
|
||||
| View | Description |
|
||||
|------|-------------|
|
||||
| **Workflows** | Lists all registered workflows; clicking shows hash, descriptor, role graph, and version history |
|
||||
| **Threads** | Lists all threads; clicking shows the full step-by-step execution record with role metadata |
|
||||
| **Run dialog** | Form to start a new thread by picking a workflow and entering a prompt |
|
||||
|
||||
### Workflow Graph
|
||||
|
||||
Each workflow's `WorkflowDescriptor.graph` is rendered as an interactive ReactFlow diagram. Nodes represent roles (plus `__start__` and `__end__` terminals); edges represent moderator transitions labeled with condition names.
|
||||
|
||||
## Authentication
|
||||
|
||||
A Bearer token (stored in `localStorage` under `workflow-api-key`) is sent with every API request. The login page prompts for this key on first load. The gateway validates the token before proxying requests to connected clients.
|
||||
|
||||
## Gateway Connection
|
||||
|
||||
`uncaged-workflow connect [--name NAME] [--gateway URL]` registers the local workflow engine as a named client with the gateway over a WebSocket. The gateway then forwards REST API calls from the dashboard to the connected CLI process. The dashboard calls `GET /api/gateway/endpoints` to discover connected clients.
|
||||
|
||||
## Private App Status
|
||||
|
||||
`workflow-dashboard` has `"private": true` in its `package.json` and is excluded from the changeset versioning pipeline. It is developed alongside the engine packages but distributed separately (e.g., as a static build hosted alongside the gateway server).
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `workflow-dashboard` | `src/app.tsx` | Root component — routing, auth state, view switching |
|
||||
| `workflow-dashboard` | `src/api.ts` | All API functions + endpoint types (`ThreadRecord`, `WorkflowDetail`, etc.) |
|
||||
| `workflow-dashboard` | `src/components/thread-detail.tsx` | Thread step viewer |
|
||||
| `workflow-dashboard` | `src/components/workflow-graph/workflow-graph.tsx` | ReactFlow graph of workflow roles and transitions |
|
||||
| `workflow-dashboard` | `src/components/sidebar.tsx` | Client selector and view navigation |
|
||||
| `@uncaged/workflow-gateway` | `src/index.ts` | Gateway server entry point |
|
||||
| `@uncaged/workflow-gateway` | `src/ws-protocol.ts` | WebSocket message protocol between CLI and gateway |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Thread](./thread.md) — the execution records the dashboard displays
|
||||
- [Engine](./engine.md) — the process that produces those records
|
||||
@@ -0,0 +1,110 @@
|
||||
# Engine
|
||||
|
||||
> The execution loop that drives a workflow bundle's `AsyncGenerator`, persisting each yielded `RoleOutput` as a CAS `StateNode` and managing thread lifecycle.
|
||||
|
||||
## Overview
|
||||
|
||||
The engine (`executeThread`) takes a `WorkflowFn` and runs it to completion. It is responsible for three concerns: persisting each role output to CAS, updating the active-thread index after every step, and terminating the thread cleanly when the generator finishes, is aborted, or is killed by the supervisor.
|
||||
|
||||
The engine does not interact with LLMs directly — that responsibility belongs to the workflow bundle's `run` function and its bound agent adapters. The engine only observes `RoleOutput` values yielded by the generator.
|
||||
|
||||
## Execution Flow
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[executeThread] --> B[putStartNode → CAS]
|
||||
B --> C[publishHead → threads.json]
|
||||
C --> D{generator.next}
|
||||
D -- done --> E[finalizeThread]
|
||||
D -- yield RoleOutput --> F[appendStateForStep → CAS]
|
||||
F --> G[publishHead → threads.json]
|
||||
G --> H{supervisorInterval?}
|
||||
H -- kill --> E
|
||||
H -- continue --> I{awaitAfterEachYield}
|
||||
I --> D
|
||||
D -- AbortSignal --> J[finalizeAbortedThread]
|
||||
E --> K[removeThreadEntry]
|
||||
K --> L[appendThreadHistoryEntry]
|
||||
```
|
||||
|
||||
## Role Loop (inside the bundle's `createWorkflow`)
|
||||
|
||||
The `WorkflowFn` produced by `createWorkflow` runs its own loop — one iteration per role step:
|
||||
|
||||
1. **Moderator**: calls `pickNext(ctx)` (derived from the `ModeratorTable`) → returns a role name or `END`.
|
||||
2. **Adapter**: calls the bound `AdapterFn` with the role's `systemPrompt` and Zod schema → returns `RoleFn` → executes → returns `RoleResult<T>`.
|
||||
3. **Persist**: calls `putContentNodeWithRefs` to store the role output in CAS, constructs a `RoleStep`, and `yield`s a `RoleOutput` to the engine.
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant E as Engine
|
||||
participant W as WorkflowFn (bundle)
|
||||
participant M as Moderator
|
||||
participant A as AdapterFn
|
||||
participant C as CAS
|
||||
|
||||
E->>W: generator.next()
|
||||
W->>M: pickNext(ctx) → roleName
|
||||
W->>A: adapter(systemPrompt, schema)(ctx, runtime)
|
||||
A-->>W: RoleResult { meta, childThread }
|
||||
W->>C: putContentNodeWithRefs(JSON.stringify(meta))
|
||||
W-->>E: yield RoleOutput
|
||||
E->>C: putStateNode(StateNodePayload)
|
||||
E->>E: publishHead(threads.json)
|
||||
```
|
||||
|
||||
## Key Types
|
||||
|
||||
```typescript
|
||||
// Engine input
|
||||
type ExecuteThreadOptions = {
|
||||
depth: number;
|
||||
parentStateHash: string | null;
|
||||
signal: AbortSignal;
|
||||
awaitAfterEachYield: () => Promise<void>; // used for pause/resume gate
|
||||
forkContinuation: ForkContinuationOptions | null;
|
||||
prefilledDiskSteps: PrefilledDiskStep[] | null;
|
||||
replayTimestamps: readonly number[] | null;
|
||||
storageRoot: string;
|
||||
};
|
||||
|
||||
// Engine output
|
||||
type WorkflowResult = {
|
||||
returnCode: number;
|
||||
summary: string;
|
||||
rootHash: string; // hash of the __end__ StateNode
|
||||
};
|
||||
```
|
||||
|
||||
## Pause Gate
|
||||
|
||||
`awaitAfterEachYield` is a function injected by the worker/runner that can block the loop between steps. The `ThreadPauseGate` in `thread-pause-gate.ts` provides `pause()` / `resume()` operations that control this gate. When paused, the loop suspends after writing the current step but before requesting the next one.
|
||||
|
||||
## Supervisor
|
||||
|
||||
If `workflowConfig.supervisorInterval > 0`, the engine runs a supervisor check after every `supervisorInterval` steps. The supervisor calls an LLM with a summary of recent steps and returns `"continue"` or `"kill"`. A `"kill"` decision finalizes the thread immediately with `returnCode: 1` and a summary string.
|
||||
|
||||
## Summarizer
|
||||
|
||||
On normal completion (generator returns), the engine calls `createSummarizer` to produce a single LLM-generated summary string from recent step content. This summary replaces the bundle's raw `WorkflowCompletion.summary` in the final history record.
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-execute` | `src/engine/engine.ts` | `executeThread` — main engine entry point |
|
||||
| `@uncaged/workflow-execute` | `src/engine/types.ts` | `ExecuteThreadOptions`, `ExecuteThreadIo`, `ChainState`, `ThreadPauseGate` |
|
||||
| `@uncaged/workflow-execute` | `src/engine/threads-index.ts` | `threads.json` persistence, history append |
|
||||
| `@uncaged/workflow-execute` | `src/engine/supervisor.ts` | Supervisor LLM check (`"continue"` / `"kill"`) |
|
||||
| `@uncaged/workflow-execute` | `src/engine/summarizer.ts` | Post-completion LLM summary |
|
||||
| `@uncaged/workflow-execute` | `src/engine/thread-pause-gate.ts` | Pause/resume gate |
|
||||
| `@uncaged/workflow-execute` | `src/engine/worker.ts` | Worker-process entry that spawns `executeThread` in a subprocess |
|
||||
| `@uncaged/workflow-runtime` | `src/create-workflow.ts` | `createWorkflow` — the role loop inside the bundle |
|
||||
| `@uncaged/workflow-protocol` | `src/types.ts` | `WorkflowFn`, `RoleOutput`, `WorkflowCompletion`, `AdvanceOutcome` |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Role](./role.md) — what the moderator selects each iteration
|
||||
- [Agent Binding](./agent-binding.md) — what executes a role and returns its output
|
||||
- [Reactor](./reactor.md) — used internally for the extract and supervisor LLM calls
|
||||
- [Thread](./thread.md) — the CAS-persisted result of running the engine
|
||||
@@ -0,0 +1,129 @@
|
||||
# Package Map
|
||||
|
||||
> All packages in the monorepo with their responsibilities, dependency layers, and publication status.
|
||||
|
||||
## Overview
|
||||
|
||||
The monorepo is organized as a strict dependency DAG. Each layer may only depend on layers below it. The execution stack flows from the shared protocol types at the bottom up to the CLI at the top. Agent packages and template packages are leaf nodes that depend on the runtime layer but are not depended upon by the core stack.
|
||||
|
||||
## Package List
|
||||
|
||||
| Package | Description |
|
||||
|---------|-------------|
|
||||
| `@uncaged/workflow-protocol` | Shared types (`ThreadContext`, `RoleDefinition`, `CasStore`, `Result`, etc.) and constants (`START`, `END`) |
|
||||
| `@uncaged/workflow-runtime` | `createWorkflow`, type re-exports; primary dependency for bundle authors |
|
||||
| `@uncaged/workflow-util` | Utilities: Crockford Base32, ULID, structured logger, storage paths |
|
||||
| `@uncaged/workflow-reactor` | `createThreadReactor` (ReAct loop), `createLlmFn` (OpenAI-compatible LLM caller) |
|
||||
| `@uncaged/workflow-cas` | `createCasStore` (filesystem CAS), XXH64 hashing, Merkle node serialization |
|
||||
| `@uncaged/workflow-register` | Bundle validation, `workflow.yaml` registry read/write, model resolution |
|
||||
| `@uncaged/workflow-execute` | Engine (`executeThread`), extract phase, fork, GC, `workflowAsAgent` |
|
||||
| `@uncaged/cli-workflow` | `uncaged-workflow` CLI — command dispatcher for all user-facing operations |
|
||||
| `@uncaged/workflow-agent-cursor` | Adapter that runs the `cursor` CLI non-interactively in a workspace |
|
||||
| `@uncaged/workflow-agent-hermes` | Adapter that runs `hermes chat` (Nerve-style CLI agent) |
|
||||
| `@uncaged/workflow-agent-llm` | Adapter for direct LLM chat completions |
|
||||
| `@uncaged/workflow-agent-react` | Adapter with ReAct loop and file/shell tools |
|
||||
| `@uncaged/workflow-util-agent` | Shared agent utilities: `buildThreadInput`, `spawnCli`, `createAgentAdapter` |
|
||||
| `@uncaged/workflow-template-develop` | `develop` workflow template (planner → coder → reviewer → tester → committer) |
|
||||
| `@uncaged/workflow-template-solve-issue` | `solve-issue` workflow template (preparer → developer → submitter) |
|
||||
| `@uncaged/workflow-gateway` | WebSocket gateway for remote CLI-to-dashboard communication |
|
||||
| `workflow-dashboard` | React dashboard (private, unpublished) — thread/workflow viewer |
|
||||
|
||||
## Dependency Layer Diagram
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
subgraph Layer 0 — Protocol
|
||||
P[workflow-protocol]
|
||||
end
|
||||
|
||||
subgraph Layer 1 — Foundations
|
||||
RT[workflow-runtime]
|
||||
UT[workflow-util]
|
||||
RX[workflow-reactor]
|
||||
end
|
||||
|
||||
subgraph Layer 2 — Storage & Register
|
||||
CAS[workflow-cas]
|
||||
REG[workflow-register]
|
||||
end
|
||||
|
||||
subgraph Layer 3 — Execute
|
||||
EX[workflow-execute]
|
||||
end
|
||||
|
||||
subgraph Layer 4 — CLI
|
||||
CLI[cli-workflow]
|
||||
end
|
||||
|
||||
subgraph Agents (leaf)
|
||||
AGC[workflow-agent-cursor]
|
||||
AGH[workflow-agent-hermes]
|
||||
AGL[workflow-agent-llm]
|
||||
AGR[workflow-agent-react]
|
||||
UA[workflow-util-agent]
|
||||
end
|
||||
|
||||
subgraph Templates (leaf)
|
||||
TD[workflow-template-develop]
|
||||
TS[workflow-template-solve-issue]
|
||||
end
|
||||
|
||||
subgraph Dashboard
|
||||
GW[workflow-gateway]
|
||||
DB[workflow-dashboard]
|
||||
end
|
||||
|
||||
RT --> P
|
||||
UT --> P
|
||||
RX --> P
|
||||
CAS --> P
|
||||
REG --> P
|
||||
REG --> UT
|
||||
EX --> RT
|
||||
EX --> UT
|
||||
EX --> CAS
|
||||
EX --> REG
|
||||
EX --> RX
|
||||
CLI --> EX
|
||||
CLI --> UT
|
||||
CLI --> REG
|
||||
AGC --> RT
|
||||
AGC --> UT
|
||||
AGC --> UA
|
||||
AGH --> RT
|
||||
AGH --> UA
|
||||
AGL --> RT
|
||||
AGR --> RT
|
||||
AGR --> RX
|
||||
UA --> RT
|
||||
TD --> RT
|
||||
TS --> RT
|
||||
DB --> GW
|
||||
```
|
||||
|
||||
## Published vs. Private
|
||||
|
||||
All `@uncaged/*` packages are published to **npmjs.org** under a fixed versioning scheme (all packages share the same version number via `@changesets/cli` in fixed mode).
|
||||
|
||||
| Status | Packages |
|
||||
|--------|---------|
|
||||
| **Published** | All packages with `@uncaged/` scope |
|
||||
| **Private** | `workflow-dashboard` (no `@uncaged/` scope, `"private": true`) |
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-protocol` | `src/types.ts` | Root type definitions for the entire stack |
|
||||
| `@uncaged/workflow-runtime` | `src/index.ts` | Public API for bundle authors |
|
||||
| `@uncaged/workflow-util` | `src/index.ts` | Utility re-exports |
|
||||
| `@uncaged/workflow-execute` | `src/index.ts` | Engine public API |
|
||||
| `@uncaged/cli-workflow` | `src/cli-dispatch.ts` | Top-level command table |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Bundle](./bundle.md) — produced by workspace authors using `@uncaged/workflow-runtime`
|
||||
- [Engine](./engine.md) — the core of `@uncaged/workflow-execute`
|
||||
- [Reactor](./reactor.md) — `@uncaged/workflow-reactor`
|
||||
- [Registry](./registry.md) — `@uncaged/workflow-register`
|
||||
- [CLI](./cli.md) — `@uncaged/cli-workflow`
|
||||
@@ -0,0 +1,102 @@
|
||||
# Reactor
|
||||
|
||||
> A generic ReAct (Reason + Act) loop that drives an LLM through multiple tool-call rounds until it produces structured output matching a Zod schema.
|
||||
|
||||
## Overview
|
||||
|
||||
The reactor is a reusable abstraction for LLM interactions that require tool use. It runs a multi-turn conversation loop: the LLM is presented with a user message and a set of tools, and responds either with a tool call (which the reactor dispatches and feeds back) or with a plain JSON object matching the expected schema. The loop repeats until structured output is obtained or `maxRounds` is exhausted.
|
||||
|
||||
The reactor is used in two places:
|
||||
|
||||
1. **Extract phase** — `createExtract` in `@uncaged/workflow-execute` uses a CAS-backed reactor to extract typed `meta` from a role's content hash.
|
||||
2. **React agent** — `createReactAdapter` in `@uncaged/workflow-agent-react` uses the reactor as its execution backbone.
|
||||
|
||||
## createThreadReactor
|
||||
|
||||
```typescript
|
||||
function createThreadReactor<TThread>(
|
||||
config: ThreadReactorConfig<TThread>,
|
||||
): ThreadReactorFn<TThread>
|
||||
```
|
||||
|
||||
`ThreadReactorConfig` bundles:
|
||||
|
||||
| Field | Purpose |
|
||||
|-------|---------|
|
||||
| `llm` | The `LlmFn` to call each round |
|
||||
| `staticTools` | Tools always available (e.g., `cas_get`) |
|
||||
| `structuredToolFromSchema` | Derives a schema-specific extraction tool from the Zod schema |
|
||||
| `systemPromptForStructuredTool` | Constructs the system prompt given the extraction tool name |
|
||||
| `toolHandler` | Handles non-structured tool calls; receives the raw `ToolCall` and thread context |
|
||||
| `maxRounds` | Hard stop after N rounds; returns `err("max_react_rounds_exceeded")` |
|
||||
|
||||
## Round Lifecycle
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant R as Reactor
|
||||
participant L as LLM
|
||||
participant H as toolHandler
|
||||
|
||||
R->>L: messages + tools
|
||||
L-->>R: response
|
||||
|
||||
alt plain JSON (valid schema)
|
||||
R-->>R: return ok(value)
|
||||
else plain JSON (invalid)
|
||||
R->>L: correction message
|
||||
else tool_calls
|
||||
loop each call
|
||||
alt structured tool
|
||||
R-->>R: validate args → return ok(value)
|
||||
else static tool
|
||||
R->>H: toolHandler(call, thread)
|
||||
H-->>R: content string
|
||||
R->>L: tool result message
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
## LlmFn
|
||||
|
||||
```typescript
|
||||
type LlmFn = (input: {
|
||||
messages: ChatMessage[];
|
||||
tools: readonly ToolDefinition[];
|
||||
}) => Promise<Result<string, string>>;
|
||||
```
|
||||
|
||||
`createLlmFn(provider)` in `@uncaged/workflow-reactor` builds an `LlmFn` that calls the OpenAI-compatible chat completions endpoint and returns the raw response body as a string for the reactor to parse.
|
||||
|
||||
## Extract Phase
|
||||
|
||||
`createExtract(provider, { cas })` in `@uncaged/workflow-execute` creates a `CasReactor` — a preconfigured `ThreadReactorFn` with a `cas_get` static tool. The extract function loads the content payload for a given hash, sends it to the reactor with the role's Zod schema, and returns `ExtractResult<T>`.
|
||||
|
||||
```typescript
|
||||
type ExtractFn = <T extends Record<string, unknown>>(
|
||||
schema: z.ZodType<T>,
|
||||
contentHash: string,
|
||||
) => Promise<ExtractResult<T>>;
|
||||
```
|
||||
|
||||
The `cas_get` tool allows the LLM to dereference CAS hashes during extraction — important when the content node references artifact hashes.
|
||||
|
||||
## Relationship to Engine
|
||||
|
||||
The reactor is called within `AdapterFn` implementations (e.g., `createLlmAdapter`, `createReactAdapter`) when the agent needs multi-turn tool interaction to complete a role. The engine itself does not call the reactor directly — it only drives the outer `WorkflowFn` generator and persists `RoleOutput` values.
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-reactor` | `src/thread-reactor.ts` | `createThreadReactor` — generic ReAct loop |
|
||||
| `@uncaged/workflow-reactor` | `src/llm-fn.ts` | `createLlmFn` — OpenAI-compatible LLM caller |
|
||||
| `@uncaged/workflow-reactor` | `src/types.ts` | `LlmFn`, `ThreadReactorConfig`, `ToolCall`, `ToolDefinition`, `ChatMessage` |
|
||||
| `@uncaged/workflow-execute` | `src/cas-reactor.ts` | `createCasReactor` — reactor with `cas_get` static tool |
|
||||
| `@uncaged/workflow-execute` | `src/extract/extract-fn.ts` | `createExtract` — extract phase using the CAS reactor |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Engine](./engine.md) — drives the workflow generator; extract is called inside the adapter layer
|
||||
- [Agent Binding](./agent-binding.md) — adapter implementations that use the reactor internally
|
||||
@@ -0,0 +1,95 @@
|
||||
# Registry
|
||||
|
||||
> `workflow.yaml` — the local file that maps workflow names to their current and historical bundle hashes, plus global LLM provider configuration.
|
||||
|
||||
## Overview
|
||||
|
||||
The registry is a single YAML file at `<storageRoot>/workflow.yaml` (default: `~/.uncaged/workflow/workflow.yaml`). It is the authoritative index of which bundles are available on a machine and what name each one is known by. All CLI workflow commands read or write this file.
|
||||
|
||||
The registry is read on every `uncaged-workflow run` invocation to look up the bundle hash for a given name, then used again to resolve the `extract` model configuration. It is written atomically via the `writeWorkflowRegistry` function.
|
||||
|
||||
## Schema
|
||||
|
||||
```yaml
|
||||
config:
|
||||
maxDepth: 3
|
||||
supervisorInterval: 5
|
||||
providers:
|
||||
openrouter:
|
||||
baseUrl: "https://openrouter.ai/api/v1"
|
||||
apiKey: "sk-or-..."
|
||||
models:
|
||||
extract: "openrouter/anthropic/claude-sonnet-4-5"
|
||||
supervisor: "openrouter/anthropic/claude-haiku-3-5"
|
||||
|
||||
workflows:
|
||||
solve-issue:
|
||||
hash: "3TNKQRJ7BM4XH"
|
||||
timestamp: 1716000000000
|
||||
history:
|
||||
- hash: "2BMJPQ6YAK3WG"
|
||||
timestamp: 1715000000000
|
||||
develop:
|
||||
hash: "7VQWX8NRHK1ZT"
|
||||
timestamp: 1716100000000
|
||||
history: []
|
||||
```
|
||||
|
||||
## Types
|
||||
|
||||
```typescript
|
||||
type WorkflowRegistryFile = {
|
||||
config: WorkflowConfig | null;
|
||||
workflows: Record<string, WorkflowRegistryEntry>;
|
||||
};
|
||||
|
||||
type WorkflowRegistryEntry = {
|
||||
hash: string; // current bundle hash (13-char Crockford Base32)
|
||||
timestamp: number; // Unix epoch ms when this version was registered
|
||||
history: WorkflowHistoryEntry[];
|
||||
};
|
||||
|
||||
type WorkflowHistoryEntry = {
|
||||
hash: string;
|
||||
timestamp: number;
|
||||
};
|
||||
```
|
||||
|
||||
## Bundle Registration Flow
|
||||
|
||||
1. `uncaged-workflow workflow add <name> <file.esm.js>` is called.
|
||||
2. The bundle bytes are hashed with XXH64 → 13-char Crockford Base32.
|
||||
3. The bundle file is copied into `<storageRoot>/bundles/<hash>/` (if not already present).
|
||||
4. `registerWorkflowVersion` prepends the current head to `history` and sets the new hash as head.
|
||||
5. The updated registry is written back to `workflow.yaml`.
|
||||
|
||||
## Version History
|
||||
|
||||
Every `workflow add` on an already-registered name pushes the previous hash into `history`. History is ordered most-recent-first. `workflow rollback <name> [hash]` swaps the specified history entry back to head (or defaults to `history[0]`).
|
||||
|
||||
## Model Resolution
|
||||
|
||||
The `config.models` section uses `provider/model` references (e.g., `"openrouter/anthropic/claude-sonnet-4-5"`). `resolveModel` splits the reference on the first `/`, looks up the provider in `config.providers`, and returns a `ResolvedModel` with `{ baseUrl, apiKey, model }`. This is used by the engine to configure the `extract` LLM.
|
||||
|
||||
```typescript
|
||||
// packages/workflow-register/src/config/resolve-model.ts
|
||||
export function resolveModel(
|
||||
config: WorkflowConfig,
|
||||
modelKey: string,
|
||||
): Result<ResolvedModel, string>
|
||||
```
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-register` | `src/registry/registry.ts` | `readWorkflowRegistry`, `writeWorkflowRegistry`, `registerWorkflowVersion`, `rollbackWorkflowToHistoryHash` |
|
||||
| `@uncaged/workflow-register` | `src/registry/types.ts` | `WorkflowRegistryFile`, `WorkflowRegistryEntry`, `WorkflowHistoryEntry` |
|
||||
| `@uncaged/workflow-register` | `src/registry/registry-normalize.ts` | YAML normalization for the registry root |
|
||||
| `@uncaged/workflow-register` | `src/config/resolve-model.ts` | `resolveModel` — splits `provider/model` refs |
|
||||
| `@uncaged/workflow-register` | `src/bundle/extract-bundle-exports.ts` | Validates bundle exports before registration |
|
||||
| `@uncaged/workflow-protocol` | `src/types.ts` | `WorkflowConfig`, `ProviderConfig`, `ResolvedModel` |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Bundle](./bundle.md) — what is stored and indexed in the registry
|
||||
@@ -0,0 +1,72 @@
|
||||
# Role
|
||||
|
||||
> A named actor within a workflow defined entirely as pure data — a description, a system prompt, an extraction schema, and an optional refs extractor — with no embedded agent logic.
|
||||
|
||||
## Overview
|
||||
|
||||
A role is a `RoleDefinition<Meta>` value: a plain TypeScript object that describes what an actor in the workflow does and how its output should be structured. Roles are authored in the template or bundle source and passed to `createWorkflow` as part of the `WorkflowDefinition`. They never hold a reference to an agent implementation.
|
||||
|
||||
This separation of concerns is deliberate. The same role definition can be executed by different agents (Cursor, Hermes, an LLM, a React loop) simply by changing the `AdapterBinding` passed to `createWorkflow`. Roles are also serialized into the `WorkflowDescriptor` for tooling like the dashboard.
|
||||
|
||||
## RoleDefinition Type
|
||||
|
||||
```typescript
|
||||
type RoleDefinition<Meta extends Record<string, unknown>> = {
|
||||
description: string;
|
||||
systemPrompt: string;
|
||||
schema: z.ZodType<Meta>;
|
||||
extractRefs: ((meta: Meta) => string[]) | null;
|
||||
};
|
||||
```
|
||||
|
||||
| Field | Purpose |
|
||||
|-------|---------|
|
||||
| `description` | Human-readable summary for tooling and the `WorkflowDescriptor` |
|
||||
| `systemPrompt` | Passed to the adapter as the agent's persona/instruction for this role |
|
||||
| `schema` | Zod v4 schema that defines the structured output (`Meta`) of the role |
|
||||
| `extractRefs` | Optional function that extracts CAS hashes from `meta` to record as artifact refs |
|
||||
|
||||
## Schema and Extraction
|
||||
|
||||
Each role's `schema` is a Zod v4 type parameterized to the role's `Meta` type. When a role executes via an `AdapterFn`, the adapter is responsible for producing a value that satisfies this schema directly (the `AdapterFn` receives the schema and system prompt and returns a `RoleFn` that yields `RoleResult<T>`).
|
||||
|
||||
If `extractRefs` is non-null, the engine calls it on the completed `meta` to collect additional CAS hashes that should appear in the `StateNode.refs` skip-list, enabling traversal of artifacts produced by the role.
|
||||
|
||||
## WorkflowDefinition
|
||||
|
||||
Roles are collected into a `WorkflowDefinition<M>` alongside the moderator table:
|
||||
|
||||
```typescript
|
||||
type WorkflowDefinition<M extends RoleMeta> = {
|
||||
description: string;
|
||||
roles: { [K in keyof M & string]: RoleDefinition<M[K]> };
|
||||
table: ModeratorTable<M>;
|
||||
};
|
||||
```
|
||||
|
||||
`M` is the `RoleMeta` map that binds each role name to its concrete `Meta` type. This gives full TypeScript type safety across the moderator, adapter, and CAS storage layers.
|
||||
|
||||
## WorkflowRoleDescriptor (Serialized)
|
||||
|
||||
The `WorkflowDescriptor` (stored in the bundle's `descriptor` export) contains a `roles` map of `WorkflowRoleDescriptor` objects — a JSON-serializable projection of each `RoleDefinition`:
|
||||
|
||||
```typescript
|
||||
type WorkflowRoleDescriptor = {
|
||||
description: string;
|
||||
systemPrompt: string;
|
||||
schema: WorkflowRoleSchema; // JSON-compatible schema shape
|
||||
};
|
||||
```
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-protocol` | `src/types.ts` | `RoleDefinition`, `WorkflowDefinition`, `RoleMeta`, `WorkflowRoleDescriptor`, `WorkflowDescriptor` |
|
||||
| `@uncaged/workflow-runtime` | `src/create-workflow.ts` | Consumes `WorkflowDefinition` roles in the adapter dispatch loop |
|
||||
| `@uncaged/workflow-register` | `src/bundle/build-descriptor.ts` | Serializes `RoleDefinition[]` to `WorkflowDescriptor` |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Engine](./engine.md) — the loop that selects and executes roles
|
||||
- [Agent Binding](./agent-binding.md) — the runtime binding that executes a role via a concrete agent
|
||||
@@ -0,0 +1,97 @@
|
||||
# Thread
|
||||
|
||||
> A single execution instance of a workflow, identified by a ULID, whose state is stored as a linked chain of immutable CAS nodes.
|
||||
|
||||
## Overview
|
||||
|
||||
A thread is the runtime envelope around one call to a workflow's `run` function. It carries a unique ULID (26-char Crockford Base32) and tracks the full sequence of role steps that have executed. Because all state is written to CAS as immutable blobs, threads are append-only and fully auditable.
|
||||
|
||||
Every thread belongs to a specific workflow bundle (identified by hash). The engine writes a `StartNode` when the thread begins and one `StateNode` per completed role step — including a final `__end__` state on completion or abort. Steps accumulate in `ThreadContext.steps` and are replayed into the context whenever a thread is resumed.
|
||||
|
||||
## Lifecycle
|
||||
|
||||
```mermaid
|
||||
stateDiagram-v2
|
||||
[*] --> Active: thread run / fork
|
||||
Active --> Active: role step yielded
|
||||
Active --> Paused: pause signal
|
||||
Paused --> Active: resume signal
|
||||
Active --> Completed: generator returns WorkflowCompletion
|
||||
Active --> Aborted: kill signal / AbortSignal
|
||||
Completed --> [*]: entry in history/*.jsonl
|
||||
Aborted --> [*]: entry in history/*.jsonl (returnCode=130)
|
||||
```
|
||||
|
||||
## Identity
|
||||
|
||||
Thread IDs are ULIDs: 26-char Crockford Base32 strings composed of a 10-char timestamp prefix and a 16-char random suffix. Generated by `generateUlid` from `@uncaged/workflow-util`.
|
||||
|
||||
## State Storage
|
||||
|
||||
Thread state is stored entirely in CAS as a linked list of nodes:
|
||||
|
||||
```
|
||||
StartNode (type: "start")
|
||||
payload: { name, hash, depth, parentState }
|
||||
refs: [promptHash, parentState?]
|
||||
|
||||
StateNode (type: "state") ← one per role step
|
||||
payload: { role, meta, start, content, ancestors[], compact, timestamp, childThread }
|
||||
refs: [contentHash, startHash, ancestor hashes...]
|
||||
|
||||
StateNode (type: "state", role: "__end__") ← final node
|
||||
payload: { returnCode, summary }
|
||||
```
|
||||
|
||||
The `ancestors` array implements a skip-list (capped at 11 entries: 1 direct parent + up to 10 ancestors) to allow efficient traversal without loading every node in the chain.
|
||||
|
||||
## Index Files
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `<bundleDir>/threads.json` | Active thread index — maps `threadId → { head, start, updatedAt }` |
|
||||
| `<bundleDir>/history/YYYY-MM-DD.jsonl` | Completed thread records — one JSON line per completed/aborted thread |
|
||||
| `<storageRoot>/cas/` | All CAS blobs shared across all bundles |
|
||||
|
||||
A thread is "active" while it appears in `threads.json`. On completion, its entry is removed from `threads.json` and a record appended to the appropriate `history/*.jsonl` file.
|
||||
|
||||
## ThreadContext
|
||||
|
||||
The `ThreadContext` type is the read-only view passed into every role and moderator call:
|
||||
|
||||
```typescript
|
||||
type ThreadContext<M extends RoleMeta = RoleMeta> = {
|
||||
threadId: string;
|
||||
depth: number;
|
||||
bundleHash: string;
|
||||
start: StartStep;
|
||||
steps: RoleStep<M>[];
|
||||
};
|
||||
```
|
||||
|
||||
`depth` tracks nesting for sub-workflow invocations (workflow-as-agent). `steps` grows by one entry after each successful role execution.
|
||||
|
||||
## Fork
|
||||
|
||||
A thread can be forked from any completed role step via `thread fork <id> [--from-role <role>]`. The fork reuses the original `StartNode` (same `startHash`) and replays CAS steps up to the fork point before resuming the generator. The forked thread gets a new ULID.
|
||||
|
||||
## Debug Logs
|
||||
|
||||
Each thread writes structured JSONL debug logs to `.info.jsonl` in the bundle directory. Each log line is `{ tag, content, timestamp }` where `tag` is an 8-char Crockford Base32 call-site identifier.
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-protocol` | `src/types.ts` | `ThreadContext`, `StartStep`, `RoleStep`, `RoleMeta` types |
|
||||
| `@uncaged/workflow-protocol` | `src/cas-types.ts` | `StartNode`, `StartNodePayload`, `StateNode`, `StateNodePayload` |
|
||||
| `@uncaged/workflow-execute` | `src/engine/threads-index.ts` | `threads.json` read/write, history append, `ThreadIndexEntry` |
|
||||
| `@uncaged/workflow-execute` | `src/engine/engine.ts` | `executeThread` — starts, drives, and finalizes a thread |
|
||||
| `@uncaged/workflow-execute` | `src/engine/fork-thread.ts` | Fork logic |
|
||||
| `@uncaged/workflow-util` | `src/ulid.ts` | `generateUlid` — ULID generation |
|
||||
|
||||
## See Also
|
||||
|
||||
- [CAS](./cas.md) — the storage layer that holds all thread state nodes
|
||||
- [Engine](./engine.md) — the execution loop that drives the thread
|
||||
- [Bundle](./bundle.md) — the workflow being executed in this thread
|
||||
@@ -0,0 +1,153 @@
|
||||
# Workflow Templates
|
||||
|
||||
> Pre-built `WorkflowDefinition` objects exported from `@uncaged/workflow-template-*` packages that bundle authors can import, customize, or use directly.
|
||||
|
||||
## Overview
|
||||
|
||||
Templates are the reference implementations of common workflow patterns. They export a complete `WorkflowDefinition<M>` — typed roles with Zod schemas, and a `ModeratorTable` — ready to be passed to `createWorkflow`. A bundle author imports a template definition, supplies an `AdapterBinding`, calls `createWorkflow`, and exports the result as `run`.
|
||||
|
||||
Templates are published as regular `@uncaged/*` npm packages. They are not bundles themselves; they are TypeScript libraries that become part of a bundle when the author's workspace is built.
|
||||
|
||||
## solve-issue Template
|
||||
|
||||
**Package**: `@uncaged/workflow-template-solve-issue`
|
||||
|
||||
Resolves an issue end-to-end by preparing the repository, delegating implementation to a nested `develop` workflow, and opening a pull request.
|
||||
|
||||
### Roles
|
||||
|
||||
| Role | Description |
|
||||
|------|-------------|
|
||||
| `preparer` | Reads the issue, clones/checks out the repo, sets up the environment |
|
||||
| `developer` | Delegates to the `develop` workflow via `workflowAsAgent` (child thread) |
|
||||
| `submitter` | Opens a pull request with the completed changes |
|
||||
|
||||
### Moderator Table
|
||||
|
||||
```
|
||||
__start__ → preparer → developer → submitter → __end__
|
||||
```
|
||||
|
||||
Linear routing — each role runs exactly once in sequence.
|
||||
|
||||
### Meta Types
|
||||
|
||||
```typescript
|
||||
type SolveIssueMeta = {
|
||||
preparer: PreparerMeta;
|
||||
developer: DeveloperMeta;
|
||||
submitter: SubmitterMeta;
|
||||
};
|
||||
```
|
||||
|
||||
## develop Template
|
||||
|
||||
**Package**: `@uncaged/workflow-template-develop`
|
||||
|
||||
Plans an implementation in phases, codes each phase incrementally, reviews, verifies with tests/build/lint, and commits.
|
||||
|
||||
### Roles
|
||||
|
||||
| Role | Description |
|
||||
|------|-------------|
|
||||
| `planner` | Produces an ordered list of implementation phases with hashes |
|
||||
| `coder` | Implements one phase; reports `completedPhase` hash in meta |
|
||||
| `reviewer` | Reviews the accumulated changes; approves or requests changes |
|
||||
| `tester` | Runs tests/lint/build; reports `passed` or `failed` |
|
||||
| `committer` | Creates the final git commit |
|
||||
|
||||
### Moderator Table
|
||||
|
||||
```
|
||||
__start__ → planner
|
||||
planner → __end__ (if status == "aborted")
|
||||
planner → coder (fallback)
|
||||
coder → reviewer (if allPhasesComplete)
|
||||
coder → coder (fallback — repeat per phase)
|
||||
reviewer → tester (if status == "approved")
|
||||
reviewer → coder (fallback — request changes)
|
||||
tester → committer (if status == "passed")
|
||||
tester → coder (fallback — fix failures)
|
||||
committer → __end__
|
||||
```
|
||||
|
||||
### Meta Types
|
||||
|
||||
```typescript
|
||||
type DevelopMeta = {
|
||||
planner: PlannerMeta;
|
||||
coder: CoderMeta;
|
||||
reviewer: ReviewerMeta;
|
||||
tester: TesterMeta;
|
||||
committer: CommitterMeta;
|
||||
};
|
||||
```
|
||||
|
||||
## Writing a Custom Template
|
||||
|
||||
A minimal custom workflow:
|
||||
|
||||
```typescript
|
||||
import { createWorkflow, type WorkflowDefinition, END, START } from "@uncaged/workflow-runtime";
|
||||
import { z } from "zod/v4";
|
||||
import type { AdapterBinding } from "@uncaged/workflow-runtime";
|
||||
|
||||
type MyMeta = {
|
||||
analyst: { summary: string; confidence: number };
|
||||
writer: { report: string };
|
||||
};
|
||||
|
||||
const def: WorkflowDefinition<MyMeta> = {
|
||||
description: "Analyse then write a report.",
|
||||
roles: {
|
||||
analyst: {
|
||||
description: "Analyses the input and produces a structured summary.",
|
||||
systemPrompt: "You are an expert analyst...",
|
||||
schema: z.object({ summary: z.string(), confidence: z.number() }),
|
||||
extractRefs: null,
|
||||
},
|
||||
writer: {
|
||||
description: "Writes the final report.",
|
||||
systemPrompt: "You are a technical writer...",
|
||||
schema: z.object({ report: z.string() }),
|
||||
extractRefs: null,
|
||||
},
|
||||
},
|
||||
table: {
|
||||
[START]: [{ condition: "FALLBACK", role: "analyst" }],
|
||||
analyst: [{ condition: "FALLBACK", role: "writer" }],
|
||||
writer: [{ condition: "FALLBACK", role: END }],
|
||||
},
|
||||
};
|
||||
|
||||
// In the bundle entry point:
|
||||
export const run = createWorkflow(def, binding);
|
||||
export const descriptor = buildDescriptor(def);
|
||||
```
|
||||
|
||||
## Template → Bundle Relationship
|
||||
|
||||
Templates are TypeScript library packages, not bundles. To use a template:
|
||||
|
||||
1. Install the template package from npm: `bun add @uncaged/workflow-template-develop`.
|
||||
2. Import the definition: `import { developWorkflowDefinition } from "@uncaged/workflow-template-develop"`.
|
||||
3. Supply an `AdapterBinding` and call `createWorkflow`.
|
||||
4. Build with `bun build` to produce `.esm.js`.
|
||||
5. Register with `uncaged-workflow workflow add`.
|
||||
|
||||
## Code Pointers
|
||||
|
||||
| Package | File | What it does |
|
||||
|---------|------|-------------|
|
||||
| `@uncaged/workflow-template-solve-issue` | `src/index.ts` | `solveIssueWorkflowDefinition`, role and moderator exports |
|
||||
| `@uncaged/workflow-template-solve-issue` | `src/roles.ts` | `SolveIssueMeta`, `solveIssueRoles` |
|
||||
| `@uncaged/workflow-template-solve-issue` | `src/moderator.ts` | `solveIssueTable` — linear transition table |
|
||||
| `@uncaged/workflow-template-develop` | `src/index.ts` | `developWorkflowDefinition`, role and moderator exports |
|
||||
| `@uncaged/workflow-template-develop` | `src/roles.ts` | `DevelopMeta`, `developRoles` |
|
||||
| `@uncaged/workflow-template-develop` | `src/moderator.ts` | `developTable` — conditional multi-phase table |
|
||||
|
||||
## See Also
|
||||
|
||||
- [Bundle](./bundle.md) — the build artifact produced from a template + adapter
|
||||
- [Role](./role.md) — the `RoleDefinition` type each template role implements
|
||||
- [Engine](./engine.md) — the execution loop that drives the template's `WorkflowFn`
|
||||
@@ -1,527 +0,0 @@
|
||||
# `uwf` — Stateless Workflow CLI
|
||||
|
||||
> 将 workflow 引擎降维为无状态单步 CLI。Workflow 是纯数据(CAS 节点),执行是单步原子操作,agent 是可插拔外部命令。
|
||||
|
||||
---
|
||||
|
||||
## 1. CLI Design
|
||||
|
||||
### 1.1 命令总览
|
||||
|
||||
```
|
||||
# thread 组
|
||||
uwf thread start <workflow> -p <prompt> # 创建 thread,不执行
|
||||
uwf thread step <thread-id> [--agent] # 单步执行
|
||||
uwf thread show <thread-id> # thread-id → head 查询
|
||||
uwf thread list [--all] # 列出活跃 threads(--all 含已归档)
|
||||
uwf thread kill <thread-id> # 终结 thread,归档
|
||||
|
||||
# workflow 组
|
||||
uwf workflow put <file.yaml> # 注册 workflow(YAML → CAS)
|
||||
uwf workflow show <workflow-id> # 查看 workflow 定义
|
||||
uwf workflow list # 列出已注册 workflows
|
||||
```
|
||||
|
||||
两组对称,各 3-4 个子命令。CAS 操作交给 `json-cas` CLI,不在 `uwf` 中重复。
|
||||
|
||||
### 1.2 `uwf thread start`
|
||||
|
||||
```bash
|
||||
uwf thread start <workflow> -p "Fix the login bug described in issue #42"
|
||||
```
|
||||
|
||||
- `<workflow>` — workflow 名或 CAS hash
|
||||
- `-p` — 用户 prompt(必填)
|
||||
|
||||
**输出(JSON to stdout):**
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"workflow": "4KNM2PXR3B1QW", // workflow CAS hash (XXH64, 13-char Crockford Base32)
|
||||
"thread": "01J7K9M2XNPQR5VWBCDF8G3H4T" // ULID
|
||||
}
|
||||
```
|
||||
|
||||
**做的事:**
|
||||
1. 解析 workflow(名字查 registry → CAS hash)
|
||||
2. 生成 thread ULID
|
||||
3. 写 StartNode 到 CAS
|
||||
4. 在 threads.yaml 中记录链头 → StartNode hash
|
||||
5. 输出 JSON
|
||||
|
||||
### 1.3 `uwf thread step`
|
||||
|
||||
```bash
|
||||
uwf thread step 01J7K9M2XNPQR5VWBCDF8G3H4T
|
||||
uwf thread step 01J7K9M2XNPQR5VWBCDF8G3H4T --agent "bunx uwf-cursor"
|
||||
```
|
||||
|
||||
**输出(JSON to stdout):**
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"workflow": "4KNM2PXR3B1QW",
|
||||
"thread": "01J7K9M2XNPQR5VWBCDF8G3H4T",
|
||||
"head": "8FWKR3TN5V1QA", // 新链头 StepNode 的 CAS hash
|
||||
"done": false // true = moderator 返回 END,thread 已归档
|
||||
}
|
||||
```
|
||||
|
||||
`done: true` 时 head 仍然有值(最后一个 StepNode),但 thread 已从 threads.yaml 移除。
|
||||
对已结束或不存在的 thread 调用 step 会报错(非 active thread)。
|
||||
|
||||
详细信息通过 `uwf thread show <thread-id>` 或 `json-cas get <head>` 查看。
|
||||
|
||||
**做的事:**
|
||||
1. 读链头 → 当前 StepNode(或 StartNode)
|
||||
2. 收集 thread 历史(遍历链)
|
||||
3. 调 moderator:评估 JSONata conditions → 得到下一个 role(或 END)
|
||||
4. 若 END → 归档 thread,输出最后链头,退出
|
||||
5. 确定 agent command(`--agent` override > config.yaml per-workflow/role > config.yaml defaultAgent)
|
||||
6. 调用:`<agent-cmd> <thread-id> <role>`,捕获 stdout 得到新 StepNode hash
|
||||
7. 更新链头指针
|
||||
8. 再次调 moderator(基于新 StepNode)判断 done
|
||||
9. 输出 JSON
|
||||
|
||||
### 1.4 `uwf thread show`
|
||||
|
||||
```bash
|
||||
uwf thread show 01J7K9M2XNPQR5VWBCDF8G3H4T
|
||||
```
|
||||
|
||||
**输出(JSON to stdout):**
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"workflow": "4KNM2PXR3B1QW",
|
||||
"thread": "01J7K9M2XNPQR5VWBCDF8G3H4T",
|
||||
"head": "8FWKR3TN5V1QA",
|
||||
"done": false
|
||||
}
|
||||
```
|
||||
|
||||
纯 thread-id → head 查询。详细内容用 `json-cas get <head>` 或 `json-cas walk <head>` 查看。
|
||||
|
||||
### 1.5 Agent CLI 协议
|
||||
|
||||
每个 agent 是一个命令,接受 thread-id 和 role 两个参数:
|
||||
|
||||
```bash
|
||||
uwf-hermes <thread-id> <role>
|
||||
```
|
||||
|
||||
**约定:**
|
||||
- `uwf step` 负责 moderator 决策,将 role 传给 agent CLI
|
||||
- agent-kit 根据 thread + role 从 CAS 读 systemPrompt / outputSchema
|
||||
- agent-kit 组装完整 prompt(role systemPrompt + thread context + user prompt from StartNode)
|
||||
- agent 执行实际逻辑,agent-kit 负责 extract
|
||||
- agent 将 StepNode 写入 CAS(含 output、detail、agent、prev),但**不挪链头指针**
|
||||
- stdout 输出新 StepNode 的 CAS hash(纯文本,一行)
|
||||
- 所有配置从环境变量读(LLM model、API key、extractor config)
|
||||
- exit 0 = 成功,非 0 = 失败
|
||||
|
||||
**stdout 输出:**
|
||||
|
||||
```
|
||||
8FWKR3TN5V1QA
|
||||
```
|
||||
|
||||
`uwf step` 拿到这个 hash 后更新链头指针、判断 done。
|
||||
|
||||
---
|
||||
|
||||
## 2. CAS 结构定义
|
||||
|
||||
### 2.1 类型层级
|
||||
|
||||
沿用 json-cas 的三层:bootstrap meta-schema → JSON Schema nodes → data nodes。
|
||||
|
||||
下面所有 CAS 节点都遵循 `{ type: cas_ref, payload: T, timestamp: number }` 的标准格式。
|
||||
`cas_ref` 类型的字符串字段在 json-cas 中已内置支持,不需要额外的 `$ref` 包装。
|
||||
|
||||
### 2.2 数据节点
|
||||
|
||||
#### `Workflow`
|
||||
|
||||
Roles 和 moderator 内联在 Workflow 中,只有 outputSchema 独立为 CAS 节点(方便 json-cas 校验)。
|
||||
|
||||
```yaml
|
||||
type: <workflow-schema-hash>
|
||||
payload:
|
||||
name: "solve-issue"
|
||||
description: "End-to-end issue resolution"
|
||||
roles:
|
||||
planner:
|
||||
description: "Creates implementation plan"
|
||||
systemPrompt: "You are a planning agent..."
|
||||
outputSchema: "5GWKR8TN1V3JA" # cas_ref → JSON Schema 节点(json-cas 内置)
|
||||
developer:
|
||||
description: "Implements code changes"
|
||||
systemPrompt: "You are a developer agent..."
|
||||
outputSchema: "8CNWT4KR6D1HV" # cas_ref → JSON Schema 节点
|
||||
reviewer:
|
||||
description: "Reviews code changes"
|
||||
systemPrompt: "You are a code reviewer..."
|
||||
outputSchema: "1VPBG9SM5E7WK" # cas_ref → JSON Schema 节点
|
||||
conditions:
|
||||
needsClarification:
|
||||
description: "Planner requests clarification from user"
|
||||
expression: "$exists(steps[-1].output.needsClarification)"
|
||||
notApproved:
|
||||
description: "Reviewer rejected the implementation"
|
||||
expression: "steps[-1].output.approved = false"
|
||||
graph:
|
||||
$START:
|
||||
- role: "planner"
|
||||
condition: null # 无条件(fallback)
|
||||
planner:
|
||||
- role: "developer"
|
||||
condition: "needsClarification"
|
||||
- role: "$END"
|
||||
condition: null
|
||||
developer:
|
||||
- role: "reviewer"
|
||||
condition: null
|
||||
reviewer:
|
||||
- role: "developer"
|
||||
condition: "notApproved"
|
||||
- role: "$END"
|
||||
condition: null
|
||||
```
|
||||
|
||||
- `roles` — 内联定义,每个 role 的 `outputSchema` 是独立的 cas_ref(指向 json-cas 内置 JSON Schema 节点)
|
||||
- `conditions` — `Record<Name, JSONata>`,命名条件,方便画图描述
|
||||
- `graph` — `Record<Role | "$START", Transition[]>`,每个 Transition = `{ role, condition }`
|
||||
- `condition` 引用 conditions 中的 key,`null` = fallback
|
||||
- 按数组顺序求值,第一个匹配的 transition 胜出
|
||||
- 不含 agent binding — agent 配置在 `~/.uncaged/workflow/config.yaml` 中管理
|
||||
|
||||
JSONata 表达式的求值上下文:
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"start": { // StartNode 信息
|
||||
"workflow": "4KNM2PXR3B1QW",
|
||||
"prompt": "Fix the login bug..."
|
||||
},
|
||||
"steps": [ // 所有已完成 steps,从旧到新
|
||||
{ "role": "planner", "output": { "phases": [...] }, "detail": "7BQST3VW9F2MA", "agent": "uwf-hermes" },
|
||||
{ "role": "developer", "output": { "filesChanged": ["src/auth.ts"], "summary": "Fixed redirect" }, "detail": "9KRVW3TN5F1QA", "agent": "uwf-cursor" },
|
||||
{ "role": "reviewer", "output": { "approved": false }, "detail": "2MXBG6PN4A8JR", "agent": "uwf-hermes" }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
注:`output` 在上下文中会被自动展开为实际的 CAS 节点内容(而非 hash),方便 JSONata 表达式直接访问字段。
|
||||
|
||||
#### `StartNode`(Thread 起点)
|
||||
|
||||
```yaml
|
||||
type: <start-node-schema-hash>
|
||||
payload:
|
||||
workflow: "4KNM2PXR3B1QW" # cas_ref → Workflow
|
||||
prompt: "Fix the login bug..."
|
||||
```
|
||||
|
||||
- 没有 thread-id — thread-id 是索引层面的事,不进 CAS 内容
|
||||
- 没有 agent binding — 运行时从 config.yaml 解析
|
||||
|
||||
#### `StepNode`(Thread 每一步)
|
||||
|
||||
```yaml
|
||||
type: <step-node-schema-hash>
|
||||
payload:
|
||||
start: "4TNVW8KR2B3MA" # cas_ref → StartNode(每个 step 都引用)
|
||||
prev: "2MXBG6PN4A8JR" # cas_ref → 前一个 StepNode,第一步为 null
|
||||
role: "developer"
|
||||
output: "9KRVW3TN5F1QA" # cas_ref → 结构化输出节点(符合 role 的 outputSchema)
|
||||
detail: "7BQST3VW9F2MA" # cas_ref → 执行详情(content node / 子 workflow terminal StepNode / ...)
|
||||
agent: "uwf-cursor" # 实际使用的 agent 命令(纯字符串)
|
||||
```
|
||||
|
||||
- `start` — 每个 StepNode 都直接引用 StartNode,方便随机访问
|
||||
- `prev` — 前一个 StepNode 的 cas_ref,第一步为 `null`(不指向 StartNode)
|
||||
- `output` — cas_ref,指向符合 role outputSchema 的 CAS 节点,可用 json-cas 校验
|
||||
- `detail` — cas_ref,指向执行详情。可以是原始 agent 输出(content node),也可以是子 workflow thread 的 terminal StepNode(workflowAsAgent 场景)
|
||||
- `agent` — 纯字符串,不是 CAS 节点
|
||||
|
||||
### 2.3 链式结构
|
||||
|
||||
```
|
||||
threads.yaml: { "01J7K9M2XNPQR5VWBCDF8G3H4T": "8FWKR3TN5V1QA" }
|
||||
│
|
||||
▼
|
||||
StepNode (step 3)
|
||||
├── start ──→ StartNode
|
||||
│ ├── workflow → CAS(Workflow)
|
||||
│ └── prompt: "Fix..."
|
||||
├── prev ──→ StepNode (step 2)
|
||||
│ ├── start ──→ (same StartNode)
|
||||
│ ├── prev ──→ StepNode (step 1)
|
||||
│ │ ├── start ──→ (same StartNode)
|
||||
│ │ ├── prev: null
|
||||
│ │ ├── role: "planner"
|
||||
│ │ └── ...
|
||||
│ ├── role: "developer"
|
||||
│ └── ...
|
||||
├── role: "reviewer"
|
||||
├── output → CAS({ approved: true })
|
||||
├── detail → CAS(raw output | sub-workflow terminal node)
|
||||
└── agent: "uwf-hermes"
|
||||
```
|
||||
|
||||
### 2.4 可变状态
|
||||
|
||||
系统两个顶层 YAML 文件和一个 env 文件:
|
||||
|
||||
```yaml
|
||||
# ~/.uncaged/workflow/config.yaml — 全局配置
|
||||
providers:
|
||||
openai:
|
||||
baseUrl: "https://api.openai.com/v1"
|
||||
apiKeyEnv: "OPENAI_API_KEY"
|
||||
anthropic:
|
||||
baseUrl: "https://api.anthropic.com/v1"
|
||||
apiKeyEnv: "ANTHROPIC_API_KEY"
|
||||
openrouter:
|
||||
baseUrl: "https://openrouter.ai/api/v1"
|
||||
apiKeyEnv: "OPENROUTER_API_KEY"
|
||||
|
||||
models:
|
||||
sonnet:
|
||||
provider: "openrouter"
|
||||
name: "anthropic/claude-sonnet-4"
|
||||
gpt4o-mini:
|
||||
provider: "openai"
|
||||
name: "gpt-4o-mini"
|
||||
|
||||
agents:
|
||||
hermes:
|
||||
command: "uwf-hermes"
|
||||
args: []
|
||||
cursor:
|
||||
command: "uwf-cursor"
|
||||
args: []
|
||||
|
||||
defaultAgent: "hermes"
|
||||
agentOverrides:
|
||||
solve-issue:
|
||||
developer: "cursor"
|
||||
|
||||
defaultModel: "sonnet"
|
||||
modelOverrides:
|
||||
extract: "gpt4o-mini"
|
||||
```
|
||||
|
||||
```yaml
|
||||
# ~/.uncaged/workflow/threads.yaml — active thread 链头指针
|
||||
01J7K9M2XNPQR5VWBCDF8G3H4T: "8FWKR3TN5V1QA"
|
||||
01J8AB3QRMSTV6WKXZ2C4DF7GN: "3CNWT9KR6D2HV"
|
||||
```
|
||||
|
||||
Thread 结束时从 threads.yaml 移除。可选:追加到 `history.jsonl` 做归档。
|
||||
|
||||
```bash
|
||||
# ~/.uncaged/workflow/.env — 敏感信息(API keys)
|
||||
OPENAI_API_KEY=sk-...
|
||||
ANTHROPIC_API_KEY=sk-ant-...
|
||||
OPENROUTER_API_KEY=sk-or-...
|
||||
```
|
||||
|
||||
- `config.yaml` — 非敏感配置(agent 命令、model 名、provider 名)
|
||||
- `.env` — 敏感信息(API keys),agent-kit 启动时自动加载
|
||||
- `threads.yaml` — 运行时状态
|
||||
|
||||
---
|
||||
|
||||
## 3. 包结构
|
||||
|
||||
全新包,不复用现有 packages,避免命名冲突。CAS 直接依赖 `@uncaged/json-cas`。
|
||||
|
||||
```
|
||||
packages/
|
||||
├── cli-uwf/ # @uncaged/cli-uwf — uwf CLI(thread/workflow 命令)
|
||||
├── uwf-moderator/ # @uncaged/uwf-moderator — JSONata moderator 引擎
|
||||
├── uwf-agent-kit/ # @uncaged/uwf-agent-kit — Agent CLI 框架(含 extractor)
|
||||
├── uwf-agent-hermes/ # @uncaged/uwf-agent-hermes — uwf-hermes CLI
|
||||
├── uwf-agent-cursor/ # @uncaged/uwf-agent-cursor — uwf-cursor CLI
|
||||
└── uwf-protocol/ # @uncaged/uwf-protocol — 共享类型定义
|
||||
```
|
||||
|
||||
**外部依赖:**
|
||||
- `@uncaged/json-cas` — CAS 存储、hash、schema 校验
|
||||
- `@uncaged/json-cas-fs` — 文件系统 CAS 后端
|
||||
|
||||
**现有包全部保留不动**,新旧并存,逐步迁移。
|
||||
|
||||
---
|
||||
|
||||
## 4. 关键数据类型
|
||||
|
||||
JSONata 求值上下文本质上是 thread 链表的线性化表达。StepNode payload 和上下文中的 step 共享大量字段,提取为公共类型。
|
||||
|
||||
### 4.1 公共类型
|
||||
|
||||
```typescript
|
||||
/** CAS hash — XXH64, 13-char Crockford Base32 */
|
||||
type CasRef = string;
|
||||
|
||||
/** Thread ID — ULID, 26-char Crockford Base32 */
|
||||
type ThreadId = string;
|
||||
|
||||
/** 一个 step 的核心数据,被 StepNode payload 和 JSONata 上下文共享 */
|
||||
type StepRecord = {
|
||||
role: string;
|
||||
output: CasRef; // cas_ref → 结构化输出节点(符合 role outputSchema)
|
||||
detail: CasRef; // cas_ref → 执行详情(content node / 子 workflow terminal StepNode)
|
||||
agent: string; // 实际使用的 agent 命令(纯字符串)
|
||||
};
|
||||
```
|
||||
|
||||
### 4.2 Workflow 定义
|
||||
|
||||
```typescript
|
||||
type RoleDefinition = {
|
||||
description: string;
|
||||
systemPrompt: string;
|
||||
outputSchema: CasRef; // cas_ref → json-cas 内置 JSON Schema 节点
|
||||
};
|
||||
|
||||
type Transition = {
|
||||
role: string; // 目标 role 名 或 "$END"
|
||||
condition: string | null; // 引用 conditions 中的 key,null = fallback
|
||||
};
|
||||
|
||||
type ConditionDefinition = {
|
||||
description: string;
|
||||
expression: string; // JSONata expression
|
||||
};
|
||||
|
||||
type WorkflowPayload = {
|
||||
name: string;
|
||||
description: string;
|
||||
roles: Record<string, RoleDefinition>;
|
||||
conditions: Record<string, ConditionDefinition>;
|
||||
graph: Record<string, Transition[]>; // Record<Role | "$START", Transition[]>
|
||||
};
|
||||
```
|
||||
|
||||
### 4.3 Thread 节点
|
||||
|
||||
```typescript
|
||||
type StartNodePayload = {
|
||||
workflow: CasRef; // cas_ref → Workflow
|
||||
prompt: string;
|
||||
};
|
||||
|
||||
type StepNodePayload = StepRecord & {
|
||||
start: CasRef; // cas_ref → StartNode(每个 step 都引用)
|
||||
prev: CasRef | null; // cas_ref → 前一个 StepNode,第一步为 null
|
||||
};
|
||||
```
|
||||
|
||||
### 4.4 JSONata 求值上下文
|
||||
|
||||
Thread 链表的线性化。`steps[n]` 的字段和 `StepRecord` 一致,但 `output` 被展开为实际内容。
|
||||
|
||||
```typescript
|
||||
/** JSONata 上下文中的 step — output 被展开 */
|
||||
type StepContext = Omit<StepRecord, "output"> & {
|
||||
output: unknown; // 展开后的 CAS 节点内容,非 hash
|
||||
};
|
||||
|
||||
type ModeratorContext = {
|
||||
start: StartNodePayload;
|
||||
steps: StepContext[]; // 从旧到新
|
||||
};
|
||||
```
|
||||
|
||||
### 4.5 CLI 输出
|
||||
|
||||
```typescript
|
||||
/** uwf thread start */
|
||||
type StartOutput = {
|
||||
workflow: CasRef;
|
||||
thread: ThreadId;
|
||||
};
|
||||
|
||||
/** uwf thread step / uwf thread show */
|
||||
type StepOutput = {
|
||||
workflow: CasRef;
|
||||
thread: ThreadId;
|
||||
head: CasRef;
|
||||
done: boolean;
|
||||
};
|
||||
|
||||
/** uwf thread list */
|
||||
type ThreadListItem = {
|
||||
thread: ThreadId;
|
||||
workflow: CasRef;
|
||||
head: CasRef;
|
||||
};
|
||||
```
|
||||
|
||||
### 4.6 配置
|
||||
|
||||
```typescript
|
||||
/** Alias types for config references */
|
||||
type AgentAlias = string;
|
||||
type ModelAlias = string;
|
||||
type ProviderAlias = string;
|
||||
type WorkflowName = string;
|
||||
type RoleName = string;
|
||||
type Scenario = string; // e.g. "extract"
|
||||
|
||||
type ProviderConfig = {
|
||||
baseUrl: string;
|
||||
apiKeyEnv: string; // env var name to read API key from
|
||||
};
|
||||
|
||||
type ModelConfig = {
|
||||
provider: ProviderAlias;
|
||||
name: string; // e.g. "anthropic/claude-sonnet-4", "gpt-4o-mini"
|
||||
};
|
||||
|
||||
type AgentConfig = {
|
||||
command: string;
|
||||
args: string[];
|
||||
};
|
||||
|
||||
/** ~/.uncaged/workflow/config.yaml */
|
||||
type WorkflowConfig = {
|
||||
providers: Record<ProviderAlias, ProviderConfig>;
|
||||
models: Record<ModelAlias, ModelConfig>;
|
||||
agents: Record<AgentAlias, AgentConfig>;
|
||||
defaultAgent: AgentAlias;
|
||||
agentOverrides: Record<WorkflowName, Record<RoleName, AgentAlias>> | null;
|
||||
defaultModel: ModelAlias;
|
||||
modelOverrides: Record<Scenario, ModelAlias> | null;
|
||||
};
|
||||
|
||||
/** ~/.uncaged/workflow/threads.yaml */
|
||||
type ThreadsIndex = Record<ThreadId, CasRef>;
|
||||
// ^ thread-id ^ head StepNode/StartNode hash
|
||||
```
|
||||
|
||||
### 4.7 类型关系图
|
||||
|
||||
```
|
||||
WorkflowConfig (config.yaml)
|
||||
ThreadsIndex (threads.yaml) ← 唯二可变状态
|
||||
│
|
||||
│ thread-id → head hash
|
||||
▼
|
||||
StepNodePayload ──extends──→ StepRecord ←──maps to──→ StepContext
|
||||
│ │ │
|
||||
├── start → StartNodePayload│ │ (output 展开)
|
||||
├── prev → StepNodePayload │ │
|
||||
│ ├── role ├── role
|
||||
│ ├── output (CasRef) ├── output (展开)
|
||||
│ ├── detail (CasRef) ├── detail (CasRef)
|
||||
│ └── agent (string) └── agent (string)
|
||||
│
|
||||
└── start.workflow → WorkflowPayload
|
||||
├── roles: Record<name, RoleDefinition>
|
||||
├── conditions: Record<name, JSONata>
|
||||
└── graph: Record<role, Transition[]>
|
||||
```
|
||||
@@ -4,6 +4,10 @@
|
||||
"workspaces": [
|
||||
"packages/*"
|
||||
],
|
||||
"overrides": {
|
||||
"@uncaged/json-cas": "file:../json-cas/packages/json-cas",
|
||||
"@uncaged/json-cas-workflow": "file:../json-cas/packages/json-cas-workflow"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "bunx tsc --build",
|
||||
"check": "bunx tsc --build && biome check . && bash scripts/lint-log-tags.sh",
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
{
|
||||
"name": "@uncaged/cli-uwf",
|
||||
"version": "0.1.0",
|
||||
"files": [
|
||||
"src",
|
||||
"dist",
|
||||
"package.json"
|
||||
],
|
||||
"type": "module",
|
||||
"bin": {
|
||||
"uwf": "./src/cli.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@uncaged/json-cas": "^0.1.1",
|
||||
"@uncaged/json-cas-fs": "^0.1.1",
|
||||
"@uncaged/uwf-agent-kit": "workspace:^",
|
||||
"@uncaged/uwf-moderator": "workspace:^",
|
||||
"@uncaged/uwf-protocol": "workspace:^",
|
||||
"@uncaged/workflow-util": "workspace:^",
|
||||
"commander": "^14.0.3",
|
||||
"dotenv": "^16.6.1",
|
||||
"yaml": "^2.8.4"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "bun test"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import { Command } from "commander";
|
||||
|
||||
import {
|
||||
cmdThreadKill,
|
||||
cmdThreadList,
|
||||
cmdThreadShow,
|
||||
cmdThreadStart,
|
||||
cmdThreadStep,
|
||||
} from "./commands/thread.js";
|
||||
import { cmdWorkflowList, cmdWorkflowPut, cmdWorkflowShow } from "./commands/workflow.js";
|
||||
import { resolveStorageRoot } from "./store.js";
|
||||
|
||||
function writeJson(data: unknown): void {
|
||||
process.stdout.write(`${JSON.stringify(data)}\n`);
|
||||
}
|
||||
|
||||
function runAction(action: () => Promise<void>): void {
|
||||
action().catch((e: unknown) => {
|
||||
const message = e instanceof Error ? e.message : String(e);
|
||||
process.stderr.write(`${message}\n`);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
const program = new Command();
|
||||
|
||||
program.name("uwf").description("Stateless workflow CLI");
|
||||
|
||||
const workflow = program.command("workflow").description("Workflow registry and CAS");
|
||||
|
||||
workflow
|
||||
.command("put")
|
||||
.description("Register a workflow from YAML")
|
||||
.argument("<file>", "Workflow YAML file")
|
||||
.action((file: string) => {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
runAction(async () => {
|
||||
const result = await cmdWorkflowPut(storageRoot, file);
|
||||
writeJson(result);
|
||||
});
|
||||
});
|
||||
|
||||
workflow
|
||||
.command("show")
|
||||
.description("Show a workflow by name or CAS hash")
|
||||
.argument("<id>", "Workflow name or hash")
|
||||
.action((id: string) => {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
runAction(async () => {
|
||||
const result = await cmdWorkflowShow(storageRoot, id);
|
||||
writeJson(result);
|
||||
});
|
||||
});
|
||||
|
||||
workflow
|
||||
.command("list")
|
||||
.description("List registered workflows")
|
||||
.action(() => {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
runAction(async () => {
|
||||
const result = await cmdWorkflowList(storageRoot);
|
||||
writeJson(result);
|
||||
});
|
||||
});
|
||||
|
||||
const thread = program.command("thread").description("Thread lifecycle and execution");
|
||||
|
||||
thread
|
||||
.command("start")
|
||||
.description("Create a thread without executing")
|
||||
.argument("<workflow>", "Workflow name or hash")
|
||||
.requiredOption("-p, --prompt <text>", "User prompt")
|
||||
.action((workflow: string, opts: { prompt: string }) => {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
runAction(async () => {
|
||||
const result = await cmdThreadStart(storageRoot, workflow, opts.prompt);
|
||||
writeJson(result);
|
||||
});
|
||||
});
|
||||
|
||||
thread
|
||||
.command("step")
|
||||
.description("Execute one step")
|
||||
.argument("<thread-id>", "Thread ULID")
|
||||
.option("--agent <cmd>", "Override agent command")
|
||||
.action((threadId: string, opts: { agent: string | undefined }) => {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
runAction(async () => {
|
||||
const agentOverride = opts.agent ?? null;
|
||||
const result = await cmdThreadStep(storageRoot, threadId, agentOverride);
|
||||
writeJson(result);
|
||||
});
|
||||
});
|
||||
|
||||
thread
|
||||
.command("show")
|
||||
.description("Show thread head pointer")
|
||||
.argument("<thread-id>", "Thread ULID")
|
||||
.action((threadId: string) => {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
runAction(async () => {
|
||||
const result = await cmdThreadShow(storageRoot, threadId);
|
||||
writeJson(result);
|
||||
});
|
||||
});
|
||||
|
||||
thread
|
||||
.command("list")
|
||||
.description("List active threads")
|
||||
.option("--all", "Include archived threads")
|
||||
.action((opts: { all: boolean }) => {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
runAction(async () => {
|
||||
const result = await cmdThreadList(storageRoot, opts.all);
|
||||
writeJson(result);
|
||||
});
|
||||
});
|
||||
|
||||
thread
|
||||
.command("kill")
|
||||
.description("Terminate and archive a thread")
|
||||
.argument("<thread-id>", "Thread ULID")
|
||||
.action((threadId: string) => {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
runAction(async () => {
|
||||
const result = await cmdThreadKill(storageRoot, threadId);
|
||||
writeJson(result);
|
||||
});
|
||||
});
|
||||
|
||||
program.parseAsync(process.argv).catch((e: unknown) => {
|
||||
const message = e instanceof Error ? e.message : String(e);
|
||||
process.stderr.write(`${message}\n`);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -1,465 +0,0 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
|
||||
import { validate } from "@uncaged/json-cas";
|
||||
import { getEnvPath, loadWorkflowConfig } from "@uncaged/uwf-agent-kit";
|
||||
import { evaluate } from "@uncaged/uwf-moderator";
|
||||
import type {
|
||||
AgentAlias,
|
||||
AgentConfig,
|
||||
CasRef,
|
||||
ModeratorContext,
|
||||
StartNodePayload,
|
||||
StartOutput,
|
||||
StepContext,
|
||||
StepNodePayload,
|
||||
StepOutput,
|
||||
ThreadId,
|
||||
ThreadListItem,
|
||||
WorkflowConfig,
|
||||
WorkflowPayload,
|
||||
} from "@uncaged/uwf-protocol";
|
||||
import { generateUlid } from "@uncaged/workflow-util";
|
||||
import { config as loadDotenv } from "dotenv";
|
||||
|
||||
import {
|
||||
appendThreadHistory,
|
||||
createUwfStore,
|
||||
findThreadInHistory,
|
||||
loadThreadHistory,
|
||||
loadThreadsIndex,
|
||||
loadWorkflowRegistry,
|
||||
resolveWorkflowHash,
|
||||
saveThreadsIndex,
|
||||
type ThreadHistoryLine,
|
||||
type UwfStore,
|
||||
} from "../store.js";
|
||||
import { isCasRef } from "../validate.js";
|
||||
|
||||
const END_ROLE = "$END";
|
||||
|
||||
type ChainState = {
|
||||
startHash: CasRef;
|
||||
start: StartNodePayload;
|
||||
stepsNewestFirst: StepNodePayload[];
|
||||
headIsStart: boolean;
|
||||
};
|
||||
|
||||
export type KillOutput = {
|
||||
thread: ThreadId;
|
||||
archived: boolean;
|
||||
};
|
||||
|
||||
function fail(message: string): never {
|
||||
process.stderr.write(`${message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function resolveWorkflowCasRef(
|
||||
uwf: UwfStore,
|
||||
storageRoot: string,
|
||||
workflowId: string,
|
||||
): Promise<CasRef> {
|
||||
const registry = await loadWorkflowRegistry(storageRoot);
|
||||
const hash = resolveWorkflowHash(registry, workflowId);
|
||||
if (!isCasRef(hash)) {
|
||||
fail(`workflow not found: ${workflowId}`);
|
||||
}
|
||||
const node = uwf.store.get(hash);
|
||||
if (node === null) {
|
||||
fail(`CAS node not found: ${hash}`);
|
||||
}
|
||||
if (node.type !== uwf.schemas.workflow) {
|
||||
fail(`node ${hash} is not a Workflow (type ${node.type})`);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
function resolveWorkflowFromHead(uwf: UwfStore, head: CasRef): CasRef | null {
|
||||
const node = uwf.store.get(head);
|
||||
if (node === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (node.type === uwf.schemas.startNode) {
|
||||
const payload = node.payload as StartNodePayload;
|
||||
return payload.workflow;
|
||||
}
|
||||
|
||||
const payload = node.payload as StepNodePayload;
|
||||
if (typeof payload.start !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const startNode = uwf.store.get(payload.start);
|
||||
if (startNode === null || startNode.type !== uwf.schemas.startNode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (startNode.payload as StartNodePayload).workflow;
|
||||
}
|
||||
|
||||
export async function cmdThreadStart(
|
||||
storageRoot: string,
|
||||
workflowId: string,
|
||||
prompt: string,
|
||||
): Promise<StartOutput> {
|
||||
const uwf = await createUwfStore(storageRoot);
|
||||
const workflowHash = await resolveWorkflowCasRef(uwf, storageRoot, workflowId);
|
||||
|
||||
const threadId = generateUlid(Date.now()) as ThreadId;
|
||||
const startPayload: StartNodePayload = {
|
||||
workflow: workflowHash,
|
||||
prompt,
|
||||
};
|
||||
|
||||
const headHash = await uwf.store.put(uwf.schemas.startNode, startPayload);
|
||||
const node = uwf.store.get(headHash);
|
||||
if (node === null || !validate(uwf.store, node)) {
|
||||
fail("stored StartNode failed schema validation");
|
||||
}
|
||||
|
||||
const index = await loadThreadsIndex(storageRoot);
|
||||
index[threadId] = headHash;
|
||||
await saveThreadsIndex(storageRoot, index);
|
||||
|
||||
return { workflow: workflowHash, thread: threadId };
|
||||
}
|
||||
|
||||
export async function cmdThreadShow(storageRoot: string, threadId: ThreadId): Promise<StepOutput> {
|
||||
const index = await loadThreadsIndex(storageRoot);
|
||||
const activeHead = index[threadId];
|
||||
if (activeHead !== undefined) {
|
||||
const uwf = await createUwfStore(storageRoot);
|
||||
const workflow = resolveWorkflowFromHead(uwf, activeHead);
|
||||
if (workflow === null) {
|
||||
fail(`failed to resolve workflow from head: ${activeHead}`);
|
||||
}
|
||||
return {
|
||||
workflow,
|
||||
thread: threadId,
|
||||
head: activeHead,
|
||||
done: false,
|
||||
};
|
||||
}
|
||||
|
||||
const hist = await findThreadInHistory(storageRoot, threadId);
|
||||
if (hist !== null) {
|
||||
return {
|
||||
workflow: hist.workflow,
|
||||
thread: threadId,
|
||||
head: hist.head,
|
||||
done: true,
|
||||
};
|
||||
}
|
||||
|
||||
fail(`thread not found: ${threadId}`);
|
||||
}
|
||||
|
||||
async function threadListItemFromActive(
|
||||
uwf: UwfStore,
|
||||
threadId: ThreadId,
|
||||
head: CasRef,
|
||||
): Promise<ThreadListItem | null> {
|
||||
const workflow = resolveWorkflowFromHead(uwf, head);
|
||||
if (workflow === null) {
|
||||
return null;
|
||||
}
|
||||
return { thread: threadId, workflow, head };
|
||||
}
|
||||
|
||||
export async function cmdThreadList(
|
||||
storageRoot: string,
|
||||
includeAll: boolean,
|
||||
): Promise<ThreadListItem[]> {
|
||||
const uwf = await createUwfStore(storageRoot);
|
||||
const index = await loadThreadsIndex(storageRoot);
|
||||
const items: ThreadListItem[] = [];
|
||||
|
||||
for (const [threadId, head] of Object.entries(index)) {
|
||||
const item = await threadListItemFromActive(uwf, threadId as ThreadId, head);
|
||||
if (item !== null) {
|
||||
items.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
if (!includeAll) {
|
||||
return items;
|
||||
}
|
||||
|
||||
const activeIds = new Set(items.map((i) => i.thread));
|
||||
const history = await loadThreadHistory(storageRoot);
|
||||
for (const entry of history) {
|
||||
if (!activeIds.has(entry.thread)) {
|
||||
items.push({
|
||||
thread: entry.thread,
|
||||
workflow: entry.workflow,
|
||||
head: entry.head,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
function walkChain(uwf: UwfStore, headHash: CasRef): ChainState {
|
||||
const headNode = uwf.store.get(headHash);
|
||||
if (headNode === null) {
|
||||
fail(`CAS node not found: ${headHash}`);
|
||||
}
|
||||
|
||||
if (headNode.type === uwf.schemas.startNode) {
|
||||
return {
|
||||
startHash: headHash,
|
||||
start: headNode.payload as StartNodePayload,
|
||||
stepsNewestFirst: [],
|
||||
headIsStart: true,
|
||||
};
|
||||
}
|
||||
|
||||
if (headNode.type !== uwf.schemas.stepNode) {
|
||||
fail(`head ${headHash} is not a StartNode or StepNode`);
|
||||
}
|
||||
|
||||
const stepsNewestFirst: StepNodePayload[] = [];
|
||||
let hash: CasRef | null = headHash;
|
||||
|
||||
while (hash !== null) {
|
||||
const node = uwf.store.get(hash);
|
||||
if (node === null) {
|
||||
fail(`CAS node not found while walking chain: ${hash}`);
|
||||
}
|
||||
if (node.type !== uwf.schemas.stepNode) {
|
||||
break;
|
||||
}
|
||||
const payload = node.payload as StepNodePayload;
|
||||
stepsNewestFirst.push(payload);
|
||||
hash = payload.prev;
|
||||
}
|
||||
|
||||
const newest = stepsNewestFirst[0];
|
||||
if (newest === undefined) {
|
||||
fail(`empty step chain at head ${headHash}`);
|
||||
}
|
||||
|
||||
const startNode = uwf.store.get(newest.start);
|
||||
if (startNode === null || startNode.type !== uwf.schemas.startNode) {
|
||||
fail(`StartNode not found: ${newest.start}`);
|
||||
}
|
||||
|
||||
return {
|
||||
startHash: newest.start,
|
||||
start: startNode.payload as StartNodePayload,
|
||||
stepsNewestFirst,
|
||||
headIsStart: false,
|
||||
};
|
||||
}
|
||||
|
||||
function expandOutput(uwf: UwfStore, outputRef: CasRef): unknown {
|
||||
const node = uwf.store.get(outputRef);
|
||||
if (node === null) {
|
||||
return {};
|
||||
}
|
||||
return node.payload;
|
||||
}
|
||||
|
||||
function buildModeratorContext(uwf: UwfStore, chain: ChainState): ModeratorContext {
|
||||
const chronological = [...chain.stepsNewestFirst].reverse();
|
||||
const steps: StepContext[] = chronological.map((step) => ({
|
||||
role: step.role,
|
||||
output: expandOutput(uwf, step.output),
|
||||
detail: step.detail,
|
||||
agent: step.agent,
|
||||
}));
|
||||
return { start: chain.start, steps };
|
||||
}
|
||||
|
||||
function loadWorkflowPayload(uwf: UwfStore, workflowRef: CasRef): WorkflowPayload {
|
||||
const node = uwf.store.get(workflowRef);
|
||||
if (node === null) {
|
||||
fail(`workflow CAS node not found: ${workflowRef}`);
|
||||
}
|
||||
if (node.type !== uwf.schemas.workflow) {
|
||||
fail(`node ${workflowRef} is not a Workflow`);
|
||||
}
|
||||
return node.payload as WorkflowPayload;
|
||||
}
|
||||
|
||||
function parseAgentOverride(override: string): AgentConfig {
|
||||
const parts = override
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.filter((p) => p.length > 0);
|
||||
const command = parts[0];
|
||||
if (command === undefined) {
|
||||
fail("agent override must not be empty");
|
||||
}
|
||||
return { command, args: parts.slice(1) };
|
||||
}
|
||||
|
||||
function resolveAgentConfig(
|
||||
config: WorkflowConfig,
|
||||
workflow: WorkflowPayload,
|
||||
role: string,
|
||||
agentOverride: string | null,
|
||||
): AgentConfig {
|
||||
if (agentOverride !== null) {
|
||||
return parseAgentOverride(agentOverride);
|
||||
}
|
||||
|
||||
let alias: AgentAlias = config.defaultAgent;
|
||||
if (config.agentOverrides !== null) {
|
||||
const roleOverrides = config.agentOverrides[workflow.name];
|
||||
if (roleOverrides !== undefined && roleOverrides[role] !== undefined) {
|
||||
alias = roleOverrides[role];
|
||||
}
|
||||
}
|
||||
|
||||
const agentConfig = config.agents[alias];
|
||||
if (agentConfig === undefined) {
|
||||
fail(`unknown agent alias in config: ${alias}`);
|
||||
}
|
||||
return agentConfig;
|
||||
}
|
||||
|
||||
function spawnAgent(agent: AgentConfig, threadId: ThreadId, role: string): CasRef {
|
||||
const argv = [...agent.args, threadId, role];
|
||||
let stdout: string;
|
||||
try {
|
||||
stdout = execFileSync(agent.command, argv, {
|
||||
encoding: "utf8",
|
||||
env: process.env,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
} catch (e) {
|
||||
const err = e as NodeJS.ErrnoException & { stderr?: Buffer | string };
|
||||
const stderr =
|
||||
err.stderr === undefined
|
||||
? ""
|
||||
: typeof err.stderr === "string"
|
||||
? err.stderr
|
||||
: err.stderr.toString("utf8");
|
||||
const detail = stderr.trim() !== "" ? `: ${stderr.trim()}` : "";
|
||||
fail(`agent command failed (${agent.command})${detail}`);
|
||||
}
|
||||
|
||||
const line = stdout.trim().split("\n").pop()?.trim() ?? "";
|
||||
if (!isCasRef(line)) {
|
||||
fail(`agent stdout is not a valid CAS hash: ${line || "(empty)"}`);
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
async function archiveThread(
|
||||
storageRoot: string,
|
||||
threadId: ThreadId,
|
||||
workflow: CasRef,
|
||||
head: CasRef,
|
||||
): Promise<void> {
|
||||
const index = await loadThreadsIndex(storageRoot);
|
||||
delete index[threadId];
|
||||
await saveThreadsIndex(storageRoot, index);
|
||||
await appendThreadHistory(storageRoot, {
|
||||
thread: threadId,
|
||||
workflow,
|
||||
head,
|
||||
completedAt: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
export async function cmdThreadStep(
|
||||
storageRoot: string,
|
||||
threadId: ThreadId,
|
||||
agentOverride: string | null,
|
||||
): Promise<StepOutput> {
|
||||
const index = await loadThreadsIndex(storageRoot);
|
||||
const headHash = index[threadId];
|
||||
if (headHash === undefined) {
|
||||
fail(`thread not active: ${threadId}`);
|
||||
}
|
||||
|
||||
const uwf = await createUwfStore(storageRoot);
|
||||
const chain = walkChain(uwf, headHash);
|
||||
const workflowHash = chain.start.workflow;
|
||||
const workflow = loadWorkflowPayload(uwf, workflowHash);
|
||||
const context = buildModeratorContext(uwf, chain);
|
||||
|
||||
const nextResult = await evaluate(workflow, context);
|
||||
if (!nextResult.ok) {
|
||||
fail(nextResult.error.message);
|
||||
}
|
||||
|
||||
if (nextResult.value === END_ROLE) {
|
||||
await archiveThread(storageRoot, threadId, workflowHash, headHash);
|
||||
return {
|
||||
workflow: workflowHash,
|
||||
thread: threadId,
|
||||
head: headHash,
|
||||
done: true,
|
||||
};
|
||||
}
|
||||
|
||||
const role = nextResult.value;
|
||||
const config = await loadWorkflowConfig(storageRoot);
|
||||
const agent = resolveAgentConfig(config, workflow, role, agentOverride);
|
||||
|
||||
loadDotenv({ path: getEnvPath(storageRoot) });
|
||||
const newHead = spawnAgent(agent, threadId, role);
|
||||
|
||||
// Re-create store to pick up nodes written by the agent subprocess
|
||||
const uwfAfter = await createUwfStore(storageRoot);
|
||||
const newNode = uwfAfter.store.get(newHead);
|
||||
if (newNode === null || newNode.type !== uwfAfter.schemas.stepNode) {
|
||||
fail(`agent returned hash that is not a StepNode: ${newHead}`);
|
||||
}
|
||||
|
||||
// Reload threads index to avoid overwriting changes made by the agent subprocess
|
||||
const freshIndex = await loadThreadsIndex(storageRoot);
|
||||
freshIndex[threadId] = newHead;
|
||||
await saveThreadsIndex(storageRoot, freshIndex);
|
||||
|
||||
const chainAfter = walkChain(uwfAfter, newHead);
|
||||
const contextAfter = buildModeratorContext(uwfAfter, chainAfter);
|
||||
const afterResult = await evaluate(workflow, contextAfter);
|
||||
if (!afterResult.ok) {
|
||||
fail(afterResult.error.message);
|
||||
}
|
||||
|
||||
const done = afterResult.value === END_ROLE;
|
||||
if (done) {
|
||||
await archiveThread(storageRoot, threadId, workflowHash, newHead);
|
||||
}
|
||||
|
||||
return {
|
||||
workflow: workflowHash,
|
||||
thread: threadId,
|
||||
head: newHead,
|
||||
done,
|
||||
};
|
||||
}
|
||||
|
||||
export async function cmdThreadKill(storageRoot: string, threadId: ThreadId): Promise<KillOutput> {
|
||||
const index = await loadThreadsIndex(storageRoot);
|
||||
const head = index[threadId];
|
||||
if (head === undefined) {
|
||||
fail(`thread not active: ${threadId}`);
|
||||
}
|
||||
|
||||
const uwf = await createUwfStore(storageRoot);
|
||||
const workflow = resolveWorkflowFromHead(uwf, head);
|
||||
if (workflow === null) {
|
||||
fail(`failed to resolve workflow from head: ${head}`);
|
||||
}
|
||||
|
||||
delete index[threadId];
|
||||
await saveThreadsIndex(storageRoot, index);
|
||||
|
||||
const historyEntry: ThreadHistoryLine = {
|
||||
thread: threadId,
|
||||
workflow,
|
||||
head,
|
||||
completedAt: Date.now(),
|
||||
};
|
||||
await appendThreadHistory(storageRoot, historyEntry);
|
||||
|
||||
return { thread: threadId, archived: true };
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
import type { JSONSchema } from "@uncaged/json-cas";
|
||||
import { putSchema, validate } from "@uncaged/json-cas";
|
||||
import type { CasRef, RoleDefinition, WorkflowPayload } from "@uncaged/uwf-protocol";
|
||||
import { parse } from "yaml";
|
||||
|
||||
import {
|
||||
createUwfStore,
|
||||
findRegistryName,
|
||||
loadWorkflowRegistry,
|
||||
resolveWorkflowHash,
|
||||
saveWorkflowRegistry,
|
||||
type UwfStore,
|
||||
} from "../store.js";
|
||||
import { isCasRef, parseWorkflowPayload } from "../validate.js";
|
||||
|
||||
export type WorkflowListEntry = {
|
||||
name: string;
|
||||
hash: CasRef;
|
||||
};
|
||||
|
||||
export type WorkflowPutOutput = {
|
||||
name: string;
|
||||
hash: CasRef;
|
||||
};
|
||||
|
||||
export type WorkflowShowOutput = {
|
||||
hash: CasRef;
|
||||
name: string | null;
|
||||
type: CasRef;
|
||||
payload: WorkflowPayload;
|
||||
timestamp: number;
|
||||
};
|
||||
|
||||
function fail(message: string): never {
|
||||
process.stderr.write(`${message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function isJsonSchema(value: unknown): value is JSONSchema {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
async function resolveOutputSchemaRef(
|
||||
uwf: UwfStore,
|
||||
outputSchema: string | JSONSchema,
|
||||
): Promise<CasRef> {
|
||||
if (typeof outputSchema === "string") {
|
||||
if (!isCasRef(outputSchema)) {
|
||||
fail(`invalid outputSchema cas_ref: ${outputSchema}`);
|
||||
}
|
||||
if (!uwf.store.has(outputSchema)) {
|
||||
fail(`outputSchema not found in CAS: ${outputSchema}`);
|
||||
}
|
||||
return outputSchema;
|
||||
}
|
||||
if (!isJsonSchema(outputSchema)) {
|
||||
fail("outputSchema must be a cas_ref string or JSON Schema object");
|
||||
}
|
||||
return putSchema(uwf.store, outputSchema);
|
||||
}
|
||||
|
||||
async function materializeWorkflowPayload(
|
||||
uwf: UwfStore,
|
||||
raw: WorkflowPayload,
|
||||
): Promise<WorkflowPayload> {
|
||||
const roles: Record<string, RoleDefinition> = {};
|
||||
for (const [roleName, role] of Object.entries(raw.roles)) {
|
||||
const outputSchema = await resolveOutputSchemaRef(
|
||||
uwf,
|
||||
role.outputSchema as string | JSONSchema,
|
||||
);
|
||||
roles[roleName] = {
|
||||
description: role.description,
|
||||
systemPrompt: role.systemPrompt,
|
||||
outputSchema,
|
||||
};
|
||||
}
|
||||
return {
|
||||
name: raw.name,
|
||||
description: raw.description,
|
||||
roles,
|
||||
conditions: raw.conditions,
|
||||
graph: raw.graph,
|
||||
};
|
||||
}
|
||||
|
||||
export async function cmdWorkflowPut(
|
||||
storageRoot: string,
|
||||
filePath: string,
|
||||
): Promise<WorkflowPutOutput> {
|
||||
let text: string;
|
||||
try {
|
||||
text = await readFile(filePath, "utf8");
|
||||
} catch {
|
||||
fail(`file not found: ${filePath}`);
|
||||
}
|
||||
|
||||
let raw: unknown;
|
||||
try {
|
||||
raw = parse(text) as unknown;
|
||||
} catch (e) {
|
||||
fail(`invalid YAML: ${e instanceof Error ? e.message : String(e)}`);
|
||||
}
|
||||
|
||||
const payload = parseWorkflowPayload(raw);
|
||||
if (payload === null) {
|
||||
fail("invalid workflow YAML: expected WorkflowPayload shape");
|
||||
}
|
||||
|
||||
const uwf = await createUwfStore(storageRoot);
|
||||
const materialized = await materializeWorkflowPayload(uwf, payload);
|
||||
|
||||
const hash = await uwf.store.put(uwf.schemas.workflow, materialized);
|
||||
const node = uwf.store.get(hash);
|
||||
if (node === null || !validate(uwf.store, node)) {
|
||||
fail("stored workflow failed schema validation");
|
||||
}
|
||||
|
||||
const registry = await loadWorkflowRegistry(storageRoot);
|
||||
registry[materialized.name] = hash;
|
||||
await saveWorkflowRegistry(storageRoot, registry);
|
||||
|
||||
return { name: materialized.name, hash };
|
||||
}
|
||||
|
||||
export async function cmdWorkflowShow(
|
||||
storageRoot: string,
|
||||
id: string,
|
||||
): Promise<WorkflowShowOutput> {
|
||||
const uwf = await createUwfStore(storageRoot);
|
||||
const registry = await loadWorkflowRegistry(storageRoot);
|
||||
const hash = resolveWorkflowHash(registry, id);
|
||||
|
||||
const node = uwf.store.get(hash);
|
||||
if (node === null) {
|
||||
fail(`CAS node not found: ${hash}`);
|
||||
}
|
||||
if (node.type !== uwf.schemas.workflow) {
|
||||
fail(`node ${hash} is not a Workflow (type ${node.type})`);
|
||||
}
|
||||
|
||||
const payload = node.payload as WorkflowPayload;
|
||||
return {
|
||||
hash,
|
||||
name: findRegistryName(registry, hash),
|
||||
type: node.type,
|
||||
payload,
|
||||
timestamp: node.timestamp,
|
||||
};
|
||||
}
|
||||
|
||||
export async function cmdWorkflowList(storageRoot: string): Promise<WorkflowListEntry[]> {
|
||||
const registry = await loadWorkflowRegistry(storageRoot);
|
||||
return Object.entries(registry).map(([name, hash]) => ({ name, hash }));
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import { putSchema } from "@uncaged/json-cas";
|
||||
import {
|
||||
START_NODE_SCHEMA,
|
||||
STEP_NODE_SCHEMA,
|
||||
WORKFLOW_SCHEMA,
|
||||
} from "@uncaged/uwf-protocol";
|
||||
|
||||
export type UwfSchemaHashes = {
|
||||
workflow: Hash;
|
||||
startNode: Hash;
|
||||
stepNode: Hash;
|
||||
};
|
||||
|
||||
/**
|
||||
* Register Workflow, StartNode, and StepNode JSON Schemas in the CAS store.
|
||||
* Idempotent: safe to call on every CLI invocation.
|
||||
*/
|
||||
export async function registerUwfSchemas(store: Store): Promise<UwfSchemaHashes> {
|
||||
const [workflow, startNode, stepNode] = await Promise.all([
|
||||
putSchema(store, WORKFLOW_SCHEMA),
|
||||
putSchema(store, START_NODE_SCHEMA),
|
||||
putSchema(store, STEP_NODE_SCHEMA),
|
||||
]);
|
||||
return { workflow, startNode, stepNode };
|
||||
}
|
||||
@@ -1,212 +0,0 @@
|
||||
import { appendFile, mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import { createFsStore } from "@uncaged/json-cas-fs";
|
||||
import type { CasRef, ThreadId, ThreadListItem, ThreadsIndex } from "@uncaged/uwf-protocol";
|
||||
import { parse, stringify } from "yaml";
|
||||
|
||||
import { registerUwfSchemas, type UwfSchemaHashes } from "./schemas.js";
|
||||
|
||||
export type WorkflowRegistry = Record<string, CasRef>;
|
||||
|
||||
/** Default filesystem root for uwf data (`~/.uncaged/workflow`). */
|
||||
export function getDefaultStorageRoot(): string {
|
||||
return join(homedir(), ".uncaged", "workflow");
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve storage root.
|
||||
* Priority: `UNCAGED_WORKFLOW_STORAGE_ROOT` → `WORKFLOW_STORAGE_ROOT` → default.
|
||||
*/
|
||||
export function resolveStorageRoot(): string {
|
||||
const internal = process.env.UNCAGED_WORKFLOW_STORAGE_ROOT;
|
||||
if (internal !== undefined && internal !== "") {
|
||||
return internal;
|
||||
}
|
||||
const userOverride = process.env.WORKFLOW_STORAGE_ROOT;
|
||||
if (userOverride !== undefined && userOverride !== "") {
|
||||
return userOverride;
|
||||
}
|
||||
return getDefaultStorageRoot();
|
||||
}
|
||||
|
||||
export function getCasDir(storageRoot: string): string {
|
||||
return join(storageRoot, "cas");
|
||||
}
|
||||
|
||||
export function getRegistryPath(storageRoot: string): string {
|
||||
return join(storageRoot, "workflows.yaml");
|
||||
}
|
||||
|
||||
export function getThreadsPath(storageRoot: string): string {
|
||||
return join(storageRoot, "threads.yaml");
|
||||
}
|
||||
|
||||
export function getHistoryPath(storageRoot: string): string {
|
||||
return join(storageRoot, "history.jsonl");
|
||||
}
|
||||
|
||||
export type ThreadHistoryLine = ThreadListItem & {
|
||||
completedAt: number;
|
||||
};
|
||||
|
||||
export type UwfStore = {
|
||||
storageRoot: string;
|
||||
store: Store;
|
||||
schemas: UwfSchemaHashes;
|
||||
};
|
||||
|
||||
export async function createUwfStore(storageRoot: string): Promise<UwfStore> {
|
||||
const casDir = getCasDir(storageRoot);
|
||||
await mkdir(casDir, { recursive: true });
|
||||
const store = createFsStore(casDir);
|
||||
const schemas = await registerUwfSchemas(store);
|
||||
return { storageRoot, store, schemas };
|
||||
}
|
||||
|
||||
export async function loadWorkflowRegistry(storageRoot: string): Promise<WorkflowRegistry> {
|
||||
const path = getRegistryPath(storageRoot);
|
||||
try {
|
||||
const text = await readFile(path, "utf8");
|
||||
const raw = parse(text) as unknown;
|
||||
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
||||
return {};
|
||||
}
|
||||
const registry: WorkflowRegistry = {};
|
||||
for (const [name, hash] of Object.entries(raw as Record<string, unknown>)) {
|
||||
if (typeof hash === "string") {
|
||||
registry[name] = hash;
|
||||
}
|
||||
}
|
||||
return registry;
|
||||
} catch (e) {
|
||||
const err = e as NodeJS.ErrnoException;
|
||||
if (err.code === "ENOENT") {
|
||||
return {};
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveWorkflowRegistry(
|
||||
storageRoot: string,
|
||||
registry: WorkflowRegistry,
|
||||
): Promise<void> {
|
||||
const path = getRegistryPath(storageRoot);
|
||||
await mkdir(storageRoot, { recursive: true });
|
||||
const text = stringify(registry, { indent: 2 });
|
||||
await writeFile(path, text, "utf8");
|
||||
}
|
||||
|
||||
export function resolveWorkflowHash(registry: WorkflowRegistry, id: string): CasRef {
|
||||
return registry[id] !== undefined ? registry[id] : id;
|
||||
}
|
||||
|
||||
export function findRegistryName(registry: WorkflowRegistry, hash: Hash): string | null {
|
||||
for (const [name, h] of Object.entries(registry)) {
|
||||
if (h === hash) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function loadThreadsIndex(storageRoot: string): Promise<ThreadsIndex> {
|
||||
const path = getThreadsPath(storageRoot);
|
||||
try {
|
||||
const text = await readFile(path, "utf8");
|
||||
const raw = parse(text) as unknown;
|
||||
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
||||
return {};
|
||||
}
|
||||
const index: ThreadsIndex = {};
|
||||
for (const [threadId, head] of Object.entries(raw as Record<string, unknown>)) {
|
||||
if (typeof head === "string") {
|
||||
index[threadId as ThreadId] = head;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
} catch (e) {
|
||||
const err = e as NodeJS.ErrnoException;
|
||||
if (err.code === "ENOENT") {
|
||||
return {};
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveThreadsIndex(storageRoot: string, index: ThreadsIndex): Promise<void> {
|
||||
const path = getThreadsPath(storageRoot);
|
||||
await mkdir(storageRoot, { recursive: true });
|
||||
const text = stringify(index, { indent: 2 });
|
||||
await writeFile(path, text, "utf8");
|
||||
}
|
||||
|
||||
export async function loadThreadHistory(storageRoot: string): Promise<ThreadHistoryLine[]> {
|
||||
const path = getHistoryPath(storageRoot);
|
||||
try {
|
||||
const text = await readFile(path, "utf8");
|
||||
const lines: ThreadHistoryLine[] = [];
|
||||
for (const line of text.split("\n")) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed === "") {
|
||||
continue;
|
||||
}
|
||||
let raw: unknown;
|
||||
try {
|
||||
raw = JSON.parse(trimmed) as unknown;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
||||
continue;
|
||||
}
|
||||
const rec = raw as Record<string, unknown>;
|
||||
const thread = rec.thread;
|
||||
const workflow = rec.workflow;
|
||||
const head = rec.head;
|
||||
const completedAt = rec.completedAt;
|
||||
if (
|
||||
typeof thread === "string" &&
|
||||
typeof workflow === "string" &&
|
||||
typeof head === "string" &&
|
||||
typeof completedAt === "number"
|
||||
) {
|
||||
lines.push({ thread: thread as ThreadId, workflow, head, completedAt });
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
} catch (e) {
|
||||
const err = e as NodeJS.ErrnoException;
|
||||
if (err.code === "ENOENT") {
|
||||
return [];
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
export async function findThreadInHistory(
|
||||
storageRoot: string,
|
||||
threadId: ThreadId,
|
||||
): Promise<ThreadHistoryLine | null> {
|
||||
const history = await loadThreadHistory(storageRoot);
|
||||
for (let i = history.length - 1; i >= 0; i--) {
|
||||
const entry = history[i];
|
||||
if (entry !== undefined && entry.thread === threadId) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function appendThreadHistory(
|
||||
storageRoot: string,
|
||||
entry: ThreadHistoryLine,
|
||||
): Promise<void> {
|
||||
const path = getHistoryPath(storageRoot);
|
||||
await mkdir(storageRoot, { recursive: true });
|
||||
const line = `${JSON.stringify(entry)}\n`;
|
||||
await appendFile(path, line, "utf8");
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
import type { CasRef, WorkflowPayload } from "@uncaged/uwf-protocol";
|
||||
|
||||
const CAS_REF_PATTERN = /^[0-9A-HJKMNP-TV-Z]{13}$/;
|
||||
|
||||
export function isCasRef(value: string): value is CasRef {
|
||||
return CAS_REF_PATTERN.test(value);
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function isRoleDefinition(value: unknown): boolean {
|
||||
if (!isRecord(value)) {
|
||||
return false;
|
||||
}
|
||||
const outputSchema = value.outputSchema;
|
||||
const schemaOk =
|
||||
typeof outputSchema === "string" ||
|
||||
(isRecord(outputSchema) && typeof outputSchema.type === "string");
|
||||
return (
|
||||
typeof value.description === "string" && typeof value.systemPrompt === "string" && schemaOk
|
||||
);
|
||||
}
|
||||
|
||||
function isConditionDefinition(value: unknown): boolean {
|
||||
if (!isRecord(value)) {
|
||||
return false;
|
||||
}
|
||||
return typeof value.description === "string" && typeof value.expression === "string";
|
||||
}
|
||||
|
||||
function isTransition(value: unknown): boolean {
|
||||
if (!isRecord(value)) {
|
||||
return false;
|
||||
}
|
||||
const condition = value.condition;
|
||||
return typeof value.role === "string" && (condition === null || typeof condition === "string");
|
||||
}
|
||||
|
||||
function isStringRecord(value: unknown, itemCheck: (item: unknown) => boolean): boolean {
|
||||
if (!isRecord(value)) {
|
||||
return false;
|
||||
}
|
||||
return Object.values(value).every(itemCheck);
|
||||
}
|
||||
|
||||
function isGraph(value: unknown): boolean {
|
||||
if (!isRecord(value)) {
|
||||
return false;
|
||||
}
|
||||
return Object.values(value).every(
|
||||
(transitions) => Array.isArray(transitions) && transitions.every((t) => isTransition(t)),
|
||||
);
|
||||
}
|
||||
|
||||
/** Validate YAML-parsed workflow document shape (outputSchema may be inline JSON Schema). */
|
||||
export function parseWorkflowPayload(raw: unknown): WorkflowPayload | null {
|
||||
if (!isRecord(raw)) {
|
||||
return null;
|
||||
}
|
||||
if (typeof raw.name !== "string" || typeof raw.description !== "string") {
|
||||
return null;
|
||||
}
|
||||
if (
|
||||
!isStringRecord(raw.roles, isRoleDefinition) ||
|
||||
!isStringRecord(raw.conditions, isConditionDefinition) ||
|
||||
!isGraph(raw.graph)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return raw as WorkflowPayload;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [
|
||||
{ "path": "../uwf-protocol" },
|
||||
{ "path": "../uwf-moderator" },
|
||||
{ "path": "../uwf-agent-kit" }
|
||||
]
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
{
|
||||
"name": "@uncaged/uwf-agent-hermes",
|
||||
"version": "0.1.0",
|
||||
"files": [
|
||||
"src",
|
||||
"dist",
|
||||
"package.json"
|
||||
],
|
||||
"type": "module",
|
||||
"bin": {
|
||||
"uwf-hermes": "./src/cli.ts"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"bun": "./src/index.ts",
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "bun test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@uncaged/uwf-agent-kit": "workspace:^"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.8.3"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import { createHermesAgent } from "./hermes.js";
|
||||
|
||||
const main = createHermesAgent();
|
||||
void main();
|
||||
@@ -1,90 +0,0 @@
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
import { type AgentContext, createAgent } from "@uncaged/uwf-agent-kit";
|
||||
|
||||
const HERMES_COMMAND = "hermes";
|
||||
const HERMES_MAX_TURNS = 90;
|
||||
|
||||
function buildHistorySummary(history: AgentContext["history"]): string {
|
||||
if (history.length === 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const lines: string[] = ["## Previous Steps"];
|
||||
for (let i = 0; i < history.length; i++) {
|
||||
const step = history[i];
|
||||
if (step === undefined) {
|
||||
continue;
|
||||
}
|
||||
lines.push("");
|
||||
lines.push(`### Step ${i + 1}: ${step.role}`);
|
||||
lines.push(`Output: ${JSON.stringify(step.output)}`);
|
||||
lines.push(`Agent: ${step.agent}`);
|
||||
}
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
/** Assemble system prompt, task, and prior step outputs for Hermes. */
|
||||
export function buildHermesPrompt(ctx: AgentContext): string {
|
||||
const parts: string[] = [ctx.systemPrompt, "", "## Task", ctx.prompt];
|
||||
const historyBlock = buildHistorySummary(ctx.history);
|
||||
if (historyBlock !== "") {
|
||||
parts.push("", historyBlock);
|
||||
}
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
function spawnHermesChat(prompt: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const args = [
|
||||
"chat",
|
||||
"-q",
|
||||
prompt,
|
||||
"--yolo",
|
||||
"--max-turns",
|
||||
String(HERMES_MAX_TURNS),
|
||||
"--quiet",
|
||||
];
|
||||
const child = spawn(HERMES_COMMAND, args, {
|
||||
env: process.env,
|
||||
shell: false,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
child.stdout?.on("data", (chunk: Buffer) => {
|
||||
stdout += chunk.toString();
|
||||
});
|
||||
child.stderr?.on("data", (chunk: Buffer) => {
|
||||
stderr += chunk.toString();
|
||||
});
|
||||
|
||||
child.on("error", (cause) => {
|
||||
const message = cause instanceof Error ? cause.message : String(cause);
|
||||
reject(new Error(`hermes spawn failed: ${message}`));
|
||||
});
|
||||
|
||||
child.on("close", (code) => {
|
||||
if (code === 0) {
|
||||
resolve(stdout);
|
||||
return;
|
||||
}
|
||||
const detail = stderr.trim() !== "" ? ` stderr=${stderr.trim()}` : "";
|
||||
reject(new Error(`hermes exited with code ${code ?? "null"}${detail}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function runHermes(ctx: AgentContext): Promise<string> {
|
||||
const fullPrompt = buildHermesPrompt(ctx);
|
||||
return spawnHermesChat(fullPrompt);
|
||||
}
|
||||
|
||||
/** Agent CLI factory: parses argv, runs Hermes, extracts output, writes StepNode. */
|
||||
export function createHermesAgent(): () => Promise<void> {
|
||||
return createAgent({
|
||||
name: "hermes",
|
||||
run: runHermes,
|
||||
});
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { buildHermesPrompt, createHermesAgent } from "./hermes.js";
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [{ "path": "../uwf-agent-kit" }]
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import type { WorkflowConfig } from "@uncaged/uwf-protocol";
|
||||
import { resolveExtractModelAlias } from "../src/extract.js";
|
||||
|
||||
function baseConfig(overrides: Partial<WorkflowConfig> = {}): WorkflowConfig {
|
||||
return {
|
||||
providers: {},
|
||||
models: {
|
||||
sonnet: { provider: "openrouter", name: "anthropic/claude-sonnet-4" },
|
||||
"gpt4o-mini": { provider: "openai", name: "gpt-4o-mini" },
|
||||
},
|
||||
agents: {},
|
||||
defaultAgent: "hermes",
|
||||
agentOverrides: null,
|
||||
defaultModel: "sonnet",
|
||||
modelOverrides: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("resolveExtractModelAlias", () => {
|
||||
test("uses modelOverrides.extract when set", () => {
|
||||
const config = baseConfig({
|
||||
modelOverrides: { extract: "gpt4o-mini" },
|
||||
});
|
||||
expect(resolveExtractModelAlias(config)).toBe("gpt4o-mini");
|
||||
});
|
||||
|
||||
test("falls back to models.extract alias when present", () => {
|
||||
const config = baseConfig({
|
||||
models: {
|
||||
extract: { provider: "openai", name: "gpt-4o-mini" },
|
||||
sonnet: { provider: "openrouter", name: "anthropic/claude-sonnet-4" },
|
||||
},
|
||||
});
|
||||
expect(resolveExtractModelAlias(config)).toBe("extract");
|
||||
});
|
||||
|
||||
test("falls back to defaultModel", () => {
|
||||
expect(resolveExtractModelAlias(baseConfig())).toBe("sonnet");
|
||||
});
|
||||
});
|
||||
@@ -1,33 +0,0 @@
|
||||
{
|
||||
"name": "@uncaged/uwf-agent-kit",
|
||||
"version": "0.1.0",
|
||||
"files": [
|
||||
"src",
|
||||
"dist",
|
||||
"package.json"
|
||||
],
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"bun": "./src/index.ts",
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "bun test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@uncaged/json-cas": "^0.1.1",
|
||||
"@uncaged/json-cas-fs": "^0.1.1",
|
||||
"@uncaged/uwf-protocol": "workspace:^",
|
||||
"dotenv": "^16.6.1",
|
||||
"yaml": "^2.8.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.8.3"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
@@ -1,199 +0,0 @@
|
||||
import type {
|
||||
CasRef,
|
||||
StartNodePayload,
|
||||
StepContext,
|
||||
StepNodePayload,
|
||||
ThreadId,
|
||||
} from "@uncaged/uwf-protocol";
|
||||
import { createAgentStore, loadThreadsIndex, resolveStorageRoot } from "./storage.js";
|
||||
import type { AgentContext } from "./types.js";
|
||||
|
||||
type ChainState = {
|
||||
startHash: CasRef;
|
||||
start: StartNodePayload;
|
||||
stepsNewestFirst: StepNodePayload[];
|
||||
headIsStart: boolean;
|
||||
};
|
||||
|
||||
function fail(message: string): never {
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
function walkChain(
|
||||
store: Awaited<ReturnType<typeof createAgentStore>>["store"],
|
||||
schemas: Awaited<ReturnType<typeof createAgentStore>>["schemas"],
|
||||
headHash: CasRef,
|
||||
): ChainState {
|
||||
const headNode = store.get(headHash);
|
||||
if (headNode === null) {
|
||||
fail(`CAS node not found: ${headHash}`);
|
||||
}
|
||||
|
||||
if (headNode.type === schemas.startNode) {
|
||||
return {
|
||||
startHash: headHash,
|
||||
start: headNode.payload as StartNodePayload,
|
||||
stepsNewestFirst: [],
|
||||
headIsStart: true,
|
||||
};
|
||||
}
|
||||
|
||||
if (headNode.type !== schemas.stepNode) {
|
||||
fail(`head ${headHash} is not a StartNode or StepNode`);
|
||||
}
|
||||
|
||||
const stepsNewestFirst: StepNodePayload[] = [];
|
||||
let hash: CasRef | null = headHash;
|
||||
|
||||
while (hash !== null) {
|
||||
const node = store.get(hash);
|
||||
if (node === null) {
|
||||
fail(`CAS node not found while walking chain: ${hash}`);
|
||||
}
|
||||
if (node.type !== schemas.stepNode) {
|
||||
break;
|
||||
}
|
||||
const payload = node.payload as StepNodePayload;
|
||||
stepsNewestFirst.push(payload);
|
||||
hash = payload.prev;
|
||||
}
|
||||
|
||||
const newest = stepsNewestFirst[0];
|
||||
if (newest === undefined) {
|
||||
fail(`empty step chain at head ${headHash}`);
|
||||
}
|
||||
|
||||
const startNode = store.get(newest.start);
|
||||
if (startNode === null || startNode.type !== schemas.startNode) {
|
||||
fail(`StartNode not found: ${newest.start}`);
|
||||
}
|
||||
|
||||
return {
|
||||
startHash: newest.start,
|
||||
start: startNode.payload as StartNodePayload,
|
||||
stepsNewestFirst,
|
||||
headIsStart: false,
|
||||
};
|
||||
}
|
||||
|
||||
function expandOutput(
|
||||
store: Awaited<ReturnType<typeof createAgentStore>>["store"],
|
||||
outputRef: CasRef,
|
||||
): unknown {
|
||||
const node = store.get(outputRef);
|
||||
if (node === null) {
|
||||
return {};
|
||||
}
|
||||
return node.payload;
|
||||
}
|
||||
|
||||
async function buildHistory(
|
||||
store: Awaited<ReturnType<typeof createAgentStore>>["store"],
|
||||
stepsNewestFirst: StepNodePayload[],
|
||||
): Promise<StepContext[]> {
|
||||
const chronological = [...stepsNewestFirst].reverse();
|
||||
const history: StepContext[] = [];
|
||||
for (const step of chronological) {
|
||||
history.push({
|
||||
role: step.role,
|
||||
output: expandOutput(store, step.output),
|
||||
detail: step.detail,
|
||||
agent: step.agent,
|
||||
});
|
||||
}
|
||||
return history;
|
||||
}
|
||||
|
||||
async function loadWorkflow(
|
||||
store: Awaited<ReturnType<typeof createAgentStore>>["store"],
|
||||
schemas: Awaited<ReturnType<typeof createAgentStore>>["schemas"],
|
||||
workflowRef: CasRef,
|
||||
) {
|
||||
const node = store.get(workflowRef);
|
||||
if (node === null) {
|
||||
fail(`workflow CAS node not found: ${workflowRef}`);
|
||||
}
|
||||
if (node.type !== schemas.workflow) {
|
||||
fail(`node ${workflowRef} is not a Workflow`);
|
||||
}
|
||||
return node.payload as AgentContext["workflow"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Build agent execution context from thread head in threads.yaml.
|
||||
* Walks the CAS chain from head to StartNode and expands step outputs.
|
||||
*/
|
||||
export async function buildContext(threadId: ThreadId, role: string): Promise<AgentContext> {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
const agentStore = await createAgentStore(storageRoot);
|
||||
const { store, schemas } = agentStore;
|
||||
|
||||
const index = await loadThreadsIndex(storageRoot);
|
||||
const headHash = index[threadId];
|
||||
if (headHash === undefined) {
|
||||
fail(`thread not found in threads.yaml: ${threadId}`);
|
||||
}
|
||||
|
||||
const chain = walkChain(store, schemas, headHash);
|
||||
const workflow = await loadWorkflow(store, schemas, chain.start.workflow);
|
||||
const roleDef = workflow.roles[role];
|
||||
if (roleDef === undefined) {
|
||||
fail(`unknown role "${role}" in workflow "${workflow.name}"`);
|
||||
}
|
||||
|
||||
const history = await buildHistory(store, chain.stepsNewestFirst);
|
||||
|
||||
return {
|
||||
threadId,
|
||||
role,
|
||||
systemPrompt: roleDef.systemPrompt,
|
||||
prompt: chain.start.prompt,
|
||||
history,
|
||||
workflow,
|
||||
};
|
||||
}
|
||||
|
||||
export type BuildContextMeta = {
|
||||
storageRoot: string;
|
||||
store: Awaited<ReturnType<typeof createAgentStore>>["store"];
|
||||
schemas: Awaited<ReturnType<typeof createAgentStore>>["schemas"];
|
||||
headHash: CasRef;
|
||||
chain: ChainState;
|
||||
};
|
||||
|
||||
/**
|
||||
* Same as {@link buildContext} but also returns chain metadata for writing the next StepNode.
|
||||
*/
|
||||
export async function buildContextWithMeta(
|
||||
threadId: ThreadId,
|
||||
role: string,
|
||||
): Promise<AgentContext & { meta: BuildContextMeta }> {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
const agentStore = await createAgentStore(storageRoot);
|
||||
const { store, schemas } = agentStore;
|
||||
|
||||
const index = await loadThreadsIndex(storageRoot);
|
||||
const headHash = index[threadId];
|
||||
if (headHash === undefined) {
|
||||
fail(`thread not found in threads.yaml: ${threadId}`);
|
||||
}
|
||||
|
||||
const chain = walkChain(store, schemas, headHash);
|
||||
const workflow = await loadWorkflow(store, schemas, chain.start.workflow);
|
||||
const roleDef = workflow.roles[role];
|
||||
if (roleDef === undefined) {
|
||||
fail(`unknown role "${role}" in workflow "${workflow.name}"`);
|
||||
}
|
||||
|
||||
const history = await buildHistory(store, chain.stepsNewestFirst);
|
||||
|
||||
return {
|
||||
threadId,
|
||||
role,
|
||||
systemPrompt: roleDef.systemPrompt,
|
||||
prompt: chain.start.prompt,
|
||||
history,
|
||||
workflow,
|
||||
meta: { storageRoot, store, schemas, headHash, chain },
|
||||
};
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
import { getSchema, validate } from "@uncaged/json-cas";
|
||||
|
||||
import type { CasRef, ModelAlias, WorkflowConfig } from "@uncaged/uwf-protocol";
|
||||
import { config as loadDotenv } from "dotenv";
|
||||
import { createAgentStore, getEnvPath, resolveStorageRoot } from "./storage.js";
|
||||
|
||||
export type ResolvedLlmProvider = {
|
||||
baseUrl: string;
|
||||
apiKey: string;
|
||||
model: string;
|
||||
};
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
/** Resolve model alias for extract: modelOverrides.extract → models.extract → defaultModel. */
|
||||
export function resolveExtractModelAlias(config: WorkflowConfig): ModelAlias {
|
||||
const fromOverride = config.modelOverrides?.extract ?? null;
|
||||
if (fromOverride !== null) {
|
||||
return fromOverride;
|
||||
}
|
||||
if (config.models.extract !== undefined) {
|
||||
return "extract";
|
||||
}
|
||||
if (config.models.default !== undefined) {
|
||||
return "default";
|
||||
}
|
||||
return config.defaultModel;
|
||||
}
|
||||
|
||||
export function resolveModel(config: WorkflowConfig, alias: ModelAlias): ResolvedLlmProvider {
|
||||
const modelEntry = config.models[alias];
|
||||
if (modelEntry === undefined) {
|
||||
throw new Error(`unknown model alias: ${alias}`);
|
||||
}
|
||||
const providerEntry = config.providers[modelEntry.provider];
|
||||
if (providerEntry === undefined) {
|
||||
throw new Error(`unknown provider "${modelEntry.provider}" for model "${alias}"`);
|
||||
}
|
||||
const apiKey = process.env[providerEntry.apiKeyEnv];
|
||||
if (apiKey === undefined || apiKey === "") {
|
||||
throw new Error(`missing API key env var: ${providerEntry.apiKeyEnv}`);
|
||||
}
|
||||
return {
|
||||
baseUrl: providerEntry.baseUrl,
|
||||
apiKey,
|
||||
model: modelEntry.name,
|
||||
};
|
||||
}
|
||||
|
||||
function chatUrl(baseUrl: string): string {
|
||||
const trimmed = baseUrl.replace(/\/+$/, "");
|
||||
return `${trimmed}/chat/completions`;
|
||||
}
|
||||
|
||||
function extractJsonFromAssistantText(text: string): unknown {
|
||||
const trimmed = text.trim();
|
||||
const fenceMatch = /^```(?:json)?\s*([\s\S]*?)```$/m.exec(trimmed);
|
||||
const candidate = fenceMatch !== null ? fenceMatch[1].trim() : trimmed;
|
||||
return JSON.parse(candidate) as unknown;
|
||||
}
|
||||
|
||||
function parseAssistantText(parsed: unknown): string {
|
||||
if (!isRecord(parsed)) {
|
||||
throw new Error("LLM response is not an object");
|
||||
}
|
||||
const choices = parsed.choices;
|
||||
if (!Array.isArray(choices) || choices.length === 0) {
|
||||
throw new Error("LLM response has no choices");
|
||||
}
|
||||
const c0 = choices[0];
|
||||
if (!isRecord(c0)) {
|
||||
throw new Error("LLM choice is not an object");
|
||||
}
|
||||
const messageObj = c0.message;
|
||||
if (!isRecord(messageObj)) {
|
||||
throw new Error("LLM message is not an object");
|
||||
}
|
||||
const content = messageObj.content;
|
||||
if (typeof content !== "string") {
|
||||
throw new Error("LLM message has no text content");
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
async function chatCompletionText(
|
||||
provider: ResolvedLlmProvider,
|
||||
messages: Array<{ role: "system" | "user"; content: string }>,
|
||||
): Promise<string> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(chatUrl(provider.baseUrl), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${provider.apiKey}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: provider.model,
|
||||
messages,
|
||||
response_format: { type: "json_object" },
|
||||
}),
|
||||
});
|
||||
} catch (cause) {
|
||||
const message = cause instanceof Error ? cause.message : String(cause);
|
||||
throw new Error(`LLM network error: ${message}`);
|
||||
}
|
||||
|
||||
const responseText = await response.text();
|
||||
if (!response.ok) {
|
||||
throw new Error(`LLM HTTP ${response.status}: ${responseText.slice(0, 2000)}`);
|
||||
}
|
||||
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(responseText) as unknown;
|
||||
} catch (cause) {
|
||||
const message = cause instanceof Error ? cause.message : String(cause);
|
||||
throw new Error(`LLM invalid JSON response: ${message}`);
|
||||
}
|
||||
|
||||
return parseAssistantText(parsed);
|
||||
}
|
||||
|
||||
export type ExtractResult = {
|
||||
value: unknown;
|
||||
hash: CasRef;
|
||||
};
|
||||
|
||||
/**
|
||||
* Call an OpenAI-compatible LLM to extract structured output matching outputSchema.
|
||||
* Loads config.yaml and .env from the workflow storage root.
|
||||
*/
|
||||
export async function extract(
|
||||
rawOutput: string,
|
||||
outputSchema: CasRef,
|
||||
config: WorkflowConfig,
|
||||
): Promise<ExtractResult> {
|
||||
const storageRoot = resolveStorageRoot();
|
||||
loadDotenv({ path: getEnvPath(storageRoot) });
|
||||
|
||||
const { store } = await createAgentStore(storageRoot);
|
||||
const schema = getSchema(store, outputSchema);
|
||||
if (schema === null) {
|
||||
throw new Error(`output schema not found in CAS: ${outputSchema}`);
|
||||
}
|
||||
|
||||
const modelAlias = resolveExtractModelAlias(config);
|
||||
const provider = resolveModel(config, modelAlias);
|
||||
|
||||
const schemaText = JSON.stringify(schema, null, 2);
|
||||
const assistantText = await chatCompletionText(provider, [
|
||||
{
|
||||
role: "system",
|
||||
content:
|
||||
"Extract structured data from the agent output. Reply with a single JSON object only, no markdown or prose. The JSON must validate against this JSON Schema:\n" +
|
||||
schemaText,
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: rawOutput,
|
||||
},
|
||||
]);
|
||||
|
||||
let structured: unknown;
|
||||
try {
|
||||
structured = extractJsonFromAssistantText(assistantText);
|
||||
} catch (cause) {
|
||||
const message = cause instanceof Error ? cause.message : String(cause);
|
||||
throw new Error(`failed to parse extracted JSON: ${message}`);
|
||||
}
|
||||
|
||||
const outputHash = await store.put(outputSchema, structured);
|
||||
const node = store.get(outputHash);
|
||||
if (node === null || !validate(store, node)) {
|
||||
throw new Error("extracted output failed JSON Schema validation");
|
||||
}
|
||||
|
||||
return { value: structured, hash: outputHash };
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
export type { BuildContextMeta } from "./context.js";
|
||||
export { buildContext, buildContextWithMeta } from "./context.js";
|
||||
export { getConfigPath, getEnvPath, loadWorkflowConfig } from "./storage.js";
|
||||
export type { ExtractResult, ResolvedLlmProvider } from "./extract.js";
|
||||
export {
|
||||
extract,
|
||||
resolveExtractModelAlias,
|
||||
resolveModel,
|
||||
} from "./extract.js";
|
||||
export { createAgent } from "./run.js";
|
||||
export type { AgentContext, AgentOptions, AgentRunFn } from "./types.js";
|
||||
@@ -1,135 +0,0 @@
|
||||
import { validate } from "@uncaged/json-cas";
|
||||
import type { CasRef, StepNodePayload, ThreadId } from "@uncaged/uwf-protocol";
|
||||
import { config as loadDotenv } from "dotenv";
|
||||
|
||||
import { buildContextWithMeta } from "./context.js";
|
||||
import { extract } from "./extract.js";
|
||||
import type { AgentStore } from "./storage.js";
|
||||
import { getEnvPath, loadWorkflowConfig, resolveStorageRoot } from "./storage.js";
|
||||
import type { AgentContext, AgentOptions } from "./types.js";
|
||||
|
||||
function fail(message: string): never {
|
||||
process.stderr.write(`${message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function agentLabel(name: string): string {
|
||||
if (name.startsWith("uwf-")) {
|
||||
return name;
|
||||
}
|
||||
return `uwf-${name}`;
|
||||
}
|
||||
|
||||
function parseArgv(argv: string[]): { threadId: ThreadId; role: string } {
|
||||
const threadId = argv[2];
|
||||
const role = argv[3];
|
||||
if (threadId === undefined || threadId === "") {
|
||||
fail("usage: <agent-cli> <thread-id> <role>");
|
||||
}
|
||||
if (role === undefined || role === "") {
|
||||
fail("usage: <agent-cli> <thread-id> <role>");
|
||||
}
|
||||
return { threadId: threadId as ThreadId, role };
|
||||
}
|
||||
|
||||
function runWithMessage<T>(label: string, fn: () => Promise<T>): Promise<T> {
|
||||
return fn().catch((e: unknown) => {
|
||||
const message = e instanceof Error ? e.message : String(e);
|
||||
fail(`${label}: ${message}`);
|
||||
});
|
||||
}
|
||||
|
||||
async function writeStepNode(options: {
|
||||
store: AgentStore["store"];
|
||||
schemas: AgentStore["schemas"];
|
||||
startHash: CasRef;
|
||||
prevHash: CasRef | null;
|
||||
role: string;
|
||||
outputHash: CasRef;
|
||||
detailHash: CasRef;
|
||||
agentName: string;
|
||||
}): Promise<CasRef> {
|
||||
const payload: StepNodePayload = {
|
||||
start: options.startHash,
|
||||
prev: options.prevHash,
|
||||
role: options.role,
|
||||
output: options.outputHash,
|
||||
detail: options.detailHash,
|
||||
agent: options.agentName,
|
||||
};
|
||||
const hash = await options.store.put(options.schemas.stepNode, payload);
|
||||
const node = options.store.get(hash);
|
||||
if (node === null || !validate(options.store, node)) {
|
||||
fail("stored StepNode failed schema validation");
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
async function runAgent(options: AgentOptions, ctx: AgentContext): Promise<string> {
|
||||
return runWithMessage("agent run failed", () => options.run(ctx));
|
||||
}
|
||||
|
||||
async function extractOutput(
|
||||
rawOutput: string,
|
||||
outputSchema: CasRef,
|
||||
storageRoot: string,
|
||||
): Promise<CasRef> {
|
||||
const config = await runWithMessage("failed to load config", () =>
|
||||
loadWorkflowConfig(storageRoot),
|
||||
);
|
||||
const extracted = await runWithMessage("extract failed", () =>
|
||||
extract(rawOutput, outputSchema, config),
|
||||
);
|
||||
return extracted.hash;
|
||||
}
|
||||
|
||||
async function persistStep(options: {
|
||||
ctx: Awaited<ReturnType<typeof buildContextWithMeta>>;
|
||||
rawOutput: string;
|
||||
outputHash: CasRef;
|
||||
agentName: string;
|
||||
}): Promise<CasRef> {
|
||||
const { store, schemas, chain, headHash } = options.ctx.meta;
|
||||
const detailHash = await store.put(null, options.rawOutput);
|
||||
return writeStepNode({
|
||||
store,
|
||||
schemas,
|
||||
startHash: chain.startHash,
|
||||
prevHash: chain.headIsStart ? null : headHash,
|
||||
role: options.ctx.role,
|
||||
outputHash: options.outputHash,
|
||||
detailHash,
|
||||
agentName: options.agentName,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an agent CLI entrypoint.
|
||||
* Parses argv (`<thread-id> <role>`), runs the agent, extracts structured output,
|
||||
* writes StepNode to CAS, and prints the new node hash to stdout.
|
||||
*/
|
||||
export function createAgent(options: AgentOptions): () => Promise<void> {
|
||||
return async function main(): Promise<void> {
|
||||
const { threadId, role } = parseArgv(process.argv);
|
||||
const storageRoot = resolveStorageRoot();
|
||||
loadDotenv({ path: getEnvPath(storageRoot) });
|
||||
|
||||
const ctx = await runWithMessage("context", () => buildContextWithMeta(threadId, role));
|
||||
|
||||
const roleDef = ctx.workflow.roles[role];
|
||||
if (roleDef === undefined) {
|
||||
fail(`unknown role: ${role}`);
|
||||
}
|
||||
|
||||
const rawOutput = await runAgent(options, ctx);
|
||||
const outputHash = await extractOutput(rawOutput, roleDef.outputSchema, storageRoot);
|
||||
const stepHash = await persistStep({
|
||||
ctx,
|
||||
rawOutput,
|
||||
outputHash,
|
||||
agentName: agentLabel(options.name),
|
||||
});
|
||||
|
||||
process.stdout.write(`${stepHash}\n`);
|
||||
};
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import { putSchema } from "@uncaged/json-cas";
|
||||
import {
|
||||
START_NODE_SCHEMA,
|
||||
STEP_NODE_SCHEMA,
|
||||
WORKFLOW_SCHEMA,
|
||||
} from "@uncaged/uwf-protocol";
|
||||
|
||||
export type UwfAgentSchemaHashes = {
|
||||
workflow: Hash;
|
||||
startNode: Hash;
|
||||
stepNode: Hash;
|
||||
};
|
||||
|
||||
/**
|
||||
* Register Workflow, StartNode, and StepNode JSON Schemas in the CAS store.
|
||||
* Idempotent: safe to call on every agent invocation.
|
||||
*/
|
||||
export async function registerAgentSchemas(store: Store): Promise<UwfAgentSchemaHashes> {
|
||||
const [workflow, startNode, stepNode] = await Promise.all([
|
||||
putSchema(store, WORKFLOW_SCHEMA),
|
||||
putSchema(store, START_NODE_SCHEMA),
|
||||
putSchema(store, STEP_NODE_SCHEMA),
|
||||
]);
|
||||
return { workflow, startNode, stepNode };
|
||||
}
|
||||
@@ -1,227 +0,0 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import type { Store } from "@uncaged/json-cas";
|
||||
import { createFsStore } from "@uncaged/json-cas-fs";
|
||||
import type {
|
||||
AgentAlias,
|
||||
AgentConfig,
|
||||
ModelAlias,
|
||||
ModelConfig,
|
||||
ProviderAlias,
|
||||
ProviderConfig,
|
||||
Scenario,
|
||||
ThreadId,
|
||||
ThreadsIndex,
|
||||
WorkflowConfig,
|
||||
WorkflowName,
|
||||
} from "@uncaged/uwf-protocol";
|
||||
import { parse } from "yaml";
|
||||
|
||||
import { registerAgentSchemas } from "./schemas.js";
|
||||
|
||||
/** Default filesystem root for uwf data (`~/.uncaged/workflow`). */
|
||||
export function getDefaultStorageRoot(): string {
|
||||
return join(homedir(), ".uncaged", "workflow");
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve storage root.
|
||||
* Priority: `UNCAGED_WORKFLOW_STORAGE_ROOT` → `WORKFLOW_STORAGE_ROOT` → default.
|
||||
*/
|
||||
export function resolveStorageRoot(): string {
|
||||
const internal = process.env.UNCAGED_WORKFLOW_STORAGE_ROOT;
|
||||
if (internal !== undefined && internal !== "") {
|
||||
return internal;
|
||||
}
|
||||
const userOverride = process.env.WORKFLOW_STORAGE_ROOT;
|
||||
if (userOverride !== undefined && userOverride !== "") {
|
||||
return userOverride;
|
||||
}
|
||||
return getDefaultStorageRoot();
|
||||
}
|
||||
|
||||
export function getCasDir(storageRoot: string): string {
|
||||
return join(storageRoot, "cas");
|
||||
}
|
||||
|
||||
export function getConfigPath(storageRoot: string): string {
|
||||
return join(storageRoot, "config.yaml");
|
||||
}
|
||||
|
||||
export function getEnvPath(storageRoot: string): string {
|
||||
return join(storageRoot, ".env");
|
||||
}
|
||||
|
||||
export function getThreadsPath(storageRoot: string): string {
|
||||
return join(storageRoot, "threads.yaml");
|
||||
}
|
||||
|
||||
export type AgentStore = {
|
||||
storageRoot: string;
|
||||
store: Store;
|
||||
schemas: Awaited<ReturnType<typeof registerAgentSchemas>>;
|
||||
};
|
||||
|
||||
export async function createAgentStore(storageRoot: string): Promise<AgentStore> {
|
||||
const store = createFsStore(getCasDir(storageRoot));
|
||||
const schemas = await registerAgentSchemas(store);
|
||||
return { storageRoot, store, schemas };
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function normalizeProviders(raw: unknown): Record<ProviderAlias, ProviderConfig> {
|
||||
if (!isRecord(raw)) {
|
||||
throw new Error("config.providers must be a mapping");
|
||||
}
|
||||
const providers: Record<ProviderAlias, ProviderConfig> = {};
|
||||
for (const [name, entry] of Object.entries(raw)) {
|
||||
if (!isRecord(entry)) {
|
||||
throw new Error(`config.providers.${name} must be a mapping`);
|
||||
}
|
||||
const baseUrl = entry.baseUrl;
|
||||
const apiKeyEnv = entry.apiKeyEnv;
|
||||
if (typeof baseUrl !== "string" || typeof apiKeyEnv !== "string") {
|
||||
throw new Error(`config.providers.${name} requires baseUrl and apiKeyEnv`);
|
||||
}
|
||||
providers[name] = { baseUrl, apiKeyEnv };
|
||||
}
|
||||
return providers;
|
||||
}
|
||||
|
||||
function normalizeModels(raw: unknown): Record<ModelAlias, ModelConfig> {
|
||||
if (!isRecord(raw)) {
|
||||
throw new Error("config.models must be a mapping");
|
||||
}
|
||||
const models: Record<ModelAlias, ModelConfig> = {};
|
||||
for (const [name, entry] of Object.entries(raw)) {
|
||||
if (!isRecord(entry)) {
|
||||
throw new Error(`config.models.${name} must be a mapping`);
|
||||
}
|
||||
const provider = entry.provider;
|
||||
const modelName = entry.name;
|
||||
if (typeof provider !== "string" || typeof modelName !== "string") {
|
||||
throw new Error(`config.models.${name} requires provider and name`);
|
||||
}
|
||||
models[name] = { provider, name: modelName };
|
||||
}
|
||||
return models;
|
||||
}
|
||||
|
||||
function normalizeAgents(raw: unknown): Record<AgentAlias, AgentConfig> {
|
||||
if (!isRecord(raw)) {
|
||||
throw new Error("config.agents must be a mapping");
|
||||
}
|
||||
const agents: Record<AgentAlias, AgentConfig> = {};
|
||||
for (const [name, entry] of Object.entries(raw)) {
|
||||
if (!isRecord(entry)) {
|
||||
throw new Error(`config.agents.${name} must be a mapping`);
|
||||
}
|
||||
const command = entry.command;
|
||||
const argsRaw = entry.args;
|
||||
if (typeof command !== "string") {
|
||||
throw new Error(`config.agents.${name} requires command`);
|
||||
}
|
||||
const args = Array.isArray(argsRaw)
|
||||
? argsRaw.filter((a): a is string => typeof a === "string")
|
||||
: [];
|
||||
agents[name] = { command, args };
|
||||
}
|
||||
return agents;
|
||||
}
|
||||
|
||||
function normalizeModelOverrides(raw: unknown): Record<Scenario, ModelAlias> | null {
|
||||
if (raw === undefined || raw === null) {
|
||||
return null;
|
||||
}
|
||||
if (!isRecord(raw)) {
|
||||
throw new Error("config.modelOverrides must be a mapping or null");
|
||||
}
|
||||
const overrides: Record<Scenario, ModelAlias> = {};
|
||||
for (const [scene, alias] of Object.entries(raw)) {
|
||||
if (typeof alias === "string") {
|
||||
overrides[scene] = alias;
|
||||
}
|
||||
}
|
||||
return overrides;
|
||||
}
|
||||
|
||||
function normalizeAgentOverrides(
|
||||
raw: unknown,
|
||||
): Record<WorkflowName, Record<string, AgentAlias>> | null {
|
||||
if (raw === undefined || raw === null) {
|
||||
return null;
|
||||
}
|
||||
if (!isRecord(raw)) {
|
||||
throw new Error("config.agentOverrides must be a mapping or null");
|
||||
}
|
||||
const overrides: Record<WorkflowName, Record<string, AgentAlias>> = {};
|
||||
for (const [workflowName, rolesRaw] of Object.entries(raw)) {
|
||||
if (!isRecord(rolesRaw)) {
|
||||
continue;
|
||||
}
|
||||
const roles: Record<string, AgentAlias> = {};
|
||||
for (const [roleName, alias] of Object.entries(rolesRaw)) {
|
||||
if (typeof alias === "string") {
|
||||
roles[roleName] = alias;
|
||||
}
|
||||
}
|
||||
overrides[workflowName] = roles;
|
||||
}
|
||||
return overrides;
|
||||
}
|
||||
|
||||
export function normalizeWorkflowConfig(raw: unknown): WorkflowConfig {
|
||||
if (!isRecord(raw)) {
|
||||
throw new Error("config.yaml root must be a mapping");
|
||||
}
|
||||
const defaultAgent = raw.defaultAgent;
|
||||
const defaultModel = raw.defaultModel;
|
||||
if (typeof defaultAgent !== "string" || typeof defaultModel !== "string") {
|
||||
throw new Error("config requires defaultAgent and defaultModel");
|
||||
}
|
||||
return {
|
||||
providers: normalizeProviders(raw.providers),
|
||||
models: normalizeModels(raw.models),
|
||||
agents: normalizeAgents(raw.agents),
|
||||
defaultAgent,
|
||||
agentOverrides: normalizeAgentOverrides(raw.agentOverrides),
|
||||
defaultModel,
|
||||
modelOverrides: normalizeModelOverrides(raw.modelOverrides),
|
||||
};
|
||||
}
|
||||
|
||||
export async function loadWorkflowConfig(storageRoot: string): Promise<WorkflowConfig> {
|
||||
const path = getConfigPath(storageRoot);
|
||||
const text = await readFile(path, "utf8");
|
||||
const raw = parse(text) as unknown;
|
||||
return normalizeWorkflowConfig(raw);
|
||||
}
|
||||
|
||||
export async function loadThreadsIndex(storageRoot: string): Promise<ThreadsIndex> {
|
||||
const path = getThreadsPath(storageRoot);
|
||||
try {
|
||||
const text = await readFile(path, "utf8");
|
||||
const raw = parse(text) as unknown;
|
||||
if (!isRecord(raw)) {
|
||||
return {};
|
||||
}
|
||||
const index: ThreadsIndex = {};
|
||||
for (const [threadId, head] of Object.entries(raw)) {
|
||||
if (typeof head === "string") {
|
||||
index[threadId as ThreadId] = head;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
} catch (e) {
|
||||
const err = e as NodeJS.ErrnoException;
|
||||
if (err.code === "ENOENT") {
|
||||
return {};
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import type { StepContext, ThreadId, WorkflowPayload } from "@uncaged/uwf-protocol";
|
||||
|
||||
export type AgentContext = {
|
||||
threadId: ThreadId;
|
||||
role: string;
|
||||
systemPrompt: string;
|
||||
prompt: string;
|
||||
history: StepContext[];
|
||||
workflow: WorkflowPayload;
|
||||
};
|
||||
|
||||
export type AgentRunFn = (ctx: AgentContext) => Promise<string>;
|
||||
|
||||
export type AgentOptions = {
|
||||
name: string;
|
||||
run: AgentRunFn;
|
||||
};
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [{ "path": "../uwf-protocol" }]
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import type { ModeratorContext, WorkflowPayload } from "@uncaged/uwf-protocol";
|
||||
|
||||
import { evaluate } from "../src/evaluate.js";
|
||||
|
||||
const solveIssueWorkflow: WorkflowPayload = {
|
||||
name: "solve-issue",
|
||||
description: "End-to-end issue resolution",
|
||||
roles: {
|
||||
planner: {
|
||||
description: "Creates implementation plan",
|
||||
systemPrompt: "You are a planning agent...",
|
||||
outputSchema: "5GWKR8TN1V3JA",
|
||||
},
|
||||
developer: {
|
||||
description: "Implements code changes",
|
||||
systemPrompt: "You are a developer agent...",
|
||||
outputSchema: "8CNWT4KR6D1HV",
|
||||
},
|
||||
reviewer: {
|
||||
description: "Reviews code changes",
|
||||
systemPrompt: "You are a code reviewer...",
|
||||
outputSchema: "1VPBG9SM5E7WK",
|
||||
},
|
||||
},
|
||||
conditions: {
|
||||
needsClarification: {
|
||||
description: "Planner requests clarification from user",
|
||||
expression: "$exists(steps[-1].output.needsClarification)",
|
||||
},
|
||||
notApproved: {
|
||||
description: "Reviewer rejected the implementation",
|
||||
expression: "steps[-1].output.approved = false",
|
||||
},
|
||||
},
|
||||
graph: {
|
||||
$START: [{ role: "planner", condition: null }],
|
||||
planner: [
|
||||
{ role: "developer", condition: "needsClarification" },
|
||||
{ role: "$END", condition: null },
|
||||
],
|
||||
developer: [{ role: "reviewer", condition: null }],
|
||||
reviewer: [
|
||||
{ role: "developer", condition: "notApproved" },
|
||||
{ role: "$END", condition: null },
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
function makeContext(steps: ModeratorContext["steps"]): ModeratorContext {
|
||||
return {
|
||||
start: {
|
||||
workflow: "4KNM2PXR3B1QW",
|
||||
prompt: "Fix the login bug",
|
||||
},
|
||||
steps,
|
||||
};
|
||||
}
|
||||
|
||||
describe("evaluate", () => {
|
||||
test("$START → first role (fallback)", async () => {
|
||||
const result = await evaluate(solveIssueWorkflow, makeContext([]));
|
||||
expect(result).toEqual({ ok: true, value: "planner" });
|
||||
});
|
||||
|
||||
test("condition match (notApproved → developer)", async () => {
|
||||
const context = makeContext([
|
||||
{
|
||||
role: "reviewer",
|
||||
output: { approved: false },
|
||||
detail: "2MXBG6PN4A8JR",
|
||||
agent: "uwf-hermes",
|
||||
},
|
||||
]);
|
||||
const result = await evaluate(solveIssueWorkflow, context);
|
||||
expect(result).toEqual({ ok: true, value: "developer" });
|
||||
});
|
||||
|
||||
test("fallback when condition does not match → $END", async () => {
|
||||
const context = makeContext([
|
||||
{
|
||||
role: "reviewer",
|
||||
output: { approved: true },
|
||||
detail: "2MXBG6PN4A8JR",
|
||||
agent: "uwf-hermes",
|
||||
},
|
||||
]);
|
||||
const result = await evaluate(solveIssueWorkflow, context);
|
||||
expect(result).toEqual({ ok: true, value: "$END" });
|
||||
});
|
||||
|
||||
test("missing role in graph → error", async () => {
|
||||
const context = makeContext([
|
||||
{
|
||||
role: "unknown-role",
|
||||
output: {},
|
||||
detail: "2MXBG6PN4A8JR",
|
||||
agent: "uwf-hermes",
|
||||
},
|
||||
]);
|
||||
const result = await evaluate(solveIssueWorkflow, context);
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) {
|
||||
expect(result.error.message).toBe('no transitions defined for role "unknown-role"');
|
||||
}
|
||||
});
|
||||
|
||||
test("output expansion in context works with JSONata", async () => {
|
||||
const context = makeContext([
|
||||
{
|
||||
role: "planner",
|
||||
output: { needsClarification: true },
|
||||
detail: "7BQST3VW9F2MA",
|
||||
agent: "uwf-hermes",
|
||||
},
|
||||
]);
|
||||
const result = await evaluate(solveIssueWorkflow, context);
|
||||
expect(result).toEqual({ ok: true, value: "developer" });
|
||||
});
|
||||
});
|
||||
@@ -1,82 +0,0 @@
|
||||
import type { ModeratorContext, WorkflowPayload } from "@uncaged/uwf-protocol";
|
||||
import jsonata from "jsonata";
|
||||
|
||||
import type { Result } from "./types.js";
|
||||
|
||||
const START_ROLE = "$START";
|
||||
|
||||
function isTruthy(value: unknown): boolean {
|
||||
if (value === null || value === undefined) {
|
||||
return false;
|
||||
}
|
||||
if (typeof value === "boolean") {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === "number") {
|
||||
return value !== 0 && !Number.isNaN(value);
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
return value.length > 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async function evaluateJsonata(expression: string, context: ModeratorContext): Promise<Result<unknown, Error>> {
|
||||
try {
|
||||
const result = await jsonata(expression).evaluate(context);
|
||||
return { ok: true, value: result };
|
||||
} catch (error) {
|
||||
return {
|
||||
ok: false,
|
||||
error: error instanceof Error ? error : new Error(String(error)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function currentRole(context: ModeratorContext): string {
|
||||
if (context.steps.length === 0) {
|
||||
return START_ROLE;
|
||||
}
|
||||
return context.steps[context.steps.length - 1].role;
|
||||
}
|
||||
|
||||
export async function evaluate(
|
||||
workflow: WorkflowPayload,
|
||||
context: ModeratorContext,
|
||||
): Promise<Result<string, Error>> {
|
||||
const role = currentRole(context);
|
||||
const transitions = workflow.graph[role];
|
||||
if (transitions === undefined) {
|
||||
return {
|
||||
ok: false,
|
||||
error: new Error(`no transitions defined for role "${role}"`),
|
||||
};
|
||||
}
|
||||
|
||||
for (const transition of transitions) {
|
||||
if (transition.condition === null) {
|
||||
return { ok: true, value: transition.role };
|
||||
}
|
||||
|
||||
const conditionDef = workflow.conditions[transition.condition];
|
||||
if (conditionDef === undefined) {
|
||||
return {
|
||||
ok: false,
|
||||
error: new Error(`unknown condition "${transition.condition}"`),
|
||||
};
|
||||
}
|
||||
|
||||
const evalResult = await evaluateJsonata(conditionDef.expression, context);
|
||||
if (!evalResult.ok) {
|
||||
return evalResult;
|
||||
}
|
||||
if (isTruthy(evalResult.value)) {
|
||||
return { ok: true, value: transition.role };
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ok: false,
|
||||
error: new Error(`no transition matched for role "${role}"`),
|
||||
};
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { evaluate } from "./evaluate.js";
|
||||
@@ -1 +0,0 @@
|
||||
export type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [{ "path": "../uwf-protocol" }]
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
{
|
||||
"name": "@uncaged/uwf-protocol",
|
||||
"version": "0.1.0",
|
||||
"files": [
|
||||
"src",
|
||||
"dist",
|
||||
"package.json"
|
||||
],
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"bun": "./src/index.ts",
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@uncaged/json-cas": "^0.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.8.3"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
export {
|
||||
START_NODE_SCHEMA,
|
||||
STEP_NODE_SCHEMA,
|
||||
WORKFLOW_SCHEMA,
|
||||
} from "./schemas.js";
|
||||
export type {
|
||||
AgentAlias,
|
||||
AgentConfig,
|
||||
CasRef,
|
||||
ConditionDefinition,
|
||||
ModelAlias,
|
||||
ModelConfig,
|
||||
ModeratorContext,
|
||||
ProviderAlias,
|
||||
ProviderConfig,
|
||||
RoleDefinition,
|
||||
RoleName,
|
||||
Scenario,
|
||||
StartNodePayload,
|
||||
StartOutput,
|
||||
StepContext,
|
||||
StepNodePayload,
|
||||
StepOutput,
|
||||
StepRecord,
|
||||
ThreadId,
|
||||
ThreadListItem,
|
||||
ThreadsIndex,
|
||||
Transition,
|
||||
WorkflowConfig,
|
||||
WorkflowName,
|
||||
WorkflowPayload,
|
||||
} from "./types.js";
|
||||
@@ -1,83 +0,0 @@
|
||||
import type { JSONSchema } from "@uncaged/json-cas";
|
||||
|
||||
const ROLE_DEFINITION: JSONSchema = {
|
||||
type: "object",
|
||||
required: ["description", "systemPrompt", "outputSchema"],
|
||||
properties: {
|
||||
description: { type: "string" },
|
||||
systemPrompt: { type: "string" },
|
||||
outputSchema: { type: "string", format: "cas_ref" },
|
||||
},
|
||||
additionalProperties: false,
|
||||
};
|
||||
|
||||
const CONDITION_DEFINITION: JSONSchema = {
|
||||
type: "object",
|
||||
required: ["description", "expression"],
|
||||
properties: {
|
||||
description: { type: "string" },
|
||||
expression: { type: "string" },
|
||||
},
|
||||
additionalProperties: false,
|
||||
};
|
||||
|
||||
const TRANSITION: JSONSchema = {
|
||||
type: "object",
|
||||
required: ["role", "condition"],
|
||||
properties: {
|
||||
role: { type: "string" },
|
||||
condition: { anyOf: [{ type: "string" }, { type: "null" }] },
|
||||
},
|
||||
additionalProperties: false,
|
||||
};
|
||||
|
||||
export const WORKFLOW_SCHEMA: JSONSchema = {
|
||||
type: "object",
|
||||
required: ["name", "description", "roles", "conditions", "graph"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
description: { type: "string" },
|
||||
roles: {
|
||||
type: "object",
|
||||
additionalProperties: ROLE_DEFINITION,
|
||||
},
|
||||
conditions: {
|
||||
type: "object",
|
||||
additionalProperties: CONDITION_DEFINITION,
|
||||
},
|
||||
graph: {
|
||||
type: "object",
|
||||
additionalProperties: {
|
||||
type: "array",
|
||||
items: TRANSITION,
|
||||
},
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
};
|
||||
|
||||
export const START_NODE_SCHEMA: JSONSchema = {
|
||||
type: "object",
|
||||
required: ["workflow", "prompt"],
|
||||
properties: {
|
||||
workflow: { type: "string", format: "cas_ref" },
|
||||
prompt: { type: "string" },
|
||||
},
|
||||
additionalProperties: false,
|
||||
};
|
||||
|
||||
export const STEP_NODE_SCHEMA: JSONSchema = {
|
||||
type: "object",
|
||||
required: ["start", "prev", "role", "output", "detail", "agent"],
|
||||
properties: {
|
||||
start: { type: "string", format: "cas_ref" },
|
||||
prev: {
|
||||
anyOf: [{ type: "string", format: "cas_ref" }, { type: "null" }],
|
||||
},
|
||||
role: { type: "string" },
|
||||
output: { type: "string", format: "cas_ref" },
|
||||
detail: { type: "string", format: "cas_ref" },
|
||||
agent: { type: "string" },
|
||||
},
|
||||
additionalProperties: false,
|
||||
};
|
||||
@@ -1,127 +0,0 @@
|
||||
// ── 4.1 公共类型 ────────────────────────────────────────────────────
|
||||
|
||||
/** CAS hash — XXH64, 13-char Crockford Base32 */
|
||||
export type CasRef = string;
|
||||
|
||||
/** Thread ID — ULID, 26-char Crockford Base32 */
|
||||
export type ThreadId = string;
|
||||
|
||||
/** 一个 step 的核心数据,被 StepNode payload 和 JSONata 上下文共享 */
|
||||
export type StepRecord = {
|
||||
role: string;
|
||||
output: CasRef;
|
||||
detail: CasRef;
|
||||
agent: string;
|
||||
};
|
||||
|
||||
// ── 4.2 Workflow 定义 ───────────────────────────────────────────────
|
||||
|
||||
export type RoleDefinition = {
|
||||
description: string;
|
||||
systemPrompt: string;
|
||||
outputSchema: CasRef;
|
||||
};
|
||||
|
||||
export type Transition = {
|
||||
role: string;
|
||||
condition: string | null;
|
||||
};
|
||||
|
||||
export type ConditionDefinition = {
|
||||
description: string;
|
||||
expression: string;
|
||||
};
|
||||
|
||||
export type WorkflowPayload = {
|
||||
name: string;
|
||||
description: string;
|
||||
roles: Record<string, RoleDefinition>;
|
||||
conditions: Record<string, ConditionDefinition>;
|
||||
graph: Record<string, Transition[]>;
|
||||
};
|
||||
|
||||
// ── 4.3 Thread 节点 ─────────────────────────────────────────────────
|
||||
|
||||
export type StartNodePayload = {
|
||||
workflow: CasRef;
|
||||
prompt: string;
|
||||
};
|
||||
|
||||
export type StepNodePayload = StepRecord & {
|
||||
start: CasRef;
|
||||
prev: CasRef | null;
|
||||
};
|
||||
|
||||
// ── 4.4 JSONata 求值上下文 ──────────────────────────────────────────
|
||||
|
||||
/** JSONata 上下文中的 step — output 被展开 */
|
||||
export type StepContext = Omit<StepRecord, "output"> & {
|
||||
output: unknown;
|
||||
};
|
||||
|
||||
export type ModeratorContext = {
|
||||
start: StartNodePayload;
|
||||
steps: StepContext[];
|
||||
};
|
||||
|
||||
// ── 4.5 CLI 输出 ────────────────────────────────────────────────────
|
||||
|
||||
/** uwf thread start */
|
||||
export type StartOutput = {
|
||||
workflow: CasRef;
|
||||
thread: ThreadId;
|
||||
};
|
||||
|
||||
/** uwf thread step / uwf thread show */
|
||||
export type StepOutput = {
|
||||
workflow: CasRef;
|
||||
thread: ThreadId;
|
||||
head: CasRef;
|
||||
done: boolean;
|
||||
};
|
||||
|
||||
/** uwf thread list */
|
||||
export type ThreadListItem = {
|
||||
thread: ThreadId;
|
||||
workflow: CasRef;
|
||||
head: CasRef;
|
||||
};
|
||||
|
||||
// ── 4.6 配置 ────────────────────────────────────────────────────────
|
||||
|
||||
/** Alias types for config references */
|
||||
export type AgentAlias = string;
|
||||
export type ModelAlias = string;
|
||||
export type ProviderAlias = string;
|
||||
export type WorkflowName = string;
|
||||
export type RoleName = string;
|
||||
export type Scenario = string;
|
||||
|
||||
export type ProviderConfig = {
|
||||
baseUrl: string;
|
||||
apiKeyEnv: string;
|
||||
};
|
||||
|
||||
export type ModelConfig = {
|
||||
provider: ProviderAlias;
|
||||
name: string;
|
||||
};
|
||||
|
||||
export type AgentConfig = {
|
||||
command: string;
|
||||
args: string[];
|
||||
};
|
||||
|
||||
/** ~/.uncaged/workflow/config.yaml */
|
||||
export type WorkflowConfig = {
|
||||
providers: Record<ProviderAlias, ProviderConfig>;
|
||||
models: Record<ModelAlias, ModelConfig>;
|
||||
agents: Record<AgentAlias, AgentConfig>;
|
||||
defaultAgent: AgentAlias;
|
||||
agentOverrides: Record<WorkflowName, Record<RoleName, AgentAlias>> | null;
|
||||
defaultModel: ModelAlias;
|
||||
modelOverrides: Record<Scenario, ModelAlias> | null;
|
||||
};
|
||||
|
||||
/** ~/.uncaged/workflow/threads.yaml */
|
||||
export type ThreadsIndex = Record<ThreadId, CasRef>;
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { packageDescriptor } from "../src/index.js";
|
||||
|
||||
describe("packageDescriptor", () => {
|
||||
test("has the correct package name", () => {
|
||||
expect(packageDescriptor.name).toBe("@uncaged/workflow-agent-cursor");
|
||||
});
|
||||
|
||||
test("has a non-empty version string", () => {
|
||||
expect(typeof packageDescriptor.version).toBe("string");
|
||||
expect(packageDescriptor.version.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("capabilities is a non-empty array of strings", () => {
|
||||
expect(Array.isArray(packageDescriptor.capabilities)).toBe(true);
|
||||
expect(packageDescriptor.capabilities.length).toBeGreaterThan(0);
|
||||
for (const cap of packageDescriptor.capabilities) {
|
||||
expect(typeof cap).toBe("string");
|
||||
}
|
||||
});
|
||||
|
||||
test("configSchema is an object with type 'object'", () => {
|
||||
expect(typeof packageDescriptor.configSchema).toBe("object");
|
||||
expect(packageDescriptor.configSchema.type).toBe("object");
|
||||
});
|
||||
|
||||
test("configSchema requires 'command' and 'timeout'", () => {
|
||||
const required = packageDescriptor.configSchema.required as string[];
|
||||
expect(required).toContain("command");
|
||||
expect(required).toContain("timeout");
|
||||
});
|
||||
|
||||
test("configSchema properties include command, model, timeout, workspace", () => {
|
||||
const props = packageDescriptor.configSchema.properties as Record<string, unknown>;
|
||||
expect(props).toHaveProperty("command");
|
||||
expect(props).toHaveProperty("model");
|
||||
expect(props).toHaveProperty("timeout");
|
||||
expect(props).toHaveProperty("workspace");
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,7 @@ import { extractWorkspacePath } from "./extract-workspace.js";
|
||||
import type { CursorAgentConfig } from "./types.js";
|
||||
import { validateCursorAgentConfig } from "./validate-config.js";
|
||||
|
||||
export { packageDescriptor } from "./package-descriptor.js";
|
||||
export type { CursorAgentConfig } from "./types.js";
|
||||
export { validateCursorAgentConfig } from "./validate-config.js";
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import type { PackageDescriptor } from "@uncaged/workflow-protocol";
|
||||
|
||||
/**
|
||||
* Static metadata for @uncaged/workflow-agent-cursor.
|
||||
* Config maps to {@link CursorAgentConfig}.
|
||||
*/
|
||||
export const packageDescriptor: PackageDescriptor = {
|
||||
name: "@uncaged/workflow-agent-cursor",
|
||||
version: "0.5.0-alpha.4",
|
||||
capabilities: ["cursor-cli", "workspace-agent"],
|
||||
configSchema: {
|
||||
type: "object",
|
||||
required: ["command", "timeout"],
|
||||
properties: {
|
||||
command: {
|
||||
type: "string",
|
||||
description: "Absolute path to the cursor-agent CLI binary.",
|
||||
},
|
||||
model: {
|
||||
anyOf: [{ type: "string" }, { type: "null" }],
|
||||
description: "Model identifier passed to cursor-agent --model; null means auto.",
|
||||
},
|
||||
timeout: {
|
||||
type: "number",
|
||||
description: "Timeout in milliseconds; 0 means no limit.",
|
||||
},
|
||||
workspace: {
|
||||
anyOf: [{ type: "string" }, { type: "null" }],
|
||||
description: "Override workspace path; null resolves from thread context.",
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { packageDescriptor } from "../src/index.js";
|
||||
|
||||
describe("packageDescriptor", () => {
|
||||
test("has the correct package name", () => {
|
||||
expect(packageDescriptor.name).toBe("@uncaged/workflow-agent-hermes");
|
||||
});
|
||||
|
||||
test("has a non-empty version string", () => {
|
||||
expect(typeof packageDescriptor.version).toBe("string");
|
||||
expect(packageDescriptor.version.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("capabilities is a non-empty array of strings", () => {
|
||||
expect(Array.isArray(packageDescriptor.capabilities)).toBe(true);
|
||||
expect(packageDescriptor.capabilities.length).toBeGreaterThan(0);
|
||||
for (const cap of packageDescriptor.capabilities) {
|
||||
expect(typeof cap).toBe("string");
|
||||
}
|
||||
});
|
||||
|
||||
test("configSchema is an object with type 'object'", () => {
|
||||
expect(typeof packageDescriptor.configSchema).toBe("object");
|
||||
expect(packageDescriptor.configSchema.type).toBe("object");
|
||||
});
|
||||
|
||||
test("configSchema requires 'command'", () => {
|
||||
const required = packageDescriptor.configSchema.required as string[];
|
||||
expect(required).toContain("command");
|
||||
});
|
||||
|
||||
test("configSchema properties include command, model, timeout", () => {
|
||||
const props = packageDescriptor.configSchema.properties as Record<string, unknown>;
|
||||
expect(props).toHaveProperty("command");
|
||||
expect(props).toHaveProperty("model");
|
||||
expect(props).toHaveProperty("timeout");
|
||||
});
|
||||
});
|
||||
@@ -13,6 +13,7 @@ const HERMES_DEFAULT_MAX_TURNS = 90;
|
||||
|
||||
type HermesAgentOpt = { prompt: string };
|
||||
|
||||
export { packageDescriptor } from "./package-descriptor.js";
|
||||
export type { HermesAgentConfig } from "./types.js";
|
||||
export { validateHermesAgentConfig } from "./validate-config.js";
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { PackageDescriptor } from "@uncaged/workflow-runtime";
|
||||
|
||||
/**
|
||||
* Static metadata for @uncaged/workflow-agent-hermes.
|
||||
* Config maps to {@link HermesAgentConfig}.
|
||||
*/
|
||||
export const packageDescriptor: PackageDescriptor = {
|
||||
name: "@uncaged/workflow-agent-hermes",
|
||||
version: "0.5.0-alpha.4",
|
||||
capabilities: ["hermes-cli", "yolo-mode"],
|
||||
configSchema: {
|
||||
type: "object",
|
||||
required: ["command"],
|
||||
properties: {
|
||||
command: {
|
||||
type: "string",
|
||||
description: "Absolute path to the hermes CLI binary.",
|
||||
},
|
||||
model: {
|
||||
anyOf: [{ type: "string" }, { type: "null" }],
|
||||
description: "Model identifier passed to hermes --model; null uses the CLI default.",
|
||||
},
|
||||
timeout: {
|
||||
anyOf: [{ type: "number" }, { type: "null" }],
|
||||
description: "Timeout in milliseconds; null means no limit.",
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { packageDescriptor } from "../src/index.js";
|
||||
|
||||
describe("packageDescriptor", () => {
|
||||
test("has the correct package name", () => {
|
||||
expect(packageDescriptor.name).toBe("@uncaged/workflow-agent-llm");
|
||||
});
|
||||
|
||||
test("has a non-empty version string", () => {
|
||||
expect(typeof packageDescriptor.version).toBe("string");
|
||||
expect(packageDescriptor.version.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("capabilities is a non-empty array of strings", () => {
|
||||
expect(Array.isArray(packageDescriptor.capabilities)).toBe(true);
|
||||
expect(packageDescriptor.capabilities.length).toBeGreaterThan(0);
|
||||
for (const cap of packageDescriptor.capabilities) {
|
||||
expect(typeof cap).toBe("string");
|
||||
}
|
||||
});
|
||||
|
||||
test("configSchema is an object with type 'object'", () => {
|
||||
expect(typeof packageDescriptor.configSchema).toBe("object");
|
||||
expect(packageDescriptor.configSchema.type).toBe("object");
|
||||
});
|
||||
|
||||
test("configSchema requires baseUrl, apiKey, model", () => {
|
||||
const required = packageDescriptor.configSchema.required as string[];
|
||||
expect(required).toContain("baseUrl");
|
||||
expect(required).toContain("apiKey");
|
||||
expect(required).toContain("model");
|
||||
});
|
||||
|
||||
test("configSchema properties include baseUrl, apiKey, model", () => {
|
||||
const props = packageDescriptor.configSchema.properties as Record<string, unknown>;
|
||||
expect(props).toHaveProperty("baseUrl");
|
||||
expect(props).toHaveProperty("apiKey");
|
||||
expect(props).toHaveProperty("model");
|
||||
});
|
||||
});
|
||||
@@ -4,3 +4,4 @@ export {
|
||||
type LlmChatError,
|
||||
type LlmMessage,
|
||||
} from "./create-llm-adapter.js";
|
||||
export { packageDescriptor } from "./package-descriptor.js";
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { PackageDescriptor } from "@uncaged/workflow-runtime";
|
||||
|
||||
/**
|
||||
* Static metadata for @uncaged/workflow-agent-llm.
|
||||
* Config maps to {@link LlmProvider}: baseUrl + apiKey + model.
|
||||
*/
|
||||
export const packageDescriptor: PackageDescriptor = {
|
||||
name: "@uncaged/workflow-agent-llm",
|
||||
version: "0.5.0-alpha.4",
|
||||
capabilities: ["llm-single-turn"],
|
||||
configSchema: {
|
||||
type: "object",
|
||||
required: ["baseUrl", "apiKey", "model"],
|
||||
properties: {
|
||||
baseUrl: {
|
||||
type: "string",
|
||||
description: "Base URL of the OpenAI-compatible chat completions endpoint.",
|
||||
},
|
||||
apiKey: {
|
||||
type: "string",
|
||||
description: "API key for the provider.",
|
||||
},
|
||||
model: {
|
||||
type: "string",
|
||||
description: "Model identifier passed as the `model` field in the request body.",
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { packageDescriptor } from "../src/index.js";
|
||||
|
||||
describe("packageDescriptor", () => {
|
||||
test("has the correct package name", () => {
|
||||
expect(packageDescriptor.name).toBe("@uncaged/workflow-agent-react");
|
||||
});
|
||||
|
||||
test("has a non-empty version string", () => {
|
||||
expect(typeof packageDescriptor.version).toBe("string");
|
||||
expect(packageDescriptor.version.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("capabilities is a non-empty array of strings", () => {
|
||||
expect(Array.isArray(packageDescriptor.capabilities)).toBe(true);
|
||||
expect(packageDescriptor.capabilities.length).toBeGreaterThan(0);
|
||||
for (const cap of packageDescriptor.capabilities) {
|
||||
expect(typeof cap).toBe("string");
|
||||
}
|
||||
});
|
||||
|
||||
test("configSchema is an object with type 'object'", () => {
|
||||
expect(typeof packageDescriptor.configSchema).toBe("object");
|
||||
expect(packageDescriptor.configSchema.type).toBe("object");
|
||||
});
|
||||
|
||||
test("configSchema requires maxRounds", () => {
|
||||
const required = packageDescriptor.configSchema.required as string[];
|
||||
expect(required).toContain("maxRounds");
|
||||
});
|
||||
|
||||
test("configSchema properties include maxRounds", () => {
|
||||
const props = packageDescriptor.configSchema.properties as Record<string, unknown>;
|
||||
expect(props).toHaveProperty("maxRounds");
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
export { createReactAdapter } from "./create-react-adapter.js";
|
||||
export { packageDescriptor } from "./package-descriptor.js";
|
||||
export type { ToolEntry, ToolHandler } from "./tools/index.js";
|
||||
export { defaultToolHandler, defaultTools } from "./tools/index.js";
|
||||
export type { ReactAdapterConfig, ReactToolHandler } from "./types.js";
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { PackageDescriptor } from "@uncaged/workflow-protocol";
|
||||
|
||||
/**
|
||||
* Static metadata for @uncaged/workflow-agent-react.
|
||||
*
|
||||
* Config represents the serializable subset of {@link ReactAdapterConfig}.
|
||||
* The `llm` function and `toolHandler` are runtime constructs and are not
|
||||
* stored in the CAS agent node; only `maxRounds` is serializable.
|
||||
*/
|
||||
export const packageDescriptor: PackageDescriptor = {
|
||||
name: "@uncaged/workflow-agent-react",
|
||||
version: "0.5.0-alpha.4",
|
||||
capabilities: ["react-loop", "tool-calling"],
|
||||
configSchema: {
|
||||
type: "object",
|
||||
required: ["maxRounds"],
|
||||
properties: {
|
||||
maxRounds: {
|
||||
type: "number",
|
||||
description: "Maximum number of LLM ↔ tool-call rounds before the loop is terminated.",
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,868 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createMemoryStore, type Store, walk } from "@uncaged/json-cas";
|
||||
import {
|
||||
type ContentPayload,
|
||||
registerWorkflowSchemas,
|
||||
type ThreadEndPayload,
|
||||
type ThreadStartPayload,
|
||||
type ThreadStepPayload,
|
||||
type WorkflowSchemaHashes,
|
||||
} from "@uncaged/json-cas-workflow";
|
||||
import { registerWorkflow, type WorkflowInput } from "@uncaged/workflow-json-def";
|
||||
|
||||
import {
|
||||
buildJsonCasThreadContext,
|
||||
buildJsonCasThreadSnapshot,
|
||||
readContentText,
|
||||
} from "../src/engine/json-cas-context.js";
|
||||
import { executeJsonCasThread } from "../src/engine/json-cas-engine.js";
|
||||
import type {
|
||||
JsonCasAgentFn,
|
||||
JsonCasEngineIo,
|
||||
JsonCasEngineOptions,
|
||||
} from "../src/engine/json-cas-types.js";
|
||||
|
||||
// ── Test fixtures ─────────────────────────────────────────────────────
|
||||
|
||||
const START = "__start__";
|
||||
const END = "__end__";
|
||||
|
||||
const SIMPLE_WORKFLOW: WorkflowInput = {
|
||||
name: "test-simple",
|
||||
description: "A simple two-role workflow for testing",
|
||||
roles: {
|
||||
planner: {
|
||||
description: "Plans the work",
|
||||
systemPrompt: "You are a planner.",
|
||||
extractPrompt: "Extract planner output.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["plan"],
|
||||
properties: { plan: { type: "string" } },
|
||||
},
|
||||
},
|
||||
coder: {
|
||||
description: "Implements the plan",
|
||||
systemPrompt: "You are a coder.",
|
||||
extractPrompt: "Extract coder output.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["code"],
|
||||
properties: { code: { type: "string" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
moderator: [
|
||||
{ from: START, to: "planner", when: null },
|
||||
{ from: "planner", to: "coder", when: null },
|
||||
{ from: "coder", to: END, when: null },
|
||||
],
|
||||
};
|
||||
|
||||
const SINGLE_ROLE_WORKFLOW: WorkflowInput = {
|
||||
name: "test-single",
|
||||
description: "A single-role workflow",
|
||||
roles: {
|
||||
worker: {
|
||||
description: "Does all the work",
|
||||
systemPrompt: "You are a worker.",
|
||||
extractPrompt: "Extract worker output.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["result"],
|
||||
properties: { result: { type: "string" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
moderator: [
|
||||
{ from: START, to: "worker", when: null },
|
||||
{ from: "worker", to: END, when: null },
|
||||
],
|
||||
};
|
||||
|
||||
const CONDITIONAL_WORKFLOW: WorkflowInput = {
|
||||
name: "test-conditional",
|
||||
description: "A workflow with JSONata conditions",
|
||||
roles: {
|
||||
checker: {
|
||||
description: "Checks the input",
|
||||
systemPrompt: "You are a checker.",
|
||||
extractPrompt: "Extract checker output.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["status"],
|
||||
properties: { status: { type: "string" } },
|
||||
},
|
||||
},
|
||||
fixer: {
|
||||
description: "Fixes issues",
|
||||
systemPrompt: "You are a fixer.",
|
||||
extractPrompt: "Extract fixer output.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["fix"],
|
||||
properties: { fix: { type: "string" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
moderator: [
|
||||
{ from: START, to: "checker", when: null },
|
||||
{ from: "checker", to: END, when: "steps[-1].meta.status = 'ok'" },
|
||||
{ from: "checker", to: "fixer", when: null },
|
||||
{ from: "fixer", to: "checker", when: null },
|
||||
],
|
||||
};
|
||||
|
||||
function noLogger(): (tag: string, content: string) => void {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
async function setupStore(): Promise<{
|
||||
store: Store;
|
||||
typeHashes: WorkflowSchemaHashes;
|
||||
}> {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
return { store, typeHashes };
|
||||
}
|
||||
|
||||
async function setupWorkflow(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
workflowDef: WorkflowInput,
|
||||
) {
|
||||
const workflowHash = await registerWorkflow(store, typeHashes, workflowDef);
|
||||
return { workflowHash };
|
||||
}
|
||||
|
||||
function makeOptions(overrides: Partial<JsonCasEngineOptions> = {}): JsonCasEngineOptions {
|
||||
return {
|
||||
depth: 0,
|
||||
parentThread: null,
|
||||
signal: new AbortController().signal,
|
||||
agents: {},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makeIo(store: Store, typeHashes: WorkflowSchemaHashes, threadId: string): JsonCasEngineIo {
|
||||
return { threadId, store, typeHashes };
|
||||
}
|
||||
|
||||
/**
|
||||
* A mock agent that returns a canned text and meta for each role.
|
||||
*/
|
||||
function createMockAgent(
|
||||
responses: Record<string, { text: string; meta: Record<string, unknown> }>,
|
||||
): JsonCasAgentFn {
|
||||
return async (role, _systemPrompt, _snapshot) => {
|
||||
const resp = responses[role];
|
||||
if (resp === undefined) {
|
||||
throw new Error(`mock agent: no response configured for role "${role}"`);
|
||||
}
|
||||
return { ...resp, react: null };
|
||||
};
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────
|
||||
|
||||
describe("executeJsonCasThread", () => {
|
||||
describe("thread lifecycle", () => {
|
||||
test("simple two-role workflow creates start, two steps, and end nodes", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SIMPLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
planner: { text: "I will plan", meta: { plan: "phase-1" } },
|
||||
coder: { text: "I wrote code", meta: { code: "done" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "Build a widget",
|
||||
moderatorRules: SIMPLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD01"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
expect(result.returnCode).toBe(0);
|
||||
expect(result.summary).toContain("END");
|
||||
expect(result.rootHash).toBeTruthy();
|
||||
|
||||
const endNode = store.get(result.rootHash);
|
||||
expect(endNode).not.toBeNull();
|
||||
const endPayload = endNode!.payload as ThreadEndPayload;
|
||||
expect(endPayload.returnCode).toBe(0);
|
||||
expect(endPayload.start).toBeTruthy();
|
||||
expect(endPayload.lastStep).toBeTruthy();
|
||||
});
|
||||
|
||||
test("single-role workflow creates correct chain", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SINGLE_ROLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
worker: { text: "work done", meta: { result: "success" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "Do the thing",
|
||||
moderatorRules: SINGLE_ROLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD02"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
expect(result.returnCode).toBe(0);
|
||||
|
||||
const endNode = store.get(result.rootHash);
|
||||
expect(endNode).not.toBeNull();
|
||||
const endPayload = endNode!.payload as ThreadEndPayload;
|
||||
|
||||
const lastStepNode = store.get(endPayload.lastStep);
|
||||
expect(lastStepNode).not.toBeNull();
|
||||
const lastStepPayload = lastStepNode!.payload as ThreadStepPayload;
|
||||
expect(lastStepPayload.role).toBe("worker");
|
||||
expect(lastStepPayload.previous).toBeNull();
|
||||
|
||||
const startNode = store.get(endPayload.start);
|
||||
expect(startNode).not.toBeNull();
|
||||
const startPayload = startNode!.payload as ThreadStartPayload;
|
||||
expect(startPayload.input).toBe("Do the thing");
|
||||
expect(startPayload.depth).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("CAS node structure", () => {
|
||||
test("thread-start contains workflow ref, input, depth, agents", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SINGLE_ROLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
worker: { text: "ok", meta: { result: "ok" } },
|
||||
});
|
||||
|
||||
const agentHash = await store.put(typeHashes.agent, {
|
||||
package: "test-agent",
|
||||
version: "1.0.0",
|
||||
config: {},
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "Test input",
|
||||
moderatorRules: SINGLE_ROLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD03"),
|
||||
options: makeOptions({ agents: { worker: agentHash }, depth: 2 }),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
const startPayload = store.get(endPayload.start)!.payload as ThreadStartPayload;
|
||||
|
||||
expect(startPayload.workflow).toBe(workflowHash);
|
||||
expect(startPayload.input).toBe("Test input");
|
||||
expect(startPayload.depth).toBe(2);
|
||||
expect(startPayload.parentThread).toBeNull();
|
||||
expect(startPayload.agents).toEqual({ worker: agentHash });
|
||||
});
|
||||
|
||||
test("thread-start records parentThread when provided", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SINGLE_ROLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
worker: { text: "nested", meta: { result: "nested" } },
|
||||
});
|
||||
|
||||
const fakeParent = "FAKEPARENT0001";
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "nested task",
|
||||
moderatorRules: SINGLE_ROLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD04"),
|
||||
options: makeOptions({ parentThread: fakeParent, depth: 1 }),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
const startPayload = store.get(endPayload.start)!.payload as ThreadStartPayload;
|
||||
expect(startPayload.parentThread).toBe(fakeParent);
|
||||
expect(startPayload.depth).toBe(1);
|
||||
});
|
||||
|
||||
test("each thread-step has content, react, start, and previous refs", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SIMPLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
planner: { text: "plan text", meta: { plan: "p1" } },
|
||||
coder: { text: "code text", meta: { code: "c1" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "go",
|
||||
moderatorRules: SIMPLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD05"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
const startHash = endPayload.start;
|
||||
|
||||
const step2 = store.get(endPayload.lastStep)!.payload as ThreadStepPayload;
|
||||
expect(step2.role).toBe("coder");
|
||||
expect(step2.start).toBe(startHash);
|
||||
expect(step2.previous).not.toBeNull();
|
||||
|
||||
const contentNode2 = store.get(step2.content);
|
||||
expect(contentNode2).not.toBeNull();
|
||||
expect((contentNode2!.payload as ContentPayload).text).toBe("code text");
|
||||
|
||||
const reactNode2 = store.get(step2.react);
|
||||
expect(reactNode2).not.toBeNull();
|
||||
|
||||
const step1 = store.get(step2.previous!)!.payload as ThreadStepPayload;
|
||||
expect(step1.role).toBe("planner");
|
||||
expect(step1.start).toBe(startHash);
|
||||
expect(step1.previous).toBeNull();
|
||||
|
||||
const contentNode1 = store.get(step1.content);
|
||||
expect(contentNode1).not.toBeNull();
|
||||
expect((contentNode1!.payload as ContentPayload).text).toBe("plan text");
|
||||
});
|
||||
|
||||
test("thread-end references start and last step", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SIMPLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
planner: { text: "plan", meta: { plan: "x" } },
|
||||
coder: { text: "code", meta: { code: "x" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "test",
|
||||
moderatorRules: SIMPLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD06"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
expect(endPayload.returnCode).toBe(0);
|
||||
expect(endPayload.summary).toBeTruthy();
|
||||
|
||||
const startNode = store.get(endPayload.start);
|
||||
expect(startNode).not.toBeNull();
|
||||
expect((startNode!.payload as ThreadStartPayload).workflow).toBe(workflowHash);
|
||||
|
||||
const lastStepNode = store.get(endPayload.lastStep);
|
||||
expect(lastStepNode).not.toBeNull();
|
||||
expect((lastStepNode!.payload as ThreadStepPayload).role).toBe("coder");
|
||||
});
|
||||
|
||||
test("content nodes store the agent text verbatim", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SINGLE_ROLE_WORKFLOW);
|
||||
|
||||
const longText = "This is a longer text with\nnewlines\nand special chars: <>&\"'";
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
worker: { text: longText, meta: { result: "done" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "process this",
|
||||
moderatorRules: SINGLE_ROLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD07"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
const stepPayload = store.get(endPayload.lastStep)!.payload as ThreadStepPayload;
|
||||
const contentPayload = store.get(stepPayload.content)!.payload as ContentPayload;
|
||||
expect(contentPayload.text).toBe(longText);
|
||||
});
|
||||
|
||||
test("meta is stored in thread-step payload", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SIMPLE_WORKFLOW);
|
||||
|
||||
const complexMeta = {
|
||||
plan: "phase-1",
|
||||
phases: [{ hash: "abc", title: "first" }],
|
||||
nested: { deep: true },
|
||||
};
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
planner: { text: "plan", meta: complexMeta },
|
||||
coder: { text: "code", meta: { code: "done" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "go",
|
||||
moderatorRules: SIMPLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD08"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
const step2 = store.get(endPayload.lastStep)!.payload as ThreadStepPayload;
|
||||
const step1 = store.get(step2.previous!)!.payload as ThreadStepPayload;
|
||||
|
||||
expect(step1.meta).toEqual(complexMeta);
|
||||
expect(step2.meta).toEqual({ code: "done" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("moderator routing", () => {
|
||||
test("conditional moderator routes based on agent meta", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, CONDITIONAL_WORKFLOW);
|
||||
|
||||
let checkerCallCount = 0;
|
||||
const agentFn: JsonCasAgentFn = async (role, _sp, _snap) => {
|
||||
if (role === "checker") {
|
||||
checkerCallCount++;
|
||||
if (checkerCallCount === 1) {
|
||||
return { text: "found issue", meta: { status: "bad" }, react: null };
|
||||
}
|
||||
return { text: "all good now", meta: { status: "ok" }, react: null };
|
||||
}
|
||||
return { text: "fixed it", meta: { fix: "patched" }, react: null };
|
||||
};
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "check and fix",
|
||||
moderatorRules: CONDITIONAL_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD09"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
expect(result.returnCode).toBe(0);
|
||||
expect(checkerCallCount).toBe(2);
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
const lastStep = store.get(endPayload.lastStep)!.payload as ThreadStepPayload;
|
||||
expect(lastStep.role).toBe("checker");
|
||||
|
||||
const step2 = store.get(lastStep.previous!)!.payload as ThreadStepPayload;
|
||||
expect(step2.role).toBe("fixer");
|
||||
|
||||
const step1 = store.get(step2.previous!)!.payload as ThreadStepPayload;
|
||||
expect(step1.role).toBe("checker");
|
||||
expect(step1.previous).toBeNull();
|
||||
});
|
||||
|
||||
test("immediate END from moderator still produces a valid thread", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
|
||||
const immediateEnd: WorkflowInput = {
|
||||
name: "test-immediate-end",
|
||||
description: "Ends immediately",
|
||||
roles: {
|
||||
worker: {
|
||||
description: "Never called",
|
||||
systemPrompt: "N/A",
|
||||
extractPrompt: "N/A",
|
||||
schema: { type: "object" },
|
||||
},
|
||||
},
|
||||
moderator: [{ from: START, to: END, when: null }],
|
||||
};
|
||||
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, immediateEnd);
|
||||
|
||||
const agentFn: JsonCasAgentFn = async (): Promise<never> => {
|
||||
throw new Error("should not be called");
|
||||
};
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "skip",
|
||||
moderatorRules: immediateEnd.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD10"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
expect(result.returnCode).toBe(0);
|
||||
|
||||
const endNode = store.get(result.rootHash);
|
||||
expect(endNode).not.toBeNull();
|
||||
const endPayload = endNode!.payload as ThreadEndPayload;
|
||||
expect(endPayload.start).toBeTruthy();
|
||||
expect(endPayload.lastStep).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("abort handling", () => {
|
||||
test("aborted signal produces returnCode 130", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SINGLE_ROLE_WORKFLOW);
|
||||
|
||||
const ac = new AbortController();
|
||||
ac.abort();
|
||||
|
||||
const agentFn: JsonCasAgentFn = async (): Promise<never> => {
|
||||
throw new Error("should not be called");
|
||||
};
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "will abort",
|
||||
moderatorRules: SINGLE_ROLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD11"),
|
||||
options: makeOptions({ signal: ac.signal }),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
expect(result.returnCode).toBe(130);
|
||||
expect(result.summary).toContain("abort");
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
expect(endPayload.returnCode).toBe(130);
|
||||
});
|
||||
});
|
||||
|
||||
describe("agent receives correct context", () => {
|
||||
test("agent receives role name, system prompt, and accumulated steps", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SIMPLE_WORKFLOW);
|
||||
|
||||
const { loadWorkflow } = await import("@uncaged/workflow-json-def");
|
||||
const hydrated = loadWorkflow(store, typeHashes, workflowHash);
|
||||
|
||||
const receivedCalls: Array<{
|
||||
role: string;
|
||||
systemPrompt: string;
|
||||
stepCount: number;
|
||||
input: string;
|
||||
}> = [];
|
||||
|
||||
const agentFn: JsonCasAgentFn = async (role, systemPrompt, snapshot) => {
|
||||
receivedCalls.push({
|
||||
role,
|
||||
systemPrompt,
|
||||
stepCount: snapshot.steps.length,
|
||||
input: snapshot.start.input,
|
||||
});
|
||||
return { text: `output for ${role}`, meta: {}, react: null };
|
||||
};
|
||||
|
||||
await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "my prompt",
|
||||
moderatorRules: SIMPLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD12"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: hydrated,
|
||||
});
|
||||
|
||||
expect(receivedCalls.length).toBe(2);
|
||||
|
||||
expect(receivedCalls[0]!.role).toBe("planner");
|
||||
expect(receivedCalls[0]!.systemPrompt).toBe("You are a planner.");
|
||||
expect(receivedCalls[0]!.stepCount).toBe(0);
|
||||
expect(receivedCalls[0]!.input).toBe("my prompt");
|
||||
|
||||
expect(receivedCalls[1]!.role).toBe("coder");
|
||||
expect(receivedCalls[1]!.systemPrompt).toBe("You are a coder.");
|
||||
expect(receivedCalls[1]!.stepCount).toBe(1);
|
||||
});
|
||||
|
||||
test("snapshot accumulates step meta from previous rounds", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, CONDITIONAL_WORKFLOW);
|
||||
|
||||
let round = 0;
|
||||
const snapshots: Array<{
|
||||
role: string;
|
||||
steps: readonly { role: string; meta: Record<string, unknown> }[];
|
||||
}> = [];
|
||||
|
||||
const agentFn: JsonCasAgentFn = async (role, _sp, snapshot) => {
|
||||
snapshots.push({ role, steps: [...snapshot.steps] });
|
||||
round++;
|
||||
if (role === "checker") {
|
||||
return round === 1
|
||||
? { text: "bad", meta: { status: "bad" }, react: null }
|
||||
: { text: "ok", meta: { status: "ok" }, react: null };
|
||||
}
|
||||
return { text: "fixed", meta: { fix: "yes" }, react: null };
|
||||
};
|
||||
|
||||
await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "go",
|
||||
moderatorRules: CONDITIONAL_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD13"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
expect(snapshots.length).toBe(3);
|
||||
|
||||
expect(snapshots[0]!.steps.length).toBe(0);
|
||||
|
||||
expect(snapshots[1]!.steps.length).toBe(1);
|
||||
expect(snapshots[1]!.steps[0]!.role).toBe("checker");
|
||||
expect(snapshots[1]!.steps[0]!.meta).toEqual({ status: "bad" });
|
||||
|
||||
expect(snapshots[2]!.steps.length).toBe(2);
|
||||
expect(snapshots[2]!.steps[0]!.role).toBe("checker");
|
||||
expect(snapshots[2]!.steps[1]!.role).toBe("fixer");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildJsonCasThreadSnapshot", () => {
|
||||
test("builds snapshot from start + step chain", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SIMPLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
planner: { text: "plan text", meta: { plan: "alpha" } },
|
||||
coder: { text: "code text", meta: { code: "beta" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "build it",
|
||||
moderatorRules: SIMPLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD_SNAP"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
const startHash = endPayload.start;
|
||||
const lastStepHash = endPayload.lastStep;
|
||||
|
||||
const snapshot = buildJsonCasThreadSnapshot(
|
||||
store,
|
||||
typeHashes,
|
||||
startHash,
|
||||
lastStepHash,
|
||||
"THREAD_SNAP",
|
||||
);
|
||||
|
||||
expect(snapshot.threadId).toBe("THREAD_SNAP");
|
||||
expect(snapshot.start.input).toBe("build it");
|
||||
expect(snapshot.start.workflowHash).toBe(workflowHash);
|
||||
expect(snapshot.steps.length).toBe(2);
|
||||
expect(snapshot.steps[0]!.role).toBe("planner");
|
||||
expect(snapshot.steps[0]!.meta).toEqual({ plan: "alpha" });
|
||||
expect(snapshot.steps[1]!.role).toBe("coder");
|
||||
expect(snapshot.steps[1]!.meta).toEqual({ code: "beta" });
|
||||
});
|
||||
|
||||
test("builds snapshot with null headStepHash (start only)", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SINGLE_ROLE_WORKFLOW);
|
||||
|
||||
const startHash = await store.put(typeHashes.threadStart, {
|
||||
workflow: workflowHash,
|
||||
input: "just started",
|
||||
depth: 0,
|
||||
parentThread: null,
|
||||
agents: {},
|
||||
});
|
||||
|
||||
const snapshot = buildJsonCasThreadSnapshot(store, typeHashes, startHash, null, "THREAD_SNAP2");
|
||||
|
||||
expect(snapshot.threadId).toBe("THREAD_SNAP2");
|
||||
expect(snapshot.start.input).toBe("just started");
|
||||
expect(snapshot.steps.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildJsonCasThreadContext", () => {
|
||||
test("builds a protocol-compatible ThreadContext", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SIMPLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
planner: { text: "plan text", meta: { plan: "ctx-test" } },
|
||||
coder: { text: "code text", meta: { code: "ctx-done" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "context test",
|
||||
moderatorRules: SIMPLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD_CTX"),
|
||||
options: makeOptions({ depth: 3 }),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
const ctx = buildJsonCasThreadContext(store, typeHashes, endPayload.start, endPayload.lastStep);
|
||||
|
||||
expect(ctx.threadId).toBe("");
|
||||
expect(ctx.depth).toBe(3);
|
||||
expect(ctx.bundleHash).toBe(workflowHash);
|
||||
expect(ctx.start.role).toBe("__start__");
|
||||
expect(ctx.start.content).toBe("context test");
|
||||
expect(ctx.steps.length).toBe(2);
|
||||
expect(ctx.steps[0]!.role).toBe("planner");
|
||||
expect(ctx.steps[0]!.meta).toEqual({ plan: "ctx-test" });
|
||||
expect(ctx.steps[1]!.role).toBe("coder");
|
||||
expect(ctx.steps[1]!.meta).toEqual({ code: "ctx-done" });
|
||||
});
|
||||
|
||||
test("context from start-only thread has empty steps", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SINGLE_ROLE_WORKFLOW);
|
||||
|
||||
const startHash = await store.put(typeHashes.threadStart, {
|
||||
workflow: workflowHash,
|
||||
input: "start only",
|
||||
depth: 0,
|
||||
parentThread: null,
|
||||
agents: {},
|
||||
});
|
||||
|
||||
const ctx = buildJsonCasThreadContext(store, typeHashes, startHash, null);
|
||||
|
||||
expect(ctx.start.content).toBe("start only");
|
||||
expect(ctx.steps.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("readContentText", () => {
|
||||
test("reads text from a content node", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const hash = await store.put(typeHashes.content, { text: "hello world" });
|
||||
|
||||
const text = readContentText(store, hash);
|
||||
expect(text).toBe("hello world");
|
||||
});
|
||||
|
||||
test("returns null for missing hash", async () => {
|
||||
const { store } = await setupStore();
|
||||
const text = readContentText(store, "NONEXISTENT0001");
|
||||
expect(text).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("CAS graph integrity", () => {
|
||||
test("all nodes are reachable via walk from thread-end", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SIMPLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
planner: { text: "plan", meta: { plan: "x" } },
|
||||
coder: { text: "code", meta: { code: "y" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "walk test",
|
||||
moderatorRules: SIMPLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD_WALK"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const visited = new Set<string>();
|
||||
walk(store, result.rootHash, (hash) => {
|
||||
visited.add(hash);
|
||||
});
|
||||
|
||||
expect(visited.has(result.rootHash)).toBe(true);
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
expect(visited.has(endPayload.start)).toBe(true);
|
||||
expect(visited.has(endPayload.lastStep)).toBe(true);
|
||||
|
||||
const step2 = store.get(endPayload.lastStep)!.payload as ThreadStepPayload;
|
||||
expect(visited.has(step2.content)).toBe(true);
|
||||
expect(visited.has(step2.react)).toBe(true);
|
||||
expect(visited.has(step2.start)).toBe(true);
|
||||
|
||||
if (step2.previous !== null) {
|
||||
expect(visited.has(step2.previous)).toBe(true);
|
||||
const step1 = store.get(step2.previous)!.payload as ThreadStepPayload;
|
||||
expect(visited.has(step1.content)).toBe(true);
|
||||
expect(visited.has(step1.react)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
test("react session nodes have empty structure when agent returns react: null", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { workflowHash } = await setupWorkflow(store, typeHashes, SINGLE_ROLE_WORKFLOW);
|
||||
|
||||
const agentFn = createMockAgent({
|
||||
worker: { text: "w", meta: { result: "r" } },
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "react check",
|
||||
moderatorRules: SINGLE_ROLE_WORKFLOW.moderator,
|
||||
io: makeIo(store, typeHashes, "THREAD_REACT"),
|
||||
options: makeOptions(),
|
||||
agentFn,
|
||||
logger: noLogger(),
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!.payload as ThreadEndPayload;
|
||||
const stepPayload = store.get(endPayload.lastStep)!.payload as ThreadStepPayload;
|
||||
const reactNode = store.get(stepPayload.react);
|
||||
|
||||
expect(reactNode).not.toBeNull();
|
||||
const reactPayload = reactNode!.payload as Record<string, unknown>;
|
||||
expect(reactPayload.turns).toEqual([]);
|
||||
expect(reactPayload.totalTokens).toBe(0);
|
||||
expect(reactPayload.durationMs).toBe(0);
|
||||
expect(reactPayload.role).toBe("worker");
|
||||
expect(typeof reactPayload.agent).toBe("string");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,415 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createMemoryStore } from "@uncaged/json-cas";
|
||||
import {
|
||||
type ContentPayload,
|
||||
type ReactSessionPayload,
|
||||
type ReactToolCallPayload,
|
||||
type ReactTurnPayload,
|
||||
registerWorkflowSchemas,
|
||||
} from "@uncaged/json-cas-workflow";
|
||||
|
||||
import { writeReactSession } from "../src/engine/json-cas-react-recorder.js";
|
||||
import type { ReactTrace } from "../src/engine/json-cas-types.js";
|
||||
|
||||
// ── Fixtures ──────────────────────────────────────────────────────────
|
||||
|
||||
async function setupStore() {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
return { store, typeHashes };
|
||||
}
|
||||
|
||||
async function makeFakeAgent(
|
||||
store: Awaited<ReturnType<typeof setupStore>>["store"],
|
||||
typeHashes: Awaited<ReturnType<typeof setupStore>>["typeHashes"],
|
||||
) {
|
||||
return store.put(typeHashes.agent, {
|
||||
package: "test-agent",
|
||||
version: "1.0.0",
|
||||
config: {},
|
||||
});
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────
|
||||
|
||||
describe("writeReactSession", () => {
|
||||
describe("empty trace", () => {
|
||||
test("produces a react-session with zero turns", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const agentHash = await makeFakeAgent(store, typeHashes);
|
||||
|
||||
const trace: ReactTrace = { turns: [], totalTokens: 0, durationMs: 0 };
|
||||
|
||||
const sessionHash = await writeReactSession(store, typeHashes, {
|
||||
agentHash,
|
||||
role: "worker",
|
||||
trace,
|
||||
});
|
||||
|
||||
const node = store.get(sessionHash);
|
||||
expect(node).not.toBeNull();
|
||||
const payload = node!.payload as ReactSessionPayload;
|
||||
expect(payload.agent).toBe(agentHash);
|
||||
expect(payload.role).toBe("worker");
|
||||
expect(payload.turns).toEqual([]);
|
||||
expect(payload.totalTokens).toBe(0);
|
||||
expect(payload.durationMs).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("single turn, no tool calls", () => {
|
||||
test("produces react-session → react-turn → content nodes", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const agentHash = await makeFakeAgent(store, typeHashes);
|
||||
|
||||
const trace: ReactTrace = {
|
||||
turns: [
|
||||
{
|
||||
input: "What is 2+2?",
|
||||
output: "4",
|
||||
toolCalls: [],
|
||||
tokens: { input: 10, output: 5 },
|
||||
latencyMs: 200,
|
||||
},
|
||||
],
|
||||
totalTokens: 15,
|
||||
durationMs: 200,
|
||||
};
|
||||
|
||||
const sessionHash = await writeReactSession(store, typeHashes, {
|
||||
agentHash,
|
||||
role: "solver",
|
||||
trace,
|
||||
});
|
||||
|
||||
const session = store.get(sessionHash)!.payload as ReactSessionPayload;
|
||||
expect(session.turns.length).toBe(1);
|
||||
expect(session.totalTokens).toBe(15);
|
||||
expect(session.durationMs).toBe(200);
|
||||
expect(session.role).toBe("solver");
|
||||
|
||||
const turnHash = session.turns[0]!;
|
||||
const turn = store.get(turnHash)!.payload as ReactTurnPayload;
|
||||
expect(turn.toolCalls).toEqual([]);
|
||||
expect(turn.tokens).toEqual({ input: 10, output: 5 });
|
||||
expect(turn.latencyMs).toBe(200);
|
||||
|
||||
const inputContent = store.get(turn.input)!.payload as ContentPayload;
|
||||
expect(inputContent.text).toBe("What is 2+2?");
|
||||
|
||||
const outputContent = store.get(turn.output)!.payload as ContentPayload;
|
||||
expect(outputContent.text).toBe("4");
|
||||
});
|
||||
});
|
||||
|
||||
describe("single turn with tool calls", () => {
|
||||
test("serialises tool calls to react-tool-call → content nodes", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const agentHash = await makeFakeAgent(store, typeHashes);
|
||||
|
||||
const trace: ReactTrace = {
|
||||
turns: [
|
||||
{
|
||||
input: "Search for cats",
|
||||
output: "Found 42 cats",
|
||||
toolCalls: [
|
||||
{
|
||||
name: "search",
|
||||
arguments: '{"query":"cats"}',
|
||||
result: '{"count":42}',
|
||||
durationMs: 80,
|
||||
},
|
||||
],
|
||||
tokens: { input: 20, output: 10 },
|
||||
latencyMs: 350,
|
||||
},
|
||||
],
|
||||
totalTokens: 30,
|
||||
durationMs: 350,
|
||||
};
|
||||
|
||||
const sessionHash = await writeReactSession(store, typeHashes, {
|
||||
agentHash,
|
||||
role: "searcher",
|
||||
trace,
|
||||
});
|
||||
|
||||
const session = store.get(sessionHash)!.payload as ReactSessionPayload;
|
||||
const turn = store.get(session.turns[0]!)!.payload as ReactTurnPayload;
|
||||
expect(turn.toolCalls.length).toBe(1);
|
||||
|
||||
const toolCall = store.get(turn.toolCalls[0]!)!.payload as ReactToolCallPayload;
|
||||
expect(toolCall.name).toBe("search");
|
||||
expect(toolCall.durationMs).toBe(80);
|
||||
|
||||
const argsContent = store.get(toolCall.arguments)!.payload as ContentPayload;
|
||||
expect(argsContent.text).toBe('{"query":"cats"}');
|
||||
|
||||
const resultContent = store.get(toolCall.result)!.payload as ContentPayload;
|
||||
expect(resultContent.text).toBe('{"count":42}');
|
||||
});
|
||||
|
||||
test("multiple tool calls in one turn are all recorded", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const agentHash = await makeFakeAgent(store, typeHashes);
|
||||
|
||||
const trace: ReactTrace = {
|
||||
turns: [
|
||||
{
|
||||
input: "Do two things",
|
||||
output: "Done",
|
||||
toolCalls: [
|
||||
{ name: "tool_a", arguments: '{"x":1}', result: '"ok_a"', durationMs: 10 },
|
||||
{ name: "tool_b", arguments: '{"y":2}', result: '"ok_b"', durationMs: 20 },
|
||||
],
|
||||
tokens: { input: 5, output: 3 },
|
||||
latencyMs: 100,
|
||||
},
|
||||
],
|
||||
totalTokens: 8,
|
||||
durationMs: 100,
|
||||
};
|
||||
|
||||
const sessionHash = await writeReactSession(store, typeHashes, {
|
||||
agentHash,
|
||||
role: "doer",
|
||||
trace,
|
||||
});
|
||||
|
||||
const session = store.get(sessionHash)!.payload as ReactSessionPayload;
|
||||
const turn = store.get(session.turns[0]!)!.payload as ReactTurnPayload;
|
||||
expect(turn.toolCalls.length).toBe(2);
|
||||
|
||||
const tc0 = store.get(turn.toolCalls[0]!)!.payload as ReactToolCallPayload;
|
||||
expect(tc0.name).toBe("tool_a");
|
||||
|
||||
const tc1 = store.get(turn.toolCalls[1]!)!.payload as ReactToolCallPayload;
|
||||
expect(tc1.name).toBe("tool_b");
|
||||
});
|
||||
});
|
||||
|
||||
describe("multiple turns", () => {
|
||||
test("each turn is stored as a separate react-turn node", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const agentHash = await makeFakeAgent(store, typeHashes);
|
||||
|
||||
const trace: ReactTrace = {
|
||||
turns: [
|
||||
{
|
||||
input: "Round 1 prompt",
|
||||
output: "Round 1 response",
|
||||
toolCalls: [],
|
||||
tokens: { input: 10, output: 8 },
|
||||
latencyMs: 100,
|
||||
},
|
||||
{
|
||||
input: "Round 2 prompt",
|
||||
output: "Round 2 response",
|
||||
toolCalls: [],
|
||||
tokens: { input: 12, output: 6 },
|
||||
latencyMs: 120,
|
||||
},
|
||||
],
|
||||
totalTokens: 36,
|
||||
durationMs: 220,
|
||||
};
|
||||
|
||||
const sessionHash = await writeReactSession(store, typeHashes, {
|
||||
agentHash,
|
||||
role: "multi",
|
||||
trace,
|
||||
});
|
||||
|
||||
const session = store.get(sessionHash)!.payload as ReactSessionPayload;
|
||||
expect(session.turns.length).toBe(2);
|
||||
expect(session.totalTokens).toBe(36);
|
||||
expect(session.durationMs).toBe(220);
|
||||
|
||||
// Turns must be distinct nodes
|
||||
expect(session.turns[0]).not.toBe(session.turns[1]);
|
||||
|
||||
const turn0 = store.get(session.turns[0]!)!.payload as ReactTurnPayload;
|
||||
expect((store.get(turn0.input)!.payload as ContentPayload).text).toBe("Round 1 prompt");
|
||||
expect(turn0.tokens).toEqual({ input: 10, output: 8 });
|
||||
|
||||
const turn1 = store.get(session.turns[1]!)!.payload as ReactTurnPayload;
|
||||
expect((store.get(turn1.input)!.payload as ContentPayload).text).toBe("Round 2 prompt");
|
||||
expect(turn1.tokens).toEqual({ input: 12, output: 6 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("token and duration values", () => {
|
||||
test("token counts and latency are preserved exactly", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const agentHash = await makeFakeAgent(store, typeHashes);
|
||||
|
||||
const trace: ReactTrace = {
|
||||
turns: [
|
||||
{
|
||||
input: "p",
|
||||
output: "r",
|
||||
toolCalls: [],
|
||||
tokens: { input: 9999, output: 1234 },
|
||||
latencyMs: 5678,
|
||||
},
|
||||
],
|
||||
totalTokens: 11233,
|
||||
durationMs: 5678,
|
||||
};
|
||||
|
||||
const sessionHash = await writeReactSession(store, typeHashes, {
|
||||
agentHash,
|
||||
role: "counter",
|
||||
trace,
|
||||
});
|
||||
|
||||
const session = store.get(sessionHash)!.payload as ReactSessionPayload;
|
||||
expect(session.totalTokens).toBe(11233);
|
||||
expect(session.durationMs).toBe(5678);
|
||||
|
||||
const turn = store.get(session.turns[0]!)!.payload as ReactTurnPayload;
|
||||
expect(turn.tokens.input).toBe(9999);
|
||||
expect(turn.tokens.output).toBe(1234);
|
||||
expect(turn.latencyMs).toBe(5678);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("writeReactSession + executeJsonCasThread integration", () => {
|
||||
test("engine stores real react session when agent provides react trace", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { registerWorkflow } = await import("@uncaged/workflow-json-def");
|
||||
const { executeJsonCasThread } = await import("../src/engine/json-cas-engine.js");
|
||||
type JsonCasAgentFn = import("../src/engine/json-cas-types.js").JsonCasAgentFn;
|
||||
|
||||
const workflowHash = await registerWorkflow(store, typeHashes, {
|
||||
name: "react-test",
|
||||
description: "Tests react instrumentation",
|
||||
roles: {
|
||||
solver: {
|
||||
description: "Solves",
|
||||
systemPrompt: "Solve it.",
|
||||
extractPrompt: "Extract.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["answer"],
|
||||
properties: { answer: { type: "string" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
moderator: [
|
||||
{ from: "__start__", to: "solver", when: null },
|
||||
{ from: "solver", to: "__end__", when: null },
|
||||
],
|
||||
});
|
||||
|
||||
const agentFn: JsonCasAgentFn = async () => ({
|
||||
text: "The answer is 42",
|
||||
meta: { answer: "42" },
|
||||
react: {
|
||||
turns: [
|
||||
{
|
||||
input: "Solve it. What is the answer?",
|
||||
output: "The answer is 42",
|
||||
toolCalls: [],
|
||||
tokens: { input: 15, output: 8 },
|
||||
latencyMs: 300,
|
||||
},
|
||||
],
|
||||
totalTokens: 23,
|
||||
durationMs: 300,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "What is the answer?",
|
||||
moderatorRules: [
|
||||
{ from: "__start__", to: "solver", when: null },
|
||||
{ from: "solver", to: "__end__", when: null },
|
||||
],
|
||||
io: { threadId: "REACT_INTEG", store, typeHashes },
|
||||
options: { depth: 0, parentThread: null, signal: new AbortController().signal, agents: {} },
|
||||
agentFn,
|
||||
logger: () => {},
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!
|
||||
.payload as import("@uncaged/json-cas-workflow").ThreadEndPayload;
|
||||
const stepPayload = store.get(endPayload.lastStep)!
|
||||
.payload as import("@uncaged/json-cas-workflow").ThreadStepPayload;
|
||||
const session = store.get(stepPayload.react)!.payload as ReactSessionPayload;
|
||||
|
||||
expect(session.turns.length).toBe(1);
|
||||
expect(session.totalTokens).toBe(23);
|
||||
expect(session.durationMs).toBe(300);
|
||||
expect(session.role).toBe("solver");
|
||||
|
||||
const turn = store.get(session.turns[0]!)!.payload as ReactTurnPayload;
|
||||
expect(turn.tokens).toEqual({ input: 15, output: 8 });
|
||||
expect(turn.latencyMs).toBe(300);
|
||||
expect((store.get(turn.input)!.payload as ContentPayload).text).toBe(
|
||||
"Solve it. What is the answer?",
|
||||
);
|
||||
});
|
||||
|
||||
test("engine falls back to empty react-session when react is null", async () => {
|
||||
const { store, typeHashes } = await setupStore();
|
||||
const { registerWorkflow } = await import("@uncaged/workflow-json-def");
|
||||
const { executeJsonCasThread } = await import("../src/engine/json-cas-engine.js");
|
||||
type JsonCasAgentFn = import("../src/engine/json-cas-types.js").JsonCasAgentFn;
|
||||
|
||||
const workflowHash = await registerWorkflow(store, typeHashes, {
|
||||
name: "null-react-test",
|
||||
description: "Tests null react fallback",
|
||||
roles: {
|
||||
worker: {
|
||||
description: "Works",
|
||||
systemPrompt: "Work.",
|
||||
extractPrompt: "Extract.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["result"],
|
||||
properties: { result: { type: "string" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
moderator: [
|
||||
{ from: "__start__", to: "worker", when: null },
|
||||
{ from: "worker", to: "__end__", when: null },
|
||||
],
|
||||
});
|
||||
|
||||
const agentFn: JsonCasAgentFn = async () => ({
|
||||
text: "done",
|
||||
meta: { result: "done" },
|
||||
react: null,
|
||||
});
|
||||
|
||||
const result = await executeJsonCasThread({
|
||||
workflowHash,
|
||||
input: "do it",
|
||||
moderatorRules: [
|
||||
{ from: "__start__", to: "worker", when: null },
|
||||
{ from: "worker", to: "__end__", when: null },
|
||||
],
|
||||
io: { threadId: "NULL_REACT", store, typeHashes },
|
||||
options: { depth: 0, parentThread: null, signal: new AbortController().signal, agents: {} },
|
||||
agentFn,
|
||||
logger: () => {},
|
||||
workflow: null,
|
||||
});
|
||||
|
||||
const endPayload = store.get(result.rootHash)!
|
||||
.payload as import("@uncaged/json-cas-workflow").ThreadEndPayload;
|
||||
const stepPayload = store.get(endPayload.lastStep)!
|
||||
.payload as import("@uncaged/json-cas-workflow").ThreadStepPayload;
|
||||
const session = store.get(stepPayload.react)!.payload as ReactSessionPayload;
|
||||
|
||||
expect(session.turns).toEqual([]);
|
||||
expect(session.totalTokens).toBe(0);
|
||||
expect(session.durationMs).toBe(0);
|
||||
expect(session.role).toBe("worker");
|
||||
});
|
||||
});
|
||||
@@ -24,6 +24,9 @@
|
||||
"@uncaged/workflow-cas": "workspace:^",
|
||||
"@uncaged/workflow-reactor": "workspace:^",
|
||||
"@uncaged/workflow-register": "workspace:^",
|
||||
"@uncaged/json-cas": "file:../../../json-cas/packages/json-cas",
|
||||
"@uncaged/json-cas-workflow": "file:../../../json-cas/packages/json-cas-workflow",
|
||||
"@uncaged/workflow-json-def": "workspace:^",
|
||||
"yaml": "^2.7.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@@ -7,6 +7,27 @@ export {
|
||||
walkStateFramesNewestFirst,
|
||||
} from "./fork-thread.js";
|
||||
export { garbageCollectCas } from "./gc.js";
|
||||
export {
|
||||
buildJsonCasThreadContext,
|
||||
buildJsonCasThreadSnapshot,
|
||||
readContentText,
|
||||
} from "./json-cas-context.js";
|
||||
export { executeJsonCasThread } from "./json-cas-engine.js";
|
||||
export { writeReactSession } from "./json-cas-react-recorder.js";
|
||||
export type {
|
||||
AgentBindings,
|
||||
JsonCasAgentFn,
|
||||
JsonCasAgentResult,
|
||||
JsonCasEngineIo,
|
||||
JsonCasEngineOptions,
|
||||
JsonCasStartSnapshot,
|
||||
JsonCasStepSnapshot,
|
||||
JsonCasThreadPauseGate,
|
||||
JsonCasThreadSnapshot,
|
||||
ReactToolCallTrace,
|
||||
ReactTrace,
|
||||
ReactTurnTrace,
|
||||
} from "./json-cas-types.js";
|
||||
export { createThreadPauseGate } from "./thread-pause-gate.js";
|
||||
export type { ThreadHistoryEntry, ThreadIndex, ThreadIndexEntry } from "./threads-index.js";
|
||||
export {
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import type {
|
||||
ContentPayload,
|
||||
ThreadStartPayload,
|
||||
ThreadStepPayload,
|
||||
WorkflowSchemaHashes,
|
||||
} from "@uncaged/json-cas-workflow";
|
||||
import type { ThreadContext } from "@uncaged/workflow-protocol";
|
||||
import { START } from "@uncaged/workflow-protocol";
|
||||
|
||||
import type { JsonCasStepSnapshot, JsonCasThreadSnapshot } from "./json-cas-types.js";
|
||||
|
||||
// ── Snapshot builder (lightweight, for agent & moderator) ─────────────
|
||||
|
||||
/**
|
||||
* Walk the thread-step chain backwards via `previous` refs, then reverse
|
||||
* to get chronological order. Returns a {@link JsonCasThreadSnapshot}.
|
||||
*/
|
||||
export function buildJsonCasThreadSnapshot(
|
||||
store: Store,
|
||||
_typeHashes: WorkflowSchemaHashes,
|
||||
startHash: Hash,
|
||||
headStepHash: Hash | null,
|
||||
threadId: string,
|
||||
): JsonCasThreadSnapshot {
|
||||
const startNode = store.get(startHash);
|
||||
if (startNode === null) {
|
||||
throw new Error(`buildJsonCasThreadSnapshot: missing thread-start node at ${startHash}`);
|
||||
}
|
||||
const startPayload = startNode.payload as ThreadStartPayload;
|
||||
|
||||
const steps: JsonCasStepSnapshot[] = [];
|
||||
|
||||
let cursor: Hash | null = headStepHash;
|
||||
while (cursor !== null) {
|
||||
const stepNode = store.get(cursor);
|
||||
if (stepNode === null) {
|
||||
throw new Error(`buildJsonCasThreadSnapshot: missing thread-step node at ${cursor}`);
|
||||
}
|
||||
const stepPayload = stepNode.payload as ThreadStepPayload;
|
||||
steps.push({
|
||||
role: stepPayload.role,
|
||||
meta: stepPayload.meta,
|
||||
contentHash: stepPayload.content,
|
||||
});
|
||||
cursor = stepPayload.previous;
|
||||
}
|
||||
|
||||
steps.reverse();
|
||||
|
||||
return {
|
||||
threadId,
|
||||
start: {
|
||||
input: startPayload.input,
|
||||
depth: startPayload.depth,
|
||||
workflowHash: startPayload.workflow,
|
||||
},
|
||||
steps,
|
||||
};
|
||||
}
|
||||
|
||||
// ── ThreadContext builder (protocol-compatible) ───────────────────────
|
||||
|
||||
/**
|
||||
* Build a full {@link ThreadContext} from a json-cas thread chain.
|
||||
* Reads the thread-start node, walks thread-step backwards, and resolves
|
||||
* content text from each step's content node.
|
||||
*
|
||||
* `bundleHash` is set from the workflow ref in the thread-start payload.
|
||||
* `threadId` is set to `""` — callers should overwrite when known.
|
||||
*/
|
||||
export function buildJsonCasThreadContext(
|
||||
store: Store,
|
||||
_typeHashes: WorkflowSchemaHashes,
|
||||
startHash: Hash,
|
||||
headStepHash: Hash | null,
|
||||
): ThreadContext {
|
||||
const startNode = store.get(startHash);
|
||||
if (startNode === null) {
|
||||
throw new Error(`buildJsonCasThreadContext: missing thread-start node at ${startHash}`);
|
||||
}
|
||||
const startPayload = startNode.payload as ThreadStartPayload;
|
||||
|
||||
const rawSteps: ThreadStepPayload[] = [];
|
||||
let cursor: Hash | null = headStepHash;
|
||||
while (cursor !== null) {
|
||||
const stepNode = store.get(cursor);
|
||||
if (stepNode === null) {
|
||||
throw new Error(`buildJsonCasThreadContext: missing thread-step node at ${cursor}`);
|
||||
}
|
||||
const payload = stepNode.payload as ThreadStepPayload;
|
||||
rawSteps.push(payload);
|
||||
cursor = payload.previous;
|
||||
}
|
||||
rawSteps.reverse();
|
||||
|
||||
const steps = rawSteps.map((sp) => ({
|
||||
role: sp.role,
|
||||
meta: sp.meta,
|
||||
contentHash: sp.content,
|
||||
refs: [] as string[],
|
||||
timestamp: 0,
|
||||
}));
|
||||
|
||||
return {
|
||||
threadId: "",
|
||||
depth: startPayload.depth,
|
||||
bundleHash: startPayload.workflow,
|
||||
start: {
|
||||
role: START,
|
||||
content: startPayload.input,
|
||||
meta: {},
|
||||
timestamp: 0,
|
||||
parentState: startPayload.parentThread,
|
||||
},
|
||||
steps,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the text payload from a content node.
|
||||
*/
|
||||
export function readContentText(store: Store, contentHash: Hash): string | null {
|
||||
const node = store.get(contentHash);
|
||||
if (node === null) {
|
||||
return null;
|
||||
}
|
||||
const payload = node.payload as ContentPayload;
|
||||
return payload.text;
|
||||
}
|
||||
@@ -0,0 +1,326 @@
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import type {
|
||||
ContentPayload,
|
||||
ThreadEndPayload,
|
||||
ThreadStartPayload,
|
||||
ThreadStepPayload,
|
||||
WorkflowSchemaHashes,
|
||||
} from "@uncaged/json-cas-workflow";
|
||||
import type { HydratedWorkflow } from "@uncaged/workflow-json-def";
|
||||
import type { ModeratorRule, WorkflowResult } from "@uncaged/workflow-protocol";
|
||||
import { END, evaluateModerator, START } from "@uncaged/workflow-protocol";
|
||||
import type { LogFn } from "@uncaged/workflow-util";
|
||||
|
||||
import { writeReactSession } from "./json-cas-react-recorder.js";
|
||||
|
||||
import type {
|
||||
AgentBindings,
|
||||
JsonCasAgentFn,
|
||||
JsonCasEngineIo,
|
||||
JsonCasEngineOptions,
|
||||
JsonCasStepSnapshot,
|
||||
JsonCasThreadSnapshot,
|
||||
} from "./json-cas-types.js";
|
||||
|
||||
// ── Helpers: CAS node writers ─────────────────────────────────────────
|
||||
|
||||
async function writeContent(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
text: string,
|
||||
): Promise<Hash> {
|
||||
const payload: ContentPayload = { text };
|
||||
return store.put(typeHashes.content, payload);
|
||||
}
|
||||
|
||||
async function writeEmptyReactSession(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
role: string,
|
||||
agentHash: Hash,
|
||||
): Promise<Hash> {
|
||||
return store.put(typeHashes.reactSession, {
|
||||
agent: agentHash,
|
||||
role,
|
||||
turns: [],
|
||||
totalTokens: 0,
|
||||
durationMs: 0,
|
||||
});
|
||||
}
|
||||
|
||||
async function writeThreadStart(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
params: {
|
||||
workflowHash: Hash;
|
||||
input: string;
|
||||
depth: number;
|
||||
parentThread: Hash | null;
|
||||
agents: AgentBindings;
|
||||
},
|
||||
): Promise<Hash> {
|
||||
const payload: ThreadStartPayload = {
|
||||
workflow: params.workflowHash,
|
||||
input: params.input,
|
||||
depth: params.depth,
|
||||
parentThread: params.parentThread,
|
||||
agents: params.agents,
|
||||
};
|
||||
return store.put(typeHashes.threadStart, payload);
|
||||
}
|
||||
|
||||
async function writeThreadStep(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
params: {
|
||||
role: string;
|
||||
meta: Record<string, unknown>;
|
||||
contentHash: Hash;
|
||||
reactHash: Hash;
|
||||
startHash: Hash;
|
||||
previousHash: Hash | null;
|
||||
},
|
||||
): Promise<Hash> {
|
||||
const payload: ThreadStepPayload = {
|
||||
role: params.role,
|
||||
meta: params.meta,
|
||||
content: params.contentHash,
|
||||
react: params.reactHash,
|
||||
start: params.startHash,
|
||||
previous: params.previousHash,
|
||||
};
|
||||
return store.put(typeHashes.threadStep, payload);
|
||||
}
|
||||
|
||||
async function writeThreadEnd(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
params: {
|
||||
returnCode: number;
|
||||
summary: string;
|
||||
startHash: Hash;
|
||||
lastStepHash: Hash;
|
||||
},
|
||||
): Promise<Hash> {
|
||||
const payload: ThreadEndPayload = {
|
||||
returnCode: params.returnCode,
|
||||
summary: params.summary,
|
||||
start: params.startHash,
|
||||
lastStep: params.lastStepHash,
|
||||
};
|
||||
return store.put(typeHashes.threadEnd, payload);
|
||||
}
|
||||
|
||||
// ── Placeholder agent ─────────────────────────────────────────────────
|
||||
|
||||
async function ensurePlaceholderAgent(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
): Promise<Hash> {
|
||||
return store.put(typeHashes.agent, {
|
||||
package: "placeholder",
|
||||
version: "0.0.0",
|
||||
config: {},
|
||||
});
|
||||
}
|
||||
|
||||
// ── JSONata moderator adapter ─────────────────────────────────────────
|
||||
|
||||
function snapshotToModeratorContext(
|
||||
snapshot: JsonCasThreadSnapshot,
|
||||
): Parameters<typeof evaluateModerator>[1] {
|
||||
return {
|
||||
threadId: snapshot.threadId,
|
||||
depth: snapshot.start.depth,
|
||||
bundleHash: snapshot.start.workflowHash,
|
||||
start: {
|
||||
role: START,
|
||||
content: snapshot.start.input,
|
||||
meta: {},
|
||||
timestamp: 0,
|
||||
parentState: null,
|
||||
},
|
||||
steps: snapshot.steps.map((s) => ({
|
||||
role: s.role,
|
||||
meta: s.meta,
|
||||
contentHash: s.contentHash,
|
||||
refs: [],
|
||||
timestamp: 0,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// ── Main engine ───────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Execute a workflow thread using json-cas as the storage layer.
|
||||
*
|
||||
* Drives the moderator→agent loop:
|
||||
* 1. Writes a thread-start node.
|
||||
* 2. On each round: evaluates the moderator, invokes the agent, writes
|
||||
* content + thread-step nodes (react is a placeholder for now).
|
||||
* 3. On END: writes a thread-end node and returns the result.
|
||||
*
|
||||
* The `agentFn` callback is invoked for each role step. It receives the
|
||||
* role name, system prompt, and current thread snapshot, and returns the
|
||||
* agent's text output plus structured meta.
|
||||
*/
|
||||
export async function executeJsonCasThread(params: {
|
||||
workflowHash: Hash;
|
||||
input: string;
|
||||
moderatorRules: readonly ModeratorRule[];
|
||||
io: JsonCasEngineIo;
|
||||
options: JsonCasEngineOptions;
|
||||
agentFn: JsonCasAgentFn;
|
||||
logger: LogFn;
|
||||
/** Hydrated workflow for role system prompts. Null disables prompt forwarding. */
|
||||
workflow: HydratedWorkflow | null;
|
||||
}): Promise<WorkflowResult> {
|
||||
const { io, options, agentFn, logger, moderatorRules, workflow } = params;
|
||||
const { store, typeHashes, threadId } = io;
|
||||
|
||||
const placeholderAgentHash = await ensurePlaceholderAgent(store, typeHashes);
|
||||
|
||||
const startHash = await writeThreadStart(store, typeHashes, {
|
||||
workflowHash: params.workflowHash,
|
||||
input: params.input,
|
||||
depth: options.depth,
|
||||
parentThread: options.parentThread,
|
||||
agents: options.agents,
|
||||
});
|
||||
|
||||
logger("X3RK7QWN", `json-cas thread ${threadId} started`);
|
||||
|
||||
let previousStepHash: Hash | null = null;
|
||||
let headStepHash: Hash | null = null;
|
||||
const stepSnapshots: JsonCasStepSnapshot[] = [];
|
||||
|
||||
while (true) {
|
||||
if (options.signal.aborted) {
|
||||
return abortThread(store, typeHashes, startHash, headStepHash, logger, threadId);
|
||||
}
|
||||
|
||||
const snapshot: JsonCasThreadSnapshot = {
|
||||
threadId,
|
||||
start: {
|
||||
input: params.input,
|
||||
depth: options.depth,
|
||||
workflowHash: params.workflowHash,
|
||||
},
|
||||
steps: stepSnapshots,
|
||||
};
|
||||
|
||||
const modCtx = snapshotToModeratorContext(snapshot);
|
||||
const nextRole = await evaluateModerator(moderatorRules, modCtx);
|
||||
|
||||
if (nextRole === END) {
|
||||
logger("Y5TN8RVK", `json-cas thread ${threadId} moderator returned END`);
|
||||
|
||||
if (headStepHash === null) {
|
||||
const dummyContentHash = await writeContent(store, typeHashes, "no-op");
|
||||
const dummyReactHash = await writeEmptyReactSession(
|
||||
store,
|
||||
typeHashes,
|
||||
END,
|
||||
placeholderAgentHash,
|
||||
);
|
||||
headStepHash = await writeThreadStep(store, typeHashes, {
|
||||
role: END,
|
||||
meta: {},
|
||||
contentHash: dummyContentHash,
|
||||
reactHash: dummyReactHash,
|
||||
startHash,
|
||||
previousHash: null,
|
||||
});
|
||||
}
|
||||
|
||||
const endHash = await writeThreadEnd(store, typeHashes, {
|
||||
returnCode: 0,
|
||||
summary: "completed: moderator returned END",
|
||||
startHash,
|
||||
lastStepHash: headStepHash,
|
||||
});
|
||||
|
||||
return { returnCode: 0, summary: "completed: moderator returned END", rootHash: endHash };
|
||||
}
|
||||
|
||||
const roleSystemPrompt =
|
||||
workflow !== null && workflow.roles[nextRole] !== undefined
|
||||
? workflow.roles[nextRole].systemPrompt
|
||||
: "";
|
||||
|
||||
const agentResult = await agentFn(nextRole, roleSystemPrompt, snapshot);
|
||||
|
||||
const contentHash = await writeContent(store, typeHashes, agentResult.text);
|
||||
|
||||
const agentHash = options.agents[nextRole] ?? placeholderAgentHash;
|
||||
const reactHash =
|
||||
agentResult.react !== null
|
||||
? await writeReactSession(store, typeHashes, {
|
||||
agentHash,
|
||||
role: nextRole,
|
||||
trace: agentResult.react,
|
||||
})
|
||||
: await writeEmptyReactSession(store, typeHashes, nextRole, agentHash);
|
||||
|
||||
const stepHash = await writeThreadStep(store, typeHashes, {
|
||||
role: nextRole,
|
||||
meta: agentResult.meta,
|
||||
contentHash,
|
||||
reactHash,
|
||||
startHash,
|
||||
previousHash: previousStepHash,
|
||||
});
|
||||
|
||||
previousStepHash = stepHash;
|
||||
headStepHash = stepHash;
|
||||
stepSnapshots.push({
|
||||
role: nextRole,
|
||||
meta: agentResult.meta,
|
||||
contentHash,
|
||||
});
|
||||
|
||||
logger("Z7WP4NHK", `json-cas thread ${threadId} wrote role ${nextRole}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function abortThread(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
startHash: Hash,
|
||||
headStepHash: Hash | null,
|
||||
logger: LogFn,
|
||||
threadId: string,
|
||||
): Promise<WorkflowResult> {
|
||||
logger("A8QK3VNR", `json-cas thread ${threadId} aborted`);
|
||||
|
||||
const placeholderAgentHash = await ensurePlaceholderAgent(store, typeHashes);
|
||||
|
||||
let lastStep = headStepHash;
|
||||
if (lastStep === null) {
|
||||
const dummyContentHash = await writeContent(store, typeHashes, "thread aborted");
|
||||
const dummyReactHash = await writeEmptyReactSession(
|
||||
store,
|
||||
typeHashes,
|
||||
END,
|
||||
placeholderAgentHash,
|
||||
);
|
||||
lastStep = await writeThreadStep(store, typeHashes, {
|
||||
role: END,
|
||||
meta: {},
|
||||
contentHash: dummyContentHash,
|
||||
reactHash: dummyReactHash,
|
||||
startHash,
|
||||
previousHash: null,
|
||||
});
|
||||
}
|
||||
|
||||
const endHash = await writeThreadEnd(store, typeHashes, {
|
||||
returnCode: 130,
|
||||
summary: "thread aborted",
|
||||
startHash,
|
||||
lastStepHash: lastStep,
|
||||
});
|
||||
|
||||
return { returnCode: 130, summary: "thread aborted", rootHash: endHash };
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import type {
|
||||
ContentPayload,
|
||||
ReactSessionPayload,
|
||||
ReactToolCallPayload,
|
||||
ReactTurnPayload,
|
||||
WorkflowSchemaHashes,
|
||||
} from "@uncaged/json-cas-workflow";
|
||||
|
||||
import type { ReactToolCallTrace, ReactTrace, ReactTurnTrace } from "./json-cas-types.js";
|
||||
|
||||
// ── Node writers ──────────────────────────────────────────────────────
|
||||
|
||||
async function writeContent(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
text: string,
|
||||
): Promise<Hash> {
|
||||
const payload: ContentPayload = { text };
|
||||
return store.put(typeHashes.content, payload);
|
||||
}
|
||||
|
||||
async function writeToolCall(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
toolCall: ReactToolCallTrace,
|
||||
): Promise<Hash> {
|
||||
const [argsHash, resultHash] = await Promise.all([
|
||||
writeContent(store, typeHashes, toolCall.arguments),
|
||||
writeContent(store, typeHashes, toolCall.result),
|
||||
]);
|
||||
const payload: ReactToolCallPayload = {
|
||||
name: toolCall.name,
|
||||
arguments: argsHash,
|
||||
result: resultHash,
|
||||
durationMs: toolCall.durationMs,
|
||||
};
|
||||
return store.put(typeHashes.reactToolCall, payload);
|
||||
}
|
||||
|
||||
async function writeTurn(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
turn: ReactTurnTrace,
|
||||
): Promise<Hash> {
|
||||
const [inputHash, outputHash, toolCallHashes] = await Promise.all([
|
||||
writeContent(store, typeHashes, turn.input),
|
||||
writeContent(store, typeHashes, turn.output),
|
||||
Promise.all(turn.toolCalls.map((tc) => writeToolCall(store, typeHashes, tc))),
|
||||
]);
|
||||
const payload: ReactTurnPayload = {
|
||||
input: inputHash,
|
||||
output: outputHash,
|
||||
toolCalls: toolCallHashes,
|
||||
tokens: turn.tokens,
|
||||
latencyMs: turn.latencyMs,
|
||||
};
|
||||
return store.put(typeHashes.reactTurn, payload);
|
||||
}
|
||||
|
||||
// ── Public API ────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Serialise a {@link ReactTrace} captured during an agent run into CAS nodes:
|
||||
*
|
||||
* content (args/result) → react-tool-call
|
||||
* content (input/output) + react-tool-calls → react-turn
|
||||
* react-turns → react-session
|
||||
*
|
||||
* Returns the hash of the written react-session node.
|
||||
*/
|
||||
export async function writeReactSession(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
params: {
|
||||
agentHash: Hash;
|
||||
role: string;
|
||||
trace: ReactTrace;
|
||||
},
|
||||
): Promise<Hash> {
|
||||
const turnHashes = await Promise.all(
|
||||
params.trace.turns.map((turn) => writeTurn(store, typeHashes, turn)),
|
||||
);
|
||||
const payload: ReactSessionPayload = {
|
||||
agent: params.agentHash,
|
||||
role: params.role,
|
||||
turns: turnHashes,
|
||||
totalTokens: params.trace.totalTokens,
|
||||
durationMs: params.trace.durationMs,
|
||||
};
|
||||
return store.put(typeHashes.reactSession, payload);
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import type { WorkflowSchemaHashes } from "@uncaged/json-cas-workflow";
|
||||
|
||||
import type { Result } from "@uncaged/workflow-util";
|
||||
|
||||
// ── Engine IO ─────────────────────────────────────────────────────────
|
||||
|
||||
export type JsonCasEngineIo = {
|
||||
threadId: string;
|
||||
store: Store;
|
||||
typeHashes: WorkflowSchemaHashes;
|
||||
};
|
||||
|
||||
// ── Agent binding ─────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Maps each role name to a CAS hash referencing an agent node.
|
||||
* Phase 4 uses a simple role→hash mapping; full agent resolution comes later.
|
||||
*/
|
||||
export type AgentBindings = Record<string, Hash>;
|
||||
|
||||
// ── Engine options ────────────────────────────────────────────────────
|
||||
|
||||
export type JsonCasEngineOptions = {
|
||||
depth: number;
|
||||
parentThread: Hash | null;
|
||||
signal: AbortSignal;
|
||||
agents: AgentBindings;
|
||||
};
|
||||
|
||||
// ── React trace (raw data before CAS serialisation) ───────────────────
|
||||
|
||||
export type ReactToolCallTrace = {
|
||||
name: string;
|
||||
/** JSON-serialised arguments */
|
||||
arguments: string;
|
||||
/** JSON-serialised result */
|
||||
result: string;
|
||||
durationMs: number;
|
||||
};
|
||||
|
||||
export type ReactTurnTrace = {
|
||||
/** Full prompt text sent to the LLM */
|
||||
input: string;
|
||||
/** Raw assistant response text */
|
||||
output: string;
|
||||
toolCalls: ReactToolCallTrace[];
|
||||
tokens: { input: number; output: number };
|
||||
latencyMs: number;
|
||||
};
|
||||
|
||||
export type ReactTrace = {
|
||||
turns: ReactTurnTrace[];
|
||||
totalTokens: number;
|
||||
durationMs: number;
|
||||
};
|
||||
|
||||
// ── Agent function result ─────────────────────────────────────────────
|
||||
|
||||
export type JsonCasAgentResult = {
|
||||
text: string;
|
||||
meta: Record<string, unknown>;
|
||||
/**
|
||||
* React trace captured during the agent run.
|
||||
* Null when the agent has no trace to record (e.g. a mock or passthrough).
|
||||
*/
|
||||
react: ReactTrace | null;
|
||||
};
|
||||
|
||||
// ── Agent function (mock-friendly) ────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Invoked for each role step. Returns the agent's raw text output,
|
||||
* structured meta, and an optional react trace. The engine stores the
|
||||
* text in a content node and the trace in react-* CAS nodes.
|
||||
*/
|
||||
export type JsonCasAgentFn = (
|
||||
role: string,
|
||||
systemPrompt: string,
|
||||
context: JsonCasThreadSnapshot,
|
||||
) => Promise<JsonCasAgentResult>;
|
||||
|
||||
// ── Thread snapshot (read-only view for agents & moderator) ───────────
|
||||
|
||||
export type JsonCasStartSnapshot = {
|
||||
input: string;
|
||||
depth: number;
|
||||
workflowHash: Hash;
|
||||
};
|
||||
|
||||
export type JsonCasStepSnapshot = {
|
||||
role: string;
|
||||
meta: Record<string, unknown>;
|
||||
contentHash: Hash;
|
||||
};
|
||||
|
||||
export type JsonCasThreadSnapshot = {
|
||||
threadId: string;
|
||||
start: JsonCasStartSnapshot;
|
||||
steps: readonly JsonCasStepSnapshot[];
|
||||
};
|
||||
|
||||
// ── Thread pause gate (re-use from existing types) ────────────────────
|
||||
|
||||
export type JsonCasThreadPauseGate = {
|
||||
awaitAfterYield: () => Promise<void>;
|
||||
pause: () => Result<void, string>;
|
||||
resume: () => Result<void, string>;
|
||||
isPaused: () => boolean;
|
||||
};
|
||||
@@ -4,6 +4,18 @@ export {
|
||||
walkStateFramesNewestFirst,
|
||||
} from "./engine/fork-thread.js";
|
||||
export { garbageCollectCas } from "./engine/gc.js";
|
||||
export { buildJsonCasThreadContext, buildJsonCasThreadSnapshot, readContentText } from "./engine/json-cas-context.js";
|
||||
export { executeJsonCasThread } from "./engine/json-cas-engine.js";
|
||||
export type {
|
||||
AgentBindings,
|
||||
JsonCasAgentFn,
|
||||
JsonCasEngineIo,
|
||||
JsonCasEngineOptions,
|
||||
JsonCasStartSnapshot,
|
||||
JsonCasStepSnapshot,
|
||||
JsonCasThreadPauseGate,
|
||||
JsonCasThreadSnapshot,
|
||||
} from "./engine/json-cas-types.js";
|
||||
export type {
|
||||
ThreadHistoryEntry,
|
||||
ThreadIndex,
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
{ "path": "../workflow-util" },
|
||||
{ "path": "../workflow-cas" },
|
||||
{ "path": "../workflow-reactor" },
|
||||
{ "path": "../workflow-register" }
|
||||
{ "path": "../workflow-register" },
|
||||
{ "path": "../workflow-json-def" }
|
||||
]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import type { CasNode } from "@uncaged/json-cas";
|
||||
import { createMemoryStore, refs, validate } from "@uncaged/json-cas";
|
||||
import type { ThreadStartPayload } from "@uncaged/json-cas-workflow";
|
||||
import { registerWorkflowSchemas } from "@uncaged/json-cas-workflow";
|
||||
import { putAgentNode } from "../src/index.js";
|
||||
|
||||
// ── Step 6: putAgentNode — CAS agent instance nodes ──────────────────────────
|
||||
|
||||
describe("Step 6: putAgentNode", () => {
|
||||
test("returns a 13-char Crockford Base32 hash", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const hash = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-llm",
|
||||
"0.5.0-alpha.4",
|
||||
{ baseUrl: "https://api.example.com", apiKey: "sk-test", model: "gpt-4o" },
|
||||
);
|
||||
|
||||
expect(hash).toHaveLength(13);
|
||||
expect(hash).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
});
|
||||
|
||||
test("stored agent node is present in the store", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const hash = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-cursor",
|
||||
"0.5.0-alpha.4",
|
||||
{
|
||||
command: "/usr/bin/cursor-agent",
|
||||
model: null,
|
||||
timeout: 0,
|
||||
workspace: null,
|
||||
},
|
||||
);
|
||||
|
||||
expect(store.get(hash)).not.toBeNull();
|
||||
});
|
||||
|
||||
test("agent node payload contains package, version, and config", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const config = { command: "/usr/bin/hermes", model: "claude-3-5-sonnet", timeout: null };
|
||||
const hash = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-hermes",
|
||||
"0.5.0-alpha.4",
|
||||
config,
|
||||
);
|
||||
|
||||
const node = store.get(hash) as CasNode;
|
||||
const payload = node.payload as Record<string, unknown>;
|
||||
expect(payload.package).toBe("@uncaged/workflow-agent-hermes");
|
||||
expect(payload.version).toBe("0.5.0-alpha.4");
|
||||
expect(payload.config).toEqual(config);
|
||||
});
|
||||
|
||||
test("idempotent: same package + version + config returns the same hash", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const config = { baseUrl: "https://api.example.com", apiKey: "sk-test", model: "gpt-4o" };
|
||||
const hash1 = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-llm",
|
||||
"0.5.0-alpha.4",
|
||||
config,
|
||||
);
|
||||
const hash2 = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-llm",
|
||||
"0.5.0-alpha.4",
|
||||
config,
|
||||
);
|
||||
|
||||
expect(hash1).toBe(hash2);
|
||||
});
|
||||
|
||||
test("different configs produce different hashes", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const hash1 = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-llm",
|
||||
"0.5.0-alpha.4",
|
||||
{
|
||||
baseUrl: "https://api.example.com",
|
||||
apiKey: "sk-test",
|
||||
model: "gpt-4o",
|
||||
},
|
||||
);
|
||||
const hash2 = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-llm",
|
||||
"0.5.0-alpha.4",
|
||||
{
|
||||
baseUrl: "https://api.example.com",
|
||||
apiKey: "sk-test",
|
||||
model: "gpt-4o-mini",
|
||||
},
|
||||
);
|
||||
|
||||
expect(hash1).not.toBe(hash2);
|
||||
});
|
||||
|
||||
test("agent node passes validation against the agent schema", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const hash = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-react",
|
||||
"0.5.0-alpha.4",
|
||||
{
|
||||
maxRounds: 10,
|
||||
},
|
||||
);
|
||||
|
||||
const node = store.get(hash) as CasNode;
|
||||
expect(validate(store, node)).toBe(true);
|
||||
});
|
||||
|
||||
test("agent node with empty config is valid", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const hash = await putAgentNode(store, typeHashes, "placeholder", "0.0.0", {});
|
||||
const node = store.get(hash) as CasNode;
|
||||
expect(validate(store, node)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// ── Step 6: refs from thread-start includes agent refs ────────────────────────
|
||||
|
||||
describe("Step 6: refs() from thread-start extracts agent refs", () => {
|
||||
test("thread-start with agents: refs() returns the agent hashes", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const agentHash1 = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-llm",
|
||||
"0.5.0-alpha.4",
|
||||
{ baseUrl: "https://api.example.com", apiKey: "sk-1", model: "gpt-4o" },
|
||||
);
|
||||
const agentHash2 = await putAgentNode(
|
||||
store,
|
||||
typeHashes,
|
||||
"@uncaged/workflow-agent-cursor",
|
||||
"0.5.0-alpha.4",
|
||||
{ command: "/usr/bin/cursor-agent", model: null, timeout: 0, workspace: null },
|
||||
);
|
||||
|
||||
const fakeWorkflowHash = "FAKEWF0000001";
|
||||
const startHash = await store.put(typeHashes.threadStart, {
|
||||
workflow: fakeWorkflowHash,
|
||||
input: "test",
|
||||
depth: 0,
|
||||
parentThread: null,
|
||||
agents: { planner: agentHash1, coder: agentHash2 },
|
||||
} satisfies ThreadStartPayload);
|
||||
|
||||
const startNode = store.get(startHash) as CasNode;
|
||||
const startRefs = refs(store, startNode);
|
||||
|
||||
expect(startRefs).toContain(agentHash1);
|
||||
expect(startRefs).toContain(agentHash2);
|
||||
});
|
||||
|
||||
test("thread-start with no agents: refs() returns only the workflow ref", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const fakeWorkflowHash = "FAKEWF0000002";
|
||||
const startHash = await store.put(typeHashes.threadStart, {
|
||||
workflow: fakeWorkflowHash,
|
||||
input: "empty agents",
|
||||
depth: 0,
|
||||
parentThread: null,
|
||||
agents: {},
|
||||
} satisfies ThreadStartPayload);
|
||||
|
||||
const startNode = store.get(startHash) as CasNode;
|
||||
const startRefs = refs(store, startNode);
|
||||
|
||||
expect(startRefs).toContain(fakeWorkflowHash);
|
||||
expect(startRefs).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("thread-start with 3 agents: refs() count includes workflow + 3 agents", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const makeAgent = (model: string) =>
|
||||
putAgentNode(store, typeHashes, "@uncaged/workflow-agent-llm", "0.5.0-alpha.4", {
|
||||
baseUrl: "https://api.example.com",
|
||||
apiKey: "sk-x",
|
||||
model,
|
||||
});
|
||||
|
||||
const [a1, a2, a3] = await Promise.all([makeAgent("m1"), makeAgent("m2"), makeAgent("m3")]);
|
||||
const fakeWorkflowHash = "FAKEWF0000003";
|
||||
|
||||
const startHash = await store.put(typeHashes.threadStart, {
|
||||
workflow: fakeWorkflowHash,
|
||||
input: "multi-agent",
|
||||
depth: 0,
|
||||
parentThread: null,
|
||||
agents: { r1: a1, r2: a2, r3: a3 },
|
||||
} satisfies ThreadStartPayload);
|
||||
|
||||
const startNode = store.get(startHash) as CasNode;
|
||||
const startRefs = refs(store, startNode);
|
||||
|
||||
// 1 workflow ref + 3 agent refs = 4
|
||||
expect(startRefs).toHaveLength(4);
|
||||
expect(startRefs).toContain(fakeWorkflowHash);
|
||||
expect(startRefs).toContain(a1);
|
||||
expect(startRefs).toContain(a2);
|
||||
expect(startRefs).toContain(a3);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,403 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import type { CasNode } from "@uncaged/json-cas";
|
||||
import { createMemoryStore, refs, validate, walk } from "@uncaged/json-cas";
|
||||
import { registerWorkflowSchemas } from "@uncaged/json-cas-workflow";
|
||||
import {
|
||||
developWorkflow,
|
||||
END,
|
||||
loadWorkflow,
|
||||
registerWorkflow,
|
||||
START,
|
||||
solveIssueWorkflow,
|
||||
} from "../src/index.js";
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Step 1: Bootstrap — registerWorkflowSchemas returns all 11 schema hashes
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("Step 1: registerWorkflowSchemas", () => {
|
||||
test("returns 11 distinct 13-char Crockford Base32 hashes", async () => {
|
||||
const store = createMemoryStore();
|
||||
const hashes = await registerWorkflowSchemas(store);
|
||||
|
||||
const values = Object.values(hashes);
|
||||
expect(values).toHaveLength(11);
|
||||
for (const h of values) {
|
||||
expect(h).toHaveLength(13);
|
||||
expect(h).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
}
|
||||
expect(new Set(values).size).toBe(11);
|
||||
});
|
||||
|
||||
test("is idempotent across multiple calls", async () => {
|
||||
const store = createMemoryStore();
|
||||
const first = await registerWorkflowSchemas(store);
|
||||
const second = await registerWorkflowSchemas(store);
|
||||
|
||||
for (const key of Object.keys(first) as (keyof typeof first)[]) {
|
||||
expect(first[key]).toBe(second[key]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Step 2: registerWorkflow — stores roles + workflow in CAS
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("Step 2: registerWorkflow", () => {
|
||||
test("returns a 13-char Crockford Base32 workflow hash", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
|
||||
expect(hash).toHaveLength(13);
|
||||
expect(hash).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
});
|
||||
|
||||
test("is idempotent: registering the same workflow twice returns the same hash", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash1 = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const hash2 = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
|
||||
expect(hash1).toBe(hash2);
|
||||
});
|
||||
|
||||
test("workflow node is present in the store after registration", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
|
||||
expect(store.get(hash)).not.toBeNull();
|
||||
});
|
||||
|
||||
test("stores role nodes — one per role in the definition", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const wfHash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const wfNode = store.get(wfHash) as CasNode;
|
||||
const roles = (wfNode.payload as Record<string, unknown>).roles as Record<string, string>;
|
||||
|
||||
expect(Object.keys(roles)).toHaveLength(Object.keys(solveIssueWorkflow.roles).length);
|
||||
for (const roleHash of Object.values(roles)) {
|
||||
expect(store.get(roleHash)).not.toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
test("stores role-schema nodes — one per role", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const wfHash = await registerWorkflow(store, typeHashes, developWorkflow);
|
||||
const wfNode = store.get(wfHash) as CasNode;
|
||||
const roles = (wfNode.payload as Record<string, unknown>).roles as Record<string, string>;
|
||||
|
||||
for (const roleHash of Object.values(roles)) {
|
||||
const roleNode = store.get(roleHash) as CasNode;
|
||||
const schemaHash = (roleNode.payload as Record<string, string>).schema;
|
||||
expect(store.get(schemaHash)).not.toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
test("workflow payload contains correct name and description", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, developWorkflow);
|
||||
const node = store.get(hash) as CasNode;
|
||||
const payload = node.payload as Record<string, unknown>;
|
||||
|
||||
expect(payload.name).toBe("develop");
|
||||
expect(payload.description).toBe(developWorkflow.description);
|
||||
});
|
||||
|
||||
test("workflow payload contains moderator rules array", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const node = store.get(hash) as CasNode;
|
||||
const payload = node.payload as Record<string, unknown>;
|
||||
|
||||
expect(Array.isArray(payload.moderator)).toBe(true);
|
||||
const rules = payload.moderator as Array<{ from: string; to: string; when: string | null }>;
|
||||
expect(rules.some((r) => r.from === START)).toBe(true);
|
||||
expect(rules.some((r) => r.to === END)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Step 3: loadWorkflow — round-trip hydration from CAS
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("Step 3: loadWorkflow", () => {
|
||||
test("returns null for an unknown hash", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
expect(loadWorkflow(store, typeHashes, "AAAAAAAAAAAAA")).toBeNull();
|
||||
});
|
||||
|
||||
test("hydrates solve-issue workflow with correct name and description", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const result = loadWorkflow(store, typeHashes, hash);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.name).toBe("solve-issue");
|
||||
expect(result?.description).toBe(solveIssueWorkflow.description);
|
||||
});
|
||||
|
||||
test("hydrated workflow contains all roles", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const result = loadWorkflow(store, typeHashes, hash);
|
||||
|
||||
const expectedRoles = Object.keys(solveIssueWorkflow.roles);
|
||||
expect(Object.keys(result?.roles ?? {})).toEqual(expect.arrayContaining(expectedRoles));
|
||||
});
|
||||
|
||||
test("hydrated role has correct systemPrompt and description", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const result = loadWorkflow(store, typeHashes, hash);
|
||||
|
||||
const preparer = result?.roles.preparer;
|
||||
expect(preparer?.description).toBe(solveIssueWorkflow.roles.preparer.description);
|
||||
expect(preparer?.systemPrompt).toBe(solveIssueWorkflow.roles.preparer.systemPrompt);
|
||||
expect(preparer?.extractPrompt).toBe(solveIssueWorkflow.roles.preparer.extractPrompt);
|
||||
});
|
||||
|
||||
test("hydrated role includes the JSON Schema", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const result = loadWorkflow(store, typeHashes, hash);
|
||||
|
||||
const schema = result?.roles.preparer?.schema;
|
||||
expect(schema).toBeDefined();
|
||||
expect((schema as Record<string, unknown>)?.type).toBe("object");
|
||||
});
|
||||
|
||||
test("hydrated workflow contains moderator rules matching the definition", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, developWorkflow);
|
||||
const result = loadWorkflow(store, typeHashes, hash);
|
||||
|
||||
expect(result?.moderator).toHaveLength(developWorkflow.moderator.length);
|
||||
expect(result?.moderator[0]).toEqual(developWorkflow.moderator[0]);
|
||||
});
|
||||
|
||||
test("develop workflow round-trip has 5 roles", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, developWorkflow);
|
||||
const result = loadWorkflow(store, typeHashes, hash);
|
||||
|
||||
expect(Object.keys(result?.roles ?? {})).toHaveLength(5);
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Step 4: validate() — CAS nodes pass validation against their schemas
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("Step 4: validate", () => {
|
||||
test("workflow node is valid against its schema", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const node = store.get(hash) as CasNode;
|
||||
|
||||
expect(validate(store, node)).toBe(true);
|
||||
});
|
||||
|
||||
test("role nodes are valid against their schema", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const wfHash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const wfNode = store.get(wfHash) as CasNode;
|
||||
const roles = (wfNode.payload as Record<string, unknown>).roles as Record<string, string>;
|
||||
|
||||
for (const roleHash of Object.values(roles)) {
|
||||
const roleNode = store.get(roleHash) as CasNode;
|
||||
expect(validate(store, roleNode)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
test("role-schema nodes are valid against their schema", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const wfHash = await registerWorkflow(store, typeHashes, developWorkflow);
|
||||
const wfNode = store.get(wfHash) as CasNode;
|
||||
const roles = (wfNode.payload as Record<string, unknown>).roles as Record<string, string>;
|
||||
|
||||
for (const roleHash of Object.values(roles)) {
|
||||
const roleNode = store.get(roleHash) as CasNode;
|
||||
const schemaHash = (roleNode.payload as Record<string, string>).schema;
|
||||
const schemaNode = store.get(schemaHash) as CasNode;
|
||||
expect(validate(store, schemaNode)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
test("workflow node with wrong type for roles fails validation", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const badHash = await store.put(typeHashes.workflow, {
|
||||
name: "bad",
|
||||
description: "bad",
|
||||
roles: "not-an-object",
|
||||
moderator: [],
|
||||
});
|
||||
const node = store.get(badHash) as CasNode;
|
||||
|
||||
expect(validate(store, node)).toBe(false);
|
||||
});
|
||||
|
||||
test("role node missing required field fails validation", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const badHash = await store.put(typeHashes.role, {
|
||||
name: "bad",
|
||||
description: "d",
|
||||
systemPrompt: "s",
|
||||
});
|
||||
const node = store.get(badHash) as CasNode;
|
||||
|
||||
expect(validate(store, node)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Step 5: refs() — extracts cas_ref hashes from workflow and role nodes
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("Step 5: refs", () => {
|
||||
test("workflow node refs() returns one hash per role", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const node = store.get(hash) as CasNode;
|
||||
const roleCount = Object.keys(solveIssueWorkflow.roles).length;
|
||||
|
||||
expect(refs(store, node)).toHaveLength(roleCount);
|
||||
});
|
||||
|
||||
test("role node refs() returns exactly one hash (the schema)", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const wfHash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const wfNode = store.get(wfHash) as CasNode;
|
||||
const roles = (wfNode.payload as Record<string, unknown>).roles as Record<string, string>;
|
||||
const firstRoleHash = Object.values(roles)[0];
|
||||
|
||||
const roleNode = store.get(firstRoleHash) as CasNode;
|
||||
const roleRefs = refs(store, roleNode);
|
||||
|
||||
expect(roleRefs).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("role refs() points to the role-schema node", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const wfHash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const wfNode = store.get(wfHash) as CasNode;
|
||||
const roles = (wfNode.payload as Record<string, unknown>).roles as Record<string, string>;
|
||||
const firstRoleHash = Object.values(roles)[0];
|
||||
const roleNode = store.get(firstRoleHash) as CasNode;
|
||||
|
||||
const schemaHash = refs(store, roleNode)[0];
|
||||
const schemaNode = store.get(schemaHash);
|
||||
|
||||
expect(schemaNode).not.toBeNull();
|
||||
expect(schemaNode?.type).toBe(typeHashes.roleSchema);
|
||||
});
|
||||
|
||||
test("develop workflow node refs() returns one hash per role (5 roles)", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const hash = await registerWorkflow(store, typeHashes, developWorkflow);
|
||||
const node = store.get(hash) as CasNode;
|
||||
|
||||
expect(refs(store, node)).toHaveLength(5);
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Step 6: walk() — BFS traversal visits workflow, role, and schema nodes
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("Step 6: walk", () => {
|
||||
test("walk from workflow hash visits workflow + role + schema nodes", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const wfHash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
|
||||
const visited = new Set<string>();
|
||||
walk(store, wfHash, (h) => visited.add(h));
|
||||
|
||||
// workflow node itself
|
||||
expect(visited.has(wfHash)).toBe(true);
|
||||
|
||||
// all role nodes and their schema nodes should be reachable
|
||||
const wfNode = store.get(wfHash) as CasNode;
|
||||
const roles = (wfNode.payload as Record<string, unknown>).roles as Record<string, string>;
|
||||
for (const roleHash of Object.values(roles)) {
|
||||
expect(visited.has(roleHash)).toBe(true);
|
||||
const roleNode = store.get(roleHash) as CasNode;
|
||||
const schemaHash = refs(store, roleNode)[0];
|
||||
expect(visited.has(schemaHash)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
test("walk visits all 5 role nodes for develop workflow", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const wfHash = await registerWorkflow(store, typeHashes, developWorkflow);
|
||||
|
||||
const visited = new Set<string>();
|
||||
walk(store, wfHash, (h) => visited.add(h));
|
||||
|
||||
const wfNode = store.get(wfHash) as CasNode;
|
||||
const roles = (wfNode.payload as Record<string, unknown>).roles as Record<string, string>;
|
||||
expect(Object.values(roles).every((rh) => visited.has(rh))).toBe(true);
|
||||
});
|
||||
|
||||
test("walk total node count = 1 workflow + N roles + N schemas", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
const wfHash = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const roleCount = Object.keys(solveIssueWorkflow.roles).length;
|
||||
|
||||
const visited = new Set<string>();
|
||||
walk(store, wfHash, (h) => visited.add(h));
|
||||
|
||||
// 1 workflow + roleCount roles + roleCount schemas
|
||||
expect(visited.size).toBe(1 + roleCount + roleCount);
|
||||
});
|
||||
|
||||
test("walk handles two workflows sharing a schema node — visits it only once", async () => {
|
||||
const store = createMemoryStore();
|
||||
const typeHashes = await registerWorkflowSchemas(store);
|
||||
// Register the same workflow twice — second call is idempotent, same hashes
|
||||
const hash1 = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
const hash2 = await registerWorkflow(store, typeHashes, solveIssueWorkflow);
|
||||
|
||||
expect(hash1).toBe(hash2);
|
||||
|
||||
const visited = new Set<string>();
|
||||
walk(store, hash1, (h) => visited.add(h));
|
||||
|
||||
// Each node should be counted exactly once despite any shared refs
|
||||
const roleCount = Object.keys(solveIssueWorkflow.roles).length;
|
||||
expect(visited.size).toBe(1 + roleCount + roleCount);
|
||||
});
|
||||
|
||||
test("walk with unknown starting hash visits nothing", () => {
|
||||
const store = createMemoryStore();
|
||||
const visited: string[] = [];
|
||||
walk(store, "AAAAAAAAAAAAA", (h) => visited.push(h));
|
||||
|
||||
expect(visited).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@uncaged/uwf-moderator",
|
||||
"version": "0.1.0",
|
||||
"name": "@uncaged/workflow-json-def",
|
||||
"version": "0.5.0-alpha.4",
|
||||
"files": [
|
||||
"src",
|
||||
"dist",
|
||||
@@ -18,8 +18,8 @@
|
||||
"test": "bun test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@uncaged/uwf-protocol": "workspace:^",
|
||||
"jsonata": "^1.8.7"
|
||||
"@uncaged/json-cas": "file:../../../json-cas/packages/json-cas",
|
||||
"@uncaged/json-cas-workflow": "file:../../../json-cas/packages/json-cas-workflow"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.8.3"
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import type { WorkflowSchemaHashes } from "@uncaged/json-cas-workflow";
|
||||
|
||||
/**
|
||||
* Store an agent instance in CAS.
|
||||
*
|
||||
* Writes an `agent` node with `{ package, version, config }` and returns
|
||||
* the content-addressed hash. Idempotent: the same inputs always produce
|
||||
* the same hash.
|
||||
*/
|
||||
export async function putAgentNode(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
pkg: string,
|
||||
version: string,
|
||||
config: Record<string, unknown>,
|
||||
): Promise<Hash> {
|
||||
return store.put(typeHashes.agent, { package: pkg, version, config });
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export const START = "__start__" as const;
|
||||
export const END = "__end__" as const;
|
||||
@@ -0,0 +1,284 @@
|
||||
import type { WorkflowInput } from "../types.js";
|
||||
import { END, START } from "./constants.js";
|
||||
|
||||
export const DEVELOP_WORKFLOW_DESCRIPTION =
|
||||
"Plan phases, implement incrementally, review, verify with tests/build/lint, and commit (planner → coder [repeat per phase] → reviewer → tester → committer).";
|
||||
|
||||
// ── JSONata conditions ────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* True when the planner aborted due to insufficient information.
|
||||
* Translates the plannerAborted TypeScript condition to JSONata.
|
||||
*/
|
||||
const PLANNER_ABORTED = "$boolean(steps[role='planner'].meta.status = 'aborted')";
|
||||
|
||||
/**
|
||||
* True when all planned phases have been completed by the coder.
|
||||
*
|
||||
* Logic:
|
||||
* - No planned phases → true (nothing to complete)
|
||||
* - Last phase hash appears in any coder step's completedPhase → true
|
||||
* - Every phase hash appears in some coder's completedPhase → true (via count check)
|
||||
*/
|
||||
const ALL_PHASES_COMPLETE = [
|
||||
"(",
|
||||
" $plannerMeta := steps[role='planner'].meta;",
|
||||
" $phases := $plannerMeta.status = 'planned' ? $plannerMeta.phases : [];",
|
||||
" $count($phases) = 0 ? true :",
|
||||
" (",
|
||||
" $lastHash := $phases[-1].hash;",
|
||||
" $completedHashes := steps[role='coder'].meta.completedPhase;",
|
||||
" $lastHash in $completedHashes or",
|
||||
" $count($phases[$not(hash in $completedHashes)]) = 0",
|
||||
" )",
|
||||
")",
|
||||
].join(" ");
|
||||
|
||||
/** True when the most recent reviewer step reported approved. */
|
||||
const REVIEW_APPROVED = "steps[-1].meta.status = 'approved'";
|
||||
|
||||
/** True when the most recent tester step reported passed. */
|
||||
const TESTS_PASSED = "steps[-1].meta.status = 'passed'";
|
||||
|
||||
// ── Workflow definition ───────────────────────────────────────────────────────
|
||||
|
||||
export const developWorkflow: WorkflowInput = {
|
||||
name: "develop",
|
||||
description: DEVELOP_WORKFLOW_DESCRIPTION,
|
||||
roles: {
|
||||
planner: {
|
||||
description: "Breaks the task into sequential phases for the coder.",
|
||||
systemPrompt: `You are a **planner** for a software task. Break the work into **sequential phases** the coder will execute one at a time. **Abort** if the prompt lacks critical information (e.g. no project/workspace path, ambiguous target repo).
|
||||
|
||||
Run \`uncaged-workflow skill develop\` for thread ID lookup, CAS commands, and meta output guide.
|
||||
|
||||
## Prerequisites — check FIRST
|
||||
|
||||
The prompt MUST include an **absolute filesystem path** to the project workspace (e.g. \`/home/user/repos/my-project\`). If no workspace path is given and you cannot reliably infer one from context, **abort immediately** with a clear reason explaining what information is missing. Do NOT guess paths.
|
||||
|
||||
## Storing phase details — MANDATORY
|
||||
|
||||
For each phase, store its full detail text in CAS via \`uncaged-workflow cas put '<content>'\`. The command prints a content-hash — use that as the phase identifier.
|
||||
|
||||
The thread ID (26-char Crockford Base32) appears in the first message. If unsure, run \`uncaged-workflow thread list\`.
|
||||
|
||||
**Do NOT store phase details in any other way** — the CLI is the only supported storage mechanism.
|
||||
|
||||
## Phase granularity
|
||||
|
||||
Match the number of phases to task complexity:
|
||||
- Trivial (add a config option, fix a typo, rename): 1 phase
|
||||
- Small (a new feature touching 2-3 files): 1-2 phases
|
||||
- Medium (cross-module refactor): 2-3 phases
|
||||
- Large (new subsystem, architectural change): 3-5 phases
|
||||
|
||||
Fewer phases is always better. Each phase must justify its existence — if two phases would be tested together anyway, merge them.
|
||||
|
||||
## Output format
|
||||
|
||||
After storing all phases via the CLI, output compact JSON only:
|
||||
{ "status": "planned", "phases": [{ "hash": "<hash-from-cas-put>", "title": "<one-line-summary>" }] }
|
||||
|
||||
If aborting:
|
||||
{ "status": "aborted", "reason": "<what is missing>" }
|
||||
|
||||
Order phases so earlier steps unblock later ones. Cover root cause, edge cases, and verification across the phases.
|
||||
|
||||
## Output rules
|
||||
|
||||
Keep your final response **short** — just the JSON with phases. Do NOT paste code snippets, diffs, or implementation details in your response. Phase details are already stored in CAS; your response should only contain the compact phases JSON.`,
|
||||
extractPrompt:
|
||||
"Extract the planner result as JSON. Use status='planned' with phases array (hash+title), or status='aborted' with reason.",
|
||||
schema: {
|
||||
type: "object",
|
||||
oneOf: [
|
||||
{
|
||||
required: ["status", "phases"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["planned"] },
|
||||
phases: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
required: ["hash", "title"],
|
||||
properties: {
|
||||
hash: { type: "string" },
|
||||
title: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
required: ["status", "reason"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["aborted"] },
|
||||
reason: { type: "string" },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
coder: {
|
||||
description:
|
||||
"Implements the next incomplete planner phase and reports structured completion metadata.",
|
||||
systemPrompt: `You are a **coder**. Read the thread for the plan and work on the NEXT incomplete phase only.
|
||||
|
||||
Run \`uncaged-workflow skill develop\` for thread ID lookup, CAS commands, and meta output guide.
|
||||
|
||||
## Reading phase details
|
||||
|
||||
Each planner phase has a content-hash and title. Read full details with \`uncaged-workflow cas get <HASH>\`.
|
||||
|
||||
The thread ID (26-char Crockford Base32) appears in the first message. If unsure, run \`uncaged-workflow thread list\`.
|
||||
|
||||
## Completing a phase
|
||||
|
||||
Report which phase you completed using the phase **hash** (not the title). If you legitimately finish every remaining phase in this single turn, set completedPhase to the **last** phase hash in the plan (the workflow treats that as full completion). List the files you changed and summarize what you did.
|
||||
|
||||
## Output rules
|
||||
|
||||
Keep your final response **short** — a brief summary paragraph plus the structured meta output. Do NOT paste diffs, file contents, or code blocks in your response. The actual changes are already on disk; repeating them wastes tokens. Just say what you did and why.`,
|
||||
extractPrompt:
|
||||
"Extract the coder result as JSON with fields: completedPhase (hash string), filesChanged (array), summary.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["completedPhase", "filesChanged", "summary"],
|
||||
properties: {
|
||||
completedPhase: { type: "string" },
|
||||
filesChanged: { type: "array", items: { type: "string" } },
|
||||
summary: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
reviewer: {
|
||||
description: "Runs git diff checks and sets approved when the change is ready.",
|
||||
systemPrompt: `You are a code reviewer. Review the git diff for correctness, consistency, and adherence to project conventions.
|
||||
|
||||
## Review process
|
||||
|
||||
1. Read the **preparer**'s output in the thread for project conventions (coding style, naming, commit format, etc.).
|
||||
2. Review the diff against these conventions.
|
||||
3. For documentation changes, verify that names, paths, and references match the actual codebase.
|
||||
|
||||
## Review checklist
|
||||
|
||||
- **Correctness** — does the code do what it claims? Logic bugs, off-by-one, missing returns?
|
||||
- **Conventions** — naming, imports, code style per project rules?
|
||||
- **Consistency** — do docs/comments match actual code? Are references current and accurate?
|
||||
- **Edge cases** — missing error handling, null checks, boundary conditions?
|
||||
|
||||
## Verdict
|
||||
|
||||
- **Approve** only if there are zero issues
|
||||
- **Reject** with specific issues that must be fixed — every issue you find is blocking
|
||||
|
||||
Be thorough. A false approve costs more than a false reject.
|
||||
|
||||
## Output rules
|
||||
|
||||
Keep your final response **short**. Summarize findings in a few bullet points, then output the structured verdict. Do NOT paste the full diff or large code blocks in your response.`,
|
||||
extractPrompt:
|
||||
"Extract the reviewer verdict as JSON. Use status='approved', or status='rejected' with issues array.",
|
||||
schema: {
|
||||
type: "object",
|
||||
oneOf: [
|
||||
{
|
||||
required: ["status"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["approved"] },
|
||||
},
|
||||
},
|
||||
{
|
||||
required: ["status", "issues"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["rejected"] },
|
||||
issues: { type: "array", items: { type: "string" } },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
tester: {
|
||||
description: "Runs test, build, and lint commands and reports pass or fail with details.",
|
||||
systemPrompt: `You are a tester. Run the project's test suite, build, and lint commands. Check what commands are available from the preparer's output in the thread. Report pass/fail with details of what failed.
|
||||
|
||||
## Output rules
|
||||
|
||||
Keep your final response **short**. Report pass/fail with a brief summary of failures (if any). Do NOT paste full test output or build logs — just the key error lines.`,
|
||||
extractPrompt:
|
||||
"Extract the tester result as JSON. Use status='passed' or status='failed', both with details string.",
|
||||
schema: {
|
||||
type: "object",
|
||||
oneOf: [
|
||||
{
|
||||
required: ["status", "details"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["passed"] },
|
||||
details: { type: "string" },
|
||||
},
|
||||
},
|
||||
{
|
||||
required: ["status", "details"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["failed"] },
|
||||
details: { type: "string" },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
committer: {
|
||||
description: "Creates a branch and commits changes.",
|
||||
systemPrompt:
|
||||
"You are the git committer. Create a branch and commit the changes. Report the branch name and commit SHA. On failure, classify as recoverable or unrecoverable. Do not attempt to fix failures yourself.",
|
||||
extractPrompt:
|
||||
"Extract the committer result as JSON. Use status='committed' with branch+commitSha, status='recoverable' with error+logRef, or status='unrecoverable' with error+logRef.",
|
||||
schema: {
|
||||
type: "object",
|
||||
oneOf: [
|
||||
{
|
||||
required: ["status", "branch", "commitSha"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["committed"] },
|
||||
branch: { type: "string" },
|
||||
commitSha: { type: "string" },
|
||||
},
|
||||
},
|
||||
{
|
||||
required: ["status", "error", "logRef"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["recoverable"] },
|
||||
error: { type: "string" },
|
||||
logRef: { anyOf: [{ type: "string" }, { type: "null" }] },
|
||||
},
|
||||
},
|
||||
{
|
||||
required: ["status", "error", "logRef"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["unrecoverable"] },
|
||||
error: { type: "string" },
|
||||
logRef: { anyOf: [{ type: "string" }, { type: "null" }] },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
moderator: [
|
||||
{ from: START, to: "planner", when: null },
|
||||
{ from: "planner", to: END, when: PLANNER_ABORTED },
|
||||
{ from: "planner", to: "coder", when: null },
|
||||
{ from: "coder", to: "reviewer", when: ALL_PHASES_COMPLETE },
|
||||
{ from: "coder", to: "coder", when: null },
|
||||
{ from: "reviewer", to: "tester", when: REVIEW_APPROVED },
|
||||
{ from: "reviewer", to: "coder", when: null },
|
||||
{ from: "tester", to: "committer", when: TESTS_PASSED },
|
||||
{ from: "tester", to: "coder", when: null },
|
||||
{ from: "committer", to: END, when: null },
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
export { END, START } from "./constants.js";
|
||||
export { DEVELOP_WORKFLOW_DESCRIPTION, developWorkflow } from "./develop.js";
|
||||
export { SOLVE_ISSUE_WORKFLOW_DESCRIPTION, solveIssueWorkflow } from "./solve-issue.js";
|
||||
@@ -0,0 +1,128 @@
|
||||
import type { WorkflowInput } from "../types.js";
|
||||
import { END, START } from "./constants.js";
|
||||
|
||||
export const SOLVE_ISSUE_WORKFLOW_DESCRIPTION =
|
||||
"Resolve an issue end-to-end by preparing the repo, delegating implementation to the develop workflow, and opening a pull request (preparer → developer → submitter).";
|
||||
|
||||
export const solveIssueWorkflow: WorkflowInput = {
|
||||
name: "solve-issue",
|
||||
description: SOLVE_ISSUE_WORKFLOW_DESCRIPTION,
|
||||
roles: {
|
||||
preparer: {
|
||||
description:
|
||||
"Locates or clones the target repository, ensures it is up to date, and gathers project context (conventions, toolchain).",
|
||||
systemPrompt: `You are a **preparer** for a software task. Your job is to locate (or clone) the target repository locally, ensure it is up to date, and gather project context before work begins.
|
||||
|
||||
## Responsibilities
|
||||
|
||||
1. Parse the issue/task prompt to identify the target repository (URL, org/repo, or name).
|
||||
2. Search for an existing local clone in these locations (in order):
|
||||
- ~/Code/<repo-name>/
|
||||
- ~/repos/<repo-name>/
|
||||
- ~/Code/<org>/<repo-name>/
|
||||
- ~/repos/<org>/<repo-name>/
|
||||
3. If not found locally, \`git clone\` it into ~/repos/<repo-name>/.
|
||||
4. \`git checkout main && git pull\` (or the default branch) to ensure latest.
|
||||
5. Read project conventions: \`CLAUDE.md\`, \`CONTRIBUTING.md\`, \`.cursor/rules/*.mdc\`, \`CONVENTIONS.md\`.
|
||||
6. Detect toolchain: package manager, test runner, linter, build system.
|
||||
|
||||
## Output
|
||||
|
||||
Report your findings as structured data:
|
||||
- **repoPath**: absolute path to the local repo
|
||||
- **defaultBranch**: the default branch name (e.g. "main")
|
||||
- **conventions**: a summary of project conventions found, or null if none
|
||||
- **toolchain**: detected commands for packageManager, testCommand, lintCommand, buildCommand (null if not detected)`,
|
||||
extractPrompt:
|
||||
"Extract the structured repo preparation result as JSON with fields: repoPath, defaultBranch, conventions, toolchain.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["repoPath", "defaultBranch", "conventions", "toolchain"],
|
||||
properties: {
|
||||
repoPath: { type: "string" },
|
||||
defaultBranch: { type: "string" },
|
||||
conventions: { anyOf: [{ type: "string" }, { type: "null" }] },
|
||||
toolchain: {
|
||||
type: "object",
|
||||
required: ["packageManager", "testCommand", "lintCommand", "buildCommand"],
|
||||
properties: {
|
||||
packageManager: { anyOf: [{ type: "string" }, { type: "null" }] },
|
||||
testCommand: { anyOf: [{ type: "string" }, { type: "null" }] },
|
||||
lintCommand: { anyOf: [{ type: "string" }, { type: "null" }] },
|
||||
buildCommand: { anyOf: [{ type: "string" }, { type: "null" }] },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
developer: {
|
||||
description:
|
||||
"Delegates the actual implementation to the develop workflow (workflow-as-agent). Produces a summary by traversing the child thread's Merkle DAG.",
|
||||
systemPrompt: `You are the **developer**. You delegate the implementation work to the \`develop\` workflow.
|
||||
|
||||
The actual implementation (planning → coding → reviewing → testing → committing) is handled by a child workflow that runs in your place. Your output is the Merkle DAG root hash of that child thread.
|
||||
|
||||
Pass through the task and let the child workflow do the work.`,
|
||||
extractPrompt:
|
||||
"Extract the developer result as JSON with fields: branch, commitSha, filesChanged (array), summary.",
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["branch", "commitSha", "filesChanged", "summary"],
|
||||
properties: {
|
||||
branch: { type: "string" },
|
||||
commitSha: { type: "string" },
|
||||
filesChanged: { type: "array", items: { type: "string" } },
|
||||
summary: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
submitter: {
|
||||
description: "Pushes the developer's branch to the remote and opens a pull request.",
|
||||
systemPrompt: `You are the **submitter**. Your job is to push the work branch to the remote and open a pull request.
|
||||
|
||||
## Inputs
|
||||
|
||||
Read the thread for context:
|
||||
- The **preparer**'s output gives you the absolute repo path and the default branch (and remote URL by inspecting the repo).
|
||||
- The **developer**'s output gives you the branch name that was committed and a list of files changed plus a summary of the work.
|
||||
|
||||
## Procedure
|
||||
|
||||
1. \`cd\` into the repo path from the preparer's output.
|
||||
2. Push the developer's branch to the remote: \`git push -u origin <branch>\`.
|
||||
3. Open a pull request (e.g. via \`gh pr create\`) targeting the default branch. The PR title should be short and describe the change. The PR description should summarize what changed (drawing from the developer's summary and filesChanged) and reference the original issue/task if applicable.
|
||||
4. Report the resulting PR URL.
|
||||
|
||||
On any failure (push rejected, gh not authenticated, PR creation failed, etc.), report status="failed" with a short error message. Do not retry — surface the error so the moderator can decide.`,
|
||||
extractPrompt:
|
||||
"Extract the submitter result as JSON. Use status='submitted' with prUrl, or status='failed' with error.",
|
||||
schema: {
|
||||
type: "object",
|
||||
oneOf: [
|
||||
{
|
||||
required: ["status", "prUrl"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["submitted"] },
|
||||
prUrl: { type: "string" },
|
||||
},
|
||||
},
|
||||
{
|
||||
required: ["status", "error"],
|
||||
properties: {
|
||||
status: { type: "string", enum: ["failed"] },
|
||||
error: { type: "string" },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
moderator: [
|
||||
{ from: START, to: "preparer", when: null },
|
||||
{ from: "preparer", to: "developer", when: null },
|
||||
{ from: "developer", to: "submitter", when: null },
|
||||
{ from: "submitter", to: END, when: null },
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
export { putAgentNode } from "./agent.js";
|
||||
export {
|
||||
DEVELOP_WORKFLOW_DESCRIPTION,
|
||||
developWorkflow,
|
||||
END,
|
||||
SOLVE_ISSUE_WORKFLOW_DESCRIPTION,
|
||||
START,
|
||||
solveIssueWorkflow,
|
||||
} from "./definitions/index.js";
|
||||
export { loadWorkflow } from "./load.js";
|
||||
export { registerWorkflow } from "./register.js";
|
||||
export type { HydratedRole, HydratedWorkflow, RoleInput, WorkflowInput } from "./types.js";
|
||||
@@ -0,0 +1,56 @@
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import type {
|
||||
RolePayload,
|
||||
WorkflowPayload,
|
||||
WorkflowSchemaHashes,
|
||||
} from "@uncaged/json-cas-workflow";
|
||||
|
||||
import type { HydratedRole, HydratedWorkflow } from "./types.js";
|
||||
|
||||
/**
|
||||
* Load a workflow from CAS by its hash.
|
||||
*
|
||||
* Reads the workflow node, then for each role ref reads the role node and
|
||||
* its associated role-schema node. Returns a fully hydrated workflow structure.
|
||||
*
|
||||
* Returns null if the workflow node cannot be found.
|
||||
*/
|
||||
export function loadWorkflow(
|
||||
store: Store,
|
||||
_typeHashes: WorkflowSchemaHashes,
|
||||
workflowHash: Hash,
|
||||
): HydratedWorkflow | null {
|
||||
const workflowNode = store.get(workflowHash);
|
||||
if (workflowNode === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const wf = workflowNode.payload as WorkflowPayload;
|
||||
const roles: Record<string, HydratedRole> = {};
|
||||
|
||||
for (const [roleName, roleHash] of Object.entries(wf.roles)) {
|
||||
const roleNode = store.get(roleHash);
|
||||
if (roleNode === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const rolePayload = roleNode.payload as RolePayload;
|
||||
const schemaNode = store.get(rolePayload.schema);
|
||||
const schema = schemaNode !== null ? (schemaNode.payload as Record<string, unknown>) : {};
|
||||
|
||||
roles[roleName] = {
|
||||
name: rolePayload.name,
|
||||
description: rolePayload.description,
|
||||
systemPrompt: rolePayload.systemPrompt,
|
||||
extractPrompt: rolePayload.extractPrompt,
|
||||
schema,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
name: wf.name,
|
||||
description: wf.description,
|
||||
roles,
|
||||
moderator: wf.moderator,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import type { Hash, Store } from "@uncaged/json-cas";
|
||||
import type { WorkflowSchemaHashes } from "@uncaged/json-cas-workflow";
|
||||
|
||||
import type { WorkflowInput } from "./types.js";
|
||||
|
||||
/**
|
||||
* Store a workflow definition in CAS.
|
||||
*
|
||||
* For each role:
|
||||
* 1. Store the role's JSON Schema as a role-schema node → schemaHash
|
||||
* 2. Store the role as a role node referencing schemaHash → roleHash
|
||||
*
|
||||
* Then store the workflow node referencing all role hashes and moderator rules.
|
||||
* Returns the workflow CAS hash.
|
||||
*/
|
||||
export async function registerWorkflow(
|
||||
store: Store,
|
||||
typeHashes: WorkflowSchemaHashes,
|
||||
workflowDef: WorkflowInput,
|
||||
): Promise<Hash> {
|
||||
const roleHashes: Record<string, Hash> = {};
|
||||
|
||||
for (const [roleName, roleInput] of Object.entries(workflowDef.roles)) {
|
||||
const schemaHash = await store.put(typeHashes.roleSchema, roleInput.schema);
|
||||
const roleHash = await store.put(typeHashes.role, {
|
||||
name: roleName,
|
||||
description: roleInput.description,
|
||||
systemPrompt: roleInput.systemPrompt,
|
||||
extractPrompt: roleInput.extractPrompt,
|
||||
schema: schemaHash,
|
||||
});
|
||||
roleHashes[roleName] = roleHash;
|
||||
}
|
||||
|
||||
const workflowHash = await store.put(typeHashes.workflow, {
|
||||
name: workflowDef.name,
|
||||
description: workflowDef.description,
|
||||
roles: roleHashes,
|
||||
moderator: workflowDef.moderator,
|
||||
});
|
||||
|
||||
return workflowHash;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import type { JSONSchema } from "@uncaged/json-cas";
|
||||
import type { WorkflowTransition } from "@uncaged/json-cas-workflow";
|
||||
|
||||
// ── Input types (high-level workflow definition) ──────────────────────────────
|
||||
|
||||
export type RoleInput = {
|
||||
description: string;
|
||||
systemPrompt: string;
|
||||
extractPrompt: string;
|
||||
schema: JSONSchema;
|
||||
};
|
||||
|
||||
export type WorkflowInput = {
|
||||
name: string;
|
||||
description: string;
|
||||
roles: Record<string, RoleInput>;
|
||||
moderator: WorkflowTransition[];
|
||||
};
|
||||
|
||||
// ── Output types (hydrated workflow from CAS) ─────────────────────────────────
|
||||
|
||||
export type HydratedRole = {
|
||||
name: string;
|
||||
description: string;
|
||||
systemPrompt: string;
|
||||
extractPrompt: string;
|
||||
schema: JSONSchema;
|
||||
};
|
||||
|
||||
export type HydratedWorkflow = {
|
||||
name: string;
|
||||
description: string;
|
||||
roles: Record<string, HydratedRole>;
|
||||
moderator: WorkflowTransition[];
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"references": [],
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"lib": ["ES2022"],
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"strict": true,
|
||||
"exactOptionalPropertyTypes": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"sourceMap": true,
|
||||
"composite": true,
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"types": ["bun-types"]
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import type { ModeratorRule } from "../src/jsonata-moderator.js";
|
||||
import { evaluateModerator } from "../src/jsonata-moderator.js";
|
||||
import type { ModeratorContext, StartStep } from "../src/types.js";
|
||||
import { END, START } from "../src/types.js";
|
||||
|
||||
// ── Helpers ─────────────────────────────────────────────────────────
|
||||
|
||||
const BASE_START: StartStep = {
|
||||
role: START,
|
||||
content: "test task",
|
||||
meta: {},
|
||||
timestamp: 1000,
|
||||
parentState: null,
|
||||
};
|
||||
|
||||
function makeCtx(
|
||||
steps: Array<{ role: string; meta: Record<string, unknown> }>,
|
||||
overrides: Partial<ModeratorContext> = {},
|
||||
): ModeratorContext {
|
||||
return {
|
||||
threadId: "01JTESTTHREADID000",
|
||||
depth: 0,
|
||||
bundleHash: "TESTHASH00001",
|
||||
start: BASE_START,
|
||||
steps: steps.map((s, i) => ({
|
||||
...s,
|
||||
contentHash: `hash-${i}`,
|
||||
refs: [],
|
||||
timestamp: 2000 + i,
|
||||
})) as ModeratorContext["steps"],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Step 1: FALLBACK rule (when=null) always matches ────────────────
|
||||
|
||||
describe("Step 1: FALLBACK rule", () => {
|
||||
test("when=null always matches → returns rule.to", async () => {
|
||||
const rules: ModeratorRule[] = [{ from: START, to: "planner", when: null }];
|
||||
const result = await evaluateModerator(rules, makeCtx([]));
|
||||
expect(result).toBe("planner");
|
||||
});
|
||||
});
|
||||
|
||||
// ── Step 2: No matching rules → __end__ ─────────────────────────────
|
||||
|
||||
describe("Step 2: no matching rules", () => {
|
||||
test("empty steps, no rule for __start__ → __end__", async () => {
|
||||
const rules: ModeratorRule[] = [{ from: "planner", to: "coder", when: null }];
|
||||
const result = await evaluateModerator(rules, makeCtx([]));
|
||||
expect(result).toBe(END);
|
||||
});
|
||||
|
||||
test("current state has no matching from rules → __end__", async () => {
|
||||
const rules: ModeratorRule[] = [{ from: START, to: "planner", when: null }];
|
||||
const result = await evaluateModerator(rules, makeCtx([{ role: "planner", meta: {} }]));
|
||||
expect(result).toBe(END);
|
||||
});
|
||||
});
|
||||
|
||||
// ── Step 3: JSONata condition — truthy → match ────────────────────
|
||||
|
||||
describe("Step 3: JSONata condition evaluation", () => {
|
||||
test("truthy expression matches and returns rule.to", async () => {
|
||||
const rules: ModeratorRule[] = [
|
||||
{
|
||||
from: "planner",
|
||||
to: END,
|
||||
when: "steps[role='planner'].meta.status = 'aborted'",
|
||||
},
|
||||
{ from: "planner", to: "coder", when: null },
|
||||
];
|
||||
const ctx = makeCtx([{ role: "planner", meta: { status: "aborted" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe(END);
|
||||
});
|
||||
|
||||
test("falsy expression skips to next rule (FALLBACK)", async () => {
|
||||
const rules: ModeratorRule[] = [
|
||||
{
|
||||
from: "planner",
|
||||
to: END,
|
||||
when: "steps[role='planner'].meta.status = 'aborted'",
|
||||
},
|
||||
{ from: "planner", to: "coder", when: null },
|
||||
];
|
||||
const ctx = makeCtx([{ role: "planner", meta: { status: "planned" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("coder");
|
||||
});
|
||||
});
|
||||
|
||||
// ── Step 4: plannerAborted condition ─────────────────────────────
|
||||
|
||||
describe("Step 4: plannerAborted", () => {
|
||||
const plannerAbortedRules: ModeratorRule[] = [
|
||||
{
|
||||
from: "planner",
|
||||
to: END,
|
||||
when: "steps[role='planner'].meta.status = 'aborted'",
|
||||
},
|
||||
{ from: "planner", to: "coder", when: null },
|
||||
];
|
||||
|
||||
test("planner aborted → __end__", async () => {
|
||||
const ctx = makeCtx([{ role: "planner", meta: { status: "aborted" } }]);
|
||||
expect(await evaluateModerator(plannerAbortedRules, ctx)).toBe(END);
|
||||
});
|
||||
|
||||
test("planner planned → coder", async () => {
|
||||
const ctx = makeCtx([{ role: "planner", meta: { status: "planned", phases: [] } }]);
|
||||
expect(await evaluateModerator(plannerAbortedRules, ctx)).toBe("coder");
|
||||
});
|
||||
});
|
||||
|
||||
// ── Step 5: reviewApproved / testsPassed (negative index) ────────
|
||||
|
||||
describe("Step 5: last-step conditions", () => {
|
||||
const rules: ModeratorRule[] = [
|
||||
{
|
||||
from: "reviewer",
|
||||
to: "tester",
|
||||
when: "steps[-1].meta.status = 'approved'",
|
||||
},
|
||||
{ from: "reviewer", to: "coder", when: null },
|
||||
{
|
||||
from: "tester",
|
||||
to: "committer",
|
||||
when: "steps[-1].meta.status = 'passed'",
|
||||
},
|
||||
{ from: "tester", to: "coder", when: null },
|
||||
];
|
||||
|
||||
test("reviewer approved → tester", async () => {
|
||||
const ctx = makeCtx([{ role: "reviewer", meta: { status: "approved" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("tester");
|
||||
});
|
||||
|
||||
test("reviewer changes-requested → coder", async () => {
|
||||
const ctx = makeCtx([{ role: "reviewer", meta: { status: "changes-requested" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("coder");
|
||||
});
|
||||
|
||||
test("tester passed → committer", async () => {
|
||||
const ctx = makeCtx([{ role: "tester", meta: { status: "passed" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("committer");
|
||||
});
|
||||
|
||||
test("tester failed → coder", async () => {
|
||||
const ctx = makeCtx([{ role: "tester", meta: { status: "failed" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("coder");
|
||||
});
|
||||
});
|
||||
|
||||
// ── Step 6: allPhasesComplete condition ──────────────────────────
|
||||
|
||||
describe("Step 6: allPhasesComplete", () => {
|
||||
// The JSONata expression checks if all planned phase hashes appear in coder completedPhase values.
|
||||
// Approximation using $count approach:
|
||||
// Use JSONata 'in' operator inside $filter to check if each planned phase hash
|
||||
// appears in the coder completedPhase values.
|
||||
const allPhasesExpr =
|
||||
"$count(steps[role='planner'].meta.phases) = 0 or $count(steps[role='planner'].meta.phases ~> $filter(function($p) { $p.hash in steps[role='coder'].meta.completedPhase })) >= $count(steps[role='planner'].meta.phases)";
|
||||
|
||||
const rules: ModeratorRule[] = [
|
||||
{ from: "coder", to: "reviewer", when: allPhasesExpr },
|
||||
{ from: "coder", to: "coder", when: null },
|
||||
];
|
||||
|
||||
test("no phases (empty array) → all complete → reviewer", async () => {
|
||||
const ctx = makeCtx([
|
||||
{ role: "planner", meta: { status: "planned", phases: [] } },
|
||||
{ role: "coder", meta: { completedPhase: "PHASE001" } },
|
||||
]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("reviewer");
|
||||
});
|
||||
|
||||
test("all phases completed → reviewer", async () => {
|
||||
const ctx = makeCtx([
|
||||
{
|
||||
role: "planner",
|
||||
meta: {
|
||||
status: "planned",
|
||||
phases: [{ hash: "PHASE001" }, { hash: "PHASE002" }],
|
||||
},
|
||||
},
|
||||
{ role: "coder", meta: { completedPhase: "PHASE001" } },
|
||||
{ role: "coder", meta: { completedPhase: "PHASE002" } },
|
||||
]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("reviewer");
|
||||
});
|
||||
|
||||
test("not all phases completed → coder (loop)", async () => {
|
||||
const ctx = makeCtx([
|
||||
{
|
||||
role: "planner",
|
||||
meta: {
|
||||
status: "planned",
|
||||
phases: [{ hash: "PHASE001" }, { hash: "PHASE002" }],
|
||||
},
|
||||
},
|
||||
{ role: "coder", meta: { completedPhase: "PHASE001" } },
|
||||
]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("coder");
|
||||
});
|
||||
});
|
||||
|
||||
// ── Step 7: Full develop workflow routing table ───────────────────
|
||||
|
||||
describe("Step 7: full develop workflow routing", () => {
|
||||
const allPhasesExpr =
|
||||
"$count(steps[role='planner'].meta.phases) = 0 or $count(steps[role='planner'].meta.phases ~> $filter(function($p) { $p.hash in steps[role='coder'].meta.completedPhase })) >= $count(steps[role='planner'].meta.phases)";
|
||||
|
||||
const rules: ModeratorRule[] = [
|
||||
{ from: START, to: "planner", when: null },
|
||||
{ from: "planner", to: END, when: "steps[role='planner'].meta.status = 'aborted'" },
|
||||
{ from: "planner", to: "coder", when: null },
|
||||
{ from: "coder", to: "reviewer", when: allPhasesExpr },
|
||||
{ from: "coder", to: "coder", when: null },
|
||||
{ from: "reviewer", to: "tester", when: "steps[-1].meta.status = 'approved'" },
|
||||
{ from: "reviewer", to: "coder", when: null },
|
||||
{ from: "tester", to: "committer", when: "steps[-1].meta.status = 'passed'" },
|
||||
{ from: "tester", to: "coder", when: null },
|
||||
{ from: "committer", to: END, when: null },
|
||||
];
|
||||
|
||||
test("initial state → planner", async () => {
|
||||
expect(await evaluateModerator(rules, makeCtx([]))).toBe("planner");
|
||||
});
|
||||
|
||||
test("planner planned, no phases → coder", async () => {
|
||||
const ctx = makeCtx([{ role: "planner", meta: { status: "planned", phases: [] } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("coder");
|
||||
});
|
||||
|
||||
test("planner aborted → __end__", async () => {
|
||||
const ctx = makeCtx([{ role: "planner", meta: { status: "aborted" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe(END);
|
||||
});
|
||||
|
||||
test("coder completed single phase → reviewer", async () => {
|
||||
const ctx = makeCtx([
|
||||
{ role: "planner", meta: { status: "planned", phases: [{ hash: "PH1" }] } },
|
||||
{ role: "coder", meta: { completedPhase: "PH1" } },
|
||||
]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("reviewer");
|
||||
});
|
||||
|
||||
test("coder incomplete phases → coder", async () => {
|
||||
const ctx = makeCtx([
|
||||
{
|
||||
role: "planner",
|
||||
meta: { status: "planned", phases: [{ hash: "PH1" }, { hash: "PH2" }] },
|
||||
},
|
||||
{ role: "coder", meta: { completedPhase: "PH1" } },
|
||||
]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("coder");
|
||||
});
|
||||
|
||||
test("reviewer approved → tester", async () => {
|
||||
const ctx = makeCtx([{ role: "reviewer", meta: { status: "approved" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("tester");
|
||||
});
|
||||
|
||||
test("reviewer rejected → coder", async () => {
|
||||
const ctx = makeCtx([{ role: "reviewer", meta: { status: "changes-requested" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("coder");
|
||||
});
|
||||
|
||||
test("tester passed → committer", async () => {
|
||||
const ctx = makeCtx([{ role: "tester", meta: { status: "passed" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("committer");
|
||||
});
|
||||
|
||||
test("tester failed → coder", async () => {
|
||||
const ctx = makeCtx([{ role: "tester", meta: { status: "failed" } }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe("coder");
|
||||
});
|
||||
|
||||
test("committer done → __end__", async () => {
|
||||
const ctx = makeCtx([{ role: "committer", meta: {} }]);
|
||||
expect(await evaluateModerator(rules, ctx)).toBe(END);
|
||||
});
|
||||
});
|
||||
@@ -28,5 +28,8 @@
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"dependencies": {
|
||||
"jsonata": "^2.2.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ export type {
|
||||
ModeratorContext,
|
||||
ModeratorTable,
|
||||
ModeratorTransition,
|
||||
PackageDescriptor,
|
||||
ProviderConfig,
|
||||
ResolvedModel,
|
||||
Result,
|
||||
@@ -54,3 +55,8 @@ export { END, START } from "./types.js";
|
||||
// ── Constructor functions ──────────────────────────────────────────
|
||||
|
||||
export { err, ok } from "./result.js";
|
||||
|
||||
// ── JSONata moderator ──────────────────────────────────────────────
|
||||
|
||||
export type { ModeratorRule } from "./jsonata-moderator.js";
|
||||
export { evaluateModerator } from "./jsonata-moderator.js";
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import jsonata from "jsonata";
|
||||
|
||||
import type { ModeratorContext, RoleMeta, StartStep } from "./types.js";
|
||||
import { END, START } from "./types.js";
|
||||
|
||||
// ── Types ───────────────────────────────────────────────────────────
|
||||
|
||||
export type ModeratorRule = {
|
||||
from: string;
|
||||
to: string;
|
||||
when: string | null;
|
||||
};
|
||||
|
||||
type JsonataContext = {
|
||||
threadId: string;
|
||||
depth: number;
|
||||
start: StartStep;
|
||||
steps: ReadonlyArray<{ role: string; meta: Record<string, unknown> }>;
|
||||
};
|
||||
|
||||
// ── Evaluator ───────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Evaluate a JSONata-based moderator rule set against the given thread context.
|
||||
* Returns the next role name or '__end__'.
|
||||
*/
|
||||
export async function evaluateModerator(
|
||||
rules: ReadonlyArray<ModeratorRule>,
|
||||
context: ModeratorContext,
|
||||
): Promise<string> {
|
||||
const lastStep = context.steps.length > 0 ? context.steps[context.steps.length - 1] : null;
|
||||
const currentState: string = lastStep ? lastStep.role : START;
|
||||
|
||||
const matching = rules.filter((r) => r.from === currentState);
|
||||
|
||||
const jsonataCtx: JsonataContext = {
|
||||
threadId: context.threadId,
|
||||
depth: context.depth,
|
||||
start: context.start,
|
||||
steps: context.steps as ReadonlyArray<{ role: string; meta: Record<string, unknown> }>,
|
||||
};
|
||||
|
||||
for (const rule of matching) {
|
||||
if (rule.when === null) {
|
||||
return rule.to;
|
||||
}
|
||||
|
||||
const expr = jsonata(rule.when);
|
||||
const result = await expr.evaluate(jsonataCtx);
|
||||
|
||||
if (result) {
|
||||
return rule.to;
|
||||
}
|
||||
}
|
||||
|
||||
return END;
|
||||
}
|
||||
|
||||
// ── Context helper ──────────────────────────────────────────────────
|
||||
|
||||
/** Build a ModeratorContext from its constituent parts (convenience for tests / callers). */
|
||||
export function makeModeratorContext<M extends RoleMeta>(
|
||||
ctx: ModeratorContext<M>,
|
||||
): ModeratorContext<M> {
|
||||
return ctx;
|
||||
}
|
||||
@@ -130,6 +130,26 @@ export type WorkflowConfig = {
|
||||
models: Record<string, string>;
|
||||
};
|
||||
|
||||
// ── Package Descriptor ────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Static metadata describing a workflow agent npm package.
|
||||
* Stored alongside the CAS agent node to document what an agent instance is.
|
||||
*/
|
||||
export type PackageDescriptor = {
|
||||
/** The npm package name, e.g. `@uncaged/workflow-agent-cursor`. */
|
||||
name: string;
|
||||
/** Semver version of the package at the time the descriptor was written. */
|
||||
version: string;
|
||||
/** Human-readable capability tags, e.g. `["cursor-cli", "workspace-agent"]`. */
|
||||
capabilities: string[];
|
||||
/**
|
||||
* JSON Schema that describes the serializable config stored in the CAS
|
||||
* agent node's `config` field.
|
||||
*/
|
||||
configSchema: Record<string, unknown>;
|
||||
};
|
||||
|
||||
// ── Functions ──────────────────────────────────────────────────────
|
||||
|
||||
/** Structured output of the extract phase (RFC v3 content Merkle + artifact refs). */
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import {
|
||||
END,
|
||||
START,
|
||||
type ModeratorTable,
|
||||
type WorkflowDefinition,
|
||||
} from "@uncaged/workflow-protocol";
|
||||
import * as z from "zod/v4";
|
||||
import { buildDescriptor } from "../src/bundle/build-descriptor.js";
|
||||
|
||||
const phaseSchema = z.object({
|
||||
hash: z.string().meta({ "x-cas-ref": true }),
|
||||
title: z.string(),
|
||||
});
|
||||
|
||||
type TestMeta = {
|
||||
planner: { phases: Array<{ hash: string; title: string }>; label: string };
|
||||
};
|
||||
|
||||
const testTable: ModeratorTable<TestMeta> = {
|
||||
[START]: [{ condition: "FALLBACK", role: "planner" }],
|
||||
planner: [{ condition: "FALLBACK", role: END }],
|
||||
};
|
||||
|
||||
describe("buildDescriptor", () => {
|
||||
test("preserves x-cas-ref in role JSON Schema", () => {
|
||||
const def: WorkflowDefinition<TestMeta> = {
|
||||
description: "test workflow",
|
||||
roles: {
|
||||
planner: {
|
||||
description: "plans work",
|
||||
systemPrompt: "plan",
|
||||
schema: z.object({
|
||||
phases: z.array(phaseSchema),
|
||||
label: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
table: testTable,
|
||||
};
|
||||
|
||||
const descriptor = buildDescriptor(def);
|
||||
const props = (descriptor.roles.planner.schema as { properties: Record<string, unknown> })
|
||||
.properties;
|
||||
const phaseProps = (
|
||||
(props.phases as { items: { properties: Record<string, unknown> } }).items
|
||||
).properties;
|
||||
|
||||
expect((phaseProps.hash as Record<string, unknown>)["x-cas-ref"]).toBe(true);
|
||||
expect((phaseProps.title as Record<string, unknown>)["x-cas-ref"]).toBeUndefined();
|
||||
expect((props.label as Record<string, unknown>)["x-cas-ref"]).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -29,6 +29,9 @@
|
||||
"zod": "^4.0.0",
|
||||
"typescript": "^5.8.3"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "bun test"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as z from "zod/v4";
|
||||
import { collectCasRefs } from "../src/collect-cas-refs.js";
|
||||
|
||||
const phaseSchema = z.object({
|
||||
hash: z.string().meta({ casRef: true }),
|
||||
hash: z.string().meta({ 'x-cas-ref': true }),
|
||||
title: z.string(),
|
||||
});
|
||||
|
||||
@@ -19,9 +19,9 @@ const plannerMetaSchema = z.discriminatedUnion("status", [
|
||||
]);
|
||||
|
||||
describe("collectCasRefs", () => {
|
||||
test("1. flat field with casRef annotation", () => {
|
||||
test("1. flat field with x-cas-ref annotation", () => {
|
||||
const schema = z.object({
|
||||
completedPhase: z.string().meta({ casRef: true }),
|
||||
completedPhase: z.string().meta({ 'x-cas-ref': true }),
|
||||
});
|
||||
expect(collectCasRefs(schema, { completedPhase: "BHAAAAAAAAAAA" })).toEqual(["BHAAAAAAAAAAA"]);
|
||||
});
|
||||
@@ -29,7 +29,7 @@ describe("collectCasRefs", () => {
|
||||
test("2. plain string without annotation is ignored", () => {
|
||||
const schema = z.object({
|
||||
summary: z.string(),
|
||||
completedPhase: z.string().meta({ casRef: true }),
|
||||
completedPhase: z.string().meta({ 'x-cas-ref': true }),
|
||||
});
|
||||
expect(
|
||||
collectCasRefs(schema, {
|
||||
@@ -76,8 +76,8 @@ describe("collectCasRefs", () => {
|
||||
|
||||
test("5. null and undefined annotated fields are skipped", () => {
|
||||
const schema = z.object({
|
||||
ref: z.string().meta({ casRef: true }).nullable(),
|
||||
optionalRef: z.string().meta({ casRef: true }).optional(),
|
||||
ref: z.string().meta({ 'x-cas-ref': true }).nullable(),
|
||||
optionalRef: z.string().meta({ 'x-cas-ref': true }).optional(),
|
||||
});
|
||||
expect(collectCasRefs(schema, { ref: null, optionalRef: undefined })).toEqual([]);
|
||||
expect(collectCasRefs(schema, { ref: "BH55555555555", optionalRef: undefined })).toEqual([
|
||||
@@ -89,7 +89,7 @@ describe("collectCasRefs", () => {
|
||||
const schema = z.object({
|
||||
label: z.string(),
|
||||
phase: z.object({
|
||||
hash: z.string().meta({ casRef: true }),
|
||||
hash: z.string().meta({ 'x-cas-ref': true }),
|
||||
title: z.string(),
|
||||
}),
|
||||
tags: z.array(z.string()),
|
||||
|
||||
@@ -6,7 +6,7 @@ type DefPipeIn = { in: ZodSchema };
|
||||
|
||||
function hasCasRef(schema: ZodSchema): boolean {
|
||||
const meta = z.globalRegistry.get(schema);
|
||||
return meta !== undefined && meta.casRef === true;
|
||||
return meta !== undefined && meta["x-cas-ref"] === true;
|
||||
}
|
||||
|
||||
function walkOptional(schema: z.ZodOptional<ZodSchema>, data: unknown): string[] {
|
||||
@@ -116,7 +116,7 @@ function walkCasRefs(schema: ZodSchema, data: unknown): string[] {
|
||||
}
|
||||
}
|
||||
|
||||
/** Collect CAS content hashes from meta using `casRef` annotations on the Zod schema. */
|
||||
/** Collect CAS content hashes from meta using `x-cas-ref` annotations on the Zod schema. */
|
||||
export function collectCasRefs(schema: ZodSchema, data: unknown): string[] {
|
||||
return walkCasRefs(schema, data);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ export type {
|
||||
ModeratorCondition,
|
||||
ModeratorContext,
|
||||
ModeratorTable,
|
||||
PackageDescriptor,
|
||||
Result,
|
||||
RoleDefinition,
|
||||
RoleFn,
|
||||
|
||||
Generated
+141
@@ -0,0 +1,141 @@
|
||||
lockfileVersion: '9.0'
|
||||
|
||||
settings:
|
||||
autoInstallPeers: true
|
||||
excludeLinksFromLockfile: false
|
||||
|
||||
importers:
|
||||
|
||||
.:
|
||||
devDependencies:
|
||||
'@biomejs/biome':
|
||||
specifier: ^2.4.14
|
||||
version: 2.4.15
|
||||
'@types/xxhashjs':
|
||||
specifier: ^0.2.4
|
||||
version: 0.2.4
|
||||
bun-types:
|
||||
specifier: ^1.3.13
|
||||
version: 1.3.13
|
||||
|
||||
packages:
|
||||
|
||||
'@biomejs/biome@2.4.15':
|
||||
resolution: {integrity: sha512-j5VH3a/h/HXTKBM50MDMxRCzkeLv9S2XJcW2WgnZT1+xyisi+0bISrXR82gCX+8S9lvK0skEvHJRN+3Ktr2hlw==}
|
||||
engines: {node: '>=14.21.3'}
|
||||
hasBin: true
|
||||
|
||||
'@biomejs/cli-darwin-arm64@2.4.15':
|
||||
resolution: {integrity: sha512-rF3PPqLq1yoST79zaQbDjVJwsuIeci/O+9bgNmC5QpgOqz6aqYuzA4abyAGx+mgyiDXn4A049xAN8gijbuR1Qg==}
|
||||
engines: {node: '>=14.21.3'}
|
||||
cpu: [arm64]
|
||||
os: [darwin]
|
||||
|
||||
'@biomejs/cli-darwin-x64@2.4.15':
|
||||
resolution: {integrity: sha512-/5KHXYMfSJs1fNXiX30xFtI8JcCFV6zaVVLxOa0M2sfqBKHkpQhRTv94yxQWxeTY2lzo2OuTlNvPC+hDQt2wcQ==}
|
||||
engines: {node: '>=14.21.3'}
|
||||
cpu: [x64]
|
||||
os: [darwin]
|
||||
|
||||
'@biomejs/cli-linux-arm64-musl@2.4.15':
|
||||
resolution: {integrity: sha512-ZPcxznxm0pogHBLZhYntyR3sR+MrZjqJIKEr7ZqVen0Rl+P/4upVmfYXjftizi9RoqZntg33fv/1fbdhbYXpEQ==}
|
||||
engines: {node: '>=14.21.3'}
|
||||
cpu: [arm64]
|
||||
os: [linux]
|
||||
libc: [musl]
|
||||
|
||||
'@biomejs/cli-linux-arm64@2.4.15':
|
||||
resolution: {integrity: sha512-owaAMZD/T4LrD0ELNCk0Km3qrRHuM0X6EAyVE1FSqGY0rbLoiDLrO4Us2tllm6cAeB2Ioa9C2C08NZPdr8+0Ug==}
|
||||
engines: {node: '>=14.21.3'}
|
||||
cpu: [arm64]
|
||||
os: [linux]
|
||||
libc: [glibc]
|
||||
|
||||
'@biomejs/cli-linux-x64-musl@2.4.15':
|
||||
resolution: {integrity: sha512-CNq/9W38SYSH023lfcQ4KKU8K0YX8T//FZUhcgtMMRABDojx5XsMV7jlweAvGSl389wJQB29Qo6Zb/a+jdvt+w==}
|
||||
engines: {node: '>=14.21.3'}
|
||||
cpu: [x64]
|
||||
os: [linux]
|
||||
libc: [musl]
|
||||
|
||||
'@biomejs/cli-linux-x64@2.4.15':
|
||||
resolution: {integrity: sha512-0jj7THz12GbUOLmMibktK6DZjqz2zV64KFxyBtcFTKPiiOIY0a7vns1elpO1dERvxpsZ5ik0oFfz0oGwFde1+g==}
|
||||
engines: {node: '>=14.21.3'}
|
||||
cpu: [x64]
|
||||
os: [linux]
|
||||
libc: [glibc]
|
||||
|
||||
'@biomejs/cli-win32-arm64@2.4.15':
|
||||
resolution: {integrity: sha512-ouhkYdlhp/1GghEJPdWwD/Vi3gQ1nFxuSpMolWsbq3Lsq3QUR4jl6UdhhscdCugKU5vOEuMiJhvKj66O0OCq+w==}
|
||||
engines: {node: '>=14.21.3'}
|
||||
cpu: [arm64]
|
||||
os: [win32]
|
||||
|
||||
'@biomejs/cli-win32-x64@2.4.15':
|
||||
resolution: {integrity: sha512-zBrGq5mx5wwpnow4+2BxUvleDM+GNd4sLbPaMapsSLQLD0NGRCquqPBTgN+7XkUteHvj7M+BstuI8tmnV7+HgQ==}
|
||||
engines: {node: '>=14.21.3'}
|
||||
cpu: [x64]
|
||||
os: [win32]
|
||||
|
||||
'@types/node@25.6.2':
|
||||
resolution: {integrity: sha512-sokuT28dxf9JT5Kady1fsXOvI4HVpjZa95NKT5y9PNTIrs2AsobR4GFAA90ZG8M+nxVRLysCXsVj6eGC7Vbrlw==}
|
||||
|
||||
'@types/xxhashjs@0.2.4':
|
||||
resolution: {integrity: sha512-E2+ZoJY2JjmVPN0iQM5gJvZkk98O2PYXSi6HrciEk3EKF34+mauEk/HgwTeCz+2r8HXHMKpucrwy4qTT12OPaQ==}
|
||||
|
||||
bun-types@1.3.13:
|
||||
resolution: {integrity: sha512-QXKeHLlOLqQX9LgYaHJfzdBaV21T63HhFJnvuRCcjZiaUDpbs5ED1MgxbMra71CsryN/1dAoXuJJJwIv/2drVA==}
|
||||
|
||||
undici-types@7.19.2:
|
||||
resolution: {integrity: sha512-qYVnV5OEm2AW8cJMCpdV20CDyaN3g0AjDlOGf1OW4iaDEx8MwdtChUp4zu4H0VP3nDRF/8RKWH+IPp9uW0YGZg==}
|
||||
|
||||
snapshots:
|
||||
|
||||
'@biomejs/biome@2.4.15':
|
||||
optionalDependencies:
|
||||
'@biomejs/cli-darwin-arm64': 2.4.15
|
||||
'@biomejs/cli-darwin-x64': 2.4.15
|
||||
'@biomejs/cli-linux-arm64': 2.4.15
|
||||
'@biomejs/cli-linux-arm64-musl': 2.4.15
|
||||
'@biomejs/cli-linux-x64': 2.4.15
|
||||
'@biomejs/cli-linux-x64-musl': 2.4.15
|
||||
'@biomejs/cli-win32-arm64': 2.4.15
|
||||
'@biomejs/cli-win32-x64': 2.4.15
|
||||
|
||||
'@biomejs/cli-darwin-arm64@2.4.15':
|
||||
optional: true
|
||||
|
||||
'@biomejs/cli-darwin-x64@2.4.15':
|
||||
optional: true
|
||||
|
||||
'@biomejs/cli-linux-arm64-musl@2.4.15':
|
||||
optional: true
|
||||
|
||||
'@biomejs/cli-linux-arm64@2.4.15':
|
||||
optional: true
|
||||
|
||||
'@biomejs/cli-linux-x64-musl@2.4.15':
|
||||
optional: true
|
||||
|
||||
'@biomejs/cli-linux-x64@2.4.15':
|
||||
optional: true
|
||||
|
||||
'@biomejs/cli-win32-arm64@2.4.15':
|
||||
optional: true
|
||||
|
||||
'@biomejs/cli-win32-x64@2.4.15':
|
||||
optional: true
|
||||
|
||||
'@types/node@25.6.2':
|
||||
dependencies:
|
||||
undici-types: 7.19.2
|
||||
|
||||
'@types/xxhashjs@0.2.4':
|
||||
dependencies:
|
||||
'@types/node': 25.6.2
|
||||
|
||||
bun-types@1.3.13:
|
||||
dependencies:
|
||||
'@types/node': 25.6.2
|
||||
|
||||
undici-types@7.19.2: {}
|
||||
@@ -1,12 +0,0 @@
|
||||
#!/usr/bin/env bun
|
||||
// Mock agent for smoke testing
|
||||
import { createAgent } from "../packages/uwf-agent-kit/src/index.js";
|
||||
|
||||
const agent = createAgent({
|
||||
name: "mock",
|
||||
run: async (ctx) => {
|
||||
return `Mock output for role ${ctx.role}: task was "${ctx.prompt}"`;
|
||||
},
|
||||
});
|
||||
|
||||
await agent();
|
||||
@@ -1,59 +0,0 @@
|
||||
name: "solve-issue"
|
||||
description: "End-to-end issue resolution"
|
||||
roles:
|
||||
planner:
|
||||
description: "Creates implementation plan"
|
||||
systemPrompt: "You are a planning agent. Analyze the issue and create a step-by-step plan."
|
||||
outputSchema:
|
||||
type: object
|
||||
properties:
|
||||
plan:
|
||||
type: string
|
||||
steps:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
required: [plan, steps]
|
||||
developer:
|
||||
description: "Implements code changes"
|
||||
systemPrompt: "You are a developer agent. Implement the plan."
|
||||
outputSchema:
|
||||
type: object
|
||||
properties:
|
||||
filesChanged:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
summary:
|
||||
type: string
|
||||
required: [filesChanged, summary]
|
||||
reviewer:
|
||||
description: "Reviews code changes"
|
||||
systemPrompt: "You are a code reviewer. Review the implementation."
|
||||
outputSchema:
|
||||
type: object
|
||||
properties:
|
||||
approved:
|
||||
type: boolean
|
||||
comments:
|
||||
type: string
|
||||
required: [approved, comments]
|
||||
conditions:
|
||||
notApproved:
|
||||
description: "Reviewer rejected the implementation"
|
||||
expression: "steps[-1].output.approved = false"
|
||||
graph:
|
||||
$START:
|
||||
- role: "planner"
|
||||
condition: null
|
||||
planner:
|
||||
- role: "developer"
|
||||
condition: null
|
||||
developer:
|
||||
- role: "reviewer"
|
||||
condition: null
|
||||
reviewer:
|
||||
- role: "developer"
|
||||
condition: "notApproved"
|
||||
- role: "$END"
|
||||
condition: null
|
||||
+1
-5
@@ -33,10 +33,6 @@
|
||||
{ "path": "packages/cli-workflow" },
|
||||
{ "path": "packages/workflow-template-solve-issue" },
|
||||
{ "path": "packages/workflow-template-develop" },
|
||||
{ "path": "packages/uwf-protocol" },
|
||||
{ "path": "packages/uwf-moderator" },
|
||||
{ "path": "packages/cli-uwf" },
|
||||
{ "path": "packages/uwf-agent-kit" },
|
||||
{ "path": "packages/uwf-agent-hermes" }
|
||||
{ "path": "packages/workflow-json-def" }
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user