Files
wehub-resource-sync 555e282cc4
pi-agent-plugin checks / lint (push) Has been cancelled
pi-agent-plugin checks / test (20) (push) Has been cancelled
pi-agent-plugin checks / test (22) (push) Has been cancelled
pi-agent-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / check_changes (push) Has been cancelled
TypeScript SDK CI / changelog_check (push) Has been cancelled
ci / changelog_check (push) Has been cancelled
ci / check_changes (push) Has been cancelled
ci / build_mem0 (3.10) (push) Has been cancelled
ci / build_mem0 (3.11) (push) Has been cancelled
ci / build_mem0 (3.12) (push) Has been cancelled
CLI Node CI / lint (push) Has been cancelled
CLI Node CI / test (20) (push) Has been cancelled
CLI Node CI / test (22) (push) Has been cancelled
CLI Node CI / build (push) Has been cancelled
CLI Python CI / lint (push) Has been cancelled
CLI Python CI / test (3.10) (push) Has been cancelled
CLI Python CI / test (3.11) (push) Has been cancelled
CLI Python CI / test (3.12) (push) Has been cancelled
CLI Python CI / build (push) Has been cancelled
openclaw checks / lint (push) Has been cancelled
openclaw checks / test (20) (push) Has been cancelled
openclaw checks / test (22) (push) Has been cancelled
openclaw checks / build (push) Has been cancelled
opencode-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (22) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (22) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:45 +08:00

72 lines
2.5 KiB
TypeScript

/**
* Memory scope resolution — ported from the pi-agent plugin's scoping model.
*
* Lets the agent choose, per memory operation, how wide to read/write:
* - "project" (default): this repo only -> { user_id, app_id }
* - "session": this run only -> { user_id, app_id, run_id }
* - "global": across ALL of the user's projects -> { user_id, app_id: "*" }
*
* Mirrors pi-agent/src/memory/scoping.ts (resolveSearchFilters / resolveAddParams)
* so the OpenCode plugin exposes scope the same way: as a per-call tool parameter,
* not a stateful "switch project" command.
*/
export type Scope = "project" | "session" | "global";
/** Filters for `search` / `get_memories` at the given scope. */
export function scopeSearchFilters(
scope: Scope,
userId: string,
appId: string,
runId: string,
): Record<string, string> {
switch (scope) {
case "session":
return { user_id: userId, app_id: appId, run_id: runId };
case "global":
return { user_id: userId, app_id: "*" };
case "project":
default:
return { user_id: userId, app_id: appId };
}
}
/** Identity params for `add` / `delete_all` at the given scope. */
export function scopeWriteParams(
scope: Scope,
userId: string,
appId: string,
runId: string,
): { user_id: string; app_id?: string; run_id?: string } {
switch (scope) {
case "session":
return { user_id: userId, app_id: appId, run_id: runId };
case "global":
return { user_id: userId };
case "project":
default:
return { user_id: userId, app_id: appId };
}
}
/** Normalize an arbitrary value to a valid Scope (defaults to "project"). */
export function asScope(value: unknown): Scope {
return value === "session" || value === "global" ? value : "project";
}
/**
* Resolve the persisted default scope from a parsed `~/.mem0/settings.json`
* object. This is the user-changeable default applied to memory operations when
* no explicit `scope` is passed (set via the `mem0-scope` skill). Falls back to
* "project" when unset or invalid.
*/
export function resolveDefaultScope(
settings: Record<string, unknown> | null | undefined,
): Scope {
return asScope(settings?.default_scope);
}
/** Guidance injected so the agent uses `global` only when explicitly asked. */
export const SCOPE_GUIDANCE =
'Memory tools accept an optional `scope`: omit it (or "project") for normal queries; use "session" to limit to the current run; use "global" ONLY when the user explicitly asks to search across all their projects in this workspace.';