Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
b49f458
-
polina-c Dec 24, 2025
62e769d
-
polina-c Dec 24, 2025
5b70ebc
Update mesage.dart
polina-c Dec 24, 2025
47f4ef5
Update mesage.dart
polina-c Dec 24, 2025
ed024ad
-
polina-c Dec 24, 2025
0f37256
Update genai_primitives.dart
polina-c Dec 24, 2025
918e4d9
Update genai_primitives_test.dart
polina-c Dec 24, 2025
daaa464
-
polina-c Dec 24, 2025
0452bea
-
polina-c Dec 24, 2025
87cc5dd
-
polina-c Dec 24, 2025
724ba2b
-
polina-c Dec 24, 2025
2ea87b5
-
polina-c Dec 24, 2025
5887cc4
-
polina-c Dec 24, 2025
f6af083
-
polina-c Dec 24, 2025
f259308
Update genai_primitives_test.dart
polina-c Dec 24, 2025
5110939
-
polina-c Dec 24, 2025
31e666a
Update message_parts.dart
polina-c Dec 24, 2025
6a245e8
-
polina-c Dec 26, 2025
52eb3a7
Update message_parts.dart
polina-c Dec 26, 2025
af0d06b
ci: add workflow_dispatch to enable manual CI triggers (#633)
andrewkolos Jan 5, 2026
4ebab33
Add A2UI support section to README (#648)
jacobsimionato Jan 5, 2026
0d4e92d
Update dartantic_ai dependency (#649)
gspencergoog Jan 5, 2026
910f4c5
Post publish commit to add sections to CHANGELOGs (#650)
gspencergoog Jan 5, 2026
4bf48a4
[docs] Suggest using `flutter pub add` for adding dependencies (#645)
parlough Jan 6, 2026
9f14e62
Improve error handling for catalog example loading (#653)
nan-yu Jan 6, 2026
c29fca4
-
polina-c Jan 9, 2026
56e12a8
Update message.dart
polina-c Jan 9, 2026
36593a0
---
polina-c Jan 9, 2026
167109f
--
polina-c Jan 9, 2026
dee7e6b
-
polina-c Jan 9, 2026
1eb2b23
-
polina-c Jan 9, 2026
b3c3014
Create custom_part_test.dart
polina-c Jan 9, 2026
aebee60
Update custom_part_test.dart
polina-c Jan 9, 2026
62a421d
-
polina-c Jan 9, 2026
29381ee
[examples] Verdure client cleanup and dependency updates (#646)
parlough Jan 8, 2026
1715110
Document how to get permissions to publish. (#657)
polina-c Jan 12, 2026
f16ca7d
- (#658)
polina-c Jan 13, 2026
1742057
-
polina-c Jan 15, 2026
006181f
-
polina-c Jan 15, 2026
f6814ee
Update genai_primitives_test.dart
polina-c Jan 15, 2026
19e9ad3
-
polina-c Jan 15, 2026
85825a6
-
polina-c Jan 15, 2026
41b1d1c
-
polina-c Jan 15, 2026
bc12115
-
polina-c Jan 15, 2026
551b51c
-
polina-c Jan 15, 2026
57b30c6
-
polina-c Jan 15, 2026
d6f56a0
-
polina-c Jan 15, 2026
5f9d61c
Post publish commit to add sections to CHANGELOGs (#650)
gspencergoog Jan 5, 2026
1bb74ba
Make ContentGeneratorError be an Exception (#660)
gspencergoog Jan 15, 2026
9f90508
Enable stricter dynamic-related analysis (#652)
parlough Jan 15, 2026
4456cec
Move Chat widgets to facade and rename. (#661)
polina-c Jan 15, 2026
f961c42
Reorganize specifications. (#662)
polina-c Jan 15, 2026
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
124 changes: 76 additions & 48 deletions packages/genai_primitives/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,26 @@ import 'dart:typed_data';
import 'package:genai_primitives/genai_primitives.dart';
import 'package:json_schema_builder/json_schema_builder.dart';

void main() {
print('--- GenAI Primitives Example ---');
enum Role { system, user, model }

class ChatMessage {
final Role role;
final Message content;

const ChatMessage({required this.role, required this.content});

const ChatMessage.system(Message content)
: this(role: Role.system, content: content);

const ChatMessage.user(Message content)
: this(role: Role.user, content: content);

const ChatMessage.model(Message content)
: this(role: Role.model, content: content);
}

void main({void Function(Object? object) output = print}) {
output('--- GenAI Primitives Example ---');

// 1. Define a Tool
final ToolDefinition<Object> getWeatherTool = ToolDefinition(
Expand All @@ -29,92 +47,102 @@ void main() {
),
);

print('\n[Tool Definition]');
print(const JsonEncoder.withIndent(' ').convert(getWeatherTool.toJson()));
output('\n[Tool Definition]');
output(const JsonEncoder.withIndent(' ').convert(getWeatherTool.toJson()));

// 2. Create a conversation history
final history = <ChatMessage>[
// System message
ChatMessage.system(
'You are a helpful weather assistant. '
'Use the get_weather tool when needed.',
Message(
'You are a helpful weather assistant. '
'Use the get_weather tool when needed.',
),
),

// User message asking for weather
ChatMessage.user('What is the weather in London?'),
ChatMessage.user(Message('What is the weather in London?')),
];

print('\n[Initial Conversation]');
output('\n[Initial Conversation]');
for (final msg in history) {
print('${msg.role.name}: ${msg.text}');
output('${msg.role.name}: ${msg.content.text}');
}

// 3. Simulate Model Response with Tool Call
final modelResponse = ChatMessage.model(
'', // Empty text for tool call
parts: [
const TextPart('Thinking: User wants weather for London...'),
const ToolPart.call(
callId: 'call_123',
toolName: 'get_weather',
arguments: {'location': 'London', 'unit': 'celsius'},
),
],
Message(
'', // Empty text for tool call
parts: [
const TextPart('Thinking: User wants weather for London...'),
const ToolPart.call(
callId: 'call_123',
toolName: 'get_weather',
arguments: {'location': 'London', 'unit': 'celsius'},
),
],
),
);
history.add(modelResponse);

print('\n[Model Response with Tool Call]');
if (modelResponse.hasToolCalls) {
for (final ToolPart call in modelResponse.toolCalls) {
print('Tool Call: ${call.toolName}(${call.arguments})');
output('\n[Model Response with Tool Call]');
if (modelResponse.content.hasToolCalls) {
for (final ToolPart call in modelResponse.content.toolCalls) {
output('Tool Call: ${call.toolName}(${call.arguments})');
}
}

// 4. Simulate Tool Execution & Result
final toolResult = ChatMessage.user(
'', // User role is typically used for tool results in many APIs
parts: [
const ToolPart.result(
callId: 'call_123',
toolName: 'get_weather',
result: {'temperature': 15, 'condition': 'Cloudy'},
),
],
Message(
'', // User role is typically used for tool results in many APIs
parts: [
const ToolPart.result(
callId: 'call_123',
toolName: 'get_weather',
result: {'temperature': 15, 'condition': 'Cloudy'},
),
],
),
);
history.add(toolResult);

print('\n[Tool Result]');
print('Result: ${toolResult.toolResults.first.result}');
output('\n[Tool Result]');
output('Result: ${toolResult.content.toolResults.first.result}');

// 5. Simulate Final Model Response with Data (e.g. an image generated or
// returned)
final finalResponse = ChatMessage.model(
'Here is a chart of the weather trend:',
parts: [
DataPart(
Uint8List.fromList([0x89, 0x50, 0x4E, 0x47]), // Fake PNG header
mimeType: 'image/png',
name: 'weather_chart.png',
),
],
Message(
'Here is a chart of the weather trend:',
parts: [
DataPart(
Uint8List.fromList([0x89, 0x50, 0x4E, 0x47]), // Fake PNG header
mimeType: 'image/png',
name: 'weather_chart.png',
),
],
),
);
history.add(finalResponse);

print('\n[Final Model Response with Data]');
print('Text: ${finalResponse.text}');
if (finalResponse.parts.any((p) => p is DataPart)) {
final DataPart dataPart = finalResponse.parts.whereType<DataPart>().first;
print(
output('\n[Final Model Response with Data]');
output('Text: ${finalResponse.content.text}');
if (finalResponse.content.parts.any((p) => p is DataPart)) {
final DataPart dataPart = finalResponse.content.parts
.whereType<DataPart>()
.first;
output(
'Attachment: ${dataPart.name} '
'(${dataPart.mimeType}, ${dataPart.bytes.length} bytes)',
);
}

// 6. Demonstrate JSON serialization of the whole history
print('\n[Full History JSON]');
print(
output('\n[Full History JSON]');
output(
const JsonEncoder.withIndent(
' ',
).convert(history.map((m) => m.toJson()).toList()),
).convert(history.map((m) => m.content.toJson()).toList()),
);
}
2 changes: 1 addition & 1 deletion packages/genai_primitives/lib/genai_primitives.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
/// A set of primitives for working with generative AI.
library;

export 'src/chat_message.dart';
export 'src/message.dart';
export 'src/message_parts.dart';
export 'src/tool_definition.dart';
141 changes: 0 additions & 141 deletions packages/genai_primitives/lib/src/chat_message.dart

This file was deleted.

Loading
Loading