@opengeni/api-router 6.8.0 → 6.8.1-canary.41842879332001

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.js CHANGED
@@ -25,7 +25,7 @@ import {
25
25
  startTeamsBotPump,
26
26
  startTemporalScheduleCleanupPump,
27
27
  withAccessGrantSessionRlsContext
28
- } from "./chunk-A2VKA7K3.js";
28
+ } from "./chunk-HG3ZBJ7P.js";
29
29
  import "./chunk-OGQDXVXY.js";
30
30
 
31
31
  // src/index.ts
@@ -387,6 +387,7 @@ export declare function capSessionDiscoveryPage(page: Awaited<ReturnType<typeof
387
387
  waitingDescendants?: number | undefined;
388
388
  attentionDescendants: number;
389
389
  pausedDescendants: number;
390
+ pausedWorkDescendants?: number | undefined;
390
391
  failedDescendants: number;
391
392
  unreadDescendants?: number | undefined;
392
393
  unreadFailedDescendants?: number | undefined;
@@ -187,6 +187,7 @@ export declare function boundSessionDetailMcp(session: Session, effectiveControl
187
187
  waitingDescendants?: number | undefined;
188
188
  attentionDescendants: number;
189
189
  pausedDescendants: number;
190
+ pausedWorkDescendants?: number | undefined;
190
191
  failedDescendants: number;
191
192
  unreadDescendants?: number | undefined;
192
193
  unreadFailedDescendants?: number | undefined;
@@ -4,8 +4,13 @@ export declare const GOOGLE_CALENDAR_PROVIDER_DOMAIN = "calendarmcp.googleapis.c
4
4
  export declare const MICROSOFT_GRAPH_PROVIDER_DOMAIN = "graph.microsoft.com";
5
5
  /** How long the lookup may take in all; a recording is created regardless. */
6
6
  export declare const MEETING_CALENDAR_LOOKUP_TIMEOUT_MS = 3000;
7
- /** One calendar event, as much of it as naming a meeting needs. */
7
+ /** The calendars events are read from. */
8
+ export type MeetingCalendarProvider = "google" | "microsoft";
9
+ /** One calendar event, as much of it as naming or joining a meeting needs. */
8
10
  export type MeetingCalendarEvent = {
11
+ /** The provider's id for the event (one occurrence of a recurring one). */
12
+ id?: string;
13
+ provider?: MeetingCalendarProvider;
9
14
  title: string;
10
15
  start: Date;
11
16
  end: Date;
@@ -21,13 +26,23 @@ export type MeetingCalendarEvent = {
21
26
  * one may be an invitation nobody expected.
22
27
  */
23
28
  unanswered?: boolean;
29
+ /** The link that joins its call, when it has one. */
30
+ joinUrl?: string;
31
+ /** The service that link joins, e.g. "Google Meet". */
32
+ service?: string;
33
+ };
34
+ /** A calendar event named by its provider and id, such as the one a recording was started for. */
35
+ export type MeetingCalendarEventRef = {
36
+ provider: MeetingCalendarProvider;
37
+ id: string;
24
38
  };
25
39
  /**
26
- * The event a recording that started at `at` is most likely of: one under
27
- * way or about to start; one the owner answered first, then the one whose
28
- * start is nearest, then the shorter. Null when none fits.
40
+ * The event a recording that started at `at` is most likely of: the one it
41
+ * was started for, when given and still on the calendar; else one under way
42
+ * or about to start; one the owner answered first, then the one whose start
43
+ * is nearest, then the shorter. Null when none fits.
29
44
  */
30
- export declare function meetingEventAt(events: readonly MeetingCalendarEvent[], at: Date): MeetingCalendarEvent | null;
45
+ export declare function meetingEventAt(events: readonly MeetingCalendarEvent[], at: Date, chosen?: MeetingCalendarEventRef): MeetingCalendarEvent | null;
31
46
  /** An event's title as a meeting name: one line, at most 300 characters. Null when blank. */
32
47
  export declare function calendarTitle(title: string): string | null;
33
48
  /** Google Calendar `events.list` items as calendar events; malformed ones are left out. */
@@ -58,13 +73,55 @@ export type MeetingCalendarLookup = {
58
73
  };
59
74
  /**
60
75
  * The name of the owner's calendar event at `at`, from every calendar of
61
- * theirs this deployment can read. Null when there is none, or on any
76
+ * theirs this deployment can read: `event` when the recording was started
77
+ * for it, else the one at that time. Null when there is none, or on any
62
78
  * failure or once the time is up.
63
79
  */
64
80
  export declare function meetingCalendarTitle(lookup: MeetingCalendarLookup, input: {
65
81
  subjectId: string;
66
82
  at: Date;
83
+ event?: MeetingCalendarEventRef;
67
84
  }): Promise<string | null>;
85
+ /** How far ahead the upcoming meetings are read. */
86
+ export declare const UPCOMING_MEETINGS_AHEAD_MS: number;
87
+ /** A meeting that started at most this long ago may still be joined. */
88
+ export declare const UPCOMING_MEETINGS_STARTED_MS: number;
89
+ /** A meeting about to start, with the link that joins it. */
90
+ export type UpcomingMeeting = {
91
+ id: string;
92
+ provider: MeetingCalendarProvider;
93
+ title: string;
94
+ /** Private, personal or confidential: shown only to its owner, never a meeting's name. */
95
+ private: boolean;
96
+ startsAt: string;
97
+ endsAt: string;
98
+ joinUrl: string;
99
+ service: string;
100
+ /** The owner hasn't answered the invitation. */
101
+ unanswered: boolean;
102
+ };
103
+ /**
104
+ * The meetings with a join link that start in the next minutes (or started a
105
+ * few minutes ago), soonest first; at the same start one the owner answered
106
+ * comes first, then the shorter. All-day, declined, cancelled and
107
+ * out-of-office events are no meetings to join.
108
+ */
109
+ export declare function upcomingMeetingsAt(events: readonly MeetingCalendarEvent[], now: Date): UpcomingMeeting[];
110
+ /**
111
+ * The owner's meetings about to start, for a reminder that joins one and
112
+ * starts its recording. `calendar` says whether they have a calendar this
113
+ * deployment can read; a calendar that can't be read now lists nothing and
114
+ * makes the answer incomplete (`complete: false`), so a reminder already
115
+ * showing isn't taken for cancelled.
116
+ */
117
+ export declare function upcomingMeetings(lookup: MeetingCalendarLookup, input: {
118
+ subjectId: string;
119
+ now: Date;
120
+ }): Promise<{
121
+ calendar: "connected" | "none";
122
+ complete: boolean;
123
+ meetings: UpcomingMeeting[];
124
+ }>;
68
125
  /** The lookup against this deployment's database and connection broker. */
69
126
  export declare function meetingCalendarLookup(input: {
70
127
  db: Database;
@@ -0,0 +1,11 @@
1
+ /** A link to join a call, and the service it joins. */
2
+ export type MeetingJoinLink = {
3
+ url: string;
4
+ service: string;
5
+ };
6
+ /** `value` as a join link when it is an https link on a known meeting service. */
7
+ export declare function meetingJoinLink(value: unknown): MeetingJoinLink | null;
8
+ /** The first join link in free text, such as an event's location or description. */
9
+ export declare function meetingJoinLinkIn(text: unknown): MeetingJoinLink | null;
10
+ /** The first of `candidates` that is a join link, links before text. */
11
+ export declare function firstMeetingJoinLink(links: readonly unknown[], texts: readonly unknown[]): MeetingJoinLink | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/api-router",
3
- "version": "6.8.0",
3
+ "version": "6.8.1-canary.41842879332001",
4
4
  "description": "Opengeni HTTP surface: the Hono adapter/router (createApp), routes, MCP HTTP transport, and HTTP access adapters over @opengeni/core. An engine-distribution surface — its runtime closure includes engine-internal packages.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -46,30 +46,30 @@
46
46
  "@llamaindex/liteparse": "2.14.2",
47
47
  "@microsoft/teams.apps": "2.1.1",
48
48
  "@modelcontextprotocol/sdk": "^1.29.0",
49
- "@opengeni/agent-proto": "6.8.0",
50
- "@opengeni/artifact-tool": "6.8.0",
51
- "@opengeni/capabilities": "6.8.0",
52
- "@opengeni/codemode": "6.8.0",
53
- "@opengeni/codex": "6.8.0",
54
- "@opengeni/config": "6.8.0",
55
- "@opengeni/contracts": "6.8.0",
56
- "@opengeni/core": "6.8.0",
57
- "@opengeni/db": "6.8.0",
58
- "@opengeni/documents": "6.8.0",
59
- "@opengeni/events": "6.8.0",
60
- "@opengeni/github": "6.8.0",
61
- "@opengeni/interaction": "6.8.0",
62
- "@opengeni/network": "6.8.0",
63
- "@opengeni/observability": "6.8.0",
64
- "@opengeni/runtime": "6.8.0",
65
- "@opengeni/storage": "6.8.0",
66
- "@opengeni/tool-gateway": "6.8.0",
67
- "@opengeni/vaults": "6.8.0",
68
- "@opengeni/vaults-bitwarden": "6.8.0",
69
- "@opengeni/vaults-keeper": "6.8.0",
70
- "@opengeni/vaults-onepassword": "6.8.0",
71
- "@opengeni/vaults-protonpass": "6.8.0",
72
- "@opengeni/xai-subscription": "6.8.0",
49
+ "@opengeni/agent-proto": "6.8.1-canary.41842879332001",
50
+ "@opengeni/artifact-tool": "6.8.1-canary.41842879332001",
51
+ "@opengeni/capabilities": "6.8.1-canary.41842879332001",
52
+ "@opengeni/codemode": "6.8.1-canary.41842879332001",
53
+ "@opengeni/codex": "6.8.1-canary.41842879332001",
54
+ "@opengeni/config": "6.8.1-canary.41842879332001",
55
+ "@opengeni/contracts": "6.8.1-canary.41842879332001",
56
+ "@opengeni/core": "6.8.1-canary.41842879332001",
57
+ "@opengeni/db": "6.8.1-canary.41842879332001",
58
+ "@opengeni/documents": "6.8.1-canary.41842879332001",
59
+ "@opengeni/events": "6.8.1-canary.41842879332001",
60
+ "@opengeni/github": "6.8.1-canary.41842879332001",
61
+ "@opengeni/interaction": "6.8.1-canary.41842879332001",
62
+ "@opengeni/network": "6.8.1-canary.41842879332001",
63
+ "@opengeni/observability": "6.8.1-canary.41842879332001",
64
+ "@opengeni/runtime": "6.8.1-canary.41842879332001",
65
+ "@opengeni/storage": "6.8.1-canary.41842879332001",
66
+ "@opengeni/tool-gateway": "6.8.1-canary.41842879332001",
67
+ "@opengeni/vaults": "6.8.1-canary.41842879332001",
68
+ "@opengeni/vaults-bitwarden": "6.8.1-canary.41842879332001",
69
+ "@opengeni/vaults-keeper": "6.8.1-canary.41842879332001",
70
+ "@opengeni/vaults-onepassword": "6.8.1-canary.41842879332001",
71
+ "@opengeni/vaults-protonpass": "6.8.1-canary.41842879332001",
72
+ "@opengeni/xai-subscription": "6.8.1-canary.41842879332001",
73
73
  "@simplewebauthn/server": "14.0.3",
74
74
  "@temporalio/client": "^1.17.0",
75
75
  "ajv": "^8.20.0",
@@ -88,5 +88,6 @@
88
88
  },
89
89
  "engines": {
90
90
  "node": ">=18"
91
- }
91
+ },
92
+ "gitHead": "ff8b16463fb0dff3e03bb65f123c9fe3f500eedf"
92
93
  }
@@ -6,6 +6,7 @@
6
6
  // failure, a slow provider or no calendar at all leaves the date name, and
7
7
  // the conversation names the meeting later.
8
8
  import type { Settings } from "@opengeni/config";
9
+ import { firstMeetingJoinLink } from "./meeting-join-links";
9
10
  import {
10
11
  buildConnectionTokenResolver,
11
12
  listConnectionsMetadata,
@@ -26,8 +27,14 @@ const STARTS_SOON_MS = 10 * 60_000;
26
27
  /** An event that ends this soon after recording began is the previous one running over. */
27
28
  const ENDS_SOON_MS = 60_000;
28
29
 
29
- /** One calendar event, as much of it as naming a meeting needs. */
30
+ /** The calendars events are read from. */
31
+ export type MeetingCalendarProvider = "google" | "microsoft";
32
+
33
+ /** One calendar event, as much of it as naming or joining a meeting needs. */
30
34
  export type MeetingCalendarEvent = {
35
+ /** The provider's id for the event (one occurrence of a recurring one). */
36
+ id?: string;
37
+ provider?: MeetingCalendarProvider;
31
38
  title: string;
32
39
  start: Date;
33
40
  end: Date;
@@ -43,27 +50,49 @@ export type MeetingCalendarEvent = {
43
50
  * one may be an invitation nobody expected.
44
51
  */
45
52
  unanswered?: boolean;
53
+ /** The link that joins its call, when it has one. */
54
+ joinUrl?: string;
55
+ /** The service that link joins, e.g. "Google Meet". */
56
+ service?: string;
46
57
  };
47
58
 
59
+ /** A calendar event named by its provider and id, such as the one a recording was started for. */
60
+ export type MeetingCalendarEventRef = { provider: MeetingCalendarProvider; id: string };
61
+
62
+ function nameable(event: MeetingCalendarEvent): boolean {
63
+ return (
64
+ event.meeting &&
65
+ !event.private &&
66
+ !event.declined &&
67
+ calendarTitle(event.title) !== null &&
68
+ event.end.getTime() > event.start.getTime()
69
+ );
70
+ }
71
+
48
72
  /**
49
- * The event a recording that started at `at` is most likely of: one under
50
- * way or about to start; one the owner answered first, then the one whose
51
- * start is nearest, then the shorter. Null when none fits.
73
+ * The event a recording that started at `at` is most likely of: the one it
74
+ * was started for, when given and still on the calendar; else one under way
75
+ * or about to start; one the owner answered first, then the one whose start
76
+ * is nearest, then the shorter. Null when none fits.
52
77
  */
53
78
  export function meetingEventAt(
54
79
  events: readonly MeetingCalendarEvent[],
55
80
  at: Date,
81
+ chosen?: MeetingCalendarEventRef,
56
82
  ): MeetingCalendarEvent | null {
83
+ if (chosen) {
84
+ // The person chose this meeting: its own name or none, never another event's.
85
+ const found = events.find(
86
+ (event) => event.id === chosen.id && event.provider === chosen.provider,
87
+ );
88
+ if (found) return nameable(found) ? found : null;
89
+ }
57
90
  const time = at.getTime();
58
91
  const fitting = events.filter(
59
92
  (event) =>
60
- event.meeting &&
61
- !event.private &&
62
- !event.declined &&
63
- calendarTitle(event.title) !== null &&
93
+ nameable(event) &&
64
94
  event.start.getTime() <= time + STARTS_SOON_MS &&
65
- event.end.getTime() > time + ENDS_SOON_MS &&
66
- event.end.getTime() > event.start.getTime(),
95
+ event.end.getTime() > time + ENDS_SOON_MS,
67
96
  );
68
97
  fitting.sort(
69
98
  (a, b) =>
@@ -81,7 +110,12 @@ export function calendarTitle(title: string): string | null {
81
110
  }
82
111
 
83
112
  type GoogleEvent = {
113
+ id?: unknown;
84
114
  summary?: unknown;
115
+ hangoutLink?: unknown;
116
+ conferenceData?: { entryPoints?: Array<{ entryPointType?: unknown; uri?: unknown }> };
117
+ location?: unknown;
118
+ description?: unknown;
85
119
  status?: unknown;
86
120
  visibility?: unknown;
87
121
  eventType?: unknown;
@@ -105,8 +139,19 @@ export function googleCalendarEvents(body: unknown): MeetingCalendarEvent[] {
105
139
  const self = Array.isArray(item.attendees)
106
140
  ? item.attendees.find((attendee) => attendee?.self === true)
107
141
  : undefined;
142
+ const video = Array.isArray(item.conferenceData?.entryPoints)
143
+ ? item.conferenceData.entryPoints
144
+ .filter((entry) => entry?.entryPointType === "video")
145
+ .map((entry) => entry.uri)
146
+ : [];
147
+ const join = firstMeetingJoinLink(
148
+ [item.hangoutLink, ...video],
149
+ [item.location, item.description],
150
+ );
108
151
  return [
109
152
  {
153
+ ...(typeof item.id === "string" && item.id ? { id: item.id } : {}),
154
+ provider: "google" as const,
110
155
  title: item.summary,
111
156
  start: start ?? new Date(0),
112
157
  end: end ?? new Date(0),
@@ -114,13 +159,19 @@ export function googleCalendarEvents(body: unknown): MeetingCalendarEvent[] {
114
159
  private: item.visibility === "private" || item.visibility === "confidential",
115
160
  declined: item.status === "cancelled" || self?.responseStatus === "declined",
116
161
  unanswered: self?.responseStatus === "needsAction",
162
+ ...(join ? { joinUrl: join.url, service: join.service } : {}),
117
163
  },
118
164
  ];
119
165
  });
120
166
  }
121
167
 
122
168
  type GraphEvent = {
169
+ id?: unknown;
123
170
  subject?: unknown;
171
+ onlineMeeting?: { joinUrl?: unknown } | null;
172
+ onlineMeetingUrl?: unknown;
173
+ location?: { displayName?: unknown; locationUri?: unknown } | null;
174
+ body?: { content?: unknown } | null;
124
175
  isAllDay?: unknown;
125
176
  isCancelled?: unknown;
126
177
  sensitivity?: unknown;
@@ -145,8 +196,14 @@ export function graphCalendarEvents(body: unknown): MeetingCalendarEvent[] {
145
196
  const start = graphTime(item?.start?.dateTime);
146
197
  const end = graphTime(item?.end?.dateTime);
147
198
  if (typeof item?.subject !== "string" || !start || !end) return [];
199
+ const join = firstMeetingJoinLink(
200
+ [item.onlineMeeting?.joinUrl, item.onlineMeetingUrl, item.location?.locationUri],
201
+ [item.location?.displayName, item.body?.content],
202
+ );
148
203
  return [
149
204
  {
205
+ ...(typeof item.id === "string" && item.id ? { id: item.id } : {}),
206
+ provider: "microsoft" as const,
150
207
  title: item.subject,
151
208
  start,
152
209
  end,
@@ -158,6 +215,7 @@ export function graphCalendarEvents(body: unknown): MeetingCalendarEvent[] {
158
215
  item.sensitivity === "confidential",
159
216
  declined: item.isCancelled === true || item.responseStatus?.response === "declined",
160
217
  unanswered: item.responseStatus?.response === "notResponded",
218
+ ...(join ? { joinUrl: join.url, service: join.service } : {}),
161
219
  },
162
220
  ];
163
221
  });
@@ -203,46 +261,58 @@ export type MeetingCalendarLookup = {
203
261
  onProblem?: (problem: { providerDomain: string; reason: string }) => void;
204
262
  };
205
263
 
206
- function eventsRequest(connection: ConnectionMetadataWithVerification, at: Date) {
207
- const from = new Date(at.getTime() + ENDS_SOON_MS).toISOString();
208
- const to = new Date(at.getTime() + STARTS_SOON_MS).toISOString();
264
+ const GOOGLE_FIELDS =
265
+ "items(id,summary,status,visibility,eventType,start,end,attendees(self,responseStatus))";
266
+ /** With what joining needs: the call's link, wherever the event keeps it. */
267
+ const GOOGLE_JOIN_FIELDS =
268
+ "items(id,summary,status,visibility,eventType,start,end,attendees(self,responseStatus)," +
269
+ "hangoutLink,conferenceData(entryPoints(entryPointType,uri)),location,description)";
270
+ const GRAPH_SELECT = "id,subject,isAllDay,isCancelled,sensitivity,showAs,start,end,responseStatus";
271
+ const GRAPH_JOIN_SELECT = `${GRAPH_SELECT},onlineMeeting,onlineMeetingUrl,location,body`;
272
+
273
+ /** The events that end after `from` and start before `to`, with join links when `join`. */
274
+ function eventsRequest(
275
+ connection: ConnectionMetadataWithVerification,
276
+ window: { from: Date; to: Date; join: boolean },
277
+ ) {
278
+ const from = window.from.toISOString();
279
+ const to = window.to.toISOString();
209
280
  if (connection.providerDomain.toLowerCase() === GOOGLE_CALENDAR_PROVIDER_DOMAIN) {
210
281
  const url = new URL(GOOGLE_EVENTS_URL);
211
- // Events that end after `from` and start before `to`.
212
282
  url.searchParams.set("timeMin", from);
213
283
  url.searchParams.set("timeMax", to);
214
284
  url.searchParams.set("singleEvents", "true");
215
285
  url.searchParams.set("maxResults", "25");
216
- url.searchParams.set(
217
- "fields",
218
- "items(summary,status,visibility,eventType,start,end,attendees(self,responseStatus))",
219
- );
286
+ url.searchParams.set("fields", window.join ? GOOGLE_JOIN_FIELDS : GOOGLE_FIELDS);
220
287
  return { url: url.toString(), headers: {}, parse: googleCalendarEvents };
221
288
  }
222
289
  const url = new URL(GRAPH_CALENDAR_VIEW_URL);
223
290
  url.searchParams.set("startDateTime", from);
224
291
  url.searchParams.set("endDateTime", to);
225
292
  url.searchParams.set("$top", "25");
226
- url.searchParams.set(
227
- "$select",
228
- "subject,isAllDay,isCancelled,sensitivity,showAs,start,end,responseStatus",
229
- );
293
+ url.searchParams.set("$select", window.join ? GRAPH_JOIN_SELECT : GRAPH_SELECT);
230
294
  return {
231
295
  url: url.toString(),
232
- headers: { Prefer: 'outlook.timezone="UTC"' },
296
+ // The body as text, where a join link written into the invitation is found.
297
+ headers: {
298
+ Prefer: window.join
299
+ ? 'outlook.timezone="UTC", outlook.body-content-type="text"'
300
+ : 'outlook.timezone="UTC"',
301
+ },
233
302
  parse: graphCalendarEvents,
234
303
  };
235
304
  }
236
305
 
237
306
  /**
238
- * The name of the owner's calendar event at `at`, from every calendar of
239
- * theirs this deployment can read. Null when there is none, or on any
240
- * failure or once the time is up.
307
+ * The events between `from` and `to` on every calendar of the owner's this
308
+ * deployment can read, and whether they have one at all. A calendar that
309
+ * fails or answers too late adds no events and makes the read incomplete;
310
+ * `null` once the time is up.
241
311
  */
242
- export async function meetingCalendarTitle(
312
+ async function readMeetingCalendars(
243
313
  lookup: MeetingCalendarLookup,
244
- input: { subjectId: string; at: Date },
245
- ): Promise<string | null> {
314
+ input: { subjectId: string; from: Date; to: Date; join: boolean },
315
+ ): Promise<{ connected: boolean; complete: boolean; events: MeetingCalendarEvent[] } | null> {
246
316
  const timeout = AbortSignal.timeout(lookup.timeoutMs ?? MEETING_CALENDAR_LOOKUP_TIMEOUT_MS);
247
317
  let settled = false;
248
318
  const problem = (providerDomain: string, reason: string) => {
@@ -253,33 +323,40 @@ export async function meetingCalendarTitle(
253
323
  // Reporting never changes the outcome.
254
324
  }
255
325
  };
256
- const read = async (): Promise<string | null> => {
326
+ const read = async () => {
257
327
  const connections = meetingCalendarConnections(await lookup.listConnections(), {
258
328
  subjectId: input.subjectId,
259
329
  googleCalendarEnabled: lookup.googleCalendarEnabled,
260
330
  });
261
- if (connections.length === 0) return null;
331
+ if (connections.length === 0) return { connected: false, complete: true, events: [] };
332
+ let complete = true;
262
333
  const found = await Promise.all(
263
334
  connections.map(async (connection) => {
264
335
  try {
265
- const request = eventsRequest(connection, input.at);
336
+ const request = eventsRequest(connection, input);
266
337
  const credential = await lookup.credentialHeaders(connection, request.url);
267
338
  if (!credential) {
339
+ complete = false;
268
340
  problem(connection.providerDomain, "credential unavailable");
269
341
  return [];
270
342
  }
271
- if (timeout.aborted) return [];
343
+ if (timeout.aborted) {
344
+ complete = false;
345
+ return [];
346
+ }
272
347
  const response = await lookup.fetch(request.url, {
273
348
  headers: { ...credential, ...request.headers, Accept: "application/json" },
274
349
  redirect: "error",
275
350
  signal: timeout,
276
351
  });
277
352
  if (!response.ok) {
353
+ complete = false;
278
354
  problem(connection.providerDomain, `HTTP ${response.status}`);
279
355
  return [];
280
356
  }
281
357
  return request.parse(await response.json());
282
358
  } catch (error) {
359
+ complete = false;
283
360
  // A lookup cut off by the time limit is reported once, below.
284
361
  if (!timeout.aborted) {
285
362
  problem(connection.providerDomain, error instanceof Error ? error.name : "failed");
@@ -288,8 +365,7 @@ export async function meetingCalendarTitle(
288
365
  }
289
366
  }),
290
367
  );
291
- const event = meetingEventAt(found.flat(), input.at);
292
- return event ? calendarTitle(event.title) : null;
368
+ return { connected: true, complete, events: found.flat() };
293
369
  };
294
370
  const expired = new Promise<null>((resolve) => {
295
371
  timeout.addEventListener(
@@ -311,6 +387,122 @@ export async function meetingCalendarTitle(
311
387
  }
312
388
  }
313
389
 
390
+ /**
391
+ * The name of the owner's calendar event at `at`, from every calendar of
392
+ * theirs this deployment can read: `event` when the recording was started
393
+ * for it, else the one at that time. Null when there is none, or on any
394
+ * failure or once the time is up.
395
+ */
396
+ export async function meetingCalendarTitle(
397
+ lookup: MeetingCalendarLookup,
398
+ input: { subjectId: string; at: Date; event?: MeetingCalendarEventRef },
399
+ ): Promise<string | null> {
400
+ const read = await readMeetingCalendars(lookup, {
401
+ subjectId: input.subjectId,
402
+ from: new Date(input.at.getTime() + ENDS_SOON_MS),
403
+ to: new Date(input.at.getTime() + STARTS_SOON_MS),
404
+ join: false,
405
+ });
406
+ const event = read ? meetingEventAt(read.events, input.at, input.event) : null;
407
+ return event ? calendarTitle(event.title) : null;
408
+ }
409
+
410
+ /** How far ahead the upcoming meetings are read. */
411
+ export const UPCOMING_MEETINGS_AHEAD_MS = 20 * 60_000;
412
+ /** A meeting that started at most this long ago may still be joined. */
413
+ export const UPCOMING_MEETINGS_STARTED_MS = 5 * 60_000;
414
+
415
+ /** A meeting about to start, with the link that joins it. */
416
+ export type UpcomingMeeting = {
417
+ id: string;
418
+ provider: MeetingCalendarProvider;
419
+ title: string;
420
+ /** Private, personal or confidential: shown only to its owner, never a meeting's name. */
421
+ private: boolean;
422
+ startsAt: string;
423
+ endsAt: string;
424
+ joinUrl: string;
425
+ service: string;
426
+ /** The owner hasn't answered the invitation. */
427
+ unanswered: boolean;
428
+ };
429
+
430
+ /**
431
+ * The meetings with a join link that start in the next minutes (or started a
432
+ * few minutes ago), soonest first; at the same start one the owner answered
433
+ * comes first, then the shorter. All-day, declined, cancelled and
434
+ * out-of-office events are no meetings to join.
435
+ */
436
+ export function upcomingMeetingsAt(
437
+ events: readonly MeetingCalendarEvent[],
438
+ now: Date,
439
+ ): UpcomingMeeting[] {
440
+ const time = now.getTime();
441
+ const seen = new Set<string>();
442
+ return events
443
+ .filter(
444
+ (event) =>
445
+ event.meeting &&
446
+ !event.declined &&
447
+ event.id !== undefined &&
448
+ event.provider !== undefined &&
449
+ event.joinUrl !== undefined &&
450
+ event.end.getTime() > time &&
451
+ event.end.getTime() > event.start.getTime() &&
452
+ event.start.getTime() >= time - UPCOMING_MEETINGS_STARTED_MS &&
453
+ event.start.getTime() <= time + UPCOMING_MEETINGS_AHEAD_MS,
454
+ )
455
+ .sort(
456
+ (a, b) =>
457
+ a.start.getTime() - b.start.getTime() ||
458
+ Number(a.unanswered === true) - Number(b.unanswered === true) ||
459
+ a.end.getTime() - a.start.getTime() - (b.end.getTime() - b.start.getTime()),
460
+ )
461
+ .flatMap((event) => {
462
+ const key = `${event.provider}:${event.id}`;
463
+ if (seen.has(key)) return [];
464
+ seen.add(key);
465
+ return [
466
+ {
467
+ id: event.id!,
468
+ provider: event.provider!,
469
+ title: calendarTitle(event.title) ?? "",
470
+ private: event.private,
471
+ startsAt: event.start.toISOString(),
472
+ endsAt: event.end.toISOString(),
473
+ joinUrl: event.joinUrl!,
474
+ service: event.service ?? "Meeting",
475
+ unanswered: event.unanswered === true,
476
+ },
477
+ ];
478
+ });
479
+ }
480
+
481
+ /**
482
+ * The owner's meetings about to start, for a reminder that joins one and
483
+ * starts its recording. `calendar` says whether they have a calendar this
484
+ * deployment can read; a calendar that can't be read now lists nothing and
485
+ * makes the answer incomplete (`complete: false`), so a reminder already
486
+ * showing isn't taken for cancelled.
487
+ */
488
+ export async function upcomingMeetings(
489
+ lookup: MeetingCalendarLookup,
490
+ input: { subjectId: string; now: Date },
491
+ ): Promise<{ calendar: "connected" | "none"; complete: boolean; meetings: UpcomingMeeting[] }> {
492
+ const read = await readMeetingCalendars(lookup, {
493
+ subjectId: input.subjectId,
494
+ from: input.now,
495
+ to: new Date(input.now.getTime() + UPCOMING_MEETINGS_AHEAD_MS),
496
+ join: true,
497
+ });
498
+ if (read === null) return { calendar: "connected", complete: false, meetings: [] };
499
+ return {
500
+ calendar: read.connected ? "connected" : "none",
501
+ complete: read.complete,
502
+ meetings: upcomingMeetingsAt(read.events, input.now),
503
+ };
504
+ }
505
+
314
506
  /** The lookup against this deployment's database and connection broker. */
315
507
  export function meetingCalendarLookup(input: {
316
508
  db: Database;