@opencode/ui 0.0.0-dev-794 → 0.0.0-dev-821

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.
@@ -1,5 +1,8 @@
1
1
  import fuzzysort from "fuzzysort"
2
2
 
3
+ /** How long an async `items(query)` may run before the list reports it failed rather than loading forever. */
4
+ export const LIST_LOAD_TIMEOUT_MS = 15_000
5
+
3
6
  export type ListGroup<T> = { category: string; items: T[] }
4
7
 
5
8
  export type ListConfig<T> = {
@@ -25,10 +28,17 @@ type ListData<T> = {
25
28
  readonly highlight: ListHighlight
26
29
  }
27
30
 
28
- /** `loading` keeps the last rows on screen; `epoch` is the last attempt that settled. */
31
+ /**
32
+ * `loading` keeps the last rows on screen; `epoch` is the last attempt that settled. `failed` means the current
33
+ * query's async load rejected or timed out, which the list reports instead of "No results". `disposed` keeps the last
34
+ * data for readers that run during cleanup and ignores every event.
35
+ */
29
36
  export type ListState<T> =
30
- | (ListData<T> & { readonly status: "ready"; readonly epoch: number })
37
+ | (ListData<T> & { readonly status: "ready" | "failed"; readonly epoch: number })
31
38
  | (ListData<T> & { readonly status: "loading"; readonly attempt: number })
39
+ | (ListData<T> & { readonly status: "disposed" })
40
+
41
+ type LiveListState<T> = Exclude<ListState<T>, { readonly status: "disposed" }>
32
42
 
33
43
  export type ListKeyDown = {
34
44
  readonly type: "key.down"
@@ -51,17 +61,26 @@ export type ListEvent<T> =
51
61
  | { readonly type: "items.reload" }
52
62
  | { readonly type: "load.resolved"; readonly attempt: number; readonly items: T[] }
53
63
  | { readonly type: "load.failed"; readonly attempt: number }
64
+ | { readonly type: "load.timed-out"; readonly attempt: number }
54
65
  | { readonly type: "pointer.move"; readonly key: string; readonly moved: boolean }
55
66
  | { readonly type: "pointer.leave" }
56
67
  | { readonly type: "active.set"; readonly key: string | null }
57
68
  | { readonly type: "active.reset" }
69
+ | { readonly type: "disposed" }
58
70
  | ListKeyDown
59
71
 
60
72
  export type ListEffect<T> =
61
73
  | { readonly type: "load"; readonly attempt: number; readonly query: string }
74
+ /** Stops the settled or abandoned attempt's timeout. */
75
+ | { readonly type: "cancel.load" }
62
76
  | { readonly type: "notify.filter"; readonly value: string }
63
77
  | { readonly type: "select"; readonly item: T; readonly index: number }
64
78
 
79
+ export type ListEmptyState =
80
+ | { readonly type: "loading" }
81
+ | { readonly type: "failed" }
82
+ | { readonly type: "empty"; readonly query: string }
83
+
65
84
  export type ListTransition<T> = {
66
85
  readonly state: ListState<T>
67
86
  readonly effects: ListEffect<T>[]
@@ -81,6 +100,21 @@ export function createListState<T>(filter?: string): ListState<T> {
81
100
  }
82
101
 
83
102
  export function transitionList<T>(state: ListState<T>, event: ListEvent<T>, config: ListConfig<T>): ListTransition<T> {
103
+ if (state.status === "disposed") return unchanged(state)
104
+
105
+ if (event.type === "disposed")
106
+ return changed(
107
+ {
108
+ status: "disposed",
109
+ filter: state.filter,
110
+ groups: state.groups,
111
+ rows: state.rows,
112
+ keys: state.keys,
113
+ highlight: state.highlight,
114
+ },
115
+ state.status === "loading" ? [{ type: "cancel.load" }] : [],
116
+ )
117
+
84
118
  if (event.type === "query.change") return queryChanged(state, event.value, event.force === true)
85
119
 
86
120
  if (event.type === "filter.controlled") {
@@ -99,22 +133,29 @@ export function transitionList<T>(state: ListState<T>, event: ListEvent<T>, conf
99
133
 
100
134
  if (event.type === "items.reload") return load(state, state.filter)
101
135
 
102
- if (event.type === "load.resolved" || event.type === "load.failed") {
103
- if (state.status !== "loading" || event.attempt !== state.attempt) return unchanged(state)
136
+ if (event.type === "load.resolved" || event.type === "load.failed" || event.type === "load.timed-out") {
137
+ const current = state.status === "loading" ? state.attempt : state.epoch
138
+ // A slow search that timed out still shows its results when they arrive, until a newer attempt starts.
139
+ const late = state.status === "failed" && event.type === "load.resolved"
140
+
141
+ if ((state.status !== "loading" && !late) || event.attempt !== current) return unchanged(state)
104
142
  const groups = event.type === "load.resolved" ? filterItems(event.items, state.filter.query, config) : []
105
143
  const rows = groups.flatMap((group) => group.items)
106
144
  const keys = rows.map(config.key)
107
145
 
108
- return changed({
109
- status: "ready",
110
- epoch: state.attempt,
111
- filter: state.filter,
112
- groups,
113
- rows,
114
- keys,
115
- // Each result set opens on its first row; `current` only marks the selected item.
116
- highlight: { mode: state.highlight.mode, activeKey: keys[0] ?? null },
117
- })
146
+ return changed(
147
+ {
148
+ status: event.type === "load.resolved" ? "ready" : "failed",
149
+ epoch: current,
150
+ filter: state.filter,
151
+ groups,
152
+ rows,
153
+ keys,
154
+ // Each result set opens on its first row; `current` only marks the selected item.
155
+ highlight: { mode: state.highlight.mode, activeKey: keys[0] ?? null },
156
+ },
157
+ late ? [] : [{ type: "cancel.load" }],
158
+ )
118
159
  }
119
160
 
120
161
  if (event.type === "pointer.move") {
@@ -137,6 +178,23 @@ export function transitionList<T>(state: ListState<T>, event: ListEvent<T>, conf
137
178
  return keyDown(state, event)
138
179
  }
139
180
 
181
+ export function selectListLoading<T>(state: ListState<T>) {
182
+ return state.status === "loading"
183
+ }
184
+
185
+ export function selectListFailed<T>(state: ListState<T>) {
186
+ return state.status === "failed"
187
+ }
188
+
189
+ /** What the list says when it has no rows to show. */
190
+ export function selectListEmptyState<T>(state: ListState<T>): ListEmptyState {
191
+ if (state.status === "loading") return { type: "loading" }
192
+
193
+ if (state.status === "failed") return { type: "failed" }
194
+
195
+ return { type: "empty", query: state.filter.query }
196
+ }
197
+
140
198
  /** The highlighted row and its index among all rows. */
141
199
  export function listActive<T>(state: Pick<ListState<T>, "rows" | "keys" | "highlight">) {
142
200
  const index = state.highlight.activeKey === null ? -1 : state.keys.indexOf(state.highlight.activeKey)
@@ -175,7 +233,7 @@ export function groupItems<T>(items: T[], config: Pick<ListConfig<T>, "groupBy"
175
233
  return config.sortGroupsBy ? groups.sort(config.sortGroupsBy) : groups
176
234
  }
177
235
 
178
- function queryChanged<T>(state: ListState<T>, value: string, force: boolean): ListTransition<T> {
236
+ function queryChanged<T>(state: LiveListState<T>, value: string, force: boolean): ListTransition<T> {
179
237
  const same = value === state.filter.query
180
238
 
181
239
  if (same && !force) return unchanged(state)
@@ -188,7 +246,7 @@ function queryChanged<T>(state: ListState<T>, value: string, force: boolean): Li
188
246
  return { ...next, effects: [notify, ...next.effects] }
189
247
  }
190
248
 
191
- function load<T>(state: ListState<T>, filter: ListFilter): ListTransition<T> {
249
+ function load<T>(state: LiveListState<T>, filter: ListFilter): ListTransition<T> {
192
250
  const attempt = (state.status === "loading" ? state.attempt : state.epoch) + 1
193
251
 
194
252
  return changed(
@@ -205,7 +263,7 @@ function load<T>(state: ListState<T>, filter: ListFilter): ListTransition<T> {
205
263
  )
206
264
  }
207
265
 
208
- function highlight<T>(state: ListState<T>, activeKey: string | null): ListTransition<T> {
266
+ function highlight<T>(state: LiveListState<T>, activeKey: string | null): ListTransition<T> {
209
267
  if (activeKey === state.highlight.activeKey) return unchanged(state)
210
268
 
211
269
  return changed({ ...state, highlight: { mode: state.highlight.mode, activeKey } })
@@ -1,5 +1,13 @@
1
1
  import { batch, createComputed, createMemo, createReaction, createSignal, on, onCleanup, untrack } from "solid-js"
2
- import { createListState, transitionList, type ListEvent, type ListGroup } from "./list-machine"
2
+ import {
3
+ createListState,
4
+ LIST_LOAD_TIMEOUT_MS,
5
+ selectListFailed,
6
+ selectListLoading,
7
+ transitionList,
8
+ type ListEvent,
9
+ type ListGroup,
10
+ } from "./list-machine"
3
11
 
4
12
  export { groupItems } from "./list-machine"
5
13
 
@@ -20,8 +28,7 @@ export interface FilteredListProps<T> {
20
28
 
21
29
  export function useFilteredList<T>(props: FilteredListProps<T>) {
22
30
  const [state, setState] = createSignal(createListState<T>(props.filter))
23
- const lifetime = new AbortController()
24
- onCleanup(() => lifetime.abort())
31
+ let loadTimer: ReturnType<typeof setTimeout> | undefined
25
32
 
26
33
  const dispatch = (event: ListEvent<T>, keyboard?: KeyboardEvent) => {
27
34
  const result = transitionList(untrack(state), event, props)
@@ -32,6 +39,8 @@ export function useFilteredList<T>(props: FilteredListProps<T>) {
32
39
  batch(() => {
33
40
  setState(() => result.state)
34
41
  result.effects.forEach((effect) => {
42
+ if (effect.type === "cancel.load") clearTimeout(loadTimer)
43
+
35
44
  if (effect.type === "load") load(effect.attempt, effect.query)
36
45
  })
37
46
  })
@@ -45,23 +54,22 @@ export function useFilteredList<T>(props: FilteredListProps<T>) {
45
54
  // Tracks what the last load read (`props.items`, or the signals `items(query)` reads) and reloads when it changes.
46
55
  const track = createReaction(() => dispatch({ type: "items.reload" }))
47
56
 
48
- const load = (attempt: number, query: string) =>
57
+ const load = (attempt: number, query: string) => {
58
+ clearTimeout(loadTimer)
49
59
  track(() => {
50
60
  // oxlint-disable-next-line anti-slop/no-runtime-typeof -- `items` is either a list or a loader for one
51
61
  const items = typeof props.items === "function" ? props.items(query) : props.items
52
62
  untrack(() => settle(attempt, items))
53
63
  })
64
+ }
54
65
 
55
66
  const settle = (attempt: number, items: T[] | Promise<T[]>) => {
56
67
  if (Array.isArray(items)) return dispatch({ type: "load.resolved", attempt, items })
57
68
 
69
+ loadTimer = setTimeout(() => dispatch({ type: "load.timed-out", attempt }), LIST_LOAD_TIMEOUT_MS)
58
70
  void items.then(
59
- (items) => {
60
- if (!lifetime.signal.aborted) dispatch({ type: "load.resolved", attempt, items: items ?? [] })
61
- },
62
- () => {
63
- if (!lifetime.signal.aborted) dispatch({ type: "load.failed", attempt })
64
- },
71
+ (items) => dispatch({ type: "load.resolved", attempt, items: items ?? [] }),
72
+ () => dispatch({ type: "load.failed", attempt }),
65
73
  )
66
74
  }
67
75
 
@@ -73,10 +81,13 @@ export function useFilteredList<T>(props: FilteredListProps<T>) {
73
81
  ),
74
82
  )
75
83
 
84
+ onCleanup(() => dispatch({ type: "disposed" }))
85
+
76
86
  dispatch({ type: "items.reload" })
77
87
 
78
88
  const groups = createMemo(() => state().groups)
79
- const loading = createMemo(() => state().status === "loading")
89
+ const loading = createMemo(() => selectListLoading(state()))
90
+ const failed = createMemo(() => selectListFailed(state()))
80
91
 
81
92
  return {
82
93
  state,
@@ -88,8 +99,10 @@ export function useFilteredList<T>(props: FilteredListProps<T>) {
88
99
  get loading() {
89
100
  return loading()
90
101
  },
91
- get state(): "ready" | "refreshing" {
92
- return loading() ? "refreshing" : "ready"
102
+ get state(): "ready" | "refreshing" | "errored" {
103
+ if (loading()) return "refreshing"
104
+
105
+ return failed() ? "errored" : "ready"
93
106
  },
94
107
  },
95
108
  filter: createMemo(() => state().filter.query),
package/src/i18n/en.ts CHANGED
@@ -151,6 +151,7 @@ const source = {
151
151
 
152
152
  "ui.list.loading": "Loading",
153
153
  "ui.list.empty": "No results",
154
+ "ui.list.failed": "Failed to load results",
154
155
  "ui.list.clearFilter": "Clear filter",
155
156
  "ui.list.emptyWithFilter.prefix": "No results for",
156
157
  "ui.list.emptyWithFilter.suffix": "",
@@ -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
+ }