suggestFollowUps function

Future<List<String>> suggestFollowUps(
  1. AiConversation conversation,
  2. LlmProvider provider, {
  3. int count = 3,
  4. AiRequestOptions? options,
})

Generates up to count short follow-up prompts a user might send next, given the current conversation, via a one-off call to provider.

This is the model call the presentational AiSuggestions strip needs to show contextual follow-ups (it renders whatever list you give it). Returns an empty list if the model produces nothing usable; it never throws for an empty or malformed reply.

final followUps = await suggestFollowUps(controller.conversation, provider);
// → feed into AiSuggestions(suggestions: followUps, onSelected: ...)

Pass options to pick a cheaper/faster model for this side call (e.g. a flash/mini model) independent of the main chat model.

Implementation

Future<List<String>> suggestFollowUps(
  AiConversation conversation,
  LlmProvider provider, {
  int count = 3,
  AiRequestOptions? options,
}) async {
  if (conversation.messages.isEmpty) return const [];

  final prompt = AiMessage.text(
    id: 'follow-ups-prompt',
    role: AiRole.user,
    text: 'Based on the conversation so far, suggest $count brief follow-up '
        'questions I might ask next. Keep each under 8 words. '
        'Respond with one question per line, no numbering, bullets, or quotes.',
  );
  final request = conversation.copyWith(
    messages: [...conversation.messages, prompt],
  );

  final buffer = StringBuffer();
  await for (final event in provider.send(request, options: options)) {
    if (event is TextDelta) buffer.write(event.delta);
  }

  return _parseLines(buffer.toString(), count);
}