Agnes 2.0-flash是推理模型,推理token占大量预算。 800仍不够→JSON截断(parse error: unexpected end of JSON input)。 1200确保推理+正文都够用。
127 lines
5.2 KiB
TypeScript
127 lines
5.2 KiB
TypeScript
import { App, Modal, setIcon } from "obsidian";
|
|
import { searchMemories, type RecallResult } from "./api";
|
|
|
|
export class SearchModal extends Modal {
|
|
private results: RecallResult["memories"] = [];
|
|
private inputEl: HTMLInputElement;
|
|
private listEl: HTMLElement;
|
|
private loading = false;
|
|
private debounceTimer: number | null = null;
|
|
onSelect?: (id: string, content: string) => void;
|
|
|
|
constructor(app: App) {
|
|
super(app);
|
|
}
|
|
|
|
onOpen() {
|
|
this.contentEl.createEl("style", {
|
|
text: `
|
|
.zhiyi-search-modal { width: 600px; max-height: 80vh; display: flex; flex-direction: column; }
|
|
.zhiyi-search-header {
|
|
display: flex; align-items: center; gap: 8px;
|
|
padding: 12px 16px; border-bottom: 1px solid var(--background-modifier-border);
|
|
}
|
|
.zhiyi-search-header h2 { margin: 0; font-size: 14px; font-weight: 600; flex: 1; }
|
|
.zhiyi-search-input {
|
|
flex: 1; padding: 8px 12px; font-size: 14px;
|
|
background: var(--background-secondary);
|
|
border: 1px solid var(--background-modifier-border);
|
|
border-radius: 6px; color: var(--text-normal); outline: none;
|
|
}
|
|
.zhiyi-search-input:focus { border-color: var(--text-accent); }
|
|
.zhiyi-search-results { flex: 1; overflow-y: auto; padding: 8px 0; }
|
|
.zhiyi-search-result {
|
|
padding: 10px 16px; border-bottom: 1px solid var(--background-modifier-border);
|
|
cursor: pointer;
|
|
}
|
|
.zhiyi-search-result:hover { background: var(--background-modifier-hover); }
|
|
.zhiyi-search-result-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
|
|
.zhiyi-search-score { font-size: 10px; color: var(--text-muted); }
|
|
.zhiyi-search-cat { font-size: 10px; background: var(--background-secondary); padding: 1px 4px; border-radius: 2px; color: var(--text-muted); }
|
|
.zhiyi-search-content { font-size: 12px; color: var(--text-normal); line-height: 1.5; }
|
|
.zhiyi-search-id { font-size: 10px; color: var(--text-faint); font-family: monospace; margin-top: 3px; }
|
|
.zhiyi-search-loading { padding: 20px; text-align: center; color: var(--text-muted); }
|
|
.zhiyi-search-empty { padding: 20px; text-align: center; color: var(--text-muted); }
|
|
.zhiyi-search-hint { padding: 8px 16px; font-size: 11px; color: var(--text-muted); text-align: center; }
|
|
`
|
|
});
|
|
|
|
const root = this.contentEl.createEl("div", { cls: "zhiyi-search-modal" });
|
|
const header = root.createEl("div", { cls: "zhiyi-search-header" });
|
|
header.createEl("h2", { text: "🔍 织忆语义搜索" });
|
|
this.inputEl = header.createEl("input", {
|
|
cls: "zhiyi-search-input",
|
|
attr: { placeholder: "输入关键词,语义搜索记忆…", autofocus: "true" }
|
|
}) as HTMLInputElement;
|
|
this.listEl = root.createEl("div", { cls: "zhiyi-search-results" });
|
|
root.createEl("div", { cls: "zhiyi-search-hint", text: "实时搜索 · 按 Enter 选择第一条结果" });
|
|
|
|
this.inputEl.oninput = () => {
|
|
if (this.debounceTimer !== null) {
|
|
(window as unknown as { clearTimeout: (n: number) => void }).clearTimeout(this.debounceTimer);
|
|
}
|
|
this.debounceTimer = (window as unknown as { setTimeout: (fn: () => void, ms: number) => number }).setTimeout(
|
|
() => this.doSearch(),
|
|
300
|
|
);
|
|
};
|
|
|
|
this.inputEl.onkeydown = (e) => {
|
|
if (e.key === "Enter" && this.results.length > 0) {
|
|
this.selectResult(this.results[0]);
|
|
}
|
|
if (e.key === "Escape") {
|
|
this.close();
|
|
}
|
|
};
|
|
}
|
|
|
|
async doSearch() {
|
|
const query = this.inputEl.value.trim();
|
|
if (!query) {
|
|
this.results = [];
|
|
this.listEl.empty();
|
|
return;
|
|
}
|
|
this.listEl.innerHTML = '<div class="zhiyi-search-loading">搜索中…</div>';
|
|
this.loading = true;
|
|
try {
|
|
const data = await searchMemories(query, "hermes-main");
|
|
this.results = data.memories || [];
|
|
this.renderResults();
|
|
} catch (e) {
|
|
this.listEl.innerHTML = `<div class="zhiyi-search-empty">搜索失败: ${(e as Error).message}</div>`;
|
|
}
|
|
}
|
|
|
|
private renderResults() {
|
|
this.listEl.empty();
|
|
if (this.results.length === 0) {
|
|
this.listEl.createEl("div", { cls: "zhiyi-search-empty", text: "未找到结果" });
|
|
return;
|
|
}
|
|
for (const r of this.results) {
|
|
const item = this.listEl.createEl("div", { cls: "zhiyi-search-result" });
|
|
const hdr = item.createEl("div", { cls: "zhiyi-search-result-header" });
|
|
hdr.createEl("span", { cls: "zhiyi-search-cat", text: r.category || "normal" });
|
|
hdr.createEl("span", { cls: "zhiyi-search-score", text: ` relevance: ${(r.score ?? 0).toFixed(3)}` });
|
|
item.createEl("div", { cls: "zhiyi-search-content", text: this.truncate(r.content, 200) });
|
|
item.createEl("div", { cls: "zhiyi-search-id", text: r.id });
|
|
item.onclick = () => this.selectResult(r);
|
|
}
|
|
}
|
|
|
|
private selectResult(r: { id: string; content: string }) {
|
|
this.onSelect?.(r.id, r.content);
|
|
this.close();
|
|
}
|
|
|
|
private truncate(s: string, max: number) {
|
|
if (!s) return "";
|
|
return s.length > max ? s.slice(0, max) + "…" : s;
|
|
}
|
|
|
|
onClose() {
|
|
this.listEl.empty();
|
|
}
|
|
} |