Skip to content

Commit

Permalink
development > main (#393)
Browse files Browse the repository at this point in the history
* fittencode_prevent

* fix#392

* lint

* 3.19.2

* lint

* lint

* lint

---------

Co-authored-by: ZhongGs <[email protected]>
  • Loading branch information
rjmacarthy and ZhongGuangshen authored Nov 14, 2024
1 parent 2f83c2c commit eec6001
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "twinny",
"displayName": "twinny - AI Code Completion and Chat",
"description": "Locally hosted AI code completion plugin for vscode",
"version": "3.19.1",
"version": "3.19.2",
"icon": "assets/icon.png",
"keywords": [
"code-inference",
Expand Down
6 changes: 6 additions & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,9 @@ export const MULTI_LINE_DELIMITERS = ["\n\n", "\r\n\r\n"]
export const SYMMETRY_EMITTER_KEY = {
inference: "inference",
}

//Define an array containing all the error messages that need to be detected when fetch error occurred
export const knownErrorMessages = [
"First parameter has member 'readable' that is not a ReadableStream.", //This error occurs When plugins such as Fitten Code are enabled
"The 'transform.readable' property must be an instance of ReadableStream. Received an instance of h" //When you try to enable the Node.js compatibility mode Compat to solve the problem, this error may pop up
];
23 changes: 22 additions & 1 deletion src/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { Base } from "../extension/base"
export class Logger extends Base {
private static instance: Logger

constructor () {
public static ErrorType = {
Default: 0, // default color
Fetch_Error: 91, // red -- fetch error
Abort: 90, // gray -- abort
Timeout: 33 // yellow -- timeout
}

constructor() {
super()
}

Expand All @@ -23,6 +30,20 @@ export class Logger extends Base {
if (!this.config.enableLogging) return
console.error(err.message)
}

public logConsoleError(
errorKey: number,
message: string,
error: Error | string
): void {
const color = errorKey
const coloredMessage = `\x1b[91m [ERROR_twinny] \x1b[32m Message: ${message} \n \x1b[${color}m Error Type: ${
error instanceof Error ? error.name : "Unknown Error"
} \n Error Message: ${
error instanceof Error ? error.message : error
} \n \x1b[31m`
console.error(coloredMessage, error)
}
}

export const logger = Logger.getInstance()
1 change: 1 addition & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export interface ServerMessage<T = LanguageType> {
type: string
}
}

export interface Message {
role: string
content: string | undefined
Expand Down
36 changes: 26 additions & 10 deletions src/extension/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Logger } from "../common/logger"
import { StreamRequest } from "../common/types"

import { logStreamOptions, safeParseJsonResponse } from "./utils"
import {
logStreamOptions,
notifyKnownErrors,
safeParseJsonResponse
} from "./utils"

const log = Logger.getInstance()

export async function streamResponse(request: StreamRequest) {
logStreamOptions(request)
Expand All @@ -11,15 +18,22 @@ export async function streamResponse(request: StreamRequest) {
const timeOut = setTimeout(() => {
controller.abort()
onError?.(new Error("Request timed out"))
log.logConsoleError(
Logger.ErrorType.Timeout,
"Failed to establish connection",
new Error("Request timed out")
)
}, 25000)

try {
const url = `${options.protocol}://${options.hostname}${options.port ? `:${options.port}` : ""}${options.path}`
const url = `${options.protocol}://${options.hostname}${
options.port ? `:${options.port}` : ""
}${options.path}`
const fetchOptions = {
method: options.method,
headers: options.headers,
body: JSON.stringify(body),
signal: controller.signal,
signal: controller.signal
}

const response = await fetch(url, fetchOptions)
Expand Down Expand Up @@ -67,7 +81,7 @@ export async function streamResponse(request: StreamRequest) {
onError?.(new Error("Error parsing JSON data from event"))
}
}
},
}
})
)
.getReader()
Expand All @@ -89,8 +103,9 @@ export async function streamResponse(request: StreamRequest) {
if (error.name === "AbortError") {
onEnd?.()
} else {
console.error("Fetch error:", error)
log.logConsoleError(Logger.ErrorType.Fetch_Error, "Fetch error", error)
onError?.(error)
notifyKnownErrors(error)
}
}
}
Expand All @@ -100,14 +115,15 @@ export async function fetchEmbedding(request: StreamRequest) {
const { body, options, onData } = request
const controller = new AbortController()


try {
const url = `${options.protocol}://${options.hostname}${options.port ? `:${options.port}` : ""}${options.path}`
const url = `${options.protocol}://${options.hostname}${
options.port ? `:${options.port}` : ""
}${options.path}`
const fetchOptions = {
method: options.method,
headers: options.headers,
body: JSON.stringify(body),
signal: controller.signal,
signal: controller.signal
}

const response = await fetch(url, fetchOptions)
Expand All @@ -123,10 +139,10 @@ export async function fetchEmbedding(request: StreamRequest) {
const data = await response.json()

onData(data)

} catch (error: unknown) {
if (error instanceof Error) {
console.error("Fetch error:", error)
log.logConsoleError(Logger.ErrorType.Fetch_Error, "Fetch error", error)
notifyKnownErrors(error)
}
}
}
2 changes: 1 addition & 1 deletion src/extension/chat-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export class ChatService extends Base {
type: EVENT_NAME.twinnyOnEnd,
value: {
error: true,
errorMessage: error.message,
errorMessage: `==## ERROR ##== : ${error.message}`, // Highlight errors on webview
},
} as ServerMessage)
}
Expand Down
23 changes: 23 additions & 0 deletions src/extension/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs"
import ignore from "ignore"
import path from "path"
import * as util from "util"
import * as vscode from "vscode"
import {
ColorThemeKind,
ExtensionContext,
Expand All @@ -24,6 +25,7 @@ import {
defaultChunkOptions,
EVENT_NAME,
EXTENSION_CONTEXT_NAME,
knownErrorMessages,
LINE_BREAK_REGEX,
MULTILINE_TYPES,
NORMALIZE_REGEX,
Expand Down Expand Up @@ -763,3 +765,24 @@ const calculateTotalCharacters = (messages: Message[] | undefined): number => {
return acc + (typeof msg.content === "string" ? msg.content.length : 0)
}, 0)
}

export function notifyKnownErrors(error: Error) {
if (knownErrorMessages.some((msg) => error.message.includes(msg))) {
vscode.window
.showInformationMessage(
"Besides Twinny, there may be other AI extensions being enabled (such as Fitten Code) that are affecting the behavior of the fetch API or ReadableStream used in the Twinny plugin. We recommend that you disable that AI plugin for the smooth use of Twinny",
"View extensions",
"Restart Visual Studio Code (after disabling related extensions)"
)
.then((selected) => {
if (selected === "View extensions") {
vscode.commands.executeCommand("workbench.view.extensions")
} else if (
selected ===
"Restart Visual Studio Code (after disabling related extensions)"
) {
vscode.commands.executeCommand("workbench.action.reloadWindow")
}
})
}
}

0 comments on commit eec6001

Please sign in to comment.