deepadata-edm-sdk 0.8.8 → 0.8.10

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.
Files changed (50) hide show
  1. package/README.md +6 -0
  2. package/dist/assembler.d.ts +33 -4
  3. package/dist/assembler.d.ts.map +1 -1
  4. package/dist/assembler.js +146 -14
  5. package/dist/assembler.js.map +1 -1
  6. package/dist/conversation.d.ts +62 -0
  7. package/dist/conversation.d.ts.map +1 -0
  8. package/dist/conversation.js +96 -0
  9. package/dist/conversation.js.map +1 -0
  10. package/dist/extractors/generate-field-block.d.ts +44 -0
  11. package/dist/extractors/generate-field-block.d.ts.map +1 -0
  12. package/dist/extractors/generate-field-block.js +253 -0
  13. package/dist/extractors/generate-field-block.js.map +1 -0
  14. package/dist/extractors/kimi-extractor.d.ts +2 -2
  15. package/dist/extractors/kimi-extractor.d.ts.map +1 -1
  16. package/dist/extractors/kimi-extractor.js +21 -13
  17. package/dist/extractors/kimi-extractor.js.map +1 -1
  18. package/dist/extractors/llm-extractor.d.ts +24 -2
  19. package/dist/extractors/llm-extractor.d.ts.map +1 -1
  20. package/dist/extractors/llm-extractor.js +95 -56
  21. package/dist/extractors/llm-extractor.js.map +1 -1
  22. package/dist/extractors/openai-extractor.d.ts +2 -2
  23. package/dist/extractors/openai-extractor.d.ts.map +1 -1
  24. package/dist/extractors/openai-extractor.js +11 -6
  25. package/dist/extractors/openai-extractor.js.map +1 -1
  26. package/dist/extractors/output-sanitizer.d.ts +26 -0
  27. package/dist/extractors/output-sanitizer.d.ts.map +1 -0
  28. package/dist/extractors/output-sanitizer.js +129 -0
  29. package/dist/extractors/output-sanitizer.js.map +1 -0
  30. package/dist/extractors/profile-prompts.d.ts +2 -2
  31. package/dist/extractors/profile-prompts.d.ts.map +1 -1
  32. package/dist/extractors/profile-prompts.js +66 -41
  33. package/dist/extractors/profile-prompts.js.map +1 -1
  34. package/dist/extractors/stance-guard.d.ts +75 -0
  35. package/dist/extractors/stance-guard.d.ts.map +1 -0
  36. package/dist/extractors/stance-guard.js +171 -0
  37. package/dist/extractors/stance-guard.js.map +1 -0
  38. package/dist/index.d.ts +12 -4
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +12 -3
  41. package/dist/index.js.map +1 -1
  42. package/dist/schema/edm-schema.d.ts +42 -26
  43. package/dist/schema/edm-schema.d.ts.map +1 -1
  44. package/dist/schema/edm-schema.js +18 -3
  45. package/dist/schema/edm-schema.js.map +1 -1
  46. package/dist/schema/types.d.ts +33 -1
  47. package/dist/schema/types.d.ts.map +1 -1
  48. package/dist/schema/types.js +12 -1
  49. package/dist/schema/types.js.map +1 -1
  50. package/package.json +4 -1
