adapted property

ChatRequest get adapted

Implementation

ChatRequest get adapted {
  ChatRequest r = this;

  if (model.capabilities.ultraCompatibleMode) {
    r = r.ultraCompatible;
  }

  if (!model.capabilities.seesToolMessages &&
      r.messages.any((i) => i is ToolMessage)) {
    r = r.copyWith(
      messages:
          r.messages
              .map((i) => i is ToolMessage ? i.toolAsSystemMessage : i)
              .toList(),
    );
  }

  r = r.copyWith(
    messages: switch (model.capabilities.systemMode) {
      ChatModelSystemMode.supported =>
        systemPrompt != null
            ? [
              SystemMessage(content: Content.text(systemPrompt!)),
              ...messages,
            ]
            : messages,
      ChatModelSystemMode.merged =>
        messages.where((i) => i is! SystemMessage).toList(),
      ChatModelSystemMode.unsupported =>
        messages
            .map((i) => i is SystemMessage ? i.asUserSystemMessage : i)
            .toList(),
    },
    deleteSystemPrompt: switch (model.capabilities.systemMode) {
      ChatModelSystemMode.supported ||
      ChatModelSystemMode.unsupported => true,
      ChatModelSystemMode.merged => false,
    },
    deleteResponseFormat: !model.capabilities.structuredOutput,
    systemPrompt:
        model.capabilities.systemMode == ChatModelSystemMode.merged
            ? messages
                .whereType<SystemMessage>()
                .map((i) => i.content)
                .followedBy([
                  if (systemPrompt != null) Content.text(systemPrompt!),
                ])
                .join('\n')
            : systemPrompt,
    tools: model.capabilities.tools ? tools : [],
  );

  return r;
}