Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"build": "tsc && cp src/voice/ui.html dist/voice/",
"dev": "tsc --watch",
"start": "node dist/index.js",
"test": "node --test test/*.test.ts --experimental-strip-types"
"test": "node --experimental-strip-types --experimental-loader=./test/ts-resolve-hook.mjs --no-warnings --test test/*.test.ts"
},
"engines": {
"node": ">=20"
Expand Down
69 changes: 69 additions & 0 deletions spec/schemas/workflow.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://gitclaw.dev/spec/workflow.schema.json",
"title": "GitClaw SkillFlow Workflow",
"description": "A SkillFlow workflow: a named sequence of steps, where each step invokes a skill with a prompt. Runtime semantics are defined by src/workflows.ts.",
"type": "object",
"additionalProperties": false,
"required": ["name", "description", "steps"],
"properties": {
"name": {
"type": "string",
"description": "Kebab-case identifier for the workflow. Used as the file name.",
"pattern": "^[a-z0-9]+(-[a-z0-9]+)*$"
},
"description": {
"type": "string",
"description": "One-line description of what the workflow does.",
"minLength": 1
},
"steps": {
"type": "array",
"description": "Ordered list of steps. Steps execute top-to-bottom.",
"minItems": 1,
"items": { "$ref": "#/definitions/step" }
}
},
"definitions": {
"step": {
"type": "object",
"additionalProperties": false,
"required": ["skill", "prompt"],
"properties": {
"id": {
"type": "string",
"description": "Optional snake_case identifier for the step. Used when other steps reference this one via depends_on.",
"pattern": "^[a-z0-9]+(_[a-z0-9]+)*$"
},
"skill": {
"type": "string",
"description": "Name of an installed skill (kebab-case) the step will invoke. Must match an entry in the agent's skills/ directory, or 'approval' for a human-review step.",
"pattern": "^[a-z0-9]+(-[a-z0-9]+)*$"
},
"prompt": {
"type": "string",
"description": "The natural-language instruction passed to the skill for this step.",
"minLength": 1
},
"channel": {
"type": "string",
"description": "Optional channel/destination for the step output (e.g. a Slack channel name).",
"minLength": 1
},
"depends_on": {
"type": "array",
"description": "Optional list of step ids that must complete before this step runs.",
"items": {
"type": "string",
"pattern": "^[a-z0-9]+(_[a-z0-9]+)*$"
},
"uniqueItems": true
},
"requires_approval": {
"type": "boolean",
"description": "If true, the workflow pauses for human approval before this step runs."
}
}
}
}
}
188 changes: 188 additions & 0 deletions src/commands/workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { mkdir, readFile, writeFile } from "fs/promises";
import { join, resolve } from "path";
import { discoverSkills } from "../skills.js";
import { validateWorkflow } from "../utils/schemas.js";
import { generateWorkflow, type LlmClient } from "../utils/workflow-generator.js";

interface GenerateFlags {
dir: string;
prompt?: string;
refine?: string;
model?: string;
apiKey?: string;
dryRun: boolean;
}

const RED = (s: string) => `\x1b[31m${s}\x1b[0m`;
const GREEN = (s: string) => `\x1b[32m${s}\x1b[0m`;
const DIM = (s: string) => `\x1b[2m${s}\x1b[0m`;
const BOLD = (s: string) => `\x1b[1m${s}\x1b[0m`;

const MAX_RETRIES = 2;

function printHelp(): void {
console.log(`${BOLD("gitclaw workflow")} — generate SkillFlow workflows from natural language

Usage:
gitclaw workflow generate [options]

Options:
-d, --dir <path> Agent directory (default: current directory)
-p, --prompt <text> Natural-language description of the workflow (required)
--refine <file> Refine an existing workflow YAML by applying --prompt as an instruction
-m, --model <spec> LLM model in provider:model form (default: openai:gpt-4o)
--api-key <key> API key for the provider (falls back to OPENAI_API_KEY or <PROVIDER>_API_KEY)
--dry-run Print the generated YAML to stdout instead of writing a file
-h, --help Show this help message

Examples:
gitclaw workflow generate -p "every morning summarize unread emails and post to Slack"
gitclaw workflow generate -p "add a human approval step before the Slack post" --refine workflows/morning-digest.yaml
`);
}

