@codemem/server 0.30.0-alpha.4 → 0.30.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -655,6 +655,29 @@ function countVisibleMemoryRows(store, filters) {
655
655
  const row = store.db.prepare(`SELECT COUNT(*) AS total FROM ${from} WHERE ${clauses.join(" AND ")}`).get(...filterResult.params);
656
656
  return Number(row?.total ?? 0);
657
657
  }
658
+ function summaryLikeSqlPredicate() {
659
+ return `(
660
+ LOWER(TRIM(COALESCE(memory_items.kind, ''))) = 'session_summary'
661
+ OR (
662
+ json_valid(COALESCE(memory_items.metadata_json, ''))
663
+ AND (
664
+ COALESCE(json_extract(memory_items.metadata_json, '$.is_summary') = 1, 0)
665
+ OR LOWER(TRIM(COALESCE(json_extract(memory_items.metadata_json, '$.source'), ''))) = 'observer_summary'
666
+ )
667
+ )
668
+ )`;
669
+ }
670
+ function countVisibleObservationRows(store, filters) {
671
+ const filterResult = buildViewerMemoryFilters(store, filters);
672
+ const clauses = [
673
+ "memory_items.active = 1",
674
+ `NOT ${summaryLikeSqlPredicate()}`,
675
+ ...filterResult.clauses
676
+ ];
677
+ const from = filterResult.joinSessions ? "memory_items JOIN sessions ON sessions.id = memory_items.session_id" : "memory_items";
678
+ const row = store.db.prepare(`SELECT COUNT(*) AS total FROM ${from} WHERE ${clauses.join(" AND ")}`).get(...filterResult.params);
679
+ return Number(row?.total ?? 0);
680
+ }
658
681
  function sessionAllowsArtifactAccess(store, sessionId) {
659
682
  const visibleCount = countVisibleMemoryRows(store, { session_id: sessionId });
660
683
  if (visibleCount === 0) return false;
@@ -838,32 +861,16 @@ function memoryRoutes(getStore) {
838
861
  let artifacts;
839
862
  let memories;
840
863
  let observations;
841
- const countObservations = (scopeProject) => {
842
- let offset = 0;
843
- let total = 0;
844
- while (true) {
845
- const page = queryMemoryPage(store, {
846
- limit: 200,
847
- offset,
848
- project: scopeProject
849
- });
850
- if (page.length === 0) break;
851
- total += page.filter((item) => !isSummaryLikeMemory$1(item)).length;
852
- if (page.length < 200) break;
853
- offset += page.length;
854
- }
855
- return total;
856
- };
857
864
  if (project) {
858
865
  prompts = countVisiblePromptRows(store, project);
859
866
  artifacts = countVisibleArtifactRows(store, project);
860
867
  memories = countVisibleMemoryRows(store, { project });
861
- observations = countObservations(project);
868
+ observations = countVisibleObservationRows(store, { project });
862
869
  } else {
863
870
  prompts = countVisiblePromptRows(store);
864
871
  artifacts = countVisibleArtifactRows(store);
865
872
  memories = countVisibleMemoryRows(store);
866
- observations = countObservations();
873
+ observations = countVisibleObservationRows(store);
867
874
  }
868
875
  const total = prompts + artifacts + memories;
869
876
  return c.json({