Setting the file. One moment.
Subchapter 1.1
references/ai-tool.mdMarkdown5 KBView on GitHub
Convert Trigger.dev tasks to Vercel AI SDK tools. Let LLMs call your tasks autonomously.
import { schemaTask, ai } from "@trigger.dev/sdk";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
// 1. Define task with schema
const lookupWeather = schemaTask({
id: "lookup-weather",
schema: z.object({
location: z.string().describe("City name"),
units: z.enum(["celsius", "fahrenheit"]).default("celsius"),
}),
run: async ({ location, units }) => {
const weather = await fetchWeather(location, units);
return { temperature: weather.temp, conditions: weather.conditions };
},
});
// 2. Convert to AI tool
const weatherTool = ai.tool(lookupWeather);
// 3. Use with AI SDK
export const weatherAgent = schemaTask({
id: "weather-agent",
schema: z.object({ question: z.string() }),
run: async ({ question }) => {
const result = await generateText({
model: openai("gpt-4o"),
prompt: question,
tools: {
lookupWeather: weatherTool,
},
});
return { answer: result.text };
},
});The task must use schemaTask with a Zod schema:
// ✅ Works - has schema
const myTask = schemaTask({
id: "my-task",
schema: z.object({
query: z.string(),
}),
run: async (payload) => { ... },
});
// ❌ Won't work - no schema
const myTask = task({
id: "my-task",
run: async (payload: { query: string }) => { ... },
});Supported schema libraries:
.toJsonSchema() methodCustomize how results are sent back to the LLM:
const searchTool = ai.tool(searchDatabase, {
experimental_toToolResultContent: (result) => {
// Return structured content for the LLM
return [
{
type: "text",
text: `Found ${result.count} results:\n${result.items.map(i => i.title).join("\n")}`,
},
];
},
});Get execution context inside the task:
const myToolTask = schemaTask({
id: "my-tool-task",
schema: z.object({ input: z.string() }),
run: async (payload) => {
// Access AI SDK tool execution options
const toolOptions = ai.currentToolOptions();
console.log(toolOptions);
// { toolCallId: "...", messages: [...], ... }
return processInput(payload.input);
},
});const searchTool = ai.tool(searchDatabase);
const calculateTool = ai.tool(calculate);
const summarizeTool = ai.tool(summarize);
export const agentTask = schemaTask({
id: "agent",
schema: z.object({ task: z.string() }),
run: async ({ task }) => {
const result = await generateText({
model: openai("gpt-4o"),
prompt: task,
tools: {
search: searchTool,
calculate: calculateTool,
summarize: summarizeTool,
},
maxSteps: 10, // Allow multiple tool calls
});
return { result: result.text };
},
});const result = await generateText({
model: openai("gpt-4o"),
prompt: "What's the weather in Tokyo?",
tools: {
weather: weatherTool,
news: newsTool,
},
toolChoice: "required", // Force tool use
// or: toolChoice: { type: "tool", toolName: "weather" }
});Add descriptions for better LLM understanding:
const searchTask = schemaTask({
id: "search-database",
description: "Search the product database for items matching a query",
schema: z.object({
query: z.string().describe("Search terms"),
limit: z.number().min(1).max(100).describe("Max results to return"),
category: z.enum(["electronics", "clothing", "books"]).optional()
.describe("Filter by product category"),
}),
run: async (payload) => { ... },
});const webSearch = schemaTask({
id: "web-search",
schema: z.object({
query: z.string(),
maxResults: z.number().default(5),
}),
run: async ({ query, maxResults }) => {
return await searchWeb(query, maxResults);
},
});
const readUrl = schemaTask({
id: "read-url",
schema: z.object({
url: z.string().url(),
}),
run: async ({ url }) => {
return await fetchAndParse(url);
},
});
export const researchAgent = schemaTask({
id: "research-agent",
schema: z.object({ topic: z.string() }),
run: async ({ topic }) => {
const result = await generateText({
model: openai("gpt-4o"),
system: "Research the topic thoroughly using available tools.",
prompt: topic,
tools: {
search: ai.tool(webSearch),
read: ai.tool(readUrl),
},
maxSteps: 20,
});
return { research: result.text };
},
});task won’t work.describe() - on schema fields for parameter hintsexperimental_toToolResultContent for better LLM context