Skip to content

Commit

Permalink
fix chat history, add set-schema command
Browse files Browse the repository at this point in the history
  • Loading branch information
ajshedivy committed Nov 4, 2024
1 parent fef79ff commit f51cd44
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
{
"name": "activity",
"description": "Summarize the activity on the system"
},
{
"name": "set-schema",
"description": "Set the current schema"
}
]
}
Expand Down
44 changes: 30 additions & 14 deletions src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
refsToMarkdown,
} from "./context";
import { chatRequest } from "./send";
import { JobManager } from "../config";

const CHAT_ID = `vscode-db2i.chat`;

Expand All @@ -36,7 +37,7 @@ export function activateChat(context: vscode.ExtensionContext) {
let messages: vscode.LanguageModelChatMessage[];

if (canTalkToDb()) {
const usingSchema = getDefaultSchema();
let usingSchema = getDefaultSchema();

switch (request.command) {
case `activity`:
Expand All @@ -58,7 +59,20 @@ export function activateChat(context: vscode.ExtensionContext) {
await streamModelResponse(messages, stream, token);

return { metadata: { command: "activity" } };

case `set-schema`:
stream.progress(`Setting Current Schema for SQL Job`);
const newSchema = request.prompt.split(' ')[0];
if (newSchema) {
const curJob = JobManager.getSelection()
if (curJob) {
const result = await curJob.job.setCurrentSchema(newSchema);
if (result) {
stream.progress(`Set Current Schema: ${newSchema}✅`);
usingSchema = newSchema;
}
}
return;
}
default:
stream.progress(
`Getting information from ${Statement.prettyName(usingSchema)}...`
Expand All @@ -82,18 +96,20 @@ export function activateChat(context: vscode.ExtensionContext) {
];

if (context.history.length > 0) {
messages.push(...context.history.map(h => {
if ('prompt' in h) {
return vscode.LanguageModelChatMessage.Assistant(h.prompt);
} else {
return vscode.LanguageModelChatMessage.Assistant(
h.response.filter(r => 'value' in r.value).map(r => r.value.value).join(`\n\n`)
);
}
}));

messages = messages.filter(m => m.content.trim().length > 0);
}
const historyMessages = context.history.map(h => {
if ('prompt' in h) {
return vscode.LanguageModelChatMessage.Assistant(h.prompt);
} else {
const responseContent = h.response
.filter(r => r.value instanceof vscode.MarkdownString)
.map(r => (r.value as vscode.MarkdownString).value)
.join('\n\n');
return vscode.LanguageModelChatMessage.Assistant(responseContent);
}
});

messages.push(...historyMessages);
}

if (Object.keys(refs).length > 0) {
messages.push(
Expand Down
19 changes: 19 additions & 0 deletions src/connection/sqlJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ export class OldSQLJob extends SQLJob {
return connectResult;
}

async setCurrentSchema(schema: string): Promise<QueryResult<any>> {
if (schema) {
const upperSchema = schema.toUpperCase();
const result = await this.execute(`set current schema = ?`, {parameters: [upperSchema]});
if (result.success) {
const config = getInstance().getConfig();
const currentLibrary = config.currentLibrary.toUpperCase();

if (upperSchema !== currentLibrary) {
config.currentLibrary = upperSchema;
await getInstance().setConfig(config);
}
}

return result;

}
}

async setSelfState(code: SelfValue) {
try {
const query: string = `SET SYSIBMADM.SELFCODES = '${code}'`
Expand Down

0 comments on commit f51cd44

Please sign in to comment.