Skip to content

Commit

Permalink
fix(agent): fix readable stream in browser. (TabbyML#2359)
Browse files Browse the repository at this point in the history
* fix(agent): fix readable stream in browser.

* [autofix.ci] apply automated fixes

* fix: revert error serialize.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
icycodes and autofix-ci[bot] authored Jun 7, 2024
1 parent 9c85f55 commit 34052fb
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 22 deletions.
3 changes: 2 additions & 1 deletion clients/tabby-agent/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
}
},
"ignorePatterns": ["dist", "node_modules"]
}
5 changes: 4 additions & 1 deletion clients/tabby-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/mocha": "^10.0.1",
"@types/node": "18.x",
"@types/object-hash": "^3.0.0",
"@types/readable-stream": "^4.0.14",
"@types/semver": "^7.5.8",
"@types/uuid": "^9.0.0",
"@types/win-ca": "^3.5.4",
Expand All @@ -57,7 +58,7 @@
"deepmerge-ts": "^5.1.0",
"diff": "^5.2.0",
"dot-prop": "^8.0.2",
"esbuild-plugin-copy": "^2.1.1",
"esbuild": "^0.19.12",
"esbuild-plugin-polyfill-node": "^0.3.0",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.0.0",
Expand All @@ -75,6 +76,8 @@
"openapi-typescript": "^6.6.1",
"pino": "^8.14.1",
"prettier": "^3.0.0",
"readable-from-web": "^1.0.0",
"readable-stream": "^4.5.2",
"semver": "^7.6.0",
"stats-logscale": "^1.0.9",
"tabby-openapi": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion clients/tabby-agent/src/TabbyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getProperty, setProperty, deleteProperty } from "dot-prop";
import createClient from "openapi-fetch";
import type { ParseAs } from "openapi-fetch";
import * as semver from "semver";
import { Readable } from "node:stream";
import { Readable } from "readable-stream";
import type { paths as TabbyApi, components as TabbyApiComponents } from "tabby-openapi/compatible";
import type {
Agent,
Expand Down
2 changes: 1 addition & 1 deletion clients/tabby-agent/src/lsp/ChatEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "./protocol";
import { TextDocuments } from "./TextDocuments";
import { TextDocument } from "vscode-languageserver-textdocument";
import { Readable } from "node:stream";
import { Readable } from "readable-stream";
import cryptoRandomString from "crypto-random-string";
import * as Diff from "diff";
import { TabbyAgent } from "../TabbyAgent";
Expand Down
6 changes: 3 additions & 3 deletions clients/tabby-agent/src/stream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Readable } from "node:stream";
import type { ReadableStream as NodeReadableStream } from "node:stream/web";
import { Readable } from "readable-stream";
import { readableFromWeb } from "readable-from-web";
import { EventSourceParserStream, ParsedEvent } from "eventsource-parser/stream";
import type { components as TabbyApiComponents } from "tabby-openapi/compatible";
import { getLogger } from "./logger";
Expand All @@ -8,7 +8,7 @@ const logger = getLogger("StreamParser");

export function readChatStream(stream: ReadableStream, signal?: AbortSignal): Readable {
const eventStream = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream());
const readableStream = Readable.fromWeb(eventStream as NodeReadableStream<ParsedEvent>, { objectMode: true, signal });
const readableStream = readableFromWeb(eventStream, { objectMode: true });
return readableStream.map(
(event: ParsedEvent): string | undefined => {
try {
Expand Down
4 changes: 2 additions & 2 deletions clients/tabby-agent/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ export function isUnauthorizedError(error: any) {
return error instanceof HttpError && [401, 403].includes(error.status);
}

export function errorToString(error: Error & { cause?: Error }) {
export function errorToString(error: Error) {
let message = error.message || error.toString();
if (error.cause) {
if (error.cause instanceof Error) {
message += "\nCaused by: " + errorToString(error.cause);
}
return message;
Expand Down
4 changes: 2 additions & 2 deletions clients/tabby-agent/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"lib": ["ES2020", "WebWorker"],
"lib": ["esnext", "webworker"],
"target": "es2020",
"sourceMap": true,
"esModuleInterop": true,
"resolveJsonModule": true,
Expand Down
4 changes: 2 additions & 2 deletions clients/tabby-agent/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { defineConfig } from "tsup";
import type { Plugin, PluginBuild } from "esbuild";
import path from "path";
import fs from "fs-extra";
import type { Plugin } from "esbuild";
import { polyfillNode } from "esbuild-plugin-polyfill-node";
import dedent from "dedent";

function processWinCa(copyRootsExe: boolean = false): Plugin {
return {
name: "processWinCa",
setup: (build) => {
setup: (build: PluginBuild) => {
build.onLoad({ filter: /win-ca\/lib\/crypt32-\w*.node$/ }, async () => {
// As win-ca fallback is used, skip not required `.node` binaries
return {
Expand Down
3 changes: 2 additions & 1 deletion clients/vscode/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"plugin:@typescript-eslint/strict",
"plugin:@typescript-eslint/stylistic",
"prettier"
]
],
"ignorePatterns": ["dist", "node_modules"]
}
7 changes: 2 additions & 5 deletions clients/vscode/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,8 @@ export class Commands {
this.chatEditCancellationTokenSource.token,
);
} catch (error) {
if (typeof error === "object" && error && "name" in error) {
error.name === "ChatEditDocumentTooLongError";
window.showErrorMessage(
"The selected text is too long to edit, please select less text and try again.",
);
if (typeof error === "object" && error && "message" in error && typeof error["message"] === "string") {
window.showErrorMessage(error["message"]);
}
}
this.chatEditCancellationTokenSource.dispose();
Expand Down
31 changes: 28 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit 34052fb

Please sign in to comment.