ktx/packages/context/src/wiki/tools/wiki-remove.tool.ts
Luca Martial 60457e9407
Improve schema setup and Notion ingest UX (#14)
* Improve schema setup and Notion ingest UX

* Handle Postgres network scan failures

* WIP: save local changes before main merge

* Refine setup prompt choices

* Tighten ingest reconciliation guidance

* Commit setup config updates

* Canonicalize unmapped fallback details

* Count reconciliation actions in reports

* Harden semantic layer source validation

* Return wiki content after edits

* Validate SL sources against manifests

* Validate wiki refs before writes

* Simplify CLI next steps

* Clarify agent setup summary

* Surface dbt target SL sources

* Recover SL write fallbacks

* Preserve failed context build metadata

* Track raw paths for ingest actions

* test(cli): update seeded demo expectations

* fix(ingest): scope fallback recovery checks

* fix(sl): tighten source validation guards

* fix(wiki): ignore empty embedding vectors

* Improve Notion ingest UX

* Enforce flat wiki keys

* test(context): update wiki key assertion

---------

Co-authored-by: Andrey Avtomonov <andreybavt@gmail.com>
2026-05-12 22:56:58 +02:00

107 lines
3.4 KiB
TypeScript

import { z } from 'zod';
import type { KnowledgeIndexPort } from '../ports.js';
import type { KnowledgeEventPort } from '../ports.js';
type BlockScope = 'GLOBAL' | 'USER';
import { KnowledgeWikiService } from '../index.js';
import { validateFlatWikiKey } from '../keys.js';
import { BaseTool, type ToolContext, type ToolOutput, validateActionRawPaths } from '../../tools/index.js';
const SYSTEM_AUTHOR = 'System User';
const SYSTEM_EMAIL = 'system@example.com';
const wikiRemoveInputSchema = z.object({
key: z.string().describe('The page key to remove'),
rawPaths: z
.array(z.string().min(1))
.optional()
.describe('In ingest sessions, raw source file paths that directly support this removal.'),
});
type WikiRemoveInput = z.infer<typeof wikiRemoveInputSchema>;
interface WikiRemoveStructured {
success: boolean;
key: string;
}
export class WikiRemoveTool extends BaseTool<typeof wikiRemoveInputSchema> {
readonly name = 'wiki_remove';
constructor(
private readonly wikiService: KnowledgeWikiService,
private readonly pagesRepository: KnowledgeIndexPort,
private readonly knowledgeRepository: KnowledgeEventPort,
) {
super();
}
get description(): string {
return `<purpose>Remove a knowledge page that is no longer relevant.</purpose>`;
}
get inputSchema() {
return wikiRemoveInputSchema;
}
async call(input: WikiRemoveInput, context: ToolContext): Promise<ToolOutput<WikiRemoveStructured>> {
const wikiService = context.session?.wikiService ?? this.wikiService;
const writesGlobal = !!context.session;
const skipIndex = context.session?.isWorktreeScoped === true;
const keyValidation = validateFlatWikiKey(input.key);
if (!keyValidation.ok) {
return {
markdown: keyValidation.error,
structured: { success: false, key: input.key },
};
}
const rawPathValidation = validateActionRawPaths(context.session, input.rawPaths);
if (!rawPathValidation.ok) {
return {
markdown: `Error: ${rawPathValidation.error}`,
structured: { success: false, key: input.key },
};
}
const scope: BlockScope = writesGlobal ? 'GLOBAL' : 'USER';
const scopeId = scope === 'USER' ? context.userId : null;
const existing = context.session
? await wikiService.readPage(scope, scopeId, input.key)
: await this.pagesRepository.findPageByKey(scope, scopeId, input.key);
if (!existing) {
return {
markdown: `Page "${input.key}" not found.`,
structured: { success: false, key: input.key },
};
}
await wikiService.deletePage(scope, scopeId, input.key, SYSTEM_AUTHOR, SYSTEM_EMAIL);
if (!skipIndex) {
await wikiService.deleteFromIndex(scope, scopeId, input.key);
}
await this.knowledgeRepository.createEvent({
blockId: null,
eventType: 'BLOCK_REMOVED',
actorId: context.userId,
chatId: null,
messageId: null,
payload: { removedKey: input.key, blockKey: input.key },
});
if (context.session) {
context.session.actions.push({
target: 'wiki',
type: 'removed',
key: input.key,
detail: `Removed page "${input.key}"`,
...(rawPathValidation.rawPaths ? { rawPaths: rawPathValidation.rawPaths } : {}),
});
}
return {
markdown: `Page "${input.key}" removed.`,
structured: { success: true, key: input.key },
};
}
}