示例工程 43-project-local-knowledge-base
43-project-local-knowledge-base/app/shared/ipc.ts
export const CH = {
snapshot: "knowledge:snapshot",
reindex: "knowledge:reindex",
startSamples: "knowledge:start-samples",
pick: "knowledge:pick",
search: "knowledge:search",
refresh: "knowledge:refresh",
} as const;
export type EventRow = {
at: number;
kind: string;
detail: string;
};
export type DocRow = {
id: number;
title: string;
path: string;
bytes: number;
chunks: number;
};
export type Hit = {
docId: number;
title: string;
path: string;
seq: number;
snippet: string;
score: number;
};
export type IndexState = {
status: "idle" | "indexing" | "ready" | "error";
current: number;
total: number;
progress: number;
lastError: string;
indexedAt: number;
docs: number;
chunks: number;
dbBytes: number;
};
export type Snapshot = {
platform: string;
electronVersion: string;
nodeVersion: string;
worker: { running: boolean; pid: number };
index: IndexState;
docs: DocRow[];
samples: string[];
lastQuery: string;
hits: Hit[];
searchMs: number;
searchError: string;
events: EventRow[];
};
export function assertSearchReq(
value: unknown,
): asserts value is { query: string } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_SEARCH_REQ");
}
const query = (value as { query?: unknown }).query;
if (
typeof query !== "string" ||
query.trim().length === 0 ||
query.length > 200
) {
throw new Error("BAD_QUERY");
}
}
export function assertNoArgs(
value: unknown,
): asserts value is Record<string, never> {
if (value !== undefined && value !== null && typeof value !== "object") {
throw new Error("BAD_ARGS");
}
}