// src/chat/types.ts
function createChatId() {
  return crypto.randomUUID();
}

// src/chat/accumulator.ts
var MessageAccumulator = class {
  messageId;
  parts = [];
  status = "pending";
  currentTextPart = null;
  currentReasoningPart = null;
  toolCallParts = /* @__PURE__ */ new Map();
  _finalized = false;
  constructor(messageId) {
    this.messageId = messageId ?? createChatId();
  }
  /** Get current message ID */
  get id() {
    return this.messageId;
  }
  /**
   * Apply an AgentEvent to accumulate into the message
   * @param event - AgentEvent to process
   * @throws Error if accumulator is already finalized
   */
  apply(event) {
    if (this._finalized) throw new Error("Cannot apply events to finalized accumulator");
    if (this.status === "pending") {
      this.status = "streaming";
    }
    switch (event.type) {
      case "text_delta":
        this.handleTextDelta(event.text);
        break;
      case "thinking_start":
        this.finalizeCurrentText();
        this.currentReasoningPart = { type: "reasoning", text: "", status: "streaming" };
        this.parts.push(this.currentReasoningPart);
        break;
      case "thinking_delta":
        if (this.currentReasoningPart) {
          this.currentReasoningPart.text += event.text;
        }
        break;
      case "thinking_end":
        if (this.currentReasoningPart) {
          this.currentReasoningPart.status = "complete";
          this.currentReasoningPart = null;
        }
        break;
      case "tool_call_start": {
        this.finalizeCurrentText();
        const toolPart = {
          type: "tool_call",
          toolCallId: event.toolCallId,
          name: event.toolName,
          args: event.args,
          status: "running"
        };
        this.toolCallParts.set(event.toolCallId, toolPart);
        this.parts.push(toolPart);
        break;
      }
      case "tool_call_end": {
        const existing = this.toolCallParts.get(event.toolCallId);
        if (existing) {
          existing.result = event.result;
          existing.status = "complete";
        }
        break;
      }
      case "error":
        this.status = "error";
        break;
    }
  }
  handleTextDelta(text) {
    if (!this.currentTextPart) {
      this.currentTextPart = { type: "text", text: "", status: "streaming" };
      this.parts.push(this.currentTextPart);
    }
    this.currentTextPart.text += text;
  }
  finalizeCurrentText() {
    if (this.currentTextPart) {
      this.currentTextPart.status = "complete";
      this.currentTextPart = null;
    }
  }
  /**
   * Get a snapshot of the current accumulated message (for streaming UI)
   * @returns ChatMessage with current parts and "streaming" status
   */
  snapshot() {
    const now = (/* @__PURE__ */ new Date()).toISOString();
    return {
      id: this.messageId,
      role: "assistant",
      parts: this.parts.map((p) => ({ ...p })),
      status: this.status === "pending" ? "pending" : "streaming",
      createdAt: now,
      updatedAt: now
    };
  }
  /**
   * Finalize the accumulator and return the complete ChatMessage
   * @returns Completed ChatMessage with all parts finalized
   * @throws Error if accumulator is already finalized
   */
  finalize() {
    if (this._finalized) throw new Error("Accumulator already finalized");
    this._finalized = true;
    this.finalizeCurrentText();
    if (this.currentReasoningPart) {
      this.currentReasoningPart.status = "complete";
      this.currentReasoningPart = null;
    }
    for (const [, toolPart] of this.toolCallParts) {
      if (toolPart.status === "running" || toolPart.status === "pending") {
        toolPart.status = "error";
      }
    }
    if (this.status !== "error" && this.status !== "cancelled") {
      this.status = "complete";
    }
    const now = (/* @__PURE__ */ new Date()).toISOString();
    return {
      id: this.messageId,
      role: "assistant",
      parts: this.parts,
      status: this.status,
      createdAt: now,
      updatedAt: now
    };
  }
  /** Check if the accumulator has been finalized */
  get finalized() {
    return this._finalized;
  }
};

export { MessageAccumulator };
//# sourceMappingURL=accumulator.js.map
//# sourceMappingURL=accumulator.js.map