UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

294 lines • 12.8 kB
import { Container, fuzzyFilter, getKeybindings, Input, Key, matchesKey, Spacer, Text, } from "@earendil-works/pi-tui"; import { getModelSearchText } from "../model-search.js"; import { theme } from "../theme/theme.js"; import { DynamicBorder } from "./dynamic-border.js"; import { keyDisplayText } from "./keybinding-hints.js"; function isEnabled(enabledIds, id) { return enabledIds === null || enabledIds.includes(id); } function toggle(enabledIds, id) { if (enabledIds === null) return [id]; const index = enabledIds.indexOf(id); if (index >= 0) return [...enabledIds.slice(0, index), ...enabledIds.slice(index + 1)]; return [...enabledIds, id]; } function enableAll(enabledIds, allIds, targetIds) { if (enabledIds === null) return null; const targets = targetIds ?? allIds; const result = [...enabledIds]; for (const id of targets) { if (!result.includes(id)) result.push(id); } return result; } function clearAll(enabledIds, allIds, targetIds) { if (enabledIds === null) { return targetIds ? allIds.filter((id) => !targetIds.includes(id)) : []; } const targets = new Set(targetIds ?? enabledIds); return enabledIds.filter((id) => !targets.has(id)); } function move(enabledIds, id, delta) { if (enabledIds === null) return null; const list = [...enabledIds]; const index = list.indexOf(id); if (index < 0) return list; const newIndex = index + delta; if (newIndex < 0 || newIndex >= list.length) return list; const result = [...list]; [result[index], result[newIndex]] = [result[newIndex], result[index]]; return result; } function getSortedIds(enabledIds, allIds) { if (enabledIds === null) return allIds; const enabledSet = new Set(enabledIds); return [...enabledIds, ...allIds.filter((id) => !enabledSet.has(id))]; } export class ScopedModelsSelectorComponent extends Container { get focused() { return this._focused; } set focused(value) { this._focused = value; this.searchInput.focused = value; } constructor(config, callbacks) { super(); this.modelsById = new Map(); this.allIds = []; this.enabledIds = null; this.filteredItems = []; this.selectedIndex = 0; this._focused = false; this.maxVisible = 8; this.isDirty = false; this.callbacks = callbacks; for (const model of config.allModels) { const fullId = `${model.provider}/${model.id}`; this.modelsById.set(fullId, model); this.allIds.push(fullId); } this.enabledIds = config.enabledModelIds === null ? null : [...config.enabledModelIds]; this.filteredItems = this.buildItems(); this.addChild(new DynamicBorder()); this.addChild(new Spacer(1)); this.addChild(new Text(theme.fg("accent", theme.bold("Model Configuration")), 0, 0)); this.addChild(new Text(theme.fg("muted", `Session-only. ${keyDisplayText("app.models.save")} to save to settings.`), 0, 0)); this.addChild(new Spacer(1)); this.searchInput = new Input(); this.addChild(this.searchInput); this.addChild(new Spacer(1)); this.listContainer = new Container(); this.addChild(this.listContainer); this.addChild(new Spacer(1)); if (config.refreshStatus) { this.refreshStatusText = new Text(theme.fg("muted", ` ${config.refreshStatus}`), 0, 0); this.addChild(this.refreshStatusText); } this.footerText = new Text(this.getFooterText(), 0, 0); this.addChild(this.footerText); this.addChild(new DynamicBorder()); this.updateList(); } updateModels(models, enabledModelIds) { const selectedId = this.filteredItems[this.selectedIndex]?.fullId; if (enabledModelIds !== undefined) this.enabledIds = enabledModelIds === null ? null : [...enabledModelIds]; this.modelsById.clear(); this.allIds = []; for (const model of models) { const fullId = `${model.provider}/${model.id}`; this.modelsById.set(fullId, model); this.allIds.push(fullId); } this.refresh(); const refreshedIndex = selectedId ? this.filteredItems.findIndex((item) => item.fullId === selectedId) : -1; if (refreshedIndex >= 0) { this.selectedIndex = refreshedIndex; this.updateList(); } } setRefreshStatus(message, kind) { this.refreshStatusText?.setText(theme.fg(kind, ` ${message}`)); } buildItems() { return getSortedIds(this.enabledIds, this.allIds).map((id) => ({ fullId: id, model: this.modelsById.get(id), enabled: isEnabled(this.enabledIds, id), })); } getFooterText() { const enabledCount = this.enabledIds?.filter((id) => this.modelsById.has(id)).length ?? this.allIds.length; const unavailableCount = this.enabledIds?.filter((id) => !this.modelsById.has(id)).length ?? 0; const countText = this.enabledIds === null ? "all enabled" : `${enabledCount}/${this.allIds.length} enabled${unavailableCount ? ` · ${unavailableCount} unavailable` : ""}`; const parts = [ `${keyDisplayText("tui.select.confirm")} toggle`, `${keyDisplayText("app.models.enableAll")} all`, `${keyDisplayText("app.models.clearAll")} clear`, `${keyDisplayText("app.models.toggleProvider")} provider`, `${keyDisplayText("app.models.reorderUp")}/${keyDisplayText("app.models.reorderDown")} reorder`, `${keyDisplayText("app.models.save")} save`, countText, ]; return this.isDirty ? theme.fg("dim", ` ${parts.join(" · ")} `) + theme.fg("warning", "(unsaved)") : theme.fg("dim", ` ${parts.join(" · ")}`); } refresh() { const query = this.searchInput.getValue(); const items = this.buildItems(); this.filteredItems = query ? fuzzyFilter(items, query, (item) => item.model ? getModelSearchText({ id: item.model.id, provider: item.model.provider, name: item.model.name }) : item.fullId) : items; this.selectedIndex = Math.min(this.selectedIndex, Math.max(0, this.filteredItems.length - 1)); this.updateList(); this.footerText.setText(this.getFooterText()); } notifyChange() { this.callbacks.onChange(this.enabledIds === null ? null : [...this.enabledIds]); } updateList() { this.listContainer.clear(); if (this.filteredItems.length === 0) { this.listContainer.addChild(new Text(theme.fg("muted", " No matching models"), 0, 0)); return; } const startIndex = Math.max(0, Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredItems.length - this.maxVisible)); const endIndex = Math.min(startIndex + this.maxVisible, this.filteredItems.length); for (let i = startIndex; i < endIndex; i++) { const item = this.filteredItems[i]; const isSelected = i === this.selectedIndex; const prefix = isSelected ? theme.fg("accent", "→ ") : " "; const id = item.model?.id ?? item.fullId; const styledId = item.model ? id : theme.strikethrough(id); const modelText = isSelected ? theme.fg("accent", styledId) : styledId; const providerBadge = theme.fg("muted", item.model ? ` [${item.model.provider}]` : " [unavailable]"); const status = item.model && item.enabled ? theme.fg("accent", "✓ ") : " "; this.listContainer.addChild(new Text(`${prefix}${status}${modelText}${providerBadge}`, 0, 0)); } if (startIndex > 0 || endIndex < this.filteredItems.length) { this.listContainer.addChild(new Text(theme.fg("muted", ` (${this.selectedIndex + 1}/${this.filteredItems.length})`), 0, 0)); } const selected = this.filteredItems[this.selectedIndex]; if (selected) { this.listContainer.addChild(new Spacer(1)); this.listContainer.addChild(new Text(theme.fg("muted", ` ${selected.model ? `Model Name: ${selected.model.name}` : "Model unavailable"}`), 0, 0)); } } handleInput(data) { const kb = getKeybindings(); if (kb.matches(data, "tui.select.up")) { if (this.filteredItems.length === 0) return; this.selectedIndex = this.selectedIndex === 0 ? this.filteredItems.length - 1 : this.selectedIndex - 1; this.updateList(); return; } if (kb.matches(data, "tui.select.down")) { if (this.filteredItems.length === 0) return; this.selectedIndex = this.selectedIndex === this.filteredItems.length - 1 ? 0 : this.selectedIndex + 1; this.updateList(); return; } const reorderUp = kb.matches(data, "app.models.reorderUp"); const reorderDown = kb.matches(data, "app.models.reorderDown"); if (reorderUp || reorderDown) { if (this.enabledIds === null) return; const item = this.filteredItems[this.selectedIndex]; if (item && isEnabled(this.enabledIds, item.fullId)) { const delta = reorderUp ? -1 : 1; const currentIndex = this.enabledIds.indexOf(item.fullId); const newIndex = currentIndex + delta; if (newIndex >= 0 && newIndex < this.enabledIds.length) { this.enabledIds = move(this.enabledIds, item.fullId, delta); this.isDirty = true; this.selectedIndex += delta; this.refresh(); this.notifyChange(); } } return; } if (kb.matches(data, "tui.select.confirm")) { const item = this.filteredItems[this.selectedIndex]; if (item) { this.enabledIds = toggle(this.enabledIds, item.fullId); this.isDirty = true; this.refresh(); this.notifyChange(); } return; } if (kb.matches(data, "app.models.enableAll")) { const targetIds = this.searchInput.getValue() ? this.filteredItems.map((item) => item.fullId) : undefined; this.enabledIds = enableAll(this.enabledIds, this.allIds, targetIds); this.isDirty = true; this.refresh(); this.notifyChange(); return; } if (kb.matches(data, "app.models.clearAll")) { const targetIds = this.searchInput.getValue() ? this.filteredItems.map((item) => item.fullId) : undefined; this.enabledIds = clearAll(this.enabledIds, this.allIds, targetIds); this.isDirty = true; this.refresh(); this.notifyChange(); return; } if (kb.matches(data, "app.models.toggleProvider")) { const item = this.filteredItems[this.selectedIndex]; if (item?.model) { const providerIds = this.allIds.filter((id) => this.modelsById.get(id)?.provider === item.model?.provider); const allEnabled = providerIds.every((id) => isEnabled(this.enabledIds, id)); this.enabledIds = allEnabled ? clearAll(this.enabledIds, this.allIds, providerIds) : enableAll(this.enabledIds, this.allIds, providerIds); this.isDirty = true; this.refresh(); this.notifyChange(); } return; } if (kb.matches(data, "app.models.save")) { this.callbacks.onPersist(this.enabledIds === null ? null : [...this.enabledIds]); this.isDirty = false; this.footerText.setText(this.getFooterText()); return; } if (matchesKey(data, Key.ctrl("c"))) { if (this.searchInput.getValue()) { this.searchInput.setValue(""); this.refresh(); } else { this.callbacks.onCancel(); } return; } if (matchesKey(data, Key.escape)) { this.callbacks.onCancel(); return; } this.searchInput.handleInput(data); this.refresh(); } getSearchInput() { return this.searchInput; } } //# sourceMappingURL=scoped-models-selector.js.map