@copilotkit/core 1.63.0 → 1.63.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 CHANGED
@@ -695,10 +695,12 @@ var ProxiedCopilotRuntimeAgent = class ProxiedCopilotRuntimeAgent extends _ag_ui
695
695
  }
696
696
  }
697
697
  connect(input) {
698
+ if (this.runtimeMode === "pending" || this.transport === "auto" && this.runtimeMode !== _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return (0, rxjs.defer)(() => (0, rxjs.from)(this.ensureRuntimeConfiguration())).pipe((0, rxjs_operators.switchMap)(() => this.connect(input)));
698
699
  if (this.runtimeMode === _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return this.#connectViaDelegate(input);
699
700
  return this.#connectViaHttp(input);
700
701
  }
701
702
  run(input) {
703
+ if (this.runtimeMode === "pending" || this.transport === "auto" && this.runtimeMode !== _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return (0, rxjs.defer)(() => (0, rxjs.from)(this.ensureRuntimeConfiguration())).pipe((0, rxjs_operators.switchMap)(() => this.run(input)));
702
704
  if (this.runtimeMode === _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return this.#runViaDelegate(input);
703
705
  return this.#runViaHttp(input);
704
706
  }
@@ -768,7 +770,7 @@ var ProxiedCopilotRuntimeAgent = class ProxiedCopilotRuntimeAgent extends _ag_ui
768
770
  this.delegate?.clearReconnectCursor?.(threadId);
769
771
  }
770
772
  async resolveDelegate() {
771
- await this.ensureRuntimeMode();
773
+ await this.ensureRuntimeConfiguration();
772
774
  if (!this.delegate) {
773
775
  if (this.runtimeMode !== _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) throw new Error("A delegate is only created for Intelligence mode");
774
776
  this.delegate = this.createIntelligenceDelegate();
@@ -776,14 +778,21 @@ var ProxiedCopilotRuntimeAgent = class ProxiedCopilotRuntimeAgent extends _ag_ui
776
778
  this.syncDelegate(this.delegate);
777
779
  return this.delegate;
778
780
  }
779
- async ensureRuntimeMode() {
780
- if (this.runtimeMode !== "pending") return;
781
+ /** Resolve transport and runtime mode before the first outbound request. */
782
+ async ensureRuntimeConfiguration() {
783
+ if (this.runtimeMode === _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE || this.runtimeMode !== "pending" && this.transport !== "auto") return;
781
784
  if (!this.runtimeUrl) throw new Error("Runtime URL is not set");
782
- this.runtimeInfoPromise ??= this.fetchRuntimeInfo().then((runtimeInfo) => {
785
+ const runtimeInfoPromise = this.runtimeInfoPromise ?? this.fetchRuntimeInfo().then((runtimeInfo) => {
783
786
  this.runtimeMode = runtimeInfo.mode ?? _copilotkit_shared.RUNTIME_MODE_SSE;
784
787
  this.intelligence = runtimeInfo.intelligence;
785
788
  });
786
- await this.runtimeInfoPromise;
789
+ this.runtimeInfoPromise = runtimeInfoPromise;
790
+ try {
791
+ await runtimeInfoPromise;
792
+ } catch (error) {
793
+ if (this.runtimeInfoPromise === runtimeInfoPromise) this.runtimeInfoPromise = void 0;
794
+ throw error;
795
+ }
787
796
  }
788
797
  async fetchRuntimeInfo() {
789
798
  const headers = { ...this.headers };
@@ -5248,6 +5257,111 @@ function ɵisCopilotKitFeature(name) {
5248
5257
  return FEATURE_SET.has(name);
5249
5258
  }
5250
5259
 
5260
+ //#endregion
5261
+ //#region src/interrupt-state.ts
5262
+ function toolResultContent(response) {
5263
+ if (response.status === "cancelled") return JSON.stringify({ status: "cancelled" });
5264
+ return JSON.stringify(response.payload ?? { status: "resolved" });
5265
+ }
5266
+ /**
5267
+ * @internal Framework-neutral interrupt response state shared by framework
5268
+ * adapters. Application authors must not depend on this API.
5269
+ */
5270
+ var ɵInterruptState = class {
5271
+ #responses = {};
5272
+ #pending = null;
5273
+ #sealed = false;
5274
+ /** Return the active normalized interrupt without exposing mutable state. */
5275
+ get pending() {
5276
+ const pending = this.#pending;
5277
+ if (pending?.kind === "standard") return {
5278
+ kind: "standard",
5279
+ interrupts: [...pending.interrupts]
5280
+ };
5281
+ return pending;
5282
+ }
5283
+ /** Replace the current interrupt with a legacy custom-event interrupt. */
5284
+ setLegacy(event) {
5285
+ this.#replace({
5286
+ kind: "legacy",
5287
+ event
5288
+ });
5289
+ }
5290
+ /** Replace the current interrupt with an AG-UI standard interrupt set. */
5291
+ setStandard(interrupts) {
5292
+ this.#replace({
5293
+ kind: "standard",
5294
+ interrupts: [...interrupts]
5295
+ });
5296
+ }
5297
+ /** Clear all pending interrupt data and accumulated responses. */
5298
+ clear() {
5299
+ this.#pending = null;
5300
+ this.#sealed = false;
5301
+ for (const id of Object.keys(this.#responses)) delete this.#responses[id];
5302
+ }
5303
+ /** Record a resolved response and return the next framework action. */
5304
+ resolve(payload, interruptId) {
5305
+ const pending = this.#pending;
5306
+ if (!pending || this.#sealed) return { kind: "ignored" };
5307
+ if (pending.kind === "legacy") {
5308
+ this.#sealed = true;
5309
+ return {
5310
+ kind: "legacy-resume",
5311
+ payload,
5312
+ interruptValue: pending.event.value
5313
+ };
5314
+ }
5315
+ return this.#respond(pending.interrupts, interruptId, {
5316
+ status: "resolved",
5317
+ payload
5318
+ });
5319
+ }
5320
+ /** Record a cancelled response and return the next framework action. */
5321
+ cancel(interruptId) {
5322
+ const pending = this.#pending;
5323
+ if (!pending || this.#sealed) return { kind: "ignored" };
5324
+ if (pending.kind === "legacy") {
5325
+ this.#sealed = true;
5326
+ return { kind: "dismiss" };
5327
+ }
5328
+ return this.#respond(pending.interrupts, interruptId, { status: "cancelled" });
5329
+ }
5330
+ #replace(pending) {
5331
+ this.clear();
5332
+ this.#pending = pending;
5333
+ }
5334
+ #respond(interrupts, interruptId, response) {
5335
+ const id = interruptId ?? interrupts[0]?.id;
5336
+ if (!id || !interrupts.some((interrupt) => interrupt.id === id)) return { kind: "ignored" };
5337
+ this.#responses[id] = response;
5338
+ if (!interrupts.every((interrupt) => this.#responses[interrupt.id])) return { kind: "waiting" };
5339
+ const expired = interrupts.find((interrupt) => (0, _ag_ui_client.isInterruptExpired)(interrupt));
5340
+ if (expired) {
5341
+ this.#sealed = true;
5342
+ return {
5343
+ kind: "expired",
5344
+ interrupt: expired
5345
+ };
5346
+ }
5347
+ const mutableInterrupts = [...interrupts];
5348
+ const resume = (0, _ag_ui_client.buildResumeArray)(mutableInterrupts, this.#responses);
5349
+ const toolResults = mutableInterrupts.flatMap((interrupt) => {
5350
+ if (!interrupt.toolCallId) return [];
5351
+ return [{
5352
+ toolCallId: interrupt.toolCallId,
5353
+ content: toolResultContent(this.#responses[interrupt.id])
5354
+ }];
5355
+ });
5356
+ this.#sealed = true;
5357
+ return {
5358
+ kind: "resume",
5359
+ resume,
5360
+ toolResults
5361
+ };
5362
+ }
5363
+ };
5364
+
5251
5365
  //#endregion
5252
5366
  exports.AgentRegistry = AgentRegistry;
5253
5367
  exports.AgentThreadLockedError = AgentThreadLockedError;
@@ -5279,6 +5393,7 @@ exports.parseToolArguments = parseToolArguments;
5279
5393
  exports.props = props;
5280
5394
  exports.select = select;
5281
5395
  exports.ɵCOPILOTKIT_FEATURES = ɵCOPILOTKIT_FEATURES;
5396
+ exports.ɵInterruptState = ɵInterruptState;
5282
5397
  exports.ɵMAX_SOCKET_RETRIES = ɵMAX_SOCKET_RETRIES;
5283
5398
  exports.ɵcreateMemoryStore = createMemoryStore;
5284
5399
  exports.ɵcreateThreadSelectors = ɵcreateThreadSelectors;