import type { Plugin } from "@opencode/plugin/effect" import { Effect, Schema } from "effect" import { directoryOfDirKey } from "./dirs.js" import { requireRouter } from "./guard.js" import { store } from "./state.js" import { renderAggregatedTickets } from "./ticket-aggregate.js" import { closeTicket, createTicket, currentTrigger, getTicket, LIST_DEFAULT, listTicketRecords, listTicketRecordsByPartition, listTickets, loadTicket, renderTicketDetail, renderTicketList, TICKET_STATES, updateTicket, type TicketState, } from "./ticket.js" import { specFor, toolPermission } from "./tools.js" /** * Router-only ticket tools. They are a thin adapter: validation, the state * machine, dedupe and KV persistence all live in `src/ticket.ts`, so the same * rules apply to the dispatch and watcher links. */ export const TicketCreateInput = Schema.Struct({ threadID: Schema.String, title: Schema.String, objective: Schema.String, acceptance: Schema.Array(Schema.String), directory: Schema.String, priority: Schema.optionalKey(Schema.Literals(["p0", "p1", "p2"])), parentTicketID: Schema.optionalKey(Schema.String), dedupeKey: Schema.optionalKey(Schema.String), }) export const TicketListInput = Schema.Struct({ threadID: Schema.optionalKey(Schema.String), status: Schema.optionalKey(Schema.Literals(TICKET_STATES)), since: Schema.optionalKey(Schema.Number), limit: Schema.optionalKey(Schema.Number), }) export const TicketUpdateInput = Schema.Struct({ ticketID: Schema.String, status: Schema.optionalKey(Schema.Literals(TICKET_STATES)), note: Schema.optionalKey(Schema.String), }) export const TicketCloseInput = Schema.Struct({ ticketID: Schema.String, outcome: Schema.Literals(["done", "failed", "cancelled"]), summary: Schema.String, }) const TicketRefOutput = Schema.Struct({ ticketID: Schema.String, state: Schema.String, reused: Schema.Boolean, }) const TicketTextOutput = Schema.Struct({ text: Schema.String }) interface TicketCreateInput { readonly threadID: string readonly title: string readonly objective: string readonly acceptance: ReadonlyArray readonly directory: string readonly priority?: "p0" | "p1" | "p2" readonly parentTicketID?: string readonly dedupeKey?: string } interface TicketListInput { readonly threadID?: string readonly status?: TicketState readonly since?: number readonly limit?: number } interface TicketUpdateInput { readonly ticketID: string readonly status?: TicketState readonly note?: string } interface TicketCloseInput { readonly ticketID: string readonly outcome: "done" | "failed" | "cancelled" readonly summary: string } const runTicketCreate = Effect.fn("superagent.ticketCreate")(function* ( ctx: Plugin.Context, input: TicketCreateInput, caller: string, ) { // The ticket belongs to the calling Router's partition. `directory` is only // the working directory: a Router may create work for any directory it can // reach, and the ticket must stay operable by the Router that created it. const dirKey = yield* requireRouter(caller, "ticket_create") const trigger = currentTrigger(dirKey) const threadID = input.threadID.trim() const requester = trigger?.threadID === threadID ? trigger : undefined const result = yield* createTicket(ctx, { threadID, title: input.title, objective: input.objective, acceptance: input.acceptance, directory: input.directory, dirKey, source: requester?.source ?? "router", ...(requester?.author === undefined ? {} : { author: requester.author }), ...(input.priority === undefined ? {} : { priority: input.priority }), ...(input.parentTicketID === undefined ? {} : { parentTicketID: input.parentTicketID }), ...(input.dedupeKey === undefined ? {} : { dedupeKey: input.dedupeKey }), }) const ticket = result.ticket const content = result.reused ? `工单 ${ticket.id} 已存在(reused,state=${ticket.state},priority=${ticket.priority}):${ticket.title}。dispatch 时带上 ticketID=${ticket.id}。` : `工单 ${ticket.id} 已建立(state=${ticket.state},priority=${ticket.priority}):${ticket.title}。下一步 dispatch 时带上 ticketID=${ticket.id}。` return { output: { ticketID: ticket.id, state: ticket.state, reused: result.reused }, content, } }) const runTicketList = Effect.fn("superagent.ticketList")(function* ( ctx: Plugin.Context, input: TicketListInput, caller: string, ) { const dirKey = yield* requireRouter(caller, "ticket_list") const text = renderTicketList( yield* listTicketRecords( ctx, { ...(input.threadID === undefined ? {} : { threadID: input.threadID }), ...(input.status === undefined ? {} : { status: input.status }), ...(input.since === undefined ? {} : { since: input.since }), ...(input.limit === undefined ? {} : { limit: input.limit }), }, dirKey, ), ) return { output: { text }, content: text } }) const runTicketUpdate = Effect.fn("superagent.ticketUpdate")(function* ( ctx: Plugin.Context, input: TicketUpdateInput, caller: string, ) { const dirKey = yield* requireRouter(caller, "ticket_update") const ticket = yield* updateTicket(ctx, { ticketID: input.ticketID, dirKey, ...(input.status === undefined ? {} : { state: input.status }), ...(input.note === undefined ? {} : { note: input.note }), }) const content = `工单 ${ticket.id} → ${ticket.state}:${ticket.title}` return { output: { ticketID: ticket.id, state: ticket.state, reused: false }, content } }) const runTicketClose = Effect.fn("superagent.ticketClose")(function* ( ctx: Plugin.Context, input: TicketCloseInput, caller: string, ) { const dirKey = yield* requireRouter(caller, "ticket_close") const ticket = yield* closeTicket(ctx, { ticketID: input.ticketID, outcome: input.outcome, summary: input.summary, dirKey, }) const content = `工单 ${ticket.id} 已结单(${ticket.state}):${ticket.closeReason ?? input.summary}` return { output: { ticketID: ticket.id, state: ticket.state, reused: false }, content } }) /** RPC helper for the TUI: ready-to-render text, no router check (the user drove it). */ export function renderTicketsText(input: TicketListInput = {}): string { return renderTicketList( listTickets({ ...(input.threadID === undefined ? {} : { threadID: input.threadID }), ...(input.status === undefined ? {} : { status: input.status }), ...(input.since === undefined ? {} : { since: input.since }), ...(input.limit === undefined ? {} : { limit: input.limit }), }), ) } export function renderTicketText(ticketID: string): string { const ticket = getTicket(ticketID.trim()) if (ticket === undefined) return `没有找到工单 ${ticketID}` return renderTicketDetail(ticket) } /** * RPC helpers that consult KV when memory no longer holds the record (it is * trimmed to the newest `MEMORY_LIMIT` tickets). The TUI reads through these * so a closed ticket that still exists in KV is never reported as missing. */ export const renderTicketsTextDeep = ( ctx: Plugin.Context, input: TicketListInput = {}, // Stage 3: `dirKey` scopes the read to one directory's partition (the RPC // passes it when the TUI sends its session directory). Omitted = the // process-wide list that the sidebar used before the split. dirKey?: string, ) => listTicketRecords( ctx, { ...(input.threadID === undefined ? {} : { threadID: input.threadID }), ...(input.status === undefined ? {} : { status: input.status }), ...(input.since === undefined ? {} : { since: input.since }), ...(input.limit === undefined ? {} : { limit: input.limit }), }, dirKey, ).pipe(Effect.map(renderTicketList)) /** * The directory a partition belongs to. * * `store.dirs` is authoritative (the partition records its own directory when * it is created), `directoryOfDirKey` is the fallback for a partition that was * only ever seen through its key (a ticket's owner). The default partition has * no directory of its own, so it stays `undefined` and is rendered as `-`. */ function partitionDirectory(dirKey: string): string | undefined { return store.dirs.get(dirKey)?.directory ?? directoryOfDirKey(dirKey) } /** * The aggregate view (`/alltickets`): **every** partition's tickets, grouped by * directory. * * The group bodies are the very same `renderTicketList` output as the * single-partition view, so `/tickets` and `/alltickets` differ only in the * group headers and the total count. With a single partition the output is * byte-identical to the single-partition view — the aggregate view must not * add noise to a gateway that only ever has one directory. */ export const renderAllTicketsTextDeep = ( ctx: Plugin.Context, input: TicketListInput = {}, ) => listTicketRecordsByPartition( ctx, { ...(input.threadID === undefined ? {} : { threadID: input.threadID }), ...(input.status === undefined ? {} : { status: input.status }), ...(input.since === undefined ? {} : { since: input.since }), ...(input.limit === undefined ? {} : { limit: input.limit }), }, input.limit ?? LIST_DEFAULT, ).pipe( Effect.map((groups) => { const rendered = renderAggregatedTickets( [...groups.entries()].map(([dirKey, tickets]) => { const directory = partitionDirectory(dirKey) // A group that reached the fetch limit may hold more tickets than it // shows; its count is then printed as a lower bound (`20+ 条`). const truncated = tickets.length >= (input.limit ?? LIST_DEFAULT) return { dirKey, ...(directory === undefined ? {} : { directory }), count: tickets.length, truncated, body: renderTicketList(tickets), } }), ) // No ticket anywhere: the single-partition empty text, so both views say // the same thing when there is nothing to show. return rendered === "" ? renderTicketList([]) : rendered }), ) export const renderTicketTextDeep = (ctx: Plugin.Context, ticketID: string) => loadTicket(ctx, ticketID).pipe( Effect.map((ticket) => ticket === undefined ? `没有找到工单 ${ticketID}` : renderTicketDetail(ticket), ), ) export const registerTicketTools = Effect.fn("superagent.registerTicketTools")(function* (ctx: Plugin.Context) { yield* ctx.tool.transform((editor) => { editor.add({ name: "ticket_create", description: specFor("ticket_create").description, input: TicketCreateInput, output: TicketRefOutput, options: { permission: toolPermission(specFor("ticket_create")) }, execute: (input, context) => runTicketCreate(ctx, input, context.sessionID), }) editor.add({ name: "ticket_list", description: specFor("ticket_list").description, input: TicketListInput, output: TicketTextOutput, options: { permission: toolPermission(specFor("ticket_list")) }, execute: (input, context) => runTicketList(ctx, input, context.sessionID), }) editor.add({ name: "ticket_update", description: specFor("ticket_update").description, input: TicketUpdateInput, output: TicketRefOutput, options: { permission: toolPermission(specFor("ticket_update")) }, execute: (input, context) => runTicketUpdate(ctx, input, context.sessionID), }) editor.add({ name: "ticket_close", description: specFor("ticket_close").description, input: TicketCloseInput, output: TicketRefOutput, options: { permission: toolPermission(specFor("ticket_close")) }, execute: (input, context) => runTicketClose(ctx, input, context.sessionID), }) }) })