ai-studio-cdk
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -117,87 +117,2 @@ "use strict"; | ||
}; | ||
document.addEventListener("keydown", (e) => { | ||
if (e.shiftKey && e.key === ":") { | ||
if (e.target?.closest("input,textarea")) return; | ||
e.preventDefault(); | ||
const modal = document.createElement("dialog"); | ||
modal.innerHTML = ` | ||
<form> | ||
<label for="cmd"><code>Command</code></label> | ||
<br/> | ||
<input id="cmd" type="text" /> | ||
<pre>[w]rite (filename)<br/>[e]dit [prompt]<br/>[o]pen</pre> | ||
<pre id="output"></pre> | ||
<button>Summit</button> | ||
</form> | ||
`; | ||
const form = modal.querySelector("form"); | ||
if (!form) throw new Error("No form found"); | ||
const cmdInput = form.querySelector("input"); | ||
if (!cmdInput) throw new Error("No input found"); | ||
const output = modal.querySelector("#output"); | ||
if (!output) throw new Error("No output found"); | ||
form.onsubmit = async (e2) => { | ||
e2.preventDefault(); | ||
const input = cmdInput.value.trim() ?? ""; | ||
if (input === "w" || input.startsWith("w ")) { | ||
const optionalFilename = input.slice(2).trim(); | ||
const downloadFilename = optionalFilename ? `${optionalFilename}.html` : `artifact-${(/* @__PURE__ */ new Date()).toISOString()}.html`; | ||
[...document.head.children].forEach((child) => cache.headChildren.includes(child) || child.remove()); | ||
document.body.innerHTML = cache.bodyInnerHTML; | ||
const thisHtmlFileContent = new Blob([document.documentElement.outerHTML], { type: "text/html" }); | ||
const downloadLink = document.createElement("a"); | ||
downloadLink.href = URL.createObjectURL(thisHtmlFileContent); | ||
downloadLink.download = downloadFilename; | ||
downloadLink.click(); | ||
location.reload(); | ||
} else if (input === "o") { | ||
} else if (input.startsWith("e ")) { | ||
output.textContent += "Authenticating...\n"; | ||
const [aid, openai] = await Promise.all([import("https://esm.sh/@azure/identity?bundle-deps"), import("https://esm.sh/openai?bundle-deps")]); | ||
output.textContent += "Generating...\n"; | ||
const credential = new aid.InteractiveBrowserCredential({ | ||
tenantId: "72f988bf-86f1-41af-91ab-2d7cd011db47", | ||
clientId: "6e4c509e-2b65-40dc-b461-beb2c824f63a" | ||
}); | ||
const azureADTokenProvider = aid.getBearerTokenProvider(credential, []); | ||
const client = new openai.AzureOpenAI({ azureADTokenProvider, apiVersion: "2024-07-01-preview", endpoint: "https://proto-api.azure-api.net/" }); | ||
const babelScript = document.querySelector("script[type='text/babel']"); | ||
if (!babelScript) throw new Error("No babel script found"); | ||
const currentScriptContent = babelScript.textContent ?? ""; | ||
const messages = [ | ||
{ | ||
role: "system", | ||
content: `Edit the following React program based on user's goal or instruction. | ||
<script type="text/jsx"> | ||
${currentScriptContent.trim()} | ||
<script> | ||
Think before you respond. Use this format: | ||
<thoughts>reason about the task...</thoughts> | ||
<script type="text/jsx"> | ||
Your implementation here... | ||
<\/script> | ||
`.trim() | ||
}, | ||
{ | ||
role: "user", | ||
content: input.slice(2) | ||
} | ||
]; | ||
const response = await client.chat.completions.create({ | ||
messages, | ||
model: "gpt-4o", | ||
max_tokens: 2e3 | ||
}).catch((e3) => e3); | ||
const dom = new DOMParser().parseFromString(response?.choices?.[0]?.message?.content, "text/html"); | ||
const newCode = dom.querySelector("script[type='text/jsx']")?.textContent; | ||
if (!newCode) throw new Error("No new code found"); | ||
location.assign(`?edit=${encodeURIComponent(newCode)}`); | ||
} | ||
}; | ||
document.body.appendChild(modal); | ||
modal.showModal(); | ||
} | ||
}); | ||
function safeAppendRootContent(content) { | ||
@@ -204,0 +119,0 @@ const root = document.getElementById("root"); |
{ | ||
"name": "ai-studio-cdk", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cdk.js", |
48906
385