continue-custom/binary/src/TcpMessenger.ts
Nate Sesti ca884b8212
fix ollama onboarding (#1539)
* allow setting apiBase for bedrock provider

* ⚡️ better linking

* ✨ new prompt file action

* ⚡️ strip <COMPLETION> from GPT-4 completion

* continue proxy FIM support

* warn user if potentially dangerous command is generated

* tab autocomplete docs

* docs: update PR template (#1531)

* chore: add docs to install script (#1533)

* fix(extensions): schema for db ctx provider (#1534)

* fix(extensions): schema for db ctx provider

* fix: invalid enum

* prettierignore

* docs: add docs and schema for "OS" provider (#1536)

* tests (#1501)

* 👷 CI for jetbrains

* default working dir

* changelog

* build binaries

* binary testing setup

* idesettings

* core binary testing

* run binary tests in ci

* remove unused targets

* needs build

* console.log bin contents

* fix ci

* fix win32 binary download

* test

* no linux arm64

* macos latest

* macos-12

* binary permissions

* upload logs

* fix

* upload full folder as binary artifact

* test

* test macos only

* set full execute permissions

* copy sqlite binary

* cd

* it worked!

* build again in test job

* debug

* remove timeout

* info

* log

* log2

* more logs

* catch

* fewer logs

* test all platforms

* test downloaded artifact

* needs build

* updates

* build

* false

* release

* add tag and upload binaryes

* change tag name

* proper artifact upload

* jest updates

* ✅ generate a few unit tests with Continue

* fix imports related to IdeSettings

* run tsc on PRs

* remove shareSession command (unused)

* update release process

* update plugin version

* don't show ghost text when jetbrains completion visible

* run jetbrains ci in main

* check ts in dev

* ignore .env

* 🚑 fix constant warnings when onboarding with Ollama

---------

Co-authored-by: Patrick Erichsen <patrick.a.erichsen@gmail.com>
2024-06-21 11:09:03 -07:00

168 lines
4.7 KiB
TypeScript

import { IProtocol } from "core/protocol";
import { IMessenger, Message } from "core/util/messenger";
import net from "net";
import { v4 as uuidv4 } from "uuid";
export class TcpMessenger<
ToProtocol extends IProtocol,
FromProtocol extends IProtocol,
> implements IMessenger<ToProtocol, FromProtocol>
{
private port: number = 3000;
private host: string = "127.0.0.1";
private socket: net.Socket | null = null;
typeListeners = new Map<keyof ToProtocol, ((message: Message) => any)[]>();
idListeners = new Map<string, (message: Message) => any>();
constructor() {
const server = net.createServer((socket) => {
this.socket = socket;
socket.on("connect", () => {
console.log("Connected to server");
});
socket.on("data", (data: Buffer) => {
this._handleData(data);
});
socket.on("end", () => {
console.log("Disconnected from server");
});
socket.on("error", (err: any) => {
console.error("Client error:", err);
});
});
server.listen(this.port, this.host, () => {
console.log(`Server listening on port ${this.port}`);
});
}
private _onErrorHandlers: ((error: Error) => void)[] = [];
onError(handler: (error: Error) => void) {
this._onErrorHandlers.push(handler);
}
public async awaitConnection() {
while (!this.socket) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
}
private _handleLine(line: string) {
try {
const msg: Message = JSON.parse(line);
if (msg.messageType === undefined || msg.messageId === undefined) {
throw new Error("Invalid message sent: " + JSON.stringify(msg));
}
// Call handler and respond with return value
const listeners = this.typeListeners.get(msg.messageType as any);
listeners?.forEach(async (handler) => {
try {
const response = await handler(msg);
if (
response &&
typeof response[Symbol.asyncIterator] === "function"
) {
for await (const update of response) {
this.send(msg.messageType, update, msg.messageId);
}
this.send(msg.messageType, { done: true }, msg.messageId);
} else {
this.send(msg.messageType, response || {}, msg.messageId);
}
} catch (e: any) {
console.warn(`Error running handler for "${msg.messageType}": `, e);
this._onErrorHandlers.forEach((handler) => {
handler(e);
});
}
});
// Call handler which is waiting for the response, nothing to return
this.idListeners.get(msg.messageId)?.(msg);
} catch (e) {
let truncatedLine = line;
if (line.length > 200) {
truncatedLine =
line.substring(0, 100) + "..." + line.substring(line.length - 100);
}
console.error("Error parsing line: ", truncatedLine, e);
return;
}
}
private _unfinishedLine: string | undefined = undefined;
private _handleData(data: Buffer) {
const d = data.toString();
const lines = d.split(/\r\n/).filter((line) => line.trim() !== "");
if (lines.length === 0) {
return;
}
if (this._unfinishedLine) {
lines[0] = this._unfinishedLine + lines[0];
this._unfinishedLine = undefined;
}
if (!d.endsWith("\r\n")) {
this._unfinishedLine = lines.pop();
}
lines.forEach((line) => this._handleLine(line));
}
send<T extends keyof FromProtocol>(
messageType: T,
data: FromProtocol[T][0],
messageId?: string,
): string {
messageId = messageId ?? uuidv4();
const msg: Message = {
messageType: messageType as string,
data,
messageId,
};
this.socket?.write(JSON.stringify(msg) + "\r\n");
return messageId;
}
on<T extends keyof ToProtocol>(
messageType: T,
handler: (message: Message<ToProtocol[T][0]>) => ToProtocol[T][1],
): void {
if (!this.typeListeners.has(messageType)) {
this.typeListeners.set(messageType, []);
}
this.typeListeners.get(messageType)?.push(handler);
}
invoke<T extends keyof ToProtocol>(
messageType: T,
data: ToProtocol[T][0],
): ToProtocol[T][1] {
return this.typeListeners.get(messageType)?.[0]?.({
messageId: uuidv4(),
messageType: messageType as string,
data,
});
}
request<T extends keyof FromProtocol>(
messageType: T,
data: FromProtocol[T][0],
): Promise<FromProtocol[T][1]> {
const messageId = uuidv4();
return new Promise((resolve) => {
const handler = (msg: Message) => {
resolve(msg.data);
this.idListeners.delete(messageId);
};
this.idListeners.set(messageId, handler);
this.send(messageType, data, messageId);
});
}
}