Skip to content

Commit

Permalink
fix: lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
CGQAQ committed Dec 6, 2023
1 parent d95be6d commit 97c7672
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 67 deletions.
9 changes: 5 additions & 4 deletions web/studio/app/api/completion/qwen/qwen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,22 +303,23 @@ type OverrideOpenAIChatCompletionCreateParams = {
top_k?: number | null;
};

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace QWenAI {
export type ChatModel = "qwen-turbo" | "qwen-plus" | "qwen-max";
export type ChatModel = 'qwen-turbo' | 'qwen-plus' | 'qwen-max';

/**
* - text 旧版本的 text
* - message 兼容 openai 的 message
*
* @defaultValue "text"
*/
export type ResultFormat = "text" | "message";
export type ResultFormat = 'text' | 'message';

export type ChatCompletionInputParam = {
/**
* 聊天上下文信息
*/
messages: OpenAI.ChatCompletionCreateParams["messages"];
messages: OpenAI.ChatCompletionCreateParams['messages'];
};

export type ChatCompletionParameters = {
Expand Down Expand Up @@ -414,7 +415,7 @@ export namespace QWenAI {
request_id: string;
output: {
text: string;
finish_reason: "stop" | "length";
finish_reason: 'stop' | 'length';
};
usage: {
output_tokens: number;
Expand Down
118 changes: 61 additions & 57 deletions web/studio/app/api/completion/qwen/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,58 @@ export type ServerSentEvent = {
}

export class SSEDecoder {
private data: string[]
private event: string | null
private chunks: string[]
private data: string[];
private event: string | null;
private chunks: string[];

constructor() {
this.event = null
this.data = []
this.chunks = []
this.event = null;
this.data = [];
this.chunks = [];
}

decode(line: string) {
if (line.endsWith('\r')) {
line = line.substring(0, line.length - 1)
line = line.substring(0, line.length - 1);
}

if (!line) {
// empty line and we didn't previously encounter any messages
if (!this.event && !this.data.length) return null
if (!this.event && !this.data.length) return null;

const sse: ServerSentEvent = {
event: this.event,
data: this.data.join('\n'),
raw: this.chunks,
}
};

this.event = null
this.data = []
this.chunks = []
this.event = null;
this.data = [];
this.chunks = [];

return sse
return sse;
}

this.chunks.push(line)
this.chunks.push(line);

if (line.startsWith(':')) {
return null
return null;
}

let [fieldname, _, value] = partition(line, ':')
// eslint-disable-next-line prefer-const
let [fieldname, , value] = partition(line, ':');

if (value.startsWith(' ')) {
value = value.substring(1)
value = value.substring(1);
}

if (fieldname === 'event') {
this.event = value
this.event = value;
} else if (fieldname === 'data') {
this.data.push(value)
this.data.push(value);
}

return null
return null;
}
}

Expand All @@ -72,99 +73,102 @@ export class SSEDecoder {
export class LineDecoder {
// prettier-ignore
static NEWLINE_CHARS = new Set(['\n', '\r', '\x0b', '\x0c', '\x1c', '\x1d', '\x1e', '\x85', '\u2028', '\u2029']);
static NEWLINE_REGEXP = /\r\n|[\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029]/g
// eslint-disable-next-line no-control-regex
static NEWLINE_REGEXP = /\r\n|[\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029]/g;

buffer: string[]
trailingCR: boolean
textDecoder: any // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types.
buffer: string[];
trailingCR: boolean;
textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types.

constructor() {
this.buffer = []
this.trailingCR = false
this.buffer = [];
this.trailingCR = false;
}

decode(chunk: Bytes): string[] {
let text = this.decodeText(chunk)
let text = this.decodeText(chunk);

if (this.trailingCR) {
text = '\r' + text
this.trailingCR = false
text = '\r' + text;
this.trailingCR = false;
}
if (text.endsWith('\r')) {
this.trailingCR = true
text = text.slice(0, -1)
this.trailingCR = true;
text = text.slice(0, -1);
}

if (!text) {
return []
return [];
}

const trailingNewline = LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || '')
let lines = text.split(LineDecoder.NEWLINE_REGEXP)
const trailingNewline = LineDecoder.NEWLINE_CHARS.has(
text[text.length - 1] || ''
);
let lines = text.split(LineDecoder.NEWLINE_REGEXP);

if (lines.length === 1 && !trailingNewline) {
this.buffer.push(lines[0]!)
return []
this.buffer.push(lines[0]!);
return [];
}

if (this.buffer.length > 0) {
lines = [this.buffer.join('') + lines[0], ...lines.slice(1)]
this.buffer = []
lines = [this.buffer.join('') + lines[0], ...lines.slice(1)];
this.buffer = [];
}

if (!trailingNewline) {
this.buffer = [lines.pop() || '']
this.buffer = [lines.pop() || ''];
}

return lines
return lines;
}

decodeText(bytes: Bytes): string {
if (bytes == null) return ''
if (typeof bytes === 'string') return bytes
if (bytes == null) return '';
if (typeof bytes === 'string') return bytes;

// Node:
if (typeof Buffer !== 'undefined') {
if (bytes instanceof Buffer) {
return bytes.toString()
return bytes.toString();
}
if (bytes instanceof Uint8Array) {
return Buffer.from(bytes).toString()
return Buffer.from(bytes).toString();
}

throw new OpenAIError(
`Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`,
)
`Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`
);
}

// Browser
if (typeof TextDecoder !== 'undefined') {
if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
this.textDecoder ??= new TextDecoder('utf8')
return this.textDecoder.decode(bytes)
this.textDecoder ??= new TextDecoder('utf8');
return this.textDecoder.decode(bytes);
}

throw new OpenAIError(
`Unexpected: received non-Uint8Array/ArrayBuffer (${
(bytes as any).constructor.name
}) in a web platform. Please report this error.`,
)
}) in a web platform. Please report this error.`
);
}

throw new OpenAIError(
`Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.`,
)
`Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.`
);
}

flush(): string[] {
if (!this.buffer.length && !this.trailingCR) {
return []
return [];
}

const lines = [this.buffer.join('')]
this.buffer = []
this.trailingCR = false
return lines
const lines = [this.buffer.join('')];
this.buffer = [];
this.trailingCR = false;
return lines;
}
}

Expand Down
13 changes: 7 additions & 6 deletions web/studio/app/api/completion/yiyan/erniebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,13 @@ type OverrideOpenAIChatCompletionCreateParams = {
enable_citation?: boolean | null;
};

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace ErnieBot {
export type ChatModel =
| "ernie-bot"
| "ernie-bot-turbo"
| "ernie-bot-4"
| "ernie-bot-8k";
| 'ernie-bot'
| 'ernie-bot-turbo'
| 'ernie-bot-4'
| 'ernie-bot-8k';

export interface ChatCompletionCreateParams {
/**
Expand Down Expand Up @@ -402,12 +403,12 @@ export namespace ErnieBot {
*
* @remarks 不支持 system 角色
*/
messages: OpenAI.ChatCompletionCreateParams["messages"];
messages: OpenAI.ChatCompletionCreateParams['messages'];

/**
* 一个可触发函数的描述列表
*/
functions?: OpenAI.ChatCompletionCreateParams["functions"];
functions?: OpenAI.ChatCompletionCreateParams['functions'];

/**
* 内容随机性
Expand Down

0 comments on commit 97c7672

Please sign in to comment.