function parseFlags(argv: string[]): GenerateFlags {
const flags: GenerateFlags = { dir: process.cwd(), dryRun: false };
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
switch (a) {
case "-d":
case "--dir":
flags.dir = argv[++i];
break;
case "-p":
case "--prompt":
flags.prompt = argv[++i];
break;
case "--refine":
flags.refine = argv[++i];
break;
case "-m":
case "--model":
flags.model = argv[++i];
break;
case "--api-key":
flags.apiKey = argv[++i];
break;
case "--dry-run":
flags.dryRun = true;
break;
case "-h":
case "--help":
printHelp();
process.exit(0);
break;
default:
if (!a.startsWith("-") && flags.prompt === undefined) {
flags.prompt = a;
} else {
console.error(RED(`Unknown option: ${a}`));
process.exit(2);
}
}
}
return flags;
}

function slugify(name: string): string {
const cleaned = name
.toLowerCase()
.trim()
.replace(/[^a-z0-9-]+/g, "-")
.replace(/^-+|-+$/g, "")
.replace(/-+/g, "-");
return cleaned || "workflow";
}

export interface RunGenerateOptions {
flags: GenerateFlags;
llm?: LlmClient;
}

export async function runGenerate(opts: RunGenerateOptions): Promise<{ filePath?: string; yaml: string; }> {
const { flags } = opts;
if (!flags.prompt || !flags.prompt.trim()) {
throw new Error("--prompt is required");
}

const agentDir = resolve(flags.dir);
const skills = await discoverSkills(agentDir);

let previousWorkflow: string | undefined;
if (flags.refine) {
const refinePath = resolve(agentDir, flags.refine);
previousWorkflow = await readFile(refinePath, "utf-8");
}

let promptForLlm = flags.prompt.trim();
let lastErrors: string[] = [];
let yaml = "";

for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
console.error(DIM(attempt === 0 ? "Generating workflow..." : `Retry ${attempt}/${MAX_RETRIES} — fixing validation errors...`));
yaml = await generateWorkflow({
prompt: promptForLlm,
skills,
previousWorkflow,
model: flags.model,
apiKey: flags.apiKey,
llm: opts.llm,
});
const result = validateWorkflow(yaml);
if (result.valid) {
lastErrors = [];
break;
}
lastErrors = result.errors;
if (attempt < MAX_RETRIES) {
promptForLlm =
`${flags.prompt.trim()}\n\nThe previous attempt failed schema validation. Fix these errors and return the full YAML again:\n` +
result.errors.map((e) => `- ${e}`).join("\n");
}
}

if (lastErrors.length > 0) {
console.error(RED("\nWorkflow validation failed after retries:"));
for (const e of lastErrors) console.error(RED(` - ${e}`));
console.error(DIM("\nLast generated YAML:\n"));
console.error(yaml);
throw new Error("Validation failed after retries");
}

if (flags.dryRun) {
process.stdout.write(yaml.endsWith("\n") ? yaml : yaml + "\n");
return { yaml };
}

// Parse the validated YAML to get the workflow name for the file path.
const validated = validateWorkflow(yaml).data!;
const slug = slugify(validated.name);
const workflowsDir = join(agentDir, "workflows");
await mkdir(workflowsDir, { recursive: true });
const filePath = join(workflowsDir, `${slug}.yaml`);
await writeFile(filePath, yaml.endsWith("\n") ? yaml : yaml + "\n", "utf-8");
console.error(GREEN(`\nWrote workflow to ${filePath}`));
return { filePath, yaml };
}

export async function handleWorkflowCommand(argv: string[]): Promise<void> {
// argv is the raw process.argv tail starting at the 'workflow' token.
// argv[0] === "workflow"; argv[1] is the sub-command.
const sub = argv[1];
if (!sub || sub === "-h" || sub === "--help") {
printHelp();
return;
}
if (sub !== "generate") {
console.error(RED(`Unknown subcommand: ${sub}`));
printHelp();
process.exit(2);
}
const flags = parseFlags(argv.slice(2));
try {
await runGenerate({ flags });
} catch (err: any) {
console.error(RED(`\nError: ${err?.message ?? String(err)}`));
process.exit(1);
}
}
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { initLocalSession } from "./session.js";
import type { LocalSession } from "./session.js";
import { startVoiceServer } from "./voice/server.js";
import { handlePluginCommand } from "./plugin-cli.js";
import { handleWorkflowCommand } from "./commands/workflow.js";
import { context as otelContext } from "@opentelemetry/api";
import {
initTelemetry,
Expand Down Expand Up @@ -301,6 +302,12 @@ async function ensureRepo(dir: string, model?: string): Promise<string> {
}

async function main(): Promise<void> {
// Handle workflow subcommand: gitclaw workflow <generate|...>
if (process.argv[2] === "workflow") {
await handleWorkflowCommand(process.argv.slice(2));
return;
}

// Handle plugin subcommand: gitclaw plugin <install|list|remove|...>
if (process.argv[2] === "plugin") {
const allArgs = process.argv.slice(3);
Expand Down
Loading