@copilotkit/core 1.62.0 → 1.62.2
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/dist/index.cjs +144 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +38 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +38 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +146 -43
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +153 -46
- package/dist/index.umd.js.map +1 -1
- package/package.json +2 -2
package/dist/index.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function(global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tanstack/pacer'), require('@ag-ui/client'), require('@copilotkit/shared'), require('rxjs'), require('rxjs/operators'), require('phoenix'), require('zod-to-json-schema')
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', '@tanstack/pacer', '@ag-ui/client', '@copilotkit/shared', 'rxjs', 'rxjs/operators', 'phoenix', 'zod-to-json-schema'
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.CopilotKitCore = {}), global._tanstack_pacer,global.AgUIClient,global.CopilotKitShared,global.rxjs,global.rxjs.operators,global.phoenix,global.zodToJsonSchema
|
|
5
|
-
})(this, function(exports, _tanstack_pacer, _ag_ui_client, _copilotkit_shared, rxjs, rxjs_operators, phoenix, zod_to_json_schema
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tanstack/pacer'), require('@ag-ui/client'), require('@copilotkit/shared'), require('rxjs'), require('rxjs/operators'), require('phoenix'), require('zod-to-json-schema')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@tanstack/pacer', '@ag-ui/client', '@copilotkit/shared', 'rxjs', 'rxjs/operators', 'phoenix', 'zod-to-json-schema'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.CopilotKitCore = {}), global._tanstack_pacer,global.AgUIClient,global.CopilotKitShared,global.rxjs,global.rxjs.operators,global.phoenix,global.zodToJsonSchema));
|
|
5
|
+
})(this, function(exports, _tanstack_pacer, _ag_ui_client, _copilotkit_shared, rxjs, rxjs_operators, phoenix, zod_to_json_schema) {
|
|
6
6
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
7
7
|
|
|
8
8
|
//#region src/utils/phoenix-observable.ts
|
|
@@ -170,6 +170,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
170
170
|
const REPLAY_COMPLETE_EVENT = "replay_complete";
|
|
171
171
|
const STREAM_IDLE_EVENT = "stream_idle";
|
|
172
172
|
const STOP_RUN_EVENT = "stop_run";
|
|
173
|
+
const CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS = 100;
|
|
173
174
|
var AgentThreadLockedError = class extends Error {
|
|
174
175
|
constructor(threadId) {
|
|
175
176
|
super(threadId ? `Thread ${threadId} is locked` : "Thread is locked");
|
|
@@ -320,12 +321,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
320
321
|
connect(input) {
|
|
321
322
|
this.threadId = input.threadId;
|
|
322
323
|
this.canonicalRunId = null;
|
|
323
|
-
|
|
324
|
+
const replayCursor = this.getReconnectCursor(input);
|
|
325
|
+
return (0, rxjs.defer)(() => this.requestJoinCredentials$("connect", input, replayCursor)).pipe((0, rxjs.switchMap)((credentials) => {
|
|
324
326
|
if (credentials === null) return rxjs.EMPTY;
|
|
325
327
|
const canonicalInput = this.applyCanonicalRunIdentity(input, credentials, { fallbackToInputRunId: false });
|
|
326
328
|
return this.observeThread$(canonicalInput, credentials, {
|
|
327
329
|
completeOnRunError: false,
|
|
328
|
-
streamMode: "connect"
|
|
330
|
+
streamMode: "connect",
|
|
331
|
+
replayCursor
|
|
329
332
|
});
|
|
330
333
|
}));
|
|
331
334
|
}
|
|
@@ -348,7 +351,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
348
351
|
cleanup() {
|
|
349
352
|
this.cleanupOwned(this.activeChannel, this.socket);
|
|
350
353
|
}
|
|
351
|
-
requestJoinCredentials$(mode, input) {
|
|
354
|
+
requestJoinCredentials$(mode, input, replayCursor) {
|
|
352
355
|
return (0, rxjs.defer)(async () => {
|
|
353
356
|
try {
|
|
354
357
|
const response = await fetch(this.buildRuntimeUrl(mode), {
|
|
@@ -365,7 +368,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
365
368
|
context: input.context,
|
|
366
369
|
state: input.state,
|
|
367
370
|
forwardedProps: input.forwardedProps,
|
|
368
|
-
...mode === "connect" ? { lastSeenEventId: this.getReconnectCursor(input) } : {}
|
|
371
|
+
...mode === "connect" ? { lastSeenEventId: replayCursor === void 0 ? this.getReconnectCursor(input) : replayCursor } : {}
|
|
369
372
|
}),
|
|
370
373
|
...this.config.credentials ? { credentials: this.config.credentials } : {}
|
|
371
374
|
});
|
|
@@ -401,16 +404,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
401
404
|
observeThread$(input, credentials, options) {
|
|
402
405
|
return this.observeThreadSession$(input, credentials, options).pipe((0, rxjs_operators.catchError)((error) => {
|
|
403
406
|
if (!this.isSocketReconnectExhaustedError(error)) return (0, rxjs.throwError)(() => error);
|
|
404
|
-
|
|
407
|
+
const replayCursor = this.getReconnectCursor(input);
|
|
408
|
+
return this.requestJoinCredentials$("connect", input, replayCursor).pipe((0, rxjs.switchMap)((refreshedCredentials) => refreshedCredentials === null ? rxjs.EMPTY : this.observeThread$(this.applyCanonicalRunIdentity(input, refreshedCredentials, { fallbackToInputRunId: options.streamMode === "run" }), refreshedCredentials, {
|
|
405
409
|
...options,
|
|
406
410
|
channelMode: "connect",
|
|
407
|
-
replayCursor
|
|
411
|
+
replayCursor
|
|
408
412
|
})));
|
|
409
413
|
}));
|
|
410
414
|
}
|
|
411
415
|
observeThreadSession$(input, credentials, options) {
|
|
412
416
|
return (0, rxjs.defer)(() => {
|
|
413
|
-
var _options$channelMode;
|
|
417
|
+
var _options$channelMode, _options$replayCursor;
|
|
414
418
|
let ownSocket = null;
|
|
415
419
|
let ownChannel = null;
|
|
416
420
|
const socket$ = ɵphoenixSocket$({
|
|
@@ -442,15 +446,24 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
442
446
|
bufferSize: 1,
|
|
443
447
|
refCount: true
|
|
444
448
|
}));
|
|
445
|
-
const
|
|
446
|
-
|
|
449
|
+
const reconnectCursor = this.readDurableEventId((_options$replayCursor = options.replayCursor) !== null && _options$replayCursor !== void 0 ? _options$replayCursor : this.getReconnectCursor(input));
|
|
450
|
+
let latestObservedReplayCursor = null;
|
|
451
|
+
const threadEvents$ = this.observeThreadEvents$(input.threadId, channel$, options).pipe((0, rxjs_operators.tap)((payload) => {
|
|
452
|
+
var _this$readEventId;
|
|
453
|
+
latestObservedReplayCursor = (_this$readEventId = this.readEventId(payload)) !== null && _this$readEventId !== void 0 ? _this$readEventId : latestObservedReplayCursor;
|
|
454
|
+
}), (0, rxjs_operators.share)());
|
|
455
|
+
const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT).pipe((0, rxjs_operators.shareReplay)({
|
|
456
|
+
bufferSize: 1,
|
|
457
|
+
refCount: true
|
|
458
|
+
}));
|
|
447
459
|
const streamIdle$ = this.observeControlEvent$(input.threadId, channel$, STREAM_IDLE_EVENT).pipe((0, rxjs_operators.shareReplay)({
|
|
448
460
|
bufferSize: 1,
|
|
449
461
|
refCount: true
|
|
450
462
|
}));
|
|
451
|
-
const streamIdleCompletion$ = options.streamMode === "connect" ? streamIdle$.pipe((0, rxjs_operators.take)(1)) : rxjs.EMPTY;
|
|
452
|
-
const
|
|
453
|
-
|
|
463
|
+
const streamIdleCompletion$ = options.streamMode === "connect" ? (0, rxjs.merge)((0, rxjs.combineLatest)([replayComplete$.pipe((0, rxjs_operators.take)(1)), streamIdle$.pipe((0, rxjs_operators.take)(1))]), streamIdle$.pipe((0, rxjs_operators.take)(1), (0, rxjs_operators.filter)((payload) => this.canFallbackCompleteConnect(payload, reconnectCursor, latestObservedReplayCursor)), (0, rxjs_operators.delay)(CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS))).pipe((0, rxjs_operators.take)(1)) : rxjs.EMPTY;
|
|
464
|
+
const threadCompleted$ = threadEvents$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.endWith)(null), (0, rxjs_operators.take)(1));
|
|
465
|
+
const terminal$ = (0, rxjs.merge)(threadCompleted$, streamIdleCompletion$);
|
|
466
|
+
return (0, rxjs.merge)(this.joinThreadChannel$(channel$), this.observeSocketHealth$(socket$).pipe((0, rxjs_operators.takeUntil)(terminal$)), threadEvents$.pipe((0, rxjs_operators.takeUntil)(streamIdleCompletion$)), replayComplete$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(terminal$)), streamIdleCompletion$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(threadCompleted$))).pipe((0, rxjs_operators.finalize)(() => this.cleanupOwned(ownChannel, ownSocket)));
|
|
454
467
|
});
|
|
455
468
|
}
|
|
456
469
|
joinThreadChannel$(channel$) {
|
|
@@ -518,23 +531,37 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
518
531
|
updateLastSeenEventId(threadId, payload) {
|
|
519
532
|
const eventId = this.readEventId(payload);
|
|
520
533
|
if (!eventId) return;
|
|
521
|
-
this.
|
|
534
|
+
this.advanceLastSeenEventId(threadId, eventId);
|
|
522
535
|
}
|
|
523
536
|
updateLastSeenEventIdFromControl(threadId, payload) {
|
|
524
537
|
const eventId = this.readControlEventId(payload);
|
|
525
538
|
if (!eventId) return;
|
|
539
|
+
this.advanceLastSeenEventId(threadId, eventId);
|
|
540
|
+
}
|
|
541
|
+
advanceLastSeenEventId(threadId, eventId) {
|
|
526
542
|
this.sharedState.lastSeenEventIds.set(threadId, eventId);
|
|
527
543
|
}
|
|
528
544
|
readEventId(payload) {
|
|
529
545
|
const metadata = payload.metadata;
|
|
530
546
|
if (!metadata || typeof metadata !== "object") return null;
|
|
531
|
-
const
|
|
532
|
-
return
|
|
547
|
+
const eventMetadata = metadata;
|
|
548
|
+
return this.readDurableEventId(eventMetadata.cpki_event_id);
|
|
533
549
|
}
|
|
534
550
|
readControlEventId(payload) {
|
|
551
|
+
var _controlPayload$lates;
|
|
535
552
|
if (!payload || typeof payload !== "object") return null;
|
|
536
|
-
const
|
|
537
|
-
|
|
553
|
+
const controlPayload = payload;
|
|
554
|
+
const latestEventId = (_controlPayload$lates = controlPayload.latestEventId) !== null && _controlPayload$lates !== void 0 ? _controlPayload$lates : controlPayload.latest_event_id;
|
|
555
|
+
return this.readDurableEventId(latestEventId);
|
|
556
|
+
}
|
|
557
|
+
readDurableEventId(eventId) {
|
|
558
|
+
if (typeof eventId !== "string" || eventId.trim() === "") return null;
|
|
559
|
+
return eventId.startsWith("cpki_ingested") ? null : eventId;
|
|
560
|
+
}
|
|
561
|
+
canFallbackCompleteConnect(streamIdlePayload, reconnectCursor, latestObservedReplayCursor) {
|
|
562
|
+
const idleCursor = this.readControlEventId(streamIdlePayload);
|
|
563
|
+
if (!idleCursor) return true;
|
|
564
|
+
return idleCursor === reconnectCursor || idleCursor === latestObservedReplayCursor;
|
|
538
565
|
}
|
|
539
566
|
applyCanonicalRunIdentity(input, credentials, options) {
|
|
540
567
|
var _credentials$runId;
|
|
@@ -1769,7 +1796,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1769
1796
|
_defineProperty(this, "_tools", []);
|
|
1770
1797
|
_defineProperty(this, "_runAbortController", null);
|
|
1771
1798
|
_defineProperty(this, "_runDepth", 0);
|
|
1772
|
-
_defineProperty(this, "
|
|
1799
|
+
_defineProperty(this, "_lastConnectedThreadIdsByAgent", /* @__PURE__ */ new Map());
|
|
1800
|
+
_defineProperty(this, "_anonymousAgentIds", /* @__PURE__ */ new WeakMap());
|
|
1801
|
+
_defineProperty(this, "_nextAnonymousAgentId", 0);
|
|
1773
1802
|
}
|
|
1774
1803
|
/**
|
|
1775
1804
|
* Abort the current run. Called by `CopilotKitCore.stopAgent()` to signal
|
|
@@ -1788,6 +1817,20 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1788
1817
|
return this.core;
|
|
1789
1818
|
}
|
|
1790
1819
|
/**
|
|
1820
|
+
* Return a stable restore-tracking key for the logical agent being
|
|
1821
|
+
* connected. Named agents share their last-thread marker across proxy
|
|
1822
|
+
* instances; anonymous agents fall back to object identity.
|
|
1823
|
+
*/
|
|
1824
|
+
getConnectRestoreKey(agent) {
|
|
1825
|
+
if (agent.agentId) return `agent:${agent.agentId}`;
|
|
1826
|
+
const existing = this._anonymousAgentIds.get(agent);
|
|
1827
|
+
if (existing) return existing;
|
|
1828
|
+
const anonymousId = `anonymous:${this._nextAnonymousAgentId}`;
|
|
1829
|
+
this._nextAnonymousAgentId += 1;
|
|
1830
|
+
this._anonymousAgentIds.set(agent, anonymousId);
|
|
1831
|
+
return anonymousId;
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1791
1834
|
* Get all tools as a readonly array
|
|
1792
1835
|
*/
|
|
1793
1836
|
get tools() {
|
|
@@ -1842,16 +1885,20 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1842
1885
|
*/
|
|
1843
1886
|
async connectAgent({ agent }) {
|
|
1844
1887
|
try {
|
|
1845
|
-
var _agent$threadId;
|
|
1888
|
+
var _agent$threadId, _this$_lastConnectedT;
|
|
1846
1889
|
const incomingThreadId = (_agent$threadId = agent.threadId) !== null && _agent$threadId !== void 0 ? _agent$threadId : null;
|
|
1847
|
-
const
|
|
1848
|
-
this.
|
|
1890
|
+
const restoreKey = this.getConnectRestoreKey(agent);
|
|
1891
|
+
const isFreshRestore = incomingThreadId !== ((_this$_lastConnectedT = this._lastConnectedThreadIdsByAgent.get(restoreKey)) !== null && _this$_lastConnectedT !== void 0 ? _this$_lastConnectedT : null);
|
|
1892
|
+
this._lastConnectedThreadIdsByAgent.set(restoreKey, incomingThreadId);
|
|
1849
1893
|
await agent.detachActiveRun();
|
|
1850
1894
|
if (isFreshRestore) {
|
|
1851
1895
|
agent.setMessages([]);
|
|
1852
1896
|
agent.setState({});
|
|
1853
|
-
const
|
|
1854
|
-
if (incomingThreadId
|
|
1897
|
+
const cursorAware = agent;
|
|
1898
|
+
if (incomingThreadId) {
|
|
1899
|
+
if (typeof cursorAware.clearReplayCursor === "function") cursorAware.clearReplayCursor(incomingThreadId);
|
|
1900
|
+
if (typeof cursorAware.clearReconnectCursor === "function") cursorAware.clearReconnectCursor(incomingThreadId);
|
|
1901
|
+
}
|
|
1855
1902
|
}
|
|
1856
1903
|
this._internal.applyHeadersToAgent(agent);
|
|
1857
1904
|
await this._internal.notifySubscribers((subscriber) => {
|
|
@@ -1868,7 +1915,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1868
1915
|
}, this.createAgentErrorSubscriber(agent));
|
|
1869
1916
|
return this.processAgentResult({
|
|
1870
1917
|
runAgentResult,
|
|
1871
|
-
agent
|
|
1918
|
+
agent,
|
|
1919
|
+
executeFrontendTools: false
|
|
1872
1920
|
});
|
|
1873
1921
|
} catch (error) {
|
|
1874
1922
|
const connectError = error instanceof Error ? error : new Error(String(error));
|
|
@@ -1890,7 +1938,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1890
1938
|
/**
|
|
1891
1939
|
* Run an agent
|
|
1892
1940
|
*/
|
|
1893
|
-
async runAgent({ agent, forwardedProps, resume }) {
|
|
1941
|
+
async runAgent({ agent, forwardedProps, resume, runId }) {
|
|
1894
1942
|
if (agent.agentId) this._internal.suggestionEngine.clearSuggestions(agent.agentId);
|
|
1895
1943
|
this._internal.applyHeadersToAgent(agent);
|
|
1896
1944
|
if (agent.detachActiveRun) await agent.detachActiveRun();
|
|
@@ -1920,6 +1968,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1920
1968
|
...forwardedProps
|
|
1921
1969
|
},
|
|
1922
1970
|
...resume !== void 0 ? { resume } : {},
|
|
1971
|
+
...runId !== void 0 ? { runId } : {},
|
|
1923
1972
|
tools: this.buildFrontendTools(agent.agentId),
|
|
1924
1973
|
context: this._internal.getContextForAgent(agent.agentId)
|
|
1925
1974
|
}, this.createAgentErrorSubscriber(agent));
|
|
@@ -1948,26 +1997,28 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1948
1997
|
/**
|
|
1949
1998
|
* Process agent result and execute tools
|
|
1950
1999
|
*/
|
|
1951
|
-
async processAgentResult({ runAgentResult, agent }) {
|
|
2000
|
+
async processAgentResult({ runAgentResult, agent, executeFrontendTools = true }) {
|
|
1952
2001
|
var _this$_runAbortContro2;
|
|
1953
2002
|
const { newMessages } = runAgentResult;
|
|
1954
2003
|
const agentId = agent.agentId;
|
|
1955
2004
|
let needsFollowUp = false;
|
|
1956
|
-
|
|
1957
|
-
for (const
|
|
1958
|
-
const tool
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
});
|
|
1962
|
-
if (tool) {
|
|
1963
|
-
if (await this.executeSpecificTool(tool, toolCall, message, agent, agentId)) needsFollowUp = true;
|
|
1964
|
-
} else {
|
|
1965
|
-
const wildcardTool = this.getTool({
|
|
1966
|
-
toolName: "*",
|
|
2005
|
+
if (executeFrontendTools) {
|
|
2006
|
+
for (const message of newMessages) if (message.role === "assistant") {
|
|
2007
|
+
for (const toolCall of message.toolCalls || []) if (newMessages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id) === -1) {
|
|
2008
|
+
const tool = this.getTool({
|
|
2009
|
+
toolName: toolCall.function.name,
|
|
1967
2010
|
agentId: agent.agentId
|
|
1968
2011
|
});
|
|
1969
|
-
if (
|
|
1970
|
-
if (await this.
|
|
2012
|
+
if (tool) {
|
|
2013
|
+
if (await this.executeSpecificTool(tool, toolCall, message, agent, agentId)) needsFollowUp = true;
|
|
2014
|
+
} else {
|
|
2015
|
+
const wildcardTool = this.getTool({
|
|
2016
|
+
toolName: "*",
|
|
2017
|
+
agentId: agent.agentId
|
|
2018
|
+
});
|
|
2019
|
+
if (wildcardTool) {
|
|
2020
|
+
if (await this.executeWildcardTool(wildcardTool, toolCall, message, agent, agentId)) needsFollowUp = true;
|
|
2021
|
+
}
|
|
1971
2022
|
}
|
|
1972
2023
|
}
|
|
1973
2024
|
}
|
|
@@ -3739,6 +3790,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3739
3790
|
//#endregion
|
|
3740
3791
|
//#region src/threads.ts
|
|
3741
3792
|
const THREADS_CHANNEL_EVENT = "thread_metadata";
|
|
3793
|
+
const THREAD_RUN_ACTIVITY_CHANNEL_EVENT = "thread_run_activity";
|
|
3742
3794
|
const THREAD_SUBSCRIBE_PATH = "/threads/subscribe";
|
|
3743
3795
|
const MAX_SOCKET_RETRIES = 5;
|
|
3744
3796
|
const REQUEST_TIMEOUT_MS = 15e3;
|
|
@@ -3782,7 +3834,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3782
3834
|
errored: props(),
|
|
3783
3835
|
joinFailed: props(),
|
|
3784
3836
|
joinTimedOut: props(),
|
|
3785
|
-
metadataReceived: props()
|
|
3837
|
+
metadataReceived: props(),
|
|
3838
|
+
runActivityReceived: props()
|
|
3786
3839
|
});
|
|
3787
3840
|
const threadDomainEvents = createActionGroup("Thread Domain", {
|
|
3788
3841
|
threadUpserted: props(),
|
|
@@ -3802,6 +3855,29 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3802
3855
|
next[existingIndex] = thread;
|
|
3803
3856
|
return sortThreadsByRecency(next);
|
|
3804
3857
|
}
|
|
3858
|
+
/**
|
|
3859
|
+
* Returns a non-empty string payload field or undefined for absent fields.
|
|
3860
|
+
*/
|
|
3861
|
+
function optionalString(value) {
|
|
3862
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
3863
|
+
}
|
|
3864
|
+
/**
|
|
3865
|
+
* Converts gateway run-activity payloads into the internal TypeScript shape.
|
|
3866
|
+
*/
|
|
3867
|
+
function normalizeThreadRunActivityNotification(payload) {
|
|
3868
|
+
var _payload$threadId, _payload$eventType, _payload$agentId, _payload$runId, _payload$latestEventI;
|
|
3869
|
+
const threadId = optionalString((_payload$threadId = payload.threadId) !== null && _payload$threadId !== void 0 ? _payload$threadId : payload.thread_id);
|
|
3870
|
+
const eventType = optionalString((_payload$eventType = payload.eventType) !== null && _payload$eventType !== void 0 ? _payload$eventType : payload.event_type);
|
|
3871
|
+
if (!threadId || !eventType) return null;
|
|
3872
|
+
return {
|
|
3873
|
+
type: "thread_run_activity",
|
|
3874
|
+
threadId,
|
|
3875
|
+
agentId: optionalString((_payload$agentId = payload.agentId) !== null && _payload$agentId !== void 0 ? _payload$agentId : payload.agent_id),
|
|
3876
|
+
runId: optionalString((_payload$runId = payload.runId) !== null && _payload$runId !== void 0 ? _payload$runId : payload.run_id),
|
|
3877
|
+
eventType,
|
|
3878
|
+
latestEventId: optionalString((_payload$latestEventI = payload.latestEventId) !== null && _payload$latestEventI !== void 0 ? _payload$latestEventI : payload.latest_event_id)
|
|
3879
|
+
};
|
|
3880
|
+
}
|
|
3805
3881
|
const threadReducer = createReducer(initialThreadState, on(threadAdapterEvents.contextChanged, (state, { context }) => ({
|
|
3806
3882
|
...state,
|
|
3807
3883
|
context,
|
|
@@ -3835,12 +3911,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3835
3911
|
};
|
|
3836
3912
|
}), on(threadRestEvents.listSucceeded, (state, { sessionId, threads, joinCode, nextCursor }) => {
|
|
3837
3913
|
if (sessionId !== state.sessionId) return state;
|
|
3914
|
+
const joinCodeChanged = joinCode !== state.metadataJoinCode;
|
|
3838
3915
|
return {
|
|
3839
3916
|
...state,
|
|
3840
3917
|
threads: sortThreadsByRecency(threads),
|
|
3841
3918
|
isLoading: false,
|
|
3842
3919
|
error: null,
|
|
3843
3920
|
metadataJoinCode: joinCode,
|
|
3921
|
+
metadataCredentialsRequested: joinCodeChanged ? false : state.metadataCredentialsRequested,
|
|
3844
3922
|
nextCursor
|
|
3845
3923
|
};
|
|
3846
3924
|
}), on(threadRestEvents.listFailed, (state, { sessionId, error }) => {
|
|
@@ -3869,7 +3947,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3869
3947
|
};
|
|
3870
3948
|
}), on(threadRestEvents.metadataCredentialsFailed, (state, { sessionId }) => {
|
|
3871
3949
|
if (sessionId !== state.sessionId) return state;
|
|
3872
|
-
return
|
|
3950
|
+
return {
|
|
3951
|
+
...state,
|
|
3952
|
+
metadataCredentialsRequested: false
|
|
3953
|
+
};
|
|
3873
3954
|
}), on(threadRestEvents.metadataCredentialsRequested, (state, { sessionId }) => {
|
|
3874
3955
|
if (sessionId !== state.sessionId) return state;
|
|
3875
3956
|
return {
|
|
@@ -4028,7 +4109,27 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4028
4109
|
return `thread-request-${threadRequestId}`;
|
|
4029
4110
|
}
|
|
4030
4111
|
function threadFromFetch(input, init) {
|
|
4031
|
-
return
|
|
4112
|
+
return new rxjs.Observable((subscriber) => {
|
|
4113
|
+
const { fetch: fetchImpl, selector, signal, ...requestInit } = init;
|
|
4114
|
+
const controller = new AbortController();
|
|
4115
|
+
const abortRequest = () => controller.abort();
|
|
4116
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) abortRequest();
|
|
4117
|
+
else signal === null || signal === void 0 || signal.addEventListener("abort", abortRequest, { once: true });
|
|
4118
|
+
fetchImpl(input, {
|
|
4119
|
+
...requestInit,
|
|
4120
|
+
signal: controller.signal
|
|
4121
|
+
}).then((response) => selector(response)).then((value) => {
|
|
4122
|
+
if (subscriber.closed) return;
|
|
4123
|
+
subscriber.next(value);
|
|
4124
|
+
subscriber.complete();
|
|
4125
|
+
}).catch((error) => {
|
|
4126
|
+
if (!subscriber.closed) subscriber.error(error);
|
|
4127
|
+
});
|
|
4128
|
+
return () => {
|
|
4129
|
+
signal === null || signal === void 0 || signal.removeEventListener("abort", abortRequest);
|
|
4130
|
+
abortRequest();
|
|
4131
|
+
};
|
|
4132
|
+
});
|
|
4032
4133
|
}
|
|
4033
4134
|
function createThreadFetchObservable(environment, context, sessionId) {
|
|
4034
4135
|
return (0, rxjs.defer)(() => {
|
|
@@ -4179,6 +4280,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4179
4280
|
return (0, rxjs.merge)(socketSignals$.pipe((0, rxjs_operators.map)((signal) => signal.type === "open" ? threadSocketEvents.opened({ sessionId: action.sessionId }) : threadSocketEvents.errored({ sessionId: action.sessionId }))), channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => ɵobservePhoenixEvent$(channel, THREADS_CHANNEL_EVENT)), (0, rxjs_operators.map)((payload) => threadSocketEvents.metadataReceived({
|
|
4180
4281
|
sessionId: action.sessionId,
|
|
4181
4282
|
payload
|
|
4283
|
+
}))), channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => ɵobservePhoenixEvent$(channel, THREAD_RUN_ACTIVITY_CHANNEL_EVENT)), (0, rxjs_operators.map)((payload) => normalizeThreadRunActivityNotification(payload)), (0, rxjs_operators.filter)((notification) => notification !== null), (0, rxjs_operators.map)((notification) => threadSocketEvents.runActivityReceived({
|
|
4284
|
+
sessionId: action.sessionId,
|
|
4285
|
+
notification
|
|
4182
4286
|
}))), ɵobservePhoenixJoinOutcome$(channel$).pipe((0, rxjs_operators.filter)((outcome) => outcome.type !== "joined"), (0, rxjs_operators.map)((outcome) => outcome.type === "timeout" ? threadSocketEvents.joinTimedOut({ sessionId: action.sessionId }) : threadSocketEvents.joinFailed({ sessionId: action.sessionId })))).pipe((0, rxjs_operators.takeUntil)((0, rxjs.merge)(shutdown$, fatalSocketShutdown$)));
|
|
4183
4287
|
});
|
|
4184
4288
|
}))),
|
|
@@ -4363,6 +4467,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4363
4467
|
threadId
|
|
4364
4468
|
}));
|
|
4365
4469
|
},
|
|
4470
|
+
subscribeToRunActivity(callback) {
|
|
4471
|
+
return store.actions$.pipe(ofType(threadSocketEvents.runActivityReceived), (0, rxjs_operators.filter)((action) => action.sessionId === store.getState().sessionId), (0, rxjs_operators.map)((action) => action.notification)).subscribe(callback);
|
|
4472
|
+
},
|
|
4366
4473
|
getState() {
|
|
4367
4474
|
return store.getState();
|
|
4368
4475
|
},
|