Skip to content

Commit facbfc9

Browse files
author
linyuan.yang
committed
memory
1 parent bf33776 commit facbfc9

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

‎packages/scorpio.ai/src/Memory/Service/MemoryService.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,26 @@ export class MemoryService {
338338
if (target.scope === MemoryScope.Global) {
339339
return { globalMenu: await this.store.listMenu(limit), workspaceMenu: [] };
340340
}
341+
const totalLimit = Math.max(0, Math.floor(limit));
342+
if (totalLimit === 0) return { globalMenu: [], workspaceMenu: [] };
343+
341344
const workspaceStore = await this.storeForTarget(target);
342-
const workspaceMenu = await workspaceStore.listMenu(limit);
343-
const globalLimit = Math.max(0, limit - workspaceMenu.length);
344-
const globalMenu = globalLimit > 0 ? await this.store.listMenu(globalLimit) : [];
345+
// 工作区与全局记忆分别保底,避免工作区条目先吃满总额度后让 global 完全消失。
346+
// 一侧不足时把空余额回填给另一侧,最终总量仍不超过调用方给出的 limit。
347+
const globalQuota = totalLimit > 1
348+
? Math.max(1, Math.floor(totalLimit * 0.4))
349+
: 0;
350+
const workspaceQuota = totalLimit - globalQuota;
351+
let [workspaceMenu, globalMenu] = await Promise.all([
352+
workspaceStore.listMenu(workspaceQuota),
353+
this.store.listMenu(globalQuota),
354+
]);
355+
356+
if (workspaceMenu.length < workspaceQuota) {
357+
globalMenu = await this.store.listMenu(totalLimit - workspaceMenu.length);
358+
} else if (globalMenu.length < globalQuota) {
359+
workspaceMenu = await workspaceStore.listMenu(totalLimit - globalMenu.length);
360+
}
345361
return { globalMenu, workspaceMenu };
346362
}
347363

0 commit comments

Comments
 (0)