forge-openclaw-plugin 0.2.4 → 0.2.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 (114) hide show
  1. package/README.md +186 -6
  2. package/dist/assets/board-C_m78kvK.js +6 -0
  3. package/dist/assets/board-C_m78kvK.js.map +1 -0
  4. package/dist/assets/favicon-BCHm9dUV.ico +0 -0
  5. package/dist/assets/index-BWtLtXwb.js +36 -0
  6. package/dist/assets/index-BWtLtXwb.js.map +1 -0
  7. package/dist/assets/index-Dp5GXY_z.css +1 -0
  8. package/dist/assets/motion-CpZvZumD.js +10 -0
  9. package/dist/assets/motion-CpZvZumD.js.map +1 -0
  10. package/dist/assets/plus-jakarta-sans-latin-ext-wght-normal-DmpS2jIq.woff2 +0 -0
  11. package/dist/assets/plus-jakarta-sans-latin-wght-normal-eXO_dkmS.woff2 +0 -0
  12. package/dist/assets/plus-jakarta-sans-vietnamese-wght-normal-qRpaaN48.woff2 +0 -0
  13. package/dist/assets/sora-latin-ext-wght-normal-CawQDOvP.woff2 +0 -0
  14. package/dist/assets/sora-latin-wght-normal-DdqRvwsR.woff2 +0 -0
  15. package/dist/assets/space-grotesk-latin-500-normal-CNSSEhBt.woff +0 -0
  16. package/dist/assets/space-grotesk-latin-500-normal-lFbtlQH6.woff2 +0 -0
  17. package/dist/assets/space-grotesk-latin-700-normal-CwsQ-cCU.woff +0 -0
  18. package/dist/assets/space-grotesk-latin-700-normal-RjhwGPKo.woff2 +0 -0
  19. package/dist/assets/space-grotesk-latin-ext-500-normal-3dgZTiw9.woff +0 -0
  20. package/dist/assets/space-grotesk-latin-ext-500-normal-DUe3BAxM.woff2 +0 -0
  21. package/dist/assets/space-grotesk-latin-ext-700-normal-BQnZhY3m.woff2 +0 -0
  22. package/dist/assets/space-grotesk-latin-ext-700-normal-HVCqSBdx.woff +0 -0
  23. package/dist/assets/space-grotesk-vietnamese-500-normal-BTqKIpxg.woff +0 -0
  24. package/dist/assets/space-grotesk-vietnamese-500-normal-BmEvtly_.woff2 +0 -0
  25. package/dist/assets/space-grotesk-vietnamese-700-normal-DMty7AZE.woff2 +0 -0
  26. package/dist/assets/space-grotesk-vietnamese-700-normal-Duxec5Rn.woff +0 -0
  27. package/dist/assets/table-DtyXTw03.js +23 -0
  28. package/dist/assets/table-DtyXTw03.js.map +1 -0
  29. package/dist/assets/ui-BXbpiKyS.js +46 -0
  30. package/dist/assets/ui-BXbpiKyS.js.map +1 -0
  31. package/dist/assets/vendor-CRS-psbw.css +1 -0
  32. package/dist/assets/vendor-QBH6qVEe.js +433 -0
  33. package/dist/assets/vendor-QBH6qVEe.js.map +1 -0
  34. package/dist/assets/viz-w-IMeueL.js +34 -0
  35. package/dist/assets/viz-w-IMeueL.js.map +1 -0
  36. package/dist/favicon.ico +0 -0
  37. package/dist/favicon.png +0 -0
  38. package/dist/index.html +29 -0
  39. package/dist/openclaw/api-client.d.ts +9 -0
  40. package/dist/openclaw/api-client.js +31 -4
  41. package/dist/openclaw/local-runtime.d.ts +3 -0
  42. package/dist/openclaw/local-runtime.js +136 -0
  43. package/dist/openclaw/parity.d.ts +4 -4
  44. package/dist/openclaw/parity.js +23 -33
  45. package/dist/openclaw/plugin-entry-shared.d.ts +4 -2
  46. package/dist/openclaw/plugin-entry-shared.js +63 -9
  47. package/dist/openclaw/routes.d.ts +12 -3
  48. package/dist/openclaw/routes.js +156 -924
  49. package/dist/openclaw/tools.js +242 -1100
  50. package/dist/server/app.js +2487 -0
  51. package/dist/server/db.js +313 -0
  52. package/dist/server/demo-data.js +49 -0
  53. package/dist/server/e2e-server.js +20 -0
  54. package/dist/server/errors.js +15 -0
  55. package/dist/server/index.js +16 -0
  56. package/dist/server/managers/base.js +17 -0
  57. package/dist/server/managers/contracts.js +47 -0
  58. package/dist/server/managers/platform/api-gateway-manager.js +11 -0
  59. package/dist/server/managers/platform/audit-manager.js +15 -0
  60. package/dist/server/managers/platform/authentication-manager.js +56 -0
  61. package/dist/server/managers/platform/authorization-manager.js +56 -0
  62. package/dist/server/managers/platform/background-job-manager.js +10 -0
  63. package/dist/server/managers/platform/configuration-manager.js +33 -0
  64. package/dist/server/managers/platform/database-manager.js +14 -0
  65. package/dist/server/managers/platform/event-bus-manager.js +7 -0
  66. package/dist/server/managers/platform/external-service-manager.js +11 -0
  67. package/dist/server/managers/platform/health-manager.js +7 -0
  68. package/dist/server/managers/platform/migration-manager.js +8 -0
  69. package/dist/server/managers/platform/search-index-manager.js +4 -0
  70. package/dist/server/managers/platform/secrets-manager.js +19 -0
  71. package/dist/server/managers/platform/session-manager.js +121 -0
  72. package/dist/server/managers/platform/storage-manager.js +16 -0
  73. package/dist/server/managers/platform/token-manager.js +37 -0
  74. package/dist/server/managers/platform/transaction-manager.js +8 -0
  75. package/dist/server/managers/platform/trusted-network.js +39 -0
  76. package/dist/server/managers/runtime.js +56 -0
  77. package/dist/server/managers/type-guards.js +4 -0
  78. package/dist/server/openapi.js +3553 -0
  79. package/dist/server/psyche-types.js +366 -0
  80. package/dist/server/repositories/activity-events.js +157 -0
  81. package/dist/server/repositories/collaboration.js +497 -0
  82. package/dist/server/repositories/deleted-entities.js +226 -0
  83. package/dist/server/repositories/domains.js +30 -0
  84. package/dist/server/repositories/event-log.js +64 -0
  85. package/dist/server/repositories/goals.js +156 -0
  86. package/dist/server/repositories/notes.js +359 -0
  87. package/dist/server/repositories/projects.js +211 -0
  88. package/dist/server/repositories/psyche.js +1353 -0
  89. package/dist/server/repositories/rewards.js +675 -0
  90. package/dist/server/repositories/settings.js +399 -0
  91. package/dist/server/repositories/tags.js +160 -0
  92. package/dist/server/repositories/task-runs.js +490 -0
  93. package/dist/server/repositories/tasks.js +424 -0
  94. package/dist/server/seed-demo.js +11 -0
  95. package/dist/server/services/context.js +214 -0
  96. package/dist/server/services/dashboard.js +173 -0
  97. package/dist/server/services/entity-crud.js +573 -0
  98. package/dist/server/services/gamification.js +215 -0
  99. package/dist/server/services/insights.js +91 -0
  100. package/dist/server/services/projects.js +77 -0
  101. package/dist/server/services/psyche.js +63 -0
  102. package/dist/server/services/relations.js +28 -0
  103. package/dist/server/services/reviews.js +88 -0
  104. package/dist/server/services/run-recovery.js +13 -0
  105. package/dist/server/services/tagging.js +49 -0
  106. package/dist/server/services/task-run-watchdog.js +92 -0
  107. package/dist/server/services/work-time.js +176 -0
  108. package/dist/server/types.js +1058 -0
  109. package/dist/server/web.js +91 -0
  110. package/openclaw.plugin.json +32 -9
  111. package/package.json +17 -4
  112. package/server/migrations/001_core.sql +411 -0
  113. package/server/migrations/002_psyche.sql +392 -0
  114. package/skills/forge-openclaw/SKILL.md +197 -271
