Skip to content
Closed
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
1 change: 1 addition & 0 deletions apps/memos-local-openclaw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ All optional — shown with defaults:
"mmrLambda": 0.7, // MMR relevance vs diversity (0-1)
"recencyHalfLifeDays": 14, // Time decay half-life
"vectorSearchMaxChunks": 0, // 0 = search all (default). Set 200000–300000 only if search is slow on huge DBs
"topicJudgeRounds": 0, // Optional LLM pre-filter. Positive values skip auto-recall when the prompt continues the current topic. 0 disables it.
"autoRecallMinQueryLength": 4 // Skip auto-recall when the normalised user prompt is shorter than this many chars (filters "好的"/"可以"/"?"). 0 disables the guard. Has no effect on explicit memory_search tool calls.
},
"dedup": {
Expand Down
14 changes: 14 additions & 0 deletions apps/memos-local-openclaw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Embedder } from "./src/embedding";
import { IngestWorker } from "./src/ingest/worker";
import { RecallEngine } from "./src/recall/engine";
import { shouldSkipAutoRecallForSession } from "./src/recall/session-policy";
import { topicJudgePreFilter } from "./src/recall/topic-judge";
import { captureMessages, stripInboundMetadata } from "./src/capture";
import { DEFAULTS } from "./src/types";
import type { SearchHit } from "./src/types";
Expand Down Expand Up @@ -1846,6 +1847,19 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`,
}
ctx.log.debug(`auto-recall: query="${query.slice(0, 80)}"`);

const topicDecision = await topicJudgePreFilter({
messages: event.messages,
query,
topicJudgeRounds: ctx.config.recall?.topicJudgeRounds ?? DEFAULTS.topicJudgeRounds,
isNewSession,
summarizer,
log: ctx.log,
});
if (topicDecision === "skip") {
ctx.log.debug("auto-recall: topic continuation detected, skipping recall");
return;
}

// ── Phase 1: Local search ∥ Hub search (parallel) ──
// Issue #1514: previously hardcoded maxResults: 10 here, which made
// `recall.maxResultsDefault` and the new `recall.autoRecallMaxResults`
Expand Down
1 change: 1 addition & 0 deletions apps/memos-local-openclaw/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function resolveConfig(raw: Partial<MemosLocalConfig> | undefined, stateD
mmrLambda: cfg.recall?.mmrLambda ?? DEFAULTS.mmrLambda,
recencyHalfLifeDays: cfg.recall?.recencyHalfLifeDays ?? DEFAULTS.recencyHalfLifeDays,
vectorSearchMaxChunks: cfg.recall?.vectorSearchMaxChunks ?? DEFAULTS.vectorSearchMaxChunks,
topicJudgeRounds: cfg.recall?.topicJudgeRounds ?? DEFAULTS.topicJudgeRounds,
autoRecallMinQueryLength: cfg.recall?.autoRecallMinQueryLength ?? DEFAULTS.autoRecallMinQueryLength,
},
dedup: {
Expand Down
138 changes: 138 additions & 0 deletions apps/memos-local-openclaw/src/recall/topic-judge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { stripInboundMetadata } from "../capture";
import type { Logger } from "../types";

export type TopicJudgeDecision = "skip" | "proceed";

interface TopicJudgeSummarizer {
judgeNewTopic(currentContext: string, newMessage: string): Promise<boolean | null>;
}

interface TopicJudgeOptions {
messages: unknown[] | undefined;
query: string;
topicJudgeRounds: number;
isNewSession?: boolean;
summarizer: TopicJudgeSummarizer;
log: Logger;
}

interface ConversationLine {
role: "user" | "assistant";
text: string;
}

const MAX_MESSAGES_TO_INSPECT = 20;
const MAX_CONTEXT_LINE_CHARS = 500;
const CONTEXT_EDGE_CHARS = 150;

function extractText(content: unknown): string {
if (typeof content === "string") return content.trim();
if (!Array.isArray(content)) return "";

return content
.filter(
(block): block is { type: "text"; text: string } =>
typeof block === "object"
&& block !== null
&& (block as { type?: unknown }).type === "text"
&& typeof (block as { text?: unknown }).text === "string",
)
.map((block) => block.text)
.join(" ")
.trim();
}

function truncateContextLine(text: string): string {
if (text.length <= MAX_CONTEXT_LINE_CHARS) return text;
return `${text.slice(0, CONTEXT_EDGE_CHARS)}...${text.slice(-CONTEXT_EDGE_CHARS)}`;
}

/** Build completed user/assistant rounds without the current user prompt. */
export function buildTopicJudgeContext(
messages: unknown[] | undefined,
topicJudgeRounds: number,
): string | null {
if (!Array.isArray(messages) || !Number.isFinite(topicJudgeRounds) || topicJudgeRounds <= 0) {
return null;
}

const merged: ConversationLine[] = [];
for (const value of messages.slice(-MAX_MESSAGES_TO_INSPECT)) {
if (typeof value !== "object" || value === null) continue;
const message = value as { role?: unknown; content?: unknown };
if (message.role !== "user" && message.role !== "assistant") continue;

const text = extractText(message.content);
if (!text) continue;

const previous = merged.at(-1);
if (previous?.role === message.role) {
previous.text += `\n\n${text}`;
} else {
merged.push({ role: message.role, text });
}
}

// before_prompt_build passes the current prompt separately. A trailing user
// message therefore represents an incomplete historical round.
if (merged.at(-1)?.role === "user") merged.pop();

const maxLines = Math.floor(topicJudgeRounds) * 2;
const completed = merged.slice(-maxLines);
while (completed.length > 0 && completed[0].role !== "user") completed.shift();
while (completed.length > 0 && completed.at(-1)?.role !== "assistant") completed.pop();

const contextLines = completed.flatMap(({ role, text }) => {
const cleaned = role === "user" ? stripInboundMetadata(text) : text.trim();
if (!cleaned) return [];
return [`${role === "user" ? "USER" : "ASSISTANT"}: ${truncateContextLine(cleaned)}`];
});

if (
contextLines.length < 2
|| !contextLines.some((line) => line.startsWith("USER:"))
|| !contextLines.some((line) => line.startsWith("ASSISTANT:"))
) {
return null;
}

return contextLines.join("\n");
}

/**
* Skip auto-recall only when topic classification conclusively returns SAME.
* Missing context, parse failures, and provider errors all fail open.
*/
export async function topicJudgePreFilter({
messages,
query,
topicJudgeRounds,
isNewSession,
summarizer,
log,
}: TopicJudgeOptions): Promise<TopicJudgeDecision> {
if (!Number.isFinite(topicJudgeRounds) || topicJudgeRounds <= 0) return "proceed";
if (isNewSession) {
log.debug("auto-recall: new session detected; bypassing topic-judge");
return "proceed";
}

try {
const currentContext = buildTopicJudgeContext(messages, topicJudgeRounds);
if (!currentContext) {
log.debug("auto-recall: topic-judge has insufficient completed context; proceeding");
return "proceed";
}

log.debug(`auto-recall: topic-judge evaluating ${currentContext.split("\n").length} context lines`);
const isNewTopic = await summarizer.judgeNewTopic(currentContext, query);
if (isNewTopic === false) return "skip";
if (isNewTopic === true) return "proceed";

log.warn("auto-recall: topic-judge returned no parseable decision; proceeding");
return "proceed";
} catch (error) {
log.warn(`auto-recall: topic-judge failed (${String(error)}); proceeding`);
return "proceed";
}
}
8 changes: 8 additions & 0 deletions apps/memos-local-openclaw/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ export interface MemosLocalConfig {
recencyHalfLifeDays?: number;
/** Cap vector search to this many most recent chunks. 0 = no cap (search all; may get slower with 200k+ chunks). If you set a cap for performance, use a large value (e.g. 200000–300000) so older memories are still in the window; FTS always searches all. */
vectorSearchMaxChunks?: number;
/**
* Number of completed conversation rounds used to decide whether the
* current prompt continues the same topic. Same-topic prompts skip
* auto-recall. Default: 0 (disabled); set a positive value to opt in.
*/
topicJudgeRounds?: number;
/**
* Minimum length (in UTF-16 code units, i.e. `string.length`) of the
* normalised auto-recall query. When the user prompt is shorter than
Expand Down Expand Up @@ -384,6 +390,8 @@ export const DEFAULTS = {
mmrLambda: 0.7,
recencyHalfLifeDays: 14,
vectorSearchMaxChunks: 0,
/** Topic judging is opt-in because it adds an LLM call before auto-recall. */
topicJudgeRounds: 0,
/**
* Default minimum length of the normalised auto-recall query before
* the `before_prompt_build` hook will run a memory search. Filters
Expand Down
16 changes: 16 additions & 0 deletions apps/memos-local-openclaw/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import { describe, expect, it } from "vitest";
import { resolveConfig } from "../src/config";
import { DEFAULTS } from "../src/types";

describe("resolveConfig recall.topicJudgeRounds", () => {
it("defaults to disabled", () => {
const resolved = resolveConfig({}, "/tmp/memos-config-topic-judge-default");
expect(resolved.recall?.topicJudgeRounds).toBe(DEFAULTS.topicJudgeRounds);
expect(resolved.recall?.topicJudgeRounds).toBe(0);
});

it("preserves an explicit opt-in value", () => {
const resolved = resolveConfig(
{ recall: { topicJudgeRounds: 4 } },
"/tmp/memos-config-topic-judge-override",
);
expect(resolved.recall?.topicJudgeRounds).toBe(4);
});
});

describe("resolveConfig recall.autoRecallMinQueryLength", () => {
it("defaults to DEFAULTS.autoRecallMinQueryLength when not set", () => {
const resolved = resolveConfig({}, "/tmp/memos-config-min-query-default");
Expand Down
Loading
Loading