@@ -0,0 +1,253 @@
1
+ /**
2
+ * JSON-Schema → prompt field-block generator (ADR-0030, amended — Step 6).
3
+ *
4
+ * Reads the PUBLISHED edm-spec package (the source of truth, ADR-0030 amended)
5
+ * rather than walking the SDK's own zod validator. The two extraction prompts
6
+ * (Full in llm-extractor.ts; Extended/Essential in profile-prompts.ts) each
7
+ * carry a hand-written JSON skeleton whose per-field `// CANONICAL:` / `// STRICT
8
+ * ENUM:` comments enumerate the field vocabulary. This generator emits that
9
+ * skeleton FROM the spec's JSON Schema fragments, so the vocabulary rendered in
10
+ * each comment is the one the spec enforces — drift between prompt, validator,
11
+ * and spec becomes impossible by construction.
12
+ *
13
+ * Phase A walked the local zod schema; this is the spec-sourced port. The OUTPUT
14
+ * FORMAT is unchanged — byte-for-byte identical to what Phase A emitted (and
15
+ * therefore to the hand-written skeletons it reproduces). Only the INPUT source
16
+ * moved: zod nodes → edm-spec JSON Schema.
17
+ *
18
+ * Classification maps directly from JSON Schema to the kinds the prompt renders:
19
+ * - "x-edm-canonical": [...] → canonical-enum (two-tier; free text accepted)
20
+ * - "enum": [...] → strict-enum (validator rejects non-members)
21
+ * - bare "type" string/number/boolean/array → free text / number / boolean / array
22
+ *
23
+ * Field MEMBERSHIP and ORDER per profile come from the composite profile schemas
24
+ * (edm-spec/schema/edm.v0.8.{essential,extended,full}.schema.json): which
25
+ * representational domains a profile includes, and — for inline domains — which
26
+ * fields, in which order. This is the role the SDK's LlmEssential/LlmExtended/
27
+ * LlmExtractedFields zod schemas used to play.
28
+ *
29
+ * Field DEFINITIONS (classification + canonical value lists) are always read from
30
+ * the fragments, never from the composite's inline copies, so the canonical
31
+ * vocabulary rendered in each comment is the one the fragment enforces.
32
+ */
33
+ import { readFileSync } from "node:fs";
34
+ import { createRequire } from "node:module";
35
+ const require = createRequire(import.meta.url);
36
+ const loadJson = (specPath) => JSON.parse(readFileSync(require.resolve(specPath), "utf8"));
37
+ // ---------------------------------------------------------------------------
38
+ // Profile / domain wiring
39
+ // ---------------------------------------------------------------------------
40
+ /** The LLM-extracted representational domains, in canonical (zod definition) order. */
41
+ const LLM_DOMAINS = ["core", "constellation", "milky_way", "gravity", "impulse"];
42
+ const FRAGMENT_SPEC = {
43
+ core: "edm-spec/schema/fragments/core.json",
44
+ constellation: "edm-spec/schema/fragments/constellation.json",
45
+ milky_way: "edm-spec/schema/fragments/milky_way.json",
46
+ gravity: "edm-spec/schema/fragments/gravity.json",
47
+ impulse: "edm-spec/schema/fragments/impulse.json",
48
+ };
49
+ const COMPOSITE_SPEC = {
50
+ essential: "edm-spec/schema/edm.v0.8.essential.schema.json",
51
+ extended: "edm-spec/schema/edm.v0.8.extended.schema.json",
52
+ full: "edm-spec/schema/edm.v0.8.full.schema.json",
53
+ };
54
+ const loadFragment = (domain) => loadJson(FRAGMENT_SPEC[domain]);
55
+ /**
56
+ * experiential_stance is a top-level extraction-only field — never sealed into
57
+ * the artifact body, so it appears in no fragment or composite schema. The SDK
58
+ * carries it as ExperientialStanceSchema (z.enum). Mirror it here as a strict
59
+ * enum so the generated block asks for it exactly as the current prompts do.
60
+ */
61
+ const EXPERIENTIAL_STANCE_DEF = {
62
+ enum: ["lived", "witnessed", "quoted_third_party", "assistant_generated", "hypothetical"],
63
+ };
64
+ /**
65
+ * Classify a JSON Schema field node into the kind the prompt comment renders.
66
+ *
67
+ * - "x-edm-canonical": [...] → canonical-enum (two-tier free text)
68
+ * - "enum": [...] → strict-enum (null stripped)
69
+ * - type number/integer → number
70
+ * - type boolean → boolean
71
+ * - type array → string-array
72
+ * - otherwise (string/null) → free text
73
+ */
74
+ export function classifyField(def) {
75
+ const canonical = def["x-edm-canonical"];
76
+ if (Array.isArray(canonical)) {
77
+ return { kind: "canonical-enum", enumValues: canonical };
78
+ }
79
+ if (Array.isArray(def.enum)) {
80
+ // JSON Schema enums carry a trailing `null` for nullable fields; zod models
81
+ // nullability with `.nullable()` and keeps it out of the enum, so strip it.
82
+ return { kind: "strict-enum", enumValues: def.enum.filter((v) => v !== null) };
83
+ }
84
+ const types = Array.isArray(def.type) ? def.type : [def.type];
85
+ if (types.includes("number") || types.includes("integer"))
86
+ return { kind: "number" };
87
+ if (types.includes("boolean"))
88
+ return { kind: "boolean" };
89
+ if (types.includes("array"))
90
+ return { kind: "string-array" };
91
+ return { kind: "string" };
92
+ }
93
+ // ---------------------------------------------------------------------------
94
+ // Presentation layer (OUTPUT_CONTRACT — hand-written, §4)
95
+ //
96
+ // Canonical comment conventions follow the Full prompt (the richest of the
97
+ // three current skeletons): STRICT/CANONICAL prefixes with explicit suffixes
98
+ // and per-field prose guidance for free-text fields. Enum VALUES are NEVER in
99
+ // these tables — they come from the fragment.
100
+ // ---------------------------------------------------------------------------
101
+ const STRICT_SUFFIX = " (pick ONE or null)";
102
+ const CANONICAL_SUFFIX = " (free text accepted if none fits)";
103
+ /** Per-field suffix overrides for strict enums (keyed `domain.field`). */
104
+ const SUFFIX_OVERRIDE = {
105
+ "constellation.narrative_archetype": " (pick ONE or null; lowercase)",
106
+ };
107
+ /** Extra prose appended after a canonical-enum comment. */
108
+ const CANONICAL_TRAILING = {
109
+ "constellation.arc_type": ". gratitude = moments of thankfulness, appreciation, acknowledging blessing; authenticity = feeling fully oneself, self-alignment, identity congruence",
110
+ };
111
+ /** Prose guidance for non-enum (free text / number / boolean / array) fields. */
112
+ const GUIDANCE = {
113
+ // core
114
+ "core.anchor": 'central theme (e.g., "dad\'s toolbox", "nana\'s traditions")',
115
+ "core.spark": 'what triggered the memory (e.g., "finding the cassette", "first snow")',
116
+ "core.wound": "The specific vulnerability, loss, or pain present — NOT generic labels like 'loss' or 'grief' but what exactly was lost or why it hurts. Examples: 'unlived travel dream', 'war silence never spoken', 'father died before I knew him', 'shame of not fitting in'. If no wound is present, use null.",
117
+ "core.fuel": 'what energized the experience (e.g., "shared laughter", "curiosity")',
118
+ "core.bridge": 'connection between past and present (e.g., "replaying old tape", "returning to the porch")',
119
+ "core.echo": 'what still resonates (e.g., "her laugh", "smell of oil", "city lights on water")',
120
+ "core.narrative": "3–5 sentences. REQUIRED: include ALL of the following — ≥1 concrete sensory detail (sight, sound, smell, texture), ≥1 temporal cue that anchors the memory in time, ≥1 symbolic callback that connects past to present. Write from the subject's perspective. Do not compress or summarise — give the memory space to breathe. Faithful and specific. Never generic.",
121
+ // constellation
122
+ "constellation.emotion_subtone": "2–4 short words (e.g., bittersweet, grateful) — free text array",
123
+ "constellation.higher_order_emotion": "free text: e.g., awe, forgiveness, pride, moral_elevation (or null)",
124
+ "constellation.meta_emotional_state": "free text: e.g., acceptance, confusion, curiosity (or null)",
125
+ "constellation.interpersonal_affect": "free text: e.g., warmth, openness, defensiveness (or null)",
126
+ "constellation.symbolic_anchor": "concrete object/place/ritual (or null)",
127
+ "constellation.identity_thread": "short sentence",
128
+ "constellation.expressed_insight": "brief insight explicitly stated by subject (extracted, not inferred)",
129
+ "constellation.transformational_pivot": "true if subject explicitly identifies this as life-changing",
130
+ "constellation.somatic_signature": 'bodily sensations explicitly described (e.g., "chest tightness", "warmth spreading") or null',
131
+ // milky_way
132
+ "milky_way.event_type": "e.g., family gathering, farewell, birthday (or null)",
133
+ "milky_way.location_context": "place from text or image (or null)",
134
+ "milky_way.associated_people": "names or roles (proper case allowed)",
135
+ "milky_way.tone_shift": "e.g., loss to gratitude (or null)",
136
+ // gravity
137
+ "gravity.emotional_weight": "0.0–1.0 (felt intensity IN THE MOMENT). Calibration: 0.9+ life-altering irreversible moments; 0.7-0.9 significant personal events with strong emotional response; 0.4-0.7 meaningful but routine emotional experiences; 0.1-0.4 mild passing emotional content",
138
+ "gravity.gravity_type": "short phrase (e.g., symbolic resonance)",
139
+ "gravity.recall_triggers": "sensory or symbolic cues (lowercase tokens)",
140
+ "gravity.retrieval_keys": "compact hooks (3–6 tokens recommended)",
141
+ "gravity.nearby_themes": "adjacent concepts",
142
+ "gravity.strength_score": "0.0–1.0 (how BOUND/STUCK this memory is)",
143
+ "gravity.resilience_markers": "1–3 (e.g., acceptance, optimism, continuity)",
144
+ // impulse
145
+ "impulse.primary_energy": "free text: e.g., curiosity, fear, compassion (or null; lowercase)",
146
+ // top-level
147
+ experiential_stance: "", // enum-driven; guidance unused
148
+ };
149
+ const PLACEHOLDER = {
150
+ "strict-enum": '""',
151
+ "canonical-enum": '""',
152
+ string: '""',
153
+ number: "0.0",
154
+ boolean: "false",
155
+ "string-array": "[]",
156
+ };
157
+ /** Build the `// ...` comment text for a field, or null if it has none. */
158
+ function commentFor(key, info) {
159
+ if (info.kind === "strict-enum") {
160
+ const suffix = SUFFIX_OVERRIDE[key] ?? STRICT_SUFFIX;
161
+ return `STRICT ENUM: ${(info.enumValues ?? []).join(" | ")}${suffix}`;
162
+ }
163
+ if (info.kind === "canonical-enum") {
164
+ const trailing = CANONICAL_TRAILING[key] ?? "";
165
+ return `CANONICAL: ${(info.enumValues ?? []).join(" | ")}${CANONICAL_SUFFIX}${trailing}`;
166
+ }
167
+ const g = GUIDANCE[key];
168
+ return g && g.length > 0 ? g : null;
169
+ }
170
+ /** Join lines, aligning each block's `//` to (max content length) + 1 space. */
171
+ function renderBlock(lines) {
172
+ const maxLen = lines.reduce((m, l) => Math.max(m, l.content.length), 0);
173
+ return lines.map((l) => {
174
+ if (l.comment === null)
175
+ return l.content;
176
+ const pad = " ".repeat(maxLen - l.content.length + 1);
177
+ return `${l.content}${pad}// ${l.comment}`;
178
+ });
179
+ }
180
+ /**
181
+ * Resolve a profile domain to its ordered `[fieldName, def]` pairs.
182
+ * Field names/order: inline composite properties when present, else fragment
183
+ * order. Field defs: always the fragment (source of truth), falling back to the
184
+ * inline composite copy only if the fragment lacks the field.
185
+ */
186
+ function domainFields(compositeProp, fragment) {
187
+ const fragProps = fragment.properties ?? {};
188
+ const inlineProps = compositeProp?.properties;
189
+ const names = inlineProps ? Object.keys(inlineProps) : Object.keys(fragProps);
190
+ return names.map((name) => [name, fragProps[name] ?? inlineProps?.[name] ?? {}]);
191
+ }
192
+ /**
193
+ * Generate the JSON field-block skeleton for a profile, from the edm-spec JSON
194
+ * Schema.
195
+ *
196
+ * Output shape (matches the current hand-written skeletons):
197
+ *
198
+ * {
199
+ * "experiential_stance": "", // STRICT ENUM: ...
200
+ * "core": {
201
+ * "anchor": "", // central theme ...
202
+ * ...
203
+ * },
204
+ * ...
205
+ * }
206
+ */
207
+ export function generateFieldBlock(profile) {
208
+ const compositeSpec = COMPOSITE_SPEC[profile];
209
+ if (!compositeSpec) {
210
+ throw new Error(`unknown profile "${profile}" (expected one of: ${Object.keys(COMPOSITE_SPEC).join(", ")})`);
211
+ }
212
+ const composite = loadJson(compositeSpec);
213
+ const props = composite.properties ?? {};
214
+ // Representational domains present in this profile, in canonical order.
215
+ const domains = LLM_DOMAINS.filter((d) => props[d]);
216
+ // Top-level: the experiential_stance scalar renders first and inline; domains
217
+ // render as nested blocks. (Mirrors the SDK's [...scalars, ...domains] order.)
218
+ const topLevelCount = 1 + domains.length;
219
+ let topIdx = 0;
220
+ const out = ["{"];
221
+ // experiential_stance (scalar)
222
+ {
223
+ const isLastTop = topIdx === topLevelCount - 1;
224
+ const topComma = isLastTop ? "" : ",";
225
+ const info = classifyField(EXPERIENTIAL_STANCE_DEF);
226
+ const comment = commentFor("experiential_stance", info);
227
+ const [rendered] = renderBlock([
228
+ { content: ` "experiential_stance": ${PLACEHOLDER[info.kind]}${topComma}`, comment },
229
+ ]);
230
+ out.push(rendered);
231
+ topIdx++;
232
+ }
233
+ for (const domain of domains) {
234
+ const isLastTop = topIdx === topLevelCount - 1;
235
+ const topComma = isLastTop ? "" : ",";
236
+ const fragment = loadFragment(domain);
237
+ const fields = domainFields(props[domain], fragment);
238
+ out.push(` "${domain}": {`);
239
+ const lines = fields.map(([field, def], idx) => {
240
+ const isLastField = idx === fields.length - 1;
241
+ const comma = isLastField ? "" : ",";
242
+ const info = classifyField(def);
243
+ const comment = commentFor(`${domain}.${field}`, info);
244
+ return { content: ` "${field}": ${PLACEHOLDER[info.kind]}${comma}`, comment };
245
+ });
246
+ out.push(...renderBlock(lines));
247
+ out.push(` }${topComma}`);
248
+ topIdx++;
249
+ }
250
+ out.push("}");
251
+ return out.join("\n");
252
+ }
253
+ //# sourceMappingURL=generate-field-block.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate-field-block.js","sourceRoot":"","sources":["../../src/extractors/generate-field-block.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,QAAQ,GAAG,CAAC,QAAgB,EAAkB,EAAE,CACpD,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAmB,CAAC;AAiBhF,8EAA8E;AAC9E,0BAA0B;AAC1B,8EAA8E;AAE9E,uFAAuF;AACvF,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,eAAe,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,CAAU,CAAC;AAG1F,MAAM,aAAa,GAA8B;IAC/C,IAAI,EAAE,qCA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,CAAC;IAED,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACd,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxB,CAAC"}
@@ -5,11 +5,11 @@
5
5
  */
