Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

9.9 KB
Raw Blame
import { claimed, unmatched } from "../..";
import {
  asRecord,
  compact,
  nullableString,
  parseIfString,
  optionalString,
  recordKeyAsParsed,
  toJsonValue,
} from "../../../core/utils/json";
import {
  filePartFromMediaReference,
  mediaTypeFromDataUri,
  parseMediaReference,
} from "../../../core/normalize/message-parts/media";
import { toolCallPart } from "../../../core/normalize/message-parts/tool-calls";
import { toolResultOutput } from "../../../core/normalize/message-parts/tool-results";
import {
  toolDefinition,
  toolDefinitionProviderMetadata,
} from "../../../core/normalize/tool-definitions";
import type {
  FilePart,
  FinishReason,
  ToolResultPart,
  ToolResultOutput,
} from "../../../types";
import type {
  IOConvention,
  MessageSource,
  PartHandler,
  ToolDefinitionCarrier,
  ToolDefinitionSource,
} from "../../io-convention";

/**
 * Vercel AI SDK convention: this module owns AI SDK's typed part vocabulary
 * and the `ai.prompt.tools` OTel attribute. `aiSdkFilePart` lives in
 * the canonical media builders because the `image`/`file` handlers use the
 * same media-reference mechanics.
 */

// The canonical FinishReason vocabulary is adopted from the AI SDK, so this
// map is the identity.
const AI_SDK_FINISH_REASON_TYPE_BY_RAW: Record<string, FinishReason["type"]> = {
  stop: "stop",
  length: "length",
  "tool-calls": "tool-calls",
  "content-filter": "content-filter",
  error: "error",
  other: "other",
  unknown: "unknown",
};

/**
 * AI SDK file payloads (`data`, legacy `image`, nested reasoning `file`):
 * raw base64 bytes, a URL string, or tagged {type: "data" | "url"} shapes.
 */
function aiSdkFilePart(
  payload: unknown,
  options: {
    mediaType?: string;
    filename?: string;
    fallbackMediaType?: string;
  } = {},
): FilePart | null {
  const tagged = asRecord(payload);
  const candidate = optionalString(tagged?.data ?? tagged?.url ?? payload);
  if (!candidate) return null;

  const reference = parseMediaReference(candidate);
  if (reference) {
    return compact({
      ...filePartFromMediaReference(reference),
      filename: options.filename,
    });
  }

  const isUrl =
    tagged?.type === "url" ||
    (tagged !== undefined && tagged.url !== undefined && !tagged.data) ||
    (!tagged && /^(https?:|data:)/.test(candidate));
  return compact<FilePart>({
    type: "file",
    mediaType:
      options.mediaType ??
      mediaTypeFromDataUri(candidate) ??
      options.fallbackMediaType,
    filename: options.filename,
    content: isUrl
      ? { kind: "url", url: candidate }
      : { kind: "base64", data: candidate },
  });
}

const normalizeAiSdkImage: PartHandler = (value) => {
  // Legacy AI SDK image parts carry the payload directly under `image`/
  // `data`; source-wrapped `image` blocks on the same type name belong to
  // the Anthropic dialect and fall through.
  if (asRecord(value.source)) return unmatched;
  const part = aiSdkFilePart(value.image ?? value.data, {
    mediaType: optionalString(value.mediaType),
    fallbackMediaType: "image/*",
  });
  return part ? claimed(part) : unmatched;
};

const normalizeAiSdkFile: PartHandler = (value) => {
  // AI SDK file parts carry a flat `data`/`url` payload; `file`-wrapped
  // blocks on the same type name belong to the OpenAI chat dialect and fall
  // through.
  if (asRecord(value.file)) return unmatched;
  const part = aiSdkFilePart(value.data ?? value.url, {
    mediaType: optionalString(value.mediaType),
    filename: optionalString(value.filename),
  });
  return part ? claimed(part) : unmatched;
};

const normalizeAiSdkToolError: PartHandler = (value) => {
  const raw = parseIfString(value.error ?? null);
  return claimed(
    compact<ToolResultPart>({
      type: "tool-result",
      toolCallId: nullableString(value.toolCallId),
      toolName: optionalString(value.toolName),
      output:
        typeof raw === "string"
          ? { type: "text", value: raw }
          : { type: "json", value: toJsonValue(raw) },
      isError: true,
    }),
  );
};

const normalizeAiSdkReasoningFile: PartHandler = (value) => {
  // AI SDK reasoning-generated file (a FilePart nested under `file`; older
  // emissions used a flat `data`): it is still a file to every consumer.
  const nested = asRecord(value.file);
  const part = aiSdkFilePart(nested?.data ?? nested?.url ?? value.data, {
    mediaType: optionalString(nested?.mediaType ?? value.mediaType),
    filename: optionalString(nested?.filename ?? value.filename),
  });
  return part ? claimed({ ...part, reasoning: true }) : unmatched;
};

const normalizeAiSdkSource: PartHandler = (value) =>
  claimed({
    // AI SDK source parts are stream-positioned references without a text
    // anchor, so they remain custom parts with their original payload.
    type: "custom",
    kind: "source",
    value: toJsonValue(value),
  });

// AI SDK tool-call parts: { toolCallId, toolName, input } (older emissions
// used `args`).
const normalizeAiSdkToolCall: PartHandler = (value) =>
  claimed(
    toolCallPart({
      toolCallId: value.toolCallId,
      toolName: value.toolName,
      input: value.input ?? value.args,
      toolType: optionalString(value.type),
      providerExecuted: value.providerExecuted,
    }),
  );
