Repository navigation
feat: wrap up and enhanced canvas page #2302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
smakosh
merged 5 commits into
theopenco:main
from
amelitoalcuitas:feat/playground-canvas-mode
May 16, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9505bfd
feat(playground): add canvas mode WIP
smakosh c008b7f
feat: wrap up and enhanced canvas page
amelitoalcuitas 6e5a0f8
feat: improve error handling for canvas generation and refine organiz…
amelitoalcuitas 02765c6
Merge branch 'main' into feat/playground-canvas-mode
amelitoalcuitas 05a904f
Merge branch 'main' into feat/playground-canvas-mode
smakosh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { streamText } from "ai"; | ||
| import { cookies } from "next/headers"; | ||
|
|
||
| import { catalog } from "@/lib/canvas/catalog"; | ||
| import { getUser } from "@/lib/getUser"; | ||
|
|
||
| import { createLLMGateway } from "@llmgateway/ai-sdk-provider"; | ||
|
|
||
| export const maxDuration = 300; | ||
|
|
||
| interface CanvasGenerateBody { | ||
| prompt: string; | ||
| model?: string; | ||
| } | ||
|
|
||
| export async function POST(req: Request) { | ||
| const user = await getUser(); | ||
|
|
||
| if (!user) { | ||
| return new Response(JSON.stringify({ error: "Unauthorized" }), { | ||
| status: 401, | ||
| }); | ||
| } | ||
|
|
||
| let body: CanvasGenerateBody; | ||
| try { | ||
| body = (await req.json()) as CanvasGenerateBody; | ||
| } catch { | ||
| return new Response(JSON.stringify({ error: "Invalid JSON" }), { | ||
| status: 400, | ||
| }); | ||
| } | ||
| const { prompt, model } = body; | ||
| const systemPrompt = catalog.prompt(); | ||
|
|
||
| if (!prompt) { | ||
| return new Response(JSON.stringify({ error: "Missing prompt" }), { | ||
| status: 400, | ||
| }); | ||
| } | ||
|
|
||
| const cookieStore = await cookies(); | ||
| const cookieApiKey = | ||
| cookieStore.get("llmgateway_playground_key")?.value ?? | ||
| cookieStore.get("__Host-llmgateway_playground_key")?.value; | ||
|
|
||
| if (!cookieApiKey) { | ||
| return new Response(JSON.stringify({ error: "Missing API key" }), { | ||
| status: 400, | ||
| }); | ||
| } | ||
|
|
||
| const gatewayUrl = | ||
| process.env.GATEWAY_URL ?? | ||
| (process.env.NODE_ENV === "development" | ||
| ? "http://localhost:4001/v1" | ||
| : "https://api.llmgateway.io/v1"); | ||
|
|
||
| const llmgateway = createLLMGateway({ | ||
| apiKey: cookieApiKey, | ||
| baseURL: gatewayUrl, | ||
| headers: { | ||
| "x-source": "chat.llmgateway.io", | ||
| }, | ||
| }); | ||
|
|
||
| const selectedModel = model ?? "anthropic/claude-sonnet-4-20250514"; | ||
|
|
||
| const result = streamText({ | ||
| model: llmgateway.chat( | ||
| selectedModel as Parameters<typeof llmgateway.chat>[0], | ||
| ), | ||
| system: systemPrompt, | ||
| messages: [{ role: "user", content: prompt }], | ||
| }); | ||
|
|
||
| return result.toTextStreamResponse(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { cookies } from "next/headers"; | ||
| import { notFound } from "next/navigation"; | ||
|
|
||
| import { LastUsedProjectTracker } from "@/components/last-used-project-tracker"; | ||
| import CanvasPageClient from "@/components/playground/canvas-page-client"; | ||
| import { fetchModels, fetchProviders } from "@/lib/fetch-models"; | ||
| import { fetchServerData } from "@/lib/server-api"; | ||
|
|
||
| import type { Project, Organization } from "@/lib/types"; | ||
| import type { Metadata } from "next"; | ||
|
|
||
| export const metadata: Metadata = { | ||
| title: "Canvas | LLM Gateway", | ||
| description: | ||
| "Build UIs from JSON specs with live preview, PDF and image export.", | ||
| }; | ||
|
|
||
| export default async function CanvasPage({ | ||
| searchParams, | ||
| }: { | ||
| searchParams: Promise<{ orgId: string; projectId: string }>; | ||
| }) { | ||
| const { orgId, projectId } = await searchParams; | ||
|
|
||
| const [models, providers] = await Promise.all([ | ||
| fetchModels(), | ||
| fetchProviders(), | ||
| ]); | ||
|
|
||
| const initialOrganizationsData = await fetchServerData("GET", "/orgs"); | ||
|
|
||
| let initialProjectsData: { projects: Project[] } | null = null; | ||
| if (orgId) { | ||
| try { | ||
| initialProjectsData = (await fetchServerData( | ||
| "GET", | ||
| "/orgs/{id}/projects", | ||
| { | ||
| params: { | ||
| path: { | ||
| id: orgId, | ||
| }, | ||
| }, | ||
| }, | ||
| )) as { projects: Project[] }; | ||
| } catch (error) { | ||
| console.warn("Failed to fetch projects for organization:", orgId, error); | ||
| } | ||
| } | ||
|
|
||
| const organizations = ( | ||
| initialOrganizationsData && | ||
| typeof initialOrganizationsData === "object" && | ||
| "organizations" in initialOrganizationsData | ||
| ? (initialOrganizationsData as { organizations: Organization[] }) | ||
| .organizations | ||
| : [] | ||
| ) as Organization[]; | ||
| const selectedOrganization = | ||
| organizations.find((o) => o.id === orgId) ?? organizations[0] ?? null; | ||
|
|
||
| if (!initialProjectsData && selectedOrganization?.id) { | ||
| try { | ||
| initialProjectsData = (await fetchServerData( | ||
| "GET", | ||
| "/orgs/{id}/projects", | ||
| { | ||
| params: { | ||
| path: { | ||
| id: selectedOrganization.id, | ||
| }, | ||
| }, | ||
| }, | ||
| )) as { projects: Project[] }; | ||
| } catch (error) { | ||
| console.warn( | ||
| "Failed to fetch projects for organization:", | ||
| selectedOrganization?.id, | ||
| error, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const projects = (initialProjectsData?.projects ?? []) as Project[]; | ||
|
|
||
| let selectedProject: Project | null = null; | ||
| if (projectId) { | ||
| selectedProject = projects.find((p) => p.id === projectId) ?? null; | ||
| if (!selectedProject) { | ||
| notFound(); | ||
| } | ||
| } else if (selectedOrganization?.id) { | ||
| const cookieStore = await cookies(); | ||
| const cookieName = `llmgateway-last-used-project-${selectedOrganization.id}`; | ||
| const lastUsed = cookieStore.get(cookieName)?.value; | ||
| if (lastUsed) { | ||
| selectedProject = projects.find((p) => p.id === lastUsed) ?? null; | ||
| } | ||
| } | ||
| selectedProject ??= projects[0] ?? null; | ||
|
|
||
| return ( | ||
| <> | ||
| {selectedOrganization?.id && selectedProject?.id ? ( | ||
| <LastUsedProjectTracker | ||
| orgId={selectedOrganization.id} | ||
| projectId={selectedProject.id} | ||
| /> | ||
| ) : null} | ||
| <CanvasPageClient | ||
| models={models} | ||
| providers={providers} | ||
| organizations={organizations} | ||
| selectedOrganization={selectedOrganization} | ||
| projects={projects} | ||
| selectedProject={selectedProject} | ||
| /> | ||
| </> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.