Skip to content

Commit

Permalink
feat: add new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvanduocit committed Feb 26, 2023
1 parent 79679ad commit 29513c0
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 136 deletions.
1 change: 1 addition & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ declare type FramePosition = 'left' | 'center' | 'right'

declare interface RequestMessage {
prompt: string
model?: string
}
29 changes: 19 additions & 10 deletions src/CanvasView.ts → src/LeafView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { dequeue, onQueueAdded } from './queue'

export const VIEW_TYPE_AI_EXPLAIN = 'ai-assistant-view'

export class CanvasView extends ItemView {
export class LeafView extends ItemView {
private plugin: SharePlugin
private responseEl: HTMLElement

Expand All @@ -14,6 +14,16 @@ export class CanvasView extends ItemView {
}

async onload(): Promise<void> {
this.renderView()

await this.processQueue()

onQueueAdded(async () => {
await this.processQueue()
})
}

renderView(): void {
this.contentEl.empty()
this.contentEl.addClass('ai-assistant-view')

Expand All @@ -26,25 +36,24 @@ export class CanvasView extends ItemView {

this.responseEl.innerText =
'To get started, select some text and press right-click > AI Assistant'

await this.processQueue()

onQueueAdded(async () => {
await this.processQueue()
})
}

async processQueue() {
const queuedText = dequeue()
if (queuedText) {
this.responseEl.innerText = 'Processing...'
await this.processPrompt(queuedText.prompt)
await this.processPrompt(queuedText.prompt, queuedText.model)
}
}

async processPrompt(instruction: string) {
async processPrompt(instruction: string, model?: string) {
let prompt = instruction
const response = await this.plugin.processPrompt(prompt)
const response = await this.plugin.createCompletion(prompt, model)
if (!response) {
new Notice('Failed to process prompt')
return
}

this.responseEl.innerText = response
}

Expand Down
15 changes: 15 additions & 0 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Configuration, OpenAIApi } from 'openai'

let apiClient: OpenAIApi | null = null

export const getOpenaiClient = (openaiApiKey: string) => {
if (!apiClient) {
const configuration = new Configuration({
apiKey: openaiApiKey
})

apiClient = new OpenAIApi(configuration)
}

return apiClient
}
9 changes: 0 additions & 9 deletions src/logo.svg

This file was deleted.

Loading

0 comments on commit 29513c0

Please sign in to comment.