Skip to content

Commit 1c5120d

Browse files
committed
fix: book.scan
1 parent bf6c613 commit 1c5120d

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lenml/char-card-reader",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "SillyTavern character card info reader",
55
"main": "dist/main.js",
66
"module": "dist/main.mjs",

src/CharacterBook.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,42 @@ export class CharacterBook implements SpecV3.Lorebook {
6767
}
6868
}
6969

70-
public scan(
70+
private _scan(
7171
context: string,
7272
matched: SpecV3.Lorebook["entries"] = [],
7373
current_depth = 1
7474
): SpecV3.Lorebook["entries"] {
7575
if (current_depth >= (this.scan_depth ?? 10)) {
7676
return uniq(matched);
7777
}
78-
const pending_entries = this.entries.filter((x) => !matched.includes(x));
78+
const current_context = [
79+
context,
80+
...uniq(matched).map((x) => x.content),
81+
].join("\n");
82+
const pending_entries = this.entries
83+
.filter((x) => x.content?.trim())
84+
.filter((x) => x.enabled && !matched.includes(x));
7985
if (pending_entries.length === 0) {
8086
return uniq(matched);
8187
}
8288
for (const entry of pending_entries) {
83-
const is_matched = entry.keys.some((k) => context.includes(k));
89+
const is_matched = entry.keys.some((k) => current_context.includes(k));
8490
if (is_matched) {
8591
matched.push(entry);
8692
}
8793
}
8894
if (this.recursive_scanning) {
89-
return this.scan(context, matched, current_depth + 1);
95+
return this._scan(context, matched, current_depth + 1);
9096
}
9197
return uniq(matched);
9298
}
99+
public scan(context: string): SpecV3.Lorebook["entries"] {
100+
const matched = this._scan(context);
101+
const constant = this.entries.filter(
102+
(x) => x.constant && x.enabled && x.content?.trim()
103+
);
104+
return uniq([...matched, ...constant]).sort(
105+
(a, b) => a.insertion_order - b.insertion_order
106+
);
107+
}
93108
}

0 commit comments

Comments
 (0)