/**
 * Per-tool contract for `create_website` — host-mediated server call.
 * Field names are snake_case because the renderer consumes this artifact
 * shape directly.
 */
import { Type } from '@sinclair/typebox';
import { HostToolResultStatus, buildRendererArtifact } from '../../../contracts/host-tool.js';
import { withAbort } from '../../../utils/abort.js';
import { CREATE_WEBSITE } from '../tool-names.js';
export const CREATE_WEBSITE_KIND = 'createWebsite';
/** Listing one-liner used by `get_tool_info`. */
export const CREATE_WEBSITE_TOOL_SUMMARY = 'Create or update a shareable website (dashboards, visualizations, reports, custom pages)';
export const CREATE_WEBSITE_TOOL_DESCRIPTION = `Create or update a shareable website from HTML content.

**WHEN TO USE:** When the user asks for a shareable dashboard, website, custom web page,
interactive visualization, or any standalone web content at its own URL.
Do NOT use for inline charts/visualizations — use execute_code for those.

**HOW TO USE:** Pass title and html_content directly. The tool handles everything else
(manifest generation, file storage, share link creation).

\`\`\`python
create_website(
    title="My Dashboard",
    html_content="<!DOCTYPE html><html>...</html>",
    description="Optional description",
)
\`\`\`

**HTML guidelines:**
- Must be a self-contained HTML document under 2MB
- Use any web technologies: HTML/CSS/JS, D3, Chart.js, Plotly, Tailwind via CDN, etc.
- All assets should be inlined or loaded from CDNs — no relative paths

**Skills to read before building:**
- For ALL websites: \`/skills/default/website-design/SKILL.md\`
- For data analysis: \`/skills/default/scientific-data-analysis/SKILL.md\`

**External data:** To pull data from external databases/APIs for a dashboard, use the \`safe\` CLI
to make authenticated requests (see \`/skills/default/external-service-integration/SKILL.md\`).

**Updating:** Pass the same website_id to update an existing website at the same /d/{id} URL.

**Sharing:** After this tool succeeds, use the returned \`link\` exactly when sharing the dashboard.
Do not construct dashboard URLs manually. Never use \`https://app.shortcut.ai\`,
\`/website/{id}\`, or \`/api/v1/websites/{id}\` as a user-facing dashboard link.`;
export const CREATE_WEBSITE_TITLE_DESCRIPTION = 'Website title.';
export const CREATE_WEBSITE_HTML_CONTENT_DESCRIPTION = 'The complete HTML content for the website (must be under 2MB).';
export const CREATE_WEBSITE_DESCRIPTION_DESCRIPTION = 'Optional website description.';
export const CREATE_WEBSITE_WEBSITE_ID_DESCRIPTION = 'Optional existing website UUID. Omit to create a new website.';
const createWebsiteLlmSchema = Type.Object({
    title: Type.String({ description: CREATE_WEBSITE_TITLE_DESCRIPTION, minLength: 1 }),
    html_content: Type.String({
        description: CREATE_WEBSITE_HTML_CONTENT_DESCRIPTION,
        minLength: 1
    }),
    description: Type.Optional(Type.String({ description: CREATE_WEBSITE_DESCRIPTION_DESCRIPTION })),
    website_id: Type.Optional(Type.String({ description: CREATE_WEBSITE_WEBSITE_ID_DESCRIPTION }))
});
function validateParams(value) {
    if (!value || typeof value !== 'object')
        return false;
    const candidate = value;
    return (typeof candidate.title === 'string' &&
        typeof candidate.html_content === 'string' &&
        (candidate.description === undefined || typeof candidate.description === 'string') &&
        (candidate.website_id === undefined || typeof candidate.website_id === 'string'));
}
function validateResultDetails(value) {
    if (value === undefined)
        return true;
    if (!value || typeof value !== 'object')
        return false;
    const candidate = value;
    const visibilityValid = candidate.visibility === undefined ||
        candidate.visibility === 'private' ||
        candidate.visibility === 'public' ||
        candidate.visibility === 'team';
    return ((candidate.website_id === undefined || typeof candidate.website_id === 'string') &&
        (candidate.link === undefined || typeof candidate.link === 'string') &&
        visibilityValid &&
        (candidate.title === undefined || typeof candidate.title === 'string') &&
        (candidate.description === undefined ||
            candidate.description === null ||
            typeof candidate.description === 'string') &&
        (candidate.share_link_id === undefined || typeof candidate.share_link_id === 'string') &&
        (candidate.workspace_path === undefined || typeof candidate.workspace_path === 'string') &&
        (candidate.file_id === undefined || typeof candidate.file_id === 'string') &&
        (candidate.updated_at === undefined || typeof candidate.updated_at === 'string'));
}
function normalizeResultDetails(value) {
    return value === undefined ? undefined : { ...value };
}
function getResultObservabilityFields(details) {
    if (!validateResultDetails(details) || details === undefined)
        return undefined;
    const metadata = {
        ...(details.visibility !== undefined ? { visibility: details.visibility } : {})
    };
    return Object.keys(metadata).length > 0 ? metadata : undefined;
}
function evaluateApproval() {
    return { required: false };
}
function formatResult(details, isError, userMessage) {
    // AgentController-visible content: JSON blob on success so downstream calls can
    // reference link + visibility; plain message on error.
    let text;
    if (isError) {
        text = userMessage?.trim() || 'Website creation failed.';
    }
    else if (details?.website_id && details.link) {
        text = JSON.stringify({
            websiteId: details.website_id,
            link: details.link,
            visibility: details.visibility ?? 'private',
            workspacePath: details.workspace_path,
            updatedAt: details.updated_at
        });
    }
    else {
        text = 'Website created.';
    }
    return {
        content: [{ type: 'text', text }],
        details: buildRendererArtifact(details, userMessage),
        isError
    };
}
function createLlmTool(options) {
    return {
        name: CREATE_WEBSITE,
        label: 'Create Website',
        isReadOnly: false,
        description: CREATE_WEBSITE_TOOL_DESCRIPTION,
        parameters: createWebsiteLlmSchema,
        async execute(toolCallId, llmArgs, signal) {
            const args = llmArgs;
            const params = {
                title: args.title,
                html_content: args.html_content,
                ...(args.description !== undefined ? { description: args.description } : {}),
                ...(args.website_id !== undefined ? { website_id: args.website_id } : {})
            };
            void options.sessionPolicy;
            const request = {
                approval: evaluateApproval(),
                heartbeat: {
                    label: 'Creating website...',
                    toolName: CREATE_WEBSITE
                },
                kind: CREATE_WEBSITE_KIND,
                params,
                requestId: toolCallId
            };
            const result = await withAbort(signal, async () => options.pendingHostToolRequests.request(request));
            const details = result.details;
            const isError = result.status === HostToolResultStatus.Failed ||
                result.status === HostToolResultStatus.Cancelled;
            return formatResult(details, isError, result.userMessage);
        }
    };
}
export const createWebsiteContract = {
    kind: CREATE_WEBSITE_KIND,
    createLlmTool,
    evaluateApproval,
    getResultObservabilityFields,
    validateParams,
    validateResultDetails,
    normalizeResultDetails
};
//# sourceMappingURL=contract.js.map