@@ -0,0 +1,1353 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { getDatabase, runInTransaction } from "../db.js";
3
+ import { recordActivityEvent } from "./activity-events.js";
4
+ import { filterDeletedEntities, filterDeletedIds, isEntityDeleted } from "./deleted-entities.js";
5
+ import { recordEventLog } from "./event-log.js";
6
+ import { unlinkNotesForEntity } from "./notes.js";
7
+ import { recordPsycheClarityReward, recordPsycheReflectionReward } from "./rewards.js";
8
+ import { behaviorPatternSchema, behaviorSchema, beliefEntrySchema, createBehaviorPatternSchema, createBehaviorSchema, createBeliefEntrySchema, createEmotionDefinitionSchema, createEventTypeSchema, createModeGuideSessionSchema, createModeProfileSchema, createPsycheValueSchema, createTriggerReportSchema, domainSchema, emotionDefinitionSchema, eventTypeSchema, modeFamilySchema, modeGuideResultSchema, modeGuideSessionSchema, modeProfileSchema, modeTimelineEntrySchema, psycheValueSchema, schemaCatalogEntrySchema, triggerReportSchema, updateBehaviorPatternSchema, updateBehaviorSchema, updateBeliefEntrySchema, updateEmotionDefinitionSchema, updateEventTypeSchema, updateModeGuideSessionSchema, updateModeProfileSchema, updatePsycheValueSchema, updateTriggerReportSchema } from "../psyche-types.js";
9
+ const PSYCHE_DOMAIN_ID = "domain_psyche";
10
+ function parseJson(value) {
11
+ return JSON.parse(value);
12
+ }
13
+ function buildId(prefix) {
14
+ return `${prefix}_${randomUUID().replaceAll("-", "").slice(0, 10)}`;
15
+ }
16
+ function enrichTriggerItems(items, prefix) {
17
+ return items.map((item) => ({
18
+ ...item,
19
+ id: item.id ?? buildId(prefix)
20
+ }));
21
+ }
22
+ function mapDomain(row) {
23
+ return domainSchema.parse({
24
+ id: row.id,
25
+ slug: row.slug,
26
+ title: row.title,
27
+ description: row.description,
28
+ themeColor: row.theme_color,
29
+ sensitive: row.sensitive === 1,
30
+ createdAt: row.created_at,
31
+ updatedAt: row.updated_at
32
+ });
33
+ }
34
+ function mapSchemaCatalogEntry(row) {
35
+ return schemaCatalogEntrySchema.parse({
36
+ id: row.id,
37
+ slug: row.slug,
38
+ title: row.title,
39
+ family: row.family,
40
+ schemaType: row.schema_type,
41
+ description: row.description,
42
+ createdAt: row.created_at,
43
+ updatedAt: row.updated_at
44
+ });
45
+ }
46
+ function mapEventType(row) {
47
+ return eventTypeSchema.parse({
48
+ id: row.id,
49
+ domainId: row.domain_id,
50
+ label: row.label,
51
+ description: row.description,
52
+ system: row.system === 1,
53
+ createdAt: row.created_at,
54
+ updatedAt: row.updated_at
55
+ });
56
+ }
57
+ function mapEmotionDefinition(row) {
58
+ return emotionDefinitionSchema.parse({
59
+ id: row.id,
60
+ domainId: row.domain_id,
61
+ label: row.label,
62
+ description: row.description,
63
+ category: row.category,
64
+ system: row.system === 1,
65
+ createdAt: row.created_at,
66
+ updatedAt: row.updated_at
67
+ });
68
+ }
69
+ function mapPsycheValue(row) {
70
+ return psycheValueSchema.parse({
71
+ id: row.id,
72
+ domainId: row.domain_id,
73
+ title: row.title,
74
+ description: row.description,
75
+ valuedDirection: row.valued_direction,
76
+ whyItMatters: row.why_it_matters,
77
+ linkedGoalIds: filterDeletedIds("goal", parseJson(row.linked_goal_ids_json)),
78
+ linkedProjectIds: filterDeletedIds("project", parseJson(row.linked_project_ids_json)),
79
+ linkedTaskIds: filterDeletedIds("task", parseJson(row.linked_task_ids_json)),
80
+ committedActions: parseJson(row.committed_actions_json),
81
+ createdAt: row.created_at,
82
+ updatedAt: row.updated_at
83
+ });
84
+ }
85
+ function mapBehaviorPattern(row) {
86
+ return behaviorPatternSchema.parse({
87
+ id: row.id,
88
+ domainId: row.domain_id,
89
+ title: row.title,
90
+ description: row.description,
91
+ targetBehavior: row.target_behavior,
92
+ cueContexts: parseJson(row.cue_contexts_json),
93
+ shortTermPayoff: row.short_term_payoff,
94
+ longTermCost: row.long_term_cost,
95
+ preferredResponse: row.preferred_response,
96
+ linkedValueIds: filterDeletedIds("psyche_value", parseJson(row.linked_value_ids_json)),
97
+ linkedSchemaLabels: parseJson(row.linked_schema_labels_json),
98
+ linkedModeLabels: parseJson(row.linked_mode_labels_json),
99
+ linkedModeIds: filterDeletedIds("mode_profile", parseJson(row.linked_mode_ids_json)),
100
+ linkedBeliefIds: filterDeletedIds("belief_entry", parseJson(row.linked_belief_ids_json)),
101
+ createdAt: row.created_at,
102
+ updatedAt: row.updated_at
103
+ });
104
+ }
105
+ function mapBehavior(row) {
106
+ return behaviorSchema.parse({
107
+ id: row.id,
108
+ domainId: row.domain_id,
109
+ kind: row.kind,
110
+ title: row.title,
111
+ description: row.description,
112
+ commonCues: parseJson(row.common_cues_json),
113
+ urgeStory: row.urge_story,
114
+ shortTermPayoff: row.short_term_payoff,
115
+ longTermCost: row.long_term_cost,
116
+ replacementMove: row.replacement_move,
117
+ repairPlan: row.repair_plan,
118
+ linkedPatternIds: filterDeletedIds("behavior_pattern", parseJson(row.linked_pattern_ids_json)),
119
+ linkedValueIds: filterDeletedIds("psyche_value", parseJson(row.linked_value_ids_json)),
120
+ linkedSchemaIds: parseJson(row.linked_schema_ids_json),
121
+ linkedModeIds: filterDeletedIds("mode_profile", parseJson(row.linked_mode_ids_json)),
122
+ createdAt: row.created_at,
123
+ updatedAt: row.updated_at
124
+ });
125
+ }
126
+ function mapBeliefEntry(row) {
127
+ return beliefEntrySchema.parse({
128
+ id: row.id,
129
+ domainId: row.domain_id,
130
+ schemaId: row.schema_id,
131
+ statement: row.statement,
132
+ beliefType: row.belief_type,
133
+ originNote: row.origin_note,
134
+ confidence: row.confidence,
135
+ evidenceFor: parseJson(row.evidence_for_json),
136
+ evidenceAgainst: parseJson(row.evidence_against_json),
137
+ flexibleAlternative: row.flexible_alternative,
138
+ linkedValueIds: filterDeletedIds("psyche_value", parseJson(row.linked_value_ids_json)),
139
+ linkedBehaviorIds: filterDeletedIds("behavior", parseJson(row.linked_behavior_ids_json)),
140
+ linkedModeIds: filterDeletedIds("mode_profile", parseJson(row.linked_mode_ids_json)),
141
+ linkedReportIds: filterDeletedIds("trigger_report", parseJson(row.linked_report_ids_json)),
142
+ createdAt: row.created_at,
143
+ updatedAt: row.updated_at
144
+ });
145
+ }
146
+ function mapModeProfile(row) {
147
+ return modeProfileSchema.parse({
148
+ id: row.id,
149
+ domainId: row.domain_id,
150
+ family: row.family,
151
+ archetype: row.archetype,
152
+ title: row.title,
153
+ persona: row.persona,
154
+ imagery: row.imagery,
155
+ symbolicForm: row.symbolic_form,
156
+ facialExpression: row.facial_expression,
157
+ fear: row.fear,
158
+ burden: row.burden,
159
+ protectiveJob: row.protective_job,
160
+ originContext: row.origin_context,
161
+ firstAppearanceAt: row.first_appearance_at,
162
+ linkedPatternIds: filterDeletedIds("behavior_pattern", parseJson(row.linked_pattern_ids_json)),
163
+ linkedBehaviorIds: filterDeletedIds("behavior", parseJson(row.linked_behavior_ids_json)),
164
+ linkedValueIds: filterDeletedIds("psyche_value", parseJson(row.linked_value_ids_json)),
165
+ createdAt: row.created_at,
166
+ updatedAt: row.updated_at
167
+ });
168
+ }
169
+ function mapModeGuideSession(row) {
170
+ return modeGuideSessionSchema.parse({
171
+ id: row.id,
172
+ summary: row.summary,
173
+ answers: parseJson(row.answers_json),
174
+ results: parseJson(row.results_json),
175
+ createdAt: row.created_at,
176
+ updatedAt: row.updated_at
177
+ });
178
+ }
179
+ function mapTriggerReport(row) {
180
+ const emotions = parseJson(row.emotions_json).map((emotion) => emotion.emotionDefinitionId && isEntityDeleted("emotion_definition", emotion.emotionDefinitionId)
181
+ ? { ...emotion, emotionDefinitionId: null }
182
+ : emotion);
183
+ const thoughts = parseJson(row.thoughts_json).map((thought) => thought.beliefId && isEntityDeleted("belief_entry", thought.beliefId)
184
+ ? { ...thought, beliefId: null }
185
+ : thought);
186
+ const behaviors = parseJson(row.behaviors_json).map((behavior) => behavior.behaviorId && isEntityDeleted("behavior", behavior.behaviorId)
187
+ ? { ...behavior, behaviorId: null }
188
+ : behavior);
189
+ const modeTimeline = parseJson(row.mode_timeline_json).map((entry) => entry.modeId && isEntityDeleted("mode_profile", entry.modeId)
190
+ ? { ...entry, modeId: null }
191
+ : entry);
192
+ return triggerReportSchema.parse({
193
+ id: row.id,
194
+ domainId: row.domain_id,
195
+ title: row.title,
196
+ status: row.status,
197
+ eventTypeId: row.event_type_id && isEntityDeleted("event_type", row.event_type_id) ? null : row.event_type_id,
198
+ customEventType: row.custom_event_type,
199
+ eventSituation: row.event_situation,
200
+ occurredAt: row.occurred_at,
201
+ emotions,
202
+ thoughts,
203
+ behaviors,
204
+ consequences: parseJson(row.consequences_json),
205
+ linkedPatternIds: filterDeletedIds("behavior_pattern", parseJson(row.linked_pattern_ids_json)),
206
+ linkedValueIds: filterDeletedIds("psyche_value", parseJson(row.linked_value_ids_json)),
207
+ linkedGoalIds: filterDeletedIds("goal", parseJson(row.linked_goal_ids_json)),
208
+ linkedProjectIds: filterDeletedIds("project", parseJson(row.linked_project_ids_json)),
209
+ linkedTaskIds: filterDeletedIds("task", parseJson(row.linked_task_ids_json)),
210
+ linkedBehaviorIds: filterDeletedIds("behavior", parseJson(row.linked_behavior_ids_json)),
211
+ linkedBeliefIds: filterDeletedIds("belief_entry", parseJson(row.linked_belief_ids_json)),
212
+ linkedModeIds: filterDeletedIds("mode_profile", parseJson(row.linked_mode_ids_json)),
213
+ modeOverlays: parseJson(row.mode_overlays_json),
214
+ schemaLinks: parseJson(row.schema_links_json),
215
+ modeTimeline,
216
+ nextMoves: parseJson(row.next_moves_json),
217
+ createdAt: row.created_at,
218
+ updatedAt: row.updated_at
219
+ });
220
+ }
221
+ function scoreModeGuideSession(input) {
222
+ const answers = new Map(input.answers.map((answer) => [answer.questionKey, answer.value]));
223
+ const results = [];
224
+ const coping = answers.get("coping_response");
225
+ if (coping && coping !== "none") {
226
+ results.push(modeGuideResultSchema.parse({
227
+ family: "coping",
228
+ archetype: coping,
229
+ label: coping === "fight"
230
+ ? "Fighter"
231
+ : coping === "flight"
232
+ ? "Escaper"
233
+ : coping === "freeze"
234
+ ? "Freezer"
235
+ : coping === "detach"
236
+ ? "Detached protector"
237
+ : coping === "comply"
238
+ ? "Compliant surrender"
239
+ : "Overcompensator",
240
+ confidence: 0.78,
241
+ reasoning: `The coping response leaned most strongly toward ${coping}.`
242
+ }));
243
+ }
244
+ const child = answers.get("child_state");
245
+ if (child && child !== "none") {
246
+ results.push(modeGuideResultSchema.parse({
247
+ family: "child",
248
+ archetype: child,
249
+ label: child === "vulnerable"
250
+ ? "Vulnerable child"
251
+ : child === "angry"
252
+ ? "Angry child"
253
+ : child === "impulsive"
254
+ ? "Impulsive child"
255
+ : child === "lonely"
256
+ ? "Lonely child"
257
+ : "Ashamed child",
258
+ confidence: 0.72,
259
+ reasoning: `The child-state answers cluster around ${child} activation.`
260
+ }));
261
+ }
262
+ const critic = answers.get("critic_style");
263
+ if (critic && critic !== "none") {
264
+ results.push(modeGuideResultSchema.parse({
265
+ family: "critic_parent",
266
+ archetype: critic,
267
+ label: critic === "demanding" ? "Demanding critic" : "Punitive critic",
268
+ confidence: 0.76,
269
+ reasoning: `The inner critical tone reads as ${critic}.`
270
+ }));
271
+ }
272
+ const healthy = answers.get("healthy_contact");
273
+ if (healthy && healthy !== "none") {
274
+ results.push(modeGuideResultSchema.parse({
275
+ family: healthy === "happy_child" ? "happy_child" : "healthy_adult",
276
+ archetype: healthy,
277
+ label: healthy === "happy_child" ? "Happy child" : "Healthy adult",
278
+ confidence: 0.69,
279
+ reasoning: `There is still some contact with ${healthy === "happy_child" ? "playful aliveness" : "steady adult leadership"}.`
280
+ }));
281
+ }
282
+ if (results.length === 0) {
283
+ results.push(modeGuideResultSchema.parse({
284
+ family: "healthy_adult",
285
+ archetype: "undifferentiated",
286
+ label: "Mixed state",
287
+ confidence: 0.41,
288
+ reasoning: "The questionnaire suggests a mixed or unclear state; name the mode manually after reflection."
289
+ }));
290
+ }
291
+ return results;
292
+ }
293
+ function mapCreateUpdateContext(input) {
294
+ recordActivityEvent({
295
+ entityType: input.entityType,
296
+ entityId: input.entityId,
297
+ eventType: input.eventKind,
298
+ title: input.title,
299
+ description: input.title,
300
+ actor: input.actor ?? null,
301
+ source: input.source,
302
+ metadata: input.metadata ?? {}
303
+ });
304
+ recordEventLog({
305
+ eventKind: input.eventKind,
306
+ entityType: input.entityType,
307
+ entityId: input.entityId,
308
+ actor: input.actor ?? null,
309
+ source: input.source,
310
+ metadata: input.metadata ?? {}
311
+ });
312
+ }
313
+ function getRow(sql, id) {
314
+ return getDatabase().prepare(sql).get(id);
315
+ }
316
+ function unlinkEntityNotes(entityType, entityId) {
317
+ unlinkNotesForEntity(entityType, entityId, { source: "system", actor: null });
318
+ }
319
+ function rewriteJsonColumn(table, column, transform) {
320
+ const rows = getDatabase()
321
+ .prepare(`SELECT id, ${column} AS payload FROM ${table}`)
322
+ .all();
323
+ const update = getDatabase().prepare(`UPDATE ${table} SET ${column} = ?, updated_at = ? WHERE id = ?`);
324
+ for (const row of rows) {
325
+ const current = parseJson(row.payload);
326
+ const next = transform(current);
327
+ const currentJson = JSON.stringify(current);
328
+ const nextJson = JSON.stringify(next);
329
+ if (nextJson !== currentJson) {
330
+ update.run(nextJson, new Date().toISOString(), row.id);
331
+ }
332
+ }
333
+ }
334
+ function removeIdFromStringArrayColumn(table, column, targetId) {
335
+ rewriteJsonColumn(table, column, (values) => values.filter((value) => value !== targetId));
336
+ }
337
+ function nullifyTriggerThoughtBeliefReferences(beliefId) {
338
+ rewriteJsonColumn("trigger_reports", "thoughts_json", (thoughts) => thoughts.map((thought) => (thought.beliefId === beliefId ? { ...thought, beliefId: null } : thought)));
339
+ }
340
+ function nullifyTriggerBehaviorReferences(behaviorId) {
341
+ rewriteJsonColumn("trigger_reports", "behaviors_json", (behaviors) => behaviors.map((behavior) => (behavior.behaviorId === behaviorId ? { ...behavior, behaviorId: null } : behavior)));
342
+ }
343
+ function nullifyTriggerEmotionReferences(emotionId) {
344
+ rewriteJsonColumn("trigger_reports", "emotions_json", (emotions) => emotions.map((emotion) => emotion.emotionDefinitionId === emotionId ? { ...emotion, emotionDefinitionId: null } : emotion));
345
+ }
346
+ function nullifyTriggerTimelineModeReferences(modeId) {
347
+ rewriteJsonColumn("trigger_reports", "mode_timeline_json", (entries) => entries.map((entry) => (entry.modeId === modeId ? { ...entry, modeId: null } : entry)));
348
+ }
349
+ export function pruneLinkedEntityReferences(entityType, entityId) {
350
+ const columnByEntityType = {
351
+ goal: "linked_goal_ids_json",
352
+ project: "linked_project_ids_json",
353
+ task: "linked_task_ids_json"
354
+ };
355
+ const column = columnByEntityType[entityType];
356
+ removeIdFromStringArrayColumn("psyche_values", column, entityId);
357
+ removeIdFromStringArrayColumn("trigger_reports", column, entityId);
358
+ }
359
+ export function getPsycheDomain() {
360
+ const row = getDatabase()
361
+ .prepare(`SELECT id, slug, title, description, theme_color, sensitive, created_at, updated_at
362
+ FROM domains
363
+ WHERE id = ?`)
364
+ .get(PSYCHE_DOMAIN_ID);
365
+ return row ? mapDomain(row) : undefined;
366
+ }
367
+ export function listSchemaCatalog() {
368
+ const rows = getDatabase()
369
+ .prepare(`SELECT id, slug, title, family, schema_type, description, created_at, updated_at
370
+ FROM schema_catalog
371
+ ORDER BY CASE schema_type WHEN 'maladaptive' THEN 0 ELSE 1 END, family, title`)
372
+ .all();
373
+ return rows.map(mapSchemaCatalogEntry);
374
+ }
375
+ export function listEventTypes() {
376
+ const rows = getDatabase()
377
+ .prepare(`SELECT id, domain_id, label, description, system, created_at, updated_at
378
+ FROM event_types
379
+ WHERE domain_id = ?
380
+ ORDER BY system DESC, label`)
381
+ .all(PSYCHE_DOMAIN_ID);
382
+ return filterDeletedEntities("event_type", rows.map(mapEventType));
383
+ }
384
+ export function getEventTypeById(eventTypeId) {
385
+ if (isEntityDeleted("event_type", eventTypeId)) {
386
+ return undefined;
387
+ }
388
+ const row = getRow(`SELECT id, domain_id, label, description, system, created_at, updated_at
389
+ FROM event_types
390
+ WHERE id = ?`, eventTypeId);
391
+ return row ? mapEventType(row) : undefined;
392
+ }
393
+ export function createEventType(input, context) {
394
+ const parsed = createEventTypeSchema.parse(input);
395
+ const now = new Date().toISOString();
396
+ const eventType = eventTypeSchema.parse({
397
+ id: buildId("evt"),
398
+ domainId: PSYCHE_DOMAIN_ID,
399
+ label: parsed.label,
400
+ description: parsed.description,
401
+ system: false,
402
+ createdAt: now,
403
+ updatedAt: now
404
+ });
405
+ getDatabase()
406
+ .prepare(`INSERT INTO event_types (id, domain_id, label, description, system, created_at, updated_at)
407
+ VALUES (?, ?, ?, ?, 0, ?, ?)`)
408
+ .run(eventType.id, eventType.domainId, eventType.label, eventType.description, eventType.createdAt, eventType.updatedAt);
409
+ mapCreateUpdateContext({
410
+ entityType: "event_type",
411
+ entityId: eventType.id,
412
+ title: "Event type added",
413
+ eventKind: "event_type.created",
414
+ source: context.source,
415
+ actor: context.actor ?? null,
416
+ metadata: { domainId: eventType.domainId }
417
+ });
418
+ return eventType;
419
+ }
420
+ export function updateEventType(eventTypeId, patch, context) {
421
+ const existing = getEventTypeById(eventTypeId);
422
+ if (!existing) {
423
+ return undefined;
424
+ }
425
+ const parsed = updateEventTypeSchema.parse(patch);
426
+ const updated = eventTypeSchema.parse({
427
+ ...existing,
428
+ ...parsed,
429
+ updatedAt: new Date().toISOString()
430
+ });
431
+ getDatabase()
432
+ .prepare(`UPDATE event_types
433
+ SET label = ?, description = ?, updated_at = ?
434
+ WHERE id = ?`)
435
+ .run(updated.label, updated.description, updated.updatedAt, eventTypeId);
436
+ mapCreateUpdateContext({
437
+ entityType: "event_type",
438
+ entityId: eventTypeId,
439
+ title: "Event type updated",
440
+ eventKind: "event_type.updated",
441
+ source: context.source,
442
+ actor: context.actor ?? null,
443
+ metadata: { domainId: updated.domainId }
444
+ });
445
+ return updated;
446
+ }
447
+ export function deleteEventType(eventTypeId, context) {
448
+ const existing = getEventTypeById(eventTypeId);
449
+ if (!existing) {
450
+ return undefined;
451
+ }
452
+ return runInTransaction(() => {
453
+ unlinkEntityNotes("event_type", eventTypeId);
454
+ getDatabase()
455
+ .prepare(`DELETE FROM event_types WHERE id = ?`)
456
+ .run(eventTypeId);
457
+ mapCreateUpdateContext({
458
+ entityType: "event_type",
459
+ entityId: eventTypeId,
460
+ title: "Event type deleted",
461
+ eventKind: "event_type.deleted",
462
+ source: context.source,
463
+ actor: context.actor ?? null,
464
+ metadata: { domainId: existing.domainId }
465
+ });
466
+ return existing;
467
+ });
468
+ }
469
+ export function listEmotionDefinitions() {
470
+ const rows = getDatabase()
471
+ .prepare(`SELECT id, domain_id, label, description, category, system, created_at, updated_at
472
+ FROM emotion_definitions
473
+ WHERE domain_id = ?
474
+ ORDER BY system DESC, label`)
475
+ .all(PSYCHE_DOMAIN_ID);
476
+ return filterDeletedEntities("emotion_definition", rows.map(mapEmotionDefinition));
477
+ }
478
+ export function getEmotionDefinitionById(emotionId) {
479
+ if (isEntityDeleted("emotion_definition", emotionId)) {
480
+ return undefined;
481
+ }
482
+ const row = getRow(`SELECT id, domain_id, label, description, category, system, created_at, updated_at
483
+ FROM emotion_definitions
484
+ WHERE id = ?`, emotionId);
485
+ return row ? mapEmotionDefinition(row) : undefined;
486
+ }
487
+ export function createEmotionDefinition(input, context) {
488
+ const parsed = createEmotionDefinitionSchema.parse(input);
489
+ const now = new Date().toISOString();
490
+ const emotion = emotionDefinitionSchema.parse({
491
+ id: buildId("emo"),
492
+ domainId: PSYCHE_DOMAIN_ID,
493
+ label: parsed.label,
494
+ description: parsed.description,
495
+ category: parsed.category,
496
+ system: false,
497
+ createdAt: now,
498
+ updatedAt: now
499
+ });
500
+ getDatabase()
501
+ .prepare(`INSERT INTO emotion_definitions (id, domain_id, label, description, category, system, created_at, updated_at)
502
+ VALUES (?, ?, ?, ?, ?, 0, ?, ?)`)
503
+ .run(emotion.id, emotion.domainId, emotion.label, emotion.description, emotion.category, emotion.createdAt, emotion.updatedAt);
504
+ mapCreateUpdateContext({
505
+ entityType: "emotion_definition",
506
+ entityId: emotion.id,
507
+ title: "Emotion definition added",
508
+ eventKind: "emotion_definition.created",
509
+ source: context.source,
510
+ actor: context.actor ?? null,
511
+ metadata: { domainId: emotion.domainId }
512
+ });
513
+ return emotion;
514
+ }
515
+ export function updateEmotionDefinition(emotionId, patch, context) {
516
+ const existing = getEmotionDefinitionById(emotionId);
517
+ if (!existing) {
518
+ return undefined;
519
+ }
520
+ const parsed = updateEmotionDefinitionSchema.parse(patch);
521
+ const updated = emotionDefinitionSchema.parse({
522
+ ...existing,
523
+ ...parsed,
524
+ updatedAt: new Date().toISOString()
525
+ });
526
+ getDatabase()
527
+ .prepare(`UPDATE emotion_definitions
528
+ SET label = ?, description = ?, category = ?, updated_at = ?
529
+ WHERE id = ?`)
530
+ .run(updated.label, updated.description, updated.category, updated.updatedAt, emotionId);
531
+ mapCreateUpdateContext({
532
+ entityType: "emotion_definition",
533
+ entityId: emotionId,
534
+ title: "Emotion definition updated",
535
+ eventKind: "emotion_definition.updated",
536
+ source: context.source,
537
+ actor: context.actor ?? null,
538
+ metadata: { domainId: updated.domainId }
539
+ });
540
+ return updated;
541
+ }
542
+ export function deleteEmotionDefinition(emotionId, context) {
543
+ const existing = getEmotionDefinitionById(emotionId);
544
+ if (!existing) {
545
+ return undefined;
546
+ }
547
+ return runInTransaction(() => {
548
+ nullifyTriggerEmotionReferences(emotionId);
549
+ unlinkEntityNotes("emotion_definition", emotionId);
550
+ getDatabase()
551
+ .prepare(`DELETE FROM emotion_definitions WHERE id = ?`)
552
+ .run(emotionId);
553
+ mapCreateUpdateContext({
554
+ entityType: "emotion_definition",
555
+ entityId: emotionId,
556
+ title: "Emotion definition deleted",
557
+ eventKind: "emotion_definition.deleted",
558
+ source: context.source,
559
+ actor: context.actor ?? null,
560
+ metadata: { domainId: existing.domainId }
561
+ });
562
+ return existing;
563
+ });
564
+ }
565
+ export function listPsycheValues() {
566
+ const rows = getDatabase()
567
+ .prepare(`SELECT
568
+ id, domain_id, title, description, valued_direction, why_it_matters,
569
+ linked_goal_ids_json, linked_project_ids_json, linked_task_ids_json, committed_actions_json, created_at, updated_at
570
+ FROM psyche_values
571
+ WHERE domain_id = ?
572
+ ORDER BY updated_at DESC`)
573
+ .all(PSYCHE_DOMAIN_ID);
574
+ return filterDeletedEntities("psyche_value", rows.map(mapPsycheValue));
575
+ }
576
+ export function getPsycheValueById(valueId) {
577
+ if (isEntityDeleted("psyche_value", valueId)) {
578
+ return undefined;
579
+ }
580
+ const row = getRow(`SELECT
581
+ id, domain_id, title, description, valued_direction, why_it_matters,
582
+ linked_goal_ids_json, linked_project_ids_json, linked_task_ids_json, committed_actions_json, created_at, updated_at
583
+ FROM psyche_values
584
+ WHERE id = ?`, valueId);
585
+ return row ? mapPsycheValue(row) : undefined;
586
+ }
587
+ export function createPsycheValue(input, context) {
588
+ const parsed = createPsycheValueSchema.parse(input);
589
+ const now = new Date().toISOString();
590
+ const value = psycheValueSchema.parse({
591
+ id: buildId("psy"),
592
+ domainId: PSYCHE_DOMAIN_ID,
593
+ title: parsed.title,
594
+ description: parsed.description,
595
+ valuedDirection: parsed.valuedDirection,
596
+ whyItMatters: parsed.whyItMatters,
597
+ linkedGoalIds: parsed.linkedGoalIds,
598
+ linkedProjectIds: parsed.linkedProjectIds,
599
+ linkedTaskIds: parsed.linkedTaskIds,
600
+ committedActions: parsed.committedActions,
601
+ createdAt: now,
602
+ updatedAt: now
603
+ });
604
+ getDatabase()
605
+ .prepare(`INSERT INTO psyche_values (
606
+ id, domain_id, title, description, valued_direction, why_it_matters, linked_goal_ids_json, linked_project_ids_json,
607
+ linked_task_ids_json, committed_actions_json, created_at, updated_at
608
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
609
+ .run(value.id, value.domainId, value.title, value.description, value.valuedDirection, value.whyItMatters, JSON.stringify(value.linkedGoalIds), JSON.stringify(value.linkedProjectIds), JSON.stringify(value.linkedTaskIds), JSON.stringify(value.committedActions), value.createdAt, value.updatedAt);
610
+ mapCreateUpdateContext({
611
+ entityType: "psyche_value",
612
+ entityId: value.id,
613
+ title: "Psyche value added",
614
+ eventKind: "psyche_value.created",
615
+ source: context.source,
616
+ actor: context.actor ?? null,
617
+ metadata: { domainId: value.domainId }
618
+ });
619
+ recordPsycheClarityReward("psyche_value", value.id, value.title, "psyche_value_defined", context);
620
+ return value;
621
+ }
622
+ export function updatePsycheValue(valueId, patch, context) {
623
+ const existing = getPsycheValueById(valueId);
624
+ if (!existing) {
625
+ return undefined;
626
+ }
627
+ const parsed = updatePsycheValueSchema.parse(patch);
628
+ const updated = psycheValueSchema.parse({
629
+ ...existing,
630
+ ...parsed,
631
+ committedActions: parsed.committedActions ?? existing.committedActions,
632
+ updatedAt: new Date().toISOString()
633
+ });
634
+ getDatabase()
635
+ .prepare(`UPDATE psyche_values
636
+ SET title = ?, description = ?, valued_direction = ?, why_it_matters = ?, linked_goal_ids_json = ?,
637
+ linked_project_ids_json = ?, linked_task_ids_json = ?, committed_actions_json = ?, updated_at = ?
638
+ WHERE id = ?`)
639
+ .run(updated.title, updated.description, updated.valuedDirection, updated.whyItMatters, JSON.stringify(updated.linkedGoalIds), JSON.stringify(updated.linkedProjectIds), JSON.stringify(updated.linkedTaskIds), JSON.stringify(updated.committedActions), updated.updatedAt, valueId);
640
+ mapCreateUpdateContext({
641
+ entityType: "psyche_value",
642
+ entityId: valueId,
643
+ title: "Psyche value updated",
644
+ eventKind: "psyche_value.updated",
645
+ source: context.source,
646
+ actor: context.actor ?? null,
647
+ metadata: { domainId: updated.domainId }
648
+ });
649
+ return updated;
650
+ }
651
+ export function deletePsycheValue(valueId, context) {
652
+ const existing = getPsycheValueById(valueId);
653
+ if (!existing) {
654
+ return undefined;
655
+ }
656
+ return runInTransaction(() => {
657
+ removeIdFromStringArrayColumn("behavior_patterns", "linked_value_ids_json", valueId);
658
+ removeIdFromStringArrayColumn("psyche_behaviors", "linked_value_ids_json", valueId);
659
+ removeIdFromStringArrayColumn("belief_entries", "linked_value_ids_json", valueId);
660
+ removeIdFromStringArrayColumn("mode_profiles", "linked_value_ids_json", valueId);
661
+ removeIdFromStringArrayColumn("trigger_reports", "linked_value_ids_json", valueId);
662
+ unlinkEntityNotes("psyche_value", valueId);
663
+ getDatabase()
664
+ .prepare(`DELETE FROM psyche_values WHERE id = ?`)
665
+ .run(valueId);
666
+ mapCreateUpdateContext({
667
+ entityType: "psyche_value",
668
+ entityId: valueId,
669
+ title: "Psyche value deleted",
670
+ eventKind: "psyche_value.deleted",
671
+ source: context.source,
672
+ actor: context.actor ?? null,
673
+ metadata: { domainId: existing.domainId }
674
+ });
675
+ return existing;
676
+ });
677
+ }
678
+ export function listBehaviorPatterns() {
679
+ const rows = getDatabase()
680
+ .prepare(`SELECT
681
+ id, domain_id, title, description, target_behavior, cue_contexts_json, short_term_payoff, long_term_cost,
682
+ preferred_response, linked_value_ids_json, linked_schema_labels_json, linked_mode_labels_json, linked_mode_ids_json,
683
+ linked_belief_ids_json, created_at, updated_at
684
+ FROM behavior_patterns
685
+ WHERE domain_id = ?
686
+ ORDER BY updated_at DESC`)
687
+ .all(PSYCHE_DOMAIN_ID);
688
+ return filterDeletedEntities("behavior_pattern", rows.map(mapBehaviorPattern));
689
+ }
690
+ export function getBehaviorPatternById(patternId) {
691
+ if (isEntityDeleted("behavior_pattern", patternId)) {
692
+ return undefined;
693
+ }
694
+ const row = getRow(`SELECT
695
+ id, domain_id, title, description, target_behavior, cue_contexts_json, short_term_payoff, long_term_cost,
696
+ preferred_response, linked_value_ids_json, linked_schema_labels_json, linked_mode_labels_json, linked_mode_ids_json,
697
+ linked_belief_ids_json, created_at, updated_at
698
+ FROM behavior_patterns
699
+ WHERE id = ?`, patternId);
700
+ return row ? mapBehaviorPattern(row) : undefined;
701
+ }
702
+ export function createBehaviorPattern(input, context) {
703
+ const parsed = createBehaviorPatternSchema.parse(input);
704
+ const now = new Date().toISOString();
705
+ const pattern = behaviorPatternSchema.parse({
706
+ id: buildId("pat"),
707
+ domainId: PSYCHE_DOMAIN_ID,
708
+ title: parsed.title,
709
+ description: parsed.description,
710
+ targetBehavior: parsed.targetBehavior,
711
+ cueContexts: parsed.cueContexts,
712
+ shortTermPayoff: parsed.shortTermPayoff,
713
+ longTermCost: parsed.longTermCost,
714
+ preferredResponse: parsed.preferredResponse,
715
+ linkedValueIds: parsed.linkedValueIds,
716
+ linkedSchemaLabels: parsed.linkedSchemaLabels,
717
+ linkedModeLabels: parsed.linkedModeLabels,
718
+ linkedModeIds: parsed.linkedModeIds,
719
+ linkedBeliefIds: parsed.linkedBeliefIds,
720
+ createdAt: now,
721
+ updatedAt: now
722
+ });
723
+ getDatabase()
724
+ .prepare(`INSERT INTO behavior_patterns (
725
+ id, domain_id, title, description, target_behavior, cue_contexts_json, short_term_payoff, long_term_cost,
726
+ preferred_response, linked_value_ids_json, linked_schema_labels_json, linked_mode_labels_json, linked_mode_ids_json,
727
+ linked_belief_ids_json, created_at, updated_at
728
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
729
+ .run(pattern.id, pattern.domainId, pattern.title, pattern.description, pattern.targetBehavior, JSON.stringify(pattern.cueContexts), pattern.shortTermPayoff, pattern.longTermCost, pattern.preferredResponse, JSON.stringify(pattern.linkedValueIds), JSON.stringify(pattern.linkedSchemaLabels), JSON.stringify(pattern.linkedModeLabels), JSON.stringify(pattern.linkedModeIds), JSON.stringify(pattern.linkedBeliefIds), pattern.createdAt, pattern.updatedAt);
730
+ mapCreateUpdateContext({
731
+ entityType: "behavior_pattern",
732
+ entityId: pattern.id,
733
+ title: "Behavior pattern added",
734
+ eventKind: "behavior_pattern.created",
735
+ source: context.source,
736
+ actor: context.actor ?? null,
737
+ metadata: { domainId: pattern.domainId }
738
+ });
739
+ recordPsycheClarityReward("behavior_pattern", pattern.id, pattern.title, "psyche_pattern_defined", context);
740
+ return pattern;
741
+ }
742
+ export function updateBehaviorPattern(patternId, patch, context) {
743
+ const existing = getBehaviorPatternById(patternId);
744
+ if (!existing) {
745
+ return undefined;
746
+ }
747
+ const parsed = updateBehaviorPatternSchema.parse(patch);
748
+ const updated = behaviorPatternSchema.parse({
749
+ ...existing,
750
+ ...parsed,
751
+ updatedAt: new Date().toISOString()
752
+ });
753
+ getDatabase()
754
+ .prepare(`UPDATE behavior_patterns
755
+ SET title = ?, description = ?, target_behavior = ?, cue_contexts_json = ?, short_term_payoff = ?, long_term_cost = ?,
756
+ preferred_response = ?, linked_value_ids_json = ?, linked_schema_labels_json = ?, linked_mode_labels_json = ?,
757
+ linked_mode_ids_json = ?, linked_belief_ids_json = ?, updated_at = ?
758
+ WHERE id = ?`)
759
+ .run(updated.title, updated.description, updated.targetBehavior, JSON.stringify(updated.cueContexts), updated.shortTermPayoff, updated.longTermCost, updated.preferredResponse, JSON.stringify(updated.linkedValueIds), JSON.stringify(updated.linkedSchemaLabels), JSON.stringify(updated.linkedModeLabels), JSON.stringify(updated.linkedModeIds), JSON.stringify(updated.linkedBeliefIds), updated.updatedAt, patternId);
760
+ mapCreateUpdateContext({
761
+ entityType: "behavior_pattern",
762
+ entityId: patternId,
763
+ title: "Behavior pattern updated",
764
+ eventKind: "behavior_pattern.updated",
765
+ source: context.source,
766
+ actor: context.actor ?? null,
767
+ metadata: { domainId: updated.domainId }
768
+ });
769
+ return updated;
770
+ }
771
+ export function deleteBehaviorPattern(patternId, context) {
772
+ const existing = getBehaviorPatternById(patternId);
773
+ if (!existing) {
774
+ return undefined;
775
+ }
776
+ return runInTransaction(() => {
777
+ removeIdFromStringArrayColumn("psyche_behaviors", "linked_pattern_ids_json", patternId);
778
+ removeIdFromStringArrayColumn("mode_profiles", "linked_pattern_ids_json", patternId);
779
+ removeIdFromStringArrayColumn("trigger_reports", "linked_pattern_ids_json", patternId);
780
+ unlinkEntityNotes("behavior_pattern", patternId);
781
+ getDatabase()
782
+ .prepare(`DELETE FROM behavior_patterns WHERE id = ?`)
783
+ .run(patternId);
784
+ mapCreateUpdateContext({
785
+ entityType: "behavior_pattern",
786
+ entityId: patternId,
787
+ title: "Behavior pattern deleted",
788
+ eventKind: "behavior_pattern.deleted",
789
+ source: context.source,
790
+ actor: context.actor ?? null,
791
+ metadata: { domainId: existing.domainId }
792
+ });
793
+ return existing;
794
+ });
795
+ }
796
+ export function listBehaviors() {
797
+ const rows = getDatabase()
798
+ .prepare(`SELECT
799
+ id, domain_id, kind, title, description, common_cues_json, urge_story, short_term_payoff, long_term_cost,
800
+ replacement_move, repair_plan, linked_pattern_ids_json, linked_value_ids_json, linked_schema_ids_json,
801
+ linked_mode_ids_json, created_at, updated_at
802
+ FROM psyche_behaviors
803
+ WHERE domain_id = ?
804
+ ORDER BY kind, updated_at DESC`)
805
+ .all(PSYCHE_DOMAIN_ID);
806
+ return filterDeletedEntities("behavior", rows.map(mapBehavior));
807
+ }
808
+ export function getBehaviorById(behaviorId) {
809
+ if (isEntityDeleted("behavior", behaviorId)) {
810
+ return undefined;
811
+ }
812
+ const row = getRow(`SELECT
813
+ id, domain_id, kind, title, description, common_cues_json, urge_story, short_term_payoff, long_term_cost,
814
+ replacement_move, repair_plan, linked_pattern_ids_json, linked_value_ids_json, linked_schema_ids_json,
815
+ linked_mode_ids_json, created_at, updated_at
816
+ FROM psyche_behaviors
817
+ WHERE id = ?`, behaviorId);
818
+ return row ? mapBehavior(row) : undefined;
819
+ }
820
+ export function createBehavior(input, context) {
821
+ const parsed = createBehaviorSchema.parse(input);
822
+ const now = new Date().toISOString();
823
+ const behavior = behaviorSchema.parse({
824
+ id: buildId("bhv"),
825
+ domainId: PSYCHE_DOMAIN_ID,
826
+ ...parsed,
827
+ createdAt: now,
828
+ updatedAt: now
829
+ });
830
+ getDatabase()
831
+ .prepare(`INSERT INTO psyche_behaviors (
832
+ id, domain_id, kind, title, description, common_cues_json, urge_story, short_term_payoff, long_term_cost,
833
+ replacement_move, repair_plan, linked_pattern_ids_json, linked_value_ids_json, linked_schema_ids_json, linked_mode_ids_json,
834
+ created_at, updated_at
835
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
836
+ .run(behavior.id, behavior.domainId, behavior.kind, behavior.title, behavior.description, JSON.stringify(behavior.commonCues), behavior.urgeStory, behavior.shortTermPayoff, behavior.longTermCost, behavior.replacementMove, behavior.repairPlan, JSON.stringify(behavior.linkedPatternIds), JSON.stringify(behavior.linkedValueIds), JSON.stringify(behavior.linkedSchemaIds), JSON.stringify(behavior.linkedModeIds), behavior.createdAt, behavior.updatedAt);
837
+ mapCreateUpdateContext({
838
+ entityType: "behavior",
839
+ entityId: behavior.id,
840
+ title: "Behavior added",
841
+ eventKind: "behavior.created",
842
+ source: context.source,
843
+ actor: context.actor ?? null,
844
+ metadata: { kind: behavior.kind, domainId: behavior.domainId }
845
+ });
846
+ recordPsycheClarityReward("behavior", behavior.id, behavior.title, "psyche_behavior_defined", context);
847
+ return behavior;
848
+ }
849
+ export function updateBehavior(behaviorId, patch, context) {
850
+ const existing = getBehaviorById(behaviorId);
851
+ if (!existing) {
852
+ return undefined;
853
+ }
854
+ const parsed = updateBehaviorSchema.parse(patch);
855
+ const updated = behaviorSchema.parse({
856
+ ...existing,
857
+ ...parsed,
858
+ updatedAt: new Date().toISOString()
859
+ });
860
+ getDatabase()
861
+ .prepare(`UPDATE psyche_behaviors
862
+ SET kind = ?, title = ?, description = ?, common_cues_json = ?, urge_story = ?, short_term_payoff = ?, long_term_cost = ?,
863
+ replacement_move = ?, repair_plan = ?, linked_pattern_ids_json = ?, linked_value_ids_json = ?, linked_schema_ids_json = ?,
864
+ linked_mode_ids_json = ?, updated_at = ?
865
+ WHERE id = ?`)
866
+ .run(updated.kind, updated.title, updated.description, JSON.stringify(updated.commonCues), updated.urgeStory, updated.shortTermPayoff, updated.longTermCost, updated.replacementMove, updated.repairPlan, JSON.stringify(updated.linkedPatternIds), JSON.stringify(updated.linkedValueIds), JSON.stringify(updated.linkedSchemaIds), JSON.stringify(updated.linkedModeIds), updated.updatedAt, behaviorId);
867
+ mapCreateUpdateContext({
868
+ entityType: "behavior",
869
+ entityId: behaviorId,
870
+ title: "Behavior updated",
871
+ eventKind: "behavior.updated",
872
+ source: context.source,
873
+ actor: context.actor ?? null,
874
+ metadata: { kind: updated.kind, domainId: updated.domainId }
875
+ });
876
+ return updated;
877
+ }
878
+ export function deleteBehavior(behaviorId, context) {
879
+ const existing = getBehaviorById(behaviorId);
880
+ if (!existing) {
881
+ return undefined;
882
+ }
883
+ return runInTransaction(() => {
884
+ removeIdFromStringArrayColumn("belief_entries", "linked_behavior_ids_json", behaviorId);
885
+ removeIdFromStringArrayColumn("mode_profiles", "linked_behavior_ids_json", behaviorId);
886
+ removeIdFromStringArrayColumn("trigger_reports", "linked_behavior_ids_json", behaviorId);
887
+ nullifyTriggerBehaviorReferences(behaviorId);
888
+ unlinkEntityNotes("behavior", behaviorId);
889
+ getDatabase()
890
+ .prepare(`DELETE FROM psyche_behaviors WHERE id = ?`)
891
+ .run(behaviorId);
892
+ mapCreateUpdateContext({
893
+ entityType: "behavior",
894
+ entityId: behaviorId,
895
+ title: "Behavior deleted",
896
+ eventKind: "behavior.deleted",
897
+ source: context.source,
898
+ actor: context.actor ?? null,
899
+ metadata: { kind: existing.kind, domainId: existing.domainId }
900
+ });
901
+ return existing;
902
+ });
903
+ }
904
+ export function listBeliefEntries() {
905
+ const rows = getDatabase()
906
+ .prepare(`SELECT
907
+ id, domain_id, schema_id, statement, belief_type, origin_note, confidence, evidence_for_json, evidence_against_json,
908
+ flexible_alternative, linked_value_ids_json, linked_behavior_ids_json, linked_mode_ids_json, linked_report_ids_json,
909
+ created_at, updated_at
910
+ FROM belief_entries
911
+ WHERE domain_id = ?
912
+ ORDER BY updated_at DESC`)
913
+ .all(PSYCHE_DOMAIN_ID);
914
+ return filterDeletedEntities("belief_entry", rows.map(mapBeliefEntry));
915
+ }
916
+ export function getBeliefEntryById(beliefId) {
917
+ if (isEntityDeleted("belief_entry", beliefId)) {
918
+ return undefined;
919
+ }
920
+ const row = getRow(`SELECT
921
+ id, domain_id, schema_id, statement, belief_type, origin_note, confidence, evidence_for_json, evidence_against_json,
922
+ flexible_alternative, linked_value_ids_json, linked_behavior_ids_json, linked_mode_ids_json, linked_report_ids_json,
923
+ created_at, updated_at
924
+ FROM belief_entries
925
+ WHERE id = ?`, beliefId);
926
+ return row ? mapBeliefEntry(row) : undefined;
927
+ }
928
+ export function createBeliefEntry(input, context) {
929
+ const parsed = createBeliefEntrySchema.parse(input);
930
+ const now = new Date().toISOString();
931
+ const belief = beliefEntrySchema.parse({
932
+ id: buildId("blf"),
933
+ domainId: PSYCHE_DOMAIN_ID,
934
+ ...parsed,
935
+ createdAt: now,
936
+ updatedAt: now
937
+ });
938
+ getDatabase()
939
+ .prepare(`INSERT INTO belief_entries (
940
+ id, domain_id, schema_id, statement, belief_type, origin_note, confidence, evidence_for_json, evidence_against_json,
941
+ flexible_alternative, linked_value_ids_json, linked_behavior_ids_json, linked_mode_ids_json, linked_report_ids_json,
942
+ created_at, updated_at
943
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
944
+ .run(belief.id, belief.domainId, belief.schemaId, belief.statement, belief.beliefType, belief.originNote, belief.confidence, JSON.stringify(belief.evidenceFor), JSON.stringify(belief.evidenceAgainst), belief.flexibleAlternative, JSON.stringify(belief.linkedValueIds), JSON.stringify(belief.linkedBehaviorIds), JSON.stringify(belief.linkedModeIds), JSON.stringify(belief.linkedReportIds), belief.createdAt, belief.updatedAt);
945
+ mapCreateUpdateContext({
946
+ entityType: "belief_entry",
947
+ entityId: belief.id,
948
+ title: "Belief captured",
949
+ eventKind: "belief_entry.created",
950
+ source: context.source,
951
+ actor: context.actor ?? null,
952
+ metadata: { schemaId: belief.schemaId ?? "" }
953
+ });
954
+ recordPsycheClarityReward("belief_entry", belief.id, belief.statement, "psyche_belief_captured", context);
955
+ return belief;
956
+ }
957
+ export function updateBeliefEntry(beliefId, patch, context) {
958
+ const existing = getBeliefEntryById(beliefId);
959
+ if (!existing) {
960
+ return undefined;
961
+ }
962
+ const parsed = updateBeliefEntrySchema.parse(patch);
963
+ const updated = beliefEntrySchema.parse({
964
+ ...existing,
965
+ ...parsed,
966
+ updatedAt: new Date().toISOString()
967
+ });
968
+ getDatabase()
969
+ .prepare(`UPDATE belief_entries
970
+ SET schema_id = ?, statement = ?, belief_type = ?, origin_note = ?, confidence = ?, evidence_for_json = ?,
971
+ evidence_against_json = ?, flexible_alternative = ?, linked_value_ids_json = ?, linked_behavior_ids_json = ?,
972
+ linked_mode_ids_json = ?, linked_report_ids_json = ?, updated_at = ?
973
+ WHERE id = ?`)
974
+ .run(updated.schemaId, updated.statement, updated.beliefType, updated.originNote, updated.confidence, JSON.stringify(updated.evidenceFor), JSON.stringify(updated.evidenceAgainst), updated.flexibleAlternative, JSON.stringify(updated.linkedValueIds), JSON.stringify(updated.linkedBehaviorIds), JSON.stringify(updated.linkedModeIds), JSON.stringify(updated.linkedReportIds), updated.updatedAt, beliefId);
975
+ mapCreateUpdateContext({
976
+ entityType: "belief_entry",
977
+ entityId: beliefId,
978
+ title: "Belief updated",
979
+ eventKind: "belief_entry.updated",
980
+ source: context.source,
981
+ actor: context.actor ?? null,
982
+ metadata: { schemaId: updated.schemaId ?? "" }
983
+ });
984
+ return updated;
985
+ }
986
+ export function deleteBeliefEntry(beliefId, context) {
987
+ const existing = getBeliefEntryById(beliefId);
988
+ if (!existing) {
989
+ return undefined;
990
+ }
991
+ return runInTransaction(() => {
992
+ removeIdFromStringArrayColumn("behavior_patterns", "linked_belief_ids_json", beliefId);
993
+ removeIdFromStringArrayColumn("trigger_reports", "linked_belief_ids_json", beliefId);
994
+ nullifyTriggerThoughtBeliefReferences(beliefId);
995
+ unlinkEntityNotes("belief_entry", beliefId);
996
+ getDatabase()
997
+ .prepare(`DELETE FROM belief_entries WHERE id = ?`)
998
+ .run(beliefId);
999
+ mapCreateUpdateContext({
1000
+ entityType: "belief_entry",
1001
+ entityId: beliefId,
1002
+ title: "Belief deleted",
1003
+ eventKind: "belief_entry.deleted",
1004
+ source: context.source,
1005
+ actor: context.actor ?? null,
1006
+ metadata: { schemaId: existing.schemaId ?? "" }
1007
+ });
1008
+ return existing;
1009
+ });
1010
+ }
1011
+ export function listModeProfiles() {
1012
+ const rows = getDatabase()
1013
+ .prepare(`SELECT
1014
+ id, domain_id, family, archetype, title, persona, imagery, symbolic_form, facial_expression, fear, burden, protective_job,
1015
+ origin_context, first_appearance_at, linked_pattern_ids_json, linked_behavior_ids_json, linked_value_ids_json, created_at, updated_at
1016
+ FROM mode_profiles
1017
+ WHERE domain_id = ?
1018
+ ORDER BY family, updated_at DESC`)
1019
+ .all(PSYCHE_DOMAIN_ID);
1020
+ return filterDeletedEntities("mode_profile", rows.map(mapModeProfile));
1021
+ }
1022
+ export function getModeProfileById(modeId) {
1023
+ if (isEntityDeleted("mode_profile", modeId)) {
1024
+ return undefined;
1025
+ }
1026
+ const row = getRow(`SELECT
1027
+ id, domain_id, family, archetype, title, persona, imagery, symbolic_form, facial_expression, fear, burden, protective_job,
1028
+ origin_context, first_appearance_at, linked_pattern_ids_json, linked_behavior_ids_json, linked_value_ids_json, created_at, updated_at
1029
+ FROM mode_profiles
1030
+ WHERE id = ?`, modeId);
1031
+ return row ? mapModeProfile(row) : undefined;
1032
+ }
1033
+ export function createModeProfile(input, context) {
1034
+ const parsed = createModeProfileSchema.parse(input);
1035
+ const now = new Date().toISOString();
1036
+ const mode = modeProfileSchema.parse({
1037
+ id: buildId("mod"),
1038
+ domainId: PSYCHE_DOMAIN_ID,
1039
+ ...parsed,
1040
+ createdAt: now,
1041
+ updatedAt: now
1042
+ });
1043
+ getDatabase()
1044
+ .prepare(`INSERT INTO mode_profiles (
1045
+ id, domain_id, family, archetype, title, persona, imagery, symbolic_form, facial_expression, fear, burden, protective_job,
1046
+ origin_context, first_appearance_at, linked_pattern_ids_json, linked_behavior_ids_json, linked_value_ids_json, created_at, updated_at
1047
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
1048
+ .run(mode.id, mode.domainId, mode.family, mode.archetype, mode.title, mode.persona, mode.imagery, mode.symbolicForm, mode.facialExpression, mode.fear, mode.burden, mode.protectiveJob, mode.originContext, mode.firstAppearanceAt, JSON.stringify(mode.linkedPatternIds), JSON.stringify(mode.linkedBehaviorIds), JSON.stringify(mode.linkedValueIds), mode.createdAt, mode.updatedAt);
1049
+ mapCreateUpdateContext({
1050
+ entityType: "mode_profile",
1051
+ entityId: mode.id,
1052
+ title: "Mode profile added",
1053
+ eventKind: "mode_profile.created",
1054
+ source: context.source,
1055
+ actor: context.actor ?? null,
1056
+ metadata: { family: mode.family }
1057
+ });
1058
+ recordPsycheClarityReward("mode_profile", mode.id, mode.title, "psyche_mode_named", context);
1059
+ return mode;
1060
+ }
1061
+ export function updateModeProfile(modeId, patch, context) {
1062
+ const existing = getModeProfileById(modeId);
1063
+ if (!existing) {
1064
+ return undefined;
1065
+ }
1066
+ const parsed = updateModeProfileSchema.parse(patch);
1067
+ if (parsed.family) {
1068
+ modeFamilySchema.parse(parsed.family);
1069
+ }
1070
+ const updated = modeProfileSchema.parse({
1071
+ ...existing,
1072
+ ...parsed,
1073
+ updatedAt: new Date().toISOString()
1074
+ });
1075
+ getDatabase()
1076
+ .prepare(`UPDATE mode_profiles
1077
+ SET family = ?, archetype = ?, title = ?, persona = ?, imagery = ?, symbolic_form = ?, facial_expression = ?, fear = ?,
1078
+ burden = ?, protective_job = ?, origin_context = ?, first_appearance_at = ?, linked_pattern_ids_json = ?,
1079
+ linked_behavior_ids_json = ?, linked_value_ids_json = ?, updated_at = ?
1080
+ WHERE id = ?`)
1081
+ .run(updated.family, updated.archetype, updated.title, updated.persona, updated.imagery, updated.symbolicForm, updated.facialExpression, updated.fear, updated.burden, updated.protectiveJob, updated.originContext, updated.firstAppearanceAt, JSON.stringify(updated.linkedPatternIds), JSON.stringify(updated.linkedBehaviorIds), JSON.stringify(updated.linkedValueIds), updated.updatedAt, modeId);
1082
+ mapCreateUpdateContext({
1083
+ entityType: "mode_profile",
1084
+ entityId: modeId,
1085
+ title: "Mode profile updated",
1086
+ eventKind: "mode_profile.updated",
1087
+ source: context.source,
1088
+ actor: context.actor ?? null,
1089
+ metadata: { family: updated.family }
1090
+ });
1091
+ return updated;
1092
+ }
1093
+ export function deleteModeProfile(modeId, context) {
1094
+ const existing = getModeProfileById(modeId);
1095
+ if (!existing) {
1096
+ return undefined;
1097
+ }
1098
+ return runInTransaction(() => {
1099
+ removeIdFromStringArrayColumn("behavior_patterns", "linked_mode_ids_json", modeId);
1100
+ removeIdFromStringArrayColumn("psyche_behaviors", "linked_mode_ids_json", modeId);
1101
+ removeIdFromStringArrayColumn("belief_entries", "linked_mode_ids_json", modeId);
1102
+ removeIdFromStringArrayColumn("trigger_reports", "linked_mode_ids_json", modeId);
1103
+ nullifyTriggerTimelineModeReferences(modeId);
1104
+ unlinkEntityNotes("mode_profile", modeId);
1105
+ getDatabase()
1106
+ .prepare(`DELETE FROM mode_profiles WHERE id = ?`)
1107
+ .run(modeId);
1108
+ mapCreateUpdateContext({
1109
+ entityType: "mode_profile",
1110
+ entityId: modeId,
1111
+ title: "Mode profile deleted",
1112
+ eventKind: "mode_profile.deleted",
1113
+ source: context.source,
1114
+ actor: context.actor ?? null,
1115
+ metadata: { family: existing.family }
1116
+ });
1117
+ return existing;
1118
+ });
1119
+ }
1120
+ export function listModeGuideSessions(limit = 20) {
1121
+ const rows = getDatabase()
1122
+ .prepare(`SELECT id, summary, answers_json, results_json, created_at, updated_at
1123
+ FROM mode_guide_sessions
1124
+ ORDER BY created_at DESC
1125
+ LIMIT ?`)
1126
+ .all(limit);
1127
+ return filterDeletedEntities("mode_guide_session", rows.map(mapModeGuideSession));
1128
+ }
1129
+ export function getModeGuideSessionById(sessionId) {
1130
+ if (isEntityDeleted("mode_guide_session", sessionId)) {
1131
+ return undefined;
1132
+ }
1133
+ const row = getRow(`SELECT id, summary, answers_json, results_json, created_at, updated_at
1134
+ FROM mode_guide_sessions
1135
+ WHERE id = ?`, sessionId);
1136
+ return row ? mapModeGuideSession(row) : undefined;
1137
+ }
1138
+ export function createModeGuideSession(input, context) {
1139
+ const parsed = createModeGuideSessionSchema.parse(input);
1140
+ const now = new Date().toISOString();
1141
+ const session = modeGuideSessionSchema.parse({
1142
+ id: buildId("mgs"),
1143
+ summary: parsed.summary,
1144
+ answers: parsed.answers,
1145
+ results: scoreModeGuideSession(parsed),
1146
+ createdAt: now,
1147
+ updatedAt: now
1148
+ });
1149
+ getDatabase()
1150
+ .prepare(`INSERT INTO mode_guide_sessions (id, summary, answers_json, results_json, created_at, updated_at)
1151
+ VALUES (?, ?, ?, ?, ?, ?)`)
1152
+ .run(session.id, session.summary, JSON.stringify(session.answers), JSON.stringify(session.results), session.createdAt, session.updatedAt);
1153
+ recordEventLog({
1154
+ eventKind: "mode_guide_session.created",
1155
+ entityType: "system",
1156
+ entityId: session.id,
1157
+ actor: context.actor ?? null,
1158
+ source: context.source,
1159
+ metadata: { summary: session.summary }
1160
+ });
1161
+ return session;
1162
+ }
1163
+ export function updateModeGuideSession(sessionId, patch, context) {
1164
+ const existing = getModeGuideSessionById(sessionId);
1165
+ if (!existing) {
1166
+ return undefined;
1167
+ }
1168
+ const parsed = updateModeGuideSessionSchema.parse(patch);
1169
+ const summary = parsed.summary ?? existing.summary;
1170
+ const answers = parsed.answers ?? existing.answers;
1171
+ const updated = modeGuideSessionSchema.parse({
1172
+ ...existing,
1173
+ summary,
1174
+ answers,
1175
+ results: scoreModeGuideSession({ summary, answers }),
1176
+ updatedAt: new Date().toISOString()
1177
+ });
1178
+ getDatabase()
1179
+ .prepare(`UPDATE mode_guide_sessions
1180
+ SET summary = ?, answers_json = ?, results_json = ?, updated_at = ?
1181
+ WHERE id = ?`)
1182
+ .run(updated.summary, JSON.stringify(updated.answers), JSON.stringify(updated.results), updated.updatedAt, sessionId);
1183
+ recordEventLog({
1184
+ eventKind: "mode_guide_session.updated",
1185
+ entityType: "system",
1186
+ entityId: sessionId,
1187
+ actor: context.actor ?? null,
1188
+ source: context.source,
1189
+ metadata: { summary: updated.summary }
1190
+ });
1191
+ return updated;
1192
+ }
1193
+ export function deleteModeGuideSession(sessionId, context) {
1194
+ const existing = getModeGuideSessionById(sessionId);
1195
+ if (!existing) {
1196
+ return undefined;
1197
+ }
1198
+ return runInTransaction(() => {
1199
+ getDatabase()
1200
+ .prepare(`DELETE FROM mode_guide_sessions WHERE id = ?`)
1201
+ .run(sessionId);
1202
+ recordEventLog({
1203
+ eventKind: "mode_guide_session.deleted",
1204
+ entityType: "system",
1205
+ entityId: sessionId,
1206
+ actor: context.actor ?? null,
1207
+ source: context.source,
1208
+ metadata: { summary: existing.summary }
1209
+ });
1210
+ return existing;
1211
+ });
1212
+ }
1213
+ export function listTriggerReports(limit) {
1214
+ const limitSql = limit ? "LIMIT ?" : "";
1215
+ const rows = getDatabase()
1216
+ .prepare(`SELECT
1217
+ id, domain_id, title, status, event_type_id, custom_event_type, event_situation, occurred_at, emotions_json, thoughts_json,
1218
+ behaviors_json, consequences_json, linked_pattern_ids_json, linked_value_ids_json, linked_goal_ids_json, linked_project_ids_json,
1219
+ linked_task_ids_json, linked_behavior_ids_json, linked_belief_ids_json, linked_mode_ids_json, mode_overlays_json,
1220
+ schema_links_json, mode_timeline_json, next_moves_json, created_at, updated_at
1221
+ FROM trigger_reports
1222
+ WHERE domain_id = ?
1223
+ ORDER BY updated_at DESC
1224
+ ${limitSql}`)
1225
+ .all(...(limit ? [PSYCHE_DOMAIN_ID, limit] : [PSYCHE_DOMAIN_ID]));
1226
+ return filterDeletedEntities("trigger_report", rows.map(mapTriggerReport));
1227
+ }
1228
+ export function getTriggerReportById(reportId) {
1229
+ if (isEntityDeleted("trigger_report", reportId)) {
1230
+ return undefined;
1231
+ }
1232
+ const row = getRow(`SELECT
1233
+ id, domain_id, title, status, event_type_id, custom_event_type, event_situation, occurred_at, emotions_json, thoughts_json,
1234
+ behaviors_json, consequences_json, linked_pattern_ids_json, linked_value_ids_json, linked_goal_ids_json, linked_project_ids_json,
1235
+ linked_task_ids_json, linked_behavior_ids_json, linked_belief_ids_json, linked_mode_ids_json, mode_overlays_json,
1236
+ schema_links_json, mode_timeline_json, next_moves_json, created_at, updated_at
1237
+ FROM trigger_reports
1238
+ WHERE id = ?`, reportId);
1239
+ return row ? mapTriggerReport(row) : undefined;
1240
+ }
1241
+ export function createTriggerReport(input, context) {
1242
+ const parsed = createTriggerReportSchema.parse(input);
1243
+ const now = new Date().toISOString();
1244
+ const report = triggerReportSchema.parse({
1245
+ id: buildId("trg"),
1246
+ domainId: PSYCHE_DOMAIN_ID,
1247
+ title: parsed.title,
1248
+ status: parsed.status,
1249
+ eventTypeId: parsed.eventTypeId,
1250
+ customEventType: parsed.customEventType,
1251
+ eventSituation: parsed.eventSituation,
1252
+ occurredAt: parsed.occurredAt,
1253
+ emotions: enrichTriggerItems(parsed.emotions, "emo"),
1254
+ thoughts: enrichTriggerItems(parsed.thoughts, "tht"),
1255
+ behaviors: enrichTriggerItems(parsed.behaviors, "beh"),
1256
+ consequences: parsed.consequences,
1257
+ linkedPatternIds: parsed.linkedPatternIds,
1258
+ linkedValueIds: parsed.linkedValueIds,
1259
+ linkedGoalIds: parsed.linkedGoalIds,
1260
+ linkedProjectIds: parsed.linkedProjectIds,
1261
+ linkedTaskIds: parsed.linkedTaskIds,
1262
+ linkedBehaviorIds: parsed.linkedBehaviorIds,
1263
+ linkedBeliefIds: parsed.linkedBeliefIds,
1264
+ linkedModeIds: parsed.linkedModeIds,
1265
+ modeOverlays: parsed.modeOverlays,
1266
+ schemaLinks: parsed.schemaLinks,
1267
+ modeTimeline: enrichTriggerItems(parsed.modeTimeline, "mdl").map((entry) => modeTimelineEntrySchema.parse(entry)),
1268
+ nextMoves: parsed.nextMoves,
1269
+ createdAt: now,
1270
+ updatedAt: now
1271
+ });
1272
+ getDatabase()
1273
+ .prepare(`INSERT INTO trigger_reports (
1274
+ id, domain_id, title, status, event_type_id, custom_event_type, event_situation, occurred_at, emotions_json, thoughts_json, behaviors_json, consequences_json,
1275
+ linked_pattern_ids_json, linked_value_ids_json, linked_goal_ids_json, linked_project_ids_json, linked_task_ids_json,
1276
+ linked_behavior_ids_json, linked_belief_ids_json, linked_mode_ids_json, mode_overlays_json, schema_links_json, mode_timeline_json,
1277
+ next_moves_json, created_at, updated_at
1278
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
1279
+ .run(report.id, report.domainId, report.title, report.status, report.eventTypeId, report.customEventType, report.eventSituation, report.occurredAt, JSON.stringify(report.emotions), JSON.stringify(report.thoughts), JSON.stringify(report.behaviors), JSON.stringify(report.consequences), JSON.stringify(report.linkedPatternIds), JSON.stringify(report.linkedValueIds), JSON.stringify(report.linkedGoalIds), JSON.stringify(report.linkedProjectIds), JSON.stringify(report.linkedTaskIds), JSON.stringify(report.linkedBehaviorIds), JSON.stringify(report.linkedBeliefIds), JSON.stringify(report.linkedModeIds), JSON.stringify(report.modeOverlays), JSON.stringify(report.schemaLinks), JSON.stringify(report.modeTimeline), JSON.stringify(report.nextMoves), report.createdAt, report.updatedAt);
1280
+ mapCreateUpdateContext({
1281
+ entityType: "trigger_report",
1282
+ entityId: report.id,
1283
+ title: "Trigger report captured",
1284
+ eventKind: "trigger_report.created",
1285
+ source: context.source,
1286
+ actor: context.actor ?? null,
1287
+ metadata: {
1288
+ domainId: report.domainId,
1289
+ status: report.status
1290
+ }
1291
+ });
1292
+ recordPsycheReflectionReward(report.id, report.title, { actor: context.actor ?? null, source: context.source });
1293
+ return report;
1294
+ }
1295
+ export function updateTriggerReport(reportId, patch, context) {
1296
+ const existing = getTriggerReportById(reportId);
1297
+ if (!existing) {
1298
+ return undefined;
1299
+ }
1300
+ const parsed = updateTriggerReportSchema.parse(patch);
1301
+ const updated = triggerReportSchema.parse({
1302
+ ...existing,
1303
+ ...parsed,
1304
+ emotions: parsed.emotions ? enrichTriggerItems(parsed.emotions, "emo") : existing.emotions,
1305
+ thoughts: parsed.thoughts ? enrichTriggerItems(parsed.thoughts, "tht") : existing.thoughts,
1306
+ behaviors: parsed.behaviors ? enrichTriggerItems(parsed.behaviors, "beh") : existing.behaviors,
1307
+ modeTimeline: parsed.modeTimeline
1308
+ ? enrichTriggerItems(parsed.modeTimeline, "mdl").map((entry) => modeTimelineEntrySchema.parse(entry))
1309
+ : existing.modeTimeline,
1310
+ updatedAt: new Date().toISOString()
1311
+ });
1312
+ getDatabase()
1313
+ .prepare(`UPDATE trigger_reports
1314
+ SET title = ?, status = ?, event_type_id = ?, custom_event_type = ?, event_situation = ?, occurred_at = ?, emotions_json = ?, thoughts_json = ?, behaviors_json = ?,
1315
+ consequences_json = ?, linked_pattern_ids_json = ?, linked_value_ids_json = ?, linked_goal_ids_json = ?, linked_project_ids_json = ?, linked_task_ids_json = ?,
1316
+ linked_behavior_ids_json = ?, linked_belief_ids_json = ?, linked_mode_ids_json = ?, mode_overlays_json = ?, schema_links_json = ?, mode_timeline_json = ?,
1317
+ next_moves_json = ?, updated_at = ?
1318
+ WHERE id = ?`)
1319
+ .run(updated.title, updated.status, updated.eventTypeId, updated.customEventType, updated.eventSituation, updated.occurredAt, JSON.stringify(updated.emotions), JSON.stringify(updated.thoughts), JSON.stringify(updated.behaviors), JSON.stringify(updated.consequences), JSON.stringify(updated.linkedPatternIds), JSON.stringify(updated.linkedValueIds), JSON.stringify(updated.linkedGoalIds), JSON.stringify(updated.linkedProjectIds), JSON.stringify(updated.linkedTaskIds), JSON.stringify(updated.linkedBehaviorIds), JSON.stringify(updated.linkedBeliefIds), JSON.stringify(updated.linkedModeIds), JSON.stringify(updated.modeOverlays), JSON.stringify(updated.schemaLinks), JSON.stringify(updated.modeTimeline), JSON.stringify(updated.nextMoves), updated.updatedAt, reportId);
1320
+ mapCreateUpdateContext({
1321
+ entityType: "trigger_report",
1322
+ entityId: reportId,
1323
+ title: "Trigger report updated",
1324
+ eventKind: "trigger_report.updated",
1325
+ source: context.source,
1326
+ actor: context.actor ?? null,
1327
+ metadata: { status: updated.status }
1328
+ });
1329
+ return updated;
1330
+ }
1331
+ export function deleteTriggerReport(reportId, context) {
1332
+ const existing = getTriggerReportById(reportId);
1333
+ if (!existing) {
1334
+ return undefined;
1335
+ }
1336
+ return runInTransaction(() => {
1337
+ removeIdFromStringArrayColumn("belief_entries", "linked_report_ids_json", reportId);
1338
+ unlinkEntityNotes("trigger_report", reportId);
1339
+ getDatabase()
1340
+ .prepare(`DELETE FROM trigger_reports WHERE id = ?`)
1341
+ .run(reportId);
1342
+ mapCreateUpdateContext({
1343
+ entityType: "trigger_report",
1344
+ entityId: reportId,
1345
+ title: "Trigger report deleted",
1346
+ eventKind: "trigger_report.deleted",
1347
+ source: context.source,
1348
+ actor: context.actor ?? null,
1349
+ metadata: { status: existing.status }
1350
+ });
1351
+ return existing;
1352
+ });
1353
+ }