import {z} from 'zod' export type GetApiV3EnterpriseSessionsInsightsArgs = {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 GetApiV3EnterpriseSessionsInsightsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sessionInsightsActionItem = z.object({action_item: z.string(), issue_id: z.string().nullable().optional(), type: z.enum(["machine_setup", "repo_config", "knowledge", "prompt_improvement", "other"]).optional()}); export const sessionInsightsClassification = z.object({category: z.string(), confidence: z.number(), programming_languages: z.array(z.string()).optional(), tools_and_frameworks: z.array(z.string()).optional()}); export const sessionInsightsIssue = z.object({id: z.string().optional(), impact: z.string(), issue: z.string(), label: z.string()}); export const sessionInsightsNoteUsageItem = z.object({message: z.string(), note_id: z.string(), reason: z.string()}); export const sessionInsightsNoteUsage = z.object({bad_usages: z.array(sessionInsightsNoteUsageItem).optional(), good_usages: z.array(sessionInsightsNoteUsageItem).optional()}); export const sessionInsightsFeedbackItem = z.object({details: z.string(), excerpt: z.string(), issue_id: z.string().nullable().optional(), summary: z.string()}); export const sessionInsightsSuggestedPrompt = z.object({feedback_items: z.array(sessionInsightsFeedbackItem).optional(), original_prompt: z.string(), suggested_prompt: z.string()}); export const sessionInsightsTimelineEvent = z.object({color: z.string().optional(), description: z.string(), issue_id: z.string().nullable().optional(), title: z.string()}); export const sessionInsightsAnalysis = z.object({action_items: z.array(sessionInsightsActionItem).optional(), classification: sessionInsightsClassification.nullable().optional(), issues: z.array(sessionInsightsIssue).optional(), note_usage: sessionInsightsNoteUsage.nullable().optional(), suggested_prompt: sessionInsightsSuggestedPrompt.nullable().optional(), timeline: z.array(sessionInsightsTimelineEvent).optional()}); export const sessionPullRequest = z.object({pr_state: z.string().nullable(), pr_url: z.string()}); export const sessionInsightsResponse = z.object({acus_consumed: z.number(), analysis: sessionInsightsAnalysis.nullable().optional(), 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(), num_devin_messages: z.number().int(), num_user_messages: z.number().int(), 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(), session_size: z.enum(["xs", "s", "m", "l", "xl"]), 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 paginatedResponseSessionInsightsResponse = z.object({end_cursor: z.string().nullable().optional(), has_next_page: z.boolean().optional(), items: z.array(sessionInsightsResponse), 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 with insights * * GET /v3/enterprise/sessions/insights */ export const getApiV3EnterpriseSessionsInsights = async (args: GetApiV3EnterpriseSessionsInsightsArgs, options: GetApiV3EnterpriseSessionsInsightsOptions) => { 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/insights${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(paginatedResponseSessionInsightsResponse, await res.json()) };