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

Function createCtxSearchTool

packages/pi-plugin/src/tools/ctx-search.ts:158–248  ·  view source on GitHub ↗
(
	deps: CtxSearchToolDeps,
)

Source from the content-addressed store, hash-verified

156}
157
158export function createCtxSearchTool(
159 deps: CtxSearchToolDeps,
160): ToolDefinition<typeof ParamsSchema> {
161 return {
162 name: "ctx_search",
163 label: "Magic Context: Search",
164 description: CTX_SEARCH_DESCRIPTION,
165 parameters: ParamsSchema,
166 async execute(
167 _toolCallId,
168 params: CtxSearchParams,
169 _signal,
170 _onUpdate,
171 ctx,
172 ) {
173 const query = params.query?.trim();
174 if (!query) {
175 return {
176 content: [{ type: "text", text: "Error: 'query' is required." }],
177 details: undefined,
178 isError: true,
179 };
180 }
181
182 const sessionId = ctx.sessionManager.getSessionId();
183 const projectIdentity = resolveProjectIdentity(ctx.cwd);
184 await deps.ensureProjectRegistered?.(ctx.cwd, deps.db);
185 const snapshot = getProjectEmbeddingSnapshot(projectIdentity);
186 const memoryEnabled =
187 snapshot?.features.memoryEnabled ?? deps.memoryEnabled;
188 const embeddingEnabled = snapshot
189 ? snapshot.enabled || snapshot.gitCommitEnabled
190 : deps.embeddingEnabled;
191 const gitCommitsEnabled =
192 snapshot?.gitCommitEnabled ?? deps.gitCommitsEnabled ?? false;
193
194 // Only search message history up to the last compartment boundary —
195 // anything after that (the live tail, including the current turn) is
196 // still in context and already visible to the agent. When NO compartment
197 // exists yet, the historian hasn't scrolled anything out of context, so
198 // the boundary is 0: every indexed message (ordinals are 1-based) is in
199 // the live tail and must be excluded. A negative sentinel here would mean
200 // "search everything" and leak the current prompt back to the agent — the
201 // exact opposite of the intent (issue #131).
202 const lastCompartmentEnd = getLastCompartmentEndMessage(
203 deps.db,
204 sessionId,
205 );
206 const messageOrdinalCutoff =
207 lastCompartmentEnd >= 0 ? lastCompartmentEnd : 0;
208
209 // Hard-filter memories already rendered in <session-history>.
210 const visibleMemoryIds = getVisibleMemoryIds(deps.db, sessionId);
211
212 const results = await unifiedSearch(
213 deps.db,
214 sessionId,
215 projectIdentity,

Callers 2

ctx-search.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected