Skip to content

Commit 788c819

Browse files
committed
Refactor prompt building to be context gathering
Signed-off-by: worksofliam <[email protected]>
1 parent 71038ed commit 788c819

File tree

2 files changed

+21
-47
lines changed

2 files changed

+21
-47
lines changed

src/aiProviders/copilot/index.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,46 +56,36 @@ export function activateChat(context: vscode.ExtensionContext) {
5656
default:
5757
stream.progress(`Building response...`);
5858

59+
let messages: vscode.LanguageModelChatMessage[] = [];
60+
5961
// get history
60-
let history: Db2ContextItems[] | undefined;
6162
if (context.history.length > 0) {
62-
history = context.history.map((h) => {
63+
messages = context.history.map((h) => {
6364
if ("prompt" in h) {
64-
return {
65-
name: `reply`,
66-
description: `reply from Copilot`,
67-
content: h.prompt,
68-
type: `assistant`,
69-
};
65+
return vscode.LanguageModelChatMessage.Assistant(h.prompt);
7066
} else {
7167
const responseContent = h.response
7268
.filter((r) => r.value instanceof vscode.MarkdownString)
7369
.map((r) => (r.value as vscode.MarkdownString).value)
7470
.join("\n\n");
75-
return {
76-
name: `message`,
77-
description: `message from user`,
78-
content: responseContent,
79-
type: `assistant`,
80-
};
71+
return vscode.LanguageModelChatMessage.Assistant(responseContent);
8172
}
8273
});
8374
}
8475

8576
const contextItems = await getContextItems(request.prompt, {
86-
history,
87-
progress: stream.progress
77+
progress: stream.progress,
78+
withDb2Prompt: true
8879
});
8980

90-
let messages = contextItems.context.map(c => {
91-
if (c.type === `user`) {
92-
return vscode.LanguageModelChatMessage.User(c.content);
93-
} else {
94-
return vscode.LanguageModelChatMessage.Assistant(c.content);
95-
}
96-
});
81+
messages.push(...contextItems.context.map(c => {
82+
return vscode.LanguageModelChatMessage.Assistant(c.content);
83+
}));
84+
85+
messages.push(
86+
vscode.LanguageModelChatMessage.User(request.prompt)
87+
);
9788

98-
const tools = vscode.lm.tools.filter(t => request.toolReferences.some(r => r.name === t.name));
9989

10090
const doRequest = (tools: vscode.LanguageModelToolInformation[] = []) => {
10191
return copilotRequest(
@@ -110,12 +100,15 @@ export function activateChat(context: vscode.ExtensionContext) {
110100
);
111101
}
112102

103+
// The first request we do can do two things: return either a stream OR return a tool request
104+
const tools = vscode.lm.tools.filter(t => request.toolReferences.some(r => r.name === t.name));
113105
let result = await doRequest(tools);
114106

107+
// Then, if there is a tool request, we do the logic to invoke the tool
115108
if (result.toolCalls.length > 0) {
116109
for (const toolcall of result.toolCalls) {
117110
if (toolcall.name === RUN_SQL_TOOL_ID) {
118-
const result = await vscode.lm.invokeTool(toolcall.name, {toolInvocationToken: request.toolInvocationToken, input: toolcall.input});
111+
const result = await vscode.lm.invokeTool(toolcall.name, { toolInvocationToken: request.toolInvocationToken, input: toolcall.input });
119112
const resultOut = result.content.map(c => {
120113
if (c instanceof vscode.LanguageModelTextPart) {
121114
return c.value;

src/aiProviders/prompt.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ import { buildSchemaDefinition, canTalkToDb, getContentItemsForRefs, getSqlConte
77
import { DB2_SYSTEM_PROMPT } from "./prompts";
88

99
export interface PromptOptions {
10-
userInputNotContext?: boolean
11-
history?: Db2ContextItems[];
1210
progress?: (text: string) => void;
11+
withDb2Prompt?: boolean;
1312
}
1413

1514
export interface Db2ContextItems {
1615
name: string;
1716
description: string;
1817
content: string;
19-
type: "user"|"assistant"|"system";
2018
specific?: "copilot"|"continue";
2119
}
2220

@@ -49,8 +47,7 @@ export async function getContextItems(input: string, options: PromptOptions = {}
4947
contextItems.push({
5048
name: `SCHEMA Definition`,
5149
description: `${currentSchema} definition`,
52-
content: JSON.stringify(schemaSemantic),
53-
type: "user"
50+
content: JSON.stringify(schemaSemantic)
5451
});
5552
}
5653
}
@@ -60,16 +57,11 @@ export async function getContextItems(input: string, options: PromptOptions = {}
6057
progress(`Finding objects to work with...`);
6158
const context = await getSqlContextItems(input);
6259

63-
if (options.history) {
64-
contextItems.push(...options.history);
65-
}
66-
6760
for (const sqlObj of context.items) {
6861
contextItems.push({
6962
name: `${sqlObj.type.toLowerCase()} definition for ${sqlObj.id}`,
7063
content: sqlObj.content,
7164
description: `${sqlObj.type} definition`,
72-
type: `assistant`
7365
});
7466
}
7567

@@ -104,7 +96,6 @@ export async function getContextItems(input: string, options: PromptOptions = {}
10496
name: `${sqlObj.type.toLowerCase()} definition for ${sqlObj.id}`,
10597
content: sqlObj.content,
10698
description: `${sqlObj.type} definition`,
107-
type: `assistant`
10899
});
109100
}
110101

@@ -115,21 +106,11 @@ export async function getContextItems(input: string, options: PromptOptions = {}
115106
followUps.push(`What are some objects related to ${prettyNameRef}?`);
116107
}
117108

118-
if (!options.history) {
109+
if (options.withDb2Prompt) {
119110
contextItems.push({
120111
name: `system prompt`,
121112
content: DB2_SYSTEM_PROMPT,
122113
description: `system prompt`,
123-
type: `system`
124-
});
125-
}
126-
127-
if (!options.userInputNotContext) {
128-
contextItems.push({
129-
name: `user prompt`,
130-
content: input,
131-
description: `user prompt`,
132-
type: `user`
133114
});
134115
}
135116
}

0 commit comments

Comments
 (0)