Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed empty response issue #98

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ async function getResponse({

const iterableStream = streamToIterable(stream);

return { readResponse: readData(iterableStream, () => true) };
return { readResponse: readData(iterableStream) };
}
53 changes: 25 additions & 28 deletions src/helpers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { AxiosError } from 'axios';
import { streamToString } from './stream-to-string';
import './replace-all-polyfill';
import i18n from './i18n';
import { stripRegexPatterns } from './strip-regex-patterns';

const explainInSecondRequest = true;

Expand All @@ -20,7 +21,7 @@ function getOpenAi(key: string, apiEndpoint: string) {

// Openai outputs markdown format for code blocks. It oftne uses
// a github style like: "```bash"
const shellCodeStartRegex = /```[^\n]*/gi;
const shellCodeExclusions = [/```[a-zA-Z]*\n/gi, /```[a-zA-Z]*/gi, '\n'];

export async function getScriptAndInfo({
prompt,
Expand All @@ -42,14 +43,9 @@ export async function getScriptAndInfo({
apiEndpoint,
});
const iterableStream = streamToIterable(stream);
const codeBlock = '```';
return {
readScript: readData(iterableStream, () => true, shellCodeStartRegex),
readInfo: readData(
iterableStream,
(content) => content.endsWith(codeBlock),
shellCodeStartRegex
),
readScript: readData(iterableStream, ...shellCodeExclusions),
readInfo: readData(iterableStream, ...shellCodeExclusions),
};
}

Expand Down Expand Up @@ -154,7 +150,7 @@ export async function getExplanation({
apiEndpoint,
});
const iterableStream = streamToIterable(stream);
return { readExplanation: readData(iterableStream, () => true) };
return { readExplanation: readData(iterableStream) };
}

export async function getRevision({
Expand All @@ -180,22 +176,24 @@ export async function getRevision({
});
const iterableStream = streamToIterable(stream);
return {
readScript: readData(iterableStream, () => true),
readScript: readData(iterableStream, ...shellCodeExclusions),
};
}

export const readData =
(
iterableStream: AsyncGenerator<string, void>,
startSignal: (content: string) => boolean,
excluded?: RegExp
...excluded: (RegExp | string | undefined)[]
) =>
(writer: (data: string) => void): Promise<string> =>
new Promise(async (resolve) => {
let data = '';
let content = '';
let dataStart = false;
let waitUntilNewline = false;
// This buffer will temporarily hold incoming data only for detecting the start
let buffer = '';

const [excludedPrefix] = excluded;

for await (const chunk of iterableStream) {
const payloads = chunk.toString().split('\n\n');
Expand All @@ -209,25 +207,24 @@ export const readData =

if (payload.startsWith('data:')) {
content = parseContent(payload);
if (!dataStart && content.match(excluded ?? '')) {
dataStart = startSignal(content);
if (!content.includes('\n')) {
waitUntilNewline = true;
// Use buffer only for start detection
if (!dataStart) {
// Append content to the buffer
buffer += content;
if (buffer.match(excludedPrefix ?? '')) {
dataStart = true;
// Clear the buffer once it has served its purpose
buffer = '';
if (excludedPrefix) break;
}
if (excluded) break;
}

if (content && waitUntilNewline) {
if (!content.includes('\n')) {
continue;
}
waitUntilNewline = false;
}

if (dataStart && content) {
const contentWithoutExcluded = excluded
? content.replaceAll(excluded, '')
: content;
const contentWithoutExcluded = stripRegexPatterns(
content,
excluded
);

data += contentWithoutExcluded;
writer(contentWithoutExcluded);
}
Expand Down
9 changes: 9 additions & 0 deletions src/helpers/strip-regex-patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const stripRegexPatterns = (
inputString: string,
patternList: (RegExp | string | undefined)[]
) =>
patternList.reduce(
(currentString: string, pattern) =>
pattern ? currentString.replaceAll(pattern, '') : currentString,
inputString
);