@circuitwall/jarela 2.5.1 → 2.5.2
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/.next/standalone/.next/BUILD_ID +1 -1
- package/.next/standalone/.next/app-path-routes-manifest.json +5 -5
- package/.next/standalone/.next/build-manifest.json +2 -2
- package/.next/standalone/.next/prerender-manifest.json +3 -3
- package/.next/standalone/.next/react-loadable-manifest.json +2 -2
- package/.next/standalone/.next/server/app/_global-error/page_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/_global-error.html +1 -1
- package/.next/standalone/.next/server/app/_global-error.rsc +1 -1
- package/.next/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/_not-found.html +1 -1
- package/.next/standalone/.next/server/app/_not-found.rsc +1 -1
- package/.next/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/chats/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/chats/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/lookup/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/lookup/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/pair/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/pair/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/status/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/bridges/[id]/status/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/builtin-tools/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/builtin-tools/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/credentials/[id]/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/credentials/[id]/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/credentials/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/credentials/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/dashboard/pricing-task/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/dashboard/pricing-task/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/delegations/codex/[job_id]/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/delegations/codex/[job_id]/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/documents/sources/[id]/reindex/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/documents/sources/[id]/reindex/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/documents/sources/[id]/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/documents/sources/[id]/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/documents/sources/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/documents/sources/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/events/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/events/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/extension/agents/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/extension/agents/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/extension/fill/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/extension/fill/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/extension/refine/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/extension/refine/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/extension/turn/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/extension/turn/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/extensions/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/extensions/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/config/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/config/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/secrets/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/secrets/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/health/probes/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/health/probes/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/integrations/[name]/test/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/integrations/[name]/test/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/integrations/gmail/oauth/callback/route.js +22 -1
- package/.next/standalone/.next/server/app/api/v1/integrations/gmail/oauth/start/route.js +22 -1
- package/.next/standalone/.next/server/app/api/v1/integrations/gmail/oauth/status/route.js +22 -1
- package/.next/standalone/.next/server/app/api/v1/integrations/outlook/oauth/callback/route.js +19 -2
- package/.next/standalone/.next/server/app/api/v1/integrations/outlook/oauth/start/route.js +19 -2
- package/.next/standalone/.next/server/app/api/v1/integrations/outlook/oauth/status/route.js +19 -2
- package/.next/standalone/.next/server/app/api/v1/packages/defaults/[id]/route.js +12 -2
- package/.next/standalone/.next/server/app/api/v1/packages/defaults/[id]/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/packages/install/[id]/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/packages/install/[id]/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/packages/install/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/packages/install/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/packages/manifests/[name]/enabled/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/packages/manifests/[name]/enabled/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/packages/manifests/[name]/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/packages/manifests/[name]/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/packages/manifests/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/packages/manifests/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/packages/reload/route.js +12 -2
- package/.next/standalone/.next/server/app/api/v1/packages/reload/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/packages/route.js +12 -2
- package/.next/standalone/.next/server/app/api/v1/packages/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/page-capture/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/page-capture/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/pending-actions/[id]/approve/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/pending-actions/[id]/approve/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/pending-actions/[id]/deny/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/pending-actions/[id]/deny/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/pending-actions/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/pending-actions/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/scheduled-tasks/[id]/run/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/scheduled-tasks/[id]/run/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/system/restart/route.js +7 -0
- package/.next/standalone/.next/server/app/api/v1/system/restart/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/threads/[thread_id]/run/route.js +52 -45
- package/.next/standalone/.next/server/app/api/v1/threads/[thread_id]/run/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/tools/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/tools/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/update/apply/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/watchers/[id]/run/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/watchers/[id]/run/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/api/v1/watchers/route.js +8 -1
- package/.next/standalone/.next/server/app/api/v1/watchers/route.js.nft.json +1 -1
- package/.next/standalone/.next/server/app/page.js +72982 -72980
- package/.next/standalone/.next/server/app/page_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/setup/page_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app-paths-manifest.json +5 -5
- package/.next/standalone/.next/server/chunks/1433.js +1 -1
- package/.next/standalone/.next/server/chunks/210.js +6 -6
- package/.next/standalone/.next/server/chunks/2471.js +17 -17
- package/.next/standalone/.next/server/chunks/3524.js +31 -15
- package/.next/standalone/.next/server/chunks/3566.js +1 -1
- package/.next/standalone/.next/server/chunks/4189.js +1 -1
- package/.next/standalone/.next/server/chunks/{9590.js → 423.js} +10 -3
- package/.next/standalone/.next/server/chunks/4508.js +2 -2
- package/.next/standalone/.next/server/chunks/4596.js +5 -2
- package/.next/standalone/.next/server/chunks/46.js +7 -0
- package/.next/standalone/.next/server/chunks/540.js +29 -5
- package/.next/standalone/.next/server/chunks/5585.js +41 -3
- package/.next/standalone/.next/server/chunks/5908.js +10 -10
- package/.next/standalone/.next/server/chunks/{3399.js → 6058.js} +882 -400
- package/.next/standalone/.next/server/chunks/{9164.js → 6190.js} +13777 -13530
- package/.next/standalone/.next/server/chunks/6649.js +1 -1
- package/.next/standalone/.next/server/chunks/{1388.js → 8842.js} +5961 -5793
- package/.next/standalone/.next/server/chunks/9032.js +5 -5
- package/.next/standalone/.next/server/chunks/{7547.js → 9217.js} +485 -3
- package/.next/standalone/.next/server/instrumentation.js +7 -0
- package/.next/standalone/.next/server/middleware-build-manifest.js +2 -2
- package/.next/standalone/.next/server/middleware-react-loadable-manifest.js +1 -1
- package/.next/standalone/.next/server/middleware.js +7 -0
- package/.next/standalone/.next/server/pages/404.html +1 -1
- package/.next/standalone/.next/server/pages/500.html +1 -1
- package/.next/standalone/.next/server/preview-props.json +3 -3
- package/.next/standalone/.next/server/server-reference-manifest.json +1 -1
- package/.next/standalone/.next/static/chunks/app/{page-dd05bcb5f5b81157.js → page-4112e14360a9909e.js} +1576 -1574
- package/.next/standalone/docs/adoption/changesets.json +139 -0
- package/.next/standalone/package.json +2 -1
- package/CHANGELOG.md +38 -0
- package/browser-extension/background.js +5 -539
- package/browser-extension/lib/form-editing.mjs +541 -0
- package/components/chat/MessageBubble.tsx +4 -736
- package/components/chat/MessageContent.tsx +749 -0
- package/components/layout/MenuPanel.tsx +1 -1
- package/hooks/useSSE.test.ts +46 -0
- package/lib/agents/llm.ts +1 -1
- package/lib/agents/output-validator/validator.test.ts +4 -0
- package/lib/agents/output-validator/validator.ts +1 -1
- package/lib/agents/prompt-registry.test.ts +11 -0
- package/lib/agents/prompt-registry.ts +13 -0
- package/lib/agents/retry-prompts.ts +17 -0
- package/lib/agents/run-retry.ts +671 -0
- package/lib/agents/run-thread.retry.test.ts +176 -2
- package/lib/agents/run-thread.test.ts +6 -6
- package/lib/agents/run-thread.ts +9 -717
- package/lib/agents/stream-collector.test.ts +28 -4
- package/lib/agents/stream-collector.ts +5 -28
- package/lib/agents/tool-outcomes.ts +61 -0
- package/lib/documents/remote/confluence.test.ts +65 -0
- package/lib/documents/remote/confluence.ts +35 -9
- package/lib/documents/remote/jira.ts +1 -1
- package/lib/health/probes.test.ts +32 -0
- package/lib/health/probes.ts +29 -14
- package/lib/integrations/atlassian/manifest.ts +5 -3
- package/lib/integrations/github/manifest.ts +7 -4
- package/lib/integrations/gmail-oauth.ts +16 -1
- package/lib/integrations/microsoft-oauth.ts +17 -2
- package/lib/integrations/outlook/manifest.ts +10 -8
- package/lib/stores/integrations.ts +1 -0
- package/lib/tools/communications/calendar-aliases.test.ts +77 -1
- package/lib/tools/communications/gmail.test.ts +13 -0
- package/lib/tools/communications/ms-graph.test.ts +26 -3
- package/lib/tools/communications/ms-graph.ts +9 -11
- package/lib/tools/communications/outlook-calendar.ts +94 -17
- package/lib/tools/packages/default-packages/atlassian.ts +8 -2
- package/package.json +2 -1
- package/scripts/standalone-footprint.mjs +1 -1
- /package/.next/standalone/.next/static/{XFZTDssHBZKKX2mzH2ld0 → VZXMorlOmSmi3dUr7K5UX}/_buildManifest.js +0 -0
- /package/.next/standalone/.next/static/{XFZTDssHBZKKX2mzH2ld0 → VZXMorlOmSmi3dUr7K5UX}/_ssgManifest.js +0 -0
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { memo, useCallback, useEffect, useId, useMemo, useState } from "react";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import ReactMarkdown from "react-markdown";
|
|
5
|
+
import remarkGfm from "remark-gfm";
|
|
6
|
+
import rehypeRaw from "rehype-raw";
|
|
7
|
+
import rehypeHighlight from "rehype-highlight";
|
|
8
|
+
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
|
|
9
|
+
import { Check, Copy, ExternalLink, FileText, Loader2, Paperclip } from "lucide-react";
|
|
10
|
+
import type { ContentPart } from "@/api/types";
|
|
11
|
+
import { Dialog } from "@/components/ui/Dialog";
|
|
12
|
+
import { parseHref } from "@/lib/ui/navigate";
|
|
13
|
+
import { pushToast } from "@/lib/ui/toasts";
|
|
14
|
+
import { errorMessage } from "@/lib/utils/error";
|
|
15
|
+
// Tracks which TTS clips have started auto-playing this session so a clip
|
|
16
|
+
// only autoplays once. Re-renders of the same <audio> (message refetched,
|
|
17
|
+
// scroll into view, list re-render) won't trigger a replay. Keyed by the
|
|
18
|
+
// full href (including any `?autoplay=1`). Cleared on hard refresh — exactly
|
|
19
|
+
// the "session" semantics we want.
|
|
20
|
+
const playedAutoplayHrefs = new Set<string>();
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
function InlineAudio({ href }: { href: string }) {
|
|
24
|
+
// Decide autoPlay at mount time: opt in only if the URL asked for it AND
|
|
25
|
+
// we haven't already auto-played this exact clip in this session.
|
|
26
|
+
const wantsAuto = /[?&]autoplay=1\b/.test(href);
|
|
27
|
+
const [auto] = useState(() => wantsAuto && !playedAutoplayHrefs.has(href));
|
|
28
|
+
return (
|
|
29
|
+
<audio
|
|
30
|
+
controls
|
|
31
|
+
preload="metadata"
|
|
32
|
+
autoPlay={auto}
|
|
33
|
+
src={href}
|
|
34
|
+
className="my-2 w-full max-w-md"
|
|
35
|
+
onPlay={() => {
|
|
36
|
+
if (wantsAuto) playedAutoplayHrefs.add(href);
|
|
37
|
+
}}
|
|
38
|
+
/>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
// Sanitizer schema = GitHub default + extras the agent is told it can use.
|
|
44
|
+
// Anything not listed here gets stripped (scripts, event handlers, javascript: URLs).
|
|
45
|
+
const sanitizeSchema = {
|
|
46
|
+
...defaultSchema,
|
|
47
|
+
tagNames: [
|
|
48
|
+
...(defaultSchema.tagNames ?? []),
|
|
49
|
+
"details", "summary", "mark", "kbd", "sub", "sup", "abbr", "small",
|
|
50
|
+
"aside", "figure", "figcaption", "dl", "dt", "dd",
|
|
51
|
+
"audio", "source",
|
|
52
|
+
],
|
|
53
|
+
attributes: {
|
|
54
|
+
...(defaultSchema.attributes ?? {}),
|
|
55
|
+
"*": [...((defaultSchema.attributes ?? {})["*"] ?? []), "className", "title"],
|
|
56
|
+
aside: ["className"],
|
|
57
|
+
abbr: ["title"],
|
|
58
|
+
details: ["open"],
|
|
59
|
+
audio: ["src", "controls", "preload", "loop", "autoplay", "muted"],
|
|
60
|
+
source: ["src", "type"],
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
// Module-scope plugin arrays — passing fresh array literals to ReactMarkdown
|
|
66
|
+
// on every render of MarkdownContent forced react-markdown's internal
|
|
67
|
+
// useMemo() inputs to differ each time, defeating its plugin-pipeline
|
|
68
|
+
// memoization. Hoisting these makes the references stable across renders.
|
|
69
|
+
const MD_REMARK_PLUGINS = [remarkGfm];
|
|
70
|
+
|
|
71
|
+
// rehype-highlight runs after rehype-raw (so any raw HTML <code> blocks are
|
|
72
|
+
// also highlighted) and before rehype-sanitize (so the spans/classes it adds
|
|
73
|
+
// pass through the sanitizer's allow-list rather than being stripped).
|
|
74
|
+
const MD_REHYPE_PLUGINS: import("unified").PluggableList = [
|
|
75
|
+
rehypeRaw,
|
|
76
|
+
[rehypeHighlight, { detect: true, ignoreMissing: true }],
|
|
77
|
+
[rehypeSanitize, sanitizeSchema],
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
function isLikelyLocalFileHref(href: string): boolean {
|
|
82
|
+
if (!href || href.startsWith("#")) return false;
|
|
83
|
+
if (/^(mailto|tel|javascript|data|blob):/i.test(href)) return false;
|
|
84
|
+
if (/^file:\/\//i.test(href)) return true;
|
|
85
|
+
if (/^[A-Za-z]:[\\/]/.test(href) || href.startsWith("~/") || href.startsWith("~\\")) return true;
|
|
86
|
+
if (/^https?:\/\//i.test(href)) {
|
|
87
|
+
try {
|
|
88
|
+
const url = new URL(href);
|
|
89
|
+
if (!["localhost", "127.0.0.1", "[::1]", "::1"].includes(url.hostname)) return false;
|
|
90
|
+
return !/^\/(api|_next)(\/|$)/.test(url.pathname);
|
|
91
|
+
} catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (href.startsWith("/")) return false;
|
|
96
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(href)) return false;
|
|
97
|
+
return /(^\.\.?[\\/]|[\\/]|\.[A-Za-z0-9]{1,12}(?:[?#]|$))/.test(href);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
interface LocalFilePreview {
|
|
102
|
+
path: string;
|
|
103
|
+
name: string;
|
|
104
|
+
size: number;
|
|
105
|
+
renderable: boolean;
|
|
106
|
+
snippet?: string;
|
|
107
|
+
truncated?: boolean;
|
|
108
|
+
error?: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
function LocalFileLink({ href, threadId, children, className, title }: { href: string; threadId?: string | null; children: ReactNode; className?: string; title?: string }) {
|
|
113
|
+
const [preview, setPreview] = useState<LocalFilePreview | null>(null);
|
|
114
|
+
const [loading, setLoading] = useState(false);
|
|
115
|
+
const [openError, setOpenError] = useState<string | null>(null);
|
|
116
|
+
|
|
117
|
+
const openDefault = useCallback(async () => {
|
|
118
|
+
setOpenError(null);
|
|
119
|
+
const res = await fetch("/api/v1/local-file", {
|
|
120
|
+
method: "POST",
|
|
121
|
+
headers: { "Content-Type": "application/json" },
|
|
122
|
+
body: JSON.stringify({ href, thread_id: threadId ?? undefined }),
|
|
123
|
+
});
|
|
124
|
+
const body = await res.json().catch(() => ({} as { error?: string }));
|
|
125
|
+
if (!res.ok) throw new Error(body.error ?? `HTTP ${res.status}`);
|
|
126
|
+
pushToast({
|
|
127
|
+
kind: "success",
|
|
128
|
+
source: "system",
|
|
129
|
+
sourceLabel: "Chat",
|
|
130
|
+
title: "Opening file",
|
|
131
|
+
body: "Sent the file to the operating system default app.",
|
|
132
|
+
agent_id: null,
|
|
133
|
+
thread_id: threadId ?? null,
|
|
134
|
+
ttl: 1800,
|
|
135
|
+
});
|
|
136
|
+
}, [href, threadId]);
|
|
137
|
+
|
|
138
|
+
const activate = useCallback(async () => {
|
|
139
|
+
setLoading(true);
|
|
140
|
+
setOpenError(null);
|
|
141
|
+
try {
|
|
142
|
+
const params = new URLSearchParams({ href });
|
|
143
|
+
if (threadId) params.set("thread_id", threadId);
|
|
144
|
+
const res = await fetch(`/api/v1/local-file?${params.toString()}`);
|
|
145
|
+
const body = await res.json().catch(() => ({} as LocalFilePreview));
|
|
146
|
+
if (!res.ok) throw new Error(body.error ?? `HTTP ${res.status}`);
|
|
147
|
+
const info = body as LocalFilePreview;
|
|
148
|
+
setPreview(info);
|
|
149
|
+
if (!info.renderable) await openDefault();
|
|
150
|
+
} catch (err) {
|
|
151
|
+
const msg = errorMessage(err);
|
|
152
|
+
setOpenError(msg);
|
|
153
|
+
pushToast({
|
|
154
|
+
kind: "error",
|
|
155
|
+
source: "system",
|
|
156
|
+
sourceLabel: "Chat",
|
|
157
|
+
title: "Could not open file",
|
|
158
|
+
body: msg,
|
|
159
|
+
agent_id: null,
|
|
160
|
+
thread_id: threadId ?? null,
|
|
161
|
+
ttl: 5000,
|
|
162
|
+
});
|
|
163
|
+
} finally {
|
|
164
|
+
setLoading(false);
|
|
165
|
+
}
|
|
166
|
+
}, [href, openDefault, threadId]);
|
|
167
|
+
|
|
168
|
+
return (
|
|
169
|
+
<span className="inline-flex flex-col max-w-full align-baseline">
|
|
170
|
+
<button
|
|
171
|
+
type="button"
|
|
172
|
+
className={`${className ?? ""} inline-flex items-baseline gap-1 text-left underline decoration-dotted hover:decoration-solid break-all`.trim()}
|
|
173
|
+
title={title ?? href}
|
|
174
|
+
onClick={activate}
|
|
175
|
+
disabled={loading}
|
|
176
|
+
>
|
|
177
|
+
<FileText size={12} className="relative top-0.5 shrink-0" />
|
|
178
|
+
<span>{children}</span>
|
|
179
|
+
{loading && <Loader2 size={11} className="relative top-0.5 shrink-0 animate-spin" />}
|
|
180
|
+
</button>
|
|
181
|
+
{preview?.renderable && typeof preview.snippet === "string" && (
|
|
182
|
+
<span className="not-prose my-2 block rounded-md border border-border/60 bg-surface-2 overflow-hidden">
|
|
183
|
+
<span className="flex items-center justify-between gap-2 px-2 py-1 text-[10px] text-fg-faint bg-surface-3">
|
|
184
|
+
<span className="truncate" title={preview.path}>{preview.path}</span>
|
|
185
|
+
<button
|
|
186
|
+
type="button"
|
|
187
|
+
onClick={openDefault}
|
|
188
|
+
className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded border border-border/60 bg-surface-2 text-fg-muted hover:text-fg shrink-0"
|
|
189
|
+
>
|
|
190
|
+
<ExternalLink size={10} />
|
|
191
|
+
Open
|
|
192
|
+
</button>
|
|
193
|
+
</span>
|
|
194
|
+
<code className="block max-h-72 overflow-auto whitespace-pre-wrap break-words px-3 py-2 text-xs leading-relaxed text-fg-muted bg-[#0d1117]">
|
|
195
|
+
{preview.snippet}{preview.truncated ? "\n..." : ""}
|
|
196
|
+
</code>
|
|
197
|
+
</span>
|
|
198
|
+
)}
|
|
199
|
+
{openError && <span className="text-[11px] text-rose-400 mt-1">{openError}</span>}
|
|
200
|
+
</span>
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
function reactChildrenToText(children: ReactNode): string {
|
|
206
|
+
if (children == null || typeof children === "boolean") return "";
|
|
207
|
+
if (typeof children === "string" || typeof children === "number") return String(children);
|
|
208
|
+
if (Array.isArray(children)) return children.map(reactChildrenToText).join("");
|
|
209
|
+
if (typeof children === "object" && "props" in (children as object)) {
|
|
210
|
+
return reactChildrenToText((children as { props: { children?: ReactNode } }).props.children);
|
|
211
|
+
}
|
|
212
|
+
return "";
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
// Markdown image renderer with a one-time automatic retry. If both attempts
|
|
217
|
+
// fail (common on transient local-network/SW races), show a compact fallback
|
|
218
|
+
// with explicit actions instead of a broken image icon.
|
|
219
|
+
function ResilientMarkdownImage({ src, alt }: { src?: string | Blob; alt?: string }) {
|
|
220
|
+
const [retryNonce, setRetryNonce] = useState(0);
|
|
221
|
+
const [failed, setFailed] = useState(false);
|
|
222
|
+
const [open, setOpen] = useState(false);
|
|
223
|
+
const objectUrl = useMemo(() => (src instanceof Blob ? URL.createObjectURL(src) : null), [src]);
|
|
224
|
+
|
|
225
|
+
useEffect(() => {
|
|
226
|
+
return () => {
|
|
227
|
+
if (objectUrl) URL.revokeObjectURL(objectUrl);
|
|
228
|
+
};
|
|
229
|
+
}, [objectUrl]);
|
|
230
|
+
|
|
231
|
+
const normalizedSrc = typeof src === "string" ? src : objectUrl ?? "";
|
|
232
|
+
const effectiveSrc = normalizedSrc
|
|
233
|
+
? `${normalizedSrc}${normalizedSrc.includes("?") ? "&" : "?"}retry=${retryNonce}`
|
|
234
|
+
: "";
|
|
235
|
+
|
|
236
|
+
if (!normalizedSrc) {
|
|
237
|
+
return (
|
|
238
|
+
<div className="my-2 px-3 py-2 rounded border border-rose-800/60 bg-rose-950/30 text-xs text-rose-700 dark:text-rose-300">
|
|
239
|
+
Invalid image URL in markdown.
|
|
240
|
+
</div>
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (failed) {
|
|
245
|
+
return (
|
|
246
|
+
<div className="my-2 px-3 py-2 rounded border border-amber-700/50 bg-amber-900/20 text-xs text-amber-300">
|
|
247
|
+
<div>Image failed to load.</div>
|
|
248
|
+
<div className="mt-1 flex items-center gap-2">
|
|
249
|
+
<button
|
|
250
|
+
type="button"
|
|
251
|
+
className="px-2 py-1 rounded border border-amber-500/60 hover:bg-amber-900/30"
|
|
252
|
+
onClick={() => {
|
|
253
|
+
setFailed(false);
|
|
254
|
+
setRetryNonce((n) => n + 1);
|
|
255
|
+
}}
|
|
256
|
+
>
|
|
257
|
+
Retry
|
|
258
|
+
</button>
|
|
259
|
+
<a
|
|
260
|
+
href={normalizedSrc}
|
|
261
|
+
target="_blank"
|
|
262
|
+
rel="noopener noreferrer"
|
|
263
|
+
className="underline decoration-dotted hover:decoration-solid break-all"
|
|
264
|
+
>
|
|
265
|
+
Open image
|
|
266
|
+
</a>
|
|
267
|
+
</div>
|
|
268
|
+
</div>
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return (
|
|
273
|
+
<>
|
|
274
|
+
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
275
|
+
<img
|
|
276
|
+
src={effectiveSrc}
|
|
277
|
+
alt={alt ?? "image"}
|
|
278
|
+
loading="lazy"
|
|
279
|
+
className="rounded-lg border border-border/50 cursor-zoom-in hover:border-fg-faint transition-colors"
|
|
280
|
+
onClick={() => setOpen(true)}
|
|
281
|
+
onError={() => {
|
|
282
|
+
if (retryNonce === 0) {
|
|
283
|
+
setRetryNonce(1);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
setFailed(true);
|
|
287
|
+
}}
|
|
288
|
+
/>
|
|
289
|
+
<ImagePreviewDialog src={normalizedSrc} alt={alt ?? "image"} open={open} onClose={() => setOpen(false)} />
|
|
290
|
+
</>
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
// Renders a Google Maps Embed inside an iframe whose src points at our
|
|
296
|
+
// server-side proxy (/api/v1/maps/embed). The proxy injects the API key so
|
|
297
|
+
// it never appears in chat HTML. Triggered by ```map fenced blocks emitted
|
|
298
|
+
// by the agent — see PRESENTATION_CTX in lib/agents/run-thread.ts.
|
|
299
|
+
function MapEmbed({ payload }: { payload: string }) {
|
|
300
|
+
const [retryNonce, setRetryNonce] = useState(0);
|
|
301
|
+
const [failed, setFailed] = useState(false);
|
|
302
|
+
let params: URLSearchParams | null = null;
|
|
303
|
+
let parseError: string | null = null;
|
|
304
|
+
try {
|
|
305
|
+
const obj = JSON.parse(payload) as Record<string, unknown>;
|
|
306
|
+
const sp = new URLSearchParams();
|
|
307
|
+
for (const k of ["q", "center", "zoom", "origin", "destination", "search", "mode"] as const) {
|
|
308
|
+
const v = obj[k];
|
|
309
|
+
if (typeof v === "string" && v.length > 0) sp.set(k, v);
|
|
310
|
+
else if (typeof v === "number") sp.set(k, String(v));
|
|
311
|
+
}
|
|
312
|
+
if ([...sp.keys()].length === 0) parseError = "no recognized fields (q / center / origin+destination / search)";
|
|
313
|
+
else params = sp;
|
|
314
|
+
} catch (e) {
|
|
315
|
+
parseError = errorMessage(e);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (!params) {
|
|
319
|
+
return (
|
|
320
|
+
<div className="my-2 px-3 py-2 rounded border border-rose-800/60 bg-rose-950/30 text-xs text-rose-700 dark:text-rose-300">
|
|
321
|
+
Invalid map block: {parseError}
|
|
322
|
+
</div>
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
const src = `/api/v1/maps/embed?${params.toString()}${retryNonce > 0 ? `&retry=${retryNonce}` : ""}`;
|
|
326
|
+
|
|
327
|
+
if (failed) {
|
|
328
|
+
const q = params.get("q") ?? params.get("search") ?? params.get("center") ?? "";
|
|
329
|
+
const direct = q
|
|
330
|
+
? `https://www.google.com/maps?q=${encodeURIComponent(q)}`
|
|
331
|
+
: "https://www.google.com/maps";
|
|
332
|
+
return (
|
|
333
|
+
<div className="my-2 px-3 py-2 rounded border border-amber-700/50 bg-amber-900/20 text-xs text-amber-300">
|
|
334
|
+
<div>Map failed to load.</div>
|
|
335
|
+
<div className="mt-1 flex items-center gap-2">
|
|
336
|
+
<button
|
|
337
|
+
type="button"
|
|
338
|
+
className="px-2 py-1 rounded border border-amber-500/60 hover:bg-amber-900/30"
|
|
339
|
+
onClick={() => {
|
|
340
|
+
setFailed(false);
|
|
341
|
+
setRetryNonce((n) => n + 1);
|
|
342
|
+
}}
|
|
343
|
+
>
|
|
344
|
+
Retry
|
|
345
|
+
</button>
|
|
346
|
+
<a
|
|
347
|
+
href={direct}
|
|
348
|
+
target="_blank"
|
|
349
|
+
rel="noopener noreferrer"
|
|
350
|
+
className="underline decoration-dotted hover:decoration-solid"
|
|
351
|
+
>
|
|
352
|
+
Open in Google Maps
|
|
353
|
+
</a>
|
|
354
|
+
</div>
|
|
355
|
+
</div>
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return (
|
|
360
|
+
<div className="my-2 rounded overflow-hidden border border-border bg-surface-2">
|
|
361
|
+
<iframe
|
|
362
|
+
src={src}
|
|
363
|
+
title="Map"
|
|
364
|
+
width="100%"
|
|
365
|
+
height="320"
|
|
366
|
+
loading="lazy"
|
|
367
|
+
referrerPolicy="no-referrer-when-downgrade"
|
|
368
|
+
allowFullScreen
|
|
369
|
+
style={{ border: 0, display: "block" }}
|
|
370
|
+
onError={() => {
|
|
371
|
+
if (retryNonce === 0) {
|
|
372
|
+
setRetryNonce(1);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
setFailed(true);
|
|
376
|
+
}}
|
|
377
|
+
/>
|
|
378
|
+
</div>
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
// Memoized: this component appears under every assistant bubble and during
|
|
384
|
+
// streaming gets re-rendered on every rAF flush. The inner ReactMarkdown
|
|
385
|
+
// pipeline (remark + rehype passes, syntax highlighting via rehypeHighlight,
|
|
386
|
+
// custom <a> / <code> renderers) is the expensive part — re-running it
|
|
387
|
+
// against unchanged text on every parent render shows up as jank on long
|
|
388
|
+
// threads. With memo, only the in-flight bubble re-renders during streaming;
|
|
389
|
+
// persisted siblings sit idle.
|
|
390
|
+
export const MarkdownContent = memo(function MarkdownContent({ text, streaming, onInAppLink, unverifiedLinks, sourceManifest, threadId }: { text: string; streaming?: boolean; onInAppLink?: (href: string) => void; unverifiedLinks?: ReadonlySet<string>; sourceManifest?: ReadonlyMap<number, { href: string; label: string }>; threadId?: string | null }) {
|
|
391
|
+
// Inline-citation pre-processor. The agent writes `[3]` markers in-prose;
|
|
392
|
+
// we resolve each to a markdown link `[3](href)` BEFORE react-markdown
|
|
393
|
+
// parses the string so the existing <a> renderer below picks it up with
|
|
394
|
+
// no special-casing. Markers whose number isn't in the manifest are
|
|
395
|
+
// left untouched (they'll show as plain `[3]` text) so an invented
|
|
396
|
+
// number is visibly un-linked rather than silently mis-routed.
|
|
397
|
+
const renderedText = useMemo(() => {
|
|
398
|
+
if (!sourceManifest || sourceManifest.size === 0) return text;
|
|
399
|
+
return text.replace(/(?<!\])\[(\d+)\]/g, (match, raw: string) => {
|
|
400
|
+
const entry = sourceManifest.get(parseInt(raw, 10));
|
|
401
|
+
return entry ? `[${raw}](${entry.href})` : match;
|
|
402
|
+
});
|
|
403
|
+
}, [text, sourceManifest]);
|
|
404
|
+
return (
|
|
405
|
+
<div className="prose prose-invert prose-sm max-w-none jarela-rich min-w-0">
|
|
406
|
+
<ReactMarkdown
|
|
407
|
+
remarkPlugins={MD_REMARK_PLUGINS}
|
|
408
|
+
rehypePlugins={MD_REHYPE_PLUGINS}
|
|
409
|
+
components={{
|
|
410
|
+
a({ href, children, ...rest }) {
|
|
411
|
+
const parsed = href ? parseHref(href) : undefined;
|
|
412
|
+
const isUnverified = !!(href && unverifiedLinks?.has(href));
|
|
413
|
+
// Detect citation markers: link text is literally `[N]` where N
|
|
414
|
+
// is a positive integer. These are rendered as small superscript
|
|
415
|
+
// chips (Wikipedia-style) so they're scannable as citations and
|
|
416
|
+
// distinct from ordinary inline links.
|
|
417
|
+
const isCitationMarker =
|
|
418
|
+
Array.isArray(children) && children.length === 1
|
|
419
|
+
? typeof children[0] === "string" && /^\[\d+\]$/.test(children[0])
|
|
420
|
+
: typeof children === "string" && /^\[\d+\]$/.test(children);
|
|
421
|
+
const citationText = isCitationMarker
|
|
422
|
+
? (Array.isArray(children) ? (children[0] as string) : (children as string))
|
|
423
|
+
: null;
|
|
424
|
+
// Inline audio: any link to a local /api/v1/files/*.{wav,mp3,ogg,webm,m4a}
|
|
425
|
+
// becomes a native <audio controls> player. The original anchor
|
|
426
|
+
// text is dropped — the player IS the answer. An `?autoplay=1`
|
|
427
|
+
// query string lets the agent (via generate_voice) ask the
|
|
428
|
+
// browser to start playback immediately.
|
|
429
|
+
if (href && /^\/api\/v1\/files\/[^?#]+\.(wav|mp3|ogg|webm|m4a)(\?|#|$)/i.test(href)) {
|
|
430
|
+
return <InlineAudio href={href} />;
|
|
431
|
+
}
|
|
432
|
+
const unverifiedCls = isUnverified ? "decoration-warn decoration-wavy underline-offset-2" : "";
|
|
433
|
+
const unverifiedTitle = isUnverified ? "Agent did not visit this source in this conversation — the citation may be invented." : undefined;
|
|
434
|
+
if (href && isLikelyLocalFileHref(href)) {
|
|
435
|
+
return (
|
|
436
|
+
<LocalFileLink href={href} threadId={threadId} title={unverifiedTitle ?? rest.title} className={`${rest.className ?? ""} ${unverifiedCls}`.trim()}>
|
|
437
|
+
{children}
|
|
438
|
+
{isUnverified && <sup className="text-warn ml-0.5" aria-label="unverified citation">⚠</sup>}
|
|
439
|
+
</LocalFileLink>
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
const inApp = !!parsed && !parsed.external && (!!parsed.tab || !!parsed.hash || (!!parsed.thread && !!parsed.agent));
|
|
443
|
+
// Citation-marker rendering: superscript chip with a tooltip
|
|
444
|
+
// showing the source label so the user can preview what the
|
|
445
|
+
// marker points to without clicking. The href is still active
|
|
446
|
+
// (jumps to anchor for #msg-… or opens external for URLs).
|
|
447
|
+
if (isCitationMarker && href) {
|
|
448
|
+
const markerNum = parseInt(citationText!.replace(/[\[\]]/g, ""), 10);
|
|
449
|
+
const entry = Number.isFinite(markerNum) ? sourceManifest?.get(markerNum) : undefined;
|
|
450
|
+
const title = entry?.label ?? unverifiedTitle ?? href;
|
|
451
|
+
const isAnchor = href.startsWith("#");
|
|
452
|
+
return (
|
|
453
|
+
<a
|
|
454
|
+
{...rest}
|
|
455
|
+
href={href}
|
|
456
|
+
target={isAnchor || inApp ? undefined : "_blank"}
|
|
457
|
+
rel={isAnchor || inApp ? undefined : "noopener noreferrer"}
|
|
458
|
+
title={title}
|
|
459
|
+
onClick={inApp && onInAppLink ? (e) => { e.preventDefault(); onInAppLink(href); } : undefined}
|
|
460
|
+
className="jarela-citation"
|
|
461
|
+
>
|
|
462
|
+
{citationText}
|
|
463
|
+
</a>
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
if (inApp && href && onInAppLink) {
|
|
467
|
+
return (
|
|
468
|
+
<a
|
|
469
|
+
{...rest}
|
|
470
|
+
href={href}
|
|
471
|
+
title={unverifiedTitle ?? rest.title}
|
|
472
|
+
className={`${rest.className ?? ""} ${unverifiedCls}`.trim()}
|
|
473
|
+
onClick={(e) => { e.preventDefault(); onInAppLink(href); }}
|
|
474
|
+
>
|
|
475
|
+
{children}
|
|
476
|
+
{isUnverified && <sup className="text-warn ml-0.5" aria-label="unverified citation">⚠</sup>}
|
|
477
|
+
</a>
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
return (
|
|
481
|
+
<a
|
|
482
|
+
{...rest}
|
|
483
|
+
href={href}
|
|
484
|
+
target="_blank"
|
|
485
|
+
rel="noopener noreferrer"
|
|
486
|
+
title={unverifiedTitle ?? rest.title}
|
|
487
|
+
className={`${rest.className ?? ""} ${unverifiedCls}`.trim()}
|
|
488
|
+
>
|
|
489
|
+
{children}
|
|
490
|
+
{isUnverified && <sup className="text-warn ml-0.5" aria-label="unverified citation">⚠</sup>}
|
|
491
|
+
</a>
|
|
492
|
+
);
|
|
493
|
+
},
|
|
494
|
+
pre({ children }) {
|
|
495
|
+
// CodeFence renders its own <pre>, so collapse react-markdown's
|
|
496
|
+
// outer wrapper to avoid <pre><pre>...</pre></pre>.
|
|
497
|
+
return <>{children}</>;
|
|
498
|
+
},
|
|
499
|
+
code({ className, children }) {
|
|
500
|
+
const match = /language-(\w+)/.exec(className ?? "");
|
|
501
|
+
if (match?.[1] === "map") {
|
|
502
|
+
return <MapEmbed payload={reactChildrenToText(children).replace(/\n$/, "")} />;
|
|
503
|
+
}
|
|
504
|
+
if (match?.[1] === "mermaid") {
|
|
505
|
+
return <MermaidFence source={reactChildrenToText(children).replace(/\n$/, "")} />;
|
|
506
|
+
}
|
|
507
|
+
return match ? (
|
|
508
|
+
<CodeFence language={match[1]} className={className ?? ""}>{children}</CodeFence>
|
|
509
|
+
) : (
|
|
510
|
+
<code className="bg-surface-2 px-1 rounded text-fg-muted break-all">{children}</code>
|
|
511
|
+
);
|
|
512
|
+
},
|
|
513
|
+
img({ src, alt }) {
|
|
514
|
+
return <ResilientMarkdownImage src={src} alt={alt} />;
|
|
515
|
+
},
|
|
516
|
+
}}
|
|
517
|
+
>
|
|
518
|
+
{renderedText}
|
|
519
|
+
</ReactMarkdown>
|
|
520
|
+
{streaming && <span className="jarela-stream-caret" aria-hidden="true" />}
|
|
521
|
+
</div>
|
|
522
|
+
);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
function CodeFence({ language, className, children }: { language: string; className: string; children: ReactNode }) {
|
|
527
|
+
const [copied, setCopied] = useState(false);
|
|
528
|
+
const codeText = useMemo(() => reactChildrenToText(children).replace(/\n$/, ""), [children]);
|
|
529
|
+
|
|
530
|
+
const copyCode = useCallback(() => {
|
|
531
|
+
void navigator.clipboard.writeText(codeText).then(() => {
|
|
532
|
+
setCopied(true);
|
|
533
|
+
setTimeout(() => setCopied(false), 1200);
|
|
534
|
+
}).catch(console.error);
|
|
535
|
+
}, [codeText]);
|
|
536
|
+
|
|
537
|
+
// rehype-highlight already populated `className` with `hljs language-X`
|
|
538
|
+
// plus per-token spans inside `children`. The `hljs` class drives styling
|
|
539
|
+
// from the imported github-dark theme.
|
|
540
|
+
return (
|
|
541
|
+
<div className="relative my-2 rounded-md overflow-hidden border border-border/60">
|
|
542
|
+
<div className="flex items-center justify-between px-2 py-1 text-[10px] bg-surface-3 text-fg-faint uppercase tracking-wide">
|
|
543
|
+
<span>{language}</span>
|
|
544
|
+
<button
|
|
545
|
+
onClick={copyCode}
|
|
546
|
+
className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded border border-border/60 bg-surface-2 text-fg-muted hover:text-fg transition-colors"
|
|
547
|
+
title="Copy code"
|
|
548
|
+
aria-label="Copy code"
|
|
549
|
+
>
|
|
550
|
+
{copied ? <Check size={10} /> : <Copy size={10} />}
|
|
551
|
+
<span>{copied ? "Copied" : "Copy"}</span>
|
|
552
|
+
</button>
|
|
553
|
+
</div>
|
|
554
|
+
<pre className="m-0 p-3 text-xs leading-relaxed overflow-x-auto bg-[#0d1117]">
|
|
555
|
+
<code className={className}>{children}</code>
|
|
556
|
+
</pre>
|
|
557
|
+
</div>
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
function MermaidFence({ source }: { source: string }) {
|
|
563
|
+
const renderId = `mermaid-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
|
|
564
|
+
const [svg, setSvg] = useState<string | null>(null);
|
|
565
|
+
const [error, setError] = useState<string | null>(null);
|
|
566
|
+
|
|
567
|
+
useEffect(() => {
|
|
568
|
+
let cancelled = false;
|
|
569
|
+
setSvg(null);
|
|
570
|
+
setError(null);
|
|
571
|
+
void (async () => {
|
|
572
|
+
try {
|
|
573
|
+
const [{ default: mermaid }, { default: DOMPurify }] = await Promise.all([
|
|
574
|
+
import("mermaid"),
|
|
575
|
+
import("dompurify"),
|
|
576
|
+
]);
|
|
577
|
+
mermaid.initialize({ startOnLoad: false, securityLevel: "strict", theme: "base" });
|
|
578
|
+
const result = await mermaid.render(renderId, source);
|
|
579
|
+
if (cancelled) return;
|
|
580
|
+
setSvg(DOMPurify.sanitize(result.svg, { USE_PROFILES: { svg: true } }));
|
|
581
|
+
} catch (err) {
|
|
582
|
+
if (!cancelled) setError(err instanceof Error ? err.message : String(err));
|
|
583
|
+
}
|
|
584
|
+
})();
|
|
585
|
+
return () => { cancelled = true; };
|
|
586
|
+
}, [renderId, source]);
|
|
587
|
+
|
|
588
|
+
if (error) {
|
|
589
|
+
return (
|
|
590
|
+
<div className="my-2 rounded-md border border-warn/40 bg-warn/5 p-2">
|
|
591
|
+
<p className="mb-1 text-[10px] uppercase tracking-wide text-warn">Mermaid preview unavailable</p>
|
|
592
|
+
<CodeFence language="mermaid" className="language-mermaid"><>{source}</></CodeFence>
|
|
593
|
+
</div>
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
if (!svg) {
|
|
597
|
+
return <div className="my-2 rounded-md border border-border/60 bg-surface-3/40 p-3 text-xs text-fg-faint">Rendering diagram...</div>;
|
|
598
|
+
}
|
|
599
|
+
return (
|
|
600
|
+
<div
|
|
601
|
+
className="my-2 max-w-full overflow-x-auto rounded-md border border-border/60 bg-surface-2 p-3"
|
|
602
|
+
dangerouslySetInnerHTML={{ __html: svg }}
|
|
603
|
+
role="img"
|
|
604
|
+
aria-label="Mermaid diagram"
|
|
605
|
+
/>
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
export function ContentPartView({ part, isUser, onInAppLink, unverifiedLinks, sourceManifest, threadId }: { part: ContentPart; isUser: boolean; onInAppLink?: (href: string) => void; unverifiedLinks?: ReadonlySet<string>; sourceManifest?: ReadonlyMap<number, { href: string; label: string }>; threadId?: string | null }) {
|
|
611
|
+
if (part.type === "text") {
|
|
612
|
+
return <MarkdownContent text={part.text} onInAppLink={onInAppLink} unverifiedLinks={unverifiedLinks} sourceManifest={sourceManifest} threadId={threadId} />;
|
|
613
|
+
}
|
|
614
|
+
if (part.type === "image") {
|
|
615
|
+
return <ClickableImage src={`data:${part.media_type};base64,${part.data}`} />;
|
|
616
|
+
}
|
|
617
|
+
if (part.type === "image_ref") {
|
|
618
|
+
// Ref-based image — server-hosted at /api/v1/files/[name]. See ADR-0065.
|
|
619
|
+
return <ClickableImage src={`/api/v1/files/${encodeURIComponent(part.name)}`} />;
|
|
620
|
+
}
|
|
621
|
+
if (part.type === "file_ref") {
|
|
622
|
+
return <AttachmentLink href={`/api/v1/files/${encodeURIComponent(part.name)}`} filename={part.filename} />;
|
|
623
|
+
}
|
|
624
|
+
// File attachment — render an inline player for audio/video so voice
|
|
625
|
+
// notes and short clips from the WhatsApp bridge are usable in-bubble.
|
|
626
|
+
// Other file types still fall through to a download/open-in-new-tab link.
|
|
627
|
+
const file = part;
|
|
628
|
+
const dataUrl = `data:${file.media_type};base64,${file.data}`;
|
|
629
|
+
// For text files our data field is plain text, not base64. Detect and wrap.
|
|
630
|
+
const href = /^text\/|^application\/json$/.test(file.media_type)
|
|
631
|
+
? `data:${file.media_type};charset=utf-8,${encodeURIComponent(file.data)}`
|
|
632
|
+
: dataUrl;
|
|
633
|
+
if (file.media_type.startsWith("audio/")) {
|
|
634
|
+
return (
|
|
635
|
+
<div className="mt-1 max-w-md">
|
|
636
|
+
{/* Native controls keep play/pause/seek consistent with the OS;
|
|
637
|
+
the filename hint below disambiguates voice-note vs file. */}
|
|
638
|
+
<audio controls className="w-full" src={dataUrl} />
|
|
639
|
+
<div className="mt-0.5 text-[10px] text-fg-faint truncate">{file.name}</div>
|
|
640
|
+
</div>
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
if (file.media_type.startsWith("video/")) {
|
|
644
|
+
return (
|
|
645
|
+
<div className="mt-1 max-w-md">
|
|
646
|
+
<video
|
|
647
|
+
controls
|
|
648
|
+
playsInline
|
|
649
|
+
className="w-full rounded-xl border border-border/40"
|
|
650
|
+
style={{ maxHeight: "400px" }}
|
|
651
|
+
src={dataUrl}
|
|
652
|
+
/>
|
|
653
|
+
<div className="mt-0.5 text-[10px] text-fg-faint truncate">{file.name}</div>
|
|
654
|
+
</div>
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
return <AttachmentLink href={href} filename={file.name} />;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function AttachmentLink({ href, filename }: { href: string; filename: string }) {
|
|
661
|
+
return (
|
|
662
|
+
<a
|
|
663
|
+
href={href}
|
|
664
|
+
target="_blank"
|
|
665
|
+
rel="noopener noreferrer"
|
|
666
|
+
className="inline-flex items-center gap-1.5 px-2.5 py-1.5 mt-1 rounded-lg border border-border/60 bg-surface-3/60 hover:bg-surface-3 hover:border-border text-[11px] text-fg-muted transition-colors"
|
|
667
|
+
title="Open in new tab"
|
|
668
|
+
>
|
|
669
|
+
<Paperclip size={11} className="text-fg-faint shrink-0" />
|
|
670
|
+
<span className="truncate max-w-[200px]">{filename}</span>
|
|
671
|
+
</a>
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
// Image attachment — thumbnail in the bubble, click for a full-screen lightbox.
|
|
677
|
+
// Accepts either a `data:` URL (inline base64) or a `/api/v1/files/[name]`
|
|
678
|
+
// URL (disk-hosted ref, see ADR-0065). Both render identically.
|
|
679
|
+
function ClickableImage({ src }: { src: string }) {
|
|
680
|
+
const [open, setOpen] = useState(false);
|
|
681
|
+
const [naturalSize, setNaturalSize] = useState<{ width: number; height: number } | null>(null);
|
|
682
|
+
return (
|
|
683
|
+
<>
|
|
684
|
+
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
685
|
+
<img
|
|
686
|
+
src={src}
|
|
687
|
+
alt="attached image"
|
|
688
|
+
onLoad={(e) => {
|
|
689
|
+
const img = e.currentTarget;
|
|
690
|
+
if (img.naturalWidth > 0 && img.naturalHeight > 0) {
|
|
691
|
+
setNaturalSize({ width: img.naturalWidth, height: img.naturalHeight });
|
|
692
|
+
}
|
|
693
|
+
}}
|
|
694
|
+
onClick={() => setOpen(true)}
|
|
695
|
+
className="block w-full max-w-full rounded-xl mt-1 border border-border/40 cursor-zoom-in hover:border-fg-faint transition-colors"
|
|
696
|
+
style={{
|
|
697
|
+
width: naturalSize ? `min(100%, ${naturalSize.width}px)` : "100%",
|
|
698
|
+
maxHeight: naturalSize ? `min(82dvh, ${naturalSize.height}px)` : "82dvh",
|
|
699
|
+
objectFit: "contain",
|
|
700
|
+
}}
|
|
701
|
+
/>
|
|
702
|
+
<ImagePreviewDialog src={src} alt="attached image" open={open} onClose={() => setOpen(false)} />
|
|
703
|
+
</>
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
function ImagePreviewDialog({ src, alt, open, onClose }: { src: string; alt: string; open: boolean; onClose: () => void }) {
|
|
709
|
+
return (
|
|
710
|
+
<Dialog
|
|
711
|
+
open={open}
|
|
712
|
+
onClose={onClose}
|
|
713
|
+
title="Image preview"
|
|
714
|
+
size="full"
|
|
715
|
+
align="center"
|
|
716
|
+
level="elevated"
|
|
717
|
+
padded={false}
|
|
718
|
+
fitViewport
|
|
719
|
+
footer={(
|
|
720
|
+
<div className="flex items-center justify-end gap-2 px-4 pb-3">
|
|
721
|
+
<a
|
|
722
|
+
href={src}
|
|
723
|
+
target="_blank"
|
|
724
|
+
rel="noopener noreferrer"
|
|
725
|
+
className="px-2.5 py-1.5 rounded-md border border-border/70 bg-surface-3 text-xs text-fg-muted hover:text-fg hover:border-border"
|
|
726
|
+
>
|
|
727
|
+
Open in new tab
|
|
728
|
+
</a>
|
|
729
|
+
<button
|
|
730
|
+
type="button"
|
|
731
|
+
onClick={onClose}
|
|
732
|
+
className="px-2.5 py-1.5 rounded-md border border-border/70 bg-surface-3 text-xs text-fg-muted hover:text-fg hover:border-border"
|
|
733
|
+
>
|
|
734
|
+
Close
|
|
735
|
+
</button>
|
|
736
|
+
</div>
|
|
737
|
+
)}
|
|
738
|
+
>
|
|
739
|
+
<div className="h-full w-full min-w-0 bg-black/95 flex items-center justify-center">
|
|
740
|
+
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
741
|
+
<img
|
|
742
|
+
src={src}
|
|
743
|
+
alt={alt}
|
|
744
|
+
className="block max-h-full max-w-full h-auto w-auto object-contain"
|
|
745
|
+
/>
|
|
746
|
+
</div>
|
|
747
|
+
</Dialog>
|
|
748
|
+
);
|
|
749
|
+
}
|