bug fixes for correct loading of chat and graph panels
This commit is contained in:
parent
2bf5763eb1
commit
8bf1394e6f
|
|
@ -9,7 +9,6 @@ export class HistoryPanel {
|
|||
container: HTMLElement;
|
||||
props: {
|
||||
graphs: any[];
|
||||
onGraphSelect: (data: { graphId: string; currentNode: string | null }) => void;
|
||||
eventBus: EventBus;
|
||||
};
|
||||
graphs: any[];
|
||||
|
|
@ -23,7 +22,6 @@ export class HistoryPanel {
|
|||
container: HTMLElement,
|
||||
props: {
|
||||
graphs: any[];
|
||||
onGraphSelect: (data: { graphId: string; currentNode: string | null }) => void;
|
||||
eventBus: EventBus;
|
||||
}
|
||||
) {
|
||||
|
|
@ -103,8 +101,8 @@ export class HistoryPanel {
|
|||
item.addEventListener('click', (e) => {
|
||||
if (!(e.target as HTMLElement).classList.contains('chat-item-options-button')) {
|
||||
const graphId = (item as HTMLElement).dataset.graphId;
|
||||
if (graphId && this.props.onGraphSelect) {
|
||||
this.props.onGraphSelect({ graphId: graphId, currentNode: null });
|
||||
if (graphId) {
|
||||
this.props.eventBus.emit('graph-selected', { graphId: graphId, currentNode: null });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -79,21 +79,27 @@ export class ChatView extends ItemView {
|
|||
* @param {string} nodeId - ID конечного узла для загрузки истории.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async fetchMessagesFromRootToNode(nodeId: string): Promise<void> {
|
||||
async fetchMessagesFromRootToNode(nodeId: string | null): Promise<void> {
|
||||
try {
|
||||
// Убедимся, что selectedGraphId установлен
|
||||
if (!this.selectedGraphId) {
|
||||
console.warn('selectedGraphId не установлен. Невозможно загрузить сообщения.');
|
||||
return;
|
||||
}
|
||||
const response = await fetch(`${this.plugin.settings.apiBaseUrl}/messages/${this.selectedGraphId}/${nodeId}`);
|
||||
|
||||
const url = nodeId
|
||||
? `${this.plugin.settings.apiBaseUrl}/messages/${this.selectedGraphId}/${nodeId}`
|
||||
: `${this.plugin.settings.apiBaseUrl}/messages/${this.selectedGraphId}/last`;
|
||||
const response = await fetch(url);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Ошибка загрузки сообщений: ${response.status} - ${errorText}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
// ИСПРАВЛЕНО: Убедиться, что type передается, если это возможно
|
||||
this.chatHistory = data.map((msg: any) => ({
|
||||
this.chatHistory =
|
||||
data.map((msg: any) => ({
|
||||
...msg,
|
||||
type: msg.type || msg.role // Используем type, если есть, иначе role как fallback
|
||||
})) || [];
|
||||
|
|
@ -114,13 +120,13 @@ export class ChatView extends ItemView {
|
|||
const response = await fetch(`${this.plugin.settings.apiBaseUrl}/chat`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
message: message,
|
||||
graph_id: this.selectedGraphId,
|
||||
parent_node_id: this.currentNode,
|
||||
}),
|
||||
parent_node_id: this.currentNode
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
|
@ -151,7 +157,6 @@ export class ChatView extends ItemView {
|
|||
this.currentNode = null; // Сбрасываем, если no current_node_id
|
||||
this.fetchMessagesFromRootToNode(''); // Передаем пустую строку или null, чтобы очистить историю
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Ошибка отправки сообщения:', error);
|
||||
}
|
||||
|
|
@ -190,15 +195,7 @@ export class ChatView extends ItemView {
|
|||
*/
|
||||
async handleGraphSelected(data: { graphId: string; currentNode: string | null }): Promise<void> {
|
||||
this.selectedGraphId = data.graphId;
|
||||
this.currentNode = data.currentNode;
|
||||
if (this.currentNode) {
|
||||
await this.fetchMessagesFromRootToNode(this.currentNode);
|
||||
} else {
|
||||
// Если текущего узла нет (например, пустой граф), очищаем историю
|
||||
this.chatHistory = [];
|
||||
if (this.chatPanel) {
|
||||
this.chatPanel.updateHistory(this.chatHistory);
|
||||
}
|
||||
}
|
||||
this.currentNode = data.currentNode; // Может быть пустым, но это не проблема
|
||||
await this.fetchMessagesFromRootToNode(data.currentNode);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,7 +99,6 @@ export class GraphView extends ItemView {
|
|||
// Инициализируем панель истории
|
||||
this.historyPanel = new HistoryPanel(this.historyContainer, {
|
||||
graphs: this.graphsList,
|
||||
onGraphSelect: this.handleGraphSelected.bind(this),
|
||||
eventBus: this.plugin.eventBus
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user