@kelpie/ui 0.22.0 → 0.23.0

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.
Files changed (196) hide show
  1. package/dist/api/resource.d.ts +12 -0
  2. package/dist/api/resource.d.ts.map +1 -1
  3. package/dist/api/resource.js +11 -0
  4. package/dist/api/resource.js.map +1 -1
  5. package/dist/api/resources/decisions.d.ts +31 -0
  6. package/dist/api/resources/decisions.d.ts.map +1 -1
  7. package/dist/api/resources/decisions.js +51 -1
  8. package/dist/api/resources/decisions.js.map +1 -1
  9. package/dist/api/resources/oauth.d.ts +2 -0
  10. package/dist/api/resources/oauth.d.ts.map +1 -1
  11. package/dist/api/resources/oauth.js +13 -1
  12. package/dist/api/resources/oauth.js.map +1 -1
  13. package/dist/api/resources/personIntake.d.ts +15 -3
  14. package/dist/api/resources/personIntake.d.ts.map +1 -1
  15. package/dist/api/resources/personIntake.js +38 -9
  16. package/dist/api/resources/personIntake.js.map +1 -1
  17. package/dist/api/resources/session.d.ts.map +1 -1
  18. package/dist/api/resources/session.js +4 -0
  19. package/dist/api/resources/session.js.map +1 -1
  20. package/dist/app/KelpieApp.d.ts.map +1 -1
  21. package/dist/app/KelpieApp.js +2 -1
  22. package/dist/app/KelpieApp.js.map +1 -1
  23. package/dist/components/AddRecordPanel.d.ts +35 -0
  24. package/dist/components/AddRecordPanel.d.ts.map +1 -0
  25. package/dist/components/AddRecordPanel.js +35 -0
  26. package/dist/components/AddRecordPanel.js.map +1 -0
  27. package/dist/components/AiDrawer.d.ts.map +1 -1
  28. package/dist/components/AiDrawer.js +29 -6
  29. package/dist/components/AiDrawer.js.map +1 -1
  30. package/dist/components/ApiKeysPanel.d.ts.map +1 -1
  31. package/dist/components/ApiKeysPanel.js +101 -44
  32. package/dist/components/ApiKeysPanel.js.map +1 -1
  33. package/dist/components/CustomFieldsPanel.d.ts.map +1 -1
  34. package/dist/components/CustomFieldsPanel.js +5 -2
  35. package/dist/components/CustomFieldsPanel.js.map +1 -1
  36. package/dist/components/DecisionProvenance.d.ts +27 -0
  37. package/dist/components/DecisionProvenance.d.ts.map +1 -0
  38. package/dist/components/DecisionProvenance.js +53 -0
  39. package/dist/components/DecisionProvenance.js.map +1 -0
  40. package/dist/components/DecisionsPanel.d.ts +3 -0
  41. package/dist/components/DecisionsPanel.d.ts.map +1 -1
  42. package/dist/components/DecisionsPanel.js +9 -5
  43. package/dist/components/DecisionsPanel.js.map +1 -1
  44. package/dist/components/MarkdownView.d.ts.map +1 -1
  45. package/dist/components/MarkdownView.js +18 -2
  46. package/dist/components/MarkdownView.js.map +1 -1
  47. package/dist/components/NotesPanel.d.ts.map +1 -1
  48. package/dist/components/NotesPanel.js +7 -3
  49. package/dist/components/NotesPanel.js.map +1 -1
  50. package/dist/components/OriginLabel.d.ts +5 -0
  51. package/dist/components/OriginLabel.d.ts.map +1 -0
  52. package/dist/components/OriginLabel.js +22 -0
  53. package/dist/components/OriginLabel.js.map +1 -0
  54. package/dist/components/Shell.d.ts.map +1 -1
  55. package/dist/components/Shell.js +17 -5
  56. package/dist/components/Shell.js.map +1 -1
  57. package/dist/components/aiDrawerState.d.ts +12 -0
  58. package/dist/components/aiDrawerState.d.ts.map +1 -1
  59. package/dist/components/aiDrawerState.js.map +1 -1
  60. package/dist/components/personIntakeTask.d.ts +15 -0
  61. package/dist/components/personIntakeTask.d.ts.map +1 -1
  62. package/dist/components/personIntakeTask.js +42 -0
  63. package/dist/components/personIntakeTask.js.map +1 -1
  64. package/dist/lib/personIntakeRunStorage.d.ts +15 -0
  65. package/dist/lib/personIntakeRunStorage.d.ts.map +1 -0
  66. package/dist/lib/personIntakeRunStorage.js +88 -0
  67. package/dist/lib/personIntakeRunStorage.js.map +1 -0
  68. package/dist/modules/PersonIntakeWizard.d.ts +1 -1
  69. package/dist/modules/PersonIntakeWizard.d.ts.map +1 -1
  70. package/dist/modules/PersonIntakeWizard.js +243 -29
  71. package/dist/modules/PersonIntakeWizard.js.map +1 -1
  72. package/dist/modules/ai.d.ts +1 -1
  73. package/dist/modules/ai.js +1 -1
  74. package/dist/pages/CompaniesPage.d.ts.map +1 -1
  75. package/dist/pages/CompaniesPage.js +13 -6
  76. package/dist/pages/CompaniesPage.js.map +1 -1
  77. package/dist/pages/DealsPage.d.ts.map +1 -1
  78. package/dist/pages/DealsPage.js +16 -8
  79. package/dist/pages/DealsPage.js.map +1 -1
  80. package/dist/pages/DecisionsPage.d.ts.map +1 -1
  81. package/dist/pages/DecisionsPage.js +7 -3
  82. package/dist/pages/DecisionsPage.js.map +1 -1
  83. package/dist/pages/EnquiriesPage.d.ts.map +1 -1
  84. package/dist/pages/EnquiriesPage.js +14 -6
  85. package/dist/pages/EnquiriesPage.js.map +1 -1
  86. package/dist/pages/FundraisingPage.d.ts.map +1 -1
  87. package/dist/pages/FundraisingPage.js +16 -11
  88. package/dist/pages/FundraisingPage.js.map +1 -1
  89. package/dist/pages/OpportunitiesPage.d.ts.map +1 -1
  90. package/dist/pages/OpportunitiesPage.js +14 -6
  91. package/dist/pages/OpportunitiesPage.js.map +1 -1
  92. package/dist/pages/PartnershipsPage.d.ts.map +1 -1
  93. package/dist/pages/PartnershipsPage.js +16 -11
  94. package/dist/pages/PartnershipsPage.js.map +1 -1
  95. package/dist/pages/PeoplePage.d.ts.map +1 -1
  96. package/dist/pages/PeoplePage.js +13 -8
  97. package/dist/pages/PeoplePage.js.map +1 -1
  98. package/dist/pages/PersonDetail.js +2 -2
  99. package/dist/pages/PersonDetail.js.map +1 -1
  100. package/dist/pages/SearchPage.d.ts.map +1 -1
  101. package/dist/pages/SearchPage.js +2 -1
  102. package/dist/pages/SearchPage.js.map +1 -1
  103. package/dist/pages/account/ConnectedAppsPage.d.ts +0 -5
  104. package/dist/pages/account/ConnectedAppsPage.d.ts.map +1 -1
  105. package/dist/pages/account/ConnectedAppsPage.js +38 -6
  106. package/dist/pages/account/ConnectedAppsPage.js.map +1 -1
  107. package/dist/pages/admin/AgentsPanel.d.ts +10 -0
  108. package/dist/pages/admin/AgentsPanel.d.ts.map +1 -0
  109. package/dist/pages/admin/AgentsPanel.js +81 -0
  110. package/dist/pages/admin/AgentsPanel.js.map +1 -0
  111. package/dist/pages/admin/AiAdminPage.d.ts.map +1 -1
  112. package/dist/pages/admin/AiAdminPage.js +9 -6
  113. package/dist/pages/admin/AiAdminPage.js.map +1 -1
  114. package/dist/pages/admin/AiPage.js +1 -1
  115. package/dist/pages/admin/TeamPage.d.ts.map +1 -1
  116. package/dist/pages/admin/TeamPage.js +16 -3
  117. package/dist/pages/admin/TeamPage.js.map +1 -1
  118. package/dist/pages/ai/AiAccessPage.d.ts +11 -0
  119. package/dist/pages/ai/AiAccessPage.d.ts.map +1 -0
  120. package/dist/pages/ai/AiAccessPage.js +16 -0
  121. package/dist/pages/ai/AiAccessPage.js.map +1 -0
  122. package/dist/pages/ai/McpPanel.d.ts +7 -0
  123. package/dist/pages/ai/McpPanel.d.ts.map +1 -0
  124. package/dist/pages/ai/McpPanel.js +86 -0
  125. package/dist/pages/ai/McpPanel.js.map +1 -0
  126. package/dist/pages/auth/nextPath.d.ts +6 -0
  127. package/dist/pages/auth/nextPath.d.ts.map +1 -1
  128. package/dist/pages/auth/nextPath.js +16 -1
  129. package/dist/pages/auth/nextPath.js.map +1 -1
  130. package/dist/pages/dashboard/sections.d.ts.map +1 -1
  131. package/dist/pages/dashboard/sections.js +4 -2
  132. package/dist/pages/dashboard/sections.js.map +1 -1
  133. package/dist/pages/forms/EmbedPanel.js +1 -1
  134. package/dist/pages/forms/EmbedPanel.js.map +1 -1
  135. package/dist/pages/forms/FormEmails.d.ts.map +1 -1
  136. package/dist/pages/forms/FormEmails.js +6 -1
  137. package/dist/pages/forms/FormEmails.js.map +1 -1
  138. package/dist/pages/forms/FormSettings.d.ts.map +1 -1
  139. package/dist/pages/forms/FormSettings.js +6 -2
  140. package/dist/pages/forms/FormSettings.js.map +1 -1
  141. package/dist/pages/forms/SubmissionDetail.js +15 -3
  142. package/dist/pages/forms/SubmissionDetail.js.map +1 -1
  143. package/dist/pages/forms/SubmissionsTable.js +5 -2
  144. package/dist/pages/forms/SubmissionsTable.js.map +1 -1
  145. package/dist/pages/oauth/ConsentPage.d.ts.map +1 -1
  146. package/dist/pages/oauth/ConsentPage.js +132 -15
  147. package/dist/pages/oauth/ConsentPage.js.map +1 -1
  148. package/dist/registry/contributions.d.ts +1 -1
  149. package/package.json +3 -3
  150. package/src/api/resource.ts +16 -0
  151. package/src/api/resources/decisions.ts +81 -1
  152. package/src/api/resources/oauth.ts +15 -1
  153. package/src/api/resources/personIntake.ts +54 -13
  154. package/src/api/resources/session.ts +4 -0
  155. package/src/app/KelpieApp.tsx +4 -2
  156. package/src/components/AddRecordPanel.tsx +143 -0
  157. package/src/components/AiDrawer.tsx +34 -7
  158. package/src/components/ApiKeysPanel.tsx +193 -75
  159. package/src/components/CustomFieldsPanel.tsx +15 -5
  160. package/src/components/DecisionProvenance.tsx +95 -0
  161. package/src/components/DecisionsPanel.tsx +30 -2
  162. package/src/components/MarkdownView.tsx +45 -11
  163. package/src/components/NotesPanel.tsx +15 -6
  164. package/src/components/OriginLabel.tsx +26 -0
  165. package/src/components/Shell.tsx +30 -6
  166. package/src/components/aiDrawerState.ts +12 -0
  167. package/src/components/personIntakeTask.tsx +48 -0
  168. package/src/lib/personIntakeRunStorage.ts +97 -0
  169. package/src/modules/PersonIntakeWizard.tsx +391 -30
  170. package/src/modules/ai.tsx +1 -1
  171. package/src/pages/CompaniesPage.tsx +22 -5
  172. package/src/pages/DealsPage.tsx +25 -8
  173. package/src/pages/DecisionsPage.tsx +28 -1
  174. package/src/pages/EnquiriesPage.tsx +23 -5
  175. package/src/pages/FundraisingPage.tsx +25 -11
  176. package/src/pages/OpportunitiesPage.tsx +23 -5
  177. package/src/pages/PartnershipsPage.tsx +25 -11
  178. package/src/pages/PeoplePage.tsx +22 -7
  179. package/src/pages/PersonDetail.tsx +4 -2
  180. package/src/pages/SearchPage.tsx +3 -0
  181. package/src/pages/account/ConnectedAppsPage.tsx +97 -14
  182. package/src/pages/admin/{McpPanel.tsx → AgentsPanel.tsx} +11 -168
  183. package/src/pages/admin/AiAdminPage.tsx +16 -11
  184. package/src/pages/admin/AiPage.tsx +1 -1
  185. package/src/pages/admin/TeamPage.tsx +33 -0
  186. package/src/pages/ai/AiAccessPage.tsx +23 -0
  187. package/src/pages/ai/McpPanel.tsx +261 -0
  188. package/src/pages/auth/nextPath.ts +18 -1
  189. package/src/pages/dashboard/sections.tsx +4 -0
  190. package/src/pages/forms/EmbedPanel.tsx +3 -2
  191. package/src/pages/forms/FormEmails.tsx +11 -2
  192. package/src/pages/forms/FormSettings.tsx +5 -0
  193. package/src/pages/forms/SubmissionDetail.tsx +23 -4
  194. package/src/pages/forms/SubmissionsTable.tsx +9 -1
  195. package/src/pages/oauth/ConsentPage.tsx +271 -33
  196. package/src/registry/contributions.ts +1 -1
