@newbase-clawchat/openclaw-clawchat 2026.4.24 → 2026.4.30
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/README.md +66 -16
- package/dist/index.js +27 -0
- package/dist/src/api-client.js +156 -0
- package/dist/src/api-types.js +17 -0
- package/dist/src/buffered-stream.js +177 -0
- package/dist/src/channel.js +191 -0
- package/dist/src/client.js +176 -0
- package/dist/src/commands.js +35 -0
- package/dist/src/config.js +214 -0
- package/dist/src/inbound.js +133 -0
- package/dist/src/login.runtime.js +130 -0
- package/dist/src/media-runtime.js +85 -0
- package/dist/src/message-mapper.js +82 -0
- package/dist/src/outbound.js +181 -0
- package/dist/src/protocol.js +38 -0
- package/dist/src/reply-dispatcher.js +440 -0
- package/dist/src/runtime.js +288 -0
- package/dist/src/streaming.js +65 -0
- package/dist/src/tools-schema.js +38 -0
- package/dist/src/tools.js +287 -0
- package/index.ts +2 -1
- package/openclaw.plugin.json +81 -1
- package/package.json +21 -9
- package/skills/clawchat-account-tools/SKILL.md +26 -0
- package/skills/clawchat-activate/SKILL.md +47 -0
- package/src/api-client.test.ts +6 -5
- package/src/api-client.ts +8 -3
- package/src/buffered-stream.test.ts +14 -4
- package/src/buffered-stream.ts +19 -11
- package/src/channel.outbound.test.ts +49 -35
- package/src/channel.test.ts +45 -10
- package/src/channel.ts +26 -17
- package/src/client.test.ts +9 -1
- package/src/client.ts +48 -21
- package/src/commands.test.ts +39 -0
- package/src/commands.ts +41 -0
- package/src/config.test.ts +40 -3
- package/src/config.ts +60 -4
- package/src/inbound.test.ts +9 -6
- package/src/inbound.ts +51 -16
- package/src/login.runtime.test.ts +142 -3
- package/src/login.runtime.ts +59 -26
- package/src/manifest.test.ts +183 -5
- package/src/outbound.test.ts +10 -7
- package/src/outbound.ts +8 -7
- package/src/plugin-entry.test.ts +27 -0
- package/src/protocol.ts +5 -0
- package/src/reply-dispatcher.test.ts +420 -3
- package/src/reply-dispatcher.ts +137 -12
- package/src/runtime.test.ts +23 -7
- package/src/runtime.ts +13 -1
- package/src/streaming.test.ts +12 -9
- package/src/streaming.ts +22 -12
- package/src/tools-schema.ts +28 -19
- package/src/tools.test.ts +181 -40
- package/src/tools.ts +107 -95
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @newbase-clawchat/openclaw-clawchat
|
|
2
2
|
|
|
3
|
-
OpenClaw channel plugin that connects an agent to ClawChat over the ClawChat Protocol v2, using [`@newbase-clawchat/sdk`](https://www.npmjs.com/package/@newbase-clawchat/sdk) for the WebSocket transport plus a small REST surface
|
|
3
|
+
OpenClaw channel plugin that connects an agent to ClawChat over the ClawChat Protocol v2, using [`@newbase-clawchat/sdk`](https://www.npmjs.com/package/@newbase-clawchat/sdk) for the WebSocket transport plus a small REST surface for profile / social / media operations (`/v1/*` plus unversioned `/media/upload`).
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
@@ -10,31 +10,64 @@ OpenClaw channel plugin that connects an agent to ClawChat over the ClawChat Pro
|
|
|
10
10
|
- Outbound text replies in `static` or `stream` mode, with a consolidated final `message.reply`
|
|
11
11
|
- Typing indicators and filtered forwarding for thinking / tool-call content
|
|
12
12
|
- Media fragments (image / file / audio / video) in either direction
|
|
13
|
-
-
|
|
13
|
+
- Invite-code onboarding via `clawchat_activate` or channel login, plus always-registered `clawchat_*` account/media tools
|
|
14
14
|
|
|
15
15
|
## Install
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
#
|
|
19
|
-
openclaw channels setup --channel openclaw-clawchat --code INV-ABC123
|
|
20
|
-
|
|
21
|
-
# Or add as a local extension
|
|
18
|
+
# Add as an OpenClaw extension
|
|
22
19
|
npm i @newbase-clawchat/openclaw-clawchat
|
|
23
20
|
```
|
|
24
21
|
|
|
25
|
-
Requires `openclaw >= 2026.
|
|
22
|
+
Requires `openclaw >= 2026.4.29` as a peer host.
|
|
23
|
+
|
|
24
|
+
For the OpenClaw plugin install/update flow, see [`INSTALL.md`](./INSTALL.md).
|
|
25
|
+
|
|
26
|
+
Example LLM prompt:
|
|
27
|
+
|
|
28
|
+
```text
|
|
29
|
+
Use https://raw.githubusercontent.com/clawling/openclaw-clawchat/refs/heads/main/INSTALL.md to install and activate the ClawChat plugin. The invite code is XXXXXX.
|
|
30
|
+
```
|
|
26
31
|
|
|
27
32
|
## Quick start
|
|
28
33
|
|
|
34
|
+
Send one of these in chat:
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
activate ClawChat with invite code A1B2C3
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
If the plugin is already loaded by the Gateway, the activation skill calls the
|
|
41
|
+
`clawchat_activate` tool and OpenClaw hot-reloads the updated `channels.*`
|
|
42
|
+
credentials. If the tool is not available yet, use channel login as the CLI
|
|
43
|
+
fallback:
|
|
44
|
+
|
|
29
45
|
```bash
|
|
30
|
-
|
|
31
|
-
openclaw channels
|
|
46
|
+
openclaw channels login --channel openclaw-clawchat
|
|
47
|
+
openclaw channels status --probe
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Restart the Gateway after installing/updating the plugin, when config reload is
|
|
51
|
+
disabled, or when the channel probe does not become healthy:
|
|
32
52
|
|
|
33
|
-
|
|
53
|
+
```bash
|
|
54
|
+
openclaw gateway restart
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
If you run the gateway manually instead of as a service and it is not already
|
|
58
|
+
running, start it after login:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
34
61
|
openclaw gateway run
|
|
35
62
|
```
|
|
36
63
|
|
|
37
|
-
|
|
64
|
+
The invite code is not a token; token fields are written only after login. The
|
|
65
|
+
plugin registers `clawchat_activate` and the six account/media tools at plugin
|
|
66
|
+
load time so they stay visible before activation. Before login, account/media
|
|
67
|
+
tools return a config error instead of disappearing; after activation/login, the
|
|
68
|
+
channel is enabled and the same tools read the hot-reloaded token/userId.
|
|
69
|
+
|
|
70
|
+
After activation/login, the channel section is enabled and has credentials:
|
|
38
71
|
|
|
39
72
|
```json5
|
|
40
73
|
{
|
|
@@ -43,8 +76,10 @@ Minimal `~/.openclaw/openclaw.json`:
|
|
|
43
76
|
enabled: true,
|
|
44
77
|
replyMode: "stream",
|
|
45
78
|
forwardThinking: true,
|
|
46
|
-
forwardToolCalls: false
|
|
47
|
-
|
|
79
|
+
forwardToolCalls: false,
|
|
80
|
+
token: "...",
|
|
81
|
+
userId: "...",
|
|
82
|
+
refreshToken: "..."
|
|
48
83
|
}
|
|
49
84
|
}
|
|
50
85
|
}
|
|
@@ -55,10 +90,11 @@ Minimal `~/.openclaw/openclaw.json`:
|
|
|
55
90
|
A minimal browser test harness is bundled under `tools/`:
|
|
56
91
|
|
|
57
92
|
```bash
|
|
58
|
-
|
|
93
|
+
node tools/standalone-webchat-server.mjs
|
|
94
|
+
# Options: --host (default 127.0.0.1), --port (default 4318), --default-ws-url
|
|
59
95
|
```
|
|
60
96
|
|
|
61
|
-
Then open the printed URL
|
|
97
|
+
Then open the printed URL (default `http://127.0.0.1:4318`) to exercise the plugin end to end against a WebSocket relay.
|
|
62
98
|
|
|
63
99
|
## Layout
|
|
64
100
|
|
|
@@ -103,9 +139,23 @@ See [`docs/openclaw-clawchat.md`](./docs/openclaw-clawchat.md) for:
|
|
|
103
139
|
```bash
|
|
104
140
|
# Tests
|
|
105
141
|
npx vitest run
|
|
142
|
+
|
|
143
|
+
# Typecheck
|
|
144
|
+
npm run typecheck
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Tests live next to the source they cover (`*.test.ts`). The development entrypoint stays in TypeScript for the OpenClaw extension loader, while npm installs use the compiled runtime entrypoint generated by `npm run build` / `prepack` under `dist/`.
|
|
148
|
+
|
|
149
|
+
For OpenClaw host SDK/source lookup while developing this plugin, optionally
|
|
150
|
+
clone OpenClaw into `tmp/openclaw`:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
npm run dev:openclaw-source
|
|
154
|
+
# equivalent: git clone --depth=1 https://github.com/openclaw/openclaw.git tmp/openclaw
|
|
106
155
|
```
|
|
107
156
|
|
|
108
|
-
|
|
157
|
+
This checkout is local-only. It is ignored by git and is not required to run the
|
|
158
|
+
plugin tests or publish the package.
|
|
109
159
|
|
|
110
160
|
## License
|
|
111
161
|
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { openclawClawlingPlugin } from "./src/channel.js";
|
|
2
|
+
import { registerOpenclawClawlingCommands } from "./src/commands.js";
|
|
3
|
+
import { setOpenclawClawlingRuntime } from "./src/runtime.js";
|
|
4
|
+
import { registerOpenclawClawlingTools } from "./src/tools.js";
|
|
5
|
+
import { openclawClawlingConfigSchema } from "./src/config.js";
|
|
6
|
+
export default {
|
|
7
|
+
id: "openclaw-clawchat",
|
|
8
|
+
name: "Clawling Chat",
|
|
9
|
+
description: "Clawling Chat Protocol v2 channel plugin (chat-sdk)",
|
|
10
|
+
configSchema: openclawClawlingConfigSchema,
|
|
11
|
+
register(api) {
|
|
12
|
+
setOpenclawClawlingRuntime(api.runtime);
|
|
13
|
+
api.registerChannel({ plugin: openclawClawlingPlugin });
|
|
14
|
+
registerOpenclawClawlingCommands(api);
|
|
15
|
+
registerOpenclawClawlingTools(api);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
// export default defineChannelPluginEntry({
|
|
19
|
+
// id: "openclaw-clawchat",
|
|
20
|
+
// name: "Clawling Chat",
|
|
21
|
+
// description: "Clawling Chat Protocol v2 channel plugin (chat-sdk)",
|
|
22
|
+
// plugin: openclawClawlingPlugin,
|
|
23
|
+
// setRuntime: setOpenclawClawlingRuntime,
|
|
24
|
+
// registerFull(api) {
|
|
25
|
+
// registerOpenclawClawlingTools(api);
|
|
26
|
+
// },
|
|
27
|
+
// });
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { ClawlingApiError, } from "./api-types.js";
|
|
2
|
+
import { CHANNEL_ID } from "./config.js";
|
|
3
|
+
export function createOpenclawClawlingApiClient(opts) {
|
|
4
|
+
if (!/^https?:\/\//i.test(opts.baseUrl)) {
|
|
5
|
+
throw new ClawlingApiError("validation", `openclaw-clawchat baseUrl must start with http:// or https:// (got "${opts.baseUrl}")`);
|
|
6
|
+
}
|
|
7
|
+
const baseUrl = opts.baseUrl.replace(/\/+$/, "");
|
|
8
|
+
const fetchImpl = opts.fetchImpl ?? globalThis.fetch.bind(globalThis);
|
|
9
|
+
function url(path) {
|
|
10
|
+
return `${baseUrl}${path}`;
|
|
11
|
+
}
|
|
12
|
+
function authHeaders(extra = {}) {
|
|
13
|
+
// `X-Device-Id` is sent on every request so the server can correlate
|
|
14
|
+
// activity back to this plugin instance. Callers may override via
|
|
15
|
+
// `extra` (e.g. tests) but the default is the channel id.
|
|
16
|
+
return {
|
|
17
|
+
authorization: `Bearer ${opts.token}`,
|
|
18
|
+
"x-device-id": CHANNEL_ID,
|
|
19
|
+
...extra,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
async function readEnvelope(res, path) {
|
|
23
|
+
if (res.status === 401 || res.status === 403) {
|
|
24
|
+
throw new ClawlingApiError("auth", `unauthorized (status ${res.status})`, {
|
|
25
|
+
status: res.status,
|
|
26
|
+
path,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (!res.ok) {
|
|
30
|
+
const snippet = await res.text().catch(() => "");
|
|
31
|
+
throw new ClawlingApiError("transport", `http ${res.status} ${snippet.slice(0, 200)}`, {
|
|
32
|
+
status: res.status,
|
|
33
|
+
path,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
let parsed;
|
|
37
|
+
try {
|
|
38
|
+
parsed = await res.json();
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
throw new ClawlingApiError("transport", `non-JSON response: ${err instanceof Error ? err.message : String(err)}`, { status: res.status, path });
|
|
42
|
+
}
|
|
43
|
+
// Unified envelope: `{ code: number, msg: string, data: T }`.
|
|
44
|
+
// `code === 0` means success; any other value is a business error whose
|
|
45
|
+
// `msg` is surfaced to callers and `code` is preserved on the error meta.
|
|
46
|
+
const env = parsed;
|
|
47
|
+
const code = typeof env.code === "number" ? env.code : Number.NaN;
|
|
48
|
+
const msg = typeof env.msg === "string"
|
|
49
|
+
? env.msg
|
|
50
|
+
: typeof env.message === "string"
|
|
51
|
+
? env.message
|
|
52
|
+
: "";
|
|
53
|
+
if (!Number.isFinite(code)) {
|
|
54
|
+
throw new ClawlingApiError("transport", "invalid envelope: missing numeric `code`", {
|
|
55
|
+
status: res.status,
|
|
56
|
+
path,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (code !== 0) {
|
|
60
|
+
throw new ClawlingApiError("api", msg || `code=${code}`, {
|
|
61
|
+
code,
|
|
62
|
+
status: res.status,
|
|
63
|
+
path,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return env.data;
|
|
67
|
+
}
|
|
68
|
+
async function call(method, path, init) {
|
|
69
|
+
let res;
|
|
70
|
+
try {
|
|
71
|
+
const requestInit = {
|
|
72
|
+
method,
|
|
73
|
+
headers: authHeaders(init?.headers),
|
|
74
|
+
};
|
|
75
|
+
if (init?.body !== undefined) {
|
|
76
|
+
requestInit.body = init.body;
|
|
77
|
+
}
|
|
78
|
+
res = await fetchImpl(url(path), requestInit);
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
throw new ClawlingApiError("transport", `fetch failed: ${err instanceof Error ? err.message : String(err)}`, { path });
|
|
82
|
+
}
|
|
83
|
+
return await readEnvelope(res, path);
|
|
84
|
+
}
|
|
85
|
+
// All JSON API endpoints live under `/v1/...`. Media upload is the one
|
|
86
|
+
// intentional exception — the upstream server mounts it at `/media/upload`
|
|
87
|
+
// without the version prefix.
|
|
88
|
+
return {
|
|
89
|
+
async getMyProfile() {
|
|
90
|
+
return await call("GET", "/v1/users/me");
|
|
91
|
+
},
|
|
92
|
+
async getUserInfo(userId) {
|
|
93
|
+
return await call("GET", `/v1/users/${encodeURIComponent(userId)}`);
|
|
94
|
+
},
|
|
95
|
+
async listFriends(params) {
|
|
96
|
+
const sp = new URLSearchParams();
|
|
97
|
+
if (typeof params.page === "number")
|
|
98
|
+
sp.set("page", String(params.page));
|
|
99
|
+
if (typeof params.pageSize === "number")
|
|
100
|
+
sp.set("pageSize", String(params.pageSize));
|
|
101
|
+
const q = sp.toString();
|
|
102
|
+
return await call("GET", q ? `/v1/friends?${q}` : "/v1/friends");
|
|
103
|
+
},
|
|
104
|
+
async updateMyProfile(patch) {
|
|
105
|
+
if (!opts.userId?.trim()) {
|
|
106
|
+
throw new ClawlingApiError("validation", "updateMyProfile: userId is required to target /v1/agents/{userId}");
|
|
107
|
+
}
|
|
108
|
+
return await call("PATCH", `/v1/users/me`, {
|
|
109
|
+
body: JSON.stringify(patch),
|
|
110
|
+
headers: { "content-type": "application/json" },
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
async agentsConnect({ code: inviteCode, platform, type }) {
|
|
114
|
+
if (!inviteCode?.trim()) {
|
|
115
|
+
throw new ClawlingApiError("validation", "agentsConnect: inviteCode is required");
|
|
116
|
+
}
|
|
117
|
+
if (!platform?.trim()) {
|
|
118
|
+
throw new ClawlingApiError("validation", "agentsConnect: platform is required");
|
|
119
|
+
}
|
|
120
|
+
if (!type?.trim()) {
|
|
121
|
+
throw new ClawlingApiError("validation", "agentsConnect: type is required");
|
|
122
|
+
}
|
|
123
|
+
return await call("POST", "/v1/agents/connect", {
|
|
124
|
+
// `X-Device-Id` is added globally via `authHeaders` on every request.
|
|
125
|
+
headers: { "content-type": "application/json" },
|
|
126
|
+
body: JSON.stringify({
|
|
127
|
+
code: inviteCode.trim(),
|
|
128
|
+
platform: platform.trim(),
|
|
129
|
+
type: type.trim(),
|
|
130
|
+
}),
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
async uploadMedia(params) {
|
|
134
|
+
const blob = new Blob([new Uint8Array(params.buffer)], {
|
|
135
|
+
type: params.mime ?? "application/octet-stream",
|
|
136
|
+
});
|
|
137
|
+
const file = new File([blob], params.filename, {
|
|
138
|
+
type: params.mime ?? "application/octet-stream",
|
|
139
|
+
});
|
|
140
|
+
const fd = new FormData();
|
|
141
|
+
fd.set("file", file);
|
|
142
|
+
return await call("POST", "/media/upload", { body: fd });
|
|
143
|
+
},
|
|
144
|
+
async uploadAvatar(params) {
|
|
145
|
+
const blob = new Blob([new Uint8Array(params.buffer)], {
|
|
146
|
+
type: params.mime ?? "application/octet-stream",
|
|
147
|
+
});
|
|
148
|
+
const file = new File([blob], params.filename, {
|
|
149
|
+
type: params.mime ?? "application/octet-stream",
|
|
150
|
+
});
|
|
151
|
+
const fd = new FormData();
|
|
152
|
+
fd.set("file", file);
|
|
153
|
+
return await call("POST", "/v1/files/upload-url", { body: fd });
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public-facing types for the Clawling Chat HTTP API.
|
|
3
|
+
*
|
|
4
|
+
* Field names mirror the upstream API (snake_case) so we don't lose
|
|
5
|
+
* fidelity on responses. Tool schemas accept camelCase externally and
|
|
6
|
+
* translate at the api-client boundary.
|
|
7
|
+
*/
|
|
8
|
+
export class ClawlingApiError extends Error {
|
|
9
|
+
kind;
|
|
10
|
+
meta;
|
|
11
|
+
constructor(kind, message, meta) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.kind = kind;
|
|
14
|
+
this.meta = meta;
|
|
15
|
+
this.name = "ClawlingApiError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { emitStreamAdd, emitStreamCreated, emitStreamDone, emitStreamFailed, } from "./client.js";
|
|
2
|
+
/**
|
|
3
|
+
* Merge two views of the same progressively-revealed text.
|
|
4
|
+
*
|
|
5
|
+
* The agent runner may give us either:
|
|
6
|
+
* - full snapshots ("Hel", "Hello", "Hello, world") where each item is
|
|
7
|
+
* a superset of the previous; or
|
|
8
|
+
* - overlapping slices ("hello ", "world hello ") that don't share a
|
|
9
|
+
* prefix but share an overlap at the join.
|
|
10
|
+
*
|
|
11
|
+
* This helper returns a longest-sensible combined string. Ported from
|
|
12
|
+
* `clawling-channel/src/reply-dispatcher.ts`.
|
|
13
|
+
*/
|
|
14
|
+
export function mergeStreamingText(previousText, nextText) {
|
|
15
|
+
const currentSnapshot = typeof previousText === "string" ? previousText : "";
|
|
16
|
+
const incomingText = typeof nextText === "string" ? nextText : "";
|
|
17
|
+
if (!incomingText)
|
|
18
|
+
return currentSnapshot;
|
|
19
|
+
if (!currentSnapshot || incomingText === currentSnapshot)
|
|
20
|
+
return incomingText;
|
|
21
|
+
if (incomingText.startsWith(currentSnapshot))
|
|
22
|
+
return incomingText;
|
|
23
|
+
if (currentSnapshot.startsWith(incomingText))
|
|
24
|
+
return currentSnapshot;
|
|
25
|
+
if (incomingText.includes(currentSnapshot))
|
|
26
|
+
return incomingText;
|
|
27
|
+
if (currentSnapshot.includes(incomingText))
|
|
28
|
+
return currentSnapshot;
|
|
29
|
+
const maxOverlap = Math.min(currentSnapshot.length, incomingText.length);
|
|
30
|
+
for (let overlap = maxOverlap; overlap > 0; overlap -= 1) {
|
|
31
|
+
if (currentSnapshot.slice(-overlap) === incomingText.slice(0, overlap)) {
|
|
32
|
+
return `${currentSnapshot}${incomingText.slice(overlap)}`;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return `${currentSnapshot}${incomingText}`;
|
|
36
|
+
}
|
|
37
|
+
function resolveRouting(options) {
|
|
38
|
+
if (options.routing)
|
|
39
|
+
return options.routing;
|
|
40
|
+
if (options.to)
|
|
41
|
+
return { chatId: options.to.id, chatType: options.to.type };
|
|
42
|
+
throw new Error("openclaw-clawchat buffered stream requires routing");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Build a streaming session wrapper around message.created/add/done events.
|
|
46
|
+
*
|
|
47
|
+
* Usage pattern (matching clawling-channel):
|
|
48
|
+
* const session = openBufferedStreamingSession({...});
|
|
49
|
+
* await session.queueSnapshot("Hel");
|
|
50
|
+
* await session.queueSnapshot("Hello");
|
|
51
|
+
* await session.queueDelta(", world");
|
|
52
|
+
* await session.done();
|
|
53
|
+
*/
|
|
54
|
+
export function openBufferedStreamingSession(options) {
|
|
55
|
+
const routing = resolveRouting(options);
|
|
56
|
+
const emitTyping = options.emitTyping !== false;
|
|
57
|
+
if (emitTyping)
|
|
58
|
+
options.client.typing(routing.chatId, true);
|
|
59
|
+
emitStreamCreated(options.client, {
|
|
60
|
+
messageId: options.messageId,
|
|
61
|
+
routing,
|
|
62
|
+
});
|
|
63
|
+
let bufferedSnapshot = "";
|
|
64
|
+
let flushedSnapshot = "";
|
|
65
|
+
let sequence = -1;
|
|
66
|
+
let flushTimer = null;
|
|
67
|
+
let pendingFlush = Promise.resolve();
|
|
68
|
+
let closed = false;
|
|
69
|
+
const clearTimer = () => {
|
|
70
|
+
if (flushTimer) {
|
|
71
|
+
clearTimeout(flushTimer);
|
|
72
|
+
flushTimer = null;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const performFlush = async () => {
|
|
76
|
+
clearTimer();
|
|
77
|
+
if (closed)
|
|
78
|
+
return;
|
|
79
|
+
if (bufferedSnapshot === flushedSnapshot)
|
|
80
|
+
return;
|
|
81
|
+
const snapshot = bufferedSnapshot;
|
|
82
|
+
const delta = snapshot.slice(flushedSnapshot.length);
|
|
83
|
+
if (!delta)
|
|
84
|
+
return;
|
|
85
|
+
sequence += 1;
|
|
86
|
+
emitStreamAdd(options.client, {
|
|
87
|
+
messageId: options.messageId,
|
|
88
|
+
routing,
|
|
89
|
+
sequence,
|
|
90
|
+
fullText: snapshot,
|
|
91
|
+
textDelta: delta,
|
|
92
|
+
});
|
|
93
|
+
flushedSnapshot = snapshot;
|
|
94
|
+
};
|
|
95
|
+
const flush = async () => {
|
|
96
|
+
pendingFlush = pendingFlush.then(performFlush);
|
|
97
|
+
await pendingFlush;
|
|
98
|
+
};
|
|
99
|
+
const scheduleFlush = () => {
|
|
100
|
+
if (flushTimer || closed)
|
|
101
|
+
return;
|
|
102
|
+
flushTimer = setTimeout(() => {
|
|
103
|
+
flushTimer = null;
|
|
104
|
+
void flush();
|
|
105
|
+
}, options.flushIntervalMs);
|
|
106
|
+
};
|
|
107
|
+
const queueSnapshot = async (snapshot) => {
|
|
108
|
+
if (closed || !snapshot)
|
|
109
|
+
return;
|
|
110
|
+
const base = bufferedSnapshot.length >= flushedSnapshot.length ? bufferedSnapshot : flushedSnapshot;
|
|
111
|
+
const merged = mergeStreamingText(base, snapshot);
|
|
112
|
+
if (merged === bufferedSnapshot)
|
|
113
|
+
return;
|
|
114
|
+
bufferedSnapshot = merged;
|
|
115
|
+
const deltaChars = Math.max(0, bufferedSnapshot.length - flushedSnapshot.length);
|
|
116
|
+
if (deltaChars >= options.minChunkChars || bufferedSnapshot.length >= options.maxBufferChars) {
|
|
117
|
+
await flush();
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
scheduleFlush();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const queueDelta = async (delta) => {
|
|
124
|
+
if (closed || !delta)
|
|
125
|
+
return;
|
|
126
|
+
bufferedSnapshot = `${bufferedSnapshot}${delta}`;
|
|
127
|
+
const deltaChars = Math.max(0, bufferedSnapshot.length - flushedSnapshot.length);
|
|
128
|
+
if (deltaChars >= options.minChunkChars || bufferedSnapshot.length >= options.maxBufferChars) {
|
|
129
|
+
await flush();
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
scheduleFlush();
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const done = async () => {
|
|
136
|
+
if (closed)
|
|
137
|
+
return;
|
|
138
|
+
await flush();
|
|
139
|
+
closed = true;
|
|
140
|
+
clearTimer();
|
|
141
|
+
emitStreamDone(options.client, {
|
|
142
|
+
messageId: options.messageId,
|
|
143
|
+
routing,
|
|
144
|
+
finalSequence: Math.max(sequence, 0),
|
|
145
|
+
finalText: bufferedSnapshot,
|
|
146
|
+
});
|
|
147
|
+
if (emitTyping)
|
|
148
|
+
options.client.typing(routing.chatId, false);
|
|
149
|
+
};
|
|
150
|
+
const fail = async (reason) => {
|
|
151
|
+
if (closed)
|
|
152
|
+
return;
|
|
153
|
+
closed = true;
|
|
154
|
+
clearTimer();
|
|
155
|
+
emitStreamFailed(options.client, {
|
|
156
|
+
messageId: options.messageId,
|
|
157
|
+
routing,
|
|
158
|
+
sequence: Math.max(sequence, 0),
|
|
159
|
+
...(reason !== undefined ? { reason } : {}),
|
|
160
|
+
});
|
|
161
|
+
if (emitTyping)
|
|
162
|
+
options.client.typing(routing.chatId, false);
|
|
163
|
+
};
|
|
164
|
+
return {
|
|
165
|
+
get currentText() {
|
|
166
|
+
return bufferedSnapshot;
|
|
167
|
+
},
|
|
168
|
+
get flushedText() {
|
|
169
|
+
return flushedSnapshot;
|
|
170
|
+
},
|
|
171
|
+
queueSnapshot,
|
|
172
|
+
queueDelta,
|
|
173
|
+
flush,
|
|
174
|
+
done,
|
|
175
|
+
fail,
|
|
176
|
+
};
|
|
177
|
+
}
|