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

promptLinks.ts

97 lines3.5 KB

1import {
2 repositoryGroupingKeyOf,
3 type ModelSelection,
4 type RepositoryIdentity,
5 type ServerProvider,
6 type ThreadLinkedPullRequest,
7 type ThreadPullRequestLink,
8} from "@t3tools/contracts";
9import { resolveSelectableModel, type SelectableModelOption } from "@t3tools/shared/model";
10import type { PromptLinkPullRequest, PromptLinkRepository } from "@t3tools/shared/promptLinks";
11import {
12 threadPullRequestKeyOf,
13 threadPullRequestsOf,
14 visibleThreadPullRequests,
15} from "@t3tools/shared/threadPullRequests";
16​
17const repositoryKey = (repository: PromptLinkRepository) =>
18 `github.com/${repository.owner}/${repository.name}`.toLowerCase();
19​
20/**
21 * Callers pass projects most recently active first, so the user lands where they last worked.
22 */
23export function findPromptLinkProject<
24 P extends { readonly repositoryIdentity?: RepositoryIdentity | null | undefined },
25>(projects: ReadonlyArray<P>, repository: PromptLinkRepository): P | undefined {
26 const key = repositoryKey(repository);
27 return projects.find(
28 (project) =>
29 project.repositoryIdentity != null &&
30 repositoryGroupingKeyOf(project.repositoryIdentity).toLowerCase() === key,
31 );
33​
34export function findPromptLinkPullRequestThread<
35 T extends {
36 readonly updatedAt: string;
37 readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined;
38 readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
39 },
40>(threads: ReadonlyArray<T>, pullRequest: PromptLinkPullRequest): T | undefined {
41 const key = threadPullRequestKeyOf({
42 host: "github.com",
43 repository: `${pullRequest.owner}/${pullRequest.name}`,
44 number: pullRequest.number,
45 });
46 let latest: T | undefined;
47 for (const thread of threads) {
48 if (latest !== undefined && thread.updatedAt <= latest.updatedAt) continue;
49 const linked = visibleThreadPullRequests(threadPullRequestsOf(thread)).some(
50 (link) => threadPullRequestKeyOf(link) === key,
51 );
52 if (linked) latest = thread;
53 }
54 return latest;
56​
57const PROVIDER_ALIASES: Record<string, string> = { claude: "claudeagent" };
58​
59/**
60 * `exact` is false when the instance exists but the model does not, so its default model
61 * stands in. Null when no usable instance of that provider exists; the user's default
62 * applies then.
63 */
64export function resolvePromptLinkModelSelection(
65 providers: ReadonlyArray<
66 Pick<ServerProvider, "instanceId" | "driver" | "enabled" | "availability" | "status"> & {
67 readonly models: ReadonlyArray<
68 SelectableModelOption & { readonly isDefault?: boolean | undefined }
69 >;
70 }
71 >,
72 request: { readonly provider: string; readonly model: string | null },
73): { readonly selection: ModelSelection; readonly exact: boolean } | null {
74 const driver = PROVIDER_ALIASES[request.provider] ?? request.provider;
75 const usable = providers.filter(
76 (provider) =>
77 provider.driver.toLowerCase() === driver &&
78 provider.enabled &&
79 provider.availability !== "unavailable",
80 );
81 const instance =
82 usable.find((provider) => provider.status === "ready") ??
83 usable.find((provider) => provider.status === "warning");
84 if (instance === undefined) return null;
85 const requested =
86 request.model === null
87 ? null
88 : resolveSelectableModel(instance.driver, request.model, instance.models);
89 const model =
90 requested ??
91 (instance.models.find((candidate) => candidate.isDefault) ?? instance.models[0])?.slug;
92 if (model === undefined) return null;
93 return {
94 selection: { instanceId: instance.instanceId, model },
95 exact: request.model === null || requested !== null,
96 };