@springbrand/message-panel 0.1.3-alpha.2 → 0.1.3-alpha.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/cloud-os/README.md +6 -6
  2. package/cloud-os/assets/followup-arrow.svg +3 -0
  3. package/cloud-os/assets/loading-corner.svg +3 -0
  4. package/cloud-os/assets/loading-mark.svg +16 -0
  5. package/cloud-os/assets/loading-spark.svg +3 -0
  6. package/cloud-os/assets/model-selected.svg +5 -0
  7. package/cloud-os/capability-chip.tsx +35 -0
  8. package/cloud-os/chat/activity-indicator.tsx +29 -0
  9. package/cloud-os/chat/cloud-os-chat-messages.tsx +130 -70
  10. package/cloud-os/chat/markdown-message.tsx +85 -40
  11. package/cloud-os/chat/rich-blocks.tsx +494 -190
  12. package/cloud-os/chat/tool-presentation.ts +67 -6
  13. package/cloud-os/chat/tool-rows.tsx +87 -43
  14. package/cloud-os/chat/transcript-model.ts +215 -23
  15. package/cloud-os/composer/cloud-os-chat-input.tsx +242 -127
  16. package/cloud-os/composer/cloud-os-model-select.tsx +105 -0
  17. package/cloud-os/file-view.tsx +266 -0
  18. package/cloud-os/index.ts +29 -2
  19. package/cloud-os/layout/cloud-os-workspace-split.tsx +107 -24
  20. package/cloud-os/primitives/workshop-controls.tsx +2 -2
  21. package/cloud-os/styles/cloud-os.css +93 -19
  22. package/demo/camel-chat-showcase.tsx +21 -13
  23. package/demo/chat-scenarios.ts +100 -40
  24. package/demo/cloud-os-chat-showcase.tsx +51 -17
  25. package/demo/fixtures.ts +4 -5
  26. package/demo/message-panel-gallery.tsx +16 -2
  27. package/package.json +3 -4
  28. package/src/camel/camel-chat-messages.tsx +82 -77
  29. package/src/camel/camel-prompt-input.tsx +2 -1
  30. package/src/camel/camel-tool-presentation.tsx +19 -15
  31. package/src/camel/camel-turn.ts +1 -17
  32. package/src/chat-summary-panel.tsx +1 -1
  33. package/src/composer/chat-composer.tsx +2 -1
  34. package/src/composer/composer-trigger-popover.tsx +1 -1
  35. package/src/composer/composer.tsx +2 -1
  36. package/src/composer/index.ts +1 -0
  37. package/src/composer/key-rules.ts +12 -0
  38. package/src/contracts.ts +0 -2
  39. package/src/message-panel.tsx +0 -23
  40. package/src/parts/index.tsx +102 -63
  41. package/src/styles/index.css +4 -1
@@ -1,14 +1,14 @@
1
1
  import { memo, useMemo, type ReactNode } from "react";
2
- import ReactMarkdown, { type Components } from "react-markdown";
3
- import remarkGfm from "remark-gfm";
2
+ import {
3
+ defaultRehypePlugins,
4
+ Streamdown,
5
+ type Components,
6
+ type StreamdownProps,
7
+ } from "streamdown";
4
8
 
5
9
  /**
6
- * cloudflare-os-main 的 `MarkdownMessage` 拷来。
7
- * 删掉的部分:capsule / format 提及的 remark 插件 —— 那是 gadgets 的资源体系,
8
- * UIMessage 协议里没有对应概念,留着只会是死代码。
9
- * 保留的部分:skipHtml、remark-gfm、表格外包一层横向滚动、外链白名单 + target=_blank。
10
- * 加上的部分:resolveUrl —— 本仓的消息里会出现 `sandbox:/workspace/...` 这种
11
- * 只有宿主知道怎么翻译成可访问 URL 的地址,原来由 camel 的同名钩子负责。
10
+ * Streamdown 依然使用原生标签,避免它的默认 Tailwind 组件样式覆盖 cloud-os CSS。
11
+ * resolveUrl 负责把 `sandbox:/workspace/...` 翻译成可访问 URL。
12
12
  */
13
13
 
14
14
  export type CloudOsUrlResolver = (url: string) => string | null | undefined;
@@ -25,54 +25,99 @@ export function safeExternalUrl(href: string | undefined): string | undefined {
25
25
  }
26
26
  }
27
27
 
