Primitives.org.ai

Function

Call AI like a function

Everything starts with ai. Call it, chain it, destructure it.

import { ai } from 'ai-functions'

// Template literal
const explanation = await ai`Explain ${topic} to a 5 year old`

// Dynamic functions
const code = await ai.generateAuthMiddleware(requirements)
const analysis = await ai.analyzeMarketTrends(data)

// Destructuring
const { title, tags, excerpt } = ai`metadata for: ${blogPost}`

Promise Pipelining

Chain operations without await—dependencies resolve automatically:

const { score, qualified, reason } = ai`qualify ${lead}`

// Chain naturally
const email = ai`follow-up email based on: ${reason}`
const subject = ai`subject line for: ${email}`

// Only await when you need the value
if (await qualified) {
  await send({ subject: await subject, body: await email })
}

Batch Processing

Process arrays in a single call:

const leads = list`leads from ${campaign}`

const qualified = await leads.map(lead => ({
  lead,
  score: ai`score 1-100: ${lead}`,
  fit: ai`${lead} matches ${icp}?`,
}))

All Functions

Core

ExportDescription
AI()Create typed AI instances with predefined schemas
aiThe simplest way to call AI
defineThe foundation for creating AI functions
generateThe core generation primitive with schema support

Function Types

Every ai.doSomething() becomes one of four function types:

TypeWhat It DoesReturns
CodeGenerates executable codeCode string
GenerativeCreates content/dataStructured data
AgenticTakes autonomous actionAction results
HumanRequires human inputHuman response

Generative

FunctionDescription
listGenerate a list of items
listsGenerate multiple named lists
writeGenerate text content
summarizeSummarize content
extractExtract structured data
diagramGenerate diagrams
slidesGenerate presentations
imageGenerate images
videoGenerate video

Logic

FunctionDescription
isBoolean classification
decideMake decisions from options

Action

FunctionDescription
doExecute actions
browseBrowse the web
readRead and parse content
researchResearch a topic
askAsk a question (agent or human)
approveRequest approval (agent or human)
reviewRequest review (agent or human)

Examples

Sales Pipeline

const leads = list`leads from ${campaign}`

const qualified = await leads.map(lead => ({
  lead,
  score: ai`score 1-100: ${lead}`,
  fit: ai`${lead} matches ${icp}?`,
}))

const approved = qualified.filter(l => l.score > 80)
const emails = await approved.map(l => write`follow-up for ${l.lead}`)

Content Generation

const ai = AI({
  blogPost: {
    title: 'Post title',
    summary: 'Brief summary',
    sections: [{
      heading: 'Section heading',
      content: 'Section content',
    }],
  },
})

const post = await ai.blogPost('TypeScript tips')
const summary = await summarize(post)
const tags = await list`tags for: ${post.title}`

Code Generation

const middleware = await ai.generateAuthMiddleware({
  provider: 'jwt',
  roles: ['admin', 'user', 'guest'],
})

const migration = await ai.createMigration({
  action: 'add users table',
  columns: ['id', 'email', 'name', 'created_at'],
})

Human-in-the-Loop

const proposal = await ai.generateProposal(requirements)
const approved = await approve`${proposal} for ${client}`

if (approved) {
  const feedback = await review`implementation plan for ${proposal}`
  const final = await ai.refineProposal(proposal, feedback)
}

Installation

npm install ai-functions

Package

The Function primitive is implemented in the ai-functions package.

See the package documentation for full API reference and additional examples.

Was this page helpful?

On this page