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

Added stop feature #103

Merged
merged 4 commits into from
Feb 9, 2024
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
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"eslint-plugin-unused-imports": "^2.0.0",
"jiti": "^1.17.0",
"pkgroll": "^1.9.0",
"prettier": "^2.8.7",
"prettier": "^2.8.8",
"typescript": "^4.9.5"
}
}
20 changes: 16 additions & 4 deletions src/helpers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { streamToString } from './stream-to-string';
import './replace-all-polyfill';
import i18n from './i18n';
import { stripRegexPatterns } from './strip-regex-patterns';
import readline from 'readline';

const explainInSecondRequest = true;

Expand Down Expand Up @@ -187,19 +188,30 @@ export const readData =
) =>
(writer: (data: string) => void): Promise<string> =>
new Promise(async (resolve) => {
let stopTextStream = false;
let data = '';
let content = '';
let dataStart = false;
// This buffer will temporarily hold incoming data only for detecting the start
let buffer = '';
let buffer = ''; // This buffer will temporarily hold incoming data only for detecting the start

const [excludedPrefix] = excluded;
const stopTextStreamKeys = ['q', 'escape']; //Group of keys that stop the text stream

const rl = readline.createInterface({
input: process.stdin,
});

process.stdin.setRawMode(true);

process.stdin.on('keypress', (key, data) => {
if (stopTextStreamKeys.includes(data.name)) {
stopTextStream = true;
}
});
for await (const chunk of iterableStream) {
const payloads = chunk.toString().split('\n\n');

for (const payload of payloads) {
if (payload.includes('[DONE]')) {
if (payload.includes('[DONE]') || stopTextStream) {
dataStart = false;
resolve(data);
return;
Expand Down
Loading