refactor(airi-plugin-vscode): better code & fixed dependency bundling issue
This commit is contained in:
@@ -66,12 +66,10 @@
|
||||
"publish": "tsx ./scripts/publish.ts",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"devDependencies": {
|
||||
"@guiiai/logg": "catalog:",
|
||||
"@moeru/std": "catalog:",
|
||||
"@proj-airi/server-sdk": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@proj-airi/server-sdk": "workspace:*",
|
||||
"@types/vscode": "^1.106.1",
|
||||
"tinyexec": "^1.0.2",
|
||||
"vscode-ext-gen": "^1.4.0"
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
import type { Events } from './types'
|
||||
|
||||
import { useLogger } from '@guiiai/logg'
|
||||
import { Client as ServerClient } from '@proj-airi/server-sdk'
|
||||
|
||||
/**
|
||||
* Airi Channel Server Client
|
||||
*/
|
||||
export class Client {
|
||||
private client: ServerClient<Events> | null = null
|
||||
|
||||
/**
|
||||
* Connect to Channel Server
|
||||
*/
|
||||
async connect(): Promise<boolean> {
|
||||
try {
|
||||
this.client = new ServerClient({ name: 'proj-airi:plugin-vscode' })
|
||||
|
||||
useLogger().log('Airi companion connected to Channel Server')
|
||||
return true
|
||||
}
|
||||
catch (error) {
|
||||
useLogger().errorWithError('Failed to connect to Airi Channel Server:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect from Channel Server
|
||||
*/
|
||||
disconnect(): void {
|
||||
if (this.client) {
|
||||
this.client.close()
|
||||
this.client = null
|
||||
useLogger().log('Airi companion disconnected')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send event to Airi
|
||||
*/
|
||||
sendEvent(event: Events): void {
|
||||
if (!this.client) {
|
||||
useLogger().warn('Cannot send event: not connected to Airi Channel Server')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// Send event to Airi
|
||||
this.client.send({
|
||||
type: 'vscode:context',
|
||||
data: event,
|
||||
})
|
||||
|
||||
useLogger().log(`Sent event to Airi: ${event.type}`, event)
|
||||
}
|
||||
catch (error) {
|
||||
useLogger().errorWithError('Failed to send event to Airi:', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is connected to Channel Server
|
||||
*/
|
||||
isConnected(): boolean {
|
||||
return !!this.client
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import type { Events } from './types'
|
||||
|
||||
import { useLogger } from '@guiiai/logg'
|
||||
import { Client as ServerClient } from '@proj-airi/server-sdk'
|
||||
|
||||
export class Client {
|
||||
private client: ServerClient<Events> | null = null
|
||||
|
||||
async connect(): Promise<boolean> {
|
||||
try {
|
||||
this.client = new ServerClient<Events>({ name: 'proj-airi:plugin-vscode' })
|
||||
useLogger().log('AIRI connected to Server Channel')
|
||||
return true
|
||||
}
|
||||
catch (error) {
|
||||
useLogger().errorWithError('Failed to connect to AIRI Server Channel:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
disconnect(): void {
|
||||
if (this.client) {
|
||||
this.client.close()
|
||||
this.client = null
|
||||
useLogger().log('AIRI disconnected')
|
||||
}
|
||||
}
|
||||
|
||||
sendEvent(event: Events): void {
|
||||
if (!this.client) {
|
||||
useLogger().warn('Cannot send event: not connected to AIRI Server Channel')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
this.client.send({ type: 'vscode:context', data: event })
|
||||
|
||||
useLogger().log(`Sent event to AIRI: ${event.type}`, event)
|
||||
}
|
||||
catch (error) {
|
||||
useLogger().errorWithError('Failed to send event to AIRI:', error)
|
||||
}
|
||||
}
|
||||
|
||||
isConnected(): boolean {
|
||||
return !!this.client
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
import type * as vscode from 'vscode'
|
||||
|
||||
import { initLogger, LoggerFormat, LoggerLevel, useLogger } from '@guiiai/logg'
|
||||
import { commands, window, workspace } from 'vscode'
|
||||
|
||||
import { Client } from './airi-client'
|
||||
import { Client } from './airi'
|
||||
import { ContextCollector } from './context-collector'
|
||||
|
||||
let client: Client
|
||||
@@ -17,8 +18,6 @@ let eventListeners: vscode.Disposable[] = []
|
||||
export async function activate(context: vscode.ExtensionContext) {
|
||||
initLogger(LoggerLevel.Debug, LoggerFormat.Pretty)
|
||||
|
||||
const { window, workspace, commands } = await import('vscode')
|
||||
|
||||
useLogger().log('AIRI is activating...')
|
||||
|
||||
// Get the configuration
|
||||
@@ -35,10 +34,10 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||
if (isEnabled) {
|
||||
const connected = await client.connect()
|
||||
if (connected) {
|
||||
window.showInformationMessage('AIRI server channel connected!')
|
||||
window.showInformationMessage('AIRI Server Channel connected!')
|
||||
}
|
||||
else {
|
||||
window.showWarningMessage('AIRI server channel connection failed!')
|
||||
window.showWarningMessage('AIRI Server Channel connection failed!')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +59,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
commands.registerCommand('airi-vscode.status', () => {
|
||||
const status = isEnabled && client ? 'Connected' : 'Disconnected'
|
||||
window.showInformationMessage(`AIRI server channel status: ${status}.`)
|
||||
window.showInformationMessage(`AIRI Server Channel status: ${status}.`)
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -78,8 +77,6 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||
async function registerListeners(sendInterval: number) {
|
||||
unregisterListeners()
|
||||
|
||||
const { window, workspace } = await import('vscode')
|
||||
|
||||
// File save event
|
||||
eventListeners.push(
|
||||
workspace.onDidSaveTextDocument(async (document) => {
|
||||
@@ -136,7 +133,6 @@ function startMonitoring(interval: number) {
|
||||
if (!isEnabled)
|
||||
return
|
||||
|
||||
const { window } = await import('vscode')
|
||||
const editor = window.activeTextEditor
|
||||
if (!editor)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user