import type { AgentBrowserModule } from '../../agent-browser'; import type { CodingAttachmentStore } from '../../coding-projects/attachment-store'; import { ConversationChangeTracker, type ConversationChangesSnapshot, } from '../../coding-projects/conversation-change-tracker'; import { buildProductCodingCommandCatalog, listProductCodingSkills, } from '../../coding-projects/skill-registry'; import type { ProductCodingCommand, ProductCodingSkill, ProductPiCommandInput, } from '../../../shared/coding-product-tools'; import { type ProductCodingPluginSkillSource, } from '../../coding-projects/skill-registry'; import { type CodingCapabilityRegistry, } from '../../coding-plugins/registry'; import type { EffectivePluginSnapshot } from '../../coding-plugins/effective-resolver'; import type { KnownToolDetails, RuntimeContextDetailsV1 } from '../contracts'; import { BUNDLED_CODING_SKILL_IDS } from '../../../shared/coding-skills'; import { PiAgentBrowserTool } from './extensions/agent-browser'; import { reportChangedFiles } from './extensions/changed-file'; import { projectTaskState } from './extensions/task-state'; export type PiProductToolName = | 'agent_browser' | 'task_state' | 'changed_file' | 'runtime_context'; const PI_PRODUCT_TOOL_NAMES = new Set([ 'agent_browser', 'task_state', 'changed_file', 'runtime_context', ]); export function isPiProductToolName(value: unknown): value is PiProductToolName { return typeof value === 'string' && PI_PRODUCT_TOOL_NAMES.has(value); } export interface PiProductToolContext { conversationId: string; runId: string; resourceId: string; projectId: string; projectPath: string; skillIds: readonly string[]; effectiveSnapshot?: EffectivePluginSnapshot; } export interface PiProductToolResult { content: Array<{ type: 'text'; text: string }>; details: KnownToolDetails; } export interface PiProductToolsOptions { browser: AgentBrowserModule; attachments: CodingAttachmentStore; bundledSkillsDir: string; changeTracker?: ConversationChangeTracker; pluginSkillSources?: readonly ProductCodingPluginSkillSource[]; getPluginSkillSources?(): readonly ProductCodingPluginSkillSource[] | Promise; capabilityRegistry?: CodingCapabilityRegistry; } export class PiProductTools { readonly changeTracker: ConversationChangeTracker; private readonly browser: PiAgentBrowserTool; private capabilityRegistry: CodingCapabilityRegistry | undefined; constructor(private readonly options: PiProductToolsOptions) { this.changeTracker = options.changeTracker ?? new ConversationChangeTracker(); this.browser = new PiAgentBrowserTool(options.browser, options.attachments); this.capabilityRegistry = options.capabilityRegistry; } configureCapabilityRegistry(registry: CodingCapabilityRegistry): void { this.capabilityRegistry = registry; } beginRun(input: { conversationId: string; runId: string; projectPath: string }) { return this.changeTracker.beginRun(input); } settleRun(conversationId: string, runId: string) { return this.changeTracker.settleRun(conversationId, runId); } getChanges(conversationId: string): ConversationChangesSnapshot | null { return this.changeTracker.getSnapshot(conversationId); } async listSkills( skillIds: readonly string[], availablePluginSkillIds: readonly string[] = [], ): Promise { const available = new Set(availablePluginSkillIds); const dynamicSources = this.options.getPluginSkillSources ? await this.options.getPluginSkillSources() : []; const uniqueSources = [...new Map( [...(this.options.pluginSkillSources ?? []), ...dynamicSources] .map((source) => [source.id, source] as const), ).values()]; const sources = uniqueSources.map((source) => ({ ...source, available: available.has(source.pluginId ?? source.id) || available.has(source.id), })); const selectedSkillIds = this.options.getPluginSkillSources ? skillIds.filter((id) => BUNDLED_CODING_SKILL_IDS.includes(id) || uniqueSources.some((source) => source.id === id)) : skillIds; return listProductCodingSkills(this.options.bundledSkillsDir, selectedSkillIds, sources); } async listCommands( skillIds: readonly string[], piCommands: readonly ProductPiCommandInput[] = [], availablePluginSkillIds: readonly string[] = [], ): Promise { return buildProductCodingCommandCatalog( await this.listSkills(skillIds, availablePluginSkillIds), piCommands, ); } async markBash(conversationId: string, runId: string): Promise { await this.changeTracker.markProjectRefresh(conversationId, runId); } recordTouchedPaths(conversationId: string, runId: string, paths: readonly string[]) { return this.changeTracker.recordTouchedPaths(conversationId, runId, paths); } async execute( toolName: PiProductToolName | string, context: PiProductToolContext, input: unknown, ): Promise { if (toolName === 'agent_browser') { return await this.browser.execute(context, input); } if (toolName === 'task_state') return projectTaskState(input); if (toolName === 'changed_file') { return await reportChangedFiles(this.changeTracker, context, input); } if (this.capabilityRegistry && toolName !== 'runtime_context') { return await this.capabilityRegistry.invoke({ toolName, context, workerRole: 'parent', effectiveSkillIds: context.skillIds, value: input, }); } if (toolName !== 'runtime_context') throw new Error('Product tool is unavailable'); const skills = await this.listSkills(context.skillIds, context.skillIds); const details: RuntimeContextDetailsV1 = { schema: 'runtime-context.v1', skills, commands: buildProductCodingCommandCatalog(skills), }; return { content: [{ type: 'text', text: JSON.stringify(details) }], details, }; } }