Skip to content

Commit

Permalink
feat: add stack management
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Jul 14, 2023
1 parent 3d712df commit 80e4fd1
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"project": "./tsconfig.json"
},
"rules": {
"no-console": ["warning", { "allow": ["error", "info", "warn"] }],
"no-console": ["warn", { "allow": ["error", "info", "warn"] }],

"import/no-extraneous-dependencies": "off"
}
Expand Down
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@types/jest": "29.5.3",
"@types/mocha": "10.0.1",
"@types/node": "20.3.3",
"@types/ramda": "0.29.3",
"@types/vscode": "1.79.1",
"@types/ws": "8.5.5",
"@typescript-eslint/eslint-plugin": "5.60.1",
Expand All @@ -49,6 +50,7 @@
"jest": "29.6.1",
"mocha": "10.2.0",
"prettier": "2.8.8",
"ramda": "0.29.0",
"rollup": "3.26.0",
"rollup-plugin-swc3": "0.8.2",
"semantic-release": "21.0.6",
Expand All @@ -71,10 +73,6 @@
"command": "extension.openai-forge.addOrRemoveCurrentDocument",
"title": "OpenAI Forge: Add/Remove current document to/from the stack"
},
{
"command": "extension.openai-forge.fake",
"title": "OpenAI Forge: Fake"
},
{
"command": "extension.openai-forge.sendCurrentDocument",
"title": "OpenAI Forge: Send current document or documents stack"
Expand All @@ -89,7 +87,19 @@
"description": "Exclude project information from the prompt message sent to ChatGPT / OpenAI API."
}
}
}
},
"keybindings": [
{
"command": "extension.openai-forge.addOrRemoveCurrentDocument",
"key": "alt+f a",
"when": "editorTextFocus"
},
{
"command": "extension.openai-forge.sendCurrentDocument",
"key": "alt+f s",
"when": "editorTextFocus"
}
]
},
"author": {
"name": "Ivan Gabriele",
Expand Down
5 changes: 0 additions & 5 deletions src/commands/fake.ts

This file was deleted.

7 changes: 5 additions & 2 deletions src/commands/sendCurrentDocumentOrStack.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stackManager } from '../libs/stackManager'
import { WebSocketDataAction, type WebSocketData } from '../types'
import { formatPrompt } from '../utils/formatPrompt'
import { getCurrentDocumentInfo } from '../utils/getCurrentDocumentInfo'
Expand All @@ -6,10 +7,12 @@ import { getCurrentWorkspaceInfo } from '../utils/getCurrentWorkspaceInfo'
import type { WebSocket } from 'ws'

