Skip to content

Commit ea56e22

Browse files
committed
feat(gpt-runner-vscode): add context menu for ask codes
1 parent 8fac930 commit ea56e22

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

packages/gpt-runner-shared/src/common/types/eventemitter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface ClientEventData {
2727

2828
[ClientEventName.UpdateUserSelectedText]: {
2929
text: string
30+
insertInputPrompt?: boolean
3031
}
3132

3233
[ClientEventName.OpenFileInIde]: {

packages/gpt-runner-vscode/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@
8181
"title": "Open GPT Runner Chat",
8282
"icon": "res/logo.svg",
8383
"category": "GPT Runner"
84+
},
85+
{
86+
"command": "gpt-runner.askSelection",
87+
"title": "💬 Ask GPT Runner",
88+
"category": "GPT Runner"
8489
}
8590
],
8691
"configuration": {
@@ -110,6 +115,13 @@
110115
"group": "navigation",
111116
"icon": "res/logo.svg"
112117
}
118+
],
119+
"editor/context": [
120+
{
121+
"command": "gpt-runner.askSelection",
122+
"group": "gpt-runner",
123+
"when": "editorHasSelection"
124+
}
113125
]
114126
},
115127
"jsonValidation": [

packages/gpt-runner-vscode/src/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export enum Commands {
88
OpenInBrowser = `${EXT_NAME}.openInBrowser`,
99
InsertCodes = `${EXT_NAME}.insertCodes`,
1010
DiffCodes = `${EXT_NAME}.diffCodes`,
11+
AskSelection = `${EXT_NAME}.askSelection`,
1112
}
1213

1314
export const URI_SCHEME = 'gpt-runner'

packages/gpt-runner-vscode/src/register/sync-select-text.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ClientEventName, debounce } from '@nicepkg/gpt-runner-shared/common'
33
import type { ContextLoader } from '../contextLoader'
44
import { state } from '../state'
55
import { emitter } from '../emitter'
6+
import { Commands } from '../constant'
67

78
export async function registerSyncSelectText(
89
cwd: string,
@@ -37,6 +38,38 @@ export async function registerSyncSelectText(
3738
debounceUpdateSelectText(editor)
3839
}))
3940

41+
disposes.push(vscode.commands.registerCommand(Commands.AskSelection, async () => {
42+
if (vscode.window.activeTextEditor)
43+
state.activeEditor = vscode.window.activeTextEditor
44+
45+
const selectText = vscode.window.activeTextEditor?.document.getText(vscode.window.activeTextEditor.selection)
46+
if (!selectText)
47+
return
48+
49+
const handleSidebarOpenAndInit = () => {
50+
// focus
51+
state.sidebarWebviewView?.show(true)
52+
53+
// send selectText to webview
54+
emitter.emit(ClientEventName.UpdateUserSelectedText, {
55+
text: selectText,
56+
insertInputPrompt: true,
57+
})
58+
}
59+
60+
// open sidebar webview when it's not open
61+
if (!state.sidebarWebviewView?.visible) {
62+
await vscode.commands.executeCommand('gpt-runner.chatView.focus')
63+
state.sidebarWebviewView?.webview.onDidReceiveMessage(({ eventName }: { eventName: ClientEventName }) => {
64+
if (eventName === ClientEventName.InitSuccess)
65+
handleSidebarOpenAndInit()
66+
})
67+
}
68+
else {
69+
handleSidebarOpenAndInit()
70+
}
71+
}))
72+
4073
return vscode.Disposable.from({
4174
dispose,
4275
})

packages/gpt-runner-web/client/src/hooks/use-emit-bind.hook.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@ import { useEffect } from 'react'
22
import { ClientEventName, toUnixPath } from '@nicepkg/gpt-runner-shared/common'
33
import { emitter } from '../helpers/emitter'
44
import { useTempStore } from '../store/zustand/temp'
5+
import { useGlobalStore } from '../store/zustand/global'
56
import { useOn } from './use-on.hook'
7+
import { useChatInstance } from './use-chat-instance.hook'
68

79
export function useEmitBind(deps: any[] = []) {
8-
const { updateIdeSelectedText, updateIdeOpeningFilePaths, updateIdeActiveFilePath } = useTempStore()
10+
const {
11+
updateIdeSelectedText,
12+
updateIdeOpeningFilePaths,
13+
updateIdeActiveFilePath,
14+
} = useTempStore()
15+
16+
const { activeChatId } = useGlobalStore()
17+
const { updateCurrentChatInstance } = useChatInstance({
18+
chatId: activeChatId,
19+
})
920

1021
useEffect(() => {
1122
emitter.emit(ClientEventName.InitSuccess)
@@ -35,9 +46,17 @@ export function useEmitBind(deps: any[] = []) {
3546

3647
useOn({
3748
eventName: ClientEventName.UpdateUserSelectedText,
38-
listener: ({ text }) => {
39-
updateIdeSelectedText(text)
49+
listener: ({ text, insertInputPrompt }) => {
50+
if (!insertInputPrompt) {
51+
updateIdeSelectedText(text)
52+
}
53+
else {
54+
updateIdeSelectedText('')
55+
updateCurrentChatInstance({
56+
inputtingPrompt: text,
57+
})
58+
}
4059
},
41-
deps: [...deps, updateIdeSelectedText],
60+
deps: [...deps, updateIdeSelectedText, updateCurrentChatInstance],
4261
})
4362
}

0 commit comments

Comments
 (0)