@kelpie/ui 0.17.0 → 0.19.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 (189) hide show
  1. package/dist/api/resources/ai.d.ts +5 -0
  2. package/dist/api/resources/ai.d.ts.map +1 -1
  3. package/dist/api/resources/ai.js +9 -1
  4. package/dist/api/resources/ai.js.map +1 -1
  5. package/dist/api/resources/forms.d.ts +8 -0
  6. package/dist/api/resources/forms.d.ts.map +1 -1
  7. package/dist/api/resources/forms.js +39 -0
  8. package/dist/api/resources/forms.js.map +1 -1
  9. package/dist/api/resources/personIntake.d.ts +12 -0
  10. package/dist/api/resources/personIntake.d.ts.map +1 -0
  11. package/dist/api/resources/personIntake.js +54 -0
  12. package/dist/api/resources/personIntake.js.map +1 -0
  13. package/dist/app/KelpieApp.js +2 -2
  14. package/dist/app/KelpieApp.js.map +1 -1
  15. package/dist/components/AgentTasks.js +2 -2
  16. package/dist/components/AgentTasks.js.map +1 -1
  17. package/dist/components/AiDrawer.d.ts +13 -0
  18. package/dist/components/AiDrawer.d.ts.map +1 -0
  19. package/dist/components/AiDrawer.js +165 -0
  20. package/dist/components/AiDrawer.js.map +1 -0
  21. package/dist/components/ApiKeysPanel.js +1 -1
  22. package/dist/components/DataTable.js +1 -1
  23. package/dist/components/KanbanBoard.d.ts +5 -1
  24. package/dist/components/KanbanBoard.d.ts.map +1 -1
  25. package/dist/components/KanbanBoard.js +37 -2
  26. package/dist/components/KanbanBoard.js.map +1 -1
  27. package/dist/components/NotesPanel.js +2 -2
  28. package/dist/components/NotesPanel.js.map +1 -1
  29. package/dist/components/PageHeader.js +1 -1
  30. package/dist/components/PageHeader.js.map +1 -1
  31. package/dist/components/PersonIntakeDialog.d.ts +19 -0
  32. package/dist/components/PersonIntakeDialog.d.ts.map +1 -0
  33. package/dist/components/PersonIntakeDialog.js +87 -0
  34. package/dist/components/PersonIntakeDialog.js.map +1 -0
  35. package/dist/components/PersonIntakePanel.d.ts +21 -0
  36. package/dist/components/PersonIntakePanel.d.ts.map +1 -0
  37. package/dist/components/PersonIntakePanel.js +76 -0
  38. package/dist/components/PersonIntakePanel.js.map +1 -0
  39. package/dist/components/SegmentedControl.js +2 -2
  40. package/dist/components/SegmentedControl.js.map +1 -1
  41. package/dist/components/Shell.d.ts.map +1 -1
  42. package/dist/components/Shell.js +40 -4
  43. package/dist/components/Shell.js.map +1 -1
  44. package/dist/components/WorkspaceSwitcher.d.ts.map +1 -1
  45. package/dist/components/WorkspaceSwitcher.js +10 -2
  46. package/dist/components/WorkspaceSwitcher.js.map +1 -1
  47. package/dist/components/aiDrawerState.d.ts +45 -0
  48. package/dist/components/aiDrawerState.d.ts.map +1 -0
  49. package/dist/components/aiDrawerState.js +14 -0
  50. package/dist/components/aiDrawerState.js.map +1 -0
  51. package/dist/components/personIntakeTask.d.ts +5 -0
  52. package/dist/components/personIntakeTask.d.ts.map +1 -0
  53. package/dist/components/personIntakeTask.js +13 -0
  54. package/dist/components/personIntakeTask.js.map +1 -0
  55. package/dist/index.d.ts +5 -3
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +5 -2
  58. package/dist/index.js.map +1 -1
  59. package/dist/lib/mediaQuery.d.ts +9 -0
  60. package/dist/lib/mediaQuery.d.ts.map +1 -0
  61. package/dist/lib/mediaQuery.js +24 -0
  62. package/dist/lib/mediaQuery.js.map +1 -0
  63. package/dist/modules/PersonIntakeWizard.d.ts +3 -0
  64. package/dist/modules/PersonIntakeWizard.d.ts.map +1 -0
  65. package/dist/modules/PersonIntakeWizard.js +227 -0
  66. package/dist/modules/PersonIntakeWizard.js.map +1 -0
  67. package/dist/modules/ai.d.ts +4 -3
  68. package/dist/modules/ai.d.ts.map +1 -1
  69. package/dist/modules/ai.js +10 -6
  70. package/dist/modules/ai.js.map +1 -1
  71. package/dist/modules/aiRunner.d.ts.map +1 -1
  72. package/dist/modules/aiRunner.js +7 -5
  73. package/dist/modules/aiRunner.js.map +1 -1
  74. package/dist/modules/personIntakeSelection.d.ts +7 -0
  75. package/dist/modules/personIntakeSelection.d.ts.map +1 -0
  76. package/dist/modules/personIntakeSelection.js +21 -0
  77. package/dist/modules/personIntakeSelection.js.map +1 -0
  78. package/dist/pages/FormDetail.d.ts.map +1 -1
  79. package/dist/pages/FormDetail.js +7 -7
  80. package/dist/pages/FormDetail.js.map +1 -1
  81. package/dist/pages/FormsPage.js +4 -4
  82. package/dist/pages/FormsPage.js.map +1 -1
  83. package/dist/pages/ListDetail.d.ts.map +1 -1
  84. package/dist/pages/ListDetail.js +2 -2
  85. package/dist/pages/ListDetail.js.map +1 -1
  86. package/dist/pages/PeoplePage.d.ts.map +1 -1
  87. package/dist/pages/PeoplePage.js +9 -1
  88. package/dist/pages/PeoplePage.js.map +1 -1
  89. package/dist/pages/RoleDetail.d.ts.map +1 -1
  90. package/dist/pages/RoleDetail.js +3 -3
  91. package/dist/pages/RoleDetail.js.map +1 -1
  92. package/dist/pages/account/ConnectedAppsPage.js +1 -1
  93. package/dist/pages/admin/AiAdminPage.d.ts +2 -0
  94. package/dist/pages/admin/AiAdminPage.d.ts.map +1 -0
  95. package/dist/pages/admin/AiAdminPage.js +19 -0
  96. package/dist/pages/admin/AiAdminPage.js.map +1 -0
  97. package/dist/pages/admin/AiPage.d.ts +4 -1
  98. package/dist/pages/admin/AiPage.d.ts.map +1 -1
  99. package/dist/pages/admin/AiPage.js +77 -15
  100. package/dist/pages/admin/AiPage.js.map +1 -1
  101. package/dist/pages/admin/DataPage.js +2 -2
  102. package/dist/pages/admin/McpPanel.d.ts +3 -0
  103. package/dist/pages/admin/McpPanel.d.ts.map +1 -0
  104. package/dist/pages/admin/McpPanel.js +123 -0
  105. package/dist/pages/admin/McpPanel.js.map +1 -0
  106. package/dist/pages/admin/PrivacyPage.js +19 -3
  107. package/dist/pages/admin/PrivacyPage.js.map +1 -1
  108. package/dist/pages/forms/AddFieldMenu.js +2 -2
  109. package/dist/pages/forms/AddFieldMenu.js.map +1 -1
  110. package/dist/pages/forms/EmbedPanel.js +5 -2
  111. package/dist/pages/forms/EmbedPanel.js.map +1 -1
  112. package/dist/pages/forms/FieldsEditor.d.ts.map +1 -1
  113. package/dist/pages/forms/FieldsEditor.js +90 -8
  114. package/dist/pages/forms/FieldsEditor.js.map +1 -1
  115. package/dist/pages/forms/FormSettings.d.ts.map +1 -1
  116. package/dist/pages/forms/FormSettings.js +73 -6
  117. package/dist/pages/forms/FormSettings.js.map +1 -1
  118. package/dist/pages/forms/SubmissionDetail.d.ts.map +1 -1
  119. package/dist/pages/forms/SubmissionDetail.js +2 -1
  120. package/dist/pages/forms/SubmissionDetail.js.map +1 -1
  121. package/dist/pages/forms/SubmissionsTable.d.ts.map +1 -1
  122. package/dist/pages/forms/SubmissionsTable.js +2 -1
  123. package/dist/pages/forms/SubmissionsTable.js.map +1 -1
  124. package/dist/pages/forms/fieldList.d.ts +2 -2
  125. package/dist/pages/forms/fieldList.d.ts.map +1 -1
  126. package/dist/pages/forms/fieldList.js +38 -10
  127. package/dist/pages/forms/fieldList.js.map +1 -1
  128. package/dist/pages/forms/template.d.ts.map +1 -1
  129. package/dist/pages/forms/template.js +13 -0
  130. package/dist/pages/forms/template.js.map +1 -1
  131. package/dist/pages/handbook/HandbookPage.js +4 -4
  132. package/dist/pages/handbook/HandbookPage.js.map +1 -1
  133. package/dist/registry/context.d.ts +5 -1
  134. package/dist/registry/context.d.ts.map +1 -1
  135. package/dist/registry/context.js +8 -0
  136. package/dist/registry/context.js.map +1 -1
  137. package/dist/registry/contributions.d.ts +37 -1
  138. package/dist/registry/contributions.d.ts.map +1 -1
  139. package/dist/registry/registry.d.ts +8 -1
  140. package/dist/registry/registry.d.ts.map +1 -1
  141. package/dist/registry/registry.js +13 -0
  142. package/dist/registry/registry.js.map +1 -1
  143. package/package.json +2 -2
  144. package/src/api/resources/ai.ts +11 -1
  145. package/src/api/resources/forms.ts +56 -0
  146. package/src/api/resources/personIntake.ts +95 -0
  147. package/src/app/KelpieApp.tsx +4 -2
  148. package/src/components/AgentTasks.tsx +2 -2
  149. package/src/components/AiDrawer.tsx +272 -0
  150. package/src/components/ApiKeysPanel.tsx +1 -1
  151. package/src/components/DataTable.tsx +1 -1
  152. package/src/components/KanbanBoard.tsx +119 -2
  153. package/src/components/NotesPanel.tsx +7 -4
  154. package/src/components/PageHeader.tsx +1 -1
  155. package/src/components/PersonIntakePanel.tsx +193 -0
  156. package/src/components/SegmentedControl.tsx +2 -2
  157. package/src/components/Shell.tsx +99 -13
  158. package/src/components/WorkspaceSwitcher.tsx +13 -2
  159. package/src/components/aiDrawerState.ts +61 -0
  160. package/src/components/personIntakeTask.tsx +14 -0
  161. package/src/index.ts +12 -1
  162. package/src/lib/mediaQuery.ts +32 -0
  163. package/src/modules/PersonIntakeWizard.tsx +638 -0
  164. package/src/modules/ai.tsx +10 -6
  165. package/src/modules/aiRunner.ts +8 -5
  166. package/src/modules/personIntakeSelection.ts +24 -0
  167. package/src/pages/FormDetail.tsx +18 -14
  168. package/src/pages/FormsPage.tsx +4 -4
  169. package/src/pages/ListDetail.tsx +5 -3
  170. package/src/pages/PeoplePage.tsx +18 -5
  171. package/src/pages/RoleDetail.tsx +10 -5
  172. package/src/pages/account/ConnectedAppsPage.tsx +1 -1
  173. package/src/pages/admin/AiAdminPage.tsx +52 -0
  174. package/src/pages/admin/AiPage.tsx +136 -32
  175. package/src/pages/admin/DataPage.tsx +2 -2
  176. package/src/pages/admin/{McpPage.tsx → McpPanel.tsx} +4 -6
  177. package/src/pages/admin/PrivacyPage.tsx +42 -0
  178. package/src/pages/forms/AddFieldMenu.tsx +2 -2
  179. package/src/pages/forms/EmbedPanel.tsx +5 -1
  180. package/src/pages/forms/FieldsEditor.tsx +204 -15
  181. package/src/pages/forms/FormSettings.tsx +156 -9
  182. package/src/pages/forms/SubmissionDetail.tsx +2 -1
  183. package/src/pages/forms/SubmissionsTable.tsx +2 -1
  184. package/src/pages/forms/fieldList.ts +39 -10
  185. package/src/pages/forms/template.ts +13 -0
  186. package/src/pages/handbook/HandbookPage.tsx +5 -5
  187. package/src/registry/context.ts +13 -0
  188. package/src/registry/contributions.ts +39 -1
  189. package/src/registry/registry.ts +30 -0
