Package Exports
- @funkai/agents
Readme
Features
- ⚡ Functions all the way down —
agent,tool,workfloware functions that return plain objects. - 🧩 Composition over configuration — Combine small pieces instead of configuring large ones.
- 🛡️ Result, never
panicthrow — Every public method returnsResult<T>. Pattern-match onokinstead of try/catch. - 🔒 Closures are state — Workflow state is just
letvariables in your handler. - 🔍
$is optional sugar — The$helpers register data flow for observability; plain imperative code works too.
Install
npm install @funkai/agentsUsage
Agent
import { agent } from "@funkai/agents";
const helper = agent({
name: "helper",
model: "openai/gpt-4.1",
system: "You are a helpful assistant.",
});
const result = await helper.generate("What is TypeScript?");
if (!result.ok) {
console.error(result.error.code, result.error.message);
} else {
console.log(result.output);
}Tool
import { tool } from "@funkai/agents";
import { z } from "zod";
const fetchPage = tool({
description: "Fetch the contents of a web page by URL",
inputSchema: z.object({ url: z.url() }),
execute: async ({ url }) => {
const res = await fetch(url);
return { url, status: res.status, body: await res.text() };
},
});Workflow
import { workflow } from "@funkai/agents";
import { z } from "zod";
const research = workflow(
{
name: "research",
input: z.object({ topic: z.string() }),
output: z.object({ summary: z.string(), sources: z.array(z.string()) }),
},
async ({ input, $ }) => {
let sources: string[] = [];
const data = await $.step({
id: "fetch-sources",
execute: async () => findSources(input.topic),
});
if (data.ok) sources = data.value;
const analysis = await $.agent({
id: "summarize",
agent: summarizer,
input: { text: sources.join("\n") },
});
return {
summary: analysis.ok ? analysis.output : "Failed to summarize",
sources,
};
},
);
const result = await research.generate({ topic: "Effect systems" });Streaming
const result = await helper.stream("Explain closures");
if (result.ok) {
for await (const chunk of result.stream) {
process.stdout.write(chunk);
}
}API
| Export | Description |
|---|---|
agent(config) |
Create an agent. Returns { generate, stream, fn }. |
tool(config) |
Create a tool for function calling. |
workflow(config, handler) |
Create a workflow with typed I/O and tracked steps. |
createWorkflowEngine(config) |
Create a workflow factory with shared configuration and custom steps. |
openrouter(modelId) |
Shorthand to create an OpenRouter language model from env key. |
createOpenRouter(options?) |
Create a reusable OpenRouter provider instance. |
Documentation
For comprehensive documentation, see docs/overview.md.