From bdc3c9cb7ee10b419cc271990377dee928e3a73f Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Tue, 26 Nov 2024 00:41:31 -0800 Subject: [PATCH] proxy works! --- src/vs/code/electron-main/app.ts | 2 +- ...sendLLMMessage.ts => llmMessageService.ts} | 17 +++++++--------- .../{sendLLMTypes.ts => llmMessageTypes.ts} | 20 +++++++++---------- .../void/electron-main/LLMMessageChannel.ts | 14 ++++++------- .../react/src/sidebar-tsx/SidebarChat.tsx | 6 ++---- .../browser/react/src/util/sendLLMMessage.tsx | 10 +++++----- .../void/browser/registerInlineDiffs.ts | 7 +++---- .../contrib/void/browser/registerSidebar.ts | 2 +- src/vs/workbench/workbench.common.main.ts | 2 +- 9 files changed, 37 insertions(+), 43 deletions(-) rename src/vs/platform/void/browser/{sendLLMMessage.ts => llmMessageService.ts} (75%) rename src/vs/platform/void/common/{sendLLMTypes.ts => llmMessageTypes.ts} (58%) diff --git a/src/vs/code/electron-main/app.ts b/src/vs/code/electron-main/app.ts index 72a5c2ce..59e8af61 100644 --- a/src/vs/code/electron-main/app.ts +++ b/src/vs/code/electron-main/app.ts @@ -121,7 +121,7 @@ import { normalizeNFC } from '../../base/common/normalization.js'; import { ICSSDevelopmentService, CSSDevelopmentService } from '../../platform/cssDev/node/cssDevService.js'; import { ExtensionSignatureVerificationService, IExtensionSignatureVerificationService } from '../../platform/extensionManagement/node/extensionSignatureVerificationService.js'; -import { LLMMessageChannel } from '../../platform/void/electron-main/LLMMessageChannel.js'; +import { LLMMessageChannel } from '../../platform/void/electron-main/llmMessageChannel.js'; /** * The main VS Code application. There will only ever be one instance, diff --git a/src/vs/platform/void/browser/sendLLMMessage.ts b/src/vs/platform/void/browser/llmMessageService.ts similarity index 75% rename from src/vs/platform/void/browser/sendLLMMessage.ts rename to src/vs/platform/void/browser/llmMessageService.ts index 65e68e27..b5e6081d 100644 --- a/src/vs/platform/void/browser/sendLLMMessage.ts +++ b/src/vs/platform/void/browser/llmMessageService.ts @@ -3,7 +3,7 @@ * Void Editor additions licensed under the AGPLv3 License. *--------------------------------------------------------------------------------------------*/ -import { LLMMessageOnTextEvent, OnErrorEvent, OnFinalMessageEvent, SendLLMMessageParams, SendLLMMessageProxyParams } from '../common/sendLLMTypes.js'; +import { ProxyOnTextPayload, ProxyOnErrorPayload, ProxyOnFinalMessagePayload, LLMMessageServiceParams, ProxyLLMMessageParams } from '../common/llmMessageTypes.js'; import { IChannel } from '../../../base/parts/ipc/common/ipc.js'; import { IMainProcessService } from '../../ipc/common/mainProcessService.js'; import { InstantiationType, registerSingleton } from '../../instantiation/common/extensions.js'; @@ -18,7 +18,7 @@ export const ISendLLMMessageService = createDecorator('s // defines an interface that node/ creates and browser/ uses export interface ISendLLMMessageService { readonly _serviceBrand: undefined; - sendLLMMessage: (params: SendLLMMessageParams) => void; + sendLLMMessage: (params: LLMMessageServiceParams) => void; } @@ -36,35 +36,32 @@ export class SendLLMMessageService implements ISendLLMMessageService { // const service = ProxyChannel.toService(mainProcessService.getChannel('void-channel-sendLLMMessage')); // lets you call it like a service, not needed here } - sendLLMMessage(params: SendLLMMessageParams) { + sendLLMMessage(params: LLMMessageServiceParams) { const requestId_ = generateUuid(); const { onText, onFinalMessage, onError, ...proxyParams } = params; // listen for listenerName='onText' | 'onFinalMessage' | 'onError', and call the original function on it - const onTextEvent: Event = this.channel.listen('onText') + const onTextEvent: Event = this.channel.listen('onText') onTextEvent(e => { - console.log('event TEXT EVENT!!!:', JSON.stringify(e, null, 5)) if (requestId_ !== e.requestId) return; onText(e) }) - const onFinalMessageEvent: Event = this.channel.listen('onFinalMessage') + const onFinalMessageEvent: Event = this.channel.listen('onFinalMessage') onFinalMessageEvent(e => { - console.log('FINAL MESSAGE EVENT!!!:', JSON.stringify(e, null, 5)) if (requestId_ !== e.requestId) return; onFinalMessage(e) }) - const onErrorEvent: Event = this.channel.listen('onError') + const onErrorEvent: Event = this.channel.listen('onError') onErrorEvent(e => { - console.log('ERROR EVENT!!!:', JSON.stringify(e, null, 5)) if (requestId_ !== e.requestId) return; onError(e) }) // params will be stripped of all its functions - this.channel.call('sendLLMMessage', { ...proxyParams, requestId: requestId_ } satisfies SendLLMMessageProxyParams); + this.channel.call('sendLLMMessage', { ...proxyParams, requestId: requestId_ } satisfies ProxyLLMMessageParams); } } diff --git a/src/vs/platform/void/common/sendLLMTypes.ts b/src/vs/platform/void/common/llmMessageTypes.ts similarity index 58% rename from src/vs/platform/void/common/sendLLMTypes.ts rename to src/vs/platform/void/common/llmMessageTypes.ts index bbb163af..a8172630 100644 --- a/src/vs/platform/void/common/sendLLMTypes.ts +++ b/src/vs/platform/void/common/llmMessageTypes.ts @@ -5,29 +5,29 @@ import { VoidConfig } from '../../../workbench/contrib/void/browser/registerConfig.js'; -// ---------- definitions ---------- +// ---------- type definitions ---------- -export type LLMMessageOnText = (p: { newText: string, fullText: string }) => void +export type OnText = (p: { newText: string, fullText: string }) => void export type OnFinalMessage = (p: { fullText: string }) => void export type OnError = (p: { error: Error | string }) => void -export type LLMMessageAbortRef = { current: (() => void) | null } +export type AbortRef = { current: (() => void) | null } export type LLMMessage = { role: 'system' | 'user' | 'assistant'; content: string; } -export type SendLLMMessageParams = { - onText: LLMMessageOnText; +export type LLMMessageServiceParams = { + onText: OnText; onFinalMessage: OnFinalMessage; onError: OnError; messages: LLMMessage[]; voidConfig: VoidConfig | null; - abortRef: LLMMessageAbortRef; + abortRef: AbortRef; logging: { loggingName: string, @@ -36,8 +36,8 @@ export type SendLLMMessageParams = { // can't send functions across a proxy, use listeners instead export const listenerNames = ['onText', 'onFinalMessage', 'onError'] as const -export type SendLLMMessageProxyParams = Omit & { requestId: string } +export type ProxyLLMMessageParams = Omit & { requestId: string } -export type LLMMessageOnTextEvent = Parameters[0] & { requestId: string } -export type OnFinalMessageEvent = Parameters[0] & { requestId: string } -export type OnErrorEvent = Parameters[0] & { requestId: string } +export type ProxyOnTextPayload = Parameters[0] & { requestId: string } +export type ProxyOnFinalMessagePayload = Parameters[0] & { requestId: string } +export type ProxyOnErrorPayload = Parameters[0] & { requestId: string } diff --git a/src/vs/platform/void/electron-main/LLMMessageChannel.ts b/src/vs/platform/void/electron-main/LLMMessageChannel.ts index 3c4b0c8b..27700928 100644 --- a/src/vs/platform/void/electron-main/LLMMessageChannel.ts +++ b/src/vs/platform/void/electron-main/LLMMessageChannel.ts @@ -11,18 +11,18 @@ import { IServerChannel } from '../../../base/parts/ipc/common/ipc.js'; import { Emitter, Event } from '../../../base/common/event.js'; import { sendLLMMessage } from '../../../workbench/contrib/void/browser/react/out/util/sendLLMMessage.js'; -import { listenerNames, LLMMessageOnTextEvent, OnErrorEvent, OnFinalMessageEvent, SendLLMMessageParams, SendLLMMessageProxyParams } from '../common/sendLLMTypes.js'; +import { listenerNames, ProxyOnTextPayload, ProxyOnErrorPayload, ProxyOnFinalMessagePayload, LLMMessageServiceParams, ProxyLLMMessageParams } from '../common/llmMessageTypes.js'; -// NODE IMPLEMENTATION OF SENDLLMMESSAGE +// NODE IMPLEMENTATION OF SENDLLMMESSAGE - calls sendLLMMessage() and returns listeners export class LLMMessageChannel implements IServerChannel { - private readonly _onText = new Emitter(); + private readonly _onText = new Emitter(); readonly onText = this._onText.event; - private readonly _onFinalMessage = new Emitter(); + private readonly _onFinalMessage = new Emitter(); readonly onFinalMessage = this._onFinalMessage.event; - private readonly _onError = new Emitter(); + private readonly _onError = new Emitter(); readonly onError = this._onError.event; constructor() { } @@ -45,13 +45,13 @@ export class LLMMessageChannel implements IServerChannel { } // both use this - async call(_: unknown, command: string, params: SendLLMMessageProxyParams): Promise { + async call(_: unknown, command: string, params: ProxyLLMMessageParams): Promise { if (command !== 'sendLLMMessage') throw new Error(`Invalid call in sendLLMMessage channel: ${command}.\nArgs:\n${JSON.stringify(params, null, 5)}`); try { const { requestId } = params; - const mainThreadParams: SendLLMMessageParams = { + const mainThreadParams: LLMMessageServiceParams = { ...params, onText: ({ newText, fullText }) => { this._onText.fire({ requestId, newText, fullText }); }, onFinalMessage: ({ fullText }) => { this._onFinalMessage.fire({ requestId, fullText }); }, diff --git a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx index 74049ef1..5b62674c 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx @@ -17,7 +17,7 @@ import { URI } from '../../../../../../../base/common/uri.js'; import { EndOfLinePreference } from '../../../../../../../editor/common/model.js'; import { IDisposable } from '../../../../../../../base/common/lifecycle.js'; import { ErrorDisplay } from '../util/ErrorDisplay.js'; -import { SendLLMMessageParams } from '../../../../../../../platform/void/common/sendLLMTypes.js'; +import { LLMMessageServiceParams } from '../../../../../../../platform/void/common/llmMessageTypes.js'; // import { } from '@vscode/webview-ui-toolkit/react'; @@ -211,7 +211,7 @@ export const SidebarChat = () => { // send message to LLM - const object: SendLLMMessageParams = { + const object: LLMMessageServiceParams = { logging: { loggingName: 'Chat' }, messages: [...(currentThread?.messages ?? []).map(m => ({ role: m.role, content: m.content })),], onText: ({ newText, fullText }) => setMessageStream(fullText), @@ -241,8 +241,6 @@ export const SidebarChat = () => { abortRef: abortFnRef, } - console.log('object!!!!!2', Object.keys(object)) - sendLLMMessageService.sendLLMMessage(object) diff --git a/src/vs/workbench/contrib/void/browser/react/src/util/sendLLMMessage.tsx b/src/vs/workbench/contrib/void/browser/react/src/util/sendLLMMessage.tsx index 8dcf6769..7457a447 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/util/sendLLMMessage.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/util/sendLLMMessage.tsx @@ -4,12 +4,12 @@ import { Ollama } from 'ollama/browser' import { Content, GoogleGenerativeAI, GoogleGenerativeAIFetchError } from '@google/generative-ai'; import { posthog } from 'posthog-js' import type { VoidConfig } from '../../../registerConfig.js'; -import type { LLMMessage, LLMMessageOnText, OnError, OnFinalMessage, } from '../../../../../../../platform/void/common/sendLLMTypes.js'; -import { SendLLMMessageParams } from '../../../../../../../platform/void/common/sendLLMTypes.js'; +import type { LLMMessage, OnText, OnError, OnFinalMessage, } from '../../../../../../../platform/void/common/llmMessageTypes.js'; +import { LLMMessageServiceParams } from '../../../../../../../platform/void/common/llmMessageTypes.js'; type SendLLMMessageFnTypeInternal = (params: { messages: LLMMessage[]; - onText: LLMMessageOnText; + onText: OnText; onFinalMessage: OnFinalMessage; onError: OnError; voidConfig: VoidConfig; @@ -284,7 +284,7 @@ export const sendLLMMessage = ({ abortRef: abortRef_, voidConfig, logging: { loggingName } -}: SendLLMMessageParams) => { +}: LLMMessageServiceParams) => { if (!voidConfig) return; // trim message content (Anthropic and other providers give an error if there is trailing whitespace) @@ -307,7 +307,7 @@ export const sendLLMMessage = ({ let _setAborter = (fn: () => void) => { _aborter = fn } let _didAbort = false - const onText: LLMMessageOnText = ({ newText, fullText }) => { + const onText: OnText = ({ newText, fullText }) => { if (_didAbort) return onText_({ newText, fullText }) _fullTextSoFar = fullText diff --git a/src/vs/workbench/contrib/void/browser/registerInlineDiffs.ts b/src/vs/workbench/contrib/void/browser/registerInlineDiffs.ts index 69d2482c..1946cef2 100644 --- a/src/vs/workbench/contrib/void/browser/registerInlineDiffs.ts +++ b/src/vs/workbench/contrib/void/browser/registerInlineDiffs.ts @@ -28,8 +28,8 @@ import { ILanguageService } from '../../../../editor/common/languages/language.j import * as dom from '../../../../base/browser/dom.js'; import { Widget } from '../../../../base/browser/ui/widget.js'; import { URI } from '../../../../base/common/uri.js'; -import { SendLLMMessageParams } from '../../../../platform/void/common/sendLLMTypes.js'; -import { ISendLLMMessageService } from '../../../../platform/void/browser/sendLLMMessage.js'; +import { LLMMessageServiceParams } from '../../../../platform/void/common/llmMessageTypes.js'; +import { ISendLLMMessageService } from '../../../../platform/void/browser/llmMessageService.js'; // import { ISendLLMMessageService } from '../../../../platform/void/common/sendLLMMessage.js'; // import { sendLLMMessage } from './react/out/util/sendLLMMessage.js'; @@ -734,7 +734,7 @@ Please finish writing the new file by applying the diff to the original file. Re const abortRef = { current: null } as { current: null | (() => void) } await new Promise((resolve, reject) => { - const object: SendLLMMessageParams = { + const object: LLMMessageServiceParams = { logging: { loggingName: 'streamChunk' }, messages: [ { role: 'system', content: writeFileWithDiffInstructions, }, @@ -764,7 +764,6 @@ Please finish writing the new file by applying the diff to the original file. Re abortRef, } - console.log('object!!!!!', Object.keys(object)) this._sendLLMMessageService.sendLLMMessage(object) }) diff --git a/src/vs/workbench/contrib/void/browser/registerSidebar.ts b/src/vs/workbench/contrib/void/browser/registerSidebar.ts index c3e6a768..cddd9853 100644 --- a/src/vs/workbench/contrib/void/browser/registerSidebar.ts +++ b/src/vs/workbench/contrib/void/browser/registerSidebar.ts @@ -47,7 +47,7 @@ import { IVoidConfigStateService } from './registerConfig.js'; import { IFileService } from '../../../../platform/files/common/files.js'; import { IInlineDiffsService } from './registerInlineDiffs.js'; import { IModelService } from '../../../../editor/common/services/model.js'; -import { ISendLLMMessageService } from '../../../../platform/void/browser/sendLLMMessage.js'; +import { ISendLLMMessageService } from '../../../../platform/void/browser/llmMessageService.js'; // import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.js'; diff --git a/src/vs/workbench/workbench.common.main.ts b/src/vs/workbench/workbench.common.main.ts index 0c3b9dd3..0f33a1d1 100644 --- a/src/vs/workbench/workbench.common.main.ts +++ b/src/vs/workbench/workbench.common.main.ts @@ -17,7 +17,7 @@ import './browser/workbench.contribution.js'; //#region --- Void // Void added this: import './contrib/void/browser/void.contribution.js'; -import '../platform/void/browser/sendLLMMessage.js'; +import '../platform/void/browser/llmMessageService.js'; //#endregion