6
6
  import OpenAI from "openai";
7
7
  import type { ExtractionInput, EdmProfile } from "../schema/types.js";
8
- import { type LlmExtractionResult } from "./llm-extractor.js";
8
+ import { type ExtractorCallOptions, type LlmExtractionResult } from "./llm-extractor.js";
9
9
  /**
10
10
  * Extract EDM fields from content using Kimi K2
11
11
  */
12
- export declare function extractWithKimi(client: OpenAI, input: ExtractionInput, model?: string, profile?: EdmProfile): Promise<LlmExtractionResult>;
12
+ export declare function extractWithKimi(client: OpenAI, input: ExtractionInput, model?: string, profile?: EdmProfile, options?: ExtractorCallOptions): Promise<LlmExtractionResult>;
13
13
  /**
14
14
  * Create a Kimi client using MoonshotAI's direct API
15
15
  * Falls back to OpenRouter if direct API key is not available
@@ -1 +1 @@
1
- {"version":3,"file":"kimi-extractor.d.ts","sourceRoot":"","sources":["../../src/extractors/kimi-extractor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,KAAK,EAAsB,eAAe,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE1F,OAAO,EAEL,KAAK,mBAAmB,EACzB,MAAM,oBAAoB,CAAC;AAkC5B;;GAEG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,eAAe,EACtB,KAAK,GAAE,MAA2B,EAClC,OAAO,GAAE,UAAmB,GAC3B,OAAO,CAAC,mBAAmB,CAAC,CAmF9B;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CA2BxD;AAED;;GAEG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAMvC"}
1
+ {"version":3,"file":"kimi-extractor.d.ts","sourceRoot":"","sources":["../../src/extractors/kimi-extractor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,KAAK,EAAsB,eAAe,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE1F,OAAO,EAIL,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACzB,MAAM,oBAAoB,CAAC;AAsC5B;;GAEG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,eAAe,EACtB,KAAK,GAAE,MAA2B,EAClC,OAAO,GAAE,UAAmB,EAC5B,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,mBAAmB,CAAC,CAwF9B;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CA2BxD;AAED;;GAEG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAMvC"}
@@ -5,7 +5,8 @@
5
5
  */
6
6
  import OpenAI from "openai";
7
7
  import { LlmExtractedFieldsSchema, LlmEssentialFieldsSchema, LlmExtendedFieldsSchema } from "../schema/edm-schema.js";
8
- import { EXTRACTION_SYSTEM_PROMPT, } from "./llm-extractor.js";
8
+ import { EXTRACTION_SYSTEM_PROMPT, defaultMaxTokens, prepareInputText, } from "./llm-extractor.js";
9
+ import { sanitizeLlmOutput } from "./output-sanitizer.js";
9
10
  /**
10
11
  * Get the appropriate schema for profile-specific validation
11
12
  */
@@ -22,10 +23,13 @@ function getProfileSchema(profile) {
22
23
  }
23
24
  import { getProfilePrompt, calculateProfileConfidence } from "./profile-prompts.js";
24
25
  /**
25
- * Default Kimi K2 model identifier
26
- * MoonshotAI exposes this via their OpenAI-compatible endpoint
26
+ * Default Kimi model identifier
27
+ * MoonshotAI exposes this via their OpenAI-compatible endpoint.
28
+ * kimi-k2-0711-preview was retired by Moonshot (404s as of 2026-06).
29
+ * Note: kimi-k2.5 is a thinking model — defaultMaxTokens() sizes the
30
+ * output budget accordingly.
27
31
  */
28
- const DEFAULT_KIMI_MODEL = "kimi-k2-0711-preview";
32
+ const DEFAULT_KIMI_MODEL = "kimi-k2.5";
29
33
  /**
30
34
  * Kimi API base URLs
31
35
  * - Direct: api.moonshot.cn or api.moonshot.ai (requires MOONSHOT_API_KEY or KIMI_API_KEY)
@@ -38,13 +42,14 @@ const OPENROUTER_KIMI_MODEL = "moonshotai/kimi-k2";
38
42
  /**
39
43
  * Extract EDM fields from content using Kimi K2
40
44
  */
