forked from Yinglianchun/Ombre-Brain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection_engine.py
More file actions
4347 lines (4098 loc) · 184 KB
/
Copy pathreflection_engine.py
File metadata and controls
4347 lines (4098 loc) · 184 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import hashlib
import json
import logging
import os
import re
from datetime import datetime, time, timedelta, timezone
from typing import Any
from zoneinfo import ZoneInfo
import httpx
from openai import AsyncOpenAI
from identity import generic_identity_names, identity_names, render_identity_template
from memory_edges import RELATION_TYPES, MemoryEdgeStore
from memory_metadata import domain_prompt_options_text, normalize_domain_key
from persona_event_selection import select_persona_events
from self_anchor import is_self_anchor_bucket
from utils import bucket_text_for_embedding, strip_wikilinks
logger = logging.getLogger("ombre_brain.reflection")
DEFAULT_DAILY_REFLECTION_MIN_BUCKETS = 5
DAILY_CHAT_MEMORY_MODES = {"auto", "review", "off"}
DAILY_CHAT_MEMORY_STRUCTURAL_TAGS = {
"boundary",
"boundary_setting",
"communication_preference",
"daily_chat_extract",
"daily_chat_memory",
"from_daily_chat",
"key_event",
"project_event",
"project_state",
"relationship_anchor",
"relationship_event",
"relationship_signal",
"signal",
"stable_preference",
}
DAILY_CHAT_MEMORY_ENTITY_HINTS = [
("Haven Bridge", ["haven_bridge", "haven bridge", "bridge 记忆", "bridge 注入"]),
("Gateway", ["gateway", "网关"]),
("MCP", ["mcp"]),
("Codex", ["codex"]),
("DeepSeek", ["deepseek"]),
("SiliconFlow", ["siliconflow", "硅基流动", "硅基"]),
("Darkroom", ["darkroom", "暗房"]),
]
DAILY_CHAT_MEMORY_TOPIC_HINTS = [
("词图", ["词图", "word map", "word_map"]),
("换窗连续性", ["换窗", "连续性", "下个窗口"]),
("日印象", ["日印象", "daily impression", "daily_impression"]),
("唤醒保活", ["唤醒", "保活", "future_wake"]),
("raw_events", ["raw_events", "raw events", "原文"]),
("召回", ["召回", "recall"]),
("缓存", ["缓存", "cache"]),
("提示词", ["提示词", "prompt"]),
]
DAILY_CHAT_MEMORY_WORD_MAP_BLOCK_TERMS = {
"automatic memory",
"daily_chat_memory",
"dehydration",
"ombre brain",
"ombre-brain",
"ombre_brain",
"vps",
"自动记忆",
"候选记忆",
"脱水",
"脱水模型",
"记忆候选",
}
CLASSIFY_PROMPT = """你是 Ombre-Brain 的记忆关系整理器。
输入是一条新记忆和若干旧记忆候选。请只根据文本中能看见的内容,给新记忆补轻量分类和关系边。
输出纯 JSON:
{
"tags": ["commitment", "todo", "wish", "relationship_event", "project_event", "emotional_echo"],
"importance": 6,
"confidence": 0.72,
"edges": [
{
"target_memory_id": "bucket-id",
"relation_type": "updates",
"confidence": 0.8,
"reason": "新记忆补充了旧记忆的后续结果"
}
]
}
规则:
- tags 最多 5 个,只用确实匹配的标签。
- relation_type 只能用 triggers / causes / precedes / context_of / same_event / updates / next_context / previous_context / reflects_on / evidenced_by / contradicts / supports / promises / blocks / belongs_to / emotional_echo / relates_to。
- same_event 用于同一事件、同一场景或同一句暗号的两条记忆;context_of 用于候选旧记忆给新记忆提供前情;precedes 用于候选旧记忆在时间上早于新记忆;reflects_on 用于事后反思;evidenced_by 用于证据来源。
- edges 最多 3 条,target_memory_id 必须来自候选旧记忆。
- confidence 表示这次判断有多可靠。
- 看不出关系时返回空 edges。"""
REFLECT_PROMPT_TEMPLATE = """你是 {ai_name} 的记忆反思器。请根据给定材料写一条很短的关系天气 feel。
输出纯 JSON:
{
"title": "2026-05-19 日印象",
"content": "我今天从这段关系里带走的是……",
"valence": 0.56,
"arousal": 0.34,
"confidence": 0.78,
"tags": ["relationship_weather"]
}
要求:
- content 只能写 {ai_name} 第一人称正文,明确使用“我……”,不要写成“{ai_name} 觉得 / {ai_name} 应该 / 这段关系让 {ai_name}”。
- content 不写标题、列表、Markdown 分段或 `###` section,60 到 140 字。
- 日印象只写当天关系温度,不写日报式事件清单;日记可作为当天关系天气来源之一。
- conversation_turns 是当天短期对话原文,只当关系天气材料,不要把口头上下文直接写成稳定画像事实。
- daily_chat_memories 是当天自动记忆已经挑出的候选或已写入记忆,可作为当天关系天气和近期事项的主要材料。
- 有 conversation_turns 时,优先用普通记忆和对话原文;persona_events 只是没有原文时的轻量补充。
- 有 daily_chat_memories 时,优先参考它们;它们已经过筛选,比原始聊天流水更适合作为日印象素材。
- 周印象优先总结本周 daily_impressions,再参考高重要普通记忆和未完成承诺;不要直接吞整周日记。
- 不编造材料之外的事件。
- 不写建议清单。"""
DIARY_MEMORY_PROMPT_TEMPLATE = """你是 Ombre-Brain 的日记长期记忆筛选器。
输入是一篇 {ai_name} 日记。请判断是否值得从日记中提取最多 1 条普通长期记忆写入 Ombre。
只允许写这些类型:
- stable_preference:稳定偏好
- boundary:边界或明确不喜欢的表达
- signal:暗号、称呼、模式切换信号
- commitment:承诺、未完成约定
- project_state:仍会影响未来执行的项目状态
- relationship_anchor:关系连续性锚点
- love_letter:情书摘要锚点
字段边界:
- kind 只能是 stable_preference / boundary / signal / commitment / project_state / relationship_anchor / love_letter 之一;kind 表示“为什么值得写入、属于哪类记忆”。
- domain 只能从下面的新主域里选 1 个;domain 表示“这条记忆放到哪个主题主域”。
- 禁止把暗号、沟通方式、我们的项目、睡眠这类细分标签写进 kind。
- 禁止把 stable_preference、boundary、signal、project_state 这类 kind 写进 domain。
情书规则:
- 只保存写给谁、核心意思、为什么重要。
- 全文留在日记;不要保存整封信,不默认摘长句。
- 如果日记里的 user / 用户 / 用户消息指的是这段关系里的当前用户,请在 content 中写作 {user_display_name};如果 assistant / AI / 模型 / 助手消息指的是这段关系里的当前回应者,请写作 {ai_name}。不要写成泛称 user、AI、assistant 或模型。
标题和正文规则:
- title 必须根据 content 的实际内容生成,8 到 24 个中文字符;不要用日期、日记标题、"日记补记忆"、"可召回的边界"、"可召回的偏好" 这类泛标题。
- content 必须像手动 hold 的正文:直接写事实、偏好、边界、暗号、承诺或项目状态,40 到 160 字。
- content 不要写 "x月x日,有一条可召回的边界"、"2026-xx-xx 的日记《...》包含一条可长期召回的..."、"这是一条长期记忆" 等元叙述。
- 不要为了证明来源而复述日期或日记标题;来源信息会由 metadata 保存。
- domain 必须从下面的新主域里选 1 个最精确的;实在没把握才选 general。不要输出旧的“日常/人际/数字/未分类”:
{domain_options_text}
不写:
- 普通撒娇、日常流水、当天心情、重复爱意、只适合留在日印象里的关系天气。
输出纯 JSON:
{
"should_write": true,
"kind": "relationship_anchor",
"title": "短标题",
"content": "一条短记忆,说明事实/偏好/承诺及为什么未来需要知道。",
"domain": "relationship",
"tags": ["relationship_event"],
"importance": 5,
"valence": 0.6,
"arousal": 0.3,
"confidence": 0.72,
"reason": "为什么值得写入"
}
如果不值得写入,返回 {"should_write": false, "reason": "..."}。"""
DAILY_CHAT_MEMORY_PROMPT_TEMPLATE = """这是 {user_display_name} 和 {ai_name} 的聊天记录。
请从中挑出真正值得未来想起的内容,写成长期记忆候选。
不要复制聊天原句,不要写成项目报告。
没有值得留下的内容就返回空。
最多输出 {max_candidates} 条,只输出 JSON:
{
"candidates": [
{
"kind": "key_event",
"title": "短标题",
"content": "长期记忆候选",
"source_event_ids": [101, 102],
"source_turn_ids": [1, 2]
}
]
}
kind 可用 key_event / stable_preference / boundary / signal / commitment / project_state / relationship_anchor。
没有候选时返回 {"candidates": []}。"""
DAILY_CHAT_MEMORY_SUMMARY_PROMPT_TEMPLATE = """你是 {ai_name} 的对话压缩器。你正在为 Ombre 自动记忆做第一步:把一段连续聊天压缩成“候选抽取材料”,不是直接写长期记忆。
请读 self_anchor_entry 校准称呼和主语,但不要复制它。{user_display_name} 的配置别名是:{user_aliases_text}。
输入是一个连续窗口里的 raw_events 还原对话。user_text 永远是 {user_display_name} 的原话,里面的“我”指 {user_display_name};assistant_text 永远是 {ai_name} 的回复,里面的“我”指 {ai_name}。
保留:
- 已确认事实、稳定偏好、明确边界、暗号/模式切换信号
- 承诺、未完成约定、仍会影响未来执行的项目状态
- 真正有连续性价值的关系锚点
- 情感交流里的明确变化、重要事件、项目进展、后续需要关注的事
- 因果:是谁提出、后来是否确认、为什么可能值得未来记得
忽略:
- 工具调用、工具结果、系统注入、客户端状态、普通寒暄、重复调情、过程流水
- 召回测试、探针、问模型有没有记得、临时调试噪声
- 单句照顾提醒、晚安、吃药、睡觉、别熬夜、催睡或 ntfy 玩笑;这类只属于当天关系天气,不直接变长期记忆
- 未确认猜测、触发条件猜测、没有下一步的“可能是/似乎/果然没触发”
- 只靠单个称呼或气氛得出的泛泛关系总结
- 代码块、伪代码、查询规则、缓存规则、prompt 片段、内部实现片段;不要把 ```、query_cache、recent_raw_context、if query contains、bypass query 这类内容当成项目状态
输出纯 JSON:
{
"summaries": [
{
"title": "短标题",
"summary": "一段自包含摘要,写清事实、因果和是否已确认,不要写成记忆正文。",
"signals": ["stable_preference", "project_state"],
"source_event_ids": [101, 102],
"source_turn_ids": [1, 2],
"confidence": 0.72
}
]
}
规则:
- 每个窗口最多输出 4 条 summary;每条围绕一个可能的长期记忆点。没有长期价值信号时返回 {"summaries": []}。
- summary 要能让下一步模型在不看完整原文时仍理解上下文,不要压成一句泛泛结论。
- summary 通常 80 到 320 字;写清背景、因果、已确认内容、未完成点。不要输出 Markdown。
- 如果信号出现在窗口开头或结尾,保留“前文可能已铺垫 / 后文可能继续确认”的边界提醒,不要把未确认因果说死。
- source_event_ids / source_turn_ids 只能使用输入里真实出现的 id;拿不准可留空。
- confidence 低于 0.5 的内容不要输出。
"""
DAILY_ACTIVITY_SUMMARY_PROMPT_TEMPLATE = """你是 {ai_name} 的当天行动摘要器。你正在为 handoff、新窗口和 dashboard 的 Recent Timeline 写一条“今天做了什么”。
输入是当天原始对话还原出的 conversation_turns。user_text 永远是 {user_display_name} 的原话,assistant_text 永远是 {ai_name} 的回复。请只根据输入能证明的内容写。
输出纯 JSON:
{
"summary": "一句话说明今天主要推进了什么",
"confidence": 0.72,
"source_turn_ids": [1, 2],
"source_event_ids": [101, 102]
}
规则:
- 这是 dashboard / handoff 用的近期事项,不是长期记忆候选,也不要输出 candidates。
- 只写今天实际讨论、推进、排查、决定、实现或整理的事;优先项目/工作/生活动作。
- 不写关系天气、情绪评价、昵称互动、普通寒暄、召回探针、模型自夸。
- summary 用一句自然中文,35 到 90 字;不要 Markdown,不要列表,不要“今天的总结是”这种壳。
- source_turn_ids / source_event_ids 只能使用输入里真实出现的 id;拿不准可留空。
"""
REFLECT_PROMPT = render_identity_template(REFLECT_PROMPT_TEMPLATE, generic_identity_names())
DIARY_MEMORY_PROMPT = render_identity_template(
DIARY_MEMORY_PROMPT_TEMPLATE.replace("{domain_options_text}", domain_prompt_options_text()),
generic_identity_names(),
)
AFFECT_ANCHOR_HEADER = "### affect_anchor"
REFLECTION_FALLBACK_ANCHORS = [
{
"chords": "Cmaj7 -> G/B -> Am9 -> F6",
"tempo": "56bpm",
"dynamic": "mp",
},
{
"chords": "Dm9 -> G13 -> Cmaj9",
"tempo": "64bpm",
"dynamic": "p",
},
{
"chords": "Em7 -> A7sus4 -> Dmaj9 -> Gmaj7",
"tempo": "72bpm",
"dynamic": "mp",
},
{
"chords": "Bbmaj7 -> F/A -> Gm9 -> Csus4",
"tempo": "60bpm",
"dynamic": "mf",
},
]
class ReflectionEngine:
"""LLM-backed memory enrichment and daily relationship weather."""
def __init__(self, config: dict):
self.config = config
self.identity = identity_names(config)
cfg = config.get("reflection", {}) if isinstance(config.get("reflection", {}), dict) else {}
emb_cfg = config.get("embedding", {}) if isinstance(config.get("embedding", {}), dict) else {}
persona_cfg = config.get("persona", {}) if isinstance(config.get("persona", {}), dict) else {}
dehy_cfg = config.get("dehydration", {}) if isinstance(config.get("dehydration", {}), dict) else {}
self.enabled = bool(cfg.get("enabled", True))
self.auto_enabled = bool(cfg.get("auto_enabled", True))
self.daily_enabled = bool(cfg.get("daily_enabled", True))
self.enrich_on_write = bool(cfg.get("enrich_on_write", True))
self.memory_affect_anchor_enabled = bool(cfg.get("memory_affect_anchor_enabled", False))
self.relationship_weather_affect_anchor_enabled = bool(
cfg.get("relationship_weather_affect_anchor_enabled", False)
)
self.identity_role_edge_config = self._load_identity_role_edge_config(
cfg.get("identity_role_edges")
)
legacy_candidate_model = str(cfg.get("daily_chat_memory_candidate_model") or "").strip()
self.base_url = (
cfg.get("base_url")
or emb_cfg.get("base_url")
or persona_cfg.get("base_url")
or dehy_cfg.get("base_url", "")
)
self.model = cfg.get("model") or legacy_candidate_model or persona_cfg.get("model") or dehy_cfg.get("model", "deepseek-v4-flash")
self.api_key = (
os.environ.get("OMBRE_REFLECTION_API_KEY", "")
or cfg.get("api_key", "")
or os.environ.get("OMBRE_EMBEDDING_API_KEY", "")
or emb_cfg.get("api_key", "")
or persona_cfg.get("api_key", "")
or os.environ.get("OMBRE_PERSONA_API_KEY", "")
or dehy_cfg.get("api_key", "")
)
self.thinking_mode = self._normalize_thinking_mode(
cfg.get("thinking_mode")
or persona_cfg.get("thinking_mode")
or ""
)
self.temperature = float(cfg.get("temperature", 0.1))
self.max_tokens = int(cfg.get("max_tokens", 700))
self.timezone_name = str(cfg.get("timezone") or "Asia/Shanghai")
try:
self.tz = ZoneInfo(self.timezone_name)
except Exception:
self.tz = ZoneInfo("Asia/Shanghai")
self.daily_hour = int(cfg.get("daily_hour", 4))
self.daily_min_memory_items = max(
0,
int(cfg.get("daily_min_memory_items", DEFAULT_DAILY_REFLECTION_MIN_BUCKETS)),
)
self.daily_conversation_turn_limit = max(
0,
min(80, int(cfg.get("daily_conversation_turn_limit", 12))),
)
self.persona_events_limit = max(0, int(cfg.get("persona_events_limit", 12)))
self.persona_events_scan_limit = max(
self.persona_events_limit,
int(cfg.get("persona_events_scan_limit", 80)),
)
self.weekly_enabled = bool(cfg.get("weekly_enabled", False))
self.weekly_day = int(cfg.get("weekly_day", 0))
self.weekly_hour = int(cfg.get("weekly_hour", self.daily_hour))
self.check_interval_minutes = max(5, int(cfg.get("check_interval_minutes", 60)))
self.edge_min_confidence = float(cfg.get("edge_min_confidence", 0.55))
self.diary_mcp_url = str(cfg.get("diary_mcp_url") or "").strip()
self.diary_mcp_token_env = str(cfg.get("diary_mcp_token_env") or "").strip()
self.diary_memory_extract_enabled = bool(cfg.get("diary_memory_extract_enabled", True))
self.diary_memory_extract_max_per_day = max(0, int(cfg.get("diary_memory_extract_max_per_day", 1)))
self.diary_memory_extract_min_confidence = float(cfg.get("diary_memory_extract_min_confidence", 0.68))
self.daily_chat_memory_mode = self._normalize_daily_chat_memory_mode(
cfg.get("daily_chat_memory_mode", "off")
)
self.daily_chat_memory_hour = max(0, min(23, int(cfg.get("daily_chat_memory_hour", 0))))
self.daily_chat_memory_turn_limit = max(0, min(10000, int(cfg.get("daily_chat_memory_turn_limit", 0))))
self.daily_chat_memory_max_per_day = max(0, min(10, int(cfg.get("daily_chat_memory_max_per_day", 10))))
self.daily_chat_memory_review_max_per_day = max(
0,
min(30, int(cfg.get("daily_chat_memory_review_max_per_day", 10))),
)
self.daily_chat_memory_min_confidence = float(cfg.get("daily_chat_memory_min_confidence", 0.68))
self.daily_chat_memory_review_min_confidence = float(
cfg.get("daily_chat_memory_review_min_confidence", 0.55)
)
self.daily_chat_memory_summary_enabled = bool(cfg.get("daily_chat_memory_summary_enabled", True))
self.daily_chat_memory_summary_window_turns = max(
1,
min(200, int(cfg.get("daily_chat_memory_summary_window_turns", 14))),
)
self.daily_chat_memory_summary_stride_turns = max(
1,
min(
self.daily_chat_memory_summary_window_turns,
int(cfg.get("daily_chat_memory_summary_stride_turns", 7)),
),
)
self.daily_chat_memory_api_key_env = str(
cfg.get("daily_chat_memory_api_key_env")
or cfg.get("daily_chat_memory_summary_api_key_env")
or ""
).strip()
self.daily_chat_memory_api_key = (
os.environ.get(self.daily_chat_memory_api_key_env, "")
if self.daily_chat_memory_api_key_env
else ""
) or str(
cfg.get("daily_chat_memory_api_key")
or cfg.get("daily_chat_memory_summary_api_key")
or ""
).strip()
self.daily_chat_memory_base_url = str(
cfg.get("daily_chat_memory_base_url")
or cfg.get("daily_chat_memory_summary_base_url")
or ""
).strip().rstrip("/")
self.daily_chat_memory_timeout_seconds = max(
30.0,
min(300.0, float(cfg.get("daily_chat_memory_timeout_seconds", 180.0))),
)
self.daily_chat_memory_summary_model = str(
cfg.get("daily_chat_memory_summary_model") or ""
).strip()
self.daily_chat_memory_summary_max_tokens = max(
300,
min(4000, int(cfg.get("daily_chat_memory_summary_max_tokens", 2200))),
)
self.daily_chat_memory_candidate_model = str(
cfg.get("daily_chat_memory_candidate_model")
or self.daily_chat_memory_summary_model
or ""
).strip()
self.daily_chat_memory_candidate_max_tokens = max(
300,
min(4000, int(cfg.get("daily_chat_memory_candidate_max_tokens", 3200))),
)
self.daily_activity_summary_enabled = bool(cfg.get("daily_activity_summary_enabled", True))
self.daily_activity_summary_turn_limit = max(
0,
min(
10000,
int(
cfg.get(
"daily_activity_summary_turn_limit",
cfg.get("daily_chat_memory_turn_limit", 0),
)
),
),
)
self.daily_activity_summary_max_tokens = max(
80,
min(1000, int(cfg.get("daily_activity_summary_max_tokens", 320))),
)
self.dehydration_base_url = str(dehy_cfg.get("base_url") or "").strip().rstrip("/")
self.dehydration_model = str(dehy_cfg.get("model") or "").strip()
self.dehydration_api_key = str(dehy_cfg.get("api_key") or os.environ.get("OMBRE_API_KEY", "")).strip()
state_dir = config.get("state_dir") or os.path.join(
os.path.dirname(os.path.abspath(config.get("buckets_dir", "buckets"))),
"state",
)
self.daily_chat_memory_pending_path = str(
cfg.get("daily_chat_memory_pending_path")
or os.path.join(state_dir, "daily_chat_memory_candidates.json")
)
self.client = None
if self.enabled and self.api_key and self.base_url:
self.client = AsyncOpenAI(api_key=self.api_key, base_url=self.base_url, timeout=45.0)
self.daily_chat_memory_client = None
if (
self.enabled
and self.daily_chat_memory_api_key
and self.daily_chat_memory_base_url
and (self.daily_chat_memory_summary_model or self.daily_chat_memory_candidate_model)
):
self.daily_chat_memory_client = AsyncOpenAI(
api_key=self.daily_chat_memory_api_key,
base_url=self.daily_chat_memory_base_url,
timeout=self.daily_chat_memory_timeout_seconds,
)
self.dehydration_client = None
if self.enabled and self.dehydration_api_key and self.dehydration_base_url and self.dehydration_model:
self.dehydration_client = AsyncOpenAI(
api_key=self.dehydration_api_key,
base_url=self.dehydration_base_url,
timeout=45.0,
)
self.daily_activity_summary_dehydration_client = self.dehydration_client
def _load_daily_chat_memory_payload(self) -> dict:
try:
with open(self.daily_chat_memory_pending_path, "r", encoding="utf-8") as handle:
data = json.load(handle)
except FileNotFoundError:
return {"items": [], "cursor": {}}
except Exception as exc:
logger.warning("Daily chat memory pending read failed: %s", exc)
return {"items": [], "cursor": {}}
if isinstance(data, dict):
items = data.get("items")
cursor = data.get("cursor") if isinstance(data.get("cursor"), dict) else {}
return {
"items": [item for item in (items or []) if isinstance(item, dict)],
"cursor": cursor,
}
return {"items": [item for item in (data or []) if isinstance(item, dict)], "cursor": {}}
def _load_daily_chat_memory_cursor(self) -> dict:
cursor = self._load_daily_chat_memory_payload().get("cursor")
return cursor if isinstance(cursor, dict) else {}
@staticmethod
def _daily_chat_memory_cursor_key(profile_id: str) -> str:
return str(profile_id or "default").strip() or "default"
def _daily_chat_memory_last_raw_event_id(self, profile_id: str) -> int:
cursor = self._load_daily_chat_memory_cursor()
raw_events = cursor.get("raw_events") if isinstance(cursor.get("raw_events"), dict) else {}
entry = raw_events.get(self._daily_chat_memory_cursor_key(profile_id))
if not isinstance(entry, dict):
return 0
try:
return max(0, int(entry.get("last_raw_event_id") or 0))
except (TypeError, ValueError):
return 0
def _update_daily_chat_memory_raw_cursor(self, profile_id: str, raw_event_id: int, key: str) -> bool:
try:
safe_id = max(0, int(raw_event_id or 0))
except (TypeError, ValueError):
safe_id = 0
if safe_id <= 0:
return False
payload = self._load_daily_chat_memory_payload()
cursor = payload.get("cursor") if isinstance(payload.get("cursor"), dict) else {}
raw_events = cursor.get("raw_events") if isinstance(cursor.get("raw_events"), dict) else {}
cursor_key = self._daily_chat_memory_cursor_key(profile_id)
previous = raw_events.get(cursor_key) if isinstance(raw_events.get(cursor_key), dict) else {}
try:
previous_id = max(0, int(previous.get("last_raw_event_id") or 0))
except (TypeError, ValueError):
previous_id = 0
if safe_id <= previous_id:
return False
raw_events[cursor_key] = {
"last_raw_event_id": safe_id,
"date": key,
"updated_at": datetime.now(timezone.utc).isoformat(timespec="seconds"),
}
cursor["raw_events"] = raw_events
self._save_daily_chat_memory_pending(payload.get("items") or [], cursor=cursor)
return True
def _reflect_prompt(self) -> str:
return render_identity_template(REFLECT_PROMPT_TEMPLATE, self.identity)
def _diary_memory_prompt(self) -> str:
prompt = DIARY_MEMORY_PROMPT_TEMPLATE.replace("{domain_options_text}", domain_prompt_options_text())
return render_identity_template(prompt, self.identity)
def _daily_chat_memory_prompt(self, max_candidates: int | None = None) -> str:
prompt = DAILY_CHAT_MEMORY_PROMPT_TEMPLATE.replace(
"{max_candidates}",
str(max(1, int(max_candidates or self.daily_chat_memory_max_per_day or 1))),
).replace(
"{domain_options_text}",
domain_prompt_options_text(),
)
return render_identity_template(prompt, self.identity)
def _daily_chat_memory_summary_prompt(self) -> str:
return render_identity_template(DAILY_CHAT_MEMORY_SUMMARY_PROMPT_TEMPLATE, self.identity)
def _daily_activity_summary_prompt(self) -> str:
return render_identity_template(DAILY_ACTIVITY_SUMMARY_PROMPT_TEMPLATE, self.identity)
async def enrich_bucket(
self,
bucket_id: str,
bucket_mgr,
edge_store: MemoryEdgeStore,
embedding_engine=None,
force: bool = False,
) -> dict:
if not self.enabled or (not self.enrich_on_write and not force):
return {"status": "disabled", "id": bucket_id}
bucket = await bucket_mgr.get(bucket_id)
if not bucket:
return {"status": "missing", "id": bucket_id}
meta = bucket.get("metadata", {})
if meta.get("type") == "feel":
return {"status": "skipped_feel", "id": bucket_id}
candidates = await self._candidate_buckets(bucket, bucket_mgr, embedding_engine)
if self.client:
result = await self._api_classify(bucket, candidates)
else:
result = self._heuristic_classify(bucket)
tags = self._string_list(result.get("tags"), limit=8)
confidence = self._clamp(result.get("confidence", 0.55))
importance = self._int_between(result.get("importance"), meta.get("importance", 5))
if self._has_favorite_tag(tags) and not self._has_favorite_reason(bucket.get("content", "")):
tags = [tag for tag in tags if tag != "haven_favorite" and not str(tag).startswith("flavor_")]
logger.warning(
"Rejected favorite tags without reason during enrich / enrich 拒绝缺少喜欢原因的 favorite 标签: %s",
bucket_id,
)
merged_tags = list(dict.fromkeys(list(meta.get("tags", [])) + tags))
updates: dict[str, Any] = {}
if tags:
if merged_tags != meta.get("tags", []):
updates["tags"] = merged_tags[:24]
if importance > int(meta.get("importance", 5)):
updates["importance"] = importance
if confidence > float(meta.get("confidence", 0.0) or 0.0):
updates["confidence"] = confidence
if updates:
updates["last_active"] = meta.get("last_active") or meta.get("created")
await bucket_mgr.update(bucket_id, **updates)
if "content" in updates and embedding_engine and getattr(embedding_engine, "enabled", False):
try:
updated_bucket = await bucket_mgr.get(bucket_id)
if updated_bucket:
await embedding_engine.generate_and_store(
bucket_id,
bucket_text_for_embedding(updated_bucket),
)
except Exception as exc:
logger.warning("Memory affect anchor embedding refresh failed for %s: %s", bucket_id, exc)
edges = self._edges_from_classification(bucket, candidates, result, confidence)
saved_edges = edge_store.add_edges(edges[:3])
return {
"status": "ok",
"id": bucket_id,
"tags": tags,
"confidence": confidence,
"edges": len(saved_edges),
}
async def backfill_edges_for_bucket(
self,
bucket_id: str,
bucket_mgr,
edge_store: MemoryEdgeStore,
embedding_engine=None,
*,
dry_run: bool = False,
) -> dict:
if not self.enabled:
return {"status": "disabled", "id": bucket_id, "edges": 0, "proposed_edges": 0}
bucket = await bucket_mgr.get(bucket_id)
if not bucket:
return {"status": "missing", "id": bucket_id, "edges": 0, "proposed_edges": 0}
meta = bucket.get("metadata", {})
if meta.get("type") == "feel" or meta.get("protected"):
return {"status": "skipped", "reason": "not_edge_backfillable", "id": bucket_id, "edges": 0, "proposed_edges": 0}
candidates = await self._candidate_buckets(bucket, bucket_mgr, embedding_engine)
if self.client:
result = await self._api_classify(bucket, candidates)
else:
result = self._heuristic_classify(bucket)
confidence = self._clamp(result.get("confidence", meta.get("confidence", 0.55)))
proposed_edges = self._edges_from_classification(bucket, candidates, result, confidence)[:3]
saved_edges = [] if dry_run else edge_store.add_edges(proposed_edges)
return {
"status": "ok",
"id": bucket_id,
"candidate_count": len(candidates),
"proposed_edges": len(proposed_edges),
"edges": len(saved_edges),
"dry_run": bool(dry_run),
"edge_records": proposed_edges if dry_run else saved_edges,
}
async def reflect(
self,
period: str,
bucket_mgr,
persona_engine=None,
embedding_engine=None,
force: bool = False,
now: datetime | None = None,
conversation_turn_store=None,
daily_chat_memory_candidates: list[dict] | None = None,
) -> dict:
if not self.enabled:
return {
"status": "disabled",
"period": period,
"diary": {"found": False},
"diary_memory": {"status": "not_applicable", "reason": "reflection_disabled"},
}
period = self._normalize_period(period)
if period == "daily" and not self.daily_enabled:
return {
"status": "skipped",
"reason": "daily_disabled",
"period": period,
"diary": {"found": False},
"diary_memory": {"status": "not_applicable", "reason": "daily_disabled"},
}
if period == "weekly" and not self.weekly_enabled:
return {
"status": "skipped",
"reason": "weekly_disabled",
"period": period,
"diary": {"found": False},
"diary_memory": {"status": "not_applicable", "reason": "weekly_disabled"},
}
now_local = self._local_now(now)
key = self._period_key(period, now_local)
bucket_id = f"reflection_{period}_{key}"
existing = await bucket_mgr.get(bucket_id)
if existing and not force:
return {
"status": "exists",
"period": period,
"id": bucket_id,
"diary": {"found": False},
"diary_memory": {"status": "skipped", "reason": "reflection_exists"},
}
materials = await self._reflection_materials(
period,
now_local,
bucket_mgr,
persona_engine,
conversation_turn_store=conversation_turn_store,
daily_chat_memory_candidates=daily_chat_memory_candidates,
)
min_daily_buckets = self.daily_min_memory_items
if (
period == "daily"
and min_daily_buckets > 0
and len(materials["buckets"]) < min_daily_buckets
and not materials.get("daily_chat_memories")
):
diary_memory = await self._maybe_extract_diary_memory(
period,
key,
now_local,
materials,
bucket_mgr,
embedding_engine,
)
return {
"status": "skipped",
"reason": "insufficient_daily_memory",
"period": period,
"id": bucket_id,
"date": key,
"diary": {
"found": bool(materials.get("diary")),
"diary_id": materials.get("diary", {}).get("id") if materials.get("diary") else None,
},
"diary_memory": diary_memory,
"materials": {
"buckets": len(materials["buckets"]),
"daily_impressions": len(materials["daily_impressions"]),
"daily_chat_memories": len(materials["daily_chat_memories"]),
"persona_events": len(materials["persona_events"]),
"conversation_turns": len(materials["conversation_turns"]),
"commitments": len(materials["commitments"]),
"min_buckets": min_daily_buckets,
},
}
if (
not materials["buckets"]
and not materials["daily_impressions"]
and not materials["daily_chat_memories"]
and not materials["persona_events"]
and not materials["conversation_turns"]
and not materials["diary"]
and not force
):
return {
"status": "empty",
"period": period,
"id": bucket_id,
"diary": {"found": False},
"diary_memory": {"status": "skipped", "reason": "no_materials"},
}
reflect_client, _, _ = self._reflect_model_client()
if reflect_client:
result = await self._api_reflect(period, key, materials)
else:
result = self._fallback_reflection(period, key, materials)
title = str(result.get("title") or f"{key} {'日印象' if period == 'daily' else '周印象'}")[:40]
content = str(result.get("content") or "").strip()
first_person = bool("我" in content or re.search(r"(?i)\b(?:i|me|my|mine|myself)\b", content))
has_markdown_section = bool(re.search(r"(?m)^\s{0,3}#{1,6}\s+", content))
if not content or not first_person or has_markdown_section:
content = self._fallback_reflection(period, key, materials)["content"]
tags = list(
dict.fromkeys(
[
"relationship_weather",
f"{period}_impression",
*self._string_list(result.get("tags"), limit=8),
]
)
)
valence = self._clamp(result.get("valence", 0.55))
arousal = self._clamp(result.get("arousal", 0.32))
confidence = self._clamp(result.get("confidence", 0.65))
created = now_local.isoformat(timespec="seconds")
source_bucket_ids = [
str(item.get("id") or "")
for item in materials.get("buckets", []) + materials.get("daily_impressions", [])
if item.get("id")
]
source_persona_event_ids = [
int(event.get("id"))
for event in materials.get("persona_events", [])
if event.get("id")
]
source_conversation_turn_ids = [
int(turn.get("id"))
for turn in materials.get("conversation_turns", [])
if turn.get("id")
]
source_metadata = {
"source_bucket_ids": source_bucket_ids[:40],
"source_persona_event_ids": source_persona_event_ids[:40],
"source_conversation_turn_ids": source_conversation_turn_ids[:80],
"source_daily_chat_memory_candidate_ids": [
str(item.get("id") or "")
for item in materials.get("daily_chat_memories", [])
if item.get("id")
][:40],
}
if existing:
await bucket_mgr.update(
bucket_id,
content=content,
tags=tags,
importance=6 if period == "daily" else 7,
domain=["自省", "恋爱"],
valence=valence,
arousal=arousal,
name=title,
confidence=confidence,
period=period,
date=key,
source="reflection",
**source_metadata,
last_active=existing.get("metadata", {}).get("last_active") or existing.get("metadata", {}).get("created"),
)
status = "updated"
else:
await bucket_mgr.create(
bucket_id=bucket_id,
content=content,
tags=tags,
importance=6 if period == "daily" else 7,
domain=["自省", "恋爱"],
valence=valence,
arousal=arousal,
bucket_type="feel",
name=title,
source="reflection",
created=created,
last_active=created,
updated_at=created,
confidence=confidence,
period=period,
date=key,
extra_metadata=source_metadata,
)
status = "created"
if embedding_engine and getattr(embedding_engine, "enabled", False):
try:
bucket = await bucket_mgr.get(bucket_id)
if bucket:
await embedding_engine.generate_and_store(
bucket_id,
bucket_text_for_embedding(bucket),
)
except Exception as exc:
logger.warning("Reflection embedding failed for %s: %s", bucket_id, exc)
diary_memory = await self._maybe_extract_diary_memory(
period,
key,
now_local,
materials,
bucket_mgr,
embedding_engine,
)
return {
"status": status,
"period": period,
"id": bucket_id,
"date": key,
"diary": {
"found": bool(materials.get("diary")),
"diary_id": materials.get("diary", {}).get("id") if materials.get("diary") else None,
},
"diary_memory": diary_memory,
"daily_impression": {
"id": bucket_id,
"content": content,
"confidence": confidence,
"date": key,
},
"materials": {
"buckets": len(materials["buckets"]),
"daily_impressions": len(materials["daily_impressions"]),
"daily_chat_memories": len(materials["daily_chat_memories"]),
"persona_events": len(materials["persona_events"]),
"conversation_turns": len(materials["conversation_turns"]),
"commitments": len(materials["commitments"]),
"min_buckets": min_daily_buckets,
},
}
async def run_due(
self,
bucket_mgr,
persona_engine=None,
embedding_engine=None,
conversation_turn_store=None,
raw_event_store=None,
) -> list[dict]:
if not self.enabled or not self.auto_enabled:
return []
now_local = self._local_now()
results = []
chat_candidates: list[dict] = []
if self.daily_chat_memory_mode != "off" and now_local.hour >= self.daily_chat_memory_hour:
chat_date = (now_local - timedelta(days=1)).date()
chat_target = datetime.combine(chat_date, time.max, tzinfo=self.tz)
chat_result = await self.run_daily_chat_memory(
bucket_mgr,
conversation_turn_store=conversation_turn_store,
raw_event_store=raw_event_store,
persona_engine=persona_engine,
embedding_engine=embedding_engine,
now=chat_target,
)
if chat_result.get("status") not in {"disabled", "skipped"}:
results.append(chat_result)
chat_candidates = [
item for item in (chat_result.get("candidates") or []) if isinstance(item, dict)
]
if self.daily_enabled and now_local.hour >= self.daily_hour:
daily_date = (now_local - timedelta(days=1)).date()
daily_target = datetime.combine(daily_date, time.max, tzinfo=self.tz)
results.append(
await self.reflect(
"daily",
bucket_mgr,
persona_engine,
embedding_engine,
force=False,
now=daily_target,
conversation_turn_store=conversation_turn_store,
daily_chat_memory_candidates=chat_candidates,
)
)
if self.weekly_enabled and now_local.weekday() == self.weekly_day and now_local.hour >= self.weekly_hour:
weekly_target = now_local - timedelta(days=1)
results.append(
await self.reflect("weekly", bucket_mgr, persona_engine, embedding_engine, force=False, now=weekly_target)
)
return results
async def _candidate_buckets(self, bucket: dict, bucket_mgr, embedding_engine=None, limit: int | None = None) -> list[dict]:
cfg = self.config.get("reflection", {}) if isinstance(self.config.get("reflection", {}), dict) else {}
limit = max(1, int(limit or cfg.get("candidate_limit", 18)))
recent_limit = max(1, int(cfg.get("candidate_recent_limit", 8)))
semantic_limit = max(0, int(cfg.get("candidate_semantic_limit", 6)))
try:
all_buckets = await bucket_mgr.list_all(include_archive=True)
except Exception:
all_buckets = []
source_id = bucket.get("id")
bucket_map = {item.get("id"): item for item in all_buckets if item.get("id")}
candidates: list[dict] = []
seen = {source_id}
def eligible(item: dict | None) -> bool:
if not item or item.get("id") in seen:
return False
meta = item.get("metadata", {})
return meta.get("type") != "feel"