Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix: present model-designed HTML as continuous answer content
Chris3774d11c4d11
diff --git a/apps/desktop/src/renderer/components/chat/HtmlAnswerPreview.tsx b/apps/desktop/src/renderer/components/chat/HtmlAnswerPreview.tsxindex a5a3bc17e8..c7656ab74d 100644--- a/apps/desktop/src/renderer/components/chat/HtmlAnswerPreview.tsx+++ b/apps/desktop/src/renderer/components/chat/HtmlAnswerPreview.tsx@@ -1,7 +1,6 @@import { useEffect, useMemo, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';-import { Code2, Eye } from 'lucide-react';-import { htmlAnswerHeight, prepareHtmlAnswer } from '@cindy/maker-shared/html-answer';+import { HTML_ANSWER_MAX_HEIGHT, htmlAnswerHeight, prepareHtmlAnswer } from '@cindy/maker-shared/html-answer';import { Button } from '@/components/ui/button';function hostTheme() {@@ -18,8 +17,7 @@ export function HtmlAnswerPreview({ html }: { html: string }) {const { t } = useTranslation();const frame = useRef<HTMLIFrameElement>(null);const [theme, setTheme] = useState(hostTheme);- const [source, setSource] = useState(false);- const [height, setHeight] = useState(320);+ const [height, setHeight] = useState(1);const [url, setUrl] = useState<string>();const [failed, setFailed] = useState(false);const [attempt, setAttempt] = useState(0);@@ -42,7 +40,6 @@ export function HtmlAnswerPreview({ html }: { html: string }) {return () => observer.disconnect();}, []);useEffect(() => {- if (source) return;let disposed = false;let viewId: string | undefined;const release = () => {@@ -50,7 +47,7 @@ export function HtmlAnswerPreview({ html }: { html: string }) {};setUrl(undefined);setFailed(false);- setHeight(320);+ setHeight(1);void window.electronAPI.htmlAnswerPreview({ html: documentHtml }).then((view) => {@@ -66,7 +63,7 @@ export function HtmlAnswerPreview({ html }: { html: string }) {disposed = true;release();};- }, [documentHtml, source, attempt]);+ }, [documentHtml, attempt]);useEffect(() => {const receive = (event: MessageEvent) => {if (!frame.current || event.source !== frame.current.contentWindow) return;@@ -76,36 +73,20 @@ export function HtmlAnswerPreview({ html }: { html: string }) {window.addEventListener('message', receive);return () => window.removeEventListener('message', receive);}, []);- const showSource = source || failed;return (- <div className="my-3 min-w-0" data-html-answer="true">- <div className="flex justify-end pb-1">- <Button- variant="secondary"- tone="quiet"- size="sm"- compact- title={t(showSource ? 'chat.htmlAnswer.preview' : 'chat.htmlAnswer.source')}- aria-label={t(showSource ? 'chat.htmlAnswer.preview' : 'chat.htmlAnswer.source')}- onClick={() => {- if (failed) setAttempt((value) => value + 1);- setFailed(false);- setSource(!showSource);- }}- >- {showSource ? <Eye size={14} /> : <Code2 size={14} />}+ <div className="min-w-0" data-html-answer="true">+ {failed ? (+ <Button variant="secondary" tone="quiet" size="sm"+ onClick={() => setAttempt(value => value + 1)}>+ {t('commonUi.retry')}</Button>- </div>- {showSource ? (- <pre className="overflow-auto whitespace-pre-wrap break-words rounded-md bg-[var(--msg-tool-card-bg)] p-3">- <code>{html}</code>- </pre>) : url ? (<iframeref={frame}src={url}title="HTML"sandbox="allow-scripts"+ scrolling={height >= HTML_ANSWER_MAX_HEIGHT ? 'auto' : 'no'}className="block w-full border-0"style={{ height }}/>diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.jsonindex 507bb8bf0a..9f63cc937e 100644--- a/apps/desktop/src/renderer/i18n/locales/en/common.json+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json@@ -11312,10 +11312,6 @@"selectionCount_one": "{{count}} selection","selectionCount_other": "{{count}} selections"},- "htmlAnswer": {- "preview": "Preview",- "source": "View source"- },"mermaid": {"clickToZoom": "Click to zoom","zoom": "Zoom",diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.jsonindex 5c9f3305c6..bcb7be84ac 100644--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json@@ -11268,10 +11268,6 @@"selectionCount_one": "{{count}} 件の引用","selectionCount_other": "{{count}} 件の引用"},- "htmlAnswer": {- "preview": "プレビュー",- "source": "ソースを表示"- },"mermaid": {"clickToZoom": "クリックで拡大","zoom": "拡大",diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.jsonindex 9a552e6c5e..b810af0d08 100644--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json@@ -11268,10 +11268,6 @@"selectionCount_one": "인용 {{count}}개","selectionCount_other": "인용 {{count}}개"},- "htmlAnswer": {- "preview": "미리 보기",- "source": "소스 보기"- },"mermaid": {"clickToZoom": "클릭하여 확대","zoom": "확대",diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.jsonindex 90baf5ab14..c049583ddd 100644--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json@@ -11268,10 +11268,6 @@"selectionCount_one": "{{count}} 处引用","selectionCount_other": "{{count}} 处引用"},- "htmlAnswer": {- "preview": "预览",- "source": "查看源码"- },"mermaid": {"clickToZoom": "点击放大","zoom": "放大",diff --git a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.jsonindex d663fa58e5..d7c89f329b 100644--- a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json+++ b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json@@ -11267,10 +11267,6 @@"selectionCount_one": "{{count}} 處引用","selectionCount_other": "{{count}} 處引用"},- "htmlAnswer": {- "preview": "預覽",- "source": "查看原始碼"- },"mermaid": {"clickToZoom": "點選放大","zoom": "放大",diff --git a/apps/mobile/src/session/HtmlAnswerPreview.tsx b/apps/mobile/src/session/HtmlAnswerPreview.tsxindex fe4c492a33..f823155f6e 100644--- a/apps/mobile/src/session/HtmlAnswerPreview.tsx+++ b/apps/mobile/src/session/HtmlAnswerPreview.tsx@@ -1,9 +1,7 @@import { useMemo, useState } from "react";-import { Pressable, View } from "react-native";-import { useTranslation } from "react-i18next";-import { Text } from "@/components/AppText";-import { useTheme, textStyles } from "@/theme";-import { prepareHtmlAnswer } from "@cindy/maker-shared/html-answer";+import { View } from "react-native";+import { useTheme } from "@/theme";+import { HTML_ANSWER_MAX_HEIGHT, prepareHtmlAnswer } from "@cindy/maker-shared/html-answer";import { HtmlFileReader } from "./HtmlFileReader";export function HtmlAnswerPreview({@@ -14,9 +12,7 @@ export function HtmlAnswerPreview({active?: boolean;}) {const { colors, mode } = useTheme();- const { t } = useTranslation();- const [source, setSource] = useState(false);- const [height, setHeight] = useState(320);+ const [height, setHeight] = useState(1);const documentHtml = useMemo(() =>prepareHtmlAnswer(html, {@@ -27,39 +23,11 @@ export function HtmlAnswerPreview({[html, colors.surface, colors.textPrimary, mode],);return (- <View testID="message.htmlAnswer" style={{ alignSelf: "stretch" }}>- <Pressable- accessibilityRole="button"- onPress={() => setSource(!source)}- style={{- alignSelf: "flex-end",- minHeight: 44,- justifyContent: "center",- paddingHorizontal: 8,- }}- >- <Text style={[textStyles.bodySmall, { color: colors.textSecondary }]}>- {t(- source- ? "files.preview.browserShowPage"- : "files.preview.browserShowSource",- )}- </Text>- </Pressable>- {source ? (- <Text- selectable- style={[textStyles.bodySmall, { color: colors.textPrimary }]}- >- {html}- </Text>- ) : (- <View style={{ height }}>- {active ? (- <HtmlFileReader html={documentHtml} onContentHeight={setHeight} />- ) : null}- </View>- )}+ <View testID="message.htmlAnswer" style={{ alignSelf: "stretch", height }}>+ {active ? (+ <HtmlFileReader html={documentHtml} onContentHeight={setHeight}+ scrollEnabled={height >= HTML_ANSWER_MAX_HEIGHT} />+ ) : null}</View>);}diff --git a/apps/mobile/src/session/HtmlFileReader.tsx b/apps/mobile/src/session/HtmlFileReader.tsxindex 1b30c923f8..f1240e28ff 100644--- a/apps/mobile/src/session/HtmlFileReader.tsx+++ b/apps/mobile/src/session/HtmlFileReader.tsx@@ -131,8 +131,8 @@ export function HtmlSnapshotReader({ preview, onError, webViewRef, viewportInset/> : null}{(!preview || paintedPreview !== preview) && loading ? <View key="loading" style={StyleSheet.absoluteFill} pointerEvents="none">{loading}</View> : null}</View></View>;}-export function HtmlFileReader({ html, testID, onContentHeight }: {- html: string; testID?: string; onContentHeight?: (height: number) => void;+export function HtmlFileReader({ html, testID, onContentHeight, scrollEnabled = true }: {+ html: string; testID?: string; onContentHeight?: (height: number) => void; scrollEnabled?: boolean;}) {// CSP 注入放在**渲染载体这一层**,而不是取件那一层:任何进到这个 WebView 的// HTML 都必须带策略,不管它有没有同目录资源(见 htmlPreviewCsp 的说明)。@@ -160,7 +160,7 @@ export function HtmlFileReader({ html, testID, onContentHeight }: {onLoadEnd={() => { documentSettledRef.current = true; }}// 见头注:收窄会让 RNW 在回调前把非白名单 URL 交给 Linking,绕过下面的策略。originWhitelist={['*']}- scrollEnabled+ scrollEnabled={scrollEnabled}// Android:关掉多窗口(review P1)。留着默认支持时,`window.open(...)` 与// `target="_blank"` 会走 onCreateWindow 而**不经过** onShouldStartLoadWithRequest,// 于是程序化打开 https 或自定义 scheme 能整个绕过上面的 click 门与 scheme 拒绝。diff --git a/packages/maker-shared/src/__tests__/htmlAnswer.test.ts b/packages/maker-shared/src/__tests__/htmlAnswer.test.tsindex 5a422efc5f..5b65c99a16 100644--- a/packages/maker-shared/src/__tests__/htmlAnswer.test.ts+++ b/packages/maker-shared/src/__tests__/htmlAnswer.test.ts@@ -1,5 +1,6 @@import { describe, expect, it } from "vitest";import {+ HTML_ANSWER_MAX_HEIGHT,htmlAnswerDocument,htmlAnswerHeight,prepareHtmlAnswer,@@ -18,9 +19,10 @@ describe("HTML answer presentation", () => {expect(htmlAnswerHeight({ type: "cindy-html-answer-size", height: 500 }),).toBe(500);+ expect(htmlAnswerHeight({ type: "cindy-html-answer-size", height: 4800 })).toBe(4800);expect(htmlAnswerHeight({ type: "cindy-html-answer-size", height: 1e9 }),- ).toBe(1200);+ ).toBe(HTML_ANSWER_MAX_HEIGHT);expect(htmlAnswerHeight({ type: "cindy-html-answer-size", height: NaN }),).toBeNull();diff --git a/packages/maker-shared/src/htmlAnswer.ts b/packages/maker-shared/src/htmlAnswer.tsindex 638419f9a1..e2f8190cce 100644--- a/packages/maker-shared/src/htmlAnswer.ts+++ b/packages/maker-shared/src/htmlAnswer.ts@@ -10,6 +10,9 @@ export function htmlAnswerDocument(text: string): string | null {: null;}+// Bound pathological documents without making ordinary long answers a nested scroller.+export const HTML_ANSWER_MAX_HEIGHT = 100_000;+export function htmlAnswerHeight(value: unknown): number | null {if (!value || typeof value !== "object") return null;const data = value as { type?: unknown; height?: unknown };@@ -17,7 +20,7 @@ export function htmlAnswerHeight(value: unknown): number | null {typeof data.height === "number" &&Number.isFinite(data.height) &&data.height > 0- ? Math.max(160, Math.min(1200, Math.ceil(data.height)))+ ? Math.max(1, Math.min(HTML_ANSWER_MAX_HEIGHT, Math.ceil(data.height))): null;}@@ -29,6 +32,7 @@ export function prepareHtmlAnswer(const encodedTheme = JSON.stringify(theme).replace(/</g, "\\u003c");return `<meta name="viewport" content="width=device-width, initial-scale=1"><style>:root { color-scheme: ${theme.dark ? "dark" : "light"}; }+ html, body { min-height: 0 !important; }body { margin: 0; font: 16px/1.6 system-ui, sans-serif; background: var(--cindy-answer-bg); color: var(--cindy-answer-fg); overflow-wrap: anywhere; }img,video,svg,canvas { max-width: 100%; } * { box-sizing: border-box; }</style><script>(()=>{@@ -38,7 +42,8 @@ export function prepareHtmlAnswer(addEventListener('DOMContentLoaded',()=>{let previous=0;const measure=()=>{- const height=Math.ceil(document.body.getBoundingClientRect().height);+ const body=document.body,style=getComputedStyle(body);+ const height=Math.max(1,Math.ceil(Math.max(body.scrollHeight,body.getBoundingClientRect().height)+(parseFloat(style.marginTop)||0)+(parseFloat(style.marginBottom)||0)));if(height===previous)return;previous=height;const data={type:'cindy-html-answer-size',height};if(window.ReactNativeWebView)window.ReactNativeWebView.postMessage(JSON.stringify(data));