-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add puck ai plugin #1022
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
Open
benlife5
wants to merge
24
commits into
puck-ai
Choose a base branch
from
basic-puck-setup
base: puck-ai
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add puck ai plugin #1022
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
eb0b10a
basic setup
benlife5 275f7fb
Automated update to THIRD-PARTY-NOTICES from github action's 3rd part…
github-actions[bot] 30dda3d
docs: auto-generate component documentation
github-actions[bot] fb7860c
revert starter changes
benlife5 22b3b76
add local dev server
benlife5 26d8cec
Update component screenshots for visual-editor
github-actions[bot] 106c7ed
package updates
benlife5 42f2f92
docs: auto-generate component documentation
github-actions[bot] 173f569
rabbit updates
benlife5 1d26405
Update component screenshots for visual-editor
github-actions[bot] 2475e4d
Update component screenshots for visual-editor
github-actions[bot] c5b3b55
streaming + error handling
benlife5 9151037
more css updates; memoize plugin
benlife5 f63755b
Update component screenshots for visual-editor
github-actions[bot] ccc8a43
Update component screenshots for visual-editor
github-actions[bot] 4a40a1e
fn name
benlife5 435f86e
Update component screenshots for visual-editor
github-actions[bot] a0b7fb2
rework as cloudflare worker with hono backend
benlife5 54b304a
docs: auto-generate component documentation
github-actions[bot] bdc34c4
dev:ai script
benlife5 f18c61e
remove old ai attempt
benlife5 7a0a106
robot comments
benlife5 59e39a2
fix css; rename files; fix error message
benlife5 cb30e0e
upgrade to puck ai 0.6.0
benlife5 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # wrangler project | ||
|
|
||
| .dev.vars* | ||
| !.dev.vars.example | ||
| .env* | ||
| !.env.example | ||
| .wrangler/ | ||
|
|
||
| worker-configuration.d.ts | ||
|
|
||
| # IDE | ||
| .vscode/** |
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,22 @@ | ||
| { | ||
| "name": "puck-ai-backend", | ||
| "version": "0.0.0", | ||
| "author": "sumo@yext.com", | ||
| "private": true, | ||
| "packageManager": "pnpm@9.12.3", | ||
| "scripts": { | ||
| "deploy": "wrangler deploy", | ||
| "dev": "wrangler dev", | ||
| "start": "wrangler dev", | ||
| "cf-typegen": "wrangler types" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^25.2.1", | ||
| "typescript": "^5.5.2", | ||
| "wrangler": "^4.63.0" | ||
| }, | ||
| "dependencies": { | ||
| "@puckeditor/cloud-client": "0.6.0", | ||
| "hono": "^4.6.12" | ||
| } | ||
| } | ||
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,53 @@ | ||
| import type { MiddlewareHandler } from "hono"; | ||
|
|
||
| const isLocalHost = (host: string) => | ||
| host === "localhost" || host === "127.0.0.1" || host === "0.0.0.0"; | ||
|
|
||
| const isAllowedOrigin = (origin: string, isLocalServer: boolean) => { | ||
| try { | ||
| const url = new URL(origin); | ||
| const host = url.hostname.toLowerCase(); | ||
| if (host.endsWith(".preview.pagescdn.com")) { | ||
| return true; | ||
| } | ||
| if (isLocalServer && isLocalHost(host)) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| export const corsForPuck: MiddlewareHandler = async (c, next) => { | ||
| const origin = c.req.header("Origin"); | ||
| const hostHeader = c.req.header("Host") ?? ""; | ||
| const requestHost = hostHeader.split(":")[0].toLowerCase(); | ||
| const isLocalServer = isLocalHost(requestHost); | ||
| const allowed = origin ? isAllowedOrigin(origin, isLocalServer) : false; | ||
|
|
||
| if (c.req.method === "OPTIONS") { | ||
| if (allowed && origin) { | ||
| c.header("Access-Control-Allow-Origin", origin); | ||
| c.header("Vary", "Origin"); | ||
| c.header("Access-Control-Allow-Methods", "POST, OPTIONS"); | ||
| const requestedHeaders = c.req.header("Access-Control-Request-Headers"); | ||
| if (requestedHeaders) { | ||
| c.header("Access-Control-Allow-Headers", requestedHeaders); | ||
| } | ||
| } | ||
| return c.body(null, 204); | ||
| } | ||
|
|
||
| await next(); | ||
|
|
||
| if (allowed && origin) { | ||
| c.res.headers.set("Access-Control-Allow-Origin", origin); | ||
| c.res.headers.set("Vary", "Origin"); | ||
| c.res.headers.set("Access-Control-Allow-Methods", "POST, OPTIONS"); | ||
| const requestedHeaders = c.req.header("Access-Control-Request-Headers"); | ||
| if (requestedHeaders) { | ||
| c.res.headers.set("Access-Control-Allow-Headers", requestedHeaders); | ||
| } | ||
| } | ||
| }; |
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,12 @@ | ||
| import { Hono } from "hono"; | ||
| import { corsForPuck } from "./cors"; | ||
| import { puckRouter } from "./puckRouter"; | ||
|
|
||
| const app = new Hono(); | ||
|
|
||
| app.get("/", (c) => c.text("Yext Pages Puck AI Backend is running.")); | ||
|
|
||
| app.use("/api/puck/*", corsForPuck); | ||
| app.post("/api/puck/:route", puckRouter); | ||
|
|
||
| export default app; |
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,87 @@ | ||
| import { puckHandler, ChatParams } from "@puckeditor/cloud-client"; | ||
| import type { Context } from "hono"; | ||
|
|
||
| type CustomChatParams = ChatParams & { systemPrompt: string }; | ||
|
|
||
| const parseBody = async (c: Context): Promise<CustomChatParams | undefined> => { | ||
| const contentType = c.req.header("content-type") ?? ""; | ||
| if (contentType.includes("application/json")) { | ||
| try { | ||
| return await c.req.json(); | ||
| } catch { | ||
| return; | ||
| } | ||
| } | ||
| if ( | ||
| contentType.includes("application/x-www-form-urlencoded") || | ||
| contentType.includes("multipart/form-data") | ||
| ) { | ||
| try { | ||
| const parsedBody = await c.req.parseBody(); | ||
| if (typeof parsedBody === "object" && "chatId" in parsedBody) { | ||
| return parsedBody as unknown as CustomChatParams; | ||
| } | ||
| return; | ||
| } catch { | ||
| return; | ||
| } | ||
| } | ||
benlife5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| }; | ||
|
|
||
| /** Accept the request from the editor, forward it to Puck, and stream the response. */ | ||
| export const puckRouter = async (c: Context) => { | ||
| try { | ||
| const puckApiKey = c.env.PUCK_API_KEY; | ||
| if (!puckApiKey) { | ||
| return c.json({ success: false, error: "Missing PUCK_API_KEY" }, 500); | ||
| } | ||
|
|
||
| const origin = c.req.header("Origin"); | ||
| if (!origin) { | ||
| return c.json({ success: false, error: "Missing Origin header" }, 400); | ||
| } | ||
| const pathname = `/api/puck/${c.req.param("route") ?? ""}`.replace( | ||
| /\/+$/, | ||
| "", | ||
| ); | ||
| const url = new URL(pathname, origin); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const body = await parseBody(c); | ||
|
|
||
| if (!body) { | ||
| return c.json({ success: false, error: "Invalid request body" }, 400); | ||
| } | ||
|
|
||
| const puckRequest = new Request(url, { | ||
| method: "POST", | ||
| body: JSON.stringify(body), | ||
| headers: { "content-type": "application/json" }, | ||
| }); | ||
benlife5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const puckResponse = await puckHandler(puckRequest, { | ||
| ai: { context: body.systemPrompt }, | ||
| apiKey: puckApiKey, | ||
| }); | ||
|
|
||
| const isServerSideEvents = ( | ||
| puckResponse.headers.get("content-type") ?? "" | ||
| ).includes("text/event-stream"); | ||
| if (!isServerSideEvents) { | ||
| return puckResponse; | ||
| } | ||
|
|
||
| const headers = new Headers(puckResponse.headers); | ||
| headers.set("Cache-Control", "no-cache"); | ||
| headers.set("Connection", "keep-alive"); | ||
|
|
||
| return new Response(puckResponse.body, { | ||
| status: puckResponse.status, | ||
| statusText: puckResponse.statusText, | ||
| headers, | ||
| }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return c.json({ success: false, error: "Failed to process request" }, 500); | ||
| } | ||
| }; | ||
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,20 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "es2024", | ||
| "lib": ["es2024"], | ||
| "jsx": "react-jsx", | ||
| "module": "es2022", | ||
| "moduleResolution": "Bundler", | ||
| "resolveJsonModule": true, | ||
| "allowJs": true, | ||
| "checkJs": false, | ||
| "noEmit": true, | ||
| "isolatedModules": true, | ||
| "allowSyntheticDefaultImports": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "types": ["./worker-configuration.d.ts", "node"] | ||
| }, | ||
| "include": ["worker-configuration.d.ts", "src/**/*.ts"] | ||
| } |
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,14 @@ | ||
| /** | ||
| * For more details on how to configure Wrangler, refer to: | ||
| * https://developers.cloudflare.com/workers/wrangler/configuration/ | ||
| */ | ||
| { | ||
| "$schema": "node_modules/wrangler/config-schema.json", | ||
| "name": "puck-ai-backend", | ||
| "main": "src/index.ts", | ||
| "compatibility_date": "2025-09-27", | ||
| "observability": { | ||
| "enabled": true, | ||
| }, | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| } |
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
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.