@ably/ai-transport 0.0.1 → 0.2.0
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 +114 -116
- package/dist/ably-ai-transport.js +1743 -961
- package/dist/ably-ai-transport.js.map +1 -1
- package/dist/ably-ai-transport.umd.cjs +1 -1
- package/dist/ably-ai-transport.umd.cjs.map +1 -1
- package/dist/constants.d.ts +117 -39
- package/dist/core/agent.d.ts +29 -0
- package/dist/core/codec/decoder.d.ts +20 -23
- package/dist/core/codec/encoder.d.ts +11 -8
- package/dist/core/codec/index.d.ts +1 -2
- package/dist/core/codec/lifecycle-tracker.d.ts +10 -9
- package/dist/core/codec/types.d.ts +410 -101
- package/dist/core/transport/agent-session.d.ts +10 -0
- package/dist/core/transport/branch-chain.d.ts +43 -0
- package/dist/core/transport/client-session.d.ts +13 -0
- package/dist/core/transport/decode-fold.d.ts +47 -0
- package/dist/core/transport/headers.d.ts +97 -17
- package/dist/core/transport/index.d.ts +5 -3
- package/dist/core/transport/internal/bounded-map.d.ts +20 -0
- package/dist/core/transport/invocation.d.ts +74 -0
- package/dist/core/transport/load-conversation.d.ts +128 -0
- package/dist/core/transport/load-history.d.ts +39 -0
- package/dist/core/transport/pipe-stream.d.ts +9 -8
- package/dist/core/transport/run-manager.d.ts +78 -0
- package/dist/core/transport/tree.d.ts +435 -0
- package/dist/core/transport/types/agent.d.ts +353 -0
- package/dist/core/transport/types/client.d.ts +168 -0
- package/dist/core/transport/types/shared.d.ts +24 -0
- package/dist/core/transport/types/tree.d.ts +315 -0
- package/dist/core/transport/types/view.d.ts +222 -0
- package/dist/core/transport/types.d.ts +13 -402
- package/dist/core/transport/view.d.ts +354 -0
- package/dist/errors.d.ts +37 -9
- package/dist/index.d.ts +6 -6
- package/dist/logger.d.ts +12 -0
- package/dist/react/ably-ai-transport-react.js +1164 -645
- package/dist/react/ably-ai-transport-react.js.map +1 -1
- package/dist/react/ably-ai-transport-react.umd.cjs +1 -1
- package/dist/react/ably-ai-transport-react.umd.cjs.map +1 -1
- package/dist/react/contexts/client-session-context.d.ts +36 -0
- package/dist/react/contexts/client-session-provider.d.ts +53 -0
- package/dist/react/create-session-hooks.d.ts +116 -0
- package/dist/react/index.d.ts +16 -10
- package/dist/react/internal/use-resolved-session.d.ts +36 -0
- package/dist/react/use-ably-messages.d.ts +20 -11
- package/dist/react/use-client-session.d.ts +81 -0
- package/dist/react/use-create-view.d.ts +23 -0
- package/dist/react/use-tree.d.ts +35 -0
- package/dist/react/use-view.d.ts +110 -0
- package/dist/utils.d.ts +32 -23
- package/dist/vercel/ably-ai-transport-vercel.js +2748 -1625
- package/dist/vercel/ably-ai-transport-vercel.js.map +1 -1
- package/dist/vercel/ably-ai-transport-vercel.umd.cjs +1 -1
- package/dist/vercel/ably-ai-transport-vercel.umd.cjs.map +1 -1
- package/dist/vercel/codec/decoder.d.ts +5 -18
- package/dist/vercel/codec/encoder.d.ts +6 -36
- package/dist/vercel/codec/events.d.ts +51 -0
- package/dist/vercel/codec/index.d.ts +24 -12
- package/dist/vercel/codec/reducer.d.ts +144 -0
- package/dist/vercel/codec/tool-transitions.d.ts +50 -0
- package/dist/vercel/index.d.ts +4 -2
- package/dist/vercel/react/ably-ai-transport-vercel-react.js +10298 -1410
- package/dist/vercel/react/ably-ai-transport-vercel-react.js.map +1 -1
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs +70 -1
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs.map +1 -1
- package/dist/vercel/react/contexts/chat-transport-context.d.ts +33 -0
- package/dist/vercel/react/contexts/chat-transport-provider.d.ts +96 -0
- package/dist/vercel/react/index.d.ts +4 -0
- package/dist/vercel/react/use-chat-transport.d.ts +66 -21
- package/dist/vercel/react/use-message-sync.d.ts +31 -12
- package/dist/vercel/run-end-reason.d.ts +29 -0
- package/dist/vercel/transport/chat-transport.d.ts +71 -30
- package/dist/vercel/transport/index.d.ts +25 -18
- package/dist/vercel/transport/run-output-stream.d.ts +56 -0
- package/dist/version.d.ts +2 -0
- package/package.json +47 -34
- package/src/constants.ts +126 -47
- package/src/core/agent.ts +68 -0
- package/src/core/codec/decoder.ts +71 -98
- package/src/core/codec/encoder.ts +115 -58
- package/src/core/codec/index.ts +13 -6
- package/src/core/codec/lifecycle-tracker.ts +10 -9
- package/src/core/codec/types.ts +438 -106
- package/src/core/transport/agent-session.ts +1344 -0
- package/src/core/transport/branch-chain.ts +58 -0
- package/src/core/transport/client-session.ts +775 -0
- package/src/core/transport/decode-fold.ts +91 -0
- package/src/core/transport/headers.ts +182 -19
- package/src/core/transport/index.ts +29 -22
- package/src/core/transport/internal/bounded-map.ts +27 -0
- package/src/core/transport/invocation.ts +98 -0
- package/src/core/transport/load-conversation.ts +355 -0
- package/src/core/transport/load-history.ts +269 -0
- package/src/core/transport/pipe-stream.ts +58 -40
- package/src/core/transport/run-manager.ts +249 -0
- package/src/core/transport/tree.ts +1167 -0
- package/src/core/transport/types/agent.ts +407 -0
- package/src/core/transport/types/client.ts +211 -0
- package/src/core/transport/types/shared.ts +27 -0
- package/src/core/transport/types/tree.ts +344 -0
- package/src/core/transport/types/view.ts +259 -0
- package/src/core/transport/types.ts +13 -527
- package/src/core/transport/view.ts +1271 -0
- package/src/errors.ts +42 -9
- package/src/event-emitter.ts +3 -2
- package/src/index.ts +55 -39
- package/src/logger.ts +14 -1
- package/src/react/contexts/client-session-context.ts +41 -0
- package/src/react/contexts/client-session-provider.tsx +186 -0
- package/src/react/create-session-hooks.ts +141 -0
- package/src/react/index.ts +27 -10
- package/src/react/internal/use-resolved-session.ts +63 -0
- package/src/react/use-ably-messages.ts +47 -19
- package/src/react/use-client-session.ts +201 -0
- package/src/react/use-create-view.ts +72 -0
- package/src/react/use-tree.ts +84 -0
- package/src/react/use-view.ts +275 -0
- package/src/react/vite.config.ts +4 -1
- package/src/utils.ts +63 -45
- package/src/vercel/codec/decoder.ts +336 -255
- package/src/vercel/codec/encoder.ts +348 -196
- package/src/vercel/codec/events.ts +87 -0
- package/src/vercel/codec/index.ts +59 -14
- package/src/vercel/codec/reducer.ts +977 -0
- package/src/vercel/codec/tool-transitions.ts +122 -0
- package/src/vercel/index.ts +7 -3
- package/src/vercel/react/contexts/chat-transport-context.ts +41 -0
- package/src/vercel/react/contexts/chat-transport-provider.tsx +150 -0
- package/src/vercel/react/index.ts +13 -1
- package/src/vercel/react/use-chat-transport.ts +162 -42
- package/src/vercel/react/use-message-sync.ts +121 -22
- package/src/vercel/react/vite.config.ts +4 -2
- package/src/vercel/run-end-reason.ts +78 -0
- package/src/vercel/transport/chat-transport.ts +553 -113
- package/src/vercel/transport/index.ts +40 -28
- package/src/vercel/transport/run-output-stream.ts +170 -0
- package/src/version.ts +2 -0
- package/dist/core/transport/client-transport.d.ts +0 -10
- package/dist/core/transport/conversation-tree.d.ts +0 -9
- package/dist/core/transport/decode-history.d.ts +0 -41
- package/dist/core/transport/server-transport.d.ts +0 -7
- package/dist/core/transport/stream-router.d.ts +0 -19
- package/dist/core/transport/turn-manager.d.ts +0 -34
- package/dist/react/use-active-turns.d.ts +0 -8
- package/dist/react/use-client-transport.d.ts +0 -7
- package/dist/react/use-conversation-tree.d.ts +0 -20
- package/dist/react/use-edit.d.ts +0 -7
- package/dist/react/use-history.d.ts +0 -19
- package/dist/react/use-messages.d.ts +0 -7
- package/dist/react/use-regenerate.d.ts +0 -7
- package/dist/react/use-send.d.ts +0 -7
- package/dist/vercel/codec/accumulator.d.ts +0 -21
- package/src/core/transport/client-transport.ts +0 -959
- package/src/core/transport/conversation-tree.ts +0 -434
- package/src/core/transport/decode-history.ts +0 -337
- package/src/core/transport/server-transport.ts +0 -458
- package/src/core/transport/stream-router.ts +0 -118
- package/src/core/transport/turn-manager.ts +0 -147
- package/src/react/use-active-turns.ts +0 -61
- package/src/react/use-client-transport.ts +0 -37
- package/src/react/use-conversation-tree.ts +0 -71
- package/src/react/use-edit.ts +0 -24
- package/src/react/use-history.ts +0 -111
- package/src/react/use-messages.ts +0 -32
- package/src/react/use-regenerate.ts +0 -24
- package/src/react/use-send.ts +0 -25
- package/src/vercel/codec/accumulator.ts +0 -603
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ably/ai-transport",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Ably transport and codecs for building AI applications with Ably.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/ably-ai-transport.umd.cjs",
|
|
@@ -36,24 +36,6 @@
|
|
|
36
36
|
},
|
|
37
37
|
"./package.json": "./package.json"
|
|
38
38
|
},
|
|
39
|
-
"scripts": {
|
|
40
|
-
"build": "npm run build:clean && npm run build:core && npm run build:react && npm run build:vercel && npm run build:vercel-react",
|
|
41
|
-
"build:clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
42
|
-
"build:core": "vite build --config ./src/vite.config.ts",
|
|
43
|
-
"build:react": "vite build --config ./src/react/vite.config.ts --emptyOutDir",
|
|
44
|
-
"build:vercel": "vite build --config ./src/vercel/vite.config.ts --emptyOutDir",
|
|
45
|
-
"build:vercel-react": "vite build --config ./src/vercel/react/vite.config.ts --emptyOutDir",
|
|
46
|
-
"prepare": "npm run build",
|
|
47
|
-
"lint": "eslint .",
|
|
48
|
-
"lint:fix": "eslint --fix .; (npm run format > /dev/null)",
|
|
49
|
-
"format": "prettier --list-different --write src demo/vercel/react/*/src",
|
|
50
|
-
"format:check": "prettier --check src demo/vercel/react/*/src",
|
|
51
|
-
"typecheck": "tsc --noEmit",
|
|
52
|
-
"test": "vitest run",
|
|
53
|
-
"test:integration": "vitest run --config vitest.config.integration.ts",
|
|
54
|
-
"check:error-codes": "tsx scripts/validate-error-codes.ts",
|
|
55
|
-
"precommit": "npm run format:check && npm run lint && npm run typecheck"
|
|
56
|
-
},
|
|
57
39
|
"files": [
|
|
58
40
|
"dist/**",
|
|
59
41
|
"src/**",
|
|
@@ -78,7 +60,14 @@
|
|
|
78
60
|
},
|
|
79
61
|
"homepage": "https://github.com/ably/ably-ai-transport-js#readme",
|
|
80
62
|
"engines": {
|
|
81
|
-
"node": ">=
|
|
63
|
+
"node": ">=22.0.0"
|
|
64
|
+
},
|
|
65
|
+
"devEngines": {
|
|
66
|
+
"packageManager": {
|
|
67
|
+
"name": "pnpm",
|
|
68
|
+
"version": "11.3.0",
|
|
69
|
+
"onFail": "download"
|
|
70
|
+
}
|
|
82
71
|
},
|
|
83
72
|
"peerDependencies": {
|
|
84
73
|
"ably": "^2.21.0",
|
|
@@ -94,30 +83,54 @@
|
|
|
94
83
|
}
|
|
95
84
|
},
|
|
96
85
|
"devDependencies": {
|
|
97
|
-
"@
|
|
86
|
+
"@ai-sdk/react": "^3.0.151",
|
|
87
|
+
"@eslint/compat": "^2.0.5",
|
|
98
88
|
"@eslint/eslintrc": "^3.3.0",
|
|
99
|
-
"@eslint/js": "^
|
|
89
|
+
"@eslint/js": "^10.0.1",
|
|
100
90
|
"@testing-library/react": "^16.3.2",
|
|
91
|
+
"@types/node": "^25.0.0",
|
|
101
92
|
"@types/react": "^19.2.14",
|
|
102
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
103
|
-
"@typescript-eslint/parser": "^8.
|
|
93
|
+
"@typescript-eslint/eslint-plugin": "^8.58.2",
|
|
94
|
+
"@typescript-eslint/parser": "^8.58.2",
|
|
95
|
+
"@vitest/coverage-v8": "^4.1.2",
|
|
104
96
|
"ai": "^6.0.137",
|
|
105
|
-
"eslint": "^
|
|
106
|
-
"eslint-plugin-import": "^
|
|
107
|
-
"eslint-plugin-jsdoc": "^
|
|
97
|
+
"eslint": "^10.2.0",
|
|
98
|
+
"eslint-plugin-import-x": "^4.16.2",
|
|
99
|
+
"eslint-plugin-jsdoc": "^62.9.0",
|
|
108
100
|
"eslint-plugin-prefer-arrow-functions": "^3.6.2",
|
|
109
|
-
"eslint-plugin-security": "^
|
|
110
|
-
"eslint-plugin-simple-import-sort": "^
|
|
111
|
-
"eslint-plugin-unicorn": "^
|
|
112
|
-
"globals": "^
|
|
101
|
+
"eslint-plugin-security": "^4.0.0",
|
|
102
|
+
"eslint-plugin-simple-import-sort": "^13.0.0",
|
|
103
|
+
"eslint-plugin-unicorn": "^64.0.0",
|
|
104
|
+
"globals": "^17.5.0",
|
|
113
105
|
"jiti": "^2.6.1",
|
|
114
106
|
"jsdom": "^29.0.1",
|
|
115
107
|
"prettier": "^3.5.3",
|
|
116
108
|
"tsx": "^4.21.0",
|
|
117
|
-
"
|
|
118
|
-
"
|
|
109
|
+
"typedoc": "^0.28.13",
|
|
110
|
+
"typedoc-plugin-no-inherit": "^1.5.0",
|
|
111
|
+
"typescript": "^6.0.2",
|
|
112
|
+
"typescript-eslint": "^8.58.2",
|
|
119
113
|
"vite": "^8.0.0",
|
|
120
114
|
"vite-plugin-dts": "^4.5.4",
|
|
121
115
|
"vitest": "^4.1.0"
|
|
116
|
+
},
|
|
117
|
+
"scripts": {
|
|
118
|
+
"build": "pnpm run build:clean && pnpm run build:core && pnpm run build:react && pnpm run build:vercel && pnpm run build:vercel-react",
|
|
119
|
+
"build:clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
120
|
+
"build:core": "vite build --config ./src/vite.config.ts",
|
|
121
|
+
"build:react": "vite build --config ./src/react/vite.config.ts --emptyOutDir",
|
|
122
|
+
"build:vercel": "vite build --config ./src/vercel/vite.config.ts --emptyOutDir",
|
|
123
|
+
"build:vercel-react": "vite build --config ./src/vercel/react/vite.config.ts --emptyOutDir",
|
|
124
|
+
"lint": "eslint .",
|
|
125
|
+
"lint:fix": "eslint --fix .; (pnpm run format > /dev/null)",
|
|
126
|
+
"format": "prettier --list-different --write .",
|
|
127
|
+
"format:check": "prettier --check .",
|
|
128
|
+
"typecheck": "tsc --noEmit",
|
|
129
|
+
"test": "vitest run",
|
|
130
|
+
"test:integration": "vitest run --config vitest.config.integration.ts",
|
|
131
|
+
"check:error-codes": "tsx scripts/validate-error-codes.ts",
|
|
132
|
+
"precommit": "pnpm run format:check && pnpm run lint && pnpm run typecheck",
|
|
133
|
+
"docs": "typedoc",
|
|
134
|
+
"docs:lint": "typedoc --emit none"
|
|
122
135
|
}
|
|
123
|
-
}
|
|
136
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared constants used by both codec and transport layers.
|
|
3
3
|
*
|
|
4
|
-
* Header constants define the
|
|
5
|
-
* name constants define the
|
|
4
|
+
* Header constants define the transport wire header names. Message and event
|
|
5
|
+
* name constants define the session lifecycle signals on the channel.
|
|
6
6
|
*
|
|
7
7
|
* These live at the top level (not in codec/ or transport/) because both
|
|
8
8
|
* layers need them — the codec core reads/writes stream and status headers,
|
|
9
|
-
* while the transport layer reads/writes
|
|
9
|
+
* while the transport layer reads/writes run, cancel, and role headers.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
// ---------------------------------------------------------------------------
|
|
@@ -14,85 +14,164 @@
|
|
|
14
14
|
// ---------------------------------------------------------------------------
|
|
15
15
|
|
|
16
16
|
/** Header: whether this Ably message uses streaming (message appends) or is discrete. Always "true" or "false". */
|
|
17
|
-
export const HEADER_STREAM = '
|
|
17
|
+
export const HEADER_STREAM = 'stream';
|
|
18
18
|
|
|
19
|
-
/** Header: lifecycle status of a streamed message. Only set when
|
|
20
|
-
export const HEADER_STATUS = '
|
|
19
|
+
/** Header: lifecycle status of a streamed message. Only set when stream is "true". One of "streaming", "complete", or "cancelled". */
|
|
20
|
+
export const HEADER_STATUS = 'status';
|
|
21
21
|
|
|
22
22
|
/** Header: stream identity. Set by the encoder on every streamed message; read by the decoder to correlate streams. */
|
|
23
|
-
export const HEADER_STREAM_ID = '
|
|
23
|
+
export const HEADER_STREAM_ID = 'stream-id';
|
|
24
|
+
|
|
25
|
+
/** Header: marks a message as a discrete message part (from writeMessages). Set by publishDiscreteBatch; not set on lifecycle events from publishDiscrete. */
|
|
26
|
+
export const HEADER_DISCRETE = 'discrete';
|
|
24
27
|
|
|
25
28
|
// ---------------------------------------------------------------------------
|
|
26
|
-
// Identity headers (used by transport for
|
|
29
|
+
// Identity headers (used by transport for run correlation)
|
|
27
30
|
// ---------------------------------------------------------------------------
|
|
28
31
|
|
|
29
|
-
/** Header:
|
|
30
|
-
export const
|
|
32
|
+
/** Header: run correlation ID. Set on every agent-published message and on continuation client inputs, but omitted from the originating fresh client input (the agent mints the run-id at run-start). */
|
|
33
|
+
export const HEADER_RUN_ID = 'run-id';
|
|
34
|
+
|
|
35
|
+
/** Header: invocation correlation ID; identifies a specific invocation under a run. Agent-minted and stamped by the agent on every event it publishes for the invocation — run lifecycle (run-start/resume/suspend/end) and assistant outputs. Never set by the client on its input. */
|
|
36
|
+
export const HEADER_INVOCATION_ID = 'invocation-id';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Header: per-event identifier stamped by the client on every
|
|
40
|
+
* client-published event in a send — user-message events AND amend
|
|
41
|
+
* events (tool-approval responses, client tool outputs). Distinct from
|
|
42
|
+
* `codec-message-id` so it survives edits/retries that reuse the same
|
|
43
|
+
* codec-message-id, and so amend events that target an existing message can
|
|
44
|
+
* carry their own per-send identity. The invocation body lists every
|
|
45
|
+
* inputEventId the agent must observe on the channel before starting LLM
|
|
46
|
+
* work — see `Run.start()`'s input-event lookup.
|
|
47
|
+
*/
|
|
48
|
+
export const HEADER_EVENT_ID = 'event-id';
|
|
31
49
|
|
|
32
50
|
/** Header: message identity. Assigned per message (user or assistant). Used for optimistic reconciliation on the client. */
|
|
33
|
-
export const
|
|
51
|
+
export const HEADER_CODEC_MESSAGE_ID = 'codec-message-id';
|
|
52
|
+
|
|
53
|
+
/** Header: clientId of the user who initiated the run. Stamped by the client on its user input and re-stamped by the agent on the run's lifecycle and stream messages. */
|
|
54
|
+
export const HEADER_RUN_CLIENT_ID = 'run-client-id';
|
|
34
55
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Header: clientId of the input event (the `ai-input`) that drove the
|
|
58
|
+
* current invocation. The agent reads the publisher's Ably-level `clientId`
|
|
59
|
+
* from the triggering input event on the channel and re-stamps it as
|
|
60
|
+
* `input-client-id` on every event it publishes for that invocation
|
|
61
|
+
* (run lifecycle and assistant outputs). May differ from
|
|
62
|
+
* `run-client-id` on continuation invocations driven by an input
|
|
63
|
+
* from a non-owner (e.g. a tool-result publish from a different client).
|
|
64
|
+
* Not stamped on `ai-input` events themselves — the wire publisher's
|
|
65
|
+
* Ably `clientId` already conveys that.
|
|
66
|
+
*/
|
|
67
|
+
export const HEADER_INPUT_CLIENT_ID = 'input-client-id';
|
|
37
68
|
|
|
38
69
|
/** Header: message role (e.g. "user", "assistant"). */
|
|
39
|
-
export const HEADER_ROLE = '
|
|
70
|
+
export const HEADER_ROLE = 'role';
|
|
40
71
|
|
|
41
72
|
// ---------------------------------------------------------------------------
|
|
42
|
-
//
|
|
73
|
+
// Fork / branching headers
|
|
43
74
|
// ---------------------------------------------------------------------------
|
|
44
75
|
|
|
45
|
-
/** Header:
|
|
46
|
-
export const
|
|
47
|
-
|
|
48
|
-
/** Header: cancel all turns belonging to the sender's clientId. */
|
|
49
|
-
export const HEADER_CANCEL_OWN = 'x-ably-cancel-own';
|
|
76
|
+
/** Header: the codec-message-id of the immediately preceding message in this branch. */
|
|
77
|
+
export const HEADER_PARENT = 'parent';
|
|
50
78
|
|
|
51
|
-
/** Header:
|
|
52
|
-
export const
|
|
79
|
+
/** Header: the codec-message-id of the message this one replaces (creates a fork). */
|
|
80
|
+
export const HEADER_FORK_OF = 'fork-of';
|
|
53
81
|
|
|
54
|
-
/**
|
|
55
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Header: the codec-message-id of the assistant message this run regenerates.
|
|
84
|
+
*
|
|
85
|
+
* Stamped on the regenerate wire (and echoed on `run-start`) when the
|
|
86
|
+
* client requested a regeneration. A regenerate run parents at the SAME input
|
|
87
|
+
* node as the reply it regenerates, so it joins that input's reply runs as a
|
|
88
|
+
* same-parent sibling (no fork-of). The View consults this header to resolve
|
|
89
|
+
* the message-level sibling group and to drop the regenerated message from
|
|
90
|
+
* earlier Runs in the visible chain (Spec: AIT-CT13d).
|
|
91
|
+
*/
|
|
92
|
+
export const HEADER_MSG_REGENERATE = 'msg-regenerate';
|
|
56
93
|
|
|
57
94
|
// ---------------------------------------------------------------------------
|
|
58
|
-
//
|
|
95
|
+
// Run lifecycle headers
|
|
59
96
|
// ---------------------------------------------------------------------------
|
|
60
97
|
|
|
61
|
-
/** Header:
|
|
62
|
-
export const
|
|
98
|
+
/** Header: reason a run ended (on ai-run-end messages). */
|
|
99
|
+
export const HEADER_RUN_REASON = 'run-reason';
|
|
63
100
|
|
|
64
|
-
/**
|
|
65
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Header: the `codec-message-id` of the input event that triggered the run.
|
|
103
|
+
* The triggering input is the one whose `event-id` matches the invocation's
|
|
104
|
+
* `inputEventId` (the last input of the originating send). The agent
|
|
105
|
+
* re-stamps it on every event it publishes for the invocation (run
|
|
106
|
+
* lifecycle + assistant outputs), mirroring `input-client-id`. This is the
|
|
107
|
+
* codec-message-id the client owns at send time, so it lets the client
|
|
108
|
+
* correlate any of those events back to the originating input without
|
|
109
|
+
* depending on a client-minted run-id or invocation-id.
|
|
110
|
+
*/
|
|
111
|
+
export const HEADER_INPUT_CODEC_MESSAGE_ID = 'input-codec-message-id';
|
|
66
112
|
|
|
67
113
|
// ---------------------------------------------------------------------------
|
|
68
|
-
//
|
|
114
|
+
// Run-end error headers (set on `ai-run-end` when `run-reason: error`)
|
|
69
115
|
// ---------------------------------------------------------------------------
|
|
70
116
|
|
|
71
|
-
/** Header:
|
|
72
|
-
export const
|
|
117
|
+
/** Header: numeric error code accompanying an `ai-run-end` with reason `error`. */
|
|
118
|
+
export const HEADER_ERROR_CODE = 'error-code';
|
|
119
|
+
|
|
120
|
+
/** Header: human-readable error message accompanying an `ai-run-end` with reason `error`. */
|
|
121
|
+
export const HEADER_ERROR_MESSAGE = 'error-message';
|
|
73
122
|
|
|
74
123
|
// ---------------------------------------------------------------------------
|
|
75
124
|
// Message / event names
|
|
76
125
|
// ---------------------------------------------------------------------------
|
|
77
126
|
|
|
78
|
-
/**
|
|
79
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Message name: client->agent cancel intent. Targets a run by `run-id` (a
|
|
129
|
+
* continuation, whose run-id the client already knows) and/or by
|
|
130
|
+
* `input-codec-message-id` (a fresh send, whose run-id the agent mints at
|
|
131
|
+
* run-start — so the client can only key the cancel by the triggering input's
|
|
132
|
+
* codec-message-id it owns at send time). The agent resolves whichever is
|
|
133
|
+
* present to the registered run; a cancel that arrives before the run is known
|
|
134
|
+
* (the input-event lookup hasn't resolved the input id to a run yet) is
|
|
135
|
+
* buffered by `input-codec-message-id` and honoured when the run resolves it.
|
|
136
|
+
* Also carries an `event-id` so channel rewind redelivers it to a per-request /
|
|
137
|
+
* serverless agent that attaches after the cancel was published.
|
|
138
|
+
*/
|
|
139
|
+
export const EVENT_CANCEL = 'ai-cancel';
|
|
80
140
|
|
|
81
|
-
/** Message name: server publishes this to signal a
|
|
82
|
-
export const
|
|
141
|
+
/** Message name: server publishes this to signal a run has started. */
|
|
142
|
+
export const EVENT_RUN_START = 'ai-run-start';
|
|
83
143
|
|
|
84
|
-
/**
|
|
85
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Message name: server publishes this to signal a run has suspended — paused
|
|
146
|
+
* awaiting participant input (e.g. a client tool result or approval) without
|
|
147
|
+
* ending. The run stays live and may be resumed under the same `runId`.
|
|
148
|
+
* Distinct from `ai-run-end`, which is terminal.
|
|
149
|
+
*/
|
|
150
|
+
export const EVENT_RUN_SUSPEND = 'ai-run-suspend';
|
|
86
151
|
|
|
87
|
-
/**
|
|
88
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Message name: server publishes this when a subsequent invocation re-enters an
|
|
154
|
+
* already-started run (e.g. a tool-result follow-up under the same `runId`).
|
|
155
|
+
* A pure re-entry signal: unlike `ai-run-start` it carries no `parent` / `fork-of`
|
|
156
|
+
* (the original `ai-run-start` already established the run's structure).
|
|
157
|
+
*/
|
|
158
|
+
export const EVENT_RUN_RESUME = 'ai-run-resume';
|
|
89
159
|
|
|
90
|
-
/** Message name:
|
|
91
|
-
export const
|
|
160
|
+
/** Message name: server publishes this to signal a run has ended. */
|
|
161
|
+
export const EVENT_RUN_END = 'ai-run-end';
|
|
92
162
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
163
|
+
/**
|
|
164
|
+
* Message name: every agent-published codec event (text, reasoning, tool calls,
|
|
165
|
+
* tool outputs, lifecycle helpers, file / source parts, data-* chunks) rides
|
|
166
|
+
* this single wire name. The codec event's own `type` is carried in the
|
|
167
|
+
* codec-level `type` header so the decoder can dispatch.
|
|
168
|
+
*/
|
|
169
|
+
export const EVENT_AI_OUTPUT = 'ai-output';
|
|
96
170
|
|
|
97
|
-
/**
|
|
98
|
-
|
|
171
|
+
/**
|
|
172
|
+
* Message name: every client-published codec event (user-message parts,
|
|
173
|
+
* tool-approval responses, regenerate signals) rides this single wire
|
|
174
|
+
* name. The codec event's own kind is carried in the codec-level `type`
|
|
175
|
+
* header so the decoder can dispatch.
|
|
176
|
+
*/
|
|
177
|
+
export const EVENT_AI_INPUT = 'ai-input';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps the two paths chat-js uses (see ChatClient._addAgent): the
|
|
3
|
+
* `options.agents` mutation (read by ably-js when opening the initial
|
|
4
|
+
* WebSocket) and the `params.agent` channel option (sent on ATTACH so
|
|
5
|
+
* an already-open connection still carries the identifier).
|
|
6
|
+
*
|
|
7
|
+
* `options.agents` is a private API on the Realtime client — no public
|
|
8
|
+
* typed accessor exists in the `ably` package — so this module casts to a
|
|
9
|
+
* `RealtimeWithOptions` shape to write it.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type * as Ably from 'ably';
|
|
13
|
+
|
|
14
|
+
import { VERSION } from '../version.js';
|
|
15
|
+
|
|
16
|
+
interface RealtimeWithOptions extends Ably.Realtime {
|
|
17
|
+
options: { agents?: Record<string, string | undefined> };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const SDK_NAME = 'ai-transport-js';
|
|
21
|
+
|
|
22
|
+
/** Internal shape a codec may carry to opt into Ably-Agent header registration. */
|
|
23
|
+
interface AdapterTagHolder {
|
|
24
|
+
readonly adapterTag?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Merge `agents` into `client.options.agents` and return the space-separated
|
|
29
|
+
* `params.agent` string for channel ATTACH.
|
|
30
|
+
* @param client - The Ably Realtime client to mutate.
|
|
31
|
+
* @param agents - Map of agent-name to version strings to register.
|
|
32
|
+
* @returns Channel options containing `params.agent` for `channels.get`.
|
|
33
|
+
*/
|
|
34
|
+
const injectAgents = (
|
|
35
|
+
client: Ably.Realtime,
|
|
36
|
+
// CAST: Ably.Realtime's public type omits `options.agents`, but the SDK
|
|
37
|
+
// does carry it at runtime. ably-chat-js relies on the same shape — see
|
|
38
|
+
// ChatClient._addAgent in https://github.com/ably/ably-chat-js.
|
|
39
|
+
agents: Record<string, string>,
|
|
40
|
+
): { params: { agent: string } } => {
|
|
41
|
+
const realtime = client as RealtimeWithOptions;
|
|
42
|
+
realtime.options.agents = { ...realtime.options.agents, ...agents };
|
|
43
|
+
const agentString = Object.entries(agents)
|
|
44
|
+
.map(([name, version]) => `${name}/${version}`)
|
|
45
|
+
.join(' ');
|
|
46
|
+
return { params: { agent: agentString } };
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Register this SDK (and optionally a codec) on the supplied Realtime client
|
|
51
|
+
* and return the channel options the caller should pass to
|
|
52
|
+
* `client.channels.get(...)` so the agent is also carried on channel ATTACH.
|
|
53
|
+
* Sets `options.agents['ai-transport-js'] = VERSION`. When the codec carries
|
|
54
|
+
* an internal `adapterTag` field (via {@link AdapterTagHolder}), also sets
|
|
55
|
+
* `options.agents[adapterTag] = VERSION`.
|
|
56
|
+
* Idempotent — repeated calls with the same client and codec produce the same keys/values.
|
|
57
|
+
* Spec: AIT-CT1a, AIT-CT1a2, AIT-CT1a3, AIT-ST1a, AIT-ST1a2, AIT-ST1a3.
|
|
58
|
+
* @param client - The Ably Realtime client to register on.
|
|
59
|
+
* @param codec - The codec instance; cast to {@link AdapterTagHolder} to detect an optional identifier.
|
|
60
|
+
* @returns Channel options containing `params.agent` for `channels.get`.
|
|
61
|
+
*/
|
|
62
|
+
export const registerAgent = (client: Ably.Realtime, codec?: unknown): { params: { agent: string } } => {
|
|
63
|
+
// CAST: AdapterTagHolder is an internal opt-in shape — not part of the public Codec interface.
|
|
64
|
+
const adapterTag = (codec as AdapterTagHolder | undefined)?.adapterTag;
|
|
65
|
+
const agents: Record<string, string> = { [SDK_NAME]: VERSION };
|
|
66
|
+
if (adapterTag) agents[adapterTag] = VERSION;
|
|
67
|
+
return injectAgents(client, agents);
|
|
68
|
+
};
|