Browse documentation

Build

Functions

Define one finite operation with Zod input and output types.

A function is one finite invocation. Use a prompt for AI work or run for native TypeScript and orchestration. A function has exactly one of them.

import { flary, z } from "flary";

const app = flary({ model: "openai/gpt-5" });

export const summarize = app.fn({
  input: z.object({ text: z.string().min(1) }),
  output: z.object({ summary: z.string() }),
  prompt: ({ text }) => `Summarize this text as JSON: ${text}`,
});

Zod validates the input before execution and the output before return or durable storage.

const result = await summarize({ text: "A long document..." });

Use start() when the caller must reconnect to a finite invocation:

const run = await summarize.start({ text });
for await (const event of run.stream()) render(event);
const result = await run.result();

Named native steps store parsed results and reuse completed work after a restart. Keep prompt text, schemas, and tools in normal TypeScript files. The starter in templates/starter is built during package acceptance tests.

Tested starter source

import { z } from "flary";

import { app } from "./flary";
import { supportTools } from "./tools";

export const support = app.fn({
  input: z.object({ question: z.string().min(1) }),
  output: z.object({
    answer: z.string(),
    sources: z.array(z.string().url()),
  }),
  tools: supportTools,
  prompt: ({ question }) => `
Answer the question. Use the documentation tool when product facts are needed.
Return JSON with an answer and a sources array.

Question: ${question}
`,
});