@1outres/dsh-bot-connector-google 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +33 -2
- package/cordis.patch.yml +6 -0
- package/icon.svg +8 -0
- package/lib/index.js +2268 -0
- package/locale/en.json +6 -0
- package/locale/ja.json +6 -0
- package/package.json +61 -3
- package/src/host/calendar/events.ts +139 -0
- package/src/host/calendar/own.ts +28 -0
- package/src/host/calendar/times.ts +40 -0
- package/src/host/calendar/tools.ts +633 -0
- package/src/host/connectors.ts +41 -0
- package/src/host/endpoints.ts +48 -0
- package/src/host/gmail/addresses.ts +73 -0
- package/src/host/gmail/compose.ts +244 -0
- package/src/host/gmail/events.ts +117 -0
- package/src/host/gmail/io.ts +212 -0
- package/src/host/gmail/message.ts +128 -0
- package/src/host/gmail/mime.ts +139 -0
- package/src/host/gmail/tools.ts +916 -0
- package/src/host/google.ts +93 -0
- package/src/host/kit.ts +62 -0
- package/src/host/provider.ts +53 -0
- package/src/host/scopes.ts +18 -0
- package/src/index.ts +70 -0
package/locale/en.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Gmail and calendar for bots",
|
|
4
|
+
"description": "Lets each bot read and act in the Google accounts you assign it: Gmail and Google Calendar tools, sending and changes others see only after you approve, and new mail and calendar changes as events. Connect accounts in the bot's Connectors settings."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/ja.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1outres/dsh-bot-connector-google",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Gives the bots of DeepSeek Harness their owner's Gmail and Google Calendar: tools that read and act as the Google accounts assigned to each bot, sending and other outside actions only after the owner approves, and new emails and calendar changes as events.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/1outres/dsh-bots.git",
|
|
8
|
+
"directory": "packages/dsh-bot-connector-google"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "lib/index.js",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./lib/index.js",
|
|
14
|
+
"./locale/*.json": "./locale/*.json",
|
|
15
|
+
"./icon": "./icon.svg",
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"lib",
|
|
20
|
+
"src",
|
|
21
|
+
"cordis.patch.yml",
|
|
22
|
+
"locale/*.json",
|
|
23
|
+
"icon.svg"
|
|
24
|
+
],
|
|
25
|
+
"keywords": [
|
|
26
|
+
"dsh",
|
|
27
|
+
"dsh-plugin",
|
|
28
|
+
"bot",
|
|
29
|
+
"connector",
|
|
30
|
+
"gmail",
|
|
31
|
+
"google-calendar",
|
|
32
|
+
"deepseek-harness"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"dsh": "0.2.2-rc.5",
|
|
40
|
+
"node": ">=22"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@1outres/dsh-bot-core": "^0.3.0",
|
|
44
|
+
"@deepseek-ai/cordis": "~4.1.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@1outres/dsh-bot-core": "0.3.0",
|
|
48
|
+
"@deepseek-ai/cordis": "~4.1.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@deepseek-ai/schemastery": "~3.19.0",
|
|
52
|
+
"@googleapis/calendar": "^20.2.0",
|
|
53
|
+
"@googleapis/gmail": "^22.1.0",
|
|
54
|
+
"google-auth-library": "^11.3.4"
|
|
55
|
+
},
|
|
56
|
+
"dsh": {
|
|
57
|
+
"bundle": {
|
|
58
|
+
"patch": [
|
|
59
|
+
"./cordis.patch.yml"
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"scripts": {}
|
|
6
64
|
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { BotConnectorEvents, ConnectorEvent, ConnectorJson } from "@1outres/dsh-bot-core"
|
|
2
|
+
import type { calendar_v3 } from "@googleapis/calendar"
|
|
3
|
+
import { type Google, googleStatus } from "../google"
|
|
4
|
+
import { oneLine } from "../kit"
|
|
5
|
+
import type { OwnChanges } from "./own"
|
|
6
|
+
import { showTime } from "./times"
|
|
7
|
+
|
|
8
|
+
export const EVENT_CREATED = "gcal.event.created"
|
|
9
|
+
export const EVENT_UPDATED = "gcal.event.updated"
|
|
10
|
+
export const EVENT_CANCELLED = "gcal.event.cancelled"
|
|
11
|
+
|
|
12
|
+
/** A change within this long of the event's creation counts as its creation. */
|
|
13
|
+
const CREATED_WITHIN_MS = 5_000
|
|
14
|
+
const MAX_SYNC_PAGES = 100
|
|
15
|
+
|
|
16
|
+
interface Cursor {
|
|
17
|
+
/** The sync token of each calendar the account can write to. */
|
|
18
|
+
readonly calendars: Readonly<Record<string, string>>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const cursorOf = (value: ConnectorJson | undefined): Cursor | undefined => {
|
|
22
|
+
const calendars = (value as { calendars?: unknown } | null | undefined)?.calendars
|
|
23
|
+
if (typeof calendars !== "object" || calendars === null || Array.isArray(calendars))
|
|
24
|
+
return undefined
|
|
25
|
+
return {
|
|
26
|
+
calendars: Object.fromEntries(
|
|
27
|
+
Object.entries(calendars).filter(([, token]) => typeof token === "string"),
|
|
28
|
+
),
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Every page of a listing; resolves to its items and the sync token of the last page. */
|
|
33
|
+
async function listAll(
|
|
34
|
+
calendar: calendar_v3.Calendar,
|
|
35
|
+
calendarId: string,
|
|
36
|
+
syncToken: string | undefined,
|
|
37
|
+
) {
|
|
38
|
+
const items: calendar_v3.Schema$Event[] = []
|
|
39
|
+
let pageToken: string | undefined
|
|
40
|
+
for (let page = 0; ; page += 1) {
|
|
41
|
+
const response = await calendar.events.list({
|
|
42
|
+
calendarId,
|
|
43
|
+
showDeleted: true,
|
|
44
|
+
maxResults: 250,
|
|
45
|
+
...(syncToken === undefined ? {} : { syncToken }),
|
|
46
|
+
...(pageToken === undefined ? {} : { pageToken }),
|
|
47
|
+
})
|
|
48
|
+
if (syncToken !== undefined) items.push(...(response.data.items ?? []))
|
|
49
|
+
pageToken = response.data.nextPageToken ?? undefined
|
|
50
|
+
if (pageToken === undefined) {
|
|
51
|
+
const next = response.data.nextSyncToken
|
|
52
|
+
if (!next) throw new Error(`the calendar ${calendarId} returned no sync token`)
|
|
53
|
+
return { items, syncToken: next }
|
|
54
|
+
}
|
|
55
|
+
if (page >= MAX_SYNC_PAGES)
|
|
56
|
+
throw new Error(`the calendar ${calendarId} has too many pages to sync`)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function kindOf(event: calendar_v3.Schema$Event): string {
|
|
61
|
+
if (event.status === "cancelled") return EVENT_CANCELLED
|
|
62
|
+
const created = Date.parse(event.created ?? "")
|
|
63
|
+
const updated = Date.parse(event.updated ?? "")
|
|
64
|
+
return Number.isFinite(created) &&
|
|
65
|
+
Number.isFinite(updated) &&
|
|
66
|
+
updated - created <= CREATED_WITHIN_MS
|
|
67
|
+
? EVENT_CREATED
|
|
68
|
+
: EVENT_UPDATED
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Changed events of the calendars the account can write to, from Calendar's
|
|
73
|
+
* incremental sync (events.list with a sync token per calendar). A calendar
|
|
74
|
+
* seen for the first time only gets its token; an expired token (410) is
|
|
75
|
+
* synced again from now and reported as a gap.
|
|
76
|
+
*/
|
|
77
|
+
export function calendarEvents(google: Google, own: OwnChanges): BotConnectorEvents {
|
|
78
|
+
return {
|
|
79
|
+
kinds: [
|
|
80
|
+
{ kind: EVENT_CREATED, label: { en: "Event created", ja: "予定の追加" } },
|
|
81
|
+
{ kind: EVENT_UPDATED, label: { en: "Event changed", ja: "予定の変更" } },
|
|
82
|
+
{
|
|
83
|
+
kind: EVENT_CANCELLED,
|
|
84
|
+
label: { en: "Event cancelled or deleted", ja: "予定の取り消し・削除" },
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
delivery: "notify",
|
|
88
|
+
intervalMs: 120_000,
|
|
89
|
+
poll: (source, value) =>
|
|
90
|
+
google.as(source.connection, async ({ calendar }) => {
|
|
91
|
+
const cursor = cursorOf(value)
|
|
92
|
+
const list = await calendar.calendarList.list({ minAccessRole: "writer" })
|
|
93
|
+
const ids = (list.data.items ?? []).flatMap((item) => (item.id ? [item.id] : []))
|
|
94
|
+
const tokens: Record<string, string> = {}
|
|
95
|
+
const events: ConnectorEvent[] = []
|
|
96
|
+
const gaps: string[] = []
|
|
97
|
+
for (const calendarId of ids) {
|
|
98
|
+
const previous = cursor?.calendars[calendarId]
|
|
99
|
+
if (previous === undefined) {
|
|
100
|
+
tokens[calendarId] = (await listAll(calendar, calendarId, undefined)).syncToken
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
let synced: Awaited<ReturnType<typeof listAll>>
|
|
104
|
+
try {
|
|
105
|
+
synced = await listAll(calendar, calendarId, previous)
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (googleStatus(error) !== 410) throw error
|
|
108
|
+
tokens[calendarId] = (await listAll(calendar, calendarId, undefined)).syncToken
|
|
109
|
+
gaps.push(calendarId)
|
|
110
|
+
continue
|
|
111
|
+
}
|
|
112
|
+
tokens[calendarId] = synced.syncToken
|
|
113
|
+
for (const item of synced.items) {
|
|
114
|
+
if (!item.id) continue
|
|
115
|
+
const kind = kindOf(item)
|
|
116
|
+
events.push({
|
|
117
|
+
key: `gcal:${calendarId}:${item.id}:${item.updated ?? item.status ?? ""}`,
|
|
118
|
+
kind,
|
|
119
|
+
occurredAt: Date.parse(item.updated ?? "") || 0,
|
|
120
|
+
summary: oneLine(
|
|
121
|
+
kind === EVENT_CANCELLED
|
|
122
|
+
? `Cancelled: ${item.summary ?? item.id} (calendar ${calendarId})`
|
|
123
|
+
: `${item.summary ?? "(no title)"} · ${showTime(item.start ?? undefined)} → ${showTime(item.end ?? undefined)} · organizer ${item.organizer?.email ?? "?"} (calendar ${calendarId})`,
|
|
124
|
+
),
|
|
125
|
+
ref: { calendar_id: calendarId, event_id: item.id },
|
|
126
|
+
...(own.isOwn(source.connection.info.id, item.id) ? { own: true } : {}),
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
events,
|
|
132
|
+
cursor: { calendars: tokens },
|
|
133
|
+
...(gaps.length === 0
|
|
134
|
+
? {}
|
|
135
|
+
: { gap: `Calendar's sync of ${gaps.join(", ")} expired and was started again` }),
|
|
136
|
+
}
|
|
137
|
+
}),
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The calendar changes this process made itself, remembered for a while so
|
|
3
|
+
* the events poll marks them `own` (bot-core keeps them untold): a bot is
|
|
4
|
+
* not woken by its own edit.
|
|
5
|
+
*/
|
|
6
|
+
export interface OwnChanges {
|
|
7
|
+
record(connectionId: string, eventId: string): void
|
|
8
|
+
isOwn(connectionId: string, eventId: string): boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function createOwnChanges(now: () => number, keepMs: number): OwnChanges {
|
|
12
|
+
const changes = new Map<string, number>()
|
|
13
|
+
const keyOf = (connectionId: string, eventId: string) => JSON.stringify([connectionId, eventId])
|
|
14
|
+
const sweep = () => {
|
|
15
|
+
const limit = now() - keepMs
|
|
16
|
+
for (const [key, at] of changes) if (at < limit) changes.delete(key)
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
record(connectionId, eventId) {
|
|
20
|
+
sweep()
|
|
21
|
+
changes.set(keyOf(connectionId, eventId), now())
|
|
22
|
+
},
|
|
23
|
+
isOwn(connectionId, eventId) {
|
|
24
|
+
sweep()
|
|
25
|
+
return changes.has(keyOf(connectionId, eventId))
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { calendar_v3 } from "@googleapis/calendar"
|
|
2
|
+
|
|
3
|
+
const DATE = /^\d{4}-\d{2}-\d{2}$/
|
|
4
|
+
const DATE_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})?$/
|
|
5
|
+
const OFFSET = /(Z|[+-]\d{2}:\d{2})$/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A start or end as the bot writes it: a date (an all-day event), a time
|
|
9
|
+
* with its offset, or a local time with `timeZone`.
|
|
10
|
+
* @throws naming what is wrong.
|
|
11
|
+
*/
|
|
12
|
+
export function eventTime(
|
|
13
|
+
value: string,
|
|
14
|
+
timeZone: string | undefined,
|
|
15
|
+
what: string,
|
|
16
|
+
): calendar_v3.Schema$EventDateTime {
|
|
17
|
+
if (DATE.test(value)) return { date: value }
|
|
18
|
+
if (!DATE_TIME.test(value))
|
|
19
|
+
throw new Error(`${what} must be YYYY-MM-DD or an ISO 8601 date and time`)
|
|
20
|
+
if (!OFFSET.test(value) && timeZone === undefined) {
|
|
21
|
+
throw new Error(`${what} has no offset: add one (+09:00) or give time_zone`)
|
|
22
|
+
}
|
|
23
|
+
return { dateTime: value, ...(timeZone === undefined ? {} : { timeZone }) }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** An event's start or end as people read it. */
|
|
27
|
+
export function showTime(time: calendar_v3.Schema$EventDateTime | undefined): string {
|
|
28
|
+
if (time === undefined) return ""
|
|
29
|
+
if (time.date) return `${time.date} (all day)`
|
|
30
|
+
return `${time.dateTime ?? ""}${time.timeZone ? ` ${time.timeZone}` : ""}`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** An RFC 3339 instant argument (time_min, time_max). */
|
|
34
|
+
export function instant(value: string | undefined, what: string): string | undefined {
|
|
35
|
+
if (value === undefined) return undefined
|
|
36
|
+
if (DATE.test(value)) return `${value}T00:00:00Z`
|
|
37
|
+
if (!DATE_TIME.test(value) || !OFFSET.test(value))
|
|
38
|
+
throw new Error(`${what} must be an ISO 8601 time with an offset`)
|
|
39
|
+
return value
|
|
40
|
+
}
|