Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions packages/genui/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# [genui](https://pub.dev/packages/genui) Changelog

## 0.10.7

- **Fix**: Validation `checks` on `CheckBox`, `Slider` and `ChoicePicker` now fail when their condition is false, and show the check's `message` instead of a generic "Invalid value".
- **Fix**: `and`/`or` now evaluate bindings and function calls inside `values`, so a failing nested check fails the rule.
- **Behavior change**: Conditions use truthiness instead of a null check. `0`, `''`, `[]`, `{}` and unknown functions evaluate to `false`, and malformed condition expressions log a warning.
- **Behavior change**: `resolve()` and `resolveContext()` recursively resolve dynamic values inside lists.

## 0.10.6

- Added `AsynchronousClientFunction` base class for single-shot asynchronous
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ final checkBox = CatalogItem(
);
}
},
subtitle: errorMessage != null
subtitle: isError
? Text(
errorMessage,
Comment on lines +95 to 97

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

Since errorMessage is a nullable String? and isError is a local boolean variable, Dart's type promotion does not automatically promote errorMessage to a non-nullable String inside the ternary operator. This will cause a static analysis/compilation error because the Text constructor expects a non-nullable String. Use the null-assertion operator ! to safely cast it, as isError guarantees it is non-null.

Suggested change
subtitle: isError
? Text(
errorMessage,
subtitle: isError
? Text(
errorMessage!,

style: TextStyle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,28 +139,3 @@ Widget buildWeightedChild({
}
return childWidget;
}

/// Converts a list of validation checks into a single expression that evaluates
/// to true if all checks pass.
Object? checksToExpression(List<JsonMap>? checks) {
if (checks == null || checks.isEmpty) {
return true;
}

final List<Object?> conditions = checks
.map((c) => c['condition'])
.where((c) => c != null)
.toList();
if (conditions.isEmpty) {
return true;
}
if (conditions.length == 1) {
return conditions.first;
}

// Combine all checks into a single 'and' condition
return {
'call': 'and',
'args': {'values': conditions},
};
}
2 changes: 1 addition & 1 deletion packages/genui/lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export 'model/catalog.dart';
export 'model/catalog_item.dart';
export 'model/chat_message.dart';
export 'model/client_function.dart';
export 'model/data_model.dart';
export 'model/data_model.dart' hide isTruthy;
export 'model/data_path.dart';
export 'model/generation_events.dart';
export 'model/parts.dart';
Expand Down
88 changes: 70 additions & 18 deletions packages/genui/lib/src/model/data_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ class DataContext implements cf.ExecutionContext {
if (value.containsKey('call')) {
return _evaluateFunctionCall(value as JsonMap);
}
if (value.containsKey('functionCall') && value['functionCall'] is Map) {
return _evaluateStream(value['functionCall']);
}
}
if (value is List) {
if (value.isEmpty) return Stream.value(<Object?>[]);
Expand Down Expand Up @@ -152,7 +149,52 @@ class DataContext implements cf.ExecutionContext {
for (final Object? key in argsJson.keys) {
final argName = key.toString();
final Object? val = argsJson[key];
args[argName] = _evaluateStream(val);
if ((name == 'and' || name == 'or') &&
argName == 'values' &&
val is List) {
final List<Stream<Object?>> itemStreams = val.map((item) {
if (item is Map) {
if (item.containsKey('functionCall')) {
final Object? fc = item['functionCall'];
if (fc is Map) {
return _evaluateStream(fc);

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

When a nested item inside and/or contains a functionCall that is a Map (fc), its shape is never validated against _isValidConditionShape(fc). This allows malformed nested function calls (e.g., {'functionCall': {'cal': 'required'}}) to bypass validation, log no warnings, and evaluate to true (since any non-empty map is truthy). Add a _isValidConditionShape(fc) check to ensure nested function calls are properly validated.

Suggested change
if (fc is Map) {
return _evaluateStream(fc);
if (fc is Map && _isValidConditionShape(fc)) {
return _evaluateStream(fc);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in dcb1029. Added _isValidConditionShape(fc) check and updated _isValidConditionShape to recursively validate functionCall envelopes.

} else {
genUiLogger.warning('Invalid condition expression: $item');
return Stream<Object?>.value(false);
}
}
if (!_isValidConditionShape(item)) {
genUiLogger.warning('Invalid condition expression: $item');
return Stream<Object?>.value(false);
}
}
return _evaluateStream(item);
}).toList();
args[argName] = itemStreams.isEmpty
? Stream.value(<Object?>[])
: itemStreams.combineLatestAll();
} else if (name == 'not' && argName == 'value') {
if (val is Map) {
if (val.containsKey('functionCall')) {
final Object? fc = val['functionCall'];
if (fc is Map) {
args[argName] = _evaluateStream(fc);

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

Similarly to the and/or validation, the nested functionCall inside not's value is not validated against _isValidConditionShape(fc). This allows malformed nested function calls to bypass validation and evaluate incorrectly. Add a _isValidConditionShape(fc) check here as well.

Suggested change
if (fc is Map) {
args[argName] = _evaluateStream(fc);
if (fc is Map && _isValidConditionShape(fc)) {
args[argName] = _evaluateStream(fc);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in dcb1029. Added _isValidConditionShape(fc) check for nested functionCall inside not's value.

} else {
genUiLogger.warning('Invalid condition expression: $val');
args[argName] = Stream<Object?>.value(true);
}
} else if (!_isValidConditionShape(val)) {
genUiLogger.warning('Invalid condition expression: $val');
args[argName] = Stream<Object?>.value(true);
} else {
args[argName] = _evaluateStream(val);
}
} else {
args[argName] = _evaluateStream(val);
}
} else {
args[argName] = _evaluateStream(val);
}
}
}

Expand Down Expand Up @@ -180,33 +222,43 @@ class DataContext implements cf.ExecutionContext {
if (condition == null) return Stream.value(false);
if (condition is bool) return Stream.value(condition);

if (condition is Map &&
!condition.containsKey('path') &&
!condition.containsKey('call') &&
!condition.containsKey('functionCall')) {
genUiLogger.warning('Invalid condition expression: $condition');
return Stream.value(false);
if (condition is Map) {
if (condition.containsKey('functionCall')) {
final Object? fc = condition['functionCall'];
if (fc is Map) {
return evaluateConditionStream(fc);
} else {
genUiLogger.warning('Invalid condition expression: $condition');
return Stream.value(false);
}
}
if (!_isValidConditionShape(condition)) {
genUiLogger.warning('Invalid condition expression: $condition');
return Stream.value(false);
}
}

final Stream<Object?> resultStream = _evaluateStream(condition);
return resultStream.map(isTruthy);
}
}

bool _isValidConditionShape(Map<Object?, Object?> map) {
return map.containsKey('path') ||
map.containsKey('call') ||
(map.containsKey('functionCall') && map['functionCall'] is Map);
}

/// Helper to determine truthiness according to A2UI expression semantics.
@internal
bool isTruthy(Object? value) {
if (value is bool) return value;
if (value == null) return false;
if (value is bool) return value;
if (value is String) return value.isNotEmpty;
if (value is num) return value != 0 && !value.isNaN;
if (value is List) return value.isNotEmpty;
if (value is Map) {
if (value.containsKey('valid') && value['valid'] is bool) {
return value['valid'] as bool;
}
return value.isNotEmpty;
}
return false;
if (value is Map) return value.isNotEmpty;
return true;
}

/// Resolves a context map definition against a [DataContext].
Expand Down
2 changes: 1 addition & 1 deletion packages/genui/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

name: genui
description: Generates and displays generative user interfaces (GenUI) in Flutter using AI.
version: 0.10.6
version: 0.10.7
homepage: https://github.com/flutter/genui/tree/main/packages/genui
license: BSD-3-Clause
issue_tracker: https://github.com/flutter/genui/issues
Expand Down
Loading
Loading