The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | normalizeThreadPullRequestKey, |
| 3 | threadPullRequestKeyOf, |
| 4 | threadPullRequestKeysEqual, |
| 5 | visibleThreadPullRequests, |
| 6 | } from "../sourceControl/threadPullRequests.ts"; |
| 7 | import { |
| 8 | PullRequestLinkedThreadsResult, |
| 9 | PullRequestOperationError, |
| 10 | threadIssueKeyOf, |
| 11 | type OrchestrationV2AppThread, |
| 12 | type OrchestrationV2DomainEvent, |
| 13 | type ThreadId, |
| 14 | type ThreadPullRequestKey, |
| 15 | } from "@t3tools/contracts"; |
| 16 | import * as DateTime from "effect/DateTime"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 19 | import * as Schema from "effect/Schema"; |
| 20 | import * as SqlClient from "effect/sql/SqlClient"; |
| 21 | |
| 22 | const decodeLinkedThreads = Schema.decodeUnknownEffect(PullRequestLinkedThreadsResult); |
| 23 | |
| 24 | export const listLinkedPullRequestThreads = Effect.fn("listLinkedPullRequestThreads")( |
| 25 | function* (input: ThreadPullRequestKey) { |
| 26 | const key = normalizeThreadPullRequestKey(input); |
| 27 | const sql = yield* SqlClient.SqlClient; |
| 28 | const rows = yield* sql<{ |
| 29 | id: string; |
| 30 | projectId: string; |
| 31 | title: string; |
| 32 | archivedAt: string | null; |
| 33 | settled: number; |
| 34 | host: string; |
| 35 | repository: string; |
| 36 | number: number; |
| 37 | url: string; |
| 38 | }>` |
| 39 | SELECT t.thread_id AS id, t.project_id AS "projectId", t.title, |
| 40 | t.archived_at AS "archivedAt", |
| 41 | (json_extract(t.payload_json, '$.settledAt') IS NOT NULL |
| 42 | OR json_extract(t.payload_json, '$.settledOverride') IS 'settled') AS settled, |
| 43 | json_extract(link.value, '$.host') AS host, |
| 44 | json_extract(link.value, '$.repository') AS repository, |
| 45 | json_extract(link.value, '$.number') AS number, |
| 46 | json_extract(link.value, '$.url') AS url, |
| 47 | t.updated_at AS "updatedAt" |
| 48 | FROM orchestration_v2_projection_threads AS t |
| 49 | JOIN json_each(t.payload_json, '$.pullRequests') AS link |
| 50 | WHERE json_extract(link.value, '$.number') = ${key.number} |
| 51 | AND json_extract(link.value, '$.source') != 'stack-dismissed' |
| 52 | AND t.deleted_at IS NULL |
| 53 | AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 |
| 54 | ORDER BY "updatedAt" DESC, id ASC |
| 55 | `.pipe(NodeSqliteClient.readOnly); |
| 56 | const threads = rows |
| 57 | .filter((row) => threadPullRequestKeysEqual(row, key)) |
| 58 | .map(({ id, projectId, title, archivedAt, settled }) => ({ |
| 59 | id, |
| 60 | projectId, |
| 61 | title, |
| 62 | archivedAt, |
| 63 | settled: settled === 1, |
| 64 | })); |
| 65 | return yield* decodeLinkedThreads({ threads }); |
| 66 | }, |
| 67 | Effect.mapError( |
| 68 | (cause) => |
| 69 | new PullRequestOperationError({ |
| 70 | operation: "linkedThreads", |
| 71 | detail: "Could not load linked threads.", |
| 72 | cause, |
| 73 | }), |
| 74 | ), |
| 75 | ); |
| 76 | |
| 77 | // Mirrors the rows and filters of `listLinkedPullRequestThreads` and `listLinkedIssueThreads`. |
| 78 | function linkedKeys(thread: OrchestrationV2AppThread): ReadonlyArray<string> { |
| 79 | if (thread.deletedAt !== null || thread.lineage.sideChat === true) return []; |
| 80 | return [ |
| 81 | ...visibleThreadPullRequests(thread.pullRequests ?? []).map(threadPullRequestKeyOf), |
| 82 | ...(thread.issues ?? []).map(threadIssueKeyOf), |
| 83 | ].toSorted(); |
| 84 | } |
| 85 | |
| 86 | function linkedThreadsView( |
| 87 | thread: OrchestrationV2AppThread, |
| 88 | keys: ReadonlyArray<string>, |
| 89 | ): string | null { |
| 90 | if (keys.length === 0) return null; |
| 91 | const settled = thread.settledAt !== null || thread.settledOverride === "settled"; |
| 92 | return JSON.stringify([ |
| 93 | keys, |
| 94 | thread.title, |
| 95 | thread.projectId, |
| 96 | thread.archivedAt !== null, |
| 97 | settled, |
| 98 | ]); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Tells how a domain event changed `pullRequests.linkedThreads` and `issues.linkedThreads` |
| 103 | * answers, so readers can refresh on change instead of polling. `reordered` means activity on a linked thread, or on one whose links |
| 104 | * are unknown since a restart, which can reorder answers. A thread's first full event after a |
| 105 | * restart counts as a change, since its earlier links are unknown. |
| 106 | */ |
| 107 | export function makeLinkedThreadsChangeDetector() { |
| 108 | const views = new Map<ThreadId, string | null>(); |
| 109 | // Last activity of each thread with visible links. |
| 110 | const activeAt = new Map<ThreadId, number>(); |
| 111 | const active = (threadId: ThreadId, at: DateTime.Utc): boolean => { |
| 112 | const previous = activeAt.get(threadId); |
| 113 | if (previous === undefined) return false; |
| 114 | const millis = DateTime.toEpochMillis(at); |
| 115 | if (millis <= previous) return false; |
| 116 | activeAt.set(threadId, millis); |
| 117 | return true; |
| 118 | }; |
| 119 | return (event: OrchestrationV2DomainEvent): "changed" | "reordered" | null => { |
| 120 | // The projection stamps every non-thread event as the thread's activity. A thread not seen |
| 121 | // since startup may be linked already, since history is not replayed. |
| 122 | if (!event.type.startsWith("thread.")) { |
| 123 | if (!views.has(event.threadId)) return "reordered"; |
| 124 | return active(event.threadId, event.occurredAt) ? "reordered" : null; |
| 125 | } |
| 126 | if (!("lineage" in event.payload)) return null; |
| 127 | const thread = event.payload; |
| 128 | const view = linkedThreadsView(thread, linkedKeys(thread)); |
| 129 | const known = views.has(event.threadId) || event.type === "thread.created"; |
| 130 | const previous = views.get(event.threadId) ?? null; |
| 131 | if (event.type === "thread.deleted") views.delete(event.threadId); |
| 132 | else views.set(event.threadId, view); |
| 133 | if (view === null) activeAt.delete(event.threadId); |
| 134 | if (!known || view !== previous) { |
| 135 | if (view !== null) activeAt.set(event.threadId, DateTime.toEpochMillis(thread.updatedAt)); |
| 136 | return "changed"; |
| 137 | } |
| 138 | return active(event.threadId, thread.updatedAt) ? "reordered" : null; |
| 139 | }; |
| 140 | } |