project static method

Folds events into a single patch for the session sessionId.

base is the session as currently stored, when the caller holds it. A message delta APPENDS to base's transcript and a tool start APPENDS to base's calls, so a runtime that flushes each batch as its own patch preserves the transcript and the tool history across flushes instead of leaving only the latest batch. Omit base for the first flush (or when the prior state is unknown), where the batch is the whole history.

An empty events list yields the neutral (empty) patch, so a runtime that emits nothing between two flushes writes nothing to the store.

Implementation

static ZorphyPatch<AgentSessionDocument> project(
  String sessionId,
  List<AgentSessionEvent> events, {
  AgentSessionDocument? base,
}) {
  final messages = <AgentMessage>[...?base?.messages];
  final toolCalls = <AgentToolCall>[...?base?.toolCalls];
  var turns = base?.turns ?? 0;
  var status = base?.status ?? AgentSessionStatus.idle;
  var activeTool = base?.activeTool;

  var touchesMessages = false;
  var touchesToolCalls = false;
  var touchesTurns = false;
  var touchesStatus = false;
  var touchesActiveTool = false;

  for (final event in events) {
    switch (event) {
      case AgentMessageDelta(:final role, :final text, :final turn):
        messages.add(
          AgentMessage.fromTrusted(role: role, text: text, turn: turn),
        );
        touchesMessages = true;
        status = AgentSessionStatus.running;
        touchesStatus = true;
      case AgentToolStart(:final toolName, :final turn):
        toolCalls.add(AgentToolCall.fromTrusted(name: toolName, turn: turn));
        touchesToolCalls = true;
        activeTool = toolName;
        touchesActiveTool = true;
        status = AgentSessionStatus.running;
        touchesStatus = true;
      case AgentToolEnd(:final toolName, :final outcome, :final turn):
        final completed = AgentToolCall.fromTrusted(
          name: toolName,
          turn: turn,
          outcome: outcome,
          summary: outcome.summary,
        );
        final index = toolCalls.lastIndexWhere(
          (call) => call.name == toolName && call.turn == turn,
        );
        if (index >= 0) {
          toolCalls[index] = completed;
        } else {
          toolCalls.add(completed);
        }
        touchesToolCalls = true;
        activeTool = null;
        touchesActiveTool = true;
      case AgentTurnEnd(:final turn):
        status = AgentSessionStatus.idle;
        touchesStatus = true;
        activeTool = null;
        touchesActiveTool = true;
        // The turn number is zero-based, so the completed-turn count is
        // `turn + 1`; carry the prior count forward when the stream is not
        // contiguous so a late event never lowers it.
        if (turn + 1 > turns) turns = turn + 1;
        touchesTurns = true;
    }
  }

  var patch = AgentSessionDocumentPatch();
  if (touchesStatus) patch = patch.withStatus(status);
  if (touchesMessages) {
    patch = patch.withMessages(List<AgentMessage>.unmodifiable(messages));
  }
  if (touchesToolCalls) {
    patch = patch.withToolCalls(List<AgentToolCall>.unmodifiable(toolCalls));
  }
  if (touchesActiveTool) patch = patch.withActiveTool(activeTool);
  if (touchesTurns) patch = patch.withTurns(turns);
  return patch;
}