@kontourai/survey 0.1.3 → 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,71 @@ 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
+
67
132
  ## Repeated observations
68
133
 
69
134
  Use `repeatedObservation` when a producer wants to describe a repeated field or
@@ -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,5 +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";
5
7
  export { repeatedObservation } from "./repeated-observation.js";
6
8
  export type { RepeatedObservationInput } from "./repeated-observation.js";
package/dist/src/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { SurveyInputBuilder } from "./builder.js";
2
2
  export { buildSurveyTrustInput } from "./to-surface.js";
3
+ export { fieldObservation } from "./field-observation.js";
3
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
+ }
@@ -1,42 +1,16 @@
1
+ import { buildObservation } from "./observation-helper.js";
1
2
  export function repeatedObservation(input) {
2
3
  const representation = input.representation ?? "aggregate-array";
3
4
  const value = [...input.value];
4
- return {
5
- id: input.id,
6
- rawSource: input.rawSource,
7
- extraction: {
8
- ...input.extraction,
9
- target: input.extraction.target ?? input.field,
10
- value,
11
- excerpt: input.extraction.excerpt ?? `${input.field}: ${value.length} item(s)`,
12
- },
13
- candidate: input.candidate,
14
- candidateSet: input.candidateSet,
15
- reviewOutcome: input.reviewOutcome,
16
- claim: {
17
- ...input.claim,
18
- fieldOrBehavior: input.claim.fieldOrBehavior ?? input.field,
19
- value,
20
- metadata: repeatedMetadata(input.claim.metadata, input.metadata, representation, value.length),
21
- },
22
- };
23
- }
24
- function repeatedMetadata(claimMetadata, metadata, representation, itemCount) {
25
- const claimSurvey = claimMetadata?.survey && isRecord(claimMetadata.survey) ? claimMetadata.survey : {};
26
- const survey = metadata?.survey && isRecord(metadata.survey) ? metadata.survey : {};
27
- return {
28
- ...claimMetadata,
29
- ...metadata,
30
- survey: {
31
- ...claimSurvey,
32
- ...survey,
5
+ return buildObservation({
6
+ ...input,
7
+ value,
8
+ surveyMetadata: {
33
9
  repeated: {
34
10
  representation,
35
- itemCount,
11
+ itemCount: value.length,
36
12
  },
37
13
  },
38
- };
39
- }
40
- function isRecord(value) {
41
- return typeof value === "object" && value !== null && !Array.isArray(value);
14
+ defaultExcerpt: `${input.field}: ${value.length} item(s)`,
15
+ });
42
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kontourai/survey",
3
- "version": "0.1.3",
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",