// This file is auto-generated by @hey-api/openapi-ts

import * as z from 'zod'

/**
 * AccountPayload
 */
export const zAccountPayload = z.object({
  email: z.string(),
  id: z.string(),
  name: z.string(),
})

/**
 * AppDescribeQuery
 *
 * `?fields=` allow-list for GET /apps/<id>/describe.
 *
 * Empty / omitted → all blocks. Unknown member → ValidationError → 422.
 */
export const zAppDescribeQuery = z.object({
  fields: z.string().optional(),
})

/**
 * AppDslExportQuery
 *
 * Query parameters for GET /apps/<app_id>/export.
 */
export const zAppDslExportQuery = z.object({
  include_secret: z.boolean().optional().default(false),
  workflow_id: z.string().nullish(),
})

/**
 * AppDslExportResponse
 *
 * Export DSL response.
 */
export const zAppDslExportResponse = z.object({
  data: z.string(),
})

/**
 * AppDslImportPayload
 *
 * Request body for POST /workspaces/<workspace_id>/apps/imports.
 */
export const zAppDslImportPayload = z.object({
  app_id: z.string().nullish(),
  description: z.string().nullish(),
  icon: z.string().nullish(),
  icon_background: z.string().nullish(),
  icon_type: z.string().nullish(),
  mode: z.enum(['yaml-content', 'yaml-url']),
  name: z.string().nullish(),
  yaml_content: z.string().nullish(),
  yaml_url: z.string().nullish(),
})

/**
 * AppMode
 */
export const zAppMode = z.enum([
  'advanced-chat',
  'agent',
  'agent-chat',
  'channel',
  'chat',
  'completion',
  'rag-pipeline',
  'workflow',
])

/**
 * AppListQuery
 *
 * mode is a closed enum.
 */
export const zAppListQuery = z.object({
  limit: z.int().gte(1).lte(200).optional().default(20),
  mode: zAppMode.nullish(),
  name: z.string().max(200).nullish(),
  page: z.int().gte(1).optional().default(1),
  tag: z.string().max(100).nullish(),
  workspace_id: z.string(),
})

/**
 * AppRunRequest
 */
export const zAppRunRequest = z.object({
  auto_generate_name: z.boolean().optional().default(true),
  conversation_id: z.string().nullish(),
  files: z.array(z.record(z.string(), z.unknown())).nullish(),
  inputs: z.record(z.string(), z.unknown()),
  query: z.string().nullish(),
  workflow_id: z.string().nullish(),
  workspace_id: z.string().nullish(),
})

/**
 * DeviceCodeRequest
 */
export const zDeviceCodeRequest = z.object({
  client_id: z.string(),
  device_label: z.string(),
})

/**
 * DeviceCodeResponse
 */
export const zDeviceCodeResponse = z.object({
  device_code: z.string(),
  expires_in: z.int(),
  interval: z.int(),
  user_code: z.string(),
  verification_uri: z.string(),
})

/**
 * DeviceLookupQuery
 */
export const zDeviceLookupQuery = z.object({
  user_code: z.string(),
})

/**
 * DeviceLookupResponse
 */
export const zDeviceLookupResponse = z.object({
  client_id: z.string().nullish(),
  expires_in_remaining: z.int().optional().default(0),
  valid: z.boolean(),
})

/**
 * DeviceMutateRequest
 */
export const zDeviceMutateRequest = z.object({
  user_code: z.string(),
})

/**
 * DeviceMutateResponse
 */
export const zDeviceMutateResponse = z.object({
  status: z.string(),
})

/**
 * DevicePollRequest
 */
export const zDevicePollRequest = z.object({
  client_id: z.string(),
  device_code: z.string(),
})

/**
 * ErrorDetail
 */
export const zErrorDetail = z.object({
  loc: z
    .array(z.union([z.string(), z.int()]))
    .optional()
    .default([]),
  msg: z.string(),
  type: z.string(),
})

/**
 * ErrorBody
 *
 * Canonical non-2xx body. ``code`` is typed ``str`` (not the enum) so the
 * generated client schema stays an open enum — old CLIs keep parsing when a
 * future server adds a code. Formatter tests pin emitted values to the enum.
 */
