/**
 * Tool-agnostic primitives for host tools: request/result envelope, approval
 * shape, per-tool ToolContract, and the host executor contract.
 */
/** Coerce any thrown value into a stable string message. */
export function errorToMessage(error) {
    return error instanceof Error ? error.message : String(error);
}
export const HostToolResultStatus = {
    Cancelled: 'cancelled',
    Failed: 'failed',
    Succeeded: 'succeeded'
};
/**
 * Build a `HostToolResult` literal. `details` carries the tool's typed
 * payload; `options.userMessage` populates the top-level canonical UI string.
 */
export function createHostToolResult(contract, requestId, status, details, options) {
    return {
        ...(details !== undefined ? { details } : {}),
        kind: contract.kind,
        requestId,
        status,
        ...(options?.userMessage !== undefined ? { userMessage: options.userMessage } : {})
    };
}
/**
 * Build a Failed `HostToolResult` whose user-facing reason rides on the
 * top-level `userMessage` field. For infra-level failures (bridge errors,
 * timeouts) where the tool's typed payload doesn't apply — no signal context,
 * always Failed, no `details`.
 */
export function createFailedHostToolResult(kind, requestId, userMessage) {
    return {
        kind,
        requestId,
        status: HostToolResultStatus.Failed,
        userMessage
    };
}
/**
 * Bridge `HostToolResult.userMessage` (top-level, canonical) into the
 * renderer-shaped artifact's `details.user_message` slot.
 *
 * Renderers read `additional_kwargs.user_message` to display the canonical
 * UI string — that slot is the LangChain `ToolMessage` artifact convention
 * shared with the LangGraph backend. SXL tool contracts must therefore
 * publish their canonical message into `details.user_message` when handing
 * results to the agent loop, even though SXL's own observability/ledger
 * code reads the top-level `userMessage` instead.
 *
 * When `userMessage` is undefined, `details` is returned unchanged (or
 * `{}` if it was undefined). This preserves per-tool fields that
 * legitimately live in `details.user_message` on the success path — e.g.
 * web_search's citation-marked body. Callers that want to overwrite such
 * a field on failure should pass `userMessage` only when `isError` is
 * true.
 */
export function buildRendererArtifact(details, userMessage) {
    if (userMessage === undefined)
        return details ?? {};
    return { ...(details ?? {}), user_message: userMessage };
}
//# sourceMappingURL=host-tool.js.map