Skip to content

Commit b7a6744

Browse files
committed
-
1 parent 4181b9c commit b7a6744

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

analysis_options.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
include: package:lints/recommended.yaml
66

77
analyzer:
8+
errors:
9+
avoid_print: ignore
810
language:
911
strict-casts: true
1012
strict-inference: true

examples/eval/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ environment:
1212
resolution: workspace
1313

1414
dependencies:
15+
dartantic_ai: ^3.1.0
1516
flutter:
1617
sdk: flutter
1718
genui: ^0.7.0

examples/eval/test/smoke_test.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
// ignore_for_file: avoid_print
6+
57
import 'package:flutter_test/flutter_test.dart';
8+
import 'package:genui/genui.dart' as dartantic;
69

10+
import 'test_infra/ai_client.dart';
711
import 'test_infra/api_key.dart';
812

913
void main() {
1014
test('test can read api key "$geminiApiKeyName"', () {
1115
final String key = apiKeyForEval();
1216
expect(key, isNotEmpty);
13-
// ignore: avoid_print
1417
print('API Key: ${key.substring(0, 1)}...${key.substring(key.length - 1)}');
1518
});
19+
20+
test('test can send message', () async {
21+
final aiClient = DartanticAiClient();
22+
addTearDown(aiClient.dispose);
23+
24+
final String result = await aiClient
25+
.sendStream('Hello, how are you?', history: [])
26+
.first;
27+
expect(result, isNotEmpty);
28+
print(result);
29+
print(result);
30+
});
1631
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import 'dart:async';
2+
3+
import 'package:dartantic_ai/dartantic_ai.dart' as dartantic;
4+
5+
import 'api_key.dart';
6+
7+
/// An abstract interface for AI clients.
8+
abstract interface class AiClient {
9+
/// Sends a message stream request to the AI service.
10+
///
11+
/// [prompt] is the user's message.
12+
/// [history] is the conversation history.
13+
Stream<String> sendStream(
14+
String prompt, {
15+
required List<dartantic.ChatMessage> history,
16+
});
17+
18+
/// Dispose of resources.
19+
void dispose();
20+
}
21+
22+
/// An implementation of [AiClient] using `package:dartantic_ai`.
23+
class DartanticAiClient implements AiClient {
24+
DartanticAiClient({String? modelName}) {
25+
final String apiKey = apiKeyForEval();
26+
_provider = dartantic.GoogleProvider(apiKey: apiKey);
27+
_agent = dartantic.Agent.forProvider(
28+
_provider,
29+
chatModelName: modelName ?? 'gemini-3-flash-preview',
30+
);
31+
}
32+
33+
late final dartantic.GoogleProvider _provider;
34+
late final dartantic.Agent _agent;
35+
36+
@override
37+
Stream<String> sendStream(
38+
String prompt, {
39+
required List<dartantic.ChatMessage> history,
40+
}) async* {
41+
final Stream<dartantic.ChatResult<String>> stream = _agent.sendStream(
42+
prompt,
43+
history: history,
44+
);
45+
46+
await for (final result in stream) {
47+
if (result.output.isNotEmpty) {
48+
yield result.output;
49+
}
50+
}
51+
}
52+
53+
@override
54+
void dispose() {
55+
// Dartantic Agent/Provider doesn't strictly require disposal currently,
56+
// but good to have the hook.
57+
}
58+
}

0 commit comments

Comments
 (0)