toolCallHeadline function

ToolCallHeadline toolCallHeadline({
  1. required String toolkit,
  2. required String tool,
  3. required Map<String, Object?>? arguments,
  4. bool failed = false,
  5. bool completed = true,
  6. bool pending = false,
  7. int argumentDeltaBytes = 0,
})

Implementation

ToolCallHeadline toolCallHeadline({
  required String toolkit,
  required String tool,
  required Map<String, Object?>? arguments,
  bool failed = false,
  bool completed = true,
  bool pending = false,
  int argumentDeltaBytes = 0,
}) {
  final label = toolCallLabel(toolkit: toolkit, tool: tool, arguments: arguments);
  final friendly = _friendlyBuiltinHeadline(
    toolkit: toolkit,
    tool: tool,
    arguments: arguments,
    failed: failed,
    completed: completed,
    pending: pending,
  );
  if (friendly != null) {
    return _headlineWithArgumentDeltaBytes(friendly, completed: completed || failed, argumentDeltaBytes: argumentDeltaBytes);
  }

  final normalizedTool = tool.trim().toLowerCase();
  final normalizedToolkit = toolkit.trim().toLowerCase();
  if (!completed && !failed && normalizedToolkit == 'openai' && _shellTools.contains(normalizedTool) && arguments == null) {
    return _headlineWithArgumentDeltaBytes(
      ToolCallHeadline(action: pending ? 'Preparing' : 'Running', rest: 'commands'),
      completed: false,
      argumentDeltaBytes: argumentDeltaBytes,
    );
  }
  if (failed || !_commandTools.contains(normalizedTool) || arguments == null) {
    return _headlineWithArgumentDeltaBytes(
      ToolCallHeadline(action: failed ? 'Failed' : (completed ? 'Ran' : (pending ? 'Preparing' : 'Running')), rest: label),
      completed: completed || failed,
      argumentDeltaBytes: argumentDeltaBytes,
    );
  }

  final commands = _commandArguments(tool: normalizedTool, arguments: arguments);
  if (commands.isEmpty) {
    return _headlineWithArgumentDeltaBytes(
      ToolCallHeadline(action: failed ? 'Failed' : (completed ? 'Ran' : (pending ? 'Preparing' : 'Running')), rest: label),
      completed: completed || failed,
      argumentDeltaBytes: argumentDeltaBytes,
    );
  }

  final parsed = <ParsedCommand>[];
  for (final command in commands) {
    parsed.addAll(parseToolCommand(command));
  }
  if (parsed.isEmpty || parsed.any((item) => item.kind == 'unknown')) {
    return _headlineWithArgumentDeltaBytes(
      ToolCallHeadline(action: completed ? 'Ran' : (pending ? 'Preparing' : 'Running'), rest: label),
      completed: completed,
      argumentDeltaBytes: argumentDeltaBytes,
    );
  }

  final lines = ['Explored'];
  for (final line in _exploringDetailLines(parsed)) {
    lines.add('  $line');
  }
  return _headlineWithArgumentDeltaBytes(
    ToolCallHeadline(action: lines.join('\n')),
    completed: completed,
    argumentDeltaBytes: argumentDeltaBytes,
  );
}