dsh-library 0.1.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/CHANGELOG.md +19 -0
- package/LICENSE +201 -0
- package/README.es.md +158 -0
- package/README.hi.md +158 -0
- package/README.md +158 -0
- package/README.pt.md +158 -0
- package/README.zh.md +158 -0
- package/SECURITY.md +38 -0
- package/THIRD_PARTY_NOTICES.md +27 -0
- package/cordis.patch.yml +79 -0
- package/lib/index.js +6074 -0
- package/lib/types/config.d.ts +208 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/embedding.d.ts +56 -0
- package/lib/types/embedding.d.ts.map +1 -0
- package/lib/types/ids.d.ts +27 -0
- package/lib/types/ids.d.ts.map +1 -0
- package/lib/types/index.d.ts +248 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/quality/chunk-visual.d.ts +93 -0
- package/lib/types/quality/chunk-visual.d.ts.map +1 -0
- package/lib/types/quality/citation.d.ts +76 -0
- package/lib/types/quality/citation.d.ts.map +1 -0
- package/lib/types/quality/diversity.d.ts +54 -0
- package/lib/types/quality/diversity.d.ts.map +1 -0
- package/lib/types/quality/few-shot.d.ts +54 -0
- package/lib/types/quality/few-shot.d.ts.map +1 -0
- package/lib/types/quality/lost-middle.d.ts +95 -0
- package/lib/types/quality/lost-middle.d.ts.map +1 -0
- package/lib/types/quality/purge.d.ts +55 -0
- package/lib/types/quality/purge.d.ts.map +1 -0
- package/lib/types/quality/reference.d.ts +75 -0
- package/lib/types/quality/reference.d.ts.map +1 -0
- package/lib/types/quality/relevance.d.ts +44 -0
- package/lib/types/quality/relevance.d.ts.map +1 -0
- package/lib/types/text.d.ts +51 -0
- package/lib/types/text.d.ts.map +1 -0
- package/package.json +149 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config schema and resolution for `dsh-library`. Every tunable is a
|
|
3
|
+
* validated {@link Config} field changeable from cordis.yml (no hardcoded
|
|
4
|
+
* tunables); `resolveConfig` is the explicit resolution step that fails loud
|
|
5
|
+
* on out-of-range values and invalid embedder commands.
|
|
6
|
+
* @module dsh-library/config
|
|
7
|
+
*/
|
|
8
|
+
import z from '@deepseek-ai/schemastery';
|
|
9
|
+
/** Library names: safe as domain keys and shown verbatim to the model. */
|
|
10
|
+
export declare const LIBRARY_NAME: RegExp;
|
|
11
|
+
/** Chunking defaults: window size and overlap, in characters. */
|
|
12
|
+
export declare const DEFAULT_CHUNK_SIZE = 900;
|
|
13
|
+
export declare const DEFAULT_CHUNK_OVERLAP = 120;
|
|
14
|
+
/** Files larger than this are rejected with a clear error instead of being truncated. */
|
|
15
|
+
export declare const DEFAULT_MAX_FILE_BYTES: number;
|
|
16
|
+
/** Absolute safety bound on one stored chunk (chunkSize must stay below it). */
|
|
17
|
+
export declare const MAX_CHUNK_CHARS = 4000;
|
|
18
|
+
/** Hash-embedding dimensionality used by the zero-download local embedder. */
|
|
19
|
+
export declare const DEFAULT_EMBEDDING_DIMS = 256;
|
|
20
|
+
/** Embedder subprocess budget. */
|
|
21
|
+
export declare const DEFAULT_EMBEDDING_TIMEOUT_MS = 30000;
|
|
22
|
+
export declare const DEFAULT_EMBEDDING_GRACE_MS = 1000;
|
|
23
|
+
export declare const DEFAULT_EMBEDDING_MAX_OUTPUT_BYTES: number;
|
|
24
|
+
export declare const DEFAULT_EMBEDDING_MAX_BATCH = 64;
|
|
25
|
+
/** Search defaults. */
|
|
26
|
+
export declare const DEFAULT_SEARCH_TOPK = 8;
|
|
27
|
+
export declare const DEFAULT_SEARCH_HYBRID_WEIGHT = 0.6;
|
|
28
|
+
export declare const DEFAULT_SEARCH_MIN_RELEVANCE = 0.15;
|
|
29
|
+
export declare const DEFAULT_SEARCH_DIVERSITY_LAMBDA = 0.5;
|
|
30
|
+
export declare const DEFAULT_SEARCH_MAX_RESULT_CHARS = 16000;
|
|
31
|
+
/** Lost-in-the-middle avoidance: strongest results pinned to head and tail. */
|
|
32
|
+
export declare const DEFAULT_LOST_MIDDLE_HEAD = 1;
|
|
33
|
+
export declare const DEFAULT_LOST_MIDDLE_TAIL = 1;
|
|
34
|
+
/** Injection defaults. */
|
|
35
|
+
export declare const DEFAULT_INJECT_MAX_CHARS = 12000;
|
|
36
|
+
/** Citation defaults. */
|
|
37
|
+
export declare const DEFAULT_CITATION_WINDOW_CHARS = 150;
|
|
38
|
+
export declare const DEFAULT_CITATION_MIN_SCORE = 40;
|
|
39
|
+
export declare const DEFAULT_CITATION_MIN_SEMANTIC = 0.1;
|
|
40
|
+
/** Purge verification defaults. */
|
|
41
|
+
export declare const DEFAULT_PURGE_SIGNATURE_LENGTH = 4;
|
|
42
|
+
export declare const DEFAULT_PURGE_MAX_PROBES = 24;
|
|
43
|
+
/** Diagnose defaults. */
|
|
44
|
+
export declare const DEFAULT_DIAGNOSE_MAX_DUPLICATE_PAIRS = 24;
|
|
45
|
+
export declare const DEFAULT_DIAGNOSE_SAMPLE_CAP = 200;
|
|
46
|
+
export declare const DEFAULT_DIAGNOSE_POSITION_BINS = 5;
|
|
47
|
+
/** Embedder selection: built-in hash embedding, or an external command through `ctx.subprocess`. */
|
|
48
|
+
export interface EmbeddingConfig {
|
|
49
|
+
/** Hash-embedding dimensionality (built-in embedder only). Must be ≥ 8. */
|
|
50
|
+
dims?: number;
|
|
51
|
+
/**
|
|
52
|
+
* Optional external embedder command line (space-separated, no shell
|
|
53
|
+
* interpretation; executed through `ctx.subprocess`). The command must read
|
|
54
|
+
* one JSON object per text line on stdin (`{"index": <n>, "text": "..."}`)
|
|
55
|
+
* and write one JSON object per line on stdout
|
|
56
|
+
* (`{"index": <n>, "vector": [<number>, ...]}`). Indices must cover every
|
|
57
|
+
* input line exactly once. When unset, the built-in deterministic hash
|
|
58
|
+
* embedder runs with zero downloads.
|
|
59
|
+
*/
|
|
60
|
+
command?: string;
|
|
61
|
+
/** Cooperative timeout for one embedder invocation (ms). */
|
|
62
|
+
timeoutMs?: number;
|
|
63
|
+
/** Terminate-escalation grace handed to the subprocess seam (ms). */
|
|
64
|
+
graceMs?: number;
|
|
65
|
+
/** Max bytes of one embedder stdout before the batch fails. */
|
|
66
|
+
maxOutputBytes?: number;
|
|
67
|
+
/** Max texts embedded per subprocess invocation; larger batches are split. */
|
|
68
|
+
maxBatchItems?: number;
|
|
69
|
+
}
|
|
70
|
+
/** Search pipeline tuning (hybrid scoring → diversity re-rank → relevance filter → position strategy). */
|
|
71
|
+
export interface SearchConfig {
|
|
72
|
+
/** How many chunks one search returns after the full pipeline. */
|
|
73
|
+
topK?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Hybrid weight: 0 = keyword-only, 1 = semantic-only. Keyword and semantic
|
|
76
|
+
* scores are each normalized to 0..1 before combining.
|
|
77
|
+
*/
|
|
78
|
+
hybridWeight?: number;
|
|
79
|
+
/** Chunks scoring below this relevance threshold are filtered out (fail closed to fewer, better results). */
|
|
80
|
+
minRelevance?: number;
|
|
81
|
+
/** Diversity re-rank weight: 1 = pure relevance order, 0 = pure diversity. */
|
|
82
|
+
diversityLambda?: number;
|
|
83
|
+
/** Strongest chunks pinned to the head of the context (lost-in-the-middle avoidance). */
|
|
84
|
+
lostMiddleHead?: number;
|
|
85
|
+
/** Strongest chunks pinned to the tail of the context. */
|
|
86
|
+
lostMiddleTail?: number;
|
|
87
|
+
/** Character budget for the model-facing result page; longer pages are truncated with a marker. */
|
|
88
|
+
maxResultChars?: number;
|
|
89
|
+
}
|
|
90
|
+
/** On-demand `agent.inject()` behavior of `library_search`. */
|
|
91
|
+
export interface InjectionConfig {
|
|
92
|
+
/** Whether `library_search` with `inject: true` injects into the calling agent. */
|
|
93
|
+
enabled?: boolean;
|
|
94
|
+
/** Character budget of one injected context message. */
|
|
95
|
+
maxChars?: number;
|
|
96
|
+
}
|
|
97
|
+
/** Citation checking thresholds for `library_cite_check`. */
|
|
98
|
+
export interface CitationConfig {
|
|
99
|
+
/** Context window (chars) around each citation marker that forms the claim. */
|
|
100
|
+
windowChars?: number;
|
|
101
|
+
/** Minimum fuzzy token-match score (0-100) for a citation to be valid. */
|
|
102
|
+
minScore?: number;
|
|
103
|
+
/** Minimum semantic similarity (hash embedding, 0-1) for a citation to be valid. */
|
|
104
|
+
minSemantic?: number;
|
|
105
|
+
}
|
|
106
|
+
/** Purge verification (RAG-Purge-Verify port) after `library_remove`. */
|
|
107
|
+
export interface PurgeConfig {
|
|
108
|
+
/** Token n-gram length of the removed-content signatures scanned for residue. */
|
|
109
|
+
signatureLength?: number;
|
|
110
|
+
/** How many signatures are probed per removal; longer documents sample deterministically. */
|
|
111
|
+
maxProbes?: number;
|
|
112
|
+
}
|
|
113
|
+
/** `library_diagnose` budget caps. */
|
|
114
|
+
export interface DiagnoseConfig {
|
|
115
|
+
/** Cap on reported near-duplicate chunk pairs. */
|
|
116
|
+
maxDuplicatePairs?: number;
|
|
117
|
+
/** Chunks sampled into the duplicate scan (the pair check is quadratic). */
|
|
118
|
+
sampleCap?: number;
|
|
119
|
+
/** How many position bins the lost-in-the-middle report uses. */
|
|
120
|
+
positionBins?: number;
|
|
121
|
+
}
|
|
122
|
+
/** Raw plugin config — every field optional; {@link Config} supplies the defaults. */
|
|
123
|
+
export interface Config {
|
|
124
|
+
/** Sliding-window chunk size in characters. */
|
|
125
|
+
chunkSize?: number;
|
|
126
|
+
/** Sliding-window overlap in characters; must be smaller than {@link Config.chunkSize}. */
|
|
127
|
+
chunkOverlap?: number;
|
|
128
|
+
/** Max bytes of one imported file; larger files fail with a clear error. */
|
|
129
|
+
maxFileBytes?: number;
|
|
130
|
+
/** Embedding options (see {@link EmbeddingConfig}). */
|
|
131
|
+
embedding?: EmbeddingConfig;
|
|
132
|
+
/** Search pipeline options (see {@link SearchConfig}). */
|
|
133
|
+
search?: SearchConfig;
|
|
134
|
+
/** Injection options (see {@link InjectionConfig}). */
|
|
135
|
+
injection?: InjectionConfig;
|
|
136
|
+
/** Citation options (see {@link CitationConfig}). */
|
|
137
|
+
citation?: CitationConfig;
|
|
138
|
+
/** Purge options (see {@link PurgeConfig}). */
|
|
139
|
+
purge?: PurgeConfig;
|
|
140
|
+
/** Diagnose options (see {@link DiagnoseConfig}). */
|
|
141
|
+
diagnose?: DiagnoseConfig;
|
|
142
|
+
}
|
|
143
|
+
/** Resolved embedding config: defaults applied, `command` explicitly optional. */
|
|
144
|
+
export interface ResolvedEmbeddingConfig {
|
|
145
|
+
readonly dims: number;
|
|
146
|
+
readonly command: string | undefined;
|
|
147
|
+
readonly timeoutMs: number;
|
|
148
|
+
readonly graceMs: number;
|
|
149
|
+
readonly maxOutputBytes: number;
|
|
150
|
+
readonly maxBatchItems: number;
|
|
151
|
+
}
|
|
152
|
+
/** Resolved search config after defaulting. */
|
|
153
|
+
export interface ResolvedSearchConfig {
|
|
154
|
+
readonly topK: number;
|
|
155
|
+
readonly hybridWeight: number;
|
|
156
|
+
readonly minRelevance: number;
|
|
157
|
+
readonly diversityLambda: number;
|
|
158
|
+
readonly lostMiddleHead: number;
|
|
159
|
+
readonly lostMiddleTail: number;
|
|
160
|
+
readonly maxResultChars: number;
|
|
161
|
+
}
|
|
162
|
+
/** Resolved injection config after defaulting. */
|
|
163
|
+
export interface ResolvedInjectionConfig {
|
|
164
|
+
readonly enabled: boolean;
|
|
165
|
+
readonly maxChars: number;
|
|
166
|
+
}
|
|
167
|
+
/** Resolved citation config after defaulting. */
|
|
168
|
+
export interface ResolvedCitationConfig {
|
|
169
|
+
readonly windowChars: number;
|
|
170
|
+
readonly minScore: number;
|
|
171
|
+
readonly minSemantic: number;
|
|
172
|
+
}
|
|
173
|
+
/** Resolved purge config after defaulting. */
|
|
174
|
+
export interface ResolvedPurgeConfig {
|
|
175
|
+
readonly signatureLength: number;
|
|
176
|
+
readonly maxProbes: number;
|
|
177
|
+
}
|
|
178
|
+
/** Resolved diagnose config after defaulting. */
|
|
179
|
+
export interface ResolvedDiagnoseConfig {
|
|
180
|
+
readonly maxDuplicatePairs: number;
|
|
181
|
+
readonly sampleCap: number;
|
|
182
|
+
readonly positionBins: number;
|
|
183
|
+
}
|
|
184
|
+
/** Config after {@link resolveConfig}: every optional field has its explicit default. */
|
|
185
|
+
export interface ResolvedConfig {
|
|
186
|
+
readonly chunkSize: number;
|
|
187
|
+
readonly chunkOverlap: number;
|
|
188
|
+
readonly maxFileBytes: number;
|
|
189
|
+
readonly embedding: ResolvedEmbeddingConfig;
|
|
190
|
+
readonly search: ResolvedSearchConfig;
|
|
191
|
+
readonly injection: ResolvedInjectionConfig;
|
|
192
|
+
readonly citation: ResolvedCitationConfig;
|
|
193
|
+
readonly purge: ResolvedPurgeConfig;
|
|
194
|
+
readonly diagnose: ResolvedDiagnoseConfig;
|
|
195
|
+
}
|
|
196
|
+
/** Schemastery schema: the loader validates and fills defaults before `apply`. */
|
|
197
|
+
export declare const Config: z<Config>;
|
|
198
|
+
/**
|
|
199
|
+
* Validate raw values and compile the resolved config. Defaults are applied
|
|
200
|
+
* HERE — the explicit resolution step — so a partially-specified config from
|
|
201
|
+
* a direct `ctx.plugin` mount behaves like the loader-filled one.
|
|
202
|
+
* @param config - raw (possibly partial) plugin config.
|
|
203
|
+
* @returns the fully resolved config.
|
|
204
|
+
* @throws TypeError on non-positive caps, an oversized chunk window, or an
|
|
205
|
+
* embedder command without an executable part (misconfiguration fails loud).
|
|
206
|
+
*/
|
|
207
|
+
export declare function resolveConfig(config?: Config): ResolvedConfig;
|
|
208
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,CAAC,MAAM,0BAA0B,CAAA;AAExC,0EAA0E;AAC1E,eAAO,MAAM,YAAY,QAA8B,CAAA;AAEvD,iEAAiE;AACjE,eAAO,MAAM,kBAAkB,MAAM,CAAA;AACrC,eAAO,MAAM,qBAAqB,MAAM,CAAA;AACxC,yFAAyF;AACzF,eAAO,MAAM,sBAAsB,QAAkB,CAAA;AAC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
|
+
* Embedding pipeline: the built-in deterministic hash embedder (zero
|
|
3
|
+
* downloads — word tokens plus character tri-grams hashed into a signed
|
|
4
|
+
* bucket vector, L2-normalized), the optional external embedder command
|
|
5
|
+
* protocol over `ctx.subprocess`, and the cosine/dot utilities the scoring
|
|
6
|
+
* modules share.
|
|
7
|
+
* @module dsh-library/embedding
|
|
8
|
+
*/
|
|
9
|
+
import type { SubprocessRuntime } from '@deepseek-ai/dsh-subprocess';
|
|
10
|
+
/**
|
|
11
|
+
* Embed one text with the built-in hash embedder. Each word token and each
|
|
12
|
+
* character tri-gram hashes (FNV-1a) to a signed bucket: the index is the
|
|
13
|
+
* hash modulo `dims` and the sign is the hash's high bit. The result is
|
|
14
|
+
* L2-normalized so cosine similarity is a dot product.
|
|
15
|
+
* @param text - input text.
|
|
16
|
+
* @param dims - vector dimensionality (≥ 1).
|
|
17
|
+
* @returns the normalized embedding as a plain number array.
|
|
18
|
+
*/
|
|
19
|
+
export declare function embedHash(text: string, dims: number): number[];
|
|
20
|
+
/**
|
|
21
|
+
* Cosine similarity of two equal-length vectors (already-normalized vectors
|
|
22
|
+
* make this a plain dot product, but the function normalizes anyway).
|
|
23
|
+
* @param a - first vector.
|
|
24
|
+
* @param b - second vector.
|
|
25
|
+
* @returns similarity within 0..1 for non-negative-input vectors.
|
|
26
|
+
*/
|
|
27
|
+
export declare function cosine(a: readonly number[], b: readonly number[]): number;
|
|
28
|
+
/**
|
|
29
|
+
* Run the configured external embedder command once over a text batch.
|
|
30
|
+
* The protocol is JSON lines both ways: each stdin line is
|
|
31
|
+
* `{"index": <n>, "text": "..."}` and each stdout line is
|
|
32
|
+
* `{"index": <n>, "vector": [<number>, ...]}`. Every requested index must be
|
|
33
|
+
* answered exactly once or the call fails closed (misconfiguration must
|
|
34
|
+
* surface, not silently degrade retrieval).
|
|
35
|
+
* @param subprocess - the mounted `ctx.subprocess` service.
|
|
36
|
+
* @param argv - the configured command line split into argv (argv[0] = program).
|
|
37
|
+
* @param cwd - working directory for the child.
|
|
38
|
+
* @param texts - the texts to embed, in index order.
|
|
39
|
+
* @param dims - expected vector dimensionality.
|
|
40
|
+
* @param caps - timeout, grace, and output caps.
|
|
41
|
+
* @returns embeddings in the same order as {@link texts}.
|
|
42
|
+
* @throws Error on spawn failure, non-zero exit, or an incomplete/oversized answer.
|
|
43
|
+
*/
|
|
44
|
+
export declare function embedWithCommand(subprocess: SubprocessRuntime, argv: readonly string[], cwd: string, texts: readonly string[], dims: number, caps: {
|
|
45
|
+
timeoutMs: number;
|
|
46
|
+
graceMs: number;
|
|
47
|
+
maxOutputBytes: number;
|
|
48
|
+
}): Promise<number[][]>;
|
|
49
|
+
/**
|
|
50
|
+
* Split a flat command line into argv without shell interpretation.
|
|
51
|
+
* Whitespace separates; quotes are NOT processed (documented: no shell).
|
|
52
|
+
* @param command - the configured command string.
|
|
53
|
+
* @returns the argv vector; an empty string yields an empty vector.
|
|
54
|
+
*/
|
|
55
|
+
export declare function splitCommandLine(command: string): string[];
|
|
56
|
+
//# sourceMappingURL=embedding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../../src/embedding.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAA;AAIpE;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAU9D;AAcD;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,SAAS,MAAM,EAAE,EAAE,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAYzE;AAqBD;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,gBAAgB,CACpC,UAAU,EAAE,iBAAiB,EAC7B,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,CAAA;CAAE,GACnE,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CA0CrB;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,CAE1D"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identity minting for dsh-library records: content-derived document ids,
|
|
3
|
+
* per-chunk ids, and the unique ids that link injected model-visible text
|
|
4
|
+
* back to its `library/inject` session event (model-visible ⟺ logged).
|
|
5
|
+
* @module dsh-library/ids
|
|
6
|
+
*/
|
|
7
|
+
/** Stable document id: first 16 hex chars of the SHA-256 of its content. */
|
|
8
|
+
export declare function documentIdOf(content: string): string;
|
|
9
|
+
/** Chunk id: the owning document id plus the chunk sequence. */
|
|
10
|
+
export declare function chunkIdOf(documentId: string, seq: number): string;
|
|
11
|
+
/** Parse a chunk id back into its document id and sequence; null when malformed. */
|
|
12
|
+
export declare function parseChunkId(chunkId: string): {
|
|
13
|
+
documentId: string;
|
|
14
|
+
seq: number;
|
|
15
|
+
} | null;
|
|
16
|
+
/** One purge-verification run's identity, recorded in the purges table. */
|
|
17
|
+
export declare function purgeId(): string;
|
|
18
|
+
/**
|
|
19
|
+
* One injection's identity: embedded in the injected marker text so the
|
|
20
|
+
* model-visible message is reconstructable from the `library/inject` event.
|
|
21
|
+
*/
|
|
22
|
+
export declare function injectId(): string;
|
|
23
|
+
/** Domain record key of one document inside the `documents` table. */
|
|
24
|
+
export declare function documentKey(library: string, documentId: string): string;
|
|
25
|
+
/** Domain record key of one chunk inside the `chunks` table. */
|
|
26
|
+
export declare function chunkKey(library: string, chunkId: string): string;
|
|
27
|
+
//# sourceMappingURL=ids.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../../src/ids.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,4EAA4E;AAC5E,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,gEAAgE;AAChE,wBAAgB,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAEjE;AAED,oFAAoF;AACpF,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAMxF;AAED,2EAA2E;AAC3E,wBAAgB,OAAO,IAAI,MAAM,CAEhC;AAED;;;GAGG;AACH,wBAAgB,QAAQ,IAAI,MAAM,CAEjC;AAED,sEAAsE;AACtE,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAEvE;AAED,gEAAgE;AAChE,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAEjE"}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `dsh-library` — the local-first knowledge base for DeepSeek Harness:
|
|
3
|
+
* import md/txt documents into a storage-domain index, retrieve with a
|
|
4
|
+
* hybrid semantic+keyword pipeline (diversity re-rank, relevance filter,
|
|
5
|
+
* lost-in-the-middle avoidance), verify citations against retrieved chunks,
|
|
6
|
+
* and diagnose chunk/retrieval quality. Everything model-visible carries a
|
|
7
|
+
* source marker and is reconstructable from the `library/inject` session
|
|
8
|
+
* event; document paths and embeddings never reach the log.
|
|
9
|
+
*
|
|
10
|
+
* Function plugin — no default export (the Loader unwraps
|
|
11
|
+
* `exports.default ?? exports`).
|
|
12
|
+
* @module dsh-library
|
|
13
|
+
*/
|
|
14
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
15
|
+
import { type Domain } from '@deepseek-ai/dsh-storage-domain';
|
|
16
|
+
import { z as zod } from 'zod';
|
|
17
|
+
import type { SubprocessRuntime } from '@deepseek-ai/dsh-subprocess';
|
|
18
|
+
import { Config, type ResolvedConfig } from './config.ts';
|
|
19
|
+
import { chunkSizeHistogram } from './quality/chunk-visual.ts';
|
|
20
|
+
export declare const name = "dsh-library";
|
|
21
|
+
/** Hard services: the durable index lives in the storage domain; tools and the command register here. */
|
|
22
|
+
export declare const inject: string[];
|
|
23
|
+
export { Config, resolveConfig, LIBRARY_NAME } from './config.ts';
|
|
24
|
+
export type { ResolvedConfig, EmbeddingConfig, SearchConfig, InjectionConfig, CitationConfig, PurgeConfig, DiagnoseConfig } from './config.ts';
|
|
25
|
+
export { embedHash, cosine, splitCommandLine } from './embedding.ts';
|
|
26
|
+
export { CHUNK_SIZE_BUCKETS, chunkSizeHistogram, chunkText, chunkHash, findDuplicateChunks } from './quality/chunk-visual.ts';
|
|
27
|
+
export type { Chunk, ChunkSizeHistogram, ChunkDiagnostics } from './quality/chunk-visual.ts';
|
|
28
|
+
export { checkDiversity, maximalMarginalRelevance } from './quality/diversity.ts';
|
|
29
|
+
export type { DiversityReport, RerankCandidate, DuplicatePair } from './quality/diversity.ts';
|
|
30
|
+
export { successByPosition, makeProbe, insertProbe, buildProbePrompt, avoidLostMiddle, positionBins, middlePenalty, PROBE_PREFIX } from './quality/lost-middle.ts';
|
|
31
|
+
export type { ProbeResult, PositionStats, RankedItem, PositionBin } from './quality/lost-middle.ts';
|
|
32
|
+
export { scoreRelevance, scoreBatch, filterDocuments } from './quality/relevance.ts';
|
|
33
|
+
export type { ScoredDocument } from './quality/relevance.ts';
|
|
34
|
+
export { validateQaPair, selectFewShot, formatFewShotPrompt } from './quality/few-shot.ts';
|
|
35
|
+
export type { QaExample, SelectedExample, FewShotOptions } from './quality/few-shot.ts';
|
|
36
|
+
export { extractCitations, extractContext, verifyReferences } from './quality/reference.ts';
|
|
37
|
+
export type { CitationMarker, ReferenceDocument, ReferenceVerdict, ReferenceReport, ReferenceOptions } from './quality/reference.ts';
|
|
38
|
+
export { extractCitationNumbers, extractSentenceWithCitation, fuzzyPartialRatio, validateCitation, validateCitations } from './quality/citation.ts';
|
|
39
|
+
export type { CitationMatch, ValidationResult } from './quality/citation.ts';
|
|
40
|
+
export { verifyPurge, sampleSignatures } from './quality/purge.ts';
|
|
41
|
+
export type { RemainingChunk, PurgeProbeResult, PurgeReport, PurgeOptions } from './quality/purge.ts';
|
|
42
|
+
/** The dsh-library storage-domain declaration (unit names allow `[a-z][a-z0-9_]*` only). */
|
|
43
|
+
export declare const libraryDomainSpec: {
|
|
44
|
+
name: string;
|
|
45
|
+
version: number;
|
|
46
|
+
tables: {
|
|
47
|
+
documents: {
|
|
48
|
+
valueSchema: zod.ZodObject<{
|
|
49
|
+
library: zod.ZodString;
|
|
50
|
+
documentId: zod.ZodString;
|
|
51
|
+
name: zod.ZodString;
|
|
52
|
+
contentHash: zod.ZodString;
|
|
53
|
+
chunks: zod.ZodArray<zod.ZodString>;
|
|
54
|
+
chars: zod.ZodNumber;
|
|
55
|
+
createdAt: zod.ZodNumber;
|
|
56
|
+
}, zod.core.$strip>;
|
|
57
|
+
};
|
|
58
|
+
chunks: {
|
|
59
|
+
valueSchema: zod.ZodObject<{
|
|
60
|
+
library: zod.ZodString;
|
|
61
|
+
chunkId: zod.ZodString;
|
|
62
|
+
documentId: zod.ZodString;
|
|
63
|
+
seq: zod.ZodNumber;
|
|
64
|
+
text: zod.ZodString;
|
|
65
|
+
embedding: zod.ZodArray<zod.ZodNumber>;
|
|
66
|
+
}, zod.core.$strip>;
|
|
67
|
+
};
|
|
68
|
+
purges: {
|
|
69
|
+
valueSchema: zod.ZodObject<{
|
|
70
|
+
purgeId: zod.ZodString;
|
|
71
|
+
library: zod.ZodString;
|
|
72
|
+
documentId: zod.ZodString;
|
|
73
|
+
passed: zod.ZodBoolean;
|
|
74
|
+
totalFound: zod.ZodNumber;
|
|
75
|
+
at: zod.ZodNumber;
|
|
76
|
+
}, zod.core.$strip>;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
/** The session audit events (declaration merging; model-visible ⟺ logged). */
|
|
81
|
+
declare module '@deepseek-ai/dsh-session/types' {
|
|
82
|
+
interface SessionEventMap {
|
|
83
|
+
/** One `library_search` injection: id links the injected marker text back to this event. */
|
|
84
|
+
'library/inject': {
|
|
85
|
+
injectId: string;
|
|
86
|
+
library: string;
|
|
87
|
+
query: string;
|
|
88
|
+
chunks: string[];
|
|
89
|
+
chars: number;
|
|
90
|
+
};
|
|
91
|
+
/** One `library_remove` purge verification outcome. */
|
|
92
|
+
'library/purge': {
|
|
93
|
+
purgeId: string;
|
|
94
|
+
library: string;
|
|
95
|
+
documentId: string;
|
|
96
|
+
passed: boolean;
|
|
97
|
+
totalFound: number;
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** Optional seams read at call time (fail closed when absent). */
|
|
102
|
+
interface StoreDeps {
|
|
103
|
+
readonly subprocess: SubprocessRuntime | undefined;
|
|
104
|
+
}
|
|
105
|
+
/** One `library_add` outcome. */
|
|
106
|
+
export interface LibraryAddResult {
|
|
107
|
+
readonly library: string;
|
|
108
|
+
readonly documentId: string;
|
|
109
|
+
readonly name: string;
|
|
110
|
+
readonly chunks: number;
|
|
111
|
+
readonly chars: number;
|
|
112
|
+
}
|
|
113
|
+
/** One `library_remove` outcome, including the purge verification. */
|
|
114
|
+
export interface LibraryRemoveResult {
|
|
115
|
+
readonly library: string;
|
|
116
|
+
readonly documentId: string;
|
|
117
|
+
readonly removedChunks: number;
|
|
118
|
+
readonly purgePassed: boolean;
|
|
119
|
+
readonly purgeTotalFound: number;
|
|
120
|
+
}
|
|
121
|
+
/** One `library_list` entry (no text). */
|
|
122
|
+
export interface LibraryListEntry {
|
|
123
|
+
readonly library: string;
|
|
124
|
+
readonly documentId: string;
|
|
125
|
+
readonly name: string;
|
|
126
|
+
readonly chunks: number;
|
|
127
|
+
readonly chars: number;
|
|
128
|
+
readonly createdAt: number;
|
|
129
|
+
}
|
|
130
|
+
/** One `library_search` hit. */
|
|
131
|
+
export interface LibrarySearchHit {
|
|
132
|
+
readonly chunkId: string;
|
|
133
|
+
readonly documentId: string;
|
|
134
|
+
readonly seq: number;
|
|
135
|
+
/** Snippet of the chunk text (sanitized, capped). */
|
|
136
|
+
readonly snippet: string;
|
|
137
|
+
/** Final combined score (post re-rank order is positional, not this value). */
|
|
138
|
+
readonly score: number;
|
|
139
|
+
}
|
|
140
|
+
/** The `library_search` canonical result. */
|
|
141
|
+
export interface LibrarySearchResult {
|
|
142
|
+
readonly library: string;
|
|
143
|
+
readonly query: string;
|
|
144
|
+
readonly results: LibrarySearchHit[];
|
|
145
|
+
/** True when the result page was injected into the calling agent. */
|
|
146
|
+
readonly injected: boolean;
|
|
147
|
+
}
|
|
148
|
+
/** One `library_cite_check` citation verdict. */
|
|
149
|
+
export interface LibraryCiteVerdict {
|
|
150
|
+
readonly citation: string;
|
|
151
|
+
readonly sourceDocumentId: string;
|
|
152
|
+
readonly sourceSeq: number;
|
|
153
|
+
readonly valid: boolean;
|
|
154
|
+
readonly fuzzyScore: number;
|
|
155
|
+
readonly semanticSimilarity: number;
|
|
156
|
+
readonly reason: string;
|
|
157
|
+
}
|
|
158
|
+
/** The `library_cite_check` report. */
|
|
159
|
+
export interface LibraryCiteReport {
|
|
160
|
+
readonly library: string;
|
|
161
|
+
readonly query: string;
|
|
162
|
+
readonly total: number;
|
|
163
|
+
readonly valid: number;
|
|
164
|
+
readonly details: LibraryCiteVerdict[];
|
|
165
|
+
}
|
|
166
|
+
/** The `library_diagnose` report. */
|
|
167
|
+
export interface LibraryDiagnoseReport {
|
|
168
|
+
readonly library: string;
|
|
169
|
+
readonly documents: number;
|
|
170
|
+
readonly chunks: number;
|
|
171
|
+
readonly histogram: ReturnType<typeof chunkSizeHistogram>;
|
|
172
|
+
readonly duplicatePairs: number;
|
|
173
|
+
readonly selfRetrieval: {
|
|
174
|
+
probes: number;
|
|
175
|
+
topKHit: number;
|
|
176
|
+
};
|
|
177
|
+
readonly middlePenalty: number;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The index runtime over one open storage domain: add/remove/list/search/
|
|
181
|
+
* cite/diagnose. All record shapes are validated by the domain at the
|
|
182
|
+
* durable boundary; this class never logs text or embeddings.
|
|
183
|
+
*/
|
|
184
|
+
export declare class LibraryStore {
|
|
185
|
+
private readonly config;
|
|
186
|
+
private readonly deps;
|
|
187
|
+
private readonly documents;
|
|
188
|
+
private readonly chunks;
|
|
189
|
+
private readonly purges;
|
|
190
|
+
/**
|
|
191
|
+
* @param domain - the opened dsh-library domain.
|
|
192
|
+
* @param config - resolved plugin config.
|
|
193
|
+
* @param deps - optional subprocess seam for the external embedder.
|
|
194
|
+
*/
|
|
195
|
+
constructor(domain: Domain<typeof libraryDomainSpec>, config: ResolvedConfig, deps: StoreDeps);
|
|
196
|
+
/** Embed a text batch with the configured embedder (external command when set). */
|
|
197
|
+
private embed;
|
|
198
|
+
/**
|
|
199
|
+
* Import one document: chunk it, embed the chunks, and store the records.
|
|
200
|
+
* @param library - target library name.
|
|
201
|
+
* @param docName - display name.
|
|
202
|
+
* @param content - full document text.
|
|
203
|
+
* @returns the add summary.
|
|
204
|
+
*/
|
|
205
|
+
add(library: string, docName: string, content: string): Promise<LibraryAddResult>;
|
|
206
|
+
/**
|
|
207
|
+
* Remove one document and verify the purge: the removed content must leave
|
|
208
|
+
* no residue in the remaining chunks of the library.
|
|
209
|
+
* @param library - owning library name.
|
|
210
|
+
* @param documentId - stable document id.
|
|
211
|
+
* @returns the removal summary plus the purge verdict.
|
|
212
|
+
*/
|
|
213
|
+
remove(library: string, documentId: string): Promise<LibraryRemoveResult>;
|
|
214
|
+
/** List documents (metadata only, never text) of one library or every library. */
|
|
215
|
+
list(library?: string): LibraryListEntry[];
|
|
216
|
+
/** All chunk records of one library, in document/seq order. */
|
|
217
|
+
private chunksOf;
|
|
218
|
+
/**
|
|
219
|
+
* The hybrid retrieval pipeline: keyword + semantic scores, diversity
|
|
220
|
+
* re-rank, relevance filter, lost-in-the-middle reorder, character budget.
|
|
221
|
+
* @param library - library to search.
|
|
222
|
+
* @param query - the query text.
|
|
223
|
+
* @param topK - result cap (default from config).
|
|
224
|
+
* @returns ranked hits.
|
|
225
|
+
*/
|
|
226
|
+
search(library: string, query: string, topK?: number): Promise<LibrarySearchHit[]>;
|
|
227
|
+
/** The citation sources for one query: the search pipeline result in order. */
|
|
228
|
+
citeCheck(library: string, query: string, answer: string): Promise<LibraryCiteReport>;
|
|
229
|
+
/** Diagnose one library: chunk stats, duplicates, self-retrieval, middle penalty. */
|
|
230
|
+
diagnose(library: string): Promise<LibraryDiagnoseReport>;
|
|
231
|
+
}
|
|
232
|
+
/** The store service shared by every tool and the command. */
|
|
233
|
+
interface LibraryServices {
|
|
234
|
+
readonly ctx: Context;
|
|
235
|
+
readonly config: ResolvedConfig;
|
|
236
|
+
readonly store: LibraryStore;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Mount the plugin: open the storage domain, build the store, register the
|
|
240
|
+
* tool family and the `/library` command. One effect owns the domain handle;
|
|
241
|
+
* every registration is its own reversible effect.
|
|
242
|
+
* @param ctx - the plugin context.
|
|
243
|
+
* @param config - raw loader config; defaults applied through {@link resolveConfig}.
|
|
244
|
+
*/
|
|
245
|
+
export declare function apply(ctx: Context, config?: Config): Promise<void>;
|
|
246
|
+
/** Every tool definition, in registration order. */
|
|
247
|
+
export declare function allTools(services: LibraryServices): import("@deepseek-ai/dsh-tools").ToolDefinition[];
|
|
248
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,OAAO,EA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AE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAuC3F,qFAAqF;IAC/E,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC;CAoChE;AASD,8DAA8D;AAC9D,UAAU,eAAe;IACvB,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAA;IACrB,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAA;IAC/B,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAA;CAC7B;AAaD;;;;;;GAMG;AACH,wBAAsB,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,GAAE,MAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAqC5E;AAED,oDAAoD;AACpD,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,eAAe,qDASjD"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port of RAG-Chunk-Visualizer (upstream/PerryLink, Apache-2.0): the
|
|
3
|
+
* sliding-window `chunk_text` algorithm plus the structured diagnostics that
|
|
4
|
+
* power `library_diagnose`. Pure functions — no I/O, no model.
|
|
5
|
+
* @module dsh-library/quality/chunk-visual
|
|
6
|
+
*/
|
|
7
|
+
/** One sliding-window chunk, mirroring the upstream `Chunk` dataclass. */
|
|
8
|
+
export interface Chunk {
|
|
9
|
+
readonly text: string;
|
|
10
|
+
readonly startPos: number;
|
|
11
|
+
readonly endPos: number;
|
|
12
|
+
readonly index: number;
|
|
13
|
+
readonly overlapChars: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Slice one text into sliding-window chunks — the exact algorithm of the
|
|
17
|
+
* upstream `chunk_text`. Validation matches upstream: `chunkSize` > 0,
|
|
18
|
+
* `overlap` ≥ 0 and strictly smaller than `chunkSize`.
|
|
19
|
+
* @param text - the text to slice.
|
|
20
|
+
* @param chunkSize - window width in characters.
|
|
21
|
+
* @param overlap - overlap between consecutive windows in characters.
|
|
22
|
+
* @returns the chunk list (empty for empty input).
|
|
23
|
+
* @throws TypeError on invalid parameters.
|
|
24
|
+
*/
|
|
25
|
+
export declare function chunkText(text: string, chunkSize: number, overlap: number): Chunk[];
|
|
26
|
+
/** Size-bucket boundary: powers of two from 256 chars up (diagnose histogram). */
|
|
27
|
+
export declare const CHUNK_SIZE_BUCKETS: readonly number[];
|
|
28
|
+
/** Per-bucket character counts of one chunk list. */
|
|
29
|
+
export interface ChunkSizeHistogram {
|
|
30
|
+
readonly buckets: readonly {
|
|
31
|
+
label: string;
|
|
32
|
+
min: number;
|
|
33
|
+
max: number | null;
|
|
34
|
+
count: number;
|
|
35
|
+
}[];
|
|
36
|
+
readonly minChars: number;
|
|
37
|
+
readonly maxChars: number;
|
|
38
|
+
readonly meanChars: number;
|
|
39
|
+
readonly totalChars: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Bucket one chunk list into size classes and fold the basic size facts.
|
|
43
|
+
* @param chunks - the chunks to analyze.
|
|
44
|
+
* @returns the histogram and size summary.
|
|
45
|
+
*/
|
|
46
|
+
export declare function chunkSizeHistogram(chunks: readonly Chunk[]): ChunkSizeHistogram;
|
|
47
|
+
/** Folded structural statistics of one document's chunk list. */
|
|
48
|
+
export interface ChunkDiagnostics {
|
|
49
|
+
readonly chunkCount: number;
|
|
50
|
+
readonly totalChars: number;
|
|
51
|
+
readonly histogram: ChunkSizeHistogram;
|
|
52
|
+
/** How many of the total chars live in overlapping windows (redundancy signal). */
|
|
53
|
+
readonly overlapChars: number;
|
|
54
|
+
/** overlapChars as a percentage of totalChars. */
|
|
55
|
+
readonly overlapPercent: number;
|
|
56
|
+
/** True when the last chunk is shorter than the others (a normal, expected tail). */
|
|
57
|
+
readonly truncatedTail: boolean;
|
|
58
|
+
/** Maximum single-chunk char count. */
|
|
59
|
+
readonly maxChunkChars: number;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Fold the structural diagnostics of one chunk list — the visualization
|
|
63
|
+
* module's numbers without any rendering.
|
|
64
|
+
* @param chunks - the chunks to analyze.
|
|
65
|
+
* @returns the folded diagnostics.
|
|
66
|
+
*/
|
|
67
|
+
export declare function diagnoseChunks(chunks: readonly Chunk[]): ChunkDiagnostics;
|
|
68
|
+
/**
|
|
69
|
+
* Stable per-chunk content hash (FNV-1a) — the duplicate-detection key.
|
|
70
|
+
* @param chunk - the chunk to hash.
|
|
71
|
+
* @returns the hex hash.
|
|
72
|
+
*/
|
|
73
|
+
export declare function chunkHash(chunk: Chunk): string;
|
|
74
|
+
/** One detected near-duplicate pair. */
|
|
75
|
+
export interface DuplicatePair {
|
|
76
|
+
readonly indexA: number;
|
|
77
|
+
readonly indexB: number;
|
|
78
|
+
readonly similarity: number;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Find exact and near-duplicate chunk pairs by cosine similarity over the
|
|
82
|
+
* pair's shared token vocabulary (the diversity module's measure). The scan
|
|
83
|
+
* is quadratic, so callers bound the input.
|
|
84
|
+
* @param chunks - the chunks to scan.
|
|
85
|
+
* @param threshold - similarity above which a pair counts as duplicate.
|
|
86
|
+
* @param maxPairs - cap on reported pairs (the scan still computes the count).
|
|
87
|
+
* @returns reported pairs and the duplicate count.
|
|
88
|
+
*/
|
|
89
|
+
export declare function findDuplicateChunks(chunks: readonly Chunk[], threshold: number, maxPairs: number): {
|
|
90
|
+
pairs: DuplicatePair[];
|
|
91
|
+
duplicateCount: number;
|
|
92
|
+
};
|
|
93
|
+
//# sourceMappingURL=chunk-visual.d.ts.map
|