Files
windmill/frontend/src/lib/components/DiffEditor.svelte
T
1e25c28183 feat: Implement sending diff to ai (#5510)
* allow mentioning specific files in instructions

* remove not working highlight implementation

* make highlighted text work

* fix tooltip position

* clean code

* cleaning

* use lib for tooltip positioning

* fix logic

* draft for db in context

* use tools for db in context

* fixes

* cleaning and bug fixes

* fix

* cleaning

* fix when script is db type

* simplify logic

* put schema in context if already here

* fix imports

* fix tooltip position and make it scrollable

* remove console logs

* check if selected is in available

* fix tooltip list

* add back lost logic

* last fix

* fix type errors

* use loaded schema from dbSchemas

* fix typing, content and lang are always there

* remove from context if not available anymore

* add not loaded yet mention if schema not loaded

* add missing callback logic

* fix prompt

* fix usage of updateselectedContext function

* fix styling for white theme

* handle tab and arrows

* fix schemas not being refreshed on contexts

* also refresh displayMessages when dbschemas change

* fix duplicate available contexts

* fix logic for new scripts

* fix new lines inside text area

* implement sending diff in context

* add button in deploy options to ask ai about diff

* also visualize change when asking for diff

* better prompt

* add limit to diff size

* put diff mode toggle in editor bar

* add button to see history from editor

* adjustements

* put see diff button in dropdown

* fixes

* better styling

* highlight if diff mode

* format files

* change buttons based on diffmode

* remove diff after sending message

* fix type error

* smaller buttons

* draft

* use existing editor in diff editor

* fix number of db resources fetches

* fix apply and add buttons on diff mode

* cleaning

* undo ai gen button show

* better buttons

* styling asjustements + show diff in badge

* styling

* fix deployed code check

* cleaning and styling

* better quick actions

* dont send code when analyzing

* remove apply in chat if only code and no diff

* fix bad code refactor

---------

Co-authored-by: HugoCasa <hugo@casademont.ch>
Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
2025-04-03 12:15:22 +00:00

148 lines
3.7 KiB
Svelte

<script lang="ts">
import { BROWSER } from 'esm-env'
import { createEventDispatcher, onMount } from 'svelte'
import '@codingame/monaco-vscode-standalone-languages'
import '@codingame/monaco-vscode-standalone-json-language-features'
import '@codingame/monaco-vscode-standalone-typescript-language-features'
import { editor as meditor } from 'monaco-editor'
import { initializeVscode } from './vscode'
import EditorTheme from './EditorTheme.svelte'
import { buildWorkerDefinition } from '$lib/monaco_workers/build_workers'
import Button from '$lib/components/common/button/Button.svelte'
buildWorkerDefinition()
const SIDE_BY_SIDE_MIN_WIDTH = 700
export let automaticLayout = true
export let fixedOverflowWidgets = true
export let defaultLang: string | undefined = undefined
export let defaultModifiedLang: string | undefined = undefined
export let defaultOriginal: string | undefined = undefined
export let defaultModified: string | undefined = undefined
export let readOnly = false
export let showButtons = false
let diffEditor: meditor.IStandaloneDiffEditor | undefined
let diffDivEl: HTMLDivElement | null = null
let editorWidth: number = SIDE_BY_SIDE_MIN_WIDTH
export let open = false
async function loadDiffEditor() {
await initializeVscode()
if (!diffDivEl) {
return
}
diffEditor = meditor.createDiffEditor(diffDivEl!, {
automaticLayout,
renderSideBySide: editorWidth >= SIDE_BY_SIDE_MIN_WIDTH,
originalEditable: false,
readOnly,
minimap: {
enabled: false
},
fixedOverflowWidgets,
scrollBeyondLastLine: false,
lineDecorationsWidth: 15,
lineNumbersMinChars: 2,
scrollbar: { alwaysConsumeMouseWheel: false }
})
if (
defaultOriginal !== undefined &&
defaultModified !== undefined &&
defaultLang !== undefined
) {
setupModel(defaultLang, defaultOriginal, defaultModified, defaultModifiedLang)
}
}
export function setupModel(
lang: string,
original?: string,
modified?: string,
modifiedLang?: string
) {
diffEditor?.setModel({
original: meditor.createModel('', lang),
modified: meditor.createModel('', modifiedLang ?? lang)
})
if (original) {
setOriginal(original)
}
if (modified) {
setModified(modified)
}
}
export function setOriginal(code: string) {
diffEditor?.getModel()?.original?.setValue(code)
defaultOriginal = code
}
export function getOriginal(): string {
return diffEditor?.getModel()?.original.getValue() ?? ''
}
export function setModified(code: string) {
diffEditor?.getModel()?.modified?.setValue(code)
defaultModified = code
}
export function getModified(): string {
return diffEditor?.getModel()?.modified.getValue() ?? ''
}
export function show(): void {
open = true
}
export function hide(): void {
open = false
}
function onWidthChange(editorWidth: number) {
diffEditor?.updateOptions({ renderSideBySide: editorWidth >= SIDE_BY_SIDE_MIN_WIDTH })
}
$: onWidthChange(editorWidth)
$: open && diffDivEl && loadDiffEditor()
onMount(() => {
if (BROWSER) {
return () => {
diffEditor?.dispose()
}
}
})
const dispatch = createEventDispatcher<{
hideDiffMode: void
seeHistory: void
}>()
</script>
{#if open}
<EditorTheme />
<div
bind:this={diffDivEl}
class="{$$props.class} editor nonmain-editor"
bind:clientWidth={editorWidth}
/>
{#if showButtons}
<div
class="absolute flex flex-row gap-2 bottom-10 left-1/2 z-10 -translate-x-1/2 rounded-md p-1 w-full justify-center"
>
<Button on:click={() => dispatch('seeHistory')} variant="contained" size="sm"
>See changes history</Button
>
<Button on:click={() => dispatch('hideDiffMode')} variant="contained" size="sm" color="red"
>Quit diff mode</Button
>
</div>
{/if}
{/if}