import { generateText, isStepCount, jsonSchema, tool } from 'ai';
import { M91Client } from '@msg91comm/m91';
import { createM91ToolApproval } from '@msg91comm/m91/integrations/vercel-ai';
// The send link comes from a channel in the M91 app. Keep it secret.
const client = new M91Client(process.env.M91_SEND_LINK!);
const issueRefund = tool({
description: 'Simulate a refund approval; no payment is issued.',
inputSchema: jsonSchema<{ orderId: string; amount: number }>({
type: 'object',
properties: { orderId: { type: 'string' }, amount: { type: 'number' } },
required: ['orderId', 'amount'],
}),
// Replace this simulation with your payment API before issuing real refunds.
execute: async ({ orderId, amount }) => ({ simulated: true, orderId, amount }),
});
const { text } = await generateText({
model: 'openai/gpt-5-mini',
tools: { issueRefund },
// issueRefund rings a phone first and runs only if a person taps Approve.
// A rejection, no answer within 10 minutes, or an M91 error denies it.
toolApproval: createM91ToolApproval(client, {
tools: ['issueRefund'],
timeoutSeconds: 600,
}),
stopWhen: isStepCount(5),
prompt: 'Order ORD-40122 never arrived. Refund the customer 48 USD.',
});
console.log(text);