{"version":3,"file":"readTool.mjs","names":[],"sources":["../../../../../../../../src/extensions/workspaces/sessions/(backend)/tool/_lib/readTool.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\n\nimport { isWritableFile, displayPath, resolveReadInputPath } from '@agimon-ai/doompi-hashline/files';\nimport {\n  createReadToolDefinition,\n  DEFAULT_MAX_BYTES,\n  formatDimensionNote,\n  formatSize,\n  resizeImage,\n  type AgentToolResult,\n  type ToolDefinition,\n} from '@earendil-works/pi-coding-agent';\n\nimport { ReadParamsSchema, type ReadParams } from '../../../../../../schemas/readTool';\nimport { applyImageLimits, imageLimits, type ReadImageResizeOperations } from '../../../../../../services/readImage';\nimport { assertNotAborted, createTaggedReadResult, isImageRead } from '../../../../../../services/readTool';\n\ntype WritableCheck = (path: string) => Promise<boolean>;\n\nconst resizeOperations: ReadImageResizeOperations = {\n  resize: resizeImage,\n  formatDimensionNote: (result) => formatDimensionNote(result),\n};\n\nexport function createHashlineReadTool(\n  writable: WritableCheck = isWritableFile,\n): ToolDefinition<typeof ReadParamsSchema> {\n  return {\n    name: 'read',\n    label: 'read',\n    description: `Read a writable text file with an exact-byte file tag and stable line anchors. Non-writable files and images retain Pi's native behavior. Text is truncated to ${formatSize(DEFAULT_MAX_BYTES)}.`,\n    promptSnippet: 'Read file contents with snapshot-bound line anchors',\n    promptGuidelines: [\n      'Use read before edit. When hashline metadata is present, preserve the @file hash and anchors such as 5#abc exactly.',\n      'Continue large reads with offset until the required anchored lines are visible.',\n    ],\n    parameters: ReadParamsSchema,\n    executionMode: 'parallel',\n    async execute(toolCallId, params, signal, onUpdate, ctx) {\n      const input = params as ReadParams;\n      assertNotAborted(signal);\n      const absolutePath = await resolveReadInputPath(input.path, ctx.cwd);\n      const nativeRead = createReadToolDefinition(ctx.cwd, { autoResizeImages: false });\n      const nativeResult = await nativeRead.execute(\n        toolCallId,\n        { ...input, path: absolutePath },\n        signal,\n        onUpdate,\n        ctx,\n      );\n      if (isImageRead(nativeResult.content)) {\n        assertNotAborted(signal);\n        return {\n          ...nativeResult,\n          content: await applyImageLimits(nativeResult.content, imageLimits(), resizeOperations),\n        };\n      }\n\n      assertNotAborted(signal);\n      const canEdit = await writable(absolutePath);\n      assertNotAborted(signal);\n      if (!canEdit) return nativeResult;\n      const bytes = await readFile(absolutePath);\n      assertNotAborted(signal);\n      return createTaggedReadResult(\n        bytes,\n        displayPath(absolutePath, ctx.cwd),\n        input,\n      ) as unknown as AgentToolResult<unknown>;\n    },\n  };\n}\n"],"mappings":";;;;;;;AAmBA,MAAM,mBAA8C;CAClD,QAAQ;CACR,sBAAsB,WAAW,oBAAoB,MAAM;AAC7D;AAEA,SAAgB,uBACd,WAA0B,gBACe;CACzC,OAAO;EACL,MAAM;EACN,OAAO;EACP,aAAa,kKAAkK,WAAW,iBAAiB,EAAE;EAC7M,eAAe;EACf,kBAAkB,CAChB,uHACA,iFACF;EACA,YAAY;EACZ,eAAe;EACf,MAAM,QAAQ,YAAY,QAAQ,QAAQ,UAAU,KAAK;GACvD,MAAM,QAAQ;GACd,iBAAiB,MAAM;GACvB,MAAM,eAAe,MAAM,qBAAqB,MAAM,MAAM,IAAI,GAAG;GAEnE,MAAM,eAAe,MADF,yBAAyB,IAAI,KAAK,EAAE,kBAAkB,MAAM,CAC3C,CAAC,CAAC,QACpC,YACA;IAAE,GAAG;IAAO,MAAM;GAAa,GAC/B,QACA,UACA,GACF;GACA,IAAI,YAAY,aAAa,OAAO,GAAG;IACrC,iBAAiB,MAAM;IACvB,OAAO;KACL,GAAG;KACH,SAAS,MAAM,iBAAiB,aAAa,SAAS,YAAY,GAAG,gBAAgB;IACvF;GACF;GAEA,iBAAiB,MAAM;GACvB,MAAM,UAAU,MAAM,SAAS,YAAY;GAC3C,iBAAiB,MAAM;GACvB,IAAI,CAAC,SAAS,OAAO;GACrB,MAAM,QAAQ,MAAM,SAAS,YAAY;GACzC,iBAAiB,MAAM;GACvB,OAAO,uBACL,OACA,YAAY,cAAc,IAAI,GAAG,GACjC,KACF;EACF;CACF;AACF"}