@f0rbit/corpus 0.1.7 → 0.1.8
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;AAuBvE;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,cAAc,GAAG,kBAAkB,CAmHrH"}
|
|
@@ -2,9 +2,9 @@
|
|
|
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
8
|
/**
|
|
9
9
|
* Convert client query opts to storage query opts.
|
|
10
10
|
* Handles Date -> ISO string conversion.
|
|
@@ -19,7 +19,7 @@ function to_storage_opts(opts) {
|
|
|
19
19
|
created_before: opts.created_before?.toISOString(),
|
|
20
20
|
observed_after: opts.after?.toISOString(),
|
|
21
21
|
observed_before: opts.before?.toISOString(),
|
|
22
|
-
limit: opts.limit
|
|
22
|
+
limit: opts.limit,
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
@@ -31,21 +31,29 @@ export function create_observations_client(storage, metadata) {
|
|
|
31
31
|
const result = await metadata.get_latest(store_id);
|
|
32
32
|
return result.ok ? result.value.version : null;
|
|
33
33
|
}
|
|
34
|
+
async function resolve_version(store_id, resolver) {
|
|
35
|
+
if (resolver) {
|
|
36
|
+
const resolved = await resolver(store_id);
|
|
37
|
+
if (resolved !== null)
|
|
38
|
+
return resolved;
|
|
39
|
+
}
|
|
40
|
+
return get_latest_version(store_id);
|
|
41
|
+
}
|
|
34
42
|
return {
|
|
35
43
|
async put(type, opts) {
|
|
36
44
|
const validation = type.schema.safeParse(opts.content);
|
|
37
45
|
if (!validation.success) {
|
|
38
46
|
return err({
|
|
39
|
-
kind:
|
|
47
|
+
kind: "validation_error",
|
|
40
48
|
cause: validation.error,
|
|
41
|
-
message: validation.error.message
|
|
49
|
+
message: validation.error.message,
|
|
42
50
|
});
|
|
43
51
|
}
|
|
44
52
|
const id = generate_observation_id();
|
|
45
53
|
const row = create_observation_row(id, type.name, opts.source, validation.data, {
|
|
46
54
|
confidence: opts.confidence,
|
|
47
55
|
observed_at: opts.observed_at,
|
|
48
|
-
derived_from: opts.derived_from
|
|
56
|
+
derived_from: opts.derived_from,
|
|
49
57
|
});
|
|
50
58
|
const result = await storage.put_row(row);
|
|
51
59
|
if (!result.ok)
|
|
@@ -58,7 +66,7 @@ export function create_observations_client(storage, metadata) {
|
|
|
58
66
|
...(opts.confidence !== undefined && { confidence: opts.confidence }),
|
|
59
67
|
...(opts.observed_at && { observed_at: opts.observed_at }),
|
|
60
68
|
created_at: new Date(row.created_at),
|
|
61
|
-
...(opts.derived_from && { derived_from: opts.derived_from })
|
|
69
|
+
...(opts.derived_from && { derived_from: opts.derived_from }),
|
|
62
70
|
};
|
|
63
71
|
return ok(observation);
|
|
64
72
|
},
|
|
@@ -67,16 +75,21 @@ export function create_observations_client(storage, metadata) {
|
|
|
67
75
|
if (!result.ok)
|
|
68
76
|
return result;
|
|
69
77
|
if (!result.value) {
|
|
70
|
-
return err({ kind:
|
|
78
|
+
return err({ kind: "observation_not_found", id });
|
|
71
79
|
}
|
|
72
80
|
return ok(row_to_observation(result.value));
|
|
73
81
|
},
|
|
74
82
|
async *query(opts = {}) {
|
|
75
83
|
const storageOpts = to_storage_opts(opts);
|
|
84
|
+
const version_cache = new Map();
|
|
76
85
|
for await (const row of storage.query_rows(storageOpts)) {
|
|
77
86
|
if (!opts.include_stale) {
|
|
78
|
-
|
|
79
|
-
if (
|
|
87
|
+
let canonical_version = version_cache.get(row.source_store_id);
|
|
88
|
+
if (canonical_version === undefined) {
|
|
89
|
+
canonical_version = await resolve_version(row.source_store_id, opts.version_resolver);
|
|
90
|
+
version_cache.set(row.source_store_id, canonical_version);
|
|
91
|
+
}
|
|
92
|
+
if (canonical_version && row.source_version !== canonical_version)
|
|
80
93
|
continue;
|
|
81
94
|
}
|
|
82
95
|
yield row_to_observation(row);
|
|
@@ -84,10 +97,15 @@ export function create_observations_client(storage, metadata) {
|
|
|
84
97
|
},
|
|
85
98
|
async *query_meta(opts = {}) {
|
|
86
99
|
const storageOpts = to_storage_opts(opts);
|
|
100
|
+
const version_cache = new Map();
|
|
87
101
|
for await (const row of storage.query_rows(storageOpts)) {
|
|
88
102
|
if (!opts.include_stale) {
|
|
89
|
-
|
|
90
|
-
if (
|
|
103
|
+
let canonical_version = version_cache.get(row.source_store_id);
|
|
104
|
+
if (canonical_version === undefined) {
|
|
105
|
+
canonical_version = await resolve_version(row.source_store_id, opts.version_resolver);
|
|
106
|
+
version_cache.set(row.source_store_id, canonical_version);
|
|
107
|
+
}
|
|
108
|
+
if (canonical_version && row.source_version !== canonical_version)
|
|
91
109
|
continue;
|
|
92
110
|
}
|
|
93
111
|
yield row_to_meta(row);
|
|
@@ -98,7 +116,7 @@ export function create_observations_client(storage, metadata) {
|
|
|
98
116
|
if (!result.ok)
|
|
99
117
|
return result;
|
|
100
118
|
if (!result.value) {
|
|
101
|
-
return err({ kind:
|
|
119
|
+
return err({ kind: "observation_not_found", id });
|
|
102
120
|
}
|
|
103
121
|
return ok(undefined);
|
|
104
122
|
},
|
|
@@ -110,6 +128,6 @@ export function create_observations_client(storage, metadata) {
|
|
|
110
128
|
if (!latest)
|
|
111
129
|
return false;
|
|
112
130
|
return pointer.version !== latest;
|
|
113
|
-
}
|
|
131
|
+
},
|
|
114
132
|
};
|
|
115
133
|
}
|
|
@@ -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,38 @@ 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
|
+
* Function that resolves which version is "canonical" for a given store.
|
|
124
|
+
* Return null to fall back to default behavior (most recent by created_at).
|
|
125
|
+
*
|
|
126
|
+
* @category Types
|
|
127
|
+
* @group Observation Types
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const resolver: VersionResolver = async (store_id) => {
|
|
132
|
+
* const published = await db.query.published_reports.findFirst({
|
|
133
|
+
* where: eq(published_reports.store_id, store_id)
|
|
134
|
+
* });
|
|
135
|
+
* return published?.version ?? null;
|
|
136
|
+
* };
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export type VersionResolver = (store_id: string) => Promise<string | null>;
|
|
109
140
|
/**
|
|
110
141
|
* Query options for filtering observations.
|
|
111
142
|
*
|
|
@@ -142,31 +173,30 @@ export type ObservationQueryOpts = {
|
|
|
142
173
|
include_stale?: boolean;
|
|
143
174
|
limit?: number;
|
|
144
175
|
cursor?: string;
|
|
176
|
+
/**
|
|
177
|
+
* Custom function to resolve which version is "current" for a given store.
|
|
178
|
+
* When provided and include_stale is false, this takes precedence over
|
|
179
|
+
* the default "most recent by created_at" staleness logic.
|
|
180
|
+
*
|
|
181
|
+
* If the resolver returns null for a store_id, falls back to default behavior.
|
|
182
|
+
*/
|
|
183
|
+
version_resolver?: VersionResolver;
|
|
145
184
|
};
|
|
146
185
|
/**
|
|
147
|
-
*
|
|
186
|
+
* Utility type to extract the content type from an ObservationTypeDef.
|
|
148
187
|
*
|
|
149
188
|
* @category Types
|
|
150
189
|
* @group Observation Types
|
|
151
190
|
*
|
|
152
191
|
* @example
|
|
153
192
|
* ```ts
|
|
154
|
-
* const
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
* observed_at: new Date(),
|
|
159
|
-
* derived_from: [{ store_id: 'raw_text', version: 'xyz789' }]
|
|
160
|
-
* }
|
|
193
|
+
* const entity_mention = define_observation_type('entity_mention', EntityMentionSchema)
|
|
194
|
+
*
|
|
195
|
+
* type EntityMention = InferObservationContent<typeof entity_mention>
|
|
196
|
+
* // => { entity: string; entity_type: 'person' | 'organization' | ... }
|
|
161
197
|
* ```
|
|
162
198
|
*/
|
|
163
|
-
export type
|
|
164
|
-
source: SnapshotPointer;
|
|
165
|
-
content: T;
|
|
166
|
-
confidence?: number;
|
|
167
|
-
observed_at?: Date;
|
|
168
|
-
derived_from?: SnapshotPointer[];
|
|
169
|
-
};
|
|
199
|
+
export type InferObservationContent<T> = T extends ObservationTypeDef<infer C> ? C : never;
|
|
170
200
|
/**
|
|
171
201
|
* Defines a typed observation schema.
|
|
172
202
|
*
|
|
@@ -201,19 +231,4 @@ export type ObservationPutOpts<T> = {
|
|
|
201
231
|
* ```
|
|
202
232
|
*/
|
|
203
233
|
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
234
|
//# 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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": "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",
|