import {z} from 'zod' export type GetApiV3EnterpriseSessionsArgs = {after?: (string | null) | undefined, first?: number | undefined, session_ids?: (Array | null) | undefined, created_after?: (number | null) | undefined, created_before?: (number | null) | undefined, updated_after?: (number | null) | undefined, updated_before?: (number | null) | undefined, tags?: (Array | null) | undefined, playbook_id?: (string | null) | undefined, origins?: (Array<'webapp' | 'slack' | 'teams' | 'api' | 'linear' | 'jira' | 'automation' | 'cli' | 'desktop' | 'code_scan' | 'other'> | null) | undefined, schedule_id?: (string | null) | undefined, user_ids?: (Array | null) | undefined, service_user_ids?: (Array | null) | undefined, repo_names?: (Array | null) | undefined, category?: ('bug_fixing' | 'ci_cd_and_devops' | 'code_quality_and_security' | 'code_review' | 'code_review_and_analysis' | 'data_and_automation' | 'documentation_and_content' | 'feature_development' | 'migrations_and_upgrades' | 'other' | 'production_investigation' | 'refactoring_and_optimization' | 'research_and_exploration' | 'security' | 'unit_test_generation' | null) | undefined, is_archived?: (boolean | null) | undefined, org_ids?: (Array | null) | undefined, include_deleted_orgs?: boolean | undefined}; export type GetApiV3EnterpriseSessionsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sessionPullRequest = z.object({pr_state: z.string().nullable(), pr_url: z.string()}); export const sessionResponse = z.object({acus_consumed: z.number(), category: z.enum(["bug_fixing", "ci_cd_and_devops", "code_quality_and_security", "code_review", "code_review_and_analysis", "data_and_automation", "documentation_and_content", "feature_development", "migrations_and_upgrades", "other", "production_investigation", "refactoring_and_optimization", "research_and_exploration", "security", "unit_test_generation"]).nullable().optional(), child_session_ids: z.array(z.string()).nullable().optional(), created_at: z.number().int(), is_archived: z.boolean().optional(), org_id: z.string(), origin: z.enum(["webapp", "slack", "teams", "api", "linear", "jira", "automation", "cli", "desktop", "code_scan", "other"]).nullable().optional(), parent_session_id: z.string().nullable().optional(), playbook_id: z.string().nullable().optional(), pull_requests: z.array(sessionPullRequest), service_user_id: z.string().nullable().optional(), session_id: z.string(), status: z.enum(["new", "claimed", "running", "exit", "error", "suspended", "resuming"]), status_detail: z.enum(["working", "waiting_for_user", "waiting_for_approval", "finished", "inactivity", "user_request", "usage_limit_exceeded", "out_of_credits", "out_of_quota", "no_quota_allocation", "payment_declined", "org_usage_limit_exceeded", "total_session_limit_exceeded", "error"]).nullable().optional(), structured_output: z.record(z.string(), z.unknown()).nullable().optional(), subcategory: z.string().nullable().optional(), tags: z.array(z.string()), title: z.string().nullable().optional(), updated_at: z.number().int(), url: z.string(), user_id: z.string().nullable().optional()}); export const paginatedResponseSessionResponse = z.object({end_cursor: z.string().nullable().optional(), has_next_page: z.boolean().optional(), items: z.array(sessionResponse), total: z.number().int().nullable().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 }; /** * List sessions for account * * GET /v3/enterprise/sessions */ export const getApiV3EnterpriseSessions = async (args: GetApiV3EnterpriseSessionsArgs, options: GetApiV3EnterpriseSessionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.first != null) query.append("first", String(args.first)) for (const item of args.session_ids ?? []) query.append("session_ids", String(item)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.updated_after != null) query.append("updated_after", String(args.updated_after)) if (args.updated_before != null) query.append("updated_before", String(args.updated_before)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.playbook_id != null) query.append("playbook_id", String(args.playbook_id)) for (const item of args.origins ?? []) query.append("origins", String(item)) if (args.schedule_id != null) query.append("schedule_id", String(args.schedule_id)) for (const item of args.user_ids ?? []) query.append("user_ids", String(item)) for (const item of args.service_user_ids ?? []) query.append("service_user_ids", String(item)) for (const item of args.repo_names ?? []) query.append("repo_names", String(item)) if (args.category != null) query.append("category", String(args.category)) if (args.is_archived != null) query.append("is_archived", String(args.is_archived)) for (const item of args.org_ids ?? []) query.append("org_ids", String(item)) if (args.include_deleted_orgs != null) query.append("include_deleted_orgs", String(args.include_deleted_orgs)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/enterprise/sessions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedResponseSessionResponse, await res.json()) };