deepadata-edm-sdk 0.8.6 → 0.8.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/dist/assembler.d.ts +31 -2
- package/dist/assembler.d.ts.map +1 -1
- package/dist/assembler.js +144 -12
- package/dist/assembler.js.map +1 -1
- package/dist/conversation.d.ts +62 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +96 -0
- package/dist/conversation.js.map +1 -0
- package/dist/extractors/domain-extractors.js +1 -1
- package/dist/extractors/domain-extractors.js.map +1 -1
- package/dist/extractors/kimi-extractor.d.ts +2 -2
- package/dist/extractors/kimi-extractor.d.ts.map +1 -1
- package/dist/extractors/kimi-extractor.js +21 -13
- package/dist/extractors/kimi-extractor.js.map +1 -1
- package/dist/extractors/llm-extractor.d.ts +24 -2
- package/dist/extractors/llm-extractor.d.ts.map +1 -1
- package/dist/extractors/llm-extractor.js +46 -5
- package/dist/extractors/llm-extractor.js.map +1 -1
- package/dist/extractors/openai-extractor.d.ts +2 -2
- package/dist/extractors/openai-extractor.d.ts.map +1 -1
- package/dist/extractors/openai-extractor.js +11 -6
- package/dist/extractors/openai-extractor.js.map +1 -1
- package/dist/extractors/output-sanitizer.d.ts +26 -0
- package/dist/extractors/output-sanitizer.d.ts.map +1 -0
- package/dist/extractors/output-sanitizer.js +129 -0
- package/dist/extractors/output-sanitizer.js.map +1 -0
- package/dist/extractors/profile-prompts.d.ts +2 -2
- package/dist/extractors/profile-prompts.d.ts.map +1 -1
- package/dist/extractors/profile-prompts.js +25 -0
- package/dist/extractors/profile-prompts.js.map +1 -1
- package/dist/extractors/stance-guard.d.ts +75 -0
- package/dist/extractors/stance-guard.d.ts.map +1 -0
- package/dist/extractors/stance-guard.js +171 -0
- package/dist/extractors/stance-guard.js.map +1 -0
- package/dist/index.d.ts +15 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +58 -3
- package/dist/index.js.map +1 -1
- package/dist/schema/edm-schema.d.ts +16 -0
- package/dist/schema/edm-schema.d.ts.map +1 -1
- package/dist/schema/edm-schema.js +14 -0
- package/dist/schema/edm-schema.js.map +1 -1
- package/dist/schema/types.d.ts +76 -0
- package/dist/schema/types.d.ts.map +1 -1
- package/dist/schema/types.js +12 -0
- package/dist/schema/types.js.map +1 -1
- package/package.json +3 -1
|
@@ -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
|
|
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
|
|
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
|
-
|
|
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
|
|
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:
|
|
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
|
|
79
|
-
if (!
|
|
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 =
|
|
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: ${
|
|
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,
|
|
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,EACxB,gBAAgB,EAChB,gBAAgB,GAGjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAE1D;;GAEG;AACH,SAAS,gBAAgB,CAAC,OAAmB;I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AAuB;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\": \"\", // 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";
|
|
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;
|
|
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;AAU1F;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,gEAAgE;IAChE,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,oDAAoD;AACpD,eAAO,MAAM,kBAAkB,OAAO,CAAC;AACvC;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,QAAS,CAAC;AAKhD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED,4EAA4E;AAC5E,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,GAAG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AE,oBAAyB,GACjC,OAAO,CAAC,mBAAmB,CAAC,CAsF9B;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,kBAAkB,GAAG,MAAM,CA6CzE;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAIhE"}
|
|
@@ -6,6 +6,27 @@
|
|
|
6
6
|
import Anthropic from "@anthropic-ai/sdk";
|
|
7
7
|
import { LlmExtractedFieldsSchema, LlmEssentialFieldsSchema, LlmExtendedFieldsSchema, } from "../schema/edm-schema.js";
|
|
8
8
|
import { getProfilePrompt, calculateProfileConfidence } from "./profile-prompts.js";
|
|
9
|
+
import { sanitizeLlmOutput } from "./output-sanitizer.js";
|
|
10
|
+
import { frameTranscript } from "../conversation.js";
|
|
11
|
+
/** Default output budget for non-thinking models */
|
|
12
|
+
export const DEFAULT_MAX_TOKENS = 4096;
|
|
13
|
+
/**
|
|
14
|
+
* Default output budget for thinking models, whose reasoning tokens count
|
|
15
|
+
* against max_tokens. 4096 silently truncated extraction JSON on exactly
|
|
16
|
+
* the most emotionally dense inputs (archive-sample run, 2026-06-10).
|
|
17
|
+
*/
|
|
18
|
+
export const THINKING_MODEL_MAX_TOKENS = 16_384;
|
|
19
|
+
/** Models that spend output tokens on reasoning before emitting JSON */
|
|
20
|
+
const THINKING_MODEL_RE = /k2\.[5-9]|k2\.\d{2,}|k3|thinking|reasoner|o[13](-|$)|gpt-5/i;
|
|
21
|
+
export function defaultMaxTokens(model) {
|
|
22
|
+
return THINKING_MODEL_RE.test(model) ? THINKING_MODEL_MAX_TOKENS : DEFAULT_MAX_TOKENS;
|
|
23
|
+
}
|
|
24
|
+
/** Apply conversation framing when the input declares transcript content */
|
|
25
|
+
export function prepareInputText(input) {
|
|
26
|
+
if (!input.text)
|
|
27
|
+
return input.text;
|
|
28
|
+
return input.inputType === "conversation" ? frameTranscript(input.text) : input.text;
|
|
29
|
+
}
|
|
9
30
|
/**
|
|
10
31
|
* System prompt for EDM extraction
|
|
11
32
|
* See CHANGELOG.md for version-specific schema changes
|
|
@@ -22,6 +43,21 @@ Rules
|
|
|
22
43
|
- Output JSON only — no commentary, markdown, or extra text.
|
|
23
44
|
- If motivation is ambiguous, choose the most conservative option (e.g., "curiosity" vs "fear") or return null.
|
|
24
45
|
|
|
46
|
+
SUBJECT ANCHORING (critical)
|
|
47
|
+
- 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.
|
|
48
|
+
- Score every field relative to the SUBJECT, not the passage. emotional_weight measures what this content meant TO THE SUBJECT — not how vivid, dramatic, or emotionally rich the text itself is.
|
|
49
|
+
- Routine work content (debugging, drafting, planning, logistics) is 0.1–0.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.
|
|
50
|
+
- 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.
|
|
51
|
+
|
|
52
|
+
EXPERIENTIAL STANCE (critical)
|
|
53
|
+
Classify whose experience the emotionally salient material is, in the top-level "experiential_stance" key:
|
|
54
|
+
- "lived" — the subject's own first-hand experience
|
|
55
|
+
- "witnessed" — events the subject personally witnessed or is directly affected by (a loved one's death, a family crisis)
|
|
56
|
+
- "quoted_third_party" — someone else's story the subject quoted, pasted, or retold without being a participant (an article, test data, a stranger's anecdote)
|
|
57
|
+
- "assistant_generated" — fiction, examples, or anecdotes produced by the assistant, not reported by the subject
|
|
58
|
+
- "hypothetical" — imagined scenarios, drafts about invented people, role-play
|
|
59
|
+
If 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 — 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.
|
|
60
|
+
|
|
25
61
|
CRITICAL: Enum Field Constraints
|
|
26
62
|
- Many fields below have CANONICAL values — preferred values for cross-artifact comparability.
|
|
27
63
|
- 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.
|
|
@@ -41,6 +77,7 @@ Normalization (very important)
|
|
|
41
77
|
|
|
42
78
|
Schema
|
|
43
79
|
{
|
|
80
|
+
"experiential_stance": "", // STRICT ENUM: lived | witnessed | quoted_third_party | assistant_generated | hypothetical (pick ONE or null)
|
|
44
81
|
"core": {
|
|
45
82
|
"anchor": "", // central theme (e.g., "dad's toolbox", "nana's traditions")
|
|
46
83
|
"spark": "", // what triggered the memory (e.g., "finding the cassette", "first snow")
|
|
@@ -188,13 +225,14 @@ function getProfileSchema(profile) {
|
|
|
188
225
|
* @param model - Model to use (default: claude-sonnet-4-20250514)
|
|
189
226
|
* @param profile - EDM profile (default: 'full')
|
|
190
227
|
*/
|
|
191
|
-
export async function extractWithLlm(client, input, model = "claude-sonnet-4-20250514", profile = "full") {
|
|
228
|
+
export async function extractWithLlm(client, input, model = "claude-sonnet-4-20250514", profile = "full", options = {}) {
|
|
192
229
|
const userContent = [];
|
|
193
|
-
// Add text content
|
|
194
|
-
|
|
230
|
+
// Add text content (conversation inputs get source-material framing)
|
|
231
|
+
const text = prepareInputText(input);
|
|
232
|
+
if (text) {
|
|
195
233
|
userContent.push({
|
|
196
234
|
type: "text",
|
|
197
|
-
text
|
|
235
|
+
text,
|
|
198
236
|
});
|
|
199
237
|
}
|
|
200
238
|
// Add image if provided
|
|
@@ -213,7 +251,7 @@ export async function extractWithLlm(client, input, model = "claude-sonnet-4-202
|
|
|
213
251
|
const systemPrompt = profilePrompt || EXTRACTION_SYSTEM_PROMPT;
|
|
214
252
|
const response = await client.messages.create({
|
|
215
253
|
model,
|
|
216
|
-
max_tokens:
|
|
254
|
+
max_tokens: options.maxTokens ?? defaultMaxTokens(model),
|
|
217
255
|
system: systemPrompt,
|
|
218
256
|
messages: [
|
|
219
257
|
{
|
|
@@ -240,6 +278,9 @@ export async function extractWithLlm(client, input, model = "claude-sonnet-4-202
|
|
|
240
278
|
catch {
|
|
241
279
|
throw new Error(`Failed to parse LLM response as JSON: ${textBlock.text.slice(0, 200)}...`);
|
|
242
280
|
}
|
|
281
|
+
// Sanitize before validation: clamp array caps, coerce invalid
|
|
282
|
+
// strict-enum values to null (prefer a null field over a dropped artifact)
|
|
283
|
+
sanitizeLlmOutput(parsed);
|
|
243
284
|
// Validate against profile-specific schema
|
|
244
285
|
const schema = getProfileSchema(profile);
|
|
245
286
|
const result = schema.safeParse(parsed);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm-extractor.js","sourceRoot":"","sources":["../../src/extractors/llm-extractor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAE1C,OAAO,EACL,wBAAwB,EACxB,wBAAwB,EACxB,uBAAuB,GACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,gBAAgB,EAAE,0BAA0B,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"llm-extractor.js","sourceRoot":"","sources":["../../src/extractors/llm-extractor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAE1C,OAAO,EACL,wBAAwB,EACxB,wBAAwB,EACxB,uBAAuB,GACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,gBAAgB,EAAE,0BAA0B,EAAE,MAAM,sBAAsB,CAAC;AACpF,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAUrD,oDAAoD;AACpD,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,CAAC;AACvC;;;;GAIG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,MAAM,CAAC;AAEhD,wEAAwE;AACxE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OAAO,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;AAC5C,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAe;IACnD,OAAO,IAAI,SAAS,CAAC;QACnB,MAAM,EAAE,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC;KACnD,CAAC,CAAC;AACL,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 OpenAI
|
|
11
11
|
*/
|
|
12
|
-
export declare function extractWithOpenAI(client: OpenAI, input: ExtractionInput, model?: string, temperature?: number, profile?: EdmProfile): Promise<LlmExtractionResult>;
|
|
12
|
+
export declare function extractWithOpenAI(client: OpenAI, input: ExtractionInput, model?: string, temperature?: number, profile?: EdmProfile, options?: ExtractorCallOptions): Promise<LlmExtractionResult>;
|
|
13
13
|
/**
|
|
14
14
|
* Create an OpenAI client
|
|
15
15
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-extractor.d.ts","sourceRoot":"","sources":["../../src/extractors/openai-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,
|
|
1
|
+
{"version":3,"file":"openai-extractor.d.ts","sourceRoot":"","sources":["../../src/extractors/openai-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;AAI5B;;GAEG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,eAAe,EACtB,KAAK,GAAE,MAAsB,EAC7B,WAAW,GAAE,MAAU,EACvB,OAAO,GAAE,UAAmB,EAC5B,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,mBAAmB,CAAC,CAyF9B;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAQ1D"}
|
|
@@ -5,18 +5,20 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import OpenAI from "openai";
|
|
7
7
|
import { LlmExtractedFieldsSchema } 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
9
|
import { getProfilePrompt, calculateProfileConfidence } from "./profile-prompts.js";
|
|
10
|
+
import { sanitizeLlmOutput } from "./output-sanitizer.js";
|
|
10
11
|
/**
|
|
11
12
|
* Extract EDM fields from content using OpenAI
|
|
12
13
|
*/
|
|
13
|
-
export async function extractWithOpenAI(client, input, model = "gpt-4o-mini", temperature = 0, profile = "full") {
|
|
14
|
+
export async function extractWithOpenAI(client, input, model = "gpt-4o-mini", temperature = 0, profile = "full", options = {}) {
|
|
14
15
|
const userContent = [];
|
|
15
|
-
// Add text content
|
|
16
|
-
|
|
16
|
+
// Add text content (conversation inputs get source-material framing)
|
|
17
|
+
const inputText = prepareInputText(input);
|
|
18
|
+
if (inputText) {
|
|
17
19
|
userContent.push({
|
|
18
20
|
type: "text",
|
|
19
|
-
text:
|
|
21
|
+
text: inputText,
|
|
20
22
|
});
|
|
21
23
|
}
|
|
22
24
|
// Add image if provided (OpenAI uses image_url with data URI)
|
|
@@ -34,7 +36,7 @@ export async function extractWithOpenAI(client, input, model = "gpt-4o-mini", te
|
|
|
34
36
|
const systemPrompt = profilePrompt || EXTRACTION_SYSTEM_PROMPT;
|
|
35
37
|
const response = await client.chat.completions.create({
|
|
36
38
|
model,
|
|
37
|
-
max_tokens:
|
|
39
|
+
max_tokens: options.maxTokens ?? defaultMaxTokens(model),
|
|
38
40
|
response_format: { type: "json_object" },
|
|
39
41
|
temperature,
|
|
40
42
|
messages: [
|
|
@@ -66,6 +68,9 @@ export async function extractWithOpenAI(client, input, model = "gpt-4o-mini", te
|
|
|
66
68
|
catch {
|
|
67
69
|
throw new Error(`Failed to parse OpenAI response as JSON: ${text.slice(0, 200)}...`);
|
|
68
70
|
}
|
|
71
|
+
// Sanitize before validation: clamp array caps, coerce invalid
|
|
72
|
+
// strict-enum values to null (prefer a null field over a dropped artifact)
|
|
73
|
+
sanitizeLlmOutput(parsed);
|
|
69
74
|
// Validate against schema
|
|
70
75
|
const result = LlmExtractedFieldsSchema.safeParse(parsed);
|
|
71
76
|
if (!result.success) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-extractor.js","sourceRoot":"","sources":["../../src/extractors/openai-extractor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAG5B,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,EACL,wBAAwB,
|
|
1
|
+
{"version":3,"file":"openai-extractor.js","sourceRoot":"","sources":["../../src/extractors/openai-extractor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAG5B,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,EACL,wBAAwB,EACxB,gBAAgB,EAChB,gBAAgB,GAGjB,MAAM,oBAAoB,CAAC;AAC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ACd,MAAM,IAAI,KAAK,CAAC,6CAA6C,YAAY,EAAE,CAAC,CAAC;IAC/E,CAAC;IAED,qCAAqC;IACrC,MAAM,UAAU,GAAG,0BAA0B,CAC3C,MAAM,CAAC,IAA0D,EACjE,OAAO,CACR,CAAC;IAEF,OAAO;QACL,SAAS,EAAE,MAAM,CAAC,IAAI;QACtB,UAAU;QACV,KAAK;QACL,OAAO;QACP,KAAK,EAAE,IAAI;KACZ,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAe;IAChD,MAAM,GAAG,GAAG,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACpD,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CACb,8FAA8F,CAC/F,CAAC;IACJ,CAAC;IACD,OAAO,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM output sanitation
|
|
3
|
+
*
|
|
4
|
+
* Runs between JSON.parse and zod validation in every extractor. Two
|
|
5
|
+
* failure modes from the 2026-06-10 archive-sample run motivate it:
|
|
6
|
+
*
|
|
7
|
+
* - emotion_subtone with >4 items on emotionally rich threads (model
|
|
8
|
+
* enthusiasm exceeds the spec cap) — hard-failed validation/sealing.
|
|
9
|
+
* - Free-text values in STRICT ENUM fields (e.g. narrative_archetype:
|
|
10
|
+
* "observer") — hard-failed validation/sealing.
|
|
11
|
+
*
|
|
12
|
+
* Policy: prefer a null/clamped field over a dropped artifact. Strict-enum
|
|
13
|
+
* coercion to null matches the prompts' own instruction ("pick ONE or
|
|
14
|
+
* null"); array caps match the JSON schema. Free-text-tolerant fields
|
|
15
|
+
* (emotion_primary, narrative_arc, arc_type, relational_dynamics,
|
|
16
|
+
* tether_type, recurrence_pattern, coping_style) are NOT touched.
|
|
17
|
+
*/
|
|
18
|
+
export interface SanitationNote {
|
|
19
|
+
path: string;
|
|
20
|
+
action: "coerced_to_null" | "truncated" | "clamped";
|
|
21
|
+
original: unknown;
|
|
22
|
+
}
|
|
23
|
+
export declare function sanitizeLlmOutput(parsed: unknown): SanitationNote[];
|
|
24
|
+
/** Render sanitation notes for telemetry.extraction_notes */
|
|
25
|
+
export declare function formatSanitationNotes(notes: SanitationNote[]): string | null;
|
|
26
|
+
//# sourceMappingURL=output-sanitizer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output-sanitizer.d.ts","sourceRoot":"","sources":["../../src/extractors/output-sanitizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AA+CH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,iBAAiB,GAAG,WAAW,GAAG,SAAS,CAAC;IACpD,QAAQ,EAAE,OAAO,CAAC;CACnB;AAQD,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,OAAO,GAAG,cAAc,EAAE,CAwDnE;AAED,6DAA6D;AAC7D,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,cAAc,EAAE,GAAG,MAAM,GAAG,IAAI,CAM5E"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM output sanitation
|
|
3
|
+
*
|
|
4
|
+
* Runs between JSON.parse and zod validation in every extractor. Two
|
|
5
|
+
* failure modes from the 2026-06-10 archive-sample run motivate it:
|
|
6
|
+
*
|
|
7
|
+
* - emotion_subtone with >4 items on emotionally rich threads (model
|
|
8
|
+
* enthusiasm exceeds the spec cap) — hard-failed validation/sealing.
|
|
9
|
+
* - Free-text values in STRICT ENUM fields (e.g. narrative_archetype:
|
|
10
|
+
* "observer") — hard-failed validation/sealing.
|
|
11
|
+
*
|
|
12
|
+
* Policy: prefer a null/clamped field over a dropped artifact. Strict-enum
|
|
13
|
+
* coercion to null matches the prompts' own instruction ("pick ONE or
|
|
14
|
+
* null"); array caps match the JSON schema. Free-text-tolerant fields
|
|
15
|
+
* (emotion_primary, narrative_arc, arc_type, relational_dynamics,
|
|
16
|
+
* tether_type, recurrence_pattern, coping_style) are NOT touched.
|
|
17
|
+
*/
|
|
18
|
+
/** Fields documented as STRICT ENUM — invalid values coerce to null */
|
|
19
|
+
const STRICT_ENUMS = {
|
|
20
|
+
constellation: {
|
|
21
|
+
temporal_context: ["childhood", "early_adulthood", "midlife", "late_life", "recent", "future", "timeless"],
|
|
22
|
+
memory_type: ["legacy_artifact", "fleeting_moment", "milestone", "reflection", "formative_experience"],
|
|
23
|
+
media_format: ["photo", "video", "audio", "text", "photo_with_story"],
|
|
24
|
+
narrative_archetype: ["hero", "caregiver", "seeker", "sage", "lover", "outlaw", "innocent", "orphan", "magician", "creator", "everyman", "jester", "ruler", "mentor"],
|
|
25
|
+
relational_perspective: ["self", "partner", "family", "friends", "community", "humanity"],
|
|
26
|
+
temporal_rhythm: ["still", "sudden", "rising", "fading", "recurring", "spiraling", "dragging", "suspended", "looping", "cyclic"],
|
|
27
|
+
},
|
|
28
|
+
milky_way: {
|
|
29
|
+
visibility_context: ["private", "family_only", "shared_publicly"],
|
|
30
|
+
},
|
|
31
|
+
gravity: {
|
|
32
|
+
emotional_density: ["low", "medium", "high"],
|
|
33
|
+
valence: ["positive", "negative", "mixed"],
|
|
34
|
+
viscosity: ["low", "medium", "high", "enduring", "fluid"],
|
|
35
|
+
temporal_decay: ["fast", "moderate", "slow"],
|
|
36
|
+
adaptation_trajectory: ["improving", "stable", "declining", "integrative", "emerging"],
|
|
37
|
+
},
|
|
38
|
+
impulse: {
|
|
39
|
+
drive_state: ["explore", "approach", "avoid", "repair", "persevere", "share", "confront", "protect", "process"],
|
|
40
|
+
motivational_orientation: ["belonging", "safety", "mastery", "meaning", "autonomy", "authenticity"],
|
|
41
|
+
temporal_focus: ["past", "present", "future"],
|
|
42
|
+
directionality: ["inward", "outward", "transcendent"],
|
|
43
|
+
social_visibility: ["private", "relational", "collective"],
|
|
44
|
+
urgency: ["calm", "elevated", "pressing", "acute"],
|
|
45
|
+
risk_posture: ["cautious", "balanced", "bold"],
|
|
46
|
+
agency_level: ["low", "medium", "high"],
|
|
47
|
+
regulation_state: ["regulated", "wavering", "dysregulated"],
|
|
48
|
+
attachment_style: ["secure", "anxious", "avoidant", "disorganized"],
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
/** Array fields with spec caps — overflow is truncated, not rejected */
|
|
52
|
+
const ARRAY_CAPS = {
|
|
53
|
+
constellation: { emotion_subtone: 4 },
|
|
54
|
+
gravity: { resilience_markers: 3 },
|
|
55
|
+
};
|
|
56
|
+
/** Numeric fields clamped to [0, 1] */
|
|
57
|
+
const UNIT_INTERVAL_FIELDS = {
|
|
58
|
+
gravity: ["emotional_weight", "strength_score"],
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Sanitize parsed LLM output in place. Returns notes describing every
|
|
62
|
+
* change so callers can surface them in telemetry.
|
|
63
|
+
*/
|
|
64
|
+
const STANCE_VALUES = ["lived", "witnessed", "quoted_third_party", "assistant_generated", "hypothetical"];
|
|
65
|
+
export function sanitizeLlmOutput(parsed) {
|
|
66
|
+
const notes = [];
|
|
67
|
+
if (typeof parsed !== "object" || parsed === null)
|
|
68
|
+
return notes;
|
|
69
|
+
const root = parsed;
|
|
70
|
+
// Normalize the top-level experiential_stance before validation:
|
|
71
|
+
// "quoted third-party" -> "quoted_third_party"; anything unrecognized -> null.
|
|
72
|
+
const stance = root["experiential_stance"];
|
|
73
|
+
if (typeof stance === "string") {
|
|
74
|
+
const normalized = stance.trim().toLowerCase().replace(/[\s-]+/g, "_");
|
|
75
|
+
if (normalized !== stance) {
|
|
76
|
+
root["experiential_stance"] = STANCE_VALUES.includes(normalized) ? normalized : null;
|
|
77
|
+
notes.push({ path: "experiential_stance", action: STANCE_VALUES.includes(normalized) ? "clamped" : "coerced_to_null", original: stance });
|
|
78
|
+
}
|
|
79
|
+
else if (!STANCE_VALUES.includes(stance)) {
|
|
80
|
+
root["experiential_stance"] = null;
|
|
81
|
+
notes.push({ path: "experiential_stance", action: "coerced_to_null", original: stance });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
for (const [domain, fields] of Object.entries(STRICT_ENUMS)) {
|
|
85
|
+
const d = root[domain];
|
|
86
|
+
if (!d || typeof d !== "object")
|
|
87
|
+
continue;
|
|
88
|
+
for (const [field, allowed] of Object.entries(fields)) {
|
|
89
|
+
const v = d[field];
|
|
90
|
+
if (typeof v === "string" && v !== "" && !allowed.includes(v)) {
|
|
91
|
+
notes.push({ path: `${domain}.${field}`, action: "coerced_to_null", original: v });
|
|
92
|
+
d[field] = null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
for (const [domain, fields] of Object.entries(ARRAY_CAPS)) {
|
|
97
|
+
const d = root[domain];
|
|
98
|
+
if (!d || typeof d !== "object")
|
|
99
|
+
continue;
|
|
100
|
+
for (const [field, cap] of Object.entries(fields)) {
|
|
101
|
+
const v = d[field];
|
|
102
|
+
if (Array.isArray(v) && v.length > cap) {
|
|
103
|
+
notes.push({ path: `${domain}.${field}`, action: "truncated", original: v.length });
|
|
104
|
+
d[field] = v.slice(0, cap);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const [domain, fields] of Object.entries(UNIT_INTERVAL_FIELDS)) {
|
|
109
|
+
const d = root[domain];
|
|
110
|
+
if (!d || typeof d !== "object")
|
|
111
|
+
continue;
|
|
112
|
+
for (const field of fields) {
|
|
113
|
+
const v = d[field];
|
|
114
|
+
if (typeof v === "number" && (v < 0 || v > 1)) {
|
|
115
|
+
notes.push({ path: `${domain}.${field}`, action: "clamped", original: v });
|
|
116
|
+
d[field] = Math.min(1, Math.max(0, v));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return notes;
|
|
121
|
+
}
|
|
122
|
+
/** Render sanitation notes for telemetry.extraction_notes */
|
|
123
|
+
export function formatSanitationNotes(notes) {
|
|
124
|
+
if (notes.length === 0)
|
|
125
|
+
return null;
|
|
126
|
+
return ("sanitized: " +
|
|
127
|
+
notes.map((n) => `${n.path} ${n.action}${n.action === "coerced_to_null" ? ` (was ${JSON.stringify(n.original)})` : ""}`).join("; "));
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=output-sanitizer.js.map
|