@langgraph-js/sdk 4.3.0 → 4.3.1
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/package.json
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# ArtifactViewer 实现说明
|
|
2
|
+
|
|
3
|
+
`ArtifactViewer` 组件主要负责与 `langgraph-js` SDK 的状态同步,并集成 `ai-artifacts` Web Component 来展示 AI 生成的内容。
|
|
4
|
+
|
|
5
|
+
## 核心功能
|
|
6
|
+
|
|
7
|
+
1. **状态同步**: 监听 `useChat` hook 中的 `artifacts` 变化。
|
|
8
|
+
2. **沙箱渲染**: 使用 `<ai-artifacts>` Web Component 加载远程渲染引擎。
|
|
9
|
+
3. **交互反馈**: 通过 `eventCenter` 捕获沙箱内的 `sendBackToAI` 事件,支持 Human-in-the-loop 流程。
|
|
10
|
+
|
|
11
|
+
## 核心实现步骤
|
|
12
|
+
|
|
13
|
+
### 1. 同步 Artifacts 状态
|
|
14
|
+
|
|
15
|
+
利用 `useEffect` 监听 SDK 的 `artifacts` 和 `loading` 状态。当数据更新且加载完成后,使用 `setArtifactStore` 将数据推送到 `ai-artifacts` 的全局存储中。
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
if (loading) return;
|
|
20
|
+
setArtifactStore({
|
|
21
|
+
artifacts: { default: artifacts as ArtifactType[] },
|
|
22
|
+
});
|
|
23
|
+
}, [artifacts, loading]);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### 2. 集成渲染引擎 (Web Component)
|
|
27
|
+
|
|
28
|
+
在 JSX 中使用 `ai-artifacts` 标签。需要提供 `src` (渲染引擎地址)、`store-id` (默认为 `default`) 以及当前选中的 `group-id` 和 `version-id`。
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
<ai-artifacts src={artifactsUrl} store-id="default" group-id={currentArtifactId?.[0] || ""} version-id={currentArtifactId?.[1] || ""} className="w-full min-h-[400px]"></ai-artifacts>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 3. 事件处理 (Human-in-the-loop)
|
|
35
|
+
|
|
36
|
+
监听来自渲染沙箱的 `sendBackToAI` 事件。这通常发生在用户点击“修复”或“修改”按钮时,将相关的错误信息或修改建议传回给 AI。
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
const handleSendBackToAI = (data: any) => {
|
|
41
|
+
onSendBackToAI(data);
|
|
42
|
+
};
|
|
43
|
+
eventCenter.on("sendBackToAI", handleSendBackToAI);
|
|
44
|
+
return () => {
|
|
45
|
+
eventCenter.off("sendBackToAI", handleSendBackToAI);
|
|
46
|
+
};
|
|
47
|
+
}, [onSendBackToAI]);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 4. TypeScript 声明
|
|
51
|
+
|
|
52
|
+
为了让 React 识别自定义的 Web Component 标签,需要添加类型声明:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
declare module "react" {
|
|
56
|
+
namespace JSX {
|
|
57
|
+
interface IntrinsicElements {
|
|
58
|
+
"ai-artifacts": React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement> & {
|
|
59
|
+
src?: string;
|
|
60
|
+
"store-id"?: string;
|
|
61
|
+
"group-id"?: string;
|
|
62
|
+
"version-id"?: string;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 配置项
|
|
70
|
+
|
|
71
|
+
- `artifactsUrl`: 渲染沙箱的 URL 地址,建议通过 `localStorage` 或环境变量进行配置,以便灵活切换(例如:`https://langgraph-artifacts.netlify.app/`)。
|
|
@@ -334,6 +334,7 @@ export const createChatStore = (initClientName: string, config: Partial<LangGrap
|
|
|
334
334
|
|
|
335
335
|
inChatError.set(null);
|
|
336
336
|
try {
|
|
337
|
+
loading.set(true);
|
|
337
338
|
await c.sendMessage(message || userInput.get(), extraData);
|
|
338
339
|
} catch (e) {
|
|
339
340
|
const isThreadRunning = (e as Error).message.includes("422");
|