import {z} from 'zod' export type CreateApiV3TeamsTeamIdUsersUserIdFi3UploadArgs = {team_id: string, user_id: string, thread_id: string, body: BodyInit}; export type CreateApiV3TeamsTeamIdUsersUserIdFi3UploadOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const fi2UploadFileResponse = z.object({success: z.boolean(), file_path: z.string().optional(), file_name: z.string().optional(), file_size: z.number().int().optional(), error: z.string().nullable().optional(), sandbox_id: z.string().nullable().optional(), retries_attempted: z.number().int().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Upload a file to the E2B sandbox for a Fi3 Ultra thread * * POST /v3/teams/{team_id}/users/{user_id}/fi3/upload */ export const createApiV3TeamsTeamIdUsersUserIdFi3Upload = async (args: CreateApiV3TeamsTeamIdUsersUserIdFi3UploadArgs, options: CreateApiV3TeamsTeamIdUsersUserIdFi3UploadOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.thread_id != null) query.append("thread_id", String(args.thread_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/teams/${encodeURIComponent(String(args.team_id))}/users/${encodeURIComponent(String(args.user_id))}/fi3/upload${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(fi2UploadFileResponse, await res.json()) };