// AI SDK tool-result parts: { toolCallId, toolName, output } (v4 emissions
// used `result`).
const normalizeAiSdkToolResult: PartHandler = (value, context) => {
  const raw = value.output ?? value.result;
  const record = asRecord(raw);
  const wrapper =
    record && ("value" in record || record.type === "execution-denied")
      ? record
      : undefined;
  let output: ToolResultOutput;
  let isError = typeof value.isError === "boolean" ? value.isError : undefined;
  switch (wrapper?.type) {
    case "text":
    case "error-text":
      output =
        typeof wrapper.value === "string"
          ? { type: "text", value: wrapper.value }
          : { type: "json", value: toJsonValue(wrapper.value) };
      isError = isError ?? (wrapper.type === "error-text" ? true : undefined);
      break;
    case "json":
    case "error-json":
      output = { type: "json", value: toJsonValue(wrapper.value) };
      isError = isError ?? (wrapper.type === "error-json" ? true : undefined);
      break;
    case "content":
      output = toolResultOutput(
        Array.isArray(wrapper.value)
          ? context.normalizePartList(wrapper.value)
          : [],
        wrapper.value,
      );
      break;
    case "execution-denied":
      output = { type: "json", value: toJsonValue(raw) };
      break;
    default: {
      const parsed = parseIfString(raw ?? null);
      output =
        typeof parsed === "string"
          ? { type: "text", value: parsed }
          : { type: "json", value: toJsonValue(parsed) };
    }
  }
  return claimed(
    compact<ToolResultPart>({
      type: "tool-result",
      toolCallId: nullableString(value.toolCallId),
      toolName: optionalString(value.toolName),
      output,
      isError,
    }),
  );
};

// Model-facing tool outputs use explicit URL/data variants rather than the
// legacy image/file payloads used in ordinary messages.
const normalizeAiSdkToolMedia: PartHandler = (value) => {
  const type = String(value.type);
  const payload = type.endsWith("-url")
    ? { type: "url", url: value.url }
    : { type: "data", data: value.data };
  const part = aiSdkFilePart(payload, {
    mediaType: optionalString(value.mediaType),
    filename: optionalString(value.filename),
    fallbackMediaType: type.startsWith("image-") ? "image/*" : undefined,
  });
  return part ? claimed(part) : unmatched;
};

const AI_SDK_PART_HANDLERS = {
  "image-data": normalizeAiSdkToolMedia,
  "image-url": normalizeAiSdkToolMedia,
  "file-data": normalizeAiSdkToolMedia,
  "file-url": normalizeAiSdkToolMedia,
  "tool-call": normalizeAiSdkToolCall,
  "tool-error": normalizeAiSdkToolError,
  "tool-result": normalizeAiSdkToolResult,
  "reasoning-file": normalizeAiSdkReasoningFile,
  source: normalizeAiSdkSource,
  image: normalizeAiSdkImage,
  file: normalizeAiSdkFile,
} satisfies Readonly<Record<string, PartHandler>>;

/**
 * The AI SDK's OTel instrumentation exports the request tools under the
 * `ai.prompt.tools` span attribute (provider built-ins may lack a name).
 */
function aiSdkToolDefinitionSources(
  carrier: ToolDefinitionCarrier,
): ToolDefinitionSource[] {
  const tools = carrier.metadataAttributes?.["ai.prompt.tools"];
  if (tools === undefined) return [];

  return [
    {
      sourceKey: "ai.prompt.tools",
      value: tools,
      options: { allowProviderToolWithoutName: true, allowToolMap: true },
    },
  ];
}

export const aiSdkProvider = {
  name: "ai-sdk",
  claimMessages: (root, kind): MessageSource[] => {
    if (kind !== "input" || root.messages !== undefined) return [];
    const prompt = root.prompt;
    if (typeof prompt === "string") {
      recordKeyAsParsed(root, "prompt");
      return [{ kind: "single", value: prompt, fallbackRole: "user" }];
    }
    if (
      Array.isArray(prompt) &&
      prompt.every((message) => typeof asRecord(message)?.role === "string")
    ) {
      recordKeyAsParsed(root, "prompt");
      return [{ kind: "sequence", values: prompt, fallbackRole: "user" }];
    }
    return [];
  },
  finishReasonTypeByRaw: AI_SDK_FINISH_REASON_TYPE_BY_RAW,
  // AI SDK / MCP tool declarations: { name?, description, inputSchema }.
  tryNormalizeToolDefinition: (value: Record<string, unknown>) => {
    if (value.inputSchema === undefined) return unmatched;
    const definition = toolDefinition({
      name: value.name,
      description: value.description,
      inputSchema: value.inputSchema,
      type: value.type,
      providerMetadata: toolDefinitionProviderMetadata(value, value),
    });
    return definition ? claimed(definition) : unmatched;
  },
  // UIMessages carry a `parts` array; camelCase `toolCalls` siblings appear
  // in AI-SDK-flavored (and koog) logging.
  messageLikeKeys: new Set(["parts", "toolCalls"]),
  typedParts: AI_SDK_PART_HANDLERS,
  collectToolDefinitionSources: aiSdkToolDefinitionSources,
} satisfies IOConvention;