Skip to content

Commit 322205d

Browse files
yardend-wixclaude
andcommitted
docs(ai-gateway): show a real code-agent example (tool loop), not a raw fetch
Replace the bare fetch example with a compact ToolLoopAgent — a background action triggered on a record, with tools that carry input schemas and a finish tool — matching the SDK's house example style (no Deno/npm: scaffolding). Keeps the "point any OpenAI-compatible client at baseURL/token" note. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent ee4d816 commit 322205d

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

src/modules/ai-gateway.types.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,41 @@ export interface AiGatewayModule {
4949
*
5050
* @example
5151
* ```typescript
52-
* // Inside a backend function: hand the connection to any OpenAI-compatible client
53-
* import { createClientFromRequest } from 'npm:@base44/sdk';
52+
* import { ToolLoopAgent, tool, stepCountIs, hasToolCall } from "ai";
53+
* import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
54+
* import { z } from "zod";
5455
*
55-
* Deno.serve(async (req) => {
56-
* const base44 = createClientFromRequest(req);
57-
* const { baseURL, token } = base44.aiGateway.connection();
56+
* const request = await base44.entities.ReturnRequest.get(returnId);
57+
* const { baseURL, token } = base44.aiGateway.connection();
58+
* // Point any OpenAI-compatible client at `baseURL` with `apiKey: token`.
59+
* const models = createOpenAICompatible({ name: "base44", baseURL, apiKey: token });
5860
*
59-
* // Point any OpenAI-compatible client at `baseURL` with `apiKey: token`.
60-
* // Shown here with a raw request to the Chat Completions endpoint:
61-
* const res = await fetch(`${baseURL}/chat/completions`, {
62-
* method: 'POST',
63-
* headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
64-
* body: JSON.stringify({
65-
* model: 'claude_sonnet_4_6',
66-
* messages: [{ role: 'user', content: 'Hello!' }],
61+
* const agent = new ToolLoopAgent({
62+
* model: models("automatic"),
63+
* instructions:
64+
* "Decide whether this return looks fine or needs the owner's attention. " +
65+
* "Check the customer's past orders, then submit your verdict.",
66+
* tools: {
67+
* searchOrders: tool({
68+
* description: "This customer's past orders, optionally filtered by status",
69+
* inputSchema: z.object({ status: z.string().optional() }),
70+
* execute: ({ status }) => {
71+
* const query = { customer_email: request.customer_email };
72+
* if (status) query.status = status;
73+
* return base44.entities.Order.filter(query, "-created_date", 50);
74+
* },
6775
* }),
68-
* });
69-
* const data = await res.json();
70-
* return Response.json({ text: data.choices[0].message.content });
76+
* submitVerdict: tool({
77+
* description: "Record the final verdict",
78+
* inputSchema: z.object({ decision: z.enum(["approved", "flagged"]), reason: z.string() }),
79+
* execute: ({ decision, reason }) =>
80+
* base44.entities.ReturnRequest.update(returnId, { status: decision, review_note: reason }),
81+
* }),
82+
* },
83+
* stopWhen: [stepCountIs(8), hasToolCall("submitVerdict")],
7184
* });
85+
*
86+
* await agent.generate({ prompt: `Review this return request: ${JSON.stringify(request)}` });
7287
* ```
7388
*/
7489
connection(): AiGatewayConnection;

0 commit comments

Comments
 (0)