import { z } from "zod" import { defineAction } from "../../../automation/actions" import { getLinearApi, LINEAR_AGENT_SESSION_SCHEMA, LINEAR_PAGE_INFO_SCHEMA, LINEAR_PAGE_INPUT_SCHEMA, linearConnectionSchema, } from "@automate.ax/integration-contracts/linear" import { LINEAR_READ_ACCOUNT_OPTIONS } from "../lib/scopes" const LINEAR_AGENT_SESSION_FIELDS = ` id slugId status summary plan url createdAt updatedAt startedAt endedAt dismissedAt appUser { id name displayName email avatarUrl active } issue { id identifier title url } externalLinks { label url } ` const LINEAR_AGENT_SESSION_CONNECTION_SCHEMA = linearConnectionSchema( LINEAR_AGENT_SESSION_SCHEMA, ) const LINEAR_AGENT_SESSION_PAGE_SCHEMA = z.object({ agentSessions: LINEAR_AGENT_SESSION_SCHEMA.array(), pageInfo: LINEAR_PAGE_INFO_SCHEMA, }) /** Gets one Linear agent session by ID. */ export const getLinearAgentSession = defineAction("Get Linear agent session") .describe("Gets the current state of one Linear agent session.") .account("linear", LINEAR_READ_ACCOUNT_OPTIONS) .input( z.object({ /** Stable Linear agent-session ID. */ agentSessionId: z.string().min(1), }), ) .output(LINEAR_AGENT_SESSION_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getLinearApi(account.secret) .graphql( `query LinearAgentSession($id: String!) { agentSession(id: $id) { ${LINEAR_AGENT_SESSION_FIELDS} } }`, { responseSchema: z.object({ agentSession: LINEAR_AGENT_SESSION_SCHEMA, }), variables: { id: input.agentSessionId }, }, ) .then(({ agentSession }) => agentSession), ) /** Lists agent sessions attached to one Linear issue. */ export const listLinearIssueAgentSessions = defineAction( "List Linear issue agent sessions", ) .describe("Lists agent sessions attached to one Linear issue.") .account("linear", LINEAR_READ_ACCOUNT_OPTIONS) .input( LINEAR_PAGE_INPUT_SCHEMA.extend({ /** UUID or human-readable identifier of the issue. */ issueId: z.string().min(1), }), ) .output(LINEAR_AGENT_SESSION_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { issue } = await getLinearApi(account.secret).graphql( `query LinearIssueAgentSessions( $id: String! $first: Int! $after: String $includeArchived: Boolean ) { issue(id: $id) { agentSessions( first: $first after: $after includeArchived: $includeArchived orderBy: updatedAt ) { nodes { ${LINEAR_AGENT_SESSION_FIELDS} } pageInfo { hasNextPage hasPreviousPage startCursor endCursor } } } }`, { responseSchema: z.object({ issue: z.object({ agentSessions: LINEAR_AGENT_SESSION_CONNECTION_SCHEMA, }), }), variables: { first: input.limit, id: input.issueId, ...(input.after && { after: input.after }), ...(input.includeArchived !== undefined && { includeArchived: input.includeArchived, }), }, }, ) return { agentSessions: issue.agentSessions.nodes, pageInfo: issue.agentSessions.pageInfo!, } })