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

ThreadTitleLinks.ts

48 lines1.7 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
4​
5const encodeSubject = Schema.encodeEffect(
6 Schema.fromJsonString(Schema.Struct({ title: Schema.String, body: Schema.String })),
7);
8​
9/** Providers select supported links before the title lookup budget is applied. */
10export const resolveThreadTitleLinks = Effect.fn("resolveThreadTitleLinks")(function* (input: {
11 message: string;
12 cwd: string;
13}) {
14 const providers = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
15 const links = new Map<string, NonNullable<ReturnType<typeof providers.resolveLink>>>();
16 for (const match of input.message.matchAll(/https:\/\/[^\s<>"')\]`]+/g)) {
17 let url: URL;
18 try {
19 url = new URL(match[0].replace(/[.,;!?]+$/, ""));
20 } catch {
21 continue;
22 }
23 url.hash = "";
24 url.search = "";
25 if (links.has(url.href)) continue;
26 const lookup = providers.resolveLink({ cwd: input.cwd, url });
27 if (!lookup) continue;
28 links.set(url.href, lookup);
29 if (links.size === 2) break;
30 }
31 const subjects = yield* Effect.forEach(
32 links,
33 ([url, lookup]) =>
34 lookup.pipe(
35 Effect.flatMap((subject) =>
36 encodeSubject({
37 title: subject.title.slice(0, 300),
38 body: subject.body?.slice(0, 1_200) ?? "",
39 }),
40 ),
41 Effect.map((summary) => `${url}\n${summary}`),
42 Effect.timeout("3 seconds"),
43 Effect.catch(() => Effect.succeed(`${url}: unavailable`)),
44 ),
45 { concurrency: 2 },
46 );
47 return subjects.length > 0 ? subjects.join("\n\n") : undefined;
48});