@db3.ai/app 0.1.0-beta.1 → 0.1.0-beta.4
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 +68 -9
- package/agent-instructions.md +76 -0
- package/dist/ai/AIErrors.d.ts +7 -1
- package/dist/ai/AIErrors.js +8 -1
- package/dist/ai/AIFailover.d.ts +10 -0
- package/dist/ai/AIFailover.js +3 -2
- package/dist/ai/AIProviderAdmission.d.ts +101 -0
- package/dist/ai/AIProviderAdmission.js +336 -0
- package/dist/ai/AIRateLimiter.d.ts +11 -2
- package/dist/ai/AIRateLimiter.js +125 -10
- package/dist/ai/AIRequestTrackingError.d.ts +22 -0
- package/dist/ai/AIRequestTrackingError.js +33 -0
- package/dist/ai/Agent.d.ts +7 -7
- package/dist/ai/Agent.js +84 -56
- package/dist/ai/Ai.d.ts +11 -3
- package/dist/ai/Ai.js +94 -44
- package/dist/ai/OpenAIProviderError.js +1 -1
- package/dist/ai/ProviderSseObserver.d.ts +19 -0
- package/dist/ai/ProviderSseObserver.js +98 -0
- package/dist/ai/aiServiceTier.d.ts +5 -0
- package/dist/ai/aiServiceTier.js +14 -0
- package/dist/ai/chunkEmbeddingText.d.ts +14 -0
- package/dist/ai/chunkEmbeddingText.js +147 -0
- package/dist/ai/contracts/AI.d.ts +16 -0
- package/dist/ai/contracts/AIRequestErrorDiagnostics.d.ts +12 -0
- package/dist/ai/contracts/AIRequestErrorDiagnostics.js +1 -0
- package/dist/ai/contracts/AIServiceTier.d.ts +2 -0
- package/dist/ai/contracts/AIServiceTier.js +1 -0
- package/dist/ai/contracts/Agent.d.ts +3 -0
- package/dist/ai/contracts/EmbeddingTextChunk.d.ts +25 -0
- package/dist/ai/contracts/EmbeddingTextChunk.js +1 -0
- package/dist/ai/contracts/Pricing.d.ts +7 -1
- package/dist/ai/contracts/Providers.d.ts +2 -0
- package/dist/ai/contracts/index.d.ts +2 -0
- package/dist/ai/contracts/index.js +1 -0
- package/dist/ai/index.d.ts +8 -2
- package/dist/ai/index.js +5 -2
- package/dist/ai/{Models.d.ts → modelPricing.d.ts} +2 -2
- package/dist/ai/{Models.js → modelPricing.js} +38 -4
- package/dist/apps/AppInstallation.d.ts +21 -0
- package/dist/apps/AppInstallation.js +16 -0
- package/dist/apps/AppRouteError.d.ts +6 -0
- package/dist/apps/AppRouteError.js +10 -0
- package/dist/apps/AppService.d.ts +24 -0
- package/dist/apps/AppService.js +21 -0
- package/dist/apps/Apps.d.ts +59 -0
- package/dist/apps/Apps.js +566 -0
- package/dist/apps/appsPlugin.d.ts +3 -0
- package/dist/apps/appsPlugin.js +36 -0
- package/dist/apps/commands/disableApp.d.ts +2 -0
- package/dist/apps/commands/disableApp.js +5 -0
- package/dist/apps/commands/enableApp.d.ts +2 -0
- package/dist/apps/commands/enableApp.js +5 -0
- package/dist/apps/commands/index.d.ts +10 -0
- package/dist/apps/commands/index.js +27 -0
- package/dist/apps/commands/installApp.d.ts +2 -0
- package/dist/apps/commands/installApp.js +5 -0
- package/dist/apps/commands/listApps.d.ts +2 -0
- package/dist/apps/commands/listApps.js +5 -0
- package/dist/apps/commands/makeAppMigration.d.ts +2 -0
- package/dist/apps/commands/makeAppMigration.js +9 -0
- package/dist/apps/commands/rollbackApp.d.ts +2 -0
- package/dist/apps/commands/rollbackApp.js +5 -0
- package/dist/apps/commands/uninstallApp.d.ts +2 -0
- package/dist/apps/commands/uninstallApp.js +5 -0
- package/dist/apps/contracts/AppAction.d.ts +2 -0
- package/dist/apps/contracts/AppAction.js +1 -0
- package/dist/apps/contracts/AppDefinition.d.ts +45 -0
- package/dist/apps/contracts/AppDefinition.js +1 -0
- package/dist/apps/contracts/AppDescription.d.ts +32 -0
- package/dist/apps/contracts/AppDescription.js +1 -0
- package/dist/apps/contracts/AppManifest.d.ts +30 -0
- package/dist/apps/contracts/AppManifest.js +1 -0
- package/dist/apps/contracts/AppNavigation.d.ts +9 -0
- package/dist/apps/contracts/AppNavigation.js +1 -0
- package/dist/apps/contracts/AppNavigationBadge.d.ts +6 -0
- package/dist/apps/contracts/AppNavigationBadge.js +1 -0
- package/dist/apps/contracts/AppNavigationContext.d.ts +8 -0
- package/dist/apps/contracts/AppNavigationContext.js +1 -0
- package/dist/apps/contracts/AppNavigationResult.d.ts +12 -0
- package/dist/apps/contracts/AppNavigationResult.js +1 -0
- package/dist/apps/contracts/AppRoute.d.ts +19 -0
- package/dist/apps/contracts/AppRoute.js +1 -0
- package/dist/apps/contracts/DiscoveredApp.d.ts +10 -0
- package/dist/apps/contracts/DiscoveredApp.js +1 -0
- package/dist/apps/contracts/InstalledApps.d.ts +3 -0
- package/dist/apps/contracts/InstalledApps.js +1 -0
- package/dist/apps/contracts/index.d.ts +11 -0
- package/dist/apps/contracts/index.js +1 -0
- package/dist/apps/defineApp.d.ts +7 -0
- package/dist/apps/defineApp.js +8 -0
- package/dist/apps/discoverApps.d.ts +3 -0
- package/dist/apps/discoverApps.js +62 -0
- package/dist/apps/fastify.d.ts +1 -0
- package/dist/apps/fastify.js +1 -0
- package/dist/apps/index.d.ts +7 -0
- package/dist/apps/index.js +7 -0
- package/dist/apps/normalizeAppNavigation.d.ts +3 -0
- package/dist/apps/normalizeAppNavigation.js +30 -0
- package/dist/apps/registerAppRoutes.d.ts +6 -0
- package/dist/apps/registerAppRoutes.js +52 -0
- package/dist/apps/validateAppDefinitions.d.ts +3 -0
- package/dist/apps/validateAppDefinitions.js +65 -0
- package/dist/apps/vite.d.ts +1 -0
- package/dist/apps/vite.js +1 -0
- package/dist/apps/writeAppTypes.d.ts +2 -0
- package/dist/apps/writeAppTypes.js +13 -0
- package/dist/auth/Auth.d.ts +18 -0
- package/dist/auth/Auth.js +89 -13
- package/dist/auth/PasswordLoginAttempt.d.ts +50 -0
- package/dist/auth/PasswordLoginAttempt.js +60 -0
- package/dist/auth/PasswordSuspendedError.d.ts +10 -0
- package/dist/auth/PasswordSuspendedError.js +13 -0
- package/dist/auth/contracts/PasswordAuthProviderOptions.d.ts +26 -0
- package/dist/auth/contracts/PasswordAuthProviderOptions.js +1 -0
- package/dist/auth/contracts/PasswordSuspension.d.ts +7 -0
- package/dist/auth/contracts/PasswordSuspension.js +1 -0
- package/dist/auth/contracts/PasswordSuspensionOptions.d.ts +10 -0
- package/dist/auth/contracts/PasswordSuspensionOptions.js +1 -0
- package/dist/auth/contracts/index.d.ts +3 -0
- package/dist/auth/contracts/index.js +3 -0
- package/dist/auth/index.d.ts +2 -0
- package/dist/auth/index.js +2 -0
- package/dist/auth/providers/PasswordAuthProvider.d.ts +19 -23
- package/dist/auth/providers/PasswordAuthProvider.js +96 -8
- package/dist/auth/providers/resolveAuthProviders.js +1 -0
- package/dist/cli/Cli.d.ts +2 -0
- package/dist/cli/Cli.js +8 -0
- package/dist/cli/commands/repl.js +2 -1
- package/dist/cli/contracts/CliCommand.d.ts +2 -0
- package/dist/cli/contracts/CliCommandInfo.d.ts +12 -0
- package/dist/cli/contracts/CliCommandInfo.js +1 -0
- package/dist/cli/contracts/index.d.ts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +1 -0
- package/dist/cli/listDb3Commands.d.ts +8 -0
- package/dist/cli/listDb3Commands.js +10 -0
- package/dist/cli/runDb3.d.ts +3 -6
- package/dist/cli/runDb3.js +5 -59
- package/dist/cli/withDb3Cli.d.ts +4 -0
- package/dist/cli/withDb3Cli.js +60 -0
- package/dist/db/ActiveQueryBuilder.d.ts +8 -0
- package/dist/db/ActiveQueryBuilder.js +11 -0
- package/dist/db/ActiveRecord.d.ts +6 -0
- package/dist/db/ActiveRecord.js +8 -0
- package/dist/db/connection.js +14 -2
- package/dist/db/fields/TimestampField.d.ts +1 -1
- package/dist/db/fields/TimestampField.js +7 -12
- package/dist/db/migrations/DatabaseMigrationManager.d.ts +3 -1
- package/dist/db/migrations/DatabaseMigrationManager.js +9 -3
- package/dist/db/migrations/contracts/DatabaseMigration.d.ts +7 -0
- package/dist/db/migrations/index.d.ts +1 -1
- package/dist/db/migrations/index.js +1 -1
- package/dist/db/migrations/{KnexMigrationRenderer.d.ts → renderKnexMigration.d.ts} +4 -3
- package/dist/db/migrations/{KnexMigrationRenderer.js → renderKnexMigration.js} +7 -6
- package/dist/flows/FlowCompiler.js +1 -1
- package/dist/flows/Flows.js +1 -1
- package/dist/flows/index.d.ts +1 -1
- package/dist/flows/index.js +1 -1
- package/dist/health/Health.d.ts +41 -0
- package/dist/health/Health.js +153 -0
- package/dist/health/contracts/Health.d.ts +36 -0
- package/dist/health/contracts/Health.js +1 -0
- package/dist/health/contracts/HealthHttp.d.ts +15 -0
- package/dist/health/contracts/HealthHttp.js +1 -0
- package/dist/health/contracts/index.d.ts +2 -0
- package/dist/health/contracts/index.js +1 -0
- package/dist/health/databaseHealthCheck.d.ts +15 -0
- package/dist/health/databaseHealthCheck.js +18 -0
- package/dist/health/fastify.d.ts +13 -0
- package/dist/health/fastify.js +44 -0
- package/dist/health/index.d.ts +3 -0
- package/dist/health/index.js +2 -0
- package/dist/in-app/InApp.d.ts +33 -0
- package/dist/in-app/InApp.js +191 -0
- package/dist/in-app/InAppError.d.ts +6 -0
- package/dist/in-app/InAppError.js +10 -0
- package/dist/in-app/InAppRecord.d.ts +36 -0
- package/dist/in-app/InAppRecord.js +24 -0
- package/dist/in-app/contracts/InAppInbox.d.ts +45 -0
- package/dist/in-app/contracts/InAppInbox.js +1 -0
- package/dist/in-app/contracts/InAppMessage.d.ts +22 -0
- package/dist/in-app/contracts/InAppMessage.js +1 -0
- package/dist/in-app/contracts/InAppScope.d.ts +17 -0
- package/dist/in-app/contracts/InAppScope.js +1 -0
- package/dist/in-app/contracts/index.d.ts +3 -0
- package/dist/in-app/contracts/index.js +3 -0
- package/dist/in-app/index.d.ts +4 -0
- package/dist/in-app/index.js +4 -0
- package/dist/in-app/validation.d.ts +21 -0
- package/dist/in-app/validation.js +50 -0
- package/dist/index.d.ts +2 -14
- package/dist/index.js +2 -5
- package/dist/logging/DevelopmentConsole.d.ts +42 -0
- package/dist/logging/DevelopmentConsole.js +162 -0
- package/dist/logging/Log.d.ts +3 -1
- package/dist/logging/Log.js +5 -2
- package/dist/logging/PrettyLogFormatter.d.ts +16 -0
- package/dist/logging/PrettyLogFormatter.js +151 -0
- package/dist/logging/acquireDevelopmentConsole.d.ts +6 -0
- package/dist/logging/acquireDevelopmentConsole.js +21 -0
- package/dist/logging/contracts/DevelopmentConsoleOptions.d.ts +16 -0
- package/dist/logging/contracts/DevelopmentConsoleOptions.js +1 -0
- package/dist/logging/contracts/EmailLogOptions.d.ts +8 -0
- package/dist/logging/contracts/EmailLogOptions.js +1 -0
- package/dist/logging/contracts/HttpExchangeMonitorOptions.d.ts +12 -0
- package/dist/logging/contracts/HttpExchangeMonitorOptions.js +1 -0
- package/dist/logging/contracts/LogTransportOptions.d.ts +19 -0
- package/dist/logging/contracts/LogTransportOptions.js +1 -0
- package/dist/logging/contracts/LoggingOptions.d.ts +12 -1
- package/dist/logging/contracts/index.d.ts +4 -0
- package/dist/logging/contracts/index.js +4 -0
- package/dist/logging/drivers/PinoLoggerDriver.d.ts +3 -1
- package/dist/logging/drivers/PinoLoggerDriver.js +75 -16
- package/dist/logging/formatLogEmail.d.ts +4 -0
- package/dist/logging/formatLogEmail.js +21 -0
- package/dist/logging/highlightQueueMessage.d.ts +11 -0
- package/dist/logging/highlightQueueMessage.js +36 -0
- package/dist/logging/httpBodyCapture.d.ts +24 -0
- package/dist/logging/httpBodyCapture.js +321 -0
- package/dist/logging/index.d.ts +2 -0
- package/dist/logging/index.js +2 -0
- package/dist/logging/redactLogEmail.d.ts +2 -0
- package/dist/logging/redactLogEmail.js +12 -0
- package/dist/logging/registerHttpExchangeMonitor.d.ts +10 -0
- package/dist/logging/registerHttpExchangeMonitor.js +43 -0
- package/dist/logging/transports/EmailLogTransport.d.ts +18 -0
- package/dist/logging/transports/EmailLogTransport.js +47 -0
- package/dist/mail/Mail.d.ts +2 -0
- package/dist/mail/Mail.js +3 -0
- package/dist/mail/MailDeliveryError.d.ts +9 -0
- package/dist/mail/MailDeliveryError.js +16 -0
- package/dist/mail/index.d.ts +1 -0
- package/dist/mail/index.js +1 -0
- package/dist/mail/transports/MailgunTransport.js +2 -0
- package/dist/mail/transports/ResendTransport.d.ts +3 -2
- package/dist/mail/transports/ResendTransport.js +22 -14
- package/dist/media/MediaManager.d.ts +4 -2
- package/dist/media/MediaManager.js +40 -8
- package/dist/network/BlockedUrlError.d.ts +16 -0
- package/dist/network/BlockedUrlError.js +19 -0
- package/dist/network/assertPublicUrl.d.ts +22 -0
- package/dist/network/assertPublicUrl.js +67 -0
- package/dist/network/contracts/GuardedFetch.d.ts +35 -0
- package/dist/network/contracts/GuardedFetch.js +1 -0
- package/dist/network/contracts/PublicUrlOptions.d.ts +17 -0
- package/dist/network/contracts/PublicUrlOptions.js +1 -0
- package/dist/network/contracts/index.d.ts +2 -0
- package/dist/network/contracts/index.js +2 -0
- package/dist/network/guardedFetch.d.ts +29 -0
- package/dist/network/guardedFetch.js +133 -0
- package/dist/network/index.d.ts +6 -0
- package/dist/network/index.js +6 -0
- package/dist/network/playwright/GuardedBrowserProxy.d.ts +32 -0
- package/dist/network/playwright/GuardedBrowserProxy.js +172 -0
- package/dist/network/playwright/contracts/GuardedBrowserContextOptions.d.ts +37 -0
- package/dist/network/playwright/contracts/GuardedBrowserContextOptions.js +1 -0
- package/dist/network/playwright/contracts/index.d.ts +1 -0
- package/dist/network/playwright/contracts/index.js +1 -0
- package/dist/network/playwright/index.d.ts +2 -0
- package/dist/network/playwright/index.js +2 -0
- package/dist/network/playwright/newGuardedBrowserContext.d.ts +21 -0
- package/dist/network/playwright/newGuardedBrowserContext.js +86 -0
- package/dist/network/privateNetworkAddresses.d.ts +44 -0
- package/dist/network/privateNetworkAddresses.js +157 -0
- package/dist/network/publicAddressLookup.d.ts +26 -0
- package/dist/network/publicAddressLookup.js +42 -0
- package/dist/notifications/Notifications.d.ts +27 -0
- package/dist/notifications/Notifications.js +99 -0
- package/dist/notifications/contracts/Notification.d.ts +56 -0
- package/dist/notifications/contracts/Notification.js +1 -0
- package/dist/notifications/contracts/NotificationDelivery.d.ts +11 -0
- package/dist/notifications/contracts/NotificationDelivery.js +1 -0
- package/dist/notifications/contracts/index.d.ts +2 -0
- package/dist/notifications/contracts/index.js +2 -0
- package/dist/notifications/index.d.ts +2 -0
- package/dist/notifications/index.js +2 -0
- package/dist/queue/JobRunAttempt.d.ts +16 -0
- package/dist/queue/JobRunAttempt.js +17 -0
- package/dist/queue/JobRunEvent.d.ts +15 -0
- package/dist/queue/JobRunEvent.js +16 -0
- package/dist/queue/JobRunStoppedError.d.ts +6 -0
- package/dist/queue/JobRunStoppedError.js +10 -0
- package/dist/queue/Queue.d.ts +19 -4
- package/dist/queue/Queue.js +122 -9
- package/dist/queue/QueueTerminalError.d.ts +9 -0
- package/dist/queue/QueueTerminalError.js +12 -0
- package/dist/queue/QueueWorker.d.ts +12 -5
- package/dist/queue/QueueWorker.js +94 -9
- package/dist/queue/QueueWorkerReporter.d.ts +17 -0
- package/dist/queue/QueueWorkerReporter.js +71 -0
- package/dist/queue/QueueableJob.d.ts +2 -2
- package/dist/queue/RunnableJob.d.ts +78 -0
- package/dist/queue/RunnableJob.js +284 -0
- package/dist/queue/console.d.ts +8 -0
- package/dist/queue/console.js +53 -19
- package/dist/queue/contracts/QueueDriver.d.ts +8 -0
- package/dist/queue/contracts/QueueJobRunner.d.ts +4 -0
- package/dist/queue/contracts/QueueJobRunner.js +1 -0
- package/dist/queue/contracts/QueuePayload.d.ts +1 -1
- package/dist/queue/contracts/QueueSelection.d.ts +11 -0
- package/dist/queue/contracts/QueueSelection.js +1 -0
- package/dist/queue/contracts/QueueService.d.ts +18 -4
- package/dist/queue/contracts/QueueWorkerLifecycle.d.ts +2 -2
- package/dist/queue/contracts/QueueWorkerSnapshot.d.ts +14 -0
- package/dist/queue/contracts/QueueWorkerSnapshot.js +1 -0
- package/dist/queue/contracts/QueueableJob.d.ts +7 -3
- package/dist/queue/contracts/QueueableJobContract.d.ts +12 -0
- package/dist/queue/contracts/QueueableJobContract.js +1 -0
- package/dist/queue/contracts/index.d.ts +4 -0
- package/dist/queue/contracts/index.js +2 -0
- package/dist/queue/drivers/DatabaseQueueDriver.d.ts +4 -0
- package/dist/queue/drivers/DatabaseQueueDriver.js +27 -3
- package/dist/queue/drivers/RedisQueueDriver.d.ts +7 -0
- package/dist/queue/drivers/RedisQueueDriver.js +37 -0
- package/dist/queue/index.d.ts +5 -0
- package/dist/queue/index.js +5 -0
- package/dist/queue/jobEnvelopeHasPrefix.d.ts +3 -0
- package/dist/queue/jobEnvelopeHasPrefix.js +4 -0
- package/dist/scheduler/ScheduledEvent.d.ts +8 -4
- package/dist/scheduler/ScheduledEvent.js +17 -3
- package/dist/scheduler/ScheduledJob.d.ts +1 -1
- package/dist/scheduler/Scheduler.d.ts +5 -0
- package/dist/scheduler/Scheduler.js +92 -28
- package/dist/scheduler/SchedulerCheckpointRecord.d.ts +16 -0
- package/dist/scheduler/SchedulerCheckpointRecord.js +53 -0
- package/dist/scheduler/SchedulerWorker.d.ts +3 -5
- package/dist/scheduler/SchedulerWorker.js +58 -14
- package/dist/scheduler/console.js +3 -1
- package/dist/scheduler/contracts/Schedule.d.ts +19 -0
- package/dist/scheduler/contracts/Schedule.js +1 -0
- package/dist/scheduler/contracts/Scheduler.d.ts +18 -6
- package/dist/scheduler/contracts/SchedulerCheckpoint.d.ts +23 -0
- package/dist/scheduler/contracts/SchedulerCheckpoint.js +1 -0
- package/dist/scheduler/contracts/SchedulerConsole.d.ts +5 -0
- package/dist/scheduler/contracts/SchedulerWorker.d.ts +6 -1
- package/dist/scheduler/contracts/index.d.ts +2 -0
- package/dist/scheduler/contracts/index.js +2 -0
- package/dist/scheduler/index.d.ts +1 -0
- package/dist/scheduler/index.js +1 -0
- package/dist/server/App.d.ts +36 -4
- package/dist/server/App.js +57 -7
- package/dist/server/contracts/HttpErrorHandlerOptions.d.ts +29 -0
- package/dist/server/contracts/HttpErrorHandlerOptions.js +1 -0
- package/dist/server/contracts/index.d.ts +1 -0
- package/dist/server/contracts/index.js +1 -0
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.js +4 -0
- package/dist/server/publicServerErrorMessage.d.ts +11 -0
- package/dist/server/publicServerErrorMessage.js +16 -0
- package/dist/server/registerBrowserJsonFormatting.d.ts +16 -0
- package/dist/server/registerBrowserJsonFormatting.js +100 -0
- package/dist/server/registerHttpErrorHandler.d.ts +20 -0
- package/dist/server/registerHttpErrorHandler.js +93 -0
- package/dist/ssr/vite/contracts/ViteSsrOptions.d.ts +2 -0
- package/dist/ssr/vite/fastifyViteSsr.js +4 -3
- package/dist/storage/drivers/FlystorageDisk.js +2 -2
- package/dist/storage/index.d.ts +2 -2
- package/dist/storage/index.js +2 -2
- package/dist/websocket/WebSocketChannels.d.ts +18 -0
- package/dist/websocket/WebSocketChannels.js +129 -0
- package/dist/websocket/WebSocketConnection.d.ts +25 -0
- package/dist/websocket/WebSocketConnection.js +244 -0
- package/dist/websocket/WebSockets.d.ts +41 -0
- package/dist/websocket/WebSockets.js +194 -0
- package/dist/websocket/client/WebSocketClient.d.ts +21 -0
- package/dist/websocket/client/WebSocketClient.js +230 -0
- package/dist/websocket/client/index.d.ts +3 -0
- package/dist/websocket/client/index.js +1 -0
- package/dist/websocket/contracts/WebSocketChannel.d.ts +20 -0
- package/dist/websocket/contracts/WebSocketChannel.js +1 -0
- package/dist/websocket/contracts/WebSocketClient.d.ts +45 -0
- package/dist/websocket/contracts/WebSocketClient.js +1 -0
- package/dist/websocket/contracts/WebSocketEndpoint.d.ts +43 -0
- package/dist/websocket/contracts/WebSocketEndpoint.js +1 -0
- package/dist/websocket/contracts/WebSocketOptions.d.ts +48 -0
- package/dist/websocket/contracts/WebSocketOptions.js +1 -0
- package/dist/websocket/contracts/WebSocketPresence.d.ts +6 -0
- package/dist/websocket/contracts/WebSocketPresence.js +1 -0
- package/dist/websocket/contracts/WebSocketServiceOptions.d.ts +12 -0
- package/dist/websocket/contracts/WebSocketServiceOptions.js +1 -0
- package/dist/websocket/contracts/index.d.ts +6 -0
- package/dist/websocket/contracts/index.js +1 -0
- package/dist/websocket/defineChannel.d.ts +8 -0
- package/dist/websocket/defineChannel.js +13 -0
- package/dist/websocket/defineWebSocket.d.ts +14 -0
- package/dist/websocket/defineWebSocket.js +4 -0
- package/dist/websocket/fastify.d.ts +11 -0
- package/dist/websocket/fastify.js +38 -0
- package/dist/websocket/index.d.ts +5 -0
- package/dist/websocket/index.js +4 -0
- package/package.json +108 -7
- package/src/ai/AIErrors.ts +10 -0
- package/src/ai/AIFailover.ts +3 -2
- package/src/ai/AIProviderAdmission.ts +329 -0
- package/src/ai/AIRateLimiter.ts +134 -10
- package/src/ai/AIRequestTrackingError.ts +37 -0
- package/src/ai/Agent.ts +85 -71
- package/src/ai/Ai.ts +104 -48
- package/src/ai/OpenAIProviderError.ts +1 -1
- package/src/ai/ProviderSseObserver.ts +89 -0
- package/src/ai/README.md +216 -11
- package/src/ai/aiServiceTier.ts +14 -0
- package/src/ai/chunkEmbeddingText.ts +137 -0
- package/src/ai/contracts/AI.ts +17 -0
- package/src/ai/contracts/AIRequestErrorDiagnostics.ts +12 -0
- package/src/ai/contracts/AIServiceTier.ts +2 -0
- package/src/ai/contracts/Agent.ts +3 -0
- package/src/ai/contracts/EmbeddingTextChunk.ts +26 -0
- package/src/ai/contracts/Pricing.ts +7 -1
- package/src/ai/contracts/Providers.ts +2 -0
- package/src/ai/contracts/index.ts +2 -0
- package/src/ai/examples/prepareDocumentEmbeddings.ts +20 -0
- package/src/ai/index.ts +9 -2
- package/src/ai/{Models.ts → modelPricing.ts} +37 -4
- package/src/apps/AppInstallation.ts +25 -0
- package/src/apps/AppRouteError.ts +8 -0
- package/src/apps/AppService.ts +30 -0
- package/src/apps/Apps.ts +508 -0
- package/src/apps/README.md +354 -0
- package/src/apps/appsPlugin.ts +34 -0
- package/src/apps/commands/disableApp.ts +6 -0
- package/src/apps/commands/enableApp.ts +6 -0
- package/src/apps/commands/index.ts +29 -0
- package/src/apps/commands/installApp.ts +6 -0
- package/src/apps/commands/listApps.ts +6 -0
- package/src/apps/commands/makeAppMigration.ts +9 -0
- package/src/apps/commands/rollbackApp.ts +6 -0
- package/src/apps/commands/uninstallApp.ts +6 -0
- package/src/apps/contracts/AppAction.ts +2 -0
- package/src/apps/contracts/AppDefinition.ts +49 -0
- package/src/apps/contracts/AppDescription.ts +27 -0
- package/src/apps/contracts/AppManifest.ts +32 -0
- package/src/apps/contracts/AppNavigation.ts +10 -0
- package/src/apps/contracts/AppNavigationBadge.ts +6 -0
- package/src/apps/contracts/AppNavigationContext.ts +6 -0
- package/src/apps/contracts/AppNavigationResult.ts +9 -0
- package/src/apps/contracts/AppRoute.ts +18 -0
- package/src/apps/contracts/DiscoveredApp.ts +11 -0
- package/src/apps/contracts/InstalledApps.ts +2 -0
- package/src/apps/contracts/index.ts +11 -0
- package/src/apps/defineApp.ts +10 -0
- package/src/apps/discoverApps.ts +56 -0
- package/src/apps/fastify.ts +1 -0
- package/src/apps/index.ts +7 -0
- package/src/apps/normalizeAppNavigation.ts +29 -0
- package/src/apps/registerAppRoutes.ts +48 -0
- package/src/apps/validateAppDefinitions.ts +52 -0
- package/src/apps/vite.ts +1 -0
- package/src/apps/writeAppTypes.ts +14 -0
- package/src/auth/Auth.ts +89 -23
- package/src/auth/PasswordLoginAttempt.ts +72 -0
- package/src/auth/PasswordSuspendedError.ts +13 -0
- package/src/auth/README.md +94 -4
- package/src/auth/contracts/PasswordAuthProviderOptions.ts +30 -0
- package/src/auth/contracts/PasswordSuspension.ts +7 -0
- package/src/auth/contracts/PasswordSuspensionOptions.ts +10 -0
- package/src/auth/contracts/index.ts +3 -0
- package/src/auth/examples/runPasswordAuth.ts +6 -6
- package/src/auth/index.ts +2 -0
- package/src/auth/providers/PasswordAuthProvider.ts +96 -30
- package/src/auth/providers/resolveAuthProviders.ts +1 -0
- package/src/cli/Cli.ts +9 -0
- package/src/cli/README.md +19 -0
- package/src/cli/commands/repl.ts +2 -1
- package/src/cli/contracts/CliCommand.ts +2 -0
- package/src/cli/contracts/CliCommandInfo.ts +13 -0
- package/src/cli/contracts/index.ts +1 -0
- package/src/cli/index.ts +1 -0
- package/src/cli/listDb3Commands.ts +12 -0
- package/src/cli/runDb3.ts +5 -51
- package/src/cli/withDb3Cli.ts +55 -0
- package/src/db/ActiveQueryBuilder.ts +12 -0
- package/src/db/ActiveRecord.ts +9 -0
- package/src/db/README.md +45 -1
- package/src/db/connection.ts +12 -1
- package/src/db/examples/FieldNote.ts +7 -13
- package/src/db/examples/KnowledgeNote.ts +7 -13
- package/src/db/examples/runNoteMigrations.ts +11 -11
- package/src/db/fields/TimestampField.ts +8 -17
- package/src/db/migrations/DatabaseMigrationManager.ts +12 -4
- package/src/db/migrations/contracts/DatabaseMigration.ts +9 -0
- package/src/db/migrations/index.ts +1 -1
- package/src/db/migrations/{KnexMigrationRenderer.ts → renderKnexMigration.ts} +8 -14
- package/src/flows/FlowCompiler.ts +1 -1
- package/src/flows/Flows.ts +1 -1
- package/src/flows/index.ts +1 -1
- package/src/health/Health.ts +163 -0
- package/src/health/README.md +46 -0
- package/src/health/contracts/Health.ts +47 -0
- package/src/health/contracts/HealthHttp.ts +19 -0
- package/src/health/contracts/index.ts +2 -0
- package/src/health/databaseHealthCheck.ts +21 -0
- package/src/health/fastify.ts +49 -0
- package/src/health/index.ts +3 -0
- package/src/in-app/InApp.ts +173 -0
- package/src/in-app/InAppError.ts +8 -0
- package/src/in-app/InAppRecord.ts +41 -0
- package/src/in-app/README.md +193 -0
- package/src/in-app/contracts/InAppInbox.ts +51 -0
- package/src/in-app/contracts/InAppMessage.ts +19 -0
- package/src/in-app/contracts/InAppScope.ts +12 -0
- package/src/in-app/contracts/index.ts +3 -0
- package/src/in-app/examples/sendReportReady.ts +14 -0
- package/src/in-app/index.ts +4 -0
- package/src/in-app/validation.ts +49 -0
- package/src/index.ts +2 -22
- package/src/logging/DevelopmentConsole.ts +154 -0
- package/src/logging/Log.ts +5 -2
- package/src/logging/PrettyLogFormatter.ts +165 -0
- package/src/logging/README.md +52 -109
- package/src/logging/acquireDevelopmentConsole.ts +19 -0
- package/src/logging/contracts/DevelopmentConsoleOptions.ts +13 -0
- package/src/logging/contracts/EmailLogOptions.ts +9 -0
- package/src/logging/contracts/HttpExchangeMonitorOptions.ts +13 -0
- package/src/logging/contracts/LogTransportOptions.ts +10 -0
- package/src/logging/contracts/LoggingOptions.ts +12 -1
- package/src/logging/contracts/index.ts +4 -0
- package/src/logging/development/README.md +88 -0
- package/src/logging/drivers/PinoLoggerDriver.ts +72 -20
- package/src/logging/examples/runDevelopmentLogs.ts +36 -0
- package/src/logging/formatLogEmail.ts +24 -0
- package/src/logging/highlightQueueMessage.ts +38 -0
- package/src/logging/httpBodyCapture.ts +393 -0
- package/src/logging/index.ts +2 -0
- package/src/logging/redactLogEmail.ts +12 -0
- package/src/logging/registerHttpExchangeMonitor.ts +45 -0
- package/src/logging/transports/EmailLogTransport.ts +46 -0
- package/src/mail/Mail.ts +5 -0
- package/src/mail/MailDeliveryError.ts +8 -0
- package/src/mail/README.md +11 -1
- package/src/mail/index.ts +1 -0
- package/src/mail/transports/MailgunTransport.ts +2 -0
- package/src/mail/transports/ResendTransport.ts +24 -22
- package/src/media/MediaManager.ts +31 -10
- package/src/media/README.md +4 -0
- package/src/media/examples/runPrivateFiles.ts +2 -2
- package/src/network/BlockedUrlError.ts +19 -0
- package/src/network/README.md +133 -0
- package/src/network/assertPublicUrl.ts +77 -0
- package/src/network/contracts/GuardedFetch.ts +40 -0
- package/src/network/contracts/PublicUrlOptions.ts +17 -0
- package/src/network/contracts/index.ts +2 -0
- package/src/network/examples/runGuardedFetch.ts +35 -0
- package/src/network/guardedFetch.ts +150 -0
- package/src/network/index.ts +6 -0
- package/src/network/playwright/GuardedBrowserProxy.ts +172 -0
- package/src/network/playwright/contracts/GuardedBrowserContextOptions.ts +43 -0
- package/src/network/playwright/contracts/index.ts +1 -0
- package/src/network/playwright/index.ts +2 -0
- package/src/network/playwright/newGuardedBrowserContext.ts +74 -0
- package/src/network/privateNetworkAddresses.ts +166 -0
- package/src/network/publicAddressLookup.ts +49 -0
- package/src/notifications/Notifications.ts +105 -0
- package/src/notifications/README.md +59 -0
- package/src/notifications/contracts/Notification.ts +65 -0
- package/src/notifications/contracts/NotificationDelivery.ts +11 -0
- package/src/notifications/contracts/index.ts +2 -0
- package/src/notifications/examples/ReportReadyNotification.ts +31 -0
- package/src/notifications/index.ts +2 -0
- package/src/queue/JobRunAttempt.ts +17 -0
- package/src/queue/JobRunEvent.ts +16 -0
- package/src/queue/JobRunStoppedError.ts +8 -0
- package/src/queue/Queue.ts +112 -11
- package/src/queue/QueueTerminalError.ts +12 -0
- package/src/queue/QueueWorker.ts +86 -7
- package/src/queue/QueueWorkerReporter.ts +59 -0
- package/src/queue/QueueableJob.ts +2 -2
- package/src/queue/README.md +142 -1
- package/src/queue/RunnableJob.ts +272 -0
- package/src/queue/console.ts +54 -21
- package/src/queue/contracts/QueueDriver.ts +9 -0
- package/src/queue/contracts/QueueJobRunner.ts +4 -0
- package/src/queue/contracts/QueuePayload.ts +1 -1
- package/src/queue/contracts/QueueSelection.ts +11 -0
- package/src/queue/contracts/QueueService.ts +19 -4
- package/src/queue/contracts/QueueWorkerLifecycle.ts +2 -2
- package/src/queue/contracts/QueueWorkerSnapshot.ts +15 -0
- package/src/queue/contracts/QueueableJob.ts +7 -3
- package/src/queue/contracts/QueueableJobContract.ts +13 -0
- package/src/queue/contracts/index.ts +6 -0
- package/src/queue/drivers/DatabaseQueueDriver.ts +25 -3
- package/src/queue/drivers/RedisQueueDriver.ts +36 -0
- package/src/queue/examples/runQueueSelection.ts +34 -0
- package/src/queue/index.ts +7 -0
- package/src/queue/jobEnvelopeHasPrefix.ts +6 -0
- package/src/scheduler/README.md +50 -9
- package/src/scheduler/ScheduledEvent.ts +20 -5
- package/src/scheduler/ScheduledJob.ts +1 -1
- package/src/scheduler/Scheduler.ts +73 -27
- package/src/scheduler/SchedulerCheckpointRecord.ts +51 -0
- package/src/scheduler/SchedulerWorker.ts +54 -19
- package/src/scheduler/console.ts +3 -1
- package/src/scheduler/contracts/Schedule.ts +22 -0
- package/src/scheduler/contracts/Scheduler.ts +21 -6
- package/src/scheduler/contracts/SchedulerCheckpoint.ts +23 -0
- package/src/scheduler/contracts/SchedulerConsole.ts +5 -0
- package/src/scheduler/contracts/SchedulerWorker.ts +6 -1
- package/src/scheduler/contracts/index.ts +2 -0
- package/src/scheduler/examples/runSchedulerWindow.ts +54 -0
- package/src/scheduler/index.ts +1 -0
- package/src/server/App.ts +73 -10
- package/src/server/README.md +95 -2
- package/src/server/contracts/HttpErrorHandlerOptions.ts +29 -0
- package/src/server/contracts/index.ts +1 -0
- package/src/server/examples/createFirstServer.ts +5 -3
- package/src/server/index.ts +4 -0
- package/src/server/publicServerErrorMessage.ts +17 -0
- package/src/server/registerBrowserJsonFormatting.ts +94 -0
- package/src/server/registerHttpErrorHandler.ts +96 -0
- package/src/ssr/README.md +6 -0
- package/src/ssr/vite/contracts/ViteSsrOptions.ts +2 -0
- package/src/ssr/vite/fastifyViteSsr.ts +4 -3
- package/src/storage/drivers/FlystorageDisk.ts +2 -2
- package/src/storage/index.ts +2 -2
- package/src/websocket/README.md +458 -0
- package/src/websocket/WebSocketChannels.ts +99 -0
- package/src/websocket/WebSocketConnection.ts +162 -0
- package/src/websocket/WebSockets.ts +157 -0
- package/src/websocket/client/WebSocketClient.ts +188 -0
- package/src/websocket/client/index.ts +3 -0
- package/src/websocket/contracts/WebSocketChannel.ts +23 -0
- package/src/websocket/contracts/WebSocketClient.ts +48 -0
- package/src/websocket/contracts/WebSocketEndpoint.ts +46 -0
- package/src/websocket/contracts/WebSocketOptions.ts +50 -0
- package/src/websocket/contracts/WebSocketPresence.ts +6 -0
- package/src/websocket/contracts/WebSocketServiceOptions.ts +12 -0
- package/src/websocket/contracts/index.ts +6 -0
- package/src/websocket/defineChannel.ts +14 -0
- package/src/websocket/defineWebSocket.ts +15 -0
- package/src/websocket/examples/board/LiveBoard.ts +97 -0
- package/src/websocket/examples/board/SummarizeBoardJob.ts +52 -0
- package/src/websocket/examples/board/boardSnapshot.ts +16 -0
- package/src/websocket/examples/board/createBoardClient.ts +43 -0
- package/src/websocket/examples/board/createBoardStore.ts +58 -0
- package/src/websocket/examples/board/registerBoardRoutes.ts +31 -0
- package/src/websocket/examples/createChannelSync.ts +66 -0
- package/src/websocket/examples/deploymentEndpoint.ts +13 -0
- package/src/websocket/examples/deploymentSnapshot.ts +36 -0
- package/src/websocket/examples/resourceChannels.ts +18 -0
- package/src/websocket/examples/userEndpoint.ts +27 -0
- package/src/websocket/fastify.ts +39 -0
- package/src/websocket/index.ts +5 -0
- package/templates/AGENTS.md +33 -2
- /package/dist/flows/{FlowValueValidator.d.ts → flowValueValidator.d.ts} +0 -0
- /package/dist/flows/{FlowValueValidator.js → flowValueValidator.js} +0 -0
- /package/dist/storage/{mime.d.ts → mimeTypeFromPath.d.ts} +0 -0
- /package/dist/storage/{mime.js → mimeTypeFromPath.js} +0 -0
- /package/dist/storage/{contents.d.ts → storageContentsToBuffer.d.ts} +0 -0
- /package/dist/storage/{contents.js → storageContentsToBuffer.js} +0 -0
- /package/src/flows/{FlowValueValidator.ts → flowValueValidator.ts} +0 -0
- /package/src/storage/{mime.ts → mimeTypeFromPath.ts} +0 -0
- /package/src/storage/{contents.ts → storageContentsToBuffer.ts} +0 -0
package/dist/ai/Ai.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { AIProviderAdmission, AIProviderDeferredError, AIProviderStoppedError } from './AIProviderAdmission.js';
|
|
2
|
+
import { isQueueRetryLaterError } from '@db3.ai/app/queue';
|
|
2
3
|
import OpenAI from 'openai';
|
|
3
4
|
import { OpenAIProvider } from '@openai/agents';
|
|
4
5
|
import { aiExecutionContext, scopedAiOptions } from './AiExecutionContext.js';
|
|
@@ -10,15 +11,17 @@ import { AiMessage, AI_MESSAGE_ROLE } from './AiMessage.js';
|
|
|
10
11
|
import { AiRequest, AI_REQUEST_STATUS } from './AiRequest.js';
|
|
11
12
|
const defaultAIConfig = { model: 'gpt-4.1-mini' };
|
|
12
13
|
import { estimateTokensFromText } from './Estimates.js';
|
|
13
|
-
import { calculateAIImageRequestCostUSD, calculateAIRequestCostUSD } from './
|
|
14
|
+
import { calculateAIImageRequestCostUSD, calculateAIRequestCostUSD } from './modelPricing.js';
|
|
14
15
|
import { AI_PROVIDER } from './contracts/AI.js';
|
|
15
16
|
import { AIAllowanceExceededError, AIConfigurationError, AIRequestError } from './AIErrors.js';
|
|
17
|
+
import { AIRequestTrackingError } from './AIRequestTrackingError.js';
|
|
16
18
|
import { isFailoverableFailure, isFailoverableResponse } from './AIFailover.js';
|
|
17
19
|
import { providerChainFromEnvironment, resolveProviderChain, simulatedProviderOutage } from './AIProviders.js';
|
|
18
20
|
import { AIRateLimitDeferredError, AIRateLimiter, embeddingsRateLimitEndpoint, imagesRateLimitEndpoint, providerResponseMetadata, responsesRateLimitEndpoint } from './AIRateLimiter.js';
|
|
19
21
|
import { isOpenAIQuotaError, openAIProviderError } from './OpenAIProviderError.js';
|
|
20
22
|
import { providerReportedCostUSD } from './ProviderCosts.js';
|
|
21
23
|
import { sendResponsesRequest } from './ResponsesProviderCall.js';
|
|
24
|
+
import { assertAIServiceTier, effectiveAIServiceTier } from './aiServiceTier.js';
|
|
22
25
|
/**
|
|
23
26
|
* Raised when exact provider usage could not be made durable after bounded retries.
|
|
24
27
|
*
|
|
@@ -60,6 +63,7 @@ export class Ai {
|
|
|
60
63
|
/** Logical ownership field used by this application. */
|
|
61
64
|
scopeField;
|
|
62
65
|
rateLimiter;
|
|
66
|
+
providerAdmission;
|
|
63
67
|
/**
|
|
64
68
|
* Create an AI service instance.
|
|
65
69
|
*
|
|
@@ -71,6 +75,9 @@ export class Ai {
|
|
|
71
75
|
this.modelConfig = modelConfig;
|
|
72
76
|
if (options.timeoutMs !== undefined && (!Number.isFinite(options.timeoutMs) || options.timeoutMs <= 0))
|
|
73
77
|
throw new AIConfigurationError('timeoutMs must be positive.');
|
|
78
|
+
this.providerAdmission = new AIProviderAdmission({ recoveryLeaseSeconds: (options.timeoutMs ?? 60_000) / 1000 + 30, ...options.providerAdmission });
|
|
79
|
+
if ((options.providerAdmission?.recoveryLeaseSeconds ?? Infinity) * 1000 <= (options.timeoutMs ?? 60_000))
|
|
80
|
+
throw new AIConfigurationError('Recovery lease must exceed provider timeout.');
|
|
74
81
|
this.models = { conversation: AiConversation, request: AiRequest, message: AiMessage, ...options.models };
|
|
75
82
|
this.scopeField = options.scopeField ?? 'scope';
|
|
76
83
|
this.rateLimiter = options.rateLimiter === false
|
|
@@ -190,6 +197,8 @@ export class Ai {
|
|
|
190
197
|
const result = await this.generateTextResult({
|
|
191
198
|
model: input.model,
|
|
192
199
|
provider: input.provider,
|
|
200
|
+
transientFailureScope: input.transientFailureScope,
|
|
201
|
+
serviceTier: input.serviceTier,
|
|
193
202
|
instructions: input.instructions,
|
|
194
203
|
input: input.input,
|
|
195
204
|
maxOutputTokens: input.maxOutputTokens,
|
|
@@ -241,6 +250,10 @@ export class Ai {
|
|
|
241
250
|
const operation = options.operation ?? 'responses.create';
|
|
242
251
|
const attempts = this.resolveProviders(input.provider ?? null, input.model ?? null)
|
|
243
252
|
.filter(attempt => attempt.supportsResponsesApi);
|
|
253
|
+
const serviceTier = input.serviceTier ?? 'default';
|
|
254
|
+
for (const attempt of attempts)
|
|
255
|
+
assertAIServiceTier(attempt.provider, serviceTier);
|
|
256
|
+
const timeoutMs = this.requestTimeoutMs(serviceTier);
|
|
244
257
|
const attemptFailures = [];
|
|
245
258
|
let tracked = null;
|
|
246
259
|
let trackedSettled = false;
|
|
@@ -248,7 +261,7 @@ export class Ai {
|
|
|
248
261
|
throw new AIConfigurationError('No configured AI provider in the chain supports the Responses API.');
|
|
249
262
|
}
|
|
250
263
|
for (let index = 0; index < attempts.length; index++) {
|
|
251
|
-
const attempt = attempts[index];
|
|
264
|
+
const attempt = { ...attempts[index], transientFailureScope: input.transientFailureScope };
|
|
252
265
|
const hasFallback = index < attempts.length - 1;
|
|
253
266
|
const attemptStartMs = Date.now();
|
|
254
267
|
const request = {
|
|
@@ -256,7 +269,7 @@ export class Ai {
|
|
|
256
269
|
instructions: input.instructions,
|
|
257
270
|
input: input.input,
|
|
258
271
|
store: false,
|
|
259
|
-
...(attempt.provider === AI_PROVIDER.openai ? { service_tier:
|
|
272
|
+
...(attempt.provider === AI_PROVIDER.openai ? { service_tier: serviceTier } : {}),
|
|
260
273
|
...maxOutputTokensPayload(input.maxOutputTokens),
|
|
261
274
|
...extraRequest,
|
|
262
275
|
};
|
|
@@ -302,12 +315,13 @@ export class Ai {
|
|
|
302
315
|
baseUrl: attempt.baseUrl,
|
|
303
316
|
apiKey: attempt.apiKey,
|
|
304
317
|
request,
|
|
305
|
-
fetcher: this.fetcher(),
|
|
318
|
+
fetcher: this.providerAdmission.transport(attempt, this.fetcher(timeoutMs), timeoutMs),
|
|
306
319
|
});
|
|
307
320
|
const { payload, metadata } = result;
|
|
308
|
-
|
|
321
|
+
const admissionFailure = this.providerAdmission.requestFailure(attempt, null);
|
|
322
|
+
if (!result.ok || admissionFailure) {
|
|
309
323
|
const message = responseErrorMessage(payload) || `AI provider ${attempt.provider} request failed with HTTP ${result.status}.`;
|
|
310
|
-
if (hasFallback && isFailoverableResponse(result.status, payload)) {
|
|
324
|
+
if (hasFallback && (isFailoverableFailure(admissionFailure) || isFailoverableResponse(result.status, payload))) {
|
|
311
325
|
const failure = providerAttemptFailure(attempt, attemptStartMs, message, result.status, responseErrorCode(payload));
|
|
312
326
|
attemptFailures.push(failure);
|
|
313
327
|
if (tracked) {
|
|
@@ -325,9 +339,9 @@ export class Ai {
|
|
|
325
339
|
lease = null;
|
|
326
340
|
continue;
|
|
327
341
|
}
|
|
328
|
-
const deferred = isRateLimitFailure(result.status, payload)
|
|
342
|
+
const deferred = admissionFailure ?? (isRateLimitFailure(result.status, payload)
|
|
329
343
|
? rateLimitDeferredError(lease, metadata, message)
|
|
330
|
-
: null;
|
|
344
|
+
: null);
|
|
331
345
|
if (tracked) {
|
|
332
346
|
attachProviderAttempts(tracked.aiRequest, attemptFailures);
|
|
333
347
|
await this.failTrackedRequest(tracked, payload, message, metadata, lease);
|
|
@@ -337,7 +351,7 @@ export class Ai {
|
|
|
337
351
|
lease = null;
|
|
338
352
|
if (deferred)
|
|
339
353
|
throw deferred;
|
|
340
|
-
throw new AIRequestError(message, responseErrorCode(payload));
|
|
354
|
+
throw new AIRequestError(message, responseErrorCode(payload), { status: result.status, requestId: metadata.providerRequestId });
|
|
341
355
|
}
|
|
342
356
|
const text = payload?.status !== undefined && payload.status !== 'completed' ? null : responseOutputText(payload);
|
|
343
357
|
if (!text) {
|
|
@@ -405,9 +419,9 @@ export class Ai {
|
|
|
405
419
|
}
|
|
406
420
|
continue;
|
|
407
421
|
}
|
|
408
|
-
if (tracked && !trackedSettled && !(error instanceof AIRequestError) && !(error instanceof AIRateLimitDeferredError)) {
|
|
422
|
+
if (tracked && !trackedSettled && (!(error instanceof AIRequestError) || error instanceof AIProviderStoppedError) && !(error instanceof AIRateLimitDeferredError)) {
|
|
409
423
|
attachProviderAttempts(tracked.aiRequest, attemptFailures);
|
|
410
|
-
await this.failTrackedRequest(tracked,
|
|
424
|
+
await this.failTrackedRequest(tracked, providerAdmissionResponse(error), errorMessage(error), providerAdmissionMetadata(error), failedLease, providerAdmissionStarted(error, providerStarted));
|
|
411
425
|
}
|
|
412
426
|
throw error;
|
|
413
427
|
}
|
|
@@ -447,7 +461,7 @@ export class Ai {
|
|
|
447
461
|
*/
|
|
448
462
|
async appendTrackedProviderAttempt(tracked, input) {
|
|
449
463
|
const usage = tokenUsage(input.response);
|
|
450
|
-
const modelCostUSD = textRequestCostUSD(input.attempt.provider, input.attempt.model, usage, input.response);
|
|
464
|
+
const modelCostUSD = textRequestCostUSD(input.attempt.provider, input.attempt.model, usage, input.response, input.request.service_tier);
|
|
451
465
|
const aiRequest = new this.models.request({
|
|
452
466
|
conversation: tracked.conversation,
|
|
453
467
|
parentAiRequest: tracked.aiRequest,
|
|
@@ -565,7 +579,7 @@ export class Ai {
|
|
|
565
579
|
*/
|
|
566
580
|
async completeTrackedRequest(tracked, response, metadata = emptyProviderResponseMetadata(), lease = null) {
|
|
567
581
|
const usage = tokenUsage(response);
|
|
568
|
-
const costUSD = textRequestCostUSD(tracked.provider, tracked.model, usage, response);
|
|
582
|
+
const costUSD = textRequestCostUSD(tracked.provider, tracked.model, usage, response, asRecord(tracked.aiRequest.request)?.service_tier);
|
|
569
583
|
tracked.aiRequest.assign({
|
|
570
584
|
response,
|
|
571
585
|
providerRequestId: metadata.providerRequestId,
|
|
@@ -600,7 +614,7 @@ export class Ai {
|
|
|
600
614
|
const usage = tokenUsage(response);
|
|
601
615
|
const costUSD = !providerStarted
|
|
602
616
|
? 0
|
|
603
|
-
: textRequestCostUSD(tracked.provider, tracked.model, usage, response);
|
|
617
|
+
: textRequestCostUSD(tracked.provider, tracked.model, usage, response, asRecord(tracked.aiRequest.request)?.service_tier);
|
|
604
618
|
tracked.aiRequest.assign({
|
|
605
619
|
response,
|
|
606
620
|
providerRequestId: metadata.providerRequestId,
|
|
@@ -715,7 +729,13 @@ export class Ai {
|
|
|
715
729
|
request,
|
|
716
730
|
metadata: options.metadata ?? null,
|
|
717
731
|
startedAt: new Date(),
|
|
718
|
-
})
|
|
732
|
+
});
|
|
733
|
+
try {
|
|
734
|
+
await aiRequest.save();
|
|
735
|
+
}
|
|
736
|
+
catch (error) {
|
|
737
|
+
throw new AIRequestTrackingError(error);
|
|
738
|
+
}
|
|
719
739
|
return {
|
|
720
740
|
aiRequest,
|
|
721
741
|
startTimeMs,
|
|
@@ -725,11 +745,11 @@ export class Ai {
|
|
|
725
745
|
/**
|
|
726
746
|
* Mark an embedding request completed with normalized token usage.
|
|
727
747
|
*/
|
|
728
|
-
async completeTrackedEmbeddingRequest(tracked, response, metadata = emptyProviderResponseMetadata(), lease = null) {
|
|
748
|
+
async completeTrackedEmbeddingRequest(tracked, response, metadata = emptyProviderResponseMetadata(), lease = null, retainResult = false) {
|
|
729
749
|
const usage = embeddingTokenUsage(response);
|
|
730
750
|
const costUSD = calculateAIRequestCostUSD(tracked.model, usage);
|
|
731
751
|
tracked.aiRequest.assign({
|
|
732
|
-
response: compactEmbeddingResponse(response),
|
|
752
|
+
response: retainResult ? response : compactEmbeddingResponse(response),
|
|
733
753
|
providerRequestId: metadata.providerRequestId,
|
|
734
754
|
providerProcessingMs: metadata.providerProcessingMs,
|
|
735
755
|
rateLimitBucket: lease?.bucket ?? null,
|
|
@@ -929,9 +949,11 @@ export class Ai {
|
|
|
929
949
|
* @param initialLease - Optional preflight reservation consumed by the first request.
|
|
930
950
|
* @returns SDK provider using the service's transport and rate-limit lifecycle.
|
|
931
951
|
*/
|
|
932
|
-
createAgentProvider(attempt, initialLease = null) {
|
|
952
|
+
createAgentProvider(attempt, initialLease = null, serviceTier = 'default') {
|
|
953
|
+
assertAIServiceTier(attempt.provider, serviceTier);
|
|
954
|
+
const timeoutMs = this.requestTimeoutMs(serviceTier);
|
|
933
955
|
return new OpenAIProvider({
|
|
934
|
-
openAIClient: new OpenAI({ apiKey: attempt.apiKey, baseURL: attempt.baseUrl, fetch: this.agentFetcher(attempt, initialLease), timeout:
|
|
956
|
+
openAIClient: new OpenAI({ apiKey: attempt.apiKey, baseURL: attempt.baseUrl, fetch: this.agentFetcher(attempt, initialLease, timeoutMs), timeout: timeoutMs, maxRetries: 0 }),
|
|
935
957
|
useResponses: attempt.supportsResponsesApi,
|
|
936
958
|
});
|
|
937
959
|
}
|
|
@@ -947,9 +969,9 @@ export class Ai {
|
|
|
947
969
|
* @param initialLease - First-request reservation, if acquired by the agent.
|
|
948
970
|
* @returns Transport that maintains provider capacity and request diagnostics.
|
|
949
971
|
*/
|
|
950
|
-
agentFetcher(attempt, initialLease) {
|
|
972
|
+
agentFetcher(attempt, initialLease, timeoutMs) {
|
|
951
973
|
let pendingLease = initialLease;
|
|
952
|
-
const transport = this.fetcher();
|
|
974
|
+
const transport = this.providerAdmission.transport(attempt, this.fetcher(timeoutMs), timeoutMs);
|
|
953
975
|
return async (url, init) => {
|
|
954
976
|
const aiRequest = aiExecutionContext.getStore()?.aiRequest ?? null;
|
|
955
977
|
const lease = pendingLease ?? await this.acquireRateLimit({
|
|
@@ -986,12 +1008,20 @@ export class Ai {
|
|
|
986
1008
|
}
|
|
987
1009
|
};
|
|
988
1010
|
}
|
|
1011
|
+
/** Resolves the account connection for OpenAI-only images and embeddings. */
|
|
1012
|
+
openAIAdmissionAttempt(model) {
|
|
1013
|
+
return { provider: AI_PROVIDER.openai, model, apiKey: this.requireApiKey(), baseUrl: this.baseUrl(), supportsResponsesApi: true };
|
|
1014
|
+
}
|
|
1015
|
+
/** Resolves a tier-aware request timeout while respecting an explicit service override. */
|
|
1016
|
+
requestTimeoutMs(serviceTier) {
|
|
1017
|
+
return this.options.timeoutMs ?? (serviceTier === 'flex' ? 900_000 : 60_000);
|
|
1018
|
+
}
|
|
989
1019
|
/**
|
|
990
1020
|
* Fetch implementation used by provider calls.
|
|
991
1021
|
*/
|
|
992
|
-
fetcher() {
|
|
1022
|
+
fetcher(timeoutMs = this.options.timeoutMs ?? 60_000) {
|
|
993
1023
|
const transport = this.options.fetch || fetch;
|
|
994
|
-
return (url, init) => transport(url, { ...init, signal: init?.signal ?? AbortSignal.timeout(
|
|
1024
|
+
return (url, init) => transport(url, { ...init, signal: init?.signal ?? AbortSignal.timeout(timeoutMs) });
|
|
995
1025
|
}
|
|
996
1026
|
/**
|
|
997
1027
|
* Responses API base URL with trailing slashes removed.
|
|
@@ -1077,6 +1107,7 @@ export class Ai {
|
|
|
1077
1107
|
const model = input.model || this.config('ImageGeneration', 'gpt-image-2');
|
|
1078
1108
|
const prompt = input.prompt.replace(/\s+/g, ' ').trim();
|
|
1079
1109
|
const outputFormat = input.outputFormat ?? 'png';
|
|
1110
|
+
const admissionAttempt = this.openAIAdmissionAttempt(model);
|
|
1080
1111
|
const operation = options.operation ?? 'images.generate';
|
|
1081
1112
|
const request = {
|
|
1082
1113
|
model,
|
|
@@ -1125,7 +1156,7 @@ export class Ai {
|
|
|
1125
1156
|
});
|
|
1126
1157
|
await this.attachRateLimitRequest(lease, tracked?.aiRequest ?? null);
|
|
1127
1158
|
providerStarted = true;
|
|
1128
|
-
const response = await this.fetcher()(this.imagesUrl(), {
|
|
1159
|
+
const response = await this.providerAdmission.transport(admissionAttempt, this.fetcher())(this.imagesUrl(), {
|
|
1129
1160
|
method: 'POST',
|
|
1130
1161
|
headers: {
|
|
1131
1162
|
authorization: `Bearer ${apiKey}`,
|
|
@@ -1135,11 +1166,12 @@ export class Ai {
|
|
|
1135
1166
|
});
|
|
1136
1167
|
const metadata = providerResponseMetadata(response.headers);
|
|
1137
1168
|
const payload = await response.json().catch(() => null);
|
|
1138
|
-
|
|
1169
|
+
const admissionFailure = this.providerAdmission.requestFailure(admissionAttempt, null);
|
|
1170
|
+
if (!response.ok || admissionFailure) {
|
|
1139
1171
|
const message = responseErrorMessage(payload) || `OpenAI image request failed with HTTP ${response.status}.`;
|
|
1140
|
-
const deferred = isRateLimitFailure(response.status, payload)
|
|
1172
|
+
const deferred = admissionFailure ?? (isRateLimitFailure(response.status, payload)
|
|
1141
1173
|
? rateLimitDeferredError(lease, metadata, message)
|
|
1142
|
-
: null;
|
|
1174
|
+
: null);
|
|
1143
1175
|
if (tracked) {
|
|
1144
1176
|
await this.failTrackedImageRequest(tracked, payload, message, metadata, lease);
|
|
1145
1177
|
trackedSettled = true;
|
|
@@ -1148,7 +1180,7 @@ export class Ai {
|
|
|
1148
1180
|
lease = null;
|
|
1149
1181
|
if (deferred)
|
|
1150
1182
|
throw deferred;
|
|
1151
|
-
throw new AIRequestError(message, responseErrorCode(payload));
|
|
1183
|
+
throw new AIRequestError(message, responseErrorCode(payload), { status: response.status, requestId: metadata.providerRequestId });
|
|
1152
1184
|
}
|
|
1153
1185
|
const b64Json = imageBase64(payload);
|
|
1154
1186
|
if (!b64Json) {
|
|
@@ -1209,10 +1241,10 @@ export class Ai {
|
|
|
1209
1241
|
}
|
|
1210
1242
|
}
|
|
1211
1243
|
await this.releaseRateLimit(lease);
|
|
1212
|
-
if (tracked && !trackedSettled
|
|
1213
|
-
await this.failTrackedImageRequest(tracked,
|
|
1244
|
+
if (tracked && !trackedSettled) {
|
|
1245
|
+
await this.failTrackedImageRequest(tracked, providerAdmissionResponse(error), errorMessage(error), providerAdmissionMetadata(error), lease, providerAdmissionStarted(error, providerStarted));
|
|
1214
1246
|
}
|
|
1215
|
-
if (error instanceof
|
|
1247
|
+
if (isQueueRetryLaterError(error) || error instanceof AIProviderStoppedError)
|
|
1216
1248
|
throw error;
|
|
1217
1249
|
if (error instanceof AIAllowanceExceededError)
|
|
1218
1250
|
throw error;
|
|
@@ -1235,6 +1267,7 @@ export class Ai {
|
|
|
1235
1267
|
if (!input)
|
|
1236
1268
|
throw new Error('Input is required for embedding generation.');
|
|
1237
1269
|
const apiKey = this.requireApiKey();
|
|
1270
|
+
const admissionAttempt = this.openAIAdmissionAttempt(model);
|
|
1238
1271
|
const operation = options.operation ?? 'embeddings.create';
|
|
1239
1272
|
const request = {
|
|
1240
1273
|
model,
|
|
@@ -1273,7 +1306,7 @@ export class Ai {
|
|
|
1273
1306
|
});
|
|
1274
1307
|
await this.attachRateLimitRequest(lease, tracked?.aiRequest ?? null);
|
|
1275
1308
|
providerStarted = true;
|
|
1276
|
-
const response = await this.fetcher()(this.embeddingsUrl(), {
|
|
1309
|
+
const response = await this.providerAdmission.transport(admissionAttempt, this.fetcher())(this.embeddingsUrl(), {
|
|
1277
1310
|
method: 'POST',
|
|
1278
1311
|
headers: {
|
|
1279
1312
|
authorization: `Bearer ${apiKey}`,
|
|
@@ -1283,11 +1316,12 @@ export class Ai {
|
|
|
1283
1316
|
});
|
|
1284
1317
|
const metadata = providerResponseMetadata(response.headers);
|
|
1285
1318
|
const payload = await response.json().catch(() => null);
|
|
1286
|
-
|
|
1319
|
+
const admissionFailure = this.providerAdmission.requestFailure(admissionAttempt, null);
|
|
1320
|
+
if (!response.ok || admissionFailure) {
|
|
1287
1321
|
const message = responseErrorMessage(payload) || `OpenAI embedding request failed with HTTP ${response.status}.`;
|
|
1288
|
-
const deferred = isRateLimitFailure(response.status, payload)
|
|
1322
|
+
const deferred = admissionFailure ?? (isRateLimitFailure(response.status, payload)
|
|
1289
1323
|
? rateLimitDeferredError(lease, metadata, message)
|
|
1290
|
-
: null;
|
|
1324
|
+
: null);
|
|
1291
1325
|
if (tracked) {
|
|
1292
1326
|
await this.failTrackedEmbeddingRequest(tracked, payload, message, metadata, lease);
|
|
1293
1327
|
trackedSettled = true;
|
|
@@ -1296,7 +1330,7 @@ export class Ai {
|
|
|
1296
1330
|
lease = null;
|
|
1297
1331
|
if (deferred)
|
|
1298
1332
|
throw deferred;
|
|
1299
|
-
throw new AIRequestError(message, responseErrorCode(payload));
|
|
1333
|
+
throw new AIRequestError(message, responseErrorCode(payload), { status: response.status, requestId: metadata.providerRequestId });
|
|
1300
1334
|
}
|
|
1301
1335
|
const vector = payload?.data?.[0]?.embedding;
|
|
1302
1336
|
if (!Array.isArray(vector)) {
|
|
@@ -1310,7 +1344,7 @@ export class Ai {
|
|
|
1310
1344
|
throw new AIRequestError(message, responseErrorCode(payload));
|
|
1311
1345
|
}
|
|
1312
1346
|
if (tracked) {
|
|
1313
|
-
await this.completeTrackedEmbeddingRequest(tracked, payload, metadata, lease);
|
|
1347
|
+
await this.completeTrackedEmbeddingRequest(tracked, payload, metadata, lease, options.retainResult === true);
|
|
1314
1348
|
trackedSettled = true;
|
|
1315
1349
|
}
|
|
1316
1350
|
await this.observeRateLimit(lease, metadata);
|
|
@@ -1331,10 +1365,12 @@ export class Ai {
|
|
|
1331
1365
|
}
|
|
1332
1366
|
}
|
|
1333
1367
|
await this.releaseRateLimit(lease);
|
|
1334
|
-
if (tracked && !trackedSettled && !(error instanceof AIRequestError) && !(error instanceof AIRateLimitDeferredError)) {
|
|
1335
|
-
await this.failTrackedEmbeddingRequest(tracked,
|
|
1368
|
+
if (tracked && !trackedSettled && (!(error instanceof AIRequestError) || error instanceof AIProviderStoppedError) && !(error instanceof AIRateLimitDeferredError)) {
|
|
1369
|
+
await this.failTrackedEmbeddingRequest(tracked, providerAdmissionResponse(error), errorMessage(error), providerAdmissionMetadata(error), lease, providerAdmissionStarted(error, providerStarted));
|
|
1336
1370
|
}
|
|
1337
|
-
if (error instanceof
|
|
1371
|
+
if (isQueueRetryLaterError(error) || error instanceof AIProviderStoppedError)
|
|
1372
|
+
throw error;
|
|
1373
|
+
if (error instanceof AIRequestTrackingError)
|
|
1338
1374
|
throw error;
|
|
1339
1375
|
if (error instanceof AIRequestError)
|
|
1340
1376
|
throw error;
|
|
@@ -1487,7 +1523,9 @@ function attachProviderAttempts(aiRequest, failures) {
|
|
|
1487
1523
|
* @returns True when the provider indicates a rate-limit failure.
|
|
1488
1524
|
*/
|
|
1489
1525
|
function isRateLimitFailure(status, payload) {
|
|
1490
|
-
|
|
1526
|
+
// Some credit-exhaustion responses omit a stable code. Do not turn their
|
|
1527
|
+
// explicit billing failure into an unlimited queue capacity deferral.
|
|
1528
|
+
if (isOpenAIQuotaError(payload) || /\b(?:you have )?no credits remaining\b/i.test(openAIProviderError(payload).message))
|
|
1491
1529
|
return false;
|
|
1492
1530
|
const code = responseErrorCode(payload);
|
|
1493
1531
|
return code === 'rate_limit_exceeded' || (status === 429 && code === null);
|
|
@@ -1552,9 +1590,9 @@ function rateLimitResetAt(metadata) {
|
|
|
1552
1590
|
* @param payload - Raw provider response containing billed-cost extensions.
|
|
1553
1591
|
* @returns Exact billed cost in USD, or null when it cannot be established.
|
|
1554
1592
|
*/
|
|
1555
|
-
function textRequestCostUSD(provider, model, usage, payload) {
|
|
1593
|
+
function textRequestCostUSD(provider, model, usage, payload, requestedTier) {
|
|
1556
1594
|
if (provider === AI_PROVIDER.openai) {
|
|
1557
|
-
return calculateAIRequestCostUSD(model, usage);
|
|
1595
|
+
return calculateAIRequestCostUSD(typeof payload?.model === 'string' ? payload.model : model, { ...usage, serviceTier: effectiveAIServiceTier(payload?.service_tier, requestedTier) });
|
|
1558
1596
|
}
|
|
1559
1597
|
return providerReportedCostUSD(provider, payload);
|
|
1560
1598
|
}
|
|
@@ -1769,3 +1807,15 @@ function scopeRecordId(scope) {
|
|
|
1769
1807
|
const id = scope?.id;
|
|
1770
1808
|
return id === null || id === undefined ? null : String(id);
|
|
1771
1809
|
}
|
|
1810
|
+
/** Retains safe provider diagnostics for admission failures in the existing audit trail. */
|
|
1811
|
+
function providerAdmissionResponse(error) {
|
|
1812
|
+
return error instanceof AIProviderStoppedError ? { error: { code: error.providerCode ?? error.code, message: error.message } } : null;
|
|
1813
|
+
}
|
|
1814
|
+
/** Distinguishes actual provider rejection from blocked-before-provider work. */
|
|
1815
|
+
function providerAdmissionStarted(error, fallback) {
|
|
1816
|
+
return error instanceof AIProviderStoppedError || error instanceof AIProviderDeferredError ? error.providerStarted : fallback;
|
|
1817
|
+
}
|
|
1818
|
+
/** Keeps provider support identifiers without duplicating response bodies. */
|
|
1819
|
+
function providerAdmissionMetadata(error) {
|
|
1820
|
+
return { ...emptyProviderResponseMetadata(), providerRequestId: error instanceof AIProviderStoppedError ? error.requestId : null };
|
|
1821
|
+
}
|
|
@@ -40,7 +40,7 @@ export function openAIProviderError(value) {
|
|
|
40
40
|
*/
|
|
41
41
|
export function isOpenAIQuotaError(value) {
|
|
42
42
|
const code = openAIProviderError(value).code;
|
|
43
|
-
return code !== null && OPENAI_QUOTA_ERROR_CODES.has(code);
|
|
43
|
+
return (code !== null && OPENAI_QUOTA_ERROR_CODES.has(code)) || /\b(?:you have )?no credits remaining\b/i.test(openAIProviderError(value).message);
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Narrows an unknown value to a non-array object record.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Observes SSE framing without rewriting transport bytes or dispatching SDK events.
|
|
3
|
+
* CR, LF and CRLF terminate lines; blank lines dispatch joined data fields. An
|
|
4
|
+
* oversized event is discarded until its blank boundary, allowing later errors
|
|
5
|
+
* to remain observable. Malformed or unfinished input permanently disqualifies
|
|
6
|
+
* this observer from asserting recovery, even if a later event completes.
|
|
7
|
+
*/
|
|
8
|
+
export declare class ProviderSseObserver {
|
|
9
|
+
#private;
|
|
10
|
+
readonly onData: (data: string) => Promise<void>;
|
|
11
|
+
/** Receives complete joined data fields; the caller owns provider semantics. */
|
|
12
|
+
constructor(onData: (data: string) => Promise<void>);
|
|
13
|
+
/** Records malformed JSON/encoding while continuing to observe later events. */
|
|
14
|
+
invalidate(): void;
|
|
15
|
+
/** True only when every observed event is valid and ends at a blank boundary. */
|
|
16
|
+
get complete(): boolean;
|
|
17
|
+
/** Consumes one decoded fragment; CRLF may be split between calls. */
|
|
18
|
+
push(text: string): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/** Maximum retained decoded characters for one SSE line or joined data event. */
|
|
2
|
+
const MAX_EVENT_CHARACTERS = 1_048_576;
|
|
3
|
+
/**
|
|
4
|
+
* Observes SSE framing without rewriting transport bytes or dispatching SDK events.
|
|
5
|
+
* CR, LF and CRLF terminate lines; blank lines dispatch joined data fields. An
|
|
6
|
+
* oversized event is discarded until its blank boundary, allowing later errors
|
|
7
|
+
* to remain observable. Malformed or unfinished input permanently disqualifies
|
|
8
|
+
* this observer from asserting recovery, even if a later event completes.
|
|
9
|
+
*/
|
|
10
|
+
export class ProviderSseObserver {
|
|
11
|
+
onData;
|
|
12
|
+
#line = '';
|
|
13
|
+
#lineNonempty = false;
|
|
14
|
+
#skipLf = false;
|
|
15
|
+
#data = [];
|
|
16
|
+
#dataLength = 0;
|
|
17
|
+
#eventOpen = false;
|
|
18
|
+
#discardEvent = false;
|
|
19
|
+
#valid = true;
|
|
20
|
+
/** Receives complete joined data fields; the caller owns provider semantics. */
|
|
21
|
+
constructor(onData) {
|
|
22
|
+
this.onData = onData;
|
|
23
|
+
}
|
|
24
|
+
/** Records malformed JSON/encoding while continuing to observe later events. */
|
|
25
|
+
invalidate() { this.#valid = false; }
|
|
26
|
+
/** True only when every observed event is valid and ends at a blank boundary. */
|
|
27
|
+
get complete() { return this.#valid && !this.#lineNonempty && !this.#eventOpen && !this.#discardEvent; }
|
|
28
|
+
/** Consumes one decoded fragment; CRLF may be split between calls. */
|
|
29
|
+
async push(text) {
|
|
30
|
+
let start = 0;
|
|
31
|
+
for (let index = 0; index < text.length; index++) {
|
|
32
|
+
const character = text[index];
|
|
33
|
+
if (this.#skipLf) {
|
|
34
|
+
this.#skipLf = false;
|
|
35
|
+
if (character === '\n') {
|
|
36
|
+
start = index + 1;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (character !== '\r' && character !== '\n')
|
|
41
|
+
continue;
|
|
42
|
+
this.#append(text.slice(start, index));
|
|
43
|
+
await this.#finishLine();
|
|
44
|
+
this.#skipLf = character === '\r';
|
|
45
|
+
start = index + 1;
|
|
46
|
+
}
|
|
47
|
+
this.#append(text.slice(start));
|
|
48
|
+
}
|
|
49
|
+
/** Bounds retained line and event storage before adding another fragment. */
|
|
50
|
+
#append(fragment) {
|
|
51
|
+
if (!fragment.length)
|
|
52
|
+
return;
|
|
53
|
+
this.#lineNonempty = true;
|
|
54
|
+
if (this.#discardEvent)
|
|
55
|
+
return;
|
|
56
|
+
if (this.#line.length + this.#dataLength + fragment.length > MAX_EVENT_CHARACTERS) {
|
|
57
|
+
this.invalidate();
|
|
58
|
+
this.#discardEvent = true;
|
|
59
|
+
this.#line = '';
|
|
60
|
+
this.#data = [];
|
|
61
|
+
this.#dataLength = 0;
|
|
62
|
+
}
|
|
63
|
+
else
|
|
64
|
+
this.#line += fragment;
|
|
65
|
+
}
|
|
66
|
+
/** Dispatches only complete events, retaining SSE's joined-data semantics. */
|
|
67
|
+
async #finishLine() {
|
|
68
|
+
const line = this.#line;
|
|
69
|
+
const nonempty = this.#lineNonempty;
|
|
70
|
+
this.#line = '';
|
|
71
|
+
this.#lineNonempty = false;
|
|
72
|
+
if (!nonempty) {
|
|
73
|
+
const data = this.#discardEvent || !this.#data.length ? null : this.#data.join('\n');
|
|
74
|
+
this.#data = [];
|
|
75
|
+
this.#dataLength = 0;
|
|
76
|
+
this.#eventOpen = false;
|
|
77
|
+
this.#discardEvent = false;
|
|
78
|
+
if (data !== null)
|
|
79
|
+
await this.onData(data);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (this.#discardEvent || line.startsWith(':'))
|
|
83
|
+
return;
|
|
84
|
+
this.#eventOpen = true;
|
|
85
|
+
if (line !== 'data' && !line.startsWith('data:'))
|
|
86
|
+
return;
|
|
87
|
+
const data = line === 'data' ? '' : line.slice(line[5] === ' ' ? 6 : 5);
|
|
88
|
+
this.#dataLength += data.length + 1;
|
|
89
|
+
if (this.#dataLength > MAX_EVENT_CHARACTERS) {
|
|
90
|
+
this.invalidate();
|
|
91
|
+
this.#discardEvent = true;
|
|
92
|
+
this.#data = [];
|
|
93
|
+
this.#dataLength = 0;
|
|
94
|
+
}
|
|
95
|
+
else
|
|
96
|
+
this.#data.push(data);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AIServiceTier } from './contracts/AIServiceTier.js';
|
|
2
|
+
/** Validates explicit tiers before provider work; other providers retain their own defaults. */
|
|
3
|
+
export declare function assertAIServiceTier(provider: string, tier: AIServiceTier): void;
|
|
4
|
+
/** Resolves billed tier without treating an unconfirmed Flex/Fast request as discounted usage. */
|
|
5
|
+
export declare function effectiveAIServiceTier(reported: unknown, requested: unknown): string | null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AIConfigurationError } from './AIErrors.js';
|
|
2
|
+
/** Validates explicit tiers before provider work; other providers retain their own defaults. */
|
|
3
|
+
export function assertAIServiceTier(provider, tier) {
|
|
4
|
+
if (!['default', 'flex', 'fast', 'priority'].includes(tier))
|
|
5
|
+
throw new AIConfigurationError('Unsupported AI service tier.');
|
|
6
|
+
if (provider !== 'openai' && tier !== 'default')
|
|
7
|
+
throw new AIConfigurationError('Explicit AI service tiers require the OpenAI provider.');
|
|
8
|
+
}
|
|
9
|
+
/** Resolves billed tier without treating an unconfirmed Flex/Fast request as discounted usage. */
|
|
10
|
+
export function effectiveAIServiceTier(reported, requested) {
|
|
11
|
+
if (typeof reported === 'string' && reported.length > 0)
|
|
12
|
+
return reported;
|
|
13
|
+
return requested === undefined || requested === 'default' ? 'default' : null;
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EmbeddingTextChunk, EmbeddingTextChunkOptions } from './contracts/EmbeddingTextChunk.js';
|
|
2
|
+
/**
|
|
3
|
+
* Keeps fitting documents whole and splits oversized Markdown at natural boundaries.
|
|
4
|
+
* Each passage retains source offsets, bounded shared context, its heading hierarchy
|
|
5
|
+
* and a small overlap. Uses cl100k_base, as used by text-embedding-3-small/large.
|
|
6
|
+
* No provider requests or generated summaries are involved.
|
|
7
|
+
*
|
|
8
|
+
* @param text - Extracted text or Markdown; HTML extraction belongs to the caller.
|
|
9
|
+
* @param options - Context and token budgets for the complete embedding input.
|
|
10
|
+
* @returns Ordered passages whose source ranges cover all of the original text.
|
|
11
|
+
* @example
|
|
12
|
+
* const chunks = chunkEmbeddingText(page.text, { context: `Title: ${page.title}` });
|
|
13
|
+
*/
|
|
14
|
+
export declare function chunkEmbeddingText(text: string, options?: EmbeddingTextChunkOptions): EmbeddingTextChunk[];
|