export const zErrorBody = z.object({
  code: z.string(),
  details: z.array(zErrorDetail).nullish(),
  hint: z.string().nullish(),
  message: z.string(),
  status: z.int(),
})

/**
 * EventStreamResponse
 */
export const zEventStreamResponse = z.string()

/**
 * FileResponse
 */
export const zFileResponse = z.object({
  conversation_id: z.string().nullish(),
  created_at: z.int().nullish(),
  created_by: z.string().nullish(),
  extension: z.string().nullish(),
  file_key: z.string().nullish(),
  id: z.string(),
  mime_type: z.string().nullish(),
  name: z.string(),
  original_url: z.string().nullish(),
  preview_url: z.string().nullish(),
  reference: z.string().nullish(),
  size: z.int(),
  source_url: z.string().nullish(),
  tenant_id: z.string().nullish(),
  user_id: z.string().nullish(),
})

/**
 * FormSubmitResponse
 *
 * Empty 200 body for POST /apps/<id>/form/human_input/<token>. `extra='forbid'`
 * pins `additionalProperties: false` so the generated contract is an exact `{}` rather
 * than an under-annotated open object.
 */
export const zFormSubmitResponse = z.record(z.string(), z.never())

/**
 * Github
 */
export const zGithub = z.object({
  github_plugin_unique_identifier: z.string(),
  package: z.string(),
  repo: z.string(),
  version: z.string(),
})

/**
 * HealthResponse
 *
 * Liveness payload for `GET /openapi/v1/_health` — no auth required.
 */
export const zHealthResponse = z.object({
  ok: z.boolean(),
})

/**
 * HumanInputFormDefinitionResponse
 */
export const zHumanInputFormDefinitionResponse = z.object({
  expiration_time: z.int().nullish(),
  form_content: z.string(),
  inputs: z.array(z.record(z.string(), z.unknown())).optional(),
  resolved_default_values: z.record(z.string(), z.string()),
  user_actions: z.array(z.record(z.string(), z.unknown())).optional(),
})

/**
 * ImportStatus
 */
export const zImportStatus = z.enum(['completed', 'completed-with-warnings', 'failed', 'pending'])

/**
 * Import
 */
export const zImport = z.object({
  app_id: z.string().nullish(),
  app_mode: z.string().nullish(),
  current_dsl_version: z.string().optional().default('0.6.0'),
  error: z.string().optional().default(''),
  id: z.string(),
  imported_dsl_version: z.string().optional().default(''),
  status: zImportStatus,
})

export const zJsonValue = z.unknown()

/**
 * HumanInputFormSubmitPayload
 */
export const zHumanInputFormSubmitPayload = z.object({
  action: z.string(),
  inputs: z.record(z.string(), zJsonValue),
})

/**
 * Marketplace
 */
export const zMarketplace = z.object({
  marketplace_plugin_unique_identifier: z.string(),
  version: z.string().nullish(),
})

/**
 * MemberActionResponse
 */
export const zMemberActionResponse = z.object({
  result: z.literal('success').optional().default('success'),
})

/**
 * MemberInvitePayload
 */
export const zMemberInvitePayload = z.object({
  email: z.string(),
  role: z.enum(['admin', 'normal']),
})

/**
 * MemberInviteResponse
 */
export const zMemberInviteResponse = z.object({
  email: z.string(),
  invite_url: z.string(),
  member_id: z.string(),
  result: z.literal('success').optional().default('success'),
  role: z.string(),
  tenant_id: z.string(),
})

/**
 * MemberListQuery
 *
 * Strict (extra='forbid').
 */
export const zMemberListQuery = z.object({
  limit: z.int().gte(1).lte(200).optional().default(20),
  page: z.int().gte(1).optional().default(1),
})

/**
 * MemberResponse
 */
export const zMemberResponse = z.object({
  avatar: z.string().nullish(),
  email: z.string(),
  id: z.string(),
  name: z.string(),
  role: z.string(),
  status: z.string(),
})

/**
 * MemberListResponse
 */
export const zMemberListResponse = z.object({
  data: z.array(zMemberResponse),
  has_more: z.boolean(),
  limit: z.int(),
  page: z.int(),
  total: z.int(),
})

