Skip to content

Commit 2a1e1c3

Browse files
committed
Add a total token test
Fix echo model echoing the first message on subsequent responses
1 parent d9bf81f commit 2a1e1c3

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

extensions/positron-assistant/src/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class EchoLanguageModel implements positron.ai.LanguageModelChatProvider {
117117
token: vscode.CancellationToken
118118
): Promise<any> {
119119
const _messages = toAIMessage(messages);
120-
const message = _messages[0];
120+
const message = _messages[_messages.length - 2]; // Get the last user message, the last message is the context
121121

122122
if (typeof message.content === 'string') {
123123
message.content = [{ type: 'text', text: message.content }];

test/e2e/pages/positronAssistant.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ export class Assistant {
186186
await expect(this.code.driver.page.locator('.token-usage')).not.toBeVisible();
187187
}
188188

189+
async verifyTotalTokenUsageVisible() {
190+
await expect(this.code.driver.page.locator('.token-usage-total')).toBeVisible();
191+
await expect(this.code.driver.page.locator('.token-usage-total')).toHaveText(/Total tokens: \d+ \d+/);
192+
}
193+
194+
async verifyNumberOfVisibleResponses(expectedCount: number, checkTokenUsage: boolean = false) {
195+
const responses = this.code.driver.page.locator('.interactive-response');
196+
await expect(responses).toHaveCount(expectedCount);
197+
if (checkTokenUsage) {
198+
this.code.driver.page.locator('.token-usage').nth(expectedCount - 1).waitFor({ state: 'visible' });
199+
}
200+
}
201+
189202
async getTokenUsage() {
190203
const tokenUsageElement = this.code.driver.page.locator('.token-usage');
191204
await expect(tokenUsageElement).toBeVisible();
@@ -198,4 +211,22 @@ export class Assistant {
198211
outputTokens: outputMatch ? parseInt(outputMatch[1], 10) : 0
199212
};
200213
}
214+
215+
async getTotalTokenUsage() {
216+
const totalTokenUsageElement = this.code.driver.page.locator('.token-usage-total');
217+
await expect(totalTokenUsageElement).toBeVisible();
218+
const text = await totalTokenUsageElement.textContent();
219+
console.log('Total Token Usage Text:', text);
220+
expect(text).not.toBeNull();
221+
const totalMatch = text ? text.match(/Total tokens: (\d+) (\d+)/) : null;
222+
return {
223+
inputTokens: totalMatch ? parseInt(totalMatch[1], 10) : 0,
224+
outputTokens: totalMatch ? parseInt(totalMatch[2], 10) : 0
225+
};
226+
}
227+
228+
async waitForReadyToSend(timeout: number = 5000) {
229+
await this.code.driver.page.waitForSelector('.chat-input-toolbars .codicon-send', { timeout });
230+
await this.code.driver.page.waitForSelector('.detail-container .detail:has-text("Working")', { state: 'hidden', timeout });
231+
}
201232
}

test/e2e/tests/positron-assistant/positron-assistant.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,23 @@ test.describe('Positron Assistant Chat Tokens', { tag: [tags.WIN, tags.ASSISTANT
215215
await settings.set({ 'positron.assistant.showTokenUsage.enable': true });
216216
await app.workbench.assistant.verifyTokenUsageVisible();
217217
});
218+
219+
test('Total token usage is displayed in chat header', async function ({ app }) {
220+
const message1 = 'What is the meaning of life?';
221+
const message2 = 'Forty-two';
222+
223+
await app.workbench.assistant.enterChatMessage(message1);
224+
await app.workbench.assistant.waitForReadyToSend();
225+
await app.workbench.assistant.enterChatMessage(message2);
226+
227+
await app.workbench.assistant.waitForReadyToSend();
228+
await app.workbench.assistant.verifyNumberOfVisibleResponses(2, true);
229+
230+
const totalTokens = await app.workbench.assistant.getTotalTokenUsage();
231+
expect(totalTokens).toBeDefined();
232+
expect(totalTokens).toMatchObject({
233+
inputTokens: message1.length + message2.length,
234+
outputTokens: message1.length + message2.length
235+
});
236+
});
218237
});

0 commit comments

Comments
 (0)