feat: add workflow-utils package

Closes #97
This commit is contained in:
2026-04-24 22:32:29 +00:00
parent d3088c623b
commit 0a4a2330dc
11 changed files with 633 additions and 2 deletions
+25
View File
@@ -0,0 +1,25 @@
{
"name": "@uncaged/nerve-workflow-utils",
"version": "0.4.0",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": ["dist"],
"publishConfig": {
"access": "public"
},
"scripts": {
"prepublishOnly": "bash ../../scripts/prepublish-check.sh",
"build": "rslib build",
"test": "vitest run"
},
"dependencies": {
"@uncaged/nerve-core": "workspace:*",
"zod": "^4.3.6"
},
"devDependencies": {
"@rslib/core": "^0.21.3",
"@types/node": "^22.0.0",
"vitest": "^4.1.5"
}
}
+19
View File
@@ -0,0 +1,19 @@
import { defineConfig } from "@rslib/core";
export default defineConfig({
lib: [
{
format: "esm",
dts: true,
},
],
source: {
entry: {
index: "src/index.ts",
},
},
output: {
target: "node",
cleanDistPath: true,
},
});
@@ -0,0 +1,110 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { z } from "zod";
import { llmExtract } from "../llm-extract.js";
describe("llmExtract", () => {
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
});
it("parses tool call arguments and validates with the zod schema", async () => {
const schema = z
.object({
name: z.string(),
description: z.string(),
})
.describe("Extract sense metadata from plan");
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
status: 200,
text: async () =>
JSON.stringify({
choices: [
{
message: {
tool_calls: [
{
function: {
name: "extract",
arguments: JSON.stringify({ name: "cpu-usage", description: "CPU load" }),
},
},
],
},
},
],
}),
});
vi.stubGlobal("fetch", fetchMock);
const result = await llmExtract({
text: "some plan",
schema,
provider: {
baseUrl: "https://example.com/v1",
apiKey: "k",
model: "m",
},
});
expect(result.ok).toBe(true);
if (!result.ok) {
return;
}
expect(result.value).toEqual({ name: "cpu-usage", description: "CPU load" });
expect(fetchMock).toHaveBeenCalledTimes(1);
const [, init] = fetchMock.mock.calls[0] as [string, RequestInit];
expect(init.method).toBe("POST");
expect(init.headers).toMatchObject({
Authorization: "Bearer k",
"Content-Type": "application/json",
});
const body = JSON.parse(init.body as string) as {
model: string;
tool_choice: { function: { name: string } };
};
expect(body.model).toBe("m");
expect(body.tool_choice.function.name).toBeDefined();
});
it("returns schema_validation_failed when arguments do not match the schema", async () => {
const schema = z.object({ n: z.number() });
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({
ok: true,
status: 200,
text: async () =>
JSON.stringify({
choices: [
{
message: {
tool_calls: [
{ function: { name: "extract", arguments: JSON.stringify({ n: "oops" }) } },
],
},
},
],
}),
}),
);
const result = await llmExtract({
text: "x",
schema,
provider: { baseUrl: "https://example.com", apiKey: "k", model: "m" },
});
expect(result.ok).toBe(false);
if (result.ok) {
return;
}
expect(result.error.kind).toBe("schema_validation_failed");
});
});
@@ -0,0 +1,39 @@
import { describe, expect, it } from "vitest";
import { spawnSafe } from "../spawn-safe.js";
describe("spawnSafe", () => {
it("passes argv literally without shell interpretation (injection-safe)", async () => {
const injection = "$(echo BAD)";
const result = await spawnSafe(
process.execPath,
["-e", "process.stdout.write(process.argv[1] ?? '')", injection],
{ cwd: null, env: null, timeoutMs: 10_000 },
);
expect(result.ok).toBe(true);
if (!result.ok) {
return;
}
expect(result.value.stdout).toBe(injection);
expect(result.value.exitCode).toBe(0);
});
it("returns err on non-zero exit", async () => {
const result = await spawnSafe(process.execPath, ["-e", "process.exit(7)"], {
cwd: null,
env: null,
timeoutMs: 10_000,
});
expect(result.ok).toBe(false);
if (result.ok) {
return;
}
expect(result.error.kind).toBe("non_zero_exit");
if (result.error.kind !== "non_zero_exit") {
return;
}
expect(result.error.exitCode).toBe(7);
});
});
+22
View File
@@ -0,0 +1,22 @@
import { readFileSync } from "node:fs";
import { join } from "node:path";
export type ReadNerveYamlOptions = {
nerveRoot: string;
};
/**
* Reads `nerve.yaml` from a Nerve data directory (typically `~/.uncaged-nerve`).
*/
export function readNerveYaml(options: ReadNerveYamlOptions): string {
return readFileSync(join(options.nerveRoot, "nerve.yaml"), "utf-8");
}
/**
* Shared context for workflow agents: how Nerve fits together and common CLI verbs.
*/
export const nerveAgentContext = `
Nerve observes the world through **Senses** (each has its own SQLite DB and a \`compute()\` function).
**Reflexes** (YAML) schedule sense runs or start **Workflows** on intervals or signals.
The \`nerve\` CLI manages config, triggers, and queries; keep paths and commands aligned with the host nerve.yaml and senses directory.
`.trim();
@@ -0,0 +1,48 @@
import { type Result, ok } from "@uncaged/nerve-core";
import { type SpawnEnv, type SpawnError, spawnSafe } from "./spawn-safe.js";
export type CursorAgentMode = "plan" | "ask" | "default";
export type CursorAgentOptions = {
prompt: string;
mode: CursorAgentMode;
cwd: string;
env: SpawnEnv | null;
timeoutMs: number | null;
};
/**
* Invokes `cursor-agent` with the prompt passed as a single argv slot (`shell: false`).
*/
export async function cursorAgent(
options: CursorAgentOptions,
): Promise<Result<string, SpawnError>> {
const args: string[] = [
"-p",
options.prompt,
"--model",
"auto",
"--output-format",
"text",
"--trust",
"--force",
];
if (options.mode === "plan") {
args.push("--mode=plan");
} else if (options.mode === "ask") {
args.push("--mode=ask");
}
const run = await spawnSafe("cursor-agent", args, {
cwd: options.cwd,
env: options.env,
timeoutMs: options.timeoutMs,
});
if (!run.ok) {
return run;
}
return ok(run.value.stdout);
}
+20
View File
@@ -0,0 +1,20 @@
export { cursorAgent, type CursorAgentMode, type CursorAgentOptions } from "./cursor-agent.js";
export {
nerveAgentContext,
readNerveYaml,
type ReadNerveYamlOptions,
} from "./context.js";
export {
llmExtract,
type LlmError,
type LlmExtractOptions,
type LlmProvider,
} from "./llm-extract.js";
export {
nerveCommandEnv,
spawnSafe,
type SpawnEnv,
type SpawnError,
type SpawnResult,
type SpawnSafeOptions,
} from "./spawn-safe.js";
+174
View File
@@ -0,0 +1,174 @@
import { type Result, err, ok } from "@uncaged/nerve-core";
import { toJSONSchema, type z } from "zod";
export type LlmProvider = {
baseUrl: string;
apiKey: string;
model: string;
};
export type LlmExtractOptions<T> = {
text: string;
schema: z.ZodType<T>;
provider: LlmProvider;
};
export type LlmError =
| { kind: "http_error"; status: number; body: string }
| { kind: "invalid_response_json"; message: string }
| { kind: "no_tool_call"; preview: string }
| { kind: "tool_arguments_invalid_json"; message: string }
| { kind: "schema_validation_failed"; message: string }
| { kind: "network_error"; message: string };
function chatCompletionsUrl(baseUrl: string): string {
const trimmed = baseUrl.replace(/\/+$/, "");
return `${trimmed}/chat/completions`;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function stripJsonSchemaMeta(json: Record<string, unknown>): Record<string, unknown> {
const { $schema: _drop, ...rest } = json;
return rest;
}
function readToolName(parametersSchema: Record<string, unknown>): string {
const title = parametersSchema.title;
if (typeof title === "string" && title.trim().length > 0) {
return title.trim();
}
return "extract";
}
function readToolArgumentsJson(parsed: unknown, previewSource: string): Result<string, LlmError> {
if (!isRecord(parsed)) {
return err({ kind: "invalid_response_json", message: "Top-level JSON is not an object" });
}
const choices = parsed.choices;
if (!Array.isArray(choices) || choices.length === 0) {
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
}
const first = choices[0];
if (!isRecord(first)) {
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
}
const messageObj = first.message;
if (!isRecord(messageObj)) {
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
}
const toolCalls = messageObj.tool_calls;
if (!Array.isArray(toolCalls) || toolCalls.length === 0) {
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
}
const call0 = toolCalls[0];
if (!isRecord(call0)) {
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
}
const fn = call0.function;
if (!isRecord(fn)) {
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
}
const argsRaw = fn.arguments;
if (typeof argsRaw !== "string") {
return err({ kind: "no_tool_call", preview: previewSource.slice(0, 500) });
}
return ok(argsRaw);
}
/**
* Calls an OpenAI-compatible chat completions API with `tool_choice` forced to a single function
* derived from a Zod v4 schema (`toJSONSchema`). Uses `fetch()` only (no shell).
*/
export async function llmExtract<T>(options: LlmExtractOptions<T>): Promise<Result<T, LlmError>> {
const rawJsonSchema = toJSONSchema(options.schema) as Record<string, unknown>;
const parameters = stripJsonSchemaMeta(rawJsonSchema);
const toolName = readToolName(parameters);
const toolDescription =
typeof options.schema.description === "string" && options.schema.description.trim().length > 0
? options.schema.description.trim()
: "Extract structured data from the input text.";
const body = {
model: options.provider.model,
messages: [
{
role: "system" as const,
content: "Extract the requested information from the provided text. Be precise.",
},
{ role: "user" as const, content: options.text },
],
tools: [
{
type: "function" as const,
function: {
name: toolName,
description: toolDescription,
parameters,
},
},
],
tool_choice: { type: "function" as const, function: { name: toolName } },
};
let response: Response;
try {
response = await fetch(chatCompletionsUrl(options.provider.baseUrl), {
method: "POST",
headers: {
Authorization: `Bearer ${options.provider.apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
} catch (cause) {
const message = cause instanceof Error ? cause.message : String(cause);
return err({ kind: "network_error", message });
}
const responseText = await response.text();
if (!response.ok) {
return err({ kind: "http_error", status: response.status, body: responseText.slice(0, 4000) });
}
let parsed: unknown;
try {
parsed = JSON.parse(responseText) as unknown;
} catch (cause) {
const message = cause instanceof Error ? cause.message : String(cause);
return err({ kind: "invalid_response_json", message });
}
const argsJson = readToolArgumentsJson(parsed, responseText);
if (!argsJson.ok) {
return argsJson;
}
let argsParsed: unknown;
try {
argsParsed = JSON.parse(argsJson.value) as unknown;
} catch (cause) {
const message = cause instanceof Error ? cause.message : String(cause);
return err({ kind: "tool_arguments_invalid_json", message });
}
const validated = options.schema.safeParse(argsParsed);
if (!validated.success) {
return err({
kind: "schema_validation_failed",
message: validated.error.message,
});
}
return ok(validated.data);
}
+140
View File
@@ -0,0 +1,140 @@
import { spawn } from "node:child_process";
import { homedir } from "node:os";
import { join } from "node:path";
import { type Result, err, ok } from "@uncaged/nerve-core";
/** Compatible with `process.env` for `child_process.spawn`. */
export type SpawnEnv = Record<string, string | undefined>;
export type SpawnResult = {
stdout: string;
stderr: string;
exitCode: number;
/** OS signal name (e.g. `"SIGTERM"`) when terminated by signal; otherwise `null`. */
signal: string | null;
};
export type SpawnError =
| {
kind: "non_zero_exit";
stdout: string;
stderr: string;
exitCode: number;
signal: string | null;
}
| { kind: "timeout"; stdout: string; stderr: string }
| { kind: "spawn_failed"; message: string };
export type SpawnSafeOptions = {
cwd: string | null;
/** When null, merges {@link nerveCommandEnv} over `process.env`. When set, merges over that default. */
env: SpawnEnv | null;
timeoutMs: number | null;
};
const DEFAULT_TIMEOUT_MS = 300_000;
/**
* PATH and PNPM_HOME for running `pnpm` and `nerve` from workflow roles.
* Uses the pnpm store home only (no npm user bin); binaries must resolve via PATH.
*/
export function nerveCommandEnv(): SpawnEnv {
const home = homedir();
const pnpmHome = join(home, ".local/share/pnpm");
return {
...process.env,
PNPM_HOME: pnpmHome,
PATH: `${pnpmHome}:${process.env.PATH ?? ""}`,
};
}
function mergeEnv(user: SpawnEnv | null): SpawnEnv {
const base = nerveCommandEnv();
if (user === null) {
return base;
}
return { ...base, ...user };
}
function resolveTimeout(timeoutMs: number | null): number {
if (timeoutMs === null) {
return DEFAULT_TIMEOUT_MS;
}
return timeoutMs;
}
/**
* Spawn a process with `shell: false` (argv only), default {@link nerveCommandEnv}, and optional timeout.
* Returns `ok` only when the process exits with code 0.
*/
export function spawnSafe(
command: string,
args: ReadonlyArray<string>,
options: SpawnSafeOptions,
): Promise<Result<SpawnResult, SpawnError>> {
return new Promise((resolve) => {
const cwd = options.cwd === null ? process.cwd() : options.cwd;
const env = mergeEnv(options.env);
const timeoutMs = resolveTimeout(options.timeoutMs);
const child = spawn(command, args, {
cwd,
env,
shell: false,
stdio: ["ignore", "pipe", "pipe"],
});
let stdout = "";
let stderr = "";
let settled = false;
const finish = (outcome: Result<SpawnResult, SpawnError>) => {
if (settled) {
return;
}
settled = true;
clearTimeout(timer);
resolve(outcome);
};
const timer = setTimeout(() => {
child.kill("SIGTERM");
finish(err({ kind: "timeout", stdout, stderr }));
}, timeoutMs);
child.stdout?.on("data", (chunk: Buffer | string) => {
stdout += typeof chunk === "string" ? chunk : chunk.toString("utf-8");
});
child.stderr?.on("data", (chunk: Buffer | string) => {
stderr += typeof chunk === "string" ? chunk : chunk.toString("utf-8");
});
child.on("error", (cause: Error) => {
finish(err({ kind: "spawn_failed", message: cause.message }));
});
child.on("close", (code, signal) => {
const exitCode = code ?? 1;
const sig = signal === undefined || signal === null ? null : String(signal);
const result: SpawnResult = {
stdout: stdout.trimEnd(),
stderr: stderr.trimEnd(),
exitCode,
signal: sig,
};
if (exitCode !== 0) {
finish(
err({
kind: "non_zero_exit",
stdout: result.stdout,
stderr: result.stderr,
exitCode,
signal: sig,
}),
);
return;
}
finish(ok(result));
});
});
}
+9
View File
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"composite": false
},
"include": ["src"]
}
+27 -2
View File
@@ -69,7 +69,7 @@ importers:
version: link:../store
drizzle-orm:
specifier: 1.0.0-beta.23-c10d10c
version: 1.0.0-beta.23-c10d10c(@types/better-sqlite3@7.6.13)(@types/mssql@9.1.11(@azure/core-client@1.10.1))(better-sqlite3@11.10.0)(mssql@11.0.1(@azure/core-client@1.10.1))(sql.js@1.14.1)
version: 1.0.0-beta.23-c10d10c(@types/better-sqlite3@7.6.13)(@types/mssql@9.1.11(@azure/core-client@1.10.1))(better-sqlite3@11.10.0)(mssql@11.0.1(@azure/core-client@1.10.1))(sql.js@1.14.1)(zod@4.3.6)
yaml:
specifier: ^2.8.3
version: 2.8.3
@@ -100,6 +100,25 @@ importers:
specifier: ^4.1.5
version: 4.1.5(@types/node@22.19.17)(vite@8.0.9(@types/node@22.19.17)(esbuild@0.27.7)(yaml@2.8.3))
packages/workflow-utils:
dependencies:
'@uncaged/nerve-core':
specifier: workspace:*
version: link:../core
zod:
specifier: ^4.3.6
version: 4.3.6
devDependencies:
'@rslib/core':
specifier: ^0.21.3
version: 0.21.3(typescript@5.9.3)
'@types/node':
specifier: ^22.0.0
version: 22.19.17
vitest:
specifier: ^4.1.5
version: 4.1.5(@types/node@22.19.17)(vite@8.0.9(@types/node@22.19.17)(esbuild@0.27.7)(yaml@2.8.3))
packages:
'@ast-grep/napi-darwin-arm64@0.37.0':
@@ -1472,6 +1491,9 @@ packages:
engines: {node: '>= 14.6'}
hasBin: true
zod@4.3.6:
resolution: {integrity: sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==}
snapshots:
'@ast-grep/napi-darwin-arm64@0.37.0':
@@ -2169,13 +2191,14 @@ snapshots:
detect-libc@2.1.2: {}
drizzle-orm@1.0.0-beta.23-c10d10c(@types/better-sqlite3@7.6.13)(@types/mssql@9.1.11(@azure/core-client@1.10.1))(better-sqlite3@11.10.0)(mssql@11.0.1(@azure/core-client@1.10.1))(sql.js@1.14.1):
drizzle-orm@1.0.0-beta.23-c10d10c(@types/better-sqlite3@7.6.13)(@types/mssql@9.1.11(@azure/core-client@1.10.1))(better-sqlite3@11.10.0)(mssql@11.0.1(@azure/core-client@1.10.1))(sql.js@1.14.1)(zod@4.3.6):
optionalDependencies:
'@types/better-sqlite3': 7.6.13
'@types/mssql': 9.1.11(@azure/core-client@1.10.1)
better-sqlite3: 11.10.0
mssql: 11.0.1(@azure/core-client@1.10.1)
sql.js: 1.14.1
zod: 4.3.6
ecdsa-sig-formatter@1.0.11:
dependencies:
@@ -2772,3 +2795,5 @@ snapshots:
optional: true
yaml@2.8.3: {}
zod@4.3.6: {}