From 87ced25580c831732cb77fbd80851058766aa43b Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Wed, 15 Nov 2023 21:17:35 +0000 Subject: [PATCH] add the API key input --- app/api/toHtml/route.ts | 4 +-- app/components/APIKeyInput.tsx | 44 +++++++++++++++++++++++++++++++++ app/components/ExportButton.tsx | 6 +++++ app/page.tsx | 2 ++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/components/APIKeyInput.tsx diff --git a/app/api/toHtml/route.ts b/app/api/toHtml/route.ts index b0fd8d5..4dc272a 100644 --- a/app/api/toHtml/route.ts +++ b/app/api/toHtml/route.ts @@ -14,7 +14,7 @@ Use JavaScript modules and unkpkg to import any necessary dependencies. Respond ONLY with the contents of the html file.` export async function POST(request: Request) { - const { image, html } = await request.json() + const { image, html, apiKey } = await request.json() const body: GPT4VCompletionRequest = { model: 'gpt-4-vision-preview', max_tokens: 4096, @@ -53,7 +53,7 @@ export async function POST(request: Request) { method: 'POST', headers: { 'Content-Type': 'application/json', - Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, + Authorization: `Bearer ${apiKey ?? process.env.OPENAI_API_KEY}`, }, body: JSON.stringify(body), }) diff --git a/app/components/APIKeyInput.tsx b/app/components/APIKeyInput.tsx new file mode 100644 index 0000000..b337bdc --- /dev/null +++ b/app/components/APIKeyInput.tsx @@ -0,0 +1,44 @@ +import { Input } from '@tldraw/tldraw' + +export function APIKeyInput() { + return ( +
+
+
+ Have your own OpenAI API key? Use it instead. (Risky but cool) +
+ +
+
+ ) +} diff --git a/app/components/ExportButton.tsx b/app/components/ExportButton.tsx index 2e491b2..3d55c6f 100644 --- a/app/components/ExportButton.tsx +++ b/app/components/ExportButton.tsx @@ -83,6 +83,12 @@ export function ExportButton() { body: JSON.stringify({ image: dataUrl, html: previousHtml, + apiKey: + ( + document.getElementById( + 'openai_key_risky_but_cool' + ) as HTMLInputElement + )?.value ?? null, }), }) diff --git a/app/page.tsx b/app/page.tsx index 493d513..2927c60 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -8,6 +8,7 @@ import React, { useEffect, useState } from 'react' import { PreviewShapeUtil } from './PreviewShape/PreviewShape' import { ExportButton } from './components/ExportButton' import { useDialogs, useEditor } from '@tldraw/tldraw' +import { APIKeyInput } from './components/APIKeyInput' const Tldraw = dynamic(async () => (await import('@tldraw/tldraw')).Tldraw, { ssr: false, @@ -42,6 +43,7 @@ export default function Home() { {/* */} +