export function sendCurrentDocument(webSocket: WebSocket) {
const currentDocumentInfo = getCurrentDocumentInfo()
const currentWorkspaceInfo = getCurrentWorkspaceInfo()
const currentOrStackDocumentInfos = stackManager.documentInfos.length
? stackManager.documentInfos
: [getCurrentDocumentInfo()]

const promptMessage = formatPrompt(currentWorkspaceInfo, [currentDocumentInfo])
const promptMessage = formatPrompt(currentWorkspaceInfo, currentOrStackDocumentInfos)

const webSocketData: WebSocketData = {
action: WebSocketDataAction.ASK,
Expand Down
35 changes: 5 additions & 30 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { type ExtensionContext, workspace, commands } from 'vscode'
import { type WebSocket, WebSocketServer } from 'ws'

import { fake } from './commands/fake'
import { addOrRemoveCurrentDocument } from './commands/addOrRemoveCurrentDocument'
import { sendCurrentDocument } from './commands/sendCurrentDocumentOrStack'
import { stateManager } from './libs/stateManager'
import { handleError } from './utils/handleError'
import { startWebSocketServer } from './utils/startWebSocketServer'
import { updateStackStatusBarItem } from './utils/updateStackStatusBarItem'
import { updateStateStatusBarItem } from './utils/updateStateStatusBarItem'
import { addOrRemoveCurrentDocument } from './commands/addOrRemoveCurrentDocument'
import { stateManager } from './libs/stateManager'
import { State } from './types'

const WEB_SOCKET_CLIENTS = new Set<WebSocket>()

export async function activate(context: ExtensionContext) {
try {
Expand All @@ -30,24 +26,7 @@ export async function activate(context: ExtensionContext) {
// -------------------------------------------------------------------------
// WebSocket Server

const webSockerServer = new WebSocketServer({ port: 4242 })

webSockerServer.on('connection', webSocket => {
stateManager.clients.add(webSocket)
updateStateStatusBarItem()

webSocket.on('message', message => {
console.debug(`New message received: ${message}.`)
})

webSocket.on('close', () => {
stateManager.clients.delete(webSocket)
updateStateStatusBarItem()
})
})

stateManager.state = State.RUNNING
updateStateStatusBarItem()
startWebSocketServer()

// -------------------------------------------------------------------------
// Commands
Expand All @@ -56,15 +35,11 @@ export async function activate(context: ExtensionContext) {
'extension.openai-forge.addOrRemoveCurrentDocument',
addOrRemoveCurrentDocument,
)
const fakeDisposable = commands.registerCommand('extension.openai-forge.fake', fake)
const sendCurrentDocumentDisposable = commands.registerCommand('extension.openai-forge.sendCurrentDocument', () => {
for (const webSocketClient of WEB_SOCKET_CLIENTS) {
sendCurrentDocument(webSocketClient)
}
stateManager.clients.forEach(sendCurrentDocument)
})

context.subscriptions.push(addOrRemoveCurrentDocumentDisposable)
context.subscriptions.push(fakeDisposable)
context.subscriptions.push(sendCurrentDocumentDisposable)
} catch (err) {
handleError(err)
Expand Down
3 changes: 2 additions & 1 deletion src/libs/stateManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { WebSocket } from 'ws'
import { State } from '../types'

import type { WebSocket } from 'ws'

class StateManager {
clients: Set<WebSocket> = new Set<WebSocket>()
#state: State = State.STARTING
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getCurrentWorkspaceInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function getCurrentWorkspaceInfo(): WorkspaceInfo {
languages: [],
mainFramework: undefined,
name: '',
subFrameworks: [],
rootPath,
subFrameworks: [],
}
}
30 changes: 30 additions & 0 deletions src/utils/startWebSocketServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { WebSocketServer } from 'ws'

import { updateStateStatusBarItem } from './updateStateStatusBarItem'
import { stateManager } from '../libs/stateManager'
import { State } from '../types'

let WEB_SOCKET_SERVER: WebSocketServer

export function startWebSocketServer() {
if (!WEB_SOCKET_SERVER) {
WEB_SOCKET_SERVER = new WebSocketServer({ port: 4242 })

WEB_SOCKET_SERVER.on('connection', webSocket => {
stateManager.clients.add(webSocket)
updateStateStatusBarItem()

// webSocket.on('message', message => {
// console.debug(`New message received: ${message}.`)
// })

webSocket.on('close', () => {
stateManager.clients.delete(webSocket)
updateStateStatusBarItem()
})
})

stateManager.state = State.RUNNING
updateStateStatusBarItem()
}
}
14 changes: 13 additions & 1 deletion src/utils/updateStackStatusBarItem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { window, type StatusBarItem, StatusBarAlignment } from 'vscode'
import { ascend, prop } from 'ramda'
import { window, type StatusBarItem, StatusBarAlignment, MarkdownString } from 'vscode'

import { stackManager } from '../libs/stackManager'

let STACK_STATUS_BAR_ITEM: StatusBarItem
Expand All @@ -13,4 +15,14 @@ export function updateStackStatusBarItem() {
STACK_STATUS_BAR_ITEM.text = `$(list-unordered) ${stackManager.documentInfos.length || 'No'} document${
stackManager.documentInfos.length > 1 ? 's' : ''
} selected`
STACK_STATUS_BAR_ITEM.tooltip = stackManager.documentInfos.length
? new MarkdownString(
[
'**Selected documents:**',
...stackManager.documentInfos
.sort(ascend(prop('relativePath')))
.map(documentInfo => `- ${documentInfo.relativePath}`),
].join('\n'),
)
: undefined
}
7 changes: 3 additions & 4 deletions src/utils/updateStateStatusBarItem.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { window, type StatusBarItem, StatusBarAlignment } from 'vscode'
import { stateManager } from '../libs/stateManager'
import { State } from '../types'

import { STATE_ICON, STATE_LABEL } from '../constants'
import { InternalError } from '../libs/InternalError'
import { stateManager } from '../libs/stateManager'
import { State } from '../types'

let STATE_STATUS_BAR_ITEM: StatusBarItem

Expand All @@ -13,8 +14,6 @@ export function updateStateStatusBarItem() {
STATE_STATUS_BAR_ITEM.show()
}

console.log(stateManager.clients)

switch (stateManager.state) {
case State.RUNNING:
STATE_STATUS_BAR_ITEM.text = `$(${STATE_ICON[stateManager.state]}) ${stateManager.clients.size || 'No'} client${
Expand Down
34 changes: 34 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,15 @@ __metadata:
languageName: node
linkType: hard

"@types/ramda@npm:0.29.3":
version: 0.29.3
resolution: "@types/ramda@npm:0.29.3"
dependencies:
types-ramda: ^0.29.4
checksum: 172b18d62473d4bffb1e4e92fa293e56fa4f85fcf91b1b2c9178955f775e34065cc408e93f1171436b52e240599ecb1be51955dab07ca389bf2da98f85f820a9
languageName: node
linkType: hard

"@types/resolve@npm:1.20.2":
version: 1.20.2
resolution: "@types/resolve@npm:1.20.2"
Expand Down Expand Up @@ -8458,6 +8467,7 @@ __metadata:
"@types/jest": 29.5.3
"@types/mocha": 10.0.1
"@types/node": 20.3.3
"@types/ramda": 0.29.3
"@types/vscode": 1.79.1
"@types/ws": 8.5.5
"@typescript-eslint/eslint-plugin": 5.60.1
Expand All @@ -8478,6 +8488,7 @@ __metadata:
jest: 29.6.1
mocha: 10.2.0
prettier: 2.8.8
ramda: 0.29.0
rollup: 3.26.0
rollup-plugin-swc3: 0.8.2
semantic-release: 21.0.6
Expand Down Expand Up @@ -9179,6 +9190,13 @@ __metadata:
languageName: node
linkType: hard

"ramda@npm:0.29.0":
version: 0.29.0
resolution: "ramda@npm:0.29.0"
checksum: 9ab26c06eb7545cbb7eebcf75526d6ee2fcaae19e338f165b2bf32772121e7b28192d6664d1ba222ff76188ba26ab307342d66e805dbb02c860560adc4d5dd57
languageName: node
linkType: hard

"randombytes@npm:^2.1.0":
version: 2.1.0
resolution: "randombytes@npm:2.1.0"
Expand Down Expand Up @@ -10475,6 +10493,13 @@ __metadata:
languageName: node
linkType: hard

"ts-toolbelt@npm:^9.6.0":
version: 9.6.0
resolution: "ts-toolbelt@npm:9.6.0"
checksum: 9f35fd95d895a5d32ea9fd2e532a695b0bae6cbff6832b77292efa188a0ed1ed6e54f63f74a8920390f3d909a7a3adb20a144686372a8e78b420246a9bd3d58a
languageName: node
linkType: hard

"tsconfig-paths@npm:^3.14.1":
version: 3.14.2
resolution: "tsconfig-paths@npm:3.14.2"
Expand Down Expand Up @@ -10640,6 +10665,15 @@ __metadata:
languageName: node
linkType: hard

"types-ramda@npm:^0.29.4":
version: 0.29.4
resolution: "types-ramda@npm:0.29.4"
dependencies:
ts-toolbelt: ^9.6.0
checksum: 08a872e71555fe6a3f1fd9381989a573d493f2156d9689f3bb72278db73c848122d67a9efff1867ce97b3dfbb2fdadaa3864daaee014ab19cf9bbfe2c171ed52
languageName: node
linkType: hard

"typescript@npm:5.1.6":
version: 5.1.6
resolution: "typescript@npm:5.1.6"
Expand Down

0 comments on commit 80e4fd1

Please sign in to comment.