/**
 * MemberRoleUpdatePayload
 */
export const zMemberRoleUpdatePayload = z.object({
  role: z.enum(['admin', 'normal']),
})

/**
 * OpenApiErrorCode
 */
export const zOpenApiErrorCode = z.enum([
  'app_unavailable',
  'bad_gateway',
  'bad_request',
  'completion_request_error',
  'conflict',
  'conversation_completed',
  'file_extension_blocked',
  'file_too_large',
  'filename_not_exists',
  'forbidden',
  'internal_server_error',
  'invalid_param',
  'member_license_exceeded',
  'member_limit_exceeded',
  'method_not_allowed',
  'model_currently_not_support',
  'no_file_uploaded',
  'not_acceptable',
  'not_found',
  'provider_not_initialize',
  'provider_quota_exceeded',
  'rate_limit_error',
  'request_entity_too_large',
  'too_many_files',
  'too_many_requests',
  'unauthorized',
  'unknown',
  'unsupported_file_type',
  'unsupported_media_type',
])

/**
 * Package
 */
export const zPackage = z.object({
  plugin_unique_identifier: z.string(),
  version: z.string().nullish(),
})

/**
 * PermittedExternalAppsListQuery
 *
 * Strict (extra='forbid').
 */
export const zPermittedExternalAppsListQuery = z.object({
  limit: z.int().gte(1).lte(200).optional().default(20),
  mode: zAppMode.nullish(),
  name: z.string().max(200).nullish(),
  page: z.int().gte(1).optional().default(1),
})

/**
 * RevokeResponse
 */
export const zRevokeResponse = z.object({
  status: z.string(),
})

/**
 * ServerVersionResponse
 *
 * Meta endpoint payload for `GET /openapi/v1/_version` — no auth required.
 */
export const zServerVersionResponse = z.object({
  edition: z.enum(['CLOUD', 'SELF_HOSTED']),
  version: z.string(),
})

/**
 * SessionListQuery
 *
 * Pagination for GET /account/sessions. Strict (extra='forbid').
 */
export const zSessionListQuery = z.object({
  limit: z.int().gte(1).lte(200).optional().default(100),
  page: z.int().gte(1).optional().default(1),
})

/**
 * SessionRow
 */
export const zSessionRow = z.object({
  client_id: z.string(),
  created_at: z.string().nullish(),
  device_label: z.string(),
  expires_at: z.string().nullish(),
  id: z.string(),
  last_used_at: z.string().nullish(),
  prefix: z.string(),
})

/**
 * SessionListResponse
 */
export const zSessionListResponse = z.object({
  data: z.array(zSessionRow),
  has_more: z.boolean(),
  limit: z.int(),
  page: z.int(),
  total: z.int(),
})

/**
 * TagItem
 */
export const zTagItem = z.object({
  name: z.string(),
})

/**
 * AppDescribeInfo
 */
export const zAppDescribeInfo = z.object({
  author: z.string().nullish(),
  description: z.string().nullish(),
  id: z.string(),
  is_agent: z.boolean().optional().default(false),
  mode: z.string(),
  name: z.string(),
  service_api_enabled: z.boolean(),
  tags: z.array(zTagItem).optional().default([]),
  updated_at: z.string().nullish(),
})

/**
 * AppDescribeResponse
 */
export const zAppDescribeResponse = z.object({
  info: zAppDescribeInfo.nullish(),
  input_schema: z.record(z.string(), z.unknown()).nullish(),
  parameters: z.record(z.string(), z.unknown()).nullish(),
})

/**
 * AppInfoResponse
 */
export const zAppInfoResponse = z.object({
  author: z.string().nullish(),
  description: z.string().nullish(),
  id: z.string(),
  mode: z.string(),
  name: z.string(),
  tags: z.array(zTagItem).optional().default([]),
})

/**
 * AppListRow
 */
export const zAppListRow = z.object({
  created_by_name: z.string().nullish(),
  description: z.string().nullish(),
  id: z.string(),
  mode: zAppMode,
  name: z.string(),
  tags: z.array(zTagItem).optional().default([]),
  updated_at: z.string().nullish(),
  workspace_id: z.string().nullish(),
  workspace_name: z.string().nullish(),
})

