Skip to content

Commit d4f6b0e

Browse files
committed
Ability to capture resulting SQL
Signed-off-by: worksofliam <[email protected]>
1 parent 061ba64 commit d4f6b0e

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/aiProviders/copilot/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface IDB2ChatResult extends vscode.ChatResult {
1010
metadata: {
1111
command: string;
1212
followUps: string[];
13+
statement?: string;
1314
};
1415
}
1516

@@ -93,15 +94,15 @@ export function activateChat(context: vscode.ExtensionContext) {
9394
}
9495
});
9596

96-
await copilotRequest(
97+
const result = await copilotRequest(
9798
request.model.family,
9899
messages,
99100
{},
100101
token,
101102
stream
102103
);
103104

104-
return { metadata: { command: "build", followUps: contextItems.followUps } };
105+
return { metadata: { command: "build", followUps: contextItems.followUps, statement: result.sqlCodeBlock } };
105106
}
106107
} else {
107108
throw new Error(
@@ -132,20 +133,37 @@ export function activateChat(context: vscode.ExtensionContext) {
132133
context.subscriptions.push(chat);
133134
}
134135

136+
interface Result {
137+
output: string;
138+
sqlCodeBlock?: string;
139+
}
140+
135141
async function copilotRequest(
136142
model: string,
137143
messages: vscode.LanguageModelChatMessage[],
138144
options: vscode.LanguageModelChatRequestOptions,
139145
token: vscode.CancellationToken,
140146
stream: vscode.ChatResponseStream
141-
): Promise<void> {
147+
): Promise<Result|undefined> {
142148
const models = await vscode.lm.selectChatModels({ family: model });
143149
if (models.length > 0) {
144150
const [first] = models;
145151
const response = await first.sendRequest(messages, options, token);
152+
let result: Result = {
153+
output: "",
154+
}
146155

147156
for await (const fragment of response.text) {
148157
stream.markdown(fragment);
158+
result.output += fragment;
149159
}
160+
161+
const codeBlockStart = result.output.indexOf("```sql");
162+
const codeBlockEnd = result.output.indexOf("```", codeBlockStart + 6);
163+
if (codeBlockStart !== -1 && codeBlockEnd !== -1) {
164+
result.sqlCodeBlock = result.output.substring(codeBlockStart + 6, codeBlockEnd);
165+
}
166+
167+
return result;
150168
}
151169
}

0 commit comments

Comments
 (0)