This repository was archived by the owner on Jun 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmemory.py
More file actions
1214 lines (1015 loc) · 40.3 KB
/
memory.py
File metadata and controls
1214 lines (1015 loc) · 40.3 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
"""
/v1/memory/* and /v2/memory/* routes - production endpoints for XMem memory operations.
All routes require a valid Bearer API key and respect the per-key rate limit.
"""
from __future__ import annotations
import asyncio
import logging
import math
import threading
import time
from typing import Any, Dict, List
from fastapi import APIRouter, Depends, Request, UploadFile, File
from fastapi.responses import JSONResponse
from src.api.dependencies import (
enforce_rate_limit,
get_code_pipeline,
get_ingest_pipeline,
get_retrieval_pipeline,
require_api_key,
require_ready,
)
from src.api.schemas import (
APIResponse,
BatchIngestRequest,
BatchIngestResponse,
DomainResult,
IngestRequest,
IngestResponse,
OperationDetail,
RetrieveRequest,
RetrieveResponse,
SearchRequest,
SearchResponse,
ScrapeRequest,
ScrapeResponse,
MessagePair,
SourceRecord,
StatusEnum,
WeaverSummary,
)
from src.pipelines.retrieval import RetrievalPipeline
from bs4 import BeautifulSoup
import json
import re
from playwright.sync_api import sync_playwright
from src.config import settings
from src.jobs.durable import (
QUEUED,
get_default_job_store,
run_job,
serialize_job,
)
logger = logging.getLogger("xmem.api.routes.memory")
_ingest_semaphore = asyncio.Semaphore(5)
_LOCAL_ENVIRONMENTS = {"development", "dev", "local", "test"}
router = APIRouter(
prefix="/v1/memory",
tags=["memory"],
dependencies=[Depends(require_ready), Depends(enforce_rate_limit)],
)
scrape_router = APIRouter(
prefix="/v1/memory",
tags=["memory"],
dependencies=[Depends(enforce_rate_limit)],
)
v2_router = APIRouter(
prefix="/v2/memory",
tags=["memory"],
dependencies=[Depends(require_ready), Depends(enforce_rate_limit)],
)
v2_scrape_router = APIRouter(
prefix="/v2/memory",
tags=["memory"],
dependencies=[Depends(enforce_rate_limit)],
)
# Helpers
def _model_name(model: Any) -> str:
return getattr(model, "model", getattr(model, "model_name", "unknown"))
def _score_value(score: float | None) -> float:
if score is None:
return 0.0
try:
value = float(score)
except (TypeError, ValueError):
return 0.0
if not math.isfinite(value):
return 0.0
return round(value, 3)
def _build_domain_result(judge: Any, weaver: Any) -> DomainResult | None:
if not judge or not getattr(judge, "operations", None):
return None
ops = [
OperationDetail(type=op.type.value, content=op.content, reason=op.reason)
for op in judge.operations
]
ws = None
if weaver:
ws = WeaverSummary(
succeeded=weaver.succeeded, skipped=weaver.skipped, failed=weaver.failed,
)
return DomainResult(confidence=judge.confidence, operations=ops, weaver=ws)
def _wrap(request: Request, data: Any, elapsed_ms: float) -> JSONResponse:
body = APIResponse(
status=StatusEnum.OK,
request_id=getattr(request.state, "request_id", None),
data=data.model_dump() if hasattr(data, "model_dump") else data,
elapsed_ms=elapsed_ms,
)
resp = JSONResponse(content=body.model_dump())
remaining = getattr(request.state, "rate_limit_remaining", None)
if remaining is not None:
resp.headers["X-RateLimit-Remaining"] = str(remaining)
return resp
def _error(
request: Request,
detail: str,
code: int,
elapsed_ms: float = 0,
) -> JSONResponse:
if code >= 500 and settings.environment.lower() not in _LOCAL_ENVIRONMENTS:
detail = "The request could not be completed. Check the server logs with the request_id."
body = APIResponse(
status=StatusEnum.ERROR,
request_id=getattr(request.state, "request_id", None),
error=detail,
elapsed_ms=elapsed_ms,
)
return JSONResponse(content=body.model_dump(), status_code=code)
def _is_static_key_user(user: dict) -> bool:
return user.get("email") == "static@xmem.ai" or user.get("name") == "Static Key User"
def _current_user_id(user: dict, requested_user_id: str = "") -> str:
if (
requested_user_id
and settings.environment.lower() in _LOCAL_ENVIRONMENTS
and _is_static_key_user(user)
):
return requested_user_id
return user.get("username") or user.get("name") or user["id"]
def _scoped_ingest_payload(user: dict, item: IngestRequest) -> Dict[str, Any]:
payload = item.model_dump()
payload["user_id"] = _current_user_id(user, payload.get("user_id", ""))
return payload
def _job_status_data(job: Dict[str, Any]) -> Dict[str, Any]:
public = serialize_job(job) or {}
return {
"job_id": public.get("job_id"),
"job_type": public.get("job_type"),
"status": public.get("status"),
"retry_count": public.get("retry_count", 0),
"max_attempts": public.get("max_attempts", 0),
"timeout_seconds": public.get("timeout_seconds"),
"error": public.get("error"),
"error_state": public.get("error_state"),
"result": public.get("result"),
"created_at": public.get("created_at"),
"updated_at": public.get("updated_at"),
"started_at": public.get("started_at"),
"completed_at": public.get("completed_at"),
"dead_lettered_at": public.get("dead_lettered_at"),
}
def _job_accepted(
request: Request,
job: Dict[str, Any],
created: bool,
status_url: str,
elapsed_ms: float,
) -> JSONResponse:
data = {
"job_id": job["job_id"],
"status": job.get("status", QUEUED),
"created": created,
"status_url": status_url,
}
return _wrap(request, data, elapsed_ms)
async def _run_ingest_payload(
payload: Dict[str, Any],
user_id: str,
) -> Dict[str, Any]:
pipeline = get_ingest_pipeline()
async with _ingest_semaphore:
result = await pipeline.run(
user_query=payload["user_query"],
agent_response=payload.get("agent_response") or "Acknowledged.",
user_id=user_id,
session_datetime=payload.get("session_datetime", ""),
image_url=payload.get("image_url", ""),
effort_level=payload.get("effort_level", "low"),
)
data = IngestResponse(
model=_model_name(pipeline.model),
classification=_safe_classifications(result),
profile=_build_domain_result(
result.get("profile_judge"),
result.get("profile_weaver"),
),
temporal=_build_domain_result(
result.get("temporal_judge"),
result.get("temporal_weaver"),
),
summary=_build_domain_result(
result.get("summary_judge"),
result.get("summary_weaver"),
),
image=_build_domain_result(
result.get("image_judge"),
result.get("image_weaver"),
),
)
return data.model_dump()
async def _run_batch_ingest_payload(
payload: Dict[str, Any],
) -> Dict[str, Any]:
results = []
for item in payload["items"]:
item_user_id = item.get("user_id") or payload["user_id"]
results.append(await _run_ingest_payload(item, item_user_id))
return {"results": results}
async def _run_scrape_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
result = await _scrape_chat_share(payload["url"])
pairs = result["pairs"]
if not pairs:
raise ValueError(_chat_share_error_message(result))
return ScrapeResponse(pairs=pairs).model_dump()
def _schedule_job(job: Dict[str, Any], handler) -> None:
if job.get("status") == QUEUED:
asyncio.create_task(run_job(get_default_job_store(), job["job_id"], handler))
def _detect_chat_provider(*urls: str) -> str:
for url in urls:
lowered = (url or "").lower()
if not lowered:
continue
if "chatgpt.com" in lowered or "chat.openai.com" in lowered or "openai.com" in lowered:
return "chatgpt"
if "claude.ai" in lowered or "claude.com" in lowered:
return "claude"
if "gemini.google.com" in lowered or "g.co/gemini" in lowered:
return "gemini"
return "unknown"
def _chat_share_error_message(result: Dict[str, Any]) -> str:
provider = result.get("provider") or "unknown"
if provider == "unknown":
return (
"Failed to extract messages from the provided link. "
"Please provide a public ChatGPT, Claude, or Gemini share link."
)
return (
f"Failed to extract messages from the provided {provider} share link. "
"Please confirm the link is public, exists, and is not redirecting to a login or deleted-chat page."
)
async def _render_chat_share(url: str) -> tuple[str, str]:
return await asyncio.to_thread(_render_chat_share_sync, url)
# ── Warm browser pool ──────────────────────────────────────────────────────
# Launching Chromium from cold takes 3-5s. We keep a singleton alive and
# reuse it across scrape requests. The browser is thread-safe when each
# request uses its own BrowserContext.
_browser_lock = threading.Lock()
_pw_instance = None
_browser_instance = None
def _get_or_create_browser():
"""Return a long-lived Playwright browser, launching one if needed."""
global _pw_instance, _browser_instance
with _browser_lock:
# If the browser is still alive, reuse it
if _browser_instance is not None and _browser_instance.is_connected():
return _browser_instance
# Tear down stale Playwright context if any
if _pw_instance is not None:
try:
_pw_instance.stop()
except Exception:
pass
_pw_instance = sync_playwright().start()
launch_errors = []
for channel in (None, "msedge", "chrome"):
try:
kwargs = {"headless": True}
if channel:
kwargs["channel"] = channel
_browser_instance = _pw_instance.chromium.launch(**kwargs)
logger.info("[scrape] Playwright browser launched (channel=%s)", channel or "bundled")
return _browser_instance
except Exception as exc:
launch_errors.append(f"{channel or 'bundled chromium'}: {exc}")
raise RuntimeError(
"Could not launch Playwright browser. Tried bundled Chromium, "
f"Edge, and Chrome. Errors: {' | '.join(launch_errors)}"
)
def _render_chat_share_sync(url: str) -> tuple[str, str]:
html = ""
final_url = url
browser = _get_or_create_browser()
context = browser.new_context(
user_agent=(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/123.0.0.0 Safari/537.36"
),
viewport={"width": 1280, "height": 800},
ignore_https_errors=True,
)
try:
page = context.new_page()
def _block_heavy_assets(route):
rtype = route.request.resource_type
if rtype in {"image", "media", "font", "stylesheet"}:
route.abort()
return
route.continue_()
page.route("**/*", _block_heavy_assets)
try:
# domcontentloaded is much faster than networkidle — we don't
# need to wait for analytics / tracking pixels to finish.
page.goto(url, wait_until="domcontentloaded", timeout=15000)
except Exception as exc:
logger.warning("Timeout or error during navigation: %s", exc)
provider = _detect_chat_provider(page.url, url)
selector = {
"chatgpt": "div[data-message-author-role]",
"claude": "script",
"gemini": "message-content, div.user-query, div.model-response",
}.get(provider)
if selector:
try:
page.wait_for_selector(selector, timeout=8000)
except Exception as exc:
logger.warning("Timed out waiting for %s content: %s", provider, exc)
# No hardcoded sleep — the selector wait above already guarantees
# the chat content DOM nodes are present.
final_url = page.url
html = page.content()
finally:
context.close()
# NOTE: we intentionally do NOT close the browser — it's pooled.
return html, final_url
def _extract_chat_pairs(
url: str,
html: str,
source_url: str = "",
) -> tuple[str, str, List[MessagePair]]:
provider = _detect_chat_provider(url, source_url)
soup = BeautifulSoup(html, "html.parser")
pairs: List[MessagePair] = []
extraction_method = "none"
if provider == "chatgpt":
user_msgs = soup.find_all("div", {"data-message-author-role": "user"})
asst_msgs = soup.find_all("div", {"data-message-author-role": "assistant"})
for u, a in zip(user_msgs, asst_msgs):
pairs.append(MessagePair(
user_query=u.get_text(separator="\n").strip(),
agent_response=a.get_text(separator="\n").strip(),
))
if pairs:
extraction_method = "dom"
elif provider == "claude":
script_state = soup.find("script", string=re.compile(r"__PRELOADED_STATE__"))
if script_state and script_state.string:
try:
match = re.search(
r"__PRELOADED_STATE__\s*=\s*(\{.*?\});",
script_state.string,
re.DOTALL,
)
if match:
data = json.loads(match.group(1))
messages = data.get("chat", {}).get("messages", [])
current_user = ""
for msg in messages:
if msg.get("sender") == "human":
current_user = msg.get("text", "")
elif msg.get("sender") == "assistant":
pairs.append(MessagePair(
user_query=current_user,
agent_response=msg.get("text", ""),
))
current_user = ""
if pairs:
extraction_method = "structured"
except Exception as exc:
logger.warning("Failed to parse Claude preloaded state: %s", exc)
elif provider == "gemini":
user_blocks = soup.select("message-content[role='user'], div.user-query")
model_blocks = soup.select("message-content[role='model'], div.model-response")
for u, m in zip(user_blocks, model_blocks):
pairs.append(MessagePair(
user_query=u.get_text(separator="\n").strip(),
agent_response=m.get_text(separator="\n").strip(),
))
if pairs:
extraction_method = "dom"
if not pairs and provider == "unknown":
paragraphs = [
p.get_text(separator="\n", strip=True)
for p in soup.find_all(["p", "div", "span"])
if len(p.get_text(strip=True)) > 50
]
unique_paras = []
for paragraph in paragraphs:
if paragraph not in unique_paras:
unique_paras.append(paragraph)
if unique_paras:
text = "\n\n".join(unique_paras[:50])
pairs.append(MessagePair(
user_query="Extracted text from link",
agent_response=text[:10000],
))
extraction_method = "fallback"
return provider, extraction_method, pairs
def _parse_cursor_transcript(text: str) -> List[MessagePair]:
"""Parse a Cursor-exported markdown transcript into message pairs.
Cursor transcripts have the format:
_Exported on ... from Cursor_
---
**User**
<user message>
---
**Cursor**
<agent response>
---
...
"""
pairs: List[MessagePair] = []
# Split by --- separator
sections = text.split("---")
# Skip the first section if it's the header (contains "Exported on")
start_idx = 0
if sections and "Exported on" in sections[0]:
start_idx = 1
current_user_query = None
for section in sections[start_idx:]:
section = section.strip()
if not section:
continue
# Check if this is a User message
if section.startswith("**User**"):
# Extract the user message (remove the **User** header)
content = section.replace("**User**", "", 1).strip()
current_user_query = content
# Check if this is a Cursor/Agent message
elif section.startswith("**Cursor**") or section.startswith("**Assistant**"):
# Extract the agent response
content = section.replace("**Cursor**", "", 1).replace("**Assistant**", "", 1).strip()
# If we have a user query, create a pair
if current_user_query:
pairs.append(MessagePair(
user_query=current_user_query,
agent_response=content,
))
current_user_query = None
return pairs
def _parse_antigravity_transcript(text: str) -> List[MessagePair]:
"""Parse an Antigravity-exported markdown transcript into message pairs.
Antigravity transcripts exported from the Antigravity coding assistant
follow this format::
# Chat Conversation
Note: _This is purely the output of the chat conversation..._
### User Input
<user message>
### Planner Response
<agent response>
### User Input
...
Multiple consecutive ``### Planner Response`` blocks (e.g. when the agent
used tools between messages) are concatenated into a single agent response.
"""
pairs: List[MessagePair] = []
# Normalise line endings
text = text.replace("\r\n", "\n")
# Split into blocks by H3 headings (### ...)
# We keep the heading so we know which role each block belongs to.
blocks = re.split(r"(?m)^(###\s+.+)$", text)
current_user_query: str | None = None
planner_chunks: List[str] = []
for i, block in enumerate(blocks):
block = block.strip()
if not block:
continue
if re.match(r"###\s+User Input", block, re.IGNORECASE):
# Flush any pending planner chunks as a completed pair
if current_user_query and planner_chunks:
pairs.append(MessagePair(
user_query=current_user_query,
agent_response="\n\n".join(planner_chunks).strip(),
))
planner_chunks = []
# The next block (index i+1) is the content of this user turn
current_user_query = None # will be filled by the content block below
elif re.match(r"###\s+Planner Response", block, re.IGNORECASE):
# The next content block belongs to the agent
pass # content handled in the else branch below
else:
# This is a content block — figure out which role it belongs to by
# looking at the previous heading token.
if i > 0:
prev_heading = blocks[i - 1].strip() if i >= 1 else ""
if re.match(r"###\s+User Input", prev_heading, re.IGNORECASE):
# New user turn — flush previous pair first
if current_user_query and planner_chunks:
pairs.append(MessagePair(
user_query=current_user_query,
agent_response="\n\n".join(planner_chunks).strip(),
))
planner_chunks = []
current_user_query = block
elif re.match(r"###\s+Planner Response", prev_heading, re.IGNORECASE):
# Accumulate (multiple tool-use steps = multiple planner blocks)
if block:
planner_chunks.append(block)
# Flush last pair
if current_user_query and planner_chunks:
pairs.append(MessagePair(
user_query=current_user_query,
agent_response="\n\n".join(planner_chunks).strip(),
))
return pairs
async def _parse_transcript_with_llm(text: str) -> List[MessagePair]:
"""Use an LLM to parse transcript text when format detection fails."""
from src.models import get_model
# Limit text size to avoid token issues
max_chars = 50000
if len(text) > max_chars:
text = text[:max_chars]
model = get_model(temperature=0.0)
prompt = f"""You are parsing a chat transcript. Extract all user-agent message pairs from the following text.
Return a JSON array of objects with this structure:
[
{{"user_query": "...", "agent_response": "..."}},
...
]
Only return valid JSON, nothing else.
Transcript:
{text}
"""
try:
response = await model.ainvoke(prompt)
content = response.content if hasattr(response, "content") else str(response)
# Try to extract JSON from the response
json_match = re.search(r'\[.*\]', content, re.DOTALL)
if json_match:
data = json.loads(json_match.group(0))
pairs = [
MessagePair(user_query=item.get("user_query", ""), agent_response=item.get("agent_response", ""))
for item in data
if isinstance(item, dict)
]
return pairs
except Exception as exc:
logger.warning("LLM transcript parsing failed: %s", exc)
return []
def _parse_transcript_text(text: str) -> tuple[str, List[MessagePair]]:
"""Parse transcript text and return (format, pairs)."""
# Detect Cursor format
if "_Exported on" in text and "from Cursor" in text:
pairs = _parse_cursor_transcript(text)
if pairs:
return "cursor", pairs
# Detect Antigravity format
if "# Chat Conversation" in text and ("### User Input" in text or "### Planner Response" in text):
pairs = _parse_antigravity_transcript(text)
if pairs:
return "antigravity", pairs
return "unknown", []
async def _scrape_chat_share(url: str) -> Dict[str, Any]:
html, final_url = await _render_chat_share(url)
provider, extraction_method, pairs = _extract_chat_pairs(final_url or url, html, url)
return {
"provider": provider,
"url": url,
"final_url": final_url,
"pairs": pairs,
"pair_count": len(pairs),
"html_length": len(html),
"extraction_method": extraction_method,
}
# POST /v1/memory/ingest
@router.post(
"/ingest",
response_model=APIResponse,
summary="Ingest a conversation turn into long-term memory",
)
async def ingest_memory(req: IngestRequest, request: Request, user: dict = Depends(require_api_key)):
start = time.perf_counter()
user_id = _current_user_id(user, req.user_id)
payload = req.model_dump()
try:
data = await asyncio.wait_for(
_run_ingest_payload(payload, user_id),
timeout=float(settings.memory_ingest_timeout_seconds),
)
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _wrap(request, data, elapsed)
except Exception as exc:
elapsed = round((time.perf_counter() - start) * 1000, 2)
logger.exception("Ingest failed for user=%s", user_id)
return _error(request, str(exc), 500, elapsed)
# POST /v2/memory/ingest
@v2_router.post(
"/ingest",
response_model=APIResponse,
summary="Start an async durable memory ingest job",
)
async def ingest_memory_v2(req: IngestRequest, request: Request, user: dict = Depends(require_api_key)):
start = time.perf_counter()
user_id = _current_user_id(user, req.user_id)
job_user_id = _current_user_id(user)
payload = req.model_dump()
payload["user_id"] = user_id
try:
store = get_default_job_store()
job, created = await asyncio.to_thread(
store.enqueue,
job_type="memory_ingest",
payload=payload,
idempotency_fields={
"user_id": user_id,
"user_query": req.user_query,
"agent_response": req.agent_response or "",
"session_datetime": req.session_datetime,
"image_url": req.image_url,
"effort_level": req.effort_level,
},
user_id=job_user_id,
timeout_seconds=float(settings.memory_ingest_timeout_seconds),
max_attempts=3,
)
_schedule_job(
job,
lambda: _run_ingest_payload(payload, user_id),
)
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _job_accepted(
request,
job,
created,
f"/v2/memory/ingest/{job['job_id']}/status",
elapsed,
)
except Exception as exc:
elapsed = round((time.perf_counter() - start) * 1000, 2)
logger.exception("Ingest enqueue failed for user=%s", user_id)
return _error(request, str(exc), 500, elapsed)
def _safe_classifications(result: Dict[str, Any]) -> list:
cr = result.get("classification_result")
if cr and getattr(cr, "classifications", None):
return cr.classifications
return []
async def _read_user_job(job_id: str, user_id: str) -> Dict[str, Any] | None:
job = await asyncio.to_thread(get_default_job_store().get, job_id)
if not job:
return None
if job.get("user_id") != user_id:
return None
return job
@v2_router.get(
"/ingest/{job_id}/status",
response_model=APIResponse,
summary="Poll an async memory ingest job",
)
async def ingest_job_status(job_id: str, request: Request, user: dict = Depends(require_api_key)):
start = time.perf_counter()
job = await _read_user_job(job_id, _current_user_id(user))
if not job:
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _error(request, "Job not found.", 404, elapsed)
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _wrap(request, _job_status_data(job), elapsed)
@v2_router.get(
"/jobs/{job_id}/status",
response_model=APIResponse,
summary="Poll an async memory job",
)
async def memory_job_status(job_id: str, request: Request, user: dict = Depends(require_api_key)):
start = time.perf_counter()
job = await _read_user_job(job_id, _current_user_id(user))
if not job:
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _error(request, "Job not found.", 404, elapsed)
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _wrap(request, _job_status_data(job), elapsed)
# POST /v1/memory/batch-ingest
@router.post(
"/batch-ingest",
response_model=APIResponse,
summary="Ingest multiple conversation turns into long-term memory sequentially",
)
async def batch_ingest_memory(req: BatchIngestRequest, request: Request, user: dict = Depends(require_api_key)):
start = time.perf_counter()
user_id = _current_user_id(user)
try:
results = []
for item in req.items:
payload = _scoped_ingest_payload(user, item)
data = await asyncio.wait_for(
_run_ingest_payload(payload, payload["user_id"]),
timeout=float(settings.memory_ingest_timeout_seconds),
)
results.append(IngestResponse(**data))
response_data = BatchIngestResponse(results=results)
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _wrap(request, response_data, elapsed)
except Exception as exc:
elapsed = round((time.perf_counter() - start) * 1000, 2)
logger.exception("Batch ingest failed for user=%s", user_id)
return _error(request, str(exc), 500, elapsed)
# POST /v2/memory/batch-ingest
@v2_router.post(
"/batch-ingest",
response_model=APIResponse,
summary="Start an async durable batch memory ingest job",
)
async def batch_ingest_memory_v2(req: BatchIngestRequest, request: Request, user: dict = Depends(require_api_key)):
start = time.perf_counter()
user_id = _current_user_id(user)
payload = req.model_dump()
payload["user_id"] = user_id
payload["items"] = [_scoped_ingest_payload(user, item) for item in req.items]
try:
store = get_default_job_store()
job, created = await asyncio.to_thread(
store.enqueue,
job_type="memory_batch_ingest",
payload=payload,
idempotency_fields={
"user_id": user_id,
"items": payload["items"],
},
user_id=user_id,
timeout_seconds=max(
float(settings.memory_ingest_timeout_seconds),
min(len(req.items) * float(settings.memory_ingest_timeout_seconds), 3600.0),
),
max_attempts=3,
)
_schedule_job(
job,
lambda: _run_batch_ingest_payload(payload),
)
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _job_accepted(
request,
job,
created,
f"/v2/memory/jobs/{job['job_id']}/status",
elapsed,
)
except Exception as exc:
elapsed = round((time.perf_counter() - start) * 1000, 2)
logger.exception("Batch ingest enqueue failed for user=%s", user_id)
return _error(request, str(exc), 500, elapsed)
# POST /v1/memory/retrieve
@router.post(
"/retrieve",
response_model=APIResponse,
summary="Retrieve an LLM-generated answer backed by stored memories",
)
async def retrieve_memory(req: RetrieveRequest, request: Request, user: dict = Depends(require_api_key)):
start = time.perf_counter()
pipeline = get_retrieval_pipeline()
# Get username from authenticated user
user_id = _current_user_id(user, req.user_id)
try:
result = await pipeline.run(query=req.query, user_id=user_id, top_k=req.top_k)
data = RetrieveResponse(
model=_model_name(pipeline.model),
answer=result.answer,
sources=[
SourceRecord(
domain=s.domain, content=s.content,
score=_score_value(s.score), metadata=s.metadata,
)
for s in result.sources
],
confidence=result.confidence,
)
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _wrap(request, data, elapsed)
except Exception as exc:
elapsed = round((time.perf_counter() - start) * 1000, 2)
logger.exception("Retrieve failed for user=%s", user_id)
return _error(request, str(exc), 500, elapsed)
# POST /v1/memory/search
@router.post(
"/search",
response_model=APIResponse,
summary="Raw semantic search across memory domains (no LLM answer)",
)
async def search_memory(req: SearchRequest, request: Request, user: dict = Depends(require_api_key)):
start = time.perf_counter()
pipeline = get_retrieval_pipeline()
# Get username from authenticated user
user_id = _current_user_id(user, req.user_id)
try:
if "code" in req.domains and not req.org_id:
elapsed = round((time.perf_counter() - start) * 1000, 2)
return _error(request, "org_id is required when searching the code domain.", 400, elapsed)
memory_domains = [domain for domain in req.domains if domain != "code"]
result = await pipeline.raw_search(
query=req.query,
user_id=user_id,
domains=memory_domains,
top_k=req.top_k,
include_answer=False,
)
records = list(result.sources)
if "code" in req.domains:
code_pipeline = get_code_pipeline(org_id=req.org_id or "", repo=req.repo)
code_results = await asyncio.gather(
code_pipeline._execute_tool(
tool_name="search_symbols",
tool_args={"query": req.query, "repo": req.repo},
repo=req.repo,
top_k=req.top_k,
user_id=user_id,
),
code_pipeline._execute_tool(
tool_name="search_files",
tool_args={"query": req.query, "repo": req.repo},
repo=req.repo,
top_k=req.top_k,
user_id=user_id,
),
return_exceptions=True,
)
for code_records in code_results:
if isinstance(code_records, Exception):
logger.warning("Code search subquery failed: %s", code_records)
continue
records.extend(code_records)
records = sorted(records, key=lambda s: s.score or 0.0, reverse=True)
answer = ""
if req.answer:
answer = await pipeline.answer_from_sources(query=req.query, sources=records)