/**
 * AppListResponse
 */
export const zAppListResponse = z.object({
  data: z.array(zAppListRow),
  has_more: z.boolean(),
  limit: z.int(),
  page: z.int(),
  total: z.int(),
})

/**
 * PermittedExternalAppsListResponse
 */
export const zPermittedExternalAppsListResponse = z.object({
  data: z.array(zAppListRow),
  has_more: z.boolean(),
  limit: z.int(),
  page: z.int(),
  total: z.int(),
})

/**
 * TaskStopResponse
 *
 * 200 body for POST /apps/<id>/tasks/<task_id>/stop. The handler always returns
 * {"result": "success"}, so `result` is required (no default) — the generated contract
 * types it as a required `'success'` rather than an optional field.
 */
export const zTaskStopResponse = z.object({
  result: z.literal('success'),
})

/**
 * Type
 */
export const zType = z.enum(['github', 'marketplace', 'package'])

/**
 * PluginDependency
 */
export const zPluginDependency = z.object({
  current_identifier: z.string().nullish(),
  type: zType,
  value: z.union([zGithub, zMarketplace, zPackage]),
})

/**
 * CheckDependenciesResult
 */
export const zCheckDependenciesResult = z.object({
  leaked_dependencies: z.array(zPluginDependency).optional(),
})

/**
 * UsageInfo
 */
export const zUsageInfo = z.object({
  completion_tokens: z.int().optional().default(0),
  prompt_tokens: z.int().optional().default(0),
  total_tokens: z.int().optional().default(0),
})

/**
 * MessageMetadata
 */
export const zMessageMetadata = z.object({
  retriever_resources: z.array(z.record(z.string(), z.unknown())).optional().default([]),
  usage: zUsageInfo.nullish(),
})

/**
 * WorkflowRunData
 */
export const zWorkflowRunData = z.object({
  created_at: z.int().nullish(),
  elapsed_time: z.number().nullish(),
  error: z.string().nullish(),
  finished_at: z.int().nullish(),
  id: z.string(),
  outputs: z.record(z.string(), z.unknown()).optional(),
  status: z.string(),
  total_steps: z.int().nullish(),
  total_tokens: z.int().nullish(),
  workflow_id: z.string(),
})

/**
 * WorkspaceDetailResponse
 */
export const zWorkspaceDetailResponse = z.object({
  created_at: z.string().nullish(),
  current: z.boolean(),
  id: z.string(),
  name: z.string(),
  role: z.string(),
  status: z.string(),
})

/**
 * WorkspacePayload
 */
export const zWorkspacePayload = z.object({
  id: z.string(),
  name: z.string(),
  role: z.string(),
})

/**
 * AccountResponse
 */
export const zAccountResponse = z.object({
  account: zAccountPayload.nullish(),
  default_workspace_id: z.string().nullish(),
  subject_email: z.string().nullish(),
  subject_issuer: z.string().nullish(),
  subject_type: z.string(),
  workspaces: z.array(zWorkspacePayload).optional().default([]),
})

/**
 * DeviceTokenResponse
 */
export const zDeviceTokenResponse = z.object({
  account: zAccountPayload.nullish(),
  default_workspace_id: z.string().nullish(),
  expires_at: z.string(),
  subject_email: z.string().nullish(),
  subject_issuer: z.string().nullish(),
  subject_type: z.enum(['account', 'external_sso']),
  token: z.string(),
  token_id: z.string(),
  workspaces: z.array(zWorkspacePayload).optional().default([]),
})

/**
 * WorkspaceSummaryResponse
 */
export const zWorkspaceSummaryResponse = z.object({
  current: z.boolean(),
  id: z.string(),
  name: z.string(),
  role: z.string(),
  status: z.string(),
})

/**
 * WorkspaceListResponse
 */
export const zWorkspaceListResponse = z.object({
  workspaces: z.array(zWorkspaceSummaryResponse),
})

/**
 * Health check
 */
export const zGetHealthResponse = zHealthResponse

/**
 * Server version
 */
export const zGetVersionResponse = zServerVersionResponse

/**
 * Account info
 */
