@ripperts/poco-agent-embed 0.1.2 → 0.1.4

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 (3) hide show
  1. package/README.md +55 -67
  2. package/index.js +65 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -5,119 +5,107 @@
5
5
  ## 安装
6
6
 
7
7
  ```bash
8
- pnpm add @ripperts/poco-agent-embed
8
+ npm install @ripperts/poco-agent-embed
9
9
  ```
10
10
 
11
- SDK 需要在浏览器环境中挂载。第三方系统必须通过 `getAccessToken` 返回当前用户有效的 Poco 登录 token。
11
+ ## 接入步骤
12
12
 
13
- ## Vue 2
13
+ 1. 在 Poco 的场景专家中开放外部嵌入,并把宿主页面的 origin 加入允许来源。
14
+ 2. 在宿主系统后端调用 `POST https://poco.example.com/openapi/v1/auth/token`,为当前登录用户换取 Poco 登录 Token,再通过自己的接口(下文示例为 `/api/poco/token`)返回给前端。
15
+ 3. 在前端挂载对话窗口,`getAccessToken` 调用上一步的接口。
14
16
 
15
- ```vue
16
- <template>
17
- <div ref="agentChat" style="height: 640px"></div>
18
- </template>
19
-
20
- <script>
21
- import { mountAgentEmbed } from "@ripperts/poco-agent-embed";
17
+ `https://poco.example.com` 替换为你在浏览器中打开 Poco 的地址,Token 接口和 SDK 使用同一个地址。下文示例共用这个函数:
22
18
 
23
- export default {
24
- props: {
25
- getAccessToken: {
26
- type: Function,
27
- required: true,
28
- },
29
- },
30
- data() {
31
- return {
32
- agentEmbedController: null,
33
- };
34
- },
35
- mounted() {
36
- this.agentEmbedController = mountAgentEmbed({
37
- baseUrl: "https://poco.example.com",
38
- agentId: "<agent-id>",
39
- target: this.$refs.agentChat,
40
- getAccessToken: () => this.getAccessToken(),
41
- });
42
- },
43
- beforeDestroy() {
44
- if (this.agentEmbedController) {
45
- this.agentEmbedController.destroy();
46
- }
47
- },
48
- };
49
- </script>
19
+ ```js
20
+ async function getAccessToken() {
21
+ const response = await fetch("/api/poco/token");
22
+ const data = await response.json();
23
+ return data.access_token;
24
+ }
50
25
  ```
51
26
 
52
27
  ## Vue 3
53
28
 
54
29
  ```vue
55
30
  <template>
56
- <div ref="agentChat" style="height: 640px"></div>
31
+ <div ref="container" style="height: 640px"></div>
57
32
  </template>
58
33
 
59
34
  <script setup>
60
35
  import { onBeforeUnmount, onMounted, ref } from "vue";
61
36
  import { mountAgentEmbed } from "@ripperts/poco-agent-embed";
62
37
 
63
- const props = defineProps({
64
- getAccessToken: {
65
- type: Function,
66
- required: true,
67
- },
68
- });
69
- const agentChat = ref(null);
70
- let agentEmbedController;
38
+ const container = ref(null);
39
+ let widget = null;
71
40
 
72
41
  onMounted(() => {
73
- agentEmbedController = mountAgentEmbed({
42
+ widget = mountAgentEmbed({
74
43
  baseUrl: "https://poco.example.com",
75
44
  agentId: "<agent-id>",
76
- target: agentChat.value,
77
- getAccessToken: () => props.getAccessToken(),
45
+ target: container.value,
46
+ getAccessToken,
78
47
  });
79
48
  });
80
49
 
81
50
  onBeforeUnmount(() => {
82
- if (agentEmbedController) {
83
- agentEmbedController.destroy();
84
- }
51
+ if (widget) widget.destroy();
85
52
  });
86
53
  </script>
87
54
  ```
88
55
 
