Fix to set agent for new chat created from obsidian

- Set the agent of the current conversation in the agent dropdown when a new conversation with a non-default agent is initialized. This was unset previously.
- Pass the current selected agent in the dropdown when creating new chat
- Correctly select the `khoj-header-agent-select' element
This commit is contained in:
Debanjum
2025-08-21 07:33:25 +05:30
committed by GitHub
parent a2a3eb8be6
commit 4728098cad

View File

@@ -127,7 +127,7 @@ export class KhojChatView extends KhojPaneView {
// Register chat view keybindings
this.scope = new Scope(this.app.scope);
this.scope.register(["Ctrl", "Alt"], 'n', (_) => this.createNewConversation());
this.scope.register(["Ctrl", "Alt"], 'n', (_) => this.createNewConversation(this.currentAgent));
this.scope.register(["Ctrl", "Alt"], 'o', async (_) => await this.toggleChatSessions());
this.scope.register(["Ctrl", "Alt"], 'v', (_) => this.speechToText(new KeyboardEvent('keydown')));
this.scope.register(["Ctrl"], 'f', (_) => new KhojSearchModal(this.app, this.setting).open());
@@ -220,7 +220,7 @@ export class KhojChatView extends KhojPaneView {
await this.fetchAgents();
// Populate the agent selector in the header
const headerAgentSelect = this.contentEl.querySelector('#khoj-header-agent-select') as HTMLSelectElement;
const headerAgentSelect = this.contentEl.querySelector('.khoj-header-agent-select') as HTMLSelectElement;
if (headerAgentSelect && this.agents.length > 0) {
// Clear existing options
headerAgentSelect.innerHTML = '';
@@ -943,7 +943,7 @@ export class KhojChatView extends KhojPaneView {
return learningMoments[Math.floor(Math.random() * learningMoments.length)];
}
async createNewConversation(agentSlug?: string) {
async createNewConversation(agentSlug?: string | null) {
let chatBodyEl = this.contentEl.getElementsByClassName("khoj-chat-body")[0] as HTMLElement;
chatBodyEl.innerHTML = "";
chatBodyEl.dataset.conversationId = "";
@@ -960,11 +960,10 @@ export class KhojChatView extends KhojPaneView {
try {
// Create a new conversation with or without an agent
let endpoint = `${this.setting.khojUrl}/api/chat/sessions`;
agentSlug = agentSlug || this.currentAgent;
if (agentSlug) {
endpoint += `?agent_slug=${encodeURIComponent(agentSlug)}`;
} else if (this.currentAgent) {
endpoint += `?agent_slug=${encodeURIComponent(this.currentAgent)}`;
}
}
const response = await fetch(endpoint, {
method: "POST",
@@ -1011,7 +1010,7 @@ export class KhojChatView extends KhojPaneView {
const newConversationButtonEl = newConversationEl.createEl("button");
newConversationButtonEl.classList.add("new-conversation-button");
newConversationButtonEl.classList.add("side-panel-button");
newConversationButtonEl.addEventListener('click', (_) => this.createNewConversation());
newConversationButtonEl.addEventListener('click', (_) => this.createNewConversation(this.currentAgent));
setIcon(newConversationButtonEl, "plus");
newConversationButtonEl.innerHTML += "New";
newConversationButtonEl.title = "New Conversation (Ctrl+Alt+N)";