41
- export async function extractWithKimi(client, input, model = DEFAULT_KIMI_MODEL, profile = "full") {
45
+ export async function extractWithKimi(client, input, model = DEFAULT_KIMI_MODEL, profile = "full", options = {}) {
42
46
  const userContent = [];
43
- // Add text content
44
- if (input.text) {
47
+ // Add text content (conversation inputs get source-material framing)
48
+ const text = prepareInputText(input);
49
+ if (text) {
45
50
  userContent.push({
46
51
  type: "text",
47
- text: input.text,
52
+ text,
48
53
  });
49
54
  }
50
55
  // Add image if provided (OpenAI-compatible format)
@@ -62,7 +67,7 @@ export async function extractWithKimi(client, input, model = DEFAULT_KIMI_MODEL,
62
67
  const systemPrompt = profilePrompt || EXTRACTION_SYSTEM_PROMPT;
63
68
  const response = await client.chat.completions.create({
64
69
  model,
65
- max_tokens: 4096,
70
+ max_tokens: options.maxTokens ?? defaultMaxTokens(model),
66
71
  messages: [
67
72
  {
68
73
  role: "system",
@@ -75,12 +80,12 @@ export async function extractWithKimi(client, input, model = DEFAULT_KIMI_MODEL,
75
80
  ],
76
81
  });
77
82
  // Extract text response
78
- const text = response.choices[0]?.message?.content;
79
- if (!text) {
83
+ const responseText = response.choices[0]?.message?.content;
84
+ if (!responseText) {
80
85
  throw new Error("No text response from Kimi K2");
81
86
  }
82
87
  // Parse JSON response (strip markdown code fences if present)
83
- let jsonText = text.trim();
88
+ let jsonText = responseText.trim();
84
89
  const fenceMatch = jsonText.match(/^```(?:json)?\s*\n?([\s\S]*?)\n?\s*```$/);
85
90
  if (fenceMatch?.[1]) {
86
91
  jsonText = fenceMatch[1].trim();
@@ -90,8 +95,11 @@ export async function extractWithKimi(client, input, model = DEFAULT_KIMI_MODEL,
90
95
  parsed = JSON.parse(jsonText);
91
96
  }
92
97
  catch {
93
- throw new Error(`Failed to parse Kimi response as JSON: ${text.slice(0, 200)}...`);
98
+ throw new Error(`Failed to parse Kimi response as JSON: ${responseText.slice(0, 200)}...`);
94
99
  }
100
+ // Sanitize before validation: clamp array caps, coerce invalid
101
+ // strict-enum values to null (prefer a null field over a dropped artifact)
102
+ sanitizeLlmOutput(parsed);
95
103
  // Validate against profile-specific schema
96
104
  const schema = getProfileSchema(profile);
97
105
  const result = schema.safeParse(parsed);
@@ -1 +1 @@
1
- {"version":3,"file":"kimi-extractor.js","sourceRoot":"","sources":["../../src/extractors/kimi-extractor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAG5B,OAAO,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AACtH,OAAO,EACL,wBAAwB,GAEzB,MAAM,oBAAoB,CAAC;AAE5B;;GAEG;AACH,SAAS,gBAAgB,CAAC,OAAmB;IAC3C,QAAQ,OAAO,EAAE,CAAC;QAChB,KAAK,WAAW;YACd,OAAO,wBAAwB,CAAC;QAClC,KAAK,UAAU;YACb,OAAO,uBAAuB,CAAC;QACjC,KAAK,MAAM,CAAC;QACZ;YACE,OAAO,wBAAwB,CAAC;IACpC,CAAC;AACH,CAAC;AACD,OAAO,EAAE,gBAAgB,EAAE,0BAA0B,EAAE,MAAM,sBAAsB,CAAC;AAEpF;;;GAGG;AACH,MAAM,kBAAkB,GAAG,sBAAsB,CAAC;AAElD;;;;;GAKG;AACH,MAAM,qBAAqB,GAAG,4BAA4B,CAAC;AAC3D,MAAM,mBAAmB,GAAG,8BAA8B,CAAC;AAC3D,MAAM,qBAAqB,GAAG,oBAAoB,CAAC;AAEnD;;GAEG;AACH,MAAM,C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G,CAAC,oBAAoB,CAAC,CAAC;IACxD,IAAI,aAAa,EAAE,CAAC;QAClB,OAAO,IAAI,MAAM,CAAC;YAChB,MAAM,EAAE,aAAa;YACrB,OAAO,EAAE,mBAAmB;YAC5B,cAAc,EAAE;gBACd,cAAc,EAAE,uBAAuB;gBACvC,SAAS,EAAE,0BAA0B;aACtC;SACF,CAAC,CAAC;IACL,CAAC;IAED,MAAM,IAAI,KAAK,CACb,2GAA2G,CAC5G,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc;IAC5B,kDAAkD;IAClD,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,CAAC;QAC1G,OAAO,qBAAqB,CAAC;IAC/B,CAAC;IACD,OAAO,kBAAkB,CAAC;AAC5B,CAAC"}
@@ -5,11 +5,29 @@
5
5
  */
6
6
  import Anthropic from "@anthropic-ai/sdk";
7
7
  import type { LlmExtractedFields, ExtractionInput, EdmProfile } from "../schema/types.js";
8
+ /**
9
+ * Per-extractor call options.
10
+ */
11
+ export interface ExtractorCallOptions {
12
+ /** Output token budget; defaults via defaultMaxTokens(model) */
13
+ maxTokens?: number;
14
+ }
15
+ /** Default output budget for non-thinking models */
16
+ export declare const DEFAULT_MAX_TOKENS = 4096;
17
+ /**
18
+ * Default output budget for thinking models, whose reasoning tokens count
19
+ * against max_tokens. 4096 silently truncated extraction JSON on exactly
20
+ * the most emotionally dense inputs (archive-sample run, 2026-06-10).
21
+ */
22
+ export declare const THINKING_MODEL_MAX_TOKENS = 16384;
23
+ export declare function defaultMaxTokens(model: string): number;
24
+ /** Apply conversation framing when the input declares transcript content */
25
+ export declare function prepareInputText(input: ExtractionInput): string | undefined;
8
26
  /**
9
27
  * System prompt for EDM extraction
10
28
  * See CHANGELOG.md for version-specific schema changes
11
29
  */
12
- export declare const EXTRACTION_SYSTEM_PROMPT = "\nYou classify emotionally rich memories into a JSON object. Input may include text and an image.\n\nRules\n- Fuse text + image. Treat text as primary; use image only to add grounded specifics (place, event, symbols, people).\n- Keep fields to single words or short phrases (1\u20133 words). Only \"narrative\" is multi-sentence (3\u20135).\n- No invention. If not supported by input, use null.\n- Always include every top-level key and sub-key from the schema, even if the value is null or an empty array.\n- Do not omit fields; if unknown, return null.\n- Output JSON only \u2014 no commentary, markdown, or extra text.\n- If motivation is ambiguous, choose the most conservative option (e.g., \"curiosity\" vs \"fear\") or return null.\n\nCRITICAL: Enum Field Constraints\n- Many fields below have CANONICAL values \u2014 preferred values for cross-artifact comparability.\n- Use canonical values where they fit. If no canonical value accurately represents the content, use the most accurate free-text term. Accuracy takes precedence over canonical conformance.\n- Cross-contamination warning: Each enum field has its own distinct value set. Do not use values from one field in another.\n Example: \"milestone\" is valid for memory_type but NOT for narrative_arc.\n Example: \"confront\" is valid for both drive_state and coping_style - check which field you're populating.\n- emotion_primary, narrative_arc, relational_dynamics, and arc_type accept free text if no canonical value fits.\n- arc_type canonical values use underscores not spaces. Use moral_awakening not moral awakening. Canonical values must match exactly as listed \u2014 no spaces, no variations.\n\nNormalization (very important)\n- Emit lowercase for all string fields except proper names in arrays like associated_people.\n- For array fields (emotion_subtone, recall_triggers, retrieval_keys, nearby_themes, resilience_markers, associated_people):\n \u2022 use short tokens/phrases without punctuation;\n \u2022 avoid duplicates;\n \u2022 prefer singular nouns where reasonable (\"tradition\" not \"traditions\").\n- Never put boolean-like strings (\"true\"/\"false\") into fields that are boolean; use real booleans.\n\nSchema\n{\n \"core\": {\n \"anchor\": \"\", // central theme (e.g., \"dad's toolbox\", \"nana's traditions\")\n \"spark\": \"\", // what triggered the memory (e.g., \"finding the cassette\", \"first snow\")\n \"wound\": \"\", // The specific vulnerability, loss, or pain present \u2014 NOT generic labels like 'loss' or 'grief' but what exactly was lost or why it hurts. Examples: 'unlived travel dream', 'war silence never spoken', 'father died before I knew him', 'shame of not fitting in'. If no wound is present, use null.\n \"fuel\": \"\", // what energized the experience (e.g., \"shared laughter\", \"curiosity\")\n \"bridge\": \"\", // connection between past and present (e.g., \"replaying old tape\", \"returning to the porch\")\n \"echo\": \"\", // what still resonates (e.g., \"her laugh\", \"smell of oil\", \"city lights on water\")\n \"narrative\": \"\" // 3\u20135 sentences. REQUIRED: include ALL of the following \u2014 \u22651 concrete sensory detail (sight, sound, smell, texture), \u22651 temporal cue that anchors the memory in time, \u22651 symbolic callback that connects past to present. Write from the subject's perspective. Do not compress or summarise \u2014 give the memory space to breathe. Faithful and specific. Never generic.\n },\n \"constellation\": {\n \"emotion_primary\": \"\", // CANONICAL: joy | sadness | fear | anger | wonder | peace | tenderness | reverence | pride | anxiety | gratitude | longing | hope | shame | disappointment | relief | frustration (free text accepted if none fits)\n \"emotion_subtone\": [], // 2\u20134 short words (e.g., bittersweet, grateful) \u2014 free text array\n \"higher_order_emotion\": \"\", // free text: e.g., awe, forgiveness, pride, moral_elevation (or null)\n \"meta_emotional_state\": \"\", // free text: e.g., acceptance, confusion, curiosity (or null)\n \"interpersonal_affect\": \"\", // free text: e.g., warmth, openness, defensiveness (or null)\n \"narrative_arc\": \"\", // CANONICAL: overcoming | transformation | connection | reflection | closure | loss | confrontation (free text accepted if none fits)\n \"relational_dynamics\": \"\", // CANONICAL: parent_child | grandparent_grandchild | romantic_partnership | couple | sibling_bond | family | friendship | friend | companionship | colleague | mentorship | reunion | community_ritual | grief | self_reflection | professional | therapeutic | service | adversarial (free text accepted if none fits)\n \"temporal_context\": \"\", // STRICT ENUM: childhood | early_adulthood | midlife | late_life | recent | future | timeless (pick ONE or null)\n \"memory_type\": \"\", // STRICT ENUM: legacy_artifact | fleeting_moment | milestone | reflection | formative_experience (pick ONE or null)\n \"media_format\": \"\", // photo, video, audio, text, photo_with_story (or null)\n \"narrative_archetype\": \"\", // STRICT ENUM: hero | caregiver | seeker | sage | lover | outlaw | innocent | orphan | magician | creator | everyman | jester | ruler | mentor (pick ONE or null; lowercase)\n \"symbolic_anchor\": \"\", // concrete object/place/ritual (or null)\n \"relational_perspective\": \"\", // STRICT ENUM: self | partner | family | friends | community | humanity (pick ONE or null)\n \"temporal_rhythm\": \"\", // STRICT ENUM: still | sudden | rising | fading | recurring | spiraling | dragging | suspended | looping | cyclic (pick ONE or null)\n \"identity_thread\": \"\", // short sentence\n \"expressed_insight\": \"\", // brief insight explicitly stated by subject (extracted, not inferred)\n \"transformational_pivot\": false, // true if subject explicitly identifies this as life-changing\n \"somatic_signature\": \"\", // bodily sensations explicitly described (e.g., \"chest tightness\", \"warmth spreading\") or null\n \"arc_type\": \"\" // CANONICAL: betrayal | liberation | grief | discovery | resistance | bond | moral_awakening | transformation | reconciliation | reckoning | threshold | exile | gratitude | authenticity (free text accepted if none fits). gratitude = moments of thankfulness, appreciation, acknowledging blessing; authenticity = feeling fully oneself, self-alignment, identity congruence\n },\n \"milky_way\": {\n \"event_type\": \"\", // e.g., family gathering, farewell, birthday (or null)\n \"location_context\": \"\", // place from text or image (or null)\n \"associated_people\": [], // names or roles (proper case allowed)\n \"visibility_context\": \"\", // STRICT ENUM: private | family_only | shared_publicly (pick ONE or null)\n \"tone_shift\": \"\" // e.g., loss to gratitude (or null)\n },\n \"gravity\": {\n \"emotional_weight\": 0.0, // 0.0\u20131.0 (felt intensity IN THE MOMENT). Calibration: 0.9+ life-altering irreversible moments; 0.7-0.9 significant personal events with strong emotional response; 0.4-0.7 meaningful but routine emotional experiences; 0.1-0.4 mild passing emotional content\n \"emotional_density\": \"\", // STRICT ENUM: low | medium | high (pick ONE or null)\n \"valence\": \"\", // STRICT ENUM: positive | negative | mixed (pick ONE or null)\n \"viscosity\": \"\", // STRICT ENUM: low | medium | high | enduring | fluid (pick ONE or null)\n \"gravity_type\": \"\", // short phrase (e.g., symbolic resonance)\n \"tether_type\": \"\", // STRICT ENUM: person | symbol | event | place | ritual | object | tradition | identity | self (pick ONE or null)\n \"recall_triggers\": [], // sensory or symbolic cues (lowercase tokens)\n \"retrieval_keys\": [], // compact hooks (3\u20136 tokens recommended)\n \"nearby_themes\": [], // adjacent concepts\n \"recurrence_pattern\": \"\", // STRICT ENUM: cyclical | isolated | chronic | emerging (pick ONE or null)\n \"strength_score\": 0.0, // 0.0\u20131.0 (how BOUND/STUCK this memory is)\n \"temporal_decay\": \"\", // STRICT ENUM: fast | moderate | slow (pick ONE or null)\n \"resilience_markers\": [], // 1\u20133 (e.g., acceptance, optimism, continuity)\n \"adaptation_trajectory\": \"\" // STRICT ENUM: improving | stable | declining | integrative | emerging (pick ONE or null)\n },\n \"impulse\": {\n \"primary_energy\": \"\", // free text: e.g., curiosity, fear, compassion (or null; lowercase)\n \"drive_state\": \"\", // STRICT ENUM: explore | approach | avoid | repair | persevere | share | confront | protect | process (pick ONE or null)\n \"motivational_orientation\": \"\", // STRICT ENUM: belonging | safety | mastery | meaning | autonomy (pick ONE or null)\n \"temporal_focus\": \"\", // STRICT ENUM: past | present | future (pick ONE or null)\n \"directionality\": \"\", // STRICT ENUM: inward | outward | transcendent (pick ONE or null)\n \"social_visibility\": \"\", // STRICT ENUM: private | relational | collective (pick ONE or null)\n \"urgency\": \"\", // STRICT ENUM: calm | elevated | pressing | acute (pick ONE or null)\n \"risk_posture\": \"\", // STRICT ENUM: cautious | balanced | bold (pick ONE or null)\n \"agency_level\": \"\", // STRICT ENUM: low | medium | high (pick ONE or null)\n \"regulation_state\": \"\", // STRICT ENUM: regulated | wavering | dysregulated (pick ONE or null)\n \"attachment_style\": \"\", // STRICT ENUM: secure | anxious | avoidant | disorganized (pick ONE or null)\n \"coping_style\": \"\" // STRICT ENUM: reframe_meaning | seek_support | distract | ritualize | confront | detach | process (pick ONE or null)\n }\n\n // Calibration \u2014 Impulse (helps apply the fields consistently)\n // - temporal_focus: past (reminisce), present (here-and-now coping), future (plans/longing).\n // - directionality: inward (self-processing), outward (toward others), transcendent (beyond self).\n // - social_visibility: private (to self or 1:1), relational (friends/family), collective (community-wide).\n // - If uncertain, choose the most conservative option or null.\n\n // CROSS-CONTAMINATION DISAMBIGUATION (read carefully)\n //\n // temporal_rhythm vs urgency:\n // - temporal_rhythm describes the CADENCE or PACE of time in the memory experience\n // (still, sudden, rising, fading, recurring, spiraling, dragging, suspended, looping, cyclic)\n // - urgency describes the INTENSITY of motivational pressure RIGHT NOW\n // (calm, elevated, pressing, acute)\n // - \"pressing\" belongs ONLY in urgency, NEVER in temporal_rhythm\n //\n // temporal_rhythm vs viscosity:\n // - temporal_rhythm is about TIME MOVEMENT in the memory\n // - viscosity is about EMOTIONAL PERSISTENCE over time\n // (low=fleeting, medium=moderate, high=sticky, enduring=long-lasting, fluid=changeable)\n // - \"enduring\" belongs ONLY in viscosity, NEVER in temporal_rhythm\n //\n // relational_dynamics vs relational_perspective:\n // - relational_dynamics: the TYPE of relationship (parent_child, friendship, mentorship, etc.)\n // - relational_perspective: WHOSE viewpoint the narrative is told from (self, partner, family, etc.)\n // - \"family\" can appear in BOTH fields with different meanings\n //\n // drive_state vs coping_style:\n // - drive_state: the MOTIVATIONAL direction (explore, approach, avoid, confront, etc.)\n // - coping_style: the STRATEGY for managing emotions (reframe_meaning, seek_support, confront, etc.)\n // - \"confront\" is valid in BOTH - use drive_state for action impulse, coping_style for emotion management\n //\n // emotion_primary (STRICT ENUM) vs higher_order_emotion (free text):\n // - emotion_primary MUST be one of the 14 listed values ONLY\n // - Do NOT put free-text emotions like \"compassion\", \"reflection\", \"frustration\" in emotion_primary\n // - Use higher_order_emotion for complex emotions not in the primary list\n //\n // narrative_arc (CRITICAL - common error):\n // - Describes the STORY TRAJECTORY (overcoming, transformation, connection, reflection, closure)\n // - \"confrontation\" is NOT a valid arc \u2014 it describes an event/scene, not a trajectory\n // - If the story involves confronting something, use \"overcoming\" (challenge faced and resolved)\n // or \"transformation\" (fundamental change through conflict)\n // - \"confront\" belongs in drive_state or coping_style, NOT in narrative_arc\n //\n // emotional_weight vs strength_score (CRITICAL - different concepts):\n // - emotional_weight: The felt INTENSITY of the experience in the moment.\n // A heated argument = high weight (0.8). A routine check-in = low weight (0.2).\n // - strength_score: How BOUND/STUCK this memory is \u2014 through association, ritual, retelling, or identity.\n // A childhood memory retold for decades = high strength (0.9) even if emotional weight was moderate.\n // A customer complaint = may have high weight (0.8) but low strength (0.3) \u2014 intense but fades quickly.\n // - These should NOT always correlate.\n // Ask: \"How heavy does this feel RIGHT NOW?\" (weight) vs \"How stuck/persistent is this memory?\" (strength)\n //\n // SYNONYM CORRECTIONS (use the canonical form):\n // - drive_state: Use \"process\" NOT \"reflect\". The enum value is \"process\" for internal processing/reflection.\n // - narrative_archetype: Use \"caregiver\" NOT \"caretaker\". The Jungian archetype label is \"caregiver\".\n}\n";
30
+ export declare const EXTRACTION_SYSTEM_PROMPT = "\nYou classify emotionally rich memories into a JSON object. Input may include text and an image.\n\nRules\n- Fuse text + image. Treat text as primary; use image only to add grounded specifics (place, event, symbols, people).\n- Keep fields to single words or short phrases (1\u20133 words). Only \"narrative\" is multi-sentence (3\u20135).\n- No invention. If not supported by input, use null.\n- Always include every top-level key and sub-key from the schema, even if the value is null or an empty array.\n- Do not omit fields; if unknown, return null.\n- Output JSON only \u2014 no commentary, markdown, or extra text.\n- If motivation is ambiguous, choose the most conservative option (e.g., \"curiosity\" vs \"fear\") or return null.\n\nSUBJECT ANCHORING (critical)\n- The SUBJECT is the person this artifact will belong to. In a chat transcript the SUBJECT is the USER speaker; ASSISTANT text is context only, never a source of the subject's experience.\n- Score every field relative to the SUBJECT, not the passage. emotional_weight measures what this content meant TO THE SUBJECT \u2014 not how vivid, dramatic, or emotionally rich the text itself is.\n- Routine work content (debugging, drafting, planning, logistics) is 0.1\u20130.4 even when the subject expresses momentary relief or frustration. Reserve 0.7+ for events with personal stakes the subject states or plainly carries. Do not invent somatic or emotional detail the subject never expressed.\n- transformational_pivot is true ONLY if the subject explicitly marks the experience as life-changing. Finishing a task, fixing a bug, or shipping a feature is not a transformational pivot.\n\nEXPERIENTIAL STANCE (critical)\nClassify whose experience the emotionally salient material is, in the top-level \"experiential_stance\" key:\n- \"lived\" \u2014 the subject's own first-hand experience\n- \"witnessed\" \u2014 events the subject personally witnessed or is directly affected by (a loved one's death, a family crisis)\n- \"quoted_third_party\" \u2014 someone else's story the subject quoted, pasted, or retold without being a participant (an article, test data, a stranger's anecdote)\n- \"assistant_generated\" \u2014 fiction, examples, or anecdotes produced by the assistant, not reported by the subject\n- \"hypothetical\" \u2014 imagined scenarios, drafts about invented people, role-play\nIf the stance is quoted_third_party, assistant_generated, or hypothetical: do NOT encode that material into wound, identity_thread, expressed_insight, somatic_signature, transformational_pivot, the impulse domain, or high emotional_weight \u2014 those fields describe the SUBJECT. Extract only what the content reveals about the subject themselves (e.g. why they engaged with it), or return null fields with low weight.\n\nCRITICAL: Enum Field Constraints\n- Many fields below have CANONICAL values \u2014 preferred values for cross-artifact comparability.\n- Use canonical values where they fit. If no canonical value accurately represents the content, use the most accurate free-text term. Accuracy takes precedence over canonical conformance.\n- Cross-contamination warning: Each enum field has its own distinct value set. Do not use values from one field in another.\n Example: \"milestone\" is valid for memory_type but NOT for narrative_arc.\n Example: \"confront\" is valid for both drive_state and coping_style - check which field you're populating.\n- emotion_primary, narrative_arc, relational_dynamics, and arc_type accept free text if no canonical value fits.\n- arc_type canonical values use underscores not spaces. Use moral_awakening not moral awakening. Canonical values must match exactly as listed \u2014 no spaces, no variations.\n\nNormalization (very important)\n- Emit lowercase for all string fields except proper names in arrays like associated_people.\n- For array fields (emotion_subtone, recall_triggers, retrieval_keys, nearby_themes, resilience_markers, associated_people):\n \u2022 use short tokens/phrases without punctuation;\n \u2022 avoid duplicates;\n \u2022 prefer singular nouns where reasonable (\"tradition\" not \"traditions\").\n- Never put boolean-like strings (\"true\"/\"false\") into fields that are boolean; use real booleans.\n\nSchema\n{\n \"experiential_stance\": \"\", // STRICT ENUM: lived | witnessed | quoted_third_party | assistant_generated | hypothetical (pick ONE or null)\n \"core\": {\n \"anchor\": \"\", // central theme (e.g., \"dad's toolbox\", \"nana's traditions\")\n \"spark\": \"\", // what triggered the memory (e.g., \"finding the cassette\", \"first snow\")\n \"wound\": \"\", // The specific vulnerability, loss, or pain present \u2014 NOT generic labels like 'loss' or 'grief' but what exactly was lost or why it hurts. Examples: 'unlived travel dream', 'war silence never spoken', 'father died before I knew him', 'shame of not fitting in'. If no wound is present, use null.\n \"fuel\": \"\", // what energized the experience (e.g., \"shared laughter\", \"curiosity\")\n \"bridge\": \"\", // connection between past and present (e.g., \"replaying old tape\", \"returning to the porch\")\n \"echo\": \"\", // what still resonates (e.g., \"her laugh\", \"smell of oil\", \"city lights on water\")\n \"narrative\": \"\" // 3\u20135 sentences. REQUIRED: include ALL of the following \u2014 \u22651 concrete sensory detail (sight, sound, smell, texture), \u22651 temporal cue that anchors the memory in time, \u22651 symbolic callback that connects past to present. Write from the subject's perspective. Do not compress or summarise \u2014 give the memory space to breathe. Faithful and specific. Never generic.\n },\n \"constellation\": {\n \"emotion_primary\": \"\", // CANONICAL: joy | sadness | fear | anger | wonder | peace | tenderness | reverence | pride | anxiety | gratitude | longing | hope | shame | disappointment | relief | frustration (free text accepted if none fits)\n \"emotion_subtone\": [], // 2\u20134 short words (e.g., bittersweet, grateful) \u2014 free text array\n \"higher_order_emotion\": \"\", // free text: e.g., awe, forgiveness, pride, moral_elevation (or null)\n \"meta_emotional_state\": \"\", // free text: e.g., acceptance, confusion, curiosity (or null)\n \"interpersonal_affect\": \"\", // free text: e.g., warmth, openness, defensiveness (or null)\n \"narrative_arc\": \"\", // CANONICAL: overcoming | transformation | connection | reflection | closure | loss | confrontation (free text accepted if none fits)\n \"relational_dynamics\": \"\", // CANONICAL: parent_child | grandparent_grandchild | romantic_partnership | couple | sibling_bond | family | friendship | friend | companionship | colleague | mentorship | reunion | community_ritual | grief | self_reflection | professional | therapeutic | service | adversarial (free text accepted if none fits)\n \"temporal_context\": \"\", // STRICT ENUM: childhood | early_adulthood | midlife | late_life | recent | future | timeless (pick ONE or null)\n \"memory_type\": \"\", // STRICT ENUM: legacy_artifact | fleeting_moment | milestone | reflection | formative_experience (pick ONE or null)\n \"media_format\": \"\", // STRICT ENUM: photo | video | audio | text | photo_with_story (pick ONE or null)\n \"narrative_archetype\": \"\", // STRICT ENUM: hero | caregiver | seeker | sage | lover | outlaw | innocent | magician | creator | everyman | jester | ruler (pick ONE or null; lowercase)\n \"symbolic_anchor\": \"\", // concrete object/place/ritual (or null)\n \"relational_perspective\": \"\", // STRICT ENUM: self | partner | family | friends | community | humanity (pick ONE or null)\n \"temporal_rhythm\": \"\", // STRICT ENUM: still | sudden | rising | fading | recurring | spiraling | dragging | suspended | looping | cyclic (pick ONE or null)\n \"identity_thread\": \"\", // short sentence\n \"expressed_insight\": \"\", // brief insight explicitly stated by subject (extracted, not inferred)\n \"transformational_pivot\": false, // true if subject explicitly identifies this as life-changing\n \"somatic_signature\": \"\", // bodily sensations explicitly described (e.g., \"chest tightness\", \"warmth spreading\") or null\n \"arc_type\": \"\" // CANONICAL: betrayal | liberation | grief | discovery | resistance | bond | moral_awakening | transformation | reconciliation | reckoning | threshold | exile | gratitude | authenticity (free text accepted if none fits). gratitude = moments of thankfulness, appreciation, acknowledging blessing; authenticity = feeling fully oneself, self-alignment, identity congruence\n },\n \"milky_way\": {\n \"event_type\": \"\", // e.g., family gathering, farewell, birthday (or null)\n \"location_context\": \"\", // place from text or image (or null)\n \"associated_people\": [], // names or roles (proper case allowed)\n \"visibility_context\": \"\", // STRICT ENUM: private | family_only | shared_publicly (pick ONE or null)\n \"tone_shift\": \"\" // e.g., loss to gratitude (or null)\n },\n \"gravity\": {\n \"emotional_weight\": 0.0, // 0.0\u20131.0 (felt intensity IN THE MOMENT). Calibration: 0.9+ life-altering irreversible moments; 0.7-0.9 significant personal events with strong emotional response; 0.4-0.7 meaningful but routine emotional experiences; 0.1-0.4 mild passing emotional content\n \"emotional_density\": \"\", // STRICT ENUM: low | medium | high (pick ONE or null)\n \"valence\": \"\", // STRICT ENUM: positive | negative | mixed (pick ONE or null)\n \"viscosity\": \"\", // STRICT ENUM: low | medium | high | enduring | fluid (pick ONE or null)\n \"gravity_type\": \"\", // short phrase (e.g., symbolic resonance)\n \"tether_type\": \"\", // CANONICAL: person | symbol | event | place | ritual | object | tradition | identity | self (free text accepted if none fits)\n \"recall_triggers\": [], // sensory or symbolic cues (lowercase tokens)\n \"retrieval_keys\": [], // compact hooks (3\u20136 tokens recommended)\n \"nearby_themes\": [], // adjacent concepts\n \"recurrence_pattern\": \"\", // CANONICAL: cyclical | isolated | chronic | emerging (free text accepted if none fits)\n \"strength_score\": 0.0, // 0.0\u20131.0 (how BOUND/STUCK this memory is)\n \"temporal_decay\": \"\", // STRICT ENUM: fast | moderate | slow (pick ONE or null)\n \"resilience_markers\": [], // 1\u20133 (e.g., acceptance, optimism, continuity)\n \"adaptation_trajectory\": \"\" // STRICT ENUM: improving | stable | declining | integrative | emerging (pick ONE or null)\n },\n \"impulse\": {\n \"primary_energy\": \"\", // free text: e.g., curiosity, fear, compassion (or null; lowercase)\n \"drive_state\": \"\", // STRICT ENUM: explore | approach | avoid | repair | persevere | share | confront | protect | process (pick ONE or null)\n \"motivational_orientation\": \"\", // STRICT ENUM: belonging | safety | mastery | meaning | autonomy | authenticity (pick ONE or null)\n \"temporal_focus\": \"\", // STRICT ENUM: past | present | future (pick ONE or null)\n \"directionality\": \"\", // STRICT ENUM: inward | outward | transcendent (pick ONE or null)\n \"social_visibility\": \"\", // STRICT ENUM: private | relational | collective (pick ONE or null)\n \"urgency\": \"\", // STRICT ENUM: calm | elevated | pressing | acute (pick ONE or null)\n \"risk_posture\": \"\", // STRICT ENUM: cautious | balanced | bold (pick ONE or null)\n \"agency_level\": \"\", // STRICT ENUM: low | medium | high (pick ONE or null)\n \"regulation_state\": \"\", // STRICT ENUM: regulated | wavering | dysregulated (pick ONE or null)\n \"attachment_style\": \"\", // STRICT ENUM: secure | anxious | avoidant | disorganized (pick ONE or null)\n \"coping_style\": \"\" // CANONICAL: reframe_meaning | seek_support | distract | ritualize | confront | detach | process (free text accepted if none fits)\n }\n}\n\n // Calibration \u2014 Impulse (helps apply the fields consistently)\n // - temporal_focus: past (reminisce), present (here-and-now coping), future (plans/longing).\n // - directionality: inward (self-processing), outward (toward others), transcendent (beyond self).\n // - social_visibility: private (to self or 1:1), relational (friends/family), collective (community-wide).\n // - If uncertain, choose the most conservative option or null.\n\n // CROSS-CONTAMINATION DISAMBIGUATION (read carefully)\n //\n // temporal_rhythm vs urgency:\n // - temporal_rhythm describes the CADENCE or PACE of time in the memory experience\n // (still, sudden, rising, fading, recurring, spiraling, dragging, suspended, looping, cyclic)\n // - urgency describes the INTENSITY of motivational pressure RIGHT NOW\n // (calm, elevated, pressing, acute)\n // - \"pressing\" belongs ONLY in urgency, NEVER in temporal_rhythm\n //\n // temporal_rhythm vs viscosity:\n // - temporal_rhythm is about TIME MOVEMENT in the memory\n // - viscosity is about EMOTIONAL PERSISTENCE over time\n // (low=fleeting, medium=moderate, high=sticky, enduring=long-lasting, fluid=changeable)\n // - \"enduring\" belongs ONLY in viscosity, NEVER in temporal_rhythm\n //\n // relational_dynamics vs relational_perspective:\n // - relational_dynamics: the TYPE of relationship (parent_child, friendship, mentorship, etc.)\n // - relational_perspective: WHOSE viewpoint the narrative is told from (self, partner, family, etc.)\n // - \"family\" can appear in BOTH fields with different meanings\n //\n // drive_state vs coping_style:\n // - drive_state: the MOTIVATIONAL direction (explore, approach, avoid, confront, etc.)\n // - coping_style: the STRATEGY for managing emotions (reframe_meaning, seek_support, confront, etc.)\n // - \"confront\" is valid in BOTH - use drive_state for action impulse, coping_style for emotion management\n //\n // emotion_primary (two-tier free text) vs higher_order_emotion (free text):\n // - emotion_primary: prefer a canonical value when one fits; otherwise use the most accurate\n // free-text term (accuracy takes precedence over canonical conformance)\n // - Use higher_order_emotion for complex/second-order emotions layered on the primary feeling\n //\n // narrative_arc (story trajectory):\n // - Describes the STORY TRAJECTORY (overcoming, transformation, connection, reflection, closure, loss, confrontation)\n // - \"confrontation\" IS a canonical narrative_arc value (a confronting trajectory); free text accepted if none fits\n // - Distinct from the impulse-domain \"confront\" (drive_state / coping_style), which describes action/coping, not story shape\n //\n // emotional_weight vs strength_score (CRITICAL - different concepts):\n // - emotional_weight: The felt INTENSITY of the experience in the moment.\n // A heated argument = high weight (0.8). A routine check-in = low weight (0.2).\n // - strength_score: How BOUND/STUCK this memory is \u2014 through association, ritual, retelling, or identity.\n // A childhood memory retold for decades = high strength (0.9) even if emotional weight was moderate.\n // A customer complaint = may have high weight (0.8) but low strength (0.3) \u2014 intense but fades quickly.\n // - These should NOT always correlate.\n // Ask: \"How heavy does this feel RIGHT NOW?\" (weight) vs \"How stuck/persistent is this memory?\" (strength)\n //\n // SYNONYM CORRECTIONS (use the canonical form):\n // - drive_state: Use \"process\" NOT \"reflect\". The enum value is \"process\" for internal processing/reflection.\n // - narrative_archetype: Use \"caregiver\" NOT \"caretaker\". The Jungian archetype label is \"caregiver\".\n";
13
31
  /**
14
32
  * Profile-specific extracted fields types
15
33
  */
@@ -20,6 +38,8 @@ export interface LlmEssentialExtracted {
20
38
  emotion_subtone: string[];
21
39
  narrative_arc: string | null;
22
40
  };
41
+ /** Present until the attribution guard consumes it (see stance-guard) */
42
+ experiential_stance?: string | null;
23
43
  }
24
44
  export interface LlmExtendedExtracted {
25
45
  core: LlmExtractedFields["core"];
@@ -32,6 +52,8 @@ export interface LlmExtendedExtracted {
32
52
  recurrence_pattern: string | null;
33
53
  strength_score: number;
34
54
  };
55
+ /** Present until the attribution guard consumes it (see stance-guard) */
56
+ experiential_stance?: string | null;
35
57
  }
36
58
  export interface LlmExtractionResult {
37
59
  extracted: LlmExtractedFields | LlmEssentialExtracted | LlmExtendedExtracted;
@@ -48,7 +70,7 @@ export interface LlmExtractionResult {
48
70
  * @param model - Model to use (default: claude-sonnet-4-20250514)
49
71
  * @param profile - EDM profile (default: 'full')
50
72
  */
51
- export declare function extractWithLlm(client: Anthropic, input: ExtractionInput, model?: string, profile?: EdmProfile): Promise<LlmExtractionResult>;
73
+ export declare function extractWithLlm(client: Anthropic, input: ExtractionInput, model?: string, profile?: EdmProfile, options?: ExtractorCallOptions): Promise<LlmExtractionResult>;
52
74
  /**
53
75
  * Calculate extraction confidence based on field population
54
76
  */
@@ -1 +1 @@
1
- {"version":3,"file":"llm-extractor.d.ts","sourceRoot":"","sources":["../../src/extractors/llm-extractor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,EAAE,kBAAkB,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAQ1F;;;GAGG;AACH,eAAO,MAAM,wBAAwB,01bA2JpC,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACjC,aAAa,EAAE;QACb,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;QAC/B,eAAe,EAAE,MAAM,EAAE,CAAC;QAC1B,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;KAC9B,CAAC;CACH;AAED,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACjC,aAAa,EAAE,kBAAkB,CAAC,eAAe,CAAC,CAAC;IACnD,SAAS,EAAE,kBAAkB,CAAC,WAAW,CAAC,CAAC;IAC3C,OAAO,EAAE;QACP,gBAAgB,EAAE,MAAM,CAAC;QACzB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;QACvB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;QAC3B,kBAAkB,EAAE,MAAM,GAAG,IAAI,CAAC;QAClC,cAAc,EAAE,MAAM,CAAC;KACxB,CAAC;CAEH;AAED,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,kBAAkB,GAAG,qBAAqB,GAAG,oBAAoB,CAAC;IAC7E,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAA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