import { Type } from "typebox";
import { ArtifactService } from "../artifacts/index.js";
import { wrapToolDefinition } from "./tool-definition-wrapper.js";
const artifactListSchema = Type.Object({
    tool: Type.Optional(Type.String({ description: "Optional tool filter, currently supports bash" })),
    limit: Type.Optional(Type.Number({ description: "Maximum artifacts to return (default 20)" })),
}, { additionalProperties: false });
const artifactReadSchema = Type.Object({
    id: Type.String({ description: "Artifact id, output path, metadata path, or artifact filename to read" }),
    startLine: Type.Optional(Type.Number({ description: "1-based line number to start reading from" })),
    maxLines: Type.Optional(Type.Number({ description: "Maximum number of lines to return (default 200)" })),
    tail: Type.Optional(Type.Boolean({ description: "Read from the end of the artifact instead of startLine" })),
}, { additionalProperties: false });
const artifactSearchSchema = Type.Object({
    id: Type.String({ description: "Artifact id, output path, metadata path, or artifact filename to search" }),
    query: Type.String({ description: "Text query to search for" }),
    maxMatches: Type.Optional(Type.Number({ description: "Maximum number of matches to return (default 100)" })),
    caseSensitive: Type.Optional(Type.Boolean({ description: "Use case-sensitive matching" })),
}, { additionalProperties: false });
export function createArtifactListToolDefinition(cwd) {
    return {
        name: "artifact_list",
        label: "artifact_list",
        description: "List recent project-local Threadwell artifacts stored under .threadwell/artifacts. Use this before artifact_read or artifact_search when the user asks for the latest or recent artifact.",
        promptSnippet: "List recent project-local output artifacts",
        parameters: artifactListSchema,
        async execute(_toolCallId, input) {
            const service = new ArtifactService(cwd);
            const limit = Math.max(1, Math.floor(input.limit ?? 20));
            const artifacts = (await service.listArtifacts())
                .filter((artifact) => input.tool === undefined || artifact.tool === input.tool)
                .slice(0, limit);
            const header = `Artifacts: ${artifacts.length}`;
            const body = artifacts
                .map((artifact) => [
                `id: ${artifact.id}`,
                `tool: ${artifact.tool}`,
                `createdAt: ${artifact.createdAt}`,
                `path: ${artifact.outputPath}`,
                `lines: ${artifact.lines}`,
                `bytes: ${artifact.bytes}`,
            ].join(" | "))
                .join("\n");
            return {
                content: [{ type: "text", text: body ? `${header}\n\n${body}` : header }],
                details: undefined,
            };
        },
    };
}
export function createArtifactReadToolDefinition(cwd) {
    return {
        name: "artifact_read",
        label: "artifact_read",
        description: "Read a bounded range or tail from a project-local Threadwell artifact stored under .threadwell/artifacts. Use this for large tool outputs that were artifacted instead of reading the whole file into context.",
        promptSnippet: "Read bounded slices from project-local output artifacts",
        parameters: artifactReadSchema,
        async execute(_toolCallId, input) {
            const service = new ArtifactService(cwd);
            const maxLines = input.maxLines;
            const result = input.tail
                ? await service.readArtifactTail(input.id, maxLines)
                : await service.readArtifact(input.id, { startLine: input.startLine, maxLines });
            const header = [
                `Artifact: ${result.artifact.id}`,
                `Path: ${result.artifact.outputPath}`,
                `Lines: ${result.startLine}-${result.endLine} of ${result.totalLines}`,
                result.truncated ? "Result truncated: yes" : "Result truncated: no",
            ].join("\n");
            return { content: [{ type: "text", text: `${header}\n\n${result.content}` }], details: undefined };
        },
    };
}
export function createArtifactSearchToolDefinition(cwd) {
    return {
        name: "artifact_search",
        label: "artifact_search",
        description: "Search a project-local Threadwell artifact stored under .threadwell/artifacts without loading the full artifact into context.",
        promptSnippet: "Search project-local output artifacts by text query",
        parameters: artifactSearchSchema,
        async execute(_toolCallId, input) {
            const service = new ArtifactService(cwd);
            const result = await service.searchArtifact(input.id, input.query, {
                maxMatches: input.maxMatches,
                caseSensitive: input.caseSensitive,
            });
            const header = [
                `Artifact: ${result.artifact.id}`,
                `Path: ${result.artifact.outputPath}`,
                `Query: ${result.query}`,
                `Matches: ${result.matches.length}${result.truncated ? "+" : ""}`,
            ].join("\n");
            const body = result.matches.map((match) => `${match.line}: ${match.text}`).join("\n");
            return {
                content: [{ type: "text", text: body ? `${header}\n\n${body}` : header }],
                details: undefined,
            };
        },
    };
}
export function createArtifactListTool(cwd) {
    return wrapToolDefinition(createArtifactListToolDefinition(cwd));
}
export function createArtifactReadTool(cwd) {
    return wrapToolDefinition(createArtifactReadToolDefinition(cwd));
}
export function createArtifactSearchTool(cwd) {
    return wrapToolDefinition(createArtifactSearchToolDefinition(cwd));
}
//# sourceMappingURL=artifact.js.map