ResponseItem.fromJson constructor

ResponseItem.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory ResponseItem.fromJson(Map<String, dynamic> json) {
  switch (json['type']) {
    // ───────── Tool outputs / calls ─────────
    case 'computer_call_output':
      return ComputerCallOutput(
        callId: json['call_id'],
        output: ComputerScreenshotOutput.fromJson(json['output'] as Map<String, dynamic>),
        acknowledgedSafetyChecks:
            (json['acknowledged_safety_checks'] as List?)?.cast<Map<String, dynamic>>().map(ComputerSafetyCheck.fromJson).toList(),
        id: json['id'],
        status: json['status'] == null ? null : ComputerResultStatus.fromJson(json['status']),
      );
    case 'file_search_call':
      return FileSearchCall(
        id: json['id'],
        queries: List<String>.from(json['queries']),
        status: FileSearchToolCallStatus.fromJson(json['status']),
        results: (json['results'] as List?)?.cast<Map<String, dynamic>>().map(FileSearchToolCallResult.fromJson).toList(),
      );
    case 'web_search_call':
      return WebSearchCall(
        id: json['id'],
        status: WebSearchToolCallStatus.fromJson(json['status']),
      );
    case 'local_shell_call':
      return LocalShellCall(
        id: json['id'],
        callId: json['call_id'],
        action: LocalShellAction.fromJson(json["action"]),
        status: LocalShellCallStatus.fromJson(json['status']),
      );
    case 'local_shell_call_output':
      return LocalShellCallOutput(
        callId: json['id'], // REST names the field "id"
        output: json['output'],
        status: json['status'] == null ? null : LocalShellCallStatus.fromJson(json['status']),
      );
    case 'mcp_call':
      return McpCall(
        id: json['id'],
        name: json['name'],
        arguments: json['arguments'],
        serverLabel: json['server_label'],
        error: json['error'] == null ? null : McpError.fromJson(json['error']),
        output: json['output'],
      );
    case 'mcp_list_tools':
      return McpListTools(
        id: json['id'],
        serverLabel: json['server_label'],
        tools: (json['tools'] as List).cast<Map<String, dynamic>>().map(MCPListToolItem.fromJson).toList(),
        error: json['error'],
      );
    case 'mcp_approval_request':
      return McpApprovalRequest(
        id: json['id'],
        arguments: json['arguments'],
        name: json['name'],
        serverLabel: json['server_label'],
      );
    case 'mcp_approval_response':
      return McpApprovalResponse(
        approvalRequestId: json['approval_request_id'],
        approve: json['approve'],
        id: json['id'],
        reason: json['reason'],
      );
    case 'function_call_output':
      return FunctionCallOutput(
        callId: json['call_id'],
        output: json['output'],
        status: json['status'] == null ? null : FunctionToolCallStatus.fromJson(json['status']),
        id: json['id'],
      );
    case 'function_call':
      return FunctionCall(
        arguments: json['arguments'],
        callId: json['call_id'],
        name: json['name'],
        id: json['id'],
        status: json['status'] == null ? null : FunctionToolCallStatus.fromJson(json['status']),
      );
    case 'image_generation_call':
      return ImageGenerationCall(
        id: json['id'],
        status: ImageGenerationCallStatus.fromJson(json['status']),
        resultBase64: json['result'],
      );

    case 'code_interpreter_call':
      return CodeInterpreterCall(
        id: json['id'],
        code: json['code'],
        results: json['results'] == null
            ? null
            : (json['results'] as List).cast<Map<String, dynamic>>().map(CodeInterpreterResult.fromJson).toList(),
        status: CodeInterpreterToolCallStatus.fromJson(json['status']),
        containerId: json['container_id'],
      );
    case 'reasoning':
      return Reasoning(
        id: json['id'],
        summary: (json['summary'] as List).map((a) => ReasoningSummary.fromJson(a)).toList(),
        encryptedContent: json['encrypted_content'],
        status: json['status'] == null ? null : ReasoningOutputStatus.fromJson(json['status']),
      );
    case 'item_reference':
      return ItemReference(id: json['id']);

    case 'computer_call':
      return ComputerCall(
        id: json['id'],
        callId: json['call_id'],
        action: ComputerAction.fromJson(json['action'] as Map<String, dynamic>),
        pendingSafetyChecks:
            (json['pending_safety_checks'] as List).cast<Map<String, dynamic>>().map(ComputerSafetyCheck.fromJson).toList(),
        status: json['status'] == null ? null : ComputerResultStatus.fromJson(json['status']),
      );
    // ───────── Messages (three shapes) ─────────
    case 'message':
      final content = json['content'];
      if (content is String) {
        return InputText(role: json['role'], text: content);
      }
      if (json.containsKey('id') && json.containsKey('status')) {
        return OutputMessage(
          role: json['role'],
          content: (content as List).cast<Map<String, dynamic>>().map(ResponseContent.fromJson).toList(),
          id: json['id'],
          status: json['status'],
        );
      }
      return InputMessage(
        role: json['role'],
        content: (content as List).cast<Map<String, dynamic>>().map(ResponseContent.fromJson).toList(),
      );
    default:
      return OtherResponseItem(json);
  }
}