logs method

Implementation

Stream<RoomLogEvent> logs() async* {
  final inputClosed = Completer<void>();

  Stream<Content> inputStream() async* {
    await inputClosed.future;
  }

  final output = await room.invoke(toolkit: "developer", tool: "logs", input: ToolStreamInput(inputStream()));
  if (output is! ToolStreamOutput) {
    if (!inputClosed.isCompleted) {
      inputClosed.complete();
    }
    throw _unexpectedResponseError("logs");
  }

  try {
    await for (final chunk in output.stream) {
      if (chunk is ErrorContent) {
        throw RoomServerException(chunk.text, code: chunk.code);
      }
      if (chunk is ControlContent) {
        if (chunk.method == "close") {
          return;
        }
        throw _unexpectedResponseError("logs");
      }
      if (chunk is! BinaryContent) {
        throw _unexpectedResponseError("logs");
      }

      final logType = chunk.headers["type"];
      if (logType is! String || logType.isEmpty) {
        throw RoomServerException("developer.logs returned a chunk without a valid type");
      }

      final dynamic decoded = chunk.data.isEmpty ? <String, dynamic>{} : jsonDecode(utf8.decode(chunk.data));
      if (decoded is! Map) {
        throw RoomServerException("developer.logs returned invalid JSON data");
      }

      final event = RoomLogEvent(type: logType, data: Map<String, dynamic>.from(decoded));
      room._eventsController.add(event);
      yield event;
    }
  } finally {
    if (!inputClosed.isCompleted) {
      inputClosed.complete();
    }
  }
}