@f0rbit/corpus 0.1.7 → 0.1.9
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.
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* @module ObservationsClient
|
|
3
3
|
* @description Centralized business logic for observations, built on storage adapters.
|
|
4
4
|
*/
|
|
5
|
-
import type { MetadataClient, ObservationsClient } from
|
|
6
|
-
import type { ObservationsStorage } from
|
|
5
|
+
import type { MetadataClient, ObservationsClient } from "../types";
|
|
6
|
+
import type { ObservationsStorage } from "./storage";
|
|
7
7
|
/**
|
|
8
8
|
* Creates an ObservationsClient from a storage adapter.
|
|
9
9
|
* All business logic (validation, staleness, etc.) is centralized here.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../observations/client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAuB,cAAc,EAAE,kBAAkB,EAAE,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../observations/client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAuB,cAAc,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAExF,OAAO,KAAK,EAAE,mBAAmB,EAAoB,MAAM,WAAW,CAAC;AA6BvE;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,cAAc,GAAG,kBAAkB,CAiGrH"}
|
|
@@ -2,9 +2,16 @@
|
|
|
2
2
|
* @module ObservationsClient
|
|
3
3
|
* @description Centralized business logic for observations, built on storage adapters.
|
|
4
4
|
*/
|
|
5
|
-
import { row_to_observation, row_to_meta, create_observation_row } from
|
|
6
|
-
import { generate_observation_id } from
|
|
7
|
-
import { ok, err } from
|
|
5
|
+
import { row_to_observation, row_to_meta, create_observation_row } from "./storage";
|
|
6
|
+
import { generate_observation_id } from "./utils";
|
|
7
|
+
import { ok, err } from "../types";
|
|
8
|
+
async function apply_version_filter(filter, store_id, version) {
|
|
9
|
+
if (typeof filter === "function")
|
|
10
|
+
return filter(store_id, version);
|
|
11
|
+
if (filter instanceof Set)
|
|
12
|
+
return filter.has(version);
|
|
13
|
+
return filter.includes(version);
|
|
14
|
+
}
|
|
8
15
|
/**
|
|
9
16
|
* Convert client query opts to storage query opts.
|
|
10
17
|
* Handles Date -> ISO string conversion.
|
|
@@ -19,7 +26,7 @@ function to_storage_opts(opts) {
|
|
|
19
26
|
created_before: opts.created_before?.toISOString(),
|
|
20
27
|
observed_after: opts.after?.toISOString(),
|
|
21
28
|
observed_before: opts.before?.toISOString(),
|
|
22
|
-
limit: opts.limit
|
|
29
|
+
limit: opts.limit,
|
|
23
30
|
};
|
|
24
31
|
}
|
|
25
32
|
/**
|
|
@@ -36,16 +43,16 @@ export function create_observations_client(storage, metadata) {
|
|
|
36
43
|
const validation = type.schema.safeParse(opts.content);
|
|
37
44
|
if (!validation.success) {
|
|
38
45
|
return err({
|
|
39
|
-
kind:
|
|
46
|
+
kind: "validation_error",
|
|
40
47
|
cause: validation.error,
|
|
41
|
-
message: validation.error.message
|
|
48
|
+
message: validation.error.message,
|
|
42
49
|
});
|
|
43
50
|
}
|
|
44
51
|
const id = generate_observation_id();
|
|
45
52
|
const row = create_observation_row(id, type.name, opts.source, validation.data, {
|
|
46
53
|
confidence: opts.confidence,
|
|
47
54
|
observed_at: opts.observed_at,
|
|
48
|
-
derived_from: opts.derived_from
|
|
55
|
+
derived_from: opts.derived_from,
|
|
49
56
|
});
|
|
50
57
|
const result = await storage.put_row(row);
|
|
51
58
|
if (!result.ok)
|
|
@@ -58,7 +65,7 @@ export function create_observations_client(storage, metadata) {
|
|
|
58
65
|
...(opts.confidence !== undefined && { confidence: opts.confidence }),
|
|
59
66
|
...(opts.observed_at && { observed_at: opts.observed_at }),
|
|
60
67
|
created_at: new Date(row.created_at),
|
|
61
|
-
...(opts.derived_from && { derived_from: opts.derived_from })
|
|
68
|
+
...(opts.derived_from && { derived_from: opts.derived_from }),
|
|
62
69
|
};
|
|
63
70
|
return ok(observation);
|
|
64
71
|
},
|
|
@@ -67,16 +74,16 @@ export function create_observations_client(storage, metadata) {
|
|
|
67
74
|
if (!result.ok)
|
|
68
75
|
return result;
|
|
69
76
|
if (!result.value) {
|
|
70
|
-
return err({ kind:
|
|
77
|
+
return err({ kind: "observation_not_found", id });
|
|
71
78
|
}
|
|
72
79
|
return ok(row_to_observation(result.value));
|
|
73
80
|
},
|
|
74
81
|
async *query(opts = {}) {
|
|
75
82
|
const storageOpts = to_storage_opts(opts);
|
|
76
83
|
for await (const row of storage.query_rows(storageOpts)) {
|
|
77
|
-
if (
|
|
78
|
-
const
|
|
79
|
-
if (
|
|
84
|
+
if (opts.version_filter !== undefined) {
|
|
85
|
+
const included = await apply_version_filter(opts.version_filter, row.source_store_id, row.source_version);
|
|
86
|
+
if (!included)
|
|
80
87
|
continue;
|
|
81
88
|
}
|
|
82
89
|
yield row_to_observation(row);
|
|
@@ -85,9 +92,9 @@ export function create_observations_client(storage, metadata) {
|
|
|
85
92
|
async *query_meta(opts = {}) {
|
|
86
93
|
const storageOpts = to_storage_opts(opts);
|
|
87
94
|
for await (const row of storage.query_rows(storageOpts)) {
|
|
88
|
-
if (
|
|
89
|
-
const
|
|
90
|
-
if (
|
|
95
|
+
if (opts.version_filter !== undefined) {
|
|
96
|
+
const included = await apply_version_filter(opts.version_filter, row.source_store_id, row.source_version);
|
|
97
|
+
if (!included)
|
|
91
98
|
continue;
|
|
92
99
|
}
|
|
93
100
|
yield row_to_meta(row);
|
|
@@ -98,7 +105,7 @@ export function create_observations_client(storage, metadata) {
|
|
|
98
105
|
if (!result.ok)
|
|
99
106
|
return result;
|
|
100
107
|
if (!result.value) {
|
|
101
|
-
return err({ kind:
|
|
108
|
+
return err({ kind: "observation_not_found", id });
|
|
102
109
|
}
|
|
103
110
|
return ok(undefined);
|
|
104
111
|
},
|
|
@@ -110,6 +117,6 @@ export function create_observations_client(storage, metadata) {
|
|
|
110
117
|
if (!latest)
|
|
111
118
|
return false;
|
|
112
119
|
return pointer.version !== latest;
|
|
113
|
-
}
|
|
120
|
+
},
|
|
114
121
|
};
|
|
115
122
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module ObservationTypes
|
|
3
3
|
* @description Type definitions for the observations feature.
|
|
4
4
|
*/
|
|
5
|
-
import type { ZodType } from
|
|
5
|
+
import type { ZodType } from "zod";
|
|
6
6
|
/**
|
|
7
7
|
* Universal address to versioned content within a corpus store.
|
|
8
8
|
*
|
|
@@ -105,7 +105,46 @@ export type Observation<T = unknown> = {
|
|
|
105
105
|
* @category Types
|
|
106
106
|
* @group Observation Types
|
|
107
107
|
*/
|
|
108
|
-
export type ObservationMeta = Omit<Observation<never>,
|
|
108
|
+
export type ObservationMeta = Omit<Observation<never>, "content">;
|
|
109
|
+
/**
|
|
110
|
+
* Options for creating a new observation.
|
|
111
|
+
*
|
|
112
|
+
* @category Types
|
|
113
|
+
* @group Observation Types
|
|
114
|
+
*/
|
|
115
|
+
export type ObservationPutOpts<T> = {
|
|
116
|
+
source: SnapshotPointer;
|
|
117
|
+
content: T;
|
|
118
|
+
confidence?: number;
|
|
119
|
+
observed_at?: Date;
|
|
120
|
+
derived_from?: SnapshotPointer[];
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Filter for version-based observation filtering.
|
|
124
|
+
* - Set<string> or string[]: Include if version is in the collection
|
|
125
|
+
* - Function: Called with (store_id, version), return true to include
|
|
126
|
+
*
|
|
127
|
+
* @category Types
|
|
128
|
+
* @group Observation Types
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* // Filter to specific versions
|
|
133
|
+
* const filter: VersionFilter = new Set(['v1', 'v2', 'v3'])
|
|
134
|
+
*
|
|
135
|
+
* // Or use an array
|
|
136
|
+
* const filter: VersionFilter = ['v1', 'v2', 'v3']
|
|
137
|
+
*
|
|
138
|
+
* // Or use a function for dynamic filtering
|
|
139
|
+
* const filter: VersionFilter = async (store_id, version) => {
|
|
140
|
+
* const published = await db.query.published_reports.findFirst({
|
|
141
|
+
* where: eq(published_reports.version, version)
|
|
142
|
+
* });
|
|
143
|
+
* return published !== null;
|
|
144
|
+
* };
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
export type VersionFilter = Set<string> | string[] | ((store_id: string, version: string) => boolean | Promise<boolean>);
|
|
109
148
|
/**
|
|
110
149
|
* Query options for filtering observations.
|
|
111
150
|
*
|
|
@@ -122,11 +161,10 @@ export type ObservationMeta = Omit<Observation<never>, 'content'>;
|
|
|
122
161
|
* limit: 100
|
|
123
162
|
* }
|
|
124
163
|
*
|
|
125
|
-
* //
|
|
164
|
+
* // Filter to only published versions
|
|
126
165
|
* const versionOpts: ObservationQueryOpts = {
|
|
127
166
|
* source_store: 'hansard',
|
|
128
|
-
*
|
|
129
|
-
* include_stale: true
|
|
167
|
+
* version_filter: new Set(['v1', 'v2'])
|
|
130
168
|
* }
|
|
131
169
|
* ```
|
|
132
170
|
*/
|
|
@@ -139,34 +177,32 @@ export type ObservationQueryOpts = {
|
|
|
139
177
|
before?: Date;
|
|
140
178
|
created_after?: Date;
|
|
141
179
|
created_before?: Date;
|
|
142
|
-
include_stale?: boolean;
|
|
143
180
|
limit?: number;
|
|
144
181
|
cursor?: string;
|
|
182
|
+
/**
|
|
183
|
+
* Filter observations by version.
|
|
184
|
+
* When provided, only observations whose source version passes the filter are included.
|
|
185
|
+
*
|
|
186
|
+
* - Set<string> or string[]: Include if version is in the collection
|
|
187
|
+
* - Function: Called with (store_id, version), return true to include
|
|
188
|
+
*/
|
|
189
|
+
version_filter?: VersionFilter;
|
|
145
190
|
};
|
|
146
191
|
/**
|
|
147
|
-
*
|
|
192
|
+
* Utility type to extract the content type from an ObservationTypeDef.
|
|
148
193
|
*
|
|
149
194
|
* @category Types
|
|
150
195
|
* @group Observation Types
|
|
151
196
|
*
|
|
152
197
|
* @example
|
|
153
198
|
* ```ts
|
|
154
|
-
* const
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
* observed_at: new Date(),
|
|
159
|
-
* derived_from: [{ store_id: 'raw_text', version: 'xyz789' }]
|
|
160
|
-
* }
|
|
199
|
+
* const entity_mention = define_observation_type('entity_mention', EntityMentionSchema)
|
|
200
|
+
*
|
|
201
|
+
* type EntityMention = InferObservationContent<typeof entity_mention>
|
|
202
|
+
* // => { entity: string; entity_type: 'person' | 'organization' | ... }
|
|
161
203
|
* ```
|
|
162
204
|
*/
|
|
163
|
-
export type
|
|
164
|
-
source: SnapshotPointer;
|
|
165
|
-
content: T;
|
|
166
|
-
confidence?: number;
|
|
167
|
-
observed_at?: Date;
|
|
168
|
-
derived_from?: SnapshotPointer[];
|
|
169
|
-
};
|
|
205
|
+
export type InferObservationContent<T> = T extends ObservationTypeDef<infer C> ? C : never;
|
|
170
206
|
/**
|
|
171
207
|
* Defines a typed observation schema.
|
|
172
208
|
*
|
|
@@ -201,19 +237,4 @@ export type ObservationPutOpts<T> = {
|
|
|
201
237
|
* ```
|
|
202
238
|
*/
|
|
203
239
|
export declare function define_observation_type<T>(name: string, schema: ZodType<T>): ObservationTypeDef<T>;
|
|
204
|
-
/**
|
|
205
|
-
* Utility type to extract the content type from an ObservationTypeDef.
|
|
206
|
-
*
|
|
207
|
-
* @category Types
|
|
208
|
-
* @group Observation Types
|
|
209
|
-
*
|
|
210
|
-
* @example
|
|
211
|
-
* ```ts
|
|
212
|
-
* const entity_mention = define_observation_type('entity_mention', EntityMentionSchema)
|
|
213
|
-
*
|
|
214
|
-
* type EntityMention = InferObservationContent<typeof entity_mention>
|
|
215
|
-
* // => { entity: string; entity_type: 'person' | 'organization' | ... }
|
|
216
|
-
* ```
|
|
217
|
-
*/
|
|
218
|
-
export type InferObservationContent<T> = T extends ObservationTypeDef<infer C> ? C : never;
|
|
219
240
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../observations/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../observations/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,MAAM,eAAe,GAAG;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE;QACN,KAAK,EAAE,MAAM,CAAC;QACd,GAAG,EAAE,MAAM,CAAC;KACZ,CAAC;CACF,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;CAC5B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAAI;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,eAAe,CAAC;IACxB,OAAO,EAAE,CAAC,CAAC;IACX,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB,UAAU,EAAE,IAAI,CAAC;IACjB,YAAY,CAAC,EAAE,eAAe,EAAE,CAAC;CACjC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GAAG,IAAI,CAAC,WAAW,CAAC,KAAK,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,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC,CAElG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@f0rbit/corpus",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "A functional snapshotting library for TypeScript with versioned data storage, lineage tracking, and multiple backend support",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"main": "dist/index.js",
|