import { z } from "@hono/zod-openapi";

//#region src/validation/stream-event-schemas.ts
/**
* Vercel AI SDK Data Stream Protocol Event Schemas
*
* These schemas define the structure of events sent by the Inkeep Agents Run API.
* They are used by both the streaming backend and AI SDK providers to ensure type safety.
*/
/**
* Marks the beginning of a text stream
*/
const TextStartEventSchema = z.object({
	type: z.literal("text-start"),
	id: z.string()
});
/**
* Represents a chunk of streaming text
*/
const TextDeltaEventSchema = z.object({
	type: z.literal("text-delta"),
	id: z.string(),
	delta: z.string()
});
/**
* Marks the end of a text stream
*/
const TextEndEventSchema = z.object({
	type: z.literal("text-end"),
	id: z.string()
});
/**
* Data component event - structured data in the stream
* Used for artifacts, visualizations, or other structured outputs
*/
const DataComponentStreamEventSchema = z.object({
	type: z.literal("data-component"),
	id: z.string(),
	data: z.any()
});
/**
* Data operation event - agent operations and state changes
* Wraps operation events (agent_initializing, completion, etc.)
*/
const DataOperationStreamEventSchema = z.object({
	type: z.literal("data-operation"),
	data: z.any()
});
/**
* Data summary event - progress summaries and status updates
*/
const DataSummaryStreamEventSchema = z.object({
	type: z.literal("data-summary"),
	data: z.any()
});
/**
* Stream error event
*/
const StreamErrorEventSchema = z.object({
	type: z.literal("error"),
	error: z.string()
});
/**
* Stream finish event with usage statistics
*/
const StreamFinishEventSchema = z.object({
	type: z.literal("finish"),
	finishReason: z.string().optional(),
	usage: z.object({
		promptTokens: z.number().optional(),
		completionTokens: z.number().optional(),
		totalTokens: z.number().optional()
	}).optional()
});
/**
* Tool input start event - marks beginning of tool input streaming
*/
const ToolInputStartEventSchema = z.object({
	type: z.literal("tool-input-start"),
	toolCallId: z.string(),
	toolName: z.string()
});
/**
* Tool input delta event - streaming chunks of tool input
*/
const ToolInputDeltaEventSchema = z.object({
	type: z.literal("tool-input-delta"),
	toolCallId: z.string(),
	inputTextDelta: z.string()
});
/**
* Tool input available event - complete tool input is available
*/
const ToolInputAvailableEventSchema = z.object({
	type: z.literal("tool-input-available"),
	toolCallId: z.string(),
	toolName: z.string(),
	input: z.any(),
	providerMetadata: z.any().optional()
});
/**
* Tool output available event - tool execution result
*/
const ToolOutputAvailableEventSchema = z.object({
	type: z.literal("tool-output-available"),
	toolCallId: z.string(),
	output: z.any()
});
/**
* Tool output error event - tool execution failed
*/
const ToolOutputErrorEventSchema = z.object({
	type: z.literal("tool-output-error"),
	toolCallId: z.string(),
	errorText: z.string()
});
/**
* Tool approval request event - requesting user approval for tool execution
*/
const ToolApprovalRequestEventSchema = z.object({
	type: z.literal("tool-approval-request"),
	approvalId: z.string(),
	toolCallId: z.string(),
	toolName: z.string().optional(),
	input: z.record(z.string(), z.unknown()).optional()
});
/**
* Tool output denied event - tool execution was denied by user
*/
const ToolOutputDeniedEventSchema = z.object({
	type: z.literal("tool-output-denied"),
	toolCallId: z.string()
});
/**
* Union of all stream event types
* This is the main schema used for validating incoming stream events
*/
const StreamEventSchema = z.discriminatedUnion("type", [
	TextStartEventSchema,
	TextDeltaEventSchema,
	TextEndEventSchema,
	DataComponentStreamEventSchema,
	DataOperationStreamEventSchema,
	DataSummaryStreamEventSchema,
	StreamErrorEventSchema,
	StreamFinishEventSchema,
	ToolInputStartEventSchema,
	ToolInputDeltaEventSchema,
	ToolInputAvailableEventSchema,
	ToolOutputAvailableEventSchema,
	ToolOutputErrorEventSchema,
	ToolApprovalRequestEventSchema,
	ToolOutputDeniedEventSchema
]);

//#endregion
export { DataComponentStreamEventSchema, DataOperationStreamEventSchema, DataSummaryStreamEventSchema, StreamErrorEventSchema, StreamEventSchema, StreamFinishEventSchema, TextDeltaEventSchema, TextEndEventSchema, TextStartEventSchema, ToolApprovalRequestEventSchema, ToolInputAvailableEventSchema, ToolInputDeltaEventSchema, ToolInputStartEventSchema, ToolOutputAvailableEventSchema, ToolOutputDeniedEventSchema, ToolOutputErrorEventSchema };