/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 * @generated-id: fdff4beacb2f
 */

import * as z from "zod/v3";
import {
  collectExtraKeys as collectExtraKeys$,
  safeParse,
} from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import { AudioContent, AudioContent$inboundSchema } from "./audiocontent.js";
import {
  EmbeddedResource,
  EmbeddedResource$inboundSchema,
} from "./embeddedresource.js";
import { ImageContent, ImageContent$inboundSchema } from "./imagecontent.js";
import {
  MCPToolCallMetadata,
  MCPToolCallMetadata$inboundSchema,
} from "./mcptoolcallmetadata.js";
import { ResourceLink, ResourceLink$inboundSchema } from "./resourcelink.js";
import { TextContent, TextContent$inboundSchema } from "./textcontent.js";

export type Content =
  | TextContent
  | ImageContent
  | AudioContent
  | ResourceLink
  | EmbeddedResource;

/**
 * Response from calling an MCP tool.
 *
 * @remarks
 *
 * We override mcp_types.CallToolResult because:
 * - Models only support `content`, not `structuredContent` at top level
 * - Downstream consumers (le-chat, etc.) need structuredContent/isError/_meta via metadata
 *
 * SYNC: Keep in sync with Harmattan (orchestrator) for harmonized tool result processing.
 */
export type MCPToolCallResponse = {
  content: Array<
    TextContent | ImageContent | AudioContent | ResourceLink | EmbeddedResource
  >;
  metadata?: MCPToolCallMetadata | null | undefined;
  additionalProperties?: { [k: string]: any } | undefined;
};

/** @internal */
export const Content$inboundSchema: z.ZodType<Content, z.ZodTypeDef, unknown> =
  z.union([
    TextContent$inboundSchema,
    ImageContent$inboundSchema,
    AudioContent$inboundSchema,
    ResourceLink$inboundSchema,
    EmbeddedResource$inboundSchema,
  ]);

export function contentFromJSON(
  jsonString: string,
): SafeParseResult<Content, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Content$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Content' from JSON`,
  );
}

/** @internal */
export const MCPToolCallResponse$inboundSchema: z.ZodType<
  MCPToolCallResponse,
  z.ZodTypeDef,
  unknown
> = collectExtraKeys$(
  z.object({
    content: z.array(
      z.union([
        TextContent$inboundSchema,
        ImageContent$inboundSchema,
        AudioContent$inboundSchema,
        ResourceLink$inboundSchema,
        EmbeddedResource$inboundSchema,
      ]),
    ),
    metadata: z.nullable(MCPToolCallMetadata$inboundSchema).optional(),
  }).catchall(z.any()),
  "additionalProperties",
  true,
);

export function mcpToolCallResponseFromJSON(
  jsonString: string,
): SafeParseResult<MCPToolCallResponse, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => MCPToolCallResponse$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'MCPToolCallResponse' from JSON`,
  );
}
