Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.
Open
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
4 changes: 4 additions & 0 deletions .idx/integrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cloud_run_deploy": {},
"gemini_api": {}
}
29 changes: 22 additions & 7 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,27 @@ The melody should be about: `;
<span>Prompt:</span>
<input placeholder="Enter your prompt" #prompt class="input">
</label>
&nbsp;<button [disabled]="loading || !prompt" class="button" (click)="generateTune(prompt.value); prompt.value = ''">
&nbsp;<button [disabled]="loading || !prompt.value" class="button" (click)="generateTune(prompt.value); prompt.value = ''">
@if (loading) {
Loading...
} @else {
Generate
}
</button>
<br>
<span>Examples:</span>
<button class="button" (click)="prompt.value = 'A fast-paced, upbeat melody in a major key.'">Upbeat</button>
<button class="button" (click)="prompt.value = 'A slow, melancholic melody in a minor key.'">Melancholic</button>
<button class="button" (click)="prompt.value = 'A mysterious and suspenseful melody.'">Mysterious</button>
<br>
<div class="error">{{ error }}</div>
<br>
<app-keyboard/>
<br>
<div class="output">
<h2>Generated Melody</h2>
<pre>{{ generatedTune }}</pre>
</div>
`,
styleUrl: 'app.component.css'
})
Expand All @@ -74,18 +84,22 @@ export class AppComponent {
protected apiKey = viewChild.required<ElementRef<HTMLInputElement>>('apiKey');
protected loading = false;
protected error = '';
protected generatedTune = '';

async generateTune(prompt: string) {
if (!this.apiKey().nativeElement.value) {
this.error = 'Please enter an API key.';
return;
}
this.error = '';
this.generatedTune = '';

prompt = basePrompt + prompt;

/**
* For production apps, make sure you use the Gemini API key **only**
* on the server. Find more at https://ai.google.dev/gemini-api/docs/get-started/web
*/
const genAI = new GoogleGenerativeAI(this.apiKey().nativeElement.value);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-critical critical

Exposing the Gemini API key on the client-side is a critical security vulnerability. The key can be easily intercepted by malicious actors by inspecting network traffic or the browser's developer tools. For any application, even a sample one, it's crucial to follow best practices. The API key should never be visible in the frontend code. Instead, you should create a backend endpoint (e.g., a Cloud Function) that the frontend calls. This backend service would then securely hold the API key and make the call to the Gemini API on the server side.


const model = genAI.getGenerativeModel({
model: 'gemini-1.5-pro-latest'
model: 'gemini-pro'
});
model.generationConfig.responseMimeType = 'application/json';

Expand All @@ -97,9 +111,10 @@ export class AppComponent {
const text = response.text();
const tune = JSON.parse(text);

this.generatedTune = JSON.stringify(tune, null, 2);
this.keyboard().playMelody(tune);
} catch (e: unknown) {
this.error = <string>e;
this.error = e as string;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While using as string is syntactically preferred over the older <string> cast, it's still an unsafe type assertion. The e variable is of type unknown, and might not be a string (it's often an Error object). A direct cast can lead to unexpected behavior or loss of information (e.g., displaying [object Object]). It's safer to explicitly convert the unknown error to a string representation to ensure this.error is always a string and contains useful information.

Suggested change
this.error = e as string;
this.error = String(e);

} finally {
this.loading = false;
}
Expand Down
32 changes: 32 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,35 @@
*/

/* You can add global styles to this file, and also import other style files */

:root {
--mat-app-background-color: #121212;
--mat-card-background-color: #1e1e1e;
--mat-card-header-text-color: #ffffff;
--mat-card-subtitle-text-color: #b3b3b3;
--mat-card-content-text-color: #ffffff;
--mat-primary-color: #bb86fc;
--mat-accent-color: #03dac6;
--mat-warn-color: #cf6679;
}

body {
background-color: var(--mat-app-background-color);
color: white;
font-family: Roboto, "Helvetica Neue", sans-serif;
margin: 0;
padding: 24px;
}

.mat-card {
background-color: var(--mat-card-background-color);
color: var(--mat-card-content-text-color);
}

.mat-card-header {
color: var(--mat-card-header-text-color);
}

.mat-card-subtitle {
color: var(--mat-card-subtitle-text-color);
}