@hanzo/browser-extension 1.9.56 → 1.9.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/chrome/background.js +527 -0
- package/chrome/content-script.js +550 -0
- package/{src → chrome}/manifest.json +2 -12
- package/chrome/newtab.js +230 -0
- package/chrome/onboarding.js +31 -0
- package/chrome/page.js +35 -0
- package/{src → chrome}/popup.html +8 -0
- package/chrome/popup.js +48 -0
- package/chrome/sidebar.js +291 -0
- package/firefox/ai-worker.js +571 -0
- package/firefox/background.js +235 -0
- package/firefox/callback.html +17 -0
- package/firefox/content-script.js +550 -0
- package/firefox/icon128.png +0 -0
- package/firefox/icon16.png +0 -0
- package/firefox/icon48.png +0 -0
- package/{src/manifest-firefox.json → firefox/manifest.json} +2 -3
- package/firefox/newtab.css +543 -0
- package/firefox/newtab.html +13 -0
- package/firefox/newtab.js +230 -0
- package/firefox/onboarding.css +78 -0
- package/firefox/onboarding.html +45 -0
- package/firefox/onboarding.js +31 -0
- package/firefox/page.js +35 -0
- package/firefox/popup.css +761 -0
- package/firefox/popup.html +413 -0
- package/firefox/popup.js +48 -0
- package/firefox/sidebar.css +1350 -0
- package/firefox/sidebar.html +388 -0
- package/firefox/sidebar.js +291 -0
- package/package.json +4 -53
- package/manifest.json +0 -34
- package/src/Chat.tsx +0 -164
- package/src/answer/AnswerEngine.tsx +0 -410
- package/src/answer/Mark.tsx +0 -14
- package/src/answer/Rail.tsx +0 -95
- package/src/answer/Stories.tsx +0 -27
- package/src/answer/Toolbar.tsx +0 -93
- package/src/answer/WidgetView.tsx +0 -136
- package/src/icon128.png +0 -0
- package/src/icon16.png +0 -0
- package/src/icon32.png +0 -0
- package/src/icon48.png +0 -0
- package/src/package.json +0 -41
- package/src/safari/SafariWebExtensionHandler.swift +0 -153
- package/src/shared/Prose.tsx +0 -29
- package/src/webgpu.d.ts +0 -131
- /package/{src → chrome}/ai-worker.js +0 -0
- /package/{src → chrome}/callback.html +0 -0
- /package/{images → chrome}/icon128.png +0 -0
- /package/{images → chrome}/icon16.png +0 -0
- /package/{images → chrome}/icon48.png +0 -0
- /package/{src → chrome}/newtab.css +0 -0
- /package/{src → chrome}/newtab.html +0 -0
- /package/{src → chrome}/onboarding.css +0 -0
- /package/{src → chrome}/onboarding.html +0 -0
- /package/{src → chrome}/popup.css +0 -0
- /package/{src → chrome}/sidebar.css +0 -0
- /package/{src → chrome}/sidebar.html +0 -0
package/package.json
CHANGED
|
@@ -1,59 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hanzo/browser-extension",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.59",
|
|
4
4
|
"description": "Hanzo AI Browser Extension",
|
|
5
|
-
"main": "dist/background.js",
|
|
6
|
-
"dependencies": {
|
|
7
|
-
"@hanzo/ai": "^0.3.1",
|
|
8
|
-
"@hanzo/data": "^1.2.3",
|
|
9
|
-
"@hanzo/event": "^0.3.18",
|
|
10
|
-
"@hanzo/gui": "^8.1.1",
|
|
11
|
-
"@hanzo/logo": "^1.0.23",
|
|
12
|
-
"@hanzo/ui": "^8.1.2",
|
|
13
|
-
"@hanzo/usage": "^0.1.6",
|
|
14
|
-
"@hanzogui/config": "^8.1.1",
|
|
15
|
-
"@hanzogui/lucide-icons-2": "^8.1.1",
|
|
16
|
-
"@supabase/supabase-js": "^2.50.3",
|
|
17
|
-
"axios": "^1.10.0",
|
|
18
|
-
"chalk": "^4.1.2",
|
|
19
|
-
"commander": "^11.1.0",
|
|
20
|
-
"react-native-web": "^0.21.2",
|
|
21
|
-
"webextension-polyfill": "0.12.0",
|
|
22
|
-
"ws": "^8.18.3"
|
|
23
|
-
},
|
|
24
|
-
"devDependencies": {
|
|
25
|
-
"@playwright/test": "^1.52.0",
|
|
26
|
-
"@types/chrome": "^0.0.270",
|
|
27
|
-
"@types/firefox-webext-browser": "^120.0.0",
|
|
28
|
-
"@types/jsdom": "^21.1.7",
|
|
29
|
-
"@types/ws": "^8.18.1",
|
|
30
|
-
"esbuild": "^0.25.8",
|
|
31
|
-
"jsdom": "^26.1.0",
|
|
32
|
-
"react": "^19.2.8",
|
|
33
|
-
"react-dom": "^19.2.8",
|
|
34
|
-
"typescript": "^5.8.3",
|
|
35
|
-
"vitest": "^3.2.6",
|
|
36
|
-
"ws": "^8.18.3"
|
|
37
|
-
},
|
|
38
|
-
"engines": {
|
|
39
|
-
"node": ">=18.0.0"
|
|
40
|
-
},
|
|
41
5
|
"license": "MIT",
|
|
42
|
-
"publishConfig": {
|
|
43
|
-
"access": "public"
|
|
44
|
-
},
|
|
45
6
|
"files": [
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
"manifest.json"
|
|
50
|
-
],
|
|
51
|
-
"scripts": {
|
|
52
|
-
"build": "node src/build.js",
|
|
53
|
-
"watch": "node src/build.js --watch",
|
|
54
|
-
"test": "vitest run",
|
|
55
|
-
"test:watch": "vitest",
|
|
56
|
-
"test:e2e": "playwright test --config=e2e/playwright.config.ts",
|
|
57
|
-
"check:bundle-budget": "node scripts/check-bundle-budgets.mjs"
|
|
58
|
-
}
|
|
7
|
+
"chrome",
|
|
8
|
+
"firefox"
|
|
9
|
+
]
|
|
59
10
|
}
|
package/manifest.json
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"manifest_version": 3,
|
|
3
|
-
"name": "Hanzo AI Dev Assistant",
|
|
4
|
-
"version": "1.0.0",
|
|
5
|
-
"description": "Click-to-code navigation with source-map support",
|
|
6
|
-
"permissions": [
|
|
7
|
-
"activeTab",
|
|
8
|
-
"storage",
|
|
9
|
-
"tabs",
|
|
10
|
-
"webNavigation",
|
|
11
|
-
"debugger",
|
|
12
|
-
"scripting"
|
|
13
|
-
],
|
|
14
|
-
"host_permissions": [
|
|
15
|
-
"http://localhost/*",
|
|
16
|
-
"https://localhost/*",
|
|
17
|
-
"<all_urls>"
|
|
18
|
-
],
|
|
19
|
-
"content_scripts": [
|
|
20
|
-
{
|
|
21
|
-
"matches": ["<all_urls>"],
|
|
22
|
-
"js": ["content-script.js"],
|
|
23
|
-
"run_at": "document_idle"
|
|
24
|
-
}
|
|
25
|
-
],
|
|
26
|
-
"action": {
|
|
27
|
-
"default_popup": "popup.html",
|
|
28
|
-
"default_icon": {
|
|
29
|
-
"16": "icon16.png",
|
|
30
|
-
"48": "icon48.png",
|
|
31
|
-
"128": "icon128.png"
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
package/src/Chat.tsx
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { Button, Checkbox, SizableText, XStack, YStack } from '@hanzo/ui';
|
|
3
|
-
import { Composer, Failure, Message, Thread } from '@hanzo/ui/chat';
|
|
4
|
-
import { ModelSelector, type ModelCatalogEntry } from '@hanzo/ui/models';
|
|
5
|
-
import { Prose } from './shared/Prose.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The sidebar's chat surface — `@hanzo/ui/chat`, the same shell the new-tab
|
|
9
|
-
* answer engine and every other Hanzo surface draw with.
|
|
10
|
-
*
|
|
11
|
-
* The sidebar is the narrow client: ~360px, an MV3 extension page, and a
|
|
12
|
-
* transport that is the service worker rather than a fetch loop. None of that
|
|
13
|
-
* reaches the presentation. Every turn, the draft, the model and the busy flag
|
|
14
|
-
* arrive as props and every action leaves as a callback, so `sidebar.ts` keeps
|
|
15
|
-
* the whole wire — token brokering, retrieval, budgets, dedupe, the stream —
|
|
16
|
-
* and this file keeps none of it.
|
|
17
|
-
*
|
|
18
|
-
* Width is the one thing the shell is asked to bend on. `Thread` caps prose at a
|
|
19
|
-
* reading measure and centres it, which in a ~360px column would inset the text
|
|
20
|
-
* from both edges, so it is told to fill — and its gutters come from the wrapper
|
|
21
|
-
* here, because the shell drops its own padding along with the cap. The model
|
|
22
|
-
* picker's trigger is `width: "100%"`, so it is boxed: left free it takes the
|
|
23
|
-
* whole toolbar row and pushes the send control off the edge.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
/** How wide the model picker is allowed to be in a sidebar toolbar row. */
|
|
27
|
-
const PICKER = 132;
|
|
28
|
-
|
|
29
|
-
export interface Turn {
|
|
30
|
-
role: 'user' | 'assistant' | 'system';
|
|
31
|
-
content: string;
|
|
32
|
-
/** The turn failed. Rendered as the shell's Failure, with a retry. */
|
|
33
|
-
error?: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface ChatProps {
|
|
37
|
-
turns: Turn[];
|
|
38
|
-
draft: string;
|
|
39
|
-
onDraft: (value: string) => void;
|
|
40
|
-
onSend: () => void;
|
|
41
|
-
onStop: () => void;
|
|
42
|
-
/** Run the last question again. */
|
|
43
|
-
onRetry: () => void;
|
|
44
|
-
busy: boolean;
|
|
45
|
-
models: ModelCatalogEntry[];
|
|
46
|
-
model: string;
|
|
47
|
-
onModel: (id: string) => void;
|
|
48
|
-
/** Whether retrieval runs before the turn, and whether it sees the open tab. */
|
|
49
|
-
rag: boolean;
|
|
50
|
-
onRag: (on: boolean) => void;
|
|
51
|
-
tab: boolean;
|
|
52
|
-
onTab: (on: boolean) => void;
|
|
53
|
-
/** What retrieval last did. Shown at the end of the composer's toolbar row. */
|
|
54
|
-
status: string;
|
|
55
|
-
signedIn: boolean;
|
|
56
|
-
onSignIn: () => void;
|
|
57
|
-
onSignUp: () => void;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** A named toggle, sized for the composer's toolbar row. */
|
|
61
|
-
function Toggle({
|
|
62
|
-
label,
|
|
63
|
-
hint,
|
|
64
|
-
on,
|
|
65
|
-
onChange,
|
|
66
|
-
}: {
|
|
67
|
-
label: string;
|
|
68
|
-
hint: string;
|
|
69
|
-
on: boolean;
|
|
70
|
-
onChange: (on: boolean) => void;
|
|
71
|
-
}) {
|
|
72
|
-
return (
|
|
73
|
-
<XStack items="center" gap="$1" title={hint}>
|
|
74
|
-
<Checkbox
|
|
75
|
-
size="$2"
|
|
76
|
-
checked={on}
|
|
77
|
-
onCheckedChange={(v) => onChange(v === true)}
|
|
78
|
-
aria-label={hint}
|
|
79
|
-
/>
|
|
80
|
-
<SizableText size="$1" color="$color10">
|
|
81
|
-
{label}
|
|
82
|
-
</SizableText>
|
|
83
|
-
</XStack>
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function Chat(props: ChatProps) {
|
|
88
|
-
const { turns, busy, signedIn } = props;
|
|
89
|
-
|
|
90
|
-
if (!signedIn) {
|
|
91
|
-
return (
|
|
92
|
-
<YStack flex={1} justify="center" items="center" gap="$3" p="$5">
|
|
93
|
-
<SizableText size="$5" fontWeight="600">
|
|
94
|
-
Sign in to Hanzo AI
|
|
95
|
-
</SizableText>
|
|
96
|
-
<SizableText size="$2" color="$color10" text="center">
|
|
97
|
-
Chat with Zen AI models, use browser tools, and build with AI agents.
|
|
98
|
-
</SizableText>
|
|
99
|
-
<Button width="100%" onPress={props.onSignIn}>
|
|
100
|
-
Sign in
|
|
101
|
-
</Button>
|
|
102
|
-
<Button width="100%" variant="outline" onPress={props.onSignUp}>
|
|
103
|
-
Create account
|
|
104
|
-
</Button>
|
|
105
|
-
<SizableText size="$1" color="$color10" text="center">
|
|
106
|
-
Browser tools work without sign-in.{' '}
|
|
107
|
-
<a href="https://docs.hanzo.ai" target="_blank" rel="noreferrer">
|
|
108
|
-
Learn more
|
|
109
|
-
</a>
|
|
110
|
-
</SizableText>
|
|
111
|
-
</YStack>
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return (
|
|
116
|
-
<YStack flex={1} minH={0}>
|
|
117
|
-
<YStack flex={1} minH={0} px="$3">
|
|
118
|
-
<Thread maxWidth={0} gap={16}>
|
|
119
|
-
{turns.length === 0 ? (
|
|
120
|
-
<Message role="system">Ask anything. Powered by Hanzo Cloud.</Message>
|
|
121
|
-
) : (
|
|
122
|
-
turns.map((t, i) => (
|
|
123
|
-
<Message
|
|
124
|
-
key={i}
|
|
125
|
-
role={t.role}
|
|
126
|
-
busy={busy && i === turns.length - 1 && t.role === 'assistant' && !t.content}
|
|
127
|
-
>
|
|
128
|
-
{t.error ? (
|
|
129
|
-
<Failure onRetry={props.onRetry}>{t.error}</Failure>
|
|
130
|
-
) : t.role === 'assistant' ? (
|
|
131
|
-
<Prose text={t.content} />
|
|
132
|
-
) : (
|
|
133
|
-
t.content
|
|
134
|
-
)}
|
|
135
|
-
</Message>
|
|
136
|
-
))
|
|
137
|
-
)}
|
|
138
|
-
</Thread>
|
|
139
|
-
</YStack>
|
|
140
|
-
|
|
141
|
-
<YStack p="$2">
|
|
142
|
-
<Composer
|
|
143
|
-
value={props.draft}
|
|
144
|
-
onChange={props.onDraft}
|
|
145
|
-
onSend={props.onSend}
|
|
146
|
-
onStop={props.onStop}
|
|
147
|
-
busy={busy}
|
|
148
|
-
hint={props.status}
|
|
149
|
-
>
|
|
150
|
-
<XStack width={PICKER} shrink={0}>
|
|
151
|
-
<ModelSelector
|
|
152
|
-
models={props.models}
|
|
153
|
-
value={props.model}
|
|
154
|
-
onChange={props.onModel}
|
|
155
|
-
size="sm"
|
|
156
|
-
/>
|
|
157
|
-
</XStack>
|
|
158
|
-
<Toggle label="RAG" hint="Retrieval context" on={props.rag} onChange={props.onRag} />
|
|
159
|
-
<Toggle label="Tab" hint="Include active tab" on={props.tab} onChange={props.onTab} />
|
|
160
|
-
</Composer>
|
|
161
|
-
</YStack>
|
|
162
|
-
</YStack>
|
|
163
|
-
);
|
|
164
|
-
}
|
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import { createAiClient, type AiClient, type SearchEvent, type SearchSource } from '@hanzo/ai';
|
|
3
|
-
import { Button, SizableText, XStack, YStack } from '@hanzo/ui';
|
|
4
|
-
import { Composer, Failure, Message, Sources, Step, Thread, type Source } from '@hanzo/ui/chat';
|
|
5
|
-
import { ModelSelector, type ModelCatalogEntry } from '@hanzo/ui/models';
|
|
6
|
-
import { fetchNews, type NewsItem } from './news.js';
|
|
7
|
-
import { AUTO, fetchCatalog } from './model-catalog.js';
|
|
8
|
-
import { Prose } from '../shared/Prose.js';
|
|
9
|
-
import { capture, contributeTrainingSample } from '../shared/consent.js';
|
|
10
|
-
import { Toolbar, type Mode } from './Toolbar.js';
|
|
11
|
-
import { Rail } from './Rail.js';
|
|
12
|
-
import { Stories } from './Stories.js';
|
|
13
|
-
import { Widgets } from './WidgetView.js';
|
|
14
|
-
import { extractWidgets } from './widget.js';
|
|
15
|
-
import { DEFAULT_PROVIDER_ID, fallbackUrl, loadProviderId, providerById } from './websearch.js';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Hanzo Answer Engine — the shared AI-answer surface (query → streamed answer +
|
|
19
|
-
* inline citations + sources + live news). Backend = api.hanzo.ai ONLY, reached
|
|
20
|
-
* through @hanzo/ai: `search` for the grounded modes, `chat.completions` for
|
|
21
|
-
* Chat (each capability defined in exactly one place).
|
|
22
|
-
*
|
|
23
|
-
* It draws with `@hanzo/ui/chat` — Thread, Message, Sources, Composer — and
|
|
24
|
-
* `@hanzo/ui/models` for the picker. This file is the state and the wire; the
|
|
25
|
-
* presentation is the shell every Hanzo chat surface shares. What is left local
|
|
26
|
-
* is only what is true HERE: the modes and sources in the composer's toolbar
|
|
27
|
-
* slot, the widgets the answer can emit, and the news rail.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Chat mode — the same question, ungrounded, straight to the model. It yields
|
|
32
|
-
* `SearchEvent`s so the surface keeps ONE stream to consume and ONE way to
|
|
33
|
-
* render an answer; a mode is a value the wire takes, never a second UI.
|
|
34
|
-
*/
|
|
35
|
-
async function* converse(
|
|
36
|
-
client: AiClient,
|
|
37
|
-
question: string,
|
|
38
|
-
model: string,
|
|
39
|
-
signal: AbortSignal,
|
|
40
|
-
): AsyncGenerator<SearchEvent> {
|
|
41
|
-
const stream = await client.chat.completions.create(
|
|
42
|
-
{ model, messages: [{ role: 'user', content: question }], stream: true },
|
|
43
|
-
{ signal },
|
|
44
|
-
);
|
|
45
|
-
let answer = '';
|
|
46
|
-
for await (const chunk of stream) {
|
|
47
|
-
const delta = chunk.choices?.[0]?.delta?.content;
|
|
48
|
-
if (!delta) continue;
|
|
49
|
-
answer += delta;
|
|
50
|
-
yield { type: 'text', delta };
|
|
51
|
-
}
|
|
52
|
-
yield { type: 'done', answer, sources: [] };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** What each wire stage is called on the surface. */
|
|
56
|
-
const STAGE: Record<string, string> = {
|
|
57
|
-
planning: 'Planning research',
|
|
58
|
-
searching: 'Searching the web',
|
|
59
|
-
reading: 'Reading sources',
|
|
60
|
-
writing: 'Writing the answer',
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/** How wide the model picker is allowed to be in the composer's toolbar row. */
|
|
64
|
-
const PICKER = 176;
|
|
65
|
-
|
|
66
|
-
/** A cited source in the shell's vocabulary. The favicon is the mark; the host
|
|
67
|
-
* initial stands in when a site serves none, so a citation always renders. */
|
|
68
|
-
function cite(s: SearchSource): Source {
|
|
69
|
-
return {
|
|
70
|
-
id: s.url,
|
|
71
|
-
title: s.title || s.url,
|
|
72
|
-
href: s.url,
|
|
73
|
-
...(s.favicon ? { icon: <img src={s.favicon} alt="" width={16} height={16} /> } : null),
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export interface AnswerEngineProps {
|
|
78
|
-
apiBase: string;
|
|
79
|
-
/** Resolves the current IAM bearer, or null when signed out. */
|
|
80
|
-
getToken: () => Promise<string | null>;
|
|
81
|
-
/** Kicks off IAM sign-in (opens the OAuth tab). */
|
|
82
|
-
signIn: () => void;
|
|
83
|
-
/** Optional initial query (from the omnibox / address bar `?q=`). */
|
|
84
|
-
initialQuery?: string;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function AnswerEngine({ apiBase, getToken, signIn, initialQuery }: AnswerEngineProps) {
|
|
88
|
-
// Rail hidden by default — the answer surface should read like hanzo.chat:
|
|
89
|
-
// a single centered column. Its own header control brings it back.
|
|
90
|
-
const [collapsed, setCollapsed] = useState(true);
|
|
91
|
-
const [query, setQuery] = useState('');
|
|
92
|
-
const [submitted, setSubmitted] = useState('');
|
|
93
|
-
const [mode, setMode] = useState<Mode>('search');
|
|
94
|
-
const [sources, setSources] = useState<Set<string>>(new Set());
|
|
95
|
-
const [models, setModels] = useState<ModelCatalogEntry[]>([]);
|
|
96
|
-
const [model, setModel] = useState<string>(AUTO);
|
|
97
|
-
const [news, setNews] = useState<NewsItem[]>([]);
|
|
98
|
-
// True once the user picks a model themselves, so the catalog's default-to-Enso
|
|
99
|
-
// never overwrites a deliberate choice made while the fetch was still in flight.
|
|
100
|
-
const picked = useRef(false);
|
|
101
|
-
const chooseModel = useCallback((id: string) => {
|
|
102
|
-
picked.current = true;
|
|
103
|
-
setModel(id);
|
|
104
|
-
}, []);
|
|
105
|
-
|
|
106
|
-
const [phase, setPhase] = useState<'idle' | 'running' | 'done'>('idle');
|
|
107
|
-
/** What the answer is doing right now, as the shell's Step reads it. */
|
|
108
|
-
const [step, setStep] = useState<{ name: string; detail?: string }>({ name: '' });
|
|
109
|
-
const [answer, setAnswer] = useState('');
|
|
110
|
-
const [answerSources, setAnswerSources] = useState<SearchSource[]>([]);
|
|
111
|
-
const [followUps, setFollowUps] = useState<string[]>([]);
|
|
112
|
-
const [error, setError] = useState('');
|
|
113
|
-
const [needAuth, setNeedAuth] = useState(false);
|
|
114
|
-
// The engine an unauthenticated query falls back to. Read once; the default
|
|
115
|
-
// stands until storage answers, so a query typed in the first moments after a
|
|
116
|
-
// new tab opens still has somewhere to go.
|
|
117
|
-
const [providerId, setProviderId] = useState<string>(DEFAULT_PROVIDER_ID);
|
|
118
|
-
const abortRef = useRef<AbortController | null>(null);
|
|
119
|
-
|
|
120
|
-
// Load real model catalog + live news (both public — work signed out).
|
|
121
|
-
//
|
|
122
|
-
// Enso is the default, selected once the catalog lands. It cannot be the
|
|
123
|
-
// useState seed: the catalog is fetched at runtime, so naming a model before
|
|
124
|
-
// it arrives would hardcode an id the server may not serve — the whole point
|
|
125
|
-
// of model-catalog.ts is that the list is never fabricated. Auto is the seed
|
|
126
|
-
// and the fallback, so a failed/slow fetch degrades to "routes to the best
|
|
127
|
-
// model" instead of a dead selection. Only the initial pick is overridden:
|
|
128
|
-
// `picked` guards a user's choice against a late catalog resolve.
|
|
129
|
-
useEffect(() => {
|
|
130
|
-
fetchCatalog()
|
|
131
|
-
.then((entries) => {
|
|
132
|
-
setModels(entries);
|
|
133
|
-
if (picked.current) return;
|
|
134
|
-
const enso = entries.find((m) => /enso/i.test(m.family ?? ''));
|
|
135
|
-
if (enso) setModel(enso.id);
|
|
136
|
-
})
|
|
137
|
-
.catch(() => {});
|
|
138
|
-
fetchNews().then(setNews).catch(() => {});
|
|
139
|
-
loadProviderId().then(setProviderId).catch(() => {});
|
|
140
|
-
}, [apiBase]);
|
|
141
|
-
|
|
142
|
-
const client = useMemo(
|
|
143
|
-
() =>
|
|
144
|
-
createAiClient({
|
|
145
|
-
baseUrl: apiBase,
|
|
146
|
-
getToken: async () => {
|
|
147
|
-
const t = await getToken();
|
|
148
|
-
if (!t) throw new Error('__NO_TOKEN__');
|
|
149
|
-
return t;
|
|
150
|
-
},
|
|
151
|
-
}),
|
|
152
|
-
[apiBase, getToken],
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
const run = useCallback(
|
|
156
|
-
async (q: string) => {
|
|
157
|
-
const question = q.trim();
|
|
158
|
-
if (!question) return;
|
|
159
|
-
abortRef.current?.abort();
|
|
160
|
-
const ctrl = new AbortController();
|
|
161
|
-
abortRef.current = ctrl;
|
|
162
|
-
|
|
163
|
-
// NO IDENTITY, NO DEAD END. /v1/ask is metered and answers 401 to a
|
|
164
|
-
// signed-out caller, but this surface IS the browser's search box: a query
|
|
165
|
-
// that returns a sign-in card has broken the address bar rather than shown a
|
|
166
|
-
// paywall. Hand it to the configured engine instead, before any spinner —
|
|
167
|
-
// checking first is what makes the handoff instant rather than a flash of
|
|
168
|
-
// "Searching the web" followed by a navigation.
|
|
169
|
-
//
|
|
170
|
-
// The fallback is not a Hanzo call: no token, no proxy, no telemetry. The
|
|
171
|
-
// query goes from this browser straight to the engine the user chose.
|
|
172
|
-
let identified = false;
|
|
173
|
-
try {
|
|
174
|
-
identified = Boolean(await getToken());
|
|
175
|
-
} catch {
|
|
176
|
-
identified = false;
|
|
177
|
-
}
|
|
178
|
-
if (!identified) {
|
|
179
|
-
const url = fallbackUrl(providerId, question);
|
|
180
|
-
if (url) {
|
|
181
|
-
void capture('answer_search_fallback', { provider: providerId, mode });
|
|
182
|
-
window.location.assign(url);
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
setSubmitted(question);
|
|
188
|
-
setPhase('running');
|
|
189
|
-
setStep({ name: mode === 'chat' ? 'Thinking' : 'Searching the web' });
|
|
190
|
-
setAnswer('');
|
|
191
|
-
setAnswerSources([]);
|
|
192
|
-
setFollowUps([]);
|
|
193
|
-
setError('');
|
|
194
|
-
setNeedAuth(false);
|
|
195
|
-
void capture('answer_search', { mode, model });
|
|
196
|
-
let fullAnswer = '';
|
|
197
|
-
|
|
198
|
-
const stream =
|
|
199
|
-
mode === 'chat'
|
|
200
|
-
? converse(client, question, model, ctrl.signal)
|
|
201
|
-
: (client.search(question, {
|
|
202
|
-
mode,
|
|
203
|
-
model,
|
|
204
|
-
sources: [...sources],
|
|
205
|
-
signal: ctrl.signal,
|
|
206
|
-
}) as AsyncGenerator<SearchEvent>);
|
|
207
|
-
try {
|
|
208
|
-
for await (const ev of stream) {
|
|
209
|
-
switch (ev.type) {
|
|
210
|
-
case 'status':
|
|
211
|
-
setStep({
|
|
212
|
-
name: STAGE[ev.stage] ?? 'Writing the answer',
|
|
213
|
-
...(ev.detail ? { detail: ev.detail } : null),
|
|
214
|
-
});
|
|
215
|
-
break;
|
|
216
|
-
case 'sources':
|
|
217
|
-
setAnswerSources(ev.sources);
|
|
218
|
-
break;
|
|
219
|
-
case 'text':
|
|
220
|
-
fullAnswer += ev.delta;
|
|
221
|
-
setAnswer((a) => a + ev.delta);
|
|
222
|
-
break;
|
|
223
|
-
case 'follow_ups':
|
|
224
|
-
setFollowUps(ev.questions);
|
|
225
|
-
break;
|
|
226
|
-
case 'done':
|
|
227
|
-
setPhase('done');
|
|
228
|
-
void capture('answer_shown', { mode, model });
|
|
229
|
-
// Opt-in only: no-ops unless the user turned on training
|
|
230
|
-
// contribution (the consent module gates the send).
|
|
231
|
-
void contributeTrainingSample({ query: question, answer: fullAnswer, model });
|
|
232
|
-
break;
|
|
233
|
-
case 'error':
|
|
234
|
-
if (ev.error.includes('__NO_TOKEN__') || /401|invalid api key/i.test(ev.error)) {
|
|
235
|
-
setNeedAuth(true);
|
|
236
|
-
} else {
|
|
237
|
-
setError(ev.error);
|
|
238
|
-
}
|
|
239
|
-
setPhase('done');
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
} catch (e) {
|
|
244
|
-
const msg = e instanceof Error ? e.message : String(e);
|
|
245
|
-
if (msg.includes('__NO_TOKEN__') || /401|invalid api key/i.test(msg)) setNeedAuth(true);
|
|
246
|
-
else setError(msg);
|
|
247
|
-
setPhase('done');
|
|
248
|
-
}
|
|
249
|
-
},
|
|
250
|
-
[client, mode, model, sources, getToken, providerId],
|
|
251
|
-
);
|
|
252
|
-
|
|
253
|
-
// Fire the initial (address-bar) query once models are ready enough.
|
|
254
|
-
const didInit = useRef(false);
|
|
255
|
-
useEffect(() => {
|
|
256
|
-
if (didInit.current || !initialQuery) return;
|
|
257
|
-
didInit.current = true;
|
|
258
|
-
setQuery(initialQuery);
|
|
259
|
-
void run(initialQuery);
|
|
260
|
-
}, [initialQuery, run]);
|
|
261
|
-
|
|
262
|
-
const submit = useCallback(() => void run(query), [query, run]);
|
|
263
|
-
const stop = useCallback(() => {
|
|
264
|
-
abortRef.current?.abort();
|
|
265
|
-
setPhase('done');
|
|
266
|
-
}, []);
|
|
267
|
-
const toggleSource = useCallback((s: string) => {
|
|
268
|
-
setSources((prev) => {
|
|
269
|
-
const next = new Set(prev);
|
|
270
|
-
next.has(s) ? next.delete(s) : next.add(s);
|
|
271
|
-
return next;
|
|
272
|
-
});
|
|
273
|
-
}, []);
|
|
274
|
-
|
|
275
|
-
const newThread = useCallback(() => {
|
|
276
|
-
abortRef.current?.abort();
|
|
277
|
-
setPhase('idle');
|
|
278
|
-
setQuery('');
|
|
279
|
-
setSubmitted('');
|
|
280
|
-
setAnswer('');
|
|
281
|
-
setAnswerSources([]);
|
|
282
|
-
setFollowUps([]);
|
|
283
|
-
setError('');
|
|
284
|
-
setNeedAuth(false);
|
|
285
|
-
}, []);
|
|
286
|
-
|
|
287
|
-
const busy = phase === 'running';
|
|
288
|
-
const composer = (
|
|
289
|
-
<Composer value={query} onChange={setQuery} onSend={submit} onStop={stop} busy={busy}>
|
|
290
|
-
<Toolbar mode={mode} setMode={setMode} sources={sources} toggleSource={toggleSource} />
|
|
291
|
-
{/* Boxed: the picker's trigger is `width: "100%"`, so left free in the
|
|
292
|
-
toolbar row it takes everything and pushes the send control out. */}
|
|
293
|
-
<XStack width={PICKER} shrink={0}>
|
|
294
|
-
<ModelSelector models={models} value={model} onChange={chooseModel} size="sm" />
|
|
295
|
-
</XStack>
|
|
296
|
-
</Composer>
|
|
297
|
-
);
|
|
298
|
-
|
|
299
|
-
const { prose, widgets } = extractWidgets(answer);
|
|
300
|
-
|
|
301
|
-
return (
|
|
302
|
-
<div className={'ae-root' + (collapsed ? ' collapsed' : '')}>
|
|
303
|
-
<Rail
|
|
304
|
-
collapsed={collapsed}
|
|
305
|
-
toggle={() => setCollapsed((v) => !v)}
|
|
306
|
-
newThread={newThread}
|
|
307
|
-
signIn={signIn}
|
|
308
|
-
/>
|
|
309
|
-
|
|
310
|
-
<main className="ae-main">
|
|
311
|
-
{phase === 'idle' ? (
|
|
312
|
-
<YStack flex={1} justify="center" items="center" gap="$6" px="$4" py="$6">
|
|
313
|
-
<YStack width="100%" maxW={768}>
|
|
314
|
-
{composer}
|
|
315
|
-
</YStack>
|
|
316
|
-
<Stories items={news} />
|
|
317
|
-
</YStack>
|
|
318
|
-
) : (
|
|
319
|
-
<>
|
|
320
|
-
<Thread>
|
|
321
|
-
<Message role="user">{submitted}</Message>
|
|
322
|
-
|
|
323
|
-
{answerSources.length > 0 && <Sources sources={answerSources.map(cite)} />}
|
|
324
|
-
|
|
325
|
-
{needAuth ? (
|
|
326
|
-
/* Reached only when a token EXISTED and the call still came back
|
|
327
|
-
401 — an expired or revoked session, since a signed-out query
|
|
328
|
-
never gets here (run() hands it to the fallback engine first).
|
|
329
|
-
Both doors are offered: renew the session for an AI answer, or
|
|
330
|
-
take the same query to the configured engine. A dead end here
|
|
331
|
-
would strand someone who simply needs to sign in again. */
|
|
332
|
-
<YStack gap="$3" items="flex-start">
|
|
333
|
-
<SizableText size="$3">
|
|
334
|
-
Your Hanzo session expired. Sign in again for AI answers.
|
|
335
|
-
</SizableText>
|
|
336
|
-
<XStack gap="$3" items="center">
|
|
337
|
-
<Button onPress={signIn}>Sign in</Button>
|
|
338
|
-
<a
|
|
339
|
-
href={fallbackUrl(providerId, submitted) || undefined}
|
|
340
|
-
style={{ color: 'inherit', opacity: 0.7, fontSize: 13 }}
|
|
341
|
-
>
|
|
342
|
-
Search {providerById(providerId).label} instead
|
|
343
|
-
</a>
|
|
344
|
-
</XStack>
|
|
345
|
-
</YStack>
|
|
346
|
-
) : error ? (
|
|
347
|
-
<Message role="assistant">
|
|
348
|
-
<Failure onRetry={() => void run(submitted)}>{error}</Failure>
|
|
349
|
-
</Message>
|
|
350
|
-
) : (
|
|
351
|
-
/* The answer picks its own presentation: any widget blocks it
|
|
352
|
-
emitted render as real components, and the prose is what is
|
|
353
|
-
left after they are removed — so a widget is never also shown
|
|
354
|
-
as raw JSON. Extraction runs per frame against a STREAMING,
|
|
355
|
-
half-written document, which is why an unterminated fence is
|
|
356
|
-
left alone until it closes. */
|
|
357
|
-
<Message role="assistant" busy={busy && !answer}>
|
|
358
|
-
{busy && !answer ? (
|
|
359
|
-
<Step name={step.name} status="running" {...(step.detail ? { detail: step.detail } : null)} />
|
|
360
|
-
) : null}
|
|
361
|
-
<Widgets widgets={widgets} />
|
|
362
|
-
<Prose text={prose} />
|
|
363
|
-
</Message>
|
|
364
|
-
)}
|
|
365
|
-
|
|
366
|
-
{followUps.length > 0 && (
|
|
367
|
-
<YStack gap="$1.5">
|
|
368
|
-
<SizableText size="$1" color="$color10" fontWeight="500">
|
|
369
|
-
Follow-up
|
|
370
|
-
</SizableText>
|
|
371
|
-
{followUps.map((f) => (
|
|
372
|
-
<XStack
|
|
373
|
-
key={f}
|
|
374
|
-
items="center"
|
|
375
|
-
gap="$2"
|
|
376
|
-
px="$3"
|
|
377
|
-
py="$2"
|
|
378
|
-
rounded="$3"
|
|
379
|
-
borderWidth={1}
|
|
380
|
-
borderColor="$borderColor"
|
|
381
|
-
cursor="pointer"
|
|
382
|
-
role="button"
|
|
383
|
-
tabIndex={0}
|
|
384
|
-
hoverStyle={{ backgroundColor: '$color3' }}
|
|
385
|
-
onPress={() => {
|
|
386
|
-
setQuery(f);
|
|
387
|
-
void run(f);
|
|
388
|
-
}}
|
|
389
|
-
>
|
|
390
|
-
<SizableText size="$2" flex={1}>
|
|
391
|
-
{f}
|
|
392
|
-
</SizableText>
|
|
393
|
-
<SizableText size="$2" color="$color10">
|
|
394
|
-
→
|
|
395
|
-
</SizableText>
|
|
396
|
-
</XStack>
|
|
397
|
-
))}
|
|
398
|
-
</YStack>
|
|
399
|
-
)}
|
|
400
|
-
</Thread>
|
|
401
|
-
|
|
402
|
-
<YStack width="100%" maxW={768} self="center" px="$3" pb="$4">
|
|
403
|
-
{composer}
|
|
404
|
-
</YStack>
|
|
405
|
-
</>
|
|
406
|
-
)}
|
|
407
|
-
</main>
|
|
408
|
-
</div>
|
|
409
|
-
);
|
|
410
|
-
}
|
package/src/answer/Mark.tsx
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
|
|
3
|
-
import { MARK_BLOCKS } from '@hanzo/logo/logos';
|
|
4
|
-
|
|
5
|
-
/** The Hanzo mark. Monochrome — it inherits colour from its parent. */
|
|
6
|
-
export function Mark({ size = 28 }: { size?: number }) {
|
|
7
|
-
return (
|
|
8
|
-
<svg viewBox="0 0 67 67" width={size} height={size} xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
|
|
9
|
-
{MARK_BLOCKS.map((d) => (
|
|
10
|
-
<path key={d} d={d} fill="currentColor" />
|
|
11
|
-
))}
|
|
12
|
-
</svg>
|
|
13
|
-
);
|
|
14
|
-
}
|