@agentshouse/kit 0.1.0-alpha.25 → 0.1.0-alpha.27
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/README.md +172 -158
- package/bin/connect-linux.sh +15 -4
- package/bin/kit.js +48 -46
- package/bootstrap.json +3 -3
- package/package.json +1 -1
- package/src/adapter-mcp.js +23 -1
- package/src/bridge.js +4 -0
- package/src/configuration.js +13 -209
- package/src/connector-login.js +3 -84
- package/src/contract.js +4 -0
- package/src/delivery.js +4 -7
- package/src/directory.js +6 -26
- package/src/google/calendar.js +39 -45
- package/src/google/drive.js +61 -186
- package/src/google/entries.js +346 -0
- package/src/google/gateway.js +3 -6
- package/src/google/gmail.js +33 -23
- package/src/google/oauth.js +106 -66
- package/src/google/source.js +21 -23
- package/src/house.js +43 -5
- package/src/how-we-work.md +25 -0
- package/src/kit-work-stream.js +1 -1
- package/src/launcher.js +11 -4
- package/src/mcp.js +5 -0
- package/src/setup.js +8 -14
- package/src/skill-set/ask-matt/PHASE-BOUNDARIES.md +55 -0
- package/src/skill-set/ask-matt/SKILL.md +91 -0
- package/src/skill-set/ask-matt/agents/openai.yaml +4 -0
- package/src/skill-set/code-review/LICENSE +21 -0
- package/src/skill-set/code-review/SKILL.md +89 -0
- package/src/skill-set/code-review/agents/openai.yaml +3 -0
- package/src/skill-set/codebase-design/DEEPENING.md +37 -0
- package/src/skill-set/codebase-design/DESIGN-IT-TWICE.md +44 -0
- package/src/skill-set/codebase-design/LICENSE +21 -0
- package/src/skill-set/codebase-design/SKILL.md +116 -0
- package/src/skill-set/codebase-design/agents/openai.yaml +3 -0
- package/src/skill-set/diagnosing-bugs/LICENSE +21 -0
- package/src/skill-set/diagnosing-bugs/SKILL.md +140 -0
- package/src/skill-set/diagnosing-bugs/agents/openai.yaml +3 -0
- package/src/skill-set/diagnosing-bugs/scripts/hitl-loop.template.sh +44 -0
- package/src/skill-set/domain-modeling/ADR-FORMAT.md +47 -0
- package/src/skill-set/domain-modeling/CONTEXT-FORMAT.md +60 -0
- package/src/skill-set/domain-modeling/LICENSE +21 -0
- package/src/skill-set/domain-modeling/SKILL.md +76 -0
- package/src/skill-set/domain-modeling/agents/openai.yaml +3 -0
- package/src/skill-set/grill-me/LICENSE +21 -0
- package/src/skill-set/grill-me/SKILL.md +8 -0
- package/src/skill-set/grill-me/agents/openai.yaml +4 -0
- package/src/skill-set/grill-with-docs/LICENSE +21 -0
- package/src/skill-set/grill-with-docs/SKILL.md +8 -0
- package/src/skill-set/grill-with-docs/agents/openai.yaml +4 -0
- package/src/skill-set/grilling/LICENSE +21 -0
- package/src/{starter-set → skill-set}/grilling/SKILL.md +1 -1
- package/src/skill-set/handoff/LICENSE +21 -0
- package/src/skill-set/handoff/SKILL.md +17 -0
- package/src/skill-set/handoff/agents/openai.yaml +4 -0
- package/src/skill-set/implement/LICENSE +21 -0
- package/src/skill-set/implement/SKILL.md +16 -0
- package/src/skill-set/implement/agents/openai.yaml +4 -0
- package/src/skill-set/improve-codebase-architecture/HTML-REPORT.md +123 -0
- package/src/skill-set/improve-codebase-architecture/LICENSE +21 -0
- package/src/skill-set/improve-codebase-architecture/SKILL.md +72 -0
- package/src/skill-set/improve-codebase-architecture/agents/openai.yaml +4 -0
- package/src/skill-set/prototype/LICENSE +21 -0
- package/src/skill-set/prototype/LOGIC.md +67 -0
- package/src/skill-set/prototype/SKILL.md +28 -0
- package/src/skill-set/prototype/UI.md +112 -0
- package/src/skill-set/prototype/agents/openai.yaml +3 -0
- package/src/skill-set/research/LICENSE +21 -0
- package/src/skill-set/research/SKILL.md +14 -0
- package/src/skill-set/research/agents/openai.yaml +3 -0
- package/src/skill-set/resolving-merge-conflicts/LICENSE +21 -0
- package/src/skill-set/resolving-merge-conflicts/SKILL.md +16 -0
- package/src/skill-set/resolving-merge-conflicts/agents/openai.yaml +3 -0
- package/src/skill-set/tdd/LICENSE +21 -0
- package/src/skill-set/tdd/SKILL.md +40 -0
- package/src/skill-set/tdd/agents/openai.yaml +3 -0
- package/src/skill-set/tdd/mocking.md +59 -0
- package/src/skill-set/tdd/tests.md +77 -0
- package/src/skill-set/teach/GLOSSARY-FORMAT.md +35 -0
- package/src/skill-set/teach/LEARNING-RECORD-FORMAT.md +46 -0
- package/src/skill-set/teach/LICENSE +21 -0
- package/src/skill-set/teach/MISSION-FORMAT.md +31 -0
- package/src/skill-set/teach/RESOURCES-FORMAT.md +32 -0
- package/src/skill-set/teach/SKILL.md +141 -0
- package/src/skill-set/teach/agents/openai.yaml +4 -0
- package/src/skill-set/to-questionnaire/LICENSE +21 -0
- package/src/skill-set/to-questionnaire/SKILL.md +55 -0
- package/src/skill-set/to-questionnaire/agents/openai.yaml +4 -0
- package/src/skill-set/to-spec/LICENSE +21 -0
- package/src/skill-set/to-spec/SKILL.md +76 -0
- package/src/skill-set/to-spec/agents/openai.yaml +4 -0
- package/src/skill-set/to-tickets/LICENSE +21 -0
- package/src/skill-set/to-tickets/SKILL.md +106 -0
- package/src/skill-set/to-tickets/agents/openai.yaml +4 -0
- package/src/skill-set/wait-what/LICENSE +21 -0
- package/src/skill-set/wait-what/SKILL.md +8 -0
- package/src/skill-set/wait-what/agents/openai.yaml +4 -0
- package/src/skill-set/wayfinder/LICENSE +21 -0
- package/src/skill-set/wayfinder/SKILL.md +129 -0
- package/src/skill-set/wayfinder/agents/openai.yaml +4 -0
- package/src/skill-set/writing-for-agents/LICENSE +21 -0
- package/src/skill-set/writing-for-agents/SKILL-MECHANICS.md +22 -0
- package/src/skill-set/writing-for-agents/SKILL.md +83 -0
- package/src/skill-set/writing-for-agents/agents/openai.yaml +3 -0
- package/src/skill-set.js +85 -0
- package/src/source.js +51 -26
- package/src/state.js +19 -1
- package/src/tasks.js +72 -0
- package/src/worker.js +46 -68
- package/src/working-copy.js +1 -15
- package/src/google/declared.js +0 -146
- package/src/google/id.js +0 -1
- package/src/instance/google.js +0 -1
- package/src/starter-set.js +0 -17
- /package/src/{starter-set/grilling → skill-set/ask-matt}/LICENSE +0 -0
- /package/src/{starter-set → skill-set}/grilling/agents/openai.yaml +0 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { SOURCE_CAPTURE_INTERVAL_MS } from "../contract.js";
|
|
2
|
+
import { desiredSources, maintainControl, openSession, reportSources } from "../house.js";
|
|
3
|
+
import { KitRefusal } from "../refusal.js";
|
|
4
|
+
import { signInModule } from "../setup.js";
|
|
5
|
+
import { captureEntry, localServiceState } from "../source.js";
|
|
6
|
+
import { noteNames, notePath, readHealth, readNote, removeNotes, writeNote } from "../state.js";
|
|
7
|
+
import { coalescedTask, residentWake } from "../tasks.js";
|
|
8
|
+
import {
|
|
9
|
+
authorizeGoogle,
|
|
10
|
+
forgetGoogleAccount,
|
|
11
|
+
googleAccount,
|
|
12
|
+
googleSignedIn,
|
|
13
|
+
GOOGLE_SCOPES,
|
|
14
|
+
GOOGLE_SOURCES,
|
|
15
|
+
} from "./oauth.js";
|
|
16
|
+
import { recordAccountEmail } from "./source.js";
|
|
17
|
+
|
|
18
|
+
const ENTRY_NOTES = ["checkpoints", "health", "gaps", "dropped", "originals", "entries"];
|
|
19
|
+
|
|
20
|
+
export const GOOGLE_APIS = Object.freeze({
|
|
21
|
+
gmail: "Gmail API",
|
|
22
|
+
calendar: "Google Calendar API",
|
|
23
|
+
drive: "Google Drive API",
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
async function heldEntries(directory) {
|
|
27
|
+
const entries = [];
|
|
28
|
+
for (const source of await noteNames(directory, GOOGLE_SOURCES, "entries")) {
|
|
29
|
+
entries.push(await readNote(notePath(directory, GOOGLE_SOURCES, source, "entries"), null));
|
|
30
|
+
}
|
|
31
|
+
return entries;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function accountEntries(directory, account) {
|
|
35
|
+
return (await heldEntries(directory)).filter((entry) => entry.account === account);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function entryReport(directory, entry) {
|
|
39
|
+
const account = await googleAccount(directory, entry.account);
|
|
40
|
+
const email = account?.email ?? null;
|
|
41
|
+
if (account?.scopes.includes(GOOGLE_SCOPES[entry.service]) !== true) {
|
|
42
|
+
return { source: entry.source, state: "signed_out", email };
|
|
43
|
+
}
|
|
44
|
+
const health = await readHealth(directory, GOOGLE_SOURCES, entry.source);
|
|
45
|
+
if (health?.state === "failed") {
|
|
46
|
+
return { source: entry.source, state: "failed", email, cause: health.cause ?? null };
|
|
47
|
+
}
|
|
48
|
+
return { source: entry.source, state: "ready", email };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function reportEntries(context, entries) {
|
|
52
|
+
if (entries.length === 0) return;
|
|
53
|
+
const sources = [];
|
|
54
|
+
for (const entry of entries) sources.push(await entryReport(context.directory, entry));
|
|
55
|
+
await reportSources(context.session, sources, context.signal);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function captureOccurrence(context, entry) {
|
|
59
|
+
const delivered = await captureEntry(context, entry);
|
|
60
|
+
await reportEntries(context, [entry]);
|
|
61
|
+
return delivered;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function collectedClient(entry, present, signal) {
|
|
65
|
+
const collected = await present(
|
|
66
|
+
{
|
|
67
|
+
steps: [
|
|
68
|
+
{
|
|
69
|
+
kind: "collect",
|
|
70
|
+
label: "Client ID of your Google OAuth client",
|
|
71
|
+
name: "client_id",
|
|
72
|
+
description: `In your own Google Cloud project, enable the ${GOOGLE_APIS[entry.service]} and create an OAuth client of type Desktop app. Paste its client ID here.`,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
kind: "collect",
|
|
76
|
+
label: "Client secret of that OAuth client",
|
|
77
|
+
name: "client_secret",
|
|
78
|
+
description: `Paste the same client's secret. It stays on this Environment beside the ${entry.account} account's sign-in.`,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
signal,
|
|
83
|
+
);
|
|
84
|
+
return { id: collected.client_id.trim(), secret: collected.client_secret.trim() };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function consent(entry, present, signal) {
|
|
88
|
+
return async ({ url }) => {
|
|
89
|
+
const collected = await present(
|
|
90
|
+
{
|
|
91
|
+
steps: [
|
|
92
|
+
{
|
|
93
|
+
kind: "visit",
|
|
94
|
+
label: `Sign in to Google for ${entry.account} and allow access for ${entry.service}`,
|
|
95
|
+
url,
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
kind: "collect",
|
|
99
|
+
label: "The address your browser ended on",
|
|
100
|
+
name: "address",
|
|
101
|
+
description:
|
|
102
|
+
"After you allow access, Google sends your browser to a 127.0.0.1 address that does not open. Copy that whole address from the address bar and paste it here.",
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
signal,
|
|
107
|
+
);
|
|
108
|
+
return collected.address;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function guidedSignIn(context, entry, present, signal) {
|
|
113
|
+
try {
|
|
114
|
+
const account = await googleAccount(context.directory, entry.account);
|
|
115
|
+
await authorizeGoogle(
|
|
116
|
+
{
|
|
117
|
+
directory: context.directory,
|
|
118
|
+
account: entry.account,
|
|
119
|
+
client: account?.client ?? (await collectedClient(entry, present, signal)),
|
|
120
|
+
scopes: [GOOGLE_SCOPES[entry.service]],
|
|
121
|
+
approve: consent(entry, present, signal),
|
|
122
|
+
},
|
|
123
|
+
context.dependencies,
|
|
124
|
+
);
|
|
125
|
+
return "ready";
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (signal.aborted) return "cancelled";
|
|
128
|
+
if (error instanceof KitRefusal && error.code === "google_authorization_declined") {
|
|
129
|
+
return "cancelled";
|
|
130
|
+
}
|
|
131
|
+
return "failed";
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function signIn(context, entry) {
|
|
136
|
+
const outcome = await signInModule(context, {
|
|
137
|
+
category: "connector",
|
|
138
|
+
name: entry.source,
|
|
139
|
+
login: { integration: "google", instance: entry.account, service: entry.service },
|
|
140
|
+
install: null,
|
|
141
|
+
signedIn: () => googleSignedIn(context.directory, entry.account, entry.service),
|
|
142
|
+
guided: ({ present, signal }) => guidedSignIn(context, entry, present, signal),
|
|
143
|
+
});
|
|
144
|
+
if (outcome.status !== "ready") return false;
|
|
145
|
+
await removeNotes(context.directory, GOOGLE_SOURCES, entry.source, ["health"]);
|
|
146
|
+
try {
|
|
147
|
+
await recordAccountEmail(context, { account: entry.account, service: entry.service });
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (!(error instanceof KitRefusal) || error.code === "kit_command_cancelled") throw error;
|
|
150
|
+
}
|
|
151
|
+
await reportEntries(context, [entry]);
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function sourceEntries(context) {
|
|
156
|
+
const { directory, session, dependencies } = context;
|
|
157
|
+
const interval = dependencies.sourceCaptureIntervalMs ?? SOURCE_CAPTURE_INTERVAL_MS;
|
|
158
|
+
const held = new Map();
|
|
159
|
+
const wake = residentWake();
|
|
160
|
+
let attempted = new Set();
|
|
161
|
+
let signingIn = null;
|
|
162
|
+
let cadence = Promise.resolve();
|
|
163
|
+
|
|
164
|
+
function occasion(record) {
|
|
165
|
+
return {
|
|
166
|
+
session,
|
|
167
|
+
directory,
|
|
168
|
+
dependencies,
|
|
169
|
+
now: context.now,
|
|
170
|
+
signal: AbortSignal.any([context.signal, record.stop.signal]),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function occur(record) {
|
|
175
|
+
const current = (record.running ?? Promise.resolve())
|
|
176
|
+
.catch(() => {})
|
|
177
|
+
.then(() => {
|
|
178
|
+
record.last = context.now().getTime();
|
|
179
|
+
return captureOccurrence(occasion(record), record.entry);
|
|
180
|
+
});
|
|
181
|
+
record.running = current;
|
|
182
|
+
current
|
|
183
|
+
.finally(() => {
|
|
184
|
+
if (record.running === current) record.running = null;
|
|
185
|
+
signIns.trigger();
|
|
186
|
+
wake.notify();
|
|
187
|
+
})
|
|
188
|
+
.catch(() => {});
|
|
189
|
+
return current;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function track(entry) {
|
|
193
|
+
const record = held.get(entry.source);
|
|
194
|
+
if (record !== undefined) record.entry = entry;
|
|
195
|
+
else held.set(entry.source, { entry, last: null, running: null, stop: new AbortController() });
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async function drop(source, record) {
|
|
199
|
+
record.stop.abort();
|
|
200
|
+
held.delete(source);
|
|
201
|
+
if (signingIn?.source === source) await signingIn.task.catch(() => {});
|
|
202
|
+
await record.running?.catch(() => {});
|
|
203
|
+
await removeNotes(directory, GOOGLE_SOURCES, source, ENTRY_NOTES);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const signIns = coalescedTask(context.signal, context.unavailable, async () => {
|
|
207
|
+
for (const record of [...held.values()]) {
|
|
208
|
+
if (context.draining()) return;
|
|
209
|
+
const { entry } = record;
|
|
210
|
+
if (record.stop.signal.aborted || attempted.has(entry.source)) continue;
|
|
211
|
+
if (await googleSignedIn(directory, entry.account, entry.service)) continue;
|
|
212
|
+
attempted.add(entry.source);
|
|
213
|
+
const task = signIn(occasion(record), entry);
|
|
214
|
+
signingIn = { source: entry.source, task };
|
|
215
|
+
try {
|
|
216
|
+
if (await task) record.last = null;
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (!record.stop.signal.aborted) throw error;
|
|
219
|
+
} finally {
|
|
220
|
+
signingIn = null;
|
|
221
|
+
}
|
|
222
|
+
wake.notify();
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const reconciling = coalescedTask(context.signal, context.unavailable, async () => {
|
|
227
|
+
const desired = await desiredSources(session, context.signal);
|
|
228
|
+
const named = new Set(desired.map((entry) => entry.source));
|
|
229
|
+
for (const [source, record] of [...held]) {
|
|
230
|
+
if (!named.has(source)) await drop(source, record);
|
|
231
|
+
}
|
|
232
|
+
for (const entry of desired) {
|
|
233
|
+
await writeNote(notePath(directory, GOOGLE_SOURCES, entry.source, "entries"), entry);
|
|
234
|
+
track(entry);
|
|
235
|
+
}
|
|
236
|
+
await reportEntries({ session, directory, signal: context.signal }, desired);
|
|
237
|
+
attempted = new Set();
|
|
238
|
+
signIns.trigger();
|
|
239
|
+
wake.notify();
|
|
240
|
+
const kept = [...new Set(desired.map((entry) => entry.account))];
|
|
241
|
+
for (const account of await noteNames(directory, GOOGLE_SOURCES, "credentials")) {
|
|
242
|
+
if (!kept.includes(account)) await forgetGoogleAccount(directory, account, kept, dependencies);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
async function startDue() {
|
|
247
|
+
let next = interval;
|
|
248
|
+
const now = context.now().getTime();
|
|
249
|
+
for (const record of [...held.values()]) {
|
|
250
|
+
if (record.running !== null) continue;
|
|
251
|
+
const due = record.last === null ? 0 : record.last + interval - now;
|
|
252
|
+
if (due > 0) {
|
|
253
|
+
next = Math.min(next, due);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
if (!(await googleSignedIn(directory, record.entry.account, record.entry.service))) continue;
|
|
258
|
+
} catch (error) {
|
|
259
|
+
context.unavailable(error);
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (record.stop.signal.aborted || record.running !== null) continue;
|
|
263
|
+
occur(record).catch((error) => {
|
|
264
|
+
if (!record.stop.signal.aborted && !context.signal.aborted) context.unavailable(error);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
return next;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return {
|
|
271
|
+
async start() {
|
|
272
|
+
try {
|
|
273
|
+
for (const entry of await heldEntries(directory)) track(entry);
|
|
274
|
+
} catch (error) {
|
|
275
|
+
context.unavailable(error);
|
|
276
|
+
}
|
|
277
|
+
cadence = (async () => {
|
|
278
|
+
while (!context.signal.aborted) {
|
|
279
|
+
await wake.wait(context.draining() ? interval : await startDue(), context.signal);
|
|
280
|
+
}
|
|
281
|
+
})();
|
|
282
|
+
},
|
|
283
|
+
reconcile: () => reconciling.trigger(),
|
|
284
|
+
capture(source) {
|
|
285
|
+
const record = held.get(source);
|
|
286
|
+
if (record === undefined) return Promise.reject(new KitRefusal("kit_instance_unknown", source));
|
|
287
|
+
return occur(record);
|
|
288
|
+
},
|
|
289
|
+
idle: () =>
|
|
290
|
+
reconciling.idle() &&
|
|
291
|
+
signIns.idle() &&
|
|
292
|
+
[...held.values()].every((record) => record.running === null),
|
|
293
|
+
async settled() {
|
|
294
|
+
await reconciling.settled();
|
|
295
|
+
await signIns.settled();
|
|
296
|
+
await cadence;
|
|
297
|
+
await Promise.all([...held.values()].map((record) => record.running?.catch(() => {})));
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function namedEntries(entries, service) {
|
|
303
|
+
if (service === null) return entries;
|
|
304
|
+
const named = entries.filter((entry) => entry.service === service);
|
|
305
|
+
if (named.length === 0) throw new KitRefusal("kit_service_unknown", service);
|
|
306
|
+
return named;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export async function captureAccount(options, dependencies = {}) {
|
|
310
|
+
const entries = namedEntries(options.entries, options.service);
|
|
311
|
+
const session = await openSession(options, dependencies);
|
|
312
|
+
return maintainControl(
|
|
313
|
+
session,
|
|
314
|
+
async (signal) => {
|
|
315
|
+
const services = [];
|
|
316
|
+
for (const entry of entries) {
|
|
317
|
+
services.push(
|
|
318
|
+
session.resident === undefined
|
|
319
|
+
? await captureOccurrence(
|
|
320
|
+
{ session, directory: options.directory, dependencies, signal, now: session.now },
|
|
321
|
+
entry,
|
|
322
|
+
)
|
|
323
|
+
: await session.capture(entry.source, signal),
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
return { instance: options.account, services };
|
|
327
|
+
},
|
|
328
|
+
options.signal,
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export async function accountStatus(options) {
|
|
333
|
+
const services = [];
|
|
334
|
+
for (const entry of namedEntries(options.entries, options.service)) {
|
|
335
|
+
const local = await localServiceState(options.directory, GOOGLE_SOURCES, entry.source);
|
|
336
|
+
services.push({
|
|
337
|
+
service: entry.service,
|
|
338
|
+
source_ref: entry.source,
|
|
339
|
+
rooms: entry.rooms,
|
|
340
|
+
signed_in: await googleSignedIn(options.directory, entry.account, entry.service),
|
|
341
|
+
...local,
|
|
342
|
+
enable: local.health.cause === "api_disabled" ? GOOGLE_APIS[entry.service] : null,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
return { instance: options.account, preset: "google", services };
|
|
346
|
+
}
|
package/src/google/gateway.js
CHANGED
|
@@ -84,7 +84,7 @@ async function googleResponse(options, dependencies) {
|
|
|
84
84
|
attempt -= 1;
|
|
85
85
|
continue;
|
|
86
86
|
}
|
|
87
|
-
await revokeGoogleAuthorization(options.directory, options.
|
|
87
|
+
await revokeGoogleAuthorization(options.directory, options.account, options.requiredScopes);
|
|
88
88
|
throw new KitRefusal("google_authority_revoked");
|
|
89
89
|
}
|
|
90
90
|
if (response.status === 403) {
|
|
@@ -98,13 +98,10 @@ async function googleResponse(options, dependencies) {
|
|
|
98
98
|
throw new KitRefusal("google_provider_unavailable");
|
|
99
99
|
}
|
|
100
100
|
if (reasons.includes("insufficientPermissions")) {
|
|
101
|
-
await revokeGoogleAuthorization(
|
|
102
|
-
options.directory,
|
|
103
|
-
options.instance,
|
|
104
|
-
options.requiredScopes,
|
|
105
|
-
);
|
|
101
|
+
await revokeGoogleAuthorization(options.directory, options.account, options.requiredScopes);
|
|
106
102
|
throw new KitRefusal("google_scope_revoked");
|
|
107
103
|
}
|
|
104
|
+
if (reasons.includes("accessNotConfigured")) throw new KitRefusal("api_disabled");
|
|
108
105
|
throw new KitRefusal("google_provider_unavailable");
|
|
109
106
|
}
|
|
110
107
|
if (RETRYABLE_STATUS.has(response.status) && attempt + 1 < RETRY_LIMIT) {
|
package/src/google/gmail.js
CHANGED
|
@@ -4,6 +4,7 @@ import { googleJson } from "./gateway.js";
|
|
|
4
4
|
import { GOOGLE_SCOPES } from "./oauth.js";
|
|
5
5
|
|
|
6
6
|
const GMAIL_ORIGIN = "https://gmail.googleapis.com";
|
|
7
|
+
const EXCLUDED_LABELS = ["SPAM", "TRASH"];
|
|
7
8
|
|
|
8
9
|
function cursorOf(value) {
|
|
9
10
|
if (value === null) return { mode: "messages", pageToken: null, historyId: null };
|
|
@@ -88,18 +89,36 @@ function normalizedMessage(message) {
|
|
|
88
89
|
};
|
|
89
90
|
}
|
|
90
91
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
function common(options) {
|
|
93
|
+
return {
|
|
94
|
+
directory: options.directory,
|
|
95
|
+
account: options.account,
|
|
96
|
+
requiredScopes: [GOOGLE_SCOPES.gmail],
|
|
97
|
+
signal: options.signal,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function profileOf(shared, dependencies) {
|
|
102
|
+
return googleJson({ ...shared, url: `${GMAIL_ORIGIN}/gmail/v1/users/me/profile` }, dependencies);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function currentHistoryId(shared, dependencies) {
|
|
106
|
+
const profile = await profileOf(shared, dependencies);
|
|
96
107
|
if (typeof profile.historyId !== "string" || !/^\d+$/.test(profile.historyId)) {
|
|
97
108
|
throw new KitRefusal("google_provider_response_invalid", "gmail profile");
|
|
98
109
|
}
|
|
99
110
|
return profile.historyId;
|
|
100
111
|
}
|
|
101
112
|
|
|
102
|
-
function
|
|
113
|
+
export async function gmailEmail(options, dependencies = {}) {
|
|
114
|
+
const profile = await profileOf(common(options), dependencies);
|
|
115
|
+
if (typeof profile.emailAddress !== "string") {
|
|
116
|
+
throw new KitRefusal("google_provider_response_invalid", "gmail profile");
|
|
117
|
+
}
|
|
118
|
+
return profile.emailAddress;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function listUrl(cursor, limit, backfillFloor) {
|
|
103
122
|
const url = new URL(
|
|
104
123
|
cursor.mode === "messages"
|
|
105
124
|
? "/gmail/v1/users/me/messages"
|
|
@@ -109,7 +128,6 @@ function listUrl(selection, cursor, limit, backfillFloor) {
|
|
|
109
128
|
url.searchParams.set("maxResults", `${limit}`);
|
|
110
129
|
if (cursor.pageToken !== null) url.searchParams.set("pageToken", cursor.pageToken);
|
|
111
130
|
if (cursor.mode === "messages") {
|
|
112
|
-
for (const label of selection.labels) url.searchParams.append("labelIds", label);
|
|
113
131
|
if (backfillFloor !== null && backfillFloor !== undefined) {
|
|
114
132
|
url.searchParams.set("q", `after:${Math.floor(Date.parse(backfillFloor) / 1000)}`);
|
|
115
133
|
}
|
|
@@ -151,22 +169,16 @@ function referencesOf(answer, mode) {
|
|
|
151
169
|
|
|
152
170
|
export async function gmailPage(options, dependencies = {}) {
|
|
153
171
|
let cursor = cursorOf(options.cursor);
|
|
154
|
-
const
|
|
155
|
-
directory: options.directory,
|
|
156
|
-
instance: options.instance,
|
|
157
|
-
client: options.client,
|
|
158
|
-
requiredScopes: [GOOGLE_SCOPES.gmail],
|
|
159
|
-
signal: options.signal,
|
|
160
|
-
};
|
|
172
|
+
const shared = common(options);
|
|
161
173
|
if (cursor.mode === "messages" && cursor.historyId === null) {
|
|
162
|
-
cursor = { ...cursor, historyId: await currentHistoryId(
|
|
174
|
+
cursor = { ...cursor, historyId: await currentHistoryId(shared, dependencies) };
|
|
163
175
|
}
|
|
164
176
|
let answer;
|
|
165
177
|
try {
|
|
166
178
|
answer = await googleJson(
|
|
167
179
|
{
|
|
168
|
-
...
|
|
169
|
-
url: listUrl(
|
|
180
|
+
...shared,
|
|
181
|
+
url: listUrl(cursor, options.limit, options.backfillFloor),
|
|
170
182
|
...(cursor.mode === "history" ? { notFoundCode: "google_history_expired" } : {}),
|
|
171
183
|
},
|
|
172
184
|
dependencies,
|
|
@@ -176,10 +188,10 @@ export async function gmailPage(options, dependencies = {}) {
|
|
|
176
188
|
cursor = {
|
|
177
189
|
mode: "messages",
|
|
178
190
|
pageToken: null,
|
|
179
|
-
historyId: await currentHistoryId(
|
|
191
|
+
historyId: await currentHistoryId(shared, dependencies),
|
|
180
192
|
};
|
|
181
193
|
answer = await googleJson(
|
|
182
|
-
{ ...
|
|
194
|
+
{ ...shared, url: listUrl(cursor, options.limit, options.backfillFloor) },
|
|
183
195
|
dependencies,
|
|
184
196
|
);
|
|
185
197
|
}
|
|
@@ -197,16 +209,14 @@ export async function gmailPage(options, dependencies = {}) {
|
|
|
197
209
|
try {
|
|
198
210
|
detail = await googleJson(
|
|
199
211
|
{
|
|
200
|
-
...
|
|
212
|
+
...shared,
|
|
201
213
|
notFoundCode: "google_gmail_item_missing",
|
|
202
214
|
url: `${GMAIL_ORIGIN}/gmail/v1/users/me/messages/${encodeURIComponent(identifier)}?format=full`,
|
|
203
215
|
},
|
|
204
216
|
dependencies,
|
|
205
217
|
);
|
|
206
218
|
const normalized = normalizedMessage(detail);
|
|
207
|
-
if (
|
|
208
|
-
items.push(normalized);
|
|
209
|
-
}
|
|
219
|
+
if (!detail.labelIds.some((label) => EXCLUDED_LABELS.includes(label))) items.push(normalized);
|
|
210
220
|
} catch (error) {
|
|
211
221
|
if (
|
|
212
222
|
error instanceof KitRefusal &&
|