28
- function buildComponents(resolveUrl?: CloudOsUrlResolver): Components {
29
- const resolve = (url: string | undefined): string | undefined => {
30
- if (!url) return undefined;
31
- return resolveUrl?.(url) ?? url;
32
- };
33
- return {
34
- table: ({ node: _node, children, ...props }) => (
35
- <div style={{ maxWidth: "100%", overflowX: "auto" }}>
36
- <table {...props}>{children}</table>
37
- </div>
38
- ),
39
- img: ({ node: _node, src, ...props }) => {
40
- const resolved = resolve(typeof src === "string" ? src : undefined);
41
- return resolved ? <img {...props} src={resolved} /> : null;
42
- },
43
- a: ({ node: _node, href, children, ...props }) => {
44
- const safeHref = safeExternalUrl(resolve(href));
45
- if (!safeHref) return <>{children}</>;
46
- return (
47
- <a {...props} href={safeHref} target="_blank" rel="noopener noreferrer">
48
- {children}
49
- </a>
50
- );
51
- },
28
+ const STREAMDOWN_REHYPE_PLUGINS = Object.values(
29
+ defaultRehypePlugins,
30
+ ) as NonNullable<StreamdownProps["rehypePlugins"]>;
31
+
32
+ type UrlNode = {
33
+ type?: string;
34
+ properties?: Record<string, unknown>;
35
+ children?: UrlNode[];
36
+ };
37
+
38
+ function resolveUrlsPlugin(resolveUrl: CloudOsUrlResolver) {
39
+ return () => (tree: UrlNode) => {
40
+ const visit = (node: UrlNode): void => {
41
+ if (node.type === "element" && node.properties) {
42
+ for (const attribute of ["href", "src"]) {
43
+ const url = node.properties[attribute];
44
+ if (typeof url !== "string") continue;
45
+ const resolved = resolveUrl(url);
46
+ if (resolved === null) delete node.properties[attribute];
47
+ else if (resolved !== undefined) node.properties[attribute] = resolved;
48
+ }
49
+ }
50
+ node.children?.forEach(visit);
51
+ };
52
+ visit(tree);
52
53
  };
53
54
  }
54
55
 
55
- const REMARK_PLUGINS = [remarkGfm];
56
- const DEFAULT_COMPONENTS = buildComponents();
56
+ const DEFAULT_COMPONENTS: Components = {
57
+ blockquote: "blockquote",
58
+ code: "code",
59
+ h1: "h1",
60
+ h2: "h2",
61
+ h3: "h3",
62
+ h4: "h4",
63
+ h5: "h5",
64
+ h6: "h6",
65
+ hr: "hr",
66
+ li: "li",
67
+ ol: "ol",
68
+ p: "p",
69
+ pre: "pre",
70
+ section: "section",
71
+ strong: "strong",
72
+ sub: "sub",
73
+ sup: "sup",
74
+ table: "table",
75
+ tbody: "tbody",
76
+ td: "td",
77
+ th: "th",
78
+ thead: "thead",
79
+ tr: "tr",
80
+ ul: "ul",
81
+ img: ({ node: _node, src, ...props }) =>
82
+ src ? <img {...props} src={src} /> : null,
83
+ a: ({ node: _node, href, children, ...props }) => {
84
+ const safeHref = safeExternalUrl(href);
85
+ if (!safeHref) return <>{children}</>;
86
+ return (
87
+ <a {...props} href={safeHref} target="_blank" rel="noopener noreferrer">
88
+ {children}
89
+ </a>
90
+ );
91
+ },
92
+ };
57
93
 
58
94
  export const MarkdownMessage = memo(function MarkdownMessage({
59
95
  message,
60
96
  resolveUrl,
97
+ streaming = false,
61
98
  }: {
62
99
  message: string;
63
100
  resolveUrl?: CloudOsUrlResolver;
101
+ streaming?: boolean;
64
102
  }): ReactNode {
65
- const components = useMemo(
66
- () => (resolveUrl ? buildComponents(resolveUrl) : DEFAULT_COMPONENTS),
103
+ const rehypePlugins = useMemo(
104
+ () =>
105
+ resolveUrl
106
+ ? [resolveUrlsPlugin(resolveUrl), ...STREAMDOWN_REHYPE_PLUGINS]
107
+ : STREAMDOWN_REHYPE_PLUGINS,
67
108
  [resolveUrl],
68
109
  );
69
110
  return (
70
- <ReactMarkdown
111
+ <Streamdown
112
+ className="space-y-0"
113
+ controls={false}
114
+ mode={streaming ? "streaming" : "static"}
115
+ isAnimating={streaming}
116
+ rehypePlugins={rehypePlugins}
71
117
  skipHtml
72
- remarkPlugins={REMARK_PLUGINS}
73
- components={components}
118
+ components={DEFAULT_COMPONENTS}
74
119
  >
75
120
  {message}
76
- </ReactMarkdown>
121
+ </Streamdown>
77
122
  );
78
123
  });