Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 183 additions & 61 deletions tool/generator/lib/src/services/gemini_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:retry/retry.dart';
import 'package:yaml/yaml.dart';

import 'prompts.dart';
import 'skill_instructions.dart';
Expand All @@ -21,6 +22,7 @@ class GeminiService {
http.Client? httpClient,
String? model,
}) : _model = model ?? defaultModel,
_ownsClient = httpClient == null,
_client = _ApiKeyClient(httpClient ?? http.Client(), apiKey);

/// Default temperature for legacy models.
Expand Down Expand Up @@ -88,10 +90,49 @@ class GeminiService {
),
];

static final RegExp _leadingCodeFenceRegExp = RegExp(
r'^\s*(`{3,})[^\r\n`]*\r?\n',
);
static final RegExp _yamlFenceLineRegExp = RegExp(
r'^---[ \t]*$',
multiLine: true,
);
static final RegExp _markdownPrefixStartRegExp = RegExp(
r'^(?:[-#>*+_~`]|\d+\.)',
);
static final RegExp _leadingBlankLineRegExp = RegExp(
r'^[ \t]*\r?\n[ \t]*\r?\n',
);
static final RegExp _trailingBlankLineRegExp = RegExp(
r'\r?\n[ \t]*\r?\n[ \t]*$',
);
static final RegExp _yamlFrontmatterKeyRegExp = RegExp(
r'^[a-z][a-zA-Z0-9_-]*$',
);
static const Set<String> _knownFrontmatterKeys = {
'name',
'description',
'metadata',
'license',
'compatibility',
'allowed-tools',
};

final String _model;
final bool _ownsClient;
final http.Client _client;
final Logger _logger = Logger('GeminiService');

/// Closes the underlying HTTP client if it was created internally by this
/// [GeminiService].
///
/// Does not close an external `httpClient` passed into the constructor.
void close() {
if (_ownsClient) {
_client.close();
}
}