export const zGetAccountResponse = zAccountResponse

export const zGetAccountSessionsQuery = z.object({
  limit: z.int().gte(1).lte(200).optional().default(100),
  page: z.int().gte(1).optional().default(1),
})

/**
 * Session list
 */
export const zGetAccountSessionsResponse = zSessionListResponse

/**
 * Session revoked
 */
export const zDeleteAccountSessionsSelfResponse = zRevokeResponse

export const zDeleteAccountSessionsBySessionIdPath = z.object({
  session_id: z.string(),
})

/**
 * Session revoked
 */
export const zDeleteAccountSessionsBySessionIdResponse = zRevokeResponse

export const zGetAppsQuery = z.object({
  limit: z.int().gte(1).lte(200).optional().default(20),
  mode: z
    .enum([
      'advanced-chat',
      'agent',
      'agent-chat',
      'channel',
      'chat',
      'completion',
      'rag-pipeline',
      'workflow',
    ])
    .optional(),
  name: z.string().max(200).optional(),
  page: z.int().gte(1).optional().default(1),
  tag: z.string().max(100).optional(),
  workspace_id: z.string(),
})

/**
 * App list
 */
export const zGetAppsResponse = zAppListResponse

export const zGetAppsByAppIdCheckDependenciesPath = z.object({
  app_id: z.string(),
})

/**
 * Dependencies checked
 */
export const zGetAppsByAppIdCheckDependenciesResponse = zCheckDependenciesResult

export const zGetAppsByAppIdDescribePath = z.object({
  app_id: z.string(),
})

export const zGetAppsByAppIdDescribeQuery = z.object({
  fields: z.string().optional(),
})

/**
 * App description
 */
export const zGetAppsByAppIdDescribeResponse = zAppDescribeResponse

export const zGetAppsByAppIdExportPath = z.object({
  app_id: z.string(),
})

export const zGetAppsByAppIdExportQuery = z.object({
  include_secret: z.boolean().optional().default(false),
  workflow_id: z.string().optional(),
})

/**
 * Export successful
 */
export const zGetAppsByAppIdExportResponse = zAppDslExportResponse

export const zPostAppsByAppIdFilesUploadPath = z.object({
  app_id: z.string(),
})

/**
 * File uploaded successfully
 */
export const zPostAppsByAppIdFilesUploadResponse = zFileResponse

export const zGetAppsByAppIdFormHumanInputByFormTokenPath = z.object({
  app_id: z.string(),
  form_token: z.string(),
})

/**
 * Form definition
 */
export const zGetAppsByAppIdFormHumanInputByFormTokenResponse = zHumanInputFormDefinitionResponse

export const zPostAppsByAppIdFormHumanInputByFormTokenBody = zHumanInputFormSubmitPayload

export const zPostAppsByAppIdFormHumanInputByFormTokenPath = z.object({
  app_id: z.string(),
  form_token: z.string(),
})

/**
 * Form submitted
 */
export const zPostAppsByAppIdFormHumanInputByFormTokenResponse = zFormSubmitResponse

export const zPostAppsByAppIdRunBody = zAppRunRequest

export const zPostAppsByAppIdRunPath = z.object({
  app_id: z.string(),
})

/**
 * Run result (SSE stream)
 */
export const zPostAppsByAppIdRunResponse = zEventStreamResponse

export const zGetAppsByAppIdTasksByTaskIdEventsPath = z.object({
  app_id: z.string(),
  task_id: z.string(),
})

export const zGetAppsByAppIdTasksByTaskIdEventsQuery = z.object({
  continue_on_pause: z.boolean().optional().default(false),
  include_state_snapshot: z.boolean().optional().default(false),
})

/**
 * SSE event stream
 */
export const zGetAppsByAppIdTasksByTaskIdEventsResponse = zEventStreamResponse

export const zPostAppsByAppIdTasksByTaskIdStopPath = z.object({
  app_id: z.string(),
  task_id: z.string(),
})

/**
 * Task stopped
 */
export const zPostAppsByAppIdTasksByTaskIdStopResponse = zTaskStopResponse

export const zPostOauthDeviceApproveBody = zDeviceMutateRequest

/**
 * Approved
 */
