@ably/ai-transport 0.1.0 → 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 +91 -100
- package/dist/ably-ai-transport.js +1553 -1238
- 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 +116 -42
- 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 +407 -115
- 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 +96 -18
- package/dist/core/transport/index.d.ts +5 -6
- 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 -9
- package/dist/core/transport/run-manager.d.ts +78 -0
- package/dist/core/transport/tree.d.ts +373 -109
- 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 -553
- package/dist/core/transport/view.d.ts +272 -84
- package/dist/errors.d.ts +21 -10
- package/dist/index.d.ts +6 -8
- package/dist/logger.d.ts +12 -0
- package/dist/react/ably-ai-transport-react.js +976 -990
- 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 +12 -12
- package/dist/react/internal/use-resolved-session.d.ts +36 -0
- package/dist/react/use-ably-messages.d.ts +17 -14
- package/dist/react/use-client-session.d.ts +81 -0
- package/dist/react/use-create-view.d.ts +14 -13
- package/dist/react/use-tree.d.ts +30 -15
- package/dist/react/use-view.d.ts +82 -51
- package/dist/utils.d.ts +32 -23
- package/dist/vercel/ably-ai-transport-vercel.js +2573 -2086
- 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 +2 -2
- package/dist/vercel/index.d.ts +4 -5
- package/dist/vercel/react/ably-ai-transport-vercel-react.js +3907 -3266
- package/dist/vercel/react/ably-ai-transport-vercel-react.js.map +1 -1
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs +33 -8
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs.map +1 -1
- package/dist/vercel/react/contexts/chat-transport-context.d.ts +7 -6
- package/dist/vercel/react/contexts/chat-transport-provider.d.ts +53 -41
- package/dist/vercel/react/index.d.ts +1 -2
- package/dist/vercel/react/use-chat-transport.d.ts +30 -26
- package/dist/vercel/react/use-message-sync.d.ts +17 -30
- package/dist/vercel/run-end-reason.d.ts +29 -0
- package/dist/vercel/transport/chat-transport.d.ts +43 -24
- package/dist/vercel/transport/index.d.ts +25 -21
- package/dist/vercel/transport/run-output-stream.d.ts +56 -0
- package/dist/version.d.ts +2 -0
- package/package.json +30 -23
- package/src/constants.ts +124 -51
- package/src/core/agent.ts +68 -0
- package/src/core/codec/decoder.ts +71 -98
- package/src/core/codec/encoder.ts +113 -65
- package/src/core/codec/index.ts +13 -6
- package/src/core/codec/lifecycle-tracker.ts +10 -9
- package/src/core/codec/types.ts +436 -120
- 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 +181 -22
- package/src/core/transport/index.ts +25 -26
- 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 +54 -39
- package/src/core/transport/run-manager.ts +249 -0
- package/src/core/transport/tree.ts +926 -308
- 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 -706
- package/src/core/transport/view.ts +864 -433
- package/src/errors.ts +22 -9
- package/src/event-emitter.ts +3 -2
- package/src/index.ts +52 -41
- 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 +23 -13
- package/src/react/internal/use-resolved-session.ts +63 -0
- package/src/react/use-ably-messages.ts +32 -22
- package/src/react/use-client-session.ts +201 -0
- package/src/react/use-create-view.ts +33 -29
- package/src/react/use-tree.ts +61 -30
- package/src/react/use-view.ts +139 -97
- package/src/utils.ts +63 -45
- package/src/vercel/codec/decoder.ts +336 -258
- package/src/vercel/codec/encoder.ts +343 -205
- package/src/vercel/codec/events.ts +87 -0
- package/src/vercel/codec/index.ts +60 -13
- package/src/vercel/codec/reducer.ts +977 -0
- package/src/vercel/codec/tool-transitions.ts +2 -2
- package/src/vercel/index.ts +6 -19
- package/src/vercel/react/contexts/chat-transport-context.ts +7 -6
- package/src/vercel/react/contexts/chat-transport-provider.tsx +87 -59
- package/src/vercel/react/index.ts +3 -5
- package/src/vercel/react/use-chat-transport.ts +47 -49
- package/src/vercel/react/use-message-sync.ts +80 -39
- package/src/vercel/run-end-reason.ts +78 -0
- package/src/vercel/transport/chat-transport.ts +392 -98
- package/src/vercel/transport/index.ts +39 -38
- 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/decode-history.d.ts +0 -43
- package/dist/core/transport/server-transport.d.ts +0 -7
- package/dist/core/transport/stream-router.d.ts +0 -29
- package/dist/core/transport/turn-manager.d.ts +0 -37
- package/dist/react/contexts/transport-context.d.ts +0 -31
- package/dist/react/contexts/transport-provider.d.ts +0 -49
- package/dist/react/create-transport-hooks.d.ts +0 -124
- package/dist/react/use-active-turns.d.ts +0 -12
- package/dist/react/use-client-transport.d.ts +0 -80
- package/dist/vercel/codec/accumulator.d.ts +0 -21
- package/dist/vercel/react/use-staged-add-tool-approval-response.d.ts +0 -30
- package/dist/vercel/tool-approvals.d.ts +0 -124
- package/dist/vercel/tool-events.d.ts +0 -26
- package/src/core/transport/client-transport.ts +0 -977
- package/src/core/transport/decode-history.ts +0 -485
- package/src/core/transport/server-transport.ts +0 -612
- package/src/core/transport/stream-router.ts +0 -136
- package/src/core/transport/turn-manager.ts +0 -165
- package/src/react/contexts/transport-context.ts +0 -37
- package/src/react/contexts/transport-provider.tsx +0 -164
- package/src/react/create-transport-hooks.ts +0 -144
- package/src/react/use-active-turns.ts +0 -72
- package/src/react/use-client-transport.ts +0 -197
- package/src/vercel/codec/accumulator.ts +0 -588
- package/src/vercel/react/use-staged-add-tool-approval-response.ts +0 -87
- package/src/vercel/tool-approvals.ts +0 -380
- package/src/vercel/tool-events.ts +0 -53
|
@@ -1,73 +1,70 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import { jsx as
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}({}), ee = (e) => ({
|
|
1
|
+
import * as e from "ably";
|
|
2
|
+
import { useAbly as t } from "ably/react";
|
|
3
|
+
import { createContext as n, useCallback as r, useContext as i, useEffect as a, useMemo as o, useRef as s, useState as c } from "react";
|
|
4
|
+
import { jsx as l } from "react/jsx-runtime";
|
|
5
|
+
var u = "status", d = "run-id", f = "invocation-id", p = "event-id", m = "codec-message-id", h = "run-client-id", g = "input-client-id", _ = "role", v = "parent", y = "fork-of", b = "msg-regenerate", x = "input-codec-message-id", S = "error-code", ee = "ai-cancel", C = /* @__PURE__ */ function(e) {
|
|
6
|
+
return e[e.BadRequest = 4e4] = "BadRequest", e[e.InvalidArgument = 40003] = "InvalidArgument", e[e.InsufficientCapability = 40160] = "InsufficientCapability", e[e.EncoderRecoveryFailed = 104e3] = "EncoderRecoveryFailed", e[e.SessionSubscriptionError = 104001] = "SessionSubscriptionError", e[e.CancelListenerError = 104002] = "CancelListenerError", e[e.RunLifecycleError = 104003] = "RunLifecycleError", e[e.SessionClosed = 104004] = "SessionClosed", e[e.SessionSendFailed = 104005] = "SessionSendFailed", e[e.ChannelContinuityLost = 104006] = "ChannelContinuityLost", e[e.ChannelNotReady = 104007] = "ChannelNotReady", e[e.StreamError = 104008] = "StreamError", e[e.InputEventNotFound = 104010] = "InputEventNotFound", e;
|
|
7
|
+
}({}), te = (e) => ({
|
|
9
8
|
logAction: (t, n, r) => {
|
|
10
9
|
e.error(n, { detail: r });
|
|
11
10
|
},
|
|
12
11
|
shouldLog: () => !0
|
|
13
|
-
}),
|
|
12
|
+
}), ne = e.Realtime.EventEmitter, w = class extends ne {
|
|
14
13
|
constructor(e) {
|
|
15
|
-
super(
|
|
14
|
+
super(te(e));
|
|
16
15
|
}
|
|
17
|
-
},
|
|
16
|
+
}, T = /* @__PURE__ */ function(e) {
|
|
18
17
|
return e.Trace = "trace", e.Debug = "debug", e.Info = "info", e.Warn = "warn", e.Error = "error", e.Silent = "silent", e;
|
|
19
|
-
}({}),
|
|
18
|
+
}({}), re = (e, t, n) => {
|
|
20
19
|
let r = n ? `, context: ${JSON.stringify(n)}` : "", i = `[${(/* @__PURE__ */ new Date()).toISOString()}] ${t.valueOf().toUpperCase()} ably-ai-transport: ${e}${r}`;
|
|
21
20
|
switch (t) {
|
|
22
|
-
case
|
|
23
|
-
case
|
|
21
|
+
case "trace":
|
|
22
|
+
case "debug":
|
|
24
23
|
console.log(i);
|
|
25
24
|
break;
|
|
26
|
-
case
|
|
25
|
+
case "info":
|
|
27
26
|
console.info(i);
|
|
28
27
|
break;
|
|
29
|
-
case
|
|
28
|
+
case "warn":
|
|
30
29
|
console.warn(i);
|
|
31
30
|
break;
|
|
32
|
-
case
|
|
31
|
+
case "error":
|
|
33
32
|
console.error(i);
|
|
34
33
|
break;
|
|
35
|
-
case
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
[
|
|
41
|
-
[
|
|
42
|
-
[
|
|
43
|
-
[
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (r === void 0) throw new s.ErrorInfo(`unable to create logger; invalid log level: ${t}`, O.InvalidArgument, 400);
|
|
51
|
-
this._levelNumber = r;
|
|
34
|
+
case "silent": break;
|
|
35
|
+
}
|
|
36
|
+
}, ie = (e) => new ae(e.logHandler ?? re, e.logLevel), E = new Map([
|
|
37
|
+
["trace", 0],
|
|
38
|
+
["debug", 1],
|
|
39
|
+
["info", 2],
|
|
40
|
+
["warn", 3],
|
|
41
|
+
["error", 4],
|
|
42
|
+
["silent", 5]
|
|
43
|
+
]), ae = class t {
|
|
44
|
+
constructor(t, n, r) {
|
|
45
|
+
this._handler = t, this._context = r;
|
|
46
|
+
let i = E.get(n);
|
|
47
|
+
if (i === void 0) throw new e.ErrorInfo(`unable to create logger; invalid log level: ${n}`, C.InvalidArgument, 400);
|
|
48
|
+
this._levelNumber = i;
|
|
52
49
|
}
|
|
53
50
|
trace(e, t) {
|
|
54
|
-
this._write(e,
|
|
51
|
+
this._write(e, "trace", 0, t);
|
|
55
52
|
}
|
|
56
53
|
debug(e, t) {
|
|
57
|
-
this._write(e,
|
|
54
|
+
this._write(e, "debug", 1, t);
|
|
58
55
|
}
|
|
59
56
|
info(e, t) {
|
|
60
|
-
this._write(e,
|
|
57
|
+
this._write(e, "info", 2, t);
|
|
61
58
|
}
|
|
62
59
|
warn(e, t) {
|
|
63
|
-
this._write(e,
|
|
60
|
+
this._write(e, "warn", 3, t);
|
|
64
61
|
}
|
|
65
62
|
error(e, t) {
|
|
66
|
-
this._write(e,
|
|
63
|
+
this._write(e, "error", 4, t);
|
|
67
64
|
}
|
|
68
|
-
withContext(
|
|
69
|
-
let n = [...
|
|
70
|
-
return new
|
|
65
|
+
withContext(e) {
|
|
66
|
+
let n = [...E.entries()].find(([, e]) => e === this._levelNumber)?.[0] ?? "error";
|
|
67
|
+
return new t(this._handler, n, this._mergeContext(e));
|
|
71
68
|
}
|
|
72
69
|
_write(e, t, n, r) {
|
|
73
70
|
n >= this._levelNumber && this._handler(e, t, this._mergeContext(r));
|
|
@@ -78,155 +75,219 @@ var d = e({}), f = e(void 0), p = "x-ably-status", m = "x-ably-turn-id", h = "x-
|
|
|
78
75
|
...e
|
|
79
76
|
} : this._context : e ?? void 0;
|
|
80
77
|
}
|
|
81
|
-
},
|
|
82
|
-
let
|
|
83
|
-
if (!
|
|
84
|
-
let
|
|
85
|
-
|
|
86
|
-
|
|
78
|
+
}, oe = (e, t) => {
|
|
79
|
+
let n = e.extras;
|
|
80
|
+
if (!n || typeof n != "object") return {};
|
|
81
|
+
let r = n.ai;
|
|
82
|
+
if (!r || typeof r != "object") return {};
|
|
83
|
+
let i = r[t];
|
|
84
|
+
return !i || typeof i != "object" ? {} : i;
|
|
85
|
+
}, D = (e) => oe(e, "transport"), O = "0.2.0", k = "ai-transport-js", se = (e, t) => {
|
|
86
|
+
let n = e;
|
|
87
|
+
return n.options.agents = {
|
|
88
|
+
...n.options.agents,
|
|
89
|
+
...t
|
|
90
|
+
}, { params: { agent: Object.entries(t).map(([e, t]) => `${e}/${t}`).join(" ") } };
|
|
91
|
+
}, ce = (e, t) => {
|
|
92
|
+
let n = t?.adapterTag, r = { [k]: O };
|
|
93
|
+
return n && (r[n] = O), se(e, r);
|
|
94
|
+
}, le = (e) => {
|
|
87
95
|
let t = {
|
|
88
96
|
[_]: e.role,
|
|
89
|
-
[m]: e.
|
|
90
|
-
[h]: e.msgId
|
|
97
|
+
[m]: e.codecMessageId
|
|
91
98
|
};
|
|
92
|
-
return e.
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
let t = this._turns.get(e);
|
|
110
|
-
if (!t) return !1;
|
|
111
|
-
this._logger.debug("StreamRouter.closeStream(); closing stream", { turnId: e });
|
|
112
|
-
try {
|
|
113
|
-
t.controller.close();
|
|
114
|
-
} catch {}
|
|
115
|
-
return this._turns.delete(e), !0;
|
|
116
|
-
}
|
|
117
|
-
errorStream(e, t) {
|
|
118
|
-
let n = this._turns.get(e);
|
|
119
|
-
if (!n) return !1;
|
|
120
|
-
this._logger.debug("StreamRouter.errorStream(); erroring stream", { turnId: e });
|
|
121
|
-
try {
|
|
122
|
-
n.controller.error(t);
|
|
123
|
-
} catch {}
|
|
124
|
-
return this._turns.delete(e), !0;
|
|
99
|
+
return e.runId !== void 0 && (t[d] = e.runId), e.runClientId !== void 0 && (t[h] = e.runClientId), e.parent && (t[v] = e.parent), e.forkOf && (t[y] = e.forkOf), e.regenerates && (t[b] = e.regenerates), e.invocationId && (t[f] = e.invocationId), e.inputClientId !== void 0 && (t[g] = e.inputClientId), e.inputCodecMessageId !== void 0 && (t[x] = e.inputCodecMessageId), e.inputEventId && (t[p] = e.inputEventId), t;
|
|
100
|
+
}, A = (e) => e === "ai-run-start" || e === "ai-run-suspend" || e === "ai-run-resume" || e === "ai-run-end", j = (e, t, n) => {
|
|
101
|
+
let r = t[d];
|
|
102
|
+
if (!r) return;
|
|
103
|
+
let i = t["run-client-id"] ?? "";
|
|
104
|
+
if (e === "ai-run-start") {
|
|
105
|
+
let e = t[v], a = t[y], o = t[b];
|
|
106
|
+
return {
|
|
107
|
+
type: "start",
|
|
108
|
+
runId: r,
|
|
109
|
+
clientId: i,
|
|
110
|
+
serial: n,
|
|
111
|
+
invocationId: t["invocation-id"] ?? "",
|
|
112
|
+
...e !== void 0 && { parent: e },
|
|
113
|
+
...a !== void 0 && { forkOf: a },
|
|
114
|
+
...o !== void 0 && { regenerates: o }
|
|
115
|
+
};
|
|
125
116
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
117
|
+
if (e === "ai-run-suspend") return {
|
|
118
|
+
type: "suspend",
|
|
119
|
+
runId: r,
|
|
120
|
+
clientId: i,
|
|
121
|
+
serial: n,
|
|
122
|
+
invocationId: t["invocation-id"] ?? ""
|
|
123
|
+
};
|
|
124
|
+
if (e === "ai-run-resume") return {
|
|
125
|
+
type: "resume",
|
|
126
|
+
runId: r,
|
|
127
|
+
clientId: i,
|
|
128
|
+
serial: n,
|
|
129
|
+
invocationId: t["invocation-id"] ?? ""
|
|
130
|
+
};
|
|
131
|
+
if (e === "ai-run-end") {
|
|
132
|
+
let e = t["run-reason"] ?? "complete";
|
|
133
|
+
return {
|
|
134
|
+
type: "end",
|
|
135
|
+
runId: r,
|
|
136
|
+
clientId: i,
|
|
137
|
+
serial: n,
|
|
138
|
+
invocationId: t["invocation-id"] ?? "",
|
|
139
|
+
reason: e
|
|
140
|
+
};
|
|
135
141
|
}
|
|
136
|
-
|
|
137
|
-
|
|
142
|
+
}, M = (e, t, n) => {
|
|
143
|
+
let r = D(n), i = n.serial;
|
|
144
|
+
if (A(n.name)) {
|
|
145
|
+
let t = j(n.name, r, i);
|
|
146
|
+
return t && e.applyRunLifecycle(t), t;
|
|
147
|
+
}
|
|
148
|
+
let { inputs: a, outputs: o } = t.decode(n);
|
|
149
|
+
(a.length > 0 || o.length > 0 || r["run-id"]) && e.applyMessage({
|
|
150
|
+
inputs: a,
|
|
151
|
+
outputs: o
|
|
152
|
+
}, r, i);
|
|
153
|
+
}, ue = class e {
|
|
154
|
+
constructor(e) {
|
|
155
|
+
this.inputEventId = e.inputEventId, this.sessionName = e.sessionName;
|
|
138
156
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return this._structuralVersion;
|
|
157
|
+
static fromJSON(t) {
|
|
158
|
+
return new e(t);
|
|
142
159
|
}
|
|
143
|
-
|
|
144
|
-
|
|
160
|
+
toJSON() {
|
|
161
|
+
return {
|
|
162
|
+
inputEventId: this.inputEventId,
|
|
163
|
+
sessionName: this.sessionName
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
}, N = (e) => e.kind === "run" ? e.runId : e.codecMessageId, P = (e) => e.kind === "run" ? e.startSerial : e.serial, F = (e, t, n) => {
|
|
167
|
+
let r = e.get(t);
|
|
168
|
+
r || (r = /* @__PURE__ */ new Set(), e.set(t, r)), r.add(n);
|
|
169
|
+
}, I = (e, t, n) => {
|
|
170
|
+
let r = e.get(t);
|
|
171
|
+
r && (r.delete(n), r.size === 0 && e.delete(t));
|
|
172
|
+
}, L = class {
|
|
173
|
+
constructor(e, t) {
|
|
174
|
+
this._nodeIndex = /* @__PURE__ */ new Map(), this._codecMessageIdToNodeKey = /* @__PURE__ */ new Map(), this._sortedNodes = [], this._parentIndex = /* @__PURE__ */ new Map(), this._replyRunsByInput = /* @__PURE__ */ new Map(), this._seqCounter = 0, this._structuralVersion = 0, this._siblingCache = /* @__PURE__ */ new Map(), this._siblingCacheVersion = -1, this._codec = e, this._logger = t, this._emitter = new w(t);
|
|
145
175
|
}
|
|
146
176
|
_compareNodes(e, t) {
|
|
147
|
-
let n = e.node
|
|
177
|
+
let n = P(e.node), r = P(t.node);
|
|
148
178
|
return n === void 0 && r === void 0 ? e.insertSeq - t.insertSeq : n === void 0 ? 1 : r === void 0 || n < r ? -1 : n > r ? 1 : e.insertSeq - t.insertSeq;
|
|
149
179
|
}
|
|
150
|
-
|
|
151
|
-
if (e.node
|
|
152
|
-
this.
|
|
180
|
+
_insertSortedNode(e) {
|
|
181
|
+
if (P(e.node) === void 0) {
|
|
182
|
+
this._sortedNodes.push(e);
|
|
153
183
|
return;
|
|
154
184
|
}
|
|
155
|
-
let t = 0, n = this.
|
|
185
|
+
let t = 0, n = this._sortedNodes.length;
|
|
156
186
|
for (; t < n;) {
|
|
157
|
-
let r = t + n >>> 1, i = this.
|
|
187
|
+
let r = t + n >>> 1, i = this._sortedNodes[r];
|
|
158
188
|
if (!i) break;
|
|
159
189
|
this._compareNodes(i, e) <= 0 ? t = r + 1 : n = r;
|
|
160
190
|
}
|
|
161
|
-
this.
|
|
191
|
+
this._sortedNodes.splice(t, 0, e);
|
|
192
|
+
}
|
|
193
|
+
_removeSortedNode(e) {
|
|
194
|
+
let t = this._sortedNodes.indexOf(e);
|
|
195
|
+
t !== -1 && this._sortedNodes.splice(t, 1);
|
|
162
196
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
197
|
+
_insertNode(e, t, n) {
|
|
198
|
+
this._nodeIndex.set(e, t), this._addToParentIndex(n, e), this._insertSortedNode(t), this._structuralVersion++;
|
|
199
|
+
}
|
|
200
|
+
_promoteSerial(e) {
|
|
201
|
+
this._removeSortedNode(e), this._insertSortedNode(e), this._structuralVersion++;
|
|
202
|
+
}
|
|
203
|
+
_foldInto(e, t, n, r) {
|
|
204
|
+
for (let i of t) try {
|
|
205
|
+
e.node.projection = this._codec.fold(e.node.projection, i, {
|
|
206
|
+
serial: n ?? "",
|
|
207
|
+
messageId: r
|
|
208
|
+
});
|
|
209
|
+
} catch (t) {
|
|
210
|
+
this._logger.error("Tree._foldInto(); fold threw", {
|
|
211
|
+
key: N(e.node),
|
|
212
|
+
messageId: r,
|
|
213
|
+
err: t
|
|
214
|
+
});
|
|
215
|
+
}
|
|
166
216
|
}
|
|
167
217
|
_addToParentIndex(e, t) {
|
|
168
|
-
|
|
169
|
-
n || (n = /* @__PURE__ */ new Set(), this._parentIndex.set(e, n)), n.add(t);
|
|
218
|
+
F(this._parentIndex, e, t);
|
|
170
219
|
}
|
|
171
220
|
_removeFromParentIndex(e, t) {
|
|
172
|
-
|
|
173
|
-
|
|
221
|
+
I(this._parentIndex, e, t);
|
|
222
|
+
}
|
|
223
|
+
_parentKeyOf(e) {
|
|
224
|
+
let t = e.parentCodecMessageId;
|
|
225
|
+
return t === void 0 ? void 0 : this._codecMessageIdToNodeKey.get(t);
|
|
226
|
+
}
|
|
227
|
+
_inputGroupRoot(e) {
|
|
228
|
+
let t = e, n = new Set([N(t)]);
|
|
229
|
+
for (; t.forkOf !== void 0 && !n.has(t.forkOf);) {
|
|
230
|
+
let e = this._nodeIndex.get(t.forkOf);
|
|
231
|
+
if (e?.node.kind !== "input" || e.node.parentCodecMessageId !== t.parentCodecMessageId) break;
|
|
232
|
+
t = e.node, n.add(N(t));
|
|
233
|
+
}
|
|
234
|
+
return t;
|
|
174
235
|
}
|
|
175
236
|
_getSiblingGroup(e) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (s) for (let e of s) {
|
|
237
|
+
this._siblingCacheVersion !== this._structuralVersion && (this._siblingCache.clear(), this._siblingCacheVersion = this._structuralVersion);
|
|
238
|
+
let t = this._siblingCache.get(e);
|
|
239
|
+
if (t) return t;
|
|
240
|
+
let n = this._nodeIndex.get(e);
|
|
241
|
+
if (!n) return [];
|
|
242
|
+
let r = n.node;
|
|
243
|
+
r.kind === "input" && (r = this._inputGroupRoot(r));
|
|
244
|
+
let i = r.parentCodecMessageId, a = [], o = this._parentIndex.get(i);
|
|
245
|
+
if (o) for (let e of o) {
|
|
186
246
|
let t = this._nodeIndex.get(e);
|
|
187
|
-
t && this._isSiblingOf(t.node,
|
|
247
|
+
t && this._isSiblingOf(t.node, r) && a.push(t);
|
|
188
248
|
}
|
|
189
|
-
|
|
249
|
+
a.sort((e, t) => this._compareNodes(e, t));
|
|
250
|
+
for (let e of a) this._siblingCache.set(N(e.node), a);
|
|
251
|
+
return this._siblingCache.set(e, a), a;
|
|
190
252
|
}
|
|
191
253
|
_isSiblingOf(e, t) {
|
|
192
|
-
if (e.
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
254
|
+
if (e.kind !== t.kind || e.parentCodecMessageId !== t.parentCodecMessageId) return !1;
|
|
255
|
+
if (e.kind === "run") return !0;
|
|
256
|
+
let n = N(t);
|
|
257
|
+
if (N(e) === n) return !0;
|
|
258
|
+
let r = e, i = new Set([N(r)]);
|
|
259
|
+
for (; r.kind === "input" && r.forkOf !== void 0;) {
|
|
260
|
+
if (r.forkOf === n) return !0;
|
|
261
|
+
if (i.has(r.forkOf)) break;
|
|
262
|
+
let e = this._nodeIndex.get(r.forkOf);
|
|
198
263
|
if (!e) break;
|
|
199
|
-
|
|
264
|
+
r = e.node, i.add(N(r));
|
|
200
265
|
}
|
|
201
266
|
return !1;
|
|
202
267
|
}
|
|
203
268
|
getGroupRoot(e) {
|
|
204
269
|
let t = this._nodeIndex.get(e);
|
|
205
270
|
if (!t) return e;
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if (!e || e.node.parentId !== n.parentId) break;
|
|
210
|
-
n = e.node, r.add(n.msgId);
|
|
211
|
-
}
|
|
212
|
-
return n.msgId;
|
|
271
|
+
if (t.node.kind === "input") return N(this._inputGroupRoot(t.node));
|
|
272
|
+
let n = this._getSiblingGroup(e)[0]?.node;
|
|
273
|
+
return n ? N(n) : e;
|
|
213
274
|
}
|
|
214
|
-
|
|
215
|
-
this._logger.trace("DefaultTree.
|
|
275
|
+
visibleNodes(e = /* @__PURE__ */ new Map()) {
|
|
276
|
+
this._logger.trace("DefaultTree.visibleNodes();");
|
|
216
277
|
let t = [], n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map();
|
|
217
|
-
for (let i of this.
|
|
218
|
-
let a = i.node,
|
|
278
|
+
for (let i of this._sortedNodes) {
|
|
279
|
+
let a = i.node, o = N(a), s = this._parentKeyOf(a);
|
|
219
280
|
if (s !== void 0 && !n.has(s)) continue;
|
|
220
281
|
let c = this._getSiblingGroup(o);
|
|
221
282
|
if (c.length > 1) {
|
|
222
283
|
let t = this.getGroupRoot(o), n = r.get(t);
|
|
223
284
|
if (n === void 0) {
|
|
224
285
|
let i = e.get(t);
|
|
225
|
-
if (i && c.some((e) => e.
|
|
286
|
+
if (i !== void 0 && c.some((e) => N(e.node) === i)) n = i;
|
|
226
287
|
else {
|
|
227
288
|
let e = c.at(-1);
|
|
228
289
|
if (!e) break;
|
|
229
|
-
n = e.
|
|
290
|
+
n = N(e.node);
|
|
230
291
|
}
|
|
231
292
|
r.set(t, n);
|
|
232
293
|
}
|
|
@@ -236,64 +297,185 @@ var d = e({}), f = e(void 0), p = "x-ably-status", m = "x-ably-turn-id", h = "x-
|
|
|
236
297
|
}
|
|
237
298
|
return t;
|
|
238
299
|
}
|
|
239
|
-
|
|
240
|
-
|
|
300
|
+
getRunNode(e) {
|
|
301
|
+
this._logger.trace("DefaultTree.getRunNode();", { runId: e });
|
|
302
|
+
let t = this._nodeIndex.get(e)?.node;
|
|
303
|
+
return t?.kind === "run" ? t : void 0;
|
|
241
304
|
}
|
|
242
|
-
|
|
243
|
-
return this.
|
|
305
|
+
getNode(e) {
|
|
306
|
+
return this._logger.trace("DefaultTree.getNode();", { key: e }), this._nodeIndex.get(e)?.node;
|
|
244
307
|
}
|
|
245
|
-
|
|
246
|
-
|
|
308
|
+
getNodeByCodecMessageId(e) {
|
|
309
|
+
this._logger.trace("DefaultTree.getNodeByCodecMessageId();", { codecMessageId: e });
|
|
310
|
+
let t = this._codecMessageIdToNodeKey.get(e);
|
|
311
|
+
return t === void 0 ? void 0 : this._nodeIndex.get(t)?.node;
|
|
247
312
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
313
|
+
getReplyRuns(e) {
|
|
314
|
+
let t = this._replyRunsByInput.get(e);
|
|
315
|
+
if (!t) return [];
|
|
316
|
+
let n = [];
|
|
317
|
+
for (let e of t) {
|
|
318
|
+
let t = this._nodeIndex.get(e)?.node;
|
|
319
|
+
t?.kind === "run" && n.push(t);
|
|
320
|
+
}
|
|
321
|
+
return n;
|
|
322
|
+
}
|
|
323
|
+
getSiblingNodes(e) {
|
|
324
|
+
return this._logger.trace("DefaultTree.getSiblingNodes();", { key: e }), this._getSiblingGroup(e).map((e) => e.node);
|
|
325
|
+
}
|
|
326
|
+
applyMessage(e, t, n) {
|
|
327
|
+
let r = t[d], i = t[m], a = r === void 0 && i !== void 0 && t.role === "user" && e.inputs.length > 0 ? i : void 0;
|
|
328
|
+
if (r === void 0 && a === void 0) {
|
|
329
|
+
this._logger.warn("Tree.applyMessage(); message has no run-id and is not a user input; skipping");
|
|
261
330
|
return;
|
|
262
331
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
332
|
+
let o = [...e.inputs, ...e.outputs], s = a ?? r;
|
|
333
|
+
if (o.length === 0 && s !== void 0 && !this._nodeIndex.has(s)) return;
|
|
334
|
+
let c = this._structuralVersion;
|
|
335
|
+
a === void 0 ? r !== void 0 && this._applyRunMessage(r, e, t, n) : this._applyInputMessage(a, t, n, o), this._structuralVersion !== c && this._emitter.emit("update");
|
|
336
|
+
}
|
|
337
|
+
_applyInputMessage(e, t, n, r) {
|
|
338
|
+
let i = this._nodeIndex.get(e);
|
|
339
|
+
i ? i.node.kind === "input" && n && !i.node.serial && (this._logger.debug("Tree.applyMessage(); promoting input serial", {
|
|
340
|
+
codecMessageId: e,
|
|
341
|
+
serial: n
|
|
342
|
+
}), i.node.serial = n, this._promoteSerial(i)) : (i = this._createInputNodeFromHeaders(e, t, n), this._insertNode(e, i, i.node.parentCodecMessageId), this._codecMessageIdToNodeKey.set(e, e), this._logger.debug("Tree.applyMessage(); created input node", { codecMessageId: e })), this._foldInto(i, r, n, e), this._emitter.emit("output", {
|
|
343
|
+
runId: void 0,
|
|
344
|
+
inputCodecMessageId: e,
|
|
345
|
+
codecMessageId: e,
|
|
346
|
+
serial: n,
|
|
347
|
+
events: []
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
_applyRunMessage(e, t, n, r) {
|
|
351
|
+
let i = n[m], a = n[x], o = [...t.inputs, ...t.outputs], s = t.outputs, c = this._nodeIndex.get(e);
|
|
352
|
+
if (!c && i !== void 0) {
|
|
353
|
+
let e = this._codecMessageIdToNodeKey.get(i), t = e === void 0 ? void 0 : this._nodeIndex.get(e);
|
|
354
|
+
t?.node.kind === "run" && t.node.startSerial === void 0 && (c = t);
|
|
355
|
+
}
|
|
356
|
+
c ? r && c.node.kind === "run" && !c.node.startSerial && (this._logger.debug("Tree.applyMessage(); promoting startSerial", {
|
|
357
|
+
runId: e,
|
|
358
|
+
serial: r
|
|
359
|
+
}), c.node.startSerial = r, this._promoteSerial(c)) : (c = this._createRunFromHeaders(e, n, r), this._insertNode(e, c, c.node.parentCodecMessageId), this._indexReplyRun(c.node, e), this._logger.debug("Tree.applyMessage(); created new Run", { runId: e }));
|
|
360
|
+
let l = N(c.node);
|
|
361
|
+
i && this._codecMessageIdToNodeKey.set(i, l), this._foldInto(c, o, r, i), this._emitter.emit("output", {
|
|
362
|
+
runId: l,
|
|
363
|
+
inputCodecMessageId: a,
|
|
364
|
+
codecMessageId: i,
|
|
365
|
+
serial: r,
|
|
366
|
+
events: s
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
_indexReplyRun(e, t) {
|
|
370
|
+
e.parentCodecMessageId !== void 0 && F(this._replyRunsByInput, e.parentCodecMessageId, t);
|
|
371
|
+
}
|
|
372
|
+
applyRunLifecycle(e) {
|
|
373
|
+
this._logger.trace("DefaultTree.applyRunLifecycle();", {
|
|
374
|
+
type: e.type,
|
|
375
|
+
runId: e.runId
|
|
267
376
|
});
|
|
268
|
-
let
|
|
377
|
+
let t = this._structuralVersion;
|
|
378
|
+
switch (e.type) {
|
|
379
|
+
case "start":
|
|
380
|
+
this._applyRunStart(e);
|
|
381
|
+
break;
|
|
382
|
+
case "suspend":
|
|
383
|
+
this._applyRunSuspend(e);
|
|
384
|
+
break;
|
|
385
|
+
case "resume":
|
|
386
|
+
this._applyRunResume(e);
|
|
387
|
+
break;
|
|
388
|
+
case "end":
|
|
389
|
+
this._applyRunEnd(e);
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
this._emitter.emit("run", e), this._structuralVersion !== t && this._emitter.emit("update");
|
|
393
|
+
}
|
|
394
|
+
_applyRunStart(e) {
|
|
395
|
+
let t = this._nodeIndex.get(e.runId);
|
|
396
|
+
if (t?.node.kind === "run") {
|
|
397
|
+
let n = t.node;
|
|
398
|
+
if (n.status !== "active" && (n.status = "active"), e.serial && !n.startSerial && (n.startSerial = e.serial, this._promoteSerial(t)), n.parentCodecMessageId === void 0 && e.parent !== void 0 && (n.parentCodecMessageId = e.parent, this._removeFromParentIndex(void 0, e.runId), this._addToParentIndex(n.parentCodecMessageId, e.runId), this._indexReplyRun(n, e.runId), this._structuralVersion++), n.forkOf === void 0 && e.forkOf !== void 0) {
|
|
399
|
+
let t = this._codecMessageIdToNodeKey.get(e.forkOf);
|
|
400
|
+
t !== void 0 && t !== e.runId && (n.forkOf = t, this._structuralVersion++);
|
|
401
|
+
}
|
|
402
|
+
n.regeneratesCodecMessageId === void 0 && e.regenerates !== void 0 && (n.regeneratesCodecMessageId = e.regenerates, this._structuralVersion++), n.invocationId === "" && e.invocationId !== "" && (n.invocationId = e.invocationId);
|
|
403
|
+
} else if (!t) {
|
|
404
|
+
let t = this._createRunFromLifecycle(e);
|
|
405
|
+
this._insertNode(e.runId, t, t.node.parentCodecMessageId), this._indexReplyRun(t.node, e.runId);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
_applyRunSuspend(e) {
|
|
409
|
+
let t = this._nodeIndex.get(e.runId);
|
|
410
|
+
t?.node.kind === "run" && (t.node.status = "suspended", t.node.endSerial = e.serial);
|
|
411
|
+
}
|
|
412
|
+
_applyRunResume(e) {
|
|
413
|
+
let t = this._nodeIndex.get(e.runId);
|
|
414
|
+
t?.node.kind === "run" && t.node.status === "suspended" && (t.node.status = "active");
|
|
415
|
+
}
|
|
416
|
+
_applyRunEnd(e) {
|
|
417
|
+
let t = this._nodeIndex.get(e.runId);
|
|
418
|
+
t?.node.kind === "run" && (t.node.status = e.reason, t.node.endSerial = e.serial);
|
|
419
|
+
}
|
|
420
|
+
delete(e) {
|
|
421
|
+
let t = this._nodeIndex.get(e);
|
|
422
|
+
t && (this._logger.debug("Tree.delete();", { key: e }), this._removeFromParentIndex(t.node.parentCodecMessageId, e), this._removeSortedNode(t), this._nodeIndex.delete(e), t.node.kind === "run" && t.node.parentCodecMessageId !== void 0 && I(this._replyRunsByInput, t.node.parentCodecMessageId, e), this._structuralVersion++, this._emitter.emit("update"));
|
|
423
|
+
}
|
|
424
|
+
_createRunFromHeaders(e, t, n) {
|
|
425
|
+
let r = t[y];
|
|
426
|
+
return this._buildRunNode({
|
|
427
|
+
runId: e,
|
|
428
|
+
parentCodecMessageId: t[v],
|
|
429
|
+
forkOf: r ? this._codecMessageIdToNodeKey.get(r) : void 0,
|
|
430
|
+
regeneratesCodecMessageId: t[b],
|
|
431
|
+
clientId: t["run-client-id"] ?? "",
|
|
432
|
+
invocationId: t["invocation-id"] ?? "",
|
|
433
|
+
startSerial: n
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
_buildRunNode(e) {
|
|
437
|
+
return {
|
|
269
438
|
node: {
|
|
270
|
-
kind: "
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
439
|
+
kind: "run",
|
|
440
|
+
runId: e.runId,
|
|
441
|
+
parentCodecMessageId: e.parentCodecMessageId,
|
|
442
|
+
forkOf: e.forkOf,
|
|
443
|
+
regeneratesCodecMessageId: e.regeneratesCodecMessageId,
|
|
444
|
+
clientId: e.clientId,
|
|
445
|
+
invocationId: e.invocationId,
|
|
446
|
+
status: "active",
|
|
447
|
+
projection: this._codec.init(),
|
|
448
|
+
startSerial: e.startSerial,
|
|
449
|
+
endSerial: void 0
|
|
277
450
|
},
|
|
278
451
|
insertSeq: this._seqCounter++
|
|
279
452
|
};
|
|
280
|
-
this._nodeIndex.set(e, s), this._addToParentIndex(i, e), this._insertSorted(s), this._structuralVersion++, this._emitter.emit("update");
|
|
281
453
|
}
|
|
282
|
-
|
|
283
|
-
let
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
454
|
+
_createInputNodeFromHeaders(e, t, n) {
|
|
455
|
+
let r = t[y];
|
|
456
|
+
return {
|
|
457
|
+
node: {
|
|
458
|
+
kind: "input",
|
|
459
|
+
codecMessageId: e,
|
|
460
|
+
parentCodecMessageId: t[v],
|
|
461
|
+
forkOf: r,
|
|
462
|
+
projection: this._codec.init(),
|
|
463
|
+
serial: n
|
|
464
|
+
},
|
|
465
|
+
insertSeq: this._seqCounter++
|
|
466
|
+
};
|
|
288
467
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
468
|
+
_createRunFromLifecycle(e) {
|
|
469
|
+
let t = e.forkOf;
|
|
470
|
+
return this._buildRunNode({
|
|
471
|
+
runId: e.runId,
|
|
472
|
+
parentCodecMessageId: e.parent,
|
|
473
|
+
forkOf: t ? this._codecMessageIdToNodeKey.get(t) : void 0,
|
|
474
|
+
regeneratesCodecMessageId: e.regenerates,
|
|
475
|
+
clientId: e.clientId,
|
|
476
|
+
invocationId: e.invocationId,
|
|
477
|
+
startSerial: e.serial
|
|
478
|
+
});
|
|
297
479
|
}
|
|
298
480
|
on(e, t) {
|
|
299
481
|
let n = t;
|
|
@@ -304,165 +486,101 @@ var d = e({}), f = e(void 0), p = "x-ably-status", m = "x-ably-turn-id", h = "x-
|
|
|
304
486
|
emitAblyMessage(e) {
|
|
305
487
|
this._logger.trace("DefaultTree.emitAblyMessage();"), this._emitter.emit("ably-message", e);
|
|
306
488
|
}
|
|
307
|
-
|
|
308
|
-
this._logger.trace("DefaultTree.emitTurn();", { turnId: e.turnId }), this._emitter.emit("turn", e);
|
|
309
|
-
}
|
|
310
|
-
trackTurn(e, t) {
|
|
311
|
-
this._logger.trace("DefaultTree.trackTurn();", {
|
|
312
|
-
turnId: e,
|
|
313
|
-
clientId: t
|
|
314
|
-
}), this._turnClientIds.set(e, t);
|
|
315
|
-
}
|
|
316
|
-
untrackTurn(e) {
|
|
317
|
-
this._logger.trace("DefaultTree.untrackTurn();", { turnId: e }), this._turnClientIds.delete(e);
|
|
318
|
-
}
|
|
319
|
-
}, z = (e) => new R(e), B = (e) => {
|
|
320
|
-
let t = [...e.rawMessages].toReversed(), n = e.codec.createDecoder(), r = /* @__PURE__ */ new Map(), i = e.codec.createAccumulator(), a = 0, o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = [], l = [];
|
|
321
|
-
for (let u of t) {
|
|
322
|
-
let t = n.decode(u), d = P(u), f = d[m], p = d[h], g = u.serial, _ = d[v];
|
|
323
|
-
if (_) {
|
|
324
|
-
for (let e of r.values()) if (e.msgHeaders.has(_)) {
|
|
325
|
-
let n = [...e.msgHeaders.keys()].indexOf(_), r = n === -1 ? void 0 : e.accumulator.messages[n];
|
|
326
|
-
r && e.accumulator.initMessage(_, r), e.accumulator.processOutputs(t), l.push({
|
|
327
|
-
accumulator: e.accumulator,
|
|
328
|
-
messageId: _
|
|
329
|
-
});
|
|
330
|
-
break;
|
|
331
|
-
}
|
|
332
|
-
continue;
|
|
333
|
-
}
|
|
334
|
-
if (f) {
|
|
335
|
-
let n = r.get(f);
|
|
336
|
-
if (n || (n = {
|
|
337
|
-
accumulator: e.codec.createAccumulator(),
|
|
338
|
-
firstSeen: a++,
|
|
339
|
-
msgHeaders: /* @__PURE__ */ new Map(),
|
|
340
|
-
msgSerials: /* @__PURE__ */ new Map()
|
|
341
|
-
}, r.set(f, n)), p) {
|
|
342
|
-
let e = n.msgHeaders.get(p);
|
|
343
|
-
e ? Object.keys(d).length > 0 && Object.assign(e, d) : (n.msgHeaders.set(p, { ...d }), g && n.msgSerials.set(p, g));
|
|
344
|
-
}
|
|
345
|
-
n.accumulator.processOutputs(t);
|
|
346
|
-
} else {
|
|
347
|
-
i.processOutputs(t);
|
|
348
|
-
for (let e of t) if (e.kind === "message" && p) {
|
|
349
|
-
c.push(p);
|
|
350
|
-
let e = o.get(p);
|
|
351
|
-
e ? Object.keys(d).length > 0 && Object.assign(e, d) : (o.set(p, { ...d }), g && s.set(p, g));
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
for (let { accumulator: e, messageId: t } of l) e.completeMessage(t);
|
|
356
|
-
let u = [];
|
|
357
|
-
for (let [e, t] of i.completedMessages.entries()) {
|
|
358
|
-
let n = c[e];
|
|
359
|
-
u.push({
|
|
360
|
-
message: t,
|
|
361
|
-
headers: n ? o.get(n) ?? {} : {},
|
|
362
|
-
serial: n ? s.get(n) ?? "" : ""
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
let d = [...r.values()].toSorted((e, t) => e.firstSeen - t.firstSeen);
|
|
366
|
-
for (let e of d) {
|
|
367
|
-
let t = [...e.msgHeaders.entries()], n = 0;
|
|
368
|
-
for (let r of e.accumulator.completedMessages) {
|
|
369
|
-
let i = t[n];
|
|
370
|
-
if (i) {
|
|
371
|
-
let [t, a] = i;
|
|
372
|
-
u.push({
|
|
373
|
-
message: r,
|
|
374
|
-
headers: a,
|
|
375
|
-
serial: e.msgSerials.get(t) ?? ""
|
|
376
|
-
}), n++;
|
|
377
|
-
} else u.push({
|
|
378
|
-
message: r,
|
|
379
|
-
headers: {},
|
|
380
|
-
serial: ""
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
return u.toReversed();
|
|
385
|
-
}, ie = (e) => {
|
|
386
|
-
if (e.cachedDecode && e.cachedAtRawLength === e.rawMessages.length) return e.cachedDecode;
|
|
387
|
-
let t = B(e);
|
|
388
|
-
return e.cachedDecode = t, e.cachedAtRawLength = e.rawMessages.length, t;
|
|
389
|
-
}, V = (e, t) => {
|
|
489
|
+
}, R = (e, t) => new L(e, t), z = (e, t) => {
|
|
390
490
|
for (let n of t) {
|
|
391
|
-
let t =
|
|
392
|
-
if (!r
|
|
393
|
-
let i = n.action, a = i === "message.create" && "
|
|
394
|
-
(a || o) && e.
|
|
491
|
+
let t = D(n), r = t[m];
|
|
492
|
+
if (!r) continue;
|
|
493
|
+
let i = n.action, a = i === "message.create" && "discrete" in t, o = t.stream === "true" && (i === "message.create" || i === "message.update" || i === "message.append"), s = t[u], c = s === "complete" || s === "cancelled";
|
|
494
|
+
(a || o) && e.startedCodecMessageIds.add(r), (a || c) && e.terminatedCodecMessageIds.add(r), e.startedCodecMessageIds.has(r) && e.terminatedCodecMessageIds.has(r) && e.completedCodecMessageIds.add(r);
|
|
395
495
|
}
|
|
396
|
-
},
|
|
397
|
-
e.rawMessages.push(...t.items), e.lastAblyPage = t,
|
|
496
|
+
}, B = async (e, t, n) => {
|
|
497
|
+
e.rawMessages.push(...t.items), e.lastAblyPage = t, z(e, t.items);
|
|
398
498
|
let r = e.returnedCount + n;
|
|
399
|
-
for (; e.
|
|
400
|
-
e.logger.debug("
|
|
499
|
+
for (; e.completedCodecMessageIds.size < r && t.hasNext();) {
|
|
500
|
+
e.logger.debug("loadHistory.fetchUntilLimit(); fetching next page", {
|
|
401
501
|
collected: e.rawMessages.length,
|
|
402
|
-
completed: e.
|
|
502
|
+
completed: e.completedCodecMessageIds.size
|
|
403
503
|
});
|
|
404
504
|
let n = await t.next();
|
|
405
505
|
if (!n) break;
|
|
406
|
-
t = n, e.rawMessages.push(...n.items), e.lastAblyPage = n,
|
|
506
|
+
t = n, e.rawMessages.push(...n.items), e.lastAblyPage = n, z(e, n.items);
|
|
407
507
|
}
|
|
408
|
-
},
|
|
409
|
-
let n =
|
|
410
|
-
e.returnedCount += r
|
|
411
|
-
let
|
|
508
|
+
}, V = (e, t) => {
|
|
509
|
+
let n = e.completedCodecMessageIds.size, r = Math.min(t, Math.max(0, n - e.returnedCount));
|
|
510
|
+
e.returnedCount += r;
|
|
511
|
+
let i = n > e.returnedCount, a = e.lastAblyPage?.hasNext() ?? !1, o = e.rawMessages.length - e.returnedRawCount > 0 ? e.rawMessages.slice(e.returnedRawCount).toReversed() : [];
|
|
412
512
|
return e.returnedRawCount = e.rawMessages.length, {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
headers: e.headers,
|
|
416
|
-
serial: e.serial
|
|
417
|
-
})),
|
|
418
|
-
rawMessages: s,
|
|
419
|
-
hasNext: () => a || o,
|
|
513
|
+
rawMessages: o,
|
|
514
|
+
hasNext: () => i || a,
|
|
420
515
|
next: async () => {
|
|
421
|
-
if (
|
|
422
|
-
if (!
|
|
516
|
+
if (i) return V(e, t);
|
|
517
|
+
if (!a || !e.lastAblyPage) return;
|
|
423
518
|
let n = await e.lastAblyPage.next();
|
|
424
|
-
if (n) return await
|
|
519
|
+
if (n) return await B(e, n, t), V(e, t);
|
|
425
520
|
}
|
|
426
521
|
};
|
|
427
|
-
},
|
|
428
|
-
let
|
|
429
|
-
codec: t,
|
|
522
|
+
}, H = async (e, t, n) => {
|
|
523
|
+
let r = t?.limit ?? 100, i = {
|
|
430
524
|
rawMessages: [],
|
|
431
525
|
returnedCount: 0,
|
|
432
526
|
returnedRawCount: 0,
|
|
433
527
|
lastAblyPage: void 0,
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
completedMsgIds: /* @__PURE__ */ new Set(),
|
|
439
|
-
logger: r
|
|
528
|
+
startedCodecMessageIds: /* @__PURE__ */ new Set(),
|
|
529
|
+
terminatedCodecMessageIds: /* @__PURE__ */ new Set(),
|
|
530
|
+
completedCodecMessageIds: /* @__PURE__ */ new Set(),
|
|
531
|
+
logger: n
|
|
440
532
|
};
|
|
441
|
-
|
|
442
|
-
let
|
|
443
|
-
return await e.attach(), await
|
|
533
|
+
n.trace("loadHistory();", { limit: r });
|
|
534
|
+
let a = r * 10;
|
|
535
|
+
return await e.attach(), await B(i, await e.history({
|
|
444
536
|
untilAttach: !0,
|
|
445
|
-
limit:
|
|
446
|
-
}),
|
|
447
|
-
},
|
|
537
|
+
limit: a
|
|
538
|
+
}), r), V(i, r);
|
|
539
|
+
}, de = (e) => Array.isArray(e) ? e : [e], fe = 3, U = (e) => ({
|
|
540
|
+
runId: e.runId,
|
|
541
|
+
clientId: e.clientId,
|
|
542
|
+
status: e.status,
|
|
543
|
+
invocationId: e.invocationId
|
|
544
|
+
}), pe = class {
|
|
448
545
|
constructor(e) {
|
|
449
|
-
this._branchSelections = /* @__PURE__ */ new Map(), this.
|
|
546
|
+
this._branchSelections = /* @__PURE__ */ new Map(), this._regenSelections = /* @__PURE__ */ new Map(), this._withheldRunIds = /* @__PURE__ */ new Set(), this._lastVisibleNodeKeys = [], this._lastVisibleProjections = [], this._lastVisibleMessagePairs = [], this._lastVisibleNodeKeySet = /* @__PURE__ */ new Set(), this._hasMoreHistory = !1, this._withheldBuffer = [], this._unsubs = [], this._cachedNodes = [], this._loadingOlder = !1, this._processingHistory = !1, this._closed = !1, this._tree = e.tree, this._channel = e.channel, this._codec = e.codec, this._sendDelegate = e.sendDelegate, this._onClose = e.onClose, this._logger = e.logger.withContext({ component: "View" }), this._logger.trace("DefaultView();"), this._emitter = new w(this._logger), this._cachedNodes = this._computeFlatNodes(), this._updateVisibleSnapshot(this._cachedNodes), this._unsubs.push(this._tree.on("update", () => {
|
|
450
547
|
this._onTreeUpdate();
|
|
451
548
|
}), this._tree.on("ably-message", (e) => {
|
|
452
549
|
this._onTreeAblyMessage(e);
|
|
453
|
-
}), this._tree.on("
|
|
454
|
-
this.
|
|
550
|
+
}), this._tree.on("run", (e) => {
|
|
551
|
+
this._onTreeRun(e);
|
|
552
|
+
}), this._tree.on("output", (e) => {
|
|
553
|
+
this._onTreeOutput(e);
|
|
455
554
|
}));
|
|
456
555
|
}
|
|
556
|
+
_onTreeOutput(e) {
|
|
557
|
+
this._processingHistory || (e.runId !== void 0 && this._lastVisibleNodeKeySet.has(e.runId) || e.inputCodecMessageId !== void 0 && this._lastVisibleNodeKeySet.has(e.inputCodecMessageId)) && (this._lastVisibleProjections = this._cachedNodes.map((e) => e.projection), this._lastVisibleMessagePairs = this._extractMessages(this._cachedNodes), this._emitter.emit("update"));
|
|
558
|
+
}
|
|
457
559
|
getMessages() {
|
|
458
|
-
return this.
|
|
560
|
+
return this._lastVisibleMessagePairs;
|
|
459
561
|
}
|
|
460
|
-
|
|
461
|
-
return this._cachedNodes;
|
|
562
|
+
runs() {
|
|
563
|
+
return this._cachedNodes.filter((e) => e.kind === "run").map((e) => U(e));
|
|
462
564
|
}
|
|
463
565
|
_computeFlatNodes() {
|
|
464
|
-
let e = this.
|
|
465
|
-
return this.
|
|
566
|
+
let e = this._treeVisibleNodes();
|
|
567
|
+
return this._withheldRunIds.size === 0 ? e : e.filter((e) => !this._withheldRunIds.has(N(e)));
|
|
568
|
+
}
|
|
569
|
+
_recomputeAndEmit() {
|
|
570
|
+
this._cachedNodes = this._computeFlatNodes(), this._updateVisibleSnapshot(this._cachedNodes), this._emitter.emit("update");
|
|
571
|
+
}
|
|
572
|
+
_recomputeAndEmitIfChanged() {
|
|
573
|
+
let e = this._computeFlatNodes();
|
|
574
|
+
this._visibleChanged(e) && (this._cachedNodes = e, this._updateVisibleSnapshot(e), this._emitter.emit("update"));
|
|
575
|
+
}
|
|
576
|
+
_runByCodecMessageId(e) {
|
|
577
|
+
let t = this._tree.getNodeByCodecMessageId(e);
|
|
578
|
+
return t?.kind === "run" ? t : void 0;
|
|
579
|
+
}
|
|
580
|
+
_extractMessages(e) {
|
|
581
|
+
let t = [];
|
|
582
|
+
for (let n of e) for (let e of this._codec.getMessages(n.projection)) t.push(e);
|
|
583
|
+
return t;
|
|
466
584
|
}
|
|
467
585
|
hasOlder() {
|
|
468
586
|
return this._withheldBuffer.length > 0 || this._hasMoreHistory;
|
|
@@ -490,7 +608,7 @@ var d = e({}), f = e(void 0), p = "x-ably-status", m = "x-ably-turn-id", h = "x-
|
|
|
490
608
|
t || (this._hasMoreHistory = !1);
|
|
491
609
|
return;
|
|
492
610
|
}
|
|
493
|
-
await this.
|
|
611
|
+
await this._revealFromPage(t, e);
|
|
494
612
|
} catch (e) {
|
|
495
613
|
throw this._logger.error("DefaultView.loadOlder(); failed", { error: e }), e;
|
|
496
614
|
} finally {
|
|
@@ -498,132 +616,182 @@ var d = e({}), f = e(void 0), p = "x-ably-status", m = "x-ably-turn-id", h = "x-
|
|
|
498
616
|
}
|
|
499
617
|
}
|
|
500
618
|
}
|
|
501
|
-
|
|
502
|
-
this._logger.trace("DefaultView.
|
|
503
|
-
|
|
619
|
+
runOf(e) {
|
|
620
|
+
this._logger.trace("DefaultView.runOf();", { codecMessageId: e });
|
|
621
|
+
let t = this._tree.getNodeByCodecMessageId(e);
|
|
622
|
+
if (!t) return;
|
|
623
|
+
if (t.kind === "run") return U(t);
|
|
624
|
+
let n = this._selectedReplyRun(t.codecMessageId);
|
|
625
|
+
return n ? U(n) : void 0;
|
|
626
|
+
}
|
|
627
|
+
_selectedReplyRun(e) {
|
|
628
|
+
let t = this._tree.getReplyRuns(e);
|
|
629
|
+
if (t.length === 0) return;
|
|
630
|
+
if (t.length === 1) return t[0];
|
|
631
|
+
let n = this._tree.getGroupRoot(t[0]?.runId ?? ""), r = this._regenSelections.get(n), i = r && r.kind !== "pending" ? r.selectedRunId : void 0;
|
|
632
|
+
if (i !== void 0) {
|
|
633
|
+
let e = t.find((e) => e.runId === i);
|
|
634
|
+
if (e) return e;
|
|
635
|
+
}
|
|
636
|
+
return t.toSorted((e, t) => (e.startSerial ?? "").localeCompare(t.startSerial ?? "")).at(-1);
|
|
637
|
+
}
|
|
638
|
+
run(e) {
|
|
639
|
+
this._logger.trace("DefaultView.run();", { runId: e });
|
|
640
|
+
let t = this._tree.getRunNode(e);
|
|
641
|
+
return t ? U(t) : void 0;
|
|
642
|
+
}
|
|
643
|
+
branchSelection(e) {
|
|
644
|
+
let t = this._resolveMessageBranchPoint(e);
|
|
645
|
+
if (t) {
|
|
646
|
+
let e = t.siblings.flatMap((e) => {
|
|
647
|
+
let t = this._codec.getMessages(e.projection).at(0);
|
|
648
|
+
return t ? [t.message] : [];
|
|
649
|
+
});
|
|
650
|
+
if (e.length > 0) {
|
|
651
|
+
let n = this._resolveSelectedIndex(t), r = Math.max(0, Math.min(n, e.length - 1)), i = e[r];
|
|
652
|
+
return {
|
|
653
|
+
hasSiblings: e.length > 1,
|
|
654
|
+
siblings: e,
|
|
655
|
+
index: r,
|
|
656
|
+
selected: i
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
let n = this._tree.getNodeByCodecMessageId(e);
|
|
661
|
+
if (n) {
|
|
662
|
+
let t = this._codec.getMessages(n.projection).find((t) => t.codecMessageId === e);
|
|
663
|
+
if (t !== void 0) return {
|
|
664
|
+
hasSiblings: !1,
|
|
665
|
+
siblings: [t.message],
|
|
666
|
+
index: 0,
|
|
667
|
+
selected: t.message
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
return {
|
|
671
|
+
hasSiblings: !1,
|
|
672
|
+
siblings: [],
|
|
673
|
+
index: 0,
|
|
674
|
+
selected: void 0
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
selectSibling(e, t) {
|
|
678
|
+
this._logger.trace("DefaultView.selectSibling();", {
|
|
679
|
+
codecMessageId: e,
|
|
504
680
|
index: t
|
|
505
681
|
});
|
|
506
|
-
let n = this.
|
|
507
|
-
if (n
|
|
508
|
-
let r =
|
|
509
|
-
|
|
682
|
+
let n = this._resolveMessageBranchPoint(e);
|
|
683
|
+
if (!n) return;
|
|
684
|
+
let r = Math.max(0, Math.min(t, n.siblings.length - 1)), i = n.siblings[r];
|
|
685
|
+
i && (n.kind === "fork-of" ? (this._branchSelections.set(n.groupRoot, {
|
|
686
|
+
kind: "user",
|
|
687
|
+
selectedKey: N(i)
|
|
688
|
+
}), this._logger.debug("DefaultView.selectSibling(); fork-of", {
|
|
689
|
+
codecMessageId: e,
|
|
690
|
+
index: r,
|
|
691
|
+
selectedKey: N(i)
|
|
692
|
+
})) : (this._regenSelections.set(n.groupRoot, {
|
|
510
693
|
kind: "user",
|
|
511
|
-
|
|
512
|
-
}), this._logger.debug("DefaultView.
|
|
513
|
-
|
|
514
|
-
index:
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
hasSiblings(e) {
|
|
531
|
-
return this._tree.hasSiblings(e);
|
|
694
|
+
selectedRunId: N(i)
|
|
695
|
+
}), this._logger.debug("DefaultView.selectSibling(); regenerate", {
|
|
696
|
+
codecMessageId: e,
|
|
697
|
+
index: r,
|
|
698
|
+
selectedRunId: N(i),
|
|
699
|
+
groupRoot: n.groupRoot
|
|
700
|
+
})), this._recomputeAndEmit());
|
|
701
|
+
}
|
|
702
|
+
_resolveSelectedIndex(e) {
|
|
703
|
+
if (e.kind === "fork-of") {
|
|
704
|
+
let t = this._branchSelections.get(e.groupRoot);
|
|
705
|
+
if (!t) return e.siblings.length - 1;
|
|
706
|
+
let n = e.siblings.findIndex((e) => N(e) === t.selectedKey);
|
|
707
|
+
return n === -1 ? e.siblings.length - 1 : n;
|
|
708
|
+
}
|
|
709
|
+
let t = this._regenSelections.get(e.groupRoot);
|
|
710
|
+
if (!t || t.kind === "pending") return e.siblings.length - 1;
|
|
711
|
+
let n = e.siblings.findIndex((e) => N(e) === t.selectedRunId);
|
|
712
|
+
return n === -1 ? e.siblings.length - 1 : n;
|
|
532
713
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
t && (this._branchSelections.set(e, {
|
|
544
|
-
kind: "auto",
|
|
545
|
-
selectedId: t
|
|
546
|
-
}), this._cachedNodes = this._computeFlatNodes(), this._updateVisibleSnapshot(this._cachedNodes), this._emitter.emit("update"));
|
|
547
|
-
} else {
|
|
548
|
-
this._branchSelections.set(e, {
|
|
549
|
-
kind: "pending",
|
|
550
|
-
turnId: r.turnId
|
|
551
|
-
}), this._logger.debug("DefaultView.send(); deferring fork auto-selection", {
|
|
552
|
-
forkOf: t.forkOf,
|
|
553
|
-
groupRoot: e,
|
|
554
|
-
turnId: r.turnId
|
|
555
|
-
});
|
|
556
|
-
let n = this._tree.on("turn", (t) => {
|
|
557
|
-
if (t.type !== "x-ably-turn-end" || t.turnId !== r.turnId) return;
|
|
558
|
-
let i = this._branchSelections.get(e);
|
|
559
|
-
i?.kind === "pending" && i.turnId === r.turnId && this._branchSelections.delete(e), n();
|
|
560
|
-
let a = this._unsubs.indexOf(n);
|
|
561
|
-
a !== -1 && this._unsubs.splice(a, 1);
|
|
562
|
-
});
|
|
563
|
-
this._unsubs.push(n);
|
|
564
|
-
}
|
|
714
|
+
_resolveMessageBranchPoint(e) {
|
|
715
|
+
let t = this._tree.getNodeByCodecMessageId(e);
|
|
716
|
+
if (!t) return;
|
|
717
|
+
if (t.kind === "input") {
|
|
718
|
+
let e = this._tree.getSiblingNodes(t.codecMessageId);
|
|
719
|
+
return e.length > 1 ? {
|
|
720
|
+
kind: "fork-of",
|
|
721
|
+
groupRoot: this._tree.getGroupRoot(t.codecMessageId),
|
|
722
|
+
siblings: e
|
|
723
|
+
} : void 0;
|
|
565
724
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
let r = n.parentId;
|
|
573
|
-
return this.send([], {
|
|
574
|
-
...t,
|
|
575
|
-
body: {
|
|
576
|
-
history: this._getHistoryBefore(e),
|
|
577
|
-
...t?.body
|
|
578
|
-
},
|
|
579
|
-
forkOf: e,
|
|
580
|
-
parent: r
|
|
581
|
-
});
|
|
725
|
+
let n = this._tree.getSiblingNodes(t.runId);
|
|
726
|
+
if (n.length > 1 && this._codec.getMessages(t.projection).at(0)?.codecMessageId === e) return {
|
|
727
|
+
kind: "regen",
|
|
728
|
+
groupRoot: this._tree.getGroupRoot(t.runId),
|
|
729
|
+
siblings: n
|
|
730
|
+
};
|
|
582
731
|
}
|
|
583
|
-
async
|
|
584
|
-
this._logger.trace("DefaultView.
|
|
585
|
-
let r = this.
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
732
|
+
async send(t, n) {
|
|
733
|
+
if (this._logger.trace("DefaultView.send();"), this._closed) throw new e.ErrorInfo("unable to send; view is closed", C.InvalidArgument, 400);
|
|
734
|
+
let r = de(t), i = this._lastVisibleMessagePairs.at(-1)?.codecMessageId, a = await this._sendDelegate(r, n, i);
|
|
735
|
+
return this._applyForkAutoSelect(a, n), a;
|
|
736
|
+
}
|
|
737
|
+
_applyForkAutoSelect(e, t) {
|
|
738
|
+
if (!t?.forkOf) return;
|
|
739
|
+
let n = e.optimisticCodecMessageIds.at(0);
|
|
740
|
+
if (n === void 0) return;
|
|
741
|
+
let r = this._tree.getGroupRoot(n);
|
|
742
|
+
this._branchSelections.set(r, {
|
|
743
|
+
kind: "auto",
|
|
744
|
+
selectedKey: n
|
|
745
|
+
}), this._recomputeAndEmit();
|
|
746
|
+
}
|
|
747
|
+
_applyRegenerateAutoSelect(e, t) {
|
|
748
|
+
let n = this._runByCodecMessageId(t);
|
|
749
|
+
if (!n) return;
|
|
750
|
+
let r = this._tree.getGroupRoot(n.runId);
|
|
751
|
+
this._regenSelections.set(r, {
|
|
752
|
+
kind: "pending",
|
|
753
|
+
carrierCodecMessageId: e.inputCodecMessageId
|
|
754
|
+
}), this._logger.debug("DefaultView._applyRegenerateAutoSelect(); deferring regenerate selection", {
|
|
755
|
+
anchorCodecMessageId: t,
|
|
756
|
+
groupRoot: r,
|
|
757
|
+
carrier: e.inputCodecMessageId
|
|
758
|
+
}), this._resolvePendingRegenSelections(), this._recomputeAndEmitIfChanged();
|
|
759
|
+
}
|
|
760
|
+
async regenerate(t, n) {
|
|
761
|
+
if (this._logger.trace("DefaultView.regenerate();", { messageId: t }), this._closed) throw new e.ErrorInfo("unable to regenerate; view is closed", C.InvalidArgument, 400);
|
|
762
|
+
let r = this._runByCodecMessageId(t);
|
|
763
|
+
if (!r) throw new e.ErrorInfo(`unable to regenerate; message not found in tree: ${t}`, C.InvalidArgument, 400);
|
|
764
|
+
let i = this._findParentMsgId(r, t);
|
|
765
|
+
if (!i) throw new e.ErrorInfo(`unable to regenerate; parent user message not found for ${t}`, C.InvalidArgument, 400);
|
|
766
|
+
let a = t;
|
|
767
|
+
r.regeneratesCodecMessageId !== void 0 && this._codec.getMessages(r.projection).at(0)?.codecMessageId === t && (a = r.regeneratesCodecMessageId);
|
|
768
|
+
let o = {
|
|
589
769
|
...n,
|
|
590
|
-
body: {
|
|
591
|
-
history: this._getHistoryBefore(e),
|
|
592
|
-
...n?.body
|
|
593
|
-
},
|
|
594
|
-
forkOf: e,
|
|
595
770
|
parent: i
|
|
771
|
+
}, s = this._codec.createRegenerate(a, i), c = await this._sendDelegate([s], o, i);
|
|
772
|
+
return this._applyRegenerateAutoSelect(c, a), c;
|
|
773
|
+
}
|
|
774
|
+
async edit(t, n, r) {
|
|
775
|
+
if (this._logger.trace("DefaultView.edit();", { messageId: t }), this._closed) throw new e.ErrorInfo("unable to edit; view is closed", C.InvalidArgument, 400);
|
|
776
|
+
let i = this._tree.getNodeByCodecMessageId(t);
|
|
777
|
+
if (!i) throw new e.ErrorInfo(`unable to edit; message not found in tree: ${t}`, C.InvalidArgument, 400);
|
|
778
|
+
let a = this._findParentMsgId(i, t);
|
|
779
|
+
return this.send(n, {
|
|
780
|
+
...r,
|
|
781
|
+
forkOf: t,
|
|
782
|
+
parent: a
|
|
596
783
|
});
|
|
597
784
|
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
events: t
|
|
608
|
-
}];
|
|
609
|
-
return this._sendDelegate([], n, this.flattenNodes(), r);
|
|
610
|
-
}
|
|
611
|
-
_getHistoryBefore(e) {
|
|
612
|
-
this._logger.trace("DefaultView._getHistoryBefore();", { messageId: e });
|
|
613
|
-
let t = this.flattenNodes(), n = t.findIndex((t) => t.msgId === e);
|
|
614
|
-
return n === -1 ? (this._logger.warn("DefaultView._getHistoryBefore(); target not in visible nodes, returning full list", { messageId: e }), t) : t.slice(0, n);
|
|
615
|
-
}
|
|
616
|
-
getActiveTurnIds() {
|
|
617
|
-
this._logger.trace("DefaultView.getActiveTurnIds();");
|
|
618
|
-
let e = this._tree.getActiveTurnIds();
|
|
619
|
-
if (this._withheldMsgIds.size === 0) return e;
|
|
620
|
-
let t = /* @__PURE__ */ new Map();
|
|
621
|
-
for (let [n, r] of e) {
|
|
622
|
-
let e = /* @__PURE__ */ new Set();
|
|
623
|
-
for (let t of r) this._lastVisibleTurnIds.has(t) && e.add(t);
|
|
624
|
-
e.size > 0 && t.set(n, e);
|
|
785
|
+
_findParentMsgId(e, t) {
|
|
786
|
+
let n = this._lastVisibleMessagePairs, r = n.findIndex((e) => e.codecMessageId === t);
|
|
787
|
+
if (r > 0) return n[r - 1]?.codecMessageId;
|
|
788
|
+
if (r === 0) return;
|
|
789
|
+
let i = this._codec.getMessages(e.projection), a = i.findIndex((e) => e.codecMessageId === t);
|
|
790
|
+
if (a > 0) return i[a - 1]?.codecMessageId;
|
|
791
|
+
if (a === 0 && e.parentCodecMessageId !== void 0) {
|
|
792
|
+
let t = this._tree.getNodeByCodecMessageId(e.parentCodecMessageId);
|
|
793
|
+
if (t) return this._codec.getMessages(t.projection).at(-1)?.codecMessageId;
|
|
625
794
|
}
|
|
626
|
-
return t;
|
|
627
795
|
}
|
|
628
796
|
on(e, t) {
|
|
629
797
|
let n = t;
|
|
@@ -632,35 +800,38 @@ var d = e({}), f = e(void 0), p = "x-ably-status", m = "x-ably-turn-id", h = "x-
|
|
|
632
800
|
};
|
|
633
801
|
}
|
|
634
802
|
close() {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
803
|
+
if (!this._closed) {
|
|
804
|
+
this._logger.info("DefaultView.close();"), this._closed = !0, this._loadingOlder = !1;
|
|
805
|
+
for (let e of this._unsubs) e();
|
|
806
|
+
this._unsubs.length = 0, this._emitter.off(), this._branchSelections.clear(), this._regenSelections.clear(), this._withheldRunIds.clear(), this._withheldBuffer.length = 0, this._onClose?.();
|
|
807
|
+
}
|
|
638
808
|
}
|
|
639
809
|
async _loadFirstPage(e) {
|
|
640
|
-
let t =
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
810
|
+
let t = e * fe, n = await H(this._channel, { limit: t }, this._logger);
|
|
811
|
+
this._closed || await this._revealFromPage(n, e);
|
|
812
|
+
}
|
|
813
|
+
async _revealFromPage(e, t) {
|
|
814
|
+
let n = new Set(this._treeVisibleNodes().map((e) => N(e))), { newVisible: r, lastPage: i } = await this._loadUntilVisible(e, t, n);
|
|
815
|
+
this._closed || (this._lastHistoryPage = i, this._hasMoreHistory = i.hasNext(), this._splitReveal(r, t));
|
|
816
|
+
}
|
|
817
|
+
_splitReveal(e, t) {
|
|
818
|
+
let n = 0, r = e.length;
|
|
819
|
+
for (let i = e.length - 1; i >= 0; i--) {
|
|
820
|
+
if (e[i]?.kind === "run") {
|
|
821
|
+
if (n === t) break;
|
|
822
|
+
n++;
|
|
823
|
+
}
|
|
824
|
+
r = i;
|
|
825
|
+
}
|
|
826
|
+
let i = e.slice(r), a = e.slice(0, r);
|
|
827
|
+
for (let e of a) this._withheldRunIds.add(N(e));
|
|
828
|
+
this._withheldBuffer.push(...a), this._releaseWithheld(i);
|
|
656
829
|
}
|
|
657
830
|
_processHistoryPage(e) {
|
|
658
831
|
this._processingHistory = !0;
|
|
659
832
|
try {
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
e && this._tree.upsert(e, t.message, t.headers, t.serial);
|
|
663
|
-
}
|
|
833
|
+
let t = this._codec.createDecoder();
|
|
834
|
+
for (let n of e.rawMessages) M(this._tree, t, n);
|
|
664
835
|
for (let t of e.rawMessages) this._tree.emitAblyMessage(t);
|
|
665
836
|
} finally {
|
|
666
837
|
this._processingHistory = !1;
|
|
@@ -670,7 +841,7 @@ var d = e({}), f = e(void 0), p = "x-ably-status", m = "x-ably-turn-id", h = "x-
|
|
|
670
841
|
this._processHistoryPage(e);
|
|
671
842
|
let r = e, i = () => {
|
|
672
843
|
let e = 0;
|
|
673
|
-
for (let t of this.
|
|
844
|
+
for (let t of this._treeVisibleNodes()) t.kind === "run" && !n.has(N(t)) && e++;
|
|
674
845
|
return e;
|
|
675
846
|
};
|
|
676
847
|
for (; i() < t && r.hasNext();) {
|
|
@@ -679,678 +850,493 @@ var d = e({}), f = e(void 0), p = "x-ably-status", m = "x-ably-turn-id", h = "x-
|
|
|
679
850
|
this._processHistoryPage(e), r = e;
|
|
680
851
|
}
|
|
681
852
|
return {
|
|
682
|
-
newVisible: this.
|
|
853
|
+
newVisible: this._treeVisibleNodes().filter((e) => !n.has(N(e))),
|
|
683
854
|
lastPage: r
|
|
684
855
|
};
|
|
685
856
|
}
|
|
686
857
|
_releaseWithheld(e) {
|
|
687
|
-
for (let t of e) this.
|
|
688
|
-
e.length > 0 &&
|
|
858
|
+
for (let t of e) this._withheldRunIds.delete(N(t));
|
|
859
|
+
e.length > 0 && this._recomputeAndEmit();
|
|
689
860
|
}
|
|
690
861
|
_updateVisibleSnapshot(e) {
|
|
691
|
-
let t = e ?? this.
|
|
692
|
-
this.
|
|
693
|
-
for (let e of t) {
|
|
694
|
-
let t = e.headers[m];
|
|
695
|
-
t && this._lastVisibleTurnIds.add(t);
|
|
696
|
-
}
|
|
862
|
+
let t = e ?? this._cachedNodes;
|
|
863
|
+
this._lastVisibleNodeKeys = t.map((e) => N(e)), this._lastVisibleNodeKeySet = new Set(this._lastVisibleNodeKeys), this._lastVisibleProjections = t.map((e) => e.projection), this._lastVisibleMessagePairs = this._extractMessages(t);
|
|
697
864
|
}
|
|
698
865
|
_onTreeUpdate() {
|
|
699
|
-
|
|
700
|
-
let e = this._tree.structuralVersion;
|
|
701
|
-
if (e === this._lastStructuralVersion) {
|
|
702
|
-
this._cachedNodes.some((e, t) => e.message !== this._lastVisibleMessages[t]) && (this._lastVisibleMessages = this._cachedNodes.map((e) => e.message), this._cachedNodes = [...this._cachedNodes], this._emitter.emit("update"));
|
|
703
|
-
return;
|
|
704
|
-
}
|
|
705
|
-
this._lastStructuralVersion = e, this._pinBranchSelections(), this._resolvePendingSelections();
|
|
706
|
-
let t = this._computeFlatNodes(), n = t.map((e) => e.msgId), r = t.map((e) => e.message);
|
|
707
|
-
this._visibleChanged(n, r) && (this._cachedNodes = t, this._updateVisibleSnapshot(t), this._emitter.emit("update"));
|
|
866
|
+
this._processingHistory || (this._pinBranchSelections(), this._resolvePendingRegenSelections(), this._recomputeAndEmitIfChanged());
|
|
708
867
|
}
|
|
709
868
|
_resolveSelections() {
|
|
710
869
|
let e = /* @__PURE__ */ new Map();
|
|
711
|
-
for (let [t, n] of this._branchSelections)
|
|
870
|
+
for (let [t, n] of this._branchSelections) e.set(t, n.selectedKey);
|
|
871
|
+
for (let [t, n] of this._regenSelections) n.kind !== "pending" && e.set(t, n.selectedRunId);
|
|
712
872
|
return e;
|
|
713
873
|
}
|
|
874
|
+
_treeVisibleNodes() {
|
|
875
|
+
return this._tree.visibleNodes(this._resolveSelections());
|
|
876
|
+
}
|
|
714
877
|
_pinBranchSelections() {
|
|
715
|
-
for (let e of this.
|
|
716
|
-
if (
|
|
717
|
-
let t = this._tree.getGroupRoot(e)
|
|
718
|
-
|
|
719
|
-
let r = this._tree.getSiblingNodes(e).at(-1);
|
|
720
|
-
r && r.msgId !== e && r.headers["x-ably-turn-id"] === n.turnId && (this._logger.debug("DefaultView._pinBranchSelections(); auto-selecting pending fork", {
|
|
721
|
-
msgId: e,
|
|
722
|
-
newestId: r.msgId,
|
|
723
|
-
turnId: n.turnId
|
|
724
|
-
}), this._branchSelections.set(t, {
|
|
725
|
-
kind: "auto",
|
|
726
|
-
selectedId: r.msgId
|
|
727
|
-
}));
|
|
728
|
-
continue;
|
|
729
|
-
}
|
|
730
|
-
n || this._branchSelections.set(t, {
|
|
878
|
+
for (let e of this._lastVisibleNodeKeys) {
|
|
879
|
+
if (this._tree.getNode(e)?.kind !== "input" || this._tree.getSiblingNodes(e).length <= 1) continue;
|
|
880
|
+
let t = this._tree.getGroupRoot(e);
|
|
881
|
+
this._branchSelections.get(t) || this._branchSelections.set(t, {
|
|
731
882
|
kind: "pinned",
|
|
732
|
-
|
|
883
|
+
selectedKey: e
|
|
733
884
|
});
|
|
734
885
|
}
|
|
735
886
|
}
|
|
736
|
-
|
|
737
|
-
for (let [e, t] of this.
|
|
738
|
-
if (t.kind
|
|
739
|
-
let n = this._tree.getSiblingNodes(e);
|
|
887
|
+
_resolvePendingRegenSelections() {
|
|
888
|
+
for (let [e, t] of this._regenSelections) {
|
|
889
|
+
if (t.kind === "user") continue;
|
|
890
|
+
let n = this._tree.getSiblingNodes(e).filter((e) => e.kind === "run");
|
|
740
891
|
if (n.length <= 1) continue;
|
|
741
892
|
let r = n.at(-1);
|
|
742
|
-
|
|
743
|
-
groupRoot: e,
|
|
744
|
-
newestId: r.msgId,
|
|
745
|
-
turnId: t.turnId
|
|
746
|
-
}), this._branchSelections.set(e, {
|
|
893
|
+
r && this._regenSelections.set(e, {
|
|
747
894
|
kind: "auto",
|
|
748
|
-
|
|
749
|
-
})
|
|
895
|
+
selectedRunId: r.runId
|
|
896
|
+
});
|
|
750
897
|
}
|
|
751
898
|
}
|
|
752
899
|
_onTreeAblyMessage(e) {
|
|
753
|
-
let t =
|
|
754
|
-
if (!
|
|
900
|
+
let t = D(e), n = t[m], r = t[d];
|
|
901
|
+
if (!n && !r) {
|
|
755
902
|
this._emitter.emit("ably-message", e);
|
|
756
903
|
return;
|
|
757
904
|
}
|
|
758
|
-
this.
|
|
905
|
+
r && this._lastVisibleNodeKeySet.has(r) && this._emitter.emit("ably-message", e);
|
|
759
906
|
}
|
|
760
|
-
|
|
761
|
-
if (this.
|
|
762
|
-
this._emitter.emit("
|
|
907
|
+
_onTreeRun(e) {
|
|
908
|
+
if (this._lastVisibleNodeKeySet.has(e.runId)) {
|
|
909
|
+
this._emitter.emit("run", e);
|
|
763
910
|
return;
|
|
764
911
|
}
|
|
765
|
-
e.type === "
|
|
912
|
+
e.type === "start" && this._isRunStartVisible(e) && (this._lastVisibleNodeKeySet.add(e.runId), this._emitter.emit("run", e));
|
|
766
913
|
}
|
|
767
|
-
|
|
914
|
+
_isRunStartVisible(e) {
|
|
768
915
|
let { parent: t } = e;
|
|
769
|
-
|
|
916
|
+
if (t === void 0) return !0;
|
|
917
|
+
let n = this._tree.getNodeByCodecMessageId(t);
|
|
918
|
+
return n ? this._lastVisibleNodeKeySet.has(N(n)) : !0;
|
|
770
919
|
}
|
|
771
|
-
_visibleChanged(e
|
|
772
|
-
if (e.length !== this.
|
|
773
|
-
for (let [t, n] of e.entries()) if (n !== this.
|
|
774
|
-
for (let [e, n] of t.entries()) if (n !== this._lastVisibleMessages[e]) return !0;
|
|
920
|
+
_visibleChanged(e) {
|
|
921
|
+
if (e.length !== this._lastVisibleNodeKeys.length) return !0;
|
|
922
|
+
for (let [t, n] of e.entries()) if (N(n) !== this._lastVisibleNodeKeys[t] || n.projection !== this._lastVisibleProjections[t]) return !0;
|
|
775
923
|
return !1;
|
|
776
924
|
}
|
|
777
|
-
}, W = (e) => new
|
|
778
|
-
return e.READY = "ready", e.CLOSED = "closed", e;
|
|
779
|
-
}(G || {}), ce = class {
|
|
925
|
+
}, W = (e) => new pe(e), me = () => {}, he = class {
|
|
780
926
|
constructor(e) {
|
|
781
|
-
|
|
927
|
+
this._views = /* @__PURE__ */ new Set(), this._state = "ready", this._pendingRunStarts = /* @__PURE__ */ new Map();
|
|
928
|
+
let t = ce(e.client, e.codec);
|
|
929
|
+
if (this._channel = e.client.channels.get(e.channelName, t), this._codec = e.codec, this._clientId = e.clientId, this._logger = (e.logger ?? ie({ logLevel: T.Silent })).withContext({ component: "ClientSession" }), this._emitter = new w(this._logger), this._hasAttachedOnce = this._channel.state === "attached", this._tree = R(this._codec, this._logger), this._view = W({
|
|
782
930
|
tree: this._tree,
|
|
783
931
|
channel: this._channel,
|
|
784
932
|
codec: this._codec,
|
|
785
933
|
sendDelegate: this._internalSend.bind(this),
|
|
786
934
|
logger: this._logger,
|
|
787
935
|
onClose: () => this._views.delete(this._view)
|
|
788
|
-
}), this.
|
|
936
|
+
}), this._decoder = this._codec.createDecoder(), this._encoder = this._codec.createEncoder(this._channel, this._clientId === void 0 ? void 0 : { clientId: this._clientId }), this._views.add(this._view), this.tree = this._tree, this.view = this._view, e.messages) {
|
|
789
937
|
let t;
|
|
790
938
|
for (let n of e.messages) {
|
|
791
|
-
let e = crypto.randomUUID(), r = {
|
|
792
|
-
|
|
939
|
+
let e = crypto.randomUUID(), r = {
|
|
940
|
+
[m]: e,
|
|
941
|
+
[_]: "user"
|
|
942
|
+
};
|
|
943
|
+
t && (r[v] = t), this._tree.applyMessage({
|
|
944
|
+
inputs: [this._codec.createUserMessage(n)],
|
|
945
|
+
outputs: []
|
|
946
|
+
}, r), t = e;
|
|
793
947
|
}
|
|
794
948
|
}
|
|
795
949
|
this._onMessage = (e) => {
|
|
796
950
|
this._handleMessage(e);
|
|
797
|
-
}, this.
|
|
951
|
+
}, this._onChannelStateChange = (e) => {
|
|
798
952
|
this._handleChannelStateChange(e);
|
|
799
953
|
}, this._channel.on(this._onChannelStateChange);
|
|
800
954
|
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
955
|
+
connect() {
|
|
956
|
+
return this._state === "closed" ? Promise.reject(new e.ErrorInfo("unable to connect; session is closed", C.SessionClosed, 400)) : this._connectPromise ? this._connectPromise : (this._logger.trace("DefaultClientSession.connect();"), this._connectPromise = this._channel.subscribe(this._onMessage).then(() => {
|
|
957
|
+
this._logger.debug("DefaultClientSession.connect(); subscribed and attached");
|
|
958
|
+
}, (t) => {
|
|
959
|
+
let n = new e.ErrorInfo(`unable to subscribe to channel; ${t instanceof Error ? t.message : String(t)}`, C.SessionSubscriptionError, 500, t instanceof e.ErrorInfo ? t : void 0);
|
|
960
|
+
throw this._logger.error("DefaultClientSession.connect(); subscribe failed"), this._emitter.emit("error", n), n;
|
|
961
|
+
}), this._connectPromise);
|
|
962
|
+
}
|
|
963
|
+
async _requireConnected(t) {
|
|
964
|
+
if (!this._connectPromise) throw new e.ErrorInfo(`unable to ${t}; connect() must be called before ${t}()`, C.InvalidArgument, 400);
|
|
965
|
+
return this._connectPromise;
|
|
966
|
+
}
|
|
967
|
+
_handleMessage(t) {
|
|
968
|
+
if (this._state !== "closed") try {
|
|
969
|
+
if (t.name === "ai-run-end") {
|
|
970
|
+
let n = D(t);
|
|
971
|
+
if ((n["run-reason"] ?? "complete") === "error") {
|
|
972
|
+
let t = n[S], r = t === void 0 ? NaN : Number(t), i = Number.isFinite(r) ? r : C.SessionSubscriptionError, a = n["error-message"] ?? "agent reported an error", o = i >= 1e4 && i < 6e4 ? Math.floor(i / 100) : 500, s = new e.ErrorInfo(a, i, o);
|
|
973
|
+
this._logger.error("ClientSession._handleMessage(); agent error received", {
|
|
974
|
+
runId: n[d],
|
|
975
|
+
invocationId: n[f],
|
|
976
|
+
code: i
|
|
977
|
+
}), this._emitter.emit("error", s);
|
|
815
978
|
}
|
|
816
|
-
this._tree.emitAblyMessage(e);
|
|
817
|
-
return;
|
|
818
979
|
}
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
let
|
|
824
|
-
|
|
825
|
-
for (let t of e) this._ownMsgIds.delete(t);
|
|
826
|
-
this._turnMsgIds.delete(n);
|
|
827
|
-
}
|
|
828
|
-
this._ownTurnIds.delete(n), this._tree.emitTurn({
|
|
829
|
-
type: D,
|
|
830
|
-
turnId: n,
|
|
831
|
-
clientId: r,
|
|
832
|
-
reason: i
|
|
833
|
-
});
|
|
980
|
+
let n = M(this._tree, this._decoder, t);
|
|
981
|
+
if (n && (n.type === "start" || n.type === "resume")) {
|
|
982
|
+
let e = D(t)[x];
|
|
983
|
+
if (e !== void 0) {
|
|
984
|
+
let t = this._pendingRunStarts.get(e);
|
|
985
|
+
t && (this._pendingRunStarts.delete(e), t.resolve(n.runId));
|
|
834
986
|
}
|
|
835
|
-
this._tree.emitAblyMessage(e);
|
|
836
|
-
return;
|
|
837
987
|
}
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
}
|
|
843
|
-
let a = n[m];
|
|
844
|
-
a && this._updateTurnObserverHeaders(a, n, r);
|
|
845
|
-
for (let i of t) i.kind === "message" ? this._handleMessageOutput(i.message, n, r, e.action) : this._handleEventOutput(i, n);
|
|
846
|
-
this._tree.emitAblyMessage(e);
|
|
847
|
-
} catch (e) {
|
|
848
|
-
let t = e instanceof s.ErrorInfo ? e : void 0;
|
|
849
|
-
this._emitter.emit("error", new s.ErrorInfo(`unable to process channel message; ${e instanceof Error ? e.message : String(e)}`, O.TransportSubscriptionError, 500, t));
|
|
988
|
+
this._tree.emitAblyMessage(t);
|
|
989
|
+
} catch (t) {
|
|
990
|
+
let n = t instanceof e.ErrorInfo ? t : void 0;
|
|
991
|
+
this._emitter.emit("error", new e.ErrorInfo(`unable to process channel message; ${t instanceof Error ? t.message : String(t)}`, C.SessionSubscriptionError, 500, n));
|
|
850
992
|
}
|
|
851
993
|
}
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
return;
|
|
857
|
-
}
|
|
858
|
-
r === "message.create" && this._upsertAndNotify(e, t, n);
|
|
859
|
-
}
|
|
860
|
-
_handleEventOutput(e, t) {
|
|
861
|
-
if (e.kind !== "event") return;
|
|
862
|
-
let n = e.event, r = t[m];
|
|
863
|
-
if (r) {
|
|
864
|
-
if (this._router.route(r, n)) {
|
|
865
|
-
this._accumulateAndEmit(r, e), this._codec.isTerminal(n) && this._turnObservers.delete(r);
|
|
866
|
-
return;
|
|
867
|
-
}
|
|
868
|
-
this._ownTurnIds.has(r) && !this._turnObservers.has(r) || (this._accumulateAndEmit(r, e), this._codec.isTerminal(n) && this._turnObservers.delete(r));
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
_handleAmendmentEvent(e, t) {
|
|
872
|
-
this._logger.trace("ClientTransport._handleAmendmentEvent();", { targetMsgId: e });
|
|
873
|
-
let n = this._tree.getNode(e);
|
|
874
|
-
if (!n) {
|
|
875
|
-
this._logger.debug("ClientTransport._handleAmendmentEvent(); target not found, dropping", { targetMsgId: e });
|
|
876
|
-
return;
|
|
877
|
-
}
|
|
878
|
-
let r = this._codec.createAccumulator();
|
|
879
|
-
r.initMessage(e, n.message), r.processOutputs([t]);
|
|
880
|
-
let i = r.messages.at(-1);
|
|
881
|
-
i && this._tree.upsert(e, i, n.headers, n.serial);
|
|
882
|
-
}
|
|
883
|
-
_handleChannelStateChange(e) {
|
|
884
|
-
if (this._state === G.CLOSED) return;
|
|
885
|
-
let { current: t, resumed: n } = e;
|
|
886
|
-
if (t === "attached" && !this._hasAttachedOnce) {
|
|
994
|
+
_handleChannelStateChange(t) {
|
|
995
|
+
if (this._state === "closed") return;
|
|
996
|
+
let { current: n, resumed: r } = t;
|
|
997
|
+
if (n === "attached" && !this._hasAttachedOnce) {
|
|
887
998
|
this._hasAttachedOnce = !0;
|
|
888
999
|
return;
|
|
889
1000
|
}
|
|
890
|
-
if (!(
|
|
891
|
-
this._logger.error("
|
|
892
|
-
current:
|
|
893
|
-
resumed:
|
|
894
|
-
previous:
|
|
895
|
-
});
|
|
896
|
-
let r = new s.ErrorInfo(`unable to deliver events; channel continuity lost (${t}${t === "attached" ? ", resumed: false" : ""})`, O.ChannelContinuityLost, 500, e.reason);
|
|
897
|
-
for (let e of this._ownTurnIds) this._router.errorStream(e, r);
|
|
898
|
-
this._emitter.emit("error", r);
|
|
899
|
-
}
|
|
900
|
-
_upsertAndNotify(e, t, n) {
|
|
901
|
-
let r = t[h];
|
|
902
|
-
r && this._tree.upsert(r, e, t, n);
|
|
903
|
-
}
|
|
904
|
-
_updateTurnObserverHeaders(e, t, n) {
|
|
905
|
-
let r = this._turnObservers.get(e);
|
|
906
|
-
r ? (Object.keys(t).length > 0 && Object.assign(r.headers, t), n !== void 0 && (r.serial = n)) : this._turnObservers.set(e, {
|
|
907
|
-
headers: { ...t },
|
|
908
|
-
serial: n,
|
|
909
|
-
accumulator: this._codec.createAccumulator()
|
|
910
|
-
});
|
|
911
|
-
}
|
|
912
|
-
_accumulateAndEmit(e, t) {
|
|
913
|
-
let n = this._turnObservers.get(e);
|
|
914
|
-
if (!n) return;
|
|
915
|
-
let r = n.headers[h];
|
|
916
|
-
if (r) {
|
|
917
|
-
let e = this._tree.getNode(r);
|
|
918
|
-
e && n.accumulator.initMessage(r, e.message);
|
|
919
|
-
}
|
|
920
|
-
n.accumulator.processOutputs([t]);
|
|
921
|
-
let i = n.accumulator.messages;
|
|
922
|
-
if (i.length === 0) return;
|
|
923
|
-
let a;
|
|
924
|
-
try {
|
|
925
|
-
a = structuredClone(i.at(-1));
|
|
926
|
-
} catch {
|
|
927
|
-
a = i.at(-1);
|
|
928
|
-
}
|
|
929
|
-
if (a) {
|
|
930
|
-
let e = n.headers[h];
|
|
931
|
-
e && this._tree.upsert(e, a, { ...n.headers }, n.serial);
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
async _publishCancel(e) {
|
|
935
|
-
this._logger.trace("ClientTransport._publishCancel();", { filter: e });
|
|
936
|
-
let t = {};
|
|
937
|
-
e.turnId ? t[y] = e.turnId : e.own ? t[b] = "true" : e.clientId ? t[S] = e.clientId : e.all && (t[x] = "true"), await this._channel.publish({
|
|
938
|
-
name: T,
|
|
939
|
-
extras: { headers: t }
|
|
1001
|
+
if (!(n === "failed" || n === "suspended" || n === "detached" || n === "attached" && !r)) return;
|
|
1002
|
+
this._logger.error("ClientSession._handleChannelStateChange(); channel continuity lost", {
|
|
1003
|
+
current: n,
|
|
1004
|
+
resumed: r,
|
|
1005
|
+
previous: t.previous
|
|
940
1006
|
});
|
|
1007
|
+
let i = new e.ErrorInfo(`unable to deliver events; channel continuity lost (${n}${n === "attached" ? ", resumed: false" : ""})`, C.ChannelContinuityLost, 500, t.reason);
|
|
1008
|
+
this._emitter.emit("error", i);
|
|
941
1009
|
}
|
|
942
|
-
|
|
943
|
-
for (let t of
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
let t = /* @__PURE__ */ new Set(), n = this._tree.getActiveTurnIds();
|
|
947
|
-
if (e.all) for (let e of n.values()) for (let n of e) t.add(n);
|
|
948
|
-
else if (e.own) {
|
|
949
|
-
let e = n.get(this._clientId ?? "");
|
|
950
|
-
if (e) for (let n of e) t.add(n);
|
|
951
|
-
} else if (e.clientId) {
|
|
952
|
-
let r = n.get(e.clientId);
|
|
953
|
-
if (r) for (let e of r) t.add(e);
|
|
954
|
-
} else if (e.turnId) {
|
|
955
|
-
for (let r of n.values()) if (r.has(e.turnId)) {
|
|
956
|
-
t.add(e.turnId);
|
|
957
|
-
break;
|
|
958
|
-
}
|
|
1010
|
+
_cleanupFailedSend(e) {
|
|
1011
|
+
for (let t of e) {
|
|
1012
|
+
let e = this._tree.getNodeByCodecMessageId(t);
|
|
1013
|
+
e?.kind === "input" && e.serial === void 0 && this._tree.delete(e.codecMessageId);
|
|
959
1014
|
}
|
|
960
|
-
return t;
|
|
961
1015
|
}
|
|
962
1016
|
createView() {
|
|
963
|
-
if (this._state ===
|
|
964
|
-
this._logger.trace("
|
|
965
|
-
let
|
|
1017
|
+
if (this._state === "closed") throw new e.ErrorInfo("unable to create view; session is closed", C.SessionClosed, 400);
|
|
1018
|
+
this._logger.trace("DefaultClientSession.createView();");
|
|
1019
|
+
let t = W({
|
|
966
1020
|
tree: this._tree,
|
|
967
1021
|
channel: this._channel,
|
|
968
1022
|
codec: this._codec,
|
|
969
1023
|
sendDelegate: this._internalSend.bind(this),
|
|
970
1024
|
logger: this._logger,
|
|
971
|
-
onClose: () => this._views.delete(
|
|
1025
|
+
onClose: () => this._views.delete(t)
|
|
972
1026
|
});
|
|
973
|
-
return this._views.add(
|
|
1027
|
+
return this._views.add(t), t;
|
|
974
1028
|
}
|
|
975
|
-
async _internalSend(
|
|
976
|
-
if (this._state ===
|
|
1029
|
+
async _internalSend(t, n, r) {
|
|
1030
|
+
if (this._state === "closed" || (await this._requireConnected("send"), this._state === "closed")) throw new e.ErrorInfo("unable to send; session is closed", C.SessionClosed, 400);
|
|
977
1031
|
let i = this._channel.state;
|
|
978
|
-
if (i !== "attached" && i !== "attaching") throw new
|
|
979
|
-
this._logger.trace("
|
|
980
|
-
let a =
|
|
981
|
-
|
|
982
|
-
let c =
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
let e =
|
|
987
|
-
e && (p = e.msgId);
|
|
988
|
-
}
|
|
989
|
-
let m = t?.parent === void 0 ? p : t.parent;
|
|
990
|
-
for (let e of a) {
|
|
991
|
-
let n = crypto.randomUUID();
|
|
992
|
-
this._ownMsgIds.add(n), u.add(n);
|
|
993
|
-
let r = t?.parent === void 0 ? p : t.parent, i = F({
|
|
1032
|
+
if (i !== "attached" && i !== "attaching") throw new e.ErrorInfo(`unable to send; channel is ${i}`, C.ChannelNotReady, 400);
|
|
1033
|
+
this._logger.trace("ClientSession._internalSend();");
|
|
1034
|
+
let a = n?.runId !== void 0, o = n?.runId, s;
|
|
1035
|
+
n?.parent === void 0 && !n?.forkOf && (s = r);
|
|
1036
|
+
let c = /* @__PURE__ */ new Set(), l = [];
|
|
1037
|
+
for (let e of t) {
|
|
1038
|
+
let t = crypto.randomUUID(), r = e.codecMessageId ?? crypto.randomUUID();
|
|
1039
|
+
c.add(r);
|
|
1040
|
+
let i = e.kind !== "user-message" && (e.kind === "regenerate" || e.codecMessageId !== void 0), a = e.parent ?? (n?.parent === void 0 ? s : n.parent), u = n?.forkOf, d = e.kind === "regenerate" ? e.target : void 0, f = le({
|
|
994
1041
|
role: "user",
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
parent:
|
|
999
|
-
forkOf:
|
|
1042
|
+
runId: o,
|
|
1043
|
+
codecMessageId: r,
|
|
1044
|
+
runClientId: this._clientId,
|
|
1045
|
+
...a !== void 0 && { parent: a },
|
|
1046
|
+
...u !== void 0 && { forkOf: u },
|
|
1047
|
+
...d !== void 0 && { regenerates: d },
|
|
1048
|
+
inputEventId: t
|
|
1000
1049
|
});
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1050
|
+
i || this._tree.applyMessage({
|
|
1051
|
+
inputs: [e],
|
|
1052
|
+
outputs: []
|
|
1053
|
+
}, f), l.push({
|
|
1054
|
+
input: e,
|
|
1055
|
+
codecMessageId: r,
|
|
1056
|
+
inputEventId: t,
|
|
1057
|
+
headers: f,
|
|
1058
|
+
isWireOnly: i
|
|
1059
|
+
}), !i && n?.parent === void 0 && !n?.forkOf && e.parent === void 0 && (s = r);
|
|
1010
1060
|
}
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
"Content-Type": "application/json",
|
|
1030
|
-
...v
|
|
1031
|
-
},
|
|
1032
|
-
body: JSON.stringify(_),
|
|
1033
|
-
...this._credentials ? { credentials: this._credentials } : {}
|
|
1034
|
-
}).then((e) => {
|
|
1035
|
-
if (!e.ok) {
|
|
1036
|
-
let t = new s.ErrorInfo(`unable to send; HTTP POST to ${this._api} returned ${String(e.status)} ${e.statusText}`, O.TransportSendFailed, e.status);
|
|
1037
|
-
this._emitter.emit("error", t), this._router.errorStream(o, t);
|
|
1061
|
+
let u = l.at(-1);
|
|
1062
|
+
if (u === void 0) throw new e.ErrorInfo("unable to send; inputs array is empty (include at least one input)", C.InvalidArgument, 400);
|
|
1063
|
+
let d = u.inputEventId, f = u.codecMessageId, p = new Promise((e, t) => {
|
|
1064
|
+
this._pendingRunStarts.set(f, {
|
|
1065
|
+
resolve: e,
|
|
1066
|
+
reject: t
|
|
1067
|
+
});
|
|
1068
|
+
});
|
|
1069
|
+
return p.catch(() => {}), await (async () => {
|
|
1070
|
+
try {
|
|
1071
|
+
for (let e of l) await this._encoder.publishInput(e.input, {
|
|
1072
|
+
extras: { headers: e.headers },
|
|
1073
|
+
messageId: e.codecMessageId,
|
|
1074
|
+
...this._clientId !== void 0 && { clientId: this._clientId }
|
|
1075
|
+
});
|
|
1076
|
+
} catch (t) {
|
|
1077
|
+
let n = t instanceof e.ErrorInfo ? t : void 0, r = n?.statusCode === 401 || n?.statusCode === 403, i = new e.ErrorInfo(r ? "unable to publish events; missing publish capability on the channel" : `unable to publish events; ${t instanceof Error ? t.message : String(t)}`, r ? C.InsufficientCapability : C.SessionSendFailed, r ? 401 : 500, n);
|
|
1078
|
+
throw this._emitter.emit("error", i), this._pendingRunStarts.delete(f), a || this._cleanupFailedSend([...c]), i;
|
|
1038
1079
|
}
|
|
1039
|
-
})
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1080
|
+
})(), {
|
|
1081
|
+
inputCodecMessageId: f,
|
|
1082
|
+
runId: p,
|
|
1083
|
+
inputEventId: d,
|
|
1084
|
+
cancel: async () => {
|
|
1085
|
+
await this._publishCancel({
|
|
1086
|
+
inputCodecMessageId: f,
|
|
1087
|
+
...o !== void 0 && { runId: o }
|
|
1088
|
+
});
|
|
1089
|
+
},
|
|
1090
|
+
optimisticCodecMessageIds: [...c],
|
|
1091
|
+
toInvocation: () => ue.fromJSON({
|
|
1092
|
+
inputEventId: d,
|
|
1093
|
+
sessionName: this._channel.name
|
|
1094
|
+
})
|
|
1047
1095
|
};
|
|
1048
1096
|
}
|
|
1049
1097
|
async cancel(e) {
|
|
1050
|
-
|
|
1051
|
-
let t = e ?? { own: !0 };
|
|
1052
|
-
this._logger.debug("ClientTransport.cancel();", { filter: t }), await this._publishCancel(t), this._closeMatchingTurnStreams(t);
|
|
1053
|
-
}
|
|
1054
|
-
stageEvents(e, t) {
|
|
1055
|
-
if (this._logger.trace("ClientTransport.stageEvents();", {
|
|
1056
|
-
msgId: e,
|
|
1057
|
-
eventCount: t.length
|
|
1058
|
-
}), this._state === G.CLOSED) {
|
|
1059
|
-
this._logger.warn("ClientTransport.stageEvents(); transport is closed", { msgId: e });
|
|
1060
|
-
return;
|
|
1061
|
-
}
|
|
1062
|
-
if (!this._tree.getNode(e)) {
|
|
1063
|
-
this._logger.warn("ClientTransport.stageEvents(); msgId not found in tree", { msgId: e });
|
|
1064
|
-
return;
|
|
1065
|
-
}
|
|
1066
|
-
if (t.length === 0) return;
|
|
1067
|
-
let n = {
|
|
1068
|
-
kind: "event",
|
|
1069
|
-
msgId: e,
|
|
1070
|
-
events: t
|
|
1071
|
-
};
|
|
1072
|
-
this._applyEventsToTree([n]), this._pendingLocalEvents.push(n);
|
|
1073
|
-
}
|
|
1074
|
-
stageMessage(e, t) {
|
|
1075
|
-
if (this._logger.trace("ClientTransport.stageMessage();", { msgId: e }), this._state === G.CLOSED) {
|
|
1076
|
-
this._logger.warn("ClientTransport.stageMessage(); transport is closed", { msgId: e });
|
|
1077
|
-
return;
|
|
1078
|
-
}
|
|
1079
|
-
let n = this._tree.getNode(e);
|
|
1080
|
-
if (!n) {
|
|
1081
|
-
this._logger.warn("ClientTransport.stageMessage(); msgId not found in tree", { msgId: e });
|
|
1082
|
-
return;
|
|
1083
|
-
}
|
|
1084
|
-
this._tree.upsert(e, t, n.headers, n.serial);
|
|
1085
|
-
}
|
|
1086
|
-
_applyEventsToTree(e) {
|
|
1087
|
-
for (let t of e) {
|
|
1088
|
-
let e = this._tree.getNode(t.msgId);
|
|
1089
|
-
if (!e) continue;
|
|
1090
|
-
let n = t.events.map((e) => ({
|
|
1091
|
-
kind: "event",
|
|
1092
|
-
event: e,
|
|
1093
|
-
messageId: t.msgId
|
|
1094
|
-
})), r = this._codec.createAccumulator();
|
|
1095
|
-
r.initMessage(t.msgId, e.message), r.processOutputs(n);
|
|
1096
|
-
let i = r.messages.at(-1);
|
|
1097
|
-
i && this._tree.upsert(t.msgId, i, e.headers, e.serial);
|
|
1098
|
-
}
|
|
1098
|
+
return this._publishCancel({ runId: e });
|
|
1099
1099
|
}
|
|
1100
|
-
async
|
|
1101
|
-
if (this._state ===
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
e.type === "x-ably-turn-end" && (n.delete(e.turnId), n.size === 0 && r());
|
|
1111
|
-
});
|
|
1112
|
-
this._closeResolvers.push(r);
|
|
1100
|
+
async _publishCancel(e) {
|
|
1101
|
+
if (this._state === "closed" || (await this._requireConnected("cancel"), this._state === "closed")) return;
|
|
1102
|
+
this._logger.debug("ClientSession._publishCancel();", {
|
|
1103
|
+
runId: e.runId,
|
|
1104
|
+
inputCodecMessageId: e.inputCodecMessageId
|
|
1105
|
+
});
|
|
1106
|
+
let t = { [p]: crypto.randomUUID() };
|
|
1107
|
+
e.runId !== void 0 && (t[d] = e.runId), e.inputCodecMessageId !== void 0 && (t[x] = e.inputCodecMessageId), await this._channel.publish({
|
|
1108
|
+
name: ee,
|
|
1109
|
+
extras: { ai: { transport: t } }
|
|
1113
1110
|
});
|
|
1114
1111
|
}
|
|
1115
1112
|
on(e, t) {
|
|
1116
|
-
if (this._state ===
|
|
1113
|
+
if (this._state === "closed") return me;
|
|
1117
1114
|
let n = t;
|
|
1118
1115
|
return this._emitter.on(e, n), () => {
|
|
1119
1116
|
this._emitter.off(e, n);
|
|
1120
1117
|
};
|
|
1121
1118
|
}
|
|
1122
|
-
async close(
|
|
1123
|
-
if (this._state !==
|
|
1124
|
-
|
|
1125
|
-
try {
|
|
1126
|
-
await this._publishCancel(e.cancel);
|
|
1127
|
-
} catch {}
|
|
1128
|
-
this._closeMatchingTurnStreams(e.cancel);
|
|
1129
|
-
}
|
|
1130
|
-
this._channel.unsubscribe(this._onMessage), this._channel.off(this._onChannelStateChange);
|
|
1131
|
-
for (let e of this._ownTurnIds) this._router.closeStream(e);
|
|
1132
|
-
this._turnObservers.clear(), this._emitter.off();
|
|
1119
|
+
async close() {
|
|
1120
|
+
if (this._state !== "closed") {
|
|
1121
|
+
this._state = "closed", this._logger.info("ClientSession.close();"), this._connectPromise && this._channel.unsubscribe(this._onMessage), this._channel.off(this._onChannelStateChange), this._emitter.off();
|
|
1133
1122
|
for (let e of this._views) e.close();
|
|
1134
|
-
this._views.clear()
|
|
1135
|
-
|
|
1136
|
-
|
|
1123
|
+
if (this._views.clear(), this._pendingRunStarts.size > 0) {
|
|
1124
|
+
let t = new e.ErrorInfo("unable to await run-start; session closed", C.SessionClosed, 400);
|
|
1125
|
+
for (let e of this._pendingRunStarts.values()) e.reject(t);
|
|
1126
|
+
this._pendingRunStarts.clear();
|
|
1127
|
+
}
|
|
1128
|
+
try {
|
|
1129
|
+
await this._encoder.close();
|
|
1130
|
+
} catch {}
|
|
1131
|
+
if (this._connectPromise) try {
|
|
1132
|
+
await this._channel.detach();
|
|
1133
|
+
} catch (e) {
|
|
1134
|
+
this._logger.debug("ClientSession.close(); channel detach failed", { error: e });
|
|
1135
|
+
}
|
|
1137
1136
|
}
|
|
1138
1137
|
}
|
|
1139
|
-
},
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1138
|
+
}, ge = (e) => new he(e), G = n({
|
|
1139
|
+
nearest: void 0,
|
|
1140
|
+
providers: {}
|
|
1141
|
+
}), K = ({ children: n, ...r }) => {
|
|
1142
|
+
let c = t(), { channelName: u } = r, d = s(void 0), f = s(u), p = s([]), m = s(!1), h = s(void 0);
|
|
1143
|
+
if (!(d.current || h.current) || f.current !== u) {
|
|
1144
|
+
f.current = u, d.current && p.current.push(d.current);
|
|
1143
1145
|
try {
|
|
1144
|
-
|
|
1145
|
-
...
|
|
1146
|
-
|
|
1147
|
-
}),
|
|
1148
|
-
} catch (
|
|
1149
|
-
|
|
1146
|
+
d.current = ge({
|
|
1147
|
+
...r,
|
|
1148
|
+
client: c
|
|
1149
|
+
}), h.current = void 0;
|
|
1150
|
+
} catch (t) {
|
|
1151
|
+
d.current = void 0, h.current = t instanceof e.ErrorInfo ? t : new e.ErrorInfo("Unknown error while creating client session", C.BadRequest, 400);
|
|
1150
1152
|
}
|
|
1151
1153
|
}
|
|
1152
|
-
let
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
}), [
|
|
1156
|
-
|
|
1157
|
-
|
|
1154
|
+
let g = i(G), _ = d.current, v = h.current, y = o(() => ({
|
|
1155
|
+
session: _,
|
|
1156
|
+
sessionError: v
|
|
1157
|
+
}), [_, v]), b = o(() => ({
|
|
1158
|
+
nearest: y,
|
|
1159
|
+
providers: {
|
|
1160
|
+
...g.providers,
|
|
1161
|
+
[u]: y
|
|
1162
|
+
}
|
|
1158
1163
|
}), [
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1164
|
+
u,
|
|
1165
|
+
g,
|
|
1166
|
+
y
|
|
1162
1167
|
]);
|
|
1163
|
-
return
|
|
1164
|
-
for (let e of
|
|
1165
|
-
}, [
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
+
return a(() => () => {
|
|
1169
|
+
for (let e of p.current) e.close();
|
|
1170
|
+
}, [u]), a(() => {
|
|
1171
|
+
d.current?.connect();
|
|
1172
|
+
}, [u]), a(() => (m.current = !1, () => {
|
|
1173
|
+
m.current = !0, Promise.resolve().then(() => {
|
|
1174
|
+
m.current && d.current?.close();
|
|
1168
1175
|
});
|
|
1169
|
-
}), []), /* @__PURE__ */
|
|
1170
|
-
value:
|
|
1171
|
-
children:
|
|
1172
|
-
value: x,
|
|
1173
|
-
children: t
|
|
1174
|
-
})
|
|
1176
|
+
}), []), /* @__PURE__ */ l(G.Provider, {
|
|
1177
|
+
value: b,
|
|
1178
|
+
children: n
|
|
1175
1179
|
});
|
|
1176
|
-
},
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
}
|
|
1180
|
-
let
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
let t = n(f), i = e ?? t?.transport, [a, s] = o(() => /* @__PURE__ */ new Map());
|
|
1189
|
-
return r(() => {
|
|
1190
|
-
if (i) return s(i.tree.getActiveTurnIds()), i.tree.on("turn", (e) => {
|
|
1191
|
-
s((t) => {
|
|
1192
|
-
let n = new Map(t);
|
|
1193
|
-
if (e.type === "x-ably-turn-start") {
|
|
1194
|
-
let t = new Set(n.get(e.clientId));
|
|
1195
|
-
t.add(e.turnId), n.set(e.clientId, t);
|
|
1196
|
-
} else {
|
|
1197
|
-
let t = n.get(e.clientId);
|
|
1198
|
-
if (t) {
|
|
1199
|
-
let r = new Set(t);
|
|
1200
|
-
r.delete(e.turnId), r.size === 0 ? n.delete(e.clientId) : n.set(e.clientId, r);
|
|
1201
|
-
}
|
|
1202
|
-
}
|
|
1203
|
-
return n;
|
|
1204
|
-
});
|
|
1180
|
+
}, q = ({ session: e, skip: t } = {}) => {
|
|
1181
|
+
let { nearest: n } = i(G), r = n?.session;
|
|
1182
|
+
if (!t && e !== null) return e ?? r;
|
|
1183
|
+
}, J = ({ session: e, skip: t } = {}) => {
|
|
1184
|
+
let n = q({
|
|
1185
|
+
session: e,
|
|
1186
|
+
skip: t
|
|
1187
|
+
}), [r, i] = c([]), o = s([]);
|
|
1188
|
+
return a(() => {
|
|
1189
|
+
if (o.current = [], i([]), n) return n.tree.on("ably-message", (e) => {
|
|
1190
|
+
let t = [...o.current, e];
|
|
1191
|
+
o.current = t, i(t);
|
|
1205
1192
|
});
|
|
1206
|
-
}, [
|
|
1193
|
+
}, [n]), r;
|
|
1207
1194
|
}, Y = {
|
|
1208
1195
|
get tree() {
|
|
1209
|
-
throw new
|
|
1196
|
+
throw new e.ErrorInfo("unable to access tree; hook is skipped", C.InvalidArgument, 400);
|
|
1210
1197
|
},
|
|
1211
1198
|
get view() {
|
|
1212
|
-
throw new
|
|
1199
|
+
throw new e.ErrorInfo("unable to access view; hook is skipped", C.InvalidArgument, 400);
|
|
1200
|
+
},
|
|
1201
|
+
connect: () => {
|
|
1202
|
+
throw new e.ErrorInfo("unable to connect; hook is skipped", C.InvalidArgument, 400);
|
|
1213
1203
|
},
|
|
1214
1204
|
createView: () => {
|
|
1215
|
-
throw new
|
|
1205
|
+
throw new e.ErrorInfo("unable to create view; hook is skipped", C.InvalidArgument, 400);
|
|
1216
1206
|
},
|
|
1217
1207
|
cancel: () => {
|
|
1218
|
-
throw new
|
|
1219
|
-
},
|
|
1220
|
-
stageEvents: () => {
|
|
1221
|
-
throw new s.ErrorInfo("unable to stage events; hook is skipped", O.InvalidArgument, 400);
|
|
1222
|
-
},
|
|
1223
|
-
stageMessage: () => {
|
|
1224
|
-
throw new s.ErrorInfo("unable to stage message; hook is skipped", O.InvalidArgument, 400);
|
|
1225
|
-
},
|
|
1226
|
-
waitForTurn: () => {
|
|
1227
|
-
throw new s.ErrorInfo("unable to wait for turn; hook is skipped", O.InvalidArgument, 400);
|
|
1208
|
+
throw new e.ErrorInfo("unable to cancel; hook is skipped", C.InvalidArgument, 400);
|
|
1228
1209
|
},
|
|
1229
1210
|
on: () => {
|
|
1230
|
-
throw new
|
|
1211
|
+
throw new e.ErrorInfo("unable to subscribe; hook is skipped", C.InvalidArgument, 400);
|
|
1231
1212
|
},
|
|
1232
1213
|
close: () => {
|
|
1233
|
-
throw new
|
|
1214
|
+
throw new e.ErrorInfo("unable to close; hook is skipped", C.InvalidArgument, 400);
|
|
1234
1215
|
}
|
|
1235
|
-
}, X = ({ channelName:
|
|
1236
|
-
let
|
|
1237
|
-
l.current =
|
|
1238
|
-
let u =
|
|
1239
|
-
if (
|
|
1216
|
+
}, X = ({ channelName: t, skip: n, onError: r } = {}) => {
|
|
1217
|
+
let { nearest: o, providers: c } = i(G), l = s(r);
|
|
1218
|
+
l.current = r;
|
|
1219
|
+
let u = n ? void 0 : t === void 0 ? o?.session : c[t]?.session;
|
|
1220
|
+
if (a(() => {
|
|
1240
1221
|
if (u) return u.on("error", (e) => {
|
|
1241
1222
|
l.current?.(e);
|
|
1242
1223
|
});
|
|
1243
|
-
}, [u]),
|
|
1244
|
-
if (
|
|
1245
|
-
let
|
|
1246
|
-
return
|
|
1247
|
-
|
|
1248
|
-
|
|
1224
|
+
}, [u]), n) return { session: Y };
|
|
1225
|
+
if (t !== void 0) {
|
|
1226
|
+
let n = c[t];
|
|
1227
|
+
return n ? n.session ? { session: n.session } : {
|
|
1228
|
+
session: Y,
|
|
1229
|
+
sessionError: n.sessionError
|
|
1249
1230
|
} : {
|
|
1250
|
-
|
|
1251
|
-
|
|
1231
|
+
session: Y,
|
|
1232
|
+
sessionError: new e.ErrorInfo(`unable to use session; no ClientSessionProvider found for channelName "${t}"`, C.BadRequest, 400)
|
|
1252
1233
|
};
|
|
1253
1234
|
}
|
|
1254
|
-
return
|
|
1255
|
-
|
|
1256
|
-
|
|
1235
|
+
return o ? o.session ? { session: o.session } : {
|
|
1236
|
+
session: Y,
|
|
1237
|
+
sessionError: o.sessionError
|
|
1257
1238
|
} : {
|
|
1258
|
-
|
|
1259
|
-
|
|
1239
|
+
session: Y,
|
|
1240
|
+
sessionError: new e.ErrorInfo("unable to use session; no ClientSessionProvider found in the tree", C.BadRequest, 400)
|
|
1260
1241
|
};
|
|
1261
|
-
},
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1242
|
+
}, _e = {
|
|
1243
|
+
hasSiblings: !1,
|
|
1244
|
+
siblings: [],
|
|
1245
|
+
index: 0,
|
|
1246
|
+
selected: void 0
|
|
1247
|
+
}, Z = ({ session: t, view: n, limit: i, skip: o } = {}) => {
|
|
1248
|
+
let l = q({
|
|
1249
|
+
session: t,
|
|
1250
|
+
skip: o
|
|
1251
|
+
}), u = o ? void 0 : n ?? l?.view, [d, f] = c(() => u?.getMessages() ?? []), [p, m] = c(() => u?.hasOlder() ?? !1), [h, g] = c(!1), [_, v] = c(), y = s(!1), b = i !== void 0, x = s(!1);
|
|
1252
|
+
a(() => {
|
|
1253
|
+
if (!u) {
|
|
1254
|
+
f([]), m(!1), v(void 0);
|
|
1266
1255
|
return;
|
|
1267
1256
|
}
|
|
1268
|
-
return
|
|
1269
|
-
|
|
1257
|
+
return x.current = !1, f(u.getMessages()), m(u.hasOlder()), v(void 0), u.on("update", () => {
|
|
1258
|
+
f(u.getMessages()), m(u.hasOlder());
|
|
1270
1259
|
});
|
|
1271
|
-
}, [
|
|
1272
|
-
let
|
|
1273
|
-
if (!(!
|
|
1274
|
-
|
|
1260
|
+
}, [u]);
|
|
1261
|
+
let S = r(async () => {
|
|
1262
|
+
if (!(!u || y.current)) {
|
|
1263
|
+
y.current = !0, g(!0);
|
|
1275
1264
|
try {
|
|
1276
|
-
await
|
|
1277
|
-
} catch (
|
|
1278
|
-
|
|
1265
|
+
await u.loadOlder(i), v(void 0);
|
|
1266
|
+
} catch (t) {
|
|
1267
|
+
t instanceof e.ErrorInfo ? v(t) : v(new e.ErrorInfo("Unknown error loading older messages", C.BadRequest, 400));
|
|
1279
1268
|
} finally {
|
|
1280
|
-
|
|
1269
|
+
y.current = !1, g(!1);
|
|
1281
1270
|
}
|
|
1282
1271
|
}
|
|
1283
|
-
}, [
|
|
1284
|
-
return
|
|
1285
|
-
!
|
|
1272
|
+
}, [u, i]);
|
|
1273
|
+
return a(() => {
|
|
1274
|
+
!b || x.current || !u || (x.current = !0, S());
|
|
1286
1275
|
}, [
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1276
|
+
b,
|
|
1277
|
+
u,
|
|
1278
|
+
S
|
|
1290
1279
|
]), {
|
|
1291
|
-
messages:
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
}, [m]),
|
|
1316
|
-
update: t(async (e, t, n) => {
|
|
1317
|
-
if (!m) throw new s.ErrorInfo("unable to update; view is not available", O.InvalidArgument, 400);
|
|
1318
|
-
return m.update(e, t, n);
|
|
1319
|
-
}, [m])
|
|
1280
|
+
messages: d,
|
|
1281
|
+
hasOlder: p,
|
|
1282
|
+
loading: h,
|
|
1283
|
+
loadError: _,
|
|
1284
|
+
loadOlder: S,
|
|
1285
|
+
runOf: r((e) => u?.runOf(e), [u]),
|
|
1286
|
+
run: r((e) => u?.run(e), [u]),
|
|
1287
|
+
runs: r(() => u?.runs() ?? [], [u]),
|
|
1288
|
+
branchSelection: r((e) => u?.branchSelection(e) ?? _e, [u]),
|
|
1289
|
+
selectSibling: r((e, t) => {
|
|
1290
|
+
u?.selectSibling(e, t);
|
|
1291
|
+
}, [u]),
|
|
1292
|
+
send: r(async (t, n) => {
|
|
1293
|
+
if (!u) throw new e.ErrorInfo("unable to send; view is not available", C.InvalidArgument, 400);
|
|
1294
|
+
return u.send(t, n);
|
|
1295
|
+
}, [u]),
|
|
1296
|
+
regenerate: r(async (t, n) => {
|
|
1297
|
+
if (!u) throw new e.ErrorInfo("unable to regenerate; view is not available", C.InvalidArgument, 400);
|
|
1298
|
+
return u.regenerate(t, n);
|
|
1299
|
+
}, [u]),
|
|
1300
|
+
edit: r(async (t, n, r) => {
|
|
1301
|
+
if (!u) throw new e.ErrorInfo("unable to edit; view is not available", C.InvalidArgument, 400);
|
|
1302
|
+
return u.edit(t, n, r);
|
|
1303
|
+
}, [u])
|
|
1320
1304
|
};
|
|
1321
|
-
}, Q = ({
|
|
1322
|
-
let
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1305
|
+
}, Q = ({ session: e, limit: t, skip: n } = {}) => {
|
|
1306
|
+
let r = q({
|
|
1307
|
+
session: e,
|
|
1308
|
+
skip: n
|
|
1309
|
+
}), [i, o] = c();
|
|
1310
|
+
return a(() => {
|
|
1311
|
+
if (!r) {
|
|
1312
|
+
o(void 0);
|
|
1326
1313
|
return;
|
|
1327
1314
|
}
|
|
1328
|
-
let e =
|
|
1329
|
-
return
|
|
1315
|
+
let e = r.createView();
|
|
1316
|
+
return o(e), () => {
|
|
1330
1317
|
e.close();
|
|
1331
1318
|
};
|
|
1332
|
-
}, [
|
|
1333
|
-
view:
|
|
1319
|
+
}, [r]), Z({
|
|
1320
|
+
view: i,
|
|
1334
1321
|
limit: t,
|
|
1335
|
-
skip:
|
|
1322
|
+
skip: n
|
|
1336
1323
|
});
|
|
1337
|
-
}, $ = ({
|
|
1338
|
-
let
|
|
1324
|
+
}, $ = ({ session: e } = {}) => {
|
|
1325
|
+
let t = q({ session: e });
|
|
1339
1326
|
return {
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1327
|
+
getRunNode: r((e) => t?.tree.getRunNode(e), [t]),
|
|
1328
|
+
getNodeByCodecMessageId: r((e) => t?.tree.getNodeByCodecMessageId(e), [t]),
|
|
1329
|
+
getSiblingNodes: r((e) => t?.tree.getSiblingNodes(e) ?? [], [t])
|
|
1343
1330
|
};
|
|
1344
|
-
},
|
|
1345
|
-
|
|
1346
|
-
|
|
1331
|
+
}, ve = () => ({
|
|
1332
|
+
ClientSessionProvider: K,
|
|
1333
|
+
useClientSession: (e) => X(e ?? {}),
|
|
1347
1334
|
useView: (e) => Z(e ?? {}),
|
|
1348
|
-
useActiveTurns: (e) => J(e ?? {}),
|
|
1349
1335
|
useTree: (e) => $(e ?? {}),
|
|
1350
|
-
useAblyMessages: (e) =>
|
|
1336
|
+
useAblyMessages: (e) => J(e ?? {}),
|
|
1351
1337
|
useCreateView: (e) => Q(e ?? {})
|
|
1352
1338
|
});
|
|
1353
1339
|
//#endregion
|
|
1354
|
-
export {
|
|
1340
|
+
export { K as ClientSessionProvider, ve as createSessionHooks, J as useAblyMessages, X as useClientSession, Q as useCreateView, $ as useTree, Z as useView };
|
|
1355
1341
|
|
|
1356
1342
|
//# sourceMappingURL=ably-ai-transport-react.js.map
|