@@ -0,0 +1,638 @@
1
+ import {
2
+ PERSON_INTAKE_KIND_LABELS,
3
+ PERSON_INTAKE_KINDS,
4
+ PERSON_INTAKE_PERSON_KEY,
5
+ personIntakeDependencies,
6
+ } from '@kelpie/schemas'
7
+ import type {
8
+ PersonIntakeCandidate,
9
+ PersonIntakeIdentifyResult,
10
+ PersonIntakeItem,
11
+ PersonIntakeKind,
12
+ PersonIntakeResearchResult,
13
+ PersonIntakeResult,
14
+ PersonIntakeResultStatus,
15
+ PersonIntakeSource,
16
+ } from '@kelpie/schemas'
17
+ import { useEffect, useState } from 'react'
18
+ import { Link } from 'react-router'
19
+
20
+ import { useAiServiceLabel } from '../api/resources/ai.ts'
21
+ import { useApplyPersonIntake, useIdentifyPerson, useResearchPerson } from '../api/resources/personIntake.ts'
22
+ import { Chip } from '../components/Chip.tsx'
23
+ import type { ChipTone } from '../components/Chip.tsx'
24
+ import { IntakeFrame, NotesField } from '../components/PersonIntakePanel.tsx'
25
+ import { ErrorPanel } from '../components/QueryState.tsx'
26
+ import type { PersonIntakeWizardProps } from '../registry/contributions.ts'
27
+ import { effectiveSelection } from './personIntakeSelection.ts'
28
+
29
+ /**
30
+ * The `ai` module's wizard behind People's **Add from notes**: notes, confirm the
31
+ * person, choose what to create, done. Spec: person intake, in the `ai`
32
+ * module's docs (`docs/agents/kelpie-ai.md`).
33
+ *
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.
36
+ */
37
+
38
+ type Step =
39
+ | { readonly kind: 'notes' }
40
+ | { readonly kind: 'confirm'; readonly identified: PersonIntakeIdentifyResult }
41
+ | { readonly kind: 'choose'; readonly research: PersonIntakeResearchResult }
42
+ | { readonly kind: 'done'; readonly results: readonly PersonIntakeResult[] }
43
+
44
+ const STEP_LABELS = ['Notes', 'Confirm', 'Choose', 'Done'] as const
45
+
46
+ const STEP_INDEX: Readonly<Record<Step['kind'], number>> = { notes: 0, confirm: 1, choose: 2, done: 3 }
47
+
48
+ /** Pipeline records start unticked: the model suggests them, the user decides. */
49
+ const OFF_BY_DEFAULT: ReadonlySet<PersonIntakeKind> = new Set(['partnership', 'deal', 'enquiry'])
50
+
51
+ const primaryButton =
52
+ 'rounded-md bg-accent px-3 py-1.5 text-[12px] font-semibold text-accent-fg hover:bg-accent-hover disabled:opacity-50'
53
+ const secondaryButton =
54
+ 'rounded-md px-2.5 py-1.5 text-[12px] font-medium text-ink-muted hover:text-ink disabled:opacity-50'
55
+
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()
61
+ const apply = useApplyPersonIntake()
62
+ const pending = identify.isPending || research.isPending || apply.isPending
63
+
64
+ useEffect(() => {
65
+ onPendingChange?.(pending)
66
+ }, [pending, onPendingChange])
67
+
68
+ async function runIdentify(): Promise<void> {
69
+ const identified = await identify.runAsync(text.trim())
70
+ setStep({ kind: 'confirm', identified })
71
+ }
72
+
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 })
76
+ }
77
+
78
+ async function runApply(items: readonly PersonIntakeItem[]): Promise<void> {
79
+ const applied = await apply.runAsync(items)
80
+ setStep({ kind: 'done', results: applied.results })
81
+ }
82
+
83
+ return (
84
+ <>
85
+ <StepBar current={STEP_INDEX[step.kind]} />
86
+ {step.kind === 'notes' && (
87
+ <NotesStep
88
+ text={text}
89
+ onChange={setText}
90
+ pending={identify.isPending}
91
+ error={identify.error}
92
+ onNext={() => {
93
+ void runIdentify().catch(() => undefined)
94
+ }}
95
+ />
96
+ )}
97
+ {step.kind === 'confirm' && (
98
+ <ConfirmStep
99
+ identified={step.identified}
100
+ pending={research.isPending}
101
+ error={research.error}
102
+ onBack={() => {
103
+ setStep({ kind: 'notes' })
104
+ }}
105
+ onNext={(candidate, existingPersonId) => {
106
+ void runResearch(candidate, existingPersonId).catch(() => undefined)
107
+ }}
108
+ />
109
+ )}
110
+ {step.kind === 'choose' && (
111
+ <ChooseStep
112
+ research={step.research}
113
+ pending={apply.isPending}
114
+ error={apply.error}
115
+ onBack={() => {
116
+ setStep({ kind: 'notes' })
117
+ }}
118
+ onNext={(items) => {
119
+ void runApply(items).catch(() => undefined)
120
+ }}
121
+ />
122
+ )}
123
+ {step.kind === 'done' && <DoneStep results={step.results} onClose={onClose} />}
124
+ </>
125
+ )
126
+ }
127
+
128
+ function StepBar({ current }: { readonly current: number }): React.JSX.Element {
129
+ return (
130
+ <ol className="flex gap-4 border-b border-border px-4 py-2 text-[11px]">
131
+ {STEP_LABELS.map((label, index) => (
132
+ <li
133
+ key={label}
134
+ aria-current={index === current ? 'step' : undefined}
135
+ className={index === current ? 'font-semibold text-ink' : index < current ? 'text-ink-muted' : 'text-ink-faint'}
136
+ >
137
+ {String(index + 1)}. {label}
138
+ </li>
139
+ ))}
140
+ </ol>
141
+ )
142
+ }
143
+
144
+ function NotesStep({
145
+ text,
146
+ onChange,
147
+ pending,
148
+ error,
149
+ onNext,
150
+ }: {
151
+ readonly text: string
152
+ readonly onChange: (value: string) => void
153
+ readonly pending: boolean
154
+ readonly error: Error | null
155
+ readonly onNext: () => void
156
+ }): React.JSX.Element {
157
+ const serviceLabel = useAiServiceLabel()
158
+ return (
159
+ <IntakeFrame
160
+ footer={
161
+ <button type="button" className={primaryButton} disabled={pending || text.trim().length === 0} onClick={onNext}>
162
+ {pending ? 'Looking them up…' : 'Find this person'}
163
+ </button>
164
+ }
165
+ >
166
+ <NotesField value={text} onChange={onChange} disabled={pending} />
167
+ {pending && (
168
+ <p className="mt-2 text-[12px] text-ink-muted">
169
+ {serviceLabel} is looking this person up. With web search on, this can take a minute. You can keep working: this task stays open in the drawer.
170
+ </p>
171
+ )}
172
+ {error !== null && (
173
+ <div className="mt-3">
174
+ <ErrorPanel error={error} />
175
+ </div>
176
+ )}
177
+ </IntakeFrame>
178
+ )
179
+ }
180
+
181
+ const CONFIDENCE_TONES: Readonly<Record<PersonIntakeCandidate['confidence'], ChipTone>> = {
182
+ high: 'success',
183
+ medium: 'warning',
184
+ low: 'neutral',
185
+ }
186
+
187
+ /** `'new'`, or the id of the existing Person to add to. */
188
+ type Target = string
189
+
190
+ function ConfirmStep({
191
+ identified,
192
+ pending,
193
+ error,
194
+ onBack,
195
+ onNext,
196
+ }: {
197
+ readonly identified: PersonIntakeIdentifyResult
198
+ readonly pending: boolean
199
+ readonly error: Error | null
200
+ readonly onBack: () => void
201
+ readonly onNext: (candidate: PersonIntakeCandidate, existingPersonId: string | null) => void
202
+ }): React.JSX.Element {
203
+ const serviceLabel = useAiServiceLabel()
204
+ const first = identified.candidates[0]
205
+ const [candidateKey, setCandidateKey] = useState(first?.key ?? '')
206
+ const [target, setTarget] = useState<Target>(first?.existingPeople[0]?.id ?? 'new')
207
+ const candidate = identified.candidates.find((entry) => entry.key === candidateKey)
208
+
209
+ if (identified.candidates.length === 0) {
210
+ return (
211
+ <IntakeFrame
212
+ footer={
213
+ <button type="button" className={primaryButton} onClick={onBack}>
214
+ Add more detail
215
+ </button>
216
+ }
217
+ >
218
+ <p className="text-[13px] font-medium text-ink">{serviceLabel} could not tell who this is.</p>
219
+ {identified.question !== null && <p className="mt-1 text-[13px] text-ink-muted">{identified.question}</p>}
220
+ </IntakeFrame>
221
+ )
222
+ }
223
+
224
+ return (
225
+ <IntakeFrame
226
+ footer={
227
+ <>
228
+ <button type="button" className={secondaryButton} disabled={pending} onClick={onBack}>
229
+ Back
230
+ </button>
231
+ <button
232
+ type="button"
233
+ className={primaryButton}
234
+ disabled={pending || candidate === undefined}
235
+ onClick={() => {
236
+ if (candidate !== undefined) onNext(candidate, target === 'new' ? null : target)
237
+ }}
238
+ >
239
+ {pending ? 'Researching…' : 'This is them: research'}
240
+ </button>
241
+ </>
242
+ }
243
+ >
244
+ <fieldset disabled={pending}>
245
+ <legend className="mb-2 text-[12px] font-medium text-ink-muted">
246
+ {identified.candidates.length === 1 ? 'Is this the right person?' : 'Which of these is the right person?'}
247
+ </legend>
248
+ <div className="space-y-2">
249
+ {identified.candidates.map((entry) => (
250
+ <label
251
+ key={entry.key}
252
+ className={[
253
+ 'block cursor-pointer rounded-md border px-3 py-2',
254
+ entry.key === candidateKey ? 'border-accent bg-accent-soft/40' : 'border-border hover:border-border-strong',
255
+ ].join(' ')}
256
+ >
257
+ <span className="flex items-start gap-2">
258
+ <input
259
+ type="radio"
260
+ name="candidate"
261
+ className="mt-1"
262
+ checked={entry.key === candidateKey}
263
+ onChange={() => {
264
+ setCandidateKey(entry.key)
265
+ setTarget(entry.existingPeople[0]?.id ?? 'new')
266
+ }}
267
+ />
268
+ <span className="min-w-0 flex-1">
269
+ <span className="flex flex-wrap items-center gap-2">
270
+ <span className="text-[13px] font-semibold text-ink">{entry.name}</span>
271
+ <Chip tone={CONFIDENCE_TONES[entry.confidence]}>{entry.confidence} confidence</Chip>
272
+ </span>
273
+ {entry.headline !== '' && <span className="block text-[12px] text-ink">{entry.headline}</span>}
274
+ <span className="block text-[12px] text-ink-muted">
275
+ {[entry.location, entry.email].filter((part) => part !== null && part !== '').join(' · ')}
276
+ </span>
277
+ {entry.profileUrls.length > 0 && (
278
+ <span className="mt-0.5 flex flex-wrap gap-x-3 text-[12px]">
279
+ {entry.profileUrls.map((url) => (
280
+ <a key={url} href={url} target="_blank" rel="noreferrer noopener" className="text-accent hover:underline">
281
+ {hostOf(url)}
282
+ </a>
283
+ ))}
284
+ </span>
285
+ )}
286
+ {entry.evidence !== '' && <span className="mt-1 block text-[12px] text-ink-muted">{entry.evidence}</span>}
287
+ </span>
288
+ </span>
289
+ </label>
290
+ ))}
291
+ </div>
292
+
293
+ {candidate !== undefined && candidate.existingPeople.length > 0 && (
294
+ <div className="mt-4 rounded-md border border-warning/40 bg-warning-soft px-3 py-2">
295
+ <p className="text-[12px] font-medium text-ink">This may already be in Kelpie.</p>
296
+ <div className="mt-1 space-y-1">
297
+ {candidate.existingPeople.map((person) => (
298
+ <label key={person.id} className="flex items-center gap-2 text-[12px] text-ink">
299
+ <input
300
+ type="radio"
301
+ name="target"
302
+ checked={target === person.id}
303
+ onChange={() => {
304
+ setTarget(person.id)
305
+ }}
306
+ />
307
+ Update{' '}
308
+ <Link to={`/people/${person.id}`} target="_blank" className="font-medium text-accent hover:underline">
309
+ {person.name}
310
+ </Link>
311
+ {person.email !== null && <span className="text-ink-muted">{person.email}</span>}
312
+ </label>
313
+ ))}
314
+ <label className="flex items-center gap-2 text-[12px] text-ink">
315
+ <input
316
+ type="radio"
317
+ name="target"
318
+ checked={target === 'new'}
319
+ onChange={() => {
320
+ setTarget('new')
321
+ }}
322
+ />
323
+ Create a new person
324
+ </label>
325
+ </div>
326
+ </div>
327
+ )}
328
+ </fieldset>
329
+ {pending && (
330
+ <p className="mt-3 text-[12px] text-ink-muted">
331
+ {serviceLabel} is researching this person. With web search on, this can take a minute. You can keep working: this task stays open in the drawer.
332
+ </p>
333
+ )}
334
+ <Sources sources={identified.sources} />
335
+ {error !== null && (
336
+ <div className="mt-3">
337
+ <ErrorPanel error={error} />
338
+ </div>
339
+ )}
340
+ </IntakeFrame>
341
+ )
342
+ }
343
+
344
+ function ChooseStep({
345
+ research,
346
+ pending,
347
+ error,
348
+ onBack,
349
+ onNext,
350
+ }: {
351
+ readonly research: PersonIntakeResearchResult
352
+ readonly pending: boolean
353
+ readonly error: Error | null
354
+ readonly onBack: () => void
355
+ readonly onNext: (items: readonly PersonIntakeItem[]) => void
356
+ }): React.JSX.Element {
357
+ const [ticked, setTicked] = useState<ReadonlySet<string>>(
358
+ () => new Set(research.items.filter((item) => !OFF_BY_DEFAULT.has(item.kind)).map((item) => item.key)),
359
+ )
360
+ const effective = effectiveSelection(research.items, ticked)
361
+ const chosen = research.items.filter((item) => effective.has(item.key))
362
+ const companyNames = new Map(
363
+ research.items.flatMap((item) => (item.kind === 'company' ? [[item.key, item.fields.name] as const] : [])),
364
+ )
365
+ const ordered = [...research.items].sort(
366
+ (left, right) => PERSON_INTAKE_KINDS.indexOf(left.kind) - PERSON_INTAKE_KINDS.indexOf(right.kind),
367
+ )
368
+
369
+ function toggle(key: string): void {
370
+ setTicked((current) => {
371
+ const next = new Set(current)
372
+ if (next.has(key)) next.delete(key)
373
+ else next.add(key)
374
+ return next
375
+ })
376
+ }
377
+
378
+ return (
379
+ <IntakeFrame
380
+ footer={
381
+ <>
382
+ <button type="button" className={secondaryButton} disabled={pending} onClick={onBack}>
383
+ Start again
384
+ </button>
385
+ <button
386
+ type="button"
387
+ className={primaryButton}
388
+ disabled={pending || chosen.length === 0}
389
+ onClick={() => {
390
+ onNext(chosen)
391
+ }}
392
+ >
393
+ {pending ? 'Creating…' : `Create ${String(chosen.length)} ${chosen.length === 1 ? 'item' : 'items'}`}
394
+ </button>
395
+ </>
396
+ }
397
+ >
398
+ {research.summary !== '' && <p className="mb-3 text-[13px] text-ink">{research.summary}</p>}
399
+ <p className="mb-2 text-[12px] font-medium text-ink-muted">Kelpie will create what you tick.</p>
400
+ <ul className="space-y-1.5">
401
+ {ordered.map((item) => {
402
+ const blocked = ticked.has(item.key) && !effective.has(item.key)
403
+ const missing = personIntakeDependencies(item).filter((key) => !effective.has(key))
404
+
405
+ return (
406
+ <li key={item.key}>
407
+ <label
408
+ className={[
409
+ 'flex cursor-pointer items-start gap-2 rounded-md border px-3 py-2',
410
+ effective.has(item.key) ? 'border-border-strong' : 'border-border',
411
+ missing.length > 0 ? 'opacity-60' : '',
412
+ ].join(' ')}
413
+ >
414
+ <input
415
+ type="checkbox"
416
+ className="mt-0.5"
417
+ checked={effective.has(item.key)}
418
+ disabled={pending || missing.length > 0}
419
+ onChange={() => {
420
+ toggle(item.key)
421
+ }}
422
+ />
423
+ <span className="min-w-0 flex-1">
424
+ <span className="flex flex-wrap items-center gap-2">
425
+ <Chip tone={item.kind === 'person' ? 'accent' : 'neutral'}>{PERSON_INTAKE_KIND_LABELS[item.kind]}</Chip>
426
+ <span className="text-[13px] font-medium text-ink">{itemTitle(item, companyNames)}</span>
427
+ {actionNote(item) !== null && <span className="text-[11px] text-ink-muted">{actionNote(item)}</span>}
428
+ </span>
429
+ <ItemDetail item={item} />
430
+ {(missing.length > 0 || blocked) && (
431
+ <span className="mt-0.5 block text-[11px] text-ink-muted">
432
+ Needs the {missing.map((key) => describeKey(key, research.items)).join(' and ')} ticked.
433
+ </span>
434
+ )}
435
+ </span>
436
+ </label>
437
+ </li>
438
+ )
439
+ })}
440
+ </ul>
441
+ <Sources sources={research.sources} />
442
+ {error !== null && (
443
+ <div className="mt-3">
444
+ <ErrorPanel error={error} />
445
+ </div>
446
+ )}
447
+ </IntakeFrame>
448
+ )
449
+ }
450
+
451
+ function describeKey(key: string, items: readonly PersonIntakeItem[]): string {
452
+ const item = items.find((entry) => entry.key === key)
453
+
454
+ if (item === undefined) return key === PERSON_INTAKE_PERSON_KEY ? 'person' : 'company'
455
+
456
+ return item.kind === 'company' || item.kind === 'person' ? `${item.kind} ${item.fields.name}` : item.kind
457
+ }
458
+
459
+ function itemTitle(item: PersonIntakeItem, companyNames: ReadonlyMap<string, string>): string {
460
+ switch (item.kind) {
461
+ case 'person':
462
+ case 'company':
463
+ case 'partnership':
464
+ case 'deal':
465
+ case 'enquiry':
466
+ return item.fields.name
467
+ case 'position': {
468
+ const company = companyNames.get(item.companyKey) ?? 'the company'
469
+ return item.title === '' ? `At ${company}` : `${item.title} at ${company}`
470
+ }
471
+ case 'note':
472
+ return 'Pinned research note'
473
+ }
474
+ }
475
+
476
+ function actionNote(item: PersonIntakeItem): string | null {
477
+ if (item.kind === 'person' && item.action === 'update') return 'Adds to the existing record'
478
+ if (item.kind === 'company' && item.action === 'existing') return 'Already in Kelpie; links to it'
479
+ return null
480
+ }
481
+
482
+ function ItemDetail({ item }: { readonly item: PersonIntakeItem }): React.JSX.Element | null {
483
+ const muted = 'mt-0.5 block text-[12px] text-ink-muted'
484
+
485
+ switch (item.kind) {
486
+ case 'person': {
487
+ const fields = item.fields
488
+ const facts = [
489
+ fields.email ?? null,
490
+ fields.phones !== undefined && fields.phones.length > 0 ? fields.phones.join(', ') : null,
491
+ fields.socialProfiles !== undefined && fields.socialProfiles.length > 0
492
+ ? fields.socialProfiles.map((profile) => profile.network).join(', ')
493
+ : null,
494
+ fields.tags !== undefined && fields.tags.length > 0 ? `Tags: ${fields.tags.join(', ')}` : null,
495
+ ].filter((fact): fact is string => fact !== null && fact !== '')
496
+
497
+ return (
498
+ <>
499
+ {facts.length > 0 && <span className={muted}>{facts.join(' · ')}</span>}
500
+ {fields.summary !== undefined && fields.summary !== '' && <span className={muted}>{fields.summary}</span>}
501
+ </>
502
+ )
503
+ }
504
+ case 'company': {
505
+ if (item.action === 'existing') return null
506
+ const fields = item.fields
507
+ const facts = [fields.domain ?? null, fields.industry ?? null, fields.sizeBand ?? null].filter(
508
+ (fact): fact is string => fact !== null && fact !== '',
509
+ )
510
+ return (
511
+ <>
512
+ {facts.length > 0 && <span className={muted}>{facts.join(' · ')}</span>}
513
+ {fields.description !== undefined && fields.description !== '' && (
514
+ <span className={muted}>{fields.description}</span>
515
+ )}
516
+ </>
517
+ )
518
+ }
519
+ case 'position':
520
+ return null
521
+ case 'note':
522
+ return <span className={`${muted} line-clamp-4 whitespace-pre-wrap`}>{item.body}</span>
523
+ case 'partnership':
524
+ case 'deal':
525
+ case 'enquiry':
526
+ return item.reason === '' ? null : <span className={`${muted} italic`}>Why: {item.reason}</span>
527
+ }
528
+ }
529
+
530
+ function Sources({ sources }: { readonly sources: readonly PersonIntakeSource[] }): React.JSX.Element | null {
531
+ if (sources.length === 0) return null
532
+
533
+ return (
534
+ <div className="mt-4">
535
+ <p className="text-[11px] font-medium uppercase tracking-wide text-ink-faint">Sources</p>
536
+ <ul className="mt-1 space-y-0.5">
537
+ {sources.map((source) => (
538
+ <li key={source.url} className="truncate text-[12px]">
539
+ <a href={source.url} target="_blank" rel="noreferrer noopener" className="text-accent hover:underline">
540
+ {source.title === source.url ? hostOf(source.url) : source.title}
541
+ </a>{' '}
542
+ <span className="text-ink-faint">{hostOf(source.url)}</span>
543
+ </li>
544
+ ))}
545
+ </ul>
546
+ </div>
547
+ )
548
+ }
549
+
550
+ const STATUS_TONES: Readonly<Record<PersonIntakeResultStatus, ChipTone>> = {
551
+ created: 'success',
552
+ updated: 'success',
553
+ linked: 'neutral',
554
+ skipped: 'warning',
555
+ failed: 'danger',
556
+ }
557
+
558
+ /** Where a written item lives. Positions and notes show on their Person. */
559
+ function pathFor(result: PersonIntakeResult, personId: string | null): string | null {
560
+ if (result.id === null) return null
561
+
562
+ switch (result.kind) {
563
+ case 'person':
564
+ return `/people/${result.id}`
565
+ case 'company':
566
+ return `/companies/${result.id}`
567
+ case 'position':
568
+ case 'note':
569
+ return personId === null ? null : `/people/${personId}`
570
+ case 'partnership':
571
+ return `/partnerships/${result.id}`
572
+ case 'deal':
573
+ return `/deals/${result.id}`
574
+ case 'enquiry':
575
+ return `/enquiries/${result.id}`
576
+ }
577
+ }
578
+
579
+ function DoneStep({
580
+ results,
581
+ onClose,
582
+ }: {
583
+ readonly results: readonly PersonIntakeResult[]
584
+ readonly onClose: () => void
585
+ }): React.JSX.Element {
586
+ const person = results.find((result) => result.kind === 'person' && result.id !== null)
587
+ const personId = person?.id ?? null
588
+ const problems = results.filter((result) => result.status === 'failed' || result.status === 'skipped').length
589
+
590
+ return (
591
+ <IntakeFrame
592
+ footer={
593
+ <>
594
+ <button type="button" className={secondaryButton} onClick={onClose}>
595
+ Close
596
+ </button>
597
+ {person !== undefined && personId !== null && (
598
+ <Link to={`/people/${personId}`} className={primaryButton}>
599
+ Open {person.label}
600
+ </Link>
601
+ )}
602
+ </>
603
+ }
604
+ >
605
+ <p className="mb-2 text-[13px] text-ink">
606
+ {problems === 0 ? 'Done. Kelpie wrote these records.' : 'Done, with problems. Check the items marked below.'}
607
+ </p>
608
+ <ul className="space-y-1.5">
609
+ {results.map((result) => {
610
+ const path = pathFor(result, personId)
611
+
612
+ return (
613
+ <li key={result.key} className="flex flex-wrap items-center gap-2 text-[13px]">
614
+ <Chip tone={STATUS_TONES[result.status]}>{result.status}</Chip>
615
+ <span className="text-[11px] text-ink-muted">{PERSON_INTAKE_KIND_LABELS[result.kind]}</span>
616
+ {path === null ? (
617
+ <span className="text-ink">{result.label}</span>
618
+ ) : (
619
+ <Link to={path} className="font-medium text-accent hover:underline">
620
+ {result.label}
621
+ </Link>
622
+ )}
623
+ {result.detail !== null && <span className="w-full pl-1 text-[12px] text-ink-muted">{result.detail}</span>}
624
+ </li>
625
+ )
626
+ })}
627
+ </ul>
628
+ </IntakeFrame>
629
+ )
630
+ }
631
+
632
+ function hostOf(url: string): string {
633
+ try {
634
+ return new URL(url).hostname.replace(/^www\./u, '')
635
+ } catch {
636
+ return url
637
+ }
638
+ }
@@ -1,23 +1,27 @@
1
- import { AiPage } from '../pages/admin/AiPage.tsx'
1
+ import { AiRunLogTab, AiSettingsTab } from '../pages/admin/AiPage.tsx'
2
2
  import type { UiModule } from '../registry/registry.ts'
