Skip to content

Commit fa35b8d

Browse files
committed
feat: support for more target keyword
1 parent d30741c commit fa35b8d

File tree

6 files changed

+45
-27
lines changed

6 files changed

+45
-27
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@
9898
"target": {
9999
"type": "string",
100100
"enum": [
101-
"inplace",
101+
"replace",
102+
"append",
103+
"prepend",
102104
"newfile"
103105
],
104-
"default": "inplace",
106+
"default": "replace",
105107
"description": "Where to put the result of the action"
106108
},
107109
"git": {

src/ai/base_ai.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Awaitable } from '../types/index.js'
1+
import type { Awaitable, BaseAIConfiguration } from '../types/index.js'
22

3-
export class BaseAI {
3+
export class BaseAI<T extends BaseAIConfiguration = BaseAIConfiguration> {
44
static name: string
55

66
protected key: string
7-
protected options: Record<string, unknown>
7+
protected options: T
88

9-
constructor(key: string, options: Record<string, unknown> = {}) {
9+
constructor(key: string, options: T) {
1010
this.key = key
1111
this.options = options
1212
}

src/ai/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { BaseAIConfiguration } from '../types/index.js'
12
import type { BaseAI } from './base_ai.js'
23
import { OpenAI } from './openai.js'
34

4-
export const ai: Record<string, typeof BaseAI> = {
5+
export const ai: Record<string, typeof BaseAI<BaseAIConfiguration>> = {
56
openai: OpenAI,
67
}

src/ai/openai.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
import type { OpenAIConfiguration } from '../types/index.js'
12
import { BaseAI } from './base_ai.js'
23

3-
export class OpenAI extends BaseAI {
4+
export class OpenAI extends BaseAI<OpenAIConfiguration> {
45
static name = 'OpenAI'
56

6-
constructor(key: string, options: Record<string, unknown> = {}) {
7-
super(key, options)
8-
}
7+
protected endpoint = 'https://api.openai.com/v1/chat/completions'
98

109
async ask(prompt: string, content: string): Promise<string> {
11-
return await fetch('https://api.openai.com/v1/chat/completions', {
10+
const { endpoint = this.endpoint, model } = this.options
11+
12+
return await fetch(endpoint, {
1213
method: 'POST',
1314
headers: {
1415
'Content-Type': 'application/json',
1516
'Authorization': `Bearer ${this.key}`,
1617
},
1718
body: JSON.stringify({
18-
model: this.options.model,
19+
model,
1920
messages: [{ role: 'system', content: prompt }, { role: 'user', content }],
2021
}),
2122
})

src/commands/run_action_command.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ export class RunActionCommand extends BaseCommand {
2626
const ai = await this.createAI(configuration)
2727
const completion = await ai.ask(prompt.content, this.getActiveEditorText())
2828

29-
if (action.target === 'inplace') {
30-
await this.applyChanges(completion)
31-
}
32-
else if (action.target === 'newfile') {
29+
if (action.target === 'newfile') {
3330
await this.showCompletion(completion)
3431
}
32+
else {
33+
await this.applyChanges(completion, action.target)
34+
}
3535
})
3636

3737
await this.commitWithAction(action, 'after')
@@ -92,7 +92,7 @@ export class RunActionCommand extends BaseCommand {
9292
}
9393

9494
protected async createAI(configuration: AI): Promise<BaseAI> {
95-
const AI = aiIndex[configuration.type]
95+
const AI = aiIndex[configuration.provider]
9696

9797
this.assert(AI, 'AI not found.')
9898

@@ -101,7 +101,7 @@ export class RunActionCommand extends BaseCommand {
101101
this.assert(key, 'Key not found.')
102102

103103
return new AI(key, {
104-
model: configuration.model,
104+
...configuration.configuration,
105105
})
106106
}
107107

@@ -138,14 +138,22 @@ export class RunActionCommand extends BaseCommand {
138138
await this.getActiveEditor().document.save()
139139
}
140140

141-
protected async applyChanges(content: string): Promise<void> {
141+
protected async applyChanges(content: string, target: Action['target']): Promise<void> {
142142
this.logger.log('Apply changes...')
143143

144144
this.getActiveEditor().edit((editBuilder) => {
145145
const range = this.hasActiveEditorSelection() ? this.getActiveEditorSelection() : new Range(0, 0, this.getActiveEditorText().length, 0)
146146

147-
editBuilder.delete(range)
148-
editBuilder.insert(range.start, content)
147+
if (target === 'replace') {
148+
editBuilder.delete(range)
149+
}
150+
151+
if (target === 'append') {
152+
editBuilder.insert(range.end, `\n${content}`)
153+
}
154+
else if (target === 'replace' || target === 'prepend') {
155+
editBuilder.insert(range.start, `${content}\n`)
156+
}
149157
})
150158

151159
await this.saveActiveEditor()
@@ -159,8 +167,6 @@ export class RunActionCommand extends BaseCommand {
159167
}
160168

161169
protected async commitWithAction(action: Action, when: 'before' | 'after'): Promise<void> {
162-
this.logger.log(`Commit changes ${when} action...`)
163-
164170
let commitMessage = when === 'before' ? action.git?.commitMessageBeforeAction : action.git?.commitMessageAfterAction
165171

166172
if (!commitMessage) {
@@ -173,6 +179,7 @@ export class RunActionCommand extends BaseCommand {
173179

174180
this.assert(commitMessage, 'Commit message is required.')
175181

182+
this.logger.log(`Commit changes ${when} action...`)
176183
await this.commit(this.getActiveEditor().document.fileName, commitMessage)
177184
}
178185

src/types/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
export type Awaitable<T> = T | Promise<T>
22

3+
export type BaseAIConfiguration = Record<string, any>
4+
5+
export interface OpenAIConfiguration {
6+
model: string
7+
endpoint?: string
8+
}
9+
310
export interface Prompt {
411
name: string
512
content: string
@@ -8,15 +15,15 @@ export interface Prompt {
815
export interface AI {
916
name: string
1017
keyName: string
11-
type: string
12-
model: string
18+
provider: 'openai'
19+
configuration: OpenAIConfiguration
1320
}
1421

1522
export interface Action {
1623
name: string
1724
ai: AI['name']
1825
prompt: Prompt['name']
19-
target: 'inplace' | 'newfile'
26+
target: 'replace' | 'append' | 'prepend' | 'newfile'
2027
git?: {
2128
commitMessageBeforeAction?: '__ask__' | (string & {})
2229
commitMessageAfterAction?: '__ask__' | (string & {})

0 commit comments

Comments
 (0)