@kontourai/survey 0.1.2 → 0.1.4

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/README.md CHANGED
@@ -64,6 +64,140 @@ const trustInput = validateTrustInput(buildSurveyTrustInput(surveyInput));
64
64
  const report = buildTrustReport(trustInput);
65
65
  ```
66
66
 
67
+ ## Field observations
68
+
69
+ Use `fieldObservation` when a producer wants to describe one scalar field value
70
+ without hand-assembling the repeated source, extraction, candidate, review, and
71
+ claim defaults. The helper returns a normal `SurveyObservationInput`, so it
72
+ works with `SurveyInputBuilder.addObservation` and the same Surface projection
73
+ path.
74
+
75
+ ```ts
76
+ import {
77
+ buildSurveyTrustInput,
78
+ fieldObservation,
79
+ SurveyInputBuilder,
80
+ } from "@kontourai/survey";
81
+
82
+ const surveyInput = new SurveyInputBuilder({
83
+ source: "example-producer:run-1",
84
+ })
85
+ .addObservation(fieldObservation({
86
+ id: "entity-123.status.current",
87
+ field: "registrationStatus",
88
+ value: "ACTIVE",
89
+ rawSource: {
90
+ kind: "api-record",
91
+ sourceRef: "example-records://entity/entity-123",
92
+ observedAt: new Date().toISOString(),
93
+ locatorScheme: "structured-field",
94
+ },
95
+ extraction: {
96
+ confidence: 0.97,
97
+ locator: "json:$.registrationStatus",
98
+ extractor: "example-extractor",
99
+ extractedAt: new Date().toISOString(),
100
+ },
101
+ reviewOutcome: {
102
+ status: "verified",
103
+ actor: "records-operator",
104
+ reviewedAt: new Date().toISOString(),
105
+ },
106
+ claim: {
107
+ subjectType: "public-record.entity",
108
+ subjectId: "entity-123",
109
+ surface: "example.profile",
110
+ claimType: "public-data.field",
111
+ status: "verified",
112
+ impactLevel: "medium",
113
+ collectedBy: "example-extractor",
114
+ },
115
+ metadata: {
116
+ producerField: "registration_status",
117
+ },
118
+ }))
119
+ .build();
120
+
121
+ const trustInput = buildSurveyTrustInput(surveyInput);
122
+ ```
123
+
124
+ `fieldObservation` sets `extraction.target` and `claim.fieldOrBehavior` from
125
+ `field` when omitted, uses the scalar as both the extraction and claim value,
126
+ and adds neutral helper metadata at
127
+ `metadata.survey.field = { representation: "scalar" }`. Producer metadata is
128
+ preserved. Producers still own scalar semantics, validation, candidate ranking,
129
+ review policy, and whether a value should be verified, proposed, rejected, or
130
+ assumed.
131
+
132
+ ## Repeated observations
133
+
134
+ Use `repeatedObservation` when a producer wants to describe a repeated field or
135
+ entity list as one aggregate observation. The helper returns a normal
136
+ `SurveyObservationInput`, so it works with `SurveyInputBuilder.addObservation`
137
+ and the same Surface projection path.
138
+
139
+ ```ts
140
+ import {
141
+ buildSurveyTrustInput,
142
+ repeatedObservation,
143
+ SurveyInputBuilder,
144
+ } from "@kontourai/survey";
145
+
146
+ const aliases = [
147
+ { name: "North Annex", sourceLabel: "record row 1" },
148
+ { name: "East Annex", sourceLabel: "record row 2" },
149
+ ];
150
+
151
+ const surveyInput = new SurveyInputBuilder({
152
+ source: "example-producer:run-1",
153
+ })
154
+ .addObservation(repeatedObservation({
155
+ id: "entity-123.aliases.current",
156
+ field: "knownAliases",
157
+ value: aliases,
158
+ rawSource: {
159
+ kind: "api-record",
160
+ sourceRef: "example-records://entity/entity-123",
161
+ observedAt: new Date().toISOString(),
162
+ locatorScheme: "structured-field",
163
+ },
164
+ extraction: {
165
+ confidence: 0.88,
166
+ locator: "json:$.aliases",
167
+ extractor: "example-extractor",
168
+ extractedAt: new Date().toISOString(),
169
+ },
170
+ reviewOutcome: {
171
+ status: "verified",
172
+ actor: "records-operator",
173
+ reviewedAt: new Date().toISOString(),
174
+ },
175
+ claim: {
176
+ subjectType: "public-record.entity",
177
+ subjectId: "entity-123",
178
+ surface: "example.profile",
179
+ claimType: "public-data.repeated-field",
180
+ status: "verified",
181
+ impactLevel: "medium",
182
+ collectedBy: "example-extractor",
183
+ },
184
+ metadata: {
185
+ producerField: "aliases",
186
+ },
187
+ }))
188
+ .build();
189
+
190
+ const trustInput = buildSurveyTrustInput(surveyInput);
191
+ ```
192
+
193
+ `repeatedObservation` sets `extraction.target` and
194
+ `claim.fieldOrBehavior` from `field` when omitted, uses the array as both the
195
+ extraction and claim value, and adds neutral helper metadata at
196
+ `metadata.survey.repeated = { representation: "aggregate-array", itemCount }`.
197
+ Producer metadata is preserved. Producers still own item semantics,
198
+ validation, candidate ranking, review policy, and whether a value should be
199
+ verified, proposed, rejected, or assumed.
200
+
67
201
  ## Product Boundary
68
202
 
69
203
  Survey does not crawl pages, parse PDFs, rank candidates, decide review policy,
@@ -0,0 +1,20 @@
1
+ import type { SurveyObservationInput } from "./builder.js";
2
+ export interface FieldObservationInput<TValue> {
3
+ id: string;
4
+ field: string;
5
+ value: TValue;
6
+ rawSource: SurveyObservationInput["rawSource"];
7
+ extraction: Omit<SurveyObservationInput["extraction"], "target" | "value" | "excerpt"> & {
8
+ target?: string;
9
+ excerpt?: string | null;
10
+ };
11
+ reviewOutcome?: SurveyObservationInput["reviewOutcome"];
12
+ claim: Omit<SurveyObservationInput["claim"], "fieldOrBehavior" | "value"> & {
13
+ fieldOrBehavior?: string;
14
+ };
15
+ candidate?: SurveyObservationInput["candidate"];
16
+ candidateSet?: SurveyObservationInput["candidateSet"];
17
+ representation?: "scalar";
18
+ metadata?: Record<string, unknown>;
19
+ }
20
+ export declare function fieldObservation<TValue>(input: FieldObservationInput<TValue>): SurveyObservationInput;
@@ -0,0 +1,16 @@
1
+ import { buildObservation } from "./observation-helper.js";
2
+ export function fieldObservation(input) {
3
+ const representation = input.representation ?? "scalar";
4
+ return buildObservation({
5
+ ...input,
6
+ surveyMetadata: {
7
+ field: { representation },
8
+ },
9
+ defaultExcerpt: `${input.field}: ${valueSummary(input.value)}`,
10
+ });
11
+ }
12
+ function valueSummary(value) {
13
+ if (value === null || value === undefined)
14
+ return "<empty>";
15
+ return String(value);
16
+ }
@@ -2,3 +2,7 @@ export type { CandidateSetStatus, Candidate, CandidateSet, ClaimTarget, DerivedC
2
2
  export { SurveyInputBuilder } from "./builder.js";
3
3
  export type { SurveyClaimRecord, SurveyInputBuilderArgs, SurveyObservationInput } from "./builder.js";
4
4
  export { buildSurveyTrustInput } from "./to-surface.js";
5
+ export { fieldObservation } from "./field-observation.js";
6
+ export type { FieldObservationInput } from "./field-observation.js";
7
+ export { repeatedObservation } from "./repeated-observation.js";
8
+ export type { RepeatedObservationInput } from "./repeated-observation.js";
package/dist/src/index.js CHANGED
@@ -1,2 +1,4 @@
1
1
  export { SurveyInputBuilder } from "./builder.js";
2
2
  export { buildSurveyTrustInput } from "./to-surface.js";
3
+ export { fieldObservation } from "./field-observation.js";
4
+ export { repeatedObservation } from "./repeated-observation.js";
@@ -0,0 +1,21 @@
1
+ import type { SurveyObservationInput } from "./builder.js";
2
+ export interface BuildObservationInput<TValue> {
3
+ id: string;
4
+ field: string;
5
+ value: TValue;
6
+ rawSource: SurveyObservationInput["rawSource"];
7
+ extraction: Omit<SurveyObservationInput["extraction"], "target" | "value" | "excerpt"> & {
8
+ target?: string;
9
+ excerpt?: string | null;
10
+ };
11
+ reviewOutcome?: SurveyObservationInput["reviewOutcome"];
12
+ claim: Omit<SurveyObservationInput["claim"], "fieldOrBehavior" | "value"> & {
13
+ fieldOrBehavior?: string;
14
+ };
15
+ candidate?: SurveyObservationInput["candidate"];
16
+ candidateSet?: SurveyObservationInput["candidateSet"];
17
+ metadata?: Record<string, unknown>;
18
+ surveyMetadata: Record<string, unknown>;
19
+ defaultExcerpt: string;
20
+ }
21
+ export declare function buildObservation<TValue>(input: BuildObservationInput<TValue>): SurveyObservationInput;
@@ -0,0 +1,63 @@
1
+ export function buildObservation(input) {
2
+ return {
3
+ id: input.id,
4
+ rawSource: input.rawSource,
5
+ extraction: {
6
+ ...input.extraction,
7
+ target: input.extraction.target ?? input.field,
8
+ value: input.value,
9
+ excerpt: input.extraction.excerpt ?? input.defaultExcerpt,
10
+ },
11
+ candidate: input.candidate,
12
+ candidateSet: input.candidateSet,
13
+ reviewOutcome: input.reviewOutcome,
14
+ claim: {
15
+ ...input.claim,
16
+ fieldOrBehavior: input.claim.fieldOrBehavior ?? input.field,
17
+ value: input.value,
18
+ metadata: mergeObservationMetadata(input.claim.metadata, input.metadata, input.surveyMetadata),
19
+ },
20
+ };
21
+ }
22
+ function mergeObservationMetadata(claimMetadata, metadata, surveyMetadata) {
23
+ const claimSurvey = claimMetadata?.survey && isRecord(claimMetadata.survey) ? claimMetadata.survey : {};
24
+ const survey = metadata?.survey && isRecord(metadata.survey) ? metadata.survey : {};
25
+ return {
26
+ ...claimMetadata,
27
+ ...metadata,
28
+ survey: {
29
+ ...claimSurvey,
30
+ ...survey,
31
+ ...mergeNestedRecords(claimSurvey, survey, surveyMetadata),
32
+ },
33
+ };
34
+ }
35
+ function mergeNestedRecords(claimSurvey, survey, surveyMetadata) {
36
+ const merged = {};
37
+ const keys = new Set([
38
+ ...Object.keys(claimSurvey),
39
+ ...Object.keys(survey),
40
+ ...Object.keys(surveyMetadata),
41
+ ]);
42
+ for (const key of keys) {
43
+ const claimValue = claimSurvey[key];
44
+ const metadataValue = survey[key];
45
+ const helperValue = surveyMetadata[key];
46
+ if (isRecord(claimValue) && isRecord(metadataValue) && isRecord(helperValue)) {
47
+ merged[key] = { ...claimValue, ...metadataValue, ...helperValue };
48
+ }
49
+ else if (isRecord(claimValue) && isRecord(helperValue) && metadataValue === undefined) {
50
+ merged[key] = { ...claimValue, ...helperValue };
51
+ }
52
+ else if (isRecord(metadataValue) && isRecord(helperValue)) {
53
+ merged[key] = { ...metadataValue, ...helperValue };
54
+ }
55
+ else {
56
+ merged[key] = helperValue ?? metadataValue ?? claimValue;
57
+ }
58
+ }
59
+ return merged;
60
+ }
61
+ function isRecord(value) {
62
+ return typeof value === "object" && value !== null && !Array.isArray(value);
63
+ }
@@ -0,0 +1,20 @@
1
+ import type { SurveyObservationInput } from "./builder.js";
2
+ export interface RepeatedObservationInput<TItem> {
3
+ id: string;
4
+ field: string;
5
+ value: readonly TItem[];
6
+ rawSource: SurveyObservationInput["rawSource"];
7
+ extraction: Omit<SurveyObservationInput["extraction"], "target" | "value" | "excerpt"> & {
8
+ target?: string;
9
+ excerpt?: string | null;
10
+ };
11
+ reviewOutcome?: SurveyObservationInput["reviewOutcome"];
12
+ claim: Omit<SurveyObservationInput["claim"], "fieldOrBehavior" | "value"> & {
13
+ fieldOrBehavior?: string;
14
+ };
15
+ candidate?: SurveyObservationInput["candidate"];
16
+ candidateSet?: SurveyObservationInput["candidateSet"];
17
+ representation?: "aggregate-array";
18
+ metadata?: Record<string, unknown>;
19
+ }
20
+ export declare function repeatedObservation<TItem>(input: RepeatedObservationInput<TItem>): SurveyObservationInput;
@@ -0,0 +1,16 @@
1
+ import { buildObservation } from "./observation-helper.js";
2
+ export function repeatedObservation(input) {
3
+ const representation = input.representation ?? "aggregate-array";
4
+ const value = [...input.value];
5
+ return buildObservation({
6
+ ...input,
7
+ value,
8
+ surveyMetadata: {
9
+ repeated: {
10
+ representation,
11
+ itemCount: value.length,
12
+ },
13
+ },
14
+ defaultExcerpt: `${input.field}: ${value.length} item(s)`,
15
+ });
16
+ }
@@ -41,6 +41,7 @@ export function buildSurveyTrustInput(input) {
41
41
  metadata: {
42
42
  ...projection.metadata,
43
43
  survey: {
44
+ ...(isRecord(projection.metadata?.survey) ? projection.metadata.survey : {}),
44
45
  rawSourceId: rawSource.id,
45
46
  extractionId: extraction.id,
46
47
  candidateSetId: candidateSet.id,
@@ -213,3 +214,6 @@ function groupBy(items, getKey) {
213
214
  }
214
215
  return map;
215
216
  }
217
+ function isRecord(value) {
218
+ return typeof value === "object" && value !== null && !Array.isArray(value);
219
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kontourai/survey",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Producer-side source, extraction, candidate, and review contracts for projecting verified claims into Surface.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",