-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(desktop): edit the selection with inline AI #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import type { LLMEvent } from '@dripnex/ai-core'; | ||
| import type { AiAPI } from '../../../preload/api/ai'; | ||
|
|
||
| export const AI_CHAT_TIMEOUT_MS = 8_000; | ||
|
|
||
| export type ChatApi = Pick<AiAPI, 'chat' | 'onEvent' | 'cancel'>; | ||
|
|
||
| /** Collect a streamed chat into one string. Does not log text (PHI). */ | ||
| export function collectChat( | ||
| ai: ChatApi, | ||
| request: Parameters<AiAPI['chat']>[0], | ||
| timeoutMs = AI_CHAT_TIMEOUT_MS | ||
| ): Promise<string | null> { | ||
| return new Promise((resolve, reject) => { | ||
| let requestId: string | null = null; | ||
| let text = ''; | ||
| const buffered: Array<{ id: string; event: LLMEvent }> = []; | ||
| let settled = false; | ||
|
|
||
| function finish(value: string | null, error?: unknown): void { | ||
| if (settled) return; | ||
| settled = true; | ||
| clearTimeout(timeout); | ||
| off(); | ||
| if (error) reject(error); | ||
| else resolve(value); | ||
| } | ||
|
|
||
| function apply(id: string, event: LLMEvent): void { | ||
| if (settled) return; | ||
| if (!requestId) { | ||
| buffered.push({ id, event }); | ||
| return; | ||
| } | ||
| if (id !== requestId) return; | ||
| switch (event.type) { | ||
| case 'text': | ||
| text += event.delta; | ||
| break; | ||
| case 'error': | ||
| finish(null, new Error(event.error)); | ||
| break; | ||
| case 'done': | ||
| finish(text); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const off = ai.onEvent((id, raw) => apply(id, raw as LLMEvent)); | ||
| const timeout = setTimeout(() => { | ||
| if (requestId) void ai.cancel(requestId); | ||
| finish(null); | ||
| }, timeoutMs); | ||
|
|
||
| void ai | ||
| .chat(request) | ||
| .then(result => { | ||
| if (settled) { | ||
| void ai.cancel(result.requestId); | ||
| return; | ||
| } | ||
| requestId = result.requestId; | ||
| for (const item of buffered) apply(item.id, item.event); | ||
| }) | ||
| .catch(error => finish(null, error)); | ||
| }); | ||
| } |
58 changes: 58 additions & 0 deletions
58
apps/desktop/src/renderer/editor/inlineAi/__tests__/parse.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { buildInlineEditPrompt, extractInlineReplacement, inlineEditContext } from '../parse'; | ||
|
|
||
| describe('inlineEditContext', () => { | ||
| it('windows text around the selection', () => { | ||
| const content = 'aaa\nHello world\nzzz'; | ||
| const from = content.indexOf('Hello'); | ||
| const to = from + 'Hello'.length; | ||
| const ctx = inlineEditContext(content, from, to, 'Note', 'Proofread'); | ||
| expect(ctx.selection).toBe('Hello'); | ||
| expect(ctx.before).toBe('aaa\n'); | ||
| expect(ctx.after).toBe(' world\nzzz'); | ||
| expect(ctx.title).toBe('Note'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('buildInlineEditPrompt', () => { | ||
| it('includes the instruction and selection', () => { | ||
| const prompt = buildInlineEditPrompt({ | ||
| title: 'Ship', | ||
| selection: 'teh list', | ||
| before: '', | ||
| after: '', | ||
| instruction: 'Proofread', | ||
| }); | ||
| expect(prompt).toContain('TITLE: Ship'); | ||
| expect(prompt).toContain('teh list'); | ||
| expect(prompt).toContain('INSTRUCTION: Proofread'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('extractInlineReplacement', () => { | ||
| it('returns plain text', () => { | ||
| expect(extractInlineReplacement('Hello world', false)).toBe('Hello world'); | ||
| }); | ||
|
|
||
| it('unwraps a single markdown fence', () => { | ||
| expect(extractInlineReplacement('```markdown\nHello\n```', false)).toBe('Hello\n'); | ||
| }); | ||
|
|
||
| it('keeps a fence when asked', () => { | ||
| expect(extractInlineReplacement('```mermaid\ngraph TD\n```', true)).toBe( | ||
| '```mermaid\ngraph TD\n```' | ||
| ); | ||
| }); | ||
|
|
||
| it('preserves indented list items', () => { | ||
| expect(extractInlineReplacement(' - child', false)).toBe(' - child'); | ||
| }); | ||
|
|
||
| it('preserves indented code inside a wrapping fence', () => { | ||
| expect(extractInlineReplacement('```\n code\n```', false)).toBe(' code\n'); | ||
| }); | ||
|
|
||
| it('rejects empty output', () => { | ||
| expect(extractInlineReplacement(' ', false)).toBeNull(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.