AgentListing.of constructor

AgentListing.of(
  1. AgentSurface surface
)

Reads surface's AgentSurface.agentCapabilities and names its items with the generated AgentSurface.agentSchema.

Throws a StateError when the list and the generated code disagree, which means the code was not generated again after the list changed.

Implementation

factory of(AgentSurface surface) {
  final schema = surface.agentSchema;
  final capabilities = surface.agentCapabilities;
  final visibleValues = [
    for (final item in capabilities)
      if (item case _Sees(:final value)) value,
  ];
  if (visibleValues.length != schema.sees.length) {
    throw StateError(
      'The agent list has ${visibleValues.length} .sees, the generated code '
      '${schema.sees.length}. Run build_runner again.',
    );
  }
  final tools = <String, AgentTool>{};
  for (final item in capabilities) {
    if (item case _Calls(:final method)) {
      final tool = schema.calls.firstWhere(
        (tool) => tool.method == method,
        orElse: () => throw StateError(
          'The generated code does not know a method of the agent list. '
          'Run build_runner again.',
        ),
      );
      if (tools.containsKey(tool.name)) {
        throw StateError('The agent list has ${tool.name} twice.');
      }
      tools[tool.name] = tool;
    }
  }
  return ._(surface, [
    for (final (index, view) in schema.sees.indexed)
      {
        AgUiFields.description: '${view.name}: ${view.description}',
        AgUiFields.value: jsonEncode(view.write(visibleValues[index])),
      },
  ], tools);
}