All files / shared/src/types/api/routes/batch-api response.ts

100% Statements 63/63
100% Branches 1/1
100% Functions 0/0
100% Lines 63/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 1681x         1x 1x 1x 1x 1x 1x 1x 1x 1x           1x   1x   1x   1x 1x             1x   1x   1x   1x   1x 1x             1x   1x   1x 1x             1x   1x   1x   1x 1x             1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x 1x             1x   1x   1x   1x   1x   1x 1x             1x             1x                 1x             1x      
import { z } from 'zod';
 
/**
 * The status of a batch request.
 */
export enum BatchStatus {
  VALIDATING = 'validating',
  FAILED = 'failed',
  IN_PROGRESS = 'in_progress',
  FINALIZING = 'finalizing',
  COMPLETED = 'completed',
  EXPIRED = 'expired',
  CANCELLING = 'cancelling',
  CANCELLED = 'cancelled',
}
 
/**
 * Usage statistics for the batch request.
 */
export const BatchUsage = z.object({
  /** Number of tokens in the prompt. */
  prompt_tokens: z.number(),
  /** Number of tokens in the generated completion. */
  completion_tokens: z.number(),
  /** Total number of tokens used in the request (prompt + completion). */
  total_tokens: z.number(),
});
 
export type BatchUsage = z.infer<typeof BatchUsage>;
 
/**
 * Error information for a batch request.
 */
export const BatchError = z.object({
  /** Machine-readable error code. */
  code: z.string(),
  /** Human-readable error message. */
  message: z.string(),
  /** The parameter that caused the error, if applicable. */
  param: z.string().nullable().optional(),
  /** The type of error. */
  type: z.string().optional(),
});
 
export type BatchError = z.infer<typeof BatchError>;
 
/**
 * List of errors for the batch object.
 */
export const BatchErrors = z.object({
  /** The object type, which is always "list". */
  object: z.literal('list'),
  /** Array of error objects. */
  data: z.array(BatchError),
});
 
export type BatchErrors = z.infer<typeof BatchErrors>;
 
/**
 * Request counts for the batch.
 */
export const BatchRequestCounts = z.object({
  /** Total number of requests in the batch. */
  total: z.number(),
  /** Number of requests that have been completed successfully. */
  completed: z.number(),
  /** Number of requests that have failed. */
  failed: z.number(),
});
 
export type BatchRequestCounts = z.infer<typeof BatchRequestCounts>;
 
/**
 * A batch object representing a collection of API requests.
 */
export const Batch = z.object({
  /** The object identifier, which can be referenced in the API endpoints. */
  id: z.string(),
  /** The object type, which is always "batch". */
  object: z.literal('batch'),
  /** The endpoint to be used for all requests in the batch. */
  endpoint: z.string(),
  /** A list of errors that occurred during the processing of the batch. */
  errors: BatchErrors.optional(),
  /** The ID of the input file for the batch. */
  input_file_id: z.string(),
  /** The time frame within which the batch should be processed. */
  completion_window: z.string().nullable().optional(),
  /** The current status of the batch. */
  status: z.enum(BatchStatus),
  /** The ID of the file containing the outputs of successfully executed requests. */
  output_file_id: z.string().nullable().optional(),
  /** The ID of the file containing the outputs of requests with errors. */
  error_file_id: z.string().nullable().optional(),
  /** The Unix timestamp (in seconds) for when the batch was created. */
  created_at: z.number(),
  /** The Unix timestamp (in seconds) for when the batch started processing. */
  in_progress_at: z.number().nullable().optional(),
  /** The Unix timestamp (in seconds) for when the batch expires. */
  expires_at: z.number().nullable().optional(),
  /** The Unix timestamp (in seconds) for when the batch started finalizing. */
  finalizing_at: z.number().nullable().optional(),
  /** The Unix timestamp (in seconds) for when the batch was completed. */
  completed_at: z.number().nullable().optional(),
  /** The Unix timestamp (in seconds) for when the batch failed. */
  failed_at: z.number().nullable().optional(),
  /** The Unix timestamp (in seconds) for when the batch expired. */
  expired_at: z.number().nullable().optional(),
  /** The Unix timestamp (in seconds) for when the batch started cancelling. */
  cancelling_at: z.number().nullable().optional(),
  /** The Unix timestamp (in seconds) for when the batch was cancelled. */
  cancelled_at: z.number().nullable().optional(),
  /** The request counts for the batch. */
  request_counts: BatchRequestCounts,
  /** Set of 16 key-value pairs that can be attached to an object. */
  metadata: z.record(z.string(), z.string()).nullable().optional(),
});
 
export type Batch = z.infer<typeof Batch>;
 
/**
 * List of batch objects.
 */
export const BatchList = z.object({
  /** The object type, which is always "list". */
  object: z.literal('list'),
  /** Array of batch objects. */
  data: z.array(Batch),
  /** The ID of the first item in the list. */
  first_id: z.string().optional(),
  /** The ID of the last item in the list. */
  last_id: z.string().optional(),
  /** True if there are more items available. */
  has_more: z.boolean(),
});
 
export type BatchList = z.infer<typeof BatchList>;
 
/**
 * Response for creating a batch.
 */
export const CreateBatchResponseBody = Batch;
 
export type CreateBatchResponseBody = z.infer<typeof CreateBatchResponseBody>;
 
/**
 * Response for retrieving a batch.
 */
export const RetrieveBatchResponseBody = Batch;
 
export type RetrieveBatchResponseBody = z.infer<
  typeof RetrieveBatchResponseBody
>;
 
/**
 * Response for listing batches.
 */
export const ListBatchesResponseBody = BatchList;
 
export type ListBatchesResponseBody = z.infer<typeof ListBatchesResponseBody>;
 
/**
 * Response for cancelling a batch.
 */
export const CancelBatchResponseBody = Batch;
 
export type CancelBatchResponseBody = z.infer<typeof CancelBatchResponseBody>;