Skip to content

Commit 6b59cda

Browse files
committed
feat: add frontend conponents for AI Assist
1 parent 016192d commit 6b59cda

20 files changed

+3813
-45
lines changed

app/.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Sway Playground Environment Configuration
2+
3+
# Server Configuration
4+
REACT_APP_SERVER_API=https://api.sway-playground.fuel.network
5+
REACT_APP_LOCAL_SERVER=false
6+
7+
REACT_APP_AI_FEATURES_ENABLED=false
8+
REACT_APP_AI_BACKEND_URL=http://localhost:3002
9+
10+
# Analytics
11+
REACT_APP_VERCEL_ANALYTICS=false

app/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24+
25+
.env

app/package-lock.json

Lines changed: 2573 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@fuel-ui/react": "^0.23.3",
1010
"@fuels/connectors": "0.5.0",
1111
"@fuels/react": "0.36.0",
12+
"@google/genai": "^1.5.1",
1213
"@mui/base": "^5.0.0-beta.2",
1314
"@mui/icons-material": "^5.11.16",
1415
"@mui/lab": "^5.0.0-alpha.46",
@@ -23,8 +24,11 @@
2324
"react": "^18.2.0",
2425
"react-ace": "^10.1.0",
2526
"react-dom": "^18.2.0",
27+
"react-markdown": "^10.1.0",
2628
"react-router-dom": "^6.23.0",
2729
"react-scripts": "^5.0.1",
30+
"react-syntax-highlighter": "^15.6.1",
31+
"remark": "^15.0.1",
2832
"typescript": "^5.4.5",
2933
"web-vitals": "^2.1.4"
3034
},
@@ -38,6 +42,7 @@
3842
"@types/node": "^16.18.32",
3943
"@types/react": "^18.2.6",
4044
"@types/react-dom": "^18.2.4",
45+
"@types/react-syntax-highlighter": "^15.5.13",
4146
"eslint-config-prettier": "^9.1.0",
4247
"eslint-plugin-prettier": "^5.1.3",
4348
"prettier": "^3.3.1",

app/src/App.tsx

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,61 +22,37 @@ import { useGist } from "./features/editor/hooks/useGist";
2222
import { useSearchParams } from "react-router-dom";
2323
import Copyable from "./components/Copyable";
2424
import useTheme from "./context/theme";
25+
import { AIGenerationDialog } from "./features/ai/components/AIGenerationDialog";
26+
import { AI_FEATURES_ENABLED } from "./constants";
2527

2628
const DRAWER_WIDTH = "40vw";
2729

