Storing of graph and node ids

This commit is contained in:
dimitrievgs 2026-05-16 16:28:40 +03:00
parent 9c5706bd86
commit 526db6feb0
3 changed files with 79 additions and 7 deletions

View File

@ -42,6 +42,9 @@ interface LLMAgentSettings {
promptMetrics: string; promptMetrics: string;
markerStart: string; markerStart: string;
markerStop: string; markerStop: string;
lastActiveGraphId: string | null;
lastActiveNodeId: string | null;
} }
const DEFAULT_SETTINGS: LLMAgentSettings = { const DEFAULT_SETTINGS: LLMAgentSettings = {
@ -71,7 +74,10 @@ const DEFAULT_SETTINGS: LLMAgentSettings = {
promptCommands: '', promptCommands: '',
promptMetrics: '', promptMetrics: '',
markerStart: 'старт', markerStart: 'старт',
markerStop: 'стоп' markerStop: 'стоп',
lastActiveGraphId: null,
lastActiveNodeId: null,
}; };
// ----------------------------------------------- Main Plugin Class --------------------------------------------------------------- // ----------------------------------------------- Main Plugin Class ---------------------------------------------------------------

View File

@ -160,13 +160,25 @@ export class ChatView extends ItemView {
async handleSendMessage(message: string, selectedModel?: string, attachments?: Attachment[], customMaxTokens?: number, customTemperature?: number): Promise<void> { async handleSendMessage(message: string, selectedModel?: string, attachments?: Attachment[], customMaxTokens?: number, customTemperature?: number): Promise<void> {
try { try {
// ЗАЩИТА: Если граф не выбран, создаем его на лету, чтобы не потерять текст
if (!this.selectedGraphId) { if (!this.selectedGraphId) {
new Notice('Граф не выбран или не инициализирован. Пожалуйста, начните новый чат или выберите существующий.', 3000); const createResponse = await fetch(`${this.plugin.settings.apiBaseUrl}/graphs/new`, {
console.warn('selectedGraphId is null in handleSendMessage. Current state:', { method: 'POST',
selectedGraphId: this.selectedGraphId, headers: { 'Content-Type': 'application/json' }
currentNode: this.currentNode });
if (!createResponse.ok) {
throw new Error(`Ошибка автоматического создания графа: ${createResponse.status}`);
}
const createResult = await createResponse.json();
this.selectedGraphId = createResult.graph_id;
// Уведомляем другие компоненты о том, что выбран новый граф
this.plugin.eventBus.emit('graph-selected', {
graphId: this.selectedGraphId,
currentNode: null
}); });
return;
} }
const cacheFolder = this.getCacheFolderPath(); const cacheFolder = this.getCacheFolderPath();

View File

@ -188,6 +188,9 @@ export class GraphView extends ItemView {
// Загружаем данные // Загружаем данные
await this.fetchGraphsList(); await this.fetchGraphsList();
// Автоматический выбор стартового графа
await this.initializeInitialGraph();
} }
// Вынес для чистоты // Вынес для чистоты
@ -233,6 +236,48 @@ export class GraphView extends ItemView {
this.plugin.eventBus.off('stream-ended', this.handleStreamEnded); this.plugin.eventBus.off('stream-ended', this.handleStreamEnded);
} }
// ----------------------------------------------- Save graph id ---------------------------------------------------
/**
* Определяет, какой граф открыть при запуске (из настроек или последний свежий).
*/
private async initializeInitialGraph(): Promise<void> {
const savedGraphId = (this.plugin.settings as any).lastActiveGraphId;
const savedNodeId = (this.plugin.settings as any).lastActiveNodeId;
if (this.graphsList && this.graphsList.length > 0) {
// Проверяем, существует ли еще граф из настроек
const graphExists = this.graphsList.some(g => g.id === savedGraphId);
if (savedGraphId && graphExists) {
// Вариант 1: Открываем граф из настроек
await this.fetchGraphData(savedGraphId);
if (savedNodeId) {
this.currentNode = savedNodeId;
}
// Синхронизируем интерфейс
this.plugin.eventBus.emit('graph-selected', { graphId: savedGraphId, currentNode: this.currentNode });
} else {
// Вариант 2: Графа из настроек нет, берем самый свежий из истории
const mostRecentGraph = this.graphsList[0];
await this.fetchGraphData(mostRecentGraph.id);
this.plugin.eventBus.emit('graph-selected', { graphId: mostRecentGraph.id, currentNode: this.currentNode });
}
} else {
// Вариант 3: История вообще пуста, создаем новый чистый лист
await this.handleNewChat();
}
}
/**
* Сохраняет текущий граф и узел в настройки плагина
*/
private async saveCurrentStateToSettings(graphId: string | null, nodeId: string | null) {
(this.plugin.settings as any).lastActiveGraphId = graphId;
(this.plugin.settings as any).lastActiveNodeId = nodeId;
await this.plugin.saveSettings();
}
// ----------------------------------------------- Custom Title Update --------------------------------------------------- // ----------------------------------------------- Custom Title Update ---------------------------------------------------
/** /**
@ -506,6 +551,9 @@ export class GraphView extends ItemView {
graph_id: graphId, graph_id: graphId,
custom_system_prompt: this.currentGraphCustomSystemPrompt custom_system_prompt: this.currentGraphCustomSystemPrompt
}); });
// Сохраняем стейт в настройки при успешной загрузке
await this.saveCurrentStateToSettings(graphId, this.currentNode);
} catch (error) { } catch (error) {
console.error('Ошибка загрузки данных графа:', error); console.error('Ошибка загрузки данных графа:', error);
this.selectedGraphTitle = null; this.selectedGraphTitle = null;
@ -621,6 +669,9 @@ export class GraphView extends ItemView {
if (this.graphSettingsButton) { if (this.graphSettingsButton) {
this.graphSettingsButton.setAttr('disabled', null); this.graphSettingsButton.setAttr('disabled', null);
} }
// Обновляем настройки после создания нового чата
await this.saveCurrentStateToSettings(this.selectedGraphId, null);
} catch (error: any) { } catch (error: any) {
console.error('Ошибка при создании нового чата:', error); console.error('Ошибка при создании нового чата:', error);
new Notice(`Ошибка при создании нового чата: ${error?.message || error}`, 5000); new Notice(`Ошибка при создании нового чата: ${error?.message || error}`, 5000);
@ -647,7 +698,7 @@ export class GraphView extends ItemView {
* Обработчик события выбора узла из других компонентов (например, ChatPanel). * Обработчик события выбора узла из других компонентов (например, ChatPanel).
* Синхронизирует визуальное состояние графа (подсветку текущего узла). * Синхронизирует визуальное состояние графа (подсветку текущего узла).
*/ */
private handleNodeSelectedFromBus = (data: { nodeId: string; graphId: string }) => { private handleNodeSelectedFromBus = async (data: { nodeId: string; graphId: string }) => {
// Реагируем только если это для нашего текущего графа // Реагируем только если это для нашего текущего графа
if (this.selectedGraphId === data.graphId) { if (this.selectedGraphId === data.graphId) {
this.currentNode = data.nodeId; this.currentNode = data.nodeId;
@ -659,6 +710,9 @@ export class GraphView extends ItemView {
// Перерисовываем канвас (это передаст новый activeNodeId в ReactFlow и сдвинет фокус) // Перерисовываем канвас (это передаст новый activeNodeId в ReactFlow и сдвинет фокус)
this.renderGraphPanel(); this.renderGraphPanel();
// Сохраняем в настройки переключение ветки
await this.saveCurrentStateToSettings(this.selectedGraphId, this.currentNode);
} }
}; };