openclaw-memory-alibaba-local 1.0.2 → 1.0.4
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/index.ts +4 -47
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -1852,58 +1852,14 @@ const memoryPlugin = {
|
|
|
1852
1852
|
);
|
|
1853
1853
|
}
|
|
1854
1854
|
|
|
1855
|
-
// --- Hooks:
|
|
1856
|
-
|
|
1857
|
-
// Session-start memory promise cache: stores the in-flight DB query Promise so
|
|
1858
|
-
// before_prompt_build can await it even if session_start hasn't finished yet.
|
|
1859
|
-
type SessionStartMemory = { category: MemoryCategory; text: string; importance: number };
|
|
1860
|
-
const _sessionStartPromiseCache = new Map<string, Promise<SessionStartMemory[]>>();
|
|
1861
|
-
|
|
1862
|
-
api.on("session_start", async (_event, ctx) => {
|
|
1863
|
-
if (!db) return;
|
|
1864
|
-
const sessionId = ctx.sessionId;
|
|
1865
|
-
// Store the Promise immediately — before_prompt_build will await it.
|
|
1866
|
-
const promise = (async (): Promise<SessionStartMemory[]> => {
|
|
1867
|
-
try {
|
|
1868
|
-
const agentId = resolveAgentIdForMemory(ctx);
|
|
1869
|
-
const topMemories = await db.listTopByImportance(agentId, [USER_MEMORY_FACT], 5);
|
|
1870
|
-
const entries = topMemories.map((m) => ({ category: m.category as MemoryCategory, text: m.text, importance: m.importance }));
|
|
1871
|
-
console.log(
|
|
1872
|
-
`[openclaw-memory-alibaba-local] session_start loaded ${entries.length} user_memory_fact for session=${sessionId} agent=${agentId}`,
|
|
1873
|
-
);
|
|
1874
|
-
return entries;
|
|
1875
|
-
} catch (err) {
|
|
1876
|
-
console.warn(`[openclaw-memory-alibaba-local] session_start memory load failed: ${String(err)}`);
|
|
1877
|
-
return [];
|
|
1878
|
-
}
|
|
1879
|
-
})();
|
|
1880
|
-
_sessionStartPromiseCache.set(sessionId, promise);
|
|
1881
|
-
});
|
|
1855
|
+
// --- Hooks: before_prompt_build (recall), agent_end (auto-capture) ---
|
|
1882
1856
|
|
|
1883
1857
|
if (cfg.autoRecall) {
|
|
1884
1858
|
api.on("before_prompt_build", async (event, ctx) => {
|
|
1859
|
+
if ((ctx as { trigger?: string }).trigger !== "user") return;
|
|
1885
1860
|
if (!db || !backend) return;
|
|
1886
1861
|
if (!event.prompt || event.prompt.length < 5) return;
|
|
1887
1862
|
|
|
1888
|
-
// Await session-start promise (inject once on the first prompt of a new session)
|
|
1889
|
-
// Timeout after 10s to avoid blocking prompt build if DB is slow.
|
|
1890
|
-
const sessionId = (ctx as { sessionId?: string }).sessionId ?? "";
|
|
1891
|
-
const sessionStartPromise = sessionId ? _sessionStartPromiseCache.get(sessionId) : undefined;
|
|
1892
|
-
if (sessionStartPromise) {
|
|
1893
|
-
_sessionStartPromiseCache.delete(sessionId);
|
|
1894
|
-
const timeout = new Promise<SessionStartMemory[]>((resolve) => setTimeout(() => resolve([]), 10_000));
|
|
1895
|
-
const entries = await Promise.race([sessionStartPromise, timeout]);
|
|
1896
|
-
if (entries.length > 0) {
|
|
1897
|
-
// Session-start: inject cached memories directly, skip recall
|
|
1898
|
-
console.log(
|
|
1899
|
-
`[openclaw-memory-alibaba-local] session_start inject ${entries.length} cached memories (importance desc), skip recall`,
|
|
1900
|
-
);
|
|
1901
|
-
return {
|
|
1902
|
-
prependContext: formatRelevantMemoriesContext(entries),
|
|
1903
|
-
};
|
|
1904
|
-
}
|
|
1905
|
-
}
|
|
1906
|
-
|
|
1907
1863
|
try {
|
|
1908
1864
|
const extracted = extractUserQueryForRecall(event.prompt);
|
|
1909
1865
|
if (extracted.query.length < 5) {
|
|
@@ -1954,7 +1910,7 @@ const memoryPlugin = {
|
|
|
1954
1910
|
`[openclaw-memory-alibaba-local] injecting ${results.length} memories into context`,
|
|
1955
1911
|
);
|
|
1956
1912
|
return {
|
|
1957
|
-
|
|
1913
|
+
appendSystemContext: formatRelevantMemoriesContext(
|
|
1958
1914
|
results.map((r) => {
|
|
1959
1915
|
let imp = r.entry.importance ?? 0;
|
|
1960
1916
|
// 将 importance 按与 score 相同的时间衰减因子修正,使显示值与实际召回权重一致
|
|
@@ -1986,6 +1942,7 @@ const memoryPlugin = {
|
|
|
1986
1942
|
|
|
1987
1943
|
if (cfg.autoCapture) {
|
|
1988
1944
|
api.on("agent_end", async (event, ctx) => {
|
|
1945
|
+
if ((ctx as { trigger?: string }).trigger !== "user") return;
|
|
1989
1946
|
if (!db || !backend) {
|
|
1990
1947
|
return;
|
|
1991
1948
|
}
|