Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

promptLinks.ts

123 lines4.6 KB

1import {
2 repositoryGroupingKeyOf,
3 type EnvironmentId,
4 type ModelSelection,
5 type PullRequestLinkedThreadsResult,
6 type RepositoryIdentity,
7 type ServerProvider,
8 type ThreadLinkedPullRequest,
9 type ThreadId,
10 type ThreadPullRequestLink,
11} from "@t3tools/contracts";
12import { resolveSelectableModel, type SelectableModelOption } from "@t3tools/shared/model";
13import type { AsyncResult } from "effect/reactivity";
14import type { PromptLinkPullRequest, PromptLinkRepository } from "@t3tools/shared/promptLinks";
15import {
16 threadPullRequestKeyOf,
17 threadPullRequestsOf,
18 visibleThreadPullRequests,
19} from "./threadPullRequests.ts";
20​
21const repositoryKey = (repository: PromptLinkRepository) =>
22 `github.com/${repository.owner}/${repository.name}`.toLowerCase();
23​
24/**
25 * The repository's projects in the order given. Callers pass projects most recently active
26 * first and open the first, so the user lands where they last worked.
27 */
28export function findPromptLinkProjects<
29 P extends { readonly repositoryIdentity?: RepositoryIdentity | null | undefined },
30>(projects: ReadonlyArray<P>, repository: PromptLinkRepository): P[] {
31 const key = repositoryKey(repository);
32 return projects.filter(
33 (project) =>
34 project.repositoryIdentity != null &&
35 repositoryGroupingKeyOf(project.repositoryIdentity).toLowerCase() === key,
36 );
38​
39export function findPromptLinkPullRequestThread<
40 T extends {
41 readonly updatedAt: string;
42 readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined;
43 readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
44 },
45>(threads: ReadonlyArray<T>, pullRequest: PromptLinkPullRequest): T | undefined {
46 const key = threadPullRequestKeyOf({
47 host: "github.com",
48 repository: `${pullRequest.owner}/${pullRequest.name}`,
49 number: pullRequest.number,
50 });
51 let latest: T | undefined;
52 for (const thread of threads) {
53 if (latest !== undefined && thread.updatedAt <= latest.updatedAt) continue;
54 const linked = visibleThreadPullRequests(threadPullRequestsOf(thread)).some(
55 (link) => threadPullRequestKeyOf(link) === key,
56 );
57 if (linked) latest = thread;
58 }
59 return latest;
61​
62/**
63 * The first open thread from per-environment pull request lookups, in the order given, once
64 * every lookup before it has answered. `null` when none has one; `undefined` until then.
65 */
66export function pickPromptLinkPullRequestThread(
67 lookups: ReadonlyArray<{
68 readonly environmentId: EnvironmentId;
69 readonly result: AsyncResult.AsyncResult<PullRequestLinkedThreadsResult, unknown>;
70 }>,
71): { readonly environmentId: EnvironmentId; readonly id: ThreadId } | null | undefined {
72 for (const { environmentId, result } of lookups) {
73 if (result._tag === "Initial" || result.waiting) return undefined;
74 const thread =
75 result._tag === "Success"
76 ? result.value.threads.find((candidate) => candidate.archivedAt === null)
77 : undefined;
78 if (thread !== undefined) return { environmentId, id: thread.id };
79 }
80 return null;
82​
83const PROVIDER_ALIASES: Record<string, string> = { claude: "claudeagent" };
84​
85/**
86 * `exact` is false when the instance exists but the model does not, so its default model
87 * stands in. Null when no usable instance of that provider exists; the user's default
88 * applies then.
89 */
90export function resolvePromptLinkModelSelection(
91 providers: ReadonlyArray<
92 Pick<ServerProvider, "instanceId" | "driver" | "enabled" | "availability" | "status"> & {
93 readonly models: ReadonlyArray<
94 SelectableModelOption & { readonly isDefault?: boolean | undefined }
95 >;
96 }
97 >,
98 request: { readonly provider: string; readonly model: string | null },
99): { readonly selection: ModelSelection; readonly exact: boolean } | null {
100 const driver = PROVIDER_ALIASES[request.provider] ?? request.provider;
101 const usable = providers.filter(
102 (provider) =>
103 provider.driver.toLowerCase() === driver &&
104 provider.enabled &&
105 provider.availability !== "unavailable",
106 );
107 const instance =
108 usable.find((provider) => provider.status === "ready") ??
109 usable.find((provider) => provider.status === "warning");
110 if (instance === undefined) return null;
111 const requested =
112 request.model === null
113 ? null
114 : resolveSelectableModel(instance.driver, request.model, instance.models);
115 const model =
116 requested ??
117 (instance.models.find((candidate) => candidate.isDefault) ?? instance.models[0])?.slug;
118 if (model === undefined) return null;
119 return {
120 selection: { instanceId: instance.instanceId, model },
121 exact: request.model === null || requested !== null,
122 };