@aintela/chat 0.6.0 → 0.8.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 +43 -0
- package/dist/components.js +9004 -7708
- package/dist/components.js.map +1 -1
- package/dist/{httpArtifactService-DYQDPVVH.js → detachedRunActivity-RNg1JDDW.js} +799 -334
- package/dist/detachedRunActivity-RNg1JDDW.js.map +1 -0
- package/dist/detachedRunModel-X59Pb5V-.js +67 -0
- package/dist/{detachedRunModel-1ii9qGTW.js.map → detachedRunModel-X59Pb5V-.js.map} +1 -1
- package/dist/hooks.js +1 -1
- package/dist/httpSessionService-DLdoBZRW.js +320 -0
- package/dist/httpSessionService-DLdoBZRW.js.map +1 -0
- package/dist/index.js +325 -275
- package/dist/models.js +47 -41
- package/dist/{serializer-BNsp8xs2.js → serializer-DvR3nOcf.js} +781 -652
- package/dist/serializer-DvR3nOcf.js.map +1 -0
- package/dist/services.js +22 -15
- package/dist/src/components/AgentTeam/BillingLimitBanner.d.ts +7 -0
- package/dist/src/components/AgentTeam/ChatAgentHeader.d.ts +34 -0
- package/dist/src/components/AgentTeam/HandoffDivider.d.ts +13 -0
- package/dist/src/components/AgentTeam/PersonaAvatar.d.ts +20 -0
- package/dist/src/components/AgentTeam/__tests__/agentTeam.test.d.ts +0 -0
- package/dist/src/components/AgentTeam/__tests__/billingLimit.test.d.ts +0 -0
- package/dist/src/components/AgentTeam/__tests__/messagePersona.test.d.ts +0 -0
- package/dist/src/components/AgentTeam/agentRoles.d.ts +33 -0
- package/dist/src/components/AgentTeam/chatAgent.d.ts +24 -0
- package/dist/src/components/AgentTeam/index.d.ts +7 -0
- package/dist/src/components/AgentTeam/personas.d.ts +60 -0
- package/dist/src/components/AiChat/AiChatContext.d.ts +9 -1
- package/dist/src/components/AiChat/AiChatTypes.d.ts +17 -1
- package/dist/src/components/AiChat/ChatPane/InteractionOriginBadge.d.ts +15 -0
- package/dist/src/components/AiChat/ChatPane/messageUserOrigin.test.d.ts +1 -0
- package/dist/src/components/AiChat/aiChatEngine.d.ts +13 -1
- package/dist/src/components/AiChat/stateConfiguration.d.ts +7 -0
- package/dist/src/components/index.d.ts +3 -0
- package/dist/src/components/v2/DetachedRunTaskBox.d.ts +30 -5
- package/dist/src/components/v2/DetachedRunTaskBox.live.test.d.ts +1 -0
- package/dist/src/components/v2/DetachedRunTranscript.d.ts +89 -0
- package/dist/src/components/v2/DetachedRunTranscript.test.d.ts +1 -0
- package/dist/src/components/v2/index.d.ts +5 -0
- package/dist/src/components/v2/useDetachedRunLiveActivity.d.ts +30 -0
- package/dist/src/components/v2/useDetachedRunResumeKey.d.ts +15 -0
- package/dist/src/components/v2/useDetachedRunResumeKey.test.d.ts +1 -0
- package/dist/src/models/Interaction.d.ts +26 -0
- package/dist/src/models/ServerEvent.d.ts +3 -0
- package/dist/src/models/billingLimit.d.ts +23 -0
- package/dist/src/models/detachedRunModel.d.ts +11 -0
- package/dist/src/models/enums.d.ts +3 -1
- package/dist/src/models/index.d.ts +5 -3
- package/dist/src/models/semanticKernelModel/models.d.ts +40 -1
- package/dist/src/services/WebSocketConnection/WebSocketConnectionV2.d.ts +4 -1
- package/dist/src/services/WebSocketConnection/webSocketConnection.d.ts +18 -2
- package/dist/src/services/detachedRun/DetachedRunFramesTransport.d.ts +68 -0
- package/dist/src/services/detachedRun/DetachedRunFramesTransport.test.d.ts +1 -0
- package/dist/src/services/detachedRun/detachedRunActivity.d.ts +25 -0
- package/dist/src/services/detachedRun/streamDetachedRunFrames.d.ts +110 -0
- package/dist/src/services/detachedRun/streamDetachedRunFrames.test.d.ts +1 -0
- package/dist/src/services/index.d.ts +7 -0
- package/dist/src/services/servers/AgentGatewayService.d.ts +1 -1
- package/dist/src/services/servers/AgentGatewayWSAsync.d.ts +67 -1
- package/dist/src/services/servers/gatewayAnnounceDetachedRun.test.d.ts +1 -0
- package/dist/src/services/servers/gatewayInteractionStarted.test.d.ts +1 -0
- package/dist/src/services/wire/decodeFrame.d.ts +6 -1
- package/dist/tests/fakes/fakeWebSocket.d.ts +8 -2
- package/dist/tests/fakes/makeGateway.d.ts +3 -1
- package/dist/{useChatSessions-Q41Jmr6P.js → useChatSessions-DwqgYo79.js} +2 -2
- package/dist/{useChatSessions-Q41Jmr6P.js.map → useChatSessions-DwqgYo79.js.map} +1 -1
- package/package.json +1 -1
- package/dist/detachedRunModel-1ii9qGTW.js +0 -50
- package/dist/httpArtifactService-DYQDPVVH.js.map +0 -1
- package/dist/httpSessionService-BY7JeW8d.js +0 -309
- package/dist/httpSessionService-BY7JeW8d.js.map +0 -1
- package/dist/serializer-BNsp8xs2.js.map +0 -1
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var o = (i, t, e) =>
|
|
5
|
-
import {
|
|
6
|
-
import { format as
|
|
7
|
-
import
|
|
8
|
-
import { C as
|
|
9
|
-
import { d as
|
|
10
|
-
import { r as
|
|
11
|
-
const
|
|
1
|
+
var mt = Object.defineProperty;
|
|
2
|
+
var Rt = (i, t, e) => t in i ? mt(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e;
|
|
3
|
+
var h = (i, t) => mt(i, "name", { value: t, configurable: !0 });
|
|
4
|
+
var o = (i, t, e) => Rt(i, typeof t != "symbol" ? t + "" : t, e);
|
|
5
|
+
import { i as N, E as I, S as x, z as wt, C as U, j as bt, L as Lt, P as z, n as Pt, q as Ft, T as _t, o as Nt, s as et, B as Ut } from "./serializer-DvR3nOcf.js";
|
|
6
|
+
import { format as xt } from "date-fns";
|
|
7
|
+
import qt from "lodash";
|
|
8
|
+
import { C as E, P as Wt } from "./Prompt-DZb7OaPp.js";
|
|
9
|
+
import { d as Bt } from "./environmentUtils-VZNTCzEE.js";
|
|
10
|
+
import { r as Gt } from "./httpSessionService-DLdoBZRW.js";
|
|
11
|
+
const Q = {
|
|
12
12
|
PROMPTS: "envision-prompts"
|
|
13
|
-
},
|
|
13
|
+
}, zt = 50, nt = class nt {
|
|
14
14
|
getPrompts() {
|
|
15
15
|
try {
|
|
16
|
-
const t = localStorage.getItem(
|
|
16
|
+
const t = localStorage.getItem(Q.PROMPTS);
|
|
17
17
|
return t ? JSON.parse(t).sort((s, n) => new Date(n.created).getTime() - new Date(s.created).getTime()) : [];
|
|
18
18
|
} catch (t) {
|
|
19
19
|
return console.error("Error loading prompts:", t), [];
|
|
@@ -40,29 +40,29 @@ const b = {
|
|
|
40
40
|
storePrompt(t) {
|
|
41
41
|
try {
|
|
42
42
|
const e = this.getPrompts().filter((s) => s.promptText !== t.promptText);
|
|
43
|
-
e.unshift(t), localStorage.setItem(
|
|
43
|
+
e.unshift(t), localStorage.setItem(Q.PROMPTS, JSON.stringify(e.slice(0, zt)));
|
|
44
44
|
} catch (e) {
|
|
45
45
|
console.error("Error storing prompt:", e);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
clearAllPrompts() {
|
|
49
49
|
try {
|
|
50
|
-
localStorage.removeItem(
|
|
50
|
+
localStorage.removeItem(Q.PROMPTS);
|
|
51
51
|
} catch (t) {
|
|
52
52
|
console.error("Error clearing prompts:", t);
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
|
-
|
|
57
|
-
let
|
|
58
|
-
const
|
|
56
|
+
h(nt, "LocalStorageService");
|
|
57
|
+
let St = nt;
|
|
58
|
+
const $e = /* @__PURE__ */ h((i) => {
|
|
59
59
|
const t = i.getPrompts(), e = t.length;
|
|
60
60
|
if (e > 0) {
|
|
61
61
|
const s = t[0].promptText, n = t[t.length - 1].promptText, r = t[0].created;
|
|
62
62
|
return { promptState: e + "_" + s + "_" + n + "_" + r };
|
|
63
63
|
} else
|
|
64
64
|
return { promptState: "0" };
|
|
65
|
-
}, "usePromptState"),
|
|
65
|
+
}, "usePromptState"), it = class it {
|
|
66
66
|
constructor() {
|
|
67
67
|
o(this, "artifactGenerated", "artifactGenerated");
|
|
68
68
|
o(this, "artifactSelected", "artifactSelected");
|
|
@@ -80,9 +80,9 @@ const te = /* @__PURE__ */ d((i) => {
|
|
|
80
80
|
o(this, "gatewayFirstToken", "gatewayFirstToken");
|
|
81
81
|
}
|
|
82
82
|
};
|
|
83
|
-
|
|
84
|
-
let
|
|
85
|
-
const
|
|
83
|
+
h(it, "EventTypes");
|
|
84
|
+
let X = it;
|
|
85
|
+
const d = new X(), rt = class rt {
|
|
86
86
|
constructor() {
|
|
87
87
|
o(this, "listeners", {});
|
|
88
88
|
}
|
|
@@ -103,95 +103,95 @@ const l = new U(), B = class B {
|
|
|
103
103
|
}
|
|
104
104
|
/* typed artifact listeners */
|
|
105
105
|
artifactOnGenerated(t) {
|
|
106
|
-
this.addListener(
|
|
106
|
+
this.addListener(d.artifactGenerated, t);
|
|
107
107
|
}
|
|
108
108
|
artifactEmitGenerated(t) {
|
|
109
|
-
this.callListeners(
|
|
109
|
+
this.callListeners(d.artifactGenerated, t);
|
|
110
110
|
}
|
|
111
111
|
artifactOffGenerated(t) {
|
|
112
|
-
this.removeListener(
|
|
112
|
+
this.removeListener(d.artifactGenerated, t);
|
|
113
113
|
}
|
|
114
114
|
artifactOnSelected(t) {
|
|
115
|
-
this.addListener(
|
|
115
|
+
this.addListener(d.artifactSelected, t);
|
|
116
116
|
}
|
|
117
117
|
artifactEmitSelected(t) {
|
|
118
|
-
this.callListeners(
|
|
118
|
+
this.callListeners(d.artifactSelected, t);
|
|
119
119
|
}
|
|
120
120
|
artifactOffSelected(t) {
|
|
121
|
-
this.removeListener(
|
|
121
|
+
this.removeListener(d.artifactSelected, t);
|
|
122
122
|
}
|
|
123
123
|
gatewayOnMessage(t) {
|
|
124
|
-
this.addListener(
|
|
124
|
+
this.addListener(d.gatewayMessage, t);
|
|
125
125
|
}
|
|
126
126
|
gatewayEmitMessage(t) {
|
|
127
|
-
this.callListeners(
|
|
127
|
+
this.callListeners(d.gatewayMessage, t);
|
|
128
128
|
}
|
|
129
129
|
gatewayOffMessage(t) {
|
|
130
|
-
this.removeListener(
|
|
130
|
+
this.removeListener(d.gatewayMessage, t);
|
|
131
131
|
}
|
|
132
132
|
gatewayOnTurnComplete(t) {
|
|
133
|
-
this.addListener(
|
|
133
|
+
this.addListener(d.gatewayTurnComplete, t);
|
|
134
134
|
}
|
|
135
135
|
gatewayEmitTurnComplete(t, e) {
|
|
136
|
-
this.callListeners(
|
|
136
|
+
this.callListeners(d.gatewayTurnComplete, t, e);
|
|
137
137
|
}
|
|
138
138
|
gatewayOffTurnComplete(t) {
|
|
139
|
-
this.removeListener(
|
|
139
|
+
this.removeListener(d.gatewayTurnComplete, t);
|
|
140
140
|
}
|
|
141
141
|
gatewayOnTurnOpen(t) {
|
|
142
|
-
this.addListener(
|
|
142
|
+
this.addListener(d.gatewayTurnOpen, t);
|
|
143
143
|
}
|
|
144
144
|
gatewayEmitTurnOpen(t) {
|
|
145
|
-
this.callListeners(
|
|
145
|
+
this.callListeners(d.gatewayTurnOpen, t);
|
|
146
146
|
}
|
|
147
147
|
gatewayOffTurnOpen(t) {
|
|
148
|
-
this.removeListener(
|
|
148
|
+
this.removeListener(d.gatewayTurnOpen, t);
|
|
149
149
|
}
|
|
150
150
|
gatewayOnStreamError(t) {
|
|
151
|
-
this.addListener(
|
|
151
|
+
this.addListener(d.gatewayStreamError, t);
|
|
152
152
|
}
|
|
153
153
|
gatewayEmitStreamError(t) {
|
|
154
|
-
this.callListeners(
|
|
154
|
+
this.callListeners(d.gatewayStreamError, t);
|
|
155
155
|
}
|
|
156
156
|
gatewayOffStreamError(t) {
|
|
157
|
-
this.removeListener(
|
|
157
|
+
this.removeListener(d.gatewayStreamError, t);
|
|
158
158
|
}
|
|
159
159
|
/** The live socket is up and the session proven — on the first open and on every reconnect (`info.reconnect`). */
|
|
160
160
|
gatewayOnConnected(t) {
|
|
161
|
-
this.addListener(
|
|
161
|
+
this.addListener(d.gatewayConnected, t);
|
|
162
162
|
}
|
|
163
163
|
gatewayEmitConnected(t, e) {
|
|
164
|
-
this.callListeners(
|
|
164
|
+
this.callListeners(d.gatewayConnected, t, e);
|
|
165
165
|
}
|
|
166
166
|
gatewayOffConnected(t) {
|
|
167
|
-
this.removeListener(
|
|
167
|
+
this.removeListener(d.gatewayConnected, t);
|
|
168
168
|
}
|
|
169
169
|
chatOnMessageSent(t) {
|
|
170
|
-
this.addListener(
|
|
170
|
+
this.addListener(d.onMessageSent, t);
|
|
171
171
|
}
|
|
172
172
|
chatOffMessageSent(t) {
|
|
173
|
-
this.removeListener(
|
|
173
|
+
this.removeListener(d.onMessageSent, t);
|
|
174
174
|
}
|
|
175
175
|
ChatEmitMessageSent(t, e) {
|
|
176
|
-
this.callListeners(
|
|
176
|
+
this.callListeners(d.onMessageSent, t, e);
|
|
177
177
|
}
|
|
178
178
|
chatOnMessageError(t) {
|
|
179
|
-
this.addListener(
|
|
179
|
+
this.addListener(d.onMessageError, t);
|
|
180
180
|
}
|
|
181
181
|
chatOffMessageError(t) {
|
|
182
|
-
this.removeListener(
|
|
182
|
+
this.removeListener(d.onMessageError, t);
|
|
183
183
|
}
|
|
184
184
|
ChatEmitMessageError(t) {
|
|
185
|
-
this.callListeners(
|
|
185
|
+
this.callListeners(d.onMessageError, t);
|
|
186
186
|
}
|
|
187
187
|
gatewayOnFunctionResponse(t) {
|
|
188
|
-
this.addListener(
|
|
188
|
+
this.addListener(d.gatewayFunctionResponse, t);
|
|
189
189
|
}
|
|
190
190
|
gatewayEmitFunctionResponse(t) {
|
|
191
|
-
this.callListeners(
|
|
191
|
+
this.callListeners(d.gatewayFunctionResponse, t);
|
|
192
192
|
}
|
|
193
193
|
gatewayOffFunctionResponse(t) {
|
|
194
|
-
this.removeListener(
|
|
194
|
+
this.removeListener(d.gatewayFunctionResponse, t);
|
|
195
195
|
}
|
|
196
196
|
// "Re-ask this" — a `#ask-…` link in the transcript, carrying the link's TEXT as the question.
|
|
197
197
|
// An event rather than component state because the prompt that must send it is not always
|
|
@@ -200,60 +200,60 @@ const l = new U(), B = class B {
|
|
|
200
200
|
// listens; ChatPane publishes only when its internal one is suppressed, so the question is
|
|
201
201
|
// never sent twice.
|
|
202
202
|
uiOnAskQuestion(t) {
|
|
203
|
-
this.addListener(
|
|
203
|
+
this.addListener(d.uiAskQuestion, t);
|
|
204
204
|
}
|
|
205
205
|
uiEmitAskQuestion(t) {
|
|
206
|
-
this.callListeners(
|
|
206
|
+
this.callListeners(d.uiAskQuestion, t);
|
|
207
207
|
}
|
|
208
208
|
uiOffAskQuestion(t) {
|
|
209
|
-
this.removeListener(
|
|
209
|
+
this.removeListener(d.uiAskQuestion, t);
|
|
210
210
|
}
|
|
211
211
|
gatewayOnEndMessageData(t) {
|
|
212
|
-
this.addListener(
|
|
212
|
+
this.addListener(d.gatewayEndMessageData, t);
|
|
213
213
|
}
|
|
214
214
|
gatewayEmitEndMessageData(t) {
|
|
215
|
-
this.callListeners(
|
|
215
|
+
this.callListeners(d.gatewayEndMessageData, t);
|
|
216
216
|
}
|
|
217
217
|
gatewayOffEndMessageData(t) {
|
|
218
|
-
this.removeListener(
|
|
218
|
+
this.removeListener(d.gatewayEndMessageData, t);
|
|
219
219
|
}
|
|
220
220
|
gatewayOnToolEvent(t) {
|
|
221
|
-
this.addListener(
|
|
221
|
+
this.addListener(d.onToolEvent, t);
|
|
222
222
|
}
|
|
223
223
|
gatewayEmitToolEvent(t, e, s) {
|
|
224
|
-
this.callListeners(
|
|
224
|
+
this.callListeners(d.onToolEvent, t, e, s);
|
|
225
225
|
}
|
|
226
226
|
gatewayOffToolEvent(t) {
|
|
227
|
-
this.removeListener(
|
|
227
|
+
this.removeListener(d.onToolEvent, t);
|
|
228
228
|
}
|
|
229
229
|
gatewayOnFirstToken(t) {
|
|
230
|
-
this.addListener(
|
|
230
|
+
this.addListener(d.gatewayFirstToken, t);
|
|
231
231
|
}
|
|
232
232
|
gatewayEmitFirstToken(t) {
|
|
233
|
-
this.callListeners(
|
|
233
|
+
this.callListeners(d.gatewayFirstToken, t);
|
|
234
234
|
}
|
|
235
235
|
gatewayOffFirstToken(t) {
|
|
236
|
-
this.removeListener(
|
|
236
|
+
this.removeListener(d.gatewayFirstToken, t);
|
|
237
237
|
}
|
|
238
238
|
};
|
|
239
|
-
|
|
240
|
-
let
|
|
241
|
-
const
|
|
242
|
-
function
|
|
239
|
+
h(rt, "EventBusService");
|
|
240
|
+
let It = rt;
|
|
241
|
+
const Ht = 2e3;
|
|
242
|
+
function q(i) {
|
|
243
243
|
i.parts === i.prev.parts && (i.parts = i.prev.parts.slice());
|
|
244
244
|
}
|
|
245
|
-
|
|
246
|
-
function
|
|
245
|
+
h(q, "ownParts");
|
|
246
|
+
function J(i) {
|
|
247
247
|
i.functionMap === i.prev.functionMap && (i.functionMap = new Map(i.functionMap));
|
|
248
248
|
}
|
|
249
|
-
|
|
250
|
-
const
|
|
251
|
-
function
|
|
249
|
+
h(J, "ownFunctionMap");
|
|
250
|
+
const Jt = /* @__PURE__ */ h((i) => i == I.FunctionCall || i == I.PartialFunctionCall, "isFunctionCall"), jt = /* @__PURE__ */ h((i) => i == I.FunctionResponse, "isFunctionResponse"), At = /* @__PURE__ */ h((i) => i.parts.length + 1 + "-" + i.prev.interactionId, "nextPartId");
|
|
251
|
+
function Kt(i, t) {
|
|
252
252
|
var e, s, n, r, a;
|
|
253
|
-
return !!(!t || i.mustPush || i.mustPush === !1 && (i.eventType !== t.eventType || ((e = i.functionDetails) == null ? void 0 : e.id) !== ((s = t.functionDetails) == null ? void 0 : s.id)) || i.textAppendable && (((r = (n = t.thoughtDetails) == null ? void 0 : n.text) == null ? void 0 : r.length) ?? ((a = t.text) == null ? void 0 : a.length) ?? 0) >
|
|
253
|
+
return !!(!t || i.mustPush || i.mustPush === !1 && (i.eventType !== t.eventType || ((e = i.functionDetails) == null ? void 0 : e.id) !== ((s = t.functionDetails) == null ? void 0 : s.id)) || i.textAppendable && (((r = (n = t.thoughtDetails) == null ? void 0 : n.text) == null ? void 0 : r.length) ?? ((a = t.text) == null ? void 0 : a.length) ?? 0) > Ht);
|
|
254
254
|
}
|
|
255
|
-
|
|
256
|
-
function
|
|
255
|
+
h(Kt, "opensNewPart");
|
|
256
|
+
function Qt(i, t) {
|
|
257
257
|
const e = t.functionDetails.id, s = t.functionDetails.clientOutput, n = i.parts.findIndex((r) => {
|
|
258
258
|
var a;
|
|
259
259
|
return ((a = r.functionDetails) == null ? void 0 : a.id) === e;
|
|
@@ -268,66 +268,66 @@ function Et(i, t) {
|
|
|
268
268
|
parked: t.functionDetails.parked === !0 || r.functionDetails.parked === !0
|
|
269
269
|
}
|
|
270
270
|
};
|
|
271
|
-
|
|
271
|
+
q(i), i.parts[n] = a, J(i), i.functionMap.set(e, a);
|
|
272
272
|
} else {
|
|
273
273
|
const r = {
|
|
274
274
|
...t,
|
|
275
|
-
id:
|
|
275
|
+
id: At(i),
|
|
276
276
|
functionDetails: { ...t.functionDetails, silent: !0 }
|
|
277
277
|
};
|
|
278
|
-
|
|
278
|
+
q(i), i.parts.push(r), J(i), i.functionMap.set(e, r);
|
|
279
279
|
}
|
|
280
280
|
}
|
|
281
|
-
|
|
282
|
-
function
|
|
283
|
-
var
|
|
284
|
-
const r = i.parts.findIndex((
|
|
285
|
-
var
|
|
286
|
-
return ((
|
|
287
|
-
}), a = r > -1 ? i.parts[r] : e, c = t.functionDetails,
|
|
281
|
+
h(Qt, "foldClientOutput");
|
|
282
|
+
function Yt(i, t, e, s, n) {
|
|
283
|
+
var p;
|
|
284
|
+
const r = i.parts.findIndex((m) => {
|
|
285
|
+
var u;
|
|
286
|
+
return ((u = m.functionDetails) == null ? void 0 : u.id) === s;
|
|
287
|
+
}), a = r > -1 ? i.parts[r] : e, c = t.functionDetails, l = a.functionDetails, f = {
|
|
288
288
|
...a,
|
|
289
289
|
eventType: t.eventType,
|
|
290
290
|
functionDetails: {
|
|
291
|
-
id:
|
|
292
|
-
name: c.name ??
|
|
291
|
+
id: l.id,
|
|
292
|
+
name: c.name ?? l.name,
|
|
293
293
|
// The settled FunctionCall carries the parsed args; the PartialFunctionCall stream it
|
|
294
294
|
// folds onto only accumulated raw JSON in `.text`.
|
|
295
|
-
args: c.args ??
|
|
295
|
+
args: c.args ?? l.args,
|
|
296
296
|
response: c.response,
|
|
297
297
|
status: c.status ?? "Composing",
|
|
298
|
-
start: c.start ??
|
|
299
|
-
end: c.end ??
|
|
300
|
-
isUi: c.isUi === !0 ||
|
|
298
|
+
start: c.start ?? l.start,
|
|
299
|
+
end: c.end ?? l.end,
|
|
300
|
+
isUi: c.isUi === !0 || l.isUi === !0,
|
|
301
301
|
// Keep the call's friendly args summary when the result event (which has none) folds in.
|
|
302
|
-
argsDescription: c.argsDescription ??
|
|
302
|
+
argsDescription: c.argsDescription ?? l.argsDescription,
|
|
303
303
|
// The tool's client-only payload rides the RESULT event; sticky so a later rep without
|
|
304
304
|
// one can't clobber it. This is what a uiTool renderer reads.
|
|
305
|
-
clientOutput: c.clientOutput ??
|
|
305
|
+
clientOutput: c.clientOutput ?? l.clientOutput,
|
|
306
306
|
// Ask-user component ([KernelUIComponent]) — sticky like isUi/clientOutput. The streaming
|
|
307
307
|
// final CALL carries it, the PartialFunctionCall it folds onto may not (issue #60).
|
|
308
|
-
askUserComponent: c.askUserComponent ??
|
|
309
|
-
isAskUser: c.isAskUser ||
|
|
308
|
+
askUserComponent: c.askUserComponent ?? l.askUserComponent,
|
|
309
|
+
isAskUser: c.isAskUser || l.isAskUser || !!(c.askUserComponent ?? l.askUserComponent),
|
|
310
310
|
// Runtime "don't render" override rides the RESULT; sticky so it survives the fold.
|
|
311
|
-
cancelUi: c.cancelUi === !0 ||
|
|
312
|
-
parked: c.parked === !0 ||
|
|
311
|
+
cancelUi: c.cancelUi === !0 || l.cancelUi === !0,
|
|
312
|
+
parked: c.parked === !0 || l.parked === !0
|
|
313
313
|
}
|
|
314
314
|
};
|
|
315
|
-
|
|
315
|
+
J(i), i.functionMap.set(s, f), f.eventType === I.FunctionResponse && ((p = n == null ? void 0 : n.onFunctionResponse) == null || p.call(n, f.functionDetails)), r > -1 && (q(i), i.parts[r] = f);
|
|
316
316
|
}
|
|
317
|
-
|
|
318
|
-
function
|
|
319
|
-
s && t.functionDetails && (
|
|
317
|
+
h(Yt, "foldFunctionEvent");
|
|
318
|
+
function Xt(i, t, e, s) {
|
|
319
|
+
s && t.functionDetails && (J(i), i.functionMap.set(s, t));
|
|
320
320
|
const n = i.prev.parts[i.prev.parts.length - 1] ?? null;
|
|
321
|
-
n && n.textAppendable && (
|
|
321
|
+
n && n.textAppendable && (q(i), i.parts[i.parts.length - 1] = { ...n, ended: /* @__PURE__ */ new Date() });
|
|
322
322
|
const r = {
|
|
323
323
|
...t,
|
|
324
|
-
id:
|
|
324
|
+
id: At(i),
|
|
325
325
|
partial: e.partial
|
|
326
326
|
};
|
|
327
327
|
i.parts = [...i.parts, r];
|
|
328
328
|
}
|
|
329
|
-
|
|
330
|
-
function
|
|
329
|
+
h(Xt, "openPart");
|
|
330
|
+
function Vt(i, t, e, s) {
|
|
331
331
|
const n = i.parts[i.parts.length - 1] ?? null;
|
|
332
332
|
if (!n) return;
|
|
333
333
|
const r = {
|
|
@@ -342,10 +342,10 @@ function Ot(i, t, e, s) {
|
|
|
342
342
|
}
|
|
343
343
|
} else
|
|
344
344
|
r.text = t.text ?? "", r.textDetails.version++, t.thoughtDetails && (r.thoughtDetails = { ...t.thoughtDetails, text: t.thoughtDetails.text.trim() });
|
|
345
|
-
|
|
345
|
+
q(i), i.parts[i.parts.length - 1] = r;
|
|
346
346
|
}
|
|
347
|
-
|
|
348
|
-
function
|
|
347
|
+
h(Vt, "appendText");
|
|
348
|
+
function Zt(i, t) {
|
|
349
349
|
if (!t || t.length === 0) return i;
|
|
350
350
|
const e = i.slice();
|
|
351
351
|
for (const s of t) {
|
|
@@ -354,10 +354,10 @@ function kt(i, t) {
|
|
|
354
354
|
}
|
|
355
355
|
return e;
|
|
356
356
|
}
|
|
357
|
-
|
|
358
|
-
function
|
|
359
|
-
var c,
|
|
360
|
-
const s =
|
|
357
|
+
h(Zt, "mergeArtifacts");
|
|
358
|
+
function te(i, t, e) {
|
|
359
|
+
var c, l;
|
|
360
|
+
const s = N.FlatParts(t == null ? void 0 : t.parts), n = i.parts[i.parts.length - 1] ?? null, r = {
|
|
361
361
|
prev: i,
|
|
362
362
|
parts: i.parts,
|
|
363
363
|
functionMap: i.functionMap ?? /* @__PURE__ */ new Map()
|
|
@@ -365,28 +365,29 @@ function Dt(i, t, e) {
|
|
|
365
365
|
let a = !1;
|
|
366
366
|
if (s) {
|
|
367
367
|
if (s.eventType === I.FunctionClientOutput && !!((c = s.functionDetails) != null && c.id))
|
|
368
|
-
|
|
369
|
-
else if (
|
|
370
|
-
const
|
|
371
|
-
|
|
372
|
-
const
|
|
373
|
-
|
|
368
|
+
Qt(r, s);
|
|
369
|
+
else if (Kt(s, n)) {
|
|
370
|
+
const p = Jt(s.eventType) || jt(s.eventType), m = p ? (l = s.functionDetails) == null ? void 0 : l.id : void 0;
|
|
371
|
+
p && !m && console.error("Got isCall or isResponse without a callId", s);
|
|
372
|
+
const u = m ? r.functionMap.get(m) : void 0;
|
|
373
|
+
u && m && s.functionDetails ? Yt(r, s, u, m, e) : (Xt(r, s, t, m), a = !0);
|
|
374
374
|
}
|
|
375
|
-
s.textAppendable &&
|
|
375
|
+
s.textAppendable && Vt(r, s, t, a);
|
|
376
376
|
}
|
|
377
377
|
return {
|
|
378
378
|
...i,
|
|
379
379
|
version: i.version + 1,
|
|
380
380
|
parts: r.parts,
|
|
381
|
-
artifactData:
|
|
381
|
+
artifactData: Zt(i.artifactData ?? [], t.llmArtifacts),
|
|
382
382
|
functionMap: r.functionMap,
|
|
383
383
|
turn_complete: t.turn_complete,
|
|
384
384
|
partial: t.partial,
|
|
385
|
-
asyncFinalResponse: t.asyncFinalResponse
|
|
385
|
+
asyncFinalResponse: t.asyncFinalResponse,
|
|
386
|
+
agent: t.agent ?? i.agent
|
|
386
387
|
};
|
|
387
388
|
}
|
|
388
|
-
|
|
389
|
-
const
|
|
389
|
+
h(te, "reduceInteraction");
|
|
390
|
+
const ot = class ot {
|
|
390
391
|
constructor(t, e) {
|
|
391
392
|
o(this, "interaction");
|
|
392
393
|
o(this, "eventBusService");
|
|
@@ -398,15 +399,15 @@ const H = class H {
|
|
|
398
399
|
* throttled subscribers cannot diverge from live state.
|
|
399
400
|
*/
|
|
400
401
|
merge(t) {
|
|
401
|
-
const e =
|
|
402
|
-
onFunctionResponse: /* @__PURE__ */
|
|
402
|
+
const e = te(this.interaction, t, {
|
|
403
|
+
onFunctionResponse: /* @__PURE__ */ h((s) => this.eventBusService.gatewayEmitFunctionResponse(s), "onFunctionResponse")
|
|
403
404
|
});
|
|
404
405
|
return this.interaction = e, e;
|
|
405
406
|
}
|
|
406
407
|
};
|
|
407
|
-
|
|
408
|
-
let
|
|
409
|
-
const
|
|
408
|
+
h(ot, "InteractionService");
|
|
409
|
+
let j = ot;
|
|
410
|
+
const Ot = 4001, ee = 4002, kt = 4004, se = 4100, H = 500, ne = 2e4, ie = 1e3, re = 0.2, _ = 300 * 1e3, oe = 0.75, ae = 2e3, w = "[Connection]", at = class at {
|
|
410
411
|
constructor(t) {
|
|
411
412
|
o(this, "ports");
|
|
412
413
|
o(this, "state");
|
|
@@ -415,7 +416,7 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
415
416
|
o(this, "inflight", null);
|
|
416
417
|
o(this, "retryTimer", null);
|
|
417
418
|
o(this, "idleTimer", null);
|
|
418
|
-
o(this, "wait",
|
|
419
|
+
o(this, "wait", H);
|
|
419
420
|
/** Consecutive failed attempts since the last proven connection. */
|
|
420
421
|
o(this, "attempt", 0);
|
|
421
422
|
o(this, "halted", null);
|
|
@@ -448,7 +449,7 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
448
449
|
/** Connected by the store AND by the transport. */
|
|
449
450
|
get isUp() {
|
|
450
451
|
var t, e;
|
|
451
|
-
return this.state.connectionStatus ===
|
|
452
|
+
return this.state.connectionStatus === E.Connected && (((e = (t = this.ports).isConnected) == null ? void 0 : e.call(t)) ?? !0);
|
|
452
453
|
}
|
|
453
454
|
// ─── Commands ───────────────────────────────────────────────────────────
|
|
454
455
|
/**
|
|
@@ -463,7 +464,7 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
463
464
|
if (this.inflight) return this.inflight;
|
|
464
465
|
if (this.isUp && this.target.sessionId && !this.halted)
|
|
465
466
|
return Promise.resolve(this.target.sessionId);
|
|
466
|
-
} else this.target && (console.log(`${
|
|
467
|
+
} else this.target && (console.log(`${w} switching from session ${this.target.sessionId ?? "(new)"} to ${t ?? "(new)"}.`), this.close());
|
|
467
468
|
return this.halted = null, this.cancelRetry(), this.target = { sessionId: t, resume: (e == null ? void 0 : e.resume) === !0, prepare: e == null ? void 0 : e.prepare }, this.startIdleTick(), this.dial();
|
|
468
469
|
}
|
|
469
470
|
/**
|
|
@@ -475,18 +476,18 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
475
476
|
var e;
|
|
476
477
|
if (this.halted)
|
|
477
478
|
throw new Error(`The connection was halted (${this.halted}); session ${((e = this.target) == null ? void 0 : e.sessionId) ?? t ?? "none"} cannot be redialled.`);
|
|
478
|
-
return this.target ? this.inflight ? this.inflight : this.isUp && this.target.sessionId ? this.target.sessionId : (console.warn(`${
|
|
479
|
+
return this.target ? this.inflight ? this.inflight : this.isUp && this.target.sessionId ? this.target.sessionId : (console.warn(`${w} send guard: socket is DOWN at send time (session ${this.target.sessionId ?? "(new)"}, status ${this.state.connectionStatus}) — redialling now instead of in ${this.wait}ms.`), this.cancelRetry(), this.target.resume = !0, this.dial()) : (console.warn(`${w} send guard: nothing supervised yet — opening session ${t ?? "(new)"} with the replay cursor.`), this.open(t, { resume: !0 }));
|
|
479
480
|
}
|
|
480
481
|
/** Stop supervising: hang up, cancel any retry, stop the idle tick. Never throws. */
|
|
481
482
|
close() {
|
|
482
483
|
const t = this.target;
|
|
483
|
-
this.target = null, this.halted = null, this.cancelRetry(), this.stopIdleTick(), this.wait =
|
|
484
|
+
this.target = null, this.halted = null, this.cancelRetry(), this.stopIdleTick(), this.wait = H, this.attempt = 0;
|
|
484
485
|
try {
|
|
485
486
|
this.ports.hangUp();
|
|
486
487
|
} catch (e) {
|
|
487
|
-
console.error(`${
|
|
488
|
+
console.error(`${w} hangUp threw`, e);
|
|
488
489
|
}
|
|
489
|
-
t && this.state.setConnectionStatus(
|
|
490
|
+
t && this.state.setConnectionStatus(E.Disconnected);
|
|
490
491
|
}
|
|
491
492
|
// ─── Transport reports ──────────────────────────────────────────────────
|
|
492
493
|
/**
|
|
@@ -500,24 +501,24 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
500
501
|
* A close that lands while a dial is in flight is remembered and judged when that dial fails.
|
|
501
502
|
*/
|
|
502
503
|
transportClosed(t, e) {
|
|
503
|
-
if (t !==
|
|
504
|
+
if (t !== Ot && (this.lastCloseCode = t, !!this.target)) {
|
|
504
505
|
if (this.inflight) {
|
|
505
|
-
console.log(`${
|
|
506
|
+
console.log(`${w} socket closed (code=${t}) while a dial is in flight — judged when it settles.`);
|
|
506
507
|
return;
|
|
507
508
|
}
|
|
508
|
-
this.state.connectionStatus ===
|
|
509
|
+
this.state.connectionStatus === E.Connected && (console.warn(`${w} socket closed: code=${t} reason="${e}" (session ${this.target.sessionId ?? "(new)"}).`), this.state.setConnectionStatus(E.Disconnected), this.judge(t));
|
|
509
510
|
}
|
|
510
511
|
}
|
|
511
512
|
// ─── The dial ───────────────────────────────────────────────────────────
|
|
512
513
|
dial() {
|
|
513
514
|
const t = this.target;
|
|
514
|
-
this.lastCloseCode = null, this.state.setConnectionStatus(
|
|
515
|
+
this.lastCloseCode = null, this.state.setConnectionStatus(E.Connecting);
|
|
515
516
|
const e = this.now();
|
|
516
|
-
console.log(`${
|
|
517
|
+
console.log(`${w} dialling session ${t.sessionId ?? "(new)"}${t.resume ? " with the replay cursor" : ""}${this.everConnected ? " (reconnect)" : ""}.`);
|
|
517
518
|
const n = (async () => (t.prepare && await t.prepare(), this.ports.dial(t.sessionId, { resume: t.resume })))().then((r) => {
|
|
518
|
-
var c,
|
|
519
|
+
var c, l;
|
|
519
520
|
if (this.target !== t) {
|
|
520
|
-
console.log(`${
|
|
521
|
+
console.log(`${w} a dial for ${t.sessionId ?? "(new)"} resolved after it was abandoned — hanging up.`);
|
|
521
522
|
try {
|
|
522
523
|
this.ports.hangUp();
|
|
523
524
|
} catch {
|
|
@@ -525,19 +526,19 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
525
526
|
throw new Error("Connection attempt abandoned");
|
|
526
527
|
}
|
|
527
528
|
if (t.sessionId && r !== t.sessionId) {
|
|
528
|
-
console.error(`${
|
|
529
|
+
console.error(`${w} asked for session ${t.sessionId}, the server answered with ${r} — refusing the swap and halting.`);
|
|
529
530
|
try {
|
|
530
531
|
this.ports.hangUp();
|
|
531
532
|
} catch {
|
|
532
533
|
}
|
|
533
|
-
throw this.state.setConnectionStatus(
|
|
534
|
+
throw this.state.setConnectionStatus(E.Disconnected), this.halt("session-gone", { sessionId: t.sessionId, actualSessionId: r }), new Error(`Session ${t.sessionId} no longer exists on the server (it answered with ${r}).`);
|
|
534
535
|
}
|
|
535
536
|
const a = this.everConnected;
|
|
536
|
-
return this.everConnected = !0, t.sessionId = r, t.resume = !0, t.prepare = void 0, this.wait =
|
|
537
|
+
return this.everConnected = !0, t.sessionId = r, t.resume = !0, t.prepare = void 0, this.wait = H, this.attempt = 0, this.state.setConnectionStatus(E.Connected), console.log(`${w} ${a ? "reconnected to" : "connected to"} session ${r} in ${this.now() - e}ms.`), (l = (c = this.ports).onConnected) == null || l.call(c, r, { reconnect: a }), r;
|
|
537
538
|
}).catch((r) => {
|
|
538
539
|
if (this.target === t && !this.halted) {
|
|
539
540
|
const a = this.lastCloseCode;
|
|
540
|
-
console.error(`${
|
|
541
|
+
console.error(`${w} dial of session ${t.sessionId ?? "(new)"} failed after ${this.now() - e}ms${a !== null ? ` (socket closed with ${a})` : ""}:`, r), this.state.setConnectionStatus(E.Disconnected), this.inflight === n && (this.inflight = null), this.judge(a);
|
|
541
542
|
}
|
|
542
543
|
throw r;
|
|
543
544
|
}).finally(() => {
|
|
@@ -549,33 +550,33 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
549
550
|
judge(t) {
|
|
550
551
|
var s;
|
|
551
552
|
const e = (s = this.target) == null ? void 0 : s.sessionId;
|
|
552
|
-
if (t ===
|
|
553
|
-
if (t ===
|
|
553
|
+
if (t === ee) return this.halt("attached-elsewhere", { sessionId: e, code: t });
|
|
554
|
+
if (t === kt) return this.halt("session-gone", { sessionId: e, code: t });
|
|
554
555
|
if (t === 1e3 || t === 1008) return this.halt("closed-by-server", { sessionId: e, code: t });
|
|
555
556
|
this.scheduleRetry();
|
|
556
557
|
}
|
|
557
558
|
halt(t, e) {
|
|
558
559
|
var s, n;
|
|
559
|
-
this.halted = t, this.cancelRetry(), console.warn(`${
|
|
560
|
+
this.halted = t, this.cancelRetry(), console.warn(`${w} halted (${t}) on session ${e.sessionId ?? "(new)"}${e.code !== void 0 ? `, close code ${e.code}` : ""} — no automatic redial; an explicit open() may try again.`);
|
|
560
561
|
try {
|
|
561
562
|
(n = (s = this.ports).onHalt) == null || n.call(s, t, e);
|
|
562
563
|
} catch (r) {
|
|
563
|
-
console.error(`${
|
|
564
|
+
console.error(`${w} onHalt threw`, r);
|
|
564
565
|
}
|
|
565
566
|
}
|
|
566
567
|
scheduleRetry() {
|
|
567
568
|
var e, s;
|
|
568
569
|
if (!this.target || this.retryTimer || this.inflight) return;
|
|
569
570
|
if (!this.state.sessionActive) {
|
|
570
|
-
console.log(`${
|
|
571
|
+
console.log(`${w} session idle — not redialling until activity resumes.`);
|
|
571
572
|
return;
|
|
572
573
|
}
|
|
573
574
|
const t = this.jittered(this.wait);
|
|
574
|
-
this.wait = Math.min(Math.floor(this.wait + this.wait * this.wait /
|
|
575
|
+
this.wait = Math.min(Math.floor(this.wait + this.wait * this.wait / ie), ne), this.attempt += 1, console.warn(`${w} redialling session ${this.target.sessionId ?? "(new)"} in ${t}ms (then ${this.wait}ms, attempt ${this.attempt}).`);
|
|
575
576
|
try {
|
|
576
577
|
(s = (e = this.ports).onRetryScheduled) == null || s.call(e, { sessionId: this.target.sessionId, delayMs: t, attempt: this.attempt });
|
|
577
578
|
} catch (n) {
|
|
578
|
-
console.error(`${
|
|
579
|
+
console.error(`${w} onRetryScheduled threw`, n);
|
|
579
580
|
}
|
|
580
581
|
this.retryTimer = setTimeout(() => {
|
|
581
582
|
this.retryTimer = null, !(!this.target || this.halted) && (this.target.resume = !0, this.dial().catch(() => {
|
|
@@ -586,7 +587,7 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
586
587
|
this.retryTimer && (clearTimeout(this.retryTimer), this.retryTimer = null);
|
|
587
588
|
}
|
|
588
589
|
jittered(t) {
|
|
589
|
-
return Math.max(0, Math.round(t + t *
|
|
590
|
+
return Math.max(0, Math.round(t + t * re * (Math.random() * 2 - 1)));
|
|
590
591
|
}
|
|
591
592
|
sameTarget(t) {
|
|
592
593
|
var e;
|
|
@@ -594,7 +595,7 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
594
595
|
}
|
|
595
596
|
// ─── Idle ───────────────────────────────────────────────────────────────
|
|
596
597
|
startIdleTick() {
|
|
597
|
-
this.idleTimer || (this.idleTimer = setInterval(() => this.tick(),
|
|
598
|
+
this.idleTimer || (this.idleTimer = setInterval(() => this.tick(), ae));
|
|
598
599
|
}
|
|
599
600
|
stopIdleTick() {
|
|
600
601
|
this.idleTimer && (clearInterval(this.idleTimer), this.idleTimer = null);
|
|
@@ -611,25 +612,25 @@ const ot = 4001, $t = 4002, Rt = 4004, Lt = 4100, $ = 500, Pt = 2e4, bt = 1e3, _
|
|
|
611
612
|
if (!this.target) return;
|
|
612
613
|
this.state.turnOpen && this.state.setLastActivity();
|
|
613
614
|
const t = this.now() - this.state.lastActivity;
|
|
614
|
-
if (t >
|
|
615
|
-
if (this.state.sessionActive && this.state.setIsSessionActive(!1), this.cancelRetry(), this.state.connectionStatus ===
|
|
616
|
-
console.warn(`${
|
|
615
|
+
if (t > _) {
|
|
616
|
+
if (this.state.sessionActive && this.state.setIsSessionActive(!1), this.cancelRetry(), this.state.connectionStatus === E.Connected) {
|
|
617
|
+
console.warn(`${w} idle for ${Math.round(t / 1e3)}s (limit ${_ / 1e3}s) — closing the socket (session ${this.target.sessionId ?? "(new)"}). Activity, or a send, redials it.`);
|
|
617
618
|
try {
|
|
618
619
|
this.ports.hangUp();
|
|
619
620
|
} catch (e) {
|
|
620
|
-
console.error(`${
|
|
621
|
+
console.error(`${w} hangUp threw`, e);
|
|
621
622
|
}
|
|
622
|
-
this.state.setConnectionStatus(
|
|
623
|
+
this.state.setConnectionStatus(E.Disconnected);
|
|
623
624
|
}
|
|
624
625
|
return;
|
|
625
626
|
}
|
|
626
|
-
this.state.sessionActive || (console.log(`${
|
|
627
|
-
}))), t >
|
|
627
|
+
this.state.sessionActive || (console.log(`${w} activity resumed — the session is active again.`), this.state.setIsSessionActive(!0), this.wait = H, this.state.connectionStatus === E.Disconnected && !this.halted && !this.inflight && (this.target.resume = !0, this.dial().catch(() => {
|
|
628
|
+
}))), t > _ * oe ? this.idleWarningLogged || (this.idleWarningLogged = !0, console.warn(`${w} idle ${Math.round(t / 1e3)}s of ${_ / 1e3}s — the socket closes in about ${Math.round((_ - t) / 1e3)}s unless something happens.`)) : this.idleWarningLogged = !1;
|
|
628
629
|
}
|
|
629
630
|
};
|
|
630
|
-
|
|
631
|
-
let
|
|
632
|
-
const
|
|
631
|
+
h(at, "ConnectionSupervisor");
|
|
632
|
+
let V = at;
|
|
633
|
+
const ct = class ct {
|
|
633
634
|
constructor(t) {
|
|
634
635
|
o(this, "pending", null);
|
|
635
636
|
o(this, "lastPublishedAt", 0);
|
|
@@ -661,10 +662,10 @@ const K = class K {
|
|
|
661
662
|
this.trailing && (clearTimeout(this.trailing), this.trailing = null);
|
|
662
663
|
}
|
|
663
664
|
};
|
|
664
|
-
|
|
665
|
-
let
|
|
666
|
-
const
|
|
667
|
-
constructor(t, e, s, n, r, a, c,
|
|
665
|
+
h(ct, "ThrottledEmitter");
|
|
666
|
+
let Z = ct;
|
|
667
|
+
const y = class y {
|
|
668
|
+
constructor(t, e, s, n, r, a, c, l, f, p, m) {
|
|
668
669
|
o(this, "base_url");
|
|
669
670
|
o(this, "msg_order", 0);
|
|
670
671
|
o(this, "interactionsMap", /* @__PURE__ */ new Map());
|
|
@@ -678,6 +679,19 @@ const S = class S {
|
|
|
678
679
|
o(this, "interactionCallIds", /* @__PURE__ */ new Map());
|
|
679
680
|
/** Interactions whose turn was committed (turn-complete or a server error); a second close is ignored. */
|
|
680
681
|
o(this, "committedTurns", /* @__PURE__ */ new Set());
|
|
682
|
+
/** Interactions this tab sent. An `InteractionStarted` for one of them announces what is already here. */
|
|
683
|
+
o(this, "localInteractionIds", /* @__PURE__ */ new Set());
|
|
684
|
+
/**
|
|
685
|
+
* Turns open right now, local and remote. `turnOpen` in the store is "any of these": a remote turn finishing
|
|
686
|
+
* must not unlock the composer under a local turn still streaming, nor the other way round.
|
|
687
|
+
*/
|
|
688
|
+
o(this, "openTurns", /* @__PURE__ */ new Set());
|
|
689
|
+
o(this, "options");
|
|
690
|
+
/** Announcements asked for in this tab, by `runId|settleKey` — a second request for the same settle is a no-op. */
|
|
691
|
+
o(this, "announceRequested", /* @__PURE__ */ new Set());
|
|
692
|
+
/** Announcements waiting for the chat to be idle (no open turn) and connected, in request order. */
|
|
693
|
+
o(this, "pendingAnnouncements", []);
|
|
694
|
+
o(this, "flushingAnnouncements", !1);
|
|
681
695
|
o(this, "rawJwt");
|
|
682
696
|
o(this, "sessionId");
|
|
683
697
|
o(this, "eventBusService");
|
|
@@ -694,12 +708,12 @@ const S = class S {
|
|
|
694
708
|
*/
|
|
695
709
|
o(this, "everConnected", !1);
|
|
696
710
|
/** Publishes the live message to the store; see ThrottledEmitter for the cadence rules. */
|
|
697
|
-
o(this, "liveMessage", new
|
|
698
|
-
windowMs:
|
|
699
|
-
publish: /* @__PURE__ */
|
|
711
|
+
o(this, "liveMessage", new Z({
|
|
712
|
+
windowMs: y.CURRENT_MSG_THROTTLE_MS,
|
|
713
|
+
publish: /* @__PURE__ */ h((t) => {
|
|
700
714
|
this.sessionState.setCurrentMessage(t), this.eventBusService.gatewayEmitMessage(t);
|
|
701
715
|
}, "publish"),
|
|
702
|
-
publishedVersion: /* @__PURE__ */
|
|
716
|
+
publishedVersion: /* @__PURE__ */ h(() => {
|
|
703
717
|
var t;
|
|
704
718
|
return ((t = this.sessionState.currentMessage) == null ? void 0 : t.version) ?? 0;
|
|
705
719
|
}, "publishedVersion")
|
|
@@ -708,31 +722,33 @@ const S = class S {
|
|
|
708
722
|
o(this, "detachedRunInteractionServices", /* @__PURE__ */ new Map());
|
|
709
723
|
// ─── Artifacts ──────────────────────────────────────────────────────────
|
|
710
724
|
o(this, "runningRequests", /* @__PURE__ */ new Map());
|
|
711
|
-
this.user = t, this.config = r, this.rawJwt = t.access_token, this.base_url = n, this.eventBusService = e, this.sessionId =
|
|
712
|
-
this.sessionState = u;
|
|
713
|
-
}), this.wsService =
|
|
725
|
+
this.options = m ?? {}, this.user = t, this.config = r, this.rawJwt = t.access_token, this.base_url = n, this.eventBusService = e, this.sessionId = p, this.artifactService = s, this.sessionState = a.getState(), this.notificationPort = c, this.storageService = f, a.subscribe((u, g) => {
|
|
726
|
+
this.sessionState = u, g.turnOpen && !u.turnOpen && this.flushAnnouncements();
|
|
727
|
+
}), this.wsService = l, this.connection = new V({
|
|
714
728
|
store: a,
|
|
715
729
|
// A redial while a turn is open carries the turn's id: the server tells us if it cannot continue it.
|
|
716
|
-
dial: /* @__PURE__ */
|
|
730
|
+
dial: /* @__PURE__ */ h((u, g) => this.wsService.connect(u, {
|
|
717
731
|
...g,
|
|
718
732
|
openInteractionId: g.resume && this.sessionState.turnOpen ? this.getCurrentInteractionId() ?? void 0 : void 0
|
|
719
733
|
}), "dial"),
|
|
720
|
-
hangUp: /* @__PURE__ */
|
|
721
|
-
isConnected: /* @__PURE__ */
|
|
722
|
-
onConnected: /* @__PURE__ */
|
|
723
|
-
this.sessionId = u, this.sessionState.setConnectionOutage(null), this.eventBusService.gatewayEmitConnected(u, g);
|
|
734
|
+
hangUp: /* @__PURE__ */ h(() => this.wsService.closeStream(), "hangUp"),
|
|
735
|
+
isConnected: /* @__PURE__ */ h(() => this.wsService.isConnected(), "isConnected"),
|
|
736
|
+
onConnected: /* @__PURE__ */ h((u, g) => {
|
|
737
|
+
this.sessionId = u, this.sessionState.setConnectionOutage(null), this.eventBusService.gatewayEmitConnected(u, g), this.flushAnnouncements();
|
|
724
738
|
}, "onConnected"),
|
|
725
|
-
onRetryScheduled: /* @__PURE__ */
|
|
739
|
+
onRetryScheduled: /* @__PURE__ */ h(({ delayMs: u, attempt: g }) => {
|
|
726
740
|
var M;
|
|
727
|
-
const
|
|
728
|
-
this.sessionState.setConnectionOutage({ since:
|
|
741
|
+
const S = Date.now(), D = ((M = this.sessionState.connectionOutage) == null ? void 0 : M.since) ?? S;
|
|
742
|
+
this.sessionState.setConnectionOutage({ since: D, retryAt: S + u, attempt: g, halted: null }), S - D >= y.OUTAGE_CLOSES_TURN_AFTER_MS && this.closeOpenTurnWith(y.OUTAGE_TURN_MESSAGE);
|
|
729
743
|
}, "onRetryScheduled"),
|
|
730
|
-
onHalt: /* @__PURE__ */
|
|
731
|
-
const
|
|
732
|
-
this.sessionState.setConnectionOutage({ since: (
|
|
744
|
+
onHalt: /* @__PURE__ */ h((u, g) => {
|
|
745
|
+
const S = this.sessionState.connectionOutage;
|
|
746
|
+
this.sessionState.setConnectionOutage({ since: (S == null ? void 0 : S.since) ?? Date.now(), retryAt: null, attempt: (S == null ? void 0 : S.attempt) ?? 0, halted: u }), this.notifyHalt(u, g), this.closeOpenTurnWith(y.HALT_TURN_MESSAGE[u]);
|
|
733
747
|
}, "onHalt")
|
|
734
748
|
}), this.wsService.onEvent = async (u) => {
|
|
735
749
|
await this.Message(u);
|
|
750
|
+
}, this.wsService.onInteractionStarted = (u) => {
|
|
751
|
+
this.interactionStarted(u);
|
|
736
752
|
}, this.wsService.onNewPacket = (u) => {
|
|
737
753
|
this.sessionState.setStatistics(u);
|
|
738
754
|
}, this.wsService.onError = (u) => {
|
|
@@ -751,6 +767,8 @@ const S = class S {
|
|
|
751
767
|
this.eventBusService.gatewayEmitStreamError(u);
|
|
752
768
|
}, this.wsService.onOpen = () => {
|
|
753
769
|
this.everConnected = !0;
|
|
770
|
+
}, this.wsService.onBillingLimit = (u) => {
|
|
771
|
+
a.getState().setBillingLimit(u);
|
|
754
772
|
}, this.wsService.onFirstToken = (u) => {
|
|
755
773
|
this.localEmitFirstToken(u);
|
|
756
774
|
}, this.wsService.onClose = (u, g) => {
|
|
@@ -763,8 +781,9 @@ const S = class S {
|
|
|
763
781
|
*/
|
|
764
782
|
closeOpenTurnWith(t) {
|
|
765
783
|
if (!this.sessionState.turnOpen) return;
|
|
766
|
-
const e = this.getCurrentInteractionId();
|
|
767
|
-
|
|
784
|
+
const e = this.getCurrentInteractionId(), s = this.openTurns.size > 0 ? [...this.openTurns] : e ? [e] : [];
|
|
785
|
+
for (const n of s)
|
|
786
|
+
console.warn(`[AgentGateway] closing interaction ${n} from the client side: ${t}`), this.Message(x.createErrorEvent(t, n));
|
|
768
787
|
}
|
|
769
788
|
/** The supervisor gave up on its own (another tab holds the session, the session is gone, …): tell the user. */
|
|
770
789
|
notifyHalt(t, e) {
|
|
@@ -840,10 +859,13 @@ const S = class S {
|
|
|
840
859
|
return this.currentIS.interaction.isSuspended = !1, this.currentIS.interaction.interactionQuestion += " + " + t, this.currentIS.interaction;
|
|
841
860
|
this.msg_order += 1;
|
|
842
861
|
let n = this.getNewInteractionId();
|
|
843
|
-
s && (n = s);
|
|
844
|
-
|
|
862
|
+
return s && (n = s), this.currentIS = this.registerInteraction(n, t), this.currentIS.interaction;
|
|
863
|
+
}
|
|
864
|
+
/** A new interaction and its merge service, in the maps. Who becomes current is the caller's decision. */
|
|
865
|
+
registerInteraction(t, e, s) {
|
|
866
|
+
const n = {
|
|
845
867
|
created: /* @__PURE__ */ new Date(),
|
|
846
|
-
interactionId:
|
|
868
|
+
interactionId: t,
|
|
847
869
|
artifactData: [],
|
|
848
870
|
partial: !0,
|
|
849
871
|
sessionId: this.sessionId,
|
|
@@ -851,9 +873,44 @@ const S = class S {
|
|
|
851
873
|
parts: [],
|
|
852
874
|
version: 0,
|
|
853
875
|
functionMap: /* @__PURE__ */ new Map(),
|
|
854
|
-
interactionQuestion:
|
|
876
|
+
interactionQuestion: e,
|
|
877
|
+
...s
|
|
855
878
|
};
|
|
856
|
-
|
|
879
|
+
this.interactionsMap.set(t, n);
|
|
880
|
+
const r = new j(n, this.eventBusService);
|
|
881
|
+
return this.interactionsServiceMap.set(t, r), r;
|
|
882
|
+
}
|
|
883
|
+
/** The current interaction's turn is still open — it owns the live message, and must stay current. */
|
|
884
|
+
currentTurnIsOpen() {
|
|
885
|
+
const t = this.getCurrentInteractionId();
|
|
886
|
+
return !!t && this.openTurns.has(t);
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* A turn this tab did not send (AIA Issue 1548): a WhatsApp message, a platform wake turn, another tab's turn, a
|
|
890
|
+
* detached run's briefing. It becomes the current interaction only when no open turn holds that place — a local
|
|
891
|
+
* turn mid-stream keeps it, and with it the resume cursor, suspend and the client-side close; the remote turn
|
|
892
|
+
* streams into its own interaction and takes over the live message when that turn ends.
|
|
893
|
+
*/
|
|
894
|
+
openRemoteInteraction(t, e, s, n) {
|
|
895
|
+
const r = this.registerInteraction(t, e, { remote: !0, ...s && { origin: s } });
|
|
896
|
+
return this.currentTurnIsOpen() || (this.currentIS = r), n && (this.openTurns.add(t), this.sessionState.setTurnOpen(!0), this.eventBusService.gatewayEmitTurnOpen(t), this.currentIS === r && this.localEmitMessage(r.interaction)), r;
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* An `InteractionStarted` frame. Opens a turn this tab did not send, with the text that started it as its
|
|
900
|
+
* question; ignores one this tab sent (its interaction already exists) and one already known or committed.
|
|
901
|
+
* Outside {@link AgentGatewayOptions.transcriptOfDetachedRun}, a detached run's turn is ignored too: it runs on
|
|
902
|
+
* the run's own session, and its box — not this transcript — shows it.
|
|
903
|
+
*/
|
|
904
|
+
interactionStarted(t) {
|
|
905
|
+
const e = t.interactionId;
|
|
906
|
+
if (!e || t.detachedRunId && !this.options.transcriptOfDetachedRun || this.localInteractionIds.has(e) || this.committedTurns.has(e)) return;
|
|
907
|
+
const s = this.interactionsServiceMap.get(e);
|
|
908
|
+
if (s) {
|
|
909
|
+
const n = s.interaction;
|
|
910
|
+
n.interactionQuestion || (n.interactionQuestion = t.content ?? ""), n.origin ?? (n.origin = wt(t.origin));
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
this.openRemoteInteraction(e, t.content ?? "", wt(t.origin), !0);
|
|
857
914
|
}
|
|
858
915
|
getInteractions() {
|
|
859
916
|
return Array.from(this.interactionsMap.values());
|
|
@@ -867,14 +924,10 @@ const S = class S {
|
|
|
867
924
|
}
|
|
868
925
|
updateMap(t) {
|
|
869
926
|
let e = this.interactionsServiceMap.get(t.interactionId);
|
|
870
|
-
|
|
871
|
-
var s = this.startInteraction("Unknown", !1, t.interactionId);
|
|
872
|
-
e = this.interactionsServiceMap.get(s.interactionId);
|
|
873
|
-
}
|
|
874
|
-
return e.merge(t);
|
|
927
|
+
return e || (e = this.openRemoteInteraction(t.interactionId, "", void 0, !1)), e.merge(t);
|
|
875
928
|
}
|
|
876
929
|
getNewInteractionId() {
|
|
877
|
-
const e =
|
|
930
|
+
const e = xt(/* @__PURE__ */ new Date(), "MMM_dd_HH_mm_ss");
|
|
878
931
|
return `${this.msg_order}_${e}`;
|
|
879
932
|
}
|
|
880
933
|
// ─── Inbound: server events ─────────────────────────────────────────────
|
|
@@ -887,7 +940,7 @@ const S = class S {
|
|
|
887
940
|
console.error("Uknown message type", t);
|
|
888
941
|
return;
|
|
889
942
|
}
|
|
890
|
-
if (e.detachedRunId) {
|
|
943
|
+
if (e.detachedRunId && !this.options.transcriptOfDetachedRun) {
|
|
891
944
|
this.handleDetachedRunEvent(e);
|
|
892
945
|
return;
|
|
893
946
|
}
|
|
@@ -908,16 +961,16 @@ const S = class S {
|
|
|
908
961
|
console.error("No updated interaction");
|
|
909
962
|
return;
|
|
910
963
|
}
|
|
911
|
-
this.localEmitMessage(n), (e.turn_complete || e.asyncFinalResponse || s) && (this.committedTurns.has(e.interactionId) || (this.committedTurns.add(e.interactionId), this.turnComplete(e.interactionId, n, e.author, e.detachedRunId)));
|
|
964
|
+
(n.interactionId === this.getCurrentInteractionId() || !this.currentTurnIsOpen()) && this.localEmitMessage(n), (e.turn_complete || e.asyncFinalResponse || s) && (this.committedTurns.has(e.interactionId) || (this.committedTurns.add(e.interactionId), this.turnComplete(e.interactionId, n, e.author, e.detachedRunId)));
|
|
912
965
|
const r = e.artifactsDelta;
|
|
913
966
|
r && r.forEach((a) => {
|
|
914
967
|
const c = n.artifactData.filter((f) => f.id == a);
|
|
915
|
-
let
|
|
916
|
-
c && c.length > 0 && (
|
|
968
|
+
let l = null;
|
|
969
|
+
c && c.length > 0 && (l = c[0], l.loadingPromise = this.asyncLoadArtifact(a).then((f) => (f === null ? console.error("Error trying to load artifact id:", l == null ? void 0 : l.id, "Artifact is null") : this.eventBusService.artifactEmitGenerated(f), f)).catch((f) => (console.error("Error trying to load artifact id:", l == null ? void 0 : l.id, f.toString()), null)));
|
|
917
970
|
});
|
|
918
971
|
} catch (e) {
|
|
919
972
|
if (this.getCurrentInteractionId()) {
|
|
920
|
-
const s =
|
|
973
|
+
const s = x.createErrorEvent("OnMessage error: " + e.toString(), this.getCurrentInteractionId() ?? "Empty Current"), n = this.updateMap(s);
|
|
921
974
|
if (console.error("Error event: ", JSON.stringify(s, void 0, 4)), !n) {
|
|
922
975
|
console.error("Error event, could not update interaction: ", JSON.stringify(s, void 0, 4));
|
|
923
976
|
return;
|
|
@@ -938,7 +991,7 @@ const S = class S {
|
|
|
938
991
|
version: 0,
|
|
939
992
|
functionMap: /* @__PURE__ */ new Map(),
|
|
940
993
|
interactionQuestion: s ?? t
|
|
941
|
-
}, r = new
|
|
994
|
+
}, r = new j(n, this.eventBusService), this.detachedRunInteractions.set(t, n), this.detachedRunInteractionServices.set(t, r)), { interaction: n, service: r };
|
|
942
995
|
}
|
|
943
996
|
handleDetachedRunEvent(t) {
|
|
944
997
|
const e = t.detachedRunId, { service: s } = this.getOrCreateDetachedRunInteraction(e, t.interactionId, t.detachedRunName);
|
|
@@ -963,14 +1016,29 @@ const S = class S {
|
|
|
963
1016
|
this.liveMessage.emit(t);
|
|
964
1017
|
}
|
|
965
1018
|
localOpen(t, e) {
|
|
966
|
-
this.sessionState.setTurnOpen(!0), this.eventBusService.gatewayEmitTurnOpen(t ?? "");
|
|
1019
|
+
t && this.openTurns.add(t), this.sessionState.setTurnOpen(!0), this.eventBusService.gatewayEmitTurnOpen(t ?? "");
|
|
967
1020
|
}
|
|
968
1021
|
async turnComplete(t, e, s, n) {
|
|
969
|
-
this.
|
|
1022
|
+
this.openTurns.delete(t);
|
|
1023
|
+
const r = t === this.getCurrentInteractionId(), a = r ? null : this.sessionState.currentMessage;
|
|
1024
|
+
r && this.liveMessage.cancelPending(), this.sessionState.setNewMessage(e), a && a.interactionId !== t ? this.sessionState.setCurrentMessage(a) : r && this.handOverLiveMessage(), this.eventBusService.gatewayEmitTurnComplete(t, e), this.interactionCallIds.delete(t), this.sessionState.setLastToolEvent(null), this.localEmitToolEvent({
|
|
970
1025
|
eventName: "todolistUpdated",
|
|
971
1026
|
eventData: "",
|
|
972
1027
|
detachedRunId: n
|
|
973
|
-
}), this.sessionState.setTurnOpen(
|
|
1028
|
+
}), this.sessionState.setTurnOpen(this.openTurns.size > 0);
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* The current turn ended while another is still open (a remote turn that started under it): that one becomes
|
|
1032
|
+
* current and its progress so far goes live at once.
|
|
1033
|
+
*/
|
|
1034
|
+
handOverLiveMessage() {
|
|
1035
|
+
for (const t of this.openTurns) {
|
|
1036
|
+
const e = this.interactionsServiceMap.get(t);
|
|
1037
|
+
if (e) {
|
|
1038
|
+
this.currentIS = e, this.sessionState.setCurrentMessage(e.interaction), this.eventBusService.gatewayEmitMessage(e.interaction);
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
974
1042
|
}
|
|
975
1043
|
async asyncLoadArtifact(t) {
|
|
976
1044
|
let e = null;
|
|
@@ -1011,7 +1079,7 @@ const S = class S {
|
|
|
1011
1079
|
}
|
|
1012
1080
|
/** Hang up and stop supervising the connection. `closeTurn` also closes an open turn in the store. */
|
|
1013
1081
|
stopStream(t = !0) {
|
|
1014
|
-
t && this.sessionState.setTurnOpen(!1), this.sessionState.setConnectionOutage(null), this.connection.close();
|
|
1082
|
+
t && (this.openTurns.clear(), this.sessionState.setTurnOpen(!1)), this.sessionState.setConnectionOutage(null), this.connection.close();
|
|
1015
1083
|
}
|
|
1016
1084
|
/**
|
|
1017
1085
|
* Make sure the socket is OPEN before a send, redialling the SAME session when it is not.
|
|
@@ -1034,23 +1102,23 @@ const S = class S {
|
|
|
1034
1102
|
const s = await this.connection.ensure(t);
|
|
1035
1103
|
console.log(`[AgentGateway] send guard: reconnected to ${s} in ${Date.now() - e}ms — sending now.`);
|
|
1036
1104
|
} catch (s) {
|
|
1037
|
-
throw console.error(`[AgentGateway] send guard: redial FAILED after ${Date.now() - e}ms — the message was not sent.`, s), new Error(
|
|
1105
|
+
throw console.error(`[AgentGateway] send guard: redial FAILED after ${Date.now() - e}ms — the message was not sent.`, s), new Error(y.NO_CONNECTION_MESSAGE, { cause: s });
|
|
1038
1106
|
}
|
|
1039
1107
|
}
|
|
1040
1108
|
// ─── Outbound: the user's turns ─────────────────────────────────────────
|
|
1041
1109
|
async sendMessage(t, e) {
|
|
1042
|
-
let s = this.getInteractions().length, n =
|
|
1110
|
+
let s = this.getInteractions().length, n = N.createWaiting(!0, !0, "Overall", /* @__PURE__ */ new Date());
|
|
1043
1111
|
this.sendStatusMessage(t.data, [n], s.toString() + "1");
|
|
1044
1112
|
try {
|
|
1045
1113
|
const { promptAsString: r, json: a } = this.sessionState.onRenderPrompt ? this.sessionState.onRenderPrompt(t.data) : { promptAsString: t.data, json: void 0 };
|
|
1046
1114
|
this.sessionState.onRenderPrompt && (t.data = a, t.friendlyMessage = r);
|
|
1047
|
-
const c = await this.sendMessageInternal(t, e),
|
|
1048
|
-
this.storageService.storePrompt(
|
|
1049
|
-
const f =
|
|
1115
|
+
const c = await this.sendMessageInternal(t, e), l = Wt.createPrompt(r ?? "", (a == null ? void 0 : a.toString()) ?? {});
|
|
1116
|
+
this.storageService.storePrompt(l);
|
|
1117
|
+
const f = N.fromText("Planejando próximos passos");
|
|
1050
1118
|
return this.sendStatusMessage(t.data, [n, f], s.toString() + "1"), c != null && c.success ? this.eventBusService.ChatEmitMessageSent(t.data, c.interaction) : this.eventBusService.ChatEmitMessageError((c == null ? void 0 : c.error) ?? ""), c;
|
|
1051
1119
|
} catch (r) {
|
|
1052
1120
|
const a = r instanceof Error ? r.message : String(r ?? "");
|
|
1053
|
-
return this.liveMessage.cancelPending(), this.sessionState.setCurrentMessage(null), this.sessionState.setTurnOpen(
|
|
1121
|
+
return this.liveMessage.cancelPending(), this.sessionState.setCurrentMessage(null), this.sessionState.setTurnOpen(this.openTurns.size > 0), this.eventBusService.ChatEmitMessageError(a), {
|
|
1054
1122
|
success: !1,
|
|
1055
1123
|
error: a,
|
|
1056
1124
|
interaction: {
|
|
@@ -1058,7 +1126,7 @@ const S = class S {
|
|
|
1058
1126
|
created: /* @__PURE__ */ new Date(),
|
|
1059
1127
|
interactionId: "",
|
|
1060
1128
|
partial: !1,
|
|
1061
|
-
parts: [
|
|
1129
|
+
parts: [N.fromError(a)],
|
|
1062
1130
|
version: 0
|
|
1063
1131
|
}
|
|
1064
1132
|
};
|
|
@@ -1066,18 +1134,18 @@ const S = class S {
|
|
|
1066
1134
|
}
|
|
1067
1135
|
// Send a message to the server over the socket
|
|
1068
1136
|
async sendMessageInternal(t, e) {
|
|
1069
|
-
var r, a, c,
|
|
1137
|
+
var r, a, c, l, f, p, m, u;
|
|
1070
1138
|
const s = this.suspendState;
|
|
1071
1139
|
this.suspendState = !1;
|
|
1072
1140
|
const n = this.startInteraction(t.friendlyMessage ?? t.data, s);
|
|
1073
|
-
if (this.registerInteractionCallId(n.interactionId, e), (r = t.userUi) != null && r.name && n.parts.push(
|
|
1141
|
+
if (this.localInteractionIds.add(n.interactionId), this.registerInteractionCallId(n.interactionId, e), (r = t.userUi) != null && r.name && n.parts.push(N.fromUserUi(t.userUi.name, t.userUi.args)), !this.wsService)
|
|
1074
1142
|
throw "no wsService created";
|
|
1075
1143
|
if (!((a = this.config.agentConfig) != null && a.id))
|
|
1076
1144
|
throw "Agent not selected";
|
|
1077
1145
|
return this.sessionState.setLastActivity(), await this.ensureConnected(), this.wsService.sendMessage({
|
|
1078
1146
|
Content: t.data,
|
|
1079
1147
|
SessionId: this.sessionId,
|
|
1080
|
-
Action:
|
|
1148
|
+
Action: U.SendMessage,
|
|
1081
1149
|
ModelId: this.config.model,
|
|
1082
1150
|
InteractionId: n.interactionId,
|
|
1083
1151
|
AgentId: (c = this.config.agentConfig) == null ? void 0 : c.id,
|
|
@@ -1086,18 +1154,18 @@ const S = class S {
|
|
|
1086
1154
|
// instead of looking up an Agent metadata row by AgentId. Plumbed through
|
|
1087
1155
|
// `agentContext.agentConfig.agentImplementation` so a host can run a turn
|
|
1088
1156
|
// against any registered LlmAgent variant without seeding a Mongo record.
|
|
1089
|
-
AgentImplementation: (
|
|
1157
|
+
AgentImplementation: (l = this.config.agentConfig) == null ? void 0 : l.agentImplementation,
|
|
1090
1158
|
Parameters: {
|
|
1091
1159
|
CustomerName: ((f = this.config.customer) == null ? void 0 : f.name) ?? "",
|
|
1092
|
-
DataSet: ((
|
|
1160
|
+
DataSet: ((p = this.config.customer) == null ? void 0 : p.dataSetId) ?? ""
|
|
1093
1161
|
},
|
|
1094
1162
|
// Forward image attachments as a structured payload (PascalCase
|
|
1095
1163
|
// keys to match the C# `Message.Images` / `MessageImage` model).
|
|
1096
|
-
Images: (
|
|
1164
|
+
Images: (m = t.images) != null && m.length ? t.images.map((g) => ({ MimeType: g.mimeType, Data: g.data })) : void 0,
|
|
1097
1165
|
// Client-only UI representation for this turn (PascalCase to match the C# `Message.UserUi`
|
|
1098
1166
|
// model). The server persists it as a UserUiContent sibling and strips it from the model
|
|
1099
1167
|
// request; Content remains the only model-facing text.
|
|
1100
|
-
UserUi: (
|
|
1168
|
+
UserUi: (u = t.userUi) != null && u.name ? { Name: t.userUi.name, Args: t.userUi.args } : void 0
|
|
1101
1169
|
}), this.localOpen(n.interactionId, "system"), {
|
|
1102
1170
|
interaction: n,
|
|
1103
1171
|
success: !0
|
|
@@ -1106,7 +1174,7 @@ const S = class S {
|
|
|
1106
1174
|
sendStatusMessage(t, e, s) {
|
|
1107
1175
|
var r, a;
|
|
1108
1176
|
let n;
|
|
1109
|
-
((a = (r = this.currentIS) == null ? void 0 : r.interaction) == null ? void 0 : a.turn_complete) === !1 ? (n =
|
|
1177
|
+
((a = (r = this.currentIS) == null ? void 0 : r.interaction) == null ? void 0 : a.turn_complete) === !1 && !this.currentIS.interaction.remote ? (n = qt.cloneDeep(this.currentIS.interaction), n.parts.push(...e)) : n = {
|
|
1110
1178
|
interactionId: "tmp" + s,
|
|
1111
1179
|
created: /* @__PURE__ */ new Date(),
|
|
1112
1180
|
partial: !0,
|
|
@@ -1131,6 +1199,54 @@ const S = class S {
|
|
|
1131
1199
|
const s = (n = this.wsService.answerHumanTool) == null ? void 0 : n.bind(this.wsService);
|
|
1132
1200
|
return s ? s(t, e) : (console.warn("[AgentGateway] transport cannot deliver parked-tool answers", t), "queued");
|
|
1133
1201
|
}
|
|
1202
|
+
// ─── Detached-run announcements ─────────────────────────────────────────
|
|
1203
|
+
/**
|
|
1204
|
+
* Ask the server to tell this conversation about a detached run of it that settled (AIA Issue 1548 §5): the
|
|
1205
|
+
* `AnnounceDetachedRun` socket action. The server claims every settled-and-unannounced run of the session at once
|
|
1206
|
+
* (two tabs asking, one wins) and starts an ordinary turn on this socket, `origin: platform`, in the host's words —
|
|
1207
|
+
* which arrives as any remote turn does (`InteractionStarted`).
|
|
1208
|
+
*
|
|
1209
|
+
* <p>Never over a turn: while one is open in this tab the request waits, and goes out when the chat is idle (and
|
|
1210
|
+
* the socket up). `settleKey` — the run's `settledAt` — scopes the dedupe: a repeated request for the same settle
|
|
1211
|
+
* is a no-op, a resumed run that settles anew is announced again.</p>
|
|
1212
|
+
*/
|
|
1213
|
+
async announceDetachedRun(t, e) {
|
|
1214
|
+
const s = `${t}|${e ?? ""}`;
|
|
1215
|
+
return this.announceRequested.has(s) ? "duplicate" : (this.announceRequested.add(s), this.pendingAnnouncements.push(t), await this.flushAnnouncements(), this.pendingAnnouncements.includes(t) ? "queued" : "sent");
|
|
1216
|
+
}
|
|
1217
|
+
/** Send the queued announcements, oldest first, while the chat is idle and the socket is open. */
|
|
1218
|
+
async flushAnnouncements() {
|
|
1219
|
+
var t, e;
|
|
1220
|
+
if (!this.flushingAnnouncements) {
|
|
1221
|
+
this.flushingAnnouncements = !0;
|
|
1222
|
+
try {
|
|
1223
|
+
for (; this.pendingAnnouncements.length > 0 && !this.sessionState.turnOpen; ) {
|
|
1224
|
+
const s = (t = this.wsService.sendBaseMessage) == null ? void 0 : t.bind(this.wsService);
|
|
1225
|
+
if (!s) {
|
|
1226
|
+
console.warn("[AgentGateway] transport cannot send an AnnounceDetachedRun; the request stays queued");
|
|
1227
|
+
return;
|
|
1228
|
+
}
|
|
1229
|
+
if (!this.wsService.isConnected() || !this.sessionId) return;
|
|
1230
|
+
const n = this.pendingAnnouncements[0];
|
|
1231
|
+
try {
|
|
1232
|
+
s({
|
|
1233
|
+
Action: U.AnnounceDetachedRun,
|
|
1234
|
+
AgentId: ((e = this.config.agentConfig) == null ? void 0 : e.id) ?? "",
|
|
1235
|
+
InteractionId: `announce_${n}_${Date.now()}`,
|
|
1236
|
+
SessionId: this.sessionId,
|
|
1237
|
+
DetachedRunId: n
|
|
1238
|
+
});
|
|
1239
|
+
} catch (r) {
|
|
1240
|
+
console.warn(`[AgentGateway] AnnounceDetachedRun for run ${n} could not be sent; it stays queued`, r);
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
this.pendingAnnouncements.shift(), console.log(`[AgentGateway] asked for the announcement of run ${n} (session ${this.sessionId}).`);
|
|
1244
|
+
}
|
|
1245
|
+
} finally {
|
|
1246
|
+
this.flushingAnnouncements = !1;
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1134
1250
|
// ─── Suspend ────────────────────────────────────────────────────────────
|
|
1135
1251
|
async suspend() {
|
|
1136
1252
|
const t = this.wsService;
|
|
@@ -1151,75 +1267,83 @@ const S = class S {
|
|
|
1151
1267
|
console.error("CurrentIS is null"), s("CurrentIS is null");
|
|
1152
1268
|
return;
|
|
1153
1269
|
}
|
|
1154
|
-
const
|
|
1155
|
-
|
|
1270
|
+
const l = this.currentIS.interaction;
|
|
1271
|
+
l.isSuspended = !0, this.openTurns.delete(l.interactionId), this.sessionState.setCurrentMessage(l), this.sessionState.setTurnOpen(this.openTurns.size > 0), this.suspendState = !0, e(!0);
|
|
1156
1272
|
});
|
|
1157
1273
|
}
|
|
1158
1274
|
async sleep(t) {
|
|
1159
1275
|
return new Promise((e) => setTimeout(e, t));
|
|
1160
1276
|
}
|
|
1161
1277
|
};
|
|
1162
|
-
|
|
1278
|
+
h(y, "AgentGatewayWSAsync"), // How often the streaming message re-renders. 150 ms made a reasoning highlight, or a burst of them, visibly lag the
|
|
1163
1279
|
// server (Ciro, 2026-10-07). The server already merges deltas over a 50 ms window (the SDK's DeltaCoalescer), so
|
|
1164
1280
|
// frames arrive at ~20/s per kind and 20 ms here costs little: it mostly stops adding a second delay on top.
|
|
1165
|
-
o(
|
|
1281
|
+
o(y, "CURRENT_MSG_THROTTLE_MS", 20), /**
|
|
1166
1282
|
* How long an open turn survives a connection outage. While the supervisor is redialling the
|
|
1167
1283
|
* turn stays open — a short blip resumes it from the replay cursor, and a server that comes back
|
|
1168
1284
|
* (or another instance) closes it with an error if its run is gone. Past this, nobody can tell
|
|
1169
1285
|
* us anything, and a turn that is still open is a composer that is still locked: the client
|
|
1170
1286
|
* closes it itself, with an error part, so the user can go on.
|
|
1171
1287
|
*/
|
|
1172
|
-
o(
|
|
1173
|
-
o(
|
|
1288
|
+
o(y, "OUTAGE_CLOSES_TURN_AFTER_MS", 6e4), o(y, "OUTAGE_TURN_MESSAGE", "Sem conexão com o servidor há mais de um minuto. A resposta foi interrompida."), /** What a send reports when the socket could not be brought up for it. */
|
|
1289
|
+
o(y, "NO_CONNECTION_MESSAGE", "Não foi possível obter conexão com o servidor"), o(y, "HALT_TURN_MESSAGE", {
|
|
1174
1290
|
"attached-elsewhere": "Esta sessão está aberta em outra janela. A resposta foi interrompida.",
|
|
1175
1291
|
"session-gone": "A sessão não existe mais no servidor. A resposta foi interrompida.",
|
|
1176
1292
|
"closed-by-server": "O servidor encerrou a conexão. A resposta foi interrompida."
|
|
1177
1293
|
});
|
|
1178
|
-
let
|
|
1179
|
-
function
|
|
1180
|
-
const t = new
|
|
1181
|
-
t.author = "main", t.streamingParts = i.streamingParts, i.partialEventType ? t.eventType = i.partialEventType : t.eventType = i.functionName ? { fullName:
|
|
1294
|
+
let Tt = y;
|
|
1295
|
+
function ce(i) {
|
|
1296
|
+
const t = new Lt();
|
|
1297
|
+
t.author = "main", t.streamingParts = i.streamingParts, i.partialEventType ? t.eventType = i.partialEventType : t.eventType = i.functionName ? { fullName: z.StreamingFunctionCallUpdateContent, type: I.PartialFunctionCall } : i.partialReasoning ? { fullName: z.StreamingReasoningContent, type: I.Thought } : { fullName: z.StreamingTextContent, type: I.Text }, t.interactionId = i.iteractionId ?? "", t.mimeType = "text/plain", t.partial = !0, t.role = { label: "Assistant" }, t.timestamp = i.timeStamp ?? (/* @__PURE__ */ new Date()).getTime(), t.turnComplete = !1;
|
|
1182
1298
|
const e = t.eventType.type;
|
|
1183
1299
|
if (e == I.PartialFunctionCall) {
|
|
1184
|
-
const s = new
|
|
1300
|
+
const s = new Pt();
|
|
1185
1301
|
s.choiceIndex = 0, s.callId = i.functionCallId, s.arguments = i.partialText, s.name = i.functionName, t.content = { items: [s], role: { label: "Assistant" } };
|
|
1186
1302
|
} else if (e == I.Text)
|
|
1187
|
-
if (t.eventType.fullName ==
|
|
1188
|
-
const s = new
|
|
1303
|
+
if (t.eventType.fullName == z.StreamingTextContent) {
|
|
1304
|
+
const s = new Ft();
|
|
1189
1305
|
s.text = i.partialText, t.content = { items: [s], role: { label: "Assistant" } };
|
|
1190
1306
|
} else {
|
|
1191
|
-
const s = new
|
|
1307
|
+
const s = new _t();
|
|
1192
1308
|
s.text = i.partialText, t.content = { items: [s], role: { label: "Assistant" } };
|
|
1193
1309
|
}
|
|
1194
1310
|
else if (e == I.Thought) {
|
|
1195
|
-
const s = new
|
|
1311
|
+
const s = new Nt();
|
|
1196
1312
|
s.text = i.partialReasoning, t.content = { items: [s], role: { label: "Assistant" } };
|
|
1197
1313
|
}
|
|
1198
1314
|
return t;
|
|
1199
1315
|
}
|
|
1200
|
-
|
|
1201
|
-
function
|
|
1202
|
-
const t = i.partialEvent ?
|
|
1316
|
+
h(ce, "llmEventFromPartial");
|
|
1317
|
+
function st(i) {
|
|
1318
|
+
const t = i.partialEvent ? ce(i.partialEvent) : i.event;
|
|
1203
1319
|
if (t)
|
|
1204
1320
|
return i.detachedRunId && (t.detachedRunId = i.detachedRunId, t.detachedRunName = i.detachedRunName), t;
|
|
1205
1321
|
}
|
|
1206
|
-
|
|
1207
|
-
function
|
|
1322
|
+
h(st, "llmEventFromFrame");
|
|
1323
|
+
function Dt(i) {
|
|
1208
1324
|
var t, e, s;
|
|
1209
1325
|
return (((e = (t = i.content) == null ? void 0 : t.items) == null ? void 0 : e.length) ?? 0) > 0 || (((s = i.streamingParts) == null ? void 0 : s.length) ?? 0) > 0;
|
|
1210
1326
|
}
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1327
|
+
h(Dt, "carriesContent");
|
|
1328
|
+
function $t(i) {
|
|
1329
|
+
var e;
|
|
1330
|
+
if (i.type !== "InteractionStarted" || !((e = i.interaction) != null && e.interactionId)) return;
|
|
1331
|
+
if (i.interaction.detachedRunId || !i.detachedRunId) return i.interaction;
|
|
1332
|
+
const t = Object.assign(new bt(), i.interaction);
|
|
1333
|
+
return t.detachedRunId = i.detachedRunId, t;
|
|
1334
|
+
}
|
|
1335
|
+
h($t, "interactionStartFromFrame");
|
|
1336
|
+
const le = 0.02;
|
|
1337
|
+
function he(i, t) {
|
|
1214
1338
|
if (!t) return null;
|
|
1215
1339
|
const e = (t.interactionsCompressed ?? 0) > 0 || (t.compressionRatio ?? 0) > 0;
|
|
1216
1340
|
if (!i)
|
|
1217
|
-
return e ?
|
|
1218
|
-
const s = i.size - t.size, n = t.maxSize || i.maxSize || 0, r = n > 0 ? s / n >=
|
|
1219
|
-
return !e && !r ? null :
|
|
1341
|
+
return e ? yt(t, t.size, t.size) : null;
|
|
1342
|
+
const s = i.size - t.size, n = t.maxSize || i.maxSize || 0, r = n > 0 ? s / n >= le : i.size > 0 && s / i.size >= 0.1;
|
|
1343
|
+
return !e && !r ? null : yt(t, i.size, t.size, i.ratio);
|
|
1220
1344
|
}
|
|
1221
|
-
|
|
1222
|
-
function
|
|
1345
|
+
h(he, "detectCompaction");
|
|
1346
|
+
function yt(i, t, e, s) {
|
|
1223
1347
|
return {
|
|
1224
1348
|
at: Date.now(),
|
|
1225
1349
|
// Where the bar stood when it happened — the marker's position. The ratio
|
|
@@ -1231,8 +1355,8 @@ function et(i, t, e, s) {
|
|
|
1231
1355
|
interactionsCompressed: i.interactionsCompressed
|
|
1232
1356
|
};
|
|
1233
1357
|
}
|
|
1234
|
-
|
|
1235
|
-
const
|
|
1358
|
+
h(yt, "entry");
|
|
1359
|
+
const vt = 4, ue = 3, de = 20, P = class P {
|
|
1236
1360
|
constructor() {
|
|
1237
1361
|
o(this, "stats");
|
|
1238
1362
|
o(this, "onFlush");
|
|
@@ -1268,35 +1392,35 @@ const st = 4, Bt = 3, Ht = 20, E = class E {
|
|
|
1268
1392
|
record(t) {
|
|
1269
1393
|
const e = Date.now();
|
|
1270
1394
|
if (this.events.push([e, t.textLength]), this.stats.eventCount += 1, this.stats.totalTransfered += t.rawLength, t.sessionInformation && !t.fromDetachedRun) {
|
|
1271
|
-
const r =
|
|
1272
|
-
r && (this.sessionCompactions.push(r), this.sessionCompactions.length >
|
|
1395
|
+
const r = he(this.previousSessionInformation, t.sessionInformation);
|
|
1396
|
+
r && (this.sessionCompactions.push(r), this.sessionCompactions.length > de && this.sessionCompactions.shift()), this.previousSessionInformation = t.sessionInformation, this.stats.sessionInformation = t.sessionInformation, this.stats.sessionCompactions = this.sessionCompactions;
|
|
1273
1397
|
}
|
|
1274
1398
|
this.stats.elapsed = (e - this.stats.startTime.getTime()) / 1e3;
|
|
1275
|
-
const s = e -
|
|
1399
|
+
const s = e - vt * 1e3;
|
|
1276
1400
|
for (; this.events.length > 0 && this.events[0][0] <= s; ) this.events.shift();
|
|
1277
1401
|
const n = this.events.reduce((r, a) => r + a[1], 0);
|
|
1278
|
-
this.stats.tokensPerSecond = n /
|
|
1402
|
+
this.stats.tokensPerSecond = n / ue / vt, this.flushCoalesced();
|
|
1279
1403
|
}
|
|
1280
1404
|
flushCoalesced() {
|
|
1281
1405
|
if (!this.onFlush) return;
|
|
1282
1406
|
const t = Date.now(), e = t - this.lastFlushed;
|
|
1283
|
-
if (e >=
|
|
1407
|
+
if (e >= P.STATS_FLUSH_MS) {
|
|
1284
1408
|
this.flushTimer && (clearTimeout(this.flushTimer), this.flushTimer = null), this.lastFlushed = t, this.onFlush({ ...this.stats });
|
|
1285
1409
|
return;
|
|
1286
1410
|
}
|
|
1287
1411
|
this.flushTimer || (this.flushTimer = setTimeout(() => {
|
|
1288
1412
|
var s;
|
|
1289
1413
|
this.flushTimer = null, this.lastFlushed = Date.now(), (s = this.onFlush) == null || s.call(this, { ...this.stats });
|
|
1290
|
-
},
|
|
1414
|
+
}, P.STATS_FLUSH_MS - e));
|
|
1291
1415
|
}
|
|
1292
1416
|
};
|
|
1293
|
-
|
|
1294
|
-
let
|
|
1295
|
-
const
|
|
1417
|
+
h(P, "StreamStatsTracker"), o(P, "STATS_FLUSH_MS", 120);
|
|
1418
|
+
let tt = P;
|
|
1419
|
+
const fe = 5e3, ge = 3e4, Y = 1e4, lt = class lt {
|
|
1296
1420
|
constructor(t = "ws://localhost:8000", e) {
|
|
1297
1421
|
o(this, "ws", null);
|
|
1298
1422
|
/** Frame counts, byte totals, the token-rate estimate and the compaction history — see StreamStatsTracker. */
|
|
1299
|
-
o(this, "statsTracker", new
|
|
1423
|
+
o(this, "statsTracker", new tt());
|
|
1300
1424
|
o(this, "baseUrl");
|
|
1301
1425
|
/**
|
|
1302
1426
|
* Optional callback that returns the current JWT. We re-evaluate it on every connect/reconnect
|
|
@@ -1310,6 +1434,7 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1310
1434
|
o(this, "onOpen");
|
|
1311
1435
|
o(this, "onFirstToken");
|
|
1312
1436
|
o(this, "onSuspended");
|
|
1437
|
+
o(this, "onBillingLimit");
|
|
1313
1438
|
o(this, "onClose");
|
|
1314
1439
|
/**
|
|
1315
1440
|
* Fired when the server signals that we lost streaming events on resume — our
|
|
@@ -1317,6 +1442,7 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1317
1442
|
* reset partial-streaming UI for this session and rely on persisted history.
|
|
1318
1443
|
*/
|
|
1319
1444
|
o(this, "onRewindLost");
|
|
1445
|
+
o(this, "onInteractionStarted");
|
|
1320
1446
|
o(this, "sessionId");
|
|
1321
1447
|
o(this, "monitorInterval", null);
|
|
1322
1448
|
o(this, "streamingResponseSerializer");
|
|
@@ -1342,7 +1468,7 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1342
1468
|
* the single-attach policy makes it undeliverable from here anyway.
|
|
1343
1469
|
*/
|
|
1344
1470
|
o(this, "pendingAnswers", []);
|
|
1345
|
-
this.baseUrl = t, this.getToken = e, this.firstTokenInteractions = /* @__PURE__ */ new Set(), this.streamingResponseSerializer =
|
|
1471
|
+
this.baseUrl = t, this.getToken = e, this.firstTokenInteractions = /* @__PURE__ */ new Set(), this.streamingResponseSerializer = et(), console.log("SOCKET INSTACE", Bt()), this.lastActivity = (/* @__PURE__ */ new Date()).getTime();
|
|
1346
1472
|
}
|
|
1347
1473
|
get stats() {
|
|
1348
1474
|
return this.statsTracker.stats;
|
|
@@ -1365,57 +1491,57 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1365
1491
|
*/
|
|
1366
1492
|
connect(t, e) {
|
|
1367
1493
|
return new Promise(async (s, n) => {
|
|
1368
|
-
var
|
|
1494
|
+
var M, F;
|
|
1369
1495
|
e != null && e.resume || (this.lastSeq = -1);
|
|
1370
|
-
const r = (
|
|
1496
|
+
const r = (M = this.getToken) == null ? void 0 : M.call(this), a = [];
|
|
1371
1497
|
r && a.push(`access_token=${encodeURIComponent(r)}`), t && this.lastSeq >= 0 && a.push(`lastSeq=${this.lastSeq}`), t && (e != null && e.resume) && e.openInteractionId && a.push(`openInteraction=${encodeURIComponent(e.openInteractionId)}`);
|
|
1372
|
-
const c = a.length > 0 ? `?${a.join("&")}` : "",
|
|
1373
|
-
console.log("[WebSocket] Connecting to", `${
|
|
1498
|
+
const c = a.length > 0 ? `?${a.join("&")}` : "", l = this.baseUrl.replace(/^http:\/\//, "ws://").replace(/^https:\/\//, "wss://"), f = `${l}/ws/connect/${t ?? ""}${c}`;
|
|
1499
|
+
console.log("[WebSocket] Connecting to", `${l}/ws/connect/${t ?? ""}` + (t && this.lastSeq >= 0 ? ` (resume lastSeq=${this.lastSeq})` : "")), this.detachSocket(), this.ws = new WebSocket(f), this.ws.onopen = () => {
|
|
1374
1500
|
console.log("[WebSocket] Socket open — waiting for the session id"), this.ping(), this.monitor();
|
|
1375
|
-
}, this.ws.onmessage = (
|
|
1501
|
+
}, this.ws.onmessage = (v) => {
|
|
1376
1502
|
try {
|
|
1377
1503
|
this.lastActivity = (/* @__PURE__ */ new Date()).getTime(), this.pingSentAt = null;
|
|
1378
|
-
const
|
|
1379
|
-
if (!
|
|
1504
|
+
const C = this.streamingResponseSerializer.parse(v.data);
|
|
1505
|
+
if (!C)
|
|
1380
1506
|
throw "Error parsing event JSON";
|
|
1381
|
-
this.handleMessage(
|
|
1382
|
-
} catch (
|
|
1383
|
-
console.error("[WebSocket] Error parsing message:",
|
|
1507
|
+
this.handleMessage(C, v.data);
|
|
1508
|
+
} catch (C) {
|
|
1509
|
+
console.error("[WebSocket] Error parsing message:", C);
|
|
1384
1510
|
}
|
|
1385
|
-
}, this.ws.onerror = (
|
|
1386
|
-
console.error("[WebSocket] Error:",
|
|
1387
|
-
}, this.ws.onclose = (
|
|
1388
|
-
var
|
|
1389
|
-
console.log(`[WebSocket] Closed: code=${
|
|
1511
|
+
}, this.ws.onerror = (v) => {
|
|
1512
|
+
console.error("[WebSocket] Error:", v), this.onError && this.onError("WebSocket error");
|
|
1513
|
+
}, this.ws.onclose = (v) => {
|
|
1514
|
+
var C;
|
|
1515
|
+
console.log(`[WebSocket] Closed: code=${v.code} reason="${v.reason}" (session ${this.sessionLabel}, lastSeq=${this.lastSeq})`), this.monitorInterval && (clearInterval(this.monitorInterval), this.monitorInterval = null), this.pingSentAt = null, this.sessionId = void 0, (C = this.onClose) == null || C.call(this, v.code, v.reason);
|
|
1390
1516
|
};
|
|
1391
|
-
const
|
|
1517
|
+
const p = 200, m = 3e4, u = 8e3, g = this.ws;
|
|
1392
1518
|
this.sessionId = void 0;
|
|
1393
|
-
let
|
|
1519
|
+
let S = 0, D = 0;
|
|
1394
1520
|
for (; ; ) {
|
|
1395
|
-
if (this.ws !==
|
|
1521
|
+
if (this.ws !== g) {
|
|
1396
1522
|
n("Connection attempt superseded by a newer connect()");
|
|
1397
1523
|
return;
|
|
1398
1524
|
}
|
|
1399
1525
|
if (this.sessionId) break;
|
|
1400
|
-
if (
|
|
1526
|
+
if (g.readyState === WebSocket.CLOSING || g.readyState === WebSocket.CLOSED) {
|
|
1401
1527
|
n("Connection closed before a session id was received");
|
|
1402
1528
|
return;
|
|
1403
1529
|
}
|
|
1404
|
-
if (
|
|
1530
|
+
if (g.readyState === WebSocket.CONNECTING && S >= m) {
|
|
1405
1531
|
try {
|
|
1406
|
-
|
|
1532
|
+
g.close();
|
|
1407
1533
|
} catch {
|
|
1408
1534
|
}
|
|
1409
|
-
n(`Server did not accept the connection within ${
|
|
1535
|
+
n(`Server did not accept the connection within ${m / 1e3}s`);
|
|
1410
1536
|
return;
|
|
1411
1537
|
}
|
|
1412
|
-
if (
|
|
1413
|
-
n(`Connected but no session id arrived within ${
|
|
1538
|
+
if (g.readyState === WebSocket.OPEN && D >= u) {
|
|
1539
|
+
n(`Connected but no session id arrived within ${u / 1e3}s`);
|
|
1414
1540
|
return;
|
|
1415
1541
|
}
|
|
1416
|
-
|
|
1542
|
+
S % 2e3 === 0 && console.log("Aguardando session Id"), await this.sleep(p), S += p, g.readyState === WebSocket.OPEN && (D += p);
|
|
1417
1543
|
}
|
|
1418
|
-
console.log(`[WebSocket] Session ${this.sessionId} proven${this.lastSeq >= 0 ? ` (replaying from lastSeq=${this.lastSeq})` : ""}.`), this.lastKnownSessionId = this.sessionId, (
|
|
1544
|
+
console.log(`[WebSocket] Session ${this.sessionId} proven${this.lastSeq >= 0 ? ` (replaying from lastSeq=${this.lastSeq})` : ""}.`), this.lastKnownSessionId = this.sessionId, (F = this.onOpen) == null || F.call(this), this.flushPendingAnswers(this.sessionId), s(this.sessionId);
|
|
1419
1545
|
});
|
|
1420
1546
|
}
|
|
1421
1547
|
/** Drop the current socket without reporting it: handlers off, then close. */
|
|
@@ -1440,6 +1566,7 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1440
1566
|
* Handle incoming messages from server
|
|
1441
1567
|
*/
|
|
1442
1568
|
handleMessage(t, e) {
|
|
1569
|
+
var n;
|
|
1443
1570
|
switch (typeof t.seq == "number" && t.seq > this.lastSeq && (this.lastSeq = t.seq), t.type) {
|
|
1444
1571
|
case "Pong":
|
|
1445
1572
|
if (this.sessionId = t.sessionId, this.lastKnownSessionId = t.sessionId ?? this.lastKnownSessionId, !t.sessionId) {
|
|
@@ -1453,6 +1580,11 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1453
1580
|
break;
|
|
1454
1581
|
case "Event":
|
|
1455
1582
|
break;
|
|
1583
|
+
case "InteractionStarted": {
|
|
1584
|
+
const r = $t(t);
|
|
1585
|
+
r ? (n = this.onInteractionStarted) == null || n.call(this, r) : console.warn("[WebSocket] InteractionStarted frame without an interaction id");
|
|
1586
|
+
break;
|
|
1587
|
+
}
|
|
1456
1588
|
case "RewindLost":
|
|
1457
1589
|
console.warn(
|
|
1458
1590
|
`[WebSocket] RewindLost: lost events on resume, resuming from seq=${t.seq}`
|
|
@@ -1461,8 +1593,8 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1461
1593
|
default:
|
|
1462
1594
|
console.warn("[WebSocket] Unknown message type:", t.type);
|
|
1463
1595
|
}
|
|
1464
|
-
const s =
|
|
1465
|
-
return s ? (
|
|
1596
|
+
const s = st(t);
|
|
1597
|
+
return s ? (Dt(s) && this.emitFirstToken(s.interactionId), this.handleEvent(s, e), s) : null;
|
|
1466
1598
|
}
|
|
1467
1599
|
emitFirstToken(t) {
|
|
1468
1600
|
var e;
|
|
@@ -1501,7 +1633,7 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1501
1633
|
}
|
|
1502
1634
|
suspendStream() {
|
|
1503
1635
|
this.sendBaseMessage({
|
|
1504
|
-
Action:
|
|
1636
|
+
Action: U.Suspend,
|
|
1505
1637
|
AgentId: "",
|
|
1506
1638
|
InteractionId: "",
|
|
1507
1639
|
ModelId: ""
|
|
@@ -1520,7 +1652,7 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1520
1652
|
*/
|
|
1521
1653
|
answerHumanTool(t, e) {
|
|
1522
1654
|
const s = {
|
|
1523
|
-
Action:
|
|
1655
|
+
Action: U.AnswerHumanTool,
|
|
1524
1656
|
AgentId: "",
|
|
1525
1657
|
InteractionId: "",
|
|
1526
1658
|
ToolCallId: t,
|
|
@@ -1547,7 +1679,7 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1547
1679
|
}
|
|
1548
1680
|
try {
|
|
1549
1681
|
this.sendBaseMessage({
|
|
1550
|
-
Action:
|
|
1682
|
+
Action: U.AnswerHumanTool,
|
|
1551
1683
|
AgentId: "",
|
|
1552
1684
|
InteractionId: "",
|
|
1553
1685
|
ToolCallId: s.toolCallId,
|
|
@@ -1581,21 +1713,21 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1581
1713
|
const t = Date.now();
|
|
1582
1714
|
if (this.pingSentAt !== null) {
|
|
1583
1715
|
const e = t - this.pingSentAt;
|
|
1584
|
-
if (e >=
|
|
1716
|
+
if (e >= Y) {
|
|
1585
1717
|
this.failSocket(
|
|
1586
|
-
|
|
1718
|
+
se,
|
|
1587
1719
|
`No frame received for ${Math.round(e / 1e3)}s after a Ping`
|
|
1588
1720
|
);
|
|
1589
1721
|
return;
|
|
1590
1722
|
}
|
|
1591
|
-
console.warn(`[WebSocket] Still no reply ${e}ms after the Ping (budget ${
|
|
1723
|
+
console.warn(`[WebSocket] Still no reply ${e}ms after the Ping (budget ${Y}ms, session ${this.sessionLabel}) — the peer may be gone.`);
|
|
1592
1724
|
return;
|
|
1593
1725
|
}
|
|
1594
|
-
t - this.lastActivity >=
|
|
1726
|
+
t - this.lastActivity >= ge && this.ping();
|
|
1595
1727
|
} catch (t) {
|
|
1596
1728
|
console.error("Error on monitor", t);
|
|
1597
1729
|
}
|
|
1598
|
-
},
|
|
1730
|
+
}, fe);
|
|
1599
1731
|
}
|
|
1600
1732
|
/**
|
|
1601
1733
|
* Send a heartbeat ping and start the reply clock (unless one is already running).
|
|
@@ -1606,7 +1738,7 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1606
1738
|
AgentId: "",
|
|
1607
1739
|
InteractionId: "",
|
|
1608
1740
|
ModelId: ""
|
|
1609
|
-
}), this.pingSentAt === null && (this.pingSentAt = Date.now(), console.log(`[WebSocket] Ping sent after ${Date.now() - this.lastActivity}ms of silence (session ${this.sessionLabel}); any frame within ${
|
|
1741
|
+
}), this.pingSentAt === null && (this.pingSentAt = Date.now(), console.log(`[WebSocket] Ping sent after ${Date.now() - this.lastActivity}ms of silence (session ${this.sessionLabel}); any frame within ${Y}ms answers it.`));
|
|
1610
1742
|
}
|
|
1611
1743
|
/**
|
|
1612
1744
|
* Behave as if the peer vanished: the socket is closed and reported as an abnormal close (1006),
|
|
@@ -1641,7 +1773,7 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1641
1773
|
* Disconnect the WebSocket
|
|
1642
1774
|
*/
|
|
1643
1775
|
disconnect() {
|
|
1644
|
-
console.log(`[WebSocket] disconnect() (session ${this.sessionLabel})`), this.monitorInterval && (clearInterval(this.monitorInterval), this.monitorInterval = null), this.pingSentAt = null, this.firstTokenInteractions.clear(), this.lastKnownSessionId = void 0, this.lastSeq = -1, this.ws && (this.ws.close(
|
|
1776
|
+
console.log(`[WebSocket] disconnect() (session ${this.sessionLabel})`), this.monitorInterval && (clearInterval(this.monitorInterval), this.monitorInterval = null), this.pingSentAt = null, this.firstTokenInteractions.clear(), this.lastKnownSessionId = void 0, this.lastSeq = -1, this.ws && (this.ws.close(Ot, "Intentional disconnect"), this.ws = null);
|
|
1645
1777
|
}
|
|
1646
1778
|
/**
|
|
1647
1779
|
* Check if connected
|
|
@@ -1650,27 +1782,43 @@ const zt = 5e3, Kt = 3e4, _ = 1e4, J = class J {
|
|
|
1650
1782
|
return this.ws !== null && this.ws.readyState === WebSocket.OPEN;
|
|
1651
1783
|
}
|
|
1652
1784
|
async handleEvent(t, e) {
|
|
1653
|
-
var
|
|
1785
|
+
var n, r, a, c;
|
|
1786
|
+
const s = (r = (n = t.content) == null ? void 0 : n.items) == null ? void 0 : r.find((l) => l instanceof Ut);
|
|
1787
|
+
if (s) {
|
|
1788
|
+
(a = this.onBillingLimit) == null || a.call(this, {
|
|
1789
|
+
plan: s.plan ?? "team",
|
|
1790
|
+
state: s.state ?? "ok",
|
|
1791
|
+
used: s.used ?? 0,
|
|
1792
|
+
included: s.included ?? 0,
|
|
1793
|
+
limitReached: s.limitReached ?? !1,
|
|
1794
|
+
canManage: s.canManage ?? !1,
|
|
1795
|
+
title: s.title,
|
|
1796
|
+
message: s.message,
|
|
1797
|
+
actionUrl: s.actionUrl,
|
|
1798
|
+
interactionId: t.interactionId || void 0
|
|
1799
|
+
});
|
|
1800
|
+
return;
|
|
1801
|
+
}
|
|
1654
1802
|
try {
|
|
1655
|
-
const
|
|
1656
|
-
if (!
|
|
1803
|
+
const l = x.parseFromTypedEvent(t);
|
|
1804
|
+
if (!l)
|
|
1657
1805
|
throw "Server event was null";
|
|
1658
1806
|
this.statsTracker.record({
|
|
1659
1807
|
rawLength: e.length,
|
|
1660
|
-
textLength: ((
|
|
1661
|
-
sessionInformation:
|
|
1662
|
-
fromDetachedRun: !!
|
|
1663
|
-
}), this.onEvent && this.onEvent(
|
|
1664
|
-
console.error("Lost error on on-event",
|
|
1808
|
+
textLength: ((c = t.text) == null ? void 0 : c.length) ?? 0,
|
|
1809
|
+
sessionInformation: l.session_information,
|
|
1810
|
+
fromDetachedRun: !!l.detachedRunId
|
|
1811
|
+
}), this.onEvent && this.onEvent(l).catch((f) => {
|
|
1812
|
+
console.error("Lost error on on-event", f);
|
|
1665
1813
|
});
|
|
1666
|
-
} catch (
|
|
1667
|
-
console.error("handleEvent error",
|
|
1814
|
+
} catch (l) {
|
|
1815
|
+
console.error("handleEvent error", l);
|
|
1668
1816
|
}
|
|
1669
1817
|
}
|
|
1670
1818
|
};
|
|
1671
|
-
|
|
1672
|
-
let
|
|
1673
|
-
const
|
|
1819
|
+
h(lt, "WebSocketConnecionV2");
|
|
1820
|
+
let Et = lt;
|
|
1821
|
+
const ht = class ht {
|
|
1674
1822
|
constructor(t, e) {
|
|
1675
1823
|
o(this, "cache", /* @__PURE__ */ new Map());
|
|
1676
1824
|
o(this, "serverBaseUrl");
|
|
@@ -1678,7 +1826,7 @@ const j = class j {
|
|
|
1678
1826
|
this.serverBaseUrl = t.replace(/\/$/, ""), this.accessToken = e;
|
|
1679
1827
|
}
|
|
1680
1828
|
get headers() {
|
|
1681
|
-
return { Authorization: `Bearer ${
|
|
1829
|
+
return { Authorization: `Bearer ${Gt(this.accessToken)}` };
|
|
1682
1830
|
}
|
|
1683
1831
|
async init() {
|
|
1684
1832
|
}
|
|
@@ -1725,20 +1873,337 @@ const j = class j {
|
|
|
1725
1873
|
}
|
|
1726
1874
|
}
|
|
1727
1875
|
};
|
|
1728
|
-
|
|
1729
|
-
let
|
|
1876
|
+
h(ht, "HttpArtifactService");
|
|
1877
|
+
let Ct = ht;
|
|
1878
|
+
const ut = class ut extends Error {
|
|
1879
|
+
constructor(t, e) {
|
|
1880
|
+
super(e ?? `GET detached-run frames answered ${t}`), this.status = t, this.name = "DetachedRunFramesHttpError";
|
|
1881
|
+
}
|
|
1882
|
+
};
|
|
1883
|
+
h(ut, "DetachedRunFramesHttpError");
|
|
1884
|
+
let L = ut;
|
|
1885
|
+
function pe(i) {
|
|
1886
|
+
if (i instanceof L) {
|
|
1887
|
+
const { status: t } = i;
|
|
1888
|
+
return { kind: t === 401 ? "unauthorized" : t === 403 ? "forbidden" : t === 404 || t === 410 ? "notFound" : "gaveUp", status: t, cause: i };
|
|
1889
|
+
}
|
|
1890
|
+
return { kind: "gaveUp", cause: i };
|
|
1891
|
+
}
|
|
1892
|
+
h(pe, "toDetachedRunFollowError");
|
|
1893
|
+
function me(i) {
|
|
1894
|
+
let t = "", e = [], s, n;
|
|
1895
|
+
const r = /* @__PURE__ */ h(() => {
|
|
1896
|
+
(e.length > 0 || s !== void 0) && i({ event: s, data: e.join(`
|
|
1897
|
+
`), id: n }), e = [], s = void 0, n = void 0;
|
|
1898
|
+
}, "dispatch"), a = /* @__PURE__ */ h((c) => {
|
|
1899
|
+
if (c === "") return r();
|
|
1900
|
+
if (c.startsWith(":")) return;
|
|
1901
|
+
const l = c.indexOf(":"), f = l === -1 ? c : c.slice(0, l);
|
|
1902
|
+
let p = l === -1 ? "" : c.slice(l + 1);
|
|
1903
|
+
p.startsWith(" ") && (p = p.slice(1)), f === "data" ? e.push(p) : f === "event" ? s = p : f === "id" && (n = p);
|
|
1904
|
+
}, "line");
|
|
1905
|
+
return {
|
|
1906
|
+
push(c) {
|
|
1907
|
+
for (t += c; ; ) {
|
|
1908
|
+
const l = t.search(/\r\n|\r|\n/);
|
|
1909
|
+
if (l === -1 || t[l] === "\r" && l === t.length - 1) return;
|
|
1910
|
+
const f = t.startsWith(`\r
|
|
1911
|
+
`, l) ? 2 : 1;
|
|
1912
|
+
a(t.slice(0, l)), t = t.slice(l + f);
|
|
1913
|
+
}
|
|
1914
|
+
},
|
|
1915
|
+
/** Nothing half-received: no partial line buffered, no event begun and not yet dispatched. */
|
|
1916
|
+
idle() {
|
|
1917
|
+
return t === "" && e.length === 0 && s === void 0 && n === void 0;
|
|
1918
|
+
},
|
|
1919
|
+
flush() {
|
|
1920
|
+
t !== "" && a(t.replace(/\r$/, "")), t = "";
|
|
1921
|
+
}
|
|
1922
|
+
};
|
|
1923
|
+
}
|
|
1924
|
+
h(me, "createSseParser");
|
|
1925
|
+
function we(i) {
|
|
1926
|
+
return i >= 500 || i === 408 || i === 429;
|
|
1927
|
+
}
|
|
1928
|
+
h(we, "retryable");
|
|
1929
|
+
function Se(i, t) {
|
|
1930
|
+
return new Promise((e) => {
|
|
1931
|
+
if (t != null && t.aborted) return e();
|
|
1932
|
+
const s = setTimeout(n, i);
|
|
1933
|
+
function n() {
|
|
1934
|
+
clearTimeout(s), t == null || t.removeEventListener("abort", n), e();
|
|
1935
|
+
}
|
|
1936
|
+
h(n, "done"), t == null || t.addEventListener("abort", n, { once: !0 });
|
|
1937
|
+
});
|
|
1938
|
+
}
|
|
1939
|
+
h(Se, "sleep");
|
|
1940
|
+
function Ie(i, t, e) {
|
|
1941
|
+
return `${i.replace(/\/+$/, "")}/detached-runs/${encodeURIComponent(t)}/frames?afterSeq=${e}`;
|
|
1942
|
+
}
|
|
1943
|
+
h(Ie, "framesUrl");
|
|
1944
|
+
async function Te(i) {
|
|
1945
|
+
var M, F, v, C, ft;
|
|
1946
|
+
const { baseUrl: t, runId: e, signal: s, onFrame: n, onEnd: r, onOpen: a, onRetry: c } = i, l = i.fetchImpl ?? ((A, $) => fetch(A, $)), f = ((M = i.retry) == null ? void 0 : M.initialMs) ?? 500, p = ((F = i.retry) == null ? void 0 : F.maxMs) ?? 15e3, m = ((v = i.retry) == null ? void 0 : v.maxAttempts) ?? Number.POSITIVE_INFINITY;
|
|
1947
|
+
let u = i.afterSeq ?? 0, g = 0, S = !1;
|
|
1948
|
+
async function D(A) {
|
|
1949
|
+
if (i.onUnauthorized)
|
|
1950
|
+
try {
|
|
1951
|
+
if (await i.onUnauthorized()) return !0;
|
|
1952
|
+
} catch (O) {
|
|
1953
|
+
console.warn(`[DetachedRunFrames] run ${e}: onUnauthorized threw`, O);
|
|
1954
|
+
}
|
|
1955
|
+
if (!i.getToken) return !1;
|
|
1956
|
+
const $ = await i.getToken() ?? void 0;
|
|
1957
|
+
return !!$ && $ !== A;
|
|
1958
|
+
}
|
|
1959
|
+
for (h(D, "secondChance"); !(s != null && s.aborted); ) {
|
|
1960
|
+
let A;
|
|
1961
|
+
try {
|
|
1962
|
+
const O = { Accept: "text/event-stream" }, K = await ((C = i.getToken) == null ? void 0 : C.call(i)) ?? void 0;
|
|
1963
|
+
K && (O.Authorization = `Bearer ${K}`);
|
|
1964
|
+
const k = await l(Ie(t, e, u), { method: "GET", headers: O, signal: s, cache: "no-store" });
|
|
1965
|
+
if (k.status === 401) {
|
|
1966
|
+
if (!S && await D(K)) {
|
|
1967
|
+
S = !0;
|
|
1968
|
+
continue;
|
|
1969
|
+
}
|
|
1970
|
+
throw new L(401);
|
|
1971
|
+
}
|
|
1972
|
+
if (S = !1, !k.ok)
|
|
1973
|
+
throw we(k.status) ? new Error(`GET detached-run frames answered ${k.status}`) : new L(k.status);
|
|
1974
|
+
if (!k.body) throw new L(k.status, "The frames response has no readable body");
|
|
1975
|
+
a == null || a();
|
|
1976
|
+
let W = !1;
|
|
1977
|
+
const B = me((T) => {
|
|
1978
|
+
if (T.event === "end") {
|
|
1979
|
+
const b = Number(T.data);
|
|
1980
|
+
T.data !== "" && Number.isFinite(b) && b > u && (u = b), W = !0;
|
|
1981
|
+
return;
|
|
1982
|
+
}
|
|
1983
|
+
if (T.event !== void 0 && T.event !== "message" || T.data === "") return;
|
|
1984
|
+
const R = T.id !== void 0 && T.id !== "" ? Number(T.id) : void 0;
|
|
1985
|
+
if (R !== void 0 && Number.isFinite(R)) {
|
|
1986
|
+
if (R <= u) return;
|
|
1987
|
+
u = R;
|
|
1988
|
+
}
|
|
1989
|
+
W = !1;
|
|
1990
|
+
let pt;
|
|
1991
|
+
try {
|
|
1992
|
+
pt = JSON.parse(T.data);
|
|
1993
|
+
} catch (b) {
|
|
1994
|
+
console.warn(`[DetachedRunFrames] run ${e}: a frame that is not JSON was skipped (seq ${T.id ?? "?"})`, b);
|
|
1995
|
+
return;
|
|
1996
|
+
}
|
|
1997
|
+
g = 0;
|
|
1998
|
+
try {
|
|
1999
|
+
n(pt, R);
|
|
2000
|
+
} catch (b) {
|
|
2001
|
+
console.error(`[DetachedRunFrames] run ${e}: onFrame threw on seq ${T.id ?? "?"}`, b);
|
|
2002
|
+
}
|
|
2003
|
+
}), G = k.body.getReader(), gt = new TextDecoder();
|
|
2004
|
+
try {
|
|
2005
|
+
for (; ; ) {
|
|
2006
|
+
const { done: T, value: R } = await G.read();
|
|
2007
|
+
if (T) break;
|
|
2008
|
+
B.push(gt.decode(R, { stream: !0 }));
|
|
2009
|
+
}
|
|
2010
|
+
B.push(gt.decode()), B.idle() || (W = !1), B.flush();
|
|
2011
|
+
} finally {
|
|
2012
|
+
(ft = G.releaseLock) == null || ft.call(G);
|
|
2013
|
+
}
|
|
2014
|
+
if (W)
|
|
2015
|
+
return r == null || r(u), { reason: "end", lastSeq: u };
|
|
2016
|
+
A = new Error("The frames stream closed before its end event");
|
|
2017
|
+
} catch (O) {
|
|
2018
|
+
if (s != null && s.aborted) break;
|
|
2019
|
+
if (O instanceof L) throw O;
|
|
2020
|
+
A = O;
|
|
2021
|
+
}
|
|
2022
|
+
if (g += 1, g >= m) throw A;
|
|
2023
|
+
const $ = Math.min(f * 2 ** (g - 1), p);
|
|
2024
|
+
try {
|
|
2025
|
+
c == null || c(A, g, $);
|
|
2026
|
+
} catch {
|
|
2027
|
+
}
|
|
2028
|
+
await Se($, s);
|
|
2029
|
+
}
|
|
2030
|
+
return { reason: "aborted", lastSeq: u };
|
|
2031
|
+
}
|
|
2032
|
+
h(Te, "streamDetachedRunFrames");
|
|
2033
|
+
const dt = class dt {
|
|
2034
|
+
constructor(t) {
|
|
2035
|
+
o(this, "onEvent");
|
|
2036
|
+
o(this, "onError");
|
|
2037
|
+
o(this, "onOpen");
|
|
2038
|
+
o(this, "onFirstToken");
|
|
2039
|
+
o(this, "onSuspended");
|
|
2040
|
+
o(this, "onClose");
|
|
2041
|
+
o(this, "onNewPacket");
|
|
2042
|
+
o(this, "onInteractionStarted");
|
|
2043
|
+
/** The stream of the current follow is over: the server closed it right after an `event: end`. */
|
|
2044
|
+
o(this, "onEnd");
|
|
2045
|
+
/** Following stopped for good — the run is gone, forbidden, the credentials failed twice, or retries ran out. */
|
|
2046
|
+
o(this, "onFollowError");
|
|
2047
|
+
o(this, "options");
|
|
2048
|
+
o(this, "serializer", et());
|
|
2049
|
+
o(this, "firstTokens", /* @__PURE__ */ new Set());
|
|
2050
|
+
o(this, "runId");
|
|
2051
|
+
o(this, "controller", null);
|
|
2052
|
+
o(this, "following", !1);
|
|
2053
|
+
o(this, "lastSeq", 0);
|
|
2054
|
+
this.options = t;
|
|
2055
|
+
}
|
|
2056
|
+
/** The highest seq delivered so far — where the next follow resumes. */
|
|
2057
|
+
get cursor() {
|
|
2058
|
+
return this.lastSeq;
|
|
2059
|
+
}
|
|
2060
|
+
/** Whether a follow is running (false after `end`, an abort or a fatal answer). */
|
|
2061
|
+
get isFollowing() {
|
|
2062
|
+
return this.following;
|
|
2063
|
+
}
|
|
2064
|
+
connect(t, e) {
|
|
2065
|
+
return t ? ((t !== this.runId || !(e != null && e.resume)) && (this.lastSeq = 0, this.firstTokens.clear()), this.runId = t, this.start(t)) : Promise.reject(new Error("DetachedRunFramesTransport: connect() needs the run id"));
|
|
2066
|
+
}
|
|
2067
|
+
/**
|
|
2068
|
+
* Follow again from the last seq — after the stream ended and the run was resumed (answered). A no-op while a
|
|
2069
|
+
* follow is running. Resolves once the server answers with the stream.
|
|
2070
|
+
*/
|
|
2071
|
+
follow() {
|
|
2072
|
+
return this.runId ? this.following ? Promise.resolve(this.runId) : this.start(this.runId) : Promise.reject(new Error("DetachedRunFramesTransport: follow() before connect()"));
|
|
2073
|
+
}
|
|
2074
|
+
start(t) {
|
|
2075
|
+
var s;
|
|
2076
|
+
(s = this.controller) == null || s.abort();
|
|
2077
|
+
const e = new AbortController();
|
|
2078
|
+
return this.controller = e, this.following = !0, new Promise((n, r) => {
|
|
2079
|
+
let a = !1;
|
|
2080
|
+
Te({
|
|
2081
|
+
...this.options,
|
|
2082
|
+
runId: t,
|
|
2083
|
+
afterSeq: this.lastSeq,
|
|
2084
|
+
signal: e.signal,
|
|
2085
|
+
onOpen: /* @__PURE__ */ h(() => {
|
|
2086
|
+
var c;
|
|
2087
|
+
a || (a = !0, (c = this.onOpen) == null || c.call(this), n(t));
|
|
2088
|
+
}, "onOpen"),
|
|
2089
|
+
onFrame: /* @__PURE__ */ h((c, l) => {
|
|
2090
|
+
typeof l == "number" && l > this.lastSeq && (this.lastSeq = l), this.dispatch(c);
|
|
2091
|
+
}, "onFrame"),
|
|
2092
|
+
onEnd: /* @__PURE__ */ h((c) => {
|
|
2093
|
+
var l;
|
|
2094
|
+
this.lastSeq = Math.max(this.lastSeq, c), (l = this.onEnd) == null || l.call(this, this.lastSeq);
|
|
2095
|
+
}, "onEnd"),
|
|
2096
|
+
onRetry: /* @__PURE__ */ h((c, l, f) => {
|
|
2097
|
+
console.warn(`[DetachedRunFrames] run ${t}: the stream failed (attempt ${l}); following again from seq ${this.lastSeq} in ${f}ms.`, c);
|
|
2098
|
+
}, "onRetry")
|
|
2099
|
+
}).then(() => {
|
|
2100
|
+
a || r(new Error("The frames stream stopped before it opened"));
|
|
2101
|
+
}).catch((c) => {
|
|
2102
|
+
var p, m, u;
|
|
2103
|
+
const l = pe(c), f = l.kind === "notFound" ? kt : l.kind === "forbidden" || l.kind === "unauthorized" ? 1008 : 1011;
|
|
2104
|
+
console.error(`[DetachedRunFrames] run ${t}: following stopped (${l.kind})`, c);
|
|
2105
|
+
try {
|
|
2106
|
+
(p = this.onFollowError) == null || p.call(this, l);
|
|
2107
|
+
} catch (g) {
|
|
2108
|
+
console.error("[DetachedRunFrames] onFollowError threw", g);
|
|
2109
|
+
}
|
|
2110
|
+
(m = this.onError) == null || m.call(this, String((c == null ? void 0 : c.message) ?? c)), (u = this.onClose) == null || u.call(this, f, String((c == null ? void 0 : c.message) ?? "")), a || r(c);
|
|
2111
|
+
}).finally(() => {
|
|
2112
|
+
this.controller === e && (this.following = !1);
|
|
2113
|
+
});
|
|
2114
|
+
});
|
|
2115
|
+
}
|
|
2116
|
+
/** One frame, decoded exactly as the chat socket decodes it. A frame that fails to decode is skipped. */
|
|
2117
|
+
dispatch(t) {
|
|
2118
|
+
var r, a, c;
|
|
2119
|
+
let e;
|
|
2120
|
+
try {
|
|
2121
|
+
e = this.serializer.parse(t);
|
|
2122
|
+
} catch (l) {
|
|
2123
|
+
console.warn("[DetachedRunFrames] a frame failed to decode", l);
|
|
2124
|
+
return;
|
|
2125
|
+
}
|
|
2126
|
+
if (!e) return;
|
|
2127
|
+
const s = $t(e);
|
|
2128
|
+
if (s) {
|
|
2129
|
+
(r = this.onInteractionStarted) == null || r.call(this, s);
|
|
2130
|
+
return;
|
|
2131
|
+
}
|
|
2132
|
+
const n = st(e);
|
|
2133
|
+
if (n) {
|
|
2134
|
+
Dt(n) && n.interactionId && !this.firstTokens.has(n.interactionId) && (this.firstTokens.add(n.interactionId), (a = this.onFirstToken) == null || a.call(this, n.interactionId));
|
|
2135
|
+
try {
|
|
2136
|
+
const l = x.parseFromTypedEvent(n);
|
|
2137
|
+
(c = this.onEvent) == null || c.call(this, l).catch((f) => console.error("[DetachedRunFrames] onEvent failed", f));
|
|
2138
|
+
} catch (l) {
|
|
2139
|
+
console.error("[DetachedRunFrames] a frame could not be mapped to a server event", l);
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
sendMessage(t) {
|
|
2144
|
+
throw new Error("A detached-run transcript is read-only: answer the run through its own endpoint.");
|
|
2145
|
+
}
|
|
2146
|
+
suspendStream() {
|
|
2147
|
+
}
|
|
2148
|
+
closeStream() {
|
|
2149
|
+
var t;
|
|
2150
|
+
(t = this.controller) == null || t.abort(), this.controller = null, this.following = !1;
|
|
2151
|
+
}
|
|
2152
|
+
isConnected() {
|
|
2153
|
+
return this.controller !== null;
|
|
2154
|
+
}
|
|
2155
|
+
};
|
|
2156
|
+
h(dt, "DetachedRunFramesTransport");
|
|
2157
|
+
let Mt = dt;
|
|
2158
|
+
const ye = "todolistUpdated";
|
|
2159
|
+
function Re(i, t = et()) {
|
|
2160
|
+
var n, r;
|
|
2161
|
+
let e;
|
|
2162
|
+
try {
|
|
2163
|
+
const a = t.parse(i), c = a ? st(a) : void 0;
|
|
2164
|
+
e = c ? x.parseFromTypedEvent(c) : void 0;
|
|
2165
|
+
} catch {
|
|
2166
|
+
return [];
|
|
2167
|
+
}
|
|
2168
|
+
if (!e) return [];
|
|
2169
|
+
const s = [];
|
|
2170
|
+
for (const a of e.parts ?? [])
|
|
2171
|
+
if (a.eventType === I.ToolEvent && ((n = a.toolDetails) == null ? void 0 : n.eventName) === ye) {
|
|
2172
|
+
const c = ve(a.toolDetails.eventData);
|
|
2173
|
+
c && s.push({ kind: "todo", todoList: c });
|
|
2174
|
+
} else (a.eventType === I.FunctionCall || a.eventType === I.PartialFunctionCall) && ((r = a.functionDetails) != null && r.name) ? s.push({ kind: "tool", name: a.functionDetails.name, interactionId: e.interactionId }) : a.eventType === I.Text && a.text && s.push({ kind: "text", text: a.text, partial: !!e.partial, interactionId: e.interactionId });
|
|
2175
|
+
return s;
|
|
2176
|
+
}
|
|
2177
|
+
h(Re, "readDetachedRunActivity");
|
|
2178
|
+
function ve(i) {
|
|
2179
|
+
if (!i) return null;
|
|
2180
|
+
try {
|
|
2181
|
+
const t = JSON.parse(i);
|
|
2182
|
+
return t && Array.isArray(t.items) ? t : null;
|
|
2183
|
+
} catch {
|
|
2184
|
+
return null;
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
h(ve, "parseTodoList");
|
|
1730
2188
|
export {
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
2189
|
+
Tt as A,
|
|
2190
|
+
V as C,
|
|
2191
|
+
L as D,
|
|
2192
|
+
It as E,
|
|
2193
|
+
Ct as H,
|
|
2194
|
+
_ as I,
|
|
2195
|
+
St as L,
|
|
2196
|
+
Ht as M,
|
|
2197
|
+
ye as T,
|
|
2198
|
+
oe as W,
|
|
2199
|
+
Mt as a,
|
|
2200
|
+
j as b,
|
|
2201
|
+
Et as c,
|
|
2202
|
+
me as d,
|
|
2203
|
+
te as e,
|
|
2204
|
+
Re as r,
|
|
2205
|
+
Te as s,
|
|
2206
|
+
pe as t,
|
|
2207
|
+
$e as u
|
|
1743
2208
|
};
|
|
1744
|
-
//# sourceMappingURL=
|
|
2209
|
+
//# sourceMappingURL=detachedRunActivity-RNg1JDDW.js.map
|