// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from '../../../core/resource'; import * as MaterialsAPI from './materials'; import { APIPromise } from '../../../core/api-promise'; import { toFile, type Uploadable } from '../../../core/uploads'; import { RequestOptions } from '../../../internal/request-options'; import { multipartFormRequestOptions } from '../../../internal/uploads'; export class Upload extends APIResource { /** * Complete upload after using presigned URL * * @example * ```ts * const materialResponse = * await client.v1.materials.upload.completeUpload({ * materialId: 'materialId', * s3Key: 's3Key', * }); * ``` */ completeUpload( body: UploadCompleteUploadParams, options?: RequestOptions, ): APIPromise { return this._client.post('/api/v1/materials/upload/complete', { body, ...options }); } /** * Get presigned URL for direct S3 upload * * @example * ```ts * const response = * await client.v1.materials.upload.getPresignedURL({ * contentType: 'application/pdf', * filename: 'document.pdf', * name: 'Chapter 1 Notes', * }); * ``` */ getPresignedURL( body: UploadGetPresignedURLParams, options?: RequestOptions, ): APIPromise { return this._client.post('/api/v1/materials/upload/presigned-url', { body, ...options }); } /** * Upload a material file * * @example * ```ts * const materialResponse = * await client.v1.materials.upload.uploadFile({ * file: fs.createReadStream('path/to/file'), * name: 'name', * }); * ``` */ uploadFile( body: UploadUploadFileParams, options?: RequestOptions, ): APIPromise { return this._client.post( '/api/v1/materials/upload', multipartUploadRequestOptions(body, options, this._client), ); } /** * Uploads a file and waits for processing to finish before returning. Useful for * synchronous API usage. * * @example * ```ts * const materialResponse = * await client.v1.materials.upload.uploadFileAndProcess({ * file: fs.createReadStream('path/to/file'), * name: 'name', * }); * ``` */ uploadFileAndProcess( body: UploadUploadFileAndProcessParams, options?: RequestOptions, ): APIPromise { return this._client.post( '/api/v1/materials/upload/file-and-process', multipartUploadRequestOptions(body, options, this._client), ); } /** * Upload material from URL * * @example * ```ts * const materialResponse = * await client.v1.materials.upload.uploadFromURL({ * name: 'name', * url: 'url', * }); * ``` */ uploadFromURL( body: UploadUploadFromURLParams, options?: RequestOptions, ): APIPromise { return this._client.post('/api/v1/materials/upload/url', { body, ...options }); } /** * Fetches content from URL and waits for processing to finish before returning. * Useful for synchronous API usage. * * @example * ```ts * const materialResponse = * await client.v1.materials.upload.uploadFromURLAndProcess({ * name: 'My Document', * url: 'https://example.com/document.pdf', * }); * ``` */ uploadFromURLAndProcess( body: UploadUploadFromURLAndProcessParams, options?: RequestOptions, ): APIPromise { return this._client.post('/api/v1/materials/upload/url-and-process', { body, ...options }); } } export interface UploadGetPresignedURLResponse { /** * Material ID to use for completion */ materialId: string; /** * S3 key for the file */ s3Key: string; /** * Presigned URL for direct S3 upload */ uploadUrl: string; } export interface UploadCompleteUploadParams { /** * Material ID from presigned URL response */ materialId: string; /** * S3 key from presigned URL response */ s3Key: string; } export interface UploadGetPresignedURLParams { /** * MIME type of the file */ contentType: string; /** * Filename to upload */ filename: string; /** * Display name for the material */ name: string; /** * Whether to extract images from files */ extractImages?: boolean; /** * Folder ID to place the material in */ folderId?: string; /** * References that this material cites */ references?: Array; } export interface UploadUploadFileParams { file: Uploadable; /** * Material name */ name: string; /** * Content-Type/MIME type of the file (e.g., video/mp4, application/pdf). * If not provided, the uploadable's existing type is used. */ contentType?: string; /** * Whether to extract images from files (true/false, default: true) */ extractImages?: string; /** * Folder ID (optional). If folderPath is provided, folderId will be ignored. */ folderId?: string; /** * Relative folder path (e.g., "folder1/folder2"). Folders will be created if * they don't exist. The filename can be included and will be extracted * automatically. */ folderPath?: string; /** * JSON string of references array (optional) */ references?: string; } export interface UploadUploadFileAndProcessParams { file: Uploadable; /** * Material name */ name: string; /** * Content-Type/MIME type of the file (e.g., video/mp4, application/pdf). * If not provided, the uploadable's existing type is used. */ contentType?: string; /** * Whether to extract images from files (true/false, default: true) */ extractImages?: string; /** * Folder ID (optional). If folderPath is provided, folderId will be ignored. */ folderId?: string; /** * Relative folder path (e.g., "folder1/folder2"). Folders will be created if * they don't exist. The filename can be included and will be extracted * automatically. */ folderPath?: string; /** * Polling interval in milliseconds (default: 2000) */ pollIntervalMs?: number; /** * Processing timeout in milliseconds (default: 300000 - 5 minutes) */ timeoutMs?: number; } export interface UploadUploadFromURLParams { /** * Material name */ name: string; /** * URL to fetch content from */ url: string; /** * Folder ID (optional). If folderPath is provided, folderId will be ignored. */ folderId?: string; /** * Relative folder path (e.g., "folder1/folder2"). Folders will be created if * they don't exist. The filename can be included and will be extracted * automatically. */ folderPath?: string; } export interface UploadUploadFromURLAndProcessParams { /** * Material name */ name: string; /** * URL to fetch content from */ url: string; /** * Folder ID (optional). If folderPath is provided, folderId will be ignored. */ folderId?: string; /** * Relative folder path (e.g., "folder1/folder2"). Folders will be created if * they don't exist. The filename can be included and will be extracted * automatically. */ folderPath?: string; /** * Polling interval in milliseconds (default: 2 seconds) */ pollIntervalMs?: number; /** * References that this material cites */ references?: Array; /** * Maximum time to wait for processing in milliseconds (default: 5 minutes) */ timeoutMs?: number; } export declare namespace Upload { export { type UploadGetPresignedURLResponse as UploadGetPresignedURLResponse, type UploadCompleteUploadParams as UploadCompleteUploadParams, type UploadGetPresignedURLParams as UploadGetPresignedURLParams, type UploadUploadFileParams as UploadUploadFileParams, type UploadUploadFileAndProcessParams as UploadUploadFileAndProcessParams, type UploadUploadFromURLParams as UploadUploadFromURLParams, type UploadUploadFromURLAndProcessParams as UploadUploadFromURLAndProcessParams, }; } type MultipartClient = Parameters[1]; type MultipartUploadBody = UploadUploadFileParams | UploadUploadFileAndProcessParams; async function multipartUploadRequestOptions( body: MultipartUploadBody, options: RequestOptions | undefined, client: MultipartClient, ): Promise { const { contentType, ...bodyWithoutContentType } = body; const multipartBody = contentType ? { ...bodyWithoutContentType, file: await toUploadableWithContentType(body.file, contentType) } : bodyWithoutContentType; return multipartFormRequestOptions({ body: multipartBody, ...options }, client); } async function toUploadableWithContentType(file: Uploadable, contentType: string): Promise { const typedFile = await toFile(file, undefined, { type: contentType }); if (typedFile.type === contentType) return typedFile; return new File([typedFile], typedFile.name, { type: contentType, lastModified: typedFile.lastModified, }); }