Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.8.0",
"react-toastify": "^10.0.4",
"semantic-ui-css": "^2.5.0",
"semantic-ui-react": "^2.1.4",
"tar": "^6.1.13",
"uuidv4": "^6.2.13",
"vite-plugin-externals": "^0.6.2",
"vite-plugin-node-polyfills": "^0.7.0",
"vscode-json-languageservice": "^5.3.2",
Expand Down
14 changes: 11 additions & 3 deletions apps/playground/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ import { Header } from "./Header";
import { useTimeout } from "usehooks-ts";
import { TooSlowAlert } from "@wing-playground/shared/src/alerts/TooSlow";
import { ServerErrorAlert } from "@wing-playground/shared/src/alerts/ServerError";
import {ServerDown} from "@wing-playground/shared/src/alerts/ServerDown";
import { ServerDown } from "@wing-playground/shared/src/alerts/ServerDown";
import { AiInput } from "./ai";

const wingPackageJson = await import(
"@winglang/compiler/package.json?raw"
Expand Down Expand Up @@ -222,6 +223,10 @@ export const ReactMonacoEditor: React.FC<EditorProps> = ({}) => {
}
}, [languageContext]);

const setEditorValue = (code: string) => {
editorRef.current?.setValue(code);
};

const simulatorTarget: TargetView = useMemo(() => {
return {
id: "simulator",
Expand Down Expand Up @@ -264,7 +269,7 @@ export const ReactMonacoEditor: React.FC<EditorProps> = ({}) => {

const getAlert = () => {
if (IS_SERVER_DOWN) {
return ServerDown;
return ServerDown;
}
if (tooSlow) {
return TooSlowAlert;
Expand Down Expand Up @@ -340,7 +345,10 @@ export const ReactMonacoEditor: React.FC<EditorProps> = ({}) => {
</select>
</div>
</PanelHeader>

<AiInput
onAiAnswer={setEditorValue}
code={editorRef.current?.getValue()}
/>
<Editor
theme={currentMode}
options={editorOptions}
Expand Down
44 changes: 44 additions & 0 deletions apps/playground/src/ai/ctas.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export const Input = (props: {
value: string;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
placeholder: string;
}) => (
<input
{...props}
className="border-slate-300 dark:border-slate-800 items-center px-2.5 py-1.5 border text-md rounded outline-none shadow-inner bg-white dark:bg-slate-800 text-slate-900 placeholder:text-slate-500 dark:text-slate-300 dark:placeholder:text-slate-500 focus:ring-2 focus:ring-sky-500/50 focus:border-sky-500 outline-none w-full"
/>
);

export const Button = (props: {
onClick: (e: React.MouseEvent<HTMLButtonElement>) => void;
isLoading?: boolean;
children: any;
}) => (
<button
{...props}
type="button"
disabled={props.isLoading}
className="inline-flex gap-2 items-center text-sm font-medium outline-none rounded focus:ring-2 focus:ring-sky-500/50 focus:border-sky-500 outline-none bg-white dark:bg-slate-800 hover:bg-slate-50 dark:hover:bg-slate-750 text-slate-900 placeholder:text-slate-500 dark:text-slate-300 dark:placeholder:text-slate-500 border shadow-sm border-slate-300 dark:border-slate-800 px-2.5 py-1.5 min-w-fit"
>
{props.isLoading && (
<svg
aria-hidden="true"
role="status"
className="inline w-4 h-4 text-slate-900 dark:text-slate-300 animate-spin"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="#E5E7EB"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentColor"
/>
</svg>
)}
{props.children}
</button>
);
33 changes: 33 additions & 0 deletions apps/playground/src/ai/icons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const AiIcon = ({ className = "" }: { className?: string }) => (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className={"w-6 h-6 " + className}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9.813 15.904 9 18.75l-.813-2.846a4.5 4.5 0 0 0-3.09-3.09L2.25 12l2.846-.813a4.5 4.5 0 0 0 3.09-3.09L9 5.25l.813 2.846a4.5 4.5 0 0 0 3.09 3.09L15.75 12l-2.846.813a4.5 4.5 0 0 0-3.09 3.09ZM18.259 8.715 18 9.75l-.259-1.035a3.375 3.375 0 0 0-2.455-2.456L14.25 6l1.036-.259a3.375 3.375 0 0 0 2.455-2.456L18 2.25l.259 1.035a3.375 3.375 0 0 0 2.456 2.456L21.75 6l-1.035.259a3.375 3.375 0 0 0-2.456 2.456ZM16.894 20.567 16.5 21.75l-.394-1.183a2.25 2.25 0 0 0-1.423-1.423L13.5 18.75l1.183-.394a2.25 2.25 0 0 0 1.423-1.423l.394-1.183.394 1.183a2.25 2.25 0 0 0 1.423 1.423l1.183.394-1.183.394a2.25 2.25 0 0 0-1.423 1.423Z"
/>
</svg>
);

export const FixIcon = ({ className = "" }: { className?: string }) => (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className={"w-4 h-4 " + className}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M11.42 15.17 17.25 21A2.652 2.652 0 0 0 21 17.25l-5.877-5.877M11.42 15.17l2.496-3.03c.317-.384.74-.626 1.208-.766M11.42 15.17l-4.655 5.653a2.548 2.548 0 1 1-3.586-3.586l6.837-5.63m5.108-.233c.55-.164 1.163-.188 1.743-.14a4.5 4.5 0 0 0 4.486-6.336l-3.276 3.277a3.004 3.004 0 0 1-2.25-2.25l3.276-3.276a4.5 4.5 0 0 0-6.336 4.486c.091 1.076-.071 2.264-.904 2.95l-.102.085m-1.745 1.437L5.909 7.5H4.5L2.25 3.75l1.5-1.5L7.5 4.5v1.409l4.26 4.26m-1.745 1.437 1.745-1.437m6.615 8.206L15.75 15.75M4.867 19.125h.008v.008h-.008v-.008Z"
/>
</svg>
);
138 changes: 138 additions & 0 deletions apps/playground/src/ai/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { uuid } from "uuidv4";
import { useEffect, useState } from "react";
import { AiIcon, FixIcon } from "./icons";
import { Button, Input } from "./ctas";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";

const formatCode = (code: string) => {
let isCode = false;
return code.split("\n").reduce((acc, line) => {
if (line.includes("```")) {
isCode = !isCode;

const newLine = line.replace(/```wing/g, "").replace(/```/g, "");
return acc ? [acc, newLine].join("\n") : newLine;
}
const nextLine = isCode ? line : `// ${line}`;
return acc ? [acc, nextLine].join("\n") : nextLine;
}, "");
};

const AI_API_URL =
"https://bxsblg1sf2.execute-api.us-east-1.amazonaws.com/prod";

const LOADING_STATES = {
GENERATE: "GENERATE",
FIX: "FIX",
NONE: "",
};

export const AiInput = ({
onAiAnswer,
code = "",
}: {
onAiAnswer: (prompt: string) => void;
code?: string;
}) => {
const [prompt, setPrompt] = useState("");
const [isLoading, setIsLoading] = useState(LOADING_STATES.NONE);

useEffect(() => {
if (sessionStorage.getItem("conversation-id") === null) {
sessionStorage.setItem("conversation-id", uuid());
}
}, []);

const askAi = async () => {
setIsLoading(LOADING_STATES.GENERATE);
try {
const res = await fetch(`${AI_API_URL}/generate`, {
method: "POST",
body: JSON.stringify({ prompt, code }),
headers: {
"conversation-id": sessionStorage.getItem("conversation-id")!,
"Access-Control-Allow-Origin": "http://localhost:5173",
"Access-Control-Allow-Credentials": "true",
},
});
if (res.ok) {
onAiAnswer(formatCode(await res.text()));
}
setPrompt("");
} catch (error) {
toast.error((error as Error).message);
} finally {
setIsLoading(LOADING_STATES.NONE);
}
};

const fixCode = async () => {
try {
setIsLoading(LOADING_STATES.FIX);
const res = await fetch(`${AI_API_URL}/fix`, {
method: "POST",
body: JSON.stringify({ code }),
headers: {
"conversation-id": sessionStorage.getItem("conversation-id")!,
"Access-Control-Allow-Origin": "http://localhost:5173",
"Access-Control-Allow-Credentials": "true",
},
});
if (res.ok) {
onAiAnswer(formatCode(await res.text()));
}
} catch (error) {
toast.error((error as Error).message);
} finally {
setIsLoading(LOADING_STATES.NONE);
}
};

const restart = () => {
sessionStorage.setItem("conversation-id", uuid());

setPrompt("");
onAiAnswer(
"// Write something down to get started, or use the examples above...",
);
};

return (
<div className="p-4 bg-slate-100 dark:bg-slate-700 relative">
<div className="flex gap-1">
<h3 className="text-slate-700 dark:text-slate-500 font-semibold my-2 ">
Use wingAi
</h3>
<AiIcon className="stroke-slate-600 dark:stroke-slate-500" />
</div>

<div className="flex gap-4">
<div className="h-full border-r-2 dark:border-r-slate-800 border-r-slate-400 w-1/6">
<Button
onClick={fixCode}
isLoading={isLoading === LOADING_STATES.FIX}
>
Fix <FixIcon />
</Button>
</div>

<Input
value={prompt}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setPrompt(e.target.value)
}
placeholder="Or ask me to build something new..."
/>
<Button
onClick={askAi}
isLoading={isLoading === LOADING_STATES.GENERATE}
>
Generate wing code
</Button>
<Button onClick={restart}>Restart</Button>
<ToastContainer />
</div>
</div>
);
};
2 changes: 1 addition & 1 deletion apps/playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"include": ["src/**/*.tsx"],
"references": [{ "path": "./tsconfig.node.json" }]
}
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.