2830
function App() {
29-
// The current sway code in the editor.
3031
const [swayCode, setSwayCode] = useState<string>(loadSwayCode());
31-
32-
// The current solidity code in the editor.
3332
const [solidityCode, setSolidityCode] = useState<string>(loadSolidityCode());
34-
35-
// An error message to display to the user.
3633
const [showSolidity, setShowSolidity] = useState(false);
37-
38-
// The most recent code that the user has requested to compile.
3934
const [codeToCompile, setCodeToCompile] = useState<string | undefined>(
4035
undefined,
4136
);
42-
43-
// The most recent code that the user has requested to transpile.
4437
const [codeToTranspile, setCodeToTranspile] = useState<string | undefined>(
4538
undefined,
4639
);
47-
48-
// Whether or not the current code in the editor has been compiled.
4940
const [isCompiled, setIsCompiled] = useState(false);
50-
51-
// The toolchain to use for compilation.
5241
const [toolchain, setToolchain] = useState<Toolchain>("testnet");
53-
54-
// The deployment state
5542
const [deployState, setDeployState] = useState(DeployState.NOT_DEPLOYED);
56-
57-
// Functions for reading and writing to the log output.
5843
const [log, updateLog] = useLog();
59-
60-
// The contract ID of the deployed contract.
6144
const [contractId, setContractId] = useState("");
62-
63-
// An error message to display to the user.
6445
const [drawerOpen, setDrawerOpen] = useState(false);
65-
66-
// The query parameters for the current URL.
6746
const [searchParams] = useSearchParams();
68-
69-
// The theme color for the app.
7047
const { themeColor } = useTheme();
48+
const [aiDialogOpen, setAiDialogOpen] = useState(false);
7149

72-
// If showSolidity is toggled on, reset the compiled state.
7350
useEffect(() => {
7451
if (showSolidity) {
7552
setIsCompiled(false);
7653
}
7754
}, [showSolidity]);
7855

79-
// Load the query parameters from the URL and set the state accordingly. Gists are loaded in useGist.
8056
useEffect(() => {
8157
if (searchParams.get("transpile") === "true") {
8258
setShowSolidity(true);
@@ -106,7 +82,6 @@ function App() {
10682
[setSolidityCode],
10783
);
10884

109-
// Loading shared code by query parameter and get a function for creating sharable permalinks.
11085
const { newGist } = useGist(onSwayCodeChange, onSolidityCodeChange);
11186

11287
const setError = useCallback(
@@ -144,7 +119,6 @@ function App() {
144119
const onCompileClick = useCallback(() => {
145120
track("Compile Click", { toolchain });
146121
if (showSolidity) {
147-
// Transpile the Solidity code before compiling.
148122
track("Transpile");
149123
setCodeToTranspile(solidityCode);
150124
} else {
@@ -159,14 +133,30 @@ function App() {
159133
toolchain,
160134
]);
161135

136+
const onAIAssistClick = useCallback(() => {
137+
track("AI Assist Click");
138+
setAiDialogOpen(true);
139+
}, []);
140+
141+
const onAICodeGenerated = useCallback((code: string) => {
142+
track("AI Code Generated");
143+
onSwayCodeChange(code);
144+
setAiDialogOpen(false);
145+
}, [onSwayCodeChange]);
146+
147+
const onAICodeFixed = useCallback((fixedCode: string) => {
148+
track("AI Code Fixed");
149+
onSwayCodeChange(fixedCode);
150+
}, [onSwayCodeChange]);
151+
162152
useTranspile(
163153
codeToTranspile,
164154
setCodeToCompile,
165155
onSwayCodeChange,
166156
setError,
167157
updateLog,
168158
);
169-
useCompile(codeToCompile, setError, setIsCompiled, updateLog, toolchain);
159+
useCompile(codeToCompile, setError, setIsCompiled, updateLog, toolchain, onAICodeFixed);
170160

171161
return (
172162
<div
@@ -188,6 +178,7 @@ function App() {
188178
showSolidity={showSolidity}
189179
setShowSolidity={setShowSolidity}
190180
updateLog={updateLog}
181+
onAIAssistClick={AI_FEATURES_ENABLED ? onAIAssistClick : undefined}
191182
/>
192183
<div
193184
style={{
@@ -215,6 +206,13 @@ function App() {
215206
contractId={contractId}
216207
updateLog={updateLog}
217208
/>
209+
{AI_FEATURES_ENABLED && (
210+
<AIGenerationDialog
211+
open={aiDialogOpen}
212+
onClose={() => setAiDialogOpen(false)}
213+
onCodeGenerated={onAICodeGenerated}
214+
/>
215+
)}
218216
<Analytics />
219217
</div>
220218
);

app/src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ export const LOCAL_SERVER_URI = "http://0.0.0.0:8080";
77
export const SERVER_URI = process.env.REACT_APP_LOCAL_SERVER
88
? LOCAL_SERVER_URI
99
: SERVER_API;
10+
11+
// AI Configuration
12+
export const AI_BACKEND_URL = process.env.REACT_APP_AI_BACKEND_URL || 'http://localhost:3001';
13+
export const AI_FEATURES_ENABLED = process.env.REACT_APP_AI_FEATURES_ENABLED === 'true';

0 commit comments

Comments
 (0)