@prbe.ai/electron-sdk 0.1.8 → 0.1.9

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
@@ -122,7 +122,7 @@ var PRBEAgentState = class extends EventEmitter {
122
122
  activeCRs = /* @__PURE__ */ new Map();
123
123
  completedCRs = [];
124
124
  // Tracked tickets
125
- trackedTicketIDs = [];
125
+ trackedSessionIDs = [];
126
126
  ticketInfo = [];
127
127
  // Computed
128
128
  get hasActiveWork() {
@@ -350,8 +350,8 @@ var PRBEAgentState = class extends EventEmitter {
350
350
  this.emit("status" /* STATUS */);
351
351
  }
352
352
  // ---------- Tickets ----------
353
- updateTrackedTicketIDs(ids) {
354
- this.trackedTicketIDs = ids;
353
+ updateTrackedSessionIDs(ids) {
354
+ this.trackedSessionIDs = ids;
355
355
  this.emit("tickets-changed" /* TICKETS_CHANGED */, ids);
356
356
  this.emit("status" /* STATUS */);
357
357
  }
@@ -1740,13 +1740,13 @@ var PRBEAgent = class _PRBEAgent {
1740
1740
  savePersistedData(this.persistedData);
1741
1741
  return newID;
1742
1742
  }
1743
- get trackedTicketIDs() {
1744
- return this.persistedData.ticketIds ?? [];
1743
+ get trackedSessionIDs() {
1744
+ return this.persistedData.sessionIds ?? [];
1745
1745
  }
1746
- set trackedTicketIDs(ids) {
1747
- this.persistedData.ticketIds = ids;
1746
+ set trackedSessionIDs(ids) {
1747
+ this.persistedData.sessionIds = ids;
1748
1748
  savePersistedData(this.persistedData);
1749
- this.state.updateTrackedTicketIDs(ids);
1749
+ this.state.updateTrackedSessionIDs(ids);
1750
1750
  this.syncPolling(ids.length > 0);
1751
1751
  }
1752
1752
  get respondedCRIDs() {
@@ -1756,19 +1756,19 @@ var PRBEAgent = class _PRBEAgent {
1756
1756
  this.persistedData.respondedCRIds = Array.from(ids);
1757
1757
  savePersistedData(this.persistedData);
1758
1758
  }
1759
- syncPolling(hasTickets) {
1760
- if (this.config.backgroundPolling && hasTickets) {
1759
+ syncPolling(hasSessions) {
1760
+ if (this.config.backgroundPolling && hasSessions) {
1761
1761
  if (this.pollingTimer === null) {
1762
1762
  this.startPolling();
1763
1763
  }
1764
- } else if (!hasTickets) {
1764
+ } else if (!hasSessions) {
1765
1765
  this.stopPolling();
1766
1766
  }
1767
1767
  }
1768
- addTrackedTicket(id) {
1769
- const ids = this.trackedTicketIDs;
1768
+ addTrackedSession(id) {
1769
+ const ids = this.trackedSessionIDs;
1770
1770
  if (!ids.includes(id)) {
1771
- this.trackedTicketIDs = [...ids, id];
1771
+ this.trackedSessionIDs = [...ids, id];
1772
1772
  }
1773
1773
  }
1774
1774
  // ---------- Constructor ----------
@@ -1826,9 +1826,9 @@ var PRBEAgent = class _PRBEAgent {
1826
1826
  if (config.ipcMain) {
1827
1827
  this.hookRendererLogs(config.ipcMain, config.rendererLogChannel ?? "prbe-renderer-log");
1828
1828
  }
1829
- const existingTickets = this.trackedTicketIDs;
1830
- if (existingTickets.length > 0) {
1831
- this.trackedTicketIDs = existingTickets;
1829
+ const existingSessions = this.trackedSessionIDs;
1830
+ if (existingSessions.length > 0) {
1831
+ this.trackedSessionIDs = existingSessions;
1832
1832
  }
1833
1833
  }
1834
1834
  // ---------- Log integration ----------
@@ -1959,8 +1959,8 @@ var PRBEAgent = class _PRBEAgent {
1959
1959
  );
1960
1960
  this.currentInvestigationSource = "user" /* USER */;
1961
1961
  this.currentCRId = null;
1962
- if (result?.ticketId) {
1963
- this.addTrackedTicket(result.ticketId);
1962
+ if (result?.sessionId) {
1963
+ this.addTrackedSession(result.sessionId);
1964
1964
  } else if (!result) {
1965
1965
  if (this.state.isInvestigating) {
1966
1966
  const message = this.userCancelled ? "Investigation cancelled" : "Investigation ended unexpectedly";
@@ -1992,33 +1992,37 @@ var PRBEAgent = class _PRBEAgent {
1992
1992
  }
1993
1993
  /**
1994
1994
  * Poll the backend for context requests on tracked tickets.
1995
+ * Resolves session IDs → ticket IDs first, then polls with ticket IDs.
1995
1996
  */
1996
1997
  async poll() {
1997
- const ticketIDs = this.trackedTicketIDs;
1998
- if (ticketIDs.length === 0) return null;
1999
- const request = {
2000
- agent_id: this.agentID,
2001
- ticket_ids: ticketIDs
2002
- };
1998
+ const sessionIDs = this.trackedSessionIDs;
1999
+ if (sessionIDs.length === 0) return null;
2003
2000
  try {
2001
+ const resolved = await this.resolveSessions(sessionIDs);
2002
+ const returnedSessionIDs = new Set(
2003
+ resolved.tickets.flatMap((t) => t.session_ids)
2004
+ );
2005
+ const resolvedSessionIDs = new Set(
2006
+ resolved.tickets.filter((t) => t.status === "resolved").flatMap((t) => t.session_ids)
2007
+ );
2008
+ const survivingSessions = sessionIDs.filter(
2009
+ (id) => returnedSessionIDs.has(id) && !resolvedSessionIDs.has(id)
2010
+ );
2011
+ if (survivingSessions.length !== sessionIDs.length) {
2012
+ this.trackedSessionIDs = survivingSessions;
2013
+ }
2014
+ const ticketIDs = resolved.tickets.filter((t) => t.status !== "resolved").map((t) => t.ticket_id);
2015
+ if (ticketIDs.length === 0) return { tickets: [] };
2016
+ const request = {
2017
+ agent_id: this.agentID,
2018
+ ticket_ids: ticketIDs
2019
+ };
2004
2020
  const response = await this.post(
2005
2021
  "/api/agent/poll",
2006
2022
  request
2007
2023
  );
2008
- const returnedIDs = new Set(response.tickets.map((t) => t.ticket_id));
2009
- const orphaned = ticketIDs.filter((id) => !returnedIDs.has(id));
2010
- if (orphaned.length > 0) {
2011
- this.trackedTicketIDs = this.trackedTicketIDs.filter(
2012
- (id) => !orphaned.includes(id)
2013
- );
2014
- }
2015
2024
  const knownCRIDs = /* @__PURE__ */ new Set();
2016
2025
  for (const ticket of response.tickets) {
2017
- if (ticket.status === "resolved") {
2018
- this.trackedTicketIDs = this.trackedTicketIDs.filter(
2019
- (id) => id !== ticket.ticket_id
2020
- );
2021
- }
2022
2026
  for (const cr of ticket.context_requests) {
2023
2027
  knownCRIDs.add(cr.id);
2024
2028
  if (!cr.is_active || this.respondedCRIDs.has(cr.id)) continue;
@@ -2044,13 +2048,17 @@ var PRBEAgent = class _PRBEAgent {
2044
2048
  }
2045
2049
  }
2046
2050
  /**
2047
- * Fetch ticket display info for all tracked tickets.
2051
+ * Fetch ticket display info for all tracked sessions.
2052
+ * Resolves session IDs → ticket IDs first, then fetches ticket info.
2048
2053
  */
2049
2054
  async fetchTicketInfo() {
2050
- const ticketIDs = this.trackedTicketIDs;
2051
- if (ticketIDs.length === 0) return [];
2052
- const request = { ticket_ids: ticketIDs };
2055
+ const sessionIDs = this.trackedSessionIDs;
2056
+ if (sessionIDs.length === 0) return [];
2053
2057
  try {
2058
+ const resolved = await this.resolveSessions(sessionIDs);
2059
+ const ticketIDs = resolved.tickets.map((t) => t.ticket_id);
2060
+ if (ticketIDs.length === 0) return [];
2061
+ const request = { ticket_ids: ticketIDs };
2054
2062
  const response = await this.post(
2055
2063
  "/api/agent/tickets",
2056
2064
  request
@@ -2068,7 +2076,7 @@ var PRBEAgent = class _PRBEAgent {
2068
2076
  }
2069
2077
  }
2070
2078
  resumePolling() {
2071
- if (this.trackedTicketIDs.length === 0) return;
2079
+ if (this.trackedSessionIDs.length === 0) return;
2072
2080
  this.startPolling();
2073
2081
  }
2074
2082
  /**
@@ -2091,7 +2099,7 @@ var PRBEAgent = class _PRBEAgent {
2091
2099
  this.state.completedInvestigations = [];
2092
2100
  this.state.activeCRs.clear();
2093
2101
  this.state.completedCRs = [];
2094
- this.state.trackedTicketIDs = [];
2102
+ this.state.trackedSessionIDs = [];
2095
2103
  this.state.ticketInfo = [];
2096
2104
  this.state.emit("status" /* STATUS */);
2097
2105
  }
@@ -2150,8 +2158,8 @@ var PRBEAgent = class _PRBEAgent {
2150
2158
  );
2151
2159
  this.currentInvestigationSource = "user" /* USER */;
2152
2160
  this.currentCRId = null;
2153
- if (result?.ticketId) {
2154
- this.addTrackedTicket(result.ticketId);
2161
+ if (result?.sessionId) {
2162
+ this.addTrackedSession(result.sessionId);
2155
2163
  }
2156
2164
  }
2157
2165
  // ---------- WebSocket Investigation ----------
@@ -2328,6 +2336,7 @@ var PRBEAgent = class _PRBEAgent {
2328
2336
  const report = msg.content ?? "";
2329
2337
  const userSummary = msg.metadata?.["user_summary"] ?? "";
2330
2338
  const ticketId2 = msg.metadata?.["ticket_id"];
2339
+ const sessionId = msg.metadata?.["session_id"];
2331
2340
  emit({
2332
2341
  type: "completed" /* COMPLETED */,
2333
2342
  report,
@@ -2335,7 +2344,7 @@ var PRBEAgent = class _PRBEAgent {
2335
2344
  ticketId: ticketId2
2336
2345
  });
2337
2346
  ws.close(1e3, "Complete");
2338
- finish({ report, userSummary, ticketId: ticketId2 });
2347
+ finish({ report, userSummary, ticketId: ticketId2, sessionId });
2339
2348
  break;
2340
2349
  }
2341
2350
  case "error" /* ERROR */:
@@ -2446,12 +2455,23 @@ var PRBEAgent = class _PRBEAgent {
2446
2455
  this.stopPolling();
2447
2456
  this.pollingTimer = setInterval(() => {
2448
2457
  void this.poll().then(() => {
2449
- if (this.trackedTicketIDs.length === 0) {
2458
+ if (this.trackedSessionIDs.length === 0) {
2450
2459
  this.stopPolling();
2451
2460
  }
2452
2461
  });
2453
2462
  }, this.config.pollingInterval);
2454
2463
  }
2464
+ // ---------- Session Resolution ----------
2465
+ async resolveSessions(sessionIDs) {
2466
+ const request = {
2467
+ agent_id: this.agentID,
2468
+ session_ids: sessionIDs
2469
+ };
2470
+ return this.post(
2471
+ "/api/agent/resolve-sessions",
2472
+ request
2473
+ );
2474
+ }
2455
2475
  // ---------- Networking ----------
2456
2476
  /**
2457
2477
  * Abort all in-flight fetch requests to prevent orphaned DNS lookups
@@ -2506,7 +2526,7 @@ var DEFAULT_PRBE_STATE = {
2506
2526
  completedInvestigations: [],
2507
2527
  activeCRs: [],
2508
2528
  completedCRs: [],
2509
- trackedTicketIDs: [],
2529
+ trackedSessionIDs: [],
2510
2530
  ticketInfo: [],
2511
2531
  hasActiveWork: false
2512
2532
  };
@@ -2550,7 +2570,7 @@ function serializePRBEState(state) {
2550
2570
  })),
2551
2571
  activeCRs: Array.from(state.activeCRs.values()).map(serializeCR),
2552
2572
  completedCRs: state.completedCRs.map(serializeCR),
2553
- trackedTicketIDs: state.trackedTicketIDs,
2573
+ trackedSessionIDs: state.trackedSessionIDs,
2554
2574
  ticketInfo: state.ticketInfo,
2555
2575
  hasActiveWork: state.hasActiveWork
2556
2576
  };