The Open Standard for Generative UI
fix(templates): hook get_weather tool into the self-hosted chat route (#1288)
devin-ai-integration[bot]3ca2e2902f62
diff --git a/templates/openui-self-hosted/README.md b/templates/openui-self-hosted/README.mdindex 05a5bb1b2..e3f97b9db 100644--- a/templates/openui-self-hosted/README.md+++ b/templates/openui-self-hosted/README.md@@ -24,8 +24,7 @@ You can start editing the page by modifying `src/app/api/chat/route.ts` and imprby adding system prompts or tools. A LangGraph scaffold puts theimplementation in `src/agent/agent.ts` instead.-If you selected LangGraph, the Vercel AI SDK, or Vercel Eve, the generated app includes a `get_weather`-example. Ask “What’s the weather in Berlin?” to exercise its native tool loop.+The generated app includes a `get_weather` tool example. Ask “What’s the weather in Berlin?” to exercise its native tool loop.## Deploydiff --git a/templates/openui-self-hosted/src/app/api/chat/route.ts b/templates/openui-self-hosted/src/app/api/chat/route.tsindex f33e8690c..efe9ff8cd 100644--- a/templates/openui-self-hosted/src/app/api/chat/route.ts+++ b/templates/openui-self-hosted/src/app/api/chat/route.ts@@ -1,38 +1,97 @@import librarySpec from "@/generated/spec.json";import { promptOptions } from "@/lib/prompt-options";+import { getWeather, WEATHER_TOOL_DESCRIPTION } from "@/lib/tools/get-weather";import { generateSystemPrompt } from "@openuidev/lang-core";import OpenAI from "openai";import type { ChatCompletionMessageParam } from "openai/resources/chat/completions";const client = new OpenAI();+// Each thread's turns as runTools() produced them, tool calls and results+// included. Kept in memory: a restart or another server instance falls back+// to the browser's copy.+const threads = new Map<string, ChatCompletionMessageParam[]>();++// The browser keeps each tool call but never receives its result, so its copy+// of earlier turns is sent to the model as text only.+function withoutToolCalls(messages: ChatCompletionMessageParam[]): ChatCompletionMessageParam[] {+ return messages.flatMap((message): ChatCompletionMessageParam[] => {+ if (message.role === "tool") return [];+ if (message.role !== "assistant") return [message];+ if (!message.content) return [];+ return [{ role: "assistant", content: message.content }];+ });+}+export async function POST(req: Request) {try {- const { messages } = (await req.json()) as {+ const { threadId, messages } = (await req.json()) as {+ threadId?: string;messages: ChatCompletionMessageParam[];};+ const question = messages.slice(-1);+ const history = (threadId && threads.get(threadId)) || withoutToolCalls(messages.slice(0, -1));- // .asResponse() forwards the SDK's raw SSE response untouched — the client- // parses it with openAIAdapter().- return await client.chat.completions- .create(- {- model: process.env.OPENAI_MODEL ?? "gpt-5.2",- messages: [- {- role: "system",- content: generateSystemPrompt({- library: librarySpec,- promptOptions,- }),+ // runTools() calls the model, runs the tools it asks for, sends the+ // results back, and repeats until the model answers.+ const runner = client.chat.completions.runTools(+ {+ model: process.env.OPENAI_MODEL ?? "gpt-5.2",+ messages: [+ {+ role: "system",+ content: generateSystemPrompt({+ library: librarySpec,+ promptOptions,+ }),+ },+ ...history,+ ...question,+ ],+ tools: [+ {+ type: "function",+ function: {+ name: "get_weather",+ description: WEATHER_TOOL_DESCRIPTION,+ parameters: {+ type: "object",+ properties: {+ location: { type: "string", description: "City or place name, e.g. Berlin." },+ },+ required: ["location"],+ additionalProperties: false,+ },+ parse: (args: string) => JSON.parse(args) as { location: string },+ function: async ({ location }: { location: string }) =>+ JSON.stringify(await getWeather(location, { signal: req.signal })),},- ...messages,- ],- stream: true,- },- { signal: req.signal }, // propagate browser aborts- )- .asResponse();+ },+ ],+ stream: true,+ },+ { signal: req.signal, maxChatCompletions: 5 }, // propagate browser aborts+ );++ const turn = [...history, ...question];+ runner.on("message", (message) => turn.push(message));+ runner.done().then(+ () => {+ if (threadId) threads.set(threadId, turn);+ },+ () => {},+ );++ // NDJSON stream of every completion's chunks — the client parses it with+ // openAIReadableStreamAdapter().+ const stream = runner.toReadableStream();++ // Surface upstream failures (bad key, unknown model) as an HTTP error.+ await Promise.race([runner.emitted("chunk"), runner.done()]);++ return new Response(stream, {+ headers: { "Content-Type": "application/x-ndjson", "Cache-Control": "no-cache" },+ });} catch (err) {console.error(err);const message = err instanceof Error ? err.message : "Unknown error";diff --git a/templates/openui-self-hosted/src/app/page.tsx b/templates/openui-self-hosted/src/app/page.tsxindex 22e75119c..1fadebb91 100644--- a/templates/openui-self-hosted/src/app/page.tsx+++ b/templates/openui-self-hosted/src/app/page.tsx@@ -4,15 +4,15 @@ import "@openuidev/react-ui/styles/index.css";import {AgentInterface,fetchLLM,- openAIAdapter,openAIMessageFormat,+ openAIReadableStreamAdapter,useSystemThemeMode,} from "@openuidev/react-ui";import { openuiLibrary } from "@openuidev/react-ui/genui-lib";const llm = fetchLLM({url: "/api/chat",- streamAdapter: openAIAdapter(),+ streamAdapter: openAIReadableStreamAdapter(),messageFormat: openAIMessageFormat,});