sse-stream-plugin-vue 1.1.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 +7 -0
- package/README.md +460 -224
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
6
6
|
|
|
7
|
+
## [1.1.1] - 2026-12-01
|
|
8
|
+
|
|
9
|
+
### 文档
|
|
10
|
+
|
|
11
|
+
- README 新增英文文档(单文件双语:英文在上、中文在下,顶部/底部附语言切换锚点),覆盖安装、快速开始、API、生命周期、暂停/继续/重新开始与高级用法。
|
|
12
|
+
- 本次为纯文档发布,无源码与 API 变更。
|
|
13
|
+
|
|
7
14
|
## [1.1.0] - 2026-11-19
|
|
8
15
|
|
|
9
16
|
### 新增
|
package/README.md
CHANGED
|
@@ -1,225 +1,461 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
[
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
<
|
|
46
|
-
|
|
47
|
-
</
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
|
63
|
-
|
|
64
|
-
| `
|
|
65
|
-
| `
|
|
66
|
-
| `
|
|
67
|
-
| `
|
|
68
|
-
| `
|
|
69
|
-
| `
|
|
70
|
-
| `
|
|
71
|
-
| `
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
|
82
|
-
|
|
83
|
-
| `
|
|
84
|
-
| `
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
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` | 暂停:中断当前流,保留实例与已输出内容 |
|
|
87
323
|
| `resume` | `() => void` | 继续:复用实例从断点续传 |
|
|
88
|
-
| `close` | `() => void` | 彻底停止:销毁实例与订阅(永久断开) |
|
|
89
|
-
| `instance` | `Ref<PluginSSE \| null>` | 当前实例(`shallowRef`,便于 `use()` 追加插件) |
|
|
90
|
-
|
|
91
|
-
## 生命周期
|
|
92
|
-
|
|
93
|
-
- 组件卸载时(`onBeforeUnmount`)自动 `destroy()` 实例并清空订阅,无需手动清理
|
|
94
|
-
- `start()` / `restart()` 会先销毁旧连接再创建新连接,可重复调用
|
|
95
|
-
- `stop()` 只中断连接(内部调用 `abort()`),实例与已输出内容保留,可用 `resume()` 继续
|
|
96
|
-
- `close()` 彻底停止:内部调用 `destroy()`,释放插件资源并清空订阅,之后需重新 `start()`
|
|
97
|
-
- 流已正常结束或实例尚未创建时,`resume()` 自动退化为 `restart()`
|
|
98
|
-
|
|
99
|
-
## 暂停、继续与重新开始
|
|
100
|
-
|
|
101
|
-
`stop()` / `resume()` / `restart()` 组合可实现「流式输出到一半暂停,稍后从断点继续」:
|
|
102
|
-
|
|
103
|
-
```vue
|
|
104
|
-
<script setup lang="ts">
|
|
105
|
-
import { useSSEStream } from 'sse-stream-plugin-vue'
|
|
106
|
-
import { recommendedPlugins } from 'sse-stream-plugin-sdk'
|
|
107
|
-
|
|
108
|
-
const { text, status, loading, start, stop, resume, restart } = useSSEStream({
|
|
109
|
-
url: 'https://api.example.com/stream',
|
|
110
|
-
method: 'POST',
|
|
111
|
-
getBody: () => JSON.stringify({ prompt: '你好' }),
|
|
112
|
-
headers: { 'Content-Type': 'application/json' },
|
|
113
|
-
plugins: recommendedPlugins(),
|
|
114
|
-
})
|
|
115
|
-
</script>
|
|
116
|
-
|
|
117
|
-
<template>
|
|
118
|
-
<div>
|
|
119
|
-
<button @click="start">发送</button>
|
|
120
|
-
<!-- 中断连接,已输出内容保留,status 变为「已暂停」 -->
|
|
121
|
-
<button @click="stop" :disabled="!loading">暂停</button>
|
|
122
|
-
<!-- 复用实例继续,text 在原有内容基础上追加 -->
|
|
123
|
-
<button @click="resume">继续</button>
|
|
124
|
-
<!-- 丢弃已输出内容,重新发送请求 -->
|
|
125
|
-
<button @click="restart">重新生成</button>
|
|
126
|
-
<p>{{ status }}</p>
|
|
127
|
-
<pre>{{ text }}</pre>
|
|
128
|
-
</div>
|
|
129
|
-
</template>
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
| 方法 | 行为 | `text` | 连接位点 |
|
|
133
|
-
|------|------|--------|----------|
|
|
134
|
-
| `stop()` | 中断当前流,实例保留 | 保留 | 保留 |
|
|
135
|
-
| `resume()` | 复用实例重新连接 | 保留并追加 | 携带 `Last-Event-ID` 续传 |
|
|
136
|
-
| `restart()` | 销毁旧实例,重新发送 | 清空 | 从头开始 |
|
|
137
|
-
| `close()` | 销毁实例与订阅,彻底停止 | 保留 | 清空 |
|
|
138
|
-
|
|
139
|
-
> `resume()` 的断点续传依赖服务端支持 `Last-Event-ID`。若服务端不支持,续传会变成全量重发,请改用 `restart()` 或额外加去重插件。
|
|
140
|
-
|
|
141
|
-
### 彻底停止(close)
|
|
142
|
-
|
|
143
|
-
`stop()` 只是暂停,实例仍然驻留;若用户要真正结束本次会话,用 `close()`:
|
|
144
|
-
|
|
145
|
-
```vue
|
|
146
|
-
<script setup lang="ts">
|
|
147
|
-
import { useSSEStream } from 'sse-stream-plugin-vue'
|
|
148
|
-
|
|
149
|
-
const { text, status, loading, start, stop, close } = useSSEStream({
|
|
150
|
-
url: 'https://api.example.com/stream',
|
|
151
|
-
method: 'POST',
|
|
152
|
-
getBody: () => JSON.stringify({ prompt: '你好' }),
|
|
153
|
-
})
|
|
154
|
-
</script>
|
|
155
|
-
|
|
156
|
-
<template>
|
|
157
|
-
<div>
|
|
158
|
-
<button @click="start">发送</button>
|
|
159
|
-
<button @click="stop" :disabled="!loading">暂停</button>
|
|
160
|
-
<!-- 销毁实例、释放插件资源并清空事件订阅,不可恢复 -->
|
|
161
|
-
<button @click="close">停止</button>
|
|
162
|
-
<p>{{ status }}</p><!-- close 后为「已停止」 -->
|
|
163
|
-
<pre>{{ text }}</pre>
|
|
164
|
-
</div>
|
|
165
|
-
</template>
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
要点:
|
|
169
|
-
|
|
170
|
-
- `close()` 等价于对实例执行 `destroy()`:中止请求、执行插件销毁回调、清空事件订阅
|
|
171
|
-
- 之后 `status` 变为「已停止」、`loading` 变为 `false`、`instance` 变为 `null`
|
|
172
|
-
- 已输出的 `text` 保留;再次点击「发送」(`start()`)会创建全新实例,从头开始
|
|
173
|
-
- 组件卸载时会自动 `destroy()`,通常无需手动调用;`close()` 适用于「用户明确结束会话、希望立即释放资源」的场景
|
|
174
|
-
- 直接调用 `instance.value?.destroy()` 效果相同,Composable 会通过 `closed` 事件同步复位状态
|
|
175
|
-
|
|
176
|
-
## 高级用法
|
|
177
|
-
|
|
178
|
-
### 响应式 url
|
|
179
|
-
|
|
180
|
-
```vue
|
|
181
|
-
<script setup lang="ts">
|
|
182
|
-
import { ref } from 'vue'
|
|
183
|
-
|
|
184
|
-
const url = ref('https://api.example.com/stream')
|
|
185
|
-
|
|
186
|
-
const { start } = useSSEStream({
|
|
187
|
-
url, // 传 ref,start 时自动取最新值
|
|
188
|
-
method: 'POST',
|
|
189
|
-
})
|
|
190
|
-
</script>
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
### 通过 `instance` 追加插件
|
|
194
|
-
|
|
195
|
-
```vue
|
|
196
|
-
<script setup lang="ts">
|
|
197
|
-
import { watch } from 'vue'
|
|
198
|
-
import { loggerPlugin } from 'sse-stream-plugin-sdk'
|
|
199
|
-
|
|
200
|
-
const { instance } = useSSEStream({ url, method: 'POST' })
|
|
201
|
-
|
|
202
|
-
watch(instance, (sse) => {
|
|
203
|
-
if (sse) sse.use(loggerPlugin())
|
|
204
|
-
})
|
|
205
|
-
</script>
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
### 小程序环境
|
|
209
|
-
|
|
210
|
-
通过 `transport` 传入小程序传输层工厂:
|
|
211
|
-
|
|
212
|
-
```vue
|
|
213
|
-
<script setup lang="ts">
|
|
214
|
-
import { createMpTransport } from 'sse-stream-plugin-sdk'
|
|
215
|
-
|
|
216
|
-
const { start } = useSSEStream({
|
|
217
|
-
url,
|
|
218
|
-
transport: createMpTransport(() => wx),
|
|
219
|
-
})
|
|
220
|
-
</script>
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
## License
|
|
224
|
-
|
|
225
|
-
MIT
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sse-stream-plugin-vue",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Vue 3 composables for sse-stream-plugin-sdk",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"sse-stream-plugin-sdk": "^1.1.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"sse-stream-plugin-sdk": "1.1.
|
|
35
|
+
"sse-stream-plugin-sdk": "1.1.1",
|
|
36
36
|
"vue": "^3.5.42"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|