-
Notifications
You must be signed in to change notification settings - Fork 513
CLI init #1242
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
Merged
CLI init #1242
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
384f219
cli onboarding
BilalG1 c8e76e9
Merge branch 'dev' into cli-onboarding
BilalG1 075b22e
Fix PR review feedback: prototype pollution, .env append, and app ID …
BilalG1 ccdc469
claude agent proxy, small fixes
BilalG1 7a394d2
Merge branch 'dev' into cli-onboarding
BilalG1 1edd51c
400 on invalid json
BilalG1 667b0ff
pr comment fixes
BilalG1 2adee15
Merge branch 'dev' into cli-onboarding
BilalG1 81a1edb
Merge branch 'dev' into cli-onboarding
BilalG1 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
68 changes: 68 additions & 0 deletions
68
apps/backend/src/app/api/latest/integrations/ai-proxy/[[...path]]/route.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,68 @@ | ||
| import { handleApiRequest } from "@/route-handlers/smart-route-handler"; | ||
| import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env"; | ||
| import { StatusError } from "@stackframe/stack-shared/dist/utils/errors"; | ||
| import { NextRequest } from "next/server"; | ||
|
|
||
| const OPENROUTER_BASE_URL = "https://openrouter.ai/api"; | ||
| const OPENROUTER_MODEL = "anthropic/claude-sonnet-4.6"; | ||
|
|
||
| function sanitizeBody(raw: ArrayBuffer): Uint8Array { | ||
| const text = new TextDecoder().decode(raw); | ||
| let parsed; | ||
| try { | ||
| parsed = JSON.parse(text); | ||
| } catch { | ||
| throw new StatusError(400, "Request body must be valid JSON"); | ||
| } | ||
|
|
||
| if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) { | ||
| throw new StatusError(400, "Request body must be a JSON object"); | ||
| } | ||
|
|
||
| parsed.model = OPENROUTER_MODEL; | ||
|
|
||
| // OpenRouter limits metadata.user_id to 128 characters | ||
| if (parsed.metadata?.user_id && parsed.metadata.user_id.length > 128) { | ||
| parsed.metadata.user_id = parsed.metadata.user_id.slice(0, 128); | ||
| } | ||
|
BilalG1 marked this conversation as resolved.
|
||
|
|
||
| return new TextEncoder().encode(JSON.stringify(parsed)); | ||
| } | ||
|
|
||
| async function proxyToOpenRouter(req: NextRequest, options: { params: Promise<{ path?: string[] }> }) { | ||
| const apiKey = getEnvVariable("STACK_OPENROUTER_API_KEY"); | ||
| const params = await options.params; | ||
| const subpath = params.path?.join("/") ?? ""; | ||
| const targetUrl = `${OPENROUTER_BASE_URL}/${subpath}${req.nextUrl.search}`; | ||
|
BilalG1 marked this conversation as resolved.
|
||
|
|
||
| const headers: Record<string, string> = { | ||
| "Authorization": `Bearer ${apiKey}`, | ||
| "anthropic-version": "2023-06-01", | ||
| }; | ||
|
|
||
| const contentType = req.headers.get("Content-Type"); | ||
| if (contentType) { | ||
| headers["Content-Type"] = contentType; | ||
| } | ||
|
|
||
| const body = req.method !== "GET" && req.method !== "HEAD" | ||
| ? Buffer.from(sanitizeBody(await req.arrayBuffer())) | ||
| : undefined; | ||
|
|
||
| const response = await fetch(targetUrl, { | ||
| method: req.method, | ||
| headers, | ||
| body, | ||
| }); | ||
|
|
||
| return new Response(response.body, { | ||
| status: response.status, | ||
| headers: { | ||
| "Content-Type": response.headers.get("Content-Type") ?? "application/json", | ||
| "Cache-Control": "no-cache", | ||
| }, | ||
| }); | ||
| } | ||
|
BilalG1 marked this conversation as resolved.
|
||
|
|
||
| export const GET = handleApiRequest(proxyToOpenRouter); | ||
| export const POST = handleApiRequest(proxyToOpenRouter); | ||
|
BilalG1 marked this conversation as resolved.
|
||
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.