@@ -2,27 +2,38 @@ import {
2
2
  PERSON_INTAKE_KIND_LABELS,
3
3
  PERSON_INTAKE_KINDS,
4
4
  PERSON_INTAKE_PERSON_KEY,
5
+ personIntakeCandidateBody,
5
6
  personIntakeDependencies,
7
+ personIntakeCandidateSchema,
6
8
  } from '@kelpie/schemas'
7
9
  import type {
8
10
  PersonIntakeCandidate,
11
+ PersonIntakeCandidateWire,
9
12
  PersonIntakeIdentifyResult,
10
13
  PersonIntakeItem,
11
14
  PersonIntakeKind,
12
15
  PersonIntakeResearchResult,
13
16
  PersonIntakeResult,
14
17
  PersonIntakeResultStatus,
18
+ PersonIntakeSavedRun,
15
19
  PersonIntakeSource,
16
20
  } from '@kelpie/schemas'
17
21
  import { useEffect, useState } from 'react'
18
22
  import { Link } from 'react-router'
19
23
 
20
24
  import { useAiServiceLabel } from '../api/resources/ai.ts'
21
- import { useApplyPersonIntake, useIdentifyPerson, useResearchPerson } from '../api/resources/personIntake.ts'
25
+ import {
26
+ useApplyPersonIntake,
27
+ usePersonIntakeRun,
28
+ useStartIdentify,
29
+ useStartResearch,
30
+ } from '../api/resources/personIntake.ts'
31
+ import { useSession } from '../api/resources/session.ts'
22
32
  import { Chip } from '../components/Chip.tsx'
