@blackbelt-technology/pi-dashboard-web-access-plugin 0.0.0-stage → 0.10.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.
- package/README.md +32 -2
- package/package.json +65 -4
- package/src/client/WebSearchEntryCard.tsx +325 -0
- package/src/client/body-limits.ts +9 -0
- package/src/client/index.tsx +10 -0
- package/src/client/parse-web-search.ts +209 -0
- package/src/i18n.ts +65 -0
package/README.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @blackbelt-technology/pi-dashboard-web-access-plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Dashboard rendering for the [`pi-web-access`](https://www.npmjs.com/package/pi-web-access)
|
|
4
|
+
extension. It claims the `custom-entry-renderer` slot for `web-search-results` entries, so
|
|
5
|
+
web searches and fetches show up in chat as compact cards instead of a raw JSON dump.
|
|
6
|
+
|
|
7
|
+
## What it shows
|
|
8
|
+
|
|
9
|
+
- **Collapsed** (default): one line, derived from the stored row body — for example
|
|
10
|
+
`Web search · 2 queries · 8 results` or `Web fetch · 4 URLs`. A count is shown only when it
|
|
11
|
+
is provably complete; research entries and unknown subtypes show the label only.
|
|
12
|
+
- **Expanded** (only for rows that carry an entry id): built from the full payload, fetched
|
|
13
|
+
on first expand.
|
|
14
|
+
- Search: up to 20 queries, each with provider(s), answer, error and up to 20 results
|
|
15
|
+
(title, link, snippet). Overflow shows "+N more".
|
|
16
|
+
- Fetch: up to 50 URLs with title, size and error (both the `urlMetadata[]` and the older
|
|
17
|
+
`urls[]` payload shapes).
|
|
18
|
+
- Research / unknown subtypes: the payload as bounded plain text.
|
|
19
|
+
- Payload unavailable: the stored body as plain text plus the failure reason.
|
|
20
|
+
|
|
21
|
+
## Safety
|
|
22
|
+
|
|
23
|
+
All text is rendered as plain text — no markdown, no HTML. Only `http(s)` URLs without
|
|
24
|
+
embedded credentials become links, opened with `rel="noopener noreferrer"`.
|
|
25
|
+
|
|
26
|
+
## Behaviour notes
|
|
27
|
+
|
|
28
|
+
- Client-only: no server part, no configuration.
|
|
29
|
+
- `requires.piExtensions: ["pi-web-access"]` is advisory (reported in the plugins UI). The
|
|
30
|
+
claim stays active, so rows in old sessions still render.
|
|
31
|
+
- Rows are hidden or shown by the shipped `search` custom event group (visible by default).
|
|
32
|
+
- When this plugin is disabled, `web-search-results` rows fall back to the generic card,
|
|
33
|
+
which shows JSON-object bodies as a key/value list.
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blackbelt-technology/pi-dashboard-web-access-plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "Dashboard rendering for the pi-web-access extension — compact, safe chat cards for web-search-results custom entries (search, fetch).",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/BlackBeltTechnology/pi-agent-dashboard",
|
|
13
|
+
"directory": "packages/web-access-plugin"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./src/client/index.tsx",
|
|
18
|
+
"default": "./src/client/index.tsx"
|
|
19
|
+
},
|
|
20
|
+
"./client": {
|
|
21
|
+
"types": "./src/client/index.tsx",
|
|
22
|
+
"default": "./src/client/index.tsx"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"src/",
|
|
27
|
+
"!**/AGENTS.md",
|
|
28
|
+
"!**/*.AGENTS.md",
|
|
29
|
+
"!**/__tests__",
|
|
30
|
+
"!**/*.test.ts",
|
|
31
|
+
"!**/*.test.tsx"
|
|
32
|
+
],
|
|
33
|
+
"pi-dashboard-plugin": {
|
|
34
|
+
"id": "web-access",
|
|
35
|
+
"displayName": "Web access",
|
|
36
|
+
"i18nCatalog": "catalog",
|
|
37
|
+
"client": "./src/client/index.tsx",
|
|
38
|
+
"claims": [
|
|
39
|
+
{
|
|
40
|
+
"slot": "custom-entry-renderer",
|
|
41
|
+
"customType": "web-search-results",
|
|
42
|
+
"component": "WebSearchEntryCard"
|
|
43
|
+
}
|
|
44
|
+
],
|
|
45
|
+
"requires": {
|
|
46
|
+
"piExtensions": [
|
|
47
|
+
"pi-web-access"
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"react": ">=18.0.0",
|
|
53
|
+
"react-dom": ">=18.0.0"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@blackbelt-technology/dashboard-plugin-runtime": "^0.10.0",
|
|
57
|
+
"@blackbelt-technology/pi-dashboard-shared": "^0.10.0",
|
|
58
|
+
"@mdi/js": "^7.4.47",
|
|
59
|
+
"@mdi/react": "^1.6.1"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@types/react": "^19.0.0",
|
|
63
|
+
"@types/react-dom": "^19.0.0",
|
|
64
|
+
"react": "^19.0.0",
|
|
65
|
+
"react-dom": "^19.0.0"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSearchEntryCard — the web-access `custom-entry-renderer` contribution for
|
|
3
|
+
* `web-search-results` entries emitted by `pi-web-access`.
|
|
4
|
+
*
|
|
5
|
+
* Contract (spec: web-search-entry-rendering; design D3):
|
|
6
|
+
* - COLLAPSED-FIRST: the collapsed line derives from the row body ALONE; the
|
|
7
|
+
* container owns the payload fetch and expansion state (presentation only).
|
|
8
|
+
* - Counts are never partial (see `summarizeBody`).
|
|
9
|
+
* - EXPANDED content comes from the fetched payload only — the stored body's
|
|
10
|
+
* nested data was replaced by ingest placeholders, so a typed view of it
|
|
11
|
+
* would be silently partial.
|
|
12
|
+
* - Everything is PLAIN TEXT except `http(s)` URLs without credentials, which
|
|
13
|
+
* become `rel="noopener noreferrer"` links (original text, normalized
|
|
14
|
+
* target). No markdown, no `dangerouslySetInnerHTML`.
|
|
15
|
+
* - Icons are decorative (`aria-hidden`) and additive to a text label.
|
|
16
|
+
* - Painted through theme tokens only (no palette classes): errors are
|
|
17
|
+
* severity pills, an unavailable payload is a warning notice, links use
|
|
18
|
+
* `--link` + underline + external-link icon.
|
|
19
|
+
*
|
|
20
|
+
* See changes: render-unclaimed-custom-entries, retro-ux-chat-rendering.
|
|
21
|
+
*/
|
|
22
|
+
import { useT } from "@blackbelt-technology/dashboard-plugin-runtime";
|
|
23
|
+
import type { CustomEntryRendererProps } from "@blackbelt-technology/pi-dashboard-shared/dashboard-plugin/slot-props.js";
|
|
24
|
+
import {
|
|
25
|
+
mdiAlertCircleOutline,
|
|
26
|
+
mdiChevronDown,
|
|
27
|
+
mdiChevronRight,
|
|
28
|
+
mdiFileDownloadOutline,
|
|
29
|
+
mdiInformationOutline,
|
|
30
|
+
mdiLoading,
|
|
31
|
+
mdiOpenInNew,
|
|
32
|
+
mdiWeb,
|
|
33
|
+
} from "@mdi/js";
|
|
34
|
+
import { Icon } from "@mdi/react";
|
|
35
|
+
import type React from "react";
|
|
36
|
+
import { useMemo } from "react";
|
|
37
|
+
import { CUSTOM_BODY_MAX_CHARS } from "./body-limits.js";
|
|
38
|
+
import {
|
|
39
|
+
type FetchView,
|
|
40
|
+
parseWebSearchPayload,
|
|
41
|
+
type SearchView,
|
|
42
|
+
safeHttpUrl,
|
|
43
|
+
summarizeBody,
|
|
44
|
+
} from "./parse-web-search.js";
|
|
45
|
+
|
|
46
|
+
type T = ReturnType<typeof useT>;
|
|
47
|
+
|
|
48
|
+
const PRE_CLASS =
|
|
49
|
+
"m-0 px-2 pt-1 pb-1 rounded bg-[var(--bg-secondary)] overflow-y-auto overflow-x-auto max-h-[240px] text-[var(--text-secondary)] font-mono text-[11px] leading-relaxed whitespace-pre-wrap break-all";
|
|
50
|
+
const MUTED = "text-[var(--text-secondary)]";
|
|
51
|
+
const ERROR_PILL =
|
|
52
|
+
"inline-flex max-w-full items-start gap-1 rounded-full border border-[var(--severity-error-border)] bg-[var(--severity-error-bg)] px-1.5 py-0.5 text-[11px] text-[var(--severity-error-fg)] break-words";
|
|
53
|
+
const MAX_PLAIN_LINES = 200;
|
|
54
|
+
|
|
55
|
+
/** Last-200-lines / 64 000-char display form (matches the reducer's body bound). */
|
|
56
|
+
function boundPlainText(text: string): string {
|
|
57
|
+
const lines = text.split("\n");
|
|
58
|
+
const tail = lines.length > MAX_PLAIN_LINES ? lines.slice(-MAX_PLAIN_LINES).join("\n") : text;
|
|
59
|
+
return tail.length > CUSTOM_BODY_MAX_CHARS ? tail.slice(-CUSTOM_BODY_MAX_CHARS) : tail;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function payloadAsText(payload: unknown): string {
|
|
63
|
+
try {
|
|
64
|
+
return boundPlainText(typeof payload === "string" ? payload : JSON.stringify(payload, null, 2));
|
|
65
|
+
} catch {
|
|
66
|
+
return "";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Clickable only when `safeHttpUrl` accepts it; otherwise plain text. */
|
|
71
|
+
function UrlText({ raw, t }: { raw: string; t: T }) {
|
|
72
|
+
const href = safeHttpUrl(raw);
|
|
73
|
+
return href ? (
|
|
74
|
+
<a
|
|
75
|
+
href={href}
|
|
76
|
+
target="_blank"
|
|
77
|
+
rel="noopener noreferrer"
|
|
78
|
+
className="focus-ring text-[12px] text-[var(--link)] underline hover:text-[var(--link-hover)] break-all"
|
|
79
|
+
>
|
|
80
|
+
<span>{raw}</span>
|
|
81
|
+
<Icon path={mdiOpenInNew} size={0.5} aria-hidden="true" className="ml-0.5 inline-block align-baseline" />
|
|
82
|
+
<span className="sr-only">{t("opensInNewTab", undefined, " (opens in a new tab)")}</span>
|
|
83
|
+
</a>
|
|
84
|
+
) : (
|
|
85
|
+
<span className="break-all text-[12px]">{raw}</span>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Error-severity pill with a decorative icon; the text carries the meaning. */
|
|
90
|
+
function ErrorPill({ children }: { children: React.ReactNode }) {
|
|
91
|
+
return (
|
|
92
|
+
<span className={ERROR_PILL}>
|
|
93
|
+
<Icon path={mdiAlertCircleOutline} size={0.5} aria-hidden="true" className="mt-px shrink-0" />
|
|
94
|
+
<span>{children}</span>
|
|
95
|
+
</span>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const plural = (t: T, base: string, count: number, one: string, other: string) =>
|
|
100
|
+
count === 1
|
|
101
|
+
? t(`${base}One`, { count }, one.replace("{count}", String(count)))
|
|
102
|
+
: t(`${base}Other`, { count }, other.replace("{count}", String(count)));
|
|
103
|
+
|
|
104
|
+
function Skipped({ t, count }: { t: T; count: number }) {
|
|
105
|
+
return count > 0 ? (
|
|
106
|
+
<div data-testid="web-search-skipped" className={`px-2 py-0.5 text-[11px] ${MUTED}`}>
|
|
107
|
+
{plural(t, "skippedItems", count, "{count} item could not be displayed", "{count} items could not be displayed")}
|
|
108
|
+
</div>
|
|
109
|
+
) : null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function More({ t, count }: { t: T; count: number }) {
|
|
113
|
+
return count > 0 ? (
|
|
114
|
+
<div className={`px-2 py-0.5 text-[11px] ${MUTED}`}>{t("moreItems", { count }, `+${count} more`)}</div>
|
|
115
|
+
) : null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function SearchBody({ view, t }: { view: SearchView; t: T }) {
|
|
119
|
+
return (
|
|
120
|
+
<div className="space-y-2" data-testid="web-search-detail">
|
|
121
|
+
{view.queries.map((q, i) => (
|
|
122
|
+
<div key={i} className="rounded bg-[var(--bg-secondary)] px-2 py-1" data-testid="web-search-query">
|
|
123
|
+
<div className="font-semibold text-[var(--text-primary)] break-words">{q.query}</div>
|
|
124
|
+
{q.providers ? (
|
|
125
|
+
<div className={`text-[11px] ${MUTED}`}>{t("providerLabel", { provider: q.providers }, `via ${q.providers}`)}</div>
|
|
126
|
+
) : null}
|
|
127
|
+
{q.error ? (
|
|
128
|
+
<div className="mt-0.5">
|
|
129
|
+
<ErrorPill>{q.error}</ErrorPill>
|
|
130
|
+
</div>
|
|
131
|
+
) : null}
|
|
132
|
+
{q.answer ? (
|
|
133
|
+
<div
|
|
134
|
+
data-testid="web-search-answer"
|
|
135
|
+
className="mt-1 text-[var(--text-primary)] whitespace-pre-wrap break-words line-clamp-6"
|
|
136
|
+
>
|
|
137
|
+
{q.answer}
|
|
138
|
+
</div>
|
|
139
|
+
) : null}
|
|
140
|
+
{q.results.length > 0 ? (
|
|
141
|
+
<ul className="m-0 mt-1 list-none space-y-1 p-0">
|
|
142
|
+
{q.results.map((r, j) => (
|
|
143
|
+
<li key={j} data-testid="web-search-result" className="min-w-0">
|
|
144
|
+
<div className="text-[var(--text-primary)] break-words">{r.title}</div>
|
|
145
|
+
<div>
|
|
146
|
+
<UrlText raw={r.url} t={t} />
|
|
147
|
+
</div>
|
|
148
|
+
{r.snippet ? (
|
|
149
|
+
<div className={`text-[11px] ${MUTED} whitespace-pre-wrap break-words line-clamp-3`}>{r.snippet}</div>
|
|
150
|
+
) : null}
|
|
151
|
+
</li>
|
|
152
|
+
))}
|
|
153
|
+
</ul>
|
|
154
|
+
) : (
|
|
155
|
+
!q.error && <div className={`text-[11px] ${MUTED}`}>{t("noResults", undefined, "No results")}</div>
|
|
156
|
+
)}
|
|
157
|
+
<More t={t} count={q.omittedResults} />
|
|
158
|
+
</div>
|
|
159
|
+
))}
|
|
160
|
+
<More t={t} count={view.omittedQueries} />
|
|
161
|
+
<Skipped t={t} count={view.skipped} />
|
|
162
|
+
</div>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function FetchBody({ view, t }: { view: FetchView; t: T }) {
|
|
167
|
+
return (
|
|
168
|
+
<div className="space-y-1" data-testid="web-search-detail">
|
|
169
|
+
<ul className="m-0 list-none space-y-1 p-0">
|
|
170
|
+
{view.items.map((it, i) => (
|
|
171
|
+
<li key={i} data-testid="web-search-fetch-item" className="rounded bg-[var(--bg-secondary)] px-2 py-1 min-w-0">
|
|
172
|
+
<div className="text-[var(--text-primary)] break-words">{it.title}</div>
|
|
173
|
+
<div>
|
|
174
|
+
<UrlText raw={it.url} t={t} />
|
|
175
|
+
</div>
|
|
176
|
+
{it.size !== null ? (
|
|
177
|
+
<div className={`text-[11px] ${MUTED}`}>{t("sizeChars", { count: it.size }, `${it.size} chars`)}</div>
|
|
178
|
+
) : null}
|
|
179
|
+
{it.error ? (
|
|
180
|
+
<div className="mt-0.5">
|
|
181
|
+
<ErrorPill>{it.error}</ErrorPill>
|
|
182
|
+
</div>
|
|
183
|
+
) : null}
|
|
184
|
+
</li>
|
|
185
|
+
))}
|
|
186
|
+
</ul>
|
|
187
|
+
<More t={t} count={view.omitted} />
|
|
188
|
+
<Skipped t={t} count={view.skipped} />
|
|
189
|
+
</div>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function WebSearchEntryCard({
|
|
194
|
+
body,
|
|
195
|
+
timestamp,
|
|
196
|
+
entryId,
|
|
197
|
+
expanded,
|
|
198
|
+
onToggle,
|
|
199
|
+
payload,
|
|
200
|
+
payloadError,
|
|
201
|
+
payloadLoading,
|
|
202
|
+
}: CustomEntryRendererProps): React.ReactElement {
|
|
203
|
+
const t = useT();
|
|
204
|
+
const summary = useMemo(() => summarizeBody(body), [body]);
|
|
205
|
+
const view = useMemo(() => parseWebSearchPayload(payload), [payload]);
|
|
206
|
+
|
|
207
|
+
const label =
|
|
208
|
+
summary.type === "search"
|
|
209
|
+
? t("labelSearch", undefined, "Web search")
|
|
210
|
+
: summary.type === "fetch"
|
|
211
|
+
? t("labelFetch", undefined, "Web fetch")
|
|
212
|
+
: (payload as { type?: unknown } | undefined)?.type === "research"
|
|
213
|
+
? t("labelResearch", undefined, "Web research")
|
|
214
|
+
: t("labelGeneric", undefined, "Web search results");
|
|
215
|
+
const icon = summary.type === "fetch" ? mdiFileDownloadOutline : mdiWeb;
|
|
216
|
+
|
|
217
|
+
const parts: string[] = [];
|
|
218
|
+
if (summary.queries !== undefined) parts.push(plural(t, "queries", summary.queries, "{count} query", "{count} queries"));
|
|
219
|
+
if (summary.results !== undefined) parts.push(plural(t, "results", summary.results, "{count} result", "{count} results"));
|
|
220
|
+
if (summary.urls !== undefined) parts.push(plural(t, "urls", summary.urls, "{count} URL", "{count} URLs"));
|
|
221
|
+
if (summary.failed !== undefined) parts.push(plural(t, "failed", summary.failed, "{count} failed", "{count} failed"));
|
|
222
|
+
const summaryText = parts.join(" · ");
|
|
223
|
+
|
|
224
|
+
const storedBody = (
|
|
225
|
+
<pre data-testid="web-search-fallback-body" className={PRE_CLASS}>
|
|
226
|
+
{body}
|
|
227
|
+
</pre>
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
let bodyContent: React.ReactNode;
|
|
231
|
+
if (payloadError) {
|
|
232
|
+
bodyContent = (
|
|
233
|
+
<div className="space-y-1">
|
|
234
|
+
<div
|
|
235
|
+
data-testid="web-search-error"
|
|
236
|
+
role="status"
|
|
237
|
+
className="flex gap-1.5 rounded-md border border-[var(--severity-warning-border)] bg-[var(--severity-warning-bg)] px-2 py-1.5 text-xs text-[var(--severity-warning-fg)] break-words"
|
|
238
|
+
>
|
|
239
|
+
<Icon path={mdiInformationOutline} size={0.6} aria-hidden="true" className="mt-px shrink-0" />
|
|
240
|
+
<span className="min-w-0">
|
|
241
|
+
{t(
|
|
242
|
+
"detailsUnavailable",
|
|
243
|
+
{ error: payloadError },
|
|
244
|
+
`Full details are not available (${payloadError}). Showing the summary that was saved with the chat.`,
|
|
245
|
+
)}
|
|
246
|
+
</span>
|
|
247
|
+
</div>
|
|
248
|
+
{storedBody}
|
|
249
|
+
</div>
|
|
250
|
+
);
|
|
251
|
+
} else if (payloadLoading) {
|
|
252
|
+
bodyContent = (
|
|
253
|
+
<div data-testid="web-search-loading" role="status" className={`flex items-center gap-1.5 px-2 py-1 text-xs ${MUTED}`}>
|
|
254
|
+
<Icon path={mdiLoading} size={0.55} aria-hidden="true" className="animate-spin" />
|
|
255
|
+
{t("loading", undefined, "Loading details…")}
|
|
256
|
+
</div>
|
|
257
|
+
);
|
|
258
|
+
} else if (payload !== undefined) {
|
|
259
|
+
bodyContent =
|
|
260
|
+
view?.kind === "search" ? (
|
|
261
|
+
<SearchBody view={view} t={t} />
|
|
262
|
+
) : view?.kind === "fetch" ? (
|
|
263
|
+
<FetchBody view={view} t={t} />
|
|
264
|
+
) : (
|
|
265
|
+
<pre data-testid="web-search-plain-payload" className={PRE_CLASS}>
|
|
266
|
+
{payloadAsText(payload)}
|
|
267
|
+
</pre>
|
|
268
|
+
);
|
|
269
|
+
} else {
|
|
270
|
+
bodyContent = storedBody;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const headerInner = (
|
|
274
|
+
<>
|
|
275
|
+
<span aria-hidden className={`inline-flex ${MUTED}`}>
|
|
276
|
+
<Icon path={icon} size={0.55} />
|
|
277
|
+
</span>
|
|
278
|
+
<span data-testid="web-search-label" className="font-semibold text-[var(--text-primary)]">
|
|
279
|
+
{label}
|
|
280
|
+
</span>
|
|
281
|
+
{summaryText ? (
|
|
282
|
+
<span data-testid="web-search-summary" className={`min-w-0 truncate ${MUTED}`}>
|
|
283
|
+
{summaryText}
|
|
284
|
+
</span>
|
|
285
|
+
) : null}
|
|
286
|
+
<span className={`ml-auto shrink-0 text-[12px] ${MUTED}`}>{new Date(timestamp).toLocaleTimeString()}</span>
|
|
287
|
+
{entryId ? (
|
|
288
|
+
<span aria-hidden className={`shrink-0 inline-flex ${MUTED}`}>
|
|
289
|
+
<Icon path={expanded ? mdiChevronDown : mdiChevronRight} size={0.6} />
|
|
290
|
+
</span>
|
|
291
|
+
) : null}
|
|
292
|
+
</>
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
return (
|
|
296
|
+
<div
|
|
297
|
+
className="my-1 mx-4 max-md:mx-2 [@media(max-height:599px)]:mx-2 rounded-lg border border-[var(--border-subtle)] bg-[var(--bg-tertiary)] overflow-hidden text-xs"
|
|
298
|
+
data-testid="web-search-entry"
|
|
299
|
+
data-entry-id={entryId ?? ""}
|
|
300
|
+
>
|
|
301
|
+
{entryId ? (
|
|
302
|
+
<button
|
|
303
|
+
type="button"
|
|
304
|
+
onClick={onToggle}
|
|
305
|
+
aria-expanded={expanded}
|
|
306
|
+
title={label}
|
|
307
|
+
data-testid="web-search-toggle"
|
|
308
|
+
aria-label={`${label}: ${expanded ? t("collapse", undefined, "Collapse entry") : t("expand", undefined, "Expand entry")}`}
|
|
309
|
+
className="focus-ring tap-target w-full flex items-center gap-2 px-3 py-1.5 text-left hover:bg-[var(--bg-secondary)] cursor-pointer"
|
|
310
|
+
>
|
|
311
|
+
{headerInner}
|
|
312
|
+
</button>
|
|
313
|
+
) : (
|
|
314
|
+
<div data-testid="web-search-header" title={label} className="w-full flex items-center gap-2 px-3 py-1.5 text-left">
|
|
315
|
+
{headerInner}
|
|
316
|
+
</div>
|
|
317
|
+
)}
|
|
318
|
+
{entryId && expanded && (
|
|
319
|
+
<div className="px-2 pb-2 max-h-[420px] overflow-y-auto" data-testid="web-search-body">
|
|
320
|
+
{bodyContent}
|
|
321
|
+
</div>
|
|
322
|
+
)}
|
|
323
|
+
</div>
|
|
324
|
+
);
|
|
325
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mirror of the client reducer's `CUSTOM_BODY_MAX_CHARS` (64 000): a row body at
|
|
3
|
+
* or above this length was tail-sliced with no marker, so it is partial. The
|
|
4
|
+
* plugin cannot import the client package, so the value is duplicated here and
|
|
5
|
+
* pinned by a drift-guard test (parse-web-search.test.ts).
|
|
6
|
+
*
|
|
7
|
+
* See change: render-unclaimed-custom-entries.
|
|
8
|
+
*/
|
|
9
|
+
export const CUSTOM_BODY_MAX_CHARS = 64_000;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client entry barrel for the web-access plugin.
|
|
3
|
+
*
|
|
4
|
+
* Exports every name the manifest claims reference (`WebSearchEntryCard`) plus
|
|
5
|
+
* the i18n catalog. The generated plugin registry imports these by name.
|
|
6
|
+
*
|
|
7
|
+
* See change: render-unclaimed-custom-entries.
|
|
8
|
+
*/
|
|
9
|
+
export { catalog } from "../i18n.js";
|
|
10
|
+
export { WebSearchEntryCard } from "./WebSearchEntryCard.js";
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure parsing for the web-access card (`web-search-results` custom entries
|
|
3
|
+
* emitted by `pi-web-access`).
|
|
4
|
+
*
|
|
5
|
+
* Everything here is TOTAL: the payload is untrusted third-party input, so no
|
|
6
|
+
* function throws. Counts are only reported when they are provably complete
|
|
7
|
+
* (under-reporting is safe; over-reporting is not).
|
|
8
|
+
*
|
|
9
|
+
* See change: render-unclaimed-custom-entries (design D3).
|
|
10
|
+
*/
|
|
11
|
+
import { CUSTOM_BODY_MAX_CHARS } from "./body-limits.js";
|
|
12
|
+
|
|
13
|
+
export const MAX_QUERIES = 20;
|
|
14
|
+
export const MAX_RESULTS_PER_QUERY = 20;
|
|
15
|
+
export const MAX_FETCH_URLS = 50;
|
|
16
|
+
|
|
17
|
+
/** Marker `truncateOutputForDisplay` prefixes when earlier lines were dropped. */
|
|
18
|
+
const LINE_TRUNCATION_MARKER = /^«\d+ earlier lines hidden»\n/;
|
|
19
|
+
|
|
20
|
+
export interface BodySummary {
|
|
21
|
+
/** Recognised payload subtype; absent when unknown / unparseable. */
|
|
22
|
+
type?: "search" | "fetch";
|
|
23
|
+
queries?: number;
|
|
24
|
+
results?: number;
|
|
25
|
+
urls?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Queries (search) or URL entries (fetch) carrying an `error`. Only set when
|
|
28
|
+
* the list is intact and every item is an object, and omitted when zero.
|
|
29
|
+
*/
|
|
30
|
+
failed?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const hasError = (v: unknown): boolean => isObject(v) && v.error !== null && v.error !== undefined;
|
|
34
|
+
|
|
35
|
+
function failedCount(items: unknown[]): number | undefined {
|
|
36
|
+
if (!items.every(isObject)) return undefined;
|
|
37
|
+
const n = items.filter(hasError).length;
|
|
38
|
+
return n > 0 ? n : undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isObject(v: unknown): v is Record<string, unknown> {
|
|
42
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function summarizeSearch(parsed: Record<string, unknown>): BodySummary {
|
|
46
|
+
const out: BodySummary = { type: "search" };
|
|
47
|
+
const queries = parsed.queries;
|
|
48
|
+
if (!Array.isArray(queries)) return out;
|
|
49
|
+
out.queries = queries.length;
|
|
50
|
+
const failed = failedCount(queries);
|
|
51
|
+
if (failed !== undefined) out.failed = failed;
|
|
52
|
+
if (queries.every((q) => isObject(q) && Array.isArray(q.results))) {
|
|
53
|
+
out.results = queries.reduce<number>((n, q) => n + (q as { results: unknown[] }).results.length, 0);
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function summarizeFetch(parsed: Record<string, unknown>): BodySummary {
|
|
59
|
+
const out: BodySummary = { type: "fetch" };
|
|
60
|
+
const list = Array.isArray(parsed.urlMetadata) ? parsed.urlMetadata : Array.isArray(parsed.urls) ? parsed.urls : null;
|
|
61
|
+
if (!list) return out;
|
|
62
|
+
out.urls = list.length;
|
|
63
|
+
const failed = failedCount(list);
|
|
64
|
+
if (failed !== undefined) out.failed = failed;
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Collapsed-line summary derived from the stored row body ONLY (no fetch).
|
|
70
|
+
* A count appears only when the matching value is actually an array — ingest
|
|
71
|
+
* either keeps an array whole (exact length) or replaces it with a string.
|
|
72
|
+
*/
|
|
73
|
+
export function summarizeBody(body: string): BodySummary {
|
|
74
|
+
try {
|
|
75
|
+
if (LINE_TRUNCATION_MARKER.test(body) || body.length >= CUSTOM_BODY_MAX_CHARS) return {};
|
|
76
|
+
const parsed: unknown = JSON.parse(body);
|
|
77
|
+
if (!isObject(parsed)) return {};
|
|
78
|
+
if (parsed.type === "search") return summarizeSearch(parsed);
|
|
79
|
+
if (parsed.type === "fetch") return summarizeFetch(parsed);
|
|
80
|
+
return {};
|
|
81
|
+
} catch {
|
|
82
|
+
return {};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface ResultView {
|
|
87
|
+
title: string;
|
|
88
|
+
url: string;
|
|
89
|
+
snippet: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface QueryView {
|
|
93
|
+
query: string;
|
|
94
|
+
/** Joined `providers[]`, else `provider`, else "". */
|
|
95
|
+
providers: string;
|
|
96
|
+
answer: string | null;
|
|
97
|
+
error: string | null;
|
|
98
|
+
results: ResultView[];
|
|
99
|
+
omittedResults: number;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface SearchView {
|
|
103
|
+
kind: "search";
|
|
104
|
+
queries: QueryView[];
|
|
105
|
+
omittedQueries: number;
|
|
106
|
+
/** Malformed entries that could not be displayed. */
|
|
107
|
+
skipped: number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface FetchItem {
|
|
111
|
+
url: string;
|
|
112
|
+
title: string;
|
|
113
|
+
size: number | null;
|
|
114
|
+
error: string | null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface FetchView {
|
|
118
|
+
kind: "fetch";
|
|
119
|
+
items: FetchItem[];
|
|
120
|
+
omitted: number;
|
|
121
|
+
skipped: number;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const str = (v: unknown): string | null => (typeof v === "string" ? v : null);
|
|
125
|
+
|
|
126
|
+
function parseSearch(p: Record<string, unknown>): SearchView | null {
|
|
127
|
+
if (!Array.isArray(p.queries)) return null;
|
|
128
|
+
let skipped = 0;
|
|
129
|
+
const queries: QueryView[] = [];
|
|
130
|
+
for (const q of p.queries.slice(0, MAX_QUERIES)) {
|
|
131
|
+
if (!isObject(q)) {
|
|
132
|
+
skipped++;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const rawResults = q.results;
|
|
136
|
+
const results: ResultView[] = [];
|
|
137
|
+
let omittedResults = 0;
|
|
138
|
+
if (Array.isArray(rawResults)) {
|
|
139
|
+
omittedResults = Math.max(0, rawResults.length - MAX_RESULTS_PER_QUERY);
|
|
140
|
+
for (const r of rawResults.slice(0, MAX_RESULTS_PER_QUERY)) {
|
|
141
|
+
if (!isObject(r) || typeof r.url !== "string") {
|
|
142
|
+
skipped++;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
results.push({ title: str(r.title) ?? r.url, url: r.url, snippet: str(r.snippet) ?? "" });
|
|
146
|
+
}
|
|
147
|
+
} else if (rawResults !== undefined) {
|
|
148
|
+
skipped++;
|
|
149
|
+
}
|
|
150
|
+
const providers = Array.isArray(q.providers)
|
|
151
|
+
? q.providers.filter((x): x is string => typeof x === "string").join(", ")
|
|
152
|
+
: (str(q.provider) ?? "");
|
|
153
|
+
queries.push({
|
|
154
|
+
query: str(q.query) ?? "",
|
|
155
|
+
providers,
|
|
156
|
+
answer: str(q.answer),
|
|
157
|
+
error: str(q.error),
|
|
158
|
+
results,
|
|
159
|
+
omittedResults,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return { kind: "search", queries, omittedQueries: Math.max(0, p.queries.length - MAX_QUERIES), skipped };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function parseFetch(p: Record<string, unknown>): FetchView | null {
|
|
166
|
+
const raw = Array.isArray(p.urlMetadata) ? p.urlMetadata : Array.isArray(p.urls) ? p.urls : null;
|
|
167
|
+
if (!raw) return null;
|
|
168
|
+
let skipped = 0;
|
|
169
|
+
const items: FetchItem[] = [];
|
|
170
|
+
for (const u of raw.slice(0, MAX_FETCH_URLS)) {
|
|
171
|
+
if (!isObject(u) || typeof u.url !== "string") {
|
|
172
|
+
skipped++;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const size =
|
|
176
|
+
typeof u.contentLength === "number" ? u.contentLength : typeof u.content === "string" ? u.content.length : null;
|
|
177
|
+
items.push({ url: u.url, title: str(u.title) ?? u.url, size, error: str(u.error) });
|
|
178
|
+
}
|
|
179
|
+
return { kind: "fetch", items, omitted: Math.max(0, raw.length - MAX_FETCH_URLS), skipped };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Typed view of a full payload; `null` for research / unknown / garbage. Never throws. */
|
|
183
|
+
export function parseWebSearchPayload(payload: unknown): SearchView | FetchView | null {
|
|
184
|
+
try {
|
|
185
|
+
if (!isObject(payload)) return null;
|
|
186
|
+
if (payload.type === "search") return parseSearch(payload);
|
|
187
|
+
if (payload.type === "fetch") return parseFetch(payload);
|
|
188
|
+
return null;
|
|
189
|
+
} catch {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* `href` for a link-safe URL, else `null`. Only http(s) without embedded
|
|
196
|
+
* credentials qualifies; the caller renders the ORIGINAL text with the
|
|
197
|
+
* normalized `href` as target.
|
|
198
|
+
*/
|
|
199
|
+
export function safeHttpUrl(raw: string): string | null {
|
|
200
|
+
try {
|
|
201
|
+
if (typeof raw !== "string") return null;
|
|
202
|
+
const u = new URL(raw);
|
|
203
|
+
if (u.protocol !== "http:" && u.protocol !== "https:") return null;
|
|
204
|
+
if (u.username !== "" || u.password !== "") return null;
|
|
205
|
+
return u.href;
|
|
206
|
+
} catch {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
}
|
package/src/i18n.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web-access plugin i18n catalog — UNPREFIXED leaf keys.
|
|
3
|
+
*
|
|
4
|
+
* The generated plugin registry imports the `catalog` named export (declared as
|
|
5
|
+
* `i18nCatalog` in package.json's `pi-dashboard-plugin` manifest); the shell
|
|
6
|
+
* merges it under `plugin.web-access.*`. Components resolve keys via the scoped
|
|
7
|
+
* `useT()` hook. English lives at the call sites as the `t(key, vars, fallback)`
|
|
8
|
+
* fallback. `zh-CN` and `hu` MUST keep identical key sets.
|
|
9
|
+
*
|
|
10
|
+
* See change: render-unclaimed-custom-entries.
|
|
11
|
+
*/
|
|
12
|
+
export const catalog = {
|
|
13
|
+
"zh-CN": {
|
|
14
|
+
labelSearch: "网络搜索",
|
|
15
|
+
labelFetch: "网页抓取",
|
|
16
|
+
labelResearch: "网络研究",
|
|
17
|
+
labelGeneric: "网络搜索结果",
|
|
18
|
+
queriesOne: "{count} 个查询",
|
|
19
|
+
queriesOther: "{count} 个查询",
|
|
20
|
+
resultsOne: "{count} 条结果",
|
|
21
|
+
resultsOther: "{count} 条结果",
|
|
22
|
+
urlsOne: "{count} 个网址",
|
|
23
|
+
urlsOther: "{count} 个网址",
|
|
24
|
+
expand: "展开条目",
|
|
25
|
+
collapse: "折叠条目",
|
|
26
|
+
loading: "正在加载详情…",
|
|
27
|
+
detailsUnavailable: "无法获取完整详情({error})。显示的是随对话保存的摘要。",
|
|
28
|
+
moreItems: "另有 {count} 项",
|
|
29
|
+
skippedItemsOne: "有 {count} 项无法显示",
|
|
30
|
+
skippedItemsOther: "有 {count} 项无法显示",
|
|
31
|
+
failedOne: "{count} 项失败",
|
|
32
|
+
failedOther: "{count} 项失败",
|
|
33
|
+
opensInNewTab: "(在新标签页中打开)",
|
|
34
|
+
noResults: "无结果",
|
|
35
|
+
answerLabel: "回答",
|
|
36
|
+
providerLabel: "来源:{provider}",
|
|
37
|
+
sizeChars: "{count} 个字符",
|
|
38
|
+
},
|
|
39
|
+
hu: {
|
|
40
|
+
labelSearch: "Webes keresés",
|
|
41
|
+
labelFetch: "Weboldal-letöltés",
|
|
42
|
+
labelResearch: "Webes kutatás",
|
|
43
|
+
labelGeneric: "Webes keresési találatok",
|
|
44
|
+
queriesOne: "{count} lekérdezés",
|
|
45
|
+
queriesOther: "{count} lekérdezés",
|
|
46
|
+
resultsOne: "{count} találat",
|
|
47
|
+
resultsOther: "{count} találat",
|
|
48
|
+
urlsOne: "{count} URL",
|
|
49
|
+
urlsOther: "{count} URL",
|
|
50
|
+
expand: "Bejegyzés kibontása",
|
|
51
|
+
collapse: "Bejegyzés összecsukása",
|
|
52
|
+
loading: "Részletek betöltése…",
|
|
53
|
+
detailsUnavailable: "A teljes részletek nem érhetők el ({error}). A beszélgetéssel mentett összegzés látható.",
|
|
54
|
+
moreItems: "még {count} elem",
|
|
55
|
+
skippedItemsOne: "{count} elem nem jeleníthető meg",
|
|
56
|
+
skippedItemsOther: "{count} elem nem jeleníthető meg",
|
|
57
|
+
failedOne: "{count} sikertelen",
|
|
58
|
+
failedOther: "{count} sikertelen",
|
|
59
|
+
opensInNewTab: " (új lapon nyílik meg)",
|
|
60
|
+
noResults: "Nincs találat",
|
|
61
|
+
answerLabel: "Válasz",
|
|
62
|
+
providerLabel: "Szolgáltató: {provider}",
|
|
63
|
+
sizeChars: "{count} karakter",
|
|
64
|
+
},
|
|
65
|
+
} as const;
|