@agentshouse/kit 0.1.0-alpha.26 → 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.
@@ -84,7 +84,7 @@ async function googleResponse(options, dependencies) {
84
84
  attempt -= 1;
85
85
  continue;
86
86
  }
87
- await revokeGoogleAuthorization(options.directory, options.instance, options.requiredScopes);
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) {
@@ -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
- async function currentHistoryId(common, dependencies) {
92
- const profile = await googleJson(
93
- { ...common, url: `${GMAIL_ORIGIN}/gmail/v1/users/me/profile` },
94
- dependencies,
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 listUrl(selection, cursor, limit, backfillFloor) {
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 common = {
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(common, dependencies) };
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
- ...common,
169
- url: listUrl(options.selection, cursor, options.limit, options.backfillFloor),
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(common, dependencies),
191
+ historyId: await currentHistoryId(shared, dependencies),
180
192
  };
181
193
  answer = await googleJson(
182
- { ...common, url: listUrl(options.selection, cursor, options.limit, options.backfillFloor) },
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
- ...common,
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 (options.selection.labels.every((label) => detail.labelIds.includes(label))) {
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 &&
@@ -1,8 +1,10 @@
1
1
  import { createHash, randomBytes, randomInt } from "node:crypto";
2
- import { readFile } from "node:fs/promises";
3
- import { join } from "node:path";
2
+ import { readFile, rm } from "node:fs/promises";
4
3
  import { KitRefusal } from "../refusal.js";
5
- import { writeNote } from "../state.js";
4
+ import { notePath, writeNote } from "../state.js";
5
+ import { exclusively } from "../tasks.js";
6
+
7
+ export const GOOGLE_SOURCES = "google";
6
8
 
7
9
  export const GOOGLE_SCOPES = Object.freeze({
8
10
  gmail: "https://www.googleapis.com/auth/gmail.readonly",
@@ -12,26 +14,30 @@ export const GOOGLE_SCOPES = Object.freeze({
12
14
 
13
15
  const AUTHORIZATION_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth";
14
16
  const TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token";
17
+ const REVOCATION_ENDPOINT = "https://oauth2.googleapis.com/revoke";
15
18
  const REFRESH_MARGIN_MS = 60_000;
16
19
  const LOOPBACK_PORT_FLOOR = 49152;
17
20
  const LOOPBACK_PORT_SPAN = 16384;
18
21
 
19
- function credentialPath(directory, instance) {
20
- return join(directory, "state", instance, "credentials", "google.json");
22
+ function credentialPath(directory, account) {
23
+ return notePath(directory, GOOGLE_SOURCES, account, "credentials");
21
24
  }
22
25
 
23
- async function rawAuthorization(directory, instance) {
24
- const path = credentialPath(directory, instance);
26
+ async function rawAccount(directory, account) {
27
+ const path = credentialPath(directory, account);
25
28
  let document;
26
29
  try {
27
30
  document = JSON.parse(await readFile(path, "utf8"));
28
31
  } catch (error) {
29
32
  if (error.code === "ENOENT") return null;
30
- throw new KitRefusal("google_authorization_unreadable", instance);
33
+ throw new KitRefusal("google_authorization_unreadable", account);
31
34
  }
32
35
  if (
33
36
  typeof document !== "object" ||
34
37
  document === null ||
38
+ typeof document.client?.id !== "string" ||
39
+ typeof document.client.secret !== "string" ||
40
+ (document.email !== null && typeof document.email !== "string") ||
35
41
  !Array.isArray(document.grants) ||
36
42
  document.grants.some(
37
43
  (grant) =>
@@ -46,21 +52,27 @@ async function rawAuthorization(directory, instance) {
46
52
  grant.scopes.some((scope) => typeof scope !== "string"),
47
53
  )
48
54
  ) {
49
- throw new KitRefusal("google_authorization_unreadable", instance);
55
+ throw new KitRefusal("google_authorization_unreadable", account);
50
56
  }
51
57
  return document;
52
58
  }
53
59
 
54
- async function writeAuthorization(directory, instance, authorization) {
55
- await writeNote(credentialPath(directory, instance), authorization);
60
+ function updateAccount(directory, account, change) {
61
+ return exclusively(credentialPath(directory, account), async () => {
62
+ const stored = await rawAccount(directory, account);
63
+ const changed = change(stored);
64
+ if (changed !== null) await writeNote(credentialPath(directory, account), changed);
65
+ });
56
66
  }
57
67
 
58
- export async function googleAuthorization(directory, instance) {
59
- const authorization = await rawAuthorization(directory, instance);
60
- if (authorization === null || authorization.grants.length === 0) return null;
68
+ export async function googleAccount(directory, account) {
69
+ const stored = await rawAccount(directory, account);
70
+ if (stored === null) return null;
61
71
  return {
62
- scopes: [...new Set(authorization.grants.flatMap((grant) => grant.scopes))].sort(),
63
- grants: authorization.grants.map((grant) => ({
72
+ client: { id: stored.client.id, secret: stored.client.secret },
73
+ email: stored.email,
74
+ scopes: [...new Set(stored.grants.flatMap((grant) => grant.scopes))].sort(),
75
+ grants: stored.grants.map((grant) => ({
64
76
  accessToken: grant.access_token,
65
77
  refreshToken: grant.refresh_token,
66
78
  expiresAt: grant.expires_at,
@@ -69,6 +81,15 @@ export async function googleAuthorization(directory, instance) {
69
81
  };
70
82
  }
71
83
 
84
+ export async function googleSignedIn(directory, account, service) {
85
+ const stored = await rawAccount(directory, account);
86
+ return stored?.grants.some((grant) => grant.scopes.includes(GOOGLE_SCOPES[service])) === true;
87
+ }
88
+
89
+ export function recordGoogleEmail(directory, account, email) {
90
+ return updateAccount(directory, account, (stored) => (stored === null ? null : { ...stored, email }));
91
+ }
92
+
72
93
  function requiredScopesOf(value) {
73
94
  if (
74
95
  !Array.isArray(value) ||
@@ -80,19 +101,51 @@ function requiredScopesOf(value) {
80
101
  return value;
81
102
  }
82
103
 
83
- export async function revokeGoogleAuthorization(directory, instance, scopes) {
104
+ export async function revokeGoogleAuthorization(directory, account, scopes) {
84
105
  const requiredScopes = requiredScopesOf(scopes);
85
- const authorization = await rawAuthorization(directory, instance);
86
- if (authorization !== null) {
87
- await writeAuthorization(directory, instance, {
88
- grants: authorization.grants
89
- .map((grant) => ({
90
- ...grant,
91
- scopes: grant.scopes.filter((scope) => !requiredScopes.includes(scope)),
92
- }))
93
- .filter((grant) => grant.scopes.length > 0),
106
+ await updateAccount(directory, account, (stored) =>
107
+ stored === null
108
+ ? null
109
+ : {
110
+ ...stored,
111
+ grants: stored.grants
112
+ .map((grant) => ({
113
+ ...grant,
114
+ scopes: grant.scopes.filter((scope) => !requiredScopes.includes(scope)),
115
+ }))
116
+ .filter((grant) => grant.scopes.length > 0),
117
+ },
118
+ );
119
+ }
120
+
121
+ async function revokedAtProvider(fetcher, token) {
122
+ let response;
123
+ try {
124
+ response = await fetcher(REVOCATION_ENDPOINT, {
125
+ method: "POST",
126
+ headers: { "content-type": "application/x-www-form-urlencoded" },
127
+ body: new URLSearchParams({ token }).toString(),
94
128
  });
129
+ } catch {
130
+ throw new KitRefusal("google_provider_unavailable");
95
131
  }
132
+ if (response.status !== 200 && response.status !== 400) {
133
+ throw new KitRefusal("google_provider_unavailable");
134
+ }
135
+ }
136
+
137
+ export async function forgetGoogleAccount(directory, account, kept, dependencies = {}) {
138
+ const stored = await rawAccount(directory, account);
139
+ if (stored === null) return;
140
+ const others = await Promise.all(kept.map((other) => rawAccount(directory, other)));
141
+ if (stored.email === null || !others.some((held) => held?.email === stored.email)) {
142
+ for (const grant of stored.grants) {
143
+ await revokedAtProvider(dependencies.fetch ?? fetch, grant.refresh_token);
144
+ }
145
+ }
146
+ await exclusively(credentialPath(directory, account), () =>
147
+ rm(credentialPath(directory, account), { force: true }),
148
+ );
96
149
  }
97
150
 
98
151
  function oauthValue() {
@@ -167,26 +220,9 @@ async function tokenRequest(fetcher, body) {
167
220
  return answer;
168
221
  }
169
222
 
170
- function configuredClient(client) {
171
- if (
172
- typeof client?.id !== "string" ||
173
- client.id.length === 0 ||
174
- typeof client.secret !== "string" ||
175
- client.secret.length === 0
176
- ) {
177
- throw new KitRefusal("google_oauth_client_unconfigured");
178
- }
179
- return client;
180
- }
181
-
182
223
  export async function authorizeGoogle(options, dependencies = {}) {
183
- const client = configuredClient(options.client);
184
- const stored = await rawAuthorization(options.directory, options.instance);
224
+ const { client } = options;
185
225
  const scopes = [...new Set(options.scopes)];
186
- const covered = stored?.grants.some((grant) =>
187
- scopes.every((scope) => grant.scopes.includes(scope)),
188
- );
189
- if (covered === true) return { status: "skipped", requestedScopes: [] };
190
226
  const verifier = oauthValue();
191
227
  const state = oauthValue();
192
228
  const redirectUri = loopbackRedirect(dependencies.loopbackPort);
@@ -215,10 +251,11 @@ export async function authorizeGoogle(options, dependencies = {}) {
215
251
  }
216
252
  const grantedScopes = [...new Set(returnedScopes.length === 0 ? scopes : returnedScopes)].sort();
217
253
  const key = grantedScopes.join("\n");
218
- const grants = (stored?.grants ?? []).filter((grant) => [...grant.scopes].sort().join("\n") !== key);
219
- await writeAuthorization(options.directory, options.instance, {
254
+ await updateAccount(options.directory, options.account, (stored) => ({
255
+ client: { id: client.id, secret: client.secret },
256
+ email: stored?.email ?? null,
220
257
  grants: [
221
- ...grants,
258
+ ...(stored?.grants ?? []).filter((grant) => [...grant.scopes].sort().join("\n") !== key),
222
259
  {
223
260
  access_token: token.access_token,
224
261
  refresh_token: refreshToken,
@@ -226,48 +263,51 @@ export async function authorizeGoogle(options, dependencies = {}) {
226
263
  scopes: grantedScopes,
227
264
  },
228
265
  ],
229
- });
230
- return { status: "ready", requestedScopes: scopes };
266
+ }));
231
267
  }
232
268
 
233
269
  export async function googleAccessToken(options, dependencies = {}) {
234
- const stored = await rawAuthorization(options.directory, options.instance);
270
+ const stored = await rawAccount(options.directory, options.account);
235
271
  const requiredScopes = requiredScopesOf(options.requiredScopes);
236
272
  const grant = stored?.grants
237
273
  .filter((candidate) => requiredScopes.every((scope) => candidate.scopes.includes(scope)))
238
274
  .sort((left, right) => left.scopes.length - right.scopes.length)[0];
239
275
  if (grant === undefined) {
240
- throw new KitRefusal("google_authorization_required", options.instance);
276
+ throw new KitRefusal("google_authorization_required", options.account);
241
277
  }
242
278
  const now = dependencies.now?.() ?? new Date();
243
279
  if (options.forceRefresh !== true && Date.parse(grant.expires_at) - now.getTime() > REFRESH_MARGIN_MS) {
244
280
  return grant.access_token;
245
281
  }
246
- const client = configuredClient(options.client);
247
282
  let token;
248
283
  try {
249
284
  token = await tokenRequest(dependencies.fetch ?? fetch, {
250
- client_id: client.id,
251
- client_secret: client.secret,
285
+ client_id: stored.client.id,
286
+ client_secret: stored.client.secret,
252
287
  refresh_token: grant.refresh_token,
253
288
  grant_type: "refresh_token",
254
289
  });
255
290
  } catch (error) {
256
291
  if (error instanceof KitRefusal && error.code === "google_authority_revoked") {
257
- await revokeGoogleAuthorization(options.directory, options.instance, grant.scopes);
292
+ await revokeGoogleAuthorization(options.directory, options.account, grant.scopes);
258
293
  }
259
294
  throw error;
260
295
  }
261
- await writeAuthorization(options.directory, options.instance, {
262
- grants: stored.grants.map((candidate) =>
263
- candidate === grant
264
- ? {
265
- ...candidate,
266
- access_token: token.access_token,
267
- expires_at: new Date(now.getTime() + token.expires_in * 1000).toISOString(),
268
- }
269
- : candidate,
270
- ),
271
- });
296
+ await updateAccount(options.directory, options.account, (current) =>
297
+ current === null
298
+ ? null
299
+ : {
300
+ ...current,
301
+ grants: current.grants.map((candidate) =>
302
+ candidate.refresh_token === grant.refresh_token
303
+ ? {
304
+ ...candidate,
305
+ access_token: token.access_token,
306
+ expires_at: new Date(now.getTime() + token.expires_in * 1000).toISOString(),
307
+ }
308
+ : candidate,
309
+ ),
310
+ },
311
+ );
272
312
  return token.access_token;
273
313
  }
@@ -1,25 +1,23 @@
1
- import { KitRefusal } from "../refusal.js";
2
- import { gmailPage } from "./gmail.js";
3
- import { calendarPage } from "./calendar.js";
4
- import { drivePage } from "./drive.js";
5
- import { declaredPage } from "./declared.js";
1
+ import { calendarEmail, calendarPage } from "./calendar.js";
2
+ import { driveEmail, drivePage } from "./drive.js";
3
+ import { gmailEmail, gmailPage } from "./gmail.js";
4
+ import { googleAccount, recordGoogleEmail } from "./oauth.js";
6
5
 
7
- export async function googlePage(options, dependencies = {}) {
8
- const common = {
9
- directory: options.directory,
10
- instance: options.instance,
11
- client: options.client,
12
- selection: options.service.selection,
13
- cursor: options.cursor,
14
- limit: options.limit,
15
- backfillFloor: options.backfillFloor,
16
- signal: options.signal,
17
- };
18
- if (options.service.service === "gmail") return gmailPage(common, dependencies);
19
- if (options.service.service === "calendar") return calendarPage(common, dependencies);
20
- if (options.service.service === "drive") return drivePage(common, dependencies);
21
- if (options.service.declaration !== null) {
22
- return declaredPage({ ...common, service: options.service }, dependencies);
23
- }
24
- throw new KitRefusal("google_service_unsupported", options.service.service);
6
+ const SERVICES = {
7
+ gmail: { page: gmailPage, email: gmailEmail },
8
+ calendar: { page: calendarPage, email: calendarEmail },
9
+ drive: { page: drivePage, email: driveEmail },
10
+ };
11
+
12
+ export function googlePage(options, dependencies = {}) {
13
+ return SERVICES[options.service].page(options, dependencies);
14
+ }
15
+
16
+ export async function recordAccountEmail(context, google) {
17
+ if ((await googleAccount(context.directory, google.account))?.email !== null) return;
18
+ const email = await SERVICES[google.service].email(
19
+ { directory: context.directory, account: google.account, signal: context.signal },
20
+ context.dependencies,
21
+ );
22
+ await recordGoogleEmail(context.directory, google.account, email);
25
23
  }
package/src/house.js CHANGED
@@ -24,6 +24,8 @@ import {
24
24
  KIT_SESSIONS_REPORT_PATH,
25
25
  KIT_SETUP_ACQUIRE_PATH,
26
26
  KIT_SETUP_REPORT_PATH,
27
+ KIT_SOURCES_DESIRED_PATH,
28
+ KIT_SOURCES_REPORT_PATH,
27
29
  KIT_WORKSPACES_ACQUIRE_PATH,
28
30
  LEASE_RENEWAL_MARGIN_MS,
29
31
  MCP_PROTOCOL_VERSION,
@@ -172,7 +174,7 @@ async function residentRelay(session, request, signal, draining) {
172
174
  }
173
175
  }
174
176
 
175
- export async function serveResident(session, directory, signal, drain, push) {
177
+ export async function serveResident(session, directory, signal, drain, push, capture) {
176
178
  const socket = residentSocket(directory);
177
179
  await mkdir(dirname(socket), { recursive: true, mode: 0o700 });
178
180
  await rm(socket, { force: true });
@@ -231,9 +233,14 @@ export async function serveResident(session, directory, signal, drain, push) {
231
233
  relayed.set(request.id, cancelled);
232
234
  const relaySignal = AbortSignal.any([signal, cancelled.signal]);
233
235
  const answered =
234
- request.push === undefined
235
- ? residentRelay(session, request, relaySignal, draining)
236
- : push(request.push, relaySignal).then((pushed) => ({ pushed }));
236
+ request.push !== undefined
237
+ ? push(request.push, relaySignal).then((pushed) => ({ pushed }))
238
+ : request.capture !== undefined
239
+ ? capture(request.capture, relaySignal).then(
240
+ (captured) => ({ captured }),
241
+ (error) => ({ refused: error?.code ?? "kit_resident_unavailable" }),
242
+ )
243
+ : residentRelay(session, request, relaySignal, draining);
237
244
  void answered.then((answer) => {
238
245
  relayed.delete(request.id);
239
246
  if (!connection.destroyed) writeFrame(connection, { id: request.id, ...answer });
@@ -330,6 +337,12 @@ async function residentDelegation(directory, fetcher) {
330
337
  if (answer.pushed === undefined) throw new KitRefusal("kit_resident_unavailable");
331
338
  return answer.pushed;
332
339
  }
340
+ async function capture(source, signal) {
341
+ const answer = await exchange({ capture: source }, signal);
342
+ if (answer.refused !== undefined) throw new KitRefusal(answer.refused);
343
+ if (answer.captured === undefined) throw new KitRefusal("kit_resident_unavailable");
344
+ return answer.captured;
345
+ }
333
346
  async function delegated(url, init = {}) {
334
347
  const { authorization, ...headers } = init.headers ?? {};
335
348
  if (authorization !== RESIDENT) return fetcher(url, init);
@@ -352,7 +365,7 @@ async function residentDelegation(directory, fetcher) {
352
365
  headers: answer.type === null ? {} : { "content-type": answer.type },
353
366
  });
354
367
  }
355
- return { connection, fetch: delegated, push };
368
+ return { connection, fetch: delegated, push, capture };
356
369
  }
357
370
 
358
371
  export async function openSession(options, dependencies = {}) {
@@ -372,6 +385,7 @@ export async function openSession(options, dependencies = {}) {
372
385
  if (resident !== null) {
373
386
  session.resident = resident.connection;
374
387
  session.push = resident.push;
388
+ session.capture = resident.capture;
375
389
  session.dependencies = { ...dependencies, fetch: resident.fetch };
376
390
  return session;
377
391
  }
@@ -695,6 +709,30 @@ export async function desiredAgents(session, signal = session.signal) {
695
709
  return answer.agents;
696
710
  }
697
711
 
712
+ export async function desiredSources(session, signal = session.signal) {
713
+ const answer = await houseRequest(
714
+ session.house,
715
+ await heldControl(session, signal),
716
+ KIT_SOURCES_DESIRED_PATH,
717
+ {},
718
+ session.dependencies,
719
+ signal,
720
+ );
721
+ if (!Array.isArray(answer?.sources)) throw new KitRefusal("kit_sources_rejected");
722
+ return answer.sources;
723
+ }
724
+
725
+ export async function reportSources(session, sources, signal = session.signal) {
726
+ return houseRequest(
727
+ session.house,
728
+ await heldControl(session, signal),
729
+ KIT_SOURCES_REPORT_PATH,
730
+ { sources },
731
+ session.dependencies,
732
+ signal,
733
+ );
734
+ }
735
+
698
736
  const HOUSE_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
699
737
 
700
738
  export async function acquireSetup(session, signal = session.signal) {
@@ -5,7 +5,7 @@ import { heldControl } from "./house.js";
5
5
  import { KitRefusal } from "./refusal.js";
6
6
 
7
7
  const PROTOCOL = "house.kit.stream.1";
8
- const WATCHED_SUBJECTS = ["agents", "working_copy"];
8
+ const WATCHED_SUBJECTS = ["agents", "working_copy", "sources"];
9
9
 
10
10
  function streamAddress(house) {
11
11
  const url = new URL("/kit/stream", house);
package/src/setup.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  updateDeclaredAgent,
7
7
  } from "./agents.js";
8
8
  import { readConfiguration } from "./configuration.js";
9
- import { connectorLogin } from "./connector-login.js";
9
+ import { customAuthorizationStatus } from "./custom/authorization.js";
10
10
  import { GUIDED_LOGIN_TIMEOUT_MS } from "./contract.js";
11
11
  import { launchBase, prepareLaunchBase } from "./directory.js";
12
12
  import {
@@ -45,24 +45,14 @@ function agentModule(context, kind) {
45
45
  }
46
46
 
47
47
  function connectorModule(context, instance) {
48
- const gateway = connectorLogin(instance, context.dependencies);
49
48
  return {
50
49
  category: "connector",
51
50
  name: instance.id,
52
51
  login: { integration: instance.preset, instance: instance.id, service: instance.id },
53
52
  install: null,
54
53
  signedIn: () =>
55
- gateway.status(instance, { directory: context.directory, signal: context.signal }),
56
- guided:
57
- gateway.guided === undefined
58
- ? null
59
- : ({ present, signal }) =>
60
- gateway.guided(instance, {
61
- directory: context.directory,
62
- dependencies: context.dependencies,
63
- present,
64
- signal,
65
- }),
54
+ customAuthorizationStatus(instance, { directory: context.directory, signal: context.signal }),
55
+ guided: null,
66
56
  };
67
57
  }
68
58
 
@@ -144,8 +134,12 @@ async function setUpModule(context, module, install = true) {
144
134
  return summary("ready");
145
135
  }
146
136
 
137
+ export function signInModule(context, module) {
138
+ return setUpModule(context, module, false);
139
+ }
140
+
147
141
  export function signInWantedAgent(context, kind) {
148
- return setUpModule(context, agentModule(context, kind), false);
142
+ return signInModule(context, agentModule(context, kind));
149
143
  }
150
144
 
151
145
  export async function runSetup(context, request) {