generateFieldValue method

  1. @override
Future<String> generateFieldValue({
  1. required FieldType fieldType,
  2. String? context,
  3. Duration timeout = const Duration(seconds: 15),
})
override

Generate a single field value given a field "type" (e.g., name, email, bio, product_description).

Implementation

@override
Future<String> generateFieldValue({
  required FieldType fieldType,
  String? context,
  Duration timeout = const Duration(seconds: 15),
}) async {
  final prompt = _buildTextPrompt(fieldType: fieldType, context: context);

  final resp = await http
      .post(
    Uri.parse(_baseUrl),
    headers: {
      HttpHeaders.contentTypeHeader: 'application/json',
      HttpHeaders.authorizationHeader: 'Bearer $_apiKey',
    },
    body: jsonEncode({
      'model': textModel,
      'messages': [
        {
          'role': 'system',
          'content':
          'You generate short, realistic, single-field values for mobile form autofill.'
        },
        {
          'role': 'user',
          'content': prompt,
        }
      ],
      'temperature': 0.7,
      'max_tokens': 32,
    }),
  )
      .timeout(timeout);

  if (resp.statusCode >= 200 && resp.statusCode < 300) {
    final data = jsonDecode(resp.body) as Map<String, dynamic>;
    final text =
    (data['choices']?[0]?['message']?['content'] ?? '').toString().trim();
    if (text.isNotEmpty) return text;
    throw StateError('Empty AI response');
  } else {
    throw HttpException('AI text error: ${resp.statusCode} ${resp.body}');
  }
}