import { ErrorEvent, OperationEvent } from "./agent-operations.js";
import { SummaryEvent } from "@inkeep/agents-core";

//#region src/domains/run/utils/stream-helpers.d.ts
interface StreamHelper {
  writeRole(role?: string): Promise<void>;
  writeContent(content: string): Promise<void>;
  streamData(data: any): Promise<void>;
  streamText(text: string, delayMs?: number): Promise<void>;
  writeError(error: string | ErrorEvent): Promise<void>;
  complete(): Promise<void>;
  writeData(type: string, data: any): Promise<void>;
  writeOperation(operation: OperationEvent): Promise<void>;
  writeSummary(summary: SummaryEvent): Promise<void>;
  writeToolInputStart(params: {
    toolCallId: string;
    toolName: string;
  }): Promise<void>;
  writeToolInputDelta(params: {
    toolCallId: string;
    inputTextDelta: string;
  }): Promise<void>;
  writeToolInputAvailable(params: {
    toolCallId: string;
    toolName: string;
    input: any;
    providerMetadata?: any;
  }): Promise<void>;
  writeToolOutputAvailable(params: {
    toolCallId: string;
    output: any;
  }): Promise<void>;
  writeToolOutputError(params: {
    toolCallId: string;
    errorText: string;
    output?: any;
  }): Promise<void>;
  writeToolApprovalRequest(params: {
    approvalId: string;
    toolCallId: string;
  }): Promise<void>;
  writeToolOutputDenied(params: {
    toolCallId: string;
  }): Promise<void>;
}
interface HonoSSEStream {
  writeSSE(message: {
    data: string;
    event?: string;
    id?: string;
  }): Promise<void>;
  sleep(ms: number): Promise<unknown>;
}
interface ChatCompletionChunk {
  id: string;
  object: string;
  created: number;
  model: string;
  choices: Array<{
    index: number;
    delta: {
      role?: string;
      content?: string;
      tool_calls?: Array<{
        index: number;
        id: string | null;
        type: 'function' | null;
        function: {
          name: string | null;
          arguments: string;
        };
      }>;
    };
    finish_reason: string | null;
  }>;
}
declare class SSEStreamHelper implements StreamHelper {
  private stream;
  private requestId;
  private timestamp;
  private isTextStreaming;
  private queuedEvents;
  private toolCallIndexById;
  constructor(stream: HonoSSEStream, requestId: string, timestamp: number);
  /**
   * Write the initial role message
   */
  writeRole(role?: string): Promise<void>;
  /**
   * Write content chunk
   */
  writeContent(content: string): Promise<void>;
  private getToolIndex;
  private writeToolCallsDelta;
  /**
   * Stream text word by word with optional delay
   */
  streamText(text: string, delayMs?: number): Promise<void>;
  streamData(data: any): Promise<void>;
  /**
   * Write error message or error event
   */
  writeError(error: string | ErrorEvent): Promise<void>;
  /**
   * Write the final completion message
   */
  writeCompletion(finishReason?: string): Promise<void>;
  writeData(type: string, data: any): Promise<void>;
  writeToolInputStart(params: {
    toolCallId: string;
    toolName: string;
  }): Promise<void>;
  writeToolInputDelta(params: {
    toolCallId: string;
    inputTextDelta: string;
  }): Promise<void>;
  writeToolInputAvailable(params: {
    toolCallId: string;
    toolName: string;
    input: any;
    providerMetadata?: any;
  }): Promise<void>;
  writeToolOutputAvailable(params: {
    toolCallId: string;
    output: any;
  }): Promise<void>;
  writeToolOutputError(params: {
    toolCallId: string;
    errorText: string;
    output?: any;
  }): Promise<void>;
  writeToolApprovalRequest(params: {
    approvalId: string;
    toolCallId: string;
  }): Promise<void>;
  writeToolOutputDenied(params: {
    toolCallId: string;
  }): Promise<void>;
  writeSummary(summary: SummaryEvent): Promise<void>;
  writeOperation(operation: OperationEvent): Promise<void>;
  /**
   * Flush all queued operations in order after text streaming completes
   */
  private flushQueuedOperations;
  /**
   * Write the final [DONE] message
   */
  writeDone(): Promise<void>;
  /**
   * Complete the stream with finish reason and done message
   */
  complete(finishReason?: string): Promise<void>;
}
/**
 * Factory function to create SSE stream helper
 */
