File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { StripeAgentToolkit } from '@stripe/agent-toolkit/ai-sdk';
2+ import { openai } from '@ai-sdk/openai';
3+ import {
4+ convertToCoreMessages,
5+ experimental_wrapLanguageModel as wrapLanguageModel,
6+ streamText
7+ } from 'ai';
8+
9+ // Instantiate a new Stripe Agent Toolkit.
10+ const stripeAgentToolkit = new StripeAgentToolkit({
11+ secretKey: process.env.STRIPE_SECRET_KEY!,
12+ configuration: {},
13+ });
14+
15+ export async function POST(req: Request) {
16+ const { messages } = await req.json();
17+
18+ // Wrap the language model and include the
19+ // billing middleware.
20+ const model = wrapLanguageModel({
21+ model: openai('gpt-4.1'),
22+ middleware: stripeAgentToolkit.middleware({
23+ billing: {
24+ customer: process.env.STRIPE_CUSTOMER_ID!,
25+ meters: {
26+ input: process.env.STRIPE_METER_INPUT!,
27+ output: process.env.STRIPE_METER_OUTPUT!,
28+ },
29+ },
30+ }),
31+ });
32+
33+ // Call the model and stream back the results.
34+ const result = await streamText({
35+ model: model,
36+ system: SYSTEM_PROMPT,
37+ messages: convertToCoreMessages(messages),
38+ });
39+
40+ return result.toDataStreamResponse();
41+ }
You can’t perform that action at this time.
0 commit comments