/// Generates the content for a skill based on raw markdown input.
Future<String?> generateSkillContent(
String rawMarkdown,
Expand All @@ -116,23 +157,16 @@ class GeminiService {

try {
const r = RetryOptions(maxAttempts: 3);
final response = await r.retry(() async {
final cleanedBody = await r.retry(() async {
final res = await service.generateContent(request);
final text = res.candidates.first.content?.parts
.where((part) => !part.thought)
.map((part) => part.text)
.where((text) => text != null)
.join('\n');

if (text == null || text.isEmpty) {
throw const FormatException('Empty response from Gemini');
final text = _extractVerifiedText(res);
final cleaned = cleanContent(text);
if (cleaned == null || cleaned.trim().isEmpty) {
throw const FormatException('Empty markdown body from Gemini');
}

return text;
return cleaned;
}, onRetry: (e) => _logger.warning('Retrying Gemini generation: $e'));

final content = response;

final frontmatter =
'''---
name: $skillName
Expand All @@ -143,7 +177,7 @@ metadata:
---
''';

return frontmatter + (cleanContent(content) ?? '');
return frontmatter + cleanedBody;
} on Object catch (e) {
_logger.severe('Gemini generation failed: $e');
return null;
Expand Down Expand Up @@ -179,23 +213,16 @@ metadata:

try {
const r = RetryOptions(maxAttempts: 3);
final response = await r.retry(() async {
final cleanedBody = await r.retry(() async {
final res = await service.generateContent(request);
final text = res.candidates.first.content?.parts
.where((part) => !part.thought)
.map((part) => part.text)
.where((text) => text != null)
.join('\n');

if (text == null || text.isEmpty) {
throw const FormatException('Empty response from Gemini');
final text = _extractVerifiedText(res);
final cleaned = cleanContent(text);
if (cleaned == null || cleaned.trim().isEmpty) {
throw const FormatException('Empty markdown body from Gemini');
}

return text;
return cleaned;
}, onRetry: (e) => _logger.warning('Retrying Gemini generation: $e'));

final content = response;

final frontmatter =
'''---
name: $skillName
Expand All @@ -206,14 +233,14 @@ metadata:
---
''';

return frontmatter + (cleanContent(content) ?? '');
return frontmatter + cleanedBody;
} on Object catch (e) {
_logger.severe('Gemini update failed: $e');
return null;
}
}

/// Validates an existing skill
/// Validates an existing skill.
Future<String?> validateExistingSkillContent(
String markdown,
String skillName,
Expand Down Expand Up @@ -246,17 +273,7 @@ metadata:
const r = RetryOptions(maxAttempts: 3);
final response = await r.retry(() async {
final res = await service.generateContent(request);
final text = res.candidates.first.content?.parts
.where((part) => !part.thought)
.map((part) => part.text)
.where((text) => text != null)
.join('\n');

if (text == null || text.isEmpty) {
throw const FormatException('Empty response from Gemini');
}

return text;
return _extractVerifiedText(res);
}, onRetry: (e) => _logger.warning('Retrying Gemini validation: $e'));

return response;
Expand All @@ -266,40 +283,140 @@ metadata:
}
}

/// Cleans the generated content by removing markdown code blocks and frontmatter.
String _extractVerifiedText(GenerateContentResponse res) {
if (res.candidates.isEmpty) {
throw FormatException(
'No candidates returned from Gemini '
'(blockReason: ${res.promptFeedback?.blockReason}).',
);
}
final candidate = res.candidates.first;
final finishReason = candidate.finishReason;
if (finishReason != Candidate_FinishReason.stop &&
finishReason != Candidate_FinishReason.finishReasonUnspecified) {
throw FormatException(
'Incomplete Gemini response (finishReason: $finishReason).',
);
}
final text = candidate.content?.parts
.where((part) => !part.thought)
.map((part) => part.text)
.where((text) => text != null)
.join('\n');

if (text == null || text.trim().isEmpty) {
throw const FormatException('Empty response from Gemini');
}
return text;
}
Comment on lines +286 to +311

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation of _extractVerifiedText has potential runtime nullability issues:

  1. res.candidates is a nullable list (List<Candidate>?) in the Gemini API response. Calling res.candidates.isEmpty directly without a null check can throw a NoSuchMethodError at runtime.
  2. candidate.content?.parts is also nullable. If content is non-null but parts is null, calling .where directly on it will throw a NoSuchMethodError because the null-aware chain only short-circuits if the receiver of ?. (i.e., content) is null, not if a subsequent property is null.
  3. Using .where((text) => text != null) keeps the iterable type as Iterable<String?>. Using .whereType<String>() is the idiomatic Dart way to filter out nulls and cast the iterable to non-nullable Strings at the same time.
  String _extractVerifiedText(GenerateContentResponse res) {
    final candidates = res.candidates;
    if (candidates == null || candidates.isEmpty) {
      throw FormatException(
        'No candidates returned from Gemini '
        '(blockReason: ${res.promptFeedback?.blockReason}).',
      );
    }
    final candidate = candidates.first;
    final finishReason = candidate.finishReason;
    if (finishReason != Candidate_FinishReason.stop &&
        finishReason != Candidate_FinishReason.finishReasonUnspecified) {
      throw FormatException(
        'Incomplete Gemini response (finishReason: $finishReason).',
      );
    }
    final parts = candidate.content?.parts;
    if (parts == null) {
      throw const FormatException('Empty response from Gemini');
    }
    final text = parts
        .where((part) => !part.thought)
        .map((part) => part.text)
        .whereType<String>()
        .join('\n');

    if (text.trim().isEmpty) {
      throw const FormatException('Empty response from Gemini');
    }
    return text;
  }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In package:google_cloud_ai_generativelanguage_v1beta (GenerateContentResponse and Content ProtoMessage definitions), both res.candidates (final List<Candidate> candidates) and Content.parts (final List<Part> parts) are non-nullable lists defaulting to const [], and candidate.content?.parts.where(...) short-circuits the full selector chain when candidate.content is null. Adding candidates == null also triggers unnecessary_null_comparison under dart analyze --fatal-infos, so keeping the current null-safe check as-is.

(AI assisted -- human reviewed -- jwren)


/// Cleans the generated content by removing outer markdown code fences and
/// leading YAML frontmatter while preserving horizontal rules (`---`) inside
/// the markdown body.
@visibleForTesting
String? cleanContent(String? content) {
if (content == null) return null;
var cleaned = content;
final startMatch = RegExp(
r'^\s*```[a-zA-Z]*\s*\n',
caseSensitive: false,
).firstMatch(cleaned);
final startMatch = _leadingCodeFenceRegExp.firstMatch(cleaned);
if (startMatch != null) {
final fence = startMatch.group(1)!;
cleaned = cleaned.substring(startMatch.end);
// Remove the last triple backticks if they exist
cleaned = cleaned.replaceAll(RegExp(r'\n```\s*$'), '');
final trailingFenceRegExp = RegExp(
'(?:^|\\r?\\n)[ \\t]*${RegExp.escape(fence)}\\s*\$',
);
cleaned = cleaned.replaceFirst(trailingFenceRegExp, '');
}

final yamlStartIndex = cleaned.indexOf('---');
if (yamlStartIndex == 0) {
// Possible frontmatter, skip it
final end = cleaned.indexOf('---', 3);
if (end != -1) {
cleaned = cleaned.substring(end + 3).trim();
}
} else if (yamlStartIndex > 0) {
// Maybe noise before frontmatter, try to strip it if it looks like frontmatter
final end = cleaned.indexOf('---', yamlStartIndex + 3);
if (end != -1) {
cleaned = cleaned.substring(end + 3).trim();
}
}
cleaned = _stripLeadingFrontmatter(cleaned);

// Ensure one trailing newline
return '${cleaned.trim()}\n';
}

String _stripLeadingFrontmatter(String input) {
final firstFence = _yamlFenceLineRegExp.firstMatch(input);
if (firstFence == null) {
return input;
}

final prefix = input.substring(0, firstFence.start).trim();
// Allow frontmatter only at the start of the response or immediately after
// a single-line conversational lead-in ending with ':' (e.g. "Here is the
// content:"). Reject multi-line prefixes and Markdown block starters
// (headings, blockquotes, lists, emphasis, or code fences).
if (prefix.isNotEmpty &&
(prefix.contains('\n') ||
prefix.contains('\r') ||
!prefix.endsWith(':') ||
_markdownPrefixStartRegExp.hasMatch(prefix))) {
return input;
}

final remainder = input.substring(firstFence.end);
final secondFence = _yamlFenceLineRegExp.firstMatch(remainder);
if (secondFence == null) {
return input;
}

final rawCandidate = remainder.substring(0, secondFence.start);
// YAML frontmatter never starts with a blank line after `---`, whereas
// Markdown horizontal rules (`---`) and Setext H2 sections do.
if (_leadingBlankLineRegExp.hasMatch(rawCandidate)) {
return input;
}

final hasTrailingBlankLine = _trailingBlankLineRegExp.hasMatch(
rawCandidate,
);
if (hasTrailingBlankLine && prefix.isNotEmpty) {
return input;
}

final bodyAfterFence = remainder.substring(secondFence.end).trim();
final candidateYaml = rawCandidate.trim();
if (candidateYaml.isEmpty) {
return prefix.isEmpty ? bodyAfterFence : input;
}

if (!_isValidFrontmatterBlock(
candidateYaml,
allowNullDocument: prefix.isEmpty && bodyAfterFence.isNotEmpty,
requireKnownKey: prefix.isNotEmpty || hasTrailingBlankLine,
)) {
return input;
}

return bodyAfterFence;
}

bool _isValidFrontmatterBlock(
String candidateYaml, {
required bool allowNullDocument,
required bool requireKnownKey,
}) {
try {
final parsed = loadYaml(candidateYaml);
if (parsed == null) {
return allowNullDocument;
}
if (parsed is! YamlMap || parsed.isEmpty) {
return false;
}
final hasValidKeys = parsed.keys.every(
(key) => key is String && _yamlFrontmatterKeyRegExp.hasMatch(key),
);
if (!hasValidKeys) {
return false;
}
if (requireKnownKey) {
return parsed.keys.any(_knownFrontmatterKeys.contains);
}
return true;
} on YamlException {
return false;
}
Comment on lines +415 to +417

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Catching only YamlException might allow other unexpected exceptions or errors (such as ArgumentError or StateError from package:yaml's internal parser) to escape and crash the generator tool. Catching any Object ensures robust error handling for any malformed input.

Suggested change
} on YamlException {
return false;
}
} on Object {
return false;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tool/generator/analysis_options.yaml enables avoid_catching_errors: true to avoid masking Error subclasses in normal predicates. loadYaml(candidateYaml) (with recover: false and no sourceUrl) throws YamlException on malformed YAML input, and the outer generateSkillContent / updateSkillContent methods already guard the top-level generation call, so keeping on YamlException here.

(AI assisted -- human reviewed -- jwren)

}

GenerateContentRequest _createRequest(
String prompt, {
String? systemInstruction,
Expand Down Expand Up @@ -345,4 +462,9 @@ class _ApiKeyClient extends http.BaseClient {
request.headers['x-goog-api-key'] = _apiKey;
return _inner.send(request);
}

@override
void close() {
_inner.close();
}
}
Loading
Loading