23
33
  import type { ChipTone } from '../components/Chip.tsx'
24
34
  import { IntakeFrame, NotesField } from '../components/PersonIntakePanel.tsx'
25
- import { ErrorPanel } from '../components/QueryState.tsx'
35
+ import { ErrorPanel, LoadingPanel } from '../components/QueryState.tsx'
36
+ import { clearSavedIntakeRuns, readSavedIntakeRun, saveIntakeRun } from '../lib/personIntakeRunStorage.ts'
26
37
  import type { PersonIntakeWizardProps } from '../registry/contributions.ts'
27
38
  import { effectiveSelection } from './personIntakeSelection.ts'
28
39
 
@@ -31,19 +42,65 @@ import { effectiveSelection } from './personIntakeSelection.ts'
31
42
  * person, choose what to create, done. Spec: person intake, in the `ai`
32
43
  * module's docs (`docs/agents/kelpie-ai.md`).
33
44
  *
34
- * The wizard holds no records of its own. Each step is one call to
35
- * `/v1/ai/person-intake/*`, and the last writes only the ticked items.
45
+ * The wizard holds no records of its own. `identify` and `research` each start
46
+ * an AI run on the server, and the wizard reads the run every 2 seconds until
47
+ * it settles. The run and the step are kept in `sessionStorage`, so a reload
48
+ * picks the task up again; the server keeps a run's result for an hour. The
49
+ * last step writes only the ticked items.
50
+ */
51
+
52
+ /**
53
+ * What the Confirm step shows. It names no run: when the step is rebuilt from
54
+ * a saved candidate after a reload, the identify run that found the person
55
+ * may be gone, and there is no id to give it. The run, when it is still known,
56
+ * is the step's `identifyRunId`.
36
57
  */