56
+ ## Vue 2
57
+
58
+ ```vue
59
+ <template>
60
+ <div ref="container" style="height: 640px"></div>
61
+ </template>
62
+
63
+ <script>
64
+ import { mountAgentEmbed } from "@ripperts/poco-agent-embed";
65
+
66
+ export default {
67
+ mounted() {
68
+ this.widget = mountAgentEmbed({
69
+ baseUrl: "https://poco.example.com",
70
+ agentId: "<agent-id>",
71
+ target: this.$refs.container,
72
+ getAccessToken,
73
+ });
74
+ },
75
+ beforeDestroy() {
76
+ if (this.widget) this.widget.destroy();
77
+ },
78
+ };
79
+ </script>
80
+ ```
81
+
89
82
  ## React
90
83
 
91
84
  ```tsx
92
85
  import { useEffect, useRef } from "react";
93
86
  import { mountAgentEmbed } from "@ripperts/poco-agent-embed";
94
87
 
95
- interface AgentChatProps {
96
- getAccessToken: () => string | Promise<string>;
97
- }
98
-
99
- export function AgentChat({ getAccessToken }: AgentChatProps) {
100
- const targetRef = useRef<HTMLDivElement>(null);
88
+ export function AgentChat() {
89
+ const containerRef = useRef(null);
101
90
 
102
91
  useEffect(() => {
103
- if (!targetRef.current) return;
104
-
105
- const controller = mountAgentEmbed({
92
+ const widget = mountAgentEmbed({
106
93
  baseUrl: "https://poco.example.com",
107
94
  agentId: "<agent-id>",
108
- target: targetRef.current,
95
+ target: containerRef.current,
109
96
  getAccessToken,
110
97
  });
98
+ return () => widget.destroy();
99
+ }, []);
111
100
 
112
- return () => controller.destroy();
113
- }, [getAccessToken]);
114
-
115
- return <div ref={targetRef} style={{ height: 640 }} />;
101
+ return <div ref={containerRef} style={{ height: 640 }} />;
116
102
  }
117
103
  ```
118
104
 
105
+ 传入 `target` 时对话窗口填满该容器,容器需要有明确高度;不传 `target` 则在页面右下角显示悬浮按钮。
106
+
119
107
  ## 浏览器支持
120
108
 
121
109
  支持现代 Chrome、Edge、Firefox 和 Safari。Vue 2 项目可以正常接入,但不支持 Internet Explorer 11。
122
110
 
