Skip to content

Commit

Permalink
fix(chat): string (#11505)
Browse files Browse the repository at this point in the history
* fix(chat): string

* fix: restrict template match number

* fix: ut

* fix: ut coverage

* fix: comment

* fix: string and API change

* fix: ut
  • Loading branch information
tecton authored and LongOddCode committed May 17, 2024
1 parent 94013e9 commit adb9d39
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/vscode-extension/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@
"teamstoolkit.chatParticipants.nextStep.whatsNext": "What should I do next?",
"teamstoolkit.chatParticipants.create.sample": "Scaffold this sample",
"teamstoolkit.chatParticipants.create.template": "Create this template",
"teamstoolkit.chatParticipants.create.tooGeneric": "Your app description is too generic. To find relevant templates or samples, give specific details of your app's capabilities or technologies.\n\nE.g. Instead of saying 'create a chat bot', you could specify 'create a chat bot that answers FAQs for customer support.'",
"teamstoolkit.chatParticipants.create.tooGeneric": "Your app description is too generic. To find relevant templates or samples, give specific details of your app's capabilities or technologies.\n\nE.g. Instead of saying 'create a bot', you could specify 'create a bot template' or 'create a notification bot that sends user the stock updates'.",
"teamstoolkit.chatParticipants.create.oneMatched": "We've found 1 project that matches your description. Take a look at it below.\n\n",
"teamstoolkit.chatParticipants.create.multipleMatched": "We've found %d projects that match your description. Take a look at them below.\n",
"teamstoolkit.chatParticipants.create.noMatched": "I cannot find any matching templates or samples. Refine your app description or explore other templates.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ declare module 'vscode' {
* console.error(e);
* }
* ```
*
* To cancel the stream, the consumer can {@link CancellationTokenSource.cancel cancel} the token that was used to make the request
* or break from the for-loop.
*/
stream: AsyncIterable<string>;
text: AsyncIterable<string>;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/vscode-extension/src/chat/commands/create/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ export async function matchProject(
if (!request.prompt.includes("template")) {
// also search in samples
matchedProjects.push(...(await matchSamples(request, token, telemetryMetadata)));
matchedProjects.sort((a, b) => b.score - a.score);
} else {
matchedProjects.sort((a, b) => b.score - a.score);
matchedProjects.splice(2);
}
matchedProjects.sort((a, b) => b.score - a.score);
const result: ProjectMetadata[] = [];
const matchedProjectIds = new Set<string>();
for (const { id, score } of matchedProjects) {
Expand Down
8 changes: 4 additions & 4 deletions packages/vscode-extension/src/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export async function verbatimCopilotInteraction(
if (!familyMatch) {
throw new Error("No chat models available for the specified family");
}
const chatRequest = await familyMatch.sendRequest(messages, {}, token);
for await (const fragment of chatRequest.stream) {
const chatResponse = await familyMatch.sendRequest(messages, {}, token);
for await (const fragment of chatResponse.text) {
response.markdown(fragment);
}
}
Expand All @@ -36,9 +36,9 @@ export async function getCopilotResponseAsString(
if (!familyMatch) {
throw new Error("No chat models available for the specified family");
}
const chatRequest = await familyMatch.sendRequest(messages, {}, token);
const chatResponse = await familyMatch.sendRequest(messages, {}, token);
let response = "";
for await (const fragment of chatRequest.stream) {
for await (const fragment of chatResponse.text) {
response += fragment;
}
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ describe("chat create command", () => {
chai.assert.isTrue(showFileTreeStub.notCalled);
chai.assert.isTrue(
response.markdown.calledOnceWith(
"Your app description is too generic. To find relevant templates or samples, give specific details of your app's capabilities or technologies.\n\nE.g. Instead of saying 'create a chat bot', you could specify 'create a chat bot that answers FAQs for customer support.'"
"Your app description is too generic. To find relevant templates or samples, give specific details of your app's capabilities or technologies.\n\nE.g. Instead of saying 'create a bot', you could specify 'create a bot template' or 'create a notification bot that sends user the stock updates'."
)
);
});
Expand Down
41 changes: 41 additions & 0 deletions packages/vscode-extension/test/chat/commands/create/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,47 @@ describe("chat create helper", () => {
chai.assert.strictEqual(result.length, 1);
chai.assert.strictEqual(result[0].id, "test1");
});

it("has matched template project", async () => {
const chatTelemetryDataMock = sandbox.createStubInstance(telemetry.ChatTelemetryData);
sandbox.stub(chatTelemetryDataMock, "properties").get(function getterFn() {
return undefined;
});
sandbox.stub(chatTelemetryDataMock, "measurements").get(function getterFn() {
return undefined;
});
sandbox.stub(sampleProvider, "SampleCollection").get(function getterFn() {
return {
samples: [
{
id: "test1",
title: "test1",
fullDescription: "test1",
},
],
};
});
chatTelemetryDataMock.chatMessages = [];
sandbox
.stub(telemetry.ChatTelemetryData, "createByParticipant")
.returns(chatTelemetryDataMock);
const sendTelemetryEventStub = sandbox.stub(ExtTelemetry, "sendTelemetryEvent");
sandbox
.stub(util, "getCopilotResponseAsString")
.onFirstCall()
.resolves('{"app":[{"id": "bot", "score": 1.0}]}')
.onSecondCall()
.resolves('{"app":[{"id": "test2", "score": 0.5}]}');

const token = new CancellationToken();
const result = await helper.matchProject(
{ prompt: "test template" } as vscode.ChatRequest,
token,
chatTelemetryDataMock
);
chai.assert.strictEqual(result.length, 1);
chai.assert.strictEqual(result[0].id, "bot");
});
});

describe("showFileTree()", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode-extension/test/chat/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("chat utils", () => {
const token = new CancellationToken();
const chatModel: vscode.LanguageModelChat = {
sendRequest: sandbox.stub().resolves({
stream: asyncIterator,
text: asyncIterator,
}),
id: "",
vendor: "",
Expand Down Expand Up @@ -77,7 +77,7 @@ describe("chat utils", () => {
const token = new CancellationToken();
const chatModel: vscode.LanguageModelChat = {
sendRequest: sandbox.stub().resolves({
stream: asyncIterator,
text: asyncIterator,
}),
id: "",
vendor: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function getCopilotResponseAsString(
}
const chatRequest = await familyMatch.sendRequest(messages, {}, token);
let response = "";
for await (const fragment of chatRequest.stream) {
for await (const fragment of chatRequest.text) {
response += fragment;
}
return response;
Expand Down

0 comments on commit adb9d39

Please sign in to comment.