-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(Experimental) Add new command to decorate contents (#14)
* feat: move context menu to submenu of Slidaiv * feat: add new command to decorate contents * refactor: move generate task to tasks.ts * fix a bit * chore: change order of context menu
- Loading branch information
Showing
8 changed files
with
205 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
interface LLMClient { | ||
generatePageContents(prompt: string, model: string, locale: string | null): Promise<string | null>; | ||
generatePageContents(prompt: string, locale: string | null): Promise<string | null>; | ||
decorateContents(prompt: string): Promise<string | null>; | ||
} |
This file contains 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 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 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 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,23 @@ | ||
import type * as vscode from 'vscode'; | ||
|
||
export class Logger { | ||
private readonly out: vscode.OutputChannel; | ||
private isDebug: boolean; | ||
constructor(out: vscode.OutputChannel, debug: boolean) { | ||
this.out = out; | ||
this.isDebug = debug; | ||
} | ||
|
||
error(message: string) { | ||
this.out.appendLine(`[ERROR] ${message}`); | ||
} | ||
|
||
info(message: string) { | ||
this.out.appendLine(`[INFO] ${message}`); | ||
} | ||
|
||
debug(message: string) { | ||
if (!this.isDebug) return; | ||
this.out.appendLine(`[DEBUG] ${message}`); | ||
} | ||
} |
This file contains 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 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,74 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
import { Client } from "./client/openai"; | ||
import { Logger } from "./logger"; | ||
import { SlidevPage } from './model/slidev'; | ||
|
||
export const getTaskGenerateContents = (client: Client, logger: Logger) => { | ||
return async (progress: vscode.Progress<any>) => { | ||
logger.info('Generating contents'); | ||
progress.report({ increment: 0, message: 'Parsing Slidev contents' }); | ||
const editor = vscode.window.activeTextEditor; | ||
if (!editor) { | ||
throw new Error('No active editor'); | ||
} | ||
const position = editor.selection.active; | ||
const slidevPage = await SlidevPage.init( | ||
editor.document.getText(), | ||
editor.document.fileName, | ||
position.line | ||
); | ||
|
||
progress.report({ increment: 10, message: 'Generating Slidev contents' }); | ||
const page = await slidevPage.rewriteByLLM(client); | ||
|
||
progress.report({ increment: 80, message: 'Write the generated slide contents' }); | ||
const range = new vscode.Range(slidevPage.start, 0, slidevPage.end, 0); | ||
const edit = new vscode.WorkspaceEdit(); | ||
edit.replace(editor.document.uri, range, page); | ||
const isEdited = await vscode.workspace.applyEdit(edit); | ||
if (!isEdited) { | ||
throw new Error('Failed to write the generated slide contents'); | ||
} | ||
|
||
progress.report({ increment: 10, message: 'Done' }); | ||
} | ||
} | ||
|
||
export const getTaskDecorateContent = (client: Client, logger: Logger) => { | ||
return async (progress: vscode.Progress<any>) => { | ||
logger.info('Decorating contents'); | ||
progress.report({ increment: 0, message: 'Get text to decorate' }); | ||
const editor = vscode.window.activeTextEditor; | ||
if (!editor) { | ||
vscode.window.showErrorMessage('No active editor'); | ||
return; | ||
} | ||
const selection = editor.selection; | ||
if (!selection || selection.isEmpty) { | ||
vscode.window.showErrorMessage('No selection'); | ||
return; | ||
} | ||
const highlighted = editor.document.getText(selection); | ||
logger.debug(`selection: \n${highlighted}`); | ||
|
||
progress.report({ increment: 10, message: 'Calling LLM...' }); | ||
logger.info('Call LLM to decorate the contents'); | ||
const decorated = await client.decorateContents(highlighted); | ||
logger.debug(`decorated: \n${decorated}`); | ||
if (!decorated) { | ||
throw new Error('Failed to decorate the contents'); | ||
} | ||
|
||
progress.report({ increment: 80, message: 'Write the decorated text' }); | ||
logger.info('Write the slide contents'); | ||
const edit = new vscode.WorkspaceEdit(); | ||
edit.replace(editor.document.uri, selection, decorated); | ||
const isEdited = await vscode.workspace.applyEdit(edit); | ||
if (!isEdited) { | ||
throw new Error('Failed to replace the slide contents'); | ||
} | ||
|
||
progress.report({ increment: 10, message: 'Done' }); | ||
} | ||
} | ||