@sixthsummit/ship-api 0.0.0-stage → 4.1.1
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/CHANGELOG.md +605 -0
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +58 -2
- package/cli/commands/add.js +537 -0
- package/cli/commands/contract.js +2 -0
- package/cli/commands/create.js +250 -0
- package/cli/commands/dev.js +227 -0
- package/cli/commands/doctor.js +286 -0
- package/cli/commands/generate.js +937 -0
- package/cli/commands/graph.js +2 -0
- package/cli/commands/migrate-orm.js +1171 -0
- package/cli/commands/update.js +37 -0
- package/cli/ship-api.js +285 -0
- package/lib/app/auth/AuthProvider.d.ts +91 -0
- package/lib/app/auth/AuthProvider.d.ts.map +1 -0
- package/lib/app/auth/AuthProvider.js +13 -0
- package/lib/app/auth/AuthProvider.js.map +1 -0
- package/lib/app/auth/adapters/firebase.d.ts +79 -0
- package/lib/app/auth/adapters/firebase.d.ts.map +1 -0
- package/lib/app/auth/adapters/firebase.js +122 -0
- package/lib/app/auth/adapters/firebase.js.map +1 -0
- package/lib/app/auth/adapters/jwtCookie.d.ts +55 -0
- package/lib/app/auth/adapters/jwtCookie.d.ts.map +1 -0
- package/lib/app/auth/adapters/jwtCookie.js +120 -0
- package/lib/app/auth/adapters/jwtCookie.js.map +1 -0
- package/lib/app/auth/index.d.ts +8 -0
- package/lib/app/auth/index.d.ts.map +1 -0
- package/lib/app/auth/index.js +7 -0
- package/lib/app/auth/index.js.map +1 -0
- package/lib/app/auth/jwt/AuthFlow.d.ts +94 -0
- package/lib/app/auth/jwt/AuthFlow.d.ts.map +1 -0
- package/lib/app/auth/jwt/AuthFlow.js +127 -0
- package/lib/app/auth/jwt/AuthFlow.js.map +1 -0
- package/lib/app/auth/jwt/DrizzleRefreshTokenStore.d.ts +80 -0
- package/lib/app/auth/jwt/DrizzleRefreshTokenStore.d.ts.map +1 -0
- package/lib/app/auth/jwt/DrizzleRefreshTokenStore.js +134 -0
- package/lib/app/auth/jwt/DrizzleRefreshTokenStore.js.map +1 -0
- package/lib/app/auth/jwt/JwtSigner.d.ts +123 -0
- package/lib/app/auth/jwt/JwtSigner.d.ts.map +1 -0
- package/lib/app/auth/jwt/JwtSigner.js +118 -0
- package/lib/app/auth/jwt/JwtSigner.js.map +1 -0
- package/lib/app/auth/jwt/RefreshTokenStore.d.ts +97 -0
- package/lib/app/auth/jwt/RefreshTokenStore.d.ts.map +1 -0
- package/lib/app/auth/jwt/RefreshTokenStore.js +122 -0
- package/lib/app/auth/jwt/RefreshTokenStore.js.map +1 -0
- package/lib/app/auth/permissionDefinition.d.ts +20 -0
- package/lib/app/auth/permissionDefinition.d.ts.map +1 -0
- package/lib/app/auth/permissionDefinition.js +2 -0
- package/lib/app/auth/permissionDefinition.js.map +1 -0
- package/lib/app/context/RequestContext.d.ts +59 -0
- package/lib/app/context/RequestContext.d.ts.map +1 -0
- package/lib/app/context/RequestContext.js +25 -0
- package/lib/app/context/RequestContext.js.map +1 -0
- package/lib/app/db/QueryOptions.d.ts +82 -0
- package/lib/app/db/QueryOptions.d.ts.map +1 -0
- package/lib/app/db/QueryOptions.js +37 -0
- package/lib/app/db/QueryOptions.js.map +1 -0
- package/lib/app/db/baseFields.d.ts +60 -0
- package/lib/app/db/baseFields.d.ts.map +1 -0
- package/lib/app/db/baseFields.js +54 -0
- package/lib/app/db/baseFields.js.map +1 -0
- package/lib/app/db/buildWhere.d.ts +22 -0
- package/lib/app/db/buildWhere.d.ts.map +1 -0
- package/lib/app/db/buildWhere.js +75 -0
- package/lib/app/db/buildWhere.js.map +1 -0
- package/lib/app/db/client.d.ts +111 -0
- package/lib/app/db/client.d.ts.map +1 -0
- package/lib/app/db/client.js +99 -0
- package/lib/app/db/client.js.map +1 -0
- package/lib/app/db/tenantScoped.d.ts +59 -0
- package/lib/app/db/tenantScoped.d.ts.map +1 -0
- package/lib/app/db/tenantScoped.js +54 -0
- package/lib/app/db/tenantScoped.js.map +1 -0
- package/lib/app/db/transaction.d.ts +58 -0
- package/lib/app/db/transaction.d.ts.map +1 -0
- package/lib/app/db/transaction.js +106 -0
- package/lib/app/db/transaction.js.map +1 -0
- package/lib/app/health/HealthCheck.d.ts +121 -0
- package/lib/app/health/HealthCheck.d.ts.map +1 -0
- package/lib/app/health/HealthCheck.js +151 -0
- package/lib/app/health/HealthCheck.js.map +1 -0
- package/lib/app/health/redisHealthCheck.d.ts +14 -0
- package/lib/app/health/redisHealthCheck.d.ts.map +1 -0
- package/lib/app/health/redisHealthCheck.js +21 -0
- package/lib/app/health/redisHealthCheck.js.map +1 -0
- package/lib/app/lifecycle/Bootstrapper.d.ts +92 -0
- package/lib/app/lifecycle/Bootstrapper.d.ts.map +1 -0
- package/lib/app/lifecycle/Bootstrapper.js +99 -0
- package/lib/app/lifecycle/Bootstrapper.js.map +1 -0
- package/lib/app/registry/routes.d.ts +89 -0
- package/lib/app/registry/routes.d.ts.map +1 -0
- package/lib/app/registry/routes.js +72 -0
- package/lib/app/registry/routes.js.map +1 -0
- package/lib/app/services/DrizzleEntityService.d.ts +123 -0
- package/lib/app/services/DrizzleEntityService.d.ts.map +1 -0
- package/lib/app/services/DrizzleEntityService.js +342 -0
- package/lib/app/services/DrizzleEntityService.js.map +1 -0
- package/lib/app/services/RedisProvider.d.ts +52 -0
- package/lib/app/services/RedisProvider.d.ts.map +1 -0
- package/lib/app/services/RedisProvider.js +75 -0
- package/lib/app/services/RedisProvider.js.map +1 -0
- package/lib/app/services/cache/Cache.d.ts +70 -0
- package/lib/app/services/cache/Cache.d.ts.map +1 -0
- package/lib/app/services/cache/Cache.js +18 -0
- package/lib/app/services/cache/Cache.js.map +1 -0
- package/lib/app/services/cache/InMemoryCache.d.ts +35 -0
- package/lib/app/services/cache/InMemoryCache.d.ts.map +1 -0
- package/lib/app/services/cache/InMemoryCache.js +76 -0
- package/lib/app/services/cache/InMemoryCache.js.map +1 -0
- package/lib/app/services/cache/RedisCache.d.ts +99 -0
- package/lib/app/services/cache/RedisCache.d.ts.map +1 -0
- package/lib/app/services/cache/RedisCache.js +150 -0
- package/lib/app/services/cache/RedisCache.js.map +1 -0
- package/lib/app/services/cache/cached.d.ts +30 -0
- package/lib/app/services/cache/cached.d.ts.map +1 -0
- package/lib/app/services/cache/cached.js +38 -0
- package/lib/app/services/cache/cached.js.map +1 -0
- package/lib/app/services/cache/index.d.ts +5 -0
- package/lib/app/services/cache/index.d.ts.map +1 -0
- package/lib/app/services/cache/index.js +4 -0
- package/lib/app/services/cache/index.js.map +1 -0
- package/lib/app/services/cron/defineCron.d.ts +57 -0
- package/lib/app/services/cron/defineCron.d.ts.map +1 -0
- package/lib/app/services/cron/defineCron.js +15 -0
- package/lib/app/services/cron/defineCron.js.map +1 -0
- package/lib/app/services/cron/index.d.ts +2 -0
- package/lib/app/services/cron/index.d.ts.map +1 -0
- package/lib/app/services/cron/index.js +2 -0
- package/lib/app/services/cron/index.js.map +1 -0
- package/lib/app/services/hooks/LifecycleHooks.d.ts +61 -0
- package/lib/app/services/hooks/LifecycleHooks.d.ts.map +1 -0
- package/lib/app/services/hooks/LifecycleHooks.js +85 -0
- package/lib/app/services/hooks/LifecycleHooks.js.map +1 -0
- package/lib/app/services/jobs/JobsManager.d.ts +67 -0
- package/lib/app/services/jobs/JobsManager.d.ts.map +1 -0
- package/lib/app/services/jobs/JobsManager.js +121 -0
- package/lib/app/services/jobs/JobsManager.js.map +1 -0
- package/lib/app/services/jobs/bullBoard.d.ts +72 -0
- package/lib/app/services/jobs/bullBoard.d.ts.map +1 -0
- package/lib/app/services/jobs/bullBoard.js +96 -0
- package/lib/app/services/jobs/bullBoard.js.map +1 -0
- package/lib/app/services/jobs/defineJob.d.ts +81 -0
- package/lib/app/services/jobs/defineJob.d.ts.map +1 -0
- package/lib/app/services/jobs/defineJob.js +49 -0
- package/lib/app/services/jobs/defineJob.js.map +1 -0
- package/lib/app/services/jobs/index.d.ts +4 -0
- package/lib/app/services/jobs/index.d.ts.map +1 -0
- package/lib/app/services/jobs/index.js +4 -0
- package/lib/app/services/jobs/index.js.map +1 -0
- package/lib/app/services/rateLimit/InMemorySlidingWindowBackend.d.ts +23 -0
- package/lib/app/services/rateLimit/InMemorySlidingWindowBackend.d.ts.map +1 -0
- package/lib/app/services/rateLimit/InMemorySlidingWindowBackend.js +51 -0
- package/lib/app/services/rateLimit/InMemorySlidingWindowBackend.js.map +1 -0
- package/lib/app/services/rateLimit/RateLimitBackend.d.ts +41 -0
- package/lib/app/services/rateLimit/RateLimitBackend.d.ts.map +1 -0
- package/lib/app/services/rateLimit/RateLimitBackend.js +19 -0
- package/lib/app/services/rateLimit/RateLimitBackend.js.map +1 -0
- package/lib/app/services/rateLimit/RedisSlidingWindowBackend.d.ts +16 -0
- package/lib/app/services/rateLimit/RedisSlidingWindowBackend.d.ts.map +1 -0
- package/lib/app/services/rateLimit/RedisSlidingWindowBackend.js +80 -0
- package/lib/app/services/rateLimit/RedisSlidingWindowBackend.js.map +1 -0
- package/lib/app/services/rateLimit/index.d.ts +4 -0
- package/lib/app/services/rateLimit/index.d.ts.map +1 -0
- package/lib/app/services/rateLimit/index.js +3 -0
- package/lib/app/services/rateLimit/index.js.map +1 -0
- package/lib/app/services/storage/DiskStorageAdapter.d.ts +62 -0
- package/lib/app/services/storage/DiskStorageAdapter.d.ts.map +1 -0
- package/lib/app/services/storage/DiskStorageAdapter.js +295 -0
- package/lib/app/services/storage/DiskStorageAdapter.js.map +1 -0
- package/lib/app/services/storage/GCSStorageAdapter.d.ts +44 -0
- package/lib/app/services/storage/GCSStorageAdapter.d.ts.map +1 -0
- package/lib/app/services/storage/GCSStorageAdapter.js +133 -0
- package/lib/app/services/storage/GCSStorageAdapter.js.map +1 -0
- package/lib/app/services/storage/S3StorageAdapter.d.ts +57 -0
- package/lib/app/services/storage/S3StorageAdapter.d.ts.map +1 -0
- package/lib/app/services/storage/S3StorageAdapter.js +162 -0
- package/lib/app/services/storage/S3StorageAdapter.js.map +1 -0
- package/lib/app/services/storage/StorageService.d.ts +88 -0
- package/lib/app/services/storage/StorageService.d.ts.map +1 -0
- package/lib/app/services/storage/StorageService.js +18 -0
- package/lib/app/services/storage/StorageService.js.map +1 -0
- package/lib/app/services/storage/index.d.ts +5 -0
- package/lib/app/services/storage/index.d.ts.map +1 -0
- package/lib/app/services/storage/index.js +4 -0
- package/lib/app/services/storage/index.js.map +1 -0
- package/lib/app/utils/ContextValidators.d.ts +46 -0
- package/lib/app/utils/ContextValidators.d.ts.map +1 -0
- package/lib/app/utils/ContextValidators.js +81 -0
- package/lib/app/utils/ContextValidators.js.map +1 -0
- package/lib/app/utils/defineEnv.d.ts +80 -0
- package/lib/app/utils/defineEnv.d.ts.map +1 -0
- package/lib/app/utils/defineEnv.js +95 -0
- package/lib/app/utils/defineEnv.js.map +1 -0
- package/lib/client.d.ts +139 -0
- package/lib/client.d.ts.map +1 -0
- package/lib/client.js +98 -0
- package/lib/client.js.map +1 -0
- package/lib/contract/application.d.ts +3 -0
- package/lib/contract/application.d.ts.map +1 -0
- package/lib/contract/application.js +2 -0
- package/lib/contract/application.js.map +1 -0
- package/lib/contract/client.d.ts +3 -0
- package/lib/contract/client.d.ts.map +1 -0
- package/lib/contract/client.js +3 -0
- package/lib/contract/client.js.map +1 -0
- package/lib/contract/index.d.ts +3 -0
- package/lib/contract/index.d.ts.map +1 -0
- package/lib/contract/index.js +3 -0
- package/lib/contract/index.js.map +1 -0
- package/lib/contract/manifest.d.ts +3 -0
- package/lib/contract/manifest.d.ts.map +1 -0
- package/lib/contract/manifest.js +2 -0
- package/lib/contract/manifest.js.map +1 -0
- package/lib/contract/openapi.d.ts +3 -0
- package/lib/contract/openapi.d.ts.map +1 -0
- package/lib/contract/openapi.js +3 -0
- package/lib/contract/openapi.js.map +1 -0
- package/lib/contract/project.d.ts +3 -0
- package/lib/contract/project.d.ts.map +1 -0
- package/lib/contract/project.js +3 -0
- package/lib/contract/project.js.map +1 -0
- package/lib/contract/schema.d.ts +3 -0
- package/lib/contract/schema.d.ts.map +1 -0
- package/lib/contract/schema.js +3 -0
- package/lib/contract/schema.js.map +1 -0
- package/lib/drizzle/index.d.ts +3 -0
- package/lib/drizzle/index.d.ts.map +1 -0
- package/lib/drizzle/index.js +3 -0
- package/lib/drizzle/index.js.map +1 -0
- package/lib/hono/auth.d.ts +6 -0
- package/lib/hono/auth.d.ts.map +1 -0
- package/lib/hono/auth.js +54 -0
- package/lib/hono/auth.js.map +1 -0
- package/lib/hono/index.d.ts +4 -0
- package/lib/hono/index.d.ts.map +1 -0
- package/lib/hono/index.js +4 -0
- package/lib/hono/index.js.map +1 -0
- package/lib/http/app/createApp.d.ts +132 -0
- package/lib/http/app/createApp.d.ts.map +1 -0
- package/lib/http/app/createApp.js +215 -0
- package/lib/http/app/createApp.js.map +1 -0
- package/lib/http/app/devRoutes.d.ts +31 -0
- package/lib/http/app/devRoutes.d.ts.map +1 -0
- package/lib/http/app/devRoutes.js +145 -0
- package/lib/http/app/devRoutes.js.map +1 -0
- package/lib/http/app/errors.d.ts +45 -0
- package/lib/http/app/errors.d.ts.map +1 -0
- package/lib/http/app/errors.js +93 -0
- package/lib/http/app/errors.js.map +1 -0
- package/lib/http/app/health.d.ts +66 -0
- package/lib/http/app/health.d.ts.map +1 -0
- package/lib/http/app/health.js +104 -0
- package/lib/http/app/health.js.map +1 -0
- package/lib/http/errors/AppError.d.ts +10 -0
- package/lib/http/errors/AppError.d.ts.map +1 -0
- package/lib/http/errors/AppError.js +19 -0
- package/lib/http/errors/AppError.js.map +1 -0
- package/lib/http/errors/AuthenticationError.d.ts +8 -0
- package/lib/http/errors/AuthenticationError.d.ts.map +1 -0
- package/lib/http/errors/AuthenticationError.js +10 -0
- package/lib/http/errors/AuthenticationError.js.map +1 -0
- package/lib/http/errors/AuthorizationError.d.ts +8 -0
- package/lib/http/errors/AuthorizationError.d.ts.map +1 -0
- package/lib/http/errors/AuthorizationError.js +10 -0
- package/lib/http/errors/AuthorizationError.js.map +1 -0
- package/lib/http/errors/BadRequestError.d.ts +8 -0
- package/lib/http/errors/BadRequestError.d.ts.map +1 -0
- package/lib/http/errors/BadRequestError.js +10 -0
- package/lib/http/errors/BadRequestError.js.map +1 -0
- package/lib/http/errors/ConflictError.d.ts +8 -0
- package/lib/http/errors/ConflictError.d.ts.map +1 -0
- package/lib/http/errors/ConflictError.js +10 -0
- package/lib/http/errors/ConflictError.js.map +1 -0
- package/lib/http/errors/InternalServerError.d.ts +8 -0
- package/lib/http/errors/InternalServerError.d.ts.map +1 -0
- package/lib/http/errors/InternalServerError.js +10 -0
- package/lib/http/errors/InternalServerError.js.map +1 -0
- package/lib/http/errors/NotFoundError.d.ts +8 -0
- package/lib/http/errors/NotFoundError.d.ts.map +1 -0
- package/lib/http/errors/NotFoundError.js +10 -0
- package/lib/http/errors/NotFoundError.js.map +1 -0
- package/lib/http/errors/RateLimitError.d.ts +13 -0
- package/lib/http/errors/RateLimitError.d.ts.map +1 -0
- package/lib/http/errors/RateLimitError.js +16 -0
- package/lib/http/errors/RateLimitError.js.map +1 -0
- package/lib/http/errors/ServiceUnavailableError.d.ts +8 -0
- package/lib/http/errors/ServiceUnavailableError.d.ts.map +1 -0
- package/lib/http/errors/ServiceUnavailableError.js +10 -0
- package/lib/http/errors/ServiceUnavailableError.js.map +1 -0
- package/lib/http/errors/ValidationError.d.ts +9 -0
- package/lib/http/errors/ValidationError.d.ts.map +1 -0
- package/lib/http/errors/ValidationError.js +12 -0
- package/lib/http/errors/ValidationError.js.map +1 -0
- package/lib/http/errors/index.d.ts +11 -0
- package/lib/http/errors/index.d.ts.map +1 -0
- package/lib/http/errors/index.js +12 -0
- package/lib/http/errors/index.js.map +1 -0
- package/lib/http/middleware/apiKey.d.ts +9 -0
- package/lib/http/middleware/apiKey.d.ts.map +1 -0
- package/lib/http/middleware/apiKey.js +71 -0
- package/lib/http/middleware/apiKey.js.map +1 -0
- package/lib/http/middleware/csrf.d.ts +63 -0
- package/lib/http/middleware/csrf.d.ts.map +1 -0
- package/lib/http/middleware/csrf.js +169 -0
- package/lib/http/middleware/csrf.js.map +1 -0
- package/lib/http/middleware/httpLogger.d.ts +39 -0
- package/lib/http/middleware/httpLogger.d.ts.map +1 -0
- package/lib/http/middleware/httpLogger.js +118 -0
- package/lib/http/middleware/httpLogger.js.map +1 -0
- package/lib/http/middleware/idempotency.d.ts +48 -0
- package/lib/http/middleware/idempotency.d.ts.map +1 -0
- package/lib/http/middleware/idempotency.js +180 -0
- package/lib/http/middleware/idempotency.js.map +1 -0
- package/lib/http/middleware/rateLimit.d.ts +105 -0
- package/lib/http/middleware/rateLimit.d.ts.map +1 -0
- package/lib/http/middleware/rateLimit.js +84 -0
- package/lib/http/middleware/rateLimit.js.map +1 -0
- package/lib/http/middleware/requireAuth.d.ts +58 -0
- package/lib/http/middleware/requireAuth.d.ts.map +1 -0
- package/lib/http/middleware/requireAuth.js +63 -0
- package/lib/http/middleware/requireAuth.js.map +1 -0
- package/lib/http/middleware/requirePermission.d.ts +23 -0
- package/lib/http/middleware/requirePermission.d.ts.map +1 -0
- package/lib/http/middleware/requirePermission.js +50 -0
- package/lib/http/middleware/requirePermission.js.map +1 -0
- package/lib/http/middleware/resolveTenant.d.ts +41 -0
- package/lib/http/middleware/resolveTenant.d.ts.map +1 -0
- package/lib/http/middleware/resolveTenant.js +17 -0
- package/lib/http/middleware/resolveTenant.js.map +1 -0
- package/lib/http/streaming/sse.d.ts +31 -0
- package/lib/http/streaming/sse.d.ts.map +1 -0
- package/lib/http/streaming/sse.js +11 -0
- package/lib/http/streaming/sse.js.map +1 -0
- package/lib/http/streaming/streamLLM.d.ts +94 -0
- package/lib/http/streaming/streamLLM.d.ts.map +1 -0
- package/lib/http/streaming/streamLLM.js +94 -0
- package/lib/http/streaming/streamLLM.js.map +1 -0
- package/lib/http/utils/authCookies.d.ts +74 -0
- package/lib/http/utils/authCookies.d.ts.map +1 -0
- package/lib/http/utils/authCookies.js +102 -0
- package/lib/http/utils/authCookies.js.map +1 -0
- package/lib/http/utils/clientIp.d.ts +85 -0
- package/lib/http/utils/clientIp.d.ts.map +1 -0
- package/lib/http/utils/clientIp.js +249 -0
- package/lib/http/utils/clientIp.js.map +1 -0
- package/lib/http/utils/index.d.ts +4 -0
- package/lib/http/utils/index.d.ts.map +1 -0
- package/lib/http/utils/index.js +5 -0
- package/lib/http/utils/index.js.map +1 -0
- package/lib/http/utils/metaTags.d.ts +49 -0
- package/lib/http/utils/metaTags.d.ts.map +1 -0
- package/lib/http/utils/metaTags.js +32 -0
- package/lib/http/utils/metaTags.js.map +1 -0
- package/lib/http/utils/publicSuffix.d.ts +34 -0
- package/lib/http/utils/publicSuffix.d.ts.map +1 -0
- package/lib/http/utils/publicSuffix.js +276 -0
- package/lib/http/utils/publicSuffix.js.map +1 -0
- package/lib/http/utils/uploads.d.ts +92 -0
- package/lib/http/utils/uploads.d.ts.map +1 -0
- package/lib/http/utils/uploads.js +126 -0
- package/lib/http/utils/uploads.js.map +1 -0
- package/lib/http/utils/validate.d.ts +41 -0
- package/lib/http/utils/validate.d.ts.map +1 -0
- package/lib/http/utils/validate.js +55 -0
- package/lib/http/utils/validate.js.map +1 -0
- package/lib/http/utils/webhooks.d.ts +63 -0
- package/lib/http/utils/webhooks.d.ts.map +1 -0
- package/lib/http/utils/webhooks.js +133 -0
- package/lib/http/utils/webhooks.js.map +1 -0
- package/lib/http/ws/auth.d.ts +46 -0
- package/lib/http/ws/auth.d.ts.map +1 -0
- package/lib/http/ws/auth.js +123 -0
- package/lib/http/ws/auth.js.map +1 -0
- package/lib/index.d.ts +80 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +61 -0
- package/lib/index.js.map +1 -0
- package/lib/kernel/index.d.ts +3 -0
- package/lib/kernel/index.d.ts.map +1 -0
- package/lib/kernel/index.js +3 -0
- package/lib/kernel/index.js.map +1 -0
- package/lib/libraries/.gitkeep +0 -0
- package/lib/libraries/ai/CHANGELOG.md +17 -0
- package/lib/libraries/ai/README.md +164 -0
- package/lib/libraries/ai/adapters/AnthropicAdapter.ts +382 -0
- package/lib/libraries/ai/adapters/LLMProvider.ts +76 -0
- package/lib/libraries/ai/adapters/OpenAIAdapter.ts +466 -0
- package/lib/libraries/ai/experimental/index.ts +20 -0
- package/lib/libraries/ai/index.ts +128 -0
- package/lib/libraries/ai/library.json +19 -0
- package/lib/libraries/ai/loop/agentLoop.ts +377 -0
- package/lib/libraries/ai/prompts/definePrompt.ts +0 -0
- package/lib/libraries/ai/rag/retrieveAndAnswer.ts +177 -0
- package/lib/libraries/ai/streaming/streamLLM.ts +94 -0
- package/lib/libraries/ai/structured/completeStructured.ts +143 -0
- package/lib/libraries/ai/tokens/Tokenizer.ts +216 -0
- package/lib/libraries/ai/tools/defineTool.ts +208 -0
- package/lib/libraries/ai/types.ts +260 -0
- package/lib/libraries/chunking/ChunkingService.ts +103 -0
- package/lib/libraries/chunking/README.md +104 -0
- package/lib/libraries/chunking/docs/api-reference.md +549 -0
- package/lib/libraries/chunking/docs/best-practices.md +321 -0
- package/lib/libraries/chunking/docs/code-organization.md +538 -0
- package/lib/libraries/chunking/docs/configuration.md +418 -0
- package/lib/libraries/chunking/docs/index.md +236 -0
- package/lib/libraries/chunking/docs/quick-start.md +298 -0
- package/lib/libraries/chunking/docs/strategies.md +407 -0
- package/lib/libraries/chunking/docs/summary.md +258 -0
- package/lib/libraries/chunking/docs/troubleshooting.md +527 -0
- package/lib/libraries/chunking/index.ts +17 -0
- package/lib/libraries/chunking/library.json +16 -0
- package/lib/libraries/chunking/strategies/ChunkingStrategy.ts +13 -0
- package/lib/libraries/chunking/strategies/FixedLengthChunkingStrategy.ts +88 -0
- package/lib/libraries/chunking/strategies/SemanticChunkingStrategy.ts +389 -0
- package/lib/libraries/chunking/strategies/SlidingWindowChunkingStrategy.ts +258 -0
- package/lib/libraries/chunking/types/index.ts +63 -0
- package/lib/libraries/email/EmailService.ts +591 -0
- package/lib/libraries/email/README.md +777 -0
- package/lib/libraries/email/TemplateRegistry.ts +148 -0
- package/lib/libraries/email/TemplateRenderer.ts +273 -0
- package/lib/libraries/email/index.ts +4 -0
- package/lib/libraries/email/initializeEmailService.ts +60 -0
- package/lib/libraries/email/library.json +12 -0
- package/lib/libraries/email/types/EmailAddress.ts +4 -0
- package/lib/libraries/email/types/EmailAttachment.ts +11 -0
- package/lib/libraries/email/types/EmailTag.ts +9 -0
- package/lib/libraries/email/types/EmailTemplate.ts +124 -0
- package/lib/libraries/email/types/SendBatchEmailsOptions.ts +9 -0
- package/lib/libraries/email/types/SendBatchEmailsResult.ts +9 -0
- package/lib/libraries/email/types/SendEmailOptions.ts +33 -0
- package/lib/libraries/email/types/SendEmailResult.ts +9 -0
- package/lib/libraries/email/types/index.ts +8 -0
- package/lib/libraries/weaviate/QueryBuilder.ts +292 -0
- package/lib/libraries/weaviate/README.md +284 -0
- package/lib/libraries/weaviate/Weaviate.ts +1419 -0
- package/lib/libraries/weaviate/WeaviateConfig.ts +268 -0
- package/lib/libraries/weaviate/docs/.gitkeep +1 -0
- package/lib/libraries/weaviate/docs/API_REFERENCE.md +1067 -0
- package/lib/libraries/weaviate/docs/CONFIGURATION.md +320 -0
- package/lib/libraries/weaviate/docs/ERROR_HANDLING.md +271 -0
- package/lib/libraries/weaviate/docs/MULTI_TENANCY.md +348 -0
- package/lib/libraries/weaviate/docs/PERFORMANCE.md +602 -0
- package/lib/libraries/weaviate/docs/PLUGIN_DEVELOPMENT.md +547 -0
- package/lib/libraries/weaviate/docs/QUICK_START.md +443 -0
- package/lib/libraries/weaviate/docs/README.md +269 -0
- package/lib/libraries/weaviate/docs/SCHEMA_GUIDE.md +630 -0
- package/lib/libraries/weaviate/docs/WEAVIATE_README.md +520 -0
- package/lib/libraries/weaviate/errors/WeaviateError.ts +92 -0
- package/lib/libraries/weaviate/errors/index.ts +1 -0
- package/lib/libraries/weaviate/index.ts +35 -0
- package/lib/libraries/weaviate/library.json +26 -0
- package/lib/libraries/weaviate/plugins/index.ts +2 -0
- package/lib/libraries/weaviate/schemas/SchemaRegistry.ts +284 -0
- package/lib/libraries/weaviate/schemas/index.ts +2 -0
- package/lib/libraries/weaviate/types/BM25SearchOptions.ts +9 -0
- package/lib/libraries/weaviate/types/ExtendedWeaviateClient.ts +7 -0
- package/lib/libraries/weaviate/types/HybridSearchOptions.ts +11 -0
- package/lib/libraries/weaviate/types/MultiTenancy.ts +81 -0
- package/lib/libraries/weaviate/types/Plugin.ts +17 -0
- package/lib/libraries/weaviate/types/SchemaConfig.ts +25 -0
- package/lib/libraries/weaviate/types/SearchMethod.ts +8 -0
- package/lib/libraries/weaviate/types/SearchOptions.ts +16 -0
- package/lib/libraries/weaviate/types/SearchRequest.ts +19 -0
- package/lib/libraries/weaviate/types/SearchResult.ts +16 -0
- package/lib/libraries/weaviate/types/SearchableField.ts +87 -0
- package/lib/libraries/weaviate/types/VectorIndexConfig.ts +19 -0
- package/lib/libraries/weaviate/types/VectorSearchOptions.ts +18 -0
- package/lib/libraries/weaviate/types/VectorizerConfig.ts +8 -0
- package/lib/libraries/weaviate/types/index.ts +29 -0
- package/lib/libraries/weaviate/utils/TenantContextManager.ts +94 -0
- package/lib/modules/accounts/index.ts +47 -0
- package/lib/modules/accounts/module.json +15 -0
- package/lib/modules/accounts/permissions.ts +33 -0
- package/lib/modules/accounts/routes.ts +200 -0
- package/lib/modules/accounts/schema.ts +219 -0
- package/lib/modules/accounts/services/AccountService.ts +44 -0
- package/lib/modules/accounts/validation/schemas.ts +70 -0
- package/lib/modules/oauth/adapters/GmailOAuthAdapter.ts +35 -0
- package/lib/modules/oauth/adapters/GoogleOAuthAdapter.ts +278 -0
- package/lib/modules/oauth/adapters/MicrosoftOAuthAdapter.ts +269 -0
- package/lib/modules/oauth/adapters/MockOAuthAdapter.ts +138 -0
- package/lib/modules/oauth/adapters/OAuthAdapter.ts +211 -0
- package/lib/modules/oauth/adapters/OAuthAdapterRegistry.ts +77 -0
- package/lib/modules/oauth/adapters/OutlookOAuthAdapter.ts +38 -0
- package/lib/modules/oauth/index.ts +79 -0
- package/lib/modules/oauth/module.json +23 -0
- package/lib/modules/oauth/permissions.ts +39 -0
- package/lib/modules/oauth/routes.ts +497 -0
- package/lib/modules/oauth/schema.ts +127 -0
- package/lib/modules/oauth/services/OAuthConnectionService.ts +147 -0
- package/lib/modules/oauth/state.ts +234 -0
- package/lib/modules/oauth/stateJtiCache.ts +85 -0
- package/lib/modules/oauth/verifyIdToken.ts +178 -0
- package/lib/modules/outbox/EventEmitter.ts +106 -0
- package/lib/modules/outbox/OutboxDrainer.ts +163 -0
- package/lib/modules/outbox/handlers/EventHandler.ts +115 -0
- package/lib/modules/outbox/index.ts +43 -0
- package/lib/modules/outbox/module.json +15 -0
- package/lib/modules/outbox/permissions.ts +36 -0
- package/lib/modules/outbox/routes.ts +219 -0
- package/lib/modules/outbox/schema.ts +207 -0
- package/lib/modules/outbox/services/OutboxEventService.ts +223 -0
- package/lib/observability/logger.d.ts +30 -0
- package/lib/observability/logger.d.ts.map +1 -0
- package/lib/observability/logger.js +110 -0
- package/lib/observability/logger.js.map +1 -0
- package/lib/observability/metrics.d.ts +3 -0
- package/lib/observability/metrics.d.ts.map +1 -0
- package/lib/observability/metrics.js +3 -0
- package/lib/observability/metrics.js.map +1 -0
- package/lib/observability/otel.d.ts +3 -0
- package/lib/observability/otel.d.ts.map +1 -0
- package/lib/observability/otel.js +3 -0
- package/lib/observability/otel.js.map +1 -0
- package/lib/observability/safeErrorFields.d.ts +14 -0
- package/lib/observability/safeErrorFields.d.ts.map +1 -0
- package/lib/observability/safeErrorFields.js +14 -0
- package/lib/observability/safeErrorFields.js.map +1 -0
- package/lib/observability/tracing.d.ts +3 -0
- package/lib/observability/tracing.d.ts.map +1 -0
- package/lib/observability/tracing.js +3 -0
- package/lib/observability/tracing.js.map +1 -0
- package/lib/package.json +4 -0
- package/lib/testing.d.ts +3 -0
- package/lib/testing.d.ts.map +1 -0
- package/lib/testing.js +3 -0
- package/lib/testing.js.map +1 -0
- package/package.json +193 -3
- package/templates/default/.claude/agents/qa-brief.md +106 -0
- package/templates/default/.claude/commands/sherpa-brief.md +23 -0
- package/templates/default/.cursor/rules/api.mdc +80 -0
- package/templates/default/.cursor/rules/commits.mdc +88 -0
- package/templates/default/.cursor/rules/security.mdc +92 -0
- package/templates/default/.cursor/rules/testing.mdc +123 -0
- package/templates/default/.cursor/rules/typescript.mdc +86 -0
- package/templates/default/.env.example +79 -0
- package/templates/default/.github/workflows/sherpa-brief.yml +180 -0
- package/templates/default/AGENTS.md +166 -0
- package/templates/default/CLAUDE.md +17 -0
- package/templates/default/README.md +97 -0
- package/templates/default/_gitignore +27 -0
- package/templates/default/docker-compose.yml +83 -0
- package/templates/default/drizzle.config.ts +34 -0
- package/templates/default/eslint.config.mjs +106 -0
- package/templates/default/package.json +57 -0
- package/templates/default/ship-api.config.ts +21 -0
- package/templates/default/src/client.ts +34 -0
- package/templates/default/src/config/modules.ts +23 -0
- package/templates/default/src/db/schema.ts +26 -0
- package/templates/default/src/environment.ts +81 -0
- package/templates/default/src/index.test.ts +53 -0
- package/templates/default/src/index.ts +11 -0
- package/templates/default/src/instrumentation.ts +13 -0
- package/templates/default/src/server.ts +90 -0
- package/templates/default/tsconfig.json +24 -0
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
# Chunking Library - Troubleshooting
|
|
2
|
+
|
|
3
|
+
Common issues, their causes, and solutions when working with the chunking library.
|
|
4
|
+
|
|
5
|
+
## 🔍 Quick Diagnosis
|
|
6
|
+
|
|
7
|
+
| Symptom | Likely Cause | Jump To |
|
|
8
|
+
|---------|-------------|---------|
|
|
9
|
+
| Empty array returned | Empty/whitespace input | [Empty Chunks](#empty-chunks) |
|
|
10
|
+
| Chunks too small | minTokens too low or merging disabled | [Chunk Size Issues](#chunk-size-issues) |
|
|
11
|
+
| Chunks too large | maxTokens set too high | [Chunk Size Issues](#chunk-size-issues) |
|
|
12
|
+
| Token count doesn't match expectations | Wrong encoding | [Token Count Mismatch](#token-count-mismatch) |
|
|
13
|
+
| Memory grows over time | Missing `dispose()` calls | [Memory Usage](#memory-usage) |
|
|
14
|
+
| Code blocks split across chunks | Using Fixed Length strategy | [Code Block Splitting](#code-block-splitting) |
|
|
15
|
+
| Sentences split mid-word | Using Fixed Length strategy | [Mid-Word Splits](#mid-word-splits) |
|
|
16
|
+
| Slow processing | Large documents or wrong strategy | [Performance Issues](#performance-issues) |
|
|
17
|
+
| Constructor throws error | Invalid options | [Configuration Errors](#configuration-errors) |
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Empty Chunks
|
|
22
|
+
|
|
23
|
+
### Symptom
|
|
24
|
+
`chunk()` returns an empty array `[]`.
|
|
25
|
+
|
|
26
|
+
### Cause
|
|
27
|
+
The input text is empty, null, undefined, or contains only whitespace.
|
|
28
|
+
|
|
29
|
+
### Solution
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// ChunkingService handles this automatically
|
|
33
|
+
const chunks = await service.chunk(""); // → []
|
|
34
|
+
const chunks = await service.chunk(" "); // → []
|
|
35
|
+
const chunks = await service.chunk("\n\n"); // → []
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Check your input before chunking:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
if (!text || text.trim().length === 0) {
|
|
42
|
+
console.warn("Skipping empty document");
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const chunks = await service.chunk(text);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Chunk Size Issues
|
|
52
|
+
|
|
53
|
+
### Chunks Too Small
|
|
54
|
+
|
|
55
|
+
**Symptom:** Many chunks with very few tokens (10-50 tokens).
|
|
56
|
+
|
|
57
|
+
**Causes:**
|
|
58
|
+
- `minTokens` set too low (Semantic strategy)
|
|
59
|
+
- `mergeSmallChunks` disabled (Semantic strategy)
|
|
60
|
+
- Document has many small sections
|
|
61
|
+
|
|
62
|
+
**Solutions:**
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// Increase minTokens
|
|
66
|
+
const service = ChunkingService.createWithSemantic({
|
|
67
|
+
maxTokens: 512,
|
|
68
|
+
minTokens: 150, // Increase from default 100
|
|
69
|
+
mergeSmallChunks: true, // Ensure merging is enabled
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Chunks Too Large
|
|
74
|
+
|
|
75
|
+
**Symptom:** Chunks exceed your embedding model's token limit.
|
|
76
|
+
|
|
77
|
+
**Cause:** `maxTokens` set higher than your model supports.
|
|
78
|
+
|
|
79
|
+
**Solution:**
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
// Match your embedding model's limit
|
|
83
|
+
const service = ChunkingService.createWithSemantic({
|
|
84
|
+
maxTokens: 480, // Leave headroom below model's 512 limit
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Inconsistent Chunk Sizes
|
|
89
|
+
|
|
90
|
+
**Symptom:** Large variance in chunk sizes with Semantic strategy.
|
|
91
|
+
|
|
92
|
+
**Cause:** This is expected behavior - the Semantic strategy creates chunks based on document structure, not uniform size.
|
|
93
|
+
|
|
94
|
+
**Solutions:**
|
|
95
|
+
- If you need consistent sizes, use Fixed Length strategy
|
|
96
|
+
- Adjust `minTokens` and `mergeSmallChunks` to reduce small chunks
|
|
97
|
+
- Accept some variance as a tradeoff for semantic quality
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Token Count Mismatch
|
|
102
|
+
|
|
103
|
+
### Symptom
|
|
104
|
+
Token counts don't match what you expect from another tokenizer.
|
|
105
|
+
|
|
106
|
+
### Cause
|
|
107
|
+
Different tokenizers produce different token counts. The library uses tiktoken with `cl100k_base` by default (OpenAI's encoding for GPT-3.5/4).
|
|
108
|
+
|
|
109
|
+
### Solution
|
|
110
|
+
|
|
111
|
+
Ensure your encoding matches your target model:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
// For OpenAI GPT-3.5/4 and text-embedding-3-*
|
|
115
|
+
{ encoding: "cl100k_base" } // Default
|
|
116
|
+
|
|
117
|
+
// For older GPT-3 models
|
|
118
|
+
{ encoding: "p50k_base" }
|
|
119
|
+
|
|
120
|
+
// For code models
|
|
121
|
+
{ encoding: "p50k_base" }
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**Verify token counts:**
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { get_encoding } from "tiktoken";
|
|
128
|
+
|
|
129
|
+
const encoder = get_encoding("cl100k_base");
|
|
130
|
+
const tokens = encoder.encode("Your text here");
|
|
131
|
+
console.log(`Token count: ${tokens.length}`);
|
|
132
|
+
encoder.free();
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Memory Usage
|
|
138
|
+
|
|
139
|
+
### Symptom
|
|
140
|
+
Memory usage grows over time, especially in long-running applications.
|
|
141
|
+
|
|
142
|
+
### Cause
|
|
143
|
+
The tiktoken encoder uses native (WASM) memory that isn't managed by JavaScript's garbage collector. Failing to call `dispose()` leaks this memory.
|
|
144
|
+
|
|
145
|
+
### Solution
|
|
146
|
+
|
|
147
|
+
**Always call `dispose()` when done:**
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
// Pattern 1: try/finally
|
|
151
|
+
const service = ChunkingService.createWithSemantic({ maxTokens: 512 });
|
|
152
|
+
try {
|
|
153
|
+
const chunks = await service.chunk(text);
|
|
154
|
+
return chunks;
|
|
155
|
+
} finally {
|
|
156
|
+
service.dispose();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Pattern 2: Singleton with cleanup
|
|
160
|
+
class ChunkingManager {
|
|
161
|
+
private service: ChunkingService;
|
|
162
|
+
|
|
163
|
+
constructor() {
|
|
164
|
+
this.service = ChunkingService.createWithSemantic({ maxTokens: 512 });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async chunk(text: string) {
|
|
168
|
+
return await this.service.chunk(text);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Call on application shutdown
|
|
172
|
+
shutdown() {
|
|
173
|
+
this.service.dispose();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
**For batch processing, reuse one instance:**
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
// Good: single instance for many documents
|
|
182
|
+
const service = ChunkingService.createWithSemantic({ maxTokens: 512 });
|
|
183
|
+
try {
|
|
184
|
+
for (const doc of documents) {
|
|
185
|
+
const chunks = await service.chunk(doc.content);
|
|
186
|
+
await processChunks(chunks);
|
|
187
|
+
}
|
|
188
|
+
} finally {
|
|
189
|
+
service.dispose();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Bad: creating and leaking instances
|
|
193
|
+
for (const doc of documents) {
|
|
194
|
+
const service = ChunkingService.createWithSemantic({ maxTokens: 512 });
|
|
195
|
+
const chunks = await service.chunk(doc.content);
|
|
196
|
+
// Missing dispose() - leaks memory each iteration!
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## Code Block Splitting
|
|
203
|
+
|
|
204
|
+
### Symptom
|
|
205
|
+
Code blocks are split across multiple chunks, breaking syntax.
|
|
206
|
+
|
|
207
|
+
### Cause
|
|
208
|
+
Using Fixed Length strategy, which has no awareness of code blocks.
|
|
209
|
+
|
|
210
|
+
### Solution
|
|
211
|
+
|
|
212
|
+
Use **Semantic** or **Sliding Window** strategy:
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
// Semantic: preserves code blocks as single units
|
|
216
|
+
const service = ChunkingService.createWithSemantic({
|
|
217
|
+
maxTokens: 1024, // Larger to accommodate big code blocks
|
|
218
|
+
minTokens: 100,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// Sliding Window: protects code blocks from splitting
|
|
222
|
+
const service = ChunkingService.createWithSlidingWindow({
|
|
223
|
+
maxTokens: 512,
|
|
224
|
+
overlapTokens: 80,
|
|
225
|
+
});
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
> **Note:** If a single code block exceeds `maxTokens`, the Semantic strategy will split it by sentences as a fallback. Consider increasing `maxTokens` for code-heavy documents.
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## Mid-Word Splits
|
|
233
|
+
|
|
234
|
+
### Symptom
|
|
235
|
+
Chunks end in the middle of a word.
|
|
236
|
+
|
|
237
|
+
### Cause
|
|
238
|
+
Fixed Length strategy splits on token boundaries, and a single word can span multiple tokens. When split, the token-to-text decoding may cut within a word.
|
|
239
|
+
|
|
240
|
+
### Solution
|
|
241
|
+
|
|
242
|
+
Use Sliding Window or Semantic strategy, which respect word/sentence boundaries:
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
const service = ChunkingService.createWithSlidingWindow({
|
|
246
|
+
maxTokens: 512,
|
|
247
|
+
overlapTokens: 80,
|
|
248
|
+
preferParagraphBoundaries: true,
|
|
249
|
+
});
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
## Performance Issues
|
|
255
|
+
|
|
256
|
+
### Symptom
|
|
257
|
+
Chunking takes longer than expected.
|
|
258
|
+
|
|
259
|
+
### Causes and Solutions
|
|
260
|
+
|
|
261
|
+
**Large documents:**
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
// For very large documents (100KB+), consider pre-splitting
|
|
265
|
+
const sections = document.split(/(?=^# )/gm); // Split by H1 headers
|
|
266
|
+
const allChunks = [];
|
|
267
|
+
for (const section of sections) {
|
|
268
|
+
const chunks = await service.chunk(section);
|
|
269
|
+
allChunks.push(...chunks);
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
**Wrong strategy for the job:**
|
|
274
|
+
|
|
275
|
+
| Document Size | Strategy Choice |
|
|
276
|
+
|--------------|-----------------|
|
|
277
|
+
| < 10KB | Any strategy is fine |
|
|
278
|
+
| 10-100KB | Semantic or Sliding Window |
|
|
279
|
+
| 100KB-1MB | Fixed Length for speed, Semantic for quality |
|
|
280
|
+
| > 1MB | Pre-split, then use any strategy |
|
|
281
|
+
|
|
282
|
+
**Repeated service creation:**
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
// Bad: creates new encoder each time
|
|
286
|
+
for (const doc of docs) {
|
|
287
|
+
const service = ChunkingService.createWithSemantic({ maxTokens: 512 });
|
|
288
|
+
const chunks = await service.chunk(doc);
|
|
289
|
+
service.dispose();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Good: reuse one service
|
|
293
|
+
const service = ChunkingService.createWithSemantic({ maxTokens: 512 });
|
|
294
|
+
for (const doc of docs) {
|
|
295
|
+
const chunks = await service.chunk(doc);
|
|
296
|
+
}
|
|
297
|
+
service.dispose();
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## Configuration Errors
|
|
303
|
+
|
|
304
|
+
### `overlapTokens must be less than maxTokens`
|
|
305
|
+
|
|
306
|
+
**Cause:** Sliding Window's `overlapTokens` is >= `maxTokens`.
|
|
307
|
+
|
|
308
|
+
**Fix:**
|
|
309
|
+
```typescript
|
|
310
|
+
// Wrong
|
|
311
|
+
{ maxTokens: 512, overlapTokens: 512 }
|
|
312
|
+
{ maxTokens: 512, overlapTokens: 600 }
|
|
313
|
+
|
|
314
|
+
// Correct: overlap should be 10-25% of maxTokens
|
|
315
|
+
{ maxTokens: 512, overlapTokens: 80 }
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### `minChunkRatio must be between 0.1 and 0.9`
|
|
319
|
+
|
|
320
|
+
**Cause:** Sliding Window's `minChunkRatio` is outside valid range.
|
|
321
|
+
|
|
322
|
+
**Fix:**
|
|
323
|
+
```typescript
|
|
324
|
+
// Wrong
|
|
325
|
+
{ maxTokens: 512, overlapTokens: 80, minChunkRatio: 0.0 }
|
|
326
|
+
{ maxTokens: 512, overlapTokens: 80, minChunkRatio: 1.0 }
|
|
327
|
+
|
|
328
|
+
// Correct
|
|
329
|
+
{ maxTokens: 512, overlapTokens: 80, minChunkRatio: 0.5 }
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### `minTokens must be less than maxTokens`
|
|
333
|
+
|
|
334
|
+
**Cause:** Semantic strategy's `minTokens` is >= `maxTokens`.
|
|
335
|
+
|
|
336
|
+
**Fix:**
|
|
337
|
+
```typescript
|
|
338
|
+
// Wrong
|
|
339
|
+
{ maxTokens: 512, minTokens: 512 }
|
|
340
|
+
{ maxTokens: 256, minTokens: 300 }
|
|
341
|
+
|
|
342
|
+
// Correct: minTokens should be much less than maxTokens
|
|
343
|
+
{ maxTokens: 512, minTokens: 100 }
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
## Large Documents
|
|
349
|
+
|
|
350
|
+
### Symptom
|
|
351
|
+
Processing very large documents (1MB+) is slow or uses excessive memory.
|
|
352
|
+
|
|
353
|
+
### Solution
|
|
354
|
+
|
|
355
|
+
**Pre-split by natural boundaries:**
|
|
356
|
+
|
|
357
|
+
```typescript
|
|
358
|
+
function preSplitDocument(text: string, maxSectionSize: number = 50000): string[] {
|
|
359
|
+
// Split by major headers
|
|
360
|
+
const sections = text.split(/(?=^#{1,2}\s)/gm);
|
|
361
|
+
|
|
362
|
+
// Merge small sections, split large ones
|
|
363
|
+
const result: string[] = [];
|
|
364
|
+
let current = "";
|
|
365
|
+
|
|
366
|
+
for (const section of sections) {
|
|
367
|
+
if (current.length + section.length > maxSectionSize && current.length > 0) {
|
|
368
|
+
result.push(current);
|
|
369
|
+
current = section;
|
|
370
|
+
} else {
|
|
371
|
+
current += section;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (current.length > 0) {
|
|
376
|
+
result.push(current);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Usage
|
|
383
|
+
const service = ChunkingService.createWithSemantic({ maxTokens: 512 });
|
|
384
|
+
const sections = preSplitDocument(largeDocument);
|
|
385
|
+
const allChunks = [];
|
|
386
|
+
|
|
387
|
+
for (const section of sections) {
|
|
388
|
+
const chunks = await service.chunk(section);
|
|
389
|
+
allChunks.push(...chunks);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Re-index chunk numbers
|
|
393
|
+
allChunks.forEach((chunk, i) => { chunk.chunkIndex = i; });
|
|
394
|
+
|
|
395
|
+
service.dispose();
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
---
|
|
399
|
+
|
|
400
|
+
## Unicode and Special Characters
|
|
401
|
+
|
|
402
|
+
### Symptom
|
|
403
|
+
Unexpected behavior with non-ASCII text (CJK characters, emoji, etc.).
|
|
404
|
+
|
|
405
|
+
### Cause
|
|
406
|
+
Token counts for non-ASCII text differ significantly from ASCII. A single Chinese character may be 2-3 tokens.
|
|
407
|
+
|
|
408
|
+
### Solution
|
|
409
|
+
|
|
410
|
+
Test with representative content and adjust `maxTokens`:
|
|
411
|
+
|
|
412
|
+
```typescript
|
|
413
|
+
// For CJK-heavy content, tokens per character is higher
|
|
414
|
+
// Adjust maxTokens accordingly
|
|
415
|
+
const service = ChunkingService.createWithSemantic({
|
|
416
|
+
maxTokens: 256, // Lower limit for CJK content
|
|
417
|
+
minTokens: 50,
|
|
418
|
+
});
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
---
|
|
422
|
+
|
|
423
|
+
## Strategy Not Matching Expected Behavior
|
|
424
|
+
|
|
425
|
+
### Symptom
|
|
426
|
+
`ChunkingService.create()` selects the wrong strategy.
|
|
427
|
+
|
|
428
|
+
### Cause
|
|
429
|
+
The auto-detection in `create()` uses option presence to determine strategy:
|
|
430
|
+
|
|
431
|
+
| Options Present | Strategy |
|
|
432
|
+
|----------------|----------|
|
|
433
|
+
| `overlapTokens` | Sliding Window |
|
|
434
|
+
| `minTokens` or `mergeSmallChunks` | Semantic |
|
|
435
|
+
| Only `maxTokens` | Fixed Length |
|
|
436
|
+
|
|
437
|
+
### Solution
|
|
438
|
+
|
|
439
|
+
Use explicit factory methods instead of auto-detection:
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
// Explicit (recommended)
|
|
443
|
+
const service = ChunkingService.createWithSemantic({ maxTokens: 512, minTokens: 100 });
|
|
444
|
+
const service = ChunkingService.createWithSlidingWindow({ maxTokens: 512, overlapTokens: 80 });
|
|
445
|
+
const service = ChunkingService.createWithFixedLength({ maxTokens: 512 });
|
|
446
|
+
|
|
447
|
+
// Auto-detection (may be ambiguous)
|
|
448
|
+
const service = ChunkingService.create(options);
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
---
|
|
452
|
+
|
|
453
|
+
## Debugging Tips
|
|
454
|
+
|
|
455
|
+
### Inspect Chunks
|
|
456
|
+
|
|
457
|
+
```typescript
|
|
458
|
+
const chunks = await service.chunk(text);
|
|
459
|
+
|
|
460
|
+
// Print chunk summary
|
|
461
|
+
console.log(`Total chunks: ${chunks.length}`);
|
|
462
|
+
console.log(`Total tokens: ${chunks.reduce((sum, c) => sum + c.tokenCount, 0)}`);
|
|
463
|
+
|
|
464
|
+
for (const chunk of chunks) {
|
|
465
|
+
console.log(`\n--- Chunk ${chunk.chunkIndex} ---`);
|
|
466
|
+
console.log(`Tokens: ${chunk.tokenCount}`);
|
|
467
|
+
console.log(`Characters: ${chunk.content.length}`);
|
|
468
|
+
console.log(`Position: ${chunk.startIndex}-${chunk.endIndex}`);
|
|
469
|
+
console.log(`Preview: ${chunk.content.substring(0, 100)}...`);
|
|
470
|
+
}
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### Verify Character Indices
|
|
474
|
+
|
|
475
|
+
```typescript
|
|
476
|
+
for (const chunk of chunks) {
|
|
477
|
+
const extracted = originalText.substring(chunk.startIndex, chunk.endIndex);
|
|
478
|
+
const matches = extracted.trim() === chunk.content.trim();
|
|
479
|
+
if (!matches) {
|
|
480
|
+
console.warn(`Chunk ${chunk.chunkIndex}: index mismatch`);
|
|
481
|
+
console.warn(` Expected: "${chunk.content.substring(0, 50)}..."`);
|
|
482
|
+
console.warn(` Got: "${extracted.substring(0, 50)}..."`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
### Compare Strategies
|
|
488
|
+
|
|
489
|
+
```typescript
|
|
490
|
+
const text = loadTestDocument();
|
|
491
|
+
|
|
492
|
+
const strategies = [
|
|
493
|
+
{ name: "Fixed", service: ChunkingService.createWithFixedLength({ maxTokens: 512 }) },
|
|
494
|
+
{ name: "Sliding", service: ChunkingService.createWithSlidingWindow({ maxTokens: 512, overlapTokens: 80 }) },
|
|
495
|
+
{ name: "Semantic", service: ChunkingService.createWithSemantic({ maxTokens: 512, minTokens: 100 }) },
|
|
496
|
+
];
|
|
497
|
+
|
|
498
|
+
for (const { name, service } of strategies) {
|
|
499
|
+
const start = Date.now();
|
|
500
|
+
const chunks = await service.chunk(text);
|
|
501
|
+
const elapsed = Date.now() - start;
|
|
502
|
+
|
|
503
|
+
const tokenCounts = chunks.map(c => c.tokenCount);
|
|
504
|
+
const avg = tokenCounts.reduce((a, b) => a + b, 0) / tokenCounts.length;
|
|
505
|
+
|
|
506
|
+
console.log(`${name}: ${chunks.length} chunks, avg ${avg.toFixed(0)} tokens, ${elapsed}ms`);
|
|
507
|
+
service.dispose();
|
|
508
|
+
}
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
---
|
|
512
|
+
|
|
513
|
+
## Getting Help
|
|
514
|
+
|
|
515
|
+
If your issue isn't covered here:
|
|
516
|
+
|
|
517
|
+
1. **Check the tests** - Strategy test files contain examples of expected behavior
|
|
518
|
+
2. **Review the source** - Strategies are well-commented with algorithm explanations
|
|
519
|
+
3. **Test with simple input** - Isolate the issue with minimal text
|
|
520
|
+
4. **Compare strategies** - Try a different strategy to see if the issue is strategy-specific
|
|
521
|
+
|
|
522
|
+
## Related Documentation
|
|
523
|
+
|
|
524
|
+
- [API Reference](api-reference.md) - Error handling details
|
|
525
|
+
- [Configuration Guide](configuration.md) - Valid option ranges
|
|
526
|
+
- [Strategies Guide](strategies.md) - Strategy behavior details
|
|
527
|
+
- [Best Practices](best-practices.md) - Production optimization
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Main service
|
|
2
|
+
export { ChunkingService } from "./ChunkingService.js";
|
|
3
|
+
|
|
4
|
+
// Strategies
|
|
5
|
+
export { ChunkingStrategy } from "./strategies/ChunkingStrategy.js";
|
|
6
|
+
export { FixedLengthChunkingStrategy } from "./strategies/FixedLengthChunkingStrategy.js";
|
|
7
|
+
export { SemanticChunkingStrategy } from "./strategies/SemanticChunkingStrategy.js";
|
|
8
|
+
export { SlidingWindowChunkingStrategy } from "./strategies/SlidingWindowChunkingStrategy.js";
|
|
9
|
+
|
|
10
|
+
// Types
|
|
11
|
+
export type {
|
|
12
|
+
ChunkingOptions,
|
|
13
|
+
FixedLengthChunkingOptions,
|
|
14
|
+
SemanticChunkingOptions,
|
|
15
|
+
SlidingWindowChunkingOptions,
|
|
16
|
+
TextChunk,
|
|
17
|
+
} from "./types/index.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chunking",
|
|
3
|
+
"description": "Document chunking library for RAG and embeddings with fixed-length, sliding window, and semantic strategies",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"dependencies": [],
|
|
6
|
+
"npmDependencies": {
|
|
7
|
+
"tiktoken": "^1.0.22"
|
|
8
|
+
},
|
|
9
|
+
"requiredEnvVars": [],
|
|
10
|
+
"optionalEnvVars": [],
|
|
11
|
+
"postInstall": [
|
|
12
|
+
"Import ChunkingService from @sixthsummit/ship-api/libraries/chunking",
|
|
13
|
+
"Choose a strategy: createWithSemantic (recommended), createWithSlidingWindow, or createWithFixedLength",
|
|
14
|
+
"Always call dispose() when done to free tokenizer resources"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { TextChunk } from "../types/index.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Base interface for all chunking strategies
|
|
5
|
+
*/
|
|
6
|
+
export interface ChunkingStrategy {
|
|
7
|
+
/**
|
|
8
|
+
* Chunks the given text according to the strategy
|
|
9
|
+
* @param text - The text to chunk
|
|
10
|
+
* @returns An array of text chunks
|
|
11
|
+
*/
|
|
12
|
+
chunk(text: string): Promise<TextChunk[]>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { encoding_for_model, get_encoding, type Tiktoken } from "tiktoken";
|
|
2
|
+
import type { FixedLengthChunkingOptions, TextChunk } from "../types/index.js";
|
|
3
|
+
import type { ChunkingStrategy } from "./ChunkingStrategy.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fixed length chunking strategy that splits text into chunks of a specified token count
|
|
7
|
+
*/
|
|
8
|
+
export class FixedLengthChunkingStrategy implements ChunkingStrategy {
|
|
9
|
+
private encoder: Tiktoken;
|
|
10
|
+
|
|
11
|
+
constructor(private options: FixedLengthChunkingOptions) {
|
|
12
|
+
// Initialize the tokenizer with the specified encoding
|
|
13
|
+
const encodingName = options.encoding || "cl100k_base";
|
|
14
|
+
try {
|
|
15
|
+
this.encoder = get_encoding(encodingName as any);
|
|
16
|
+
} catch {
|
|
17
|
+
// Fallback to GPT-3.5/4 encoding
|
|
18
|
+
this.encoder = encoding_for_model("gpt-3.5-turbo");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Chunks the text into fixed-length chunks based on token count
|
|
24
|
+
* @param text - The text to chunk
|
|
25
|
+
* @returns An array of text chunks
|
|
26
|
+
*/
|
|
27
|
+
async chunk(text: string): Promise<TextChunk[]> {
|
|
28
|
+
const chunks: TextChunk[] = [];
|
|
29
|
+
|
|
30
|
+
// Tokenize the entire text
|
|
31
|
+
const tokens = this.encoder.encode(text);
|
|
32
|
+
const maxTokens = this.options.maxTokens;
|
|
33
|
+
|
|
34
|
+
// Split into chunks
|
|
35
|
+
let chunkIndex = 0;
|
|
36
|
+
for (let i = 0; i < tokens.length; i += maxTokens) {
|
|
37
|
+
const chunkTokens = tokens.slice(i, i + maxTokens);
|
|
38
|
+
const decoded = this.encoder.decode(chunkTokens);
|
|
39
|
+
// Ensure we have a proper string
|
|
40
|
+
const chunkText = typeof decoded === "string" ? decoded : new TextDecoder().decode(decoded);
|
|
41
|
+
|
|
42
|
+
// Calculate character indices
|
|
43
|
+
const startIndex = this.getCharacterIndex(text, tokens, i);
|
|
44
|
+
const endIndex = startIndex + chunkText.length;
|
|
45
|
+
|
|
46
|
+
chunks.push({
|
|
47
|
+
content: chunkText,
|
|
48
|
+
startIndex,
|
|
49
|
+
endIndex,
|
|
50
|
+
tokenCount: chunkTokens.length,
|
|
51
|
+
chunkIndex: chunkIndex++,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return chunks;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Helper method to get the character index from a token index
|
|
60
|
+
* @param text - The original text
|
|
61
|
+
* @param tokens - All tokens
|
|
62
|
+
* @param tokenIndex - The token index to convert
|
|
63
|
+
* @returns The character index
|
|
64
|
+
*/
|
|
65
|
+
private getCharacterIndex(text: string, tokens: Uint32Array, tokenIndex: number): number {
|
|
66
|
+
if (tokenIndex === 0) return 0;
|
|
67
|
+
|
|
68
|
+
// Decode tokens up to the target index to find character position
|
|
69
|
+
const tokensUpToIndex = tokens.slice(0, tokenIndex);
|
|
70
|
+
const decoded = this.encoder.decode(tokensUpToIndex);
|
|
71
|
+
const decodedUpToIndex =
|
|
72
|
+
typeof decoded === "string" ? decoded : new TextDecoder().decode(decoded);
|
|
73
|
+
return decodedUpToIndex.length;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Cleanup method to free the encoder
|
|
78
|
+
*/
|
|
79
|
+
dispose(): void {
|
|
80
|
+
try {
|
|
81
|
+
if (this.encoder) {
|
|
82
|
+
this.encoder.free();
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
// Encoder may have already been freed
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|