58
+ type Identified = Omit<PersonIntakeIdentifyResult, 'runId'>
37
59
 
38
60
  type Step =
61
+ | { readonly kind: 'restoring'; readonly saved: PersonIntakeSavedRun['step'] }
39
62
  | { readonly kind: 'notes' }
40
- | { readonly kind: 'confirm'; readonly identified: PersonIntakeIdentifyResult }
63
+ | {
64
+ readonly kind: 'confirm'
65
+ readonly identified: Identified
66
+ /** The identify run behind this step, when its result is still on the server. */
67
+ readonly identifyRunId: string | null
68
+ readonly target?: string | null
69
+ }
41
70
  | { readonly kind: 'choose'; readonly research: PersonIntakeResearchResult }
42
71
  | { readonly kind: 'done'; readonly results: readonly PersonIntakeResult[] }
43
72
 
73
+ /** A run the wizard is waiting on, and what it was started with. */
74
+ interface WatchedRun {
75
+ readonly runId: string
76
+ readonly text: string
77
+ /** The identify run a research run was started from. */
78
+ readonly identifyRunId: string | null
79
+ /** The confirmed person, for a research run. */
80
+ readonly candidate: PersonIntakeCandidateWire | null
81
+ readonly existingPersonId: string | null
82
+ /**
83
+ * After a reload of a failed research run, the person it was started with.
84
+ * The wizard reads the identify run again for the full Confirm step; when
85
+ * that run or its result has gone, it rebuilds Confirm from this instead.
86
+ */
87
+ readonly fallback: { readonly candidate: PersonIntakeCandidateWire; readonly existingPersonId: string | null } | null
88
+ }
89
+
90
+ /** What the wizard keeps for a reload, less the user it belongs to. */
91
+ type SavedState = Omit<PersonIntakeSavedRun, 'user_id'>
92
+
44
93
  const STEP_LABELS = ['Notes', 'Confirm', 'Choose', 'Done'] as const
45
94
 
46
- const STEP_INDEX: Readonly<Record<Step['kind'], number>> = { notes: 0, confirm: 1, choose: 2, done: 3 }
95
+ const STEP_INDEX: Readonly<Record<Exclude<Step['kind'], 'restoring'>, number>> = {
96
+ notes: 0,
97
+ confirm: 1,
98
+ choose: 2,
99
+ done: 3,
100
+ }
101
+
102
+ /** Shown, with the notes still filled in, when a run's result has gone. */
103
+ const RESULT_GONE_MESSAGE = 'This result is no longer available. Start again.'
47
104
 
48
105
  /** Pipeline records start unticked: the model suggests them, the user decides. */
49
106
  const OFF_BY_DEFAULT: ReadonlySet<PersonIntakeKind> = new Set(['partnership', 'deal', 'enquiry'])
@@ -53,42 +110,306 @@ const primaryButton =
53
110
  const secondaryButton =
54
111
  'rounded-md px-2.5 py-1.5 text-[12px] font-medium text-ink-muted hover:text-ink disabled:opacity-50'
55
112
 
