@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,538 @@
|
|
|
1
|
+
# Chunking Library - Code Organization
|
|
2
|
+
|
|
3
|
+
Guide to the library's architecture, code structure, conventions, and how to extend it.
|
|
4
|
+
|
|
5
|
+
## 🏗️ Architecture
|
|
6
|
+
|
|
7
|
+
### Design Pattern: Strategy
|
|
8
|
+
|
|
9
|
+
The library is built around the **Strategy pattern**, allowing chunking algorithms to be swapped at runtime without changing the client code.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
┌──────────────────────┐
|
|
13
|
+
│ ChunkingService │ ← Client-facing service
|
|
14
|
+
│ │
|
|
15
|
+
│ + chunk(text) │
|
|
16
|
+
│ + setStrategy() │
|
|
17
|
+
│ + dispose() │
|
|
18
|
+
│ + static factories │
|
|
19
|
+
└─────────┬────────────┘
|
|
20
|
+
│ uses
|
|
21
|
+
▼
|
|
22
|
+
┌──────────────────────┐
|
|
23
|
+
│ ChunkingStrategy │ ← Interface
|
|
24
|
+
│ (interface) │
|
|
25
|
+
│ │
|
|
26
|
+
│ + chunk(text) │
|
|
27
|
+
└─────────┬────────────┘
|
|
28
|
+
│ implemented by
|
|
29
|
+
▼
|
|
30
|
+
┌─────────────────┐ ┌──────────────────────┐ ┌────────────────────────┐
|
|
31
|
+
│ FixedLength │ │ SlidingWindow │ │ Semantic │
|
|
32
|
+
│ Strategy │ │ Strategy │ │ Strategy │
|
|
33
|
+
│ │ │ │ │ │
|
|
34
|
+
│ Simple split │ │ Overlap + boundaries │ │ Structure-aware │
|
|
35
|
+
│ by token count │ │ paragraph/sentence │ │ headers/lists/code │
|
|
36
|
+
└─────────────────┘ └──────────────────────┘ └────────────────────────┘
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Key Principles
|
|
40
|
+
|
|
41
|
+
1. **Single Responsibility:** Each strategy handles only its chunking algorithm
|
|
42
|
+
2. **Open/Closed:** New strategies can be added without modifying existing code
|
|
43
|
+
3. **Interface Segregation:** The `ChunkingStrategy` interface has a single method
|
|
44
|
+
4. **Dependency Inversion:** `ChunkingService` depends on the abstract interface, not concrete implementations
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## 📁 File Structure
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
libraries/chunking/
|
|
52
|
+
│
|
|
53
|
+
├── index.ts # Barrel exports (all public API)
|
|
54
|
+
├── ChunkingService.ts # Main service class
|
|
55
|
+
├── ChunkingService.test.ts # Service integration tests
|
|
56
|
+
├── ChunkingService.example.ts # Usage examples
|
|
57
|
+
│
|
|
58
|
+
├── strategies/
|
|
59
|
+
│ ├── ChunkingStrategy.ts # Base strategy interface
|
|
60
|
+
│ ├── FixedLengthChunkingStrategy.ts # Fixed length implementation
|
|
61
|
+
│ ├── SlidingWindowChunkingStrategy.ts # Sliding window implementation
|
|
62
|
+
│ ├── SlidingWindowChunkingStrategy.test.ts
|
|
63
|
+
│ ├── SemanticChunkingStrategy.ts # Semantic implementation
|
|
64
|
+
│ └── SemanticChunkingStrategy.test.ts
|
|
65
|
+
│
|
|
66
|
+
├── types/
|
|
67
|
+
│ └── index.ts # All TypeScript type definitions
|
|
68
|
+
│
|
|
69
|
+
├── docs/
|
|
70
|
+
│ ├── index.md # Documentation index
|
|
71
|
+
│ ├── summary.md # Quick reference
|
|
72
|
+
│ ├── quick-start.md # Getting started guide
|
|
73
|
+
│ ├── api-reference.md # Complete API docs
|
|
74
|
+
│ ├── strategies.md # Strategy deep dive
|
|
75
|
+
│ ├── configuration.md # Configuration guide
|
|
76
|
+
│ ├── code-organization.md # This file
|
|
77
|
+
│ └── troubleshooting.md # Common issues
|
|
78
|
+
│
|
|
79
|
+
├── README.md # Library overview
|
|
80
|
+
│
|
|
81
|
+
├── docs/
|
|
82
|
+
│ ├── index.md # Documentation index
|
|
83
|
+
│ ├── summary.md # Quick reference
|
|
84
|
+
│ ├── quick-start.md # Getting started guide
|
|
85
|
+
│ ├── api-reference.md # Complete API docs
|
|
86
|
+
│ ├── strategies.md # Strategy deep dive
|
|
87
|
+
│ ├── configuration.md # Configuration guide
|
|
88
|
+
│ ├── best-practices.md # Production best practices
|
|
89
|
+
│ ├── code-organization.md # This file
|
|
90
|
+
│ └── troubleshooting.md # Common issues
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### File Responsibilities
|
|
94
|
+
|
|
95
|
+
| File | Responsibility |
|
|
96
|
+
|------|---------------|
|
|
97
|
+
| `index.ts` | Public API surface - exports all classes, interfaces, and types |
|
|
98
|
+
| `ChunkingService.ts` | Service facade with factory methods and strategy delegation |
|
|
99
|
+
| `types/index.ts` | All TypeScript interfaces and type definitions |
|
|
100
|
+
| `strategies/ChunkingStrategy.ts` | Base interface contract |
|
|
101
|
+
| `strategies/*Strategy.ts` | Individual strategy implementations |
|
|
102
|
+
| `strategies/*.test.ts` | Strategy-specific unit tests |
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## 📦 Module Exports
|
|
107
|
+
|
|
108
|
+
All public API is exported through `index.ts`:
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
// Classes (concrete implementations)
|
|
112
|
+
export { ChunkingService } from "./ChunkingService.js";
|
|
113
|
+
export { FixedLengthChunkingStrategy } from "./strategies/FixedLengthChunkingStrategy.js";
|
|
114
|
+
export { SlidingWindowChunkingStrategy } from "./strategies/SlidingWindowChunkingStrategy.js";
|
|
115
|
+
export { SemanticChunkingStrategy } from "./strategies/SemanticChunkingStrategy.js";
|
|
116
|
+
|
|
117
|
+
// Interfaces
|
|
118
|
+
export type { ChunkingStrategy } from "./strategies/ChunkingStrategy.js";
|
|
119
|
+
|
|
120
|
+
// Types
|
|
121
|
+
export type {
|
|
122
|
+
TextChunk,
|
|
123
|
+
ChunkingOptions,
|
|
124
|
+
FixedLengthChunkingOptions,
|
|
125
|
+
SlidingWindowChunkingOptions,
|
|
126
|
+
SemanticChunkingOptions,
|
|
127
|
+
} from "./types/index.js";
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Import Conventions
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
// Import from the library barrel (preferred)
|
|
134
|
+
import { ChunkingService, SemanticChunkingStrategy } from "@sixthsummit/ship-api/libraries/chunking";
|
|
135
|
+
import type { TextChunk, SemanticChunkingOptions } from "@sixthsummit/ship-api/libraries/chunking";
|
|
136
|
+
|
|
137
|
+
// Import specific files (internal use)
|
|
138
|
+
import { ChunkingStrategy } from "./strategies/ChunkingStrategy.js";
|
|
139
|
+
import { TextChunk } from "./types/index.js";
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## 🔧 Conventions
|
|
145
|
+
|
|
146
|
+
### Naming
|
|
147
|
+
|
|
148
|
+
| Item | Convention | Example |
|
|
149
|
+
|------|-----------|---------|
|
|
150
|
+
| Strategy classes | `PascalCase` + `Strategy` suffix | `SemanticChunkingStrategy` |
|
|
151
|
+
| Option interfaces | `PascalCase` + `Options` suffix | `SemanticChunkingOptions` |
|
|
152
|
+
| Factory methods | `createWith` prefix | `createWithSemantic()` |
|
|
153
|
+
| Test files | Colocated, `.test.ts` suffix | `SemanticChunkingStrategy.test.ts` |
|
|
154
|
+
| Type files | `index.ts` in `types/` folder | `types/index.ts` |
|
|
155
|
+
|
|
156
|
+
### Strategy Implementation Pattern
|
|
157
|
+
|
|
158
|
+
Every strategy follows this pattern:
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
import { encoding_for_model, get_encoding, type Tiktoken } from "tiktoken";
|
|
162
|
+
import { ChunkingStrategy } from "./ChunkingStrategy.js";
|
|
163
|
+
import { TextChunk } from "../types/index.js";
|
|
164
|
+
|
|
165
|
+
export class YourStrategy implements ChunkingStrategy {
|
|
166
|
+
private encoder: Tiktoken;
|
|
167
|
+
|
|
168
|
+
constructor(options: YourOptions) {
|
|
169
|
+
// 1. Validate options
|
|
170
|
+
if (/* invalid */) {
|
|
171
|
+
throw new Error("Descriptive error message");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// 2. Initialize tiktoken encoder
|
|
175
|
+
const encodingName = options.encoding || "cl100k_base";
|
|
176
|
+
try {
|
|
177
|
+
this.encoder = get_encoding(encodingName as any);
|
|
178
|
+
} catch {
|
|
179
|
+
this.encoder = encoding_for_model("gpt-3.5-turbo");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async chunk(text: string): Promise<TextChunk[]> {
|
|
184
|
+
// 3. Implement chunking algorithm
|
|
185
|
+
// Always return TextChunk[] with all required fields
|
|
186
|
+
return chunks;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
dispose(): void {
|
|
190
|
+
// 4. Clean up tiktoken encoder
|
|
191
|
+
try {
|
|
192
|
+
if (this.encoder) {
|
|
193
|
+
this.encoder.free();
|
|
194
|
+
}
|
|
195
|
+
} catch {
|
|
196
|
+
// Encoder may have already been freed
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Error Handling
|
|
203
|
+
|
|
204
|
+
- **Construction:** Validate options and throw descriptive errors
|
|
205
|
+
- **Encoding fallback:** If the encoding name is invalid, fall back to `gpt-3.5-turbo` silently
|
|
206
|
+
- **Dispose:** Catch and swallow errors during cleanup (encoder may already be freed)
|
|
207
|
+
- **Empty input:** Handled by `ChunkingService.chunk()` which returns `[]` for empty/whitespace text
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
## 🧪 Testing
|
|
212
|
+
|
|
213
|
+
### Test Structure
|
|
214
|
+
|
|
215
|
+
Tests are colocated with source files:
|
|
216
|
+
|
|
217
|
+
```
|
|
218
|
+
ChunkingService.test.ts # Service-level tests
|
|
219
|
+
strategies/SlidingWindowChunkingStrategy.test.ts # Strategy-specific tests
|
|
220
|
+
strategies/SemanticChunkingStrategy.test.ts # Strategy-specific tests
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Running Tests
|
|
224
|
+
|
|
225
|
+
```bash
|
|
226
|
+
# Run all chunking library tests
|
|
227
|
+
npm test -- libraries/chunking
|
|
228
|
+
|
|
229
|
+
# Run specific strategy tests
|
|
230
|
+
npm test -- libraries/chunking/strategies/SemanticChunkingStrategy
|
|
231
|
+
|
|
232
|
+
# Run with coverage
|
|
233
|
+
npm run test:coverage -- libraries/chunking
|
|
234
|
+
|
|
235
|
+
# Watch mode
|
|
236
|
+
npm run test:watch -- libraries/chunking
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Writing Tests
|
|
240
|
+
|
|
241
|
+
Follow the existing patterns:
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
245
|
+
import { SemanticChunkingStrategy } from "./SemanticChunkingStrategy.js";
|
|
246
|
+
|
|
247
|
+
describe("SemanticChunkingStrategy", () => {
|
|
248
|
+
let strategy: SemanticChunkingStrategy;
|
|
249
|
+
|
|
250
|
+
beforeEach(() => {
|
|
251
|
+
strategy = new SemanticChunkingStrategy({
|
|
252
|
+
maxTokens: 512,
|
|
253
|
+
minTokens: 50,
|
|
254
|
+
mergeSmallChunks: true,
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
afterEach(() => {
|
|
259
|
+
strategy.dispose();
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
describe("chunk", () => {
|
|
263
|
+
it("should preserve code blocks", async () => {
|
|
264
|
+
const text = `Some text before.
|
|
265
|
+
|
|
266
|
+
\`\`\`typescript
|
|
267
|
+
function hello() {
|
|
268
|
+
console.log("Hello!");
|
|
269
|
+
}
|
|
270
|
+
\`\`\`
|
|
271
|
+
|
|
272
|
+
Some text after.`;
|
|
273
|
+
|
|
274
|
+
const chunks = await strategy.chunk(text);
|
|
275
|
+
|
|
276
|
+
// Code block should be in a single chunk
|
|
277
|
+
const codeChunk = chunks.find(c => c.content.includes("function hello"));
|
|
278
|
+
expect(codeChunk).toBeDefined();
|
|
279
|
+
expect(codeChunk!.content).toContain("```typescript");
|
|
280
|
+
expect(codeChunk!.content).toContain("```");
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("should handle empty text", async () => {
|
|
284
|
+
// Note: Empty text is handled by ChunkingService, not strategies directly
|
|
285
|
+
const chunks = await strategy.chunk(" ");
|
|
286
|
+
// Behavior may vary by strategy
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe("constructor", () => {
|
|
291
|
+
it("should throw if minTokens >= maxTokens", () => {
|
|
292
|
+
expect(() => {
|
|
293
|
+
new SemanticChunkingStrategy({
|
|
294
|
+
maxTokens: 100,
|
|
295
|
+
minTokens: 100,
|
|
296
|
+
});
|
|
297
|
+
}).toThrow("minTokens must be less than maxTokens");
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Test Categories
|
|
304
|
+
|
|
305
|
+
| Category | What to Test | Example |
|
|
306
|
+
|----------|-------------|---------|
|
|
307
|
+
| **Basic functionality** | Core chunking behavior | Text splits into expected chunks |
|
|
308
|
+
| **Boundary handling** | Edge cases at boundaries | Sentence/paragraph detection |
|
|
309
|
+
| **Structure preservation** | Semantic units kept intact | Code blocks not split |
|
|
310
|
+
| **Configuration** | Option validation and effects | Invalid options throw errors |
|
|
311
|
+
| **Edge cases** | Unusual input | Empty text, single word, very long text |
|
|
312
|
+
| **Performance** | Processing time | Large documents complete in reasonable time |
|
|
313
|
+
|
|
314
|
+
### Mocking
|
|
315
|
+
|
|
316
|
+
To mock the chunking service in consumer code:
|
|
317
|
+
|
|
318
|
+
```typescript
|
|
319
|
+
import { vi } from "vitest";
|
|
320
|
+
import { ChunkingService } from "@sixthsummit/ship-api/libraries/chunking";
|
|
321
|
+
import type { TextChunk } from "@sixthsummit/ship-api/libraries/chunking";
|
|
322
|
+
|
|
323
|
+
// Mock the entire module
|
|
324
|
+
vi.mock("@sixthsummit/ship-api/libraries/chunking", () => ({
|
|
325
|
+
ChunkingService: {
|
|
326
|
+
createWithSemantic: vi.fn().mockReturnValue({
|
|
327
|
+
chunk: vi.fn().mockResolvedValue([
|
|
328
|
+
{
|
|
329
|
+
content: "Mock chunk content",
|
|
330
|
+
startIndex: 0,
|
|
331
|
+
endIndex: 18,
|
|
332
|
+
tokenCount: 3,
|
|
333
|
+
chunkIndex: 0,
|
|
334
|
+
},
|
|
335
|
+
] as TextChunk[]),
|
|
336
|
+
dispose: vi.fn(),
|
|
337
|
+
}),
|
|
338
|
+
},
|
|
339
|
+
}));
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
## 🔌 Creating a Custom Strategy
|
|
345
|
+
|
|
346
|
+
### Step 1: Define Options Interface
|
|
347
|
+
|
|
348
|
+
Add your options to `types/index.ts`:
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
export interface CustomChunkingOptions {
|
|
352
|
+
maxTokens: number;
|
|
353
|
+
encoding?: string;
|
|
354
|
+
// Your custom options...
|
|
355
|
+
customParam: string;
|
|
356
|
+
}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Update the union type:
|
|
360
|
+
|
|
361
|
+
```typescript
|
|
362
|
+
export type ChunkingOptions =
|
|
363
|
+
| FixedLengthChunkingOptions
|
|
364
|
+
| SlidingWindowChunkingOptions
|
|
365
|
+
| SemanticChunkingOptions
|
|
366
|
+
| CustomChunkingOptions;
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
### Step 2: Implement the Strategy
|
|
370
|
+
|
|
371
|
+
Create `strategies/CustomChunkingStrategy.ts`:
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
import { encoding_for_model, get_encoding, type Tiktoken } from "tiktoken";
|
|
375
|
+
import { ChunkingStrategy } from "./ChunkingStrategy.js";
|
|
376
|
+
import { CustomChunkingOptions, TextChunk } from "../types/index.js";
|
|
377
|
+
|
|
378
|
+
export class CustomChunkingStrategy implements ChunkingStrategy {
|
|
379
|
+
private encoder: Tiktoken;
|
|
380
|
+
private maxTokens: number;
|
|
381
|
+
|
|
382
|
+
constructor(options: CustomChunkingOptions) {
|
|
383
|
+
this.maxTokens = options.maxTokens;
|
|
384
|
+
|
|
385
|
+
// Validate options
|
|
386
|
+
if (!options.customParam) {
|
|
387
|
+
throw new Error("customParam is required");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Initialize tokenizer
|
|
391
|
+
const encodingName = options.encoding || "cl100k_base";
|
|
392
|
+
try {
|
|
393
|
+
this.encoder = get_encoding(encodingName as any);
|
|
394
|
+
} catch {
|
|
395
|
+
this.encoder = encoding_for_model("gpt-3.5-turbo");
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async chunk(text: string): Promise<TextChunk[]> {
|
|
400
|
+
// Your chunking algorithm here
|
|
401
|
+
const chunks: TextChunk[] = [];
|
|
402
|
+
// ...
|
|
403
|
+
return chunks;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
dispose(): void {
|
|
407
|
+
try {
|
|
408
|
+
if (this.encoder) {
|
|
409
|
+
this.encoder.free();
|
|
410
|
+
}
|
|
411
|
+
} catch {
|
|
412
|
+
// Encoder may have already been freed
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### Step 3: Add Factory Method
|
|
419
|
+
|
|
420
|
+
Add to `ChunkingService.ts`:
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
static createWithCustom(options: CustomChunkingOptions): ChunkingService {
|
|
424
|
+
const strategy = new CustomChunkingStrategy(options);
|
|
425
|
+
return new ChunkingService(strategy);
|
|
426
|
+
}
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
Update `create()` auto-detection if applicable.
|
|
430
|
+
|
|
431
|
+
### Step 4: Export
|
|
432
|
+
|
|
433
|
+
Add to `index.ts`:
|
|
434
|
+
|
|
435
|
+
```typescript
|
|
436
|
+
export { CustomChunkingStrategy } from "./strategies/CustomChunkingStrategy.js";
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
### Step 5: Write Tests
|
|
440
|
+
|
|
441
|
+
Create `strategies/CustomChunkingStrategy.test.ts` covering:
|
|
442
|
+
- Basic chunking behavior
|
|
443
|
+
- Option validation
|
|
444
|
+
- Edge cases
|
|
445
|
+
- Resource cleanup
|
|
446
|
+
|
|
447
|
+
### Step 6: Document
|
|
448
|
+
|
|
449
|
+
Add the strategy to:
|
|
450
|
+
- `docs/strategies.md` - Strategy comparison and details
|
|
451
|
+
- `docs/api-reference.md` - API documentation
|
|
452
|
+
- `docs/configuration.md` - Configuration options
|
|
453
|
+
- `README.md` - Overview mention
|
|
454
|
+
|
|
455
|
+
---
|
|
456
|
+
|
|
457
|
+
## 📐 Dependencies
|
|
458
|
+
|
|
459
|
+
### Runtime Dependencies
|
|
460
|
+
|
|
461
|
+
| Package | Purpose | Usage |
|
|
462
|
+
|---------|---------|-------|
|
|
463
|
+
| `tiktoken` | Token counting and encoding | All strategies use for accurate token counting |
|
|
464
|
+
|
|
465
|
+
### Dev Dependencies
|
|
466
|
+
|
|
467
|
+
| Package | Purpose |
|
|
468
|
+
|---------|---------|
|
|
469
|
+
| `vitest` | Testing framework |
|
|
470
|
+
| `typescript` | Type checking and compilation |
|
|
471
|
+
|
|
472
|
+
### Why tiktoken?
|
|
473
|
+
|
|
474
|
+
- Provides the same tokenization as OpenAI's models
|
|
475
|
+
- Native performance (WASM-based)
|
|
476
|
+
- Multiple encoding support
|
|
477
|
+
- Accurate token counting is critical for chunk sizing
|
|
478
|
+
- Must be explicitly freed via `dispose()` to avoid memory leaks
|
|
479
|
+
|
|
480
|
+
---
|
|
481
|
+
|
|
482
|
+
## 🔄 Data Flow
|
|
483
|
+
|
|
484
|
+
### Chunking Pipeline
|
|
485
|
+
|
|
486
|
+
```
|
|
487
|
+
Input Text (string)
|
|
488
|
+
│
|
|
489
|
+
▼
|
|
490
|
+
ChunkingService.chunk()
|
|
491
|
+
│
|
|
492
|
+
├── Empty/whitespace check → return []
|
|
493
|
+
│
|
|
494
|
+
▼
|
|
495
|
+
Strategy.chunk()
|
|
496
|
+
│
|
|
497
|
+
├── Tokenize (tiktoken)
|
|
498
|
+
├── Apply strategy algorithm
|
|
499
|
+
├── Calculate character indices
|
|
500
|
+
│
|
|
501
|
+
▼
|
|
502
|
+
TextChunk[] output
|
|
503
|
+
│
|
|
504
|
+
├── content: string
|
|
505
|
+
├── startIndex: number
|
|
506
|
+
├── endIndex: number
|
|
507
|
+
├── tokenCount: number
|
|
508
|
+
└── chunkIndex: number
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
### Character Index Mapping
|
|
512
|
+
|
|
513
|
+
All strategies maintain a mapping from token positions back to character positions in the original document:
|
|
514
|
+
|
|
515
|
+
```typescript
|
|
516
|
+
private getCharacterIndex(text: string, tokens: Uint32Array, tokenIndex: number): number {
|
|
517
|
+
if (tokenIndex === 0) return 0;
|
|
518
|
+
const tokensUpToIndex = tokens.slice(0, tokenIndex);
|
|
519
|
+
const decoded = this.encoder.decode(tokensUpToIndex);
|
|
520
|
+
return (typeof decoded === "string" ? decoded : new TextDecoder().decode(decoded)).length;
|
|
521
|
+
}
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
This ensures `startIndex` and `endIndex` in the output can be used to extract the exact original text:
|
|
525
|
+
|
|
526
|
+
```typescript
|
|
527
|
+
const originalText = document.substring(chunk.startIndex, chunk.endIndex);
|
|
528
|
+
// originalText should match chunk.content (may differ slightly due to trimming)
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
---
|
|
532
|
+
|
|
533
|
+
## Related Documentation
|
|
534
|
+
|
|
535
|
+
- [API Reference](api-reference.md) - Complete API docs
|
|
536
|
+
- [Strategies Guide](strategies.md) - Strategy details
|
|
537
|
+
- [Quick Start](quick-start.md) - Get started
|
|
538
|
+
- [Troubleshooting](troubleshooting.md) - Common issues
|