Skip to content

Commit ae8e8bd

Browse files
committed
docs(showcase/langroid): tool-rendering regions + open-gen-ui canonical names
- tool-rendering: @region[render-weather-tool] on page.tsx; @region[weather-tool-backend] on src/agents/agent.py (the GetWeatherTool ToolMessage class). New render-flight-tool.snippet.tsx sibling for @region[render-flight-tool] + @region[catchall-renderer]. - open-gen-ui: renamed legacy @region[ogui-runtime-flag] to canonical @region[minimal-runtime-flag] + @region[advanced-runtime-config] in copilotkit-ogui/route.ts (no behavior change; aligns with other frameworks). Manifest highlight updated to point at the real backend src/agents/agent.py (replacing the stub demo agent.py reference).
1 parent a130634 commit ae8e8bd

5 files changed

Lines changed: 96 additions & 3 deletions

File tree

showcase/integrations/langroid/manifest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ demos:
130130
route: /demos/tool-rendering
131131
animated_preview_url:
132132
highlight:
133-
- src/app/demos/tool-rendering/agent.py
133+
- src/agents/agent.py
134134
- src/app/demos/tool-rendering/page.tsx
135135
- src/app/api/copilotkit/route.ts
136136
- id: gen-ui-tool-based

showcase/integrations/langroid/src/agents/agent.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ def _tool_error(*, error: _ToolErrorKind, message: str) -> str:
607607
return _json_dumps({"error": error.value, "message": message})
608608

609609

610+
# @region[weather-tool-backend]
610611
class GetWeatherTool(ToolMessage):
611612
request: str = "get_weather"
612613
purpose: str = "Get current weather for a location."
@@ -622,6 +623,7 @@ def handle(self) -> str:
622623
error=_ToolErrorKind.GET_WEATHER_FAILED,
623624
message=f"{exc.__class__.__name__}: {str(exc)[:200]}",
624625
)
626+
# @endregion[weather-tool-backend]
625627

626628

627629
class QueryDataTool(ToolMessage):

showcase/integrations/langroid/src/app/api/copilotkit-ogui/route.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export const POST = async (req: NextRequest) => {
2424
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
2525
endpoint: "/api/copilotkit-ogui",
2626
serviceAdapter: new ExperimentalEmptyAdapter(),
27-
// @region[ogui-runtime-flag]
27+
// @region[minimal-runtime-flag]
28+
// @region[advanced-runtime-config]
2829
// Server-side config is identical for the minimal and advanced cells —
2930
// the advanced behaviour (sandbox -> host function calls) is wired
3031
// entirely on the frontend via `openGenerativeUI.sandboxFunctions` on
@@ -39,7 +40,8 @@ export const POST = async (req: NextRequest) => {
3940
agents: ["open-gen-ui", "open-gen-ui-advanced"],
4041
},
4142
}),
42-
// @endregion[ogui-runtime-flag]
43+
// @endregion[advanced-runtime-config]
44+
// @endregion[minimal-runtime-flag]
4345
});
4446
return await handleRequest(req);
4547
} catch (error: unknown) {

showcase/integrations/langroid/src/app/demos/tool-rendering/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default function ToolRenderingDemo() {
2727
}
2828

2929
function Chat() {
30+
// @region[render-weather-tool]
3031
useRenderTool({
3132
name: "get_weather",
3233
parameters: z.object({
@@ -61,6 +62,7 @@ function Chat() {
6162
);
6263
},
6364
});
65+
// @endregion[render-weather-tool]
6466

6567
useConfigureSuggestions({
6668
suggestions: [
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Docs-only snippet — not imported or rendered. Langroid's tool-rendering
2+
// demo at page.tsx exercises the get_weather renderer only; the docs
3+
// page at /generative-ui/tool-rendering also teaches the search_flights
4+
// per-tool pattern and the wildcard catch-all. These two regions show
5+
// what those would look like in the same Langroid demo shape, so the
6+
// docs can render real teaching code rather than a missing-snippet box.
7+
//
8+
// See chat-component.snippet.tsx in agentic-chat for the same pattern.
9+
10+
import { useRenderTool, useDefaultRenderTool } from "@copilotkit/react-core/v2";
11+
import { z } from "zod";
12+
13+
type CatchallToolStatus = "in_progress" | "complete" | "error";
14+
15+
interface FlightSearchResult {
16+
origin?: string;
17+
destination?: string;
18+
flights?: unknown[];
19+
}
20+
21+
function FlightListCard(_props: {
22+
loading: boolean;
23+
origin: string;
24+
destination: string;
25+
flights: unknown[];
26+
}) {
27+
return null;
28+
}
29+
30+
function CustomCatchallRenderer(_props: {
31+
name: string;
32+
parameters: unknown;
33+
status: CatchallToolStatus;
34+
result: unknown;
35+
}) {
36+
return null;
37+
}
38+
39+
function parseJsonResult<T>(_result: unknown): T {
40+
return {} as T;
41+
}
42+
43+
export function FlightToolRenderers() {
44+
// @region[render-flight-tool]
45+
// Per-tool renderer: search_flights → branded FlightListCard.
46+
useRenderTool(
47+
{
48+
name: "search_flights",
49+
parameters: z.object({
50+
origin: z.string(),
51+
destination: z.string(),
52+
}),
53+
render: ({ parameters, result, status }) => {
54+
const loading = status !== "complete";
55+
const parsed = parseJsonResult<FlightSearchResult>(result);
56+
return (
57+
<FlightListCard
58+
loading={loading}
59+
origin={parameters?.origin ?? parsed.origin ?? ""}
60+
destination={parameters?.destination ?? parsed.destination ?? ""}
61+
flights={parsed.flights ?? []}
62+
/>
63+
);
64+
},
65+
},
66+
[],
67+
);
68+
// @endregion[render-flight-tool]
69+
70+
// @region[catchall-renderer]
71+
// Wildcard catch-all for every remaining tool — anything the agent might
72+
// call that doesn't have a dedicated useRenderTool registration.
73+
useDefaultRenderTool(
74+
{
75+
render: ({ name, parameters, status, result }) => (
76+
<CustomCatchallRenderer
77+
name={name}
78+
parameters={parameters}
79+
status={status as CatchallToolStatus}
80+
result={result}
81+
/>
82+
),
83+
},
84+
[],
85+
);
86+
// @endregion[catchall-renderer]
87+
}

0 commit comments

Comments
 (0)