sse-stream-plugin-vue 1.0.0 → 1.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,40 @@
1
+ # 更新日志
2
+
3
+ 本文件记录 sse-stream-plugin-vue 的版本变更。
4
+
5
+ 格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
6
+
7
+ ## [1.1.1] - 2026-12-01
8
+
9
+ ### 文档
10
+
11
+ - README 新增英文文档(单文件双语:英文在上、中文在下,顶部/底部附语言切换锚点),覆盖安装、快速开始、API、生命周期、暂停/继续/重新开始与高级用法。
12
+ - 本次为纯文档发布,无源码与 API 变更。
13
+
14
+ ## [1.1.0] - 2026-11-19
15
+
16
+ ### 新增
17
+
18
+ - 新增 `resume()`:复用当前实例从断点续传;实例不存在或上次流已正常结束时自动退化为重新开始。
19
+ - 新增 `restart()`:销毁旧实例并清空输出,从头重新请求(与原 `start()` 行为一致)。
20
+ - 新增 `close()`:销毁实例与订阅并复位状态,用于「直接停止」;已输出内容保留。
21
+ - 新增 `closed` 事件监听:无论通过 `close()` 还是直接调用 `instance.value?.destroy()`,`status` / `loading` / `instance` 都会同步复位。
22
+ - 订阅 `connecting` 事件:自动重连或续传期间 `loading` 能正确恢复为 `true`。
23
+
24
+ ### 变更
25
+
26
+ - `stop()` 由调用 `destroy()` 改为调用核心 SDK 新增的 `abort()`:只中断当前流,实例与已输出内容保留,可通过 `resume()` 继续。
27
+ - peerDependencies 中 `sse-stream-plugin-sdk` 升级为 `^1.1.0`(`stop()` 依赖新增的 `abort()`)。
28
+ - `start()` 行为保持不变,等价于 `restart()`。
29
+
30
+ ## [1.0.0] - 2026-10-04
31
+
32
+ ### 新增
33
+
34
+ - 首次发布。sse-stream-plugin-sdk 的 Vue 3 Composable 封装。
35
+ - 提供 `useSSEStream` Composable:以响应式方式管理 SSE 连接,返回 `text` / `loading` / `status` 响应式状态以及 `start` / `stop` 控制方法。
36
+ - 入参支持 `MaybeRef`(如 `url`、`headers` 可直接传 ref),`getBody` 在每次发起连接时读取最新值。
37
+ - 支持 `onMessage` / `onError` 等回调,并可透传核心 SDK 的 plugins 等全部配置。
38
+ - 自动管理实例生命周期与事件订阅,组件卸载时自动销毁连接。
39
+ - peerDependencies:`vue >= 3.2.0`、`sse-stream-plugin-sdk ^1.0.0`。
40
+ - 产物格式:ESM(`dist/index.mjs`)、CJS(`dist/index.cjs`)及 TypeScript 类型声明(`dist/index.d.ts`)。
package/README.md CHANGED
@@ -1,142 +1,461 @@
1
- # sse-stream-plugin-vue
2
-
3
- [sse-stream-plugin-sdk](../core) 的 Vue 3 Composable 封装。将插件化 SSE SDK 封装为响应式 API,支持 `MaybeRef` 入参与自动生命周期管理。
4
-
5
- ## 安装
6
-
7
- 需要同时安装核心 SDK:
8
-
9
- ```bash
10
- npm install sse-stream-plugin-vue sse-stream-plugin-sdk
11
- # 或
12
- pnpm add sse-stream-plugin-vue sse-stream-plugin-sdk
13
- ```
14
-
15
- > `vue` 为 peerDependency,要求 `>=3.2.0`。
16
-
17
- ## 快速开始
18
-
19
- ```vue
20
- <script setup lang="ts">
21
- import { ref } from 'vue'
22
- import { useSSEStream } from 'sse-stream-plugin-vue'
23
- import { recommendedPlugins } from 'sse-stream-plugin-sdk'
24
-
25
- const prompt = ref('你好')
26
-
27
- const { text, loading, status, start, stop } = useSSEStream({
28
- url: 'https://api.example.com/stream',
29
- method: 'POST',
30
- // getBody 每次 start 时调用,读取最新 prompt
31
- getBody: () => JSON.stringify({ prompt: prompt.value }),
32
- headers: { 'Content-Type': 'application/json' },
33
- plugins: recommendedPlugins(),
34
- onMessage: (data) => console.log(data),
35
- onError: (err) => console.error(err),
36
- })
37
- </script>
38
-
39
- <template>
40
- <div>
41
- <input v-model="prompt" />
42
- <button @click="start" :disabled="loading">发送</button>
43
- <button @click="stop">停止</button>
44
- <p>{{ status }}</p>
45
- <pre>{{ text }}</pre>
46
- </div>
47
- </template>
48
- ```
49
-
50
- ## API
51
-
52
- ### `useSSEStream(options)`
53
-
54
- #### 入参 `UseSSEStreamOptions`
55
-
56
- 核心配置字段支持 `MaybeRef`(可传 `ref` 或原始值),`start` 时 `unref` 读取最新值:
57
-
58
- | 字段 | 类型 | 说明 |
59
- |------|------|------|
60
- | `url` | `MaybeRef<string>` | 请求地址(必填) |
61
- | `method` | `MaybeRef<'GET' \| 'POST'>` | 请求方法,默认 `GET` |
62
- | `body` | `MaybeRef<string>` | 请求体字符串 |
63
- | `getBody` | `() => string` | 每次 `start` 时动态生成请求体,优先级高于 `body` |
64
- | `headers` | `MaybeRef<Record<string, string>>` | 自定义请求头 |
65
- | `transport` | `MaybeRef<string \| TransportFactory>` | 传输层,省略则自动探测 `fetch` |
66
- | `parser` | `MaybeRef<ParserFactory>` | 自定义解析器 |
67
- | `plugins` | `MaybeRef<SSEPlugin[]>` | 插件列表 |
68
- | `onMessage` | `(data, event) => void` | 收到 message 事件回调 |
69
- | `onOpen` | `(info: { transport }) => void` | 连接打开回调 |
70
- | `onEnd` | `() => void` | 流结束回调 |
71
- | `onError` | `(err) => void` | 错误回调 |
72
-
73
- #### 返回值 `UseSSEStreamReturn`
74
-
75
- 状态字段均为只读 `Ref`:
76
-
77
- | 字段 | 类型 | 说明 |
78
- |------|------|------|
79
- | `text` | `Readonly<Ref<string>>` | 已拼接的消息文本(`message` 事件 `data` 累加) |
80
- | `status` | `Readonly<Ref<string>>` | 连接状态描述 |
81
- | `loading` | `Readonly<Ref<boolean>>` | 是否处于连接中/流式接收中 |
82
- | `msgCount` | `Readonly<Ref<number>>` | 已接收 message 事件条数 |
83
- | `charCount` | `Readonly<Ref<number>>` | 已接收字符总数 |
84
- | `start` | `() => void` | 发起一次连接(先销毁旧连接) |
85
- | `stop` | `() => void` | 手动断开并销毁实例 |
86
- | `instance` | `Ref<PluginSSE \| null>` | 当前实例(`shallowRef`,便于 `use()` 追加插件) |
87
-
88
- ## 生命周期
89
-
90
- - 组件卸载时(`onBeforeUnmount`)自动 `destroy()` 实例并清空订阅,无需手动清理
91
- - `start()` 会先销毁旧连接再创建新连接,可重复调用
92
-
93
- ## 高级用法
94
-
95
- ### 响应式 url
96
-
97
- ```vue
98
- <script setup lang="ts">
99
- import { ref } from 'vue'
100
-
101
- const url = ref('https://api.example.com/stream')
102
-
103
- const { start } = useSSEStream({
104
- url, // 传 ref,start 时自动取最新值
105
- method: 'POST',
106
- })
107
- </script>
108
- ```
109
-
110
- ### 通过 `instance` 追加插件
111
-
112
- ```vue
113
- <script setup lang="ts">
114
- import { watch } from 'vue'
115
- import { loggerPlugin } from 'sse-stream-plugin-sdk'
116
-
117
- const { instance } = useSSEStream({ url, method: 'POST' })
118
-
119
- watch(instance, (sse) => {
120
- if (sse) sse.use(loggerPlugin())
121
- })
122
- </script>
123
- ```
124
-
125
- ### 小程序环境
126
-
127
- 通过 `transport` 传入小程序传输层工厂:
128
-
129
- ```vue
130
- <script setup lang="ts">
131
- import { createMpTransport } from 'sse-stream-plugin-sdk'
132
-
133
- const { start } = useSSEStream({
134
- url,
135
- transport: createMpTransport(() => wx),
136
- })
137
- </script>
138
- ```
139
-
140
- ## License
141
-
142
- MIT
1
+ <a id="english"></a>
2
+
3
+ **English** | [简体中文](#zh-cn)
4
+
5
+ # sse-stream-plugin-vue
6
+
7
+ Vue 3 Composable wrapper for [sse-stream-plugin-sdk](../core). Wraps the plugin-based SSE SDK into a reactive API, supporting `MaybeRef` inputs and automatic lifecycle management.
8
+
9
+ ## Install
10
+
11
+ The core SDK must be installed as well:
12
+
13
+ ```bash
14
+ npm install sse-stream-plugin-vue sse-stream-plugin-sdk
15
+ # or
16
+ pnpm add sse-stream-plugin-vue sse-stream-plugin-sdk
17
+ ```
18
+
19
+ > `vue` is a peerDependency, requiring `>=3.2.0`.
20
+
21
+ ## Quick Start
22
+
23
+ ```vue
24
+ <script setup lang="ts">
25
+ import { ref } from 'vue'
26
+ import { useSSEStream } from 'sse-stream-plugin-vue'
27
+ import { recommendedPlugins } from 'sse-stream-plugin-sdk'
28
+
29
+ const prompt = ref('Hello')
30
+
31
+ const { text, loading, status, start, stop } = useSSEStream({
32
+ url: 'https://api.example.com/stream',
33
+ method: 'POST',
34
+ // getBody is called on each start, reading the latest prompt
35
+ getBody: () => JSON.stringify({ prompt: prompt.value }),
36
+ headers: { 'Content-Type': 'application/json' },
37
+ plugins: recommendedPlugins(),
38
+ onMessage: (data) => console.log(data),
39
+ onError: (err) => console.error(err),
40
+ })
41
+ </script>
42
+
43
+ <template>
44
+ <div>
45
+ <input v-model="prompt" />
46
+ <button @click="start" :disabled="loading">Send</button>
47
+ <button @click="stop">Pause</button>
48
+ <p>{{ status }}</p>
49
+ <pre>{{ text }}</pre>
50
+ </div>
51
+ </template>
52
+ ```
53
+
54
+ ## API
55
+
56
+ ### `useSSEStream(options)`
57
+
58
+ #### Parameters `UseSSEStreamOptions`
59
+
60
+ The core config fields support `MaybeRef` (pass a `ref` or a raw value); `unref` reads the latest value on `start`:
61
+
62
+ | Field | Type | Description |
63
+ |------|------|------|
64
+ | `url` | `MaybeRef<string>` | Request URL (required) |
65
+ | `method` | `MaybeRef<'GET' \| 'POST'>` | HTTP method, default `GET` |
66
+ | `body` | `MaybeRef<string>` | Request body string |
67
+ | `getBody` | `() => string` | Dynamically generates the request body on each `start`, takes precedence over `body` |
68
+ | `headers` | `MaybeRef<Record<string, string>>` | Custom request headers |
69
+ | `transport` | `MaybeRef<string \| TransportFactory>` | Transport, auto-detects `fetch` when omitted |
70
+ | `parser` | `MaybeRef<ParserFactory>` | Custom parser |
71
+ | `plugins` | `MaybeRef<SSEPlugin[]>` | Plugin list |
72
+ | `onMessage` | `(data, event) => void` | Callback on a `message` event |
73
+ | `onOpen` | `(info: { transport }) => void` | Callback when the connection opens |
74
+ | `onEnd` | `() => void` | Callback when the stream ends |
75
+ | `onError` | `(err) => void` | Error callback |
76
+
77
+ #### Return value `UseSSEStreamReturn`
78
+
79
+ All state fields are read-only `Ref`:
80
+
81
+ | Field | Type | Description |
82
+ |------|------|------|
83
+ | `text` | `Readonly<Ref<string>>` | Concatenated message text (accumulated `message` event `data`) |
84
+ | `status` | `Readonly<Ref<string>>` | Connection status description |
85
+ | `loading` | `Readonly<Ref<boolean>>` | Whether connecting / streaming |
86
+ | `msgCount` | `Readonly<Ref<number>>` | Number of `message` events received |
87
+ | `charCount` | `Readonly<Ref<number>>` | Total characters received |
88
+ | `start` | `() => void` | Start a connection (destroys the old instance and clears output; equivalent to `restart`) |
89
+ | `restart` | `() => void` | Restart: discard output and create a new instance |
90
+ | `stop` | `() => void` | Pause: interrupt the current stream, keep the instance and output |
91
+ | `resume` | `() => void` | Resume: reuse the instance to continue from the breakpoint |
92
+ | `close` | `() => void` | Fully stop: destroy the instance and subscriptions (permanently disconnect) |
93
+ | `instance` | `Ref<PluginSSE \| null>` | Current instance (`shallowRef`, for `use()` to add plugins) |
94
+
95
+ ## Lifecycle
96
+
97
+ - On component unmount (`onBeforeUnmount`) the instance is automatically `destroy()`ed and subscriptions cleared — no manual cleanup needed
98
+ - `start()` / `restart()` destroy the old connection before creating a new one; can be called repeatedly
99
+ - `stop()` only interrupts the connection (internally calls `abort()`); the instance and output are kept, and `resume()` can continue
100
+ - `close()` fully stops: internally calls `destroy()`, releases plugin resources and clears subscriptions; you must `start()` again afterwards
101
+ - When the stream has ended normally or the instance hasn't been created, `resume()` automatically falls back to `restart()`
102
+
103
+ ## Pause, Resume & Restart
104
+
105
+ The `stop()` / `resume()` / `restart()` combo lets you "pause mid-stream and resume from the breakpoint later":
106
+
107
+ ```vue
108
+ <script setup lang="ts">
109
+ import { useSSEStream } from 'sse-stream-plugin-vue'
110
+ import { recommendedPlugins } from 'sse-stream-plugin-sdk'
111
+
112
+ const { text, status, loading, start, stop, resume, restart } = useSSEStream({
113
+ url: 'https://api.example.com/stream',
114
+ method: 'POST',
115
+ getBody: () => JSON.stringify({ prompt: 'Hello' }),
116
+ headers: { 'Content-Type': 'application/json' },
117
+ plugins: recommendedPlugins(),
118
+ })
119
+ </script>
120
+
121
+ <template>
122
+ <div>
123
+ <button @click="start">Send</button>
124
+ <!-- interrupt the connection, keep the output, status becomes "Paused" -->
125
+ <button @click="stop" :disabled="!loading">Pause</button>
126
+ <!-- reuse the instance to continue, text appends to existing content -->
127
+ <button @click="resume">Resume</button>
128
+ <!-- discard output and resend the request -->
129
+ <button @click="restart">Regenerate</button>
130
+ <p>{{ status }}</p>
131
+ <pre>{{ text }}</pre>
132
+ </div>
133
+ </template>
134
+ ```
135
+
136
+ | Method | Behavior | `text` | Connection position |
137
+ |------|------|--------|----------|
138
+ | `stop()` | Interrupt current stream, keep instance | Kept | Kept |
139
+ | `resume()` | Reuse instance to reconnect | Kept and appended | Resumes with `Last-Event-ID` |
140
+ | `restart()` | Destroy old instance, resend | Cleared | From scratch |
141
+ | `close()` | Destroy instance and subscriptions, fully stop | Kept | Cleared |
142
+
143
+ > `resume()`'s resumable transfer depends on the server supporting `Last-Event-ID`. If the server doesn't support it, the resume becomes a full resend — use `restart()` instead or add a dedup plugin.
144
+
145
+ ### Fully stop (close)
146
+
147
+ `stop()` only pauses; the instance is still resident. If the user wants to truly end the session, use `close()`:
148
+
149
+ ```vue
150
+ <script setup lang="ts">
151
+ import { useSSEStream } from 'sse-stream-plugin-vue'
152
+
153
+ const { text, status, loading, start, stop, close } = useSSEStream({
154
+ url: 'https://api.example.com/stream',
155
+ method: 'POST',
156
+ getBody: () => JSON.stringify({ prompt: 'Hello' }),
157
+ })
158
+ </script>
159
+
160
+ <template>
161
+ <div>
162
+ <button @click="start">Send</button>
163
+ <button @click="stop" :disabled="!loading">Pause</button>
164
+ <!-- destroy the instance, release plugin resources and clear event subscriptions; unrecoverable -->
165
+ <button @click="close">Stop</button>
166
+ <p>{{ status }}</p><!-- "Stopped" after close -->
167
+ <pre>{{ text }}</pre>
168
+ </div>
169
+ </template>
170
+ ```
171
+
172
+ Key points:
173
+
174
+ - `close()` is equivalent to calling `destroy()` on the instance: abort the request, run plugin dispose callbacks, clear event subscriptions
175
+ - Afterwards `status` becomes "Stopped", `loading` becomes `false`, `instance` becomes `null`
176
+ - The already-output `text` is kept; clicking "Send" (`start()`) again creates a brand-new instance from scratch
177
+ - On component unmount `destroy()` is called automatically, so manual invocation is usually unnecessary; `close()` suits "the user explicitly ends the session and wants resources released immediately"
178
+ - Calling `instance.value?.destroy()` directly has the same effect; the Composable syncs state via the `closed` event
179
+
180
+ ## Advanced Usage
181
+
182
+ ### Reactive url
183
+
184
+ ```vue
185
+ <script setup lang="ts">
186
+ import { ref } from 'vue'
187
+
188
+ const url = ref('https://api.example.com/stream')
189
+
190
+ const { start } = useSSEStream({
191
+ url, // pass a ref, the latest value is read automatically on start
192
+ method: 'POST',
193
+ })
194
+ </script>
195
+ ```
196
+
197
+ ### Add plugins via `instance`
198
+
199
+ ```vue
200
+ <script setup lang="ts">
201
+ import { watch } from 'vue'
202
+ import { loggerPlugin } from 'sse-stream-plugin-sdk'
203
+
204
+ const { instance } = useSSEStream({ url, method: 'POST' })
205
+
206
+ watch(instance, (sse) => {
207
+ if (sse) sse.use(loggerPlugin())
208
+ })
209
+ </script>
210
+ ```
211
+
212
+ ### Mini-program environment
213
+
214
+ Pass a mini-program transport factory via `transport`:
215
+
216
+ ```vue
217
+ <script setup lang="ts">
218
+ import { createMpTransport } from 'sse-stream-plugin-sdk'
219
+
220
+ const { start } = useSSEStream({
221
+ url,
222
+ transport: createMpTransport(() => wx),
223
+ })
224
+ </script>
225
+ ```
226
+
227
+ ## License
228
+
229
+ MIT
230
+
231
+ ---
232
+
233
+ <a id="zh-cn"></a>
234
+
235
+ [← English](#english) | **简体中文**
236
+
237
+ # sse-stream-plugin-vue
238
+
239
+ [sse-stream-plugin-sdk](../core) 的 Vue 3 Composable 封装。将插件化 SSE SDK 封装为响应式 API,支持 `MaybeRef` 入参与自动生命周期管理。
240
+
241
+ ## 安装
242
+
243
+ 需要同时安装核心 SDK:
244
+
245
+ ```bash
246
+ npm install sse-stream-plugin-vue sse-stream-plugin-sdk
247
+ # 或
248
+ pnpm add sse-stream-plugin-vue sse-stream-plugin-sdk
249
+ ```
250
+
251
+ > `vue` 为 peerDependency,要求 `>=3.2.0`。
252
+
253
+ ## 快速开始
254
+
255
+ ```vue
256
+ <script setup lang="ts">
257
+ import { ref } from 'vue'
258
+ import { useSSEStream } from 'sse-stream-plugin-vue'
259
+ import { recommendedPlugins } from 'sse-stream-plugin-sdk'
260
+
261
+ const prompt = ref('你好')
262
+
263
+ const { text, loading, status, start, stop } = useSSEStream({
264
+ url: 'https://api.example.com/stream',
265
+ method: 'POST',
266
+ // getBody 每次 start 时调用,读取最新 prompt
267
+ getBody: () => JSON.stringify({ prompt: prompt.value }),
268
+ headers: { 'Content-Type': 'application/json' },
269
+ plugins: recommendedPlugins(),
270
+ onMessage: (data) => console.log(data),
271
+ onError: (err) => console.error(err),
272
+ })
273
+ </script>
274
+
275
+ <template>
276
+ <div>
277
+ <input v-model="prompt" />
278
+ <button @click="start" :disabled="loading">发送</button>
279
+ <button @click="stop">暂停</button>
280
+ <p>{{ status }}</p>
281
+ <pre>{{ text }}</pre>
282
+ </div>
283
+ </template>
284
+ ```
285
+
286
+ ## API
287
+
288
+ ### `useSSEStream(options)`
289
+
290
+ #### 入参 `UseSSEStreamOptions`
291
+
292
+ 核心配置字段支持 `MaybeRef`(可传 `ref` 或原始值),`start` 时 `unref` 读取最新值:
293
+
294
+ | 字段 | 类型 | 说明 |
295
+ |------|------|------|
296
+ | `url` | `MaybeRef<string>` | 请求地址(必填) |
297
+ | `method` | `MaybeRef<'GET' \| 'POST'>` | 请求方法,默认 `GET` |
298
+ | `body` | `MaybeRef<string>` | 请求体字符串 |
299
+ | `getBody` | `() => string` | 每次 `start` 时动态生成请求体,优先级高于 `body` |
300
+ | `headers` | `MaybeRef<Record<string, string>>` | 自定义请求头 |
301
+ | `transport` | `MaybeRef<string \| TransportFactory>` | 传输层,省略则自动探测 `fetch` |
302
+ | `parser` | `MaybeRef<ParserFactory>` | 自定义解析器 |
303
+ | `plugins` | `MaybeRef<SSEPlugin[]>` | 插件列表 |
304
+ | `onMessage` | `(data, event) => void` | 收到 message 事件回调 |
305
+ | `onOpen` | `(info: { transport }) => void` | 连接打开回调 |
306
+ | `onEnd` | `() => void` | 流结束回调 |
307
+ | `onError` | `(err) => void` | 错误回调 |
308
+
309
+ #### 返回值 `UseSSEStreamReturn`
310
+
311
+ 状态字段均为只读 `Ref`:
312
+
313
+ | 字段 | 类型 | 说明 |
314
+ |------|------|------|
315
+ | `text` | `Readonly<Ref<string>>` | 已拼接的消息文本(`message` 事件 `data` 累加) |
316
+ | `status` | `Readonly<Ref<string>>` | 连接状态描述 |
317
+ | `loading` | `Readonly<Ref<boolean>>` | 是否处于连接中/流式接收中 |
318
+ | `msgCount` | `Readonly<Ref<number>>` | 已接收 message 事件条数 |
319
+ | `charCount` | `Readonly<Ref<number>>` | 已接收字符总数 |
320
+ | `start` | `() => void` | 发起连接(销毁旧实例、清空已输出内容,等价 `restart`) |
321
+ | `restart` | `() => void` | 重新开始:丢弃已输出内容并新建实例 |
322
+ | `stop` | `() => void` | 暂停:中断当前流,保留实例与已输出内容 |
323
+ | `resume` | `() => void` | 继续:复用实例从断点续传 |
324
+ | `close` | `() => void` | 彻底停止:销毁实例与订阅(永久断开) |
325
+ | `instance` | `Ref<PluginSSE \| null>` | 当前实例(`shallowRef`,便于 `use()` 追加插件) |
326
+
327
+ ## 生命周期
328
+
329
+ - 组件卸载时(`onBeforeUnmount`)自动 `destroy()` 实例并清空订阅,无需手动清理
330
+ - `start()` / `restart()` 会先销毁旧连接再创建新连接,可重复调用
331
+ - `stop()` 只中断连接(内部调用 `abort()`),实例与已输出内容保留,可用 `resume()` 继续
332
+ - `close()` 彻底停止:内部调用 `destroy()`,释放插件资源并清空订阅,之后需重新 `start()`
333
+ - 流已正常结束或实例尚未创建时,`resume()` 自动退化为 `restart()`
334
+
335
+ ## 暂停、继续与重新开始
336
+
337
+ `stop()` / `resume()` / `restart()` 组合可实现「流式输出到一半暂停,稍后从断点继续」:
338
+
339
+ ```vue
340
+ <script setup lang="ts">
341
+ import { useSSEStream } from 'sse-stream-plugin-vue'
342
+ import { recommendedPlugins } from 'sse-stream-plugin-sdk'
343
+
344
+ const { text, status, loading, start, stop, resume, restart } = useSSEStream({
345
+ url: 'https://api.example.com/stream',
346
+ method: 'POST',
347
+ getBody: () => JSON.stringify({ prompt: '你好' }),
348
+ headers: { 'Content-Type': 'application/json' },
349
+ plugins: recommendedPlugins(),
350
+ })
351
+ </script>
352
+
353
+ <template>
354
+ <div>
355
+ <button @click="start">发送</button>
356
+ <!-- 中断连接,已输出内容保留,status 变为「已暂停」 -->
357
+ <button @click="stop" :disabled="!loading">暂停</button>
358
+ <!-- 复用实例继续,text 在原有内容基础上追加 -->
359
+ <button @click="resume">继续</button>
360
+ <!-- 丢弃已输出内容,重新发送请求 -->
361
+ <button @click="restart">重新生成</button>
362
+ <p>{{ status }}</p>
363
+ <pre>{{ text }}</pre>
364
+ </div>
365
+ </template>
366
+ ```
367
+
368
+ | 方法 | 行为 | `text` | 连接位点 |
369
+ |------|------|--------|----------|
370
+ | `stop()` | 中断当前流,实例保留 | 保留 | 保留 |
371
+ | `resume()` | 复用实例重新连接 | 保留并追加 | 携带 `Last-Event-ID` 续传 |
372
+ | `restart()` | 销毁旧实例,重新发送 | 清空 | 从头开始 |
373
+ | `close()` | 销毁实例与订阅,彻底停止 | 保留 | 清空 |
374
+
375
+ > `resume()` 的断点续传依赖服务端支持 `Last-Event-ID`。若服务端不支持,续传会变成全量重发,请改用 `restart()` 或额外加去重插件。
376
+
377
+ ### 彻底停止(close)
378
+
379
+ `stop()` 只是暂停,实例仍然驻留;若用户要真正结束本次会话,用 `close()`:
380
+
381
+ ```vue
382
+ <script setup lang="ts">
383
+ import { useSSEStream } from 'sse-stream-plugin-vue'
384
+
385
+ const { text, status, loading, start, stop, close } = useSSEStream({
386
+ url: 'https://api.example.com/stream',
387
+ method: 'POST',
388
+ getBody: () => JSON.stringify({ prompt: '你好' }),
389
+ })
390
+ </script>
391
+
392
+ <template>
393
+ <div>
394
+ <button @click="start">发送</button>
395
+ <button @click="stop" :disabled="!loading">暂停</button>
396
+ <!-- 销毁实例、释放插件资源并清空事件订阅,不可恢复 -->
397
+ <button @click="close">停止</button>
398
+ <p>{{ status }}</p><!-- close 后为「已停止」 -->
399
+ <pre>{{ text }}</pre>
400
+ </div>
401
+ </template>
402
+ ```
403
+
404
+ 要点:
405
+
406
+ - `close()` 等价于对实例执行 `destroy()`:中止请求、执行插件销毁回调、清空事件订阅
407
+ - 之后 `status` 变为「已停止」、`loading` 变为 `false`、`instance` 变为 `null`
408
+ - 已输出的 `text` 保留;再次点击「发送」(`start()`)会创建全新实例,从头开始
409
+ - 组件卸载时会自动 `destroy()`,通常无需手动调用;`close()` 适用于「用户明确结束会话、希望立即释放资源」的场景
410
+ - 直接调用 `instance.value?.destroy()` 效果相同,Composable 会通过 `closed` 事件同步复位状态
411
+
412
+ ## 高级用法
413
+
414
+ ### 响应式 url
415
+
416
+ ```vue
417
+ <script setup lang="ts">
418
+ import { ref } from 'vue'
419
+
420
+ const url = ref('https://api.example.com/stream')
421
+
422
+ const { start } = useSSEStream({
423
+ url, // 传 ref,start 时自动取最新值
424
+ method: 'POST',
425
+ })
426
+ </script>
427
+ ```
428
+
429
+ ### 通过 `instance` 追加插件
430
+
431
+ ```vue
432
+ <script setup lang="ts">
433
+ import { watch } from 'vue'
434
+ import { loggerPlugin } from 'sse-stream-plugin-sdk'
435
+
436
+ const { instance } = useSSEStream({ url, method: 'POST' })
437
+
438
+ watch(instance, (sse) => {
439
+ if (sse) sse.use(loggerPlugin())
440
+ })
441
+ </script>
442
+ ```
443
+
444
+ ### 小程序环境
445
+
446
+ 通过 `transport` 传入小程序传输层工厂:
447
+
448
+ ```vue
449
+ <script setup lang="ts">
450
+ import { createMpTransport } from 'sse-stream-plugin-sdk'
451
+
452
+ const { start } = useSSEStream({
453
+ url,
454
+ transport: createMpTransport(() => wx),
455
+ })
456
+ </script>
457
+ ```
458
+
459
+ ## License
460
+
461
+ MIT
package/dist/index.cjs CHANGED
@@ -18,10 +18,19 @@ function useSSEStream(options) {
18
18
  const msgCount = vue.ref(0);
19
19
  const charCount = vue.ref(0);
20
20
  const instance = vue.shallowRef(null);
21
+ /** 上一次连接是否正常结束(正常结束后续传位点已归零,resume 需从头开始) */
22
+ let endedNormally = false;
23
+ // 暂停:中断当前流但保留实例与已输出内容,状态由 abort 事件更新
21
24
  function stop() {
22
- instance.value?.destroy();
23
- instance.value = null;
24
- status.value = '已手动断开';
25
+ instance.value?.abort();
26
+ }
27
+ // 彻底停止:销毁实例与订阅,状态由 closed 事件统一复位
28
+ function close() {
29
+ if (instance.value) {
30
+ instance.value.destroy();
31
+ return;
32
+ }
33
+ status.value = '已停止';
25
34
  loading.value = false;
26
35
  }
27
36
  function start() {
@@ -33,6 +42,7 @@ function useSSEStream(options) {
33
42
  charCount.value = 0;
34
43
  status.value = '连接中…';
35
44
  loading.value = true;
45
+ endedNormally = false;
36
46
  const sse = new sseStreamPluginSdk.PluginSSE({
37
47
  url: vue.unref(url),
38
48
  method: vue.unref(method) ?? 'GET',
@@ -42,6 +52,11 @@ function useSSEStream(options) {
42
52
  parser: vue.unref(parser),
43
53
  plugins: vue.unref(plugins),
44
54
  });
55
+ // 每次(含自动重连、resume)发起连接都会触发,保证 loading 状态不丢失
56
+ sse.on('connecting', () => {
57
+ status.value = '连接中…';
58
+ loading.value = true;
59
+ });
45
60
  sse.on('open', ({ transport: name }) => {
46
61
  status.value = `已连接(${name})`;
47
62
  onOpen?.({ transport: name });
@@ -53,6 +68,7 @@ function useSSEStream(options) {
53
68
  onMessage?.(data, event);
54
69
  });
55
70
  sse.on('end', () => {
71
+ endedNormally = true;
56
72
  status.value = '流正常结束';
57
73
  loading.value = false;
58
74
  onEnd?.();
@@ -63,9 +79,30 @@ function useSSEStream(options) {
63
79
  loading.value = false;
64
80
  onError?.(err);
65
81
  });
82
+ sse.on('abort', () => {
83
+ status.value = '已暂停';
84
+ loading.value = false;
85
+ });
86
+ sse.on('closed', () => {
87
+ endedNormally = false;
88
+ instance.value = null;
89
+ status.value = '已停止';
90
+ loading.value = false;
91
+ });
66
92
  instance.value = sse;
67
93
  sse.connect();
68
94
  }
95
+ // 继续:复用实例从断点续传;无实例或上次已正常结束时退化为重新开始
96
+ function resume() {
97
+ const sse = instance.value;
98
+ if (!sse || endedNormally) {
99
+ start();
100
+ return;
101
+ }
102
+ status.value = '连接中…';
103
+ loading.value = true;
104
+ void sse.start();
105
+ }
69
106
  // 组件卸载:释放连接、清空订阅
70
107
  vue.onBeforeUnmount(() => {
71
108
  instance.value?.destroy();
@@ -78,7 +115,10 @@ function useSSEStream(options) {
78
115
  msgCount: vue.readonly(msgCount),
79
116
  charCount: vue.readonly(charCount),
80
117
  start,
118
+ restart: start,
81
119
  stop,
120
+ resume,
121
+ close,
82
122
  instance,
83
123
  };
84
124
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/useSSEStream.ts"],"sourcesContent":[null],"names":["ref","shallowRef","PluginSSE","unref","onBeforeUnmount","readonly"],"mappings":";;;;;AAAA;;;;;;;AAOG;AAyCG,SAAU,YAAY,CAAC,OAA4B,EAAA;AACvD,IAAA,MAAM,IAAI,GAAGA,OAAG,CAAC,EAAE,CAAC;AACpB,IAAA,MAAM,MAAM,GAAGA,OAAG,CAAC,KAAK,CAAC;AACzB,IAAA,MAAM,OAAO,GAAGA,OAAG,CAAC,KAAK,CAAC;AAC1B,IAAA,MAAM,QAAQ,GAAGA,OAAG,CAAC,CAAC,CAAC;AACvB,IAAA,MAAM,SAAS,GAAGA,OAAG,CAAC,CAAC,CAAC;AACxB,IAAA,MAAM,QAAQ,GAAGC,cAAU,CAAmB,IAAI,CAAC;AAEnD,IAAA,SAAS,IAAI,GAAA;AACX,QAAA,QAAQ,CAAC,KAAK,EAAE,OAAO,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AO,CAAC;AAC1B,QAAA,QAAQ,EAAEA,YAAQ,CAAC,QAAQ,CAAC;AAC5B,QAAA,SAAS,EAAEA,YAAQ,CAAC,SAAS,CAAC;QAC9B,KAAK;QACL,IAAI;QACJ,QAAQ;KACT;AACH;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/useSSEStream.ts"],"sourcesContent":[null],"names":["ref","shallowRef","PluginSSE","unref","onBeforeUnmount","readonly"],"mappings":";;;;;AAAA;;;;;;;AAOG;AAkDG,SAAU,YAAY,CAAC,OAA4B,EAAA;AACvD,IAAA,MAAM,IAAI,GAAGA,OAAG,CAAC,EAAE,CAAC;AACpB,IAAA,MAAM,MAAM,GAAGA,OAAG,CAAC,KAAK,CAAC;AACzB,IAAA,MAAM,OAAO,GAAGA,OAAG,CAAC,KAAK,CAAC;AAC1B,IAAA,MAAM,QAAQ,GAAGA,OAAG,CAAC,CAAC,CAAC;AACvB,IAAA,MAAM,SAAS,GAAGA,OAAG,CAAC,CAAC,CAAC;AACxB,IAAA,MAAM,QAAQ,GAAGC,cAAU,CAAmB,IAAI,CAAC;;IAEnD,IAAI,aAAa,GAAG,KAAK;;AAGzB,IAAA,SAAS,IAAI,GAAA;AACX,QAAA,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE;IACzB;;AAGA,IAAA,SAAS,KAAK,GAAA;AACZ,QAAA,IAAI,QAAQ,CAAC,KAAK,EAAE;AAClB,YAAA,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE;YACxB;QACF;AACA,QAAA,MAAM,CAAC,KAAK,GAAG,KAAK;AACpB,QAAA,OAAO,CAAC,KAAK,GAAG,KAAK;IACvB;AAEA,IAAA,SAAS,KAAK,GAAA;QACZ,MAAM,EACJ,GAAG,EACH,MAAM,EACN,IAAI,EACJ,OAAO,EACP,SAAS,EACT,MAAM,EACN,OAAO,EACP,OAAO,EACP,SAAS,EACT,MAAM,EACN,KAAK,EACL,OAAO,GACR,GAAG,OAAO;;AAGX,QAAA,QAAQ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,GAAG,GAAG,QAAQ,CAAC,KAAK;AAC1B,QAAA,IAAI,CAAC,GAAG,IAAI,aAAa,EAAE;AACzB,YAAA,KAAK,EAAE;YACP;QACF;AACA,QAAA,MAAM,CAAC,KAAK,GAAG,MAAM;AACrB,QAAA,OAAO,CAAC,KAAK,GAAG,IAAI;AACpB,QAAA,KAAK,GAAG,CAAC,KAAK,EAAE;IAClB;;IAGAC,mBAAe,CAAC,MAAK;AACnB,QAAA,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE;AACzB,QAAA,QAAQ,CAAC,KAAK,GAAG,IAAI;AACvB,IAAA,CAAC,CAAC;IAEF,OAAO;AACL,QAAA,IAAI,EAAEC,YAAQ,CAAC,IAAI,CAAC;AACpB,QAAA,MAAM,EAAEA,YAAQ,CAAC,MAAM,CAAC;AACxB,QAAA,OAAO,EAAEA,YAAQ,CAAC,OAAO,CAAC;AAC1B,QAAA,QAAQ,EAAEA,YAAQ,CAAC,QAAQ,CAAC;AAC5B,QAAA,SAAS,EAAEA,YAAQ,CAAC,SAAS,CAAC;QAC9B,KAAK;AACL,QAAA,OAAO,EAAE,KAAK;QACd,IAAI;QACJ,MAAM;QACN,KAAK;QACL,QAAQ;KACT;AACH;;"}
package/dist/index.d.ts CHANGED
@@ -37,9 +37,18 @@ interface UseSSEStreamReturn {
37
37
  status: Ref<string>;
38
38
  loading: Ref<boolean>;
39
39
  msgCount: Ref<number>;
40
+ /** 已接收字符总数 */
40
41
  charCount: Ref<number>;
42
+ /** 发起一次连接:销毁旧实例并清空已输出内容,从头开始(等价 restart) */
41
43
  start: () => void;
44
+ /** 重新开始:丢弃已输出内容并新建实例(等价 start) */
45
+ restart: () => void;
46
+ /** 暂停:中断当前流,保留实例与已输出内容,可用 resume 继续 */
42
47
  stop: () => void;
48
+ /** 继续:复用实例从断点续传(无实例或上次已正常结束时退化为重新开始) */
49
+ resume: () => void;
50
+ /** 彻底停止:销毁实例与订阅并复位状态,已输出内容保留 */
51
+ close: () => void;
43
52
  instance: Ref<PluginSSE | null>;
44
53
  }
45
54
  declare function useSSEStream(options: UseSSEStreamOptions): UseSSEStreamReturn;
package/dist/index.mjs CHANGED
@@ -16,10 +16,19 @@ function useSSEStream(options) {
16
16
  const msgCount = ref(0);
17
17
  const charCount = ref(0);
18
18
  const instance = shallowRef(null);
19
+ /** 上一次连接是否正常结束(正常结束后续传位点已归零,resume 需从头开始) */
20
+ let endedNormally = false;
21
+ // 暂停:中断当前流但保留实例与已输出内容,状态由 abort 事件更新
19
22
  function stop() {
20
- instance.value?.destroy();
21
- instance.value = null;
22
- status.value = '已手动断开';
23
+ instance.value?.abort();
24
+ }
25
+ // 彻底停止:销毁实例与订阅,状态由 closed 事件统一复位
26
+ function close() {
27
+ if (instance.value) {
28
+ instance.value.destroy();
29
+ return;
30
+ }
31
+ status.value = '已停止';
23
32
  loading.value = false;
24
33
  }
25
34
  function start() {
@@ -31,6 +40,7 @@ function useSSEStream(options) {
31
40
  charCount.value = 0;
32
41
  status.value = '连接中…';
33
42
  loading.value = true;
43
+ endedNormally = false;
34
44
  const sse = new PluginSSE({
35
45
  url: unref(url),
36
46
  method: unref(method) ?? 'GET',
@@ -40,6 +50,11 @@ function useSSEStream(options) {
40
50
  parser: unref(parser),
41
51
  plugins: unref(plugins),
42
52
  });
53
+ // 每次(含自动重连、resume)发起连接都会触发,保证 loading 状态不丢失
54
+ sse.on('connecting', () => {
55
+ status.value = '连接中…';
56
+ loading.value = true;
57
+ });
43
58
  sse.on('open', ({ transport: name }) => {
44
59
  status.value = `已连接(${name})`;
45
60
  onOpen?.({ transport: name });
@@ -51,6 +66,7 @@ function useSSEStream(options) {
51
66
  onMessage?.(data, event);
52
67
  });
53
68
  sse.on('end', () => {
69
+ endedNormally = true;
54
70
  status.value = '流正常结束';
55
71
  loading.value = false;
56
72
  onEnd?.();
@@ -61,9 +77,30 @@ function useSSEStream(options) {
61
77
  loading.value = false;
62
78
  onError?.(err);
63
79
  });
80
+ sse.on('abort', () => {
81
+ status.value = '已暂停';
82
+ loading.value = false;
83
+ });
84
+ sse.on('closed', () => {
85
+ endedNormally = false;
86
+ instance.value = null;
87
+ status.value = '已停止';
88
+ loading.value = false;
89
+ });
64
90
  instance.value = sse;
65
91
  sse.connect();
66
92
  }
93
+ // 继续:复用实例从断点续传;无实例或上次已正常结束时退化为重新开始
94
+ function resume() {
95
+ const sse = instance.value;
96
+ if (!sse || endedNormally) {
97
+ start();
98
+ return;
99
+ }
100
+ status.value = '连接中…';
101
+ loading.value = true;
102
+ void sse.start();
103
+ }
67
104
  // 组件卸载:释放连接、清空订阅
68
105
  onBeforeUnmount(() => {
69
106
  instance.value?.destroy();
@@ -76,7 +113,10 @@ function useSSEStream(options) {
76
113
  msgCount: readonly(msgCount),
77
114
  charCount: readonly(charCount),
78
115
  start,
116
+ restart: start,
79
117
  stop,
118
+ resume,
119
+ close,
80
120
  instance,
81
121
  };
82
122
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/useSSEStream.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAAA;;;;;;;AAOG;AAyCG,SAAU,YAAY,CAAC,OAA4B,EAAA;AACvD,IAAA,MAAM,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;AACpB,IAAA,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC;AACzB,IAAA,MAAM,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC;AAC1B,IAAA,MAAM,QAAQ,GAAG,GAAG,CAAC,CAAC,CAAC;AACvB,IAAA,MAAM,SAAS,GAAG,GAAG,CAAC,CAAC,CAAC;AACxB,IAAA,MAAM,QAAQ,GAAG,UAAU,CAAmB,IAAI,CAAC;AAEnD,IAAA,SAAS,IAAI,GAAA;AACX,QAAA,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE;AACzB,QAAA,QAAQ,CAAC,KAAK,GAAG,IAAI;AACrB,QAAA,MAAM,CAAC,KAAK,GAAG,OAAO;AACtB,QAAA,OAAO,CAAC,KAA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AACpB,QAAA,KAAK,GAAG,CAAC,KAAK,EAAE;IAClB;;IAGA,eAAe,CAAC,MAAK;AACnB,QAAA,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE;AACzB,QAAA,QAAQ,CAAC,KAAK,GAAG,IAAI;AACvB,IAAA,CAAC,CAAC;IAEF,OAAO;AACL,QAAA,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC;AACpB,QAAA,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC;AACxB,QAAA,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC;AAC1B,QAAA,QAAQ,EAAE,QAAQ,CAAC,QAAQ,CAAC;AAC5B,QAAA,SAAS,EAAE,QAAQ,CAAC,SAAS,CAAC;QAC9B,KAAK;AACL,QAAA,OAAO,EAAE,KAAK;QACd,IAAI;QACJ,MAAM;QACN,KAAK;QACL,QAAQ;KACT;AACH;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sse-stream-plugin-vue",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Vue 3 composables for sse-stream-plugin-sdk",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -24,18 +24,19 @@
24
24
  "files": [
25
25
  "dist",
26
26
  "LICENSE",
27
- "README.md"
27
+ "README.md",
28
+ "CHANGELOG.md"
28
29
  ],
29
- "scripts": {
30
- "build": "rollup -c",
31
- "build:prod": "rollup -c --environment NODE_ENV:production"
32
- },
33
30
  "peerDependencies": {
34
31
  "vue": ">=3.2.0",
35
- "sse-stream-plugin-sdk": "^1.0.0"
32
+ "sse-stream-plugin-sdk": "^1.1.0"
36
33
  },
37
34
  "devDependencies": {
38
- "sse-stream-plugin-sdk": "workspace:*",
35
+ "sse-stream-plugin-sdk": "1.1.1",
39
36
  "vue": "^3.5.42"
37
+ },
38
+ "scripts": {
39
+ "build": "rollup -c",
40
+ "build:prod": "rollup -c --environment NODE_ENV:production"
40
41
  }
41
- }
42
+ }