123
- 完整参数和安全配置请参阅 [Agent 外部嵌入文档](https://ai-agent.fsxgt.cn/docs/external-integration/agent-embed)。
111
+ 完整参数和安全配置请参阅 [Agent 外部嵌入文档](https://ai-agent.fsxgt.cn/docs/getting-started/agent-embed)。
package/index.js CHANGED
@@ -18,6 +18,7 @@
18
18
  var MESSAGE_REQUEST_AUTH = "poco-agent-embed:request-auth";
19
19
  var MESSAGE_AUTHENTICATE = "poco-agent-embed:authenticate";
20
20
  var MESSAGE_ERROR = "poco-agent-embed:error";
21
+ var MESSAGE_DRAG = "poco-agent-embed:drag";
21
22
 
22
23
  function trimTrailingSlash(value) {
23
24
  return String(value || "").replace(/\/+$/, "");
@@ -114,6 +115,7 @@
114
115
  ? options.theme
115
116
  : "system";
116
117
  var query = new URLSearchParams({ channel: channel, theme: theme });
118
+ if (!target && options.draggable !== false) query.set("draggable", "1");
117
119
  var iframe = document.createElement("iframe");
118
120
  iframe.src =
119
121
  baseUrl +
@@ -137,6 +139,7 @@
137
139
  var container = document.createElement("div");
138
140
  var button = null;
139
141
  var updateViewportPosition = null;
142
+ var handleFrameDrag = null;
140
143
  var openIcon = createIcon(options.openIcon, false);
141
144
  var closeIcon = createIcon(options.closeIcon, true);
142
145
 
@@ -165,8 +168,6 @@
165
168
  container.style.cssText = [
166
169
  "position:fixed",
167
170
  "width:min(420px,calc(100vw - 24px))",
168
- "height:min(720px,calc(100dvh - 96px))",
169
- "min-height:min(360px,calc(100dvh - 96px))",
170
171
  "overflow:hidden",
171
172
  "border:1px solid color-mix(in srgb,CanvasText 25%,transparent)",
172
173
  "border-radius:16px",
@@ -201,10 +202,15 @@
201
202
  var position = options.position || "bottom-right";
202
203
  var vertical = position.indexOf("top") === 0 ? "top" : "bottom";
203
204
  var horizontal = position.indexOf("left") > -1 ? "left" : "right";
204
- button.style[vertical] = "20px";
205
+ var verticalOffset = vertical === "bottom" ? 100 : 20;
206
+ var popupAvailableHeight =
207
+ "calc(100dvh - " + (verticalOffset + 76) + "px)";
208
+ button.style[vertical] = verticalOffset + "px";
205
209
  button.style[horizontal] = "20px";
206
- container.style[vertical] = "84px";
210
+ container.style[vertical] = verticalOffset + 64 + "px";
207
211
  container.style[horizontal] = "20px";
212
+ container.style.height = "min(720px," + popupAvailableHeight + ")";
213
+ container.style.minHeight = "min(360px," + popupAvailableHeight + ")";
208
214
  container.style.transformOrigin = vertical + " " + horizontal;
209
215
 
210
216
  function anchorContainerToButton() {
@@ -255,6 +261,52 @@
255
261
  anchorContainerToButton();
256
262
  };
257
263
 
264
+ var frameDrag = null;
265
+ handleFrameDrag = function (data) {
266
+ if (data.phase === "start") {
267
+ var buttonRect = button.getBoundingClientRect();
268
+ var containerRect = container.getBoundingClientRect();
269
+ frameDrag = {
270
+ buttonLeft: buttonRect.left,
271
+ buttonTop: buttonRect.top,
272
+ containerLeft: containerRect.left,
273
+ containerTop: containerRect.top,
274
+ minX: 8 - Math.min(buttonRect.left, containerRect.left),
275
+ maxX:
276
+ window.innerWidth -
277
+ 8 -
278
+ Math.max(buttonRect.right, containerRect.right),
279
+ minY: 8 - Math.min(buttonRect.top, containerRect.top),
280
+ maxY:
281
+ window.innerHeight -
282
+ 8 -
283
+ Math.max(buttonRect.bottom, containerRect.bottom),
284
+ };
285
+ container.style.height = containerRect.height + "px";
286
+ container.style.minHeight = containerRect.height + "px";
287
+ return;
288
+ }
289
+ if (!frameDrag) return;
290
+ if (data.phase === "move") {
291
+ var offsetX = Math.max(
292
+ frameDrag.minX,
293
+ Math.min(frameDrag.maxX, data.dx),
294
+ );
295
+ var offsetY = Math.max(
296
+ frameDrag.minY,
297
+ Math.min(frameDrag.maxY, data.dy),
298
+ );
299
+ button.style.left = frameDrag.buttonLeft + offsetX + "px";
300
+ button.style.top = frameDrag.buttonTop + offsetY + "px";
301
+ container.style.left = frameDrag.containerLeft + offsetX + "px";
302
+ container.style.top = frameDrag.containerTop + offsetY + "px";
303
+ container.style.bottom = "auto";
304
+ return;
305
+ }
306
+ frameDrag = null;
307
+ anchorContainerToButton();
308
+ };
309
+
258
310
  var dragState = null;
259
311
  var suppressClickUntil = 0;
260
312
  button.addEventListener("pointerdown", function (event) {
@@ -358,6 +410,15 @@
358
410
  event.data.type === MESSAGE_REQUEST_AUTH
359
411
  ) {
360
412
  authenticate();
413
+ } else if (
414
+ event.data.type === MESSAGE_DRAG &&
415
+ handleFrameDrag &&
416
+ options.draggable !== false &&
417
+ ["start", "move", "end"].includes(event.data.phase) &&
418
+ Number.isFinite(event.data.dx) &&
419
+ Number.isFinite(event.data.dy)
420
+ ) {
421
+ handleFrameDrag(event.data);
361
422
  } else if (
362
423
  event.data.type === MESSAGE_ERROR &&
363
424
  typeof options.onError === "function"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ripperts/poco-agent-embed",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Zero-dependency SDK for embedding a Poco Agent conversation",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",