Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "./lib/validate.js"
import { log } from "./logger.ts"
import { clampEntityContext } from "./memory.ts"
import { buildAddMemoryMetadata } from "./metadata.ts"

export type SearchResult = {
id: string
Expand Down Expand Up @@ -72,14 +73,7 @@ export class SupermemoryClient {
const cleaned = sanitizeContent(content)
const tag = containerTag ?? this.containerTag

// Always stamp `sm_source` so mono's `document.source` column attributes
// these writes to the OpenClaw plugin. Existing callers can still pass
// extra metadata (e.g. `source: "openclaw_tool"`) and it is preserved
// underneath the canonical `sm_source` key.
const mergedMetadata: Record<string, string | number | boolean> = {
sm_source: "openclaw",
...(metadata ?? {}),
}
const mergedMetadata = buildAddMemoryMetadata(metadata)

log.debugRequest("add", {
contentLength: cleaned.length,
Expand Down
2 changes: 1 addition & 1 deletion hooks/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function buildCaptureHandler(
try {
await client.addMemory(
content,
{ source: "openclaw", timestamp: new Date().toISOString() },
{ source: "openclaw" },
customId,
undefined,
cfg.entityContext,
Expand Down
46 changes: 46 additions & 0 deletions metadata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, test } from "bun:test"
import { buildAddMemoryMetadata } from "./metadata.ts"

const FIXED = new Date("2026-08-13T15:30:00.000Z")

describe("buildAddMemoryMetadata", () => {
test("stamps sm_source and captured_at when metadata is omitted", () => {
expect(buildAddMemoryMetadata(undefined, () => FIXED)).toEqual({
sm_source: "openclaw",
captured_at: "2026-08-13T15:30:00.000Z",
})
})

test("preserves caller fields and fills missing captured_at", () => {
expect(
buildAddMemoryMetadata(
{ type: "preference", source: "openclaw_command" },
() => FIXED,
),
).toEqual({
sm_source: "openclaw",
type: "preference",
source: "openclaw_command",
captured_at: "2026-08-13T15:30:00.000Z",
})
})

test("keeps an explicit captured_at", () => {
expect(
buildAddMemoryMetadata(
{ captured_at: "2026-07-01T00:00:00.000Z" },
() => FIXED,
),
).toEqual({
sm_source: "openclaw",
captured_at: "2026-07-01T00:00:00.000Z",
})
})

test("replaces blank captured_at", () => {
expect(buildAddMemoryMetadata({ captured_at: " " }, () => FIXED)).toEqual({
sm_source: "openclaw",
captured_at: "2026-08-13T15:30:00.000Z",
})
})
})
21 changes: 21 additions & 0 deletions metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export type MemoryMetadata = Record<string, string | number | boolean>

/** Merge caller metadata with OpenClaw defaults for `add()`. */
export function buildAddMemoryMetadata(
metadata?: MemoryMetadata,
now: () => Date = () => new Date(),
): MemoryMetadata {
const merged: MemoryMetadata = {
sm_source: "openclaw",
...(metadata ?? {}),
}

// Give extraction a real capture-time anchor (issue #61). Callers can still
// pass an explicit `captured_at` (e.g. when replaying older events).
const existing = merged.captured_at
if (typeof existing !== "string" || !existing.trim()) {
merged.captured_at = now().toISOString()
}

return merged
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"config.ts",
"logger.ts",
"memory.ts",
"metadata.ts",
"runtime.ts",
"types",
"lib/version-check.ts",
Expand All @@ -32,6 +33,7 @@
"check-types": "tsc --noEmit",
"lint": "bunx @biomejs/biome ci .",
"lint:fix": "bunx @biomejs/biome check --write .",
"test": "bun test",
"build:lib": "node -e \"const fs=require('node:fs'); fs.accessSync('lib/validate.js'); fs.accessSync('lib/validate.d.ts')\"",
"prepack": "npm run build"
},
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"lib/*.ts",
"lib/*.d.ts"
],
"exclude": ["node_modules", "dist"]
"exclude": ["node_modules", "dist", "**/*.test.ts"]
}