@hanzo/browser-extension 1.9.35 → 1.9.46

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 (53) hide show
  1. package/dist/safari/Hanzo AI/Hanzo AI.xcodeproj/project.pbxproj +1059 -0
  2. package/dist/safari/Hanzo AI/Hanzo AI.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  3. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AccentColor.colorset/Contents.json +11 -0
  4. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/Contents.json +98 -0
  5. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-128@1x.png +0 -0
  6. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-128@2x.png +0 -0
  7. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-16@1x.png +0 -0
  8. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-16@2x.png +0 -0
  9. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-256@1x.png +0 -0
  10. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-256@2x.png +0 -0
  11. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-32@1x.png +0 -0
  12. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-32@2x.png +0 -0
  13. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-512@1x.png +0 -0
  14. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/mac-icon-512@2x.png +0 -0
  15. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/AppIcon.appiconset/universal-icon-1024@1x.png +0 -0
  16. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/Contents.json +6 -0
  17. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/LargeIcon.imageset/Contents.json +21 -0
  18. package/dist/safari/Hanzo AI/Shared (App)/Assets.xcassets/LargeIcon.imageset/icon128.png +0 -0
  19. package/dist/safari/Hanzo AI/Shared (App)/Resources/Base.lproj/Main.html +20 -0
  20. package/dist/safari/Hanzo AI/Shared (App)/Resources/Icon.png +0 -0
  21. package/dist/safari/Hanzo AI/Shared (App)/Resources/Script.js +24 -0
  22. package/dist/safari/Hanzo AI/Shared (App)/Resources/Style.css +61 -0
  23. package/dist/safari/Hanzo AI/Shared (App)/ViewController.swift +81 -0
  24. package/dist/safari/Hanzo AI/Shared (Extension)/SafariWebExtensionHandler.swift +42 -0
  25. package/dist/safari/Hanzo AI/iOS (App)/AppDelegate.swift +24 -0
  26. package/dist/safari/Hanzo AI/iOS (App)/Base.lproj/LaunchScreen.storyboard +36 -0
  27. package/dist/safari/Hanzo AI/iOS (App)/Base.lproj/Main.storyboard +38 -0
  28. package/dist/safari/Hanzo AI/iOS (App)/Info.plist +27 -0
  29. package/dist/safari/Hanzo AI/iOS (App)/SceneDelegate.swift +18 -0
  30. package/dist/safari/Hanzo AI/iOS (Extension)/Info.plist +13 -0
  31. package/dist/safari/Hanzo AI/macOS (App)/AppDelegate.swift +21 -0
  32. package/dist/safari/Hanzo AI/macOS (App)/Base.lproj/Main.storyboard +124 -0
  33. package/dist/safari/Hanzo AI/macOS (App)/Info.plist +8 -0
  34. package/dist/safari/Hanzo AI/macOS (Extension)/Info.plist +13 -0
  35. package/package.json +20 -14
  36. package/src/answer/AnswerEngine.tsx +180 -348
  37. package/src/answer/Composer.tsx +147 -0
  38. package/src/answer/Mark.tsx +14 -0
  39. package/src/answer/Models.tsx +118 -0
  40. package/src/answer/Rail.tsx +69 -0
  41. package/src/answer/Source.tsx +26 -0
  42. package/src/answer/Stories.tsx +27 -0
  43. package/src/answer/WidgetView.tsx +136 -0
  44. package/src/manifest.json +4 -1
  45. package/src/newtab.css +187 -35
  46. package/src/onboarding.css +78 -0
  47. package/src/onboarding.html +45 -0
  48. package/src/popup.html +29 -1
  49. package/src/sidebar.css +0 -40
  50. package/src/sidebar.html +5 -11
  51. package/LICENSE +0 -12
  52. package/src/chat-widget.tsx +0 -96
  53. package/src/gui-primitives.tsx +0 -116
