@luxass/pi-session-search 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +85 -2
- package/package.json +49 -3
- package/src/current-session-search.ts +233 -0
- package/src/display-text.ts +391 -0
- package/src/finder-overlay.ts +1075 -0
- package/src/hits.ts +77 -0
- package/src/index-client.ts +404 -0
- package/src/index-search.ts +578 -0
- package/src/index-worker.ts +102 -0
- package/src/index.ts +196 -0
- package/src/jsonl-reader.ts +100 -0
- package/src/match-text.ts +65 -0
- package/src/open-match.ts +544 -0
- package/src/query.ts +490 -0
- package/src/searchable-text.ts +176 -0
- package/src/session-index.ts +577 -0
- package/src/transcript-scroll.ts +456 -0
- package/src/worker-protocol.ts +66 -0
package/src/hits.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The search result shape shared by both scopes, and the helper that cuts a
|
|
3
|
+
* long text down to the part around the match.
|
|
4
|
+
*
|
|
5
|
+
* Loaded by the index worker, which strips types itself: type annotations only, and no Pi packages.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { bestMatchIndex } from "./query.ts";
|
|
9
|
+
import type { ItemKind } from "./searchable-text.ts";
|
|
10
|
+
|
|
11
|
+
/** One verified match, ready for the picker. */
|
|
12
|
+
export interface SearchHit {
|
|
13
|
+
/** Absolute session file path. Undefined for an unsaved current session. */
|
|
14
|
+
sessionFile: string | undefined;
|
|
15
|
+
sessionId: string;
|
|
16
|
+
/** Session name from /name, if any. */
|
|
17
|
+
sessionName?: string;
|
|
18
|
+
/** Working directory recorded in the session header. */
|
|
19
|
+
cwd: string;
|
|
20
|
+
entryId: string;
|
|
21
|
+
part: number;
|
|
22
|
+
kind: ItemKind;
|
|
23
|
+
toolName?: string;
|
|
24
|
+
timestamp: number;
|
|
25
|
+
/** True when the entry is on the branch pi opens for that session. */
|
|
26
|
+
onActiveBranch: boolean;
|
|
27
|
+
/** Text shown in the preview: the item text, or the window around the match. */
|
|
28
|
+
text: string;
|
|
29
|
+
/** True when `text` starts after the beginning of the item text. */
|
|
30
|
+
clippedStart: boolean;
|
|
31
|
+
/** True when `text` stops before the end of the item text. */
|
|
32
|
+
clippedEnd: boolean;
|
|
33
|
+
/** True when the hit came from the fuzzy fallback instead of literal terms. */
|
|
34
|
+
fuzzy: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** How much text around the first match the preview keeps. */
|
|
38
|
+
export const PREVIEW_WINDOW_CHARS = 6000;
|
|
39
|
+
|
|
40
|
+
export interface TextWindow {
|
|
41
|
+
text: string;
|
|
42
|
+
clippedStart: boolean;
|
|
43
|
+
clippedEnd: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Keeps at most `size` characters, starting a little before the first match,
|
|
48
|
+
* so huge tool outputs stay cheap to send, wrap, and render.
|
|
49
|
+
*/
|
|
50
|
+
export function windowAroundMatch(
|
|
51
|
+
text: string,
|
|
52
|
+
patterns: RegExp[],
|
|
53
|
+
size = PREVIEW_WINDOW_CHARS,
|
|
54
|
+
): TextWindow {
|
|
55
|
+
if (text.length <= size) {
|
|
56
|
+
return { text, clippedStart: false, clippedEnd: false };
|
|
57
|
+
}
|
|
58
|
+
const matchAt = bestMatchIndex(text, patterns);
|
|
59
|
+
let start = Math.max(0, matchAt - Math.floor(size / 4));
|
|
60
|
+
start = Math.min(start, text.length - size);
|
|
61
|
+
// Start on a line boundary when one is close, so the preview does not open mid-line.
|
|
62
|
+
const lineStart = text.lastIndexOf("\n", matchAt);
|
|
63
|
+
if (lineStart >= start - 200 && lineStart < matchAt && lineStart >= 0) {
|
|
64
|
+
start = Math.max(0, Math.min(lineStart + 1, text.length - size));
|
|
65
|
+
}
|
|
66
|
+
const end = Math.min(text.length, start + size);
|
|
67
|
+
return {
|
|
68
|
+
text: text.slice(start, end),
|
|
69
|
+
clippedStart: start > 0,
|
|
70
|
+
clippedEnd: end < text.length,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Stable identity of a hit, used to keep the selection when results refresh. */
|
|
75
|
+
export function hitKey(hit: SearchHit): string {
|
|
76
|
+
return `${hit.sessionFile ?? hit.sessionId}#${hit.entryId}#${hit.part}`;
|
|
77
|
+
}
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main-thread side of the /find-all index. Starts index workers, forwards
|
|
3
|
+
* requests, and turns replies into promises and progress events. Nothing here
|
|
4
|
+
* touches SQLite or reads session files.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Worker } from "node:worker_threads";
|
|
8
|
+
|
|
9
|
+
import type { SearchHit } from "./hits.ts";
|
|
10
|
+
import type { RoleFilter } from "./query.ts";
|
|
11
|
+
import type { SyncProgress, SyncSummary } from "./session-index.ts";
|
|
12
|
+
import {
|
|
13
|
+
SEARCH_SLOT,
|
|
14
|
+
SIGNAL_SLOTS,
|
|
15
|
+
STOP_SLOT,
|
|
16
|
+
type WorkerReply,
|
|
17
|
+
type WorkerRequest,
|
|
18
|
+
type WorkerSetup,
|
|
19
|
+
} from "./worker-protocol.ts";
|
|
20
|
+
|
|
21
|
+
/** Where the index lives and which session directories it covers. */
|
|
22
|
+
export interface IndexLocation {
|
|
23
|
+
databasePath: string;
|
|
24
|
+
sessionRoots: string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Source of the script that starts an index worker. Node refuses to strip
|
|
29
|
+
* TypeScript types from files under node_modules, which is where pi installs
|
|
30
|
+
* packages. The script registers a load hook, scoped to this worker thread and
|
|
31
|
+
* to this package's source folder, that strips the types itself, then imports
|
|
32
|
+
* index-worker.ts. The type stripping API is experimental in Node 24 and warns
|
|
33
|
+
* once; that warning is dropped because the worker's stderr is captured.
|
|
34
|
+
*
|
|
35
|
+
* `sourceFolder` is the folder that holds index-worker.ts. Tests pass a copy
|
|
36
|
+
* placed under node_modules.
|
|
37
|
+
*/
|
|
38
|
+
export function indexWorkerLoaderSource(
|
|
39
|
+
sourceFolder: URL = new URL("./", import.meta.url),
|
|
40
|
+
): string {
|
|
41
|
+
const sourceFolderUrl = sourceFolder.href;
|
|
42
|
+
const workerUrl = new URL("./index-worker.ts", sourceFolder).href;
|
|
43
|
+
return `"use strict";
|
|
44
|
+
const { readFileSync } = require("node:fs");
|
|
45
|
+
const { registerHooks, stripTypeScriptTypes } = require("node:module");
|
|
46
|
+
const sourceFolderUrl = ${JSON.stringify(sourceFolderUrl)};
|
|
47
|
+
const emitWarning = process.emitWarning.bind(process);
|
|
48
|
+
process.emitWarning = (warning, ...rest) => {
|
|
49
|
+
const message = typeof warning === "string" ? warning : warning && warning.message;
|
|
50
|
+
if (typeof message === "string" && message.includes("stripTypeScriptTypes")) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
emitWarning(warning, ...rest);
|
|
54
|
+
};
|
|
55
|
+
registerHooks({
|
|
56
|
+
load(url, context, nextLoad) {
|
|
57
|
+
if (url.startsWith(sourceFolderUrl) && url.endsWith(".ts")) {
|
|
58
|
+
const source = readFileSync(new URL(url), "utf8");
|
|
59
|
+
return { format: "module", source: stripTypeScriptTypes(source), shortCircuit: true };
|
|
60
|
+
}
|
|
61
|
+
return nextLoad(url, context);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
import(${JSON.stringify(workerUrl)}).catch((error) => {
|
|
65
|
+
process.nextTick(() => {
|
|
66
|
+
throw error;
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** One worker thread plus the shared slots used to stop or supersede its work. */
|
|
73
|
+
class IndexWorker {
|
|
74
|
+
private readonly worker: Worker;
|
|
75
|
+
private readonly signals: Int32Array;
|
|
76
|
+
private readonly stderrTail: string[] = [];
|
|
77
|
+
private exited = false;
|
|
78
|
+
|
|
79
|
+
constructor(
|
|
80
|
+
databasePath: string,
|
|
81
|
+
onReply: (reply: WorkerReply) => void,
|
|
82
|
+
onCrash: (message: string) => void,
|
|
83
|
+
) {
|
|
84
|
+
const sharedSignals = new SharedArrayBuffer(SIGNAL_SLOTS * Int32Array.BYTES_PER_ELEMENT);
|
|
85
|
+
this.signals = new Int32Array(sharedSignals);
|
|
86
|
+
const setup: WorkerSetup = { databasePath, signals: sharedSignals };
|
|
87
|
+
// stdout and stderr are captured so warnings never draw over pi's screen.
|
|
88
|
+
this.worker = new Worker(indexWorkerLoaderSource(), {
|
|
89
|
+
eval: true,
|
|
90
|
+
workerData: setup,
|
|
91
|
+
stdout: true,
|
|
92
|
+
stderr: true,
|
|
93
|
+
execArgv: [],
|
|
94
|
+
});
|
|
95
|
+
this.worker.stdout.resume();
|
|
96
|
+
this.worker.stderr.on("data", (chunk: Buffer) => {
|
|
97
|
+
this.stderrTail.push(chunk.toString("utf8"));
|
|
98
|
+
if (this.stderrTail.length > 20) {
|
|
99
|
+
this.stderrTail.shift();
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
this.worker.on("message", onReply);
|
|
103
|
+
this.worker.on("error", (error: Error) => onCrash(error.message));
|
|
104
|
+
this.worker.on("exit", (code) => {
|
|
105
|
+
this.exited = true;
|
|
106
|
+
if (code !== 0) {
|
|
107
|
+
const detail = this.stderrTail.join("").trim().split("\n").pop() ?? "";
|
|
108
|
+
onCrash(`index worker exited with code ${code}${detail === "" ? "" : `: ${detail}`}`);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
// Never keep pi alive just for the index.
|
|
112
|
+
this.worker.unref();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
send(request: WorkerRequest): void {
|
|
116
|
+
if (!this.exited) {
|
|
117
|
+
// oxlint-disable-next-line unicorn/require-post-message-target-origin -- a worker thread, not a window.
|
|
118
|
+
this.worker.postMessage(request);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
setLatestSearch(requestId: number): void {
|
|
123
|
+
Atomics.store(this.signals, SEARCH_SLOT, requestId);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
requestStop(): void {
|
|
127
|
+
Atomics.store(this.signals, STOP_SLOT, 1);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Asks the worker to close the database and exit after its current work. */
|
|
131
|
+
close(): void {
|
|
132
|
+
this.requestStop();
|
|
133
|
+
this.setLatestSearch(-1);
|
|
134
|
+
this.send({ type: "close" });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type ProgressListener = (progress: SyncProgress) => void;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Runs one index update at a time in this process. Callers that ask while an
|
|
142
|
+
* update runs share its promise and progress events.
|
|
143
|
+
*/
|
|
144
|
+
export class IndexUpdater {
|
|
145
|
+
private readonly location: IndexLocation;
|
|
146
|
+
private running: Promise<SyncSummary> | undefined;
|
|
147
|
+
private stopRequested = false;
|
|
148
|
+
private worker: IndexWorker | undefined;
|
|
149
|
+
private readonly listeners = new Set<ProgressListener>();
|
|
150
|
+
private latest: SyncProgress | undefined;
|
|
151
|
+
|
|
152
|
+
constructor(location: IndexLocation) {
|
|
153
|
+
this.location = location;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
get isRunning(): boolean {
|
|
157
|
+
return this.running !== undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Last progress of the running update, for status lines. */
|
|
161
|
+
get progress(): SyncProgress | undefined {
|
|
162
|
+
return this.running === undefined ? undefined : this.latest;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
onProgress(listener: ProgressListener): () => void {
|
|
166
|
+
this.listeners.add(listener);
|
|
167
|
+
return () => {
|
|
168
|
+
this.listeners.delete(listener);
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Starts an update, or joins the one already running. */
|
|
173
|
+
update(): Promise<SyncSummary> {
|
|
174
|
+
if (this.running !== undefined) {
|
|
175
|
+
// A stopping update would end early, so queue a fresh one behind it.
|
|
176
|
+
if (this.stopRequested) {
|
|
177
|
+
return this.running.then(
|
|
178
|
+
() => this.update(),
|
|
179
|
+
() => this.update(),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
return this.running;
|
|
183
|
+
}
|
|
184
|
+
this.latest = undefined;
|
|
185
|
+
this.stopRequested = false;
|
|
186
|
+
// The executor runs at once, so both are set before the worker starts.
|
|
187
|
+
let resolve: ((summary: SyncSummary) => void) | undefined;
|
|
188
|
+
let reject: ((error: Error) => void) | undefined;
|
|
189
|
+
const running = new Promise<SyncSummary>((resolveRunning, rejectRunning) => {
|
|
190
|
+
resolve = resolveRunning;
|
|
191
|
+
reject = rejectRunning;
|
|
192
|
+
});
|
|
193
|
+
// Publish the running update before the worker starts, so a worker that
|
|
194
|
+
// fails at once still clears it and the next update can start fresh.
|
|
195
|
+
this.running = running;
|
|
196
|
+
let settled = false;
|
|
197
|
+
const finish = (outcome: () => void): void => {
|
|
198
|
+
if (settled) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
settled = true;
|
|
202
|
+
this.worker?.close();
|
|
203
|
+
this.worker = undefined;
|
|
204
|
+
if (this.running === running) {
|
|
205
|
+
this.running = undefined;
|
|
206
|
+
}
|
|
207
|
+
outcome();
|
|
208
|
+
};
|
|
209
|
+
try {
|
|
210
|
+
this.worker = new IndexWorker(
|
|
211
|
+
this.location.databasePath,
|
|
212
|
+
(reply) => {
|
|
213
|
+
if (reply.type === "progress") {
|
|
214
|
+
this.latest = { done: reply.done, total: reply.total, changed: reply.changed };
|
|
215
|
+
for (const listener of this.listeners) {
|
|
216
|
+
listener(this.latest);
|
|
217
|
+
}
|
|
218
|
+
} else if (reply.type === "synced") {
|
|
219
|
+
finish(() => resolve?.(reply.summary));
|
|
220
|
+
} else if (reply.type === "failed") {
|
|
221
|
+
finish(() => reject?.(new Error(reply.message)));
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
(message) => finish(() => reject?.(new Error(message))),
|
|
225
|
+
);
|
|
226
|
+
this.worker.send({ type: "sync", roots: this.location.sessionRoots });
|
|
227
|
+
} catch (error) {
|
|
228
|
+
finish(() => reject?.(error instanceof Error ? error : new Error(String(error))));
|
|
229
|
+
}
|
|
230
|
+
return running;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Stops a running update after its current transaction. Committed work stays. */
|
|
234
|
+
stop(): void {
|
|
235
|
+
if (this.running !== undefined) {
|
|
236
|
+
this.stopRequested = true;
|
|
237
|
+
this.worker?.requestStop();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export interface IndexSearchReply {
|
|
243
|
+
hits: SearchHit[];
|
|
244
|
+
/** True when the search stopped early, so more matches may exist. */
|
|
245
|
+
truncated: boolean;
|
|
246
|
+
/** Session files that had more matches than the per-session cap allowed. */
|
|
247
|
+
sessionsWithMore: string[];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Which indexed item to read back in full; a hit of the all-sessions index. */
|
|
251
|
+
export interface IndexedItem {
|
|
252
|
+
sessionFile: string;
|
|
253
|
+
entryId: string;
|
|
254
|
+
part: number;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
interface PendingRead {
|
|
258
|
+
resolve: (text: string | undefined) => void;
|
|
259
|
+
reject: (error: Error) => void;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Searches the index in a worker. Only the newest search matters: starting a
|
|
264
|
+
* search makes older ones resolve to undefined and stop inside the worker.
|
|
265
|
+
* Item reads are independent of searches and are all answered.
|
|
266
|
+
*/
|
|
267
|
+
export class IndexSearcher {
|
|
268
|
+
private readonly location: IndexLocation;
|
|
269
|
+
private worker: IndexWorker | undefined;
|
|
270
|
+
private nextRequestId = 1;
|
|
271
|
+
private pending:
|
|
272
|
+
| {
|
|
273
|
+
requestId: number;
|
|
274
|
+
resolve: (reply: IndexSearchReply | undefined) => void;
|
|
275
|
+
reject: (error: Error) => void;
|
|
276
|
+
}
|
|
277
|
+
| undefined;
|
|
278
|
+
private readonly pendingReads = new Map<number, PendingRead>();
|
|
279
|
+
private crashMessage: string | undefined;
|
|
280
|
+
|
|
281
|
+
constructor(location: IndexLocation) {
|
|
282
|
+
this.location = location;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Reads the full text of an indexed item. Resolves to undefined when the
|
|
287
|
+
* session file no longer holds the item as indexed, or the searcher closes.
|
|
288
|
+
*/
|
|
289
|
+
readItem(item: IndexedItem): Promise<string | undefined> {
|
|
290
|
+
if (this.crashMessage !== undefined) {
|
|
291
|
+
return Promise.reject(new Error(this.crashMessage));
|
|
292
|
+
}
|
|
293
|
+
const worker = this.ensureWorker();
|
|
294
|
+
const requestId = this.nextRequestId;
|
|
295
|
+
this.nextRequestId += 1;
|
|
296
|
+
return new Promise((resolve, reject) => {
|
|
297
|
+
this.pendingReads.set(requestId, { resolve, reject });
|
|
298
|
+
worker.send({
|
|
299
|
+
type: "readItem",
|
|
300
|
+
requestId,
|
|
301
|
+
sessionFile: item.sessionFile,
|
|
302
|
+
entryId: item.entryId,
|
|
303
|
+
part: item.part,
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* `perSessionLimit` caps the hits of each session file; undefined for no cap.
|
|
310
|
+
* `now` is the time relative since: and until: values count back from.
|
|
311
|
+
*/
|
|
312
|
+
search(
|
|
313
|
+
queryText: string,
|
|
314
|
+
roleFilter: RoleFilter,
|
|
315
|
+
limit: number,
|
|
316
|
+
perSessionLimit?: number,
|
|
317
|
+
now: number = Date.now(),
|
|
318
|
+
): Promise<IndexSearchReply | undefined> {
|
|
319
|
+
if (this.crashMessage !== undefined) {
|
|
320
|
+
return Promise.reject(new Error(this.crashMessage));
|
|
321
|
+
}
|
|
322
|
+
this.pending?.resolve(undefined);
|
|
323
|
+
this.pending = undefined;
|
|
324
|
+
|
|
325
|
+
const worker = this.ensureWorker();
|
|
326
|
+
const requestId = this.nextRequestId;
|
|
327
|
+
this.nextRequestId += 1;
|
|
328
|
+
worker.setLatestSearch(requestId);
|
|
329
|
+
return new Promise((resolve, reject) => {
|
|
330
|
+
this.pending = { requestId, resolve, reject };
|
|
331
|
+
worker.send({
|
|
332
|
+
type: "search",
|
|
333
|
+
requestId,
|
|
334
|
+
queryText,
|
|
335
|
+
roleFilter,
|
|
336
|
+
limit,
|
|
337
|
+
now,
|
|
338
|
+
perSessionLimit,
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
close(): void {
|
|
344
|
+
this.pending?.resolve(undefined);
|
|
345
|
+
this.pending = undefined;
|
|
346
|
+
for (const read of this.pendingReads.values()) {
|
|
347
|
+
read.resolve(undefined);
|
|
348
|
+
}
|
|
349
|
+
this.pendingReads.clear();
|
|
350
|
+
this.worker?.close();
|
|
351
|
+
this.worker = undefined;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
private ensureWorker(): IndexWorker {
|
|
355
|
+
if (this.worker === undefined) {
|
|
356
|
+
this.worker = new IndexWorker(
|
|
357
|
+
this.location.databasePath,
|
|
358
|
+
(reply) => this.onReply(reply),
|
|
359
|
+
(message) => {
|
|
360
|
+
this.crashMessage = message;
|
|
361
|
+
this.worker = undefined;
|
|
362
|
+
this.pending?.reject(new Error(message));
|
|
363
|
+
this.pending = undefined;
|
|
364
|
+
for (const read of this.pendingReads.values()) {
|
|
365
|
+
read.reject(new Error(message));
|
|
366
|
+
}
|
|
367
|
+
this.pendingReads.clear();
|
|
368
|
+
},
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
return this.worker;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private onReply(reply: WorkerReply): void {
|
|
375
|
+
if (reply.type === "item" || reply.type === "failed") {
|
|
376
|
+
const read =
|
|
377
|
+
reply.requestId === undefined ? undefined : this.pendingReads.get(reply.requestId);
|
|
378
|
+
if (read !== undefined && reply.requestId !== undefined) {
|
|
379
|
+
this.pendingReads.delete(reply.requestId);
|
|
380
|
+
if (reply.type === "item") {
|
|
381
|
+
read.resolve(reply.text);
|
|
382
|
+
} else {
|
|
383
|
+
read.reject(new Error(reply.message));
|
|
384
|
+
}
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
const pending = this.pending;
|
|
389
|
+
if (pending === undefined) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (reply.type === "results" && reply.requestId === pending.requestId) {
|
|
393
|
+
this.pending = undefined;
|
|
394
|
+
pending.resolve({
|
|
395
|
+
hits: reply.hits,
|
|
396
|
+
truncated: reply.truncated,
|
|
397
|
+
sessionsWithMore: reply.sessionsWithMore,
|
|
398
|
+
});
|
|
399
|
+
} else if (reply.type === "failed" && reply.requestId === pending.requestId) {
|
|
400
|
+
this.pending = undefined;
|
|
401
|
+
pending.reject(new Error(reply.message));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|