@lobu/runtime-device 21.2.1 → 22.0.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.
- package/dist/component.json +1 -1
- package/dist/dependencies.bun.lock +91 -83
- package/npm-shrinkwrap.json +492 -438
- package/package.json +12 -2
- package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +5 -6
- package/vendor/cli/dist/commands/_lib/apply/client.d.ts +1 -0
- package/vendor/cli/dist/commands/_lib/apply/client.js +26 -7
- package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +9 -1
- package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +1 -0
- package/vendor/cli/dist/commands/_lib/apply/diff.js +46 -4
- package/vendor/cli/dist/commands/_lib/apply/map-config.js +18 -38
- package/vendor/cli/dist/commands/_lib/init-from-org/bootstrap.js +5 -17
- package/vendor/cli/dist/commands/init.d.ts +0 -2
- package/vendor/cli/dist/commands/init.js +0 -27
- package/vendor/cli/dist/commands/login.d.ts +17 -0
- package/vendor/cli/dist/commands/login.js +43 -17
- package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +0 -8
- package/vendor/cli/dist/commands/memory/_lib/errors.js +0 -12
- package/vendor/cli/dist/commands/memory/_lib/mcp.js +6 -6
- package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +4 -10
- package/vendor/cli/dist/config/define.d.ts +21 -15
- package/vendor/cli/dist/config/define.js +2 -2
- package/vendor/cli/dist/connectors/README.md +7 -5
- package/vendor/cli/dist/connectors/_google/actions.ts +270 -0
- package/vendor/cli/dist/connectors/_google/discovery/calendar_v3.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/chat_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/docs_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/drive_v3.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/gmail_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/people_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/sheets_v4.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/tasks_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/youtube_v3.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery.ts +442 -0
- package/vendor/cli/dist/connectors/_google/errors.ts +91 -0
- package/vendor/cli/dist/connectors/_google/list-feed.ts +223 -0
- package/vendor/cli/dist/connectors/_google/policies.ts +127 -0
- package/vendor/cli/dist/connectors/agentid.ts +43 -0
- package/vendor/cli/dist/connectors/chat-user-identity.ts +2 -1
- package/vendor/cli/dist/connectors/github-automation-events.ts +9 -0
- package/vendor/cli/dist/connectors/github-identity.ts +28 -3
- package/vendor/cli/dist/connectors/github.ts +101 -59
- package/vendor/cli/dist/connectors/google_calendar.ts +207 -719
- package/vendor/cli/dist/connectors/google_drive.ts +16 -2
- package/vendor/cli/dist/connectors/google_gmail.ts +51 -33
- package/vendor/cli/dist/connectors/herdr.ts +406 -0
- package/vendor/cli/dist/connectors/jira.ts +6 -6
- package/vendor/cli/dist/connectors/linear.ts +7 -4
- package/vendor/cli/dist/connectors/postgres.ts +27 -2
- package/vendor/cli/dist/connectors/tiktok-inbox-page.js +230 -0
- package/vendor/cli/dist/connectors/tiktok-web-page.js +211 -0
- package/vendor/cli/dist/connectors/tiktok_web.ts +286 -0
- package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +228 -10
- package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +47 -14
- package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +121 -0
- package/vendor/cli/dist/connectors/whatsapp_web.ts +170 -29
- package/vendor/cli/dist/connectors/x.ts +276 -136
- package/vendor/cli/dist/index.js +1 -37
- package/vendor/cli/dist/internal/context.d.ts +10 -0
- package/vendor/cli/dist/internal/context.js +26 -16
- package/vendor/cli/dist/internal/node-version.d.ts +1 -1
- package/vendor/cli/dist/internal/node-version.js +1 -1
- package/vendor/cli/dist/internal/oauth.d.ts +3 -1
- package/vendor/cli/dist/internal/oauth.js +4 -3
- package/vendor/cli/dist/internal/runtime-components.d.ts +9 -0
- package/vendor/cli/dist/internal/runtime-components.js +67 -2
- package/vendor/cli/dist/mac-auth.js +17 -1
- package/vendor/cli/dist/providers.json +1 -1
- package/vendor/cli/dist/runtime-components.json +4 -4
- package/vendor/cli/dist/templates/AGENTS.md.tmpl +11 -4
- package/vendor/cli/dist/templates/README.md.tmpl +1 -1
- package/vendor/cli/dist/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
- package/vendor/cli/dist/vendor/ora/index.d.ts +332 -0
- package/vendor/cli/dist/vendor/ora/index.js +424 -0
- package/vendor/cli/dist/vendor/ora/license +9 -0
- package/vendor/cli/dist/vendor/ora/package.json +64 -0
- package/vendor/cli/dist/vendor/ora/readme.md +329 -0
- package/vendor/cli/package.json +2 -2
- package/vendor/connector-sdk/dist/browser-requirement.d.ts +36 -0
- package/vendor/connector-sdk/dist/browser-requirement.js +60 -0
- package/vendor/connector-sdk/dist/connector-runtime.d.ts +3 -1
- package/vendor/connector-sdk/dist/connector-runtime.js +10 -0
- package/vendor/connector-sdk/dist/connector-types.d.ts +159 -45
- package/vendor/connector-sdk/dist/define-connector.d.ts +4 -2
- package/vendor/connector-sdk/dist/define-connector.js +3 -0
- package/vendor/connector-sdk/dist/index.d.ts +5 -2
- package/vendor/connector-sdk/dist/index.js +1 -0
- package/vendor/connector-sdk/dist/reaction-client-types.d.ts +16 -21
- package/vendor/connector-sdk/dist/reaction-client-types.typecheck.d.ts +5 -0
- package/vendor/connector-sdk/package.json +5 -1
- package/vendor/connector-worker/dist/agent-turn/guest-entry.js +31 -27
- package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +131 -185
- package/vendor/connector-worker/dist/agent-turn/memory.d.ts +3 -2
- package/vendor/connector-worker/dist/agent-turn/memory.js +3 -20
- package/vendor/connector-worker/dist/agent-turn/types.d.ts +3 -15
- package/vendor/connector-worker/dist/agent-turn/workspace.d.ts +2 -2
- package/vendor/connector-worker/dist/agent-turn/workspace.js +2 -4
- package/vendor/connector-worker/dist/daemon/action-monitor.d.ts +12 -0
- package/vendor/connector-worker/dist/daemon/action-monitor.js +25 -0
- package/vendor/connector-worker/dist/daemon/agent-turn.js +0 -9
- package/vendor/connector-worker/dist/daemon/builtins/index.d.ts +12 -0
- package/vendor/connector-worker/dist/daemon/builtins/index.js +61 -1
- package/vendor/connector-worker/dist/daemon/builtins/os-shell.d.ts +2 -1
- package/vendor/connector-worker/dist/daemon/builtins/os-shell.js +40 -27
- package/vendor/connector-worker/dist/daemon/client.d.ts +5 -1
- package/vendor/connector-worker/dist/daemon/client.js +3 -0
- package/vendor/connector-worker/dist/daemon/device-concurrency.d.ts +8 -0
- package/vendor/connector-worker/dist/daemon/device-concurrency.js +13 -0
- package/vendor/connector-worker/dist/daemon/executor.d.ts +1 -1
- package/vendor/connector-worker/dist/daemon/executor.js +48 -117
- package/vendor/connector-worker/dist/daemon/log.d.ts +2 -0
- package/vendor/connector-worker/dist/daemon/log.js +23 -2
- package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +20 -4
- package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +53 -13
- package/vendor/connector-worker/dist/daemon/native-bridge/client.d.ts +6 -0
- package/vendor/connector-worker/dist/daemon/native-bridge/client.js +15 -0
- package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +2 -0
- package/vendor/connector-worker/dist/daemon/native-bridge/protocol.d.ts +1 -1
- package/vendor/connector-worker/dist/daemon/native-bridge/protocol.js +1 -0
- package/vendor/connector-worker/dist/daemon/poll-loop.d.ts +3 -0
- package/vendor/connector-worker/dist/daemon/poll-loop.js +5 -1
- package/vendor/connector-worker/dist/daemon/start.d.ts +1 -0
- package/vendor/connector-worker/dist/daemon/start.js +12 -5
- package/vendor/connector-worker/dist/executor/interface.d.ts +34 -2
- package/vendor/connector-worker/dist/executor/isolate.js +66 -15
- package/vendor/connector-worker/dist/executor/redact.d.ts +1 -0
- package/vendor/connector-worker/dist/executor/redact.js +97 -26
- package/vendor/connector-worker/dist/executor/runtime.d.ts +7 -0
- package/vendor/connector-worker/dist/executor/runtime.js +26 -0
- package/vendor/connector-worker/dist/isolate/bridge.js +3 -1
- package/vendor/connector-worker/dist/mac-device-daemon.js +10 -3
- package/vendor/connector-worker/package.json +2 -2
- package/vendor/core/dist/agent-policy.js +1 -1
- package/vendor/core/dist/agent-store.d.ts +2 -0
- package/vendor/core/dist/agent-tooling.d.ts +1 -1
- package/vendor/core/dist/agent-tooling.js +1 -1
- package/vendor/core/dist/capabilities.js +2 -0
- package/vendor/core/dist/contracts/agent-settings.d.ts +0 -11
- package/vendor/core/dist/contracts/agent-settings.js +1 -7
- package/vendor/core/dist/contracts/tools/collection-presentation.d.ts +93 -0
- package/vendor/core/dist/contracts/tools/collection-presentation.js +88 -0
- package/vendor/core/dist/contracts/tools/collection-selection.d.ts +47 -0
- package/vendor/core/dist/contracts/tools/collection-selection.js +74 -0
- package/vendor/core/dist/contracts/tools/manage-agents.d.ts +10 -4
- package/vendor/core/dist/contracts/tools/manage-auth-profiles.d.ts +1 -1
- package/vendor/core/dist/contracts/tools/manage-auth-profiles.js +5 -1
- package/vendor/core/dist/contracts/tools/manage-automations.d.ts +19 -4
- package/vendor/core/dist/contracts/tools/manage-automations.js +25 -8
- package/vendor/core/dist/contracts/tools/manage-connections.d.ts +1 -0
- package/vendor/core/dist/contracts/tools/manage-connections.js +3 -0
- package/vendor/core/dist/contracts/tools/manage-entity-schema.d.ts +11 -0
- package/vendor/core/dist/contracts/tools/manage-entity-schema.js +42 -3
- package/vendor/core/dist/contracts/tools/manage-entity.d.ts +181 -152
- package/vendor/core/dist/contracts/tools/manage-entity.js +118 -162
- package/vendor/core/dist/contracts/tools/manage-operations.d.ts +41 -3
- package/vendor/core/dist/contracts/tools/manage-operations.js +40 -2
- package/vendor/core/dist/contracts/tools/manage-views.d.ts +182 -7
- package/vendor/core/dist/contracts/tools/manage-views.js +49 -13
- package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +11 -2
- package/vendor/core/dist/contracts/tools/read-knowledge.js +23 -3
- package/vendor/core/dist/contracts/tools/save-memory.d.ts +3 -6
- package/vendor/core/dist/contracts/tools/save-memory.js +2 -6
- package/vendor/core/dist/contracts/tools/view-attach.d.ts +2 -1
- package/vendor/core/dist/contracts/tools/view-attach.js +7 -2
- package/vendor/core/dist/contracts/worker/protocol.d.ts +122 -28
- package/vendor/core/dist/contracts/worker/protocol.js +81 -17
- package/vendor/core/dist/index.d.ts +3 -4
- package/vendor/core/dist/index.js +3 -6
- package/vendor/core/dist/json-template.d.ts +6 -10
- package/vendor/core/dist/json-template.js +5 -9
- package/vendor/core/dist/logger.js +11 -5
- package/vendor/core/dist/shell-safety.d.ts +9 -0
- package/vendor/core/dist/shell-safety.js +154 -0
- package/vendor/core/dist/suggestions.d.ts +1 -1
- package/vendor/core/dist/suggestions.js +1 -1
- package/vendor/core/dist/tool-trace-summary.d.ts +1 -1
- package/vendor/core/dist/tool-trace-summary.js +1 -1
- package/vendor/core/dist/types.d.ts +1 -3
- package/vendor/core/dist/utils/diagnostic-scrubber.d.ts +7 -0
- package/vendor/core/dist/utils/{sentry-scrubber.js → diagnostic-scrubber.js} +25 -53
- package/vendor/core/dist/worker/auth.d.ts +44 -6
- package/vendor/core/dist/worker/auth.js +44 -11
- package/vendor/core/package.json +15 -6
- package/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
- package/vendor/ora/index.d.ts +332 -0
- package/vendor/ora/index.js +424 -0
- package/vendor/ora/license +9 -0
- package/vendor/ora/package.json +53 -0
- package/vendor/ora/readme.md +329 -0
- package/vendor/cli/dist/commands/telemetry.d.ts +0 -10
- package/vendor/cli/dist/commands/telemetry.js +0 -66
- package/vendor/core/dist/sentry.d.ts +0 -26
- package/vendor/core/dist/sentry.js +0 -133
- package/vendor/core/dist/tool-policy.d.ts +0 -74
- package/vendor/core/dist/tool-policy.js +0 -305
- package/vendor/core/dist/utils/sentry-scrubber.d.ts +0 -29
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Google Calendar Connector (V1 runtime)
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Actions are compiled from Calendar's pinned Discovery document — one per
|
|
5
|
+
* API method (see `_google/actions.ts`). The two feeds are declarations over
|
|
6
|
+
* `calendar.events.list` run by the shared syncToken engine
|
|
7
|
+
* (`_google/list-feed.ts`); this file owns only what Google cannot describe:
|
|
8
|
+
* the traversal filters and how an event becomes a memory event.
|
|
6
9
|
*/
|
|
7
10
|
|
|
8
11
|
import {
|
|
@@ -12,12 +15,13 @@ import {
|
|
|
12
15
|
createHttpClient,
|
|
13
16
|
type EventEnvelope,
|
|
14
17
|
type HttpClient,
|
|
15
|
-
type FeedReadContext,
|
|
16
|
-
type FeedReadResult,
|
|
17
18
|
type RuntimeConnectorDefinition,
|
|
18
|
-
type SyncContext,
|
|
19
|
-
type SyncResult,
|
|
20
19
|
} from '@lobu/connector-sdk';
|
|
20
|
+
import { compileGoogleActions, executeGoogleAction, oauthScopes } from './_google/actions';
|
|
21
|
+
import type { DiscoveryDocument } from './_google/discovery';
|
|
22
|
+
import calendarDiscovery from './_google/discovery/calendar_v3.json';
|
|
23
|
+
import { type ListRead, listMethod, listRead, type SyncTokenFeed, syncTokenSync } from './_google/list-feed';
|
|
24
|
+
import { GOOGLE_API_POLICIES } from './_google/policies';
|
|
21
25
|
|
|
22
26
|
// ---------------------------------------------------------------------------
|
|
23
27
|
// Calendar API types
|
|
@@ -43,20 +47,12 @@ interface CalendarEvent {
|
|
|
43
47
|
updated: string;
|
|
44
48
|
}
|
|
45
49
|
|
|
46
|
-
interface CalendarEventListResponse {
|
|
47
|
-
kind: string;
|
|
48
|
-
summary?: string;
|
|
49
|
-
items?: CalendarEvent[];
|
|
50
|
-
nextPageToken?: string;
|
|
51
|
-
nextSyncToken?: string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
50
|
// ---------------------------------------------------------------------------
|
|
55
51
|
// Checkpoint
|
|
56
52
|
// ---------------------------------------------------------------------------
|
|
57
53
|
|
|
58
54
|
/**
|
|
59
|
-
* Leading element of `
|
|
55
|
+
* Leading element of the stored checkpoint's `scope`. Bumping it retires every
|
|
60
56
|
* stored checkpoint, because a cursor is only safe to resume when this exact
|
|
61
57
|
* code minted it. Bumped to 2 when the bootstrap became resumable: before that
|
|
62
58
|
* a capped run advanced the sync token after silently discarding the unread
|
|
@@ -64,33 +60,6 @@ interface CalendarEventListResponse {
|
|
|
64
60
|
*/
|
|
65
61
|
const CHECKPOINT_SCOPE_VERSION = 2;
|
|
66
62
|
|
|
67
|
-
/**
|
|
68
|
-
* Hard ceiling on pages fetched per run, shared by both traversals. At 250
|
|
69
|
-
* events/page that is 50k events — more than any reasonable calendar window,
|
|
70
|
-
* and the only bound when every item on a page is filtered out.
|
|
71
|
-
*/
|
|
72
|
-
const MAX_SYNC_PAGES = 200;
|
|
73
|
-
|
|
74
|
-
interface CalendarCheckpoint {
|
|
75
|
-
/**
|
|
76
|
-
* Query identity this checkpoint's cursors belong to, as
|
|
77
|
-
* `[CHECKPOINT_SCOPE_VERSION, calendarId, lookbackDays]`. A checkpoint belongs
|
|
78
|
-
* to one configured feed; changing its calendar or lookback invalidates
|
|
79
|
-
* both cursors below.
|
|
80
|
-
* Absent or mismatched means "start a fresh bootstrap".
|
|
81
|
-
*/
|
|
82
|
-
scope?: string;
|
|
83
|
-
/**
|
|
84
|
-
* Unfinished traversal: the `events.list` query as first issued (so the
|
|
85
|
-
* `timeMin`/`timeMax` window stays fixed across runs, which the page token
|
|
86
|
-
* requires) plus the token for the page still to fetch. Absent once the
|
|
87
|
-
* traversal has reached the last page.
|
|
88
|
-
*/
|
|
89
|
-
pending?: { params: string; page_token: string };
|
|
90
|
-
sync_token?: string;
|
|
91
|
-
last_sync_at?: string;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
63
|
interface CalendarConfig extends Record<string, unknown> {
|
|
95
64
|
calendar_id?: string;
|
|
96
65
|
query?: string;
|
|
@@ -115,34 +84,193 @@ const CALENDAR_EVENT_COLUMNS = [
|
|
|
115
84
|
{ name: 'url', type: 'text' },
|
|
116
85
|
] as const;
|
|
117
86
|
|
|
87
|
+
function calendarEventToRow(calEvent: CalendarEvent): Record<string, unknown> | null {
|
|
88
|
+
if (calEvent.status === 'cancelled') return null;
|
|
89
|
+
const startTime = calEvent.start.dateTime || calEvent.start.date;
|
|
90
|
+
if (!startTime) return null;
|
|
91
|
+
const endTime = calEvent.end.dateTime || calEvent.end.date || '';
|
|
92
|
+
return {
|
|
93
|
+
id: calEvent.id,
|
|
94
|
+
summary: calEvent.summary || '(no title)',
|
|
95
|
+
description: calEvent.description || '',
|
|
96
|
+
location: calEvent.location || '',
|
|
97
|
+
status: calEvent.status,
|
|
98
|
+
start_time: startTime,
|
|
99
|
+
end_time: endTime,
|
|
100
|
+
all_day: !calEvent.start.dateTime,
|
|
101
|
+
organizer: calEvent.organizer?.displayName || calEvent.organizer?.email || '',
|
|
102
|
+
attendee_count: calEvent.attendees?.length ?? 0,
|
|
103
|
+
created_at: calEvent.created,
|
|
104
|
+
updated_at: calEvent.updated,
|
|
105
|
+
url: calEvent.htmlLink,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Description, then attendees: the text an event is searched and embedded by. */
|
|
110
|
+
function eventPayloadText(calEvent: CalendarEvent): string {
|
|
111
|
+
const attendees = (calEvent.attendees ?? []).map((a) => a.displayName || a.email).join(', ');
|
|
112
|
+
return [calEvent.description, attendees && `Attendees: ${attendees}`].filter(Boolean).join('\n\n');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function calendarEventToChangeEnvelope(calEvent: CalendarEvent): EventEnvelope {
|
|
116
|
+
const startTime = calEvent.start?.dateTime || calEvent.start?.date;
|
|
117
|
+
const endTime = calEvent.end?.dateTime || calEvent.end?.date;
|
|
118
|
+
const changedAt = new Date(calEvent.updated || startTime || Date.now());
|
|
119
|
+
const occurredAt = Number.isNaN(changedAt.getTime()) ? new Date() : changedAt;
|
|
120
|
+
const isCancelled = calEvent.status === 'cancelled';
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
origin_id: calEvent.id,
|
|
124
|
+
title: calEvent.summary || (isCancelled ? '(cancelled calendar event)' : '(no title)'),
|
|
125
|
+
payload_text: eventPayloadText(calEvent),
|
|
126
|
+
author_name: calEvent.organizer?.displayName || calEvent.organizer?.email,
|
|
127
|
+
source_url: calEvent.htmlLink,
|
|
128
|
+
occurred_at: occurredAt,
|
|
129
|
+
origin_type: 'calendar_event',
|
|
130
|
+
metadata: {
|
|
131
|
+
status: calEvent.status,
|
|
132
|
+
change_type: isCancelled ? 'cancelled' : 'upserted',
|
|
133
|
+
...(calEvent.location ? { location: calEvent.location } : {}),
|
|
134
|
+
...(calEvent.organizer?.email ? { organizer: calEvent.organizer.email } : {}),
|
|
135
|
+
attendee_count: calEvent.attendees?.length ?? 0,
|
|
136
|
+
...(startTime ? { start_time: startTime } : {}),
|
|
137
|
+
...(endTime ? { end_time: endTime } : {}),
|
|
138
|
+
all_day: Boolean(startTime && !calEvent.start?.dateTime),
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function calendarEventToEnvelope(calEvent: CalendarEvent): EventEnvelope | null {
|
|
144
|
+
if (calEvent.status === 'cancelled') return null;
|
|
145
|
+
|
|
146
|
+
const startTime = calEvent.start.dateTime || calEvent.start.date;
|
|
147
|
+
if (!startTime) return null;
|
|
148
|
+
|
|
149
|
+
const occurredAt = new Date(startTime);
|
|
150
|
+
if (Number.isNaN(occurredAt.getTime())) return null;
|
|
151
|
+
|
|
152
|
+
const isAllDay = !calEvent.start.dateTime;
|
|
153
|
+
const endTime = calEvent.end.dateTime || calEvent.end.date;
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
origin_id: calEvent.id,
|
|
157
|
+
title: calEvent.summary || '(no title)',
|
|
158
|
+
payload_text: eventPayloadText(calEvent),
|
|
159
|
+
author_name: calEvent.organizer?.displayName || calEvent.organizer?.email,
|
|
160
|
+
source_url: calEvent.htmlLink,
|
|
161
|
+
occurred_at: occurredAt,
|
|
162
|
+
origin_type: 'calendar_event',
|
|
163
|
+
metadata: {
|
|
164
|
+
status: calEvent.status,
|
|
165
|
+
...(calEvent.location ? { location: calEvent.location } : {}),
|
|
166
|
+
...(calEvent.organizer?.email ? { organizer: calEvent.organizer.email } : {}),
|
|
167
|
+
attendee_count: calEvent.attendees?.length ?? 0,
|
|
168
|
+
start_time: startTime,
|
|
169
|
+
...(endTime ? { end_time: endTime } : {}),
|
|
170
|
+
all_day: isAllDay,
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---------------------------------------------------------------------------
|
|
176
|
+
// Discovery-compiled actions and feed declarations
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
|
|
179
|
+
const CALENDAR_DOC = calendarDiscovery as unknown as DiscoveryDocument;
|
|
180
|
+
const CALENDAR_POLICY = GOOGLE_API_POLICIES.calendar_v3;
|
|
181
|
+
const CALENDAR = compileGoogleActions(CALENDAR_DOC, CALENDAR_POLICY);
|
|
182
|
+
const EVENTS_LIST = listMethod(CALENDAR_DOC, 'calendar.events.list');
|
|
183
|
+
|
|
184
|
+
const calendarId = (config: CalendarConfig) => config.calendar_id || 'primary';
|
|
185
|
+
const lookbackDays = (config: CalendarConfig) => config.lookback_days ?? 30;
|
|
186
|
+
const daysFrom = (now: Date, days: number) => new Date(now.getTime() + days * 86_400_000).toISOString();
|
|
187
|
+
|
|
188
|
+
/** `events`: the calendar as it stands. Cancelled events never surface. */
|
|
189
|
+
const EVENTS_FEED: SyncTokenFeed<CalendarConfig> = {
|
|
190
|
+
list: EVENTS_LIST,
|
|
191
|
+
path: (config) => ({ calendarId: calendarId(config) }),
|
|
192
|
+
pageSize: 250,
|
|
193
|
+
scope: (config) => [CHECKPOINT_SCOPE_VERSION, calendarId(config), lookbackDays(config)],
|
|
194
|
+
budget: (config) => Math.min(config.max_results ?? 100, 2500),
|
|
195
|
+
// No `orderBy`: Google omits `nextSyncToken` from any query that orders its
|
|
196
|
+
// results, which left this feed re-listing its whole window every run. A
|
|
197
|
+
// sync has no use for order; live reads (EVENTS_READ) keep `startTime`.
|
|
198
|
+
bootstrap: (config, now) => ({
|
|
199
|
+
singleEvents: 'true',
|
|
200
|
+
timeMin: daysFrom(now, -lookbackDays(config)),
|
|
201
|
+
timeMax: daysFrom(now, 365),
|
|
202
|
+
}),
|
|
203
|
+
// Google binds the bootstrap's non-window parameters to the token it mints.
|
|
204
|
+
incremental: () => ({ singleEvents: 'true' }),
|
|
205
|
+
toEnvelope: (item) => calendarEventToEnvelope(item as unknown as CalendarEvent),
|
|
206
|
+
};
|
|
207
|
+
|
|
118
208
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* Two shapes qualify:
|
|
123
|
-
* - 410 GONE — Google's documented signal that an incremental sync token has
|
|
124
|
-
* expired or been invalidated.
|
|
125
|
-
* - 403 with `insufficientPermissions` / `ACCESS_TOKEN_SCOPE_INSUFFICIENT` —
|
|
126
|
-
* a token minted under a *previous* grant. Google validates the syncToken
|
|
127
|
-
* against the grant that produced it, so re-authorizing the connection with
|
|
128
|
-
* the same scopes leaves the old token permanently rejected even though the
|
|
129
|
-
* live credentials are fine.
|
|
130
|
-
*
|
|
131
|
-
* In both cases the recovery is identical and is what Google documents: drop
|
|
132
|
-
* the token and perform a full resync.
|
|
133
|
-
*
|
|
134
|
-
* A genuinely-missing scope produces the *same* 403 body, so this predicate
|
|
135
|
-
* cannot distinguish them on its own — the caller resolves the ambiguity by
|
|
136
|
-
* retrying exactly once without the token. If the scope really is missing the
|
|
137
|
-
* full sync fails the same way and that error propagates, which is why the
|
|
138
|
-
* retry is bounded to a single attempt and never becomes a resync loop.
|
|
209
|
+
* `changes`: every change, cancellations included, for Automations. Google
|
|
210
|
+
* allows `singleEvents`/`showDeleted` alongside a syncToken, and the feed
|
|
211
|
+
* fails closed rather than finishing a traversal it could not resume from.
|
|
139
212
|
*/
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
213
|
+
const CHANGES_FEED: SyncTokenFeed<CalendarConfig> = {
|
|
214
|
+
...EVENTS_FEED,
|
|
215
|
+
bootstrap: (config, now) => ({
|
|
216
|
+
singleEvents: 'true',
|
|
217
|
+
showDeleted: 'true',
|
|
218
|
+
timeMin: daysFrom(now, -lookbackDays(config)),
|
|
219
|
+
}),
|
|
220
|
+
incremental: () => ({ singleEvents: 'true', showDeleted: 'true' }),
|
|
221
|
+
requireSyncToken: true,
|
|
222
|
+
toEnvelope: (item) => calendarEventToChangeEnvelope(item as unknown as CalendarEvent),
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const EVENTS_READ: ListRead<CalendarConfig> = {
|
|
226
|
+
list: EVENTS_LIST,
|
|
227
|
+
path: (config) => ({ calendarId: calendarId(config) }),
|
|
228
|
+
maxPageSize: 250,
|
|
229
|
+
columns: [...CALENDAR_EVENT_COLUMNS],
|
|
230
|
+
toRow: (item) => calendarEventToRow(item as unknown as CalendarEvent),
|
|
231
|
+
params: (ctx) => {
|
|
232
|
+
if (ctx.sort && !(ctx.sort.column === 'start_time' && ctx.sort.order === 'asc')) {
|
|
233
|
+
throw new Error(
|
|
234
|
+
"Google Calendar source reads only support sort {column:'start_time', order:'asc'}."
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
if ((ctx.offset ?? 0) > 0) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
'Google Calendar source reads paginate with the returned cursor, not an offset.'
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
const requested = Math.min(Math.max(Math.trunc(ctx.limit ?? 50), 1), 2500);
|
|
243
|
+
const configured = Math.min(Math.max(Math.trunc(ctx.config.max_results ?? 100), 1), 2500);
|
|
244
|
+
const now = new Date();
|
|
245
|
+
const q = [ctx.config.query, ctx.query].map((part) => part?.trim()).filter(Boolean).join(' ');
|
|
246
|
+
return {
|
|
247
|
+
maxResults: String(Math.min(250, requested, configured)),
|
|
248
|
+
orderBy: 'startTime',
|
|
249
|
+
singleEvents: 'true',
|
|
250
|
+
timeMin: daysFrom(now, -lookbackDays(ctx.config)),
|
|
251
|
+
timeMax: daysFrom(now, ctx.config.lookahead_days ?? 365),
|
|
252
|
+
...(q ? { q } : {}),
|
|
253
|
+
};
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const EVENT_METADATA_SCHEMA = {
|
|
258
|
+
type: 'object',
|
|
259
|
+
properties: {
|
|
260
|
+
status: { type: 'string' },
|
|
261
|
+
location: { type: 'string' },
|
|
262
|
+
organizer: { type: 'string' },
|
|
263
|
+
attendee_count: { type: 'number' },
|
|
264
|
+
start_time: { type: 'string' },
|
|
265
|
+
end_time: { type: 'string' },
|
|
266
|
+
all_day: { type: 'boolean' },
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
function requireToken(credentials: { accessToken?: string } | null): string {
|
|
271
|
+
const token = credentials?.accessToken;
|
|
272
|
+
if (!token) throw new Error('Google Calendar requires Google OAuth credentials.');
|
|
273
|
+
return token;
|
|
146
274
|
}
|
|
147
275
|
|
|
148
276
|
// ---------------------------------------------------------------------------
|
|
@@ -153,7 +281,7 @@ export default class GoogleCalendarConnector extends ConnectorRuntime<Record<str
|
|
|
153
281
|
readonly definition: RuntimeConnectorDefinition<Record<string, unknown>, CalendarConfig> = {
|
|
154
282
|
key: 'google.calendar',
|
|
155
283
|
name: 'Google Calendar',
|
|
156
|
-
description: 'Syncs
|
|
284
|
+
description: 'Syncs Google Calendar events and exposes supported Calendar API methods as actions.',
|
|
157
285
|
version: '1.1.2',
|
|
158
286
|
faviconDomain: 'calendar.google.com',
|
|
159
287
|
authSchema: {
|
|
@@ -161,8 +289,7 @@ export default class GoogleCalendarConnector extends ConnectorRuntime<Record<str
|
|
|
161
289
|
{
|
|
162
290
|
type: 'oauth',
|
|
163
291
|
provider: 'google',
|
|
164
|
-
|
|
165
|
-
optionalScopes: ['https://www.googleapis.com/auth/calendar.events'],
|
|
292
|
+
...oauthScopes(CALENDAR_POLICY),
|
|
166
293
|
loginScopes: ['openid', 'email', 'profile'],
|
|
167
294
|
clientIdKey: 'GOOGLE_CLIENT_ID',
|
|
168
295
|
clientSecretKey: 'GOOGLE_CLIENT_SECRET',
|
|
@@ -181,8 +308,8 @@ export default class GoogleCalendarConnector extends ConnectorRuntime<Record<str
|
|
|
181
308
|
requiredScopes: ['https://www.googleapis.com/auth/calendar.readonly'],
|
|
182
309
|
description:
|
|
183
310
|
'Google Calendar events can sync into memory and be read directly from Google.',
|
|
184
|
-
sync: (ctx) => this.
|
|
185
|
-
read: (ctx) => this.
|
|
311
|
+
sync: (ctx) => syncTokenSync(EVENTS_FEED, ctx, this.client(requireToken(ctx.credentials))),
|
|
312
|
+
read: (ctx) => listRead(EVENTS_READ, ctx, this.client(requireToken(ctx.credentials))),
|
|
186
313
|
configSchema: {
|
|
187
314
|
type: 'object',
|
|
188
315
|
properties: {
|
|
@@ -212,18 +339,7 @@ export default class GoogleCalendarConnector extends ConnectorRuntime<Record<str
|
|
|
212
339
|
eventKinds: {
|
|
213
340
|
calendar_event: {
|
|
214
341
|
description: 'A Google Calendar event',
|
|
215
|
-
metadataSchema:
|
|
216
|
-
type: 'object',
|
|
217
|
-
properties: {
|
|
218
|
-
status: { type: 'string' },
|
|
219
|
-
location: { type: 'string' },
|
|
220
|
-
organizer: { type: 'string' },
|
|
221
|
-
attendee_count: { type: 'number' },
|
|
222
|
-
start_time: { type: 'string' },
|
|
223
|
-
end_time: { type: 'string' },
|
|
224
|
-
all_day: { type: 'boolean' },
|
|
225
|
-
},
|
|
226
|
-
},
|
|
342
|
+
metadataSchema: EVENT_METADATA_SCHEMA,
|
|
227
343
|
},
|
|
228
344
|
},
|
|
229
345
|
},
|
|
@@ -233,7 +349,7 @@ export default class GoogleCalendarConnector extends ConnectorRuntime<Record<str
|
|
|
233
349
|
requiredScopes: ['https://www.googleapis.com/auth/calendar.readonly'],
|
|
234
350
|
description:
|
|
235
351
|
'Durable incremental Google Calendar changes for Automations and event-driven workflows.',
|
|
236
|
-
sync: (ctx) => this.
|
|
352
|
+
sync: (ctx) => syncTokenSync(CHANGES_FEED, ctx, this.client(requireToken(ctx.credentials))),
|
|
237
353
|
configSchema: {
|
|
238
354
|
type: 'object',
|
|
239
355
|
properties: {
|
|
@@ -256,648 +372,20 @@ export default class GoogleCalendarConnector extends ConnectorRuntime<Record<str
|
|
|
256
372
|
eventKinds: {
|
|
257
373
|
calendar_event: {
|
|
258
374
|
description: 'A Google Calendar event change',
|
|
259
|
-
metadataSchema:
|
|
260
|
-
type: 'object',
|
|
261
|
-
properties: {
|
|
262
|
-
status: { type: 'string' },
|
|
263
|
-
location: { type: 'string' },
|
|
264
|
-
organizer: { type: 'string' },
|
|
265
|
-
attendee_count: { type: 'number' },
|
|
266
|
-
start_time: { type: 'string' },
|
|
267
|
-
end_time: { type: 'string' },
|
|
268
|
-
all_day: { type: 'boolean' },
|
|
269
|
-
},
|
|
270
|
-
},
|
|
271
|
-
},
|
|
272
|
-
},
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
actions: {
|
|
276
|
-
create_event: {
|
|
277
|
-
key: 'create_event',
|
|
278
|
-
name: 'Create Event',
|
|
279
|
-
description: 'Create a new event on Google Calendar.',
|
|
280
|
-
requiredScopes: ['https://www.googleapis.com/auth/calendar.events'],
|
|
281
|
-
inputSchema: {
|
|
282
|
-
type: 'object',
|
|
283
|
-
required: ['summary', 'start', 'end'],
|
|
284
|
-
properties: {
|
|
285
|
-
summary: { type: 'string', description: 'Event title.' },
|
|
286
|
-
start: { type: 'string', description: 'Start time (ISO 8601 datetime).' },
|
|
287
|
-
end: { type: 'string', description: 'End time (ISO 8601 datetime).' },
|
|
288
|
-
description: { type: 'string', description: 'Event description.' },
|
|
289
|
-
location: { type: 'string', description: 'Event location.' },
|
|
290
|
-
attendees: {
|
|
291
|
-
type: 'string',
|
|
292
|
-
description: 'Comma-separated attendee email addresses.',
|
|
293
|
-
},
|
|
294
|
-
calendar_id: {
|
|
295
|
-
type: 'string',
|
|
296
|
-
description: 'Calendar ID (default: "primary").',
|
|
297
|
-
},
|
|
298
|
-
},
|
|
299
|
-
},
|
|
300
|
-
},
|
|
301
|
-
update_event: {
|
|
302
|
-
key: 'update_event',
|
|
303
|
-
name: 'Update Event',
|
|
304
|
-
description: 'Update an existing calendar event.',
|
|
305
|
-
requiredScopes: ['https://www.googleapis.com/auth/calendar.events'],
|
|
306
|
-
inputSchema: {
|
|
307
|
-
type: 'object',
|
|
308
|
-
required: ['event_id'],
|
|
309
|
-
properties: {
|
|
310
|
-
event_id: { type: 'string', description: 'Event ID to update.' },
|
|
311
|
-
calendar_id: {
|
|
312
|
-
type: 'string',
|
|
313
|
-
description: 'Calendar ID (default: "primary").',
|
|
314
|
-
},
|
|
315
|
-
summary: { type: 'string', description: 'Event title.' },
|
|
316
|
-
start: { type: 'string', description: 'Start time (ISO 8601 datetime).' },
|
|
317
|
-
end: { type: 'string', description: 'End time (ISO 8601 datetime).' },
|
|
318
|
-
description: { type: 'string', description: 'Event description.' },
|
|
319
|
-
location: { type: 'string', description: 'Event location.' },
|
|
320
|
-
},
|
|
321
|
-
},
|
|
322
|
-
},
|
|
323
|
-
delete_event: {
|
|
324
|
-
key: 'delete_event',
|
|
325
|
-
name: 'Delete Event',
|
|
326
|
-
description: 'Delete/cancel an event.',
|
|
327
|
-
annotations: {
|
|
328
|
-
destructiveHint: true,
|
|
329
|
-
},
|
|
330
|
-
requiredScopes: ['https://www.googleapis.com/auth/calendar.events'],
|
|
331
|
-
inputSchema: {
|
|
332
|
-
type: 'object',
|
|
333
|
-
required: ['event_id'],
|
|
334
|
-
properties: {
|
|
335
|
-
event_id: { type: 'string', description: 'Event ID to delete.' },
|
|
336
|
-
calendar_id: {
|
|
337
|
-
type: 'string',
|
|
338
|
-
description: 'Calendar ID (default: "primary").',
|
|
339
|
-
},
|
|
340
|
-
},
|
|
341
|
-
},
|
|
342
|
-
},
|
|
343
|
-
get_event: {
|
|
344
|
-
key: 'get_event',
|
|
345
|
-
kind: 'read',
|
|
346
|
-
name: 'Get Event',
|
|
347
|
-
description: 'Get full event details.',
|
|
348
|
-
inputSchema: {
|
|
349
|
-
type: 'object',
|
|
350
|
-
required: ['event_id'],
|
|
351
|
-
properties: {
|
|
352
|
-
event_id: { type: 'string', description: 'Event ID to retrieve.' },
|
|
353
|
-
calendar_id: {
|
|
354
|
-
type: 'string',
|
|
355
|
-
description: 'Calendar ID (default: "primary").',
|
|
356
|
-
},
|
|
375
|
+
metadataSchema: EVENT_METADATA_SCHEMA,
|
|
357
376
|
},
|
|
358
377
|
},
|
|
359
378
|
},
|
|
360
379
|
},
|
|
380
|
+
actions: CALENDAR.actions,
|
|
361
381
|
};
|
|
362
382
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
// -------------------------------------------------------------------------
|
|
366
|
-
// Direct source read — current state, never persisted.
|
|
367
|
-
// -------------------------------------------------------------------------
|
|
368
|
-
|
|
369
|
-
private async readFeed(ctx: FeedReadContext<CalendarConfig>): Promise<FeedReadResult> {
|
|
370
|
-
const token = ctx.credentials?.accessToken;
|
|
371
|
-
if (!token) {
|
|
372
|
-
throw new Error('Google Calendar source reads require Google OAuth credentials.');
|
|
373
|
-
}
|
|
374
|
-
if (ctx.sort && !(ctx.sort.column === 'start_time' && ctx.sort.order === 'asc')) {
|
|
375
|
-
throw new Error(
|
|
376
|
-
"Google Calendar source reads only support sort {column:'start_time', order:'asc'}."
|
|
377
|
-
);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
const calendarId = ctx.config.calendar_id || 'primary';
|
|
381
|
-
const lookbackDays = ctx.config.lookback_days ?? 30;
|
|
382
|
-
const lookaheadDays = ctx.config.lookahead_days ?? 365;
|
|
383
|
-
const requestedLimit = Math.min(Math.max(Math.trunc(ctx.limit ?? 50), 1), 2500);
|
|
384
|
-
const configuredLimit = Math.min(
|
|
385
|
-
Math.max(Math.trunc(ctx.config.max_results ?? 100), 1),
|
|
386
|
-
2500
|
|
387
|
-
);
|
|
388
|
-
const limit = Math.min(requestedLimit, configuredLimit);
|
|
389
|
-
const offset = Math.max(Math.trunc(ctx.offset ?? 0), 0);
|
|
390
|
-
if (offset > 0) {
|
|
391
|
-
throw new Error(
|
|
392
|
-
'Google Calendar source reads paginate with the returned cursor, not an offset.'
|
|
393
|
-
);
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
const timeMin = new Date();
|
|
397
|
-
timeMin.setDate(timeMin.getDate() - lookbackDays);
|
|
398
|
-
const timeMax = new Date();
|
|
399
|
-
timeMax.setDate(timeMax.getDate() + lookaheadDays);
|
|
400
|
-
|
|
401
|
-
const queryParts = [ctx.config.query, ctx.query].map((part) => part?.trim()).filter(Boolean);
|
|
402
|
-
const q = queryParts.join(' ');
|
|
403
|
-
const http = this.client(token);
|
|
404
|
-
const params = new URLSearchParams({
|
|
405
|
-
maxResults: String(Math.min(250, limit)),
|
|
406
|
-
orderBy: 'startTime',
|
|
407
|
-
singleEvents: 'true',
|
|
408
|
-
timeMin: timeMin.toISOString(),
|
|
409
|
-
timeMax: timeMax.toISOString(),
|
|
410
|
-
});
|
|
411
|
-
if (q) params.set('q', q);
|
|
412
|
-
if (ctx.cursor) params.set('pageToken', ctx.cursor);
|
|
413
|
-
|
|
414
|
-
const url =
|
|
415
|
-
this.BASE_URL +
|
|
416
|
-
'/calendars/' +
|
|
417
|
-
encodeURIComponent(calendarId) +
|
|
418
|
-
'/events?' +
|
|
419
|
-
params.toString();
|
|
420
|
-
const response = await http.raw(url);
|
|
421
|
-
if (!response.ok) {
|
|
422
|
-
throw new Error(
|
|
423
|
-
'Calendar events.list error (' + response.status + '): ' + (await response.text())
|
|
424
|
-
);
|
|
425
|
-
}
|
|
426
|
-
const data = (await response.json()) as CalendarEventListResponse;
|
|
427
|
-
const rows = (data.items ?? [])
|
|
428
|
-
.map((event) => this.calendarEventToRow(event))
|
|
429
|
-
.filter((row): row is Record<string, unknown> => row !== null);
|
|
430
|
-
|
|
431
|
-
return {
|
|
432
|
-
rows,
|
|
433
|
-
columns: [...CALENDAR_EVENT_COLUMNS],
|
|
434
|
-
nextCursor: data.nextPageToken,
|
|
435
|
-
hasMore: Boolean(data.nextPageToken),
|
|
436
|
-
};
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
// -------------------------------------------------------------------------
|
|
440
|
-
// sync
|
|
441
|
-
// -------------------------------------------------------------------------
|
|
442
|
-
|
|
443
|
-
private async syncFeed(ctx: SyncContext): Promise<SyncResult> {
|
|
383
|
+
async execute(ctx: ActionContext): Promise<ActionResult> {
|
|
444
384
|
const token = ctx.credentials?.accessToken;
|
|
445
385
|
if (!token) {
|
|
446
|
-
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
const http = this.client(token);
|
|
450
|
-
const calendarId = (ctx.config.calendar_id as string) || 'primary';
|
|
451
|
-
const maxResults = Math.min((ctx.config.max_results as number) ?? 100, 2500);
|
|
452
|
-
const lookbackDays = (ctx.config.lookback_days as number) ?? 30;
|
|
453
|
-
|
|
454
|
-
const checkpoint = (ctx.checkpoint ?? {}) as CalendarCheckpoint;
|
|
455
|
-
const durableChanges = ctx.feedKey === 'changes';
|
|
456
|
-
|
|
457
|
-
// A checkpoint minted under a different scope (or by an older version that
|
|
458
|
-
// truncated its bootstrap) cannot be resumed: revisit the configured
|
|
459
|
-
// lookback once instead. Origin IDs are unchanged, so the replay supersedes
|
|
460
|
-
// the existing rows rather than duplicating them.
|
|
461
|
-
const scope = JSON.stringify([CHECKPOINT_SCOPE_VERSION, calendarId, lookbackDays]);
|
|
462
|
-
const resumable = checkpoint.scope === scope;
|
|
463
|
-
|
|
464
|
-
/** Traversal reached the last page: commit it with its cursor and stamp the run. */
|
|
465
|
-
const finish = async (items: EventEnvelope[], syncToken?: string): Promise<SyncResult> => {
|
|
466
|
-
await ctx.commit(items, {
|
|
467
|
-
...this.completedCheckpoint(syncToken),
|
|
468
|
-
scope,
|
|
469
|
-
} as Record<string, unknown>);
|
|
470
|
-
return { status: 'complete' };
|
|
471
|
-
};
|
|
472
|
-
|
|
473
|
-
const syncToken = resumable ? checkpoint.sync_token : undefined;
|
|
474
|
-
// Bootstrap and incremental traversal share the same page commit boundary.
|
|
475
|
-
// An incremental page keeps its original sync token until the final page.
|
|
476
|
-
const timeMin = new Date();
|
|
477
|
-
timeMin.setDate(timeMin.getDate() - lookbackDays);
|
|
478
|
-
const timeMax = new Date();
|
|
479
|
-
timeMax.setDate(timeMax.getDate() + 365); // Include future events
|
|
480
|
-
|
|
481
|
-
const pending = resumable ? checkpoint.pending : undefined;
|
|
482
|
-
// Google permits timeMin on the initial full sync but forbids it with
|
|
483
|
-
// syncToken. Once issued, the query is replayed verbatim on every resuming
|
|
484
|
-
// run: a page token is only valid against the window that minted it.
|
|
485
|
-
const params = pending
|
|
486
|
-
? new URLSearchParams(pending.params)
|
|
487
|
-
: syncToken
|
|
488
|
-
? new URLSearchParams({
|
|
489
|
-
maxResults: '250',
|
|
490
|
-
syncToken,
|
|
491
|
-
...(durableChanges ? { singleEvents: 'true', showDeleted: 'true' } : {}),
|
|
492
|
-
})
|
|
493
|
-
: durableChanges
|
|
494
|
-
? new URLSearchParams({
|
|
495
|
-
maxResults: '250',
|
|
496
|
-
singleEvents: 'true',
|
|
497
|
-
showDeleted: 'true',
|
|
498
|
-
timeMin: timeMin.toISOString(),
|
|
499
|
-
})
|
|
500
|
-
: new URLSearchParams({
|
|
501
|
-
maxResults: '250',
|
|
502
|
-
orderBy: 'startTime',
|
|
503
|
-
singleEvents: 'true',
|
|
504
|
-
timeMin: timeMin.toISOString(),
|
|
505
|
-
timeMax: timeMax.toISOString(),
|
|
506
|
-
});
|
|
507
|
-
const baseParams = params.toString();
|
|
508
|
-
|
|
509
|
-
/**
|
|
510
|
-
* Mid-traversal: preserve the old sync token and completed watermark,
|
|
511
|
-
* alongside the next page and the exact query that minted it.
|
|
512
|
-
* A run that stops here resumes from `token`.
|
|
513
|
-
*/
|
|
514
|
-
const parked = (token: string) =>
|
|
515
|
-
({
|
|
516
|
-
scope,
|
|
517
|
-
...(syncToken ? { sync_token: syncToken, last_sync_at: checkpoint.last_sync_at } : {}),
|
|
518
|
-
pending: { params: baseParams, page_token: token },
|
|
519
|
-
}) as Record<string, unknown>;
|
|
520
|
-
|
|
521
|
-
let collected = 0;
|
|
522
|
-
let pageToken = pending?.page_token;
|
|
523
|
-
const seenTokens = new Set<string>();
|
|
524
|
-
for (let page = 0; ; page++) {
|
|
525
|
-
if (pageToken) {
|
|
526
|
-
seenTokens.add(pageToken);
|
|
527
|
-
params.set('pageToken', pageToken);
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
const url = `${this.BASE_URL}/calendars/${encodeURIComponent(calendarId)}/events?${params.toString()}`;
|
|
531
|
-
const response = await http.raw(url);
|
|
532
|
-
if (!response.ok) {
|
|
533
|
-
const body = await response.text();
|
|
534
|
-
if (syncToken && isSyncTokenRejection(response.status, body)) {
|
|
535
|
-
// Discard both tokens together. Replayed items keep their origin IDs;
|
|
536
|
-
// a failed bootstrap remains retryable without treating it as complete.
|
|
537
|
-
return this.syncFeed({ ...ctx, checkpoint: { scope } });
|
|
538
|
-
}
|
|
539
|
-
throw new Error(`Calendar events.list error (${response.status}): ${body}`);
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
const data = (await response.json()) as CalendarEventListResponse;
|
|
543
|
-
const items = data.items ?? [];
|
|
544
|
-
// A provider page is atomic: `max_results` caps how much a run starts,
|
|
545
|
-
// never how much of a fetched page is stored. Dropping a page's tail
|
|
546
|
-
// would lose those events for good once the cursor moved past them.
|
|
547
|
-
const events: EventEnvelope[] = [];
|
|
548
|
-
for (const calEvent of items) {
|
|
549
|
-
const envelope = durableChanges
|
|
550
|
-
? this.calendarEventToChangeEnvelope(calEvent)
|
|
551
|
-
: this.calendarEventToEnvelope(calEvent);
|
|
552
|
-
if (envelope) events.push(envelope);
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
// Google only returns nextSyncToken on the LAST page (no nextPageToken),
|
|
556
|
-
// so the traversal has to reach it before the feed can go incremental.
|
|
557
|
-
if (!data.nextPageToken) {
|
|
558
|
-
if (durableChanges && !data.nextSyncToken) {
|
|
559
|
-
throw new Error(
|
|
560
|
-
'Google Calendar changes traversal completed without a durable sync token.'
|
|
561
|
-
);
|
|
562
|
-
}
|
|
563
|
-
return finish(events, data.nextSyncToken);
|
|
564
|
-
}
|
|
565
|
-
if (seenTokens.has(data.nextPageToken)) {
|
|
566
|
-
throw new Error('Google Calendar returned a repeated page token.');
|
|
567
|
-
}
|
|
568
|
-
pageToken = data.nextPageToken;
|
|
569
|
-
await ctx.commit(events, parked(pageToken));
|
|
570
|
-
collected += events.length;
|
|
571
|
-
|
|
572
|
-
// Stop once this run has collected its share, once the provider returns
|
|
573
|
-
// an empty page, or at the hard ceiling — the last of which is the only
|
|
574
|
-
// bound on a window of nothing but filtered-out items, where
|
|
575
|
-
// `collected` never reaches the cap. The traversal continues next run.
|
|
576
|
-
if (
|
|
577
|
-
(!syncToken && collected >= maxResults) ||
|
|
578
|
-
items.length === 0 ||
|
|
579
|
-
page + 1 >= MAX_SYNC_PAGES
|
|
580
|
-
) {
|
|
581
|
-
return { status: 'more' };
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
// -------------------------------------------------------------------------
|
|
587
|
-
// execute
|
|
588
|
-
// -------------------------------------------------------------------------
|
|
589
|
-
|
|
590
|
-
async execute(ctx: ActionContext): Promise<ActionResult> {
|
|
591
|
-
try {
|
|
592
|
-
const token = ctx.credentials?.accessToken;
|
|
593
|
-
if (!token) {
|
|
594
|
-
return {
|
|
595
|
-
success: false,
|
|
596
|
-
error: 'Google Calendar actions require Google OAuth credentials.',
|
|
597
|
-
};
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
const http = this.client(token);
|
|
601
|
-
|
|
602
|
-
switch (ctx.actionKey) {
|
|
603
|
-
case 'create_event':
|
|
604
|
-
return await this.createEvent(http, ctx.input);
|
|
605
|
-
case 'update_event':
|
|
606
|
-
return await this.updateEvent(http, ctx.input);
|
|
607
|
-
case 'delete_event':
|
|
608
|
-
return await this.deleteEvent(http, ctx.input);
|
|
609
|
-
case 'get_event':
|
|
610
|
-
return await this.getEvent(http, ctx.input);
|
|
611
|
-
default:
|
|
612
|
-
return { success: false, error: `Unknown action: ${ctx.actionKey}` };
|
|
613
|
-
}
|
|
614
|
-
} catch (error) {
|
|
615
|
-
return {
|
|
616
|
-
success: false,
|
|
617
|
-
error: error instanceof Error ? error.message : String(error),
|
|
618
|
-
};
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
// -------------------------------------------------------------------------
|
|
623
|
-
// Actions
|
|
624
|
-
// -------------------------------------------------------------------------
|
|
625
|
-
|
|
626
|
-
private async createEvent(http: HttpClient, input: Record<string, unknown>): Promise<ActionResult> {
|
|
627
|
-
const summary = input.summary as string;
|
|
628
|
-
const start = input.start as string;
|
|
629
|
-
const end = input.end as string;
|
|
630
|
-
const description = input.description as string | undefined;
|
|
631
|
-
const location = input.location as string | undefined;
|
|
632
|
-
const attendeesStr = input.attendees as string | undefined;
|
|
633
|
-
const calendarId = (input.calendar_id as string) || 'primary';
|
|
634
|
-
|
|
635
|
-
if (!summary || !start || !end) {
|
|
636
|
-
return { success: false, error: 'summary, start, and end are required.' };
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
const eventBody: Record<string, unknown> = {
|
|
640
|
-
summary,
|
|
641
|
-
start: { dateTime: start },
|
|
642
|
-
end: { dateTime: end },
|
|
643
|
-
};
|
|
644
|
-
|
|
645
|
-
if (description) eventBody.description = description;
|
|
646
|
-
if (location) eventBody.location = location;
|
|
647
|
-
|
|
648
|
-
if (attendeesStr) {
|
|
649
|
-
const attendees = attendeesStr
|
|
650
|
-
.split(',')
|
|
651
|
-
.map((email) => email.trim())
|
|
652
|
-
.filter((email) => email.length > 0)
|
|
653
|
-
.map((email) => ({ email }));
|
|
654
|
-
if (attendees.length > 0) {
|
|
655
|
-
eventBody.attendees = attendees;
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
const url = `${this.BASE_URL}/calendars/${encodeURIComponent(calendarId)}/events`;
|
|
660
|
-
const response = await http.raw(url, {
|
|
661
|
-
method: 'POST',
|
|
662
|
-
headers: { 'Content-Type': 'application/json' },
|
|
663
|
-
body: JSON.stringify(eventBody),
|
|
664
|
-
});
|
|
665
|
-
|
|
666
|
-
if (!response.ok) {
|
|
667
|
-
const errText = await response.text();
|
|
668
|
-
return { success: false, error: `Calendar create error (${response.status}): ${errText}` };
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
const created = (await response.json()) as CalendarEvent;
|
|
672
|
-
|
|
673
|
-
return {
|
|
674
|
-
success: true,
|
|
675
|
-
output: {
|
|
676
|
-
event_id: created.id,
|
|
677
|
-
html_link: created.htmlLink,
|
|
678
|
-
summary: created.summary,
|
|
679
|
-
start: created.start.dateTime || created.start.date,
|
|
680
|
-
end: created.end.dateTime || created.end.date,
|
|
681
|
-
},
|
|
682
|
-
};
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
private async updateEvent(http: HttpClient, input: Record<string, unknown>): Promise<ActionResult> {
|
|
686
|
-
const eventId = input.event_id as string;
|
|
687
|
-
const calendarId = (input.calendar_id as string) || 'primary';
|
|
688
|
-
|
|
689
|
-
if (!eventId) {
|
|
690
|
-
return { success: false, error: 'event_id is required.' };
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
const patch: Record<string, unknown> = {};
|
|
694
|
-
if (input.summary !== undefined) patch.summary = input.summary;
|
|
695
|
-
if (input.description !== undefined) patch.description = input.description;
|
|
696
|
-
if (input.location !== undefined) patch.location = input.location;
|
|
697
|
-
if (input.start !== undefined) patch.start = { dateTime: input.start as string };
|
|
698
|
-
if (input.end !== undefined) patch.end = { dateTime: input.end as string };
|
|
699
|
-
|
|
700
|
-
const url = `${this.BASE_URL}/calendars/${encodeURIComponent(calendarId)}/events/${encodeURIComponent(eventId)}`;
|
|
701
|
-
const response = await http.raw(url, {
|
|
702
|
-
method: 'PATCH',
|
|
703
|
-
headers: { 'Content-Type': 'application/json' },
|
|
704
|
-
body: JSON.stringify(patch),
|
|
705
|
-
});
|
|
706
|
-
|
|
707
|
-
if (!response.ok) {
|
|
708
|
-
const errText = await response.text();
|
|
709
|
-
return { success: false, error: `Calendar update error (${response.status}): ${errText}` };
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
const updated = (await response.json()) as CalendarEvent;
|
|
713
|
-
|
|
714
|
-
return {
|
|
715
|
-
success: true,
|
|
716
|
-
output: {
|
|
717
|
-
event_id: updated.id,
|
|
718
|
-
url: updated.htmlLink,
|
|
719
|
-
summary: updated.summary,
|
|
720
|
-
},
|
|
721
|
-
};
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
private async deleteEvent(http: HttpClient, input: Record<string, unknown>): Promise<ActionResult> {
|
|
725
|
-
const eventId = input.event_id as string;
|
|
726
|
-
const calendarId = (input.calendar_id as string) || 'primary';
|
|
727
|
-
|
|
728
|
-
if (!eventId) {
|
|
729
|
-
return { success: false, error: 'event_id is required.' };
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
const url = `${this.BASE_URL}/calendars/${encodeURIComponent(calendarId)}/events/${encodeURIComponent(eventId)}`;
|
|
733
|
-
const response = await http.raw(url, { method: 'DELETE' });
|
|
734
|
-
|
|
735
|
-
if (!response.ok) {
|
|
736
|
-
const errText = await response.text();
|
|
737
|
-
return { success: false, error: `Calendar delete error (${response.status}): ${errText}` };
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
return {
|
|
741
|
-
success: true,
|
|
742
|
-
output: { deleted: true, event_id: eventId },
|
|
743
|
-
};
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
private async getEvent(http: HttpClient, input: Record<string, unknown>): Promise<ActionResult> {
|
|
747
|
-
const eventId = input.event_id as string;
|
|
748
|
-
const calendarId = (input.calendar_id as string) || 'primary';
|
|
749
|
-
|
|
750
|
-
if (!eventId) {
|
|
751
|
-
return { success: false, error: 'event_id is required.' };
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
const url = `${this.BASE_URL}/calendars/${encodeURIComponent(calendarId)}/events/${encodeURIComponent(eventId)}`;
|
|
755
|
-
const response = await http.raw(url);
|
|
756
|
-
|
|
757
|
-
if (!response.ok) {
|
|
758
|
-
const errText = await response.text();
|
|
759
|
-
return { success: false, error: `Calendar get error (${response.status}): ${errText}` };
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
const event = (await response.json()) as CalendarEvent;
|
|
763
|
-
|
|
764
|
-
return {
|
|
765
|
-
success: true,
|
|
766
|
-
output: {
|
|
767
|
-
event_id: event.id,
|
|
768
|
-
summary: event.summary,
|
|
769
|
-
start: event.start.dateTime || event.start.date,
|
|
770
|
-
end: event.end.dateTime || event.end.date,
|
|
771
|
-
description: event.description,
|
|
772
|
-
location: event.location,
|
|
773
|
-
attendees: event.attendees?.map((a) => ({
|
|
774
|
-
email: a.email,
|
|
775
|
-
name: a.displayName,
|
|
776
|
-
status: a.responseStatus,
|
|
777
|
-
})),
|
|
778
|
-
organizer: event.organizer
|
|
779
|
-
? { email: event.organizer.email, name: event.organizer.displayName }
|
|
780
|
-
: undefined,
|
|
781
|
-
url: event.htmlLink,
|
|
782
|
-
status: event.status,
|
|
783
|
-
},
|
|
784
|
-
};
|
|
785
|
-
}
|
|
786
|
-
|
|
787
|
-
// -------------------------------------------------------------------------
|
|
788
|
-
// Helpers
|
|
789
|
-
// -------------------------------------------------------------------------
|
|
790
|
-
|
|
791
|
-
private calendarEventToRow(calEvent: CalendarEvent): Record<string, unknown> | null {
|
|
792
|
-
if (calEvent.status === 'cancelled') return null;
|
|
793
|
-
const startTime = calEvent.start.dateTime || calEvent.start.date;
|
|
794
|
-
if (!startTime) return null;
|
|
795
|
-
const endTime = calEvent.end.dateTime || calEvent.end.date || '';
|
|
796
|
-
return {
|
|
797
|
-
id: calEvent.id,
|
|
798
|
-
summary: calEvent.summary || '(no title)',
|
|
799
|
-
description: calEvent.description || '',
|
|
800
|
-
location: calEvent.location || '',
|
|
801
|
-
status: calEvent.status,
|
|
802
|
-
start_time: startTime,
|
|
803
|
-
end_time: endTime,
|
|
804
|
-
all_day: !calEvent.start.dateTime,
|
|
805
|
-
organizer: calEvent.organizer?.displayName || calEvent.organizer?.email || '',
|
|
806
|
-
attendee_count: calEvent.attendees?.length ?? 0,
|
|
807
|
-
created_at: calEvent.created,
|
|
808
|
-
updated_at: calEvent.updated,
|
|
809
|
-
url: calEvent.htmlLink,
|
|
810
|
-
};
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
private calendarEventToChangeEnvelope(calEvent: CalendarEvent): EventEnvelope {
|
|
814
|
-
const startTime = calEvent.start?.dateTime || calEvent.start?.date;
|
|
815
|
-
const endTime = calEvent.end?.dateTime || calEvent.end?.date;
|
|
816
|
-
const changedAt = new Date(calEvent.updated || startTime || Date.now());
|
|
817
|
-
const occurredAt = Number.isNaN(changedAt.getTime()) ? new Date() : changedAt;
|
|
818
|
-
const isCancelled = calEvent.status === 'cancelled';
|
|
819
|
-
|
|
820
|
-
const parts: string[] = [];
|
|
821
|
-
if (calEvent.description) {
|
|
822
|
-
parts.push(calEvent.description);
|
|
823
|
-
}
|
|
824
|
-
if (calEvent.attendees && calEvent.attendees.length > 0) {
|
|
825
|
-
const attendeeList = calEvent.attendees.map((a) => a.displayName || a.email).join(', ');
|
|
826
|
-
parts.push(`Attendees: ${attendeeList}`);
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
return {
|
|
830
|
-
origin_id: calEvent.id,
|
|
831
|
-
title: calEvent.summary || (isCancelled ? '(cancelled calendar event)' : '(no title)'),
|
|
832
|
-
payload_text: parts.join('\n\n'),
|
|
833
|
-
author_name: calEvent.organizer?.displayName || calEvent.organizer?.email,
|
|
834
|
-
source_url: calEvent.htmlLink,
|
|
835
|
-
occurred_at: occurredAt,
|
|
836
|
-
origin_type: 'calendar_event',
|
|
837
|
-
metadata: {
|
|
838
|
-
status: calEvent.status,
|
|
839
|
-
change_type: isCancelled ? 'cancelled' : 'upserted',
|
|
840
|
-
...(calEvent.location ? { location: calEvent.location } : {}),
|
|
841
|
-
...(calEvent.organizer?.email ? { organizer: calEvent.organizer.email } : {}),
|
|
842
|
-
attendee_count: calEvent.attendees?.length ?? 0,
|
|
843
|
-
...(startTime ? { start_time: startTime } : {}),
|
|
844
|
-
...(endTime ? { end_time: endTime } : {}),
|
|
845
|
-
all_day: Boolean(startTime && !calEvent.start?.dateTime),
|
|
846
|
-
},
|
|
847
|
-
};
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
private calendarEventToEnvelope(calEvent: CalendarEvent): EventEnvelope | null {
|
|
851
|
-
if (calEvent.status === 'cancelled') return null;
|
|
852
|
-
|
|
853
|
-
const startTime = calEvent.start.dateTime || calEvent.start.date;
|
|
854
|
-
if (!startTime) return null;
|
|
855
|
-
|
|
856
|
-
const occurredAt = new Date(startTime);
|
|
857
|
-
if (Number.isNaN(occurredAt.getTime())) return null;
|
|
858
|
-
|
|
859
|
-
const isAllDay = !calEvent.start.dateTime;
|
|
860
|
-
const endTime = calEvent.end.dateTime || calEvent.end.date;
|
|
861
|
-
|
|
862
|
-
// Build payload text from description + attendees
|
|
863
|
-
const parts: string[] = [];
|
|
864
|
-
if (calEvent.description) {
|
|
865
|
-
parts.push(calEvent.description);
|
|
866
|
-
}
|
|
867
|
-
if (calEvent.attendees && calEvent.attendees.length > 0) {
|
|
868
|
-
const attendeeList = calEvent.attendees.map((a) => a.displayName || a.email).join(', ');
|
|
869
|
-
parts.push(`Attendees: ${attendeeList}`);
|
|
386
|
+
return { success: false, error: 'Google Calendar actions require Google OAuth credentials.' };
|
|
870
387
|
}
|
|
871
|
-
|
|
872
|
-
return {
|
|
873
|
-
origin_id: calEvent.id,
|
|
874
|
-
title: calEvent.summary || '(no title)',
|
|
875
|
-
payload_text: parts.join('\n\n'),
|
|
876
|
-
author_name: calEvent.organizer?.displayName || calEvent.organizer?.email,
|
|
877
|
-
source_url: calEvent.htmlLink,
|
|
878
|
-
occurred_at: occurredAt,
|
|
879
|
-
origin_type: 'calendar_event',
|
|
880
|
-
metadata: {
|
|
881
|
-
status: calEvent.status,
|
|
882
|
-
...(calEvent.location ? { location: calEvent.location } : {}),
|
|
883
|
-
...(calEvent.organizer?.email ? { organizer: calEvent.organizer.email } : {}),
|
|
884
|
-
attendee_count: calEvent.attendees?.length ?? 0,
|
|
885
|
-
start_time: startTime,
|
|
886
|
-
...(endTime ? { end_time: endTime } : {}),
|
|
887
|
-
all_day: isAllDay,
|
|
888
|
-
},
|
|
889
|
-
};
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
private completedCheckpoint(syncToken: string | undefined): CalendarCheckpoint {
|
|
893
|
-
// Built fresh rather than spread over the previous checkpoint: the whole
|
|
894
|
-
// object replaces the stored one, so a run that recovered from a rejected
|
|
895
|
-
// token cannot leave that token behind. When the sync produced no new token
|
|
896
|
-
// the key is absent, and the next run correctly starts from a full sync.
|
|
897
|
-
return {
|
|
898
|
-
...(syncToken ? { sync_token: syncToken } : {}),
|
|
899
|
-
last_sync_at: new Date().toISOString(),
|
|
900
|
-
};
|
|
388
|
+
return executeGoogleAction(CALENDAR, ctx, this.client(token));
|
|
901
389
|
}
|
|
902
390
|
|
|
903
391
|
// Auth-aware client (Bearer + retry/backoff on transient 429/5xx). Built per
|