Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3import {
4 CommandId,
5 IsoDateTime,
6 NonNegativeInt,
7 PositiveInt,
8 ThreadId,
9 TrimmedNonEmptyString,
10} from "./baseSchemas.ts";
11import { ThreadLinkedPullRequest, ThreadTitleRegeneration } from "./orchestration.ts";
12
13function hasOnlyPairedSurrogates(value: string): boolean {
14 for (let index = 0; index < value.length; index += 1) {
15 const codeUnit = value.charCodeAt(index);
16 if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
17 const nextCodeUnit = value.charCodeAt(index + 1);
18 if (index + 1 >= value.length || nextCodeUnit < 0xdc00 || nextCodeUnit > 0xdfff) {
19 return false;
20 }
21 index += 1;
22 } else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
23 return false;
24 }
25 }
26 return true;
27}
28
29function isHttpUrl(value: string): boolean {
30 try {
31 const url = new URL(value);
32 return url.protocol === "http:" || url.protocol === "https:";
33 } catch {
34 return false;
35 }
36}
37
38const ThreadMetadataTitle = TrimmedNonEmptyString.check(Schema.isMaxLength(512)).annotate({
39 description: "New concise display title. Required only when action is rename.",
40});
41
42const ThreadMetadataClientRequestId = TrimmedNonEmptyString.check(Schema.isMaxLength(256))
43 .check(
44 Schema.makeFilter((value) =>
45 hasOnlyPairedSurrogates(value)
46 ? true
47 : "clientRequestId must contain valid paired Unicode surrogates.",
48 ),
49 )
50 .annotate({ description: "Stable idempotency key to reuse when retrying this mutation." });
51
52const ThreadMetadataMcpPullRequestUrl = TrimmedNonEmptyString.check(
53 Schema.makeFilter((value) =>
54 isHttpUrl(value) ? true : "Pull request URL must be a well-formed HTTP(S) URL.",
55 ),
56).annotate({
57 description: "Canonical HTTP(S) pull request URL, including self-hosted repository URLs.",
58});
59
60export const ThreadMetadataMcpAction = Schema.Literals([
61 "rename",
62 "regenerate_title",
63 "link_pull_request",
64 "unlink_pull_request",
65]).annotate({
66 description:
67 "Metadata mutation: rename, regenerate_title, link_pull_request, or unlink_pull_request.",
68});
69export type ThreadMetadataMcpAction = typeof ThreadMetadataMcpAction.Type;
70
71export const ThreadMetadataMcpPullRequest = Schema.Struct({
72 repository: TrimmedNonEmptyString.annotate({
73 description: "Repository name as owner/name.",
74 }),
75 number: PositiveInt.annotate({ description: "Pull request number." }),
76 url: ThreadMetadataMcpPullRequestUrl,
77});
78export type ThreadMetadataMcpPullRequest = typeof ThreadMetadataMcpPullRequest.Type;
79
80export const ThreadMetadataMcpUpdateInput = Schema.Struct({
81 threadId: Schema.optional(ThreadId).annotate({
82 description: "Thread in the calling project. Omit to update the calling thread.",
83 }),
84 action: ThreadMetadataMcpAction,
85 title: Schema.optional(ThreadMetadataTitle),
86 pullRequest: Schema.optional(ThreadMetadataMcpPullRequest).annotate({
87 description: "Pull request to link. Required only when action is link_pull_request.",
88 }),
89 clientRequestId: Schema.optional(ThreadMetadataClientRequestId),
90}).check(
91 Schema.makeFilter((input) => {
92 switch (input.action) {
93 case "rename":
94 return input.title !== undefined && input.pullRequest === undefined
95 ? true
96 : "rename requires title and does not accept pullRequest.";
97 case "link_pull_request":
98 return input.pullRequest !== undefined && input.title === undefined
99 ? true
100 : "link_pull_request requires pullRequest and does not accept title.";
101 case "regenerate_title":
102 case "unlink_pull_request":
103 return input.title === undefined && input.pullRequest === undefined
104 ? true
105 : `${input.action} does not accept title or pullRequest.`;
106 }
107 }),
108);
109export type ThreadMetadataMcpUpdateInput = typeof ThreadMetadataMcpUpdateInput.Type;
110
111export const ThreadMetadataMcpUpdateResult = Schema.Struct({
112 threadId: ThreadId,
113 action: ThreadMetadataMcpAction,
114 commandId: CommandId,
115 sequence: NonNegativeInt,
116 title: Schema.String,
117 titleRegeneration: Schema.NullOr(ThreadTitleRegeneration),
118 linkedPullRequest: Schema.NullOr(ThreadLinkedPullRequest),
119 updatedAt: IsoDateTime,
120});
121export type ThreadMetadataMcpUpdateResult = typeof ThreadMetadataMcpUpdateResult.Type;