Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 1x 1x 40x 40x 2x 2x 40x 40x 40x 41x 41x 41x 37x 37x 4x 4x 3x 1x 1x 41x 2x 2x 1x 1x 1x 1x 3x 3x 3x 3x 58x 58x 3x 59x 58x 58x 58x 59x 8x 3x 3x 5x 4x 8x 8x 53x 53x 53x 53x 7x 7x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 7x 7x 46x 45x 45x 45x 45x 1x 1x 46x 45x 45x 45x 45x 6x 5x 45x 46x 46x 46x 46x 45x 45x 45x 45x 6x 5x 5x 5x 5x 4x 5x 10x 10x 10x 3x 2x 1x 1x 3x 7x 7x 7x 7x 7x 7x 7x 2x 1x 1x 1x 5x 5x 5x 2x 2x 3x 3x 3x 2x 1x 1x 3x 4x 4x 4x 3x 3x 3x 2x 2x 2x 1x 1x 1x 1x | import { App, Modal, Notice, setIcon } from "obsidian";
import type LilbeePlugin from "../main";
import { isHttpStatus } from "../api";
import { HTTP_STATUS, type SessionExport, type SessionMeta } from "../types";
import { ConfirmModal } from "./confirm-modal";
import { MESSAGES } from "../locales/en";
import { bindEscapeToClose, errorMessage, relativeTimeFromIso } from "../utils";
import { displayLabelForRef } from "../utils/model-ref";
import { applyConfig } from "../utils/reindex";
import { EXPORT_ICON, FORK_ICON, SAVE_ICON, exportChatFile, saveChatNote } from "../utils/session";
/** Hooks the chat view supplies so the modal can drive it without importing it. */
export interface SessionsModalHooks {
/** Id of the conversation the chat view currently has open, if any. */
activeId: string | null;
resume: (id: string) => void;
startNew: () => void;
/** Fork the whole conversation and open the fork. */
fork: (id: string) => void;
/** Save the open chat to the vault the way the chat toolbar does. */
saveActive: () => void;
/** Export the open chat to a file the way the chat toolbar does. */
exportActive: () => void;
/** Settles once the chat view's queued session writes have finished. */
writesSettled: () => Promise<void>;
}
export class SessionsModal extends Modal {
private plugin: LilbeePlugin;
private hooks: SessionsModalHooks;
private sessions: SessionMeta[] = [];
private filter = "";
private renamingId: string | null = null;
private loadFailed = false;
/** True when the server said sessions are switched off; renders the turn-on offer. */
private disabled = false;
private listEl: HTMLElement | null = null;
private countEl: HTMLElement | null = null;
constructor(app: App, plugin: LilbeePlugin, hooks: SessionsModalHooks) {
super(app);
this.plugin = plugin;
this.hooks = hooks;
bindEscapeToClose(this);
}
onOpen(): void {
const { contentEl } = this;
contentEl.empty();
contentEl.addClass("lilbee-sessions-modal");
const header = contentEl.createDiv({ cls: "lilbee-sessions-header" });
header.createEl("h2", { text: MESSAGES.TITLE_SESSIONS });
this.countEl = header.createSpan({ cls: "lilbee-sessions-count" });
const newBtn = header.createEl("button", { cls: "lilbee-sessions-new" });
setIcon(newBtn, "plus");
newBtn.setAttribute("aria-label", MESSAGES.LABEL_NEW_SESSION);
newBtn.addEventListener("click", () => {
this.hooks.startNew();
this.close();
});
const filterEl = contentEl.createEl("input", {
cls: "lilbee-sessions-filter",
placeholder: MESSAGES.PLACEHOLDER_FILTER_SESSIONS,
attr: { type: "text" },
});
filterEl.addEventListener("input", () => {
this.filter = filterEl.value;
this.renderList();
});
window.setTimeout(() => filterEl.focus(), 0);
this.listEl = contentEl.createDiv({ cls: "lilbee-sessions-list" });
void this.load();
}
private async load(): Promise<void> {
try {
await this.hooks.writesSettled();
this.sessions = await this.plugin.api.listSessions();
this.loadFailed = false;
this.disabled = false;
} catch (err) {
this.loadFailed = true;
// The server 404s every session route when sessions_enabled is off.
if (err instanceof Error && isHttpStatus(err, HTTP_STATUS.NOT_FOUND)) {
this.disabled = true;
} else {
const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
new Notice(MESSAGES.ERROR_SESSIONS_LOAD_FAILED(reason));
}
}
this.renderList();
}
/** Flip the server's writable `sessions_enabled` flag, then reload the list. */
private async enableSessions(): Promise<void> {
try {
await applyConfig(this.plugin, { sessions_enabled: true });
} catch (err) {
const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
new Notice(MESSAGES.ERROR_SESSIONS_ENABLE_FAILED(reason));
return;
}
await this.load();
}
private renderDisabledState(container: HTMLElement): void {
const box = container.createDiv({ cls: "lilbee-sessions-disabled" });
box.createDiv({ cls: "lilbee-sessions-disabled-text", text: MESSAGES.SESSIONS_DISABLED });
const enableBtn = box.createEl("button", {
cls: "lilbee-sessions-enable",
text: MESSAGES.LABEL_ENABLE_SESSIONS,
});
enableBtn.addEventListener("click", () => void this.enableSessions());
}
/** Case-insensitive substring on the title, matching the TUI's filter. */
private visible(): SessionMeta[] {
const needle = this.filter.trim().toLowerCase();
if (!needle) return this.sessions;
return this.sessions.filter((s) => s.title.toLowerCase().includes(needle));
}
private renderList(): void {
if (!this.listEl) return;
this.listEl.empty();
const rows = this.visible();
this.countEl?.setText(MESSAGES.SESSIONS_COUNT(rows.length));
if (rows.length === 0) {
if (this.disabled) {
this.renderDisabledState(this.listEl);
return;
}
if (this.loadFailed) return;
const empty = this.sessions.length === 0 ? MESSAGES.SESSIONS_EMPTY : MESSAGES.SESSIONS_NO_MATCH;
this.listEl.createDiv({ cls: "lilbee-sessions-empty", text: empty });
return;
}
for (const meta of rows) this.renderRow(this.listEl, meta);
}
private renderRow(container: HTMLElement, meta: SessionMeta): void {
const isActive = meta.id === this.hooks.activeId;
const row = container.createDiv({
cls: `lilbee-session-row${isActive ? " is-active" : ""}`,
});
if (this.renamingId === meta.id) {
this.renderRenameField(row, meta);
return;
}
const main = row.createDiv({ cls: "lilbee-session-main" });
const titleRow = main.createDiv({ cls: "lilbee-session-title-row" });
titleRow.createSpan({ cls: "lilbee-session-title", text: meta.title });
const dateEl = titleRow.createSpan({
cls: "lilbee-session-date",
text: relativeTimeFromIso(meta.updated_at),
});
if (meta.updated_at) dateEl.setAttribute("title", meta.updated_at);
main.createDiv({
cls: "lilbee-session-meta",
text: MESSAGES.SESSIONS_ROW_META(meta.message_count, displayLabelForRef(meta.model_ref)),
});
main.addEventListener("click", () => this.resume(meta));
this.renderActions(row, meta);
}
private renderActions(row: HTMLElement, meta: SessionMeta): void {
const actions = row.createDiv({ cls: "lilbee-session-actions" });
const renameBtn = actions.createEl("button", { cls: "lilbee-session-rename" });
setIcon(renameBtn, "pencil");
renameBtn.setAttribute("aria-label", MESSAGES.LABEL_RENAME_SESSION);
renameBtn.addEventListener("click", () => {
this.renamingId = meta.id;
this.renderList();
});
if (this.plugin.serverSupportsSessionFork()) {
const forkBtn = actions.createEl("button", { cls: "lilbee-session-fork" });
setIcon(forkBtn, FORK_ICON);
forkBtn.setAttribute("aria-label", MESSAGES.LABEL_FORK_SESSION);
forkBtn.addEventListener("click", () => {
this.hooks.fork(meta.id);
this.close();
});
}
if (this.plugin.serverSupportsSessionExport()) {
const saveBtn = actions.createEl("button", { cls: "lilbee-session-save" });
setIcon(saveBtn, SAVE_ICON);
saveBtn.setAttribute("aria-label", MESSAGES.LABEL_SAVE_VAULT);
saveBtn.addEventListener("click", () => {
if (meta.id === this.hooks.activeId) this.hooks.saveActive();
else void this.saveToVault(meta);
});
this.renderExportAction(actions, meta);
}
const deleteBtn = actions.createEl("button", { cls: "lilbee-session-delete" });
setIcon(deleteBtn, "trash-2");
deleteBtn.setAttribute("aria-label", MESSAGES.LABEL_DELETE_SESSION);
deleteBtn.addEventListener("click", () => void this.confirmDelete(meta));
}
private renderExportAction(actions: HTMLElement, meta: SessionMeta): void {
const exportBtn = actions.createEl("button", { cls: "lilbee-session-export" });
setIcon(exportBtn, EXPORT_ICON);
exportBtn.setAttribute("aria-label", MESSAGES.LABEL_EXPORT_CHAT);
exportBtn.addEventListener("click", () => {
if (meta.id === this.hooks.activeId) this.hooks.exportActive();
else void this.exportToFile(meta);
});
}
private async saveToVault(meta: SessionMeta): Promise<void> {
const exported = await this.sessionExport(meta);
if (exported !== null) await saveChatNote(this.app.vault, exported.markdown);
}
private async exportToFile(meta: SessionMeta): Promise<void> {
await exportChatFile(
(name) => this.plugin.chooseChatExportPath(name),
() => this.sessionExport(meta),
meta.id,
);
}
/** The server's markdown export of `meta`, or null after a notice saying why there is none. */
private async sessionExport(meta: SessionMeta): Promise<SessionExport | null> {
try {
await this.hooks.writesSettled();
return await this.plugin.api.getSessionMarkdown(meta.id);
} catch (err) {
if (err instanceof Error && isHttpStatus(err, HTTP_STATUS.NOT_FOUND)) {
new Notice(MESSAGES.ERROR_SESSION_EXPORT_NOT_FOUND);
} else {
const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
new Notice(MESSAGES.ERROR_SESSION_EXPORT_FAILED(reason));
}
return null;
}
}
private renderRenameField(row: HTMLElement, meta: SessionMeta): void {
const input = row.createEl("input", {
cls: "lilbee-session-rename-input",
placeholder: MESSAGES.PLACEHOLDER_RENAME_SESSION,
attr: { type: "text" },
});
input.value = meta.title;
// Focus after the row re-renders; select the title so typing replaces it.
window.setTimeout(() => {
input.focus();
input.select();
}, 0);
input.addEventListener("keydown", (evt: KeyboardEvent) => {
if (evt.key === "Enter") void this.commitRename(meta, input.value);
else if (evt.key === "Escape") {
evt.stopPropagation();
this.renamingId = null;
this.renderList();
}
});
}
/** An empty title is discarded rather than written, matching the TUI. */
private async commitRename(meta: SessionMeta, raw: string): Promise<void> {
const title = raw.trim();
this.renamingId = null;
if (!title || title === meta.title) {
this.renderList();
return;
}
try {
// A queued automatic title lands first, so the user's title is the last write.
await this.hooks.writesSettled();
await this.plugin.api.renameSession(meta.id, title);
meta.title = title;
} catch (err) {
const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
new Notice(MESSAGES.ERROR_SESSION_RENAME_FAILED(reason));
}
this.renderList();
}
private async confirmDelete(meta: SessionMeta): Promise<void> {
const modal = new ConfirmModal(this.app, MESSAGES.NOTICE_CONFIRM_DELETE_SESSION(meta.title));
modal.open();
if (!(await modal.result)) return;
try {
await this.hooks.writesSettled();
await this.plugin.api.deleteSession(meta.id);
this.sessions = this.sessions.filter((s) => s.id !== meta.id);
new Notice(MESSAGES.NOTICE_SESSION_DELETED(meta.title));
this.renderList();
} catch (err) {
const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
new Notice(MESSAGES.ERROR_SESSION_DELETE_FAILED(reason));
}
}
private resume(meta: SessionMeta): void {
this.hooks.resume(meta.id);
this.close();
}
}
|