export const zPostOauthDeviceApproveResponse = zDeviceMutateResponse

export const zPostOauthDeviceCodeBody = zDeviceCodeRequest

/**
 * Device code created
 */
export const zPostOauthDeviceCodeResponse = zDeviceCodeResponse

export const zPostOauthDeviceDenyBody = zDeviceMutateRequest

/**
 * Denied
 */
export const zPostOauthDeviceDenyResponse = zDeviceMutateResponse

export const zGetOauthDeviceLookupQuery = z.object({
  user_code: z.string(),
})

/**
 * Device lookup result
 */
export const zGetOauthDeviceLookupResponse = zDeviceLookupResponse

export const zPostOauthDeviceTokenBody = zDevicePollRequest

/**
 * Device token
 */
export const zPostOauthDeviceTokenResponse = zDeviceTokenResponse

export const zGetPermittedExternalAppsQuery = z.object({
  limit: z.int().gte(1).lte(200).optional().default(20),
  mode: z
    .enum([
      'advanced-chat',
      'agent',
      'agent-chat',
      'channel',
      'chat',
      'completion',
      'rag-pipeline',
      'workflow',
    ])
    .optional(),
  name: z.string().max(200).optional(),
  page: z.int().gte(1).optional().default(1),
})

/**
 * Permitted external apps list
 */
export const zGetPermittedExternalAppsResponse = zPermittedExternalAppsListResponse

/**
 * Workspace list
 */
export const zGetWorkspacesResponse = zWorkspaceListResponse

export const zGetWorkspacesByWorkspaceIdPath = z.object({
  workspace_id: z.string(),
})

/**
 * Workspace detail
 */
export const zGetWorkspacesByWorkspaceIdResponse = zWorkspaceDetailResponse

export const zPostWorkspacesByWorkspaceIdAppsImportsBody = zAppDslImportPayload

export const zPostWorkspacesByWorkspaceIdAppsImportsPath = z.object({
  workspace_id: z.string(),
})

/**
 * Import completed
 */
export const zPostWorkspacesByWorkspaceIdAppsImportsResponse = zImport

export const zPostWorkspacesByWorkspaceIdAppsImportsByImportIdConfirmPath = z.object({
  import_id: z.string(),
  workspace_id: z.string(),
})

/**
 * Import confirmed
 */
export const zPostWorkspacesByWorkspaceIdAppsImportsByImportIdConfirmResponse = zImport

export const zGetWorkspacesByWorkspaceIdMembersPath = z.object({
  workspace_id: z.string(),
})

export const zGetWorkspacesByWorkspaceIdMembersQuery = z.object({
  limit: z.int().gte(1).lte(200).optional().default(20),
  page: z.int().gte(1).optional().default(1),
})

/**
 * Member list
 */
export const zGetWorkspacesByWorkspaceIdMembersResponse = zMemberListResponse

export const zPostWorkspacesByWorkspaceIdMembersBody = zMemberInvitePayload

export const zPostWorkspacesByWorkspaceIdMembersPath = z.object({
  workspace_id: z.string(),
})

/**
 * Member invited
 */
export const zPostWorkspacesByWorkspaceIdMembersResponse = zMemberInviteResponse

export const zDeleteWorkspacesByWorkspaceIdMembersByMemberIdPath = z.object({
  member_id: z.string(),
  workspace_id: z.string(),
})

/**
 * Member removed
 */
export const zDeleteWorkspacesByWorkspaceIdMembersByMemberIdResponse = zMemberActionResponse

export const zPutWorkspacesByWorkspaceIdMembersByMemberIdRoleBody = zMemberRoleUpdatePayload

export const zPutWorkspacesByWorkspaceIdMembersByMemberIdRolePath = z.object({
  member_id: z.string(),
  workspace_id: z.string(),
})

/**
 * Role updated
 */
export const zPutWorkspacesByWorkspaceIdMembersByMemberIdRoleResponse = zMemberActionResponse

export const zPostWorkspacesByWorkspaceIdSwitchPath = z.object({
  workspace_id: z.string(),
})

/**
 * Workspace detail
 */
export const zPostWorkspacesByWorkspaceIdSwitchResponse = zWorkspaceDetailResponse
