Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a5c09adae6 | |||
| f81e2a8aac | |||
| 2b38e583be | |||
| 4ff1394224 | |||
| 2bbe5a3d0e | |||
| a4237c0462 | |||
| 321e5b1379 | |||
| 7c3e14c473 | |||
| aecce595e8 | |||
| cf17dedac3 | |||
| 661fdbb263 | |||
| 201abf98ce | |||
| 665965fd01 | |||
| 6a99f84025 | |||
| f61474bec0 | |||
| 9bdb18afd0 | |||
| 2af299f3ce | |||
| d9f79c60a1 | |||
| 485bfcb0b6 | |||
| a47ed06ea5 | |||
| 2ef004eecf | |||
| 2616259a0f | |||
| 23b2c3b47d | |||
| 7d3954097d | |||
| 4a925b98af | |||
| bfea771a52 | |||
| 5e411a1f19 | |||
| 21238f7825 | |||
| 6b3aa4ce35 | |||
| f042c9d640 | |||
| 66bca9ef03 | |||
| 309af39447 | |||
| 86a422f7e2 | |||
| 648f0c6dec | |||
| 8456a8337b | |||
| 9c8b98a551 | |||
| c3272be760 | |||
| c44b773a86 | |||
| 2776f8e419 |
@@ -97,6 +97,36 @@ type WorkflowEntry = {
|
|||||||
|
|
||||||
Workflow bundles (`.esm.js`) follow the same rule: export `const run` and `const descriptor`, not `export default`.
|
Workflow bundles (`.esm.js`) follow the same rule: export `const run` and `const descriptor`, not `export default`.
|
||||||
|
|
||||||
|
### Folder Module Discipline
|
||||||
|
|
||||||
|
Every folder under `src/` is a **module boundary**. Four rules:
|
||||||
|
|
||||||
|
| # | Rule | Rationale |
|
||||||
|
|---|------|-----------|
|
||||||
|
| 1 | **Every folder exports via `index.ts`** | Single entry point for the module |
|
||||||
|
| 2 | **Types live in `types.ts`** | Each folder's type definitions go in `<folder>/types.ts`, not scattered across files |
|
||||||
|
| 3 | **Single export source** | Only `index.ts` may re-export. No file may re-export from another module's internals. Cross-module imports must go through `index.ts` — never reach past it to import a specific file |
|
||||||
|
| 4 | **`index.ts` is pure re-exports** | No type definitions, no function implementations — only `export { ... } from` statements |
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Good — import through module boundary
|
||||||
|
import { createCasStore } from "../cas/index.js";
|
||||||
|
import type { CasStore } from "../cas/index.js";
|
||||||
|
|
||||||
|
// ❌ Bad — reaching past index.ts
|
||||||
|
import { createCasStore } from "../cas/cas.js";
|
||||||
|
|
||||||
|
// ❌ Bad — re-exporting from non-index file
|
||||||
|
// in engine/engine.ts:
|
||||||
|
export { createCasStore } from "../cas/cas.js";
|
||||||
|
|
||||||
|
// ❌ Bad — types defined in index.ts
|
||||||
|
// in cas/index.ts:
|
||||||
|
export type CasStore = { ... }; // should be in cas/types.ts
|
||||||
|
```
|
||||||
|
|
||||||
|
**Exception**: The package-level `src/index.ts` is the public API surface and re-exports from folder `index.ts` files. Files that remain at `src/` root (e.g. `types.ts`, `workflow-as-agent.ts`) are not inside a folder module and follow normal rules.
|
||||||
|
|
||||||
## Naming
|
## Naming
|
||||||
|
|
||||||
| Type | Style | Example |
|
| Type | Style | Example |
|
||||||
@@ -197,9 +227,8 @@ Test files (`__tests__/**`) are exempt.
|
|||||||
### Commands
|
### Commands
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bun run check # biome check (lint + format)
|
bun run check # tsc --build + biome check
|
||||||
bun run format # biome format --write
|
bun run format # biome format --write
|
||||||
bun run build # full build
|
|
||||||
bun test # run tests
|
bun test # run tests
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
# @uncaged/workflow
|
||||||
|
|
||||||
|
A workflow engine that executes single-file ESM bundles. Each workflow is a self-contained `.esm.js` file identified by its XXH64 hash (Crockford Base32).
|
||||||
|
|
||||||
|
## Core Concepts
|
||||||
|
|
||||||
|
| Concept | Description |
|
||||||
|
|---------|-------------|
|
||||||
|
| **Workflow** | A single-file ESM module exporting `run` (workflow function) and `descriptor` (metadata). Identified by its XXH64 hash. |
|
||||||
|
| **Bundle** | The physical `.esm.js` file stored in `~/.uncaged/workflow/bundles/`. |
|
||||||
|
| **Thread** | A single execution of a workflow, identified by a ULID. Persisted as `.data.jsonl` + `.info.jsonl`. |
|
||||||
|
| **Role** | A named actor within a workflow. Each role produces output with typed `meta`. Roles live inside template packages (`src/roles/`). |
|
||||||
|
| **Registry** | `workflow.yaml` — maps workflow names to current/historical bundle hashes. |
|
||||||
|
| **CAS** | Content-Addressed Storage — bundles are immutable and addressed by hash. |
|
||||||
|
|
||||||
|
## Monorepo Packages
|
||||||
|
|
||||||
|
```
|
||||||
|
packages/
|
||||||
|
workflow/ # @uncaged/workflow — core lib (types, engine, hash, ULID, registry)
|
||||||
|
cli-workflow/ # @uncaged/cli-workflow — CLI (`uncaged-workflow` command)
|
||||||
|
workflow-template-develop/ # @uncaged/workflow-template-develop — develop workflow template (includes roles)
|
||||||
|
workflow-template-solve-issue/ # @uncaged/workflow-template-solve-issue — solve-issue workflow template (includes roles)
|
||||||
|
workflow-agent-hermes/ # @uncaged/workflow-agent-hermes — Hermes agent adapter
|
||||||
|
workflow-agent-cursor/ # @uncaged/workflow-agent-cursor — Cursor agent adapter
|
||||||
|
workflow-agent-llm/ # @uncaged/workflow-agent-llm — LLM agent adapter
|
||||||
|
workflow-util-agent/ # @uncaged/workflow-util-agent — agent utilities (buildAgentPrompt, spawnCli)
|
||||||
|
```
|
||||||
|
|
||||||
|
Managed with **bun workspace** using the `workspace:*` protocol.
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Install dependencies
|
||||||
|
bun install
|
||||||
|
|
||||||
|
# Build all packages
|
||||||
|
bun run build
|
||||||
|
|
||||||
|
# Register a workflow bundle
|
||||||
|
uncaged-workflow workflow add solve-issue dist/packages/workflow-template-solve-issue/solve-issue.esm.js
|
||||||
|
|
||||||
|
# Run a workflow
|
||||||
|
uncaged-workflow run solve-issue --prompt "Fix bug #42"
|
||||||
|
```
|
||||||
|
|
||||||
|
## CLI Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uncaged-workflow help # Show all commands
|
||||||
|
uncaged-workflow workflow list # List registered workflows
|
||||||
|
uncaged-workflow run <name> # Start a workflow thread
|
||||||
|
uncaged-workflow thread list # List all threads
|
||||||
|
uncaged-workflow thread show <id> # Inspect a thread
|
||||||
|
uncaged-workflow skill # Agent-consumable reference docs
|
||||||
|
```
|
||||||
|
|
||||||
|
See `uncaged-workflow help` for the full command reference.
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun run check # Biome lint + format check
|
||||||
|
bun run format # Auto-format with Biome
|
||||||
|
bun test # Run tests
|
||||||
|
```
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
See [docs/architecture.md](docs/architecture.md) for the full design — three-phase engine loop, bundle contract, storage layout, and design decisions.
|
||||||
@@ -17,11 +17,8 @@ A workflow engine that executes single-file ESM bundles. Each workflow is a self
|
|||||||
| `workflow-agent-cursor` | `@uncaged/workflow-agent-cursor` | Cursor CLI agent (extracts workspace from ctx) |
|
| `workflow-agent-cursor` | `@uncaged/workflow-agent-cursor` | Cursor CLI agent (extracts workspace from ctx) |
|
||||||
| `workflow-agent-hermes` | `@uncaged/workflow-agent-hermes` | Hermes CLI agent |
|
| `workflow-agent-hermes` | `@uncaged/workflow-agent-hermes` | Hermes CLI agent |
|
||||||
| `workflow-agent-llm` | `@uncaged/workflow-agent-llm` | OpenAI-compatible LLM agent |
|
| `workflow-agent-llm` | `@uncaged/workflow-agent-llm` | OpenAI-compatible LLM agent |
|
||||||
| `workflow-role-planner` | `@uncaged/workflow-role-planner` | Pure data: phased planning prompt + schema |
|
| `workflow-template-develop` | `@uncaged/workflow-template-develop` | Develop workflow template (roles in `src/roles/`) |
|
||||||
| `workflow-role-coder` | `@uncaged/workflow-role-coder` | Pure data: coding prompt + schema |
|
| `workflow-template-solve-issue` | `@uncaged/workflow-template-solve-issue` | Solve-issue workflow template (roles in `src/roles/`) |
|
||||||
| `workflow-role-reviewer` | `@uncaged/workflow-role-reviewer` | Pure data: code review prompt + schema |
|
|
||||||
| `workflow-role-committer` | `@uncaged/workflow-role-committer` | Pure data: git commit prompt + schema |
|
|
||||||
| `workflow-template-solve-issue` | `@uncaged/workflow-template-solve-issue` | Composes roles + moderator into a complete workflow |
|
|
||||||
| `workflow-util-agent` | `@uncaged/workflow-util-agent` | `buildAgentPrompt` + `spawnCli` utilities |
|
| `workflow-util-agent` | `@uncaged/workflow-util-agent` | `buildAgentPrompt` + `spawnCli` utilities |
|
||||||
|
|
||||||
Monorepo with **bun workspace**, `workspace:*` protocol.
|
Monorepo with **bun workspace**, `workspace:*` protocol.
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
"examples"
|
"examples"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "bun run --filter '*' build",
|
|
||||||
"check": "bunx tsc --build && biome check .",
|
"check": "bunx tsc --build && biome check .",
|
||||||
"typecheck": "bunx tsc --build",
|
"typecheck": "bunx tsc --build",
|
||||||
"format": "biome format --write .",
|
"format": "biome format --write .",
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
# @uncaged/cli-workflow
|
||||||
|
|
||||||
|
Command-line interface for the Uncaged workflow engine (`uncaged-workflow`).
|
||||||
|
|
||||||
|
The CLI reads and writes the workflow registry, starts and inspects threads, manages CAS blobs, and prints agent-oriented reference docs via `skill`. It uses the same storage layout as `@uncaged/workflow` (default `~/.uncaged/workflow`).
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun add @uncaged/cli-workflow
|
||||||
|
```
|
||||||
|
|
||||||
|
In this monorepo: `"@uncaged/cli-workflow": "workspace:*"`. Depends on `"@uncaged/workflow": "workspace:*"`.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uncaged-workflow workflow list
|
||||||
|
uncaged-workflow run <name> --prompt "Your task"
|
||||||
|
uncaged-workflow thread show <id>
|
||||||
|
uncaged-workflow skill
|
||||||
|
```
|
||||||
|
|
||||||
|
Invoking the CLI with no command (or from this repo: `bun packages/cli-workflow/src/cli.ts`) prints:
|
||||||
|
|
||||||
|
```
|
||||||
|
uncaged-workflow — workflow engine CLI
|
||||||
|
|
||||||
|
Workflow registry:
|
||||||
|
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 details of a registered workflow
|
||||||
|
workflow rm <name> Remove a workflow from the registry
|
||||||
|
workflow history <name> Show version history of a workflow
|
||||||
|
workflow rollback <name> [hash] Rollback a workflow to a previous version
|
||||||
|
|
||||||
|
Thread execution:
|
||||||
|
thread run <name> [--prompt <text>] [--max-rounds N] Start a new thread executing a workflow
|
||||||
|
thread list [name] List threads, optionally filtered by workflow name
|
||||||
|
thread show <id> Show thread details and state
|
||||||
|
thread rm <id> Remove a thread
|
||||||
|
thread fork <thread-id> [--from-role <role>] Fork a thread, optionally from a specific role
|
||||||
|
thread ps List running threads
|
||||||
|
thread kill <thread-id> Kill a running thread
|
||||||
|
thread live <thread-id> | --latest [--debug] [--role <name>] Attach to a thread and stream output live
|
||||||
|
thread pause <thread-id> Pause a running thread
|
||||||
|
thread resume <thread-id> Resume a paused thread
|
||||||
|
|
||||||
|
Content-addressable storage:
|
||||||
|
cas get <hash> Retrieve content by hash from CAS
|
||||||
|
cas put <content> Store content in CAS, prints hash
|
||||||
|
cas list List all hashes in CAS
|
||||||
|
cas rm <hash> Remove a CAS entry by hash
|
||||||
|
cas gc Garbage-collect unreferenced CAS entries
|
||||||
|
|
||||||
|
Development:
|
||||||
|
init workspace <name> Initialize a new workflow workspace
|
||||||
|
init template <name> Initialize a new workflow template
|
||||||
|
|
||||||
|
Shortcuts:
|
||||||
|
run <name> [...] → thread run
|
||||||
|
live <id> [...] → thread live
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
skill [topic] Agent-consumable docs (cli, develop, author)
|
||||||
|
|
||||||
|
Use <command> --help for subcommand details.
|
||||||
|
|
||||||
|
Environment variables:
|
||||||
|
WORKFLOW_STORAGE_ROOT Override storage directory (default: ~/.uncaged/workflow)
|
||||||
|
UNCAGED_WORKFLOW_STORAGE_ROOT Internal override (takes priority over WORKFLOW_STORAGE_ROOT)
|
||||||
|
```
|
||||||
|
|
||||||
|
## API overview
|
||||||
|
|
||||||
|
This package is bin-only; programmatic use is via `@uncaged/workflow`. Entry: `src/cli.ts` → `runCli` in `src/cli-dispatch.js`.
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { ParsedAddArgv } from "../src/cmd-add.js";
|
import type { ParsedAddArgv } from "../src/commands/workflow/index.js";
|
||||||
|
|
||||||
export function addCliArgs(name: string, filePath: string): ParsedAddArgv {
|
export function addCliArgs(name: string, filePath: string): ParsedAddArgv {
|
||||||
return { name, filePath, typesPath: null };
|
return { name, filePath, typesPath: null };
|
||||||
|
|||||||
@@ -4,13 +4,16 @@ import { tmpdir } from "node:os";
|
|||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
import { getGlobalCasDir, getRegisteredWorkflow, readWorkflowRegistry } from "@uncaged/workflow";
|
import { getGlobalCasDir, getRegisteredWorkflow, readWorkflowRegistry } from "@uncaged/workflow";
|
||||||
import { cmdAdd } from "../src/cmd-add.js";
|
import { cmdCasGet, cmdCasList, cmdCasPut, cmdCasRm } from "../src/commands/cas/index.js";
|
||||||
import { cmdCasGet, cmdCasList, cmdCasPut, cmdCasRm } from "../src/cmd-cas.js";
|
import {
|
||||||
import { cmdHistory } from "../src/cmd-history.js";
|
cmdAdd,
|
||||||
import { cmdList, formatListLines } from "../src/cmd-list.js";
|
cmdHistory,
|
||||||
import { cmdRemove } from "../src/cmd-remove.js";
|
cmdList,
|
||||||
import { cmdRollback } from "../src/cmd-rollback.js";
|
cmdRemove,
|
||||||
import { cmdShow } from "../src/cmd-show.js";
|
cmdRollback,
|
||||||
|
cmdShow,
|
||||||
|
formatListLines,
|
||||||
|
} from "../src/commands/workflow/index.js";
|
||||||
import { addCliArgs } from "./bundle-fixture.js";
|
import { addCliArgs } from "./bundle-fixture.js";
|
||||||
|
|
||||||
const fixtureDescriptor = `export const descriptor = { description: "fixture", roles: {} };
|
const fixtureDescriptor = `export const descriptor = { description: "fixture", roles: {} };
|
||||||
@@ -399,7 +402,7 @@ export const run = async function* (input, options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("cas put/get/list/rm use global cas dir (thread id not required for storage)", async () => {
|
test("cas put/get/list/rm use global cas dir (thread id not required for storage)", async () => {
|
||||||
const put = await cmdCasPut(storageRoot, "nonexistent-thread-id", "phase doc");
|
const put = await cmdCasPut(storageRoot, "phase doc");
|
||||||
expect(put.ok).toBe(true);
|
expect(put.ok).toBe(true);
|
||||||
if (!put.ok) {
|
if (!put.ok) {
|
||||||
return;
|
return;
|
||||||
@@ -408,24 +411,24 @@ export const run = async function* (input, options) {
|
|||||||
const blobPath = join(getGlobalCasDir(storageRoot), `${hash}.txt`);
|
const blobPath = join(getGlobalCasDir(storageRoot), `${hash}.txt`);
|
||||||
expect(await readFile(blobPath, "utf8")).toBe("phase doc");
|
expect(await readFile(blobPath, "utf8")).toBe("phase doc");
|
||||||
|
|
||||||
const got = await cmdCasGet(storageRoot, "other-thread", hash);
|
const got = await cmdCasGet(storageRoot, hash);
|
||||||
expect(got.ok).toBe(true);
|
expect(got.ok).toBe(true);
|
||||||
if (!got.ok) {
|
if (!got.ok) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
expect(got.value).toBe("phase doc");
|
expect(got.value).toBe("phase doc");
|
||||||
|
|
||||||
const listed = await cmdCasList(storageRoot, "another-thread");
|
const listed = await cmdCasList(storageRoot);
|
||||||
expect(listed.ok).toBe(true);
|
expect(listed.ok).toBe(true);
|
||||||
if (!listed.ok) {
|
if (!listed.ok) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
expect(listed.value).toContain(hash);
|
expect(listed.value).toContain(hash);
|
||||||
|
|
||||||
const removed = await cmdCasRm(storageRoot, "rm-thread", hash);
|
const removed = await cmdCasRm(storageRoot, hash);
|
||||||
expect(removed.ok).toBe(true);
|
expect(removed.ok).toBe(true);
|
||||||
|
|
||||||
const missing = await cmdCasGet(storageRoot, "after-rm", hash);
|
const missing = await cmdCasGet(storageRoot, hash);
|
||||||
expect(missing.ok).toBe(false);
|
expect(missing.ok).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,8 @@ import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { createCasStore, getContentMerklePayload, getGlobalCasDir } from "@uncaged/workflow";
|
import { createCasStore, getContentMerklePayload, getGlobalCasDir } from "@uncaged/workflow";
|
||||||
import { cmdAdd } from "../src/cmd-add.js";
|
import { cmdFork, cmdRun } from "../src/commands/thread/index.js";
|
||||||
import { cmdFork } from "../src/cmd-fork.js";
|
import { cmdAdd } from "../src/commands/workflow/index.js";
|
||||||
import { cmdRun } from "../src/cmd-run.js";
|
|
||||||
import { pathExists } from "../src/fs-utils.js";
|
import { pathExists } from "../src/fs-utils.js";
|
||||||
import { addCliArgs } from "./bundle-fixture.js";
|
import { addCliArgs } from "./bundle-fixture.js";
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
getGlobalCasDir,
|
getGlobalCasDir,
|
||||||
putContentMerkleNode,
|
putContentMerkleNode,
|
||||||
} from "@uncaged/workflow";
|
} from "@uncaged/workflow";
|
||||||
import { cmdThreadRemove } from "../src/cmd-thread.js";
|
import { cmdThreadRemove } from "../src/commands/thread/index.js";
|
||||||
import { pathExists } from "../src/fs-utils.js";
|
import { pathExists } from "../src/fs-utils.js";
|
||||||
|
|
||||||
const cliEntryPath = fileURLToPath(new URL("../src/cli.ts", import.meta.url));
|
const cliEntryPath = fileURLToPath(new URL("../src/cli.ts", import.meta.url));
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
import { describe, expect, test } from "bun:test";
|
import { describe, expect, test } from "bun:test";
|
||||||
import { runCli } from "../src/cli-dispatch.js";
|
import { formatCliUsage, runCli } from "../src/cli-dispatch.js";
|
||||||
import { formatSkillDoc } from "../src/cmd-help.js";
|
import {
|
||||||
|
formatSkillDoc,
|
||||||
|
formatSkillIndex,
|
||||||
|
formatSkillTopic,
|
||||||
|
getSkillTopics,
|
||||||
|
} from "../src/skill.js";
|
||||||
|
|
||||||
const STORAGE_ROOT = "/tmp/help-test-storage";
|
const STORAGE_ROOT = "/tmp/help-test-storage";
|
||||||
|
|
||||||
@@ -10,13 +15,120 @@ describe("help command", () => {
|
|||||||
expect(code).toBe(0);
|
expect(code).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("help --skill returns 0", async () => {
|
test("no args prints usage (not red) and returns 1", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, []);
|
||||||
|
expect(code).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("skill command", () => {
|
||||||
|
test("skill (no topic) lists topics and returns 0", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, ["skill"]);
|
||||||
|
expect(code).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("skill cli returns 0", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, ["skill", "cli"]);
|
||||||
|
expect(code).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("skill develop returns 0", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, ["skill", "develop"]);
|
||||||
|
expect(code).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("skill author returns 0", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, ["skill", "author"]);
|
||||||
|
expect(code).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("skill unknown returns 1", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, ["skill", "unknown"]);
|
||||||
|
expect(code).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("--help flag on groups", () => {
|
||||||
|
test("workflow --help returns 0", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, ["workflow", "--help"]);
|
||||||
|
expect(code).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("thread --help returns 0", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, ["thread", "--help"]);
|
||||||
|
expect(code).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("cas --help returns 0", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, ["cas", "--help"]);
|
||||||
|
expect(code).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("init --help returns 0", async () => {
|
||||||
|
const code = await runCli(STORAGE_ROOT, ["init", "--help"]);
|
||||||
|
expect(code).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("legacy help --skill compat", () => {
|
||||||
|
test("help --skill still works (lists topics)", async () => {
|
||||||
const code = await runCli(STORAGE_ROOT, ["help", "--skill"]);
|
const code = await runCli(STORAGE_ROOT, ["help", "--skill"]);
|
||||||
expect(code).toBe(0);
|
expect(code).toBe(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("formatSkillDoc", () => {
|
describe("getSkillTopics", () => {
|
||||||
|
test("returns all topics", () => {
|
||||||
|
const topics = getSkillTopics();
|
||||||
|
const names = topics.map((t) => t.name);
|
||||||
|
expect(names).toContain("cli");
|
||||||
|
expect(names).toContain("develop");
|
||||||
|
expect(names).toContain("author");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("formatSkillIndex", () => {
|
||||||
|
test("lists all topics", () => {
|
||||||
|
const idx = formatSkillIndex();
|
||||||
|
expect(idx).toContain("# uncaged-workflow skill");
|
||||||
|
expect(idx).not.toContain("# uncaged-workflow help --skill");
|
||||||
|
expect(idx).toContain("cli");
|
||||||
|
expect(idx).toContain("develop");
|
||||||
|
expect(idx).toContain("author");
|
||||||
|
expect(idx).toContain("skill <topic>");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("formatCliUsage", () => {
|
||||||
|
test("has tagline, grouped sections, help hint, and env vars", () => {
|
||||||
|
const u = formatCliUsage();
|
||||||
|
expect(u.startsWith("uncaged-workflow — workflow engine CLI")).toBe(true);
|
||||||
|
expect(u).toContain("Workflow registry:");
|
||||||
|
expect(u).toContain("Thread execution:");
|
||||||
|
expect(u).toContain("Content-addressable storage:");
|
||||||
|
expect(u).toContain("Development:");
|
||||||
|
expect(u).toContain("Shortcuts:");
|
||||||
|
expect(u).toContain("Reference:");
|
||||||
|
expect(u).toContain("skill [topic]");
|
||||||
|
expect(u).toContain("Agent-consumable docs");
|
||||||
|
expect(u).toContain("Use <command> --help for subcommand details.");
|
||||||
|
expect(u).toContain("Environment variables:");
|
||||||
|
expect(u).toContain("WORKFLOW_STORAGE_ROOT");
|
||||||
|
expect(u).toContain("UNCAGED_WORKFLOW_STORAGE_ROOT");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("lists commands from registry with descriptions", () => {
|
||||||
|
const u = formatCliUsage();
|
||||||
|
expect(u).toContain("workflow add");
|
||||||
|
expect(u).toContain("Register a workflow bundle in the registry");
|
||||||
|
expect(u).toContain("thread run");
|
||||||
|
expect(u).toContain("Start a new thread executing a workflow");
|
||||||
|
expect(u).toContain("cas gc");
|
||||||
|
expect(u).toContain("Garbage-collect unreferenced CAS entries");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("formatSkillTopic('cli') — legacy formatSkillDoc", () => {
|
||||||
const doc = formatSkillDoc();
|
const doc = formatSkillDoc();
|
||||||
|
|
||||||
test("contains title", () => {
|
test("contains title", () => {
|
||||||
@@ -82,3 +194,52 @@ describe("formatSkillDoc", () => {
|
|||||||
expect(doc).toContain("## Typical Workflow");
|
expect(doc).toContain("## Typical Workflow");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("formatSkillTopic('develop')", () => {
|
||||||
|
const doc = formatSkillTopic("develop");
|
||||||
|
|
||||||
|
test("returns non-null", () => {
|
||||||
|
expect(doc).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("contains thread ID info", () => {
|
||||||
|
expect(doc).toContain("Thread ID");
|
||||||
|
expect(doc).toContain("Crockford Base32");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("contains CAS commands", () => {
|
||||||
|
expect(doc).toContain("cas put");
|
||||||
|
expect(doc).toContain("cas get");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("contains meta output section", () => {
|
||||||
|
expect(doc).toContain("Meta Output");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("formatSkillTopic('author')", () => {
|
||||||
|
const doc = formatSkillTopic("author");
|
||||||
|
|
||||||
|
test("returns non-null", () => {
|
||||||
|
expect(doc).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("contains bundle structure", () => {
|
||||||
|
expect(doc).toContain("Bundle Structure");
|
||||||
|
expect(doc).toContain(".esm.js");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("contains descriptor info", () => {
|
||||||
|
expect(doc).toContain("WorkflowDescriptor");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("contains role definition", () => {
|
||||||
|
expect(doc).toContain("Role Definition");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("formatSkillTopic unknown", () => {
|
||||||
|
test("returns null for unknown topic", () => {
|
||||||
|
expect(formatSkillTopic("nonexistent")).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { tmpdir } from "node:os";
|
|||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
import { runCli } from "../src/cli-dispatch.js";
|
import { runCli } from "../src/cli-dispatch.js";
|
||||||
import { cmdInitTemplate, cmdInitWorkspace } from "../src/cmd-init.js";
|
import { cmdInitTemplate, cmdInitWorkspace } from "../src/commands/init/index.js";
|
||||||
import { pathExists } from "../src/fs-utils.js";
|
import { pathExists } from "../src/fs-utils.js";
|
||||||
|
|
||||||
describe("init template", () => {
|
describe("init template", () => {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { tmpdir } from "node:os";
|
|||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
import { formatCliUsage, runCli } from "../src/cli-dispatch.js";
|
import { formatCliUsage, runCli } from "../src/cli-dispatch.js";
|
||||||
import { cmdInitWorkspace } from "../src/cmd-init.js";
|
import { cmdInitWorkspace } from "../src/commands/init/index.js";
|
||||||
import { pathExists } from "../src/fs-utils.js";
|
import { pathExists } from "../src/fs-utils.js";
|
||||||
|
|
||||||
describe("init workspace", () => {
|
describe("init workspace", () => {
|
||||||
@@ -129,8 +129,9 @@ describe("init workspace", () => {
|
|||||||
|
|
||||||
test("usage lists init subcommands", () => {
|
test("usage lists init subcommands", () => {
|
||||||
const u = formatCliUsage();
|
const u = formatCliUsage();
|
||||||
expect(u).toContain("uncaged-workflow init workspace <name>");
|
expect(u).toContain("init workspace <name>");
|
||||||
expect(u).toContain("uncaged-workflow init template <name>");
|
expect(u).toContain("init template <name>");
|
||||||
|
expect(u).toContain("Development:");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("runCli rejects unknown init subcommand", async () => {
|
test("runCli rejects unknown init subcommand", async () => {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
LIVE_CONTENT_MAX_LINES,
|
LIVE_CONTENT_MAX_LINES,
|
||||||
type LiveRoleRow,
|
type LiveRoleRow,
|
||||||
renderLiveRoleStepLines,
|
renderLiveRoleStepLines,
|
||||||
} from "../src/cmd-live.js";
|
} from "../src/commands/thread/index.js";
|
||||||
import { parseLiveArgv } from "../src/live-argv.js";
|
import { parseLiveArgv } from "../src/live-argv.js";
|
||||||
|
|
||||||
const cliEntryPath = fileURLToPath(new URL("../src/cli.ts", import.meta.url));
|
const cliEntryPath = fileURLToPath(new URL("../src/cli.ts", import.meta.url));
|
||||||
|
|||||||
@@ -5,15 +5,18 @@ import { tmpdir } from "node:os";
|
|||||||
import { dirname, join } from "node:path";
|
import { dirname, join } from "node:path";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
import { getGlobalCasDir } from "@uncaged/workflow";
|
import { getGlobalCasDir } from "@uncaged/workflow";
|
||||||
import { cmdAdd } from "../src/cmd-add.js";
|
import { cmdCasPut } from "../src/commands/cas/index.js";
|
||||||
import { cmdCasPut } from "../src/cmd-cas.js";
|
import {
|
||||||
import { cmdKill } from "../src/cmd-kill.js";
|
cmdKill,
|
||||||
import { cmdPause } from "../src/cmd-pause.js";
|
cmdPause,
|
||||||
import { cmdPs } from "../src/cmd-ps.js";
|
cmdPs,
|
||||||
import { cmdResume } from "../src/cmd-resume.js";
|
cmdResume,
|
||||||
import { cmdRun } from "../src/cmd-run.js";
|
cmdRun,
|
||||||
import { cmdThreadRemove, cmdThreadShow } from "../src/cmd-thread.js";
|
cmdThreadRemove,
|
||||||
import { cmdThreads } from "../src/cmd-threads.js";
|
cmdThreadShow,
|
||||||
|
cmdThreads,
|
||||||
|
} from "../src/commands/thread/index.js";
|
||||||
|
import { cmdAdd } from "../src/commands/workflow/index.js";
|
||||||
import { pathExists, readTextFileIfExists } from "../src/fs-utils.js";
|
import { pathExists, readTextFileIfExists } from "../src/fs-utils.js";
|
||||||
import { addCliArgs } from "./bundle-fixture.js";
|
import { addCliArgs } from "./bundle-fixture.js";
|
||||||
|
|
||||||
@@ -233,7 +236,7 @@ describe("cli thread commands", () => {
|
|||||||
const runningPath = join(dirname(dataPath), `${threadId}.running`);
|
const runningPath = join(dirname(dataPath), `${threadId}.running`);
|
||||||
await waitUntilRunningFileAbsent(runningPath, 120);
|
await waitUntilRunningFileAbsent(runningPath, 120);
|
||||||
|
|
||||||
const put = await cmdCasPut(storageRoot, threadId, "keep-after-thread-rm");
|
const put = await cmdCasPut(storageRoot, "keep-after-thread-rm");
|
||||||
expect(put.ok).toBe(true);
|
expect(put.ok).toBe(true);
|
||||||
if (!put.ok) {
|
if (!put.ok) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
"yaml": "^2.8.4"
|
"yaml": "^2.8.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "bun test"
|
"test": "bun test"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,9 @@
|
|||||||
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
import { err, ok, type Result } from "@uncaged/workflow";
|
import { err, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
async function pathExists(path: string): Promise<boolean> {
|
import { pathExists } from "./fs-utils.js";
|
||||||
try {
|
|
||||||
await stat(path);
|
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export type BundleFileSource = { kind: "text"; text: string } | { kind: "path"; path: string };
|
export type BundleFileSource = { kind: "text"; text: string } | { kind: "path"; path: string };
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
export function shouldUseColor(): boolean {
|
||||||
|
return process.stdout.isTTY === true && process.env.NO_COLOR === undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function highlightLiveRole(name: string): string {
|
||||||
|
if (!shouldUseColor()) {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
return `\x1b[1m\x1b[36m${name}\x1b[0m`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function dimGreyLine(line: string): string {
|
||||||
|
if (!shouldUseColor()) {
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
return `\x1b[2m\x1b[90m${line}\x1b[0m`;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
export type DispatchFn = (storageRoot: string, argv: string[]) => Promise<number>;
|
||||||
|
|
||||||
|
export type CommandEntry = {
|
||||||
|
handler: DispatchFn;
|
||||||
|
args: string;
|
||||||
|
description: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CommandGroup = {
|
||||||
|
name: string;
|
||||||
|
commands: ReadonlyArray<{ name: string; args: string; description: string }>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type DispatchGroupFn = (
|
||||||
|
tableName: string,
|
||||||
|
table: Record<string, CommandEntry>,
|
||||||
|
storageRoot: string,
|
||||||
|
argv: string[],
|
||||||
|
) => Promise<number> | null;
|
||||||
@@ -1,538 +1,111 @@
|
|||||||
|
import type { CommandEntry, DispatchFn } from "./cli-command-types.js";
|
||||||
import { printCliError, printCliLine, printCliWarn } from "./cli-output.js";
|
import { printCliError, printCliLine, printCliWarn } from "./cli-output.js";
|
||||||
import { cmdAdd, formatAddSuccess, parseAddArgv } from "./cmd-add.js";
|
import { getCommandRegistry } from "./cli-registry.js";
|
||||||
import { cmdCasGet, cmdCasList, cmdCasPut, cmdCasRm } from "./cmd-cas.js";
|
import { formatCliUsage as formatCliUsageWithGroups } from "./cli-usage.js";
|
||||||
import { cmdFork, parseForkArgv } from "./cmd-fork.js";
|
import { createCasDispatcher, dispatchGc } from "./commands/cas/index.js";
|
||||||
import { cmdGc } from "./cmd-gc.js";
|
import { createInitDispatcher } from "./commands/init/index.js";
|
||||||
import { formatSkillDoc } from "./cmd-help.js";
|
import {
|
||||||
import { cmdHistory } from "./cmd-history.js";
|
createThreadDispatcher,
|
||||||
import { cmdInitTemplate, cmdInitWorkspace } from "./cmd-init.js";
|
dispatchFork,
|
||||||
import { cmdKill } from "./cmd-kill.js";
|
dispatchKill,
|
||||||
import { cmdList, formatListLines } from "./cmd-list.js";
|
dispatchLive,
|
||||||
import { cmdLive } from "./cmd-live.js";
|
dispatchPause,
|
||||||
import { cmdPause } from "./cmd-pause.js";
|
dispatchPs,
|
||||||
import { cmdPs } from "./cmd-ps.js";
|
dispatchResume,
|
||||||
import { cmdRemove } from "./cmd-remove.js";
|
dispatchRun,
|
||||||
import { cmdResume } from "./cmd-resume.js";
|
dispatchThreadList,
|
||||||
import { cmdRollback } from "./cmd-rollback.js";
|
} from "./commands/thread/index.js";
|
||||||
import { cmdRun } from "./cmd-run.js";
|
import {
|
||||||
import { cmdShow, formatShowYaml } from "./cmd-show.js";
|
createWorkflowDispatcher,
|
||||||
import { cmdThreadRemove, cmdThreadShow } from "./cmd-thread.js";
|
dispatchAdd,
|
||||||
import { cmdThreads } from "./cmd-threads.js";
|
dispatchHistory,
|
||||||
import { parseLiveArgv } from "./live-argv.js";
|
dispatchList,
|
||||||
import { parseRunArgv } from "./run-argv.js";
|
dispatchRemove,
|
||||||
|
dispatchRollback,
|
||||||
|
dispatchShow,
|
||||||
|
} from "./commands/workflow/index.js";
|
||||||
|
import { formatSkillIndex, formatSkillTopic, getSkillTopics } from "./skill.js";
|
||||||
|
|
||||||
export function formatCliUsage(): string {
|
export type { CommandEntry, CommandGroup, DispatchFn } from "./cli-command-types.js";
|
||||||
return [
|
export { getCommandRegistry } from "./cli-registry.js";
|
||||||
"Usage:",
|
|
||||||
" uncaged-workflow workflow add <name> <file.esm.js> [--types <path>]",
|
function dispatchGroup(
|
||||||
" uncaged-workflow workflow list",
|
tableName: string,
|
||||||
" uncaged-workflow workflow show <name>",
|
table: Record<string, CommandEntry>,
|
||||||
" uncaged-workflow workflow rm <name>",
|
storageRoot: string,
|
||||||
" uncaged-workflow workflow history <name>",
|
argv: string[],
|
||||||
" uncaged-workflow workflow rollback <name> [hash]",
|
): Promise<number> | null {
|
||||||
"",
|
const sub = argv[0];
|
||||||
" uncaged-workflow thread run <name> [--prompt <text>] [--max-rounds N]",
|
if (sub === undefined || sub === "--help" || sub === "-h") {
|
||||||
" uncaged-workflow thread list [name]",
|
const entries = Object.entries(table);
|
||||||
" uncaged-workflow thread show <id>",
|
const lines = [`${tableName} subcommands:\n`];
|
||||||
" uncaged-workflow thread rm <id>",
|
for (const [name, e] of entries) {
|
||||||
" uncaged-workflow thread ps",
|
const args = e.args ? ` ${e.args}` : "";
|
||||||
" uncaged-workflow thread kill <thread-id>",
|
lines.push(` uncaged-workflow ${tableName} ${name}${args}`);
|
||||||
" uncaged-workflow thread live <thread-id> [--debug] [--role <name>]",
|
lines.push(` ${e.description}\n`);
|
||||||
" uncaged-workflow thread live --latest [--debug] [--role <name>]",
|
}
|
||||||
" uncaged-workflow thread pause <thread-id>",
|
printCliLine(lines.join("\n"));
|
||||||
" uncaged-workflow thread resume <thread-id>",
|
return Promise.resolve(sub === undefined ? 1 : 0);
|
||||||
" uncaged-workflow thread fork <thread-id> [--from-role <role>]",
|
}
|
||||||
"",
|
const entry = table[sub];
|
||||||
" uncaged-workflow cas get <thread-id> <hash>",
|
if (entry === undefined) {
|
||||||
" uncaged-workflow cas put <thread-id> <content>",
|
return null;
|
||||||
" uncaged-workflow cas list <thread-id>",
|
}
|
||||||
" uncaged-workflow cas rm <thread-id> <hash>",
|
return entry.handler(storageRoot, argv.slice(1));
|
||||||
" uncaged-workflow cas gc",
|
|
||||||
"",
|
|
||||||
" uncaged-workflow init workspace <name>",
|
|
||||||
" uncaged-workflow init template <name>",
|
|
||||||
"",
|
|
||||||
" uncaged-workflow run <name> [...] (shortcut for thread run)",
|
|
||||||
" uncaged-workflow live <thread-id> [...] (shortcut for thread live)",
|
|
||||||
"",
|
|
||||||
"Environment variables:",
|
|
||||||
" WORKFLOW_STORAGE_ROOT Override storage directory (default: ~/.uncaged/workflow)",
|
|
||||||
" UNCAGED_WORKFLOW_STORAGE_ROOT Internal override (takes priority over WORKFLOW_STORAGE_ROOT)",
|
|
||||||
].join("\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function printDeprecation(oldCmd: string, newCmd: string): void {
|
function printDeprecation(oldCmd: string, newCmd: string): void {
|
||||||
printCliWarn(`⚠ "${oldCmd}" is deprecated, use "${newCmd}" instead`);
|
printCliWarn(`⚠ "${oldCmd}" is deprecated, use "${newCmd}" instead`);
|
||||||
}
|
}
|
||||||
|
|
||||||
type DispatchFn = (storageRoot: string, argv: string[]) => Promise<number>;
|
export function formatCliUsage(): string {
|
||||||
|
return formatCliUsageWithGroups(getCommandRegistry(), getSkillTopics());
|
||||||
|
}
|
||||||
|
|
||||||
// ── Individual dispatch functions ──────────────────────────────────────
|
const dispatchWorkflow = createWorkflowDispatcher({ dispatchGroup, printDeprecation });
|
||||||
|
const dispatchThread = createThreadDispatcher({ dispatchGroup });
|
||||||
|
const dispatchCas = createCasDispatcher({ dispatchGroup });
|
||||||
|
const dispatchInit = createInitDispatcher({ dispatchGroup });
|
||||||
|
|
||||||
async function dispatchInit(_storageRoot: string, argv: string[]): Promise<number> {
|
async function showSkillDocOrIndex(topic: string | undefined): Promise<number> {
|
||||||
const sub = argv[0];
|
if (topic === undefined) {
|
||||||
const name = argv[1];
|
printCliLine(formatSkillIndex());
|
||||||
if (sub === undefined || name === undefined || argv.length > 2) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: init requires workspace|template <name>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sub === "workspace") {
|
|
||||||
const result = await cmdInitWorkspace(process.cwd(), name);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(`initialized workflow workspace at ${result.value.rootPath}`);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
const doc = formatSkillTopic(topic);
|
||||||
if (sub === "template") {
|
if (doc === null) {
|
||||||
const result = await cmdInitTemplate(process.cwd(), name);
|
printCliError(`unknown skill topic: ${topic}\n\n${formatSkillIndex()}`);
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(`initialized template at ${result.value.templatePath}`);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: unknown init subcommand: ${sub}`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchAdd(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const parsed = parseAddArgv(argv);
|
|
||||||
if (!parsed.ok) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: ${parsed.error}`);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
const result = await cmdAdd(storageRoot, parsed.value);
|
printCliLine(doc);
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
for (const w of result.value.warnings) {
|
|
||||||
printCliWarn(w);
|
|
||||||
}
|
|
||||||
printCliLine(formatAddSuccess(parsed.value.name, parsed.value.filePath, result.value.hash));
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function dispatchList(storageRoot: string, argv: string[]): Promise<number> {
|
async function dispatchSkill(_storageRoot: string, argv: string[]): Promise<number> {
|
||||||
if (argv.length > 0) {
|
return showSkillDocOrIndex(argv[0]);
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: list takes no arguments`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdList(storageRoot);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
for (const line of formatListLines(result.value)) {
|
|
||||||
printCliLine(line);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function dispatchShow(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const name = argv[0];
|
|
||||||
if (name === undefined || argv.length > 1) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: show requires <name>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdShow(storageRoot, name);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(formatShowYaml(name, result.value));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchRemove(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const name = argv[0];
|
|
||||||
if (name === undefined || argv.length > 1) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: remove requires <name>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdRemove(storageRoot, name);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(`removed workflow "${name}" from registry`);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchRun(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const parsed = parseRunArgv(argv);
|
|
||||||
if (!parsed.ok) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: ${parsed.error}`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await cmdRun(
|
|
||||||
storageRoot,
|
|
||||||
parsed.value.name,
|
|
||||||
parsed.value.prompt,
|
|
||||||
parsed.value.maxRounds,
|
|
||||||
);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printCliLine(result.value.threadId);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchPs(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
if (argv.length > 0) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: ps takes no arguments`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
for (const line of await cmdPs(storageRoot)) {
|
|
||||||
printCliLine(line);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchKill(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const threadId = argv[0];
|
|
||||||
if (threadId === undefined || argv.length > 1) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: kill requires <thread-id>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdKill(storageRoot, threadId);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(`kill sent for thread ${threadId}`);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchLive(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const parsed = parseLiveArgv(argv);
|
|
||||||
if (!parsed.ok) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: ${parsed.error}`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return cmdLive(storageRoot, parsed.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchHistory(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const name = argv[0];
|
|
||||||
if (name === undefined || argv.length > 1) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: history requires <name>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdHistory(storageRoot, name);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
for (const line of result.value) {
|
|
||||||
printCliLine(line);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchRollback(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const name = argv[0];
|
|
||||||
if (name === undefined || argv.length > 2) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: rollback requires <name> [hash]`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const hashArg = argv[1];
|
|
||||||
const result = await cmdRollback(storageRoot, name, hashArg === undefined ? null : hashArg);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(`rolled back workflow "${name}"`);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchPause(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const threadId = argv[0];
|
|
||||||
if (threadId === undefined || argv.length > 1) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: pause requires <thread-id>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdPause(storageRoot, threadId);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(`pause sent for thread ${threadId}`);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchResume(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const threadId = argv[0];
|
|
||||||
if (threadId === undefined || argv.length > 1) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: resume requires <thread-id>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdResume(storageRoot, threadId);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(`resume sent for thread ${threadId}`);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchThreadList(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const result = await cmdThreads(storageRoot, argv);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
for (const line of result.value) {
|
|
||||||
printCliLine(line);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchThreadShow(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const id = argv[0];
|
|
||||||
if (id === undefined || argv.length > 1) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: thread show requires <id>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdThreadShow(storageRoot, id);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(result.value);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchThreadRm(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const id = argv[0];
|
|
||||||
if (id === undefined || argv.length > 1) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: thread rm requires <id>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdThreadRemove(storageRoot, id);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(`removed thread ${id}`);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchGc(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
if (argv.length > 0) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: gc takes no arguments`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdGc(storageRoot);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const stats = result.value;
|
|
||||||
printCliLine(
|
|
||||||
`scanned ${stats.scannedThreads} threads, ${stats.activeRefs} active refs, deleted ${stats.deletedEntries} entries`,
|
|
||||||
);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchFork(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const parsed = parseForkArgv(argv);
|
|
||||||
if (!parsed.ok) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: ${parsed.error}`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdFork(storageRoot, parsed.value.threadId, parsed.value.fromRole);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(result.value.threadId);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── CAS subcommand table ───────────────────────────────────────────────
|
|
||||||
|
|
||||||
async function dispatchCasGet(storageRoot: string, rest: string[]): Promise<number> {
|
|
||||||
const threadId = rest[0];
|
|
||||||
const hash = rest[1];
|
|
||||||
if (threadId === undefined || hash === undefined || rest.length > 2) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: cas get requires <thread-id> <hash>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdCasGet(storageRoot, threadId, hash);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(result.value);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchCasPut(storageRoot: string, rest: string[]): Promise<number> {
|
|
||||||
const threadId = rest[0];
|
|
||||||
const content = rest[1];
|
|
||||||
if (threadId === undefined || content === undefined || rest.length > 2) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: cas put requires <thread-id> <content>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdCasPut(storageRoot, threadId, content);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(result.value);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchCasList(storageRoot: string, rest: string[]): Promise<number> {
|
|
||||||
const threadId = rest[0];
|
|
||||||
if (threadId === undefined || rest.length > 1) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: cas list requires <thread-id>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdCasList(storageRoot, threadId);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
for (const hash of result.value) {
|
|
||||||
printCliLine(hash);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dispatchCasRm(storageRoot: string, rest: string[]): Promise<number> {
|
|
||||||
const threadId = rest[0];
|
|
||||||
const hash = rest[1];
|
|
||||||
if (threadId === undefined || hash === undefined || rest.length > 2) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: cas rm requires <thread-id> <hash>`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const result = await cmdCasRm(storageRoot, threadId, hash);
|
|
||||||
if (!result.ok) {
|
|
||||||
printCliError(result.error);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printCliLine(`removed cas entry ${hash}`);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const CAS_SUBCOMMAND_TABLE: Record<string, DispatchFn> = {
|
|
||||||
get: dispatchCasGet,
|
|
||||||
put: dispatchCasPut,
|
|
||||||
list: dispatchCasList,
|
|
||||||
rm: dispatchCasRm,
|
|
||||||
gc: dispatchGc,
|
|
||||||
};
|
|
||||||
|
|
||||||
async function dispatchCas(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const sub = argv[0];
|
|
||||||
if (sub === undefined) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: unknown cas subcommand: (none)`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const handler = CAS_SUBCOMMAND_TABLE[sub];
|
|
||||||
if (handler === undefined) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: unknown cas subcommand: ${sub}`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return handler(storageRoot, argv.slice(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Workflow subcommand table (Phase 1) ────────────────────────────────
|
|
||||||
|
|
||||||
const WORKFLOW_SUBCOMMAND_TABLE: Record<string, DispatchFn> = {
|
|
||||||
add: dispatchAdd,
|
|
||||||
list: dispatchList,
|
|
||||||
show: dispatchShow,
|
|
||||||
rm: dispatchRemove,
|
|
||||||
history: dispatchHistory,
|
|
||||||
rollback: dispatchRollback,
|
|
||||||
};
|
|
||||||
|
|
||||||
async function dispatchWorkflow(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const sub = argv[0];
|
|
||||||
if (sub === undefined) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: unknown workflow subcommand: (none)`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const handler = WORKFLOW_SUBCOMMAND_TABLE[sub];
|
|
||||||
if (handler !== undefined) {
|
|
||||||
return handler(storageRoot, argv.slice(1));
|
|
||||||
}
|
|
||||||
if (sub === "remove") {
|
|
||||||
printDeprecation("workflow remove", "workflow rm");
|
|
||||||
return dispatchRemove(storageRoot, argv.slice(1));
|
|
||||||
}
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: unknown workflow subcommand: ${sub}`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Thread subcommand table (Phase 2) ──────────────────────────────────
|
|
||||||
|
|
||||||
const THREAD_SUBCOMMAND_TABLE: Record<string, DispatchFn> = {
|
|
||||||
run: dispatchRun,
|
|
||||||
list: dispatchThreadList,
|
|
||||||
show: dispatchThreadShow,
|
|
||||||
rm: dispatchThreadRm,
|
|
||||||
fork: dispatchFork,
|
|
||||||
ps: dispatchPs,
|
|
||||||
kill: dispatchKill,
|
|
||||||
live: dispatchLive,
|
|
||||||
pause: dispatchPause,
|
|
||||||
resume: dispatchResume,
|
|
||||||
};
|
|
||||||
|
|
||||||
async function dispatchThread(storageRoot: string, argv: string[]): Promise<number> {
|
|
||||||
const sub = argv[0];
|
|
||||||
if (sub === undefined) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: unknown thread subcommand: (none)`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const handler = THREAD_SUBCOMMAND_TABLE[sub];
|
|
||||||
if (handler === undefined) {
|
|
||||||
printCliError(`${formatCliUsage()}\n\nerror: unknown thread subcommand: ${sub}`);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return handler(storageRoot, argv.slice(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Help ────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
async function dispatchHelp(_storageRoot: string, argv: string[]): Promise<number> {
|
async function dispatchHelp(_storageRoot: string, argv: string[]): Promise<number> {
|
||||||
if (argv.includes("--skill")) {
|
printCliWarn('⚠ "help" is deprecated, use "skill" instead');
|
||||||
printCliLine(formatSkillDoc());
|
const skillIdx = argv.indexOf("--skill");
|
||||||
} else {
|
if (skillIdx !== -1) {
|
||||||
printCliLine(formatCliUsage());
|
return showSkillDocOrIndex(argv[skillIdx + 1]);
|
||||||
}
|
}
|
||||||
|
printCliLine(formatCliUsage());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Top-level command table (Phase 3) ──────────────────────────────────
|
|
||||||
|
|
||||||
const COMMAND_TABLE: Record<string, DispatchFn> = {
|
const COMMAND_TABLE: Record<string, DispatchFn> = {
|
||||||
// Grouped commands (primary)
|
|
||||||
workflow: dispatchWorkflow,
|
workflow: dispatchWorkflow,
|
||||||
thread: dispatchThread,
|
thread: dispatchThread,
|
||||||
cas: dispatchCas,
|
cas: dispatchCas,
|
||||||
init: dispatchInit,
|
init: dispatchInit,
|
||||||
help: dispatchHelp,
|
help: dispatchHelp,
|
||||||
|
skill: dispatchSkill,
|
||||||
// Top-level shortcuts (no deprecation)
|
|
||||||
run: dispatchRun,
|
run: dispatchRun,
|
||||||
live: dispatchLive,
|
live: dispatchLive,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Deprecated flat commands that delegate to grouped commands
|
|
||||||
const DEPRECATED_ALIASES: Record<string, { newCmd: string; handler: DispatchFn }> = {
|
const DEPRECATED_ALIASES: Record<string, { newCmd: string; handler: DispatchFn }> = {
|
||||||
add: { newCmd: "workflow add", handler: dispatchAdd },
|
add: { newCmd: "workflow add", handler: dispatchAdd },
|
||||||
list: { newCmd: "workflow list", handler: dispatchList },
|
list: { newCmd: "workflow list", handler: dispatchList },
|
||||||
@@ -551,12 +124,12 @@ const DEPRECATED_ALIASES: Record<string, { newCmd: string; handler: DispatchFn }
|
|||||||
|
|
||||||
export async function runCli(storageRoot: string, argv: string[]): Promise<number> {
|
export async function runCli(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
if (argv.length === 0) {
|
if (argv.length === 0) {
|
||||||
printCliError(formatCliUsage());
|
printCliLine(formatCliUsage());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
const command = argv[0];
|
const command = argv[0];
|
||||||
if (command === undefined) {
|
if (command === undefined) {
|
||||||
printCliError(formatCliUsage());
|
printCliLine(formatCliUsage());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
const rest = argv.slice(1);
|
const rest = argv.slice(1);
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import type { CommandGroup } from "./cli-command-types.js";
|
||||||
|
import { setCommandGroupsForUsage } from "./cli-usage-context.js";
|
||||||
|
import { CAS_SUBCOMMAND_TABLE } from "./commands/cas/index.js";
|
||||||
|
import { INIT_SUBCOMMAND_TABLE } from "./commands/init/index.js";
|
||||||
|
import { THREAD_SUBCOMMAND_TABLE } from "./commands/thread/index.js";
|
||||||
|
import { WORKFLOW_SUBCOMMAND_TABLE } from "./commands/workflow/index.js";
|
||||||
|
|
||||||
|
export function getCommandRegistry(): ReadonlyArray<CommandGroup> {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
name: "workflow",
|
||||||
|
commands: Object.entries(WORKFLOW_SUBCOMMAND_TABLE).map(([name, e]) => ({
|
||||||
|
name,
|
||||||
|
args: e.args,
|
||||||
|
description: e.description,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "thread",
|
||||||
|
commands: Object.entries(THREAD_SUBCOMMAND_TABLE).map(([name, e]) => ({
|
||||||
|
name,
|
||||||
|
args: e.args,
|
||||||
|
description: e.description,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "cas",
|
||||||
|
commands: Object.entries(CAS_SUBCOMMAND_TABLE).map(([name, e]) => ({
|
||||||
|
name,
|
||||||
|
args: e.args,
|
||||||
|
description: e.description,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "init",
|
||||||
|
commands: Object.entries(INIT_SUBCOMMAND_TABLE).map(([name, e]) => ({
|
||||||
|
name,
|
||||||
|
args: e.args,
|
||||||
|
description: e.description,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
setCommandGroupsForUsage(getCommandRegistry());
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import type { CommandGroup } from "./cli-command-types.js";
|
||||||
|
|
||||||
|
let commandGroupsForUsage: ReadonlyArray<CommandGroup> | null = null;
|
||||||
|
|
||||||
|
export function setCommandGroupsForUsage(groups: ReadonlyArray<CommandGroup>): void {
|
||||||
|
commandGroupsForUsage = groups;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getCommandGroupsForUsage(): ReadonlyArray<CommandGroup> {
|
||||||
|
if (commandGroupsForUsage === null) {
|
||||||
|
throw new Error("BUG: command groups for usage not initialized");
|
||||||
|
}
|
||||||
|
return commandGroupsForUsage;
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
import type { CommandGroup } from "./cli-command-types.js";
|
||||||
|
|
||||||
|
/** Keep aligned with `getSkillTopics()` in skill.ts (names only, for error usage lines). */
|
||||||
|
export const USAGE_SKILL_TOPIC_ROWS: ReadonlyArray<{ name: string }> = [
|
||||||
|
{ name: "cli" },
|
||||||
|
{ name: "develop" },
|
||||||
|
{ name: "author" },
|
||||||
|
];
|
||||||
|
|
||||||
|
const USAGE_SECTION_BY_GROUP: Record<string, string> = {
|
||||||
|
workflow: "Workflow registry:",
|
||||||
|
thread: "Thread execution:",
|
||||||
|
cas: "Content-addressable storage:",
|
||||||
|
init: "Development:",
|
||||||
|
};
|
||||||
|
|
||||||
|
export function formatUsageCommandLines(
|
||||||
|
rows: ReadonlyArray<{ prefix: string; description: string }>,
|
||||||
|
): string[] {
|
||||||
|
const maxPrefix = rows.reduce((max, row) => Math.max(max, row.prefix.length), 0);
|
||||||
|
const gap = 2;
|
||||||
|
return rows.map((row) => {
|
||||||
|
const pad = " ".repeat(maxPrefix - row.prefix.length + gap);
|
||||||
|
return ` ${row.prefix}${pad}${row.description}`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatCliUsage(
|
||||||
|
groups: ReadonlyArray<CommandGroup>,
|
||||||
|
skillTopics: ReadonlyArray<{ name: string }>,
|
||||||
|
): string {
|
||||||
|
const lines: string[] = ["uncaged-workflow — workflow engine CLI", ""];
|
||||||
|
|
||||||
|
for (const group of groups) {
|
||||||
|
const sectionTitle = USAGE_SECTION_BY_GROUP[group.name];
|
||||||
|
if (sectionTitle === undefined) {
|
||||||
|
throw new Error(`BUG: missing usage section title for group "${group.name}"`);
|
||||||
|
}
|
||||||
|
lines.push(sectionTitle);
|
||||||
|
const rows = group.commands.map((cmd) => {
|
||||||
|
const args = cmd.args ? ` ${cmd.args}` : "";
|
||||||
|
return {
|
||||||
|
prefix: `${group.name} ${cmd.name}${args}`,
|
||||||
|
description: cmd.description,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
lines.push(...formatUsageCommandLines(rows));
|
||||||
|
lines.push("");
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push("Shortcuts:");
|
||||||
|
lines.push(
|
||||||
|
...formatUsageCommandLines([
|
||||||
|
{ prefix: "run <name> [...]", description: "→ thread run" },
|
||||||
|
{ prefix: "live <id> [...]", description: "→ thread live" },
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
lines.push("");
|
||||||
|
|
||||||
|
lines.push("Reference:");
|
||||||
|
const skillTopicNames = skillTopics.map((t) => t.name).join(", ");
|
||||||
|
lines.push(
|
||||||
|
...formatUsageCommandLines([
|
||||||
|
{
|
||||||
|
prefix: "skill [topic]",
|
||||||
|
description: `Agent-consumable docs (${skillTopicNames})`,
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
lines.push("");
|
||||||
|
lines.push("Use <command> --help for subcommand details.");
|
||||||
|
lines.push("");
|
||||||
|
lines.push("Environment variables:");
|
||||||
|
lines.push(
|
||||||
|
" WORKFLOW_STORAGE_ROOT Override storage directory (default: ~/.uncaged/workflow)",
|
||||||
|
);
|
||||||
|
lines.push(
|
||||||
|
" UNCAGED_WORKFLOW_STORAGE_ROOT Internal override (takes priority over WORKFLOW_STORAGE_ROOT)",
|
||||||
|
);
|
||||||
|
return lines.join("\n");
|
||||||
|
}
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
import { createCasStore, err, getGlobalCasDir, ok, type Result } from "@uncaged/workflow";
|
|
||||||
|
|
||||||
export async function cmdCasGet(
|
|
||||||
storageRoot: string,
|
|
||||||
_threadId: string,
|
|
||||||
hash: string,
|
|
||||||
): Promise<Result<string, string>> {
|
|
||||||
const cas = createCasStore(getGlobalCasDir(storageRoot));
|
|
||||||
const content = await cas.get(hash);
|
|
||||||
if (content === null) {
|
|
||||||
return err(`cas entry not found: ${hash}`);
|
|
||||||
}
|
|
||||||
return ok(content);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function cmdCasPut(
|
|
||||||
storageRoot: string,
|
|
||||||
_threadId: string,
|
|
||||||
content: string,
|
|
||||||
): Promise<Result<string, string>> {
|
|
||||||
const cas = createCasStore(getGlobalCasDir(storageRoot));
|
|
||||||
const hash = await cas.put(content);
|
|
||||||
return ok(hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function cmdCasList(
|
|
||||||
storageRoot: string,
|
|
||||||
_threadId: string,
|
|
||||||
): Promise<Result<string[], string>> {
|
|
||||||
const cas = createCasStore(getGlobalCasDir(storageRoot));
|
|
||||||
const hashes = await cas.list();
|
|
||||||
return ok(hashes);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function cmdCasRm(
|
|
||||||
storageRoot: string,
|
|
||||||
_threadId: string,
|
|
||||||
hash: string,
|
|
||||||
): Promise<Result<void, string>> {
|
|
||||||
const cas = createCasStore(getGlobalCasDir(storageRoot));
|
|
||||||
await cas.delete(hash);
|
|
||||||
return ok(undefined);
|
|
||||||
}
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
export function formatSkillDoc(): string {
|
|
||||||
return `# uncaged-workflow CLI Reference
|
|
||||||
|
|
||||||
## Core Concepts
|
|
||||||
|
|
||||||
| Concept | Description |
|
|
||||||
|---------|-------------|
|
|
||||||
| **Workflow** | A single-file ESM bundle (\`.esm.js\`) that exports \`run\` and \`descriptor\`. Identified by name and XXH64 hash. |
|
|
||||||
| **Bundle** | The physical \`.esm.js\` file stored in the bundles directory. Immutable once written. |
|
|
||||||
| **Thread** | A single execution of a workflow, identified by a ULID. Persists state as JSONL files. |
|
|
||||||
| **CAS** | Content-Addressable Storage. Per-thread key-value store keyed by content hash. |
|
|
||||||
| **Registry** | \`workflow.yaml\` — maps workflow names to their current and historical bundle hashes. |
|
|
||||||
|
|
||||||
## Commands
|
|
||||||
|
|
||||||
### workflow
|
|
||||||
|
|
||||||
| Command | Args | Description |
|
|
||||||
|---------|------|-------------|
|
|
||||||
| \`workflow add\` | \`<name> <file.esm.js> [--types <path>]\` | Register a workflow bundle in the registry |
|
|
||||||
| \`workflow list\` | (none) | List all registered workflows |
|
|
||||||
| \`workflow show\` | \`<name>\` | Show details of a registered workflow |
|
|
||||||
| \`workflow rm\` | \`<name>\` | Remove a workflow from the registry |
|
|
||||||
| \`workflow history\` | \`<name>\` | Show version history of a workflow |
|
|
||||||
| \`workflow rollback\` | \`<name> [hash]\` | Rollback a workflow to a previous version |
|
|
||||||
|
|
||||||
### thread
|
|
||||||
|
|
||||||
| Command | Args | Description |
|
|
||||||
|---------|------|-------------|
|
|
||||||
| \`thread run\` | \`<name> [--prompt <text>] [--max-rounds N]\` | Start a new thread executing a workflow |
|
|
||||||
| \`thread list\` | \`[name]\` | List threads, optionally filtered by workflow name |
|
|
||||||
| \`thread show\` | \`<id>\` | Show thread details and state |
|
|
||||||
| \`thread rm\` | \`<id>\` | Remove a thread |
|
|
||||||
| \`thread fork\` | \`<thread-id> [--from-role <role>]\` | Fork a thread, optionally from a specific role |
|
|
||||||
| \`thread ps\` | (none) | List running threads |
|
|
||||||
| \`thread kill\` | \`<thread-id>\` | Kill a running thread |
|
|
||||||
| \`thread live\` | \`<thread-id> [--debug] [--role <name>]\` or \`--latest [--debug] [--role <name>]\` | Attach to a thread and stream output live |
|
|
||||||
| \`thread pause\` | \`<thread-id>\` | Pause a running thread |
|
|
||||||
| \`thread resume\` | \`<thread-id>\` | Resume a paused thread |
|
|
||||||
|
|
||||||
### cas
|
|
||||||
|
|
||||||
| Command | Args | Description |
|
|
||||||
|---------|------|-------------|
|
|
||||||
| \`cas get\` | \`<thread-id> <hash>\` | Retrieve content by hash from a thread's CAS |
|
|
||||||
| \`cas put\` | \`<thread-id> <content>\` | Store content in a thread's CAS, returns hash |
|
|
||||||
| \`cas list\` | \`<thread-id>\` | List all CAS entries for a thread |
|
|
||||||
| \`cas rm\` | \`<thread-id> <hash>\` | Remove a CAS entry |
|
|
||||||
| \`cas gc\` | (none) | Garbage-collect unreferenced CAS entries |
|
|
||||||
|
|
||||||
### init
|
|
||||||
|
|
||||||
| Command | Args | Description |
|
|
||||||
|---------|------|-------------|
|
|
||||||
| \`init workspace\` | \`<name>\` | Initialize a new workflow workspace |
|
|
||||||
| \`init template\` | \`<name>\` | Initialize a new workflow template |
|
|
||||||
|
|
||||||
### Top-level shortcuts
|
|
||||||
|
|
||||||
| Command | Equivalent | Description |
|
|
||||||
|---------|------------|-------------|
|
|
||||||
| \`run\` | \`thread run\` | Shortcut to start a thread |
|
|
||||||
| \`live\` | \`thread live\` | Shortcut to attach to a thread |
|
|
||||||
|
|
||||||
## Typical Workflow
|
|
||||||
|
|
||||||
1. \`uncaged-workflow workflow add my-wf ./my-wf.esm.js\` — register a workflow
|
|
||||||
2. \`uncaged-workflow run my-wf --prompt "do the thing"\` — start a thread
|
|
||||||
3. \`uncaged-workflow live --latest\` — attach and watch output
|
|
||||||
4. \`uncaged-workflow thread show <thread-id>\` — inspect completed thread
|
|
||||||
|
|
||||||
## Exit Codes
|
|
||||||
|
|
||||||
| Code | Meaning |
|
|
||||||
|------|---------|
|
|
||||||
| 0 | Success |
|
|
||||||
| 1 | Error |
|
|
||||||
|
|
||||||
## Environment Variables
|
|
||||||
|
|
||||||
| Variable | Description |
|
|
||||||
|----------|-------------|
|
|
||||||
| \`UNCAGED_WORKFLOW_STORAGE_ROOT\` | Override the default storage directory for all workflow data |
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
import { join } from "node:path";
|
|
||||||
|
|
||||||
import { err, type Result } from "@uncaged/workflow";
|
|
||||||
|
|
||||||
import { readTextFileIfExists } from "./fs-utils.js";
|
|
||||||
import {
|
|
||||||
resolveRunningHashForThread,
|
|
||||||
sendWorkerTcpCommand,
|
|
||||||
type WorkerCtl,
|
|
||||||
} from "./worker-spawn.js";
|
|
||||||
|
|
||||||
export async function cmdKill(
|
|
||||||
storageRoot: string,
|
|
||||||
threadId: string,
|
|
||||||
): Promise<Result<void, string>> {
|
|
||||||
const hashResult = await resolveRunningHashForThread(storageRoot, threadId);
|
|
||||||
if (!hashResult.ok) {
|
|
||||||
return hashResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ctlPath = join(storageRoot, "workers", `${hashResult.value}.json`);
|
|
||||||
const ctlText = await readTextFileIfExists(ctlPath);
|
|
||||||
if (ctlText === null) {
|
|
||||||
return err(`worker control file missing for bundle hash ${hashResult.value}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
let ctl: WorkerCtl;
|
|
||||||
try {
|
|
||||||
ctl = JSON.parse(ctlText) as WorkerCtl;
|
|
||||||
} catch {
|
|
||||||
return err(`corrupt worker control file: ${ctlPath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof ctl.port !== "number" || ctl.port <= 0) {
|
|
||||||
return err(`invalid worker control file: ${ctlPath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await sendWorkerTcpCommand(
|
|
||||||
ctl.port,
|
|
||||||
{ type: "kill", threadId },
|
|
||||||
{ awaitResponseLine: true },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
import { join } from "node:path";
|
|
||||||
|
|
||||||
import { err, type Result } from "@uncaged/workflow";
|
|
||||||
|
|
||||||
import { readTextFileIfExists } from "./fs-utils.js";
|
|
||||||
import {
|
|
||||||
resolveRunningHashForThread,
|
|
||||||
sendWorkerTcpCommand,
|
|
||||||
type WorkerCtl,
|
|
||||||
} from "./worker-spawn.js";
|
|
||||||
|
|
||||||
export async function cmdPause(
|
|
||||||
storageRoot: string,
|
|
||||||
threadId: string,
|
|
||||||
): Promise<Result<void, string>> {
|
|
||||||
const hashResult = await resolveRunningHashForThread(storageRoot, threadId);
|
|
||||||
if (!hashResult.ok) {
|
|
||||||
return hashResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ctlPath = join(storageRoot, "workers", `${hashResult.value}.json`);
|
|
||||||
const ctlText = await readTextFileIfExists(ctlPath);
|
|
||||||
if (ctlText === null) {
|
|
||||||
return err(`worker control file missing for bundle hash ${hashResult.value}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
let ctl: WorkerCtl;
|
|
||||||
try {
|
|
||||||
ctl = JSON.parse(ctlText) as WorkerCtl;
|
|
||||||
} catch {
|
|
||||||
return err(`corrupt worker control file: ${ctlPath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof ctl.port !== "number" || ctl.port <= 0) {
|
|
||||||
return err(`invalid worker control file: ${ctlPath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await sendWorkerTcpCommand(
|
|
||||||
ctl.port,
|
|
||||||
{ type: "pause", threadId },
|
|
||||||
{ awaitResponseLine: true },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
import { join } from "node:path";
|
|
||||||
|
|
||||||
import { err, type Result } from "@uncaged/workflow";
|
|
||||||
|
|
||||||
import { readTextFileIfExists } from "./fs-utils.js";
|
|
||||||
import {
|
|
||||||
resolveRunningHashForThread,
|
|
||||||
sendWorkerTcpCommand,
|
|
||||||
type WorkerCtl,
|
|
||||||
} from "./worker-spawn.js";
|
|
||||||
|
|
||||||
export async function cmdResume(
|
|
||||||
storageRoot: string,
|
|
||||||
threadId: string,
|
|
||||||
): Promise<Result<void, string>> {
|
|
||||||
const hashResult = await resolveRunningHashForThread(storageRoot, threadId);
|
|
||||||
if (!hashResult.ok) {
|
|
||||||
return hashResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ctlPath = join(storageRoot, "workers", `${hashResult.value}.json`);
|
|
||||||
const ctlText = await readTextFileIfExists(ctlPath);
|
|
||||||
if (ctlText === null) {
|
|
||||||
return err(`worker control file missing for bundle hash ${hashResult.value}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
let ctl: WorkerCtl;
|
|
||||||
try {
|
|
||||||
ctl = JSON.parse(ctlText) as WorkerCtl;
|
|
||||||
} catch {
|
|
||||||
return err(`corrupt worker control file: ${ctlPath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof ctl.port !== "number" || ctl.port <= 0) {
|
|
||||||
return err(`invalid worker control file: ${ctlPath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await sendWorkerTcpCommand(
|
|
||||||
ctl.port,
|
|
||||||
{ type: "resume", threadId },
|
|
||||||
{ awaitResponseLine: true },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
import type { CommandEntry } from "../../cli-command-types.js";
|
||||||
|
import { printCliError, printCliLine } from "../../cli-output.js";
|
||||||
|
import { formatCliUsage, USAGE_SKILL_TOPIC_ROWS } from "../../cli-usage.js";
|
||||||
|
import { getCommandGroupsForUsage } from "../../cli-usage-context.js";
|
||||||
|
import { cmdGc } from "./gc.js";
|
||||||
|
import { cmdCasGet } from "./get.js";
|
||||||
|
import { cmdCasList } from "./list.js";
|
||||||
|
import { cmdCasPut } from "./put.js";
|
||||||
|
import { cmdCasRm } from "./rm.js";
|
||||||
|
import type { CasDispatchDeps } from "./types.js";
|
||||||
|
|
||||||
|
function usageText(): string {
|
||||||
|
return formatCliUsage(getCommandGroupsForUsage(), USAGE_SKILL_TOPIC_ROWS);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchGc(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
if (argv.length > 0) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: gc takes no arguments`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdGc(storageRoot);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const stats = result.value;
|
||||||
|
printCliLine(
|
||||||
|
`scanned ${stats.scannedThreads} threads, ${stats.activeRefs} active refs, deleted ${stats.deletedEntries} entries`,
|
||||||
|
);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchCasGet(storageRoot: string, rest: string[]): Promise<number> {
|
||||||
|
const hash = rest[0];
|
||||||
|
if (hash === undefined || rest.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: cas get requires <hash>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdCasGet(storageRoot, hash);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(result.value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchCasPut(storageRoot: string, rest: string[]): Promise<number> {
|
||||||
|
const content = rest[0];
|
||||||
|
if (content === undefined || rest.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: cas put requires <content>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdCasPut(storageRoot, content);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(result.value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchCasList(storageRoot: string, rest: string[]): Promise<number> {
|
||||||
|
if (rest.length > 0) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: cas list takes no arguments`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdCasList(storageRoot);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (const hash of result.value) {
|
||||||
|
printCliLine(hash);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchCasRm(storageRoot: string, rest: string[]): Promise<number> {
|
||||||
|
const hash = rest[0];
|
||||||
|
if (hash === undefined || rest.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: cas rm requires <hash>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdCasRm(storageRoot, hash);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(`removed cas entry ${hash}`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const CAS_SUBCOMMAND_TABLE: Record<string, CommandEntry> = {
|
||||||
|
get: {
|
||||||
|
handler: dispatchCasGet,
|
||||||
|
args: "<hash>",
|
||||||
|
description: "Retrieve content by hash from CAS",
|
||||||
|
},
|
||||||
|
put: {
|
||||||
|
handler: dispatchCasPut,
|
||||||
|
args: "<content>",
|
||||||
|
description: "Store content in CAS, prints hash",
|
||||||
|
},
|
||||||
|
list: {
|
||||||
|
handler: dispatchCasList,
|
||||||
|
args: "",
|
||||||
|
description: "List all hashes in CAS",
|
||||||
|
},
|
||||||
|
rm: { handler: dispatchCasRm, args: "<hash>", description: "Remove a CAS entry by hash" },
|
||||||
|
gc: { handler: dispatchGc, args: "", description: "Garbage-collect unreferenced CAS entries" },
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createCasDispatcher(deps: CasDispatchDeps) {
|
||||||
|
const { dispatchGroup } = deps;
|
||||||
|
return async function dispatchCas(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const result = dispatchGroup("cas", CAS_SUBCOMMAND_TABLE, storageRoot, argv);
|
||||||
|
if (result !== null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
const sub = argv[0];
|
||||||
|
printCliError(`${usageText()}\n\nerror: unknown cas subcommand: ${sub}`);
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { createCasStore, err, getGlobalCasDir, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
export async function cmdCasGet(
|
||||||
|
storageRoot: string,
|
||||||
|
hash: string,
|
||||||
|
): Promise<Result<string, string>> {
|
||||||
|
const cas = createCasStore(getGlobalCasDir(storageRoot));
|
||||||
|
const content = await cas.get(hash);
|
||||||
|
if (content === null) {
|
||||||
|
return err(`cas entry not found: ${hash}`);
|
||||||
|
}
|
||||||
|
return ok(content);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
export {
|
||||||
|
CAS_SUBCOMMAND_TABLE,
|
||||||
|
createCasDispatcher,
|
||||||
|
dispatchCasGet,
|
||||||
|
dispatchCasList,
|
||||||
|
dispatchCasPut,
|
||||||
|
dispatchCasRm,
|
||||||
|
dispatchGc,
|
||||||
|
} from "./dispatch.js";
|
||||||
|
export { cmdGc } from "./gc.js";
|
||||||
|
export { cmdCasGet } from "./get.js";
|
||||||
|
export { cmdCasList } from "./list.js";
|
||||||
|
export { cmdCasPut } from "./put.js";
|
||||||
|
export { cmdCasRm } from "./rm.js";
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { createCasStore, getGlobalCasDir, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
export async function cmdCasList(storageRoot: string): Promise<Result<string[], string>> {
|
||||||
|
const cas = createCasStore(getGlobalCasDir(storageRoot));
|
||||||
|
const hashes = await cas.list();
|
||||||
|
return ok(hashes);
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { createCasStore, getGlobalCasDir, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
export async function cmdCasPut(
|
||||||
|
storageRoot: string,
|
||||||
|
content: string,
|
||||||
|
): Promise<Result<string, string>> {
|
||||||
|
const cas = createCasStore(getGlobalCasDir(storageRoot));
|
||||||
|
const hash = await cas.put(content);
|
||||||
|
return ok(hash);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { createCasStore, getGlobalCasDir, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
export async function cmdCasRm(storageRoot: string, hash: string): Promise<Result<void, string>> {
|
||||||
|
const cas = createCasStore(getGlobalCasDir(storageRoot));
|
||||||
|
await cas.delete(hash);
|
||||||
|
return ok(undefined);
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import type { DispatchGroupFn } from "../../cli-command-types.js";
|
||||||
|
|
||||||
|
export type CasDispatchDeps = {
|
||||||
|
dispatchGroup: DispatchGroupFn;
|
||||||
|
};
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import type { CommandEntry } from "../../cli-command-types.js";
|
||||||
|
import { printCliError, printCliLine } from "../../cli-output.js";
|
||||||
|
import { formatCliUsage, USAGE_SKILL_TOPIC_ROWS } from "../../cli-usage.js";
|
||||||
|
import { getCommandGroupsForUsage } from "../../cli-usage-context.js";
|
||||||
|
import { cmdInitTemplate } from "./template.js";
|
||||||
|
import type { InitDispatchDeps } from "./types.js";
|
||||||
|
import { cmdInitWorkspace } from "./workspace.js";
|
||||||
|
|
||||||
|
function usageText(): string {
|
||||||
|
return formatCliUsage(getCommandGroupsForUsage(), USAGE_SKILL_TOPIC_ROWS);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchInitWorkspace(_storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const name = argv[0];
|
||||||
|
if (name === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: init workspace requires <name>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdInitWorkspace(process.cwd(), name);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(`initialized workflow workspace at ${result.value.rootPath}`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchInitTemplate(_storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const name = argv[0];
|
||||||
|
if (name === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: init template requires <name>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdInitTemplate(process.cwd(), name);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(`initialized template at ${result.value.templatePath}`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const INIT_SUBCOMMAND_TABLE: Record<string, CommandEntry> = {
|
||||||
|
workspace: {
|
||||||
|
handler: dispatchInitWorkspace,
|
||||||
|
args: "<name>",
|
||||||
|
description: "Initialize a new workflow workspace",
|
||||||
|
},
|
||||||
|
template: {
|
||||||
|
handler: dispatchInitTemplate,
|
||||||
|
args: "<name>",
|
||||||
|
description: "Initialize a new workflow template",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createInitDispatcher(deps: InitDispatchDeps) {
|
||||||
|
const { dispatchGroup } = deps;
|
||||||
|
return async function dispatchInit(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const result = dispatchGroup("init", INIT_SUBCOMMAND_TABLE, storageRoot, argv);
|
||||||
|
if (result !== null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
const sub = argv[0];
|
||||||
|
printCliError(`${usageText()}\n\nerror: unknown init subcommand: ${sub}`);
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
export {
|
||||||
|
createInitDispatcher,
|
||||||
|
dispatchInitTemplate,
|
||||||
|
dispatchInitWorkspace,
|
||||||
|
INIT_SUBCOMMAND_TABLE,
|
||||||
|
} from "./dispatch.js";
|
||||||
|
export { cmdInitTemplate } from "./template.js";
|
||||||
|
export type { CmdInitTemplateSuccess, CmdInitWorkspaceSuccess } from "./types.js";
|
||||||
|
export { cmdInitWorkspace } from "./workspace.js";
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||||
|
import { dirname, join, resolve } from "node:path";
|
||||||
|
|
||||||
|
import { err, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
import { pathExists } from "../../fs-utils.js";
|
||||||
|
|
||||||
|
import {
|
||||||
|
templateIndexTs,
|
||||||
|
templateModeratorTs,
|
||||||
|
templatePackageJson,
|
||||||
|
templateRolesTs,
|
||||||
|
templateTsconfigJson,
|
||||||
|
} from "./templates.js";
|
||||||
|
import type { CmdInitTemplateSuccess } from "./types.js";
|
||||||
|
|
||||||
|
function validateWorkspaceSegment(name: string): Result<void, string> {
|
||||||
|
if (name.length === 0) {
|
||||||
|
return err("workspace name must not be empty");
|
||||||
|
}
|
||||||
|
if (name === "." || name === "..") {
|
||||||
|
return err("invalid workspace name");
|
||||||
|
}
|
||||||
|
if (name.includes("/") || name.includes("\\")) {
|
||||||
|
return err("workspace name must not contain path separators");
|
||||||
|
}
|
||||||
|
return ok(undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasTemplatesWorkspaceGlob(workspaces: unknown): boolean {
|
||||||
|
return Array.isArray(workspaces) && workspaces.includes("templates/*");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readPackageJsonWorkspaces(dir: string): Promise<unknown | null> {
|
||||||
|
const pkgPath = join(dir, "package.json");
|
||||||
|
if (!(await pathExists(pkgPath))) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
let raw: string;
|
||||||
|
try {
|
||||||
|
raw = await readFile(pkgPath, "utf8");
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
let parsed: unknown;
|
||||||
|
try {
|
||||||
|
parsed = JSON.parse(raw) as unknown;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (typeof parsed !== "object" || parsed === null || !("workspaces" in parsed)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (parsed as { workspaces: unknown }).workspaces;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Resolve uncaged-workflow workspace root (package.json with `templates/*` in `workspaces`). */
|
||||||
|
async function findWorkflowWorkspaceRoot(startDir: string): Promise<Result<string, string>> {
|
||||||
|
let dir = resolve(startDir);
|
||||||
|
for (;;) {
|
||||||
|
const workspaces = await readPackageJsonWorkspaces(dir);
|
||||||
|
if (workspaces !== null && hasTemplatesWorkspaceGlob(workspaces)) {
|
||||||
|
return ok(dir);
|
||||||
|
}
|
||||||
|
const parent = dirname(dir);
|
||||||
|
if (parent === dir) {
|
||||||
|
return err(
|
||||||
|
'not inside a workflow workspace (no package.json with workspaces containing "templates/*")',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
dir = parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function cmdInitTemplate(
|
||||||
|
startDir: string,
|
||||||
|
templateName: string,
|
||||||
|
): Promise<Result<CmdInitTemplateSuccess, string>> {
|
||||||
|
const validated = validateWorkspaceSegment(templateName);
|
||||||
|
if (!validated.ok) {
|
||||||
|
return validated;
|
||||||
|
}
|
||||||
|
|
||||||
|
const rootResult = await findWorkflowWorkspaceRoot(startDir);
|
||||||
|
if (!rootResult.ok) {
|
||||||
|
return rootResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspaceRoot = rootResult.value;
|
||||||
|
const templateDir = join(workspaceRoot, "templates", templateName);
|
||||||
|
if (await pathExists(templateDir)) {
|
||||||
|
return err(`template already exists: ${templateDir}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
await mkdir(join(templateDir, "src"), { recursive: true });
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
writeFile(join(templateDir, "package.json"), templatePackageJson(templateName), "utf8"),
|
||||||
|
writeFile(join(templateDir, "tsconfig.json"), templateTsconfigJson(), "utf8"),
|
||||||
|
writeFile(join(templateDir, "src", "roles.ts"), templateRolesTs(), "utf8"),
|
||||||
|
writeFile(join(templateDir, "src", "moderator.ts"), templateModeratorTs(), "utf8"),
|
||||||
|
writeFile(join(templateDir, "src", "index.ts"), templateIndexTs(), "utf8"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return ok({ templatePath: templateDir });
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
export function templatePackageJson(templateName: string): string {
|
||||||
|
return `${JSON.stringify(
|
||||||
|
{
|
||||||
|
name: `template-${templateName}`,
|
||||||
|
version: "0.0.0",
|
||||||
|
private: true,
|
||||||
|
type: "module",
|
||||||
|
dependencies: {
|
||||||
|
"@uncaged/workflow": "^0.1.0",
|
||||||
|
zod: "^4.0.0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
)}\n`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function templateTsconfigJson(): string {
|
||||||
|
return `${JSON.stringify(
|
||||||
|
{
|
||||||
|
extends: "../../tsconfig.json",
|
||||||
|
compilerOptions: {
|
||||||
|
rootDir: "src",
|
||||||
|
outDir: "dist",
|
||||||
|
},
|
||||||
|
include: ["src/**/*.ts"],
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
)}\n`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function templateRolesTs(): string {
|
||||||
|
return `import type { RoleDefinition } from "@uncaged/workflow";
|
||||||
|
import * as z from "zod/v4";
|
||||||
|
|
||||||
|
export const HELLO_TEMPLATE_DESCRIPTION =
|
||||||
|
"Minimal starter template: one greeter role, then END.";
|
||||||
|
|
||||||
|
export type HelloTemplateMeta = {
|
||||||
|
greeter: {
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const greeterMetaSchema = z.object({
|
||||||
|
message: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const greeterRole: RoleDefinition<HelloTemplateMeta["greeter"]> = {
|
||||||
|
description: "Says hello — replace with your first role.",
|
||||||
|
systemPrompt: "You are a helpful assistant. Reply with one short friendly sentence.",
|
||||||
|
extractPrompt: "Extract the assistant's greeting as message.",
|
||||||
|
schema: greeterMetaSchema,
|
||||||
|
extractRefs: null,
|
||||||
|
};
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function templateModeratorTs(): string {
|
||||||
|
return `import { END, type Moderator, type ModeratorContext } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
import type { HelloTemplateMeta } from "./roles.js";
|
||||||
|
|
||||||
|
export const helloTemplateModerator: Moderator<HelloTemplateMeta> = (
|
||||||
|
ctx: ModeratorContext<HelloTemplateMeta>,
|
||||||
|
) => {
|
||||||
|
if (ctx.steps.length === 0) {
|
||||||
|
return "greeter";
|
||||||
|
}
|
||||||
|
return END;
|
||||||
|
};
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function templateIndexTs(): string {
|
||||||
|
return `import type { WorkflowDefinition } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
import { helloTemplateModerator } from "./moderator.js";
|
||||||
|
import {
|
||||||
|
HELLO_TEMPLATE_DESCRIPTION,
|
||||||
|
type HelloTemplateMeta,
|
||||||
|
greeterRole,
|
||||||
|
} from "./roles.js";
|
||||||
|
|
||||||
|
export {
|
||||||
|
HELLO_TEMPLATE_DESCRIPTION,
|
||||||
|
type HelloTemplateMeta,
|
||||||
|
greeterRole,
|
||||||
|
} from "./roles.js";
|
||||||
|
export { helloTemplateModerator } from "./moderator.js";
|
||||||
|
|
||||||
|
export const helloTemplateWorkflowDefinition: WorkflowDefinition<HelloTemplateMeta> = {
|
||||||
|
description: HELLO_TEMPLATE_DESCRIPTION,
|
||||||
|
roles: {
|
||||||
|
greeter: greeterRole,
|
||||||
|
},
|
||||||
|
moderator: helloTemplateModerator,
|
||||||
|
};
|
||||||
|
`;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import type { DispatchGroupFn } from "../../cli-command-types.js";
|
||||||
|
|
||||||
|
export type CmdInitTemplateSuccess = {
|
||||||
|
templatePath: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CmdInitWorkspaceSuccess = {
|
||||||
|
rootPath: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type InitDispatchDeps = {
|
||||||
|
dispatchGroup: DispatchGroupFn;
|
||||||
|
};
|
||||||
+4
-191
@@ -1,17 +1,10 @@
|
|||||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
import { mkdir, writeFile } from "node:fs/promises";
|
||||||
import { dirname, join, resolve } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
import { err, ok, type Result } from "@uncaged/workflow";
|
import { err, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
import { pathExists } from "./fs-utils.js";
|
import { pathExists } from "../../fs-utils.js";
|
||||||
|
import type { CmdInitWorkspaceSuccess } from "./types.js";
|
||||||
export type CmdInitWorkspaceSuccess = {
|
|
||||||
rootPath: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CmdInitTemplateSuccess = {
|
|
||||||
templatePath: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
function validateWorkspaceSegment(name: string): Result<void, string> {
|
function validateWorkspaceSegment(name: string): Result<void, string> {
|
||||||
if (name.length === 0) {
|
if (name.length === 0) {
|
||||||
@@ -233,183 +226,3 @@ export async function cmdInitWorkspace(
|
|||||||
|
|
||||||
return ok({ rootPath });
|
return ok({ rootPath });
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasTemplatesWorkspaceGlob(workspaces: unknown): boolean {
|
|
||||||
return Array.isArray(workspaces) && workspaces.includes("templates/*");
|
|
||||||
}
|
|
||||||
|
|
||||||
async function readPackageJsonWorkspaces(dir: string): Promise<unknown | null> {
|
|
||||||
const pkgPath = join(dir, "package.json");
|
|
||||||
if (!(await pathExists(pkgPath))) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
let raw: string;
|
|
||||||
try {
|
|
||||||
raw = await readFile(pkgPath, "utf8");
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
let parsed: unknown;
|
|
||||||
try {
|
|
||||||
parsed = JSON.parse(raw) as unknown;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (typeof parsed !== "object" || parsed === null || !("workspaces" in parsed)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return (parsed as { workspaces: unknown }).workspaces;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Resolve uncaged-workflow workspace root (package.json with `templates/*` in `workspaces`). */
|
|
||||||
async function findWorkflowWorkspaceRoot(startDir: string): Promise<Result<string, string>> {
|
|
||||||
let dir = resolve(startDir);
|
|
||||||
for (;;) {
|
|
||||||
const workspaces = await readPackageJsonWorkspaces(dir);
|
|
||||||
if (workspaces !== null && hasTemplatesWorkspaceGlob(workspaces)) {
|
|
||||||
return ok(dir);
|
|
||||||
}
|
|
||||||
const parent = dirname(dir);
|
|
||||||
if (parent === dir) {
|
|
||||||
return err(
|
|
||||||
'not inside a workflow workspace (no package.json with workspaces containing "templates/*")',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
dir = parent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function templatePackageJson(templateName: string): string {
|
|
||||||
return `${JSON.stringify(
|
|
||||||
{
|
|
||||||
name: `template-${templateName}`,
|
|
||||||
version: "0.0.0",
|
|
||||||
private: true,
|
|
||||||
type: "module",
|
|
||||||
dependencies: {
|
|
||||||
"@uncaged/workflow": "^0.1.0",
|
|
||||||
zod: "^4.0.0",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
null,
|
|
||||||
2,
|
|
||||||
)}\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function templateTsconfigJson(): string {
|
|
||||||
return `${JSON.stringify(
|
|
||||||
{
|
|
||||||
extends: "../../tsconfig.json",
|
|
||||||
compilerOptions: {
|
|
||||||
rootDir: "src",
|
|
||||||
outDir: "dist",
|
|
||||||
},
|
|
||||||
include: ["src/**/*.ts"],
|
|
||||||
},
|
|
||||||
null,
|
|
||||||
2,
|
|
||||||
)}\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function templateRolesTs(): string {
|
|
||||||
return `import type { RoleDefinition } from "@uncaged/workflow";
|
|
||||||
import * as z from "zod/v4";
|
|
||||||
|
|
||||||
export const HELLO_TEMPLATE_DESCRIPTION =
|
|
||||||
"Minimal starter template: one greeter role, then END.";
|
|
||||||
|
|
||||||
export type HelloTemplateMeta = {
|
|
||||||
greeter: {
|
|
||||||
message: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const greeterMetaSchema = z.object({
|
|
||||||
message: z.string(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const greeterRole: RoleDefinition<HelloTemplateMeta["greeter"]> = {
|
|
||||||
description: "Says hello — replace with your first role.",
|
|
||||||
systemPrompt: "You are a helpful assistant. Reply with one short friendly sentence.",
|
|
||||||
extractPrompt: "Extract the assistant's greeting as message.",
|
|
||||||
schema: greeterMetaSchema,
|
|
||||||
extractRefs: null,
|
|
||||||
};
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function templateModeratorTs(): string {
|
|
||||||
return `import { END, type Moderator, type ModeratorContext } from "@uncaged/workflow";
|
|
||||||
|
|
||||||
import type { HelloTemplateMeta } from "./roles.js";
|
|
||||||
|
|
||||||
export const helloTemplateModerator: Moderator<HelloTemplateMeta> = (
|
|
||||||
ctx: ModeratorContext<HelloTemplateMeta>,
|
|
||||||
) => {
|
|
||||||
if (ctx.steps.length === 0) {
|
|
||||||
return "greeter";
|
|
||||||
}
|
|
||||||
return END;
|
|
||||||
};
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function templateIndexTs(): string {
|
|
||||||
return `import type { WorkflowDefinition } from "@uncaged/workflow";
|
|
||||||
|
|
||||||
import { helloTemplateModerator } from "./moderator.js";
|
|
||||||
import {
|
|
||||||
HELLO_TEMPLATE_DESCRIPTION,
|
|
||||||
type HelloTemplateMeta,
|
|
||||||
greeterRole,
|
|
||||||
} from "./roles.js";
|
|
||||||
|
|
||||||
export {
|
|
||||||
HELLO_TEMPLATE_DESCRIPTION,
|
|
||||||
type HelloTemplateMeta,
|
|
||||||
greeterRole,
|
|
||||||
} from "./roles.js";
|
|
||||||
export { helloTemplateModerator } from "./moderator.js";
|
|
||||||
|
|
||||||
export const helloTemplateWorkflowDefinition: WorkflowDefinition<HelloTemplateMeta> = {
|
|
||||||
description: HELLO_TEMPLATE_DESCRIPTION,
|
|
||||||
roles: {
|
|
||||||
greeter: greeterRole,
|
|
||||||
},
|
|
||||||
moderator: helloTemplateModerator,
|
|
||||||
};
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function cmdInitTemplate(
|
|
||||||
startDir: string,
|
|
||||||
templateName: string,
|
|
||||||
): Promise<Result<CmdInitTemplateSuccess, string>> {
|
|
||||||
const validated = validateWorkspaceSegment(templateName);
|
|
||||||
if (!validated.ok) {
|
|
||||||
return validated;
|
|
||||||
}
|
|
||||||
|
|
||||||
const rootResult = await findWorkflowWorkspaceRoot(startDir);
|
|
||||||
if (!rootResult.ok) {
|
|
||||||
return rootResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
const workspaceRoot = rootResult.value;
|
|
||||||
const templateDir = join(workspaceRoot, "templates", templateName);
|
|
||||||
if (await pathExists(templateDir)) {
|
|
||||||
return err(`template already exists: ${templateDir}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
await mkdir(join(templateDir, "src"), { recursive: true });
|
|
||||||
|
|
||||||
await Promise.all([
|
|
||||||
writeFile(join(templateDir, "package.json"), templatePackageJson(templateName), "utf8"),
|
|
||||||
writeFile(join(templateDir, "tsconfig.json"), templateTsconfigJson(), "utf8"),
|
|
||||||
writeFile(join(templateDir, "src", "roles.ts"), templateRolesTs(), "utf8"),
|
|
||||||
writeFile(join(templateDir, "src", "moderator.ts"), templateModeratorTs(), "utf8"),
|
|
||||||
writeFile(join(templateDir, "src", "index.ts"), templateIndexTs(), "utf8"),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return ok({ templatePath: templateDir });
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import type { Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
import {
|
||||||
|
readWorkerCtl,
|
||||||
|
resolveRunningHashForThread,
|
||||||
|
sendWorkerTcpCommand,
|
||||||
|
} from "../../worker-spawn.js";
|
||||||
|
|
||||||
|
type ThreadControlAction = "kill" | "pause" | "resume";
|
||||||
|
|
||||||
|
async function cmdThreadControl(
|
||||||
|
storageRoot: string,
|
||||||
|
threadId: string,
|
||||||
|
action: ThreadControlAction,
|
||||||
|
): Promise<Result<void, string>> {
|
||||||
|
const hashResult = await resolveRunningHashForThread(storageRoot, threadId);
|
||||||
|
if (!hashResult.ok) {
|
||||||
|
return hashResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ctlResult = await readWorkerCtl(storageRoot, hashResult.value);
|
||||||
|
if (!ctlResult.ok) {
|
||||||
|
return ctlResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await sendWorkerTcpCommand(
|
||||||
|
ctlResult.value.port,
|
||||||
|
{ type: action, threadId },
|
||||||
|
{ awaitResponseLine: true },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function cmdKill(
|
||||||
|
storageRoot: string,
|
||||||
|
threadId: string,
|
||||||
|
): Promise<Result<void, string>> {
|
||||||
|
return cmdThreadControl(storageRoot, threadId, "kill");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function cmdPause(
|
||||||
|
storageRoot: string,
|
||||||
|
threadId: string,
|
||||||
|
): Promise<Result<void, string>> {
|
||||||
|
return cmdThreadControl(storageRoot, threadId, "pause");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function cmdResume(
|
||||||
|
storageRoot: string,
|
||||||
|
threadId: string,
|
||||||
|
): Promise<Result<void, string>> {
|
||||||
|
return cmdThreadControl(storageRoot, threadId, "resume");
|
||||||
|
}
|
||||||
@@ -0,0 +1,206 @@
|
|||||||
|
import type { CommandEntry } from "../../cli-command-types.js";
|
||||||
|
import { printCliError, printCliLine } from "../../cli-output.js";
|
||||||
|
import { formatCliUsage, USAGE_SKILL_TOPIC_ROWS } from "../../cli-usage.js";
|
||||||
|
import { getCommandGroupsForUsage } from "../../cli-usage-context.js";
|
||||||
|
import { parseLiveArgv } from "../../live-argv.js";
|
||||||
|
import { parseRunArgv } from "../../run-argv.js";
|
||||||
|
import { cmdKill, cmdPause, cmdResume } from "./control.js";
|
||||||
|
import { cmdFork } from "./fork.js";
|
||||||
|
import { parseForkArgv } from "./fork-argv.js";
|
||||||
|
import { cmdThreads } from "./list.js";
|
||||||
|
import { cmdLive } from "./live.js";
|
||||||
|
import { cmdPs } from "./ps.js";
|
||||||
|
import { cmdThreadRemove } from "./rm.js";
|
||||||
|
import { cmdRun } from "./run.js";
|
||||||
|
import { cmdThreadShow } from "./show.js";
|
||||||
|
import type { ThreadDispatchDeps } from "./types.js";
|
||||||
|
|
||||||
|
function usageText(): string {
|
||||||
|
return formatCliUsage(getCommandGroupsForUsage(), USAGE_SKILL_TOPIC_ROWS);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchRun(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const parsed = parseRunArgv(argv);
|
||||||
|
if (!parsed.ok) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: ${parsed.error}`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await cmdRun(
|
||||||
|
storageRoot,
|
||||||
|
parsed.value.name,
|
||||||
|
parsed.value.prompt,
|
||||||
|
parsed.value.maxRounds,
|
||||||
|
);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printCliLine(result.value.threadId);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchPs(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
if (argv.length > 0) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: ps takes no arguments`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (const line of await cmdPs(storageRoot)) {
|
||||||
|
printCliLine(line);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchKill(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const threadId = argv[0];
|
||||||
|
if (threadId === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: kill requires <thread-id>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdKill(storageRoot, threadId);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(`kill sent for thread ${threadId}`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchLive(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const parsed = parseLiveArgv(argv);
|
||||||
|
if (!parsed.ok) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: ${parsed.error}`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return cmdLive(storageRoot, parsed.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchPause(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const threadId = argv[0];
|
||||||
|
if (threadId === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: pause requires <thread-id>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdPause(storageRoot, threadId);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(`pause sent for thread ${threadId}`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchResume(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const threadId = argv[0];
|
||||||
|
if (threadId === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: resume requires <thread-id>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdResume(storageRoot, threadId);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(`resume sent for thread ${threadId}`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchThreadList(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const result = await cmdThreads(storageRoot, argv);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (const line of result.value) {
|
||||||
|
printCliLine(line);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchThreadShow(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const id = argv[0];
|
||||||
|
if (id === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: thread show requires <id>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdThreadShow(storageRoot, id);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(result.value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchThreadRm(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const id = argv[0];
|
||||||
|
if (id === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: thread rm requires <id>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdThreadRemove(storageRoot, id);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(`removed thread ${id}`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchFork(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const parsed = parseForkArgv(argv);
|
||||||
|
if (!parsed.ok) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: ${parsed.error}`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdFork(storageRoot, parsed.value.threadId, parsed.value.fromRole);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(result.value.threadId);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const THREAD_SUBCOMMAND_TABLE: Record<string, CommandEntry> = {
|
||||||
|
run: {
|
||||||
|
handler: dispatchRun,
|
||||||
|
args: "<name> [--prompt <text>] [--max-rounds N]",
|
||||||
|
description: "Start a new thread executing a workflow",
|
||||||
|
},
|
||||||
|
list: {
|
||||||
|
handler: dispatchThreadList,
|
||||||
|
args: "[name]",
|
||||||
|
description: "List threads, optionally filtered by workflow name",
|
||||||
|
},
|
||||||
|
show: { handler: dispatchThreadShow, args: "<id>", description: "Show thread details and state" },
|
||||||
|
rm: { handler: dispatchThreadRm, args: "<id>", description: "Remove a thread" },
|
||||||
|
fork: {
|
||||||
|
handler: dispatchFork,
|
||||||
|
args: "<thread-id> [--from-role <role>]",
|
||||||
|
description: "Fork a thread, optionally from a specific role",
|
||||||
|
},
|
||||||
|
ps: { handler: dispatchPs, args: "", description: "List running threads" },
|
||||||
|
kill: { handler: dispatchKill, args: "<thread-id>", description: "Kill a running thread" },
|
||||||
|
live: {
|
||||||
|
handler: dispatchLive,
|
||||||
|
args: "<thread-id> | --latest [--debug] [--role <name>]",
|
||||||
|
description: "Attach to a thread and stream output live",
|
||||||
|
},
|
||||||
|
pause: { handler: dispatchPause, args: "<thread-id>", description: "Pause a running thread" },
|
||||||
|
resume: { handler: dispatchResume, args: "<thread-id>", description: "Resume a paused thread" },
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createThreadDispatcher(deps: ThreadDispatchDeps) {
|
||||||
|
const { dispatchGroup } = deps;
|
||||||
|
return async function dispatchThread(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const result = dispatchGroup("thread", THREAD_SUBCOMMAND_TABLE, storageRoot, argv);
|
||||||
|
if (result !== null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
const sub = argv[0];
|
||||||
|
printCliError(`${usageText()}\n\nerror: unknown thread subcommand: ${sub}`);
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { err, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
import type { ParsedForkArgv } from "./types.js";
|
||||||
|
|
||||||
|
export function parseForkArgv(argv: string[]): Result<ParsedForkArgv, string> {
|
||||||
|
if (argv.length === 0) {
|
||||||
|
return err("fork requires <thread-id>");
|
||||||
|
}
|
||||||
|
const threadId = argv[0];
|
||||||
|
if (threadId === undefined || threadId === "") {
|
||||||
|
return err("fork requires <thread-id>");
|
||||||
|
}
|
||||||
|
let fromRole: string | null = null;
|
||||||
|
for (let i = 1; i < argv.length; i++) {
|
||||||
|
const a = argv[i];
|
||||||
|
if (a === "--from-role") {
|
||||||
|
const r = argv[i + 1];
|
||||||
|
if (r === undefined || r === "") {
|
||||||
|
return err("--from-role requires a role name");
|
||||||
|
}
|
||||||
|
fromRole = r;
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return err(`unexpected argument: ${a}`);
|
||||||
|
}
|
||||||
|
return ok({ threadId, fromRole });
|
||||||
|
}
|
||||||
+3
-30
@@ -2,36 +2,9 @@ import { join } from "node:path";
|
|||||||
|
|
||||||
import { buildForkPlan, err, generateUlid, ok, type Result } from "@uncaged/workflow";
|
import { buildForkPlan, err, generateUlid, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
import { pathExists, readTextFileIfExists } from "./fs-utils.js";
|
import { pathExists, readTextFileIfExists } from "../../fs-utils.js";
|
||||||
import { resolveThreadDataPath } from "./thread-scan.js";
|
import { resolveThreadDataPath } from "../../thread-scan.js";
|
||||||
import { ensureWorkerForHash, sendWorkerTcpCommand } from "./worker-spawn.js";
|
import { ensureWorkerForHash, sendWorkerTcpCommand } from "../../worker-spawn.js";
|
||||||
|
|
||||||
export function parseForkArgv(
|
|
||||||
argv: string[],
|
|
||||||
): Result<{ threadId: string; fromRole: string | null }, string> {
|
|
||||||
if (argv.length === 0) {
|
|
||||||
return err("fork requires <thread-id>");
|
|
||||||
}
|
|
||||||
const threadId = argv[0];
|
|
||||||
if (threadId === undefined || threadId === "") {
|
|
||||||
return err("fork requires <thread-id>");
|
|
||||||
}
|
|
||||||
let fromRole: string | null = null;
|
|
||||||
for (let i = 1; i < argv.length; i++) {
|
|
||||||
const a = argv[i];
|
|
||||||
if (a === "--from-role") {
|
|
||||||
const r = argv[i + 1];
|
|
||||||
if (r === undefined || r === "") {
|
|
||||||
return err("--from-role requires a role name");
|
|
||||||
}
|
|
||||||
fromRole = r;
|
|
||||||
i++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
return err(`unexpected argument: ${a}`);
|
|
||||||
}
|
|
||||||
return ok({ threadId, fromRole });
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function cmdFork(
|
export async function cmdFork(
|
||||||
storageRoot: string,
|
storageRoot: string,
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
export { cmdKill, cmdPause, cmdResume } from "./control.js";
|
||||||
|
export {
|
||||||
|
createThreadDispatcher,
|
||||||
|
dispatchFork,
|
||||||
|
dispatchKill,
|
||||||
|
dispatchLive,
|
||||||
|
dispatchPause,
|
||||||
|
dispatchPs,
|
||||||
|
dispatchResume,
|
||||||
|
dispatchRun,
|
||||||
|
dispatchThreadList,
|
||||||
|
dispatchThreadRm,
|
||||||
|
dispatchThreadShow,
|
||||||
|
THREAD_SUBCOMMAND_TABLE,
|
||||||
|
} from "./dispatch.js";
|
||||||
|
export { cmdFork } from "./fork.js";
|
||||||
|
export { parseForkArgv } from "./fork-argv.js";
|
||||||
|
export { cmdThreads } from "./list.js";
|
||||||
|
export {
|
||||||
|
cmdLive,
|
||||||
|
formatLiveDebugLine,
|
||||||
|
formatLiveTimeLabel,
|
||||||
|
LIVE_CONTENT_MAX_LINES,
|
||||||
|
renderLiveRoleStepLines,
|
||||||
|
} from "./live.js";
|
||||||
|
export { cmdPs } from "./ps.js";
|
||||||
|
export { cmdThreadRemove } from "./rm.js";
|
||||||
|
export { cmdRun } from "./run.js";
|
||||||
|
export { cmdThreadShow } from "./show.js";
|
||||||
|
export type { LiveRoleRow } from "./types.js";
|
||||||
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
import { err, ok, type Result } from "@uncaged/workflow";
|
import { err, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
import { listHistoricalThreads } from "./thread-scan.js";
|
import { listHistoricalThreads } from "../../thread-scan.js";
|
||||||
import { validateCliWorkflowName } from "./workflow-name.js";
|
import { validateCliWorkflowName } from "../../workflow-name.js";
|
||||||
|
|
||||||
export async function cmdThreads(
|
export async function cmdThreads(
|
||||||
storageRoot: string,
|
storageRoot: string,
|
||||||
+6
-29
@@ -12,20 +12,15 @@ import {
|
|||||||
type WorkflowCompletion,
|
type WorkflowCompletion,
|
||||||
} from "@uncaged/workflow";
|
} from "@uncaged/workflow";
|
||||||
|
|
||||||
import { printCliError, printCliLine } from "./cli-output.js";
|
import { dimGreyLine, highlightLiveRole } from "../../cli-color.js";
|
||||||
import { pathExists } from "./fs-utils.js";
|
import { printCliError, printCliLine } from "../../cli-output.js";
|
||||||
import type { ParsedLiveArgv } from "./live-argv.js";
|
import { pathExists } from "../../fs-utils.js";
|
||||||
import { findLatestThreadDataPath, resolveThreadDataPath } from "./thread-scan.js";
|
import type { ParsedLiveArgv } from "../../live-argv.js";
|
||||||
|
import { findLatestThreadDataPath, resolveThreadDataPath } from "../../thread-scan.js";
|
||||||
|
import type { LiveRoleRow } from "./types.js";
|
||||||
|
|
||||||
export const LIVE_CONTENT_MAX_LINES = 10;
|
export const LIVE_CONTENT_MAX_LINES = 10;
|
||||||
|
|
||||||
export type LiveRoleRow = {
|
|
||||||
role: string;
|
|
||||||
content: string;
|
|
||||||
meta: Record<string, unknown>;
|
|
||||||
timestamp: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function formatLiveTimeLabel(timestampMs: number): string {
|
export function formatLiveTimeLabel(timestampMs: number): string {
|
||||||
const d = new Date(timestampMs);
|
const d = new Date(timestampMs);
|
||||||
const hh = String(d.getHours()).padStart(2, "0");
|
const hh = String(d.getHours()).padStart(2, "0");
|
||||||
@@ -34,24 +29,6 @@ export function formatLiveTimeLabel(timestampMs: number): string {
|
|||||||
return `${hh}:${mm}:${ss}`;
|
return `${hh}:${mm}:${ss}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function shouldUseColor(): boolean {
|
|
||||||
return process.stdout.isTTY === true && process.env.NO_COLOR === undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function highlightLiveRole(name: string): string {
|
|
||||||
if (!shouldUseColor()) {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
return `\x1b[1m\x1b[36m${name}\x1b[0m`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function dimGreyLine(line: string): string {
|
|
||||||
if (!shouldUseColor()) {
|
|
||||||
return line;
|
|
||||||
}
|
|
||||||
return `\x1b[2m\x1b[90m${line}\x1b[0m`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function formatLiveDebugLine(timestampMs: number, tag: string, message: string): string {
|
export function formatLiveDebugLine(timestampMs: number, tag: string, message: string): string {
|
||||||
const label = `[${formatLiveTimeLabel(timestampMs)}] [${tag}] ${message.replace(/\n/g, " ")}`;
|
const label = `[${formatLiveTimeLabel(timestampMs)}] [${tag}] ${message.replace(/\n/g, " ")}`;
|
||||||
return dimGreyLine(label);
|
return dimGreyLine(label);
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
import { listRunningThreads } from "./thread-scan.js";
|
import { listRunningThreads } from "../../thread-scan.js";
|
||||||
|
|
||||||
export async function cmdPs(storageRoot: string): Promise<string[]> {
|
export async function cmdPs(storageRoot: string): Promise<string[]> {
|
||||||
const rows = await listRunningThreads(storageRoot);
|
const rows = await listRunningThreads(storageRoot);
|
||||||
+1
-17
@@ -3,23 +3,7 @@ import { dirname, join } from "node:path";
|
|||||||
|
|
||||||
import { err, garbageCollectCas, ok, type Result } from "@uncaged/workflow";
|
import { err, garbageCollectCas, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
import { readTextFileIfExists } from "./fs-utils.js";
|
import { resolveThreadDataPath } from "../../thread-scan.js";
|
||||||
import { resolveThreadDataPath } from "./thread-scan.js";
|
|
||||||
|
|
||||||
export async function cmdThreadShow(
|
|
||||||
storageRoot: string,
|
|
||||||
threadId: string,
|
|
||||||
): Promise<Result<string, string>> {
|
|
||||||
const dataPath = await resolveThreadDataPath(storageRoot, threadId);
|
|
||||||
if (dataPath === null) {
|
|
||||||
return err(`thread not found: ${threadId}`);
|
|
||||||
}
|
|
||||||
const text = await readTextFileIfExists(dataPath);
|
|
||||||
if (text === null) {
|
|
||||||
return err(`thread data missing: ${threadId}`);
|
|
||||||
}
|
|
||||||
return ok(text.endsWith("\n") ? text.slice(0, -1) : text);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function cmdThreadRemove(
|
export async function cmdThreadRemove(
|
||||||
storageRoot: string,
|
storageRoot: string,
|
||||||
+2
-2
@@ -8,8 +8,8 @@ import {
|
|||||||
type Result,
|
type Result,
|
||||||
readWorkflowRegistry,
|
readWorkflowRegistry,
|
||||||
} from "@uncaged/workflow";
|
} from "@uncaged/workflow";
|
||||||
import { ensureWorkerForHash, sendWorkerTcpCommand } from "./worker-spawn.js";
|
import { ensureWorkerForHash, sendWorkerTcpCommand } from "../../worker-spawn.js";
|
||||||
import { validateCliWorkflowName } from "./workflow-name.js";
|
import { validateCliWorkflowName } from "../../workflow-name.js";
|
||||||
|
|
||||||
export async function cmdRun(
|
export async function cmdRun(
|
||||||
storageRoot: string,
|
storageRoot: string,
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { err, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
import { readTextFileIfExists } from "../../fs-utils.js";
|
||||||
|
import { resolveThreadDataPath } from "../../thread-scan.js";
|
||||||
|
|
||||||
|
export async function cmdThreadShow(
|
||||||
|
storageRoot: string,
|
||||||
|
threadId: string,
|
||||||
|
): Promise<Result<string, string>> {
|
||||||
|
const dataPath = await resolveThreadDataPath(storageRoot, threadId);
|
||||||
|
if (dataPath === null) {
|
||||||
|
return err(`thread not found: ${threadId}`);
|
||||||
|
}
|
||||||
|
const text = await readTextFileIfExists(dataPath);
|
||||||
|
if (text === null) {
|
||||||
|
return err(`thread data missing: ${threadId}`);
|
||||||
|
}
|
||||||
|
return ok(text.endsWith("\n") ? text.slice(0, -1) : text);
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import type { DispatchGroupFn } from "../../cli-command-types.js";
|
||||||
|
|
||||||
|
export type LiveRoleRow = {
|
||||||
|
role: string;
|
||||||
|
content: string;
|
||||||
|
meta: Record<string, unknown>;
|
||||||
|
timestamp: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ParsedForkArgv = {
|
||||||
|
threadId: string;
|
||||||
|
fromRole: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ThreadDispatchDeps = {
|
||||||
|
dispatchGroup: DispatchGroupFn;
|
||||||
|
};
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
import { err, ok, type Result } from "@uncaged/workflow";
|
||||||
|
|
||||||
|
import type { ParsedAddArgv } from "./types.js";
|
||||||
|
|
||||||
|
type ParsedLongFlag = { advance: 2; kind: "types"; value: string };
|
||||||
|
|
||||||
|
function tryParseAddLongFlag(argv: string[], index: number): Result<ParsedLongFlag | null, string> {
|
||||||
|
const tok = argv[index];
|
||||||
|
if (tok !== "--types") {
|
||||||
|
return ok(null);
|
||||||
|
}
|
||||||
|
const value = argv[index + 1];
|
||||||
|
if (value === undefined || value.startsWith("--")) {
|
||||||
|
return err("missing value for --types");
|
||||||
|
}
|
||||||
|
return ok({ advance: 2, kind: "types", value });
|
||||||
|
}
|
||||||
|
|
||||||
|
type PositionalSlots = {
|
||||||
|
name: string | undefined;
|
||||||
|
filePath: string | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
function assignPositional(tok: string, slots: PositionalSlots): Result<void, string> {
|
||||||
|
if (slots.name === undefined) {
|
||||||
|
slots.name = tok;
|
||||||
|
return ok(undefined);
|
||||||
|
}
|
||||||
|
if (slots.filePath === undefined) {
|
||||||
|
slots.filePath = tok;
|
||||||
|
return ok(undefined);
|
||||||
|
}
|
||||||
|
return err("too many arguments");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseAddArgv(argv: string[]): Result<ParsedAddArgv, string> {
|
||||||
|
const slots: PositionalSlots = { name: undefined, filePath: undefined };
|
||||||
|
let typesPath: string | null = null;
|
||||||
|
|
||||||
|
let i = 0;
|
||||||
|
while (i < argv.length) {
|
||||||
|
const flag = tryParseAddLongFlag(argv, i);
|
||||||
|
if (!flag.ok) {
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
if (flag.value !== null) {
|
||||||
|
typesPath = flag.value.value;
|
||||||
|
i += flag.value.advance;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tok = argv[i];
|
||||||
|
if (tok?.startsWith("--")) {
|
||||||
|
return err(`unknown add flag: ${tok}`);
|
||||||
|
}
|
||||||
|
if (tok === undefined) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const placed = assignPositional(tok, slots);
|
||||||
|
if (!placed.ok) {
|
||||||
|
return placed;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { name, filePath } = slots;
|
||||||
|
if (name === undefined || name === "" || filePath === undefined || filePath === "") {
|
||||||
|
return err("add requires <name> <file>");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok({ name, filePath, typesPath });
|
||||||
|
}
|
||||||
+3
-82
@@ -14,20 +14,10 @@ import {
|
|||||||
writeWorkflowRegistry,
|
writeWorkflowRegistry,
|
||||||
} from "@uncaged/workflow";
|
} from "@uncaged/workflow";
|
||||||
|
|
||||||
import { storeWorkflowBundleArtifacts } from "./bundle-store.js";
|
import { storeWorkflowBundleArtifacts } from "../../bundle-store.js";
|
||||||
import { validateCliWorkflowName } from "./workflow-name.js";
|
import { validateCliWorkflowName } from "../../workflow-name.js";
|
||||||
|
|
||||||
export type ParsedAddArgv = {
|
import type { CmdAddSuccess, ParsedAddArgv } from "./types.js";
|
||||||
name: string;
|
|
||||||
filePath: string;
|
|
||||||
/** Override path to `.d.ts` when adding a bundle. */
|
|
||||||
typesPath: string | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CmdAddSuccess = {
|
|
||||||
hash: string;
|
|
||||||
warnings: ReadonlyArray<string>;
|
|
||||||
};
|
|
||||||
|
|
||||||
function isEsmBundle(path: string): boolean {
|
function isEsmBundle(path: string): boolean {
|
||||||
return path.endsWith(".esm.js");
|
return path.endsWith(".esm.js");
|
||||||
@@ -37,75 +27,6 @@ function defaultTypesPath(bundlePath: string): string {
|
|||||||
return bundlePath.replace(/\.esm\.js$/i, ".d.ts");
|
return bundlePath.replace(/\.esm\.js$/i, ".d.ts");
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParsedLongFlag = { advance: 2; kind: "types"; value: string };
|
|
||||||
|
|
||||||
function tryParseAddLongFlag(argv: string[], index: number): Result<ParsedLongFlag | null, string> {
|
|
||||||
const tok = argv[index];
|
|
||||||
if (tok !== "--types") {
|
|
||||||
return ok(null);
|
|
||||||
}
|
|
||||||
const value = argv[index + 1];
|
|
||||||
if (value === undefined || value.startsWith("--")) {
|
|
||||||
return err("missing value for --types");
|
|
||||||
}
|
|
||||||
return ok({ advance: 2, kind: "types", value });
|
|
||||||
}
|
|
||||||
|
|
||||||
type PositionalSlots = {
|
|
||||||
name: string | undefined;
|
|
||||||
filePath: string | undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
function assignPositional(tok: string, slots: PositionalSlots): Result<void, string> {
|
|
||||||
if (slots.name === undefined) {
|
|
||||||
slots.name = tok;
|
|
||||||
return ok(undefined);
|
|
||||||
}
|
|
||||||
if (slots.filePath === undefined) {
|
|
||||||
slots.filePath = tok;
|
|
||||||
return ok(undefined);
|
|
||||||
}
|
|
||||||
return err("too many arguments");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseAddArgv(argv: string[]): Result<ParsedAddArgv, string> {
|
|
||||||
const slots: PositionalSlots = { name: undefined, filePath: undefined };
|
|
||||||
let typesPath: string | null = null;
|
|
||||||
|
|
||||||
let i = 0;
|
|
||||||
while (i < argv.length) {
|
|
||||||
const flag = tryParseAddLongFlag(argv, i);
|
|
||||||
if (!flag.ok) {
|
|
||||||
return flag;
|
|
||||||
}
|
|
||||||
if (flag.value !== null) {
|
|
||||||
typesPath = flag.value.value;
|
|
||||||
i += flag.value.advance;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const tok = argv[i];
|
|
||||||
if (tok?.startsWith("--")) {
|
|
||||||
return err(`unknown add flag: ${tok}`);
|
|
||||||
}
|
|
||||||
if (tok === undefined) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
const placed = assignPositional(tok, slots);
|
|
||||||
if (!placed.ok) {
|
|
||||||
return placed;
|
|
||||||
}
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { name, filePath } = slots;
|
|
||||||
if (name === undefined || name === "" || filePath === undefined || filePath === "") {
|
|
||||||
return err("add requires <name> <file>");
|
|
||||||
}
|
|
||||||
|
|
||||||
return ok({ name, filePath, typesPath });
|
|
||||||
}
|
|
||||||
|
|
||||||
async function registerHash(
|
async function registerHash(
|
||||||
storageRoot: string,
|
storageRoot: string,
|
||||||
name: string,
|
name: string,
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
import type { CommandEntry } from "../../cli-command-types.js";
|
||||||
|
import { printCliError, printCliLine, printCliWarn } from "../../cli-output.js";
|
||||||
|
import { formatCliUsage, USAGE_SKILL_TOPIC_ROWS } from "../../cli-usage.js";
|
||||||
|
import { getCommandGroupsForUsage } from "../../cli-usage-context.js";
|
||||||
|
import { cmdAdd, formatAddSuccess } from "./add.js";
|
||||||
|
import { parseAddArgv } from "./add-argv.js";
|
||||||
|
import { cmdHistory } from "./history.js";
|
||||||
|
import { cmdList, formatListLines } from "./list.js";
|
||||||
|
import { cmdRemove } from "./rm.js";
|
||||||
|
import { cmdRollback } from "./rollback.js";
|
||||||
|
import { cmdShow, formatShowYaml } from "./show.js";
|
||||||
|
import type { WorkflowDispatchDeps } from "./types.js";
|
||||||
|
|
||||||
|
function usageText(): string {
|
||||||
|
return formatCliUsage(getCommandGroupsForUsage(), USAGE_SKILL_TOPIC_ROWS);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchAdd(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const parsed = parseAddArgv(argv);
|
||||||
|
if (!parsed.ok) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: ${parsed.error}`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdAdd(storageRoot, parsed.value);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (const w of result.value.warnings) {
|
||||||
|
printCliWarn(w);
|
||||||
|
}
|
||||||
|
printCliLine(formatAddSuccess(parsed.value.name, parsed.value.filePath, result.value.hash));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchList(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
if (argv.length > 0) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: list takes no arguments`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdList(storageRoot);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (const line of formatListLines(result.value)) {
|
||||||
|
printCliLine(line);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchShow(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const name = argv[0];
|
||||||
|
if (name === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: show requires <name>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdShow(storageRoot, name);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(formatShowYaml(name, result.value));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchRemove(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const name = argv[0];
|
||||||
|
if (name === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: remove requires <name>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdRemove(storageRoot, name);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(`removed workflow "${name}" from registry`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchHistory(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const name = argv[0];
|
||||||
|
if (name === undefined || argv.length > 1) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: history requires <name>`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const result = await cmdHistory(storageRoot, name);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (const line of result.value) {
|
||||||
|
printCliLine(line);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function dispatchRollback(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const name = argv[0];
|
||||||
|
if (name === undefined || argv.length > 2) {
|
||||||
|
printCliError(`${usageText()}\n\nerror: rollback requires <name> [hash]`);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const hashArg = argv[1];
|
||||||
|
const result = await cmdRollback(storageRoot, name, hashArg === undefined ? null : hashArg);
|
||||||
|
if (!result.ok) {
|
||||||
|
printCliError(result.error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printCliLine(`rolled back workflow "${name}"`);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const WORKFLOW_SUBCOMMAND_TABLE: Record<string, CommandEntry> = {
|
||||||
|
add: {
|
||||||
|
handler: dispatchAdd,
|
||||||
|
args: "<name> <file.esm.js> [--types <path>]",
|
||||||
|
description: "Register a workflow bundle in the registry",
|
||||||
|
},
|
||||||
|
list: { handler: dispatchList, args: "", description: "List all registered workflows" },
|
||||||
|
show: {
|
||||||
|
handler: dispatchShow,
|
||||||
|
args: "<name>",
|
||||||
|
description: "Show details of a registered workflow",
|
||||||
|
},
|
||||||
|
rm: {
|
||||||
|
handler: dispatchRemove,
|
||||||
|
args: "<name>",
|
||||||
|
description: "Remove a workflow from the registry",
|
||||||
|
},
|
||||||
|
history: {
|
||||||
|
handler: dispatchHistory,
|
||||||
|
args: "<name>",
|
||||||
|
description: "Show version history of a workflow",
|
||||||
|
},
|
||||||
|
rollback: {
|
||||||
|
handler: dispatchRollback,
|
||||||
|
args: "<name> [hash]",
|
||||||
|
description: "Rollback a workflow to a previous version",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createWorkflowDispatcher(deps: WorkflowDispatchDeps) {
|
||||||
|
const { dispatchGroup, printDeprecation } = deps;
|
||||||
|
return async function dispatchWorkflow(storageRoot: string, argv: string[]): Promise<number> {
|
||||||
|
const result = dispatchGroup("workflow", WORKFLOW_SUBCOMMAND_TABLE, storageRoot, argv);
|
||||||
|
if (result !== null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
const sub = argv[0];
|
||||||
|
if (sub === "remove") {
|
||||||
|
printDeprecation("workflow remove", "workflow rm");
|
||||||
|
return dispatchRemove(storageRoot, argv.slice(1));
|
||||||
|
}
|
||||||
|
printCliError(`${usageText()}\n\nerror: unknown workflow subcommand: ${sub}`);
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
}
|
||||||
+1
-1
@@ -6,7 +6,7 @@ import {
|
|||||||
readWorkflowRegistry,
|
readWorkflowRegistry,
|
||||||
} from "@uncaged/workflow";
|
} from "@uncaged/workflow";
|
||||||
|
|
||||||
import { validateCliWorkflowName } from "./workflow-name.js";
|
import { validateCliWorkflowName } from "../../workflow-name.js";
|
||||||
|
|
||||||
export async function cmdHistory(
|
export async function cmdHistory(
|
||||||
storageRoot: string,
|
storageRoot: string,
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
export { cmdAdd, formatAddSuccess } from "./add.js";
|
||||||
|
export { parseAddArgv } from "./add-argv.js";
|
||||||
|
export {
|
||||||
|
createWorkflowDispatcher,
|
||||||
|
dispatchAdd,
|
||||||
|
dispatchHistory,
|
||||||
|
dispatchList,
|
||||||
|
dispatchRemove,
|
||||||
|
dispatchRollback,
|
||||||
|
dispatchShow,
|
||||||
|
WORKFLOW_SUBCOMMAND_TABLE,
|
||||||
|
} from "./dispatch.js";
|
||||||
|
export { cmdHistory } from "./history.js";
|
||||||
|
export { cmdList, formatListLines } from "./list.js";
|
||||||
|
export { cmdRemove } from "./rm.js";
|
||||||
|
export { cmdRollback } from "./rollback.js";
|
||||||
|
export { cmdShow, formatShowYaml } from "./show.js";
|
||||||
|
export type { CmdAddSuccess, ParsedAddArgv } from "./types.js";
|
||||||
+1
-1
@@ -7,7 +7,7 @@ import {
|
|||||||
writeWorkflowRegistry,
|
writeWorkflowRegistry,
|
||||||
} from "@uncaged/workflow";
|
} from "@uncaged/workflow";
|
||||||
|
|
||||||
import { validateCliWorkflowName } from "./workflow-name.js";
|
import { validateCliWorkflowName } from "../../workflow-name.js";
|
||||||
|
|
||||||
export async function cmdRemove(storageRoot: string, name: string): Promise<Result<void, string>> {
|
export async function cmdRemove(storageRoot: string, name: string): Promise<Result<void, string>> {
|
||||||
const nameOk = validateCliWorkflowName(name);
|
const nameOk = validateCliWorkflowName(name);
|
||||||
+2
-2
@@ -10,8 +10,8 @@ import {
|
|||||||
writeWorkflowRegistry,
|
writeWorkflowRegistry,
|
||||||
} from "@uncaged/workflow";
|
} from "@uncaged/workflow";
|
||||||
|
|
||||||
import { pathExists } from "./fs-utils.js";
|
import { pathExists } from "../../fs-utils.js";
|
||||||
import { validateCliWorkflowName } from "./workflow-name.js";
|
import { validateCliWorkflowName } from "../../workflow-name.js";
|
||||||
|
|
||||||
export async function cmdRollback(
|
export async function cmdRollback(
|
||||||
storageRoot: string,
|
storageRoot: string,
|
||||||
+1
-1
@@ -8,7 +8,7 @@ import {
|
|||||||
} from "@uncaged/workflow";
|
} from "@uncaged/workflow";
|
||||||
import { stringify } from "yaml";
|
import { stringify } from "yaml";
|
||||||
|
|
||||||
import { validateCliWorkflowName } from "./workflow-name.js";
|
import { validateCliWorkflowName } from "../../workflow-name.js";
|
||||||
|
|
||||||
export async function cmdShow(
|
export async function cmdShow(
|
||||||
storageRoot: string,
|
storageRoot: string,
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import type { DispatchGroupFn } from "../../cli-command-types.js";
|
||||||
|
|
||||||
|
export type ParsedAddArgv = {
|
||||||
|
name: string;
|
||||||
|
filePath: string;
|
||||||
|
/** Override path to `.d.ts` when adding a bundle. */
|
||||||
|
typesPath: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CmdAddSuccess = {
|
||||||
|
hash: string;
|
||||||
|
warnings: ReadonlyArray<string>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type WorkflowDispatchDeps = {
|
||||||
|
dispatchGroup: DispatchGroupFn;
|
||||||
|
printDeprecation: (oldCmd: string, newCmd: string) => void;
|
||||||
|
};
|
||||||
@@ -0,0 +1,238 @@
|
|||||||
|
import { getCommandRegistry } from "./cli-registry.js";
|
||||||
|
|
||||||
|
type SkillTopic = {
|
||||||
|
name: string;
|
||||||
|
description: string;
|
||||||
|
format: () => string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const SKILL_TOPICS: ReadonlyArray<SkillTopic> = [
|
||||||
|
{ name: "cli", description: "Full CLI command reference", format: formatSkillCli },
|
||||||
|
{
|
||||||
|
name: "develop",
|
||||||
|
description: "Guide for agents executing roles inside a workflow",
|
||||||
|
format: formatSkillDevelop,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "author",
|
||||||
|
description: "Guide for building and publishing workflow bundles",
|
||||||
|
format: formatSkillAuthor,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export function getSkillTopics(): ReadonlyArray<{ name: string; description: string }> {
|
||||||
|
return SKILL_TOPICS.map((t) => ({ name: t.name, description: t.description }));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatSkillTopic(topic: string): string | null {
|
||||||
|
const entry = SKILL_TOPICS.find((t) => t.name === topic);
|
||||||
|
if (entry === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return entry.format();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatSkillIndex(): string {
|
||||||
|
const rows = SKILL_TOPICS.map((t) => `| \`${t.name}\` | ${t.description} |`);
|
||||||
|
return `# uncaged-workflow skill
|
||||||
|
|
||||||
|
Available topics:
|
||||||
|
|
||||||
|
| Topic | Description |
|
||||||
|
|-------|-------------|
|
||||||
|
${rows.join("\n")}
|
||||||
|
|
||||||
|
Usage: \`uncaged-workflow skill <topic>\`
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── cli topic (existing full reference) ────────────────────────────────
|
||||||
|
|
||||||
|
function formatSkillCli(): string {
|
||||||
|
const groups = getCommandRegistry();
|
||||||
|
|
||||||
|
const commandSections: string[] = [];
|
||||||
|
for (const group of groups) {
|
||||||
|
const rows = group.commands.map((cmd) => {
|
||||||
|
const args = cmd.args ? `\`${cmd.args}\`` : "(none)";
|
||||||
|
return `| \`${group.name} ${cmd.name}\` | ${args} | ${cmd.description} |`;
|
||||||
|
});
|
||||||
|
commandSections.push(
|
||||||
|
`### ${group.name}\n\n| Command | Args | Description |\n|---------|------|-------------|\n${rows.join("\n")}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return `# uncaged-workflow CLI Reference
|
||||||
|
|
||||||
|
## Core Concepts
|
||||||
|
|
||||||
|
| Concept | Description |
|
||||||
|
|---------|-------------|
|
||||||
|
| **Workflow** | A single-file ESM bundle (\`.esm.js\`) that exports \`run\` and \`descriptor\`. Identified by name and XXH64 hash. |
|
||||||
|
| **Bundle** | The physical \`.esm.js\` file stored in the bundles directory. Immutable once written. |
|
||||||
|
| **Thread** | A single execution of a workflow, identified by a ULID. Persists state as JSONL files. |
|
||||||
|
| **CAS** | Content-Addressable Storage. Per-thread key-value store keyed by content hash. |
|
||||||
|
| **Registry** | \`workflow.yaml\` — maps workflow names to their current and historical bundle hashes. |
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
${commandSections.join("\n\n")}
|
||||||
|
|
||||||
|
### Top-level shortcuts
|
||||||
|
|
||||||
|
| Command | Equivalent | Description |
|
||||||
|
|---------|------------|-------------|
|
||||||
|
| \`run\` | \`thread run\` | Shortcut to start a thread |
|
||||||
|
| \`live\` | \`thread live\` | Shortcut to attach to a thread |
|
||||||
|
|
||||||
|
## Typical Workflow
|
||||||
|
|
||||||
|
1. \`uncaged-workflow workflow add my-wf ./my-wf.esm.js\` — register a workflow
|
||||||
|
2. \`uncaged-workflow run my-wf --prompt "do the thing"\` — start a thread
|
||||||
|
3. \`uncaged-workflow live --latest\` — attach and watch output
|
||||||
|
4. \`uncaged-workflow thread show <thread-id>\` — inspect completed thread
|
||||||
|
|
||||||
|
## Exit Codes
|
||||||
|
|
||||||
|
| Code | Meaning |
|
||||||
|
|------|---------|
|
||||||
|
| 0 | Success |
|
||||||
|
| 1 | Error |
|
||||||
|
|
||||||
|
## Environment Variables
|
||||||
|
|
||||||
|
| Variable | Description |
|
||||||
|
|----------|-------------|
|
||||||
|
| \`UNCAGED_WORKFLOW_STORAGE_ROOT\` | Override the default storage directory for all workflow data |
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── develop topic (for agents inside a workflow) ───────────────────────
|
||||||
|
|
||||||
|
function formatSkillDevelop(): string {
|
||||||
|
return `# Workflow Role Guide
|
||||||
|
|
||||||
|
Reference for agents executing roles (planner, coder, reviewer, etc.) inside a running workflow thread.
|
||||||
|
|
||||||
|
## Thread ID
|
||||||
|
|
||||||
|
Every thread has a 26-character Crockford Base32 ULID (e.g. \`06F03H5V6JTMDST6P3TVH42RWM\`).
|
||||||
|
|
||||||
|
It appears in the **first message** of the conversation. If unsure:
|
||||||
|
|
||||||
|
\`\`\`
|
||||||
|
uncaged-workflow thread list
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
## CAS (Content-Addressable Storage)
|
||||||
|
|
||||||
|
Store and retrieve content by hash in workflow storage (global CAS directory).
|
||||||
|
|
||||||
|
| Operation | Command |
|
||||||
|
|-----------|---------|
|
||||||
|
| **Store** | \`uncaged-workflow cas put '<content>'\` → prints hash |
|
||||||
|
| **Read** | \`uncaged-workflow cas get <HASH>\` → prints content |
|
||||||
|
| **List** | \`uncaged-workflow cas list\` |
|
||||||
|
|
||||||
|
CAS is the **only** supported way to persist structured data (phase plans, review notes, etc.) within a thread. Do not use temp files.
|
||||||
|
|
||||||
|
## Meta Output
|
||||||
|
|
||||||
|
Each role must produce structured output that the moderator extracts. The exact schema depends on the role, but the pattern is:
|
||||||
|
|
||||||
|
1. Do your work (write code, run tests, etc.)
|
||||||
|
2. Output a compact JSON object matching the role's schema
|
||||||
|
3. The moderator extracts and validates it automatically
|
||||||
|
|
||||||
|
## Thread Context
|
||||||
|
|
||||||
|
The conversation history contains outputs from previous roles. Read it to understand:
|
||||||
|
- What task was requested (from the initial prompt)
|
||||||
|
- What previous roles produced (plans, code changes, review results)
|
||||||
|
- What the moderator decided (which phase to work on, whether to retry)
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── author topic (for workflow developers) ─────────────────────────────
|
||||||
|
|
||||||
|
function formatSkillAuthor(): string {
|
||||||
|
return `# Workflow Authoring Guide
|
||||||
|
|
||||||
|
How to build, test, and publish workflow bundles for uncaged-workflow.
|
||||||
|
|
||||||
|
## Bundle Structure
|
||||||
|
|
||||||
|
A workflow bundle is a single ESM file (\`.esm.js\`) that exports:
|
||||||
|
|
||||||
|
\`\`\`typescript
|
||||||
|
// Required exports
|
||||||
|
export const descriptor: WorkflowDescriptor;
|
||||||
|
export const run: WorkflowRun;
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
## WorkflowDescriptor
|
||||||
|
|
||||||
|
Defines the workflow's metadata and role sequence:
|
||||||
|
|
||||||
|
\`\`\`typescript
|
||||||
|
type WorkflowDescriptor = {
|
||||||
|
name: string; // verb-first kebab-case, e.g. "solve-issue"
|
||||||
|
description: string; // one-line summary
|
||||||
|
roles: string[]; // ordered role names, e.g. ["planner", "coder", "reviewer"]
|
||||||
|
};
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
## WorkflowRun
|
||||||
|
|
||||||
|
The main function that creates and returns a moderator:
|
||||||
|
|
||||||
|
\`\`\`typescript
|
||||||
|
type WorkflowRun = (ctx: WorkflowContext) => Moderator;
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
The **Moderator** controls the flow — it decides which role runs next, handles retries, and determines when the workflow is complete.
|
||||||
|
|
||||||
|
## Role Definition
|
||||||
|
|
||||||
|
Each role has:
|
||||||
|
|
||||||
|
| Field | Type | Purpose |
|
||||||
|
|-------|------|---------|
|
||||||
|
| \`description\` | string | What the role does |
|
||||||
|
| \`systemPrompt\` | string | System prompt for the agent |
|
||||||
|
| \`extractPrompt\` | string | Instruction for extracting structured meta |
|
||||||
|
| \`schema\` | ZodSchema | Validates the extracted meta |
|
||||||
|
| \`extractRefs\` | fn or null | Extracts CAS hashes from meta for DAG linking |
|
||||||
|
| \`extractMode\` | "single" | Extraction mode |
|
||||||
|
|
||||||
|
## Development Workflow
|
||||||
|
|
||||||
|
\`\`\`bash
|
||||||
|
# 1. Initialize a workspace
|
||||||
|
uncaged-workflow init workspace my-workflow
|
||||||
|
|
||||||
|
# 2. Write your template (roles + moderator + descriptor)
|
||||||
|
|
||||||
|
# 3. Build the ESM bundle
|
||||||
|
bun run build
|
||||||
|
|
||||||
|
# 4. Register locally
|
||||||
|
uncaged-workflow workflow add my-workflow ./dist/my-workflow.esm.js
|
||||||
|
|
||||||
|
# 5. Test
|
||||||
|
uncaged-workflow run my-workflow --prompt "test task"
|
||||||
|
uncaged-workflow live --latest
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
## Versioning
|
||||||
|
|
||||||
|
Bundles are immutable and identified by XXH64 hash. Re-registering a workflow with a new bundle creates a new version. Use \`workflow history\` and \`workflow rollback\` to manage versions.
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Legacy compat ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/** @deprecated Use formatSkillTopic("cli") instead */
|
||||||
|
export function formatSkillDoc(): string {
|
||||||
|
return formatSkillCli();
|
||||||
|
}
|
||||||
@@ -3,6 +3,23 @@ import { join } from "node:path";
|
|||||||
|
|
||||||
import { pathExists, readTextFileIfExists } from "./fs-utils.js";
|
import { pathExists, readTextFileIfExists } from "./fs-utils.js";
|
||||||
|
|
||||||
|
function parseFirstJsonLineObject(text: string): Record<string, unknown> | null {
|
||||||
|
const firstLine = text.split("\n")[0];
|
||||||
|
if (firstLine === undefined || firstLine.trim() === "") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
let parsed: unknown;
|
||||||
|
try {
|
||||||
|
parsed = JSON.parse(firstLine) as unknown;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (parsed === null || typeof parsed !== "object") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return parsed as Record<string, unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
export type RunningThreadRow = {
|
export type RunningThreadRow = {
|
||||||
threadId: string;
|
threadId: string;
|
||||||
hash: string;
|
hash: string;
|
||||||
@@ -20,20 +37,11 @@ async function readThreadStartTimestampMs(dataPath: string): Promise<number | nu
|
|||||||
if (text === null) {
|
if (text === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const firstLine = text.split("\n")[0];
|
const parsed = parseFirstJsonLineObject(text);
|
||||||
if (firstLine === undefined || firstLine.trim() === "") {
|
if (parsed === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
let parsed: unknown;
|
const ts = parsed.timestamp;
|
||||||
try {
|
|
||||||
parsed = JSON.parse(firstLine) as unknown;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (parsed === null || typeof parsed !== "object") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const ts = (parsed as Record<string, unknown>).timestamp;
|
|
||||||
return typeof ts === "number" && Number.isFinite(ts) ? ts : null;
|
return typeof ts === "number" && Number.isFinite(ts) ? ts : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,20 +50,11 @@ async function readWorkflowNameFromDataJsonl(dataPath: string): Promise<string |
|
|||||||
if (text === null) {
|
if (text === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const firstLine = text.split("\n")[0];
|
const parsed = parseFirstJsonLineObject(text);
|
||||||
if (firstLine === undefined || firstLine.trim() === "") {
|
if (parsed === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
let parsed: unknown;
|
const name = parsed.name;
|
||||||
try {
|
|
||||||
parsed = JSON.parse(firstLine) as unknown;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (parsed === null || typeof parsed !== "object") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const name = (parsed as Record<string, unknown>).name;
|
|
||||||
return typeof name === "string" ? name : null;
|
return typeof name === "string" ? name : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -237,6 +237,30 @@ export async function sendWorkerTcpCommand(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function readWorkerCtl(
|
||||||
|
storageRoot: string,
|
||||||
|
hash: string,
|
||||||
|
): Promise<Result<WorkerCtl, string>> {
|
||||||
|
const ctlPath = join(storageRoot, "workers", `${hash}.json`);
|
||||||
|
const ctlText = await readTextFileIfExists(ctlPath);
|
||||||
|
if (ctlText === null) {
|
||||||
|
return err(`worker control file missing for bundle hash ${hash}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let ctl: WorkerCtl;
|
||||||
|
try {
|
||||||
|
ctl = JSON.parse(ctlText) as WorkerCtl;
|
||||||
|
} catch {
|
||||||
|
return err(`corrupt worker control file: ${ctlPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof ctl.port !== "number" || ctl.port <= 0) {
|
||||||
|
return err(`invalid worker control file: ${ctlPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok(ctl);
|
||||||
|
}
|
||||||
|
|
||||||
export async function resolveRunningHashForThread(
|
export async function resolveRunningHashForThread(
|
||||||
storageRoot: string,
|
storageRoot: string,
|
||||||
threadId: string,
|
threadId: string,
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# @uncaged/workflow-agent-cursor
|
||||||
|
|
||||||
|
`AgentFn` adapter that runs the `cursor-agent` CLI against a workspace path derived from the thread.
|
||||||
|
|
||||||
|
The agent builds a full prompt (system + task + step history via `@uncaged/workflow-util-agent`), extracts the absolute workspace path with your `extract` + Zod schema, then spawns `cursor-agent` with `--workspace`, model, and non-interactive flags.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun add @uncaged/workflow-agent-cursor @uncaged/workflow @uncaged/workflow-util-agent zod
|
||||||
|
```
|
||||||
|
|
||||||
|
In this monorepo: `"@uncaged/workflow-agent-cursor": "workspace:*"` plus `workspace:*` for `@uncaged/workflow` and `@uncaged/workflow-util-agent`.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { createCursorAgent } from "@uncaged/workflow-agent-cursor";
|
||||||
|
|
||||||
|
const agent = createCursorAgent({
|
||||||
|
model: null, // null → "auto"
|
||||||
|
timeout: 0, // ms; 0 = no limit (spawnCli timeout disabled)
|
||||||
|
extract: myExtractFn,
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## API overview
|
||||||
|
|
||||||
|
| Export | Description |
|
||||||
|
|--------|-------------|
|
||||||
|
| `createCursorAgent(config)` | Returns `AgentFn` that runs `cursor-agent` with `buildAgentPrompt(ctx)` |
|
||||||
|
| `CursorAgentConfig` | `model`, `timeout`, `extract` (must supply workspace path) |
|
||||||
|
| `validateCursorAgentConfig` | Config validation result |
|
||||||
|
| `buildAgentPrompt` | Re-exported from `@uncaged/workflow-util-agent` |
|
||||||
|
|
||||||
|
Requires `cursor-agent` on `PATH` at runtime.
|
||||||
@@ -5,7 +5,6 @@
|
|||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "bun test"
|
"test": "bun test"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
# @uncaged/workflow-agent-hermes
|
||||||
|
|
||||||
|
`AgentFn` adapter that runs the `hermes` CLI in non-interactive `chat` mode (Nerve-style flags: `-q`, `--yolo`, `--quiet`, bounded `--max-turns`).
|
||||||
|
|
||||||
|
The agent composes the same thread-aware prompt as other CLI-backed agents via `buildAgentPrompt` from `@uncaged/workflow-util-agent`, then spawns `hermes` and returns stdout on success.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun add @uncaged/workflow-agent-hermes @uncaged/workflow @uncaged/workflow-util-agent
|
||||||
|
```
|
||||||
|
|
||||||
|
In this monorepo: use `workspace:*` for all three `@uncaged/*` packages.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { createHermesAgent } from "@uncaged/workflow-agent-hermes";
|
||||||
|
|
||||||
|
const agent = createHermesAgent({
|
||||||
|
model: "your-model", // or null to omit --model
|
||||||
|
timeout: 600_000, // ms, or null for no timeout
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## API overview
|
||||||
|
|
||||||
|
| Export | Description |
|
||||||
|
|--------|-------------|
|
||||||
|
| `createHermesAgent(config)` | Returns `AgentFn` wrapping `hermes chat -q ...` |
|
||||||
|
| `HermesAgentConfig` | `model`, `timeout` |
|
||||||
|
| `validateHermesAgentConfig` | Config validation result |
|
||||||
|
| `buildAgentPrompt` | Re-exported from `@uncaged/workflow-util-agent` |
|
||||||
|
|
||||||
|
Requires `hermes` on `PATH` at runtime.
|
||||||
@@ -5,7 +5,6 @@
|
|||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "bun test"
|
"test": "bun test"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
# @uncaged/workflow-agent-llm
|
||||||
|
|
||||||
|
`AgentFn` adapter that calls an OpenAI-compatible `POST /chat/completions` endpoint using `@uncaged/workflow`’s `LlmProvider` (base URL, API key, model).
|
||||||
|
|
||||||
|
Single-turn: system text is the current role’s `systemPrompt`, user text is the thread’s initial prompt (`ctx.start.content`). Errors from HTTP, JSON, or empty choices are thrown as `Error` with a JSON payload string.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun add @uncaged/workflow-agent-llm @uncaged/workflow
|
||||||
|
```
|
||||||
|
|
||||||
|
In this monorepo: `"@uncaged/workflow-agent-llm": "workspace:*"`, `"@uncaged/workflow": "workspace:*"`.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { createLlmAdapter } from "@uncaged/workflow-agent-llm";
|
||||||
|
|
||||||
|
const agent = createLlmAdapter({
|
||||||
|
baseUrl: "https://api.openai.com/v1",
|
||||||
|
apiKey: process.env.OPENAI_API_KEY!,
|
||||||
|
model: "gpt-4.1-mini",
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## API overview
|
||||||
|
|
||||||
|
| Export | Description |
|
||||||
|
|--------|-------------|
|
||||||
|
| `createLlmAdapter(provider)` | `LlmProvider` → `AgentFn` |
|
||||||
|
| `chatCompletionText({ provider, messages })` | Low-level `Result<string, LlmChatError>` helper |
|
||||||
|
| `LlmMessage` | `{ role: "system" \| "user" \| "assistant"; content: string }` |
|
||||||
|
| `LlmChatError` | Discriminated error kinds for failed completions |
|
||||||
@@ -5,7 +5,6 @@
|
|||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "bun test"
|
"test": "bun test"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@uncaged/workflow-role-coder",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"type": "module",
|
|
||||||
"main": "src/index.ts",
|
|
||||||
"types": "src/index.ts",
|
|
||||||
"scripts": {
|
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "echo no tests"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@uncaged/workflow": "workspace:*",
|
|
||||||
"zod": "^4.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export { type CoderMeta, coderMetaSchema, coderRole } from "./coder.js";
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../../tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"rootDir": "src",
|
|
||||||
"outDir": "dist",
|
|
||||||
"composite": true
|
|
||||||
},
|
|
||||||
"include": ["src/**/*.ts"],
|
|
||||||
"references": [{ "path": "../workflow" }]
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@uncaged/workflow-role-committer",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"type": "module",
|
|
||||||
"main": "src/index.ts",
|
|
||||||
"types": "src/index.ts",
|
|
||||||
"scripts": {
|
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "bun test"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@uncaged/workflow": "workspace:*",
|
|
||||||
"zod": "^4.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export { type CommitterMeta, committerMetaSchema, committerRole } from "./committer.js";
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../../tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"rootDir": "src",
|
|
||||||
"outDir": "dist",
|
|
||||||
"composite": true
|
|
||||||
},
|
|
||||||
"include": ["src/**/*.ts"],
|
|
||||||
"references": [{ "path": "../workflow" }]
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@uncaged/workflow-role-planner",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"type": "module",
|
|
||||||
"main": "src/index.ts",
|
|
||||||
"types": "src/index.ts",
|
|
||||||
"scripts": {
|
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "echo no tests"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@uncaged/workflow": "workspace:*",
|
|
||||||
"zod": "^4.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
export {
|
|
||||||
type PlannerMeta,
|
|
||||||
phaseSchema,
|
|
||||||
plannerMetaSchema,
|
|
||||||
plannerRole,
|
|
||||||
} from "./planner.js";
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../../tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"rootDir": "src",
|
|
||||||
"outDir": "dist",
|
|
||||||
"composite": true
|
|
||||||
},
|
|
||||||
"include": ["src/**/*.ts"],
|
|
||||||
"references": [{ "path": "../workflow" }]
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@uncaged/workflow-role-preparer",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"type": "module",
|
|
||||||
"main": "src/index.ts",
|
|
||||||
"types": "src/index.ts",
|
|
||||||
"scripts": {
|
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "echo no tests"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@uncaged/workflow": "workspace:*",
|
|
||||||
"zod": "^4.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../../tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"rootDir": "src",
|
|
||||||
"outDir": "dist"
|
|
||||||
},
|
|
||||||
"include": ["src"]
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@uncaged/workflow-role-reviewer",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"type": "module",
|
|
||||||
"main": "src/index.ts",
|
|
||||||
"types": "src/index.ts",
|
|
||||||
"scripts": {
|
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "bun test"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@uncaged/workflow": "workspace:*",
|
|
||||||
"zod": "^4.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export { type ReviewerMeta, reviewerMetaSchema, reviewerRole } from "./reviewer.js";
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
import type { RoleDefinition } from "@uncaged/workflow";
|
|
||||||
import * as z from "zod/v4";
|
|
||||||
|
|
||||||
export const reviewerMetaSchema = z.discriminatedUnion("status", [
|
|
||||||
z.object({
|
|
||||||
status: z.literal("approved"),
|
|
||||||
}),
|
|
||||||
z.object({
|
|
||||||
status: z.literal("rejected"),
|
|
||||||
issues: z.array(z.string()).describe("blocking issues that must be fixed"),
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
export type ReviewerMeta = z.infer<typeof reviewerMetaSchema>;
|
|
||||||
|
|
||||||
const REVIEWER_SYSTEM = `You are a code reviewer. Review the current git diff. Give a clear approve or reject verdict.
|
|
||||||
Only reject for blocking issues. End with your verdict.`;
|
|
||||||
|
|
||||||
export const reviewerRole: RoleDefinition<ReviewerMeta> = {
|
|
||||||
description: "Runs git diff checks and sets approved when the change is ready.",
|
|
||||||
systemPrompt: REVIEWER_SYSTEM,
|
|
||||||
extractPrompt:
|
|
||||||
"Extract the review verdict: approved or rejected. If rejected, list the blocking issues.",
|
|
||||||
schema: reviewerMetaSchema,
|
|
||||||
extractRefs: null,
|
|
||||||
extractMode: "single",
|
|
||||||
};
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../../tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"rootDir": "src",
|
|
||||||
"outDir": "dist",
|
|
||||||
"composite": true
|
|
||||||
},
|
|
||||||
"include": ["src/**/*.ts"],
|
|
||||||
"references": [{ "path": "../workflow" }]
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@uncaged/workflow-role-submitter",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"type": "module",
|
|
||||||
"main": "src/index.ts",
|
|
||||||
"types": "src/index.ts",
|
|
||||||
"scripts": {
|
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "bun test"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@uncaged/workflow": "workspace:*",
|
|
||||||
"zod": "^4.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export { type SubmitterMeta, submitterMetaSchema, submitterRole } from "./submitter.js";
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../../tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"rootDir": "src",
|
|
||||||
"outDir": "dist",
|
|
||||||
"composite": true
|
|
||||||
},
|
|
||||||
"include": ["src/**/*.ts"],
|
|
||||||
"references": [{ "path": "../workflow" }]
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@uncaged/workflow-role-tester",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"type": "module",
|
|
||||||
"main": "src/index.ts",
|
|
||||||
"types": "src/index.ts",
|
|
||||||
"scripts": {
|
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "bun test"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@uncaged/workflow": "workspace:*",
|
|
||||||
"zod": "^4.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export { type TesterMeta, testerMetaSchema, testerRole } from "./tester.js";
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../../tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"rootDir": "src",
|
|
||||||
"outDir": "dist",
|
|
||||||
"composite": true
|
|
||||||
},
|
|
||||||
"include": ["src/**/*.ts"],
|
|
||||||
"references": [{ "path": "../workflow" }]
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
# @uncaged/workflow-template-develop
|
||||||
|
|
||||||
|
Reference **develop** workflow template: plan phases, implement in a loop, review, test, then commit.
|
||||||
|
|
||||||
|
Export a `WorkflowDefinition` and `createDevelopRun` so a host can bind agents/LLM and run the same graph the bundled `.esm.js` would use. Use `buildDevelopDescriptor()` when assembling `descriptor` metadata for a bundle.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun add @uncaged/workflow-template-develop @uncaged/workflow zod
|
||||||
|
```
|
||||||
|
|
||||||
|
In this monorepo: `workspace:*` for `@uncaged/workflow-template-develop` and `@uncaged/workflow`.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { createDevelopRun, developWorkflowDefinition } from "@uncaged/workflow-template-develop";
|
||||||
|
|
||||||
|
const run = createDevelopRun(binding, extract, llmProvider);
|
||||||
|
// run(...) executes the develop moderator graph with your AgentBinding
|
||||||
|
```
|
||||||
|
|
||||||
|
## Roles
|
||||||
|
|
||||||
|
| Role | Purpose |
|
||||||
|
|------|---------|
|
||||||
|
| **planner** | Break work into ordered phases (hashes) |
|
||||||
|
| **coder** | Implement current phase; repeats until phases complete or limits hit |
|
||||||
|
| **reviewer** | Code review gate (`approved` vs send back to coder) |
|
||||||
|
| **tester** | Verify via tests/build/lint (`passed` vs send back to coder) |
|
||||||
|
| **committer** | Final commit step |
|
||||||
|
|
||||||
|
Also exported: role factories/meta schemas (`plannerRole`, `coderRole`, …), `DevelopMeta`, `developRoles`.
|
||||||
|
|
||||||
|
## Moderator flow
|
||||||
|
|
||||||
|
1. **Start** → `planner`
|
||||||
|
2. After **planner** → `coder`
|
||||||
|
3. After **coder** → if all planned phases are done (or last phase completed) → `reviewer`; else `coder` again, until `maxRounds` then `END`
|
||||||
|
4. After **reviewer** → if approved → `tester`; else `coder` (or `END` if out of rounds)
|
||||||
|
5. After **tester** → if passed → `committer`; else `coder` (or `END` if out of rounds)
|
||||||
|
6. After **committer** → `END`
|
||||||
|
|
||||||
|
## API overview
|
||||||
|
|
||||||
|
| Export | Description |
|
||||||
|
|--------|-------------|
|
||||||
|
| `createDevelopRun` | `createWorkflow(developWorkflowDefinition, …)` factory |
|
||||||
|
| `developWorkflowDefinition` | `description`, `roles`, `developModerator` |
|
||||||
|
| `developModerator` | `Moderator<DevelopMeta>` |
|
||||||
|
| `buildDevelopDescriptor` | `buildDescriptor({ … })` for bundle metadata |
|
||||||
|
| `DEVELOP_WORKFLOW_DESCRIPTION` | Human-readable one-liner |
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, test } from "bun:test";
|
import { describe, expect, test } from "bun:test";
|
||||||
|
|
||||||
import { committerMetaSchema, committerRole } from "../src/committer.js";
|
import { committerMetaSchema, committerRole } from "../src/roles/committer.js";
|
||||||
|
|
||||||
describe("committerRole", () => {
|
describe("committerRole", () => {
|
||||||
test("committed sample validates against schema", () => {
|
test("committed sample validates against schema", () => {
|
||||||
@@ -6,12 +6,9 @@ import {
|
|||||||
START,
|
START,
|
||||||
validateWorkflowDescriptor,
|
validateWorkflowDescriptor,
|
||||||
} from "@uncaged/workflow";
|
} from "@uncaged/workflow";
|
||||||
|
|
||||||
import type { CommitterMeta } from "@uncaged/workflow-role-committer";
|
|
||||||
import type { PlannerMeta } from "@uncaged/workflow-role-planner";
|
|
||||||
|
|
||||||
import { buildDevelopDescriptor } from "../src/descriptor.js";
|
import { buildDevelopDescriptor } from "../src/descriptor.js";
|
||||||
import { developModerator } from "../src/index.js";
|
import { developModerator } from "../src/index.js";
|
||||||
|
import type { CommitterMeta, PlannerMeta } from "../src/roles/index.js";
|
||||||
import type { DevelopMeta } from "../src/roles.js";
|
import type { DevelopMeta } from "../src/roles.js";
|
||||||
|
|
||||||
const DEFAULT_PHASES: PlannerMeta["phases"] = [
|
const DEFAULT_PHASES: PlannerMeta["phases"] = [
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, test } from "bun:test";
|
import { describe, expect, test } from "bun:test";
|
||||||
|
|
||||||
import { reviewerMetaSchema, reviewerRole } from "../src/reviewer.js";
|
import { reviewerMetaSchema, reviewerRole } from "../src/roles/reviewer.js";
|
||||||
|
|
||||||
describe("reviewerRole", () => {
|
describe("reviewerRole", () => {
|
||||||
test("approved sample validates against schema", () => {
|
test("approved sample validates against schema", () => {
|
||||||
@@ -5,15 +5,10 @@
|
|||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "echo 'TODO'",
|
|
||||||
"test": "bun test"
|
"test": "bun test"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@uncaged/workflow": "workspace:*",
|
"@uncaged/workflow": "workspace:*",
|
||||||
"@uncaged/workflow-role-coder": "workspace:*",
|
"zod": "^4.0.0"
|
||||||
"@uncaged/workflow-role-committer": "workspace:*",
|
|
||||||
"@uncaged/workflow-role-planner": "workspace:*",
|
|
||||||
"@uncaged/workflow-role-reviewer": "workspace:*",
|
|
||||||
"@uncaged/workflow-role-tester": "workspace:*"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,34 +10,26 @@ import {
|
|||||||
import { developModerator } from "./moderator.js";
|
import { developModerator } from "./moderator.js";
|
||||||
import { DEVELOP_WORKFLOW_DESCRIPTION, type DevelopMeta, developRoles } from "./roles.js";
|
import { DEVELOP_WORKFLOW_DESCRIPTION, type DevelopMeta, developRoles } from "./roles.js";
|
||||||
|
|
||||||
|
export { buildDevelopDescriptor } from "./descriptor.js";
|
||||||
|
export { developModerator } from "./moderator.js";
|
||||||
export {
|
export {
|
||||||
type CoderMeta,
|
type CoderMeta,
|
||||||
|
type CommitterMeta,
|
||||||
coderMetaSchema,
|
coderMetaSchema,
|
||||||
coderRole,
|
coderRole,
|
||||||
} from "@uncaged/workflow-role-coder";
|
|
||||||
export {
|
|
||||||
type CommitterMeta,
|
|
||||||
committerMetaSchema,
|
committerMetaSchema,
|
||||||
committerRole,
|
committerRole,
|
||||||
} from "@uncaged/workflow-role-committer";
|
|
||||||
export {
|
|
||||||
type PlannerMeta,
|
type PlannerMeta,
|
||||||
phaseSchema,
|
phaseSchema,
|
||||||
plannerMetaSchema,
|
plannerMetaSchema,
|
||||||
plannerRole,
|
plannerRole,
|
||||||
} from "@uncaged/workflow-role-planner";
|
|
||||||
export {
|
|
||||||
type ReviewerMeta,
|
type ReviewerMeta,
|
||||||
reviewerMetaSchema,
|
reviewerMetaSchema,
|
||||||
reviewerRole,
|
reviewerRole,
|
||||||
} from "@uncaged/workflow-role-reviewer";
|
|
||||||
export {
|
|
||||||
type TesterMeta,
|
type TesterMeta,
|
||||||
testerMetaSchema,
|
testerMetaSchema,
|
||||||
testerRole,
|
testerRole,
|
||||||
} from "@uncaged/workflow-role-tester";
|
} from "./roles/index.js";
|
||||||
export { buildDevelopDescriptor } from "./descriptor.js";
|
|
||||||
export { developModerator } from "./moderator.js";
|
|
||||||
export {
|
export {
|
||||||
DEVELOP_WORKFLOW_DESCRIPTION,
|
DEVELOP_WORKFLOW_DESCRIPTION,
|
||||||
type DevelopMeta,
|
type DevelopMeta,
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import type { RoleDefinition } from "@uncaged/workflow";
|
import type { RoleDefinition } from "@uncaged/workflow";
|
||||||
import { type CoderMeta, coderRole } from "@uncaged/workflow-role-coder";
|
import { type CoderMeta, coderRole } from "./roles/coder.js";
|
||||||
import { type CommitterMeta, committerRole } from "@uncaged/workflow-role-committer";
|
import { type CommitterMeta, committerRole } from "./roles/committer.js";
|
||||||
import { type PlannerMeta, plannerRole } from "@uncaged/workflow-role-planner";
|
import { type PlannerMeta, plannerRole } from "./roles/planner.js";
|
||||||
import { type ReviewerMeta, reviewerRole } from "@uncaged/workflow-role-reviewer";
|
import { type ReviewerMeta, reviewerRole } from "./roles/reviewer.js";
|
||||||
import { type TesterMeta, testerRole } from "@uncaged/workflow-role-tester";
|
import { type TesterMeta, testerRole } from "./roles/tester.js";
|
||||||
|
|
||||||
export const DEVELOP_WORKFLOW_DESCRIPTION =
|
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).";
|
"Plan phases, implement incrementally, review, verify with tests/build/lint, and commit (planner → coder [repeat per phase] → reviewer → tester → committer).";
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user