@almadar/integrations 2.39.0 → 2.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -61,6 +61,11 @@ declare function isKnownIntegration(name: string): boolean;
61
61
  */
62
62
  declare function getRegisteredIntegrations(): string[];
63
63
 
64
+ /**
65
+ * Run `fn` as one tenant of a multi-tenant host: every credential reference resolved inside it
66
+ * (across awaits) comes from `store` alone, never from the host's env or installed store.
67
+ */
68
+ declare function runWithCredentialScope<T>(store: CredentialStore, fn: () => T): T;
64
69
  /**
65
70
  * The factory the host's `RuntimeIntegrationManager` configures — installed
66
71
  * by its constructor so the `credentials.test` probe executes through the
@@ -79,7 +84,8 @@ declare function getInstalledCredentialStore(): CredentialStore | null;
79
84
  /** Testing/reset hook. */
80
85
  declare function uninstallCredentialStore(): void;
81
86
  /**
82
- * The one credential-reference resolver: store → env → undefined.
87
+ * The one credential-reference resolver: store → env → undefined; inside a tenant's
88
+ * credential scope, that tenant's store only.
83
89
  * `ref` is an env-var name — the same name `serviceCredentials` declares and
84
90
  * `connectionRef` params carry. Every place that used to read
85
91
  * `process.env[ref]` for a credential routes through here.
@@ -1079,4 +1085,4 @@ declare class RiggerIntegration extends BaseIntegration {
1079
1085
  private generate;
1080
1086
  }
1081
1087
 
1082
- export { AccountingIntegration, type AlmadarCheckoutSession, type AlmadarCustomer, type AlmadarInvoiceFailure, type AlmadarInvoicePayment, type AlmadarPortalSession, type AlmadarStripeEvent, type AlmadarStripeEventOk, type AlmadarSubscription, type AlmadarSubscriptionStatus, type AlmadarTier, ArxivIntegration, type ArxivResult, BankingIntegration, BaseIntegration, CLIIntegration, CREDENTIALS_FILE_ENV, CalendarIntegration, type CancelSubscriptionInput, ConsoleLogger, type CreateCheckoutInput, type CreateCustomerInput, type CreatePortalInput, CredentialPersistence, CredentialStore, CredentialsIntegration, DatabaseIntegration, DeepAgentIntegration, DockerIntegration, DriveIntegration, EmailIntegration, EsignIntegration, FileCredentialPersistence, HookDeclaration, type HookProviderInput, type HookProviderResult, IconifyIntegration, InMemoryPendingGrantStore, IntegrationCallContext, IntegrationConfig, type IntegrationConstructor, IntegrationErrorCode, IntegrationFactory, IntegrationLogger, IntegrationParams, IntegrationResult, LLMIntegration, MLIntegration, MetaAdsIntegration, type OAuthAuthorizeInput, OAuthGrantNotFoundError, OAuthIntegration, type OAuthProvider, OAuthSubjectMismatchError, type OAuthTokenInput, type OAuthTokenResult, OtelIntegration, type PendingAuthorization, type PendingGrantStore, PushIntegration, QueueIntegration, RedisIntegration, type RegisteredHookProvider, type RetryConfig, RiggerIntegration, type SqlGuardResult, StorageIntegration, StripeIntegration, type StripePriceMap, TwilioIntegration, type UpdateSubscriptionInput, type VerifyAndParseInput, WebhookIntegration, WikimediaIntegration, YouTubeIntegration, allHookDeclarations, assertReadOnlySelect, getActiveFactory, getInstalledCredentialStore, getIntegration, getRegisteredIntegrations, googleCalendarHookProvider, installActiveFactory, installCredentialStore, installPendingGrantStore, isKnownIntegration, parseCalendarPushNotification, registerIntegration, registeredHookProviders, resolveCredentialRef, uninstallCredentialStore, verifyAndParseStripeEvent, withRetry };
1088
+ export { AccountingIntegration, type AlmadarCheckoutSession, type AlmadarCustomer, type AlmadarInvoiceFailure, type AlmadarInvoicePayment, type AlmadarPortalSession, type AlmadarStripeEvent, type AlmadarStripeEventOk, type AlmadarSubscription, type AlmadarSubscriptionStatus, type AlmadarTier, ArxivIntegration, type ArxivResult, BankingIntegration, BaseIntegration, CLIIntegration, CREDENTIALS_FILE_ENV, CalendarIntegration, type CancelSubscriptionInput, ConsoleLogger, type CreateCheckoutInput, type CreateCustomerInput, type CreatePortalInput, CredentialPersistence, CredentialStore, CredentialsIntegration, DatabaseIntegration, DeepAgentIntegration, DockerIntegration, DriveIntegration, EmailIntegration, EsignIntegration, FileCredentialPersistence, HookDeclaration, type HookProviderInput, type HookProviderResult, IconifyIntegration, InMemoryPendingGrantStore, IntegrationCallContext, IntegrationConfig, type IntegrationConstructor, IntegrationErrorCode, IntegrationFactory, IntegrationLogger, IntegrationParams, IntegrationResult, LLMIntegration, MLIntegration, MetaAdsIntegration, type OAuthAuthorizeInput, OAuthGrantNotFoundError, OAuthIntegration, type OAuthProvider, OAuthSubjectMismatchError, type OAuthTokenInput, type OAuthTokenResult, OtelIntegration, type PendingAuthorization, type PendingGrantStore, PushIntegration, QueueIntegration, RedisIntegration, type RegisteredHookProvider, type RetryConfig, RiggerIntegration, type SqlGuardResult, StorageIntegration, StripeIntegration, type StripePriceMap, TwilioIntegration, type UpdateSubscriptionInput, type VerifyAndParseInput, WebhookIntegration, WikimediaIntegration, YouTubeIntegration, allHookDeclarations, assertReadOnlySelect, getActiveFactory, getInstalledCredentialStore, getIntegration, getRegisteredIntegrations, googleCalendarHookProvider, installActiveFactory, installCredentialStore, installPendingGrantStore, isKnownIntegration, parseCalendarPushNotification, registerIntegration, registeredHookProviders, resolveCredentialRef, runWithCredentialScope, uninstallCredentialStore, verifyAndParseStripeEvent, withRetry };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createLogger } from '@almadar/logger';
2
2
  import { integratorsRegistry } from '@almadar/core/patterns';
3
3
  import { randomBytes, createCipheriv, createDecipheriv, createHmac, createHash } from 'crypto';
4
+ import { AsyncLocalStorage } from 'async_hooks';
4
5
  import { readFileSync, mkdirSync, writeFileSync, renameSync, mkdtempSync, rmSync, promises } from 'fs';
5
6
  import { join, dirname } from 'path';
6
7
  import Stripe from 'stripe';
@@ -412,8 +413,7 @@ function isKnownIntegration(name) {
412
413
  function getRegisteredIntegrations() {
413
414
  return Object.keys(INTEGRATION_REGISTRY);
414
415
  }
415
-
416
- // src/factory.ts
416
+ var callLog = createLogger("almadar:integrations:calls");
417
417
  function instanceKey(name, principal) {
418
418
  return principal ? `${name}\0${principal}` : name;
419
419
  }
@@ -458,8 +458,16 @@ var IntegrationFactory = class {
458
458
  * Execute an action on an integration
459
459
  */
