Skip to content

Commit

Permalink
fix(openapi): fix chat completion api to openai compatible. (#2632)
Browse files Browse the repository at this point in the history
* fix(openapi): fix chat completion api to openai compatible.

* fix(openapi): fix types compatibility issues.
  • Loading branch information
icycodes authored Jul 13, 2024
1 parent 4e1e539 commit 8778da4
Show file tree
Hide file tree
Showing 14 changed files with 813 additions and 577 deletions.
1 change: 0 additions & 1 deletion clients/tabby-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
"mocha": "^10.2.0",
"object-hash": "^3.0.0",
"openapi-fetch": "^0.7.6",
"openapi-typescript": "^6.6.1",
"pino": "^8.14.1",
"prettier": "^3.0.0",
"readable-from-web": "^1.0.0",
Expand Down
18 changes: 14 additions & 4 deletions clients/tabby-agent/src/TabbyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,14 +831,19 @@ export class TabbyAgent extends EventEmitter implements Agent {
throw new Error("http client not initialized");
}
const requestPath = options?.useBetaVersion ? "/v1beta/chat/completions" : "/v1/chat/completions";
const messages = [
// FIXME(@icycodes): use openai for nodejs instead of tabby-openapi schema
const messages: { role: "user"; content: string }[] = [
{
role: "user",
content: promptTemplate.replace("{{diff}}", selectedDiff),
},
];
const requestOptions = {
body: { messages },
body: {
messages,
model: "",
stream: true,
},
signal: this.createAbortSignal(options),
parseAs: "stream" as ParseAs,
};
Expand Down Expand Up @@ -930,7 +935,8 @@ export class TabbyAgent extends EventEmitter implements Agent {
throw new Error("http client not initialized");
}
const requestPath = options?.useBetaVersion ? "/v1beta/chat/completions" : "/v1/chat/completions";
const messages = [
// FIXME(@icycodes): use openai for nodejs instead of tabby-openapi schema
const messages: { role: "user"; content: string }[] = [
{
role: "user",
content: promptTemplate.replace(
Expand All @@ -957,7 +963,11 @@ export class TabbyAgent extends EventEmitter implements Agent {
},
];
const requestOptions = {
body: { messages },
body: {
messages,
model: "",
stream: true,
},
signal: this.createAbortSignal(options),
parseAs: "stream" as ParseAs,
};
Expand Down
7 changes: 6 additions & 1 deletion clients/tabby-agent/src/lsp/ChatEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,12 @@ export class ChatEditProvider {
}
} catch (error) {
await finalize("stopped");
throw error;
// FIXME(@icycodes): use openai for nodejs instead of tabby-openapi schema
if (error instanceof TypeError && error.message.startsWith("terminated")) {
// ignore server side close error
} else {
throw error;
}
}
await finalize("completed");
}
Expand Down
15 changes: 14 additions & 1 deletion clients/tabby-agent/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,18 @@ export async function parseChatResponse(response: Response, signal?: AbortSignal
return "";
}
const readableStream = readChatStream(response.body, signal);
return readableStream.reduce<string>((text, delta) => text + delta, "", { signal });
// FIXME(@icycodes): use openai for nodejs instead of tabby-openapi schema
let output = "";
try {
for await (const delta of readableStream) {
output += delta;
}
} catch (error) {
if (error instanceof TypeError && error.message.startsWith("terminated")) {
// ignore server side close error
} else {
throw error;
}
}
return output;
}
20 changes: 8 additions & 12 deletions clients/tabby-openapi/compatible/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,28 @@ import {
webhooks as _webhooks,
components as _components,
$defs as _$defs,
external as _external,
operations as _operations,
} from "../lib";

export interface paths extends _paths {
"/v1/health": _paths["/v1/health"] & {
// backward compatible for Tabby server 0.2.x and earlier
export interface paths extends Omit<_paths, "/v1/health"> {
// backward compatible for Tabby server 0.2.x and earlier
"/v1/health": Omit<_paths["/v1/health"], "post"> & {
post: operations["health"];
};
// backward compatible for Tabby server 0.10.x and earlier
"/v1beta/chat/completions": {
post: operations["chat_completions"];
};
"/v1beta/chat/completions": _paths["/v1/chat/completions"];
}

export type webhooks = _webhooks;
export type components = _components;
export type $defs = _$defs;
export type external = _external;

export interface operations extends _operations {
event: _operations["event"] & {
export interface operations extends Omit<_operations, "event"> {
event: Omit<_operations["event"], "parameters"> & {
// Add a query parameter to specify the select kind
parameters: {
parameters: Omit<_operations["event"]["parameters"], "query"> & {
query: {
select_kind?: string | null;
select_kind?: string | undefined | null;
};
};
};
Expand Down
Loading

0 comments on commit 8778da4

Please sign in to comment.