MCPcopy Create free account
hub / github.com/cortexkit/magic-context / createCtxMemoryTool

Function createCtxMemoryTool

packages/pi-plugin/src/tools/ctx-memory.ts:270–758  ·  view source on GitHub ↗
(
	deps: CtxMemoryToolDeps,
)

Source from the content-addressed store, hash-verified

268}
269
270export function createCtxMemoryTool(
271 deps: CtxMemoryToolDeps,
272): ToolDefinition<typeof ParamsSchema> {
273 const dreamerAllowed = deps.allowDreamerActions === true;
274 const description = dreamerAllowed
275 ? `${CTX_MEMORY_DESCRIPTION}\n- list: enumerate stored memories (maintenance sessions).`
276 : CTX_MEMORY_DESCRIPTION;
277
278 return {
279 name: "ctx_memory",
280 label: "Magic Context: Memory",
281 description,
282 parameters: ParamsSchema,
283 async execute(
284 _toolCallId,
285 params: CtxMemoryParams,
286 _signal,
287 _onUpdate,
288 ctx,
289 ) {
290 // Gate dreamer-only actions on the allowlist flag. Mirrors
291 // OpenCode's `if (toolContext.agent !== DREAMER_AGENT && !allowedActions.includes(args.action))`.
292 if (!dreamerAllowed && DREAMER_ONLY_ACTIONS.has(params.action)) {
293 return err(
294 `Error: Action '${params.action}' is not allowed in this context.`,
295 );
296 }
297
298 const projectIdentity = resolveProjectIdentity(ctx.cwd);
299 await deps.ensureProjectRegistered?.(ctx.cwd, deps.db);
300 const workspaceIdentitySet = resolveWorkspaceIdentitySet(
301 deps.db,
302 projectIdentity,
303 );
304 const expandedWorkspace = expandWorkspaceIdentitySetWithAliases(
305 deps.db,
306 workspaceIdentitySet.identities,
307 );
308 const workspaceVisibleIdentities =
309 workspaceIdentitySet.identities.length > 1
310 ? expandedWorkspace.expandedIdentities
311 : workspaceIdentitySet.identities;
312 const targetIdentityForStoredPath = (rawProjectPath: string) =>
313 workspaceIdentitySet.identities.length > 1
314 ? (resolveStoredPathWorkspaceIdentity(
315 rawProjectPath,
316 workspaceIdentitySet.identities,
317 expandedWorkspace.canonicalIdentityByStoredPath,
318 ) ?? normalizeStoredProjectPath(rawProjectPath))
319 : normalizeStoredProjectPath(rawProjectPath);
320 // The workspace's share-category policy, identical to the render path
321 // (resolveWorkspaceRenderContextPi): null = share all categories.
322 const toolShareCategories =
323 workspaceIdentitySet.identities.length > 1
324 ? resolveWorkspaceShareCategories(deps.db, projectIdentity)
325 : null;
326 // Tool visibility MUST match render visibility, or the agent could
327 // mutate (update/archive) a foreign workspace memory it can't even

Callers 2

ctx-memory.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected