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 { KnownToolDetails, RuntimeContextDetailsV1 } from '../contracts'; import { PiAgentBrowserTool } from './extensions/agent-browser'; import { reportChangedFiles } from './extensions/changed-file'; import { PiGameAssetTools } from './extensions/game-assets'; import { projectTaskState } from './extensions/task-state'; export type PiProductToolName = | 'agent_browser' | 'game_asset_browser' | 'game_asset_review' | 'task_state' | 'changed_file' | 'runtime_context'; export interface PiProductToolContext { conversationId: string; runId: string; resourceId: string; projectId: string; projectPath: string; skillIds: readonly string[]; } export interface PiProductToolResult { content: Array<{ type: 'text'; text: string }>; details: KnownToolDetails; } export interface PiProductToolsOptions { browser: AgentBrowserModule; attachments: CodingAttachmentStore; bundledSkillsDir: string; changeTracker?: ConversationChangeTracker; } export class PiProductTools { readonly changeTracker: ConversationChangeTracker; private readonly browser: PiAgentBrowserTool; private readonly gameAssets = new PiGameAssetTools(); constructor(private readonly options: PiProductToolsOptions) { this.changeTracker = options.changeTracker ?? new ConversationChangeTracker(); this.browser = new PiAgentBrowserTool(options.browser, options.attachments); } 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 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, context: PiProductToolContext, input: unknown, ): Promise { if (toolName === 'agent_browser') { return await this.browser.execute(context, input); } if (toolName === 'game_asset_browser') { return await this.gameAssets.browse(context.projectPath, input, context.resourceId); } if (toolName === 'game_asset_review') { return await this.gameAssets.review(context.projectPath, input, context.resourceId); } if (toolName === 'task_state') return projectTaskState(input); if (toolName === 'changed_file') { return await reportChangedFiles(this.changeTracker, context, input); } if (toolName !== 'runtime_context') throw new Error('Product tool is unavailable'); const skills = await listProductCodingSkills(this.options.bundledSkillsDir, context.skillIds); const details: RuntimeContextDetailsV1 = { schema: 'runtime-context.v1', skills, commands: buildProductCodingCommandCatalog(skills), }; return { content: [{ type: 'text', text: JSON.stringify(details) }], details, }; } }