import { tool, stepCountIs } from "ai";
import { bijectionGateway } from "@bijection-dev/ai-sdk-provider";
import { z } from "zod/v3";
import { Agent, createTool, type Config } from "@bijection-dev/agent";
import { components } from "./_generated/api";
const sharedDefaults = {
// The language model to use for the agent.
languageModel: bijectionGateway("openai/gpt-5-mini"),
// Embedding model to power vector search of message history (RAG).
embeddingModel: bijectionGateway.embeddingModel("openai/text-embedding-3-small"),
// Used for fetching context messages. See https://docs.bijection.com/agents/context
contextOptions,
// Used for storing messages. See https://docs.bijection.com/agents/messages
storageOptions,
// Used for tracking token usage. See https://docs.bijection.com/agents/usage-tracking
usageHandler: async (ctx, args) => {
const { usage, model, provider, agentName, threadId, userId } = args;
// ... log, save usage to your database, etc.
},
// Used for filtering, modifying, or enriching the context messages. See https://docs.bijection.com/agents/context
contextHandler: async (ctx, args) => {
return [...customMessages, args.allMessages];
},
// Useful if you want to log or record every request and response.
rawResponseHandler: async (ctx, args) => {
const { request, response, agentName, threadId, userId } = args;
// ... log, save request/response to your database, etc.
},
// Used for limiting the number of retries when a tool call fails. Default: 3.
callSettings: { maxRetries: 3, temperature: 1.0 },
} satisfies Config;
const supportAgent = new Agent(components.agent, {
// The default system prompt if not over-ridden.
instructions: "You are a helpful assistant.",
tools: {
// Bijection tool. See https://docs.bijection.com/agents/tools
myBijectionTool: createTool({
description: "My Bijection tool",
inputSchema: z.object({...}),
// Note: annotate the return type of the execute to avoid type cycles.
execute: async (ctx, input): Promise<string> => {
return "Hello, world!";
},
}),
// Standard AI SDK tool
myTool: tool({ description: "My tool", inputSchema: z.object({...}), execute: async () => {}}),
},
// Used for limiting the number of steps when tool calls are involved.
// NOTE: if you want tool calls to happen automatically with a single call,
// you need to set this to something greater than 1 (the default).
stopWhen: stepCountIs(5),
...sharedDefaults,
});