display tokens as they arrive

master
Alessandro Mauri 1 year ago
parent b75dce72cd
commit 12f3c82d3e
  1. 18
      src/common.ts
  2. 3
      src/extension.ts
  3. 4
      src/openai-api.ts

@ -1,3 +1,4 @@
import { mkdirSync } from 'fs';
import * as vscode from 'vscode';
// common data structures and functions
@ -56,3 +57,20 @@ export async function showPendingStatusBar(
);
// we already resolve the operation elsewhere
}
let st_msg: vscode.StatusBarItem | undefined;
export function updateStatusBarMessage(text: string) {
if (!st_msg) {
st_msg = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, -100);
}
const run_color = new vscode.ThemeColor('statusBarItem.warningBackground');
if (text.length > 0) {
st_msg.backgroundColor = run_color;
st_msg.text = '$(megaphone) ' + text.trim();
st_msg.show();
} else {
st_msg.hide();
}
}

@ -18,6 +18,7 @@ import {
ResponseData,
showMessageWithTimeout,
showPendingStatusBar,
updateStatusBarMessage,
} from './common';
// clean up the document
@ -53,6 +54,8 @@ export function activate(context: vscode.ExtensionContext) {
config.update('completionEnabled', false);
});
updateStatusBarMessage('');
// Register a new provider of inline completions, this does not decide how it is invoked
// only what the completion should be
// https://github.com/microsoft/vscode-extension-samples/blob/main/inline-completions/src/extension.ts

@ -4,6 +4,7 @@ import {
ResponseData,
showMessageWithTimeout,
showPendingStatusBar,
updateStatusBarMessage,
} from './common';
import { config } from 'process';
@ -193,6 +194,7 @@ export async function openAIMakeRequest(
// FIXME: why the choices may be multiple?
// TODO: display the multiple choices
//console.log(data.choices[0].text);
updateStatusBarMessage(data.choices[0].text);
ret.content += data.choices[0].text;
ret.tokens += data.usage?.completion_tokens || 0;
}
@ -200,6 +202,8 @@ export async function openAIMakeRequest(
// stop the timer
const timer_end = performance.now();
ret.time = (timer_end - timer_start) / 1000.0;
// clear the status bar item
updateStatusBarMessage('');
} catch (e: any) {
console.error(e);
const err = e as TypeError;

Loading…
Cancel
Save