@copilotkit/runtime 1.77.1 → 1.78.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.
Files changed (40) hide show
  1. package/README.md +16 -0
  2. package/dist/agent/index.cjs +84 -14
  3. package/dist/agent/index.cjs.map +1 -1
  4. package/dist/agent/index.d.cts +4 -3
  5. package/dist/agent/index.d.cts.map +1 -1
  6. package/dist/agent/index.d.mts +4 -3
  7. package/dist/agent/index.d.mts.map +1 -1
  8. package/dist/agent/index.mjs +84 -14
  9. package/dist/agent/index.mjs.map +1 -1
  10. package/dist/package.cjs +1 -1
  11. package/dist/package.mjs +1 -1
  12. package/dist/service-adapters/google/google-genai-adapter.cjs +2 -2
  13. package/dist/service-adapters/google/google-genai-adapter.cjs.map +1 -1
  14. package/dist/service-adapters/google/google-genai-adapter.mjs +2 -2
  15. package/dist/service-adapters/google/google-genai-adapter.mjs.map +1 -1
  16. package/dist/v2/runtime/core/package-info.cjs +1 -1
  17. package/dist/v2/runtime/core/package-info.mjs +1 -1
  18. package/dist/v2/runtime/handlers/handle-trajectory-connect.cjs +1 -1
  19. package/dist/v2/runtime/handlers/handle-trajectory-connect.cjs.map +1 -1
  20. package/dist/v2/runtime/handlers/handle-trajectory-connect.mjs +1 -1
  21. package/dist/v2/runtime/handlers/handle-trajectory-connect.mjs.map +1 -1
  22. package/dist/v2/runtime/intelligence-platform/client.cjs +73 -20
  23. package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
  24. package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
  25. package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
  26. package/dist/v2/runtime/intelligence-platform/client.mjs +73 -20
  27. package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
  28. package/dist/v2/runtime/intelligence-platform/trajectories.cjs +19 -3
  29. package/dist/v2/runtime/intelligence-platform/trajectories.cjs.map +1 -1
  30. package/dist/v2/runtime/intelligence-platform/trajectories.d.cts.map +1 -1
  31. package/dist/v2/runtime/intelligence-platform/trajectories.d.mts.map +1 -1
  32. package/dist/v2/runtime/intelligence-platform/trajectories.mjs +19 -3
  33. package/dist/v2/runtime/intelligence-platform/trajectories.mjs.map +1 -1
  34. package/dist/v2/runtime/runner/intelligence.cjs +1 -1
  35. package/dist/v2/runtime/runner/intelligence.cjs.map +1 -1
  36. package/dist/v2/runtime/runner/intelligence.d.cts.map +1 -1
  37. package/dist/v2/runtime/runner/intelligence.d.mts.map +1 -1
  38. package/dist/v2/runtime/runner/intelligence.mjs +1 -1
  39. package/dist/v2/runtime/runner/intelligence.mjs.map +1 -1
  40. package/package.json +5 -5
@@ -120,6 +120,10 @@ const MANAGED_INTELLIGENCE_WS_URL = "wss://realtime.intelligence.copilotkit.ai";
120
120
  /** Maximum time spent on the optional Inspector metadata provider request. */
121
121
  const INSPECTOR_METADATA_REQUEST_TIMEOUT_MS = 5e3;
122
122
  const INSPECTOR_LEARNING_REQUEST_TIMEOUT_MS = 5e3;
123
+ /** A 404 the caller expects: a thread lookup made before the thread exists. */
124
+ const NOT_FOUND = new Set([404]);
125
+ /** A 409 the caller expects: another request created the thread first. */
126
+ const CONFLICT = new Set([409]);
123
127
  /**
124
128
  * Error thrown when a CopilotKit Intelligence HTTP request returns a non-2xx
125
129
  * status. Carries the HTTP {@link status} code so callers can branch on
@@ -645,24 +649,42 @@ var CopilotKitIntelligence = class {
645
649
  }
646
650
  /** Mint a browser capture grant using only the Runtime's project and user. */