3
3
  import { aiRunner } from './aiRunner.ts'
4
+ import { PersonIntakeWizard } from './PersonIntakeWizard.tsx'
4
5
 
5
6
  /**
6
7
  * The UI half of the optional `ai` module in `@kelpie/server`.
7
8
  *
8
9
  * Not registered by default. An assembly that lists `createAiModule()` on the
9
10
  * server lists `aiUi` in `kelpie.ui.config.ts`, the same pairing any module
10
- * uses. One Admin nav item, after MCP, and one page. On record pages it is the
11
- * Agent menu's runner: Run dispatches straight to the "Kelpie AI" agent row, and
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
13
  * stays disabled until AI is enabled and has a key. Preview's Run… still lists
13
- * every registered agent, Kelpie AI included.
14
+ * every registered agent, Kelpie AI included. On People it is the wizard behind
15
+ * Add from notes.
14
16
  */
15
17
  export const aiUi: UiModule = {
16
18
  id: 'ai',
17
19
 
18
20
  register(context) {
19
- context.nav('admin', { id: 'ai', label: 'AI', to: '/admin/ai', order: 550 })
20
- context.route({ path: 'admin/ai', element: <AiPage /> })
21
+ context.adminTab('ai', { id: 'settings', label: 'Settings', order: 100, render: () => <AiSettingsTab /> })
22
+ context.adminTab('ai', { id: 'runs', label: 'Run log', order: 200, render: () => <AiRunLogTab /> })
21
23
  context.agentRunner(aiRunner)
24
+ // Ready on exactly the terms Run is: enabled, keyed, and its agent row in place.
25
+ context.personIntake({ id: 'ai', useAvailability: aiRunner.useAvailability, Wizard: PersonIntakeWizard })
22
26
  },
23
27
  }