@@ -1,17 +1,56 @@
1
1
  import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
- import { createAiClient, type SearchEvent, type SearchMode, type SearchSource } from '@hanzo/ai';
2
+ import { createAiClient, type AiClient, type SearchEvent, type SearchSource } from '@hanzo/ai';
3
3
  import { fetchNews, type NewsItem } from './news.js';
4
4
  import { AUTO_MODEL, fetchModelCatalog, type ChatModel, type ModelGroup } from './model-catalog.js';
5
5
  import { renderMarkdown } from './markdown.js';
6
+ import { capture, contributeTrainingSample } from '../shared/consent.js';
7
+ import { Composer, type Mode } from './Composer.js';
8
+ import { Rail } from './Rail.js';
9
+ import { Source } from './Source.js';
10
+ import { Stories } from './Stories.js';
11
+ import { Widgets } from './WidgetView.js';
12
+ import { extractWidgets } from './widget.js';
13
+ import { DEFAULT_PROVIDER_ID, fallbackUrl, loadProviderId, providerById } from './websearch.js';
6
14
 
7
15
  /**
8
16
  * Hanzo Answer Engine — the shared AI-answer surface (query → streamed answer +
9
17
  * inline citations + sources + live news). Backend = api.hanzo.ai ONLY, reached
10
- * through the @hanzo/ai `search`/`deepResearch` primitive (the one place the
11
- * capability is defined). Rendered on the @hanzo/gui primitive surface; strict
12
- * monochrome per the Hanzo brand.
18
+ * through @hanzo/ai: `search` for the grounded modes, `chat.completions` for
19
+ * Chat (each capability defined in exactly one place). Strict monochrome per
20
+ * the Hanzo brand.
21
+ *
22
+ * This file holds the state and the wire. Everything it draws with is a
23
+ * presentational sibling — Rail, Composer (Models), Source, Stories — each one
24
+ * a component whose data and effects arrive as props, so the same parts can be
25
+ * lifted into @hanzo/ui and mounted by chat and app without dragging the
26
+ * extension's transport along.
13
27
  */
14
28
 