647
651
  async ɵconnectTrajectory(params) {
648
- const response = await fetch(`${this.#apiUrl}/api/trajectories/join`, {
649
- method: "POST",
650
- headers: {
651
- Authorization: `Bearer ${this.#apiKey}`,
652
- "Content-Type": "application/json"
653
- },
654
- body: JSON.stringify({
655
- trajectoryId: params.trajectoryId,
656
- appUserId: params.user.id
657
- }),
658
- signal: params.signal,
659
- redirect: "error"
660
- });
652
+ let response;
653
+ try {
654
+ response = await fetch(`${this.#apiUrl}/api/trajectories/join`, {
655
+ method: "POST",
656
+ headers: {
657
+ Authorization: `Bearer ${this.#apiKey}`,
658
+ "Content-Type": "application/json"
659
+ },
660
+ body: JSON.stringify({
661
+ trajectoryId: params.trajectoryId,
662
+ appUserId: params.user.id
663
+ }),
664
+ signal: params.signal,
665
+ redirect: "error"
666
+ });
667
+ } catch (error) {
668
+ if (!params.signal?.aborted) logger.warn({
669
+ error: error instanceof Error ? error.name : typeof error,
670
+ causeCode: networkErrorCode(error instanceof Error ? error.cause : void 0),
671
+ host: urlHost(this.#apiUrl)
672
+ }, "Could not reach Intelligence to connect a Trajectory");
673
+ throw error;
674
+ }
661
675
  const payload = await response.json().catch(() => void 0);
662
676
  if (!response.ok) throw trajectoryResponseError(payload, response.status, this.#apiKey);
663
677
  return parseTrajectoryConnectionGrant(payload, params.trajectoryId, this.ɵgetClientWsUrl());
664
678
  }
665
- async #request(method, path, body, extraHeaders, signal) {
679
+ /**
680
+ * Sends one request to the platform and parses its JSON body.
681
+ *
682
+ * @param expectedStatuses - Non-2xx statuses the caller handles as a normal
683
+ * outcome, such as the 404 of a thread that does not exist yet. They are
684
+ * logged at debug instead of error, and still throw.
685
+ * @throws {@link PlatformRequestError} on every non-2xx response.
686
+ */
687
+ async #request(method, path, body, extraHeaders, signal, expectedStatuses) {
666
688
  const url = `${this.#apiUrl}${path}`;
667
689
  const headers = {
668
690
  Authorization: `Bearer ${this.#apiKey}`,
@@ -677,7 +699,11 @@ var CopilotKitIntelligence = class {
677
699
  });
678
700
  if (!response.ok) {
679
701
  const text = await response.text().catch(() => "");
680
- logger.error({
702
+ if (expectedStatuses?.has(response.status)) logger.debug({
703
+ status: response.status,
704
+ path
705
+ }, "Intelligence platform request returned an expected status");
706
+ else logger.error({
681
707
  status: response.status,
682
708
  body: text,
683
709
  path
@@ -836,13 +862,20 @@ var CopilotKitIntelligence = class {
836
862
  * same `threadId` already exists.
837
863
  */
838
864
  async createThread(params) {
865
+ return this.#createThread(params);
866
+ }
867
+ /**
868
+ * Creates a thread. {@link getOrCreateThread} passes 409 as expected,
869
+ * because there it means another request created the thread first.
870
+ */
871
+ async #createThread(params, expectedStatuses) {
839
872
  const response = await this.#request("POST", `/api/threads`, {
840
873
  threadId: params.threadId,
841
874
  userId: params.userId,
842
875
  agentId: params.agentId,
843
876
  ...params.name !== void 0 ? { name: params.name } : {},
844
877
  ...params.learningContainerId !== void 0 ? { learningContainerId: params.learningContainerId } : {}
845
- });
878
+ }, void 0, void 0, expectedStatuses);
846
879
  this.#invokeLifecycleCallback("onThreadCreated", response.thread);
847
880
  return response.thread;
848
881
  }
@@ -854,8 +887,15 @@ var CopilotKitIntelligence = class {
854
887
  * not exist.
855
888
  */
856
889
  async getThread(params) {
890
+ return this.#getThread(params);
891
+ }
892
+ /**
893
+ * Fetches a thread. {@link getOrCreateThread} passes 404 as expected,
894
+ * because there it means the thread is about to be created (PE-678).
895
+ */
896
+ async #getThread(params, expectedStatuses) {
857
897
  const qs = new URLSearchParams({ userId: params.userId }).toString();
858
- return (await this.#request("GET", `/api/threads/${encodeURIComponent(params.threadId)}?${qs}`)).thread;
898
+ return (await this.#request("GET", `/api/threads/${encodeURIComponent(params.threadId)}?${qs}`, void 0, void 0, void 0, expectedStatuses)).thread;
859
899
  }
860
900
  /**
861
901
  * Get an existing thread or create it if it does not exist.
@@ -875,10 +915,10 @@ var CopilotKitIntelligence = class {
875
915
  async getOrCreateThread(params) {
876
916
  try {
877
917
  return {
878
- thread: await this.getThread({
918
+ thread: await this.#getThread({
879
919
  threadId: params.threadId,
880
920
  userId: params.userId
881
- }),
921
+ }, NOT_FOUND),
882
922
  created: false
883
923
  };
884
924
  } catch (error) {
@@ -886,7 +926,7 @@ var CopilotKitIntelligence = class {
886
926
  }
887
927
  try {
888
928
  return {
889
- thread: await this.createThread(params),
929
+ thread: await this.#createThread(params, CONFLICT),
890
930
  created: true
891
931
  };
892
932
  } catch (error) {
@@ -1113,6 +1153,19 @@ function deriveRunnerWsUrl(wsUrl) {
1113
1153
  if (wsUrl.endsWith("/channels")) return `${wsUrl.slice(0, -9)}/runner`;
1114
1154
  return `${wsUrl}/runner`;
1115
1155
  }
1156
+ /** A system error code such as ECONNREFUSED, or undefined for anything else. */
1157
+ function networkErrorCode(cause) {
1158
+ const code = typeof cause === "object" && cause !== null && "code" in cause ? cause.code : void 0;
1159
+ return typeof code === "string" && /^[A-Z][A-Z0-9_]{0,63}$/.test(code) ? code : void 0;
1160
+ }
1161
+ /** The host of a URL without its credentials, path or query. */
1162
+ function urlHost(url) {
1163
+ try {
1164
+ return new URL(url).host;
1165
+ } catch {
1166
+ return;
1167
+ }
1168
+ }
1116
1169
  function deriveClientWsUrl(wsUrl) {
1117
1170
  if (wsUrl.endsWith("/client")) return wsUrl;
1118
1171
  if (wsUrl.endsWith("/runner")) return `${wsUrl.slice(0, -7)}/client`;