@docs-sdk-dev/reader 0.0.0-stage → 0.4.0-next.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 +97 -2
- package/dist/annotation-projection.d.ts +35 -0
- package/dist/annotation-projection.js +78 -0
- package/dist/annotation-projection.js.map +1 -0
- package/dist/artifacts.d.ts +19 -0
- package/dist/artifacts.js +33 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/ask.d.ts +41 -0
- package/dist/ask.js +89 -0
- package/dist/ask.js.map +1 -0
- package/dist/collection-view.d.ts +190 -0
- package/dist/collection-view.js +170 -0
- package/dist/collection-view.js.map +1 -0
- package/dist/composition.d.ts +199 -0
- package/dist/composition.js +473 -0
- package/dist/composition.js.map +1 -0
- package/dist/condition.d.ts +17 -0
- package/dist/condition.js +61 -0
- package/dist/condition.js.map +1 -0
- package/dist/content-schema.d.ts +182 -0
- package/dist/content-schema.js +288 -0
- package/dist/content-schema.js.map +1 -0
- package/dist/context-search.d.ts +168 -0
- package/dist/context-search.js +73 -0
- package/dist/context-search.js.map +1 -0
- package/dist/context-store.d.ts +93 -0
- package/dist/context-store.js +438 -0
- package/dist/context-store.js.map +1 -0
- package/dist/dimension-runtime.d.ts +203 -0
- package/dist/dimension-runtime.js +737 -0
- package/dist/dimension-runtime.js.map +1 -0
- package/dist/dimensions.d.ts +84 -0
- package/dist/dimensions.js +136 -0
- package/dist/dimensions.js.map +1 -0
- package/dist/directory-navigation.d.ts +60 -0
- package/dist/directory-navigation.js +41 -0
- package/dist/directory-navigation.js.map +1 -0
- package/dist/directory-sections.d.ts +9 -0
- package/dist/directory-sections.js +76 -0
- package/dist/directory-sections.js.map +1 -0
- package/dist/document-reader.d.ts +1017 -0
- package/dist/document-reader.js +396 -0
- package/dist/document-reader.js.map +1 -0
- package/dist/document-selection.d.ts +412 -0
- package/dist/document-selection.js +45 -0
- package/dist/document-selection.js.map +1 -0
- package/dist/evidence.d.ts +102 -0
- package/dist/evidence.js +104 -0
- package/dist/evidence.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +23 -0
- package/dist/model.d.ts +89 -0
- package/dist/model.js +31 -0
- package/dist/model.js.map +1 -0
- package/dist/module.d.ts +657 -0
- package/dist/module.js +199 -0
- package/dist/module.js.map +1 -0
- package/dist/navigation-view.d.ts +33 -0
- package/dist/navigation-view.js +68 -0
- package/dist/navigation-view.js.map +1 -0
- package/dist/node.d.ts +2004 -0
- package/dist/node.js +210 -0
- package/dist/node.js.map +1 -0
- package/dist/open-docs.d.ts +1396 -0
- package/dist/open-docs.js +198 -0
- package/dist/open-docs.js.map +1 -0
- package/dist/outline.d.ts +97 -0
- package/dist/outline.js +171 -0
- package/dist/outline.js.map +1 -0
- package/dist/persistence.d.ts +82 -0
- package/dist/persistence.js +377 -0
- package/dist/persistence.js.map +1 -0
- package/dist/projection-catalog.d.ts +23 -0
- package/dist/projection-catalog.js +45 -0
- package/dist/projection-catalog.js.map +1 -0
- package/dist/publication.d.ts +59 -0
- package/dist/publication.js +11 -0
- package/dist/publication.js.map +1 -0
- package/dist/reader.d.ts +236 -0
- package/dist/reader.js +400 -0
- package/dist/reader.js.map +1 -0
- package/dist/references.d.ts +54 -0
- package/dist/references.js +70 -0
- package/dist/references.js.map +1 -0
- package/dist/retrieval.d.ts +82 -0
- package/dist/retrieval.js +135 -0
- package/dist/retrieval.js.map +1 -0
- package/dist/search-view.d.ts +11 -0
- package/dist/search-view.js +30 -0
- package/dist/search-view.js.map +1 -0
- package/dist/search.d.ts +95 -0
- package/dist/search.js +234 -0
- package/dist/search.js.map +1 -0
- package/dist/semantic.d.ts +177 -0
- package/dist/semantic.js +104 -0
- package/dist/semantic.js.map +1 -0
- package/dist/skills.d.ts +21 -0
- package/dist/skills.js +42 -0
- package/dist/skills.js.map +1 -0
- package/docs/index.md +10 -0
- package/docs/sdk/composition.md +72 -0
- package/docs/sdk/reader-storage.md +70 -0
- package/docs/sdk/reader.md +99 -0
- package/docs/zh-CN/sdk/composition.md +72 -0
- package/docs/zh-CN/sdk/reader-storage.md +70 -0
- package/docs/zh-CN/sdk/reader.md +99 -0
- package/package.json +69 -4
package/dist/reader.d.ts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
export * from "./document-reader.js";
|
|
2
|
+
import { type PublicationHost } from "./document-reader.js";
|
|
3
|
+
import { type Provenance } from "./model.js";
|
|
4
|
+
export type { Resolution } from "./semantic.js";
|
|
5
|
+
import { type Resolution, type Context, type EntityVariant, type ExternalReference, type Relation, type RelationType, type SemanticEntity, type SemanticIndex, type SemanticPointer, type SemanticShard } from "./semantic.js";
|
|
6
|
+
export type { ExternalReference } from "./semantic.js";
|
|
7
|
+
export declare function createReferenceReader(publications: readonly {
|
|
8
|
+
project: string;
|
|
9
|
+
inventoryRevision: string;
|
|
10
|
+
host: PublicationHost;
|
|
11
|
+
}[]): {
|
|
12
|
+
resolve: (reference: ExternalReference) => Promise<{
|
|
13
|
+
status: "unknown-project";
|
|
14
|
+
project: string;
|
|
15
|
+
inventoryRevision?: undefined;
|
|
16
|
+
reader?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
status: "unknown-publication";
|
|
19
|
+
project: string;
|
|
20
|
+
inventoryRevision: string;
|
|
21
|
+
reader?: undefined;
|
|
22
|
+
} | Resolution<EntityVariant<unknown>>>;
|
|
23
|
+
trace: (start: ExternalReference, maxSteps?: number) => Promise<{
|
|
24
|
+
reference: ExternalReference;
|
|
25
|
+
from?: number;
|
|
26
|
+
result: Awaited<ReturnType<(reference: ExternalReference) => Promise<{
|
|
27
|
+
status: "unknown-project";
|
|
28
|
+
project: string;
|
|
29
|
+
inventoryRevision?: undefined;
|
|
30
|
+
reader?: undefined;
|
|
31
|
+
} | {
|
|
32
|
+
status: "unknown-publication";
|
|
33
|
+
project: string;
|
|
34
|
+
inventoryRevision: string;
|
|
35
|
+
reader?: undefined;
|
|
36
|
+
} | Resolution<EntityVariant<unknown>>>>>;
|
|
37
|
+
}[]>;
|
|
38
|
+
consumers: (target: ExternalReference, roots: readonly ExternalReference[], maxSteps?: number) => Promise<{
|
|
39
|
+
status: "not-found";
|
|
40
|
+
} | {
|
|
41
|
+
status: "unsupported-context";
|
|
42
|
+
available: Context[];
|
|
43
|
+
} | {
|
|
44
|
+
status: "ambiguous";
|
|
45
|
+
candidates: Context[];
|
|
46
|
+
} | {
|
|
47
|
+
status: "unknown-project";
|
|
48
|
+
project: string;
|
|
49
|
+
inventoryRevision?: undefined;
|
|
50
|
+
reader?: undefined;
|
|
51
|
+
} | {
|
|
52
|
+
status: "unknown-publication";
|
|
53
|
+
project: string;
|
|
54
|
+
inventoryRevision: string;
|
|
55
|
+
reader?: undefined;
|
|
56
|
+
} | {
|
|
57
|
+
consumers: {
|
|
58
|
+
root: ExternalReference;
|
|
59
|
+
path: ExternalReference[];
|
|
60
|
+
}[];
|
|
61
|
+
unchecked: {
|
|
62
|
+
root: ExternalReference;
|
|
63
|
+
reference: ExternalReference;
|
|
64
|
+
reason: string;
|
|
65
|
+
}[];
|
|
66
|
+
status: "exact";
|
|
67
|
+
target: ExternalReference;
|
|
68
|
+
}>;
|
|
69
|
+
review: (target: ExternalReference, candidateInventoryRevision: string, roots: readonly ExternalReference[], maxSteps?: number) => Promise<{
|
|
70
|
+
status: "baseline-unavailable";
|
|
71
|
+
target: ExternalReference;
|
|
72
|
+
candidate: {
|
|
73
|
+
project: string;
|
|
74
|
+
entity: string;
|
|
75
|
+
context?: Context;
|
|
76
|
+
inventoryRevision: string;
|
|
77
|
+
};
|
|
78
|
+
before: {
|
|
79
|
+
status: "not-found";
|
|
80
|
+
} | {
|
|
81
|
+
status: "unsupported-context";
|
|
82
|
+
available: Context[];
|
|
83
|
+
} | {
|
|
84
|
+
status: "ambiguous";
|
|
85
|
+
candidates: Context[];
|
|
86
|
+
} | {
|
|
87
|
+
status: "unknown-project";
|
|
88
|
+
project: string;
|
|
89
|
+
inventoryRevision?: undefined;
|
|
90
|
+
reader?: undefined;
|
|
91
|
+
} | {
|
|
92
|
+
status: "unknown-publication";
|
|
93
|
+
project: string;
|
|
94
|
+
inventoryRevision: string;
|
|
95
|
+
reader?: undefined;
|
|
96
|
+
};
|
|
97
|
+
} | {
|
|
98
|
+
consumers: {
|
|
99
|
+
root: ExternalReference;
|
|
100
|
+
path: ExternalReference[];
|
|
101
|
+
}[];
|
|
102
|
+
unchecked: {
|
|
103
|
+
root: ExternalReference;
|
|
104
|
+
reference: ExternalReference;
|
|
105
|
+
reason: string;
|
|
106
|
+
}[];
|
|
107
|
+
status: "candidate-unavailable";
|
|
108
|
+
target: ExternalReference;
|
|
109
|
+
candidate: {
|
|
110
|
+
project: string;
|
|
111
|
+
entity: string;
|
|
112
|
+
context?: Context;
|
|
113
|
+
inventoryRevision: string;
|
|
114
|
+
};
|
|
115
|
+
before: {
|
|
116
|
+
status: "exact";
|
|
117
|
+
value: EntityVariant<unknown>;
|
|
118
|
+
};
|
|
119
|
+
after: {
|
|
120
|
+
status: "not-found";
|
|
121
|
+
} | {
|
|
122
|
+
status: "unsupported-context";
|
|
123
|
+
available: Context[];
|
|
124
|
+
} | {
|
|
125
|
+
status: "ambiguous";
|
|
126
|
+
candidates: Context[];
|
|
127
|
+
} | {
|
|
128
|
+
status: "unknown-project";
|
|
129
|
+
project: string;
|
|
130
|
+
inventoryRevision?: undefined;
|
|
131
|
+
reader?: undefined;
|
|
132
|
+
} | {
|
|
133
|
+
status: "unknown-publication";
|
|
134
|
+
project: string;
|
|
135
|
+
inventoryRevision: string;
|
|
136
|
+
reader?: undefined;
|
|
137
|
+
};
|
|
138
|
+
} | {
|
|
139
|
+
consumers: {
|
|
140
|
+
root: ExternalReference;
|
|
141
|
+
path: ExternalReference[];
|
|
142
|
+
}[];
|
|
143
|
+
unchecked: {
|
|
144
|
+
root: ExternalReference;
|
|
145
|
+
reference: ExternalReference;
|
|
146
|
+
reason: string;
|
|
147
|
+
}[];
|
|
148
|
+
status: "exact";
|
|
149
|
+
target: ExternalReference;
|
|
150
|
+
candidate: {
|
|
151
|
+
project: string;
|
|
152
|
+
entity: string;
|
|
153
|
+
context?: Context;
|
|
154
|
+
inventoryRevision: string;
|
|
155
|
+
};
|
|
156
|
+
before: {
|
|
157
|
+
status: "exact";
|
|
158
|
+
value: EntityVariant<unknown>;
|
|
159
|
+
};
|
|
160
|
+
after: {
|
|
161
|
+
status: "exact";
|
|
162
|
+
value: EntityVariant<unknown>;
|
|
163
|
+
};
|
|
164
|
+
changes: {
|
|
165
|
+
variant: boolean;
|
|
166
|
+
facts: boolean;
|
|
167
|
+
provenance: boolean;
|
|
168
|
+
externalReferences: boolean;
|
|
169
|
+
};
|
|
170
|
+
}>;
|
|
171
|
+
};
|
|
172
|
+
export interface TraceOptions {
|
|
173
|
+
maxSteps?: number;
|
|
174
|
+
includeEditions?: boolean;
|
|
175
|
+
}
|
|
176
|
+
export interface TraceStep {
|
|
177
|
+
entity: string;
|
|
178
|
+
type: SemanticEntity["type"];
|
|
179
|
+
variant: string;
|
|
180
|
+
/** The relation followed to reach this entity; absent for the starting entity. */
|
|
181
|
+
via?: {
|
|
182
|
+
relation: string;
|
|
183
|
+
type: RelationType;
|
|
184
|
+
from: string;
|
|
185
|
+
fromVariant?: string;
|
|
186
|
+
};
|
|
187
|
+
/** The variant's facts, such as a prop's type and required flag. Absent when the entity has no variant for the context. */
|
|
188
|
+
data?: unknown;
|
|
189
|
+
provenance: Provenance[];
|
|
190
|
+
}
|
|
191
|
+
export type RelationQuery = {
|
|
192
|
+
type?: RelationType;
|
|
193
|
+
direction?: "out" | "in";
|
|
194
|
+
context?: Context;
|
|
195
|
+
};
|
|
196
|
+
/** Read Runtime over a complete in-memory semantic index. It needs no build toolchain, file system or DOM. */
|
|
197
|
+
export declare function createSemanticReader(index: SemanticIndex): {
|
|
198
|
+
entity: (id: string) => SemanticEntity | null;
|
|
199
|
+
entities: (filter?: {
|
|
200
|
+
type?: string;
|
|
201
|
+
}) => SemanticEntity<unknown>[];
|
|
202
|
+
resolve: (id: string, context?: Context) => Resolution<EntityVariant>;
|
|
203
|
+
relations: (id: string, options?: RelationQuery) => Relation[];
|
|
204
|
+
/**
|
|
205
|
+
* Follow derivedFrom and contains edges from an entity to the facts it is built from, breadth first, and report each
|
|
206
|
+
* step's provenance. Entities with no variant for the context are reported once and not expanded.
|
|
207
|
+
*/
|
|
208
|
+
trace(id: string, context?: Context, options?: TraceOptions): Resolution<TraceStep[]>;
|
|
209
|
+
};
|
|
210
|
+
/** Expand a serialized shard into entities and relations, restoring the IDs and provenance it stores compactly. */
|
|
211
|
+
export declare function decodeSemanticShard(shard: SemanticShard): Pick<SemanticIndex, "entities" | "relations">;
|
|
212
|
+
/**
|
|
213
|
+
* Read Runtime over a sharded build. It loads only the shards that hold the requested entities and, for trace, the
|
|
214
|
+
* shards of the entities it reaches. Each shard holds a subject's entities and every relation touching them.
|
|
215
|
+
*/
|
|
216
|
+
export declare function createSemanticLoader(pointer: SemanticPointer, readText: (path: string) => Promise<string>, options?: {
|
|
217
|
+
digest?: (text: string) => string;
|
|
218
|
+
}): {
|
|
219
|
+
revision: string;
|
|
220
|
+
/** Read one exact published subject, including its entities, ordered variants and touching relations. */
|
|
221
|
+
subject: (subject: string) => Promise<null> | Promise<Pick<SemanticIndex, "entities" | "relations">>;
|
|
222
|
+
/** Subjects whose shards have been read, for cost reporting. */
|
|
223
|
+
loaded: () => string[];
|
|
224
|
+
/** Discover entities in subject then ID order. A prefix limits shard reads; after resumes after an entity ID. */
|
|
225
|
+
entities({ subjectPrefix, type, after, }?: {
|
|
226
|
+
subjectPrefix?: string;
|
|
227
|
+
type?: string;
|
|
228
|
+
after?: string;
|
|
229
|
+
}): AsyncGenerator<SemanticEntity<unknown>, void, unknown>;
|
|
230
|
+
entity(id: string): Promise<SemanticEntity<unknown> | null>;
|
|
231
|
+
resolve(id: string, context?: Context): Promise<Resolution<EntityVariant<unknown>>>;
|
|
232
|
+
relations(id: string, options?: RelationQuery): Promise<Relation[]>;
|
|
233
|
+
trace(id: string, context?: Context, options?: TraceOptions): Promise<Resolution<TraceStep[]>>;
|
|
234
|
+
/** Read every shard, for full scans such as listing all entities of a type. */
|
|
235
|
+
loadAll(): Promise<SemanticIndex>;
|
|
236
|
+
};
|
package/dist/reader.js
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import { createReader, documentKey, factContext, openPinnedReader, projectReadingChoices, readPublicationArtifacts, readingContext, sharedPageContext } from "./document-reader.js";
|
|
2
|
+
import { DocsError } from "./model.js";
|
|
3
|
+
import { resolveDocumentContext } from "./dimension-runtime.js";
|
|
4
|
+
import { canonicalJson, relationId, resolveContext, shardKey, variantId } from "./semantic.js";
|
|
5
|
+
|
|
6
|
+
// src/reader.ts
|
|
7
|
+
function createReferenceReader(publications) {
|
|
8
|
+
async function open(revision, host) {
|
|
9
|
+
const publication = await openPinnedReader(revision, host);
|
|
10
|
+
if (!publication.manifest.semantic) throw new DocsError("DOCS_SEMANTIC_MISSING", "This publication has no semantic index.");
|
|
11
|
+
return createSemanticLoader(publication.manifest.semantic, publication.readText);
|
|
12
|
+
}
|
|
13
|
+
const projects = /* @__PURE__ */ new Map();
|
|
14
|
+
for (const { project, inventoryRevision, host } of publications) {
|
|
15
|
+
const versions = projects.get(project) ?? /* @__PURE__ */ new Map();
|
|
16
|
+
if (!project.trim() || !/^[a-f0-9]{64}$/.test(inventoryRevision) || versions.has(inventoryRevision)) throw new DocsError("DOCS_REFERENCE_INVALID", "Expected a distinct project and SHA-256 publication pair.", { source: project });
|
|
17
|
+
versions.set(inventoryRevision, { host });
|
|
18
|
+
projects.set(project, versions);
|
|
19
|
+
}
|
|
20
|
+
async function lookup({ project, inventoryRevision }) {
|
|
21
|
+
const versions = projects.get(project);
|
|
22
|
+
if (!versions) return {
|
|
23
|
+
status: "unknown-project",
|
|
24
|
+
project
|
|
25
|
+
};
|
|
26
|
+
const entry = versions.get(inventoryRevision);
|
|
27
|
+
if (!entry) return {
|
|
28
|
+
status: "unknown-publication",
|
|
29
|
+
project,
|
|
30
|
+
inventoryRevision
|
|
31
|
+
};
|
|
32
|
+
entry.reader ??= open(inventoryRevision, entry.host).catch((error) => {
|
|
33
|
+
delete entry.reader;
|
|
34
|
+
throw error;
|
|
35
|
+
});
|
|
36
|
+
return {
|
|
37
|
+
status: "opened",
|
|
38
|
+
reader: await entry.reader
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
async function resolve(reference) {
|
|
42
|
+
const opened = await lookup(reference);
|
|
43
|
+
return opened.status === "opened" ? opened.reader.resolve(reference.entity, reference.context) : opened;
|
|
44
|
+
}
|
|
45
|
+
async function trace(start, maxSteps = 64) {
|
|
46
|
+
if (!Number.isSafeInteger(maxSteps) || maxSteps < 1) throw new DocsError("DOCS_TRACE_LIMIT", "Expected a positive traversal bound.");
|
|
47
|
+
const pending = [];
|
|
48
|
+
const seen = /* @__PURE__ */ new Set();
|
|
49
|
+
function add(reference, from) {
|
|
50
|
+
const key = canonicalJson([
|
|
51
|
+
reference.project,
|
|
52
|
+
reference.inventoryRevision,
|
|
53
|
+
reference.entity,
|
|
54
|
+
reference.context ?? {}
|
|
55
|
+
]);
|
|
56
|
+
if (seen.has(key)) return;
|
|
57
|
+
if (pending.length >= maxSteps) throw new DocsError("DOCS_TRACE_LIMIT", `Trace exceeds ${maxSteps} references.`);
|
|
58
|
+
seen.add(key);
|
|
59
|
+
pending.push({
|
|
60
|
+
reference,
|
|
61
|
+
...from !== void 0 && { from }
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
add(start);
|
|
65
|
+
const steps = [];
|
|
66
|
+
for (const [position, step] of pending.entries()) {
|
|
67
|
+
const opened = await lookup(step.reference);
|
|
68
|
+
const context = step.reference.context ?? {};
|
|
69
|
+
const result = opened.status === "opened" ? await opened.reader.resolve(step.reference.entity, context) : opened;
|
|
70
|
+
steps.push({
|
|
71
|
+
...step,
|
|
72
|
+
result
|
|
73
|
+
});
|
|
74
|
+
if (opened.status !== "opened" || result.status !== "exact") continue;
|
|
75
|
+
for (const edge of await opened.reader.relations(step.reference.entity, { context })) if (edge.propagatesImpact || edge.type === "contains" || edge.type === "derivedFrom" || edge.type === "references") add({
|
|
76
|
+
...step.reference,
|
|
77
|
+
entity: edge.target,
|
|
78
|
+
context: edge.targetContext ?? context
|
|
79
|
+
}, position);
|
|
80
|
+
for (const ref of result.value.externalReferences ?? []) add(ref, position);
|
|
81
|
+
}
|
|
82
|
+
return steps;
|
|
83
|
+
}
|
|
84
|
+
async function findConsumers(target, variant, roots, maxSteps) {
|
|
85
|
+
const matches = [];
|
|
86
|
+
const unchecked = [];
|
|
87
|
+
for (const root of roots) {
|
|
88
|
+
const steps = await trace(root, maxSteps);
|
|
89
|
+
let match;
|
|
90
|
+
for (const [index, step] of steps.entries()) if (step.result.status !== "exact") unchecked.push({
|
|
91
|
+
root,
|
|
92
|
+
reference: step.reference,
|
|
93
|
+
reason: step.result.status
|
|
94
|
+
});
|
|
95
|
+
else if (match === void 0 && step.reference.project === target.project && step.reference.inventoryRevision === target.inventoryRevision && step.result.value.variantId === variant) match = index;
|
|
96
|
+
if (match !== void 0) {
|
|
97
|
+
const path = [];
|
|
98
|
+
for (let index = match; index !== void 0; index = steps[index].from) path.push(steps[index].reference);
|
|
99
|
+
matches.push({
|
|
100
|
+
root,
|
|
101
|
+
path: path.reverse()
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
consumers: matches,
|
|
107
|
+
unchecked
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** Check only the supplied consumer roots. A missing registration or edition stays visible in the report. */
|
|
111
|
+
async function consumers(target, roots, maxSteps = 64) {
|
|
112
|
+
const resolved = await resolve(target);
|
|
113
|
+
if (resolved.status !== "exact") return resolved;
|
|
114
|
+
return {
|
|
115
|
+
status: "exact",
|
|
116
|
+
target,
|
|
117
|
+
...await findConsumers(target, resolved.value.variantId, roots, maxSteps)
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/** Review a candidate publication in the same project and context. Consumer paths still refer to the original pin. */
|
|
121
|
+
async function review(target, candidateInventoryRevision, roots, maxSteps = 64) {
|
|
122
|
+
const candidate = {
|
|
123
|
+
...target,
|
|
124
|
+
inventoryRevision: candidateInventoryRevision
|
|
125
|
+
};
|
|
126
|
+
const before = await resolve(target);
|
|
127
|
+
if (before.status !== "exact") return {
|
|
128
|
+
status: "baseline-unavailable",
|
|
129
|
+
target,
|
|
130
|
+
candidate,
|
|
131
|
+
before
|
|
132
|
+
};
|
|
133
|
+
const [after, usage] = await Promise.all([resolve(candidate), findConsumers(target, before.value.variantId, roots, maxSteps)]);
|
|
134
|
+
if (after.status !== "exact") return {
|
|
135
|
+
status: "candidate-unavailable",
|
|
136
|
+
target,
|
|
137
|
+
candidate,
|
|
138
|
+
before,
|
|
139
|
+
after,
|
|
140
|
+
...usage
|
|
141
|
+
};
|
|
142
|
+
const sorted = (values = []) => canonicalJson(values.map(canonicalJson).sort());
|
|
143
|
+
return {
|
|
144
|
+
status: "exact",
|
|
145
|
+
target,
|
|
146
|
+
candidate,
|
|
147
|
+
before,
|
|
148
|
+
after,
|
|
149
|
+
...usage,
|
|
150
|
+
changes: {
|
|
151
|
+
variant: before.value.variantId !== after.value.variantId,
|
|
152
|
+
facts: canonicalJson({ data: before.value.data }) !== canonicalJson({ data: after.value.data }),
|
|
153
|
+
provenance: sorted(before.value.provenance) !== sorted(after.value.provenance),
|
|
154
|
+
externalReferences: sorted(before.value.externalReferences) !== sorted(after.value.externalReferences)
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
resolve,
|
|
160
|
+
trace,
|
|
161
|
+
consumers,
|
|
162
|
+
review
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
const applies = (required, context) => !context || !required || Object.entries(required).every(([key, value]) => context[key] === value);
|
|
166
|
+
const relationOrder = (left, right) => (left.order ?? 1e400) - (right.order ?? 1e400);
|
|
167
|
+
const traceOrder = (left, right) => relationOrder(left, right) || (left.id < right.id ? -1 : left.id > right.id ? 1 : 0);
|
|
168
|
+
/** Mutable lookup tables shared by the in-memory reader and the shard loader. */
|
|
169
|
+
function createStore() {
|
|
170
|
+
const entities = /* @__PURE__ */ new Map();
|
|
171
|
+
const relations = /* @__PURE__ */ new Map();
|
|
172
|
+
const outgoing = /* @__PURE__ */ new Map();
|
|
173
|
+
const incoming = /* @__PURE__ */ new Map();
|
|
174
|
+
function add(index) {
|
|
175
|
+
for (const entity of index.entities) entities.set(entity.id, entity);
|
|
176
|
+
for (const relation of index.relations) {
|
|
177
|
+
if (relations.has(relation.id)) continue;
|
|
178
|
+
relations.set(relation.id, relation);
|
|
179
|
+
const out = outgoing.get(relation.source);
|
|
180
|
+
if (out) out.push(relation);
|
|
181
|
+
else outgoing.set(relation.source, [relation]);
|
|
182
|
+
const into = incoming.get(relation.target);
|
|
183
|
+
if (into) into.push(relation);
|
|
184
|
+
else incoming.set(relation.target, [relation]);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const resolve = (id, context = {}) => resolveContext(entities.get(id)?.variants ?? [], context);
|
|
188
|
+
function matchingRelations(id, options) {
|
|
189
|
+
return (options.direction === "in" ? incoming.get(id) ?? [] : outgoing.get(id) ?? []).filter((relation) => (!options.type || relation.type === options.type) && applies(options.direction === "in" ? relation.targetContext ?? relation.context : relation.context, options.context));
|
|
190
|
+
}
|
|
191
|
+
function relationsOf(id, options = {}) {
|
|
192
|
+
return matchingRelations(id, options).sort(relationOrder);
|
|
193
|
+
}
|
|
194
|
+
/** Yield missing entities one frontier at a time so the shard reader can load them before resolving the layer. */
|
|
195
|
+
function* trace(id, context = {}, { maxSteps, includeEditions } = {}) {
|
|
196
|
+
if (maxSteps !== void 0 && (!Number.isSafeInteger(maxSteps) || maxSteps < 1)) throw new DocsError("DOCS_TRACE_LIMIT", "maxSteps must be a positive safe integer.", { field: "maxSteps" });
|
|
197
|
+
const steps = [];
|
|
198
|
+
const visitKey = (entity, edition) => includeEditions ? canonicalJson([entity, edition]) : entity;
|
|
199
|
+
const seen = /* @__PURE__ */ new Set([visitKey(id, context)]);
|
|
200
|
+
let frontier = [{
|
|
201
|
+
entity: id,
|
|
202
|
+
context
|
|
203
|
+
}];
|
|
204
|
+
while (frontier.length) {
|
|
205
|
+
const missing = frontier.filter((step) => !entities.has(step.entity)).map((step) => step.entity);
|
|
206
|
+
if (missing.length) yield missing;
|
|
207
|
+
const next = [];
|
|
208
|
+
for (const step of frontier) {
|
|
209
|
+
const found = entities.get(step.entity);
|
|
210
|
+
const resolution = resolveContext(found?.variants ?? [], step.context);
|
|
211
|
+
if (resolution.status !== "exact") {
|
|
212
|
+
if (!step.via) return resolution;
|
|
213
|
+
steps.push({
|
|
214
|
+
entity: step.entity,
|
|
215
|
+
type: found?.type ?? "unknown",
|
|
216
|
+
variant: step.entity,
|
|
217
|
+
via: step.via,
|
|
218
|
+
provenance: []
|
|
219
|
+
});
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const { variantId, data, provenance } = resolution.value;
|
|
223
|
+
steps.push({
|
|
224
|
+
entity: step.entity,
|
|
225
|
+
type: found.type,
|
|
226
|
+
variant: variantId,
|
|
227
|
+
...step.via && { via: step.via },
|
|
228
|
+
data,
|
|
229
|
+
provenance
|
|
230
|
+
});
|
|
231
|
+
for (const relation of matchingRelations(step.entity, { context: step.context }).sort(traceOrder)) {
|
|
232
|
+
const pinned = includeEditions && relation.targetContext !== void 0 && [
|
|
233
|
+
"derivesFrom",
|
|
234
|
+
"variantOf",
|
|
235
|
+
"supersedes"
|
|
236
|
+
].includes(relation.type);
|
|
237
|
+
if (!pinned && !relation.propagatesImpact && relation.type !== "derivedFrom" && relation.type !== "contains") continue;
|
|
238
|
+
const nextContext = pinned ? relation.targetContext : step.context;
|
|
239
|
+
const key = visitKey(relation.target, nextContext);
|
|
240
|
+
if (seen.has(key)) continue;
|
|
241
|
+
if (seen.size === maxSteps) throw new DocsError("DOCS_TRACE_LIMIT", `Trace exceeds the requested limit of ${maxSteps} entities.`, { field: "maxSteps" });
|
|
242
|
+
seen.add(key);
|
|
243
|
+
next.push({
|
|
244
|
+
entity: relation.target,
|
|
245
|
+
context: nextContext,
|
|
246
|
+
via: {
|
|
247
|
+
relation: relation.id,
|
|
248
|
+
type: relation.type,
|
|
249
|
+
from: step.entity,
|
|
250
|
+
...includeEditions && { fromVariant: variantId }
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
frontier = next;
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
status: "exact",
|
|
259
|
+
value: steps
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
entities,
|
|
264
|
+
all: () => [...relations.values()],
|
|
265
|
+
add,
|
|
266
|
+
resolve,
|
|
267
|
+
relations: relationsOf,
|
|
268
|
+
trace
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
/** Read Runtime over a complete in-memory semantic index. It needs no build toolchain, file system or DOM. */
|
|
272
|
+
function createSemanticReader(index) {
|
|
273
|
+
const store = createStore();
|
|
274
|
+
store.add(index);
|
|
275
|
+
return {
|
|
276
|
+
entity: (id) => store.entities.get(id) ?? null,
|
|
277
|
+
entities: (filter = {}) => index.entities.filter((entity) => !filter.type || entity.type === filter.type),
|
|
278
|
+
resolve: store.resolve,
|
|
279
|
+
relations: store.relations,
|
|
280
|
+
/**
|
|
281
|
+
* Follow derivedFrom and contains edges from an entity to the facts it is built from, breadth first, and report each
|
|
282
|
+
* step's provenance. Entities with no variant for the context are reported once and not expanded.
|
|
283
|
+
*/
|
|
284
|
+
trace(id, context = {}, options = {}) {
|
|
285
|
+
const walk = store.trace(id, context, options);
|
|
286
|
+
let current = walk.next();
|
|
287
|
+
while (!current.done) current = walk.next();
|
|
288
|
+
return current.value;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
/** Expand a serialized shard into entities and relations, restoring the IDs and provenance it stores compactly. */
|
|
293
|
+
function decodeSemanticShard(shard) {
|
|
294
|
+
const sources = (refs) => refs.map((ref) => {
|
|
295
|
+
if (!Number.isInteger(ref) || !shard.sources[ref]) throw new DocsError("DOCS_SHARD_INVALID", "Invalid provenance reference.");
|
|
296
|
+
return shard.sources[ref];
|
|
297
|
+
});
|
|
298
|
+
return {
|
|
299
|
+
entities: shard.entities.map((entity) => ({
|
|
300
|
+
id: entity.id,
|
|
301
|
+
type: entity.type,
|
|
302
|
+
variants: entity.variants.map((variant) => ({
|
|
303
|
+
variantId: variantId(entity.id, variant.context),
|
|
304
|
+
...variant,
|
|
305
|
+
provenance: sources(variant.provenance)
|
|
306
|
+
}))
|
|
307
|
+
})),
|
|
308
|
+
relations: shard.relations.map(({ provenance, ...relation }) => ({
|
|
309
|
+
id: relationId(relation.type, relation.source, relation.target, relation.context, relation.targetContext),
|
|
310
|
+
...relation,
|
|
311
|
+
provenance: sources(provenance)
|
|
312
|
+
}))
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Read Runtime over a sharded build. It loads only the shards that hold the requested entities and, for trace, the
|
|
317
|
+
* shards of the entities it reaches. Each shard holds a subject's entities and every relation touching them.
|
|
318
|
+
*/
|
|
319
|
+
function createSemanticLoader(pointer, readText, options = {}) {
|
|
320
|
+
if (pointer.schemaVersion !== 2 || options.digest && options.digest(canonicalJson(pointer.shards)) !== pointer.revision) throw new DocsError("DOCS_SHARD_INVALID", "Invalid semantic pointer schema or revision.");
|
|
321
|
+
const store = createStore();
|
|
322
|
+
const loads = /* @__PURE__ */ new Map();
|
|
323
|
+
const load = (id) => loadSubject(shardKey(id));
|
|
324
|
+
function loadSubject(subject) {
|
|
325
|
+
const shard = Object.hasOwn(pointer.shards, subject) ? pointer.shards[subject] : void 0;
|
|
326
|
+
if (!shard) return Promise.resolve(null);
|
|
327
|
+
let pending = loads.get(subject);
|
|
328
|
+
if (!pending) {
|
|
329
|
+
pending = (async () => {
|
|
330
|
+
if (/[:\\?#%]/.test(shard.path) || shard.path.split("/").some((part) => !part || part === "." || part === "..")) throw new DocsError("DOCS_SHARD_INVALID", `Invalid shard path for ${subject}.`);
|
|
331
|
+
const text = await readText(shard.path);
|
|
332
|
+
if (options.digest && options.digest(text) !== shard.revision && !(text.endsWith("\n") && options.digest(text.slice(0, -1)) === shard.revision)) throw new DocsError("DOCS_SHARD_INVALID", `Shard revision differs for ${subject}.`);
|
|
333
|
+
const data = JSON.parse(text);
|
|
334
|
+
if (data.schemaVersion !== 1 || data.subject !== subject || !data.entities.every((entity) => shardKey(entity.id) === subject) || !data.relations.every((edge) => shardKey(edge.source) === subject || shardKey(edge.target) === subject)) throw new DocsError("DOCS_SHARD_INVALID", `Invalid semantic shard for ${subject}.`);
|
|
335
|
+
const decoded = decodeSemanticShard(data);
|
|
336
|
+
decoded.entities.sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
337
|
+
store.add(decoded);
|
|
338
|
+
return decoded;
|
|
339
|
+
})().catch((error) => {
|
|
340
|
+
loads.delete(subject);
|
|
341
|
+
throw error;
|
|
342
|
+
});
|
|
343
|
+
loads.set(subject, pending);
|
|
344
|
+
}
|
|
345
|
+
return pending;
|
|
346
|
+
}
|
|
347
|
+
return {
|
|
348
|
+
revision: pointer.revision,
|
|
349
|
+
/** Read one exact published subject, including its entities, ordered variants and touching relations. */
|
|
350
|
+
subject: loadSubject,
|
|
351
|
+
/** Subjects whose shards have been read, for cost reporting. */
|
|
352
|
+
loaded: () => [...loads.keys()],
|
|
353
|
+
/** Discover entities in subject then ID order. A prefix limits shard reads; after resumes after an entity ID. */
|
|
354
|
+
async *entities({ subjectPrefix = "", type, after } = {}) {
|
|
355
|
+
const start = after === void 0 ? void 0 : shardKey(after);
|
|
356
|
+
for (const subject of Object.keys(pointer.shards).sort()) {
|
|
357
|
+
if (!subject.startsWith(subjectPrefix) || start !== void 0 && subject < start) continue;
|
|
358
|
+
const shard = await loadSubject(subject);
|
|
359
|
+
for (const entity of shard?.entities ?? []) {
|
|
360
|
+
if (subject === start && after !== void 0 && entity.id <= after) continue;
|
|
361
|
+
if (type === void 0 || entity.type === type) yield entity;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
async entity(id) {
|
|
366
|
+
await load(id);
|
|
367
|
+
return store.entities.get(id) ?? null;
|
|
368
|
+
},
|
|
369
|
+
async resolve(id, context = {}) {
|
|
370
|
+
await load(id);
|
|
371
|
+
return store.resolve(id, context);
|
|
372
|
+
},
|
|
373
|
+
async relations(id, options = {}) {
|
|
374
|
+
await load(id);
|
|
375
|
+
return store.relations(id, options);
|
|
376
|
+
},
|
|
377
|
+
async trace(id, context = {}, options = {}) {
|
|
378
|
+
const walk = store.trace(id, context, options);
|
|
379
|
+
let current = walk.next();
|
|
380
|
+
while (!current.done) {
|
|
381
|
+
await Promise.all(current.value.map(load));
|
|
382
|
+
current = walk.next();
|
|
383
|
+
}
|
|
384
|
+
return current.value;
|
|
385
|
+
},
|
|
386
|
+
/** Read every shard, for full scans such as listing all entities of a type. */
|
|
387
|
+
async loadAll() {
|
|
388
|
+
await Promise.all(Object.keys(pointer.shards).map(loadSubject));
|
|
389
|
+
const sort = (left, right) => left.id < right.id ? -1 : left.id > right.id ? 1 : 0;
|
|
390
|
+
return {
|
|
391
|
+
schemaVersion: 1,
|
|
392
|
+
entities: [...store.entities.values()].sort(sort),
|
|
393
|
+
relations: store.all().sort(sort)
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export { createReader, createReferenceReader, createSemanticLoader, createSemanticReader, decodeSemanticShard, documentKey, factContext, openPinnedReader, projectReadingChoices, readPublicationArtifacts, readingContext, resolveDocumentContext, sharedPageContext };
|
|
400
|
+
//# sourceMappingURL=reader.js.map
|