29
+ /**
30
+ * Chat mode — the same question, ungrounded, straight to the model. It yields
31
+ * `SearchEvent`s so the surface keeps ONE stream to consume and ONE way to
32
+ * render an answer; a mode is a value the wire takes, never a second UI.
33
+ */
34
+ async function* converse(
35
+ client: AiClient,
36
+ question: string,
37
+ model: string,
38
+ signal: AbortSignal,
39
+ ): AsyncGenerator<SearchEvent> {
40
+ const stream = await client.chat.completions.create(
41
+ { model, messages: [{ role: 'user', content: question }], stream: true },
42
+ { signal },
43
+ );
44
+ let answer = '';
45
+ for await (const chunk of stream) {
46
+ const delta = chunk.choices?.[0]?.delta?.content;
47
+ if (!delta) continue;
48
+ answer += delta;
49
+ yield { type: 'text', delta };
50
+ }
51
+ yield { type: 'done', answer, sources: [] };
52
+ }
53
+
15
54
  export interface AnswerEngineProps {
16
55
  apiBase: string;
17
56
  /** Resolves the current IAM bearer, or null when signed out. */
@@ -22,257 +61,24 @@ export interface AnswerEngineProps {
22
61
  initialQuery?: string;
23
62
  }
24
63
 
25
- const MODES: { id: SearchMode; label: string; hint: string }[] = [
26
- { id: 'search', label: 'Search', hint: 'Fast grounded answer' },
27
- { id: 'news', label: 'News', hint: 'Recency-biased' },
28
- { id: 'research', label: 'Research', hint: 'Multi-source synthesis' },
29
- { id: 'deep', label: 'Deep', hint: 'Plan + wide research' },
30
- ];
31
-
32
- const SOURCE_CHIPS = ['web', 'news', 'academic', 'github', 'reddit', 'x'];
33
-
34
- // Hanzo tiers mirror @hanzo/plans (the SoT for pricing); shown in the picker's
35
- // upgrade banner. Full plan detail lives at console.hanzo.ai/pricing.
36
- const PLAN_TIERS = [
37
- { name: 'Pro', price: '$20' },
38
- { name: 'Max', price: '$100' },
39
- { name: 'Ultra', price: '$200' },
40
- ];
41
-
42
- function HanzoMark({ size = 28 }: { size?: number }) {
43
- return (
44
- <svg viewBox="0 0 67 67" width={size} height={size} xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
45
- <path d="M22.21 67V44.6369H0V67H22.21Z" fill="currentColor" />
46
- <path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z" fill="currentColor" />
47
- <path d="M22.21 0H0V22.3184H22.21V0Z" fill="currentColor" />
48
- <path d="M66.7198 0H44.5098V22.3184H66.7198V0Z" fill="currentColor" />
49
- <path d="M66.7198 67V44.6369H44.5098V67H66.7198Z" fill="currentColor" />
50
- </svg>
51
- );
52
- }
53
-
54
- function ModelPicker({
55
- groups,
56
- current,
57
- onPick,
58
- }: {
59
- groups: ModelGroup[];
60
- current: ChatModel;
61
- onPick: (m: ChatModel) => void;
62
- }) {
63
- const [open, setOpen] = useState(false);
64
- const [filter, setFilter] = useState('');
65
- const ref = useRef<HTMLDivElement>(null);
66
-
67
- useEffect(() => {
68
- if (!open) return;
69
- const onDoc = (e: MouseEvent) => {
70
- if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
71
- };
72
- document.addEventListener('mousedown', onDoc);
73
- return () => document.removeEventListener('mousedown', onDoc);
74
- }, [open]);
75
-
76
- const q = filter.trim().toLowerCase();
77
- const shown = useMemo(
78
- () =>
79
- groups
80
- .map((g) => ({
81
- ...g,
82
- models: g.models.filter(
83
- (m) => !q || m.name.toLowerCase().includes(q) || m.id.toLowerCase().includes(q),
84
- ),
85
- }))
86
- .filter((g) => g.models.length),
87
- [groups, q],
88
- );
89
-
90
- return (
91
- <div className="ae-picker" ref={ref}>
92
- <button className="ae-picker-btn" onClick={() => setOpen((v) => !v)} title="Choose model">
93
- <span className="ae-dot" />
94
- {current.name}
95
- <svg viewBox="0 0 16 16" width="12" height="12" aria-hidden="true">
96
- <path d="M4 6l4 4 4-4" fill="none" stroke="currentColor" strokeWidth="1.5" />
97
- </svg>
98
- </button>
99
- {open && (
100
- <div className="ae-picker-menu">
101
- <input
102
- className="ae-picker-filter"
103
- placeholder="Search models…"
104
- value={filter}
105
- autoFocus
106
- onChange={(e) => setFilter(e.target.value)}
107
- />
108
- <div className="ae-picker-list">
109
- <button
110
- className={'ae-model-row' + (current.id === AUTO_MODEL.id ? ' active' : '')}
111
- onClick={() => {
112
- onPick(AUTO_MODEL);
113
- setOpen(false);
114
- }}
115
- >
116
- <span className="ae-model-name">Auto</span>
117
- <span className="ae-model-desc">Routes to the best model</span>
118
- </button>
119
- {shown.map((g) => (
120
- <div key={g.family} className="ae-model-group">
121
- <div className="ae-model-group-label">{g.label}</div>
122
- {g.models.map((m) => (
123
- <button
124
- key={m.id}
125
- className={'ae-model-row' + (current.id === m.id ? ' active' : '')}
126
- onClick={() => {
127
- onPick(m);
128
- setOpen(false);
129
- }}
130
- >
131
- <span className="ae-model-name">
132
- {m.name}
133
- {m.tier && <span className="ae-badge">{m.tier}</span>}
134
- </span>
135
- {m.description && <span className="ae-model-desc">{m.description}</span>}
136
- </button>
137
- ))}
138
- </div>
139
- ))}
140
- </div>
141
- <a className="ae-plan-banner" href="https://console.hanzo.ai/pricing" target="_blank" rel="noreferrer">
142
- <span>Unlock every model</span>
143
- <span className="ae-plan-tiers">
144
- {PLAN_TIERS.map((t) => (
145
- <span key={t.name} className="ae-plan-tier">
146
- {t.name} <b>{t.price}</b>
147
- </span>
148
- ))}
149
- </span>
150
- </a>
151
- </div>
152
- )}
153
- </div>
154
- );
155
- }
156
-
157
- function SourceCard({ s, index }: { s: SearchSource; index: number }) {
158
- let host = '';
159
- try {
160
- host = new URL(s.url).hostname.replace(/^www\./, '');
161
- } catch {
162
- host = s.url;
163
- }
164
- return (
165
- <a className="ae-source" href={s.url} target="_blank" rel="noreferrer" title={s.title}>
166
- <span className="ae-source-head">
167
- {s.favicon ? <img src={s.favicon} alt="" width="14" height="14" /> : <span className="ae-dot" />}
168
- <span className="ae-source-host">{host}</span>
169
- <span className="ae-source-idx">{index + 1}</span>
170
- </span>
171
- <span className="ae-source-title">{s.title}</span>
172
- </a>
173
- );
174
- }
175
-
176
- function InputCard({
177
- value,
178
- onChange,
179
- onSubmit,
180
- mode,
181
- setMode,
182
- sources,
183
- toggleSource,
184
- models,
185
- model,
186
- setModel,
187
- busy,
188
- }: {
189
- value: string;
190
- onChange: (v: string) => void;
191
- onSubmit: () => void;
192
- mode: SearchMode;
193
- setMode: (m: SearchMode) => void;
194
- sources: Set<string>;
195
- toggleSource: (s: string) => void;
196
- models: ModelGroup[];
197
- model: ChatModel;
198
- setModel: (m: ChatModel) => void;
199
- busy: boolean;
200
- }) {
201
- const taRef = useRef<HTMLTextAreaElement>(null);
202
- useEffect(() => {
203
- const ta = taRef.current;
204
- if (!ta) return;
205
- ta.style.height = 'auto';
206
- ta.style.height = Math.min(ta.scrollHeight, 200) + 'px';
207
- }, [value]);
208
-
209
- return (
210
- <div className="ae-card">
211
- <textarea
212
- ref={taRef}
213
- className="ae-input"
214
- placeholder="Type @ for sources or / for modes"
215
- value={value}
216
- rows={1}
217
- onChange={(e) => onChange(e.target.value)}
218
- onKeyDown={(e) => {
219
- if (e.key === 'Enter' && !e.shiftKey) {
220
- e.preventDefault();
221
- onSubmit();
222
- }
223
- }}
224
- />
225
- <div className="ae-card-row">
226
- <div className="ae-chips">
227
- {SOURCE_CHIPS.map((s) => (
228
- <button
229
- key={s}
230
- className={'ae-chip' + (sources.has(s) ? ' on' : '')}
231
- onClick={() => toggleSource(s)}
232
- title={`@${s}`}
233
- >
234
- @{s}
235
- </button>
236
- ))}
237
- </div>
238
- <div className="ae-card-right">
239
- <div className="ae-modes">
240
- {MODES.map((m) => (
241
- <button
242
- key={m.id}
243
- className={'ae-mode' + (mode === m.id ? ' on' : '')}
244
- onClick={() => setMode(m.id)}
245
- title={m.hint}
246
- >
247
- {m.label}
248
- </button>
249
- ))}
250
- </div>
251
- <ModelPicker groups={models} current={model} onPick={setModel} />
252
- <button className="ae-send" onClick={onSubmit} disabled={busy || !value.trim()} title="Search (Enter)">
253
- {busy ? (
254
- <span className="ae-spinner" />
255
- ) : (
256
- <svg viewBox="0 0 16 16" width="15" height="15" aria-hidden="true">
257
- <path d="M3 13V3l10 5-10 5z" fill="currentColor" />
258
- </svg>
259
- )}
260
- </button>
261
- </div>
262
- </div>
263
- </div>
264
- );
265
- }
266
-
267
64
  export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: AnswerEngineProps) {
268
- const [collapsed, setCollapsed] = useState(false);
65
+ // Sidebar hidden by default — the answer surface should read like hanzo.chat:
66
+ // a single centered column. A fixed toggle (below) brings the rail back.
67
+ const [collapsed, setCollapsed] = useState(true);
269
68
  const [query, setQuery] = useState('');
270
69
  const [submitted, setSubmitted] = useState('');
271
- const [mode, setMode] = useState<SearchMode>('search');
70
+ const [mode, setMode] = useState<Mode>('search');
272
71
  const [sources, setSources] = useState<Set<string>>(new Set());
273
72
  const [modelGroups, setModelGroups] = useState<ModelGroup[]>([]);
274
73
  const [model, setModel] = useState<ChatModel>(AUTO_MODEL);
275
74
  const [news, setNews] = useState<NewsItem[]>([]);
75
+ // True once the user picks a model themselves, so the catalog's default-to-Enso
76
+ // never overwrites a deliberate choice made while the fetch was still in flight.
77
+ const picked = useRef(false);
78
+ const chooseModel = useCallback((m: ChatModel) => {
79
+ picked.current = true;
80
+ setModel(m);
81
+ }, []);
276
82
 
277
83
  const [phase, setPhase] = useState<'idle' | 'running' | 'done'>('idle');
278
84
  const [status, setStatus] = useState('');
@@ -281,15 +87,32 @@ export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: Answer
281
87
  const [followUps, setFollowUps] = useState<string[]>([]);
282
88
  const [error, setError] = useState('');
283
89
  const [needAuth, setNeedAuth] = useState(false);
90
+ // The engine an unauthenticated query falls back to. Read once; the default
91
+ // stands until storage answers, so a query typed in the first moments after a
92
+ // new tab opens still has somewhere to go.
93
+ const [providerId, setProviderId] = useState<string>(DEFAULT_PROVIDER_ID);
284
94
  const abortRef = useRef<AbortController | null>(null);
285
95
 
286
96
  // Load real model catalog + live news (both public — work signed out).
287
- // Default stays on Auto ("routes to the best model"); the picker changes it.
97
+ //
98
+ // Enso is the default, selected once the catalog lands. It cannot be the
99
+ // useState seed: the catalog is fetched at runtime, so naming a model before
100
+ // it arrives would hardcode an id the server may not serve — the whole point
101
+ // of model-catalog.ts is that the list is never fabricated. AUTO_MODEL is the
102
+ // seed and the fallback, so a failed/slow fetch degrades to "routes to the
103
+ // best model" instead of a dead selection. Only the initial pick is
104
+ // overridden: `picked` guards a user's choice against a late catalog resolve.
288
105
  useEffect(() => {
289
- fetchModelCatalog(apiBase)
290
- .then(({ groups }) => setModelGroups(groups))
106
+ fetchModelCatalog()
107
+ .then(({ groups }) => {
108
+ setModelGroups(groups);
109
+ if (picked.current) return;
110
+ const enso = groups.find((g) => g.family === 'enso')?.models[0];
111
+ if (enso) setModel(enso);
112
+ })
291
113
  .catch(() => {});
292
114
  fetchNews().then(setNews).catch(() => {});
115
+ loadProviderId().then(setProviderId).catch(() => {});
293
116
  }, [apiBase]);
294
117
 
295
118
  const client = useMemo(
@@ -313,32 +136,61 @@ export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: Answer
313
136
  const ctrl = new AbortController();
314
137
  abortRef.current = ctrl;
315
138
 
139
+ // NO IDENTITY, NO DEAD END. /v1/ask is metered and answers 401 to a
140
+ // signed-out caller, but this surface IS the browser's search box: a query
141
+ // that returns a sign-in card has broken the address bar rather than shown a
142
+ // paywall. Hand it to the configured engine instead, before any spinner —
143
+ // checking first is what makes the handoff instant rather than a flash of
144
+ // "Searching the web" followed by a navigation.
145
+ //
146
+ // The fallback is not a Hanzo call: no token, no proxy, no telemetry. The
147
+ // query goes from this browser straight to the engine the user chose.
148
+ let identified = false;
149
+ try {
150
+ identified = Boolean(await getToken());
151
+ } catch {
152
+ identified = false;
153
+ }
154
+ if (!identified) {
155
+ const url = fallbackUrl(providerId, question);
156
+ if (url) {
157
+ void capture('answer_search_fallback', { provider: providerId, mode });
158
+ window.location.assign(url);
159
+ return;
160
+ }
161
+ }
162
+
316
163
  setSubmitted(question);
317
164
  setPhase('running');
318
- setStatus('Searching the web');
165
+ setStatus(mode === 'chat' ? 'Thinking' : 'Searching the web');
319
166
  setAnswer('');
320
167
  setAnswerSources([]);
321
168
  setFollowUps([]);
322
169
  setError('');
323
170
  setNeedAuth(false);
171
+ void capture('answer_search', { mode, model: model.id });
172
+ let fullAnswer = '';
324
173
 
325
- const opts = {
326
- mode,
327
- model: model.id,
328
- sources: [...sources],
329
- signal: ctrl.signal,
330
- };
174
+ const stream =
175
+ mode === 'chat'
176
+ ? converse(client, question, model.id, ctrl.signal)
177
+ : (client.search(question, {
178
+ mode,
179
+ model: model.id,
180
+ sources: [...sources],
181
+ signal: ctrl.signal,
182
+ }) as AsyncGenerator<SearchEvent>);
331
183
  try {
332
- for await (const ev of client.search(question, opts) as AsyncGenerator<SearchEvent>) {
184
+ for await (const ev of stream) {
333
185
  switch (ev.type) {
334
186
  case 'status':
335
187
  setStatus(
336
188
  ev.stage === 'planning'
337
- ? 'Planning research'
189
+ ? 'Planning research' + (ev.detail ? `: ${ev.detail}` : '')
338
190
  : ev.stage === 'searching'
339
191
  ? 'Searching the web' + (ev.detail ? `: ${ev.detail}` : '')
340
192
  : ev.stage === 'reading'
341
- ? 'Reading sources'
193
+ ? 'Reading sources' + (ev.detail ? `: ${ev.detail}` : '')
342
194
  : 'Writing the answer',
343
195
  );
344
196
  break;
@@ -346,6 +198,7 @@ export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: Answer
346
198
  setAnswerSources(ev.sources);
347
199
  break;
348
200
  case 'text':
201
+ fullAnswer += ev.delta;
349
202
  setAnswer((a) => a + ev.delta);
350
203
  break;
351
204
  case 'follow_ups':
@@ -353,6 +206,10 @@ export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: Answer
353
206
  break;
354
207
  case 'done':
355
208
  setPhase('done');
209
+ void capture('answer_shown', { mode, model: model.id });
210
+ // Opt-in only: no-ops unless the user turned on training
211
+ // contribution (the consent module gates the send).
212
+ void contributeTrainingSample({ query: question, answer: fullAnswer, model: model.id });
356
213
  break;
357
214
  case 'error':
358
215
  if (ev.error.includes('__NO_TOKEN__') || /401|invalid api key/i.test(ev.error)) {
@@ -371,7 +228,7 @@ export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: Answer
371
228
  setPhase('done');
372
229
  }
373
230
  },
374
- [client, mode, model, sources],
231
+ [client, mode, model, sources, getToken, providerId],
375
232
  );
376
233
 
377
234
  // Fire the initial (address-bar) query once models are ready enough.
@@ -404,76 +261,39 @@ export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: Answer
404
261
  setNeedAuth(false);
405
262
  }, []);
406
263
 
264
+ const composer = (busy: boolean) => (
265
+ <Composer
266
+ value={query}
267
+ onChange={setQuery}
268
+ onSubmit={submit}
269
+ mode={mode}
270
+ setMode={setMode}
271
+ sources={sources}
272
+ toggleSource={toggleSource}
273
+ models={modelGroups}
274
+ model={model}
275
+ setModel={chooseModel}
276
+ busy={busy}
277
+ />
278
+ );
279
+
407
280
  return (
408
281
  <div className={'ae-root' + (collapsed ? ' collapsed' : '')}>
409
- <aside className="ae-sidebar">
410
- <div className="ae-side-top">
411
- <button className="ae-brand" onClick={newThread} title="Hanzo">
412
- <HanzoMark size={22} />
413
- {!collapsed && <span>Hanzo</span>}
414
- </button>
415
- <button className="ae-collapse" onClick={() => setCollapsed((v) => !v)} title="Toggle sidebar">
416
- <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
417
- <path d="M6 3v10M3 3h10v10H3z" fill="none" stroke="currentColor" strokeWidth="1.3" />
418
- </svg>
419
- </button>
420
- </div>
421
- <button className="ae-new" onClick={newThread}>
422
- <svg viewBox="0 0 16 16" width="15" height="15" aria-hidden="true">
423
- <path d="M8 3v10M3 8h10" fill="none" stroke="currentColor" strokeWidth="1.5" />
424
- </svg>
425
- {!collapsed && <span>New thread</span>}
426
- </button>
427
- <nav className="ae-nav">
428
- <a href="https://hanzo.chat" target="_blank" rel="noreferrer">Threads</a>
429
- <a href="https://docs.hanzo.ai" target="_blank" rel="noreferrer">Docs</a>
430
- <a href="https://hanzo.ai" target="_blank" rel="noreferrer">About</a>
431
- </nav>
432
- <div className="ae-side-bottom">
433
- <button className="ae-signin" onClick={signIn}>
434
- {collapsed ? '↪' : 'Sign in — free to start'}
435
- </button>
436
- </div>
437
- </aside>
282
+ {/* No fixed reveal button: the rail itself is always present now, and it
283
+ carries its own expand control. A floating toggle only existed because
284
+ the collapsed rail rendered nothing to click. */}
285
+ <Rail
286
+ collapsed={collapsed}
287
+ toggle={() => setCollapsed((v) => !v)}
288
+ newThread={newThread}
289
+ signIn={signIn}
290
+ />
438
291
 
439
292
  <main className="ae-main">
440
293
  {phase === 'idle' ? (
441
294
  <div className="ae-hero">
442
- <div className="ae-wordmark">
443
- <HanzoMark size={44} />
444
- <span>Hanzo</span>
445
- </div>
446
- <div className="ae-hero-card">
447
- <InputCard
448
- value={query}
449
- onChange={setQuery}
450
- onSubmit={submit}
451
- mode={mode}
452
- setMode={setMode}
453
- sources={sources}
454
- toggleSource={toggleSource}
455
- models={modelGroups}
456
- model={model}
457
- setModel={setModel}
458
- busy={false}
459
- />
460
- </div>
461
- {news.length > 0 && (
462
- <div className="ae-news">
463
- <div className="ae-news-head">Top stories</div>
464
- <div className="ae-news-grid">
465
- {news.slice(0, 6).map((n) => (
466
- <a key={n.url} className="ae-news-item" href={n.url} target="_blank" rel="noreferrer">
467
- <span className="ae-news-title">{n.title}</span>
468
- <span className="ae-news-meta">
469
- {n.source}
470
- {typeof n.points === 'number' ? ` · ${n.points} pts` : ''}
471
- </span>
472
- </a>
473
- ))}
474
- </div>
475
- </div>
476
- )}
295
+ <div className="ae-hero-card">{composer(false)}</div>
296
+ <Stories items={news} />
477
297
  </div>
478
298
  ) : (
479
299
  <div className="ae-answer-wrap">
@@ -483,7 +303,7 @@ export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: Answer
483
303
  {answerSources.length > 0 && (
484
304
  <div className="ae-sources">
485
305
  {answerSources.map((s, i) => (
486
- <SourceCard key={s.url} s={s} index={i} />
306
+ <Source key={s.url} source={s} index={i} />
487
307
  ))}
488
308
  </div>
489
309
  )}
@@ -495,19 +315,45 @@ export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: Answer
495
315
  )}
496
316
 
497
317
  {needAuth ? (
318
+ /* Reached only when a token EXISTED and the call still came back
319
+ 401 — an expired or revoked session, since a signed-out query
320
+ never gets here (run() hands it to the fallback engine first).
321
+ Both doors are offered: renew the session for an AI answer, or
322
+ take the same query to the configured engine. A dead end here
323
+ would strand someone who simply needs to sign in again. */
498
324
  <div className="ae-auth">
499
- <p>Sign in with Hanzo to get AI answers.</p>
325
+ <p>Your Hanzo session expired. Sign in again for AI answers.</p>
500
326
  <button className="ae-signin big" onClick={signIn}>
501
- Sign in — free to start
327
+ Sign in
502
328
  </button>
329
+ <a
330
+ className="ae-fallback-link"
331
+ href={fallbackUrl(providerId, submitted) || undefined}
332
+ >
333
+ Search {providerById(providerId).label} instead
334
+ </a>
503
335
  </div>
504
336
  ) : error ? (
505
337
  <div className="ae-error">{error}</div>
506
338
  ) : (
507
- <div
508
- className="ae-answer markdown"
509
- dangerouslySetInnerHTML={{ __html: renderMarkdown(answer) }}
510
- />
339
+ /* The answer picks its own presentation: any widget blocks it
340
+ emitted render as real components, and the prose is what is
341
+ left after they are removed — so a widget is never also shown
342
+ as raw JSON. Extraction runs per frame against a STREAMING,
343
+ half-written document, which is why an unterminated fence is
344
+ left alone until it closes. */
345
+ (() => {
346
+ const { prose, widgets } = extractWidgets(answer);
347
+ return (
348
+ <>
349
+ <Widgets widgets={widgets} />
350
+ <div
351
+ className="ae-answer markdown"
352
+ dangerouslySetInnerHTML={{ __html: renderMarkdown(prose) }}
353
+ />
354
+ </>
355
+ );
356
+ })()
511
357
  )}
512
358
 
513
359
  {followUps.length > 0 && (
@@ -530,21 +376,7 @@ export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: Answer
530
376
  )}
531
377
  </div>
532
378
 
533
- <div className="ae-answer-composer">
534
- <InputCard
535
- value={query}
536
- onChange={setQuery}
537
- onSubmit={submit}
538
- mode={mode}
539
- setMode={setMode}
540
- sources={sources}
541
- toggleSource={toggleSource}
542
- models={modelGroups}
543
- model={model}
544
- setModel={setModel}
545
- busy={phase === 'running'}
546
- />
547
- </div>
379
+ <div className="ae-answer-composer">{composer(phase === 'running')}</div>
548
380
  </div>
549
381
  )}
550
382
  </main>