@opencode/ui 2.2.5 → 2.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/button/button.d.ts +2 -2
- package/dist/actions/icon-button/icon-button.d.ts +1 -1
- package/dist/components/animated-number-machine.d.ts +95 -0
- package/dist/components/file-icon-machine.d.ts +40 -0
- package/dist/components/file-icon.d.ts +0 -2
- package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
- package/dist/components/image-flight-machine.d.ts +135 -0
- package/dist/components/image-zoom-machine.d.ts +95 -0
- package/dist/components/image-zoom.d.ts +6 -3
- package/dist/components/list.d.ts +2 -6
- package/dist/components/menu-scroll.d.ts +9 -3
- package/dist/components/popover-machine.d.ts +96 -0
- package/dist/components/popover.d.ts +2 -2
- package/dist/components/resize-handle-machine.d.ts +71 -0
- package/dist/components/resize-handle.d.ts +2 -0
- package/dist/components/scroll-view-machine.d.ts +68 -0
- package/dist/components/scroll-view.d.ts +2 -2
- package/dist/components/spinner.d.ts +11 -3
- package/dist/components/text-field-machine.d.ts +62 -0
- package/dist/components/text-reveal-machine.d.ts +90 -0
- package/dist/components/typewriter-machine.d.ts +62 -0
- package/dist/context/dialog-machine.d.ts +125 -0
- package/dist/context/dialog.d.ts +17 -16
- package/dist/data-display/line-comment/machine.d.ts +157 -0
- package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
- package/dist/data-display/project-avatar/animation.d.ts +1 -1
- package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
- package/dist/feedback/toast/machine.d.ts +57 -0
- package/dist/feedback/toast/toast.d.ts +1 -1
- package/dist/forms/checkbox/checkbox.d.ts +6 -1
- package/dist/forms/select/machine.d.ts +45 -0
- package/dist/forms/select/select.d.ts +4 -0
- package/dist/forms/typeahead/machine.d.ts +85 -0
- package/dist/hooks/auto-scroll-machine.d.ts +89 -0
- package/dist/hooks/create-auto-scroll.d.ts +1 -1
- package/dist/hooks/list-machine.d.ts +150 -0
- package/dist/hooks/use-filtered-list.d.ts +21 -35
- package/dist/i18n/en.d.ts +5 -0
- package/dist/icons/catalog.d.ts +8 -0
- package/dist/navigation/menu/machine.d.ts +49 -0
- package/dist/navigation/menu/menu.d.ts +5 -3
- package/dist/navigation/segmented-control/machine.d.ts +46 -0
- package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
- package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
- package/dist/overlays/tooltip/machine.d.ts +146 -0
- package/dist/theme/context.d.ts +38 -14
- package/dist/theme/machine.d.ts +174 -0
- package/dist/typography/text-shimmer/machine.d.ts +48 -0
- package/package.json +128 -111
- package/src/actions/button/button.tsx +11 -6
- package/src/actions/icon-button/icon-button.tsx +17 -4
- package/src/components/animated-number-machine.ts +214 -0
- package/src/components/animated-number.tsx +134 -83
- package/src/components/file-icon-machine.ts +78 -0
- package/src/components/file-icon.tsx +96 -579
- package/src/components/file-icons/choose-icon-name.ts +574 -0
- package/src/components/image-flight-machine.ts +247 -0
- package/src/components/image-preview.css +5 -2
- package/src/components/image-preview.tsx +390 -252
- package/src/components/image-zoom-machine.ts +191 -0
- package/src/components/image-zoom.ts +22 -14
- package/src/components/list.tsx +46 -137
- package/src/components/menu-scroll.css +19 -8
- package/src/components/menu-scroll.tsx +111 -34
- package/src/components/popover-machine.ts +198 -0
- package/src/components/popover.tsx +157 -89
- package/src/components/resize-handle-machine.ts +174 -0
- package/src/components/resize-handle.tsx +116 -67
- package/src/components/scroll-view-machine.ts +244 -0
- package/src/components/scroll-view.tsx +163 -116
- package/src/components/spinner.css +18 -5
- package/src/components/spinner.tsx +28 -14
- package/src/components/text-field-machine.ts +102 -0
- package/src/components/text-field.tsx +78 -24
- package/src/components/text-reveal-machine.ts +184 -0
- package/src/components/text-reveal.tsx +96 -93
- package/src/components/typewriter-machine.ts +134 -0
- package/src/components/typewriter.tsx +50 -39
- package/src/context/dialog-machine.ts +280 -0
- package/src/context/dialog.tsx +168 -177
- package/src/data-display/line-comment/line-comment.tsx +157 -178
- package/src/data-display/line-comment/machine.ts +391 -0
- package/src/data-display/project-avatar/animation-machine.ts +182 -0
- package/src/data-display/project-avatar/animation.ts +67 -38
- package/src/feedback/activity-ring/activity-ring.tsx +1 -1
- package/src/feedback/toast/machine.ts +115 -0
- package/src/feedback/toast/toast.tsx +59 -59
- package/src/forms/checkbox/checkbox-compact.css +169 -0
- package/src/forms/checkbox/checkbox.tsx +93 -31
- package/src/forms/inline-input/inline-input.css +0 -1
- package/src/forms/search-list/search-list.css +5 -1
- package/src/forms/search-list/search-list.tsx +18 -10
- package/src/forms/select/machine.ts +81 -0
- package/src/forms/select/select.css +59 -17
- package/src/forms/select/select.tsx +74 -32
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +70 -62
- package/src/hooks/auto-scroll-machine.ts +261 -0
- package/src/hooks/create-auto-scroll.tsx +110 -205
- package/src/hooks/list-machine.ts +359 -0
- package/src/hooks/use-filtered-list.tsx +101 -112
- package/src/i18n/en.ts +6 -1
- package/src/i18n/uk.ts +3 -3
- package/src/icons/catalog.ts +8 -0
- package/src/navigation/menu/machine.ts +109 -0
- package/src/navigation/menu/menu.tsx +133 -22
- package/src/navigation/segmented-control/machine.ts +72 -0
- package/src/navigation/segmented-control/segmented-control.tsx +46 -46
- package/src/overlays/dialog/dialog.tsx +7 -1
- package/src/overlays/mcp-sign-in/machine.ts +343 -0
- package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
- package/src/overlays/tooltip/machine.ts +303 -0
- package/src/overlays/tooltip/tooltip.tsx +159 -92
- package/src/styles/theme.css +10 -2
- package/src/theme/context.tsx +192 -269
- package/src/theme/machine.ts +508 -0
- package/src/typography/text-shimmer/machine.ts +90 -0
- package/src/typography/text-shimmer/text-shimmer.css +71 -39
- package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/** Delay between OAuth status polls while the attempt is pending or the server is unreachable. */
|
|
2
|
+
export const MCP_SIGN_IN_POLL_MS = 1_000
|
|
3
|
+
|
|
4
|
+
/** How long the MCP server and resource sync after a sign-in may run before it shows a retryable error. */
|
|
5
|
+
export const MCP_SIGN_IN_SYNC_TIMEOUT_MS = 30_000
|
|
6
|
+
|
|
7
|
+
/** The server's answer to a status poll of the sign-in attempt. */
|
|
8
|
+
export type McpSignInAttemptStatus =
|
|
9
|
+
| { readonly status: "pending" | "complete" | "expired" }
|
|
10
|
+
| { readonly status: "failed"; readonly message: string }
|
|
11
|
+
|
|
12
|
+
export type McpSignInPollOutcome =
|
|
13
|
+
| { readonly status: "pending" }
|
|
14
|
+
| { readonly status: "complete" }
|
|
15
|
+
| { readonly status: "failed"; readonly message: string }
|
|
16
|
+
| { readonly status: "expired" }
|
|
17
|
+
/** The status request threw; the attempt may still be open on the server until its lifetime ends. */
|
|
18
|
+
| { readonly status: "unreachable"; readonly message: string }
|
|
19
|
+
|
|
20
|
+
export type McpSignInEntryError =
|
|
21
|
+
| { readonly type: "required" }
|
|
22
|
+
| { readonly type: "rejected"; readonly message: string }
|
|
23
|
+
|
|
24
|
+
export type McpSignInFailure =
|
|
25
|
+
| { readonly type: "auth-expired" }
|
|
26
|
+
| { readonly type: "auth-failed"; readonly message: string }
|
|
27
|
+
/** The sign-in succeeded, but reloading the MCP lists failed; a retry reloads them again. */
|
|
28
|
+
| { readonly type: "sync-failed"; readonly message: string }
|
|
29
|
+
| { readonly type: "sync-timed-out" }
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* An MCP sign-in that also accepts the redirect URL pasted from the browser. Requests take attempt numbers from
|
|
33
|
+
* `issued`, so a result whose attempt the state no longer holds is stale.
|
|
34
|
+
*/
|
|
35
|
+
export type McpSignInState =
|
|
36
|
+
| {
|
|
37
|
+
readonly status: "waiting"
|
|
38
|
+
readonly url: string
|
|
39
|
+
readonly issued: number
|
|
40
|
+
readonly pollAttempt: number
|
|
41
|
+
readonly value: string
|
|
42
|
+
/** The server accepted a pasted redirect URL and finishes the sign-in; the status poll reports the outcome. */
|
|
43
|
+
readonly pasteAccepted: boolean
|
|
44
|
+
readonly entryError: McpSignInEntryError | undefined
|
|
45
|
+
/** The last status request failed; shown if the attempt's lifetime ends before the server answers again. */
|
|
46
|
+
readonly unreachableError: string | undefined
|
|
47
|
+
}
|
|
48
|
+
| {
|
|
49
|
+
readonly status: "submitting"
|
|
50
|
+
readonly url: string
|
|
51
|
+
readonly issued: number
|
|
52
|
+
readonly pollAttempt: number
|
|
53
|
+
readonly submitAttempt: number
|
|
54
|
+
readonly value: string
|
|
55
|
+
readonly unreachableError: string | undefined
|
|
56
|
+
}
|
|
57
|
+
/** The sign-in succeeded; the MCP server and resource lists reload before the dialog closes. */
|
|
58
|
+
| { readonly status: "completing"; readonly url: string; readonly issued: number; readonly syncAttempt: number }
|
|
59
|
+
| { readonly status: "failed"; readonly url: string; readonly issued: number; readonly failure: McpSignInFailure }
|
|
60
|
+
| { readonly status: "done" }
|
|
61
|
+
| { readonly status: "disposed" }
|
|
62
|
+
|
|
63
|
+
export type McpSignInEvent =
|
|
64
|
+
| { readonly type: "browser.open" }
|
|
65
|
+
| { readonly type: "paste.changed"; readonly value: string }
|
|
66
|
+
| { readonly type: "paste.submitted" }
|
|
67
|
+
| { readonly type: "polled"; readonly pollAttempt: number; readonly outcome: McpSignInPollOutcome }
|
|
68
|
+
| { readonly type: "submit.succeeded"; readonly submitAttempt: number }
|
|
69
|
+
| { readonly type: "submit.failed"; readonly submitAttempt: number; readonly message: string }
|
|
70
|
+
/** The attempt's lifetime ended before the server reported an outcome. */
|
|
71
|
+
| { readonly type: "waiting.timed-out" }
|
|
72
|
+
| { readonly type: "sync.succeeded"; readonly syncAttempt: number }
|
|
73
|
+
| { readonly type: "sync.failed"; readonly syncAttempt: number; readonly message: string }
|
|
74
|
+
| { readonly type: "sync.timed-out"; readonly syncAttempt: number }
|
|
75
|
+
| { readonly type: "retry.requested" }
|
|
76
|
+
| { readonly type: "disposed" }
|
|
77
|
+
|
|
78
|
+
export type McpSignInEffect =
|
|
79
|
+
| { readonly type: "deadline.start"; readonly delayMs: number }
|
|
80
|
+
| { readonly type: "deadline.clear" }
|
|
81
|
+
/** Aborts the poll in flight or scheduled, then polls the attempt's status after `delayMs`. */
|
|
82
|
+
| { readonly type: "poll"; readonly pollAttempt: number; readonly delayMs: number }
|
|
83
|
+
| { readonly type: "poll.abort" }
|
|
84
|
+
| { readonly type: "submit"; readonly submitAttempt: number; readonly code: string }
|
|
85
|
+
| { readonly type: "submit.abort" }
|
|
86
|
+
/** Reloads the MCP server and resource lists, reporting back within `timeoutMs`. */
|
|
87
|
+
| { readonly type: "sync"; readonly syncAttempt: number; readonly timeoutMs: number }
|
|
88
|
+
| { readonly type: "sync.clear" }
|
|
89
|
+
/** Reloads the MCP lists without reporting back, for a sign-in the server finishes after the dialog closed. */
|
|
90
|
+
| { readonly type: "refresh" }
|
|
91
|
+
| { readonly type: "open-external"; readonly url: string }
|
|
92
|
+
/** Ends the attempt on the server, so a late browser callback cannot connect the server. */
|
|
93
|
+
| { readonly type: "cancel" }
|
|
94
|
+
| { readonly type: "close" }
|
|
95
|
+
|
|
96
|
+
export type McpSignInTransition = {
|
|
97
|
+
readonly state: McpSignInState
|
|
98
|
+
readonly effects: readonly McpSignInEffect[]
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function startMcpSignIn(input: { url: string; lifetimeMs: number }): McpSignInTransition {
|
|
102
|
+
return {
|
|
103
|
+
state: {
|
|
104
|
+
status: "waiting",
|
|
105
|
+
url: input.url,
|
|
106
|
+
issued: 1,
|
|
107
|
+
pollAttempt: 1,
|
|
108
|
+
value: "",
|
|
109
|
+
pasteAccepted: false,
|
|
110
|
+
entryError: undefined,
|
|
111
|
+
unreachableError: undefined,
|
|
112
|
+
},
|
|
113
|
+
effects: [
|
|
114
|
+
{ type: "deadline.start", delayMs: Math.max(0, input.lifetimeMs) },
|
|
115
|
+
{ type: "poll", pollAttempt: 1, delayMs: 0 },
|
|
116
|
+
],
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function transitionMcpSignIn(state: McpSignInState, event: McpSignInEvent): McpSignInTransition {
|
|
121
|
+
if (state.status === "disposed") return unchanged(state)
|
|
122
|
+
|
|
123
|
+
if (event.type === "disposed") return { state: { status: "disposed" }, effects: dispose(state) }
|
|
124
|
+
|
|
125
|
+
if (state.status === "done") return unchanged(state)
|
|
126
|
+
|
|
127
|
+
if (event.type === "browser.open") {
|
|
128
|
+
if (!selectMcpSignInView(state).showOpenBrowser) return unchanged(state)
|
|
129
|
+
|
|
130
|
+
return unchanged(state, [{ type: "open-external", url: state.url }])
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (state.status === "completing") return completing(state, event)
|
|
134
|
+
|
|
135
|
+
if (state.status === "failed") {
|
|
136
|
+
if (event.type !== "retry.requested") return unchanged(state)
|
|
137
|
+
|
|
138
|
+
if (state.failure.type !== "sync-failed" && state.failure.type !== "sync-timed-out") return unchanged(state)
|
|
139
|
+
|
|
140
|
+
return sync(state)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (event.type === "polled") return polled(state, event)
|
|
144
|
+
|
|
145
|
+
if (event.type === "waiting.timed-out")
|
|
146
|
+
return {
|
|
147
|
+
state: {
|
|
148
|
+
status: "failed",
|
|
149
|
+
url: state.url,
|
|
150
|
+
issued: state.issued,
|
|
151
|
+
failure: state.unreachableError
|
|
152
|
+
? { type: "auth-failed", message: state.unreachableError }
|
|
153
|
+
: { type: "auth-expired" },
|
|
154
|
+
},
|
|
155
|
+
// An accepted paste is the server's to finish; cancelling it could drop a sign-in that is about to succeed.
|
|
156
|
+
effects: [...stop(state), ...(state.status === "waiting" && state.pasteAccepted ? [] : [cancel])],
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (state.status === "submitting") {
|
|
160
|
+
if (event.type === "submit.succeeded" && event.submitAttempt === state.submitAttempt) {
|
|
161
|
+
const pollAttempt = state.issued + 1
|
|
162
|
+
|
|
163
|
+
// Polls at once instead of a tick later, so the dialog moves on as soon as the server finishes.
|
|
164
|
+
return {
|
|
165
|
+
state: {
|
|
166
|
+
status: "waiting",
|
|
167
|
+
url: state.url,
|
|
168
|
+
issued: pollAttempt,
|
|
169
|
+
pollAttempt,
|
|
170
|
+
value: state.value,
|
|
171
|
+
pasteAccepted: true,
|
|
172
|
+
entryError: undefined,
|
|
173
|
+
unreachableError: state.unreachableError,
|
|
174
|
+
},
|
|
175
|
+
effects: [{ type: "poll", pollAttempt, delayMs: 0 }],
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (event.type === "submit.failed" && event.submitAttempt === state.submitAttempt)
|
|
180
|
+
return unchanged({
|
|
181
|
+
status: "waiting",
|
|
182
|
+
url: state.url,
|
|
183
|
+
issued: state.issued,
|
|
184
|
+
pollAttempt: state.pollAttempt,
|
|
185
|
+
value: state.value,
|
|
186
|
+
pasteAccepted: false,
|
|
187
|
+
entryError: { type: "rejected", message: event.message },
|
|
188
|
+
unreachableError: state.unreachableError,
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
return unchanged(state)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (state.pasteAccepted) return unchanged(state)
|
|
195
|
+
|
|
196
|
+
if (event.type === "paste.changed") {
|
|
197
|
+
if (event.value === state.value && state.entryError === undefined) return unchanged(state)
|
|
198
|
+
|
|
199
|
+
return unchanged({ ...state, value: event.value, entryError: undefined })
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (event.type !== "paste.submitted") return unchanged(state)
|
|
203
|
+
|
|
204
|
+
const code = state.value.trim()
|
|
205
|
+
|
|
206
|
+
if (!code) return unchanged({ ...state, entryError: { type: "required" } })
|
|
207
|
+
|
|
208
|
+
const submitAttempt = state.issued + 1
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
state: {
|
|
212
|
+
status: "submitting",
|
|
213
|
+
url: state.url,
|
|
214
|
+
issued: submitAttempt,
|
|
215
|
+
pollAttempt: state.pollAttempt,
|
|
216
|
+
submitAttempt,
|
|
217
|
+
value: state.value,
|
|
218
|
+
unreachableError: state.unreachableError,
|
|
219
|
+
},
|
|
220
|
+
effects: [{ type: "submit", submitAttempt, code }],
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function selectMcpSignInView(state: McpSignInState) {
|
|
225
|
+
const failure = state.status === "failed" ? state.failure : undefined
|
|
226
|
+
const canRetry = failure?.type === "sync-failed" || failure?.type === "sync-timed-out"
|
|
227
|
+
const showForm = state.status === "waiting" || state.status === "submitting"
|
|
228
|
+
const locked = state.status === "submitting" || (state.status === "waiting" && state.pasteAccepted)
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
status: state.status,
|
|
232
|
+
failure,
|
|
233
|
+
canRetry,
|
|
234
|
+
// Once the server reports the sign-in done, the browser has nothing left to do.
|
|
235
|
+
showOpenBrowser: showForm || failure?.type === "auth-expired" || failure?.type === "auth-failed",
|
|
236
|
+
showForm,
|
|
237
|
+
value: showForm ? state.value : "",
|
|
238
|
+
entryError: state.status === "waiting" ? state.entryError : undefined,
|
|
239
|
+
locked,
|
|
240
|
+
submitting: state.status === "submitting",
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export type McpSignInView = ReturnType<typeof selectMcpSignInView>
|
|
245
|
+
|
|
246
|
+
const cancel: McpSignInEffect = { type: "cancel" }
|
|
247
|
+
|
|
248
|
+
function polled(
|
|
249
|
+
state: Extract<McpSignInState, { status: "waiting" | "submitting" }>,
|
|
250
|
+
event: Extract<McpSignInEvent, { type: "polled" }>,
|
|
251
|
+
): McpSignInTransition {
|
|
252
|
+
if (event.pollAttempt !== state.pollAttempt) return unchanged(state)
|
|
253
|
+
const outcome = event.outcome
|
|
254
|
+
|
|
255
|
+
// A failed request says nothing about the attempt, which may still be pending, so polling goes on until it expires.
|
|
256
|
+
if (outcome.status === "pending" || outcome.status === "unreachable") {
|
|
257
|
+
const pollAttempt = state.issued + 1
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
state: {
|
|
261
|
+
...state,
|
|
262
|
+
issued: pollAttempt,
|
|
263
|
+
pollAttempt,
|
|
264
|
+
unreachableError: outcome.status === "unreachable" ? outcome.message : undefined,
|
|
265
|
+
},
|
|
266
|
+
effects: [{ type: "poll", pollAttempt, delayMs: MCP_SIGN_IN_POLL_MS }],
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (outcome.status === "complete") {
|
|
271
|
+
const next = sync(state)
|
|
272
|
+
|
|
273
|
+
return { state: next.state, effects: [...stop(state), ...next.effects] }
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return {
|
|
277
|
+
state: {
|
|
278
|
+
status: "failed",
|
|
279
|
+
url: state.url,
|
|
280
|
+
issued: state.issued,
|
|
281
|
+
failure:
|
|
282
|
+
outcome.status === "expired" ? { type: "auth-expired" } : { type: "auth-failed", message: outcome.message },
|
|
283
|
+
},
|
|
284
|
+
effects: stop(state),
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function completing(
|
|
289
|
+
state: Extract<McpSignInState, { status: "completing" }>,
|
|
290
|
+
event: McpSignInEvent,
|
|
291
|
+
): McpSignInTransition {
|
|
292
|
+
if (event.type !== "sync.succeeded" && event.type !== "sync.failed" && event.type !== "sync.timed-out")
|
|
293
|
+
return unchanged(state)
|
|
294
|
+
|
|
295
|
+
if (event.syncAttempt !== state.syncAttempt) return unchanged(state)
|
|
296
|
+
|
|
297
|
+
if (event.type === "sync.succeeded")
|
|
298
|
+
return { state: { status: "done" }, effects: [{ type: "sync.clear" }, { type: "close" }] }
|
|
299
|
+
|
|
300
|
+
return {
|
|
301
|
+
state: {
|
|
302
|
+
status: "failed",
|
|
303
|
+
url: state.url,
|
|
304
|
+
issued: state.issued,
|
|
305
|
+
failure:
|
|
306
|
+
event.type === "sync.failed" ? { type: "sync-failed", message: event.message } : { type: "sync-timed-out" },
|
|
307
|
+
},
|
|
308
|
+
effects: [{ type: "sync.clear" }],
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function sync(state: Exclude<McpSignInState, { status: "done" | "disposed" }>): McpSignInTransition {
|
|
313
|
+
const syncAttempt = state.issued + 1
|
|
314
|
+
|
|
315
|
+
return {
|
|
316
|
+
state: { status: "completing", url: state.url, issued: syncAttempt, syncAttempt },
|
|
317
|
+
effects: [{ type: "sync", syncAttempt, timeoutMs: MCP_SIGN_IN_SYNC_TIMEOUT_MS }],
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Stops the requests and timers of a sign-in that is still waiting for the server.
|
|
322
|
+
function stop(state: Extract<McpSignInState, { status: "waiting" | "submitting" }>): McpSignInEffect[] {
|
|
323
|
+
return [
|
|
324
|
+
{ type: "deadline.clear" },
|
|
325
|
+
{ type: "poll.abort" },
|
|
326
|
+
...(state.status === "submitting" ? [{ type: "submit.abort" } as const] : []),
|
|
327
|
+
]
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Like the TUI, dismissing the dialog ends an attempt still waiting, so a late browser callback cannot connect the
|
|
331
|
+
// server. An accepted paste or a finished sign-in already saved its credential, which still has to reach the lists.
|
|
332
|
+
function dispose(state: Exclude<McpSignInState, { status: "disposed" }>): McpSignInEffect[] {
|
|
333
|
+
if (state.status === "waiting" || state.status === "submitting")
|
|
334
|
+
return [...stop(state), state.status === "waiting" && state.pasteAccepted ? { type: "refresh" } : cancel]
|
|
335
|
+
|
|
336
|
+
if (state.status === "completing") return [{ type: "sync.clear" }]
|
|
337
|
+
|
|
338
|
+
return []
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function unchanged(state: McpSignInState, effects: readonly McpSignInEffect[] = []): McpSignInTransition {
|
|
342
|
+
return { state, effects }
|
|
343
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { createMemo, createSignal, onCleanup, onMount, untrack } from "solid-js"
|
|
2
|
+
import {
|
|
3
|
+
selectMcpSignInView,
|
|
4
|
+
startMcpSignIn,
|
|
5
|
+
transitionMcpSignIn,
|
|
6
|
+
type McpSignInAttemptStatus,
|
|
7
|
+
type McpSignInEffect,
|
|
8
|
+
type McpSignInEvent,
|
|
9
|
+
} from "./machine"
|
|
10
|
+
|
|
11
|
+
export type { McpSignInAttemptStatus, McpSignInFailure, McpSignInView } from "./machine"
|
|
12
|
+
|
|
13
|
+
/** The server and host calls of one MCP sign-in; the app and the details plugin bind them to their own SDK. */
|
|
14
|
+
export type McpSignInHost = {
|
|
15
|
+
/** The authorization URL the browser opens. */
|
|
16
|
+
url: string
|
|
17
|
+
/** The attempt's lifetime in milliseconds, from the server's `time`; the two clocks may disagree. */
|
|
18
|
+
lifetime: number
|
|
19
|
+
/** Polls the attempt's status. */
|
|
20
|
+
status: (signal: AbortSignal) => Promise<McpSignInAttemptStatus>
|
|
21
|
+
/** Sends a redirect URL pasted from the browser. */
|
|
22
|
+
complete: (code: string, signal: AbortSignal) => Promise<void>
|
|
23
|
+
/** Ends the attempt on the server. */
|
|
24
|
+
cancel: () => Promise<void>
|
|
25
|
+
/** Reloads the MCP server and resource lists after the sign-in succeeded. */
|
|
26
|
+
sync: () => Promise<void>
|
|
27
|
+
openExternal: (url: string) => void
|
|
28
|
+
/** Closes the dialog once the lists reloaded. */
|
|
29
|
+
close: () => void
|
|
30
|
+
/** The text shown for a failed request. The SDK and the data layer reject with an `Error`. */
|
|
31
|
+
message: (error: Error) => string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Drives an MCP sign-in dialog's machine for the owner's lifetime: polls the attempt, sends a pasted redirect URL,
|
|
36
|
+
* reloads the MCP lists once it succeeded, and cancels an attempt still open when the owner is disposed.
|
|
37
|
+
*/
|
|
38
|
+
export function createMcpSignIn(host: McpSignInHost) {
|
|
39
|
+
const initial = startMcpSignIn({ url: host.url, lifetimeMs: host.lifetime })
|
|
40
|
+
const [signInState, setSignInState] = createSignal(initial.state)
|
|
41
|
+
const view = createMemo(() => selectMcpSignInView(signInState()))
|
|
42
|
+
// The machine decides when each of these starts and stops; the driver only holds them.
|
|
43
|
+
let deadlineTimer: ReturnType<typeof setTimeout> | undefined
|
|
44
|
+
let pollTimer: ReturnType<typeof setTimeout> | undefined
|
|
45
|
+
let syncTimer: ReturnType<typeof setTimeout> | undefined
|
|
46
|
+
let pollController: AbortController | undefined
|
|
47
|
+
let submitController: AbortController | undefined
|
|
48
|
+
|
|
49
|
+
const describe = (error: Error | string) => host.message(error instanceof Error ? error : new Error(error))
|
|
50
|
+
|
|
51
|
+
const dispatch = (event: McpSignInEvent) => {
|
|
52
|
+
const previous = untrack(signInState)
|
|
53
|
+
const next = transitionMcpSignIn(previous, event)
|
|
54
|
+
|
|
55
|
+
if (next.state !== previous) setSignInState(next.state)
|
|
56
|
+
next.effects.forEach((effect) => void Promise.try(() => run(effect)).catch((cause) => console.error(cause)))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const run = (effect: McpSignInEffect) => {
|
|
60
|
+
if (effect.type === "deadline.start") {
|
|
61
|
+
clearTimeout(deadlineTimer)
|
|
62
|
+
deadlineTimer = setTimeout(() => dispatch({ type: "waiting.timed-out" }), effect.delayMs)
|
|
63
|
+
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (effect.type === "deadline.clear") return clearTimeout(deadlineTimer)
|
|
68
|
+
|
|
69
|
+
if (effect.type === "poll") {
|
|
70
|
+
abortPoll()
|
|
71
|
+
|
|
72
|
+
if (effect.delayMs <= 0) return poll(effect.pollAttempt)
|
|
73
|
+
pollTimer = setTimeout(() => poll(effect.pollAttempt), effect.delayMs)
|
|
74
|
+
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (effect.type === "poll.abort") return abortPoll()
|
|
79
|
+
|
|
80
|
+
if (effect.type === "submit") {
|
|
81
|
+
submitController?.abort()
|
|
82
|
+
const controller = new AbortController()
|
|
83
|
+
submitController = controller
|
|
84
|
+
void Promise.try(() => host.complete(effect.code, controller.signal)).then(
|
|
85
|
+
() => {
|
|
86
|
+
if (!controller.signal.aborted) dispatch({ type: "submit.succeeded", submitAttempt: effect.submitAttempt })
|
|
87
|
+
},
|
|
88
|
+
(error) => {
|
|
89
|
+
if (controller.signal.aborted) return
|
|
90
|
+
dispatch({ type: "submit.failed", submitAttempt: effect.submitAttempt, message: describe(error) })
|
|
91
|
+
},
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (effect.type === "submit.abort") {
|
|
98
|
+
submitController?.abort()
|
|
99
|
+
submitController = undefined
|
|
100
|
+
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (effect.type === "sync") {
|
|
105
|
+
clearTimeout(syncTimer)
|
|
106
|
+
syncTimer = setTimeout(
|
|
107
|
+
() => dispatch({ type: "sync.timed-out", syncAttempt: effect.syncAttempt }),
|
|
108
|
+
effect.timeoutMs,
|
|
109
|
+
)
|
|
110
|
+
void Promise.try(host.sync).then(
|
|
111
|
+
() => dispatch({ type: "sync.succeeded", syncAttempt: effect.syncAttempt }),
|
|
112
|
+
(error) => dispatch({ type: "sync.failed", syncAttempt: effect.syncAttempt, message: describe(error) }),
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (effect.type === "sync.clear") return clearTimeout(syncTimer)
|
|
119
|
+
|
|
120
|
+
if (effect.type === "refresh") return void Promise.try(host.sync).catch(() => undefined)
|
|
121
|
+
|
|
122
|
+
if (effect.type === "open-external") return host.openExternal(effect.url)
|
|
123
|
+
|
|
124
|
+
if (effect.type === "cancel") return void Promise.try(host.cancel).catch(() => undefined)
|
|
125
|
+
|
|
126
|
+
host.close()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const poll = (pollAttempt: number) => {
|
|
130
|
+
const controller = new AbortController()
|
|
131
|
+
pollController = controller
|
|
132
|
+
void Promise.try(() => host.status(controller.signal)).then(
|
|
133
|
+
(status) => {
|
|
134
|
+
if (controller.signal.aborted) return
|
|
135
|
+
dispatch({
|
|
136
|
+
type: "polled",
|
|
137
|
+
pollAttempt,
|
|
138
|
+
outcome:
|
|
139
|
+
status.status === "failed" ? { status: "failed", message: status.message } : { status: status.status },
|
|
140
|
+
})
|
|
141
|
+
},
|
|
142
|
+
(error) => {
|
|
143
|
+
if (controller.signal.aborted) return
|
|
144
|
+
dispatch({ type: "polled", pollAttempt, outcome: { status: "unreachable", message: describe(error) } })
|
|
145
|
+
},
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const abortPoll = () => {
|
|
150
|
+
clearTimeout(pollTimer)
|
|
151
|
+
pollController?.abort()
|
|
152
|
+
pollController = undefined
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
onMount(() => initial.effects.forEach(run))
|
|
156
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
157
|
+
|
|
158
|
+
return { view, dispatch }
|
|
159
|
+
}
|