declare function createSSEStreamHelper(stream: HonoSSEStream, requestId: string, timestamp: number): SSEStreamHelper;
interface VercelUIWriter {
  write(chunk: any): void;
  merge(stream: any): void;
  onError?: (error: Error) => void;
}
declare class VercelDataStreamHelper implements StreamHelper {
  private writer;
  private textId;
  private jsonBuffer;
  private sentItems;
  private completedItems;
  private static readonly MAX_BUFFER_SIZE;
  private isCompleted;
  private isTextStreaming;
  private queuedEvents;
  private lastTextEndTimestamp;
  private connectionDropTimer?;
  constructor(writer: VercelUIWriter);
  setSessionId(_sessionId: string): void;
  writeRole(_?: string): Promise<void>;
  writeContent(content: string): Promise<void>;
  streamText(text: string, delayMs?: number): Promise<void>;
  writeData(type: string, data: any): Promise<void>;
  writeError(error: string | ErrorEvent): Promise<void>;
  writeToolInputStart(params: {
    toolCallId: string;
    toolName: string;
  }): Promise<void>;
  writeToolInputDelta(params: {
    toolCallId: string;
    inputTextDelta: string;
  }): Promise<void>;
  writeToolInputAvailable(params: {
    toolCallId: string;
    toolName: string;
    input: any;
    providerMetadata?: any;
  }): Promise<void>;
  writeToolOutputAvailable(params: {
    toolCallId: string;
    output: any;
  }): Promise<void>;
  writeToolOutputError(params: {
    toolCallId: string;
    errorText: string;
    output?: any;
  }): Promise<void>;
  writeToolApprovalRequest(params: {
    approvalId: string;
    toolCallId: string;
  }): Promise<void>;
  writeToolOutputDenied(params: {
    toolCallId: string;
  }): Promise<void>;
  streamData(data: any): Promise<void>;
  mergeStream(stream: any): Promise<void>;
  /**
   * Clean up all memory allocations
   * Should be called when the stream helper is no longer needed
   */
  cleanup(): void;
  /**
   * JSON-aware buffer truncation that preserves complete JSON structures
   */
  private truncateJsonBufferSafely;
  /**
   * Reindex sent items after buffer truncation
   */
  private reindexSentItems;
  /**
   * Force cleanup on connection drop or timeout
   */
  private forceCleanup;
  /**
   * Check if the stream has been completed and cleaned up
   */
  isStreamCompleted(): boolean;
  /**
   * Get current memory usage stats (for debugging/monitoring)
   */
  getMemoryStats(): {
    bufferSize: number;
    sentItemsCount: number;
    completedItemsCount: number;
    isCompleted: boolean;
  };
  writeSummary(summary: SummaryEvent): Promise<void>;
  writeOperation(operation: OperationEvent): Promise<void>;
  /**
   * Flush all queued operations in order after text streaming completes
   */
  private flushQueuedOperations;
  writeCompletion(_finishReason?: string): Promise<void>;
  writeDone(): Promise<void>;
  /**
   * Complete the stream and clean up all memory
   * This is the primary cleanup point to prevent memory leaks between requests
   */
  complete(): Promise<void>;
}
declare function createVercelStreamHelper(writer: VercelUIWriter): VercelDataStreamHelper;
/**
 * Buffering Stream Helper that captures content instead of streaming
 * Used for MCP tool responses and non-streaming API responses that require a single complete message
 */
declare class BufferingStreamHelper implements StreamHelper {
  private capturedText;
  private capturedData;
  private capturedOperations;
  private capturedSummaries;
  private hasError;
  private errorMessage;
  setSessionId(_sessionId: string): void;
  writeRole(_role?: string): Promise<void>;
  writeContent(content: string): Promise<void>;
  streamText(text: string, _delayMs?: number): Promise<void>;
  streamData(data: any): Promise<void>;
  streamSummary(summary: SummaryEvent): Promise<void>;
  streamOperation(operation: OperationEvent): Promise<void>;
  writeData(_type: string, data: any): Promise<void>;
  writeSummary(summary: SummaryEvent): Promise<void>;
  writeOperation(operation: OperationEvent): Promise<void>;
  writeError(error: string | ErrorEvent): Promise<void>;
  writeToolInputStart(params: {
    toolCallId: string;
    toolName: string;
  }): Promise<void>;
  writeToolInputDelta(params: {
    toolCallId: string;
    inputTextDelta: string;
  }): Promise<void>;
  writeToolInputAvailable(params: {
    toolCallId: string;
    toolName: string;
    input: any;
    providerMetadata?: any;
  }): Promise<void>;
  writeToolOutputAvailable(params: {
    toolCallId: string;
    output: any;
  }): Promise<void>;
  writeToolOutputError(params: {
    toolCallId: string;
    errorText: string;
    output?: any;
  }): Promise<void>;
  writeToolApprovalRequest(params: {
    approvalId: string;
    toolCallId: string;
  }): Promise<void>;
  writeToolOutputDenied(params: {
    toolCallId: string;
  }): Promise<void>;
  complete(): Promise<void>;
  /**
   * Get the captured response for non-streaming output
   */
  getCapturedResponse(): {
    text: string;
    data: any[];
    operations: OperationEvent[];
    hasError: boolean;
    errorMessage: string;
  };
}
declare function createBufferingStreamHelper(): BufferingStreamHelper;
declare const createMCPStreamHelper: typeof createBufferingStreamHelper;
type MCPStreamHelper = BufferingStreamHelper;
//#endregion
export { BufferingStreamHelper, ChatCompletionChunk, HonoSSEStream, MCPStreamHelper, SSEStreamHelper, StreamHelper, VercelDataStreamHelper, VercelUIWriter, createBufferingStreamHelper, createMCPStreamHelper, createSSEStreamHelper, createVercelStreamHelper };