@granular-software/sdk 0.4.14 → 0.4.15

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.mjs CHANGED
@@ -5725,7 +5725,8 @@ var Environment = class extends Session {
5725
5725
  "Authorization": `Bearer ${this._apiKey}`
5726
5726
  },
5727
5727
  body: JSON.stringify({
5728
- reason: "sdk_disconnect_http_fallback"
5728
+ reason: "sdk_disconnect_http_fallback",
5729
+ sessionId: this.client.currentSessionId
5729
5730
  })
5730
5731
  }
5731
5732
  );
@@ -6509,7 +6510,7 @@ var Environment = class extends Session {
6509
6510
  return super.unpublishAllEffects();
6510
6511
  }
6511
6512
  };
6512
- var Granular = class {
6513
+ var Granular = class _Granular {
6513
6514
  apiKey;
6514
6515
  apiUrl;
6515
6516
  httpUrl;
@@ -6679,6 +6680,120 @@ var Granular = class {
6679
6680
  initialHeap: options.initialHeap
6680
6681
  })
6681
6682
  });
6683
+ return this.bindWebSocketEnvironment(envData, clientId, session);
6684
+ }
6685
+ /**
6686
+ * List active (open) sessions for an environment — each session is one agent conversation thread.
6687
+ */
6688
+ async listOpenSessions(filters) {
6689
+ return this.listSessionsForEnvironment(filters.environmentId, "active");
6690
+ }
6691
+ /**
6692
+ * List closed sessions for an environment (conversations that have disconnected).
6693
+ */
6694
+ async listClosedSessions(filters) {
6695
+ return this.listSessionsForEnvironment(filters.environmentId, "closed");
6696
+ }
6697
+ async listSessionsForEnvironment(environmentId, status) {
6698
+ const query = new URLSearchParams({ environmentId, status });
6699
+ const res = await this.request(
6700
+ `/control/sessions?${query.toString()}`
6701
+ );
6702
+ const items = Array.isArray(res.items) ? res.items : [];
6703
+ return items.map((row) => this.normalizeConversationSession(row));
6704
+ }
6705
+ normalizeConversationSession(row) {
6706
+ const sessionId = String(row.sessionId ?? row.session_id ?? "");
6707
+ const environmentId = String(row.environmentId ?? row.environment_id ?? "");
6708
+ return {
6709
+ sessionId,
6710
+ tenantId: row.tenantId != null ? String(row.tenantId) : void 0,
6711
+ environmentId,
6712
+ versionId: row.versionId != null ? String(row.versionId) : null,
6713
+ docId: String(row.docId ?? row.doc_id ?? ""),
6714
+ status: row.status || "active",
6715
+ createdAt: _Granular.coerceIsoDate(row.createdAt ?? row.created_at),
6716
+ lastSeenAt: _Granular.coerceIsoDate(row.lastSeenAt ?? row.last_seen_at),
6717
+ summary: row.summary != null ? String(row.summary) : null,
6718
+ summaryUpdatedAt: row.summaryUpdatedAt != null || row.summary_updated_at != null ? _Granular.coerceIsoDate(row.summaryUpdatedAt ?? row.summary_updated_at) : null,
6719
+ subjectId: row.subjectId != null ? String(row.subjectId) : null,
6720
+ jobCount: typeof row.jobCount === "number" ? row.jobCount : void 0,
6721
+ toolCallCount: typeof row.toolCallCount === "number" ? row.toolCallCount : void 0
6722
+ };
6723
+ }
6724
+ static coerceIsoDate(value) {
6725
+ if (value instanceof Date) {
6726
+ return value.toISOString();
6727
+ }
6728
+ if (typeof value === "number") {
6729
+ return new Date(value).toISOString();
6730
+ }
6731
+ if (typeof value === "string") {
6732
+ return value;
6733
+ }
6734
+ return (/* @__PURE__ */ new Date(0)).toISOString();
6735
+ }
6736
+ /**
6737
+ * Create a new session (conversation) for an existing environment and connect to it.
6738
+ * The runtime graph is shared across all sessions for the same environment.
6739
+ */
6740
+ async createSession(options) {
6741
+ const clientId = options.clientId || `client_${Date.now()}`;
6742
+ await this.activateEnvironment(options.environmentId);
6743
+ const envData = await this.environments.get(options.environmentId);
6744
+ const session = await this.request("/ws/sessions", {
6745
+ method: "POST",
6746
+ body: JSON.stringify({
6747
+ environmentId: options.environmentId,
6748
+ clientId,
6749
+ initialHeap: options.initialHeap
6750
+ })
6751
+ });
6752
+ return this.bindWebSocketEnvironment(envData, clientId, session);
6753
+ }
6754
+ /**
6755
+ * Connect to an existing open session (same conversation thread) using a freshly minted WebSocket token.
6756
+ */
6757
+ async connectSession(options) {
6758
+ const clientId = options.clientId || `client_${Date.now()}`;
6759
+ const minted = await this.request(`/ws/sessions/${encodeURIComponent(options.sessionId)}/token`, {
6760
+ method: "POST",
6761
+ body: JSON.stringify({})
6762
+ });
6763
+ const envData = await this.environments.get(minted.environmentId);
6764
+ return this.bindWebSocketEnvironment(envData, clientId, minted);
6765
+ }
6766
+ /**
6767
+ * Mark a session closed in the control plane. If `environment` is the connected handle for that
6768
+ * `sessionId`, disconnects the WebSocket so the runtime tears down cleanly.
6769
+ */
6770
+ async closeSession(sessionId, environment) {
6771
+ if (environment && environment.sessionId === sessionId) {
6772
+ await environment.disconnect();
6773
+ return;
6774
+ }
6775
+ await this.request(`/control/sessions/${encodeURIComponent(sessionId)}`, {
6776
+ method: "PATCH",
6777
+ body: JSON.stringify({
6778
+ status: "closed",
6779
+ lastSeenAt: (/* @__PURE__ */ new Date()).toISOString()
6780
+ })
6781
+ });
6782
+ }
6783
+ /**
6784
+ * Re-open a closed session in the index and connect to its existing runtime document.
6785
+ */
6786
+ async reopenSession(sessionId, options) {
6787
+ await this.request(`/control/sessions/${encodeURIComponent(sessionId)}`, {
6788
+ method: "PATCH",
6789
+ body: JSON.stringify({
6790
+ status: "active",
6791
+ lastSeenAt: (/* @__PURE__ */ new Date()).toISOString()
6792
+ })
6793
+ });
6794
+ return this.connectSession({ sessionId, clientId: options?.clientId });
6795
+ }
6796
+ async bindWebSocketEnvironment(envData, clientId, session) {
6682
6797
  const client = new WSClient({
6683
6798
  url: session.wsUrl,
6684
6799
  sessionId: session.sessionId,