56
- export function PersonIntakeWizard({ onClose, onPendingChange }: PersonIntakeWizardProps): React.JSX.Element {
57
- const [step, setStep] = useState<Step>({ kind: 'notes' })
58
- const [text, setText] = useState('')
59
- const identify = useIdentifyPerson()
60
- const research = useResearchPerson()
113
+ /** The Confirm step for a research run, rebuilt from the person it was started with. */
114
+ function confirmFrom(
115
+ candidate: PersonIntakeCandidateWire | null,
116
+ identifyRunId: string | null,
117
+ existingPersonId: string | null,
118
+ ): Step {
119
+ if (candidate === null) return { kind: 'notes' }
120
+
121
+ return {
122
+ kind: 'confirm',
123
+ identified: { candidates: [personIntakeCandidateSchema.parse(candidate)], question: null, sources: [] },
124
+ identifyRunId,
125
+ target: existingPersonId,
126
+ }
127
+ }
128
+
129
+ function confirmFor(watched: WatchedRun): Step {
130
+ return confirmFrom(watched.candidate, watched.identifyRunId, watched.existingPersonId)
131
+ }
132
+
133
+ function stepIndex(step: Step): number {
134
+ return step.kind === 'restoring' ? STEP_INDEX[step.saved] : STEP_INDEX[step.kind]
135
+ }
136
+
137
+ /**
138
+ * Where a reload starts: the run to read, or, for a run that failed, the step
139
+ * and the notes alone. A failed research run reads its identify run again,
140
+ * while that result is kept, to rebuild the Confirm step.
141
+ */
142
+ function restoredState(saved: PersonIntakeSavedRun | undefined): { step: Step; watched: WatchedRun | undefined } {
143
+ if (saved === undefined) return { step: { kind: 'notes' }, watched: undefined }
144
+
145
+ const runId = saved.run_id ?? (saved.step === 'confirm' ? saved.identify_run_id : null)
146
+
147
+ if (runId !== null) {
148
+ return {
149
+ step: { kind: 'restoring', saved: saved.step },
150
+ watched: {
151
+ runId,
152
+ text: saved.text,
153
+ identifyRunId: saved.run_id === null ? null : saved.identify_run_id,
154
+ candidate: saved.run_id === null ? null : saved.candidate,
155
+ existingPersonId: saved.run_id === null ? null : saved.existing_person_id,
156
+ fallback:
157
+ saved.run_id === null && saved.candidate !== null
158
+ ? { candidate: saved.candidate, existingPersonId: saved.existing_person_id }
159
+ : null,
160
+ },
161
+ }
162
+ }
163
+
164
+ return {
165
+ step: saved.step === 'confirm' ? confirmFrom(saved.candidate, null, saved.existing_person_id) : { kind: 'notes' },
166
+ watched: undefined,
167
+ }
168
+ }
169
+
170
+ export function PersonIntakeWizard(props: PersonIntakeWizardProps): React.JSX.Element {
171
+ const { session } = useSession()
172
+
173
+ // The saved run is per workspace and user, so the wizard waits until it knows both.
174
+ if (session === undefined) return <LoadingPanel label="Loading…" />
175
+
176
+ return (
177
+ <IntakeWizard
178
+ key={`${session.workspaceId ?? ''}:${session.userId}`}
179
+ {...props}
180
+ workspaceId={session.workspaceId}
181
+ userId={session.userId}
182
+ />
183
+ )
184
+ }
185
+
186
+ function IntakeWizard({
187
+ onClose,
188
+ onPendingChange,
189
+ workspaceId,
190
+ userId,
191
+ }: PersonIntakeWizardProps & { readonly workspaceId: string | null; readonly userId: string }): React.JSX.Element {
192
+ const [restored] = useState(() => {
193
+ const saved = workspaceId === null ? undefined : readSavedIntakeRun(workspaceId, userId)
194
+
195
+ return { ...restoredState(saved), text: saved?.text ?? '' }
196
+ })
197
+ const [step, setStep] = useState<Step>(restored.step)
198
+ const [text, setText] = useState(restored.text)
199
+ const [watched, setWatched] = useState<WatchedRun | undefined>(restored.watched)
200
+ /** Why the last run failed, from the server. Shown as written: it is the run log's reason. */
201
+ const [runFailure, setRunFailure] = useState<string | null>(null)
202
+ const [resultGone, setResultGone] = useState(false)
203
+ const identify = useStartIdentify()
204
+ const research = useStartResearch()
61
205
  const apply = useApplyPersonIntake()
62
- const pending = identify.isPending || research.isPending || apply.isPending
206
+ const polled = usePersonIntakeRun(watched?.runId)
207
+ // While a run is watched, even one the wizard cannot read just now, nothing
208
+ // new starts: the server run goes on, and a second would spend another.
209
+ const pending = identify.isPending || research.isPending || apply.isPending || watched !== undefined
210
+ const readError = watched !== undefined && !polled.isNotFound ? polled.error : null
63
211
 
64
212
  useEffect(() => {
65
213
  onPendingChange?.(pending)
66
214
  }, [pending, onPendingChange])
67
215
 
216
+ // Moves the wizard on when the run it waits on changes, once per settled run.
217
+ useEffect(() => {
218
+ if (watched === undefined) return
219
+
220
+ function keep(state: SavedState): void {
221
+ if (workspaceId !== null) saveIntakeRun(workspaceId, { user_id: userId, ...state })
222
+ }
223
+
224
+ function forget(): void {
225
+ if (workspaceId !== null) clearSavedIntakeRuns(workspaceId)
226
+ }
227
+
228
+ /** The run's result has gone: Confirm from the saved person when there is one, else the notes. */
229
+ function resultGoneFor(current: WatchedRun): void {
230
+ setWatched(undefined)
231
+
232
+ if (current.fallback !== null) {
233
+ setStep(confirmFrom(current.fallback.candidate, null, current.fallback.existingPersonId))
234
+ keep({
235
+ run_id: null,
236
+ identify_run_id: null,
237
+ step: 'confirm',
238
+ text: current.text,
239
+ candidate: current.fallback.candidate,
240
+ existing_person_id: current.fallback.existingPersonId,
241
+ })
242
+ return
243
+ }
244
+
245
+ setResultGone(true)
246
+ setStep({ kind: 'notes' })
247
+ forget()
248
+ }
249
+
250
+ if (polled.isNotFound) {
251
+ resultGoneFor(watched)
252
+ return
253
+ }
254
+
255
+ if (polled.error !== null) {
256
+ // The run may still finish. Keep watching it, and show the error with
257
+ // a way to read it again; the step leaves the loading state.
258
+ setStep((current) => (current.kind === 'restoring' ? confirmOrNotes(watched) : current))
259
+ return
260
+ }
261
+
262
+ const run = polled.run
263
+ if (run === undefined) return
264
+
265
+ if (run.status === 'queued' || run.status === 'running') {
266
+ setStep((current) =>
267
+ current.kind !== 'restoring' ? current : run.kind === 'identify' ? { kind: 'notes' } : confirmFor(watched),
268
+ )
269
+ return
270
+ }
271
+
272
+ setWatched(undefined)
273
+
274
+ if (run.status === 'failed') {
275
+ setRunFailure(run.failureReason ?? 'The AI run failed.')
276
+ setStep((current) =>
277
+ current.kind === 'confirm' ? current : run.kind === 'identify' ? { kind: 'notes' } : confirmFor(watched),
278
+ )
279
+ // The step and the notes stay saved, with no run to wait on.
280
+ keep({
281
+ run_id: null,
282
+ identify_run_id: run.kind === 'research' ? watched.identifyRunId : null,
283
+ step: run.kind === 'identify' ? 'notes' : 'confirm',
284
+ text: watched.text,
285
+ candidate: run.kind === 'research' ? watched.candidate : null,
286
+ existing_person_id: run.kind === 'research' ? watched.existingPersonId : null,
287
+ })
288
+ return
289
+ }
290
+
291
+ if (run.kind === 'identify' && run.result !== null) {
292
+ setStep({ kind: 'confirm', identified: run.result, identifyRunId: run.runId })
293
+ keep({
294
+ run_id: run.runId,
295
+ identify_run_id: null,
296
+ step: 'confirm',
297
+ text: watched.text,
298
+ candidate: null,
299
+ existing_person_id: null,
300
+ })
301
+ return
302
+ }
303
+
304
+ if (run.kind === 'research' && run.result !== null) {
305
+ setStep({ kind: 'choose', research: run.result })
306
+ keep({
307
+ run_id: run.runId,
308
+ identify_run_id: watched.identifyRunId,
309
+ step: 'choose',
310
+ text: watched.text,
311
+ candidate: watched.candidate,
312
+ existing_person_id: watched.existingPersonId,
313
+ })
314
+ return
315
+ }
316
+
317
+ // Succeeded, and the hour has passed: the result is gone.
318
+ resultGoneFor(watched)
319
+ }, [watched, polled.run, polled.error, polled.isNotFound, workspaceId, userId])
320
+
321
+ function remember(run: WatchedRun, savedStep: PersonIntakeSavedRun['step']): void {
322
+ setWatched(run)
323
+ if (workspaceId === null) return
324
+
325
+ saveIntakeRun(workspaceId, {
326
+ user_id: userId,
327
+ run_id: run.runId,
328
+ identify_run_id: run.identifyRunId,
329
+ step: savedStep,
330
+ text: run.text,
331
+ candidate: run.candidate,
332
+ existing_person_id: run.existingPersonId,
333
+ })
334
+ }
335
+
336
+ function forgetSaved(): void {
337
+ if (workspaceId !== null) clearSavedIntakeRuns(workspaceId)
338
+ }
339
+
68
340
  async function runIdentify(): Promise<void> {
69
- const identified = await identify.runAsync(text.trim())
70
- setStep({ kind: 'confirm', identified })
341
+ setRunFailure(null)
342
+ setResultGone(false)
343
+ const notes = text.trim()
344
+ const started = await identify.runAsync(notes)
345
+ remember(
346
+ { runId: started.runId, text: notes, identifyRunId: null, candidate: null, existingPersonId: null, fallback: null },
347
+ 'notes',
348
+ )
71
349
  }
72
350
 
73
- async function runResearch(candidate: PersonIntakeCandidate, existingPersonId: string | null): Promise<void> {
74
- const researched = await research.runAsync({ text: text.trim(), candidate, existingPersonId })
75
- setStep({ kind: 'choose', research: researched })
351
+ async function runResearch(
352
+ candidate: PersonIntakeCandidate,
353
+ existingPersonId: string | null,
354
+ identifyRunId: string | null,
355
+ ): Promise<void> {
356
+ setRunFailure(null)
357
+ const notes = text.trim()
358
+ const started = await research.runAsync({ text: notes, candidate, existingPersonId })
359
+ remember(
360
+ {
361
+ runId: started.runId,
362
+ text: notes,
363
+ identifyRunId,
364
+ candidate: personIntakeCandidateBody(candidate),
365
+ existingPersonId,
366
+ fallback: null,
367
+ },
368
+ 'confirm',
369
+ )
76
370
  }
77
371
 
78
372
  async function runApply(items: readonly PersonIntakeItem[]): Promise<void> {
79
373
  const applied = await apply.runAsync(items)
80
374
  setStep({ kind: 'done', results: applied.results })
375
+ forgetSaved()
376
+ }
377
+
378
+ function startAgain(): void {
379
+ setRunFailure(null)
380
+ setStep({ kind: 'notes' })
381
+ forgetSaved()
81
382
  }
82
383
 
83
384
  return (
84
385
  <>
85
- <StepBar current={STEP_INDEX[step.kind]} />
386
+ <StepBar current={stepIndex(step)} />
387
+ {readError !== null && (
388
+ <div role="alert" className="border-b border-border px-4 py-3">
389
+ <ErrorPanel error={readError} />
390
+ <p className="mt-2 text-[12px] text-ink-muted">
391
+ The run may still be working. Kelpie will not start another until this one is read.
392
+ </p>
393
+ <button
394
+ type="button"
395
+ className={`${secondaryButton} mt-1`}
396
+ onClick={() => {
397
+ polled.refetch()
398
+ }}
399
+ >
400
+ Check again
401
+ </button>
402
+ </div>
403
+ )}
404
+ {step.kind === 'restoring' && <LoadingPanel label="Picking up where you left off…" />}
86
405
  {step.kind === 'notes' && (
87
406
  <NotesStep
88
407
  text={text}
89
408
  onChange={setText}
90
- pending={identify.isPending}
409
+ pending={pending}
91
410
  error={identify.error}
411
+ failure={runFailure}
412
+ notice={resultGone ? RESULT_GONE_MESSAGE : null}
92
413
  onNext={() => {
93
414
  void runIdentify().catch(() => undefined)
94
415
  }}
@@ -97,13 +418,13 @@ export function PersonIntakeWizard({ onClose, onPendingChange }: PersonIntakeWiz
97
418
  {step.kind === 'confirm' && (
98
419
  <ConfirmStep
99
420
  identified={step.identified}
100
- pending={research.isPending}
421
+ initialTarget={step.target}
422
+ pending={pending}
101
423
  error={research.error}
102
- onBack={() => {
103
- setStep({ kind: 'notes' })
104
- }}
424
+ failure={runFailure}
425
+ onBack={startAgain}
105
426
  onNext={(candidate, existingPersonId) => {
106
- void runResearch(candidate, existingPersonId).catch(() => undefined)
427
+ void runResearch(candidate, existingPersonId, step.identifyRunId).catch(() => undefined)
107
428
  }}
108
429
  />
109
430
  )}
@@ -112,9 +433,7 @@ export function PersonIntakeWizard({ onClose, onPendingChange }: PersonIntakeWiz
112
433
  research={step.research}
113
434
  pending={apply.isPending}
114
435
  error={apply.error}
115
- onBack={() => {
116
- setStep({ kind: 'notes' })
117
- }}
436
+ onBack={startAgain}
118
437
  onNext={(items) => {
119
438
  void runApply(items).catch(() => undefined)
120
439
  }}
@@ -125,6 +444,27 @@ export function PersonIntakeWizard({ onClose, onPendingChange }: PersonIntakeWiz
125
444
  )
126
445
  }
127
446
 
447
+ /**
448
+ * Where a run the wizard could not read leaves it: the Confirm step for
449
+ * research, or for a reload of a failed research run the saved person's
450
+ * Confirm step, else the notes.
451
+ */
452
+ function confirmOrNotes(watched: WatchedRun): Step {
453
+ if (watched.candidate !== null) return confirmFor(watched)
454
+ if (watched.fallback !== null) return confirmFrom(watched.fallback.candidate, null, watched.fallback.existingPersonId)
455
+
456
+ return { kind: 'notes' }
457
+ }
458
+
459
+ /** A failed run's reason. The server wrote it for the user, so it is shown as it is. */
460
+ function RunFailure({ reason }: { readonly reason: string }): React.JSX.Element {
461
+ return (
462
+ <div role="alert" className="mt-3 rounded-md border border-danger/40 bg-danger-soft px-4 py-3">
463
+ <p className="text-[13px] font-medium text-danger">{reason}</p>
464
+ </div>
465
+ )
466
+ }
467
+
128
468
  function StepBar({ current }: { readonly current: number }): React.JSX.Element {
129
469
  return (
130
470
  <ol className="flex gap-4 border-b border-border px-4 py-2 text-[11px]">
@@ -146,12 +486,18 @@ function NotesStep({
146
486
  onChange,
147
487
  pending,
148
488
  error,
489
+ notice,
490
+ failure,
149
491
  onNext,
150
492
  }: {
151
493
  readonly text: string
152
494
  readonly onChange: (value: string) => void
153
495
  readonly pending: boolean
154
496
  readonly error: Error | null
497
+ /** Why the run failed, as the server put it. */
498
+ readonly failure: string | null
499
+ /** A line above the notes, such as the result having gone. */
500
+ readonly notice: string | null
155
501
  readonly onNext: () => void
156
502
  }): React.JSX.Element {
157
503
  const serviceLabel = useAiServiceLabel()
@@ -163,6 +509,11 @@ function NotesStep({
163
509
  </button>
164
510
  }
165
511
  >
512
+ {notice !== null && (
513
+ <p role="status" className="mb-2 rounded-md border border-warning/40 bg-warning-soft px-3 py-2 text-[12px] text-ink">
514
+ {notice}
515
+ </p>
516
+ )}
166
517
  <NotesField value={text} onChange={onChange} disabled={pending} />
167
518
  {pending && (
168
519
  <p className="mt-2 text-[12px] text-ink-muted">
@@ -174,6 +525,7 @@ function NotesStep({
174
525
  <ErrorPanel error={error} />
175
526
  </div>
176
527
  )}
528
+ {failure !== null && <RunFailure reason={failure} />}
177
529
  </IntakeFrame>
178
530
  )
179
531
  }
@@ -189,21 +541,29 @@ type Target = string
189
541
 
190
542
  function ConfirmStep({
191
543
  identified,
544
+ initialTarget,
192
545
  pending,
193
546
  error,
547
+ failure,
194
548
  onBack,
195
549
  onNext,
196
550
  }: {
197
- readonly identified: PersonIntakeIdentifyResult
551
+ readonly identified: Identified
552
+ /** The target a research run was started with, when the step is rebuilt after a reload. */
553
+ readonly initialTarget?: string | null | undefined
198
554
  readonly pending: boolean
199
555
  readonly error: Error | null
556
+ /** Why the research run failed, as the server put it. */
557
+ readonly failure: string | null
200
558
  readonly onBack: () => void
201
559
  readonly onNext: (candidate: PersonIntakeCandidate, existingPersonId: string | null) => void
202
560
  }): React.JSX.Element {
203
561
  const serviceLabel = useAiServiceLabel()
204
562
  const first = identified.candidates[0]
205
563
  const [candidateKey, setCandidateKey] = useState(first?.key ?? '')
206
- const [target, setTarget] = useState<Target>(first?.existingPeople[0]?.id ?? 'new')
564
+ const [target, setTarget] = useState<Target>(
565
+ initialTarget === undefined ? (first?.existingPeople[0]?.id ?? 'new') : (initialTarget ?? 'new'),
566
+ )
207
567
  const candidate = identified.candidates.find((entry) => entry.key === candidateKey)
208
568
 
209
569
  if (identified.candidates.length === 0) {
@@ -337,6 +697,7 @@ function ConfirmStep({
337
697
  <ErrorPanel error={error} />
338
698
  </div>
339
699
  )}
700
+ {failure !== null && <RunFailure reason={failure} />}
340
701
  </IntakeFrame>
341
702
  )
342
703
  }
@@ -469,7 +830,7 @@ function itemTitle(item: PersonIntakeItem, companyNames: ReadonlyMap<string, str
469
830
  return item.title === '' ? `At ${company}` : `${item.title} at ${company}`
470
831
  }
471
832
  case 'note':
472
- return 'Pinned research note'
833
+ return 'Research note'
473
834
  }
474
835
  }
475
836
 
@@ -9,7 +9,7 @@ import { PersonIntakeWizard } from './PersonIntakeWizard.tsx'
9
9
  * Not registered by default. An assembly that lists `createAiModule()` on the
10
10
  * server lists `aiUi` in `kelpie.ui.config.ts`, the same pairing any module
11
11
  * uses. Two tabs on core's Admin → AI page, Settings and Run log, before
12
- * core's MCP tab. On record pages it is the Agent menu's runner: Run dispatches straight to the "Kelpie AI" agent row, and
12
+ * core's Agents tab. On record pages it is the Agent menu's runner: Run dispatches straight to the "Kelpie AI" agent row, and
13
13
  * stays disabled until AI is enabled and has a key. Preview's Run… still lists
14
14
  * every registered agent, Kelpie AI included. On People it is the wizard behind
15
15
  * Add from notes.
@@ -5,6 +5,8 @@ import { useNavigate } from 'react-router'
5
5
 
6
6
  import { useTimezone } from '../api/resources/account.ts'
7
7
  import { useCompanies, useCreateCompany } from '../api/resources/companies.ts'
8
+ import { AddRecordPanel } from '../components/AddRecordPanel.tsx'
9
+ import type { AddRecordValues } from '../components/AddRecordPanel.tsx'
8
10
  import { Chip } from '../components/Chip.tsx'
9
11
  import type { ChipTone } from '../components/Chip.tsx'
10
12
  import { ColumnPicker } from '../components/ColumnPicker.tsx'
@@ -50,10 +52,14 @@ export function CompaniesPage(): React.JSX.Element {
50
52
  const headcounts = useCompanyHeadcounts(companies.records.map((company) => company.id))
51
53
  const createCompany = useCreateCompany()
52
54
 
53
- async function addCompany(): Promise<void> {
54
- const company = await createCompany.runAsync({ name: 'New company' })
55
+ const [adding, setAdding] = useState(false)
55
56
 
56
- await navigate(`/companies/${company.id}`)
57
+ function addCompany(values: AddRecordValues): void {
58
+ createCompany
59
+ .runAsync({ name: values.name })
60
+ .then((record) => navigate(`/companies/${record.id}`))
61
+ // The failure is shown from `createCompany.error` below.
62
+ .catch(() => undefined)
57
63
  }
58
64
 
59
65
  const columns: readonly Column<Company>[] = [
@@ -195,7 +201,7 @@ export function CompaniesPage(): React.JSX.Element {
195
201
  <PageHeader
196
202
  title="Companies"
197
203
  onAdd={() => {
198
- void addCompany()
204
+ setAdding(true)
199
205
  }}
200
206
  addLabel="Add company"
201
207
  actions={
@@ -208,6 +214,17 @@ export function CompaniesPage(): React.JSX.Element {
208
214
  />
209
215
  <FilterBar value={term} onChange={setTerm} placeholder="Filter by name, type, tags…" />
210
216
 
217
+ {adding && (
218
+ <AddRecordPanel
219
+ title="Add company"
220
+ isPending={createCompany.isPending}
221
+ onCancel={() => {
222
+ setAdding(false)
223
+ }}
224
+ onCreate={addCompany}
225
+ />
226
+ )}
227
+
211
228
  {createCompany.error !== null && (
212
229
  <div className="mb-3">
213
230
  <ErrorPanel error={createCompany.error} />
@@ -236,7 +253,7 @@ export function CompaniesPage(): React.JSX.Element {
236
253
  : {
237
254
  label: 'Add company',
238
255
  onClick: () => {
239
- void addCompany()
256
+ setAdding(true)
240
257
  },
241
258
  }
242
259
  }