botinabox 2.5.0 → 2.5.2
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 -21
- package/README.md +190 -190
- package/bin/botinabox.mjs +2 -2
- package/dist/channels/discord/adapter.d.ts +32 -0
- package/dist/channels/discord/adapter.js +70 -0
- package/dist/channels/discord/inbound.d.ts +25 -0
- package/dist/channels/discord/inbound.js +24 -0
- package/dist/channels/discord/models.d.ts +8 -0
- package/dist/channels/discord/models.js +5 -0
- package/dist/channels/discord/outbound.d.ts +14 -0
- package/dist/channels/discord/outbound.js +38 -0
- package/dist/channels/slack/adapter.d.ts +33 -0
- package/dist/channels/slack/adapter.js +74 -0
- package/dist/channels/slack/inbound.d.ts +59 -0
- package/dist/channels/slack/inbound.js +96 -0
- package/dist/channels/slack/models.d.ts +9 -0
- package/dist/channels/slack/models.js +5 -0
- package/dist/channels/slack/outbound.d.ts +12 -0
- package/dist/channels/slack/outbound.js +18 -0
- package/dist/channels/slack/transcribe.d.ts +41 -0
- package/dist/channels/slack/transcribe.js +106 -0
- package/dist/channels/webhook/adapter.d.ts +23 -0
- package/dist/channels/webhook/adapter.js +86 -0
- package/dist/channels/webhook/hmac.d.ts +13 -0
- package/dist/channels/webhook/hmac.js +26 -0
- package/dist/channels/webhook/models.d.ts +9 -0
- package/dist/channels/webhook/models.js +5 -0
- package/dist/channels/webhook/server.d.ts +20 -0
- package/dist/channels/webhook/server.js +91 -0
- package/dist/chat-pipeline-BWrtVqEP.d.ts +652 -0
- package/dist/chat-pipeline-C-XlLGNl.d.ts +648 -0
- package/dist/chat-pipeline-CR1KF6eX.d.ts +652 -0
- package/dist/chat-pipeline-DisuC8SB.d.ts +643 -0
- package/dist/chunk-2LGXQPEA.js +41 -0
- package/dist/chunk-3X3YKI4T.js +357 -0
- package/dist/chunk-D47AIFOD.js +351 -0
- package/dist/chunk-DSNJKNEW.js +328 -0
- package/dist/chunk-GS2JFL6I.js +144 -0
- package/dist/chunk-J6S6QMUY.js +144 -0
- package/dist/chunk-QLA6YOFN.js +22 -0
- package/dist/chunk-UACT2WXX.js +381 -0
- package/dist/cli/templates/config.yml.d.ts +7 -0
- package/dist/cli/templates/config.yml.js +61 -0
- package/dist/cli/templates/env.d.ts +1 -0
- package/dist/cli/templates/env.js +30 -0
- package/dist/cli/templates/index.ts.d.ts +2 -0
- package/dist/cli/templates/index.ts.js +30 -0
- package/dist/cli/templates/package.json.d.ts +5 -0
- package/dist/cli/templates/package.json.js +28 -0
- package/dist/cli.js +0 -0
- package/dist/connector-DDahQw-2.d.ts +63 -0
- package/dist/connectors/google/calendar-connector.d.ts +40 -0
- package/dist/connectors/google/calendar-connector.js +243 -0
- package/dist/connectors/google/gmail-connector.d.ts +42 -0
- package/dist/connectors/google/gmail-connector.js +345 -0
- package/dist/connectors/google/index.d.ts +67 -1
- package/dist/connectors/google/index.js +240 -0
- package/dist/connectors/google/oauth.d.ts +48 -0
- package/dist/connectors/google/oauth.js +112 -0
- package/dist/connectors/google/types.d.ts +78 -0
- package/dist/connectors/google/types.js +2 -0
- package/dist/core/chat/auto-discovery.d.ts +16 -0
- package/dist/core/chat/auto-discovery.js +54 -0
- package/dist/core/chat/channel-registry.d.ts +45 -0
- package/dist/core/chat/channel-registry.js +96 -0
- package/dist/core/chat/chat-pipeline.d.ts +113 -0
- package/dist/core/chat/chat-pipeline.js +395 -0
- package/dist/core/chat/chat-responder.d.ts +90 -0
- package/dist/core/chat/chat-responder.js +185 -0
- package/dist/core/chat/formatter.d.ts +11 -0
- package/dist/core/chat/formatter.js +60 -0
- package/dist/core/chat/index.d.ts +24 -0
- package/dist/core/chat/index.js +18 -0
- package/dist/core/chat/message-interpreter.d.ts +91 -0
- package/dist/core/chat/message-interpreter.js +166 -0
- package/dist/core/chat/message-store.d.ts +66 -0
- package/dist/core/chat/message-store.js +131 -0
- package/dist/core/chat/notification-queue.d.ts +34 -0
- package/dist/core/chat/notification-queue.js +111 -0
- package/dist/core/chat/pipeline.d.ts +38 -0
- package/dist/core/chat/pipeline.js +89 -0
- package/dist/core/chat/policies.d.ts +16 -0
- package/dist/core/chat/policies.js +25 -0
- package/dist/core/chat/routing.d.ts +17 -0
- package/dist/core/chat/routing.js +36 -0
- package/dist/core/chat/session-key.d.ts +30 -0
- package/dist/core/chat/session-key.js +65 -0
- package/dist/core/chat/session-manager.d.ts +17 -0
- package/dist/core/chat/session-manager.js +23 -0
- package/dist/core/chat/text-chunker.d.ts +9 -0
- package/dist/core/chat/text-chunker.js +48 -0
- package/dist/core/chat/triage-router.d.ts +75 -0
- package/dist/core/chat/triage-router.js +142 -0
- package/dist/core/chat/types.d.ts +5 -0
- package/dist/core/chat/types.js +5 -0
- package/dist/core/config/defaults.d.ts +2 -0
- package/dist/core/config/defaults.js +38 -0
- package/dist/core/config/index.d.ts +6 -0
- package/dist/core/config/index.js +4 -0
- package/dist/core/config/interpolate.d.ts +5 -0
- package/dist/core/config/interpolate.js +27 -0
- package/dist/core/config/loader.d.ts +24 -0
- package/dist/core/config/loader.js +59 -0
- package/dist/core/config/schema.d.ts +5 -0
- package/dist/core/config/schema.js +119 -0
- package/dist/core/data/core-entity-contexts.d.ts +14 -0
- package/dist/core/data/core-entity-contexts.js +197 -0
- package/dist/core/data/core-migrations.d.ts +5 -0
- package/dist/core/data/core-migrations.js +45 -0
- package/dist/core/data/core-schema.d.ts +6 -0
- package/dist/core/data/core-schema.js +454 -0
- package/dist/core/data/data-store.d.ts +67 -0
- package/dist/core/data/data-store.js +218 -0
- package/dist/core/data/domain-entity-contexts.d.ts +29 -0
- package/dist/core/data/domain-entity-contexts.js +321 -0
- package/dist/core/data/domain-schema.d.ts +36 -0
- package/dist/core/data/domain-schema.js +323 -0
- package/dist/core/data/index.d.ts +7 -0
- package/dist/core/data/index.js +7 -0
- package/dist/core/data/types.d.ts +111 -0
- package/dist/core/data/types.js +1 -0
- package/dist/core/hooks/hook-bus.d.ts +18 -0
- package/dist/core/hooks/hook-bus.js +120 -0
- package/dist/core/hooks/index.d.ts +2 -0
- package/dist/core/hooks/index.js +1 -0
- package/dist/core/hooks/types.d.ts +19 -0
- package/dist/core/hooks/types.js +1 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.js +4 -0
- package/dist/core/llm/auto-discovery.d.ts +11 -0
- package/dist/core/llm/auto-discovery.js +49 -0
- package/dist/core/llm/cost-tracker.d.ts +6 -0
- package/dist/core/llm/cost-tracker.js +38 -0
- package/dist/core/llm/index.d.ts +4 -0
- package/dist/core/llm/index.js +3 -0
- package/dist/core/llm/model-router.d.ts +25 -0
- package/dist/core/llm/model-router.js +49 -0
- package/dist/core/llm/provider-registry.d.ts +9 -0
- package/dist/core/llm/provider-registry.js +25 -0
- package/dist/core/llm/types.d.ts +2 -0
- package/dist/core/llm/types.js +2 -0
- package/dist/core/orchestrator/adapters/api-adapter.d.ts +34 -0
- package/dist/core/orchestrator/adapters/api-adapter.js +88 -0
- package/dist/core/orchestrator/adapters/cli-adapter.d.ts +22 -0
- package/dist/core/orchestrator/adapters/cli-adapter.js +69 -0
- package/dist/core/orchestrator/adapters/deterministic-adapter.d.ts +35 -0
- package/dist/core/orchestrator/adapters/deterministic-adapter.js +75 -0
- package/dist/core/orchestrator/adapters/env-whitelist.d.ts +4 -0
- package/dist/core/orchestrator/adapters/env-whitelist.js +27 -0
- package/dist/core/orchestrator/adapters/output-extractor.d.ts +11 -0
- package/dist/core/orchestrator/adapters/output-extractor.js +59 -0
- package/dist/core/orchestrator/adapters/process-manager.d.ts +15 -0
- package/dist/core/orchestrator/adapters/process-manager.js +26 -0
- package/dist/core/orchestrator/adapters/tool-loop.d.ts +22 -0
- package/dist/core/orchestrator/adapters/tool-loop.js +66 -0
- package/dist/core/orchestrator/agent-registry.d.ts +31 -0
- package/dist/core/orchestrator/agent-registry.js +135 -0
- package/dist/core/orchestrator/budget-controller.d.ts +19 -0
- package/dist/core/orchestrator/budget-controller.js +73 -0
- package/dist/core/orchestrator/chain-guard.d.ts +14 -0
- package/dist/core/orchestrator/chain-guard.js +23 -0
- package/dist/core/orchestrator/circuit-breaker.d.ts +65 -0
- package/dist/core/orchestrator/circuit-breaker.js +159 -0
- package/dist/core/orchestrator/claude-stream-parser.d.ts +31 -0
- package/dist/core/orchestrator/claude-stream-parser.js +99 -0
- package/dist/core/orchestrator/config-revisions.d.ts +6 -0
- package/dist/core/orchestrator/config-revisions.js +17 -0
- package/dist/core/orchestrator/dependency-resolver.d.ts +20 -0
- package/dist/core/orchestrator/dependency-resolver.js +78 -0
- package/dist/core/orchestrator/governance-gate.d.ts +110 -0
- package/dist/core/orchestrator/governance-gate.js +170 -0
- package/dist/core/orchestrator/learning-pipeline.d.ts +109 -0
- package/dist/core/orchestrator/learning-pipeline.js +249 -0
- package/dist/core/orchestrator/loop-detector.d.ts +51 -0
- package/dist/core/orchestrator/loop-detector.js +133 -0
- package/dist/core/orchestrator/ndjson-logger.d.ts +6 -0
- package/dist/core/orchestrator/ndjson-logger.js +18 -0
- package/dist/core/orchestrator/permission-relay.d.ts +72 -0
- package/dist/core/orchestrator/permission-relay.js +164 -0
- package/dist/core/orchestrator/run-manager.d.ts +31 -0
- package/dist/core/orchestrator/run-manager.js +178 -0
- package/dist/core/orchestrator/scheduler.d.ts +70 -0
- package/dist/core/orchestrator/scheduler.js +198 -0
- package/dist/core/orchestrator/secret-store.d.ts +57 -0
- package/dist/core/orchestrator/secret-store.js +171 -0
- package/dist/core/orchestrator/session-manager.d.ts +13 -0
- package/dist/core/orchestrator/session-manager.js +66 -0
- package/dist/core/orchestrator/task-queue.d.ts +34 -0
- package/dist/core/orchestrator/task-queue.js +83 -0
- package/dist/core/orchestrator/template-interpolate.d.ts +5 -0
- package/dist/core/orchestrator/template-interpolate.js +18 -0
- package/dist/core/orchestrator/user-registry.d.ts +47 -0
- package/dist/core/orchestrator/user-registry.js +76 -0
- package/dist/core/orchestrator/wakeup-queue.d.ts +9 -0
- package/dist/core/orchestrator/wakeup-queue.js +45 -0
- package/dist/core/orchestrator/workflow-engine.d.ts +47 -0
- package/dist/core/orchestrator/workflow-engine.js +204 -0
- package/dist/core/security/audit.d.ts +20 -0
- package/dist/core/security/audit.js +33 -0
- package/dist/core/security/column-validator.d.ts +20 -0
- package/dist/core/security/column-validator.js +37 -0
- package/dist/core/security/index.d.ts +5 -0
- package/dist/core/security/index.js +5 -0
- package/dist/core/security/process-env.d.ts +13 -0
- package/dist/core/security/process-env.js +49 -0
- package/dist/core/security/sanitizer.d.ts +11 -0
- package/dist/core/security/sanitizer.js +39 -0
- package/dist/core/security/types.d.ts +11 -0
- package/dist/core/security/types.js +1 -0
- package/dist/core/update/auto-update.d.ts +21 -0
- package/dist/core/update/auto-update.js +102 -0
- package/dist/core/update/backup-manager.d.ts +7 -0
- package/dist/core/update/backup-manager.js +24 -0
- package/dist/core/update/index.d.ts +8 -0
- package/dist/core/update/index.js +6 -0
- package/dist/core/update/migration-hooks.d.ts +11 -0
- package/dist/core/update/migration-hooks.js +10 -0
- package/dist/core/update/types.d.ts +11 -0
- package/dist/core/update/types.js +1 -0
- package/dist/core/update/update-checker.d.ts +11 -0
- package/dist/core/update/update-checker.js +63 -0
- package/dist/core/update/update-manager.d.ts +25 -0
- package/dist/core/update/update-manager.js +101 -0
- package/dist/core/update/version-utils.d.ts +6 -0
- package/dist/core/update/version-utils.js +34 -0
- package/dist/gmail-connector-2FVYTQJH.js +6 -0
- package/dist/gmail-connector-MNUBRNFM.js +6 -0
- package/dist/gmail-connector-PS2VLGNE.js +6 -0
- package/dist/gmail-connector-ULSMN6X2.js +6 -0
- package/dist/gmail-connector-URRFX6A3.js +6 -0
- package/dist/inbound-AFBUPSPG.js +10 -0
- package/dist/inbound-AFOHYNUY.js +6 -0
- package/dist/inbound-CGIXRXGC.js +8 -0
- package/dist/inbound-MCOLRH6U.js +10 -0
- package/dist/inbound-SNEMBLGA.js +6 -0
- package/dist/inbound-ZJHAYVMF.js +10 -0
- package/dist/index.d.ts +11 -1
- package/dist/index.js +27 -11
- package/dist/provider-qqJYv9nv.d.ts +75 -0
- package/dist/providers/anthropic/models.d.ts +2 -0
- package/dist/providers/anthropic/models.js +29 -0
- package/dist/providers/anthropic/provider.d.ts +13 -0
- package/dist/providers/anthropic/provider.js +119 -0
- package/dist/providers/anthropic/tool-converter.d.ts +10 -0
- package/dist/providers/anthropic/tool-converter.js +7 -0
- package/dist/providers/ollama/provider.d.ts +17 -0
- package/dist/providers/ollama/provider.js +185 -0
- package/dist/providers/openai/models.d.ts +2 -0
- package/dist/providers/openai/models.js +29 -0
- package/dist/providers/openai/provider.d.ts +13 -0
- package/dist/providers/openai/provider.js +163 -0
- package/dist/providers/openai/tool-converter.d.ts +10 -0
- package/dist/providers/openai/tool-converter.js +10 -0
- package/dist/shared/constants.d.ts +50 -0
- package/dist/shared/constants.js +64 -0
- package/dist/shared/index.d.ts +14 -0
- package/dist/shared/index.js +14 -0
- package/dist/shared/types/agent.d.ts +36 -0
- package/dist/shared/types/agent.js +2 -0
- package/dist/shared/types/channel.d.ts +70 -0
- package/dist/shared/types/channel.js +2 -0
- package/dist/shared/types/config.d.ts +111 -0
- package/dist/shared/types/config.js +2 -0
- package/dist/shared/types/connector.d.ts +77 -0
- package/dist/shared/types/connector.js +2 -0
- package/dist/shared/types/execution.d.ts +29 -0
- package/dist/shared/types/execution.js +2 -0
- package/dist/shared/types/provider.d.ts +73 -0
- package/dist/shared/types/provider.js +2 -0
- package/dist/shared/types/task.d.ts +47 -0
- package/dist/shared/types/task.js +2 -0
- package/dist/shared/types/workflow.d.ts +39 -0
- package/dist/shared/types/workflow.js +2 -0
- package/dist/shared/utils.d.ts +6 -0
- package/dist/shared/utils.js +13 -0
- package/dist/update-check.d.ts +5 -0
- package/dist/update-check.js +56 -0
- package/package.json +100 -100
|
@@ -54,6 +54,29 @@ interface EmailRecord {
|
|
|
54
54
|
labels: string[];
|
|
55
55
|
isRead: boolean;
|
|
56
56
|
}
|
|
57
|
+
interface DriveOwner {
|
|
58
|
+
displayName: string;
|
|
59
|
+
emailAddress: string;
|
|
60
|
+
}
|
|
61
|
+
interface DriveFileRecord {
|
|
62
|
+
driveFileId: string;
|
|
63
|
+
account: string;
|
|
64
|
+
name: string;
|
|
65
|
+
mimeType: string;
|
|
66
|
+
webViewLink: string;
|
|
67
|
+
webContentLink?: string;
|
|
68
|
+
/** ISO 8601 */
|
|
69
|
+
modifiedTime: string;
|
|
70
|
+
/** ISO 8601 */
|
|
71
|
+
createdTime: string;
|
|
72
|
+
size?: number;
|
|
73
|
+
parents?: string[];
|
|
74
|
+
description?: string;
|
|
75
|
+
owners: DriveOwner[];
|
|
76
|
+
lastModifyingUser?: DriveOwner;
|
|
77
|
+
starred: boolean;
|
|
78
|
+
trashed: boolean;
|
|
79
|
+
}
|
|
57
80
|
interface CalendarAttendee {
|
|
58
81
|
email: string;
|
|
59
82
|
displayName?: string;
|
|
@@ -209,4 +232,47 @@ declare class GoogleCalendarConnector implements Connector<CalendarEventRecord>
|
|
|
209
232
|
private mapEvent;
|
|
210
233
|
}
|
|
211
234
|
|
|
212
|
-
|
|
235
|
+
/**
|
|
236
|
+
* Google Drive connector — pulls file metadata from Drive.
|
|
237
|
+
*
|
|
238
|
+
* Produces `DriveFileRecord` objects. Does NOT write to any database
|
|
239
|
+
* table; the consuming application decides how to store records.
|
|
240
|
+
*
|
|
241
|
+
* Supports incremental sync via Drive Changes API (startPageToken)
|
|
242
|
+
* and full sync via files.list with optional folder/MIME filters.
|
|
243
|
+
*/
|
|
244
|
+
|
|
245
|
+
interface DriveConnectorOpts {
|
|
246
|
+
/** Load persisted tokens for a given account key (OAuth2 flow only). */
|
|
247
|
+
tokenLoader?: (key: string) => Promise<string | null>;
|
|
248
|
+
/** Persist tokens for a given account key (OAuth2 flow only). */
|
|
249
|
+
tokenSaver?: (key: string, value: string) => Promise<void>;
|
|
250
|
+
}
|
|
251
|
+
declare class GoogleDriveConnector implements Connector<DriveFileRecord> {
|
|
252
|
+
readonly id = "google-drive";
|
|
253
|
+
readonly meta: ConnectorMeta;
|
|
254
|
+
private tokenLoader?;
|
|
255
|
+
private tokenSaver?;
|
|
256
|
+
private client;
|
|
257
|
+
private config;
|
|
258
|
+
private tokens;
|
|
259
|
+
private drive;
|
|
260
|
+
constructor(opts?: DriveConnectorOpts);
|
|
261
|
+
connect(config: GoogleConnectorConfig): Promise<void>;
|
|
262
|
+
disconnect(): Promise<void>;
|
|
263
|
+
healthCheck(): Promise<{
|
|
264
|
+
ok: boolean;
|
|
265
|
+
account?: string;
|
|
266
|
+
error?: string;
|
|
267
|
+
}>;
|
|
268
|
+
authenticate(codeProvider: (authUrl: string) => Promise<string>): Promise<AuthResult>;
|
|
269
|
+
sync(options?: SyncOptions): Promise<SyncResult<DriveFileRecord>>;
|
|
270
|
+
/** Incremental sync using Drive Changes API. */
|
|
271
|
+
private syncIncremental;
|
|
272
|
+
/** Full sync using files.list. */
|
|
273
|
+
private syncFull;
|
|
274
|
+
private ensureConnected;
|
|
275
|
+
private mapFile;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export { type CalendarAttendee, type CalendarConnectorOpts, type CalendarEventRecord, type DriveConnectorOpts, type DriveFileRecord, type DriveOwner, type EmailAddress, type EmailRecord, type GmailConnectorOpts, GoogleCalendarConnector, type GoogleConnectorConfig, GoogleDriveConnector, GoogleGmailConnector, type GoogleOAuthConfig, type GoogleServiceAccountConfig, type GoogleTokens, createOAuth2Client, createServiceAccountClient, exchangeCode, getAuthUrl, loadTokens, refreshIfNeeded, saveTokens };
|
|
@@ -235,8 +235,248 @@ var GoogleCalendarConnector = class {
|
|
|
235
235
|
function errorMessage(err) {
|
|
236
236
|
return err instanceof Error ? err.message : String(err);
|
|
237
237
|
}
|
|
238
|
+
|
|
239
|
+
// src/connectors/google/drive-connector.ts
|
|
240
|
+
var FILE_FIELDS = "id, name, mimeType, webViewLink, webContentLink, modifiedTime, createdTime, size, parents, description, owners, lastModifyingUser, starred, trashed";
|
|
241
|
+
var GoogleDriveConnector = class {
|
|
242
|
+
id = "google-drive";
|
|
243
|
+
meta = {
|
|
244
|
+
displayName: "Google Drive",
|
|
245
|
+
provider: "google",
|
|
246
|
+
dataType: "document"
|
|
247
|
+
};
|
|
248
|
+
tokenLoader;
|
|
249
|
+
tokenSaver;
|
|
250
|
+
client = null;
|
|
251
|
+
config = null;
|
|
252
|
+
tokens = null;
|
|
253
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
254
|
+
drive = null;
|
|
255
|
+
constructor(opts = {}) {
|
|
256
|
+
this.tokenLoader = opts.tokenLoader;
|
|
257
|
+
this.tokenSaver = opts.tokenSaver;
|
|
258
|
+
}
|
|
259
|
+
// ── Lifecycle ──────────────────────────────────────────────────
|
|
260
|
+
async connect(config) {
|
|
261
|
+
this.config = config;
|
|
262
|
+
const scopes = config.scopes ?? [
|
|
263
|
+
"https://www.googleapis.com/auth/drive.readonly"
|
|
264
|
+
];
|
|
265
|
+
if (config.serviceAccount) {
|
|
266
|
+
this.client = await createServiceAccountClient(config.serviceAccount, scopes);
|
|
267
|
+
} else if (config.oauth) {
|
|
268
|
+
this.client = await createOAuth2Client(config.oauth);
|
|
269
|
+
if (!this.tokenLoader) {
|
|
270
|
+
throw new Error("tokenLoader required for OAuth2 flow");
|
|
271
|
+
}
|
|
272
|
+
this.tokens = await loadTokens(this.tokenLoader, config.account);
|
|
273
|
+
if (!this.tokens) {
|
|
274
|
+
throw new Error(
|
|
275
|
+
`No stored tokens for account ${config.account}. Complete the OAuth flow first.`
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
this.tokens = await refreshIfNeeded(
|
|
279
|
+
this.client,
|
|
280
|
+
this.tokens,
|
|
281
|
+
this.tokenSaver ? async (t) => saveTokens(this.tokenSaver, config.account, t) : void 0
|
|
282
|
+
);
|
|
283
|
+
this.client.setCredentials(this.tokens);
|
|
284
|
+
} else {
|
|
285
|
+
throw new Error("Either serviceAccount or oauth config is required");
|
|
286
|
+
}
|
|
287
|
+
const { google } = await import("googleapis");
|
|
288
|
+
this.drive = google.drive({ version: "v3", auth: this.client });
|
|
289
|
+
}
|
|
290
|
+
async disconnect() {
|
|
291
|
+
this.client = null;
|
|
292
|
+
this.drive = null;
|
|
293
|
+
this.tokens = null;
|
|
294
|
+
this.config = null;
|
|
295
|
+
}
|
|
296
|
+
async healthCheck() {
|
|
297
|
+
try {
|
|
298
|
+
this.ensureConnected();
|
|
299
|
+
const res = await this.drive.about.get({ fields: "user" });
|
|
300
|
+
return { ok: true, account: res.data.user?.emailAddress ?? this.config.account };
|
|
301
|
+
} catch (err) {
|
|
302
|
+
return { ok: false, error: errorMessage2(err) };
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// ── Auth ───────────────────────────────────────────────────────
|
|
306
|
+
async authenticate(codeProvider) {
|
|
307
|
+
if (!this.config) {
|
|
308
|
+
return { success: false, error: "Call connect() first." };
|
|
309
|
+
}
|
|
310
|
+
try {
|
|
311
|
+
if (!this.config.oauth) {
|
|
312
|
+
return { success: false, error: "OAuth config required. Use serviceAccount for headless auth." };
|
|
313
|
+
}
|
|
314
|
+
if (!this.tokenSaver) {
|
|
315
|
+
return { success: false, error: "tokenSaver required for authenticate() flow." };
|
|
316
|
+
}
|
|
317
|
+
const client = await createOAuth2Client(this.config.oauth);
|
|
318
|
+
const scopes = this.config.scopes ?? [
|
|
319
|
+
"https://www.googleapis.com/auth/drive.readonly"
|
|
320
|
+
];
|
|
321
|
+
const authUrl = getAuthUrl(client, scopes);
|
|
322
|
+
const code = await codeProvider(authUrl);
|
|
323
|
+
const tokens = await exchangeCode(client, code);
|
|
324
|
+
await saveTokens(this.tokenSaver, this.config.account, tokens);
|
|
325
|
+
this.tokens = tokens;
|
|
326
|
+
this.client = client;
|
|
327
|
+
this.client.setCredentials(tokens);
|
|
328
|
+
const { google } = await import("googleapis");
|
|
329
|
+
this.drive = google.drive({ version: "v3", auth: this.client });
|
|
330
|
+
return { success: true, account: this.config.account };
|
|
331
|
+
} catch (err) {
|
|
332
|
+
return { success: false, error: errorMessage2(err) };
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
// ── Sync ───────────────────────────────────────────────────────
|
|
336
|
+
async sync(options) {
|
|
337
|
+
this.ensureConnected();
|
|
338
|
+
if (options?.cursor) {
|
|
339
|
+
return this.syncIncremental(options.cursor, options);
|
|
340
|
+
}
|
|
341
|
+
return this.syncFull(options);
|
|
342
|
+
}
|
|
343
|
+
/** Incremental sync using Drive Changes API. */
|
|
344
|
+
async syncIncremental(startPageToken, options) {
|
|
345
|
+
const records = [];
|
|
346
|
+
const errors = [];
|
|
347
|
+
let pageToken = startPageToken;
|
|
348
|
+
let newStartPageToken;
|
|
349
|
+
try {
|
|
350
|
+
do {
|
|
351
|
+
const res = await this.drive.changes.list({
|
|
352
|
+
pageToken,
|
|
353
|
+
fields: `nextPageToken, newStartPageToken, changes(fileId, removed, file(${FILE_FIELDS}))`,
|
|
354
|
+
pageSize: options?.limit ? Math.min(options.limit - records.length, 100) : 100
|
|
355
|
+
});
|
|
356
|
+
for (const change of res.data.changes ?? []) {
|
|
357
|
+
try {
|
|
358
|
+
if (change.removed || !change.file) {
|
|
359
|
+
if (change.fileId) {
|
|
360
|
+
records.push({
|
|
361
|
+
driveFileId: change.fileId,
|
|
362
|
+
account: this.config.account,
|
|
363
|
+
name: "",
|
|
364
|
+
mimeType: "",
|
|
365
|
+
webViewLink: "",
|
|
366
|
+
modifiedTime: (/* @__PURE__ */ new Date()).toISOString(),
|
|
367
|
+
createdTime: "",
|
|
368
|
+
owners: [],
|
|
369
|
+
starred: false,
|
|
370
|
+
trashed: true
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
} else {
|
|
374
|
+
records.push(this.mapFile(change.file));
|
|
375
|
+
}
|
|
376
|
+
} catch (err) {
|
|
377
|
+
errors.push({ id: change.fileId ?? "unknown", error: errorMessage2(err) });
|
|
378
|
+
}
|
|
379
|
+
if (options?.limit && records.length >= options.limit) break;
|
|
380
|
+
}
|
|
381
|
+
pageToken = res.data.nextPageToken ?? void 0;
|
|
382
|
+
newStartPageToken = res.data.newStartPageToken ?? void 0;
|
|
383
|
+
} while (pageToken && (!options?.limit || records.length < options.limit));
|
|
384
|
+
} catch (err) {
|
|
385
|
+
if (err?.code === 403 || err?.code === 404) {
|
|
386
|
+
return this.syncFull(options);
|
|
387
|
+
}
|
|
388
|
+
throw err;
|
|
389
|
+
}
|
|
390
|
+
return {
|
|
391
|
+
records,
|
|
392
|
+
cursor: newStartPageToken,
|
|
393
|
+
hasMore: !!pageToken,
|
|
394
|
+
errors
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
/** Full sync using files.list. */
|
|
398
|
+
async syncFull(options) {
|
|
399
|
+
const records = [];
|
|
400
|
+
const errors = [];
|
|
401
|
+
const maxResults = options?.limit ?? 500;
|
|
402
|
+
const queryParts = ["trashed = false"];
|
|
403
|
+
const folderId = options?.filters?.folderId;
|
|
404
|
+
if (folderId) {
|
|
405
|
+
queryParts.push(`'${folderId}' in parents`);
|
|
406
|
+
}
|
|
407
|
+
const mimeType = options?.filters?.mimeType;
|
|
408
|
+
if (mimeType) {
|
|
409
|
+
queryParts.push(`mimeType = '${mimeType}'`);
|
|
410
|
+
}
|
|
411
|
+
if (options?.since) {
|
|
412
|
+
queryParts.push(`modifiedTime > '${new Date(options.since).toISOString()}'`);
|
|
413
|
+
}
|
|
414
|
+
const q = queryParts.join(" and ");
|
|
415
|
+
let pageToken;
|
|
416
|
+
do {
|
|
417
|
+
const res = await this.drive.files.list({
|
|
418
|
+
q,
|
|
419
|
+
fields: `nextPageToken, files(${FILE_FIELDS})`,
|
|
420
|
+
orderBy: "modifiedTime desc",
|
|
421
|
+
pageSize: Math.min(maxResults - records.length, 100),
|
|
422
|
+
...pageToken ? { pageToken } : {}
|
|
423
|
+
});
|
|
424
|
+
for (const file of res.data.files ?? []) {
|
|
425
|
+
try {
|
|
426
|
+
records.push(this.mapFile(file));
|
|
427
|
+
} catch (err) {
|
|
428
|
+
errors.push({ id: file.id, error: errorMessage2(err) });
|
|
429
|
+
}
|
|
430
|
+
if (records.length >= maxResults) break;
|
|
431
|
+
}
|
|
432
|
+
pageToken = res.data.nextPageToken ?? void 0;
|
|
433
|
+
} while (pageToken && records.length < maxResults);
|
|
434
|
+
const tokenRes = await this.drive.changes.getStartPageToken({});
|
|
435
|
+
const cursor = tokenRes.data.startPageToken ?? void 0;
|
|
436
|
+
return {
|
|
437
|
+
records,
|
|
438
|
+
cursor,
|
|
439
|
+
hasMore: !!pageToken,
|
|
440
|
+
errors
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
// ── Internals ─────────────────────────────────────────────────
|
|
444
|
+
ensureConnected() {
|
|
445
|
+
if (!this.drive || !this.config) {
|
|
446
|
+
throw new Error("GoogleDriveConnector is not connected. Call connect() first.");
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
450
|
+
mapFile(file) {
|
|
451
|
+
const mapOwner = (o) => ({
|
|
452
|
+
displayName: o?.displayName ?? "",
|
|
453
|
+
emailAddress: o?.emailAddress ?? ""
|
|
454
|
+
});
|
|
455
|
+
return {
|
|
456
|
+
driveFileId: file.id,
|
|
457
|
+
account: this.config.account,
|
|
458
|
+
name: file.name ?? "(Untitled)",
|
|
459
|
+
mimeType: file.mimeType ?? "",
|
|
460
|
+
webViewLink: file.webViewLink ?? "",
|
|
461
|
+
webContentLink: file.webContentLink ?? void 0,
|
|
462
|
+
modifiedTime: file.modifiedTime ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
463
|
+
createdTime: file.createdTime ?? "",
|
|
464
|
+
size: file.size ? parseInt(file.size, 10) : void 0,
|
|
465
|
+
parents: file.parents ?? void 0,
|
|
466
|
+
description: file.description ?? void 0,
|
|
467
|
+
owners: (file.owners ?? []).map(mapOwner),
|
|
468
|
+
lastModifyingUser: file.lastModifyingUser ? mapOwner(file.lastModifyingUser) : void 0,
|
|
469
|
+
starred: file.starred ?? false,
|
|
470
|
+
trashed: file.trashed ?? false
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
function errorMessage2(err) {
|
|
475
|
+
return err instanceof Error ? err.message : String(err);
|
|
476
|
+
}
|
|
238
477
|
export {
|
|
239
478
|
GoogleCalendarConnector,
|
|
479
|
+
GoogleDriveConnector,
|
|
240
480
|
GoogleGmailConnector,
|
|
241
481
|
createOAuth2Client,
|
|
242
482
|
createServiceAccountClient,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google OAuth2 helpers.
|
|
3
|
+
*
|
|
4
|
+
* Uses dynamic `import('googleapis')` so the package is only required
|
|
5
|
+
* at runtime by consumers who actually use the Google connectors.
|
|
6
|
+
*/
|
|
7
|
+
import type { GoogleOAuthConfig, GoogleServiceAccountConfig, GoogleTokens } from './types.js';
|
|
8
|
+
type OAuth2Client = any;
|
|
9
|
+
/**
|
|
10
|
+
* Create a Google OAuth2 client from app credentials.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createOAuth2Client(config: GoogleOAuthConfig): Promise<OAuth2Client>;
|
|
13
|
+
/**
|
|
14
|
+
* Generate the consent screen URL for the given scopes.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getAuthUrl(client: OAuth2Client, scopes: string[]): string;
|
|
17
|
+
/**
|
|
18
|
+
* Exchange an authorization code for tokens.
|
|
19
|
+
*/
|
|
20
|
+
export declare function exchangeCode(client: OAuth2Client, code: string): Promise<GoogleTokens>;
|
|
21
|
+
/**
|
|
22
|
+
* Create an authenticated client using a service account with
|
|
23
|
+
* domain-wide delegation (impersonation). No browser flow needed.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createServiceAccountClient(config: GoogleServiceAccountConfig, scopes: string[]): Promise<OAuth2Client>;
|
|
26
|
+
/**
|
|
27
|
+
* Load persisted tokens via a generic getter callback.
|
|
28
|
+
*
|
|
29
|
+
* @param getter Reads a string value by key (e.g. SecretStore.get)
|
|
30
|
+
* @param accountKey Unique key prefix for this account
|
|
31
|
+
*/
|
|
32
|
+
export declare function loadTokens(getter: (key: string) => Promise<string | null>, accountKey: string): Promise<GoogleTokens | null>;
|
|
33
|
+
/**
|
|
34
|
+
* Persist tokens via a generic setter callback.
|
|
35
|
+
*
|
|
36
|
+
* @param setter Writes a string value by key (e.g. SecretStore.set)
|
|
37
|
+
* @param accountKey Unique key prefix for this account
|
|
38
|
+
* @param tokens Tokens to persist
|
|
39
|
+
*/
|
|
40
|
+
export declare function saveTokens(setter: (key: string, value: string) => Promise<void>, accountKey: string, tokens: GoogleTokens): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Refresh the access token if it has expired (or is about to within 60 s).
|
|
43
|
+
*
|
|
44
|
+
* If the token was refreshed and a `saver` callback is provided, the new
|
|
45
|
+
* tokens are persisted automatically.
|
|
46
|
+
*/
|
|
47
|
+
export declare function refreshIfNeeded(client: OAuth2Client, tokens: GoogleTokens, saver?: (tokens: GoogleTokens) => Promise<void>): Promise<GoogleTokens>;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google OAuth2 helpers.
|
|
3
|
+
*
|
|
4
|
+
* Uses dynamic `import('googleapis')` so the package is only required
|
|
5
|
+
* at runtime by consumers who actually use the Google connectors.
|
|
6
|
+
*/
|
|
7
|
+
// ── Lazy googleapis import ─────────────────────────────────────────
|
|
8
|
+
let _google;
|
|
9
|
+
async function getGoogle() {
|
|
10
|
+
if (!_google) {
|
|
11
|
+
try {
|
|
12
|
+
const mod = await import('googleapis');
|
|
13
|
+
_google = mod.google;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
throw new Error('googleapis is required for Google connectors. Install it: npm install googleapis');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return _google;
|
|
20
|
+
}
|
|
21
|
+
// ── Public helpers ─────────────────────────────────────────────────
|
|
22
|
+
/**
|
|
23
|
+
* Create a Google OAuth2 client from app credentials.
|
|
24
|
+
*/
|
|
25
|
+
export async function createOAuth2Client(config) {
|
|
26
|
+
const google = await getGoogle();
|
|
27
|
+
return new google.auth.OAuth2(config.clientId, config.clientSecret, config.redirectUri);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Generate the consent screen URL for the given scopes.
|
|
31
|
+
*/
|
|
32
|
+
export function getAuthUrl(client, scopes) {
|
|
33
|
+
return client.generateAuthUrl({
|
|
34
|
+
access_type: 'offline',
|
|
35
|
+
prompt: 'consent',
|
|
36
|
+
scope: scopes,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Exchange an authorization code for tokens.
|
|
41
|
+
*/
|
|
42
|
+
export async function exchangeCode(client, code) {
|
|
43
|
+
const { tokens } = await client.getToken(code);
|
|
44
|
+
return tokens;
|
|
45
|
+
}
|
|
46
|
+
// ── Service account auth ──────────────────────────────────────────
|
|
47
|
+
/**
|
|
48
|
+
* Create an authenticated client using a service account with
|
|
49
|
+
* domain-wide delegation (impersonation). No browser flow needed.
|
|
50
|
+
*/
|
|
51
|
+
export async function createServiceAccountClient(config, scopes) {
|
|
52
|
+
const google = await getGoogle();
|
|
53
|
+
const auth = new google.auth.GoogleAuth({
|
|
54
|
+
...(config.keyFile ? { keyFile: config.keyFile } : {}),
|
|
55
|
+
...(config.credentials ? { credentials: config.credentials } : {}),
|
|
56
|
+
scopes,
|
|
57
|
+
clientOptions: { subject: config.subject },
|
|
58
|
+
});
|
|
59
|
+
return auth.getClient();
|
|
60
|
+
}
|
|
61
|
+
// ── Token persistence ──────────────────────────────────────────────
|
|
62
|
+
/**
|
|
63
|
+
* Load persisted tokens via a generic getter callback.
|
|
64
|
+
*
|
|
65
|
+
* @param getter Reads a string value by key (e.g. SecretStore.get)
|
|
66
|
+
* @param accountKey Unique key prefix for this account
|
|
67
|
+
*/
|
|
68
|
+
export async function loadTokens(getter, accountKey) {
|
|
69
|
+
const raw = await getter(`google_tokens:${accountKey}`);
|
|
70
|
+
if (!raw)
|
|
71
|
+
return null;
|
|
72
|
+
try {
|
|
73
|
+
return JSON.parse(raw);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Persist tokens via a generic setter callback.
|
|
81
|
+
*
|
|
82
|
+
* @param setter Writes a string value by key (e.g. SecretStore.set)
|
|
83
|
+
* @param accountKey Unique key prefix for this account
|
|
84
|
+
* @param tokens Tokens to persist
|
|
85
|
+
*/
|
|
86
|
+
export async function saveTokens(setter, accountKey, tokens) {
|
|
87
|
+
await setter(`google_tokens:${accountKey}`, JSON.stringify(tokens));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Refresh the access token if it has expired (or is about to within 60 s).
|
|
91
|
+
*
|
|
92
|
+
* If the token was refreshed and a `saver` callback is provided, the new
|
|
93
|
+
* tokens are persisted automatically.
|
|
94
|
+
*/
|
|
95
|
+
export async function refreshIfNeeded(client, tokens, saver) {
|
|
96
|
+
const buffer = 60_000; // 60 seconds
|
|
97
|
+
const isExpired = tokens.expiry_date != null && Date.now() >= tokens.expiry_date - buffer;
|
|
98
|
+
if (!isExpired)
|
|
99
|
+
return tokens;
|
|
100
|
+
client.setCredentials(tokens);
|
|
101
|
+
const { credentials } = await client.refreshAccessToken();
|
|
102
|
+
const refreshed = {
|
|
103
|
+
access_token: credentials.access_token,
|
|
104
|
+
refresh_token: credentials.refresh_token ?? tokens.refresh_token,
|
|
105
|
+
expiry_date: credentials.expiry_date ?? undefined,
|
|
106
|
+
token_type: credentials.token_type ?? 'Bearer',
|
|
107
|
+
};
|
|
108
|
+
if (saver) {
|
|
109
|
+
await saver(refreshed);
|
|
110
|
+
}
|
|
111
|
+
return refreshed;
|
|
112
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** Google connector types. */
|
|
2
|
+
import type { ConnectorConfig } from '../../shared/types/connector.js';
|
|
3
|
+
export interface GoogleOAuthConfig {
|
|
4
|
+
clientId: string;
|
|
5
|
+
clientSecret: string;
|
|
6
|
+
redirectUri: string;
|
|
7
|
+
}
|
|
8
|
+
export interface GoogleTokens {
|
|
9
|
+
access_token: string;
|
|
10
|
+
refresh_token?: string;
|
|
11
|
+
expiry_date?: number;
|
|
12
|
+
token_type: string;
|
|
13
|
+
}
|
|
14
|
+
export interface GoogleServiceAccountConfig {
|
|
15
|
+
/** Path to service account key JSON file */
|
|
16
|
+
keyFile?: string;
|
|
17
|
+
/** Inline service account credentials (alternative to keyFile) */
|
|
18
|
+
credentials?: {
|
|
19
|
+
client_email: string;
|
|
20
|
+
private_key: string;
|
|
21
|
+
project_id?: string;
|
|
22
|
+
};
|
|
23
|
+
/** Email of the user to impersonate via domain-wide delegation */
|
|
24
|
+
subject: string;
|
|
25
|
+
}
|
|
26
|
+
export interface GoogleConnectorConfig extends ConnectorConfig {
|
|
27
|
+
/** Google account email */
|
|
28
|
+
account: string;
|
|
29
|
+
/** OAuth2 user auth (requires browser flow) */
|
|
30
|
+
oauth?: GoogleOAuthConfig;
|
|
31
|
+
/** Service account auth (headless, for cloud deployments) */
|
|
32
|
+
serviceAccount?: GoogleServiceAccountConfig;
|
|
33
|
+
scopes?: string[];
|
|
34
|
+
}
|
|
35
|
+
export interface EmailAddress {
|
|
36
|
+
name?: string;
|
|
37
|
+
email: string;
|
|
38
|
+
}
|
|
39
|
+
export interface EmailRecord {
|
|
40
|
+
gmailId: string;
|
|
41
|
+
threadId: string;
|
|
42
|
+
account: string;
|
|
43
|
+
subject: string;
|
|
44
|
+
from: EmailAddress;
|
|
45
|
+
to: EmailAddress[];
|
|
46
|
+
cc: EmailAddress[];
|
|
47
|
+
bcc: EmailAddress[];
|
|
48
|
+
/** ISO 8601 timestamp */
|
|
49
|
+
date: string;
|
|
50
|
+
snippet: string;
|
|
51
|
+
body?: string;
|
|
52
|
+
labels: string[];
|
|
53
|
+
isRead: boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface CalendarAttendee {
|
|
56
|
+
email: string;
|
|
57
|
+
displayName?: string;
|
|
58
|
+
responseStatus?: string;
|
|
59
|
+
}
|
|
60
|
+
export interface CalendarEventRecord {
|
|
61
|
+
googleEventId: string;
|
|
62
|
+
calendarId: string;
|
|
63
|
+
account: string;
|
|
64
|
+
title: string;
|
|
65
|
+
description?: string;
|
|
66
|
+
location?: string;
|
|
67
|
+
/** ISO 8601 timestamp */
|
|
68
|
+
startAt: string;
|
|
69
|
+
/** ISO 8601 timestamp */
|
|
70
|
+
endAt: string;
|
|
71
|
+
allDay: boolean;
|
|
72
|
+
timezone?: string;
|
|
73
|
+
status: string;
|
|
74
|
+
organizerEmail: string;
|
|
75
|
+
attendees: CalendarAttendee[];
|
|
76
|
+
recurrence?: string[];
|
|
77
|
+
htmlLink?: string;
|
|
78
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-discovery for channel adapters.
|
|
3
|
+
* Scans @botinabox/* packages for botinabox.type === 'channel'.
|
|
4
|
+
* Story 4.1
|
|
5
|
+
*/
|
|
6
|
+
import type { ChannelAdapter } from "./types.js";
|
|
7
|
+
type Importer = (packageName: string) => Promise<unknown>;
|
|
8
|
+
/**
|
|
9
|
+
* Scans nodeModulesPath/@botinabox/{pkg}/package.json for each package in the scope.
|
|
10
|
+
* If pkg.botinabox?.type === 'channel', dynamically imports the package
|
|
11
|
+
* and returns the discovered ChannelAdapter instances.
|
|
12
|
+
*
|
|
13
|
+
* The optional importer parameter allows injection for testing.
|
|
14
|
+
*/
|
|
15
|
+
export declare function discoverChannels(nodeModulesPath: string, importer?: Importer): Promise<ChannelAdapter[]>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-discovery for channel adapters.
|
|
3
|
+
* Scans @botinabox/* packages for botinabox.type === 'channel'.
|
|
4
|
+
* Story 4.1
|
|
5
|
+
*/
|
|
6
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
/**
|
|
9
|
+
* Scans nodeModulesPath/@botinabox/{pkg}/package.json for each package in the scope.
|
|
10
|
+
* If pkg.botinabox?.type === 'channel', dynamically imports the package
|
|
11
|
+
* and returns the discovered ChannelAdapter instances.
|
|
12
|
+
*
|
|
13
|
+
* The optional importer parameter allows injection for testing.
|
|
14
|
+
*/
|
|
15
|
+
export async function discoverChannels(nodeModulesPath, importer = (name) => import(name)) {
|
|
16
|
+
const scopeDir = join(nodeModulesPath, "@botinabox");
|
|
17
|
+
let entries;
|
|
18
|
+
try {
|
|
19
|
+
entries = await readdir(scopeDir);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// No @botinabox scope directory — gracefully return empty
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
const adapters = [];
|
|
26
|
+
for (const entry of entries) {
|
|
27
|
+
const pkgPath = join(scopeDir, entry, "package.json");
|
|
28
|
+
let pkg;
|
|
29
|
+
try {
|
|
30
|
+
const raw = await readFile(pkgPath, "utf8");
|
|
31
|
+
pkg = JSON.parse(raw);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const botinabox = pkg.botinabox;
|
|
37
|
+
if (botinabox?.type !== "channel") {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const packageName = pkg.name;
|
|
41
|
+
if (!packageName) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const mod = await importer(packageName);
|
|
46
|
+
const adapter = "default" in mod && mod.default ? mod.default : mod;
|
|
47
|
+
adapters.push(adapter);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// Failed to import — skip silently
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return adapters;
|
|
54
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChannelRegistry — manages channel adapter lifecycle.
|
|
3
|
+
* Story 4.1
|
|
4
|
+
*/
|
|
5
|
+
import type { ChannelAdapter, HealthStatus } from "./types.js";
|
|
6
|
+
export declare class ChannelRegistryError extends Error {
|
|
7
|
+
constructor(message: string);
|
|
8
|
+
}
|
|
9
|
+
export declare class ChannelRegistry {
|
|
10
|
+
private readonly adapters;
|
|
11
|
+
private started;
|
|
12
|
+
/**
|
|
13
|
+
* Register a channel adapter.
|
|
14
|
+
* Throws if an adapter with the same id is already registered.
|
|
15
|
+
* If registry is already started, immediately connects the adapter.
|
|
16
|
+
*/
|
|
17
|
+
register(adapter: ChannelAdapter, config?: unknown): void;
|
|
18
|
+
/**
|
|
19
|
+
* Unregister a channel adapter.
|
|
20
|
+
* Disconnects the adapter if it exists.
|
|
21
|
+
*/
|
|
22
|
+
unregister(id: string): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Reconfigure an adapter: disconnect, update config, reconnect.
|
|
25
|
+
*/
|
|
26
|
+
reconfigure(id: string, config: unknown): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Run health checks on all registered adapters.
|
|
29
|
+
*/
|
|
30
|
+
healthCheck(): Promise<Record<string, HealthStatus>>;
|
|
31
|
+
/** Check if an adapter is registered. */
|
|
32
|
+
has(id: string): boolean;
|
|
33
|
+
/** Get an adapter by ID. */
|
|
34
|
+
get(id: string): ChannelAdapter | undefined;
|
|
35
|
+
/** List all registered adapters. */
|
|
36
|
+
list(): ChannelAdapter[];
|
|
37
|
+
/**
|
|
38
|
+
* Start: connect all registered adapters and mark registry as started.
|
|
39
|
+
*/
|
|
40
|
+
start(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Stop: disconnect all registered adapters.
|
|
43
|
+
*/
|
|
44
|
+
stop(): Promise<void>;
|
|
45
|
+
}
|