Skip to content

Commit b09d5d7

Browse files
Create stripe
Update stripe
1 parent 9b84ef9 commit b09d5d7

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

stripe

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

0 commit comments

Comments
 (0)