460
460
  async execute(integration, action, params, context) {
461
- const instance = this.get(integration, context?.principal);
462
- return await instance.execute(action, params, context);
461
+ const start = performance.now();
462
+ const record = (ok, error) => callLog.call({ kind: "external", service: integration, op: action, durationMs: performance.now() - start, ok, ...error !== void 0 ? { error } : {} });
463
+ try {
464
+ const result = await this.get(integration, context?.principal).execute(action, params, context);
465
+ record(result.success, result.success ? void 0 : result.error?.message);
466
+ return result;
467
+ } catch (err) {
468
+ record(false, err instanceof Error ? err.message : String(err));
469
+ throw err;
470
+ }
463
471
  }
464
472
  /**
465
473
  * Check if integration is configured
@@ -736,9 +744,11 @@ var CredentialStore = class {
736
744
  return true;
737
745
  }
738
746
  };
739
-
740
- // src/credentials/resolver.ts
741
747
  var installedStore = null;
748
+ var tenantScope = new AsyncLocalStorage();
749
+ function runWithCredentialScope(store, fn) {
750
+ return tenantScope.run(store, fn);
751
+ }
742
752
  var activeFactory = null;
743
753
  function installActiveFactory(factory) {
744
754
  activeFactory = factory;
@@ -756,6 +766,8 @@ function uninstallCredentialStore() {
756
766
  installedStore = null;
757
767
  }
758
768
  function resolveCredentialRef(ref, env = process.env) {
769
+ const tenant = tenantScope.getStore();
770
+ if (tenant) return tenant.resolve(ref);
759
771
  return installedStore?.resolve(ref) ?? env[ref];
760
772
  }
761
773
  var CREDENTIALS_FILE_ENV = "ALMADAR_CREDENTIALS_FILE";
@@ -5825,6 +5837,6 @@ var RiggerIntegration = class extends BaseIntegration {
5825
5837
  };
5826
5838
  registerIntegration("rigger", RiggerIntegration);
5827
5839
 
5828
- export { AccountingIntegration, ArxivIntegration, BankingIntegration, BaseIntegration, CLIIntegration, CREDENTIALS_FILE_ENV, CREDENTIAL_ENTITY_TYPE, CREDENTIAL_MASTER_KEY_ENV, CalendarIntegration, ConsoleLogger, CredentialStore, CredentialsIntegration, DatabaseIntegration, DeepAgentIntegration, DockerIntegration, DriveIntegration, EmailIntegration, EsignIntegration, FileCredentialPersistence, GitHubIntegration, IconifyIntegration, InMemoryPendingGrantStore, IntegrationError, IntegrationFactory, LLMIntegration, MLIntegration, MetaAdsIntegration, OAuthGrantNotFoundError, OAuthIntegration, OAuthSubjectMismatchError, OtelIntegration, PushIntegration, QueueIntegration, RedisIntegration, RiggerIntegration, StorageIntegration, StripeIntegration, TwilioIntegration, UnknownActionError, WebhookIntegration, WikimediaIntegration, YouTubeIntegration, allCredentialEnvVars, allHookDeclarations, assertReadOnlySelect, declaredCredentialEnvVars, getActiveFactory, getInstalledCredentialStore, getIntegration, getIntegrationFactory, getRegisteredIntegrations, googleCalendarHookProvider, installActiveFactory, installCredentialStore, installPendingGrantStore, isKnownIntegration, parseCalendarPushNotification, registerIntegration, registeredHookProviders, resetIntegrationFactory, resolveCredentialRef, serviceHooks, uninstallCredentialStore, validateParams, verifyAndParseStripeEvent, withRetry };
5840
+ export { AccountingIntegration, ArxivIntegration, BankingIntegration, BaseIntegration, CLIIntegration, CREDENTIALS_FILE_ENV, CREDENTIAL_ENTITY_TYPE, CREDENTIAL_MASTER_KEY_ENV, CalendarIntegration, ConsoleLogger, CredentialStore, CredentialsIntegration, DatabaseIntegration, DeepAgentIntegration, DockerIntegration, DriveIntegration, EmailIntegration, EsignIntegration, FileCredentialPersistence, GitHubIntegration, IconifyIntegration, InMemoryPendingGrantStore, IntegrationError, IntegrationFactory, LLMIntegration, MLIntegration, MetaAdsIntegration, OAuthGrantNotFoundError, OAuthIntegration, OAuthSubjectMismatchError, OtelIntegration, PushIntegration, QueueIntegration, RedisIntegration, RiggerIntegration, StorageIntegration, StripeIntegration, TwilioIntegration, UnknownActionError, WebhookIntegration, WikimediaIntegration, YouTubeIntegration, allCredentialEnvVars, allHookDeclarations, assertReadOnlySelect, declaredCredentialEnvVars, getActiveFactory, getInstalledCredentialStore, getIntegration, getIntegrationFactory, getRegisteredIntegrations, googleCalendarHookProvider, installActiveFactory, installCredentialStore, installPendingGrantStore, isKnownIntegration, parseCalendarPushNotification, registerIntegration, registeredHookProviders, resetIntegrationFactory, resolveCredentialRef, runWithCredentialScope, serviceHooks, uninstallCredentialStore, validateParams, verifyAndParseStripeEvent, withRetry };
5829
5841
  //# sourceMappingURL=index.js.map
5830
5842
  //# sourceMappingURL=index.js.map