Skip to content

Commit

Permalink
fixed editing script
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnOutDev committed Sep 21, 2023
1 parent 6b3be66 commit 09c9daa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
30 changes: 11 additions & 19 deletions src/helpers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +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 = /```[a-zA-Z]*\n/gi;
const shellCodeEndRegex = /```[a-zA-Z]*/gi;
const shellCodeExclusions = [/```[a-zA-Z]*\n/gi, /```[a-zA-Z]*/gi, '\n'];

export async function getScriptAndInfo({
prompt,
Expand All @@ -45,12 +44,8 @@ export async function getScriptAndInfo({
});
const iterableStream = streamToIterable(stream);
return {
readScript: readData(
iterableStream,
shellCodeStartRegex,
shellCodeEndRegex
),
readInfo: readData(iterableStream, shellCodeStartRegex, shellCodeEndRegex),
readScript: readData(iterableStream, ...shellCodeExclusions),
readInfo: readData(iterableStream, ...shellCodeExclusions),
};
}

Expand Down Expand Up @@ -181,19 +176,14 @@ export async function getRevision({
});
const iterableStream = streamToIterable(stream);
return {
readScript: readData(
iterableStream,
shellCodeStartRegex,
shellCodeEndRegex
),
readScript: readData(iterableStream, ...shellCodeExclusions),
};
}

export const readData =
(
iterableStream: AsyncGenerator<string, void>,
excludedPrefix?: RegExp,
excludedSuffix?: RegExp
...excluded: (RegExp | string | undefined)[]
) =>
(writer: (data: string) => void): Promise<string> =>
new Promise(async (resolve) => {
Expand All @@ -203,6 +193,8 @@ export const readData =
// 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 @@ -228,10 +220,10 @@ export const readData =
}

if (dataStart && content) {
const contentWithoutExcluded = stripRegexPatterns(content, [
excludedPrefix,
excludedSuffix,
]);
const contentWithoutExcluded = stripRegexPatterns(
content,
excluded
);

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

0 comments on commit 09c9daa

Please sign in to comment.