Skip to content

Commit

Permalink
Polish editor toolbar and AI service (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen authored Aug 29, 2024
1 parent 234393b commit 151f1ba
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 13 deletions.
5 changes: 4 additions & 1 deletion ai/src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def save_interaction_endpoint() -> Response | dict[str, str]:
else:
metadata = None
folder_name = generate_folder_name(prompt)
base_path = "../ft/goldens"
base_path = "ft/goldens"
folder_path = os.path.join(base_path, folder_name)

os.makedirs(folder_path, exist_ok=True)
Expand Down Expand Up @@ -88,6 +88,9 @@ def generate():


def generate_folder_name(prompt: str) -> str:
prompt = prompt.replace(
" ", "_"
) # Replace spaces with underscores to avoid %20
# Generate a unique 4-character suffix to avoid naming collisions
suffix = secrets.token_urlsafe(4)
cleaned_prompt = urllib.parse.quote(prompt)[:50]
Expand Down
2 changes: 1 addition & 1 deletion mesop/web/src/editor_toolbar/editor_history_dialog.ng.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ <h3 mat-dialog-title>Mesop Editor History</h3>
</div>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-button [mat-dialog-close]="false">Cancel</button>
<button
mat-button
[mat-dialog-close]="selectedInteraction"
Expand All @@ -39,5 +40,4 @@ <h3 mat-dialog-title>Mesop Editor History</h3>
>
Revert
</button>
<button mat-button [mat-dialog-close]="false">Cancel</button>
</mat-dialog-actions>
6 changes: 3 additions & 3 deletions mesop/web/src/editor_toolbar/editor_response_dialog.ng.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<h3 mat-dialog-title>Mesop Editor Response</h3>
<h3 mat-dialog-title>Review generated code</h3>
<mat-dialog-content class="mat-typography">
<mesop-code-mirror-diff
[beforeCode]="data.response.beforeCode"
[afterCode]="data.response.afterCode"
/>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-button [mat-dialog-close]="true">OK</button>
<button mat-button [mat-dialog-close]="false" cdkFocusInitial>Cancel</button>
<button mat-button [mat-dialog-close]="false">Cancel</button>
<button mat-button [mat-dialog-close]="true" cdkFocusInitial>Apply</button>
</mat-dialog-actions>
5 changes: 1 addition & 4 deletions mesop/web/src/editor_toolbar/editor_toolbar.ng.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
</button>

@if (!isToolbarMinimized) {
<div class="drag-handle" cdkDragHandle>
<mat-icon>drag_indicator</mat-icon>
</div>

<button
mat-icon-button
Expand All @@ -27,7 +24,7 @@
<textarea
#textarea
disabled="{{isLoading}}"
placeholder="{{isLoading ? 'Generating...' : 'Type in the UI change you want...' }}"
placeholder="{{isLoading ? 'Generating...' : 'Type in the UI change... ' + getToolbarShortcutText() }}"
class="floating-editor-toolbar-textarea"
[(ngModel)]="prompt"
(ngModelChange)="onPromptChange($event)"
Expand Down
2 changes: 1 addition & 1 deletion mesop/web/src/editor_toolbar/editor_toolbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
color: var(--sys-on-surface-variant);
}
height: 30px;
width: min(80vw, 480px);
width: max(min(60vw, 480px), 180px);
outline: none;
resize: none;
overflow: hidden;
Expand Down
44 changes: 42 additions & 2 deletions mesop/web/src/editor_toolbar/editor_toolbar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import {Component, ElementRef, Inject, OnInit, ViewChild} from '@angular/core';
import {
Component,
ElementRef,
HostListener,
Inject,
OnInit,
ViewChild,
} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {CdkDrag, CdkDragEnd} from '@angular/cdk/drag-drop';
import {MatButtonModule} from '@angular/material/button';
Expand Down Expand Up @@ -112,6 +119,27 @@ export class EditorToolbar implements OnInit {
return 'Select component - Ctrl ⇧ E';
}

@HostListener('window:keydown', ['$event'])
handleKeyDown(event: KeyboardEvent) {
// Hotkey for focusing on editor toolbar textarea
//
// Binds:
// cmd + k (MacOs)
// ctrl + k (Other platforms)
if (event.key === 'k' && (isMac() ? event.metaKey : event.ctrlKey)) {
this.textarea.nativeElement.focus();
event.preventDefault();
return;
}
}

getToolbarShortcutText(): string {
if (isMac()) {
return '⌘ K';
}
return 'Ctrl K';
}

isSelectingMode(): boolean {
return this.editorService.getSelectionMode() === SelectionMode.SELECTING;
}
Expand Down Expand Up @@ -202,8 +230,11 @@ export class EditorToolbar implements OnInit {
width: '90%',
});
const response = await responsePromise;
progressDialogRef.afterClosed().subscribe(() => {
this.autocompleteTrigger.closePanel();
});
progressDialogRef.close();
this.autocompleteTrigger.closePanel();

const dialogRef = this.dialog.open(EditorPromptResponseDialog, {
data: {response: response, responseTime: this.responseTime},
width: '90%',
Expand Down Expand Up @@ -250,6 +281,15 @@ export class EditorToolbar implements OnInit {
if (Number.isInteger(result)) {
const interaction = this.editorToolbarService.getHistory()[result];
this.editorToolbarService.commit(interaction.beforeCode);
this.editorToolbarService.addHistoryEntry({
prompt: `Revert: ${interaction.prompt}`,
path: interaction.path,
// Swap before and after code to represent the revert
beforeCode: interaction.afterCode,
afterCode: interaction.beforeCode,
diff: '<revert>',
lineNumber: undefined,
});
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion mesop/web/src/editor_toolbar/editor_toolbar_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class EditorToolbarService {
}
}

private addHistoryEntry(entry: PromptInteraction) {
addHistoryEntry(entry: PromptInteraction) {
this.history.unshift(entry);
this.saveHistoryToStorage();
}
Expand Down

0 comments on commit 151f1ba

Please sign in to comment.