Skip to main content

Shared Packages

The internal/pkg/ directory contains reusable packages shared across the orchestrator, agent runtime, CLI, and webhook processes. Each package has a single responsibility.


Package Reference

Core Infrastructure

PackagePurposeKey Exports
databasePostgreSQL connection and query buildingNew(), SessionRunner interface, dbr.Session wrappers
redisclientRedis client with typed operationsNew(), Get/Set, counters, hashes, sets
crawblnatsNATS JetStream client for usage eventsNew(), Publish(), stream lifecycle management
configenvEnv var loading with _FILE secret fallbackSecretString(), String(), Int(), Bool()

Authentication & Security

PackagePurposeKey Exports
hmacHMAC-SHA256 token generation and validationGenerateToken(), ValidateToken()
firebaseFirebase Cloud Messaging (FCM) push notificationsClient, SendNotification()
grpcgRPC server utilities and auth interceptorsNewServer(), HMAC bearer token interceptor
httpserverHTTP header constants and auth metadataTokenSourceFromHeaders(), header name constants

Domain Helpers

PackagePurposeKey Exports
errorsStructured error types (business vs server)BusinessError, ServerError, Is(), As()
pricingLLM model pricing cache with DB refreshCache, CostForTokens(), periodic refresh goroutine
embedVector embedding interface for LLM providersEmbedder interface, Embed(text) -> []float32
realtimeBroadcaster interface for workspace eventsBroadcaster, event types (message, agent, artifact, workflow, usage)
kubeKubernetes naming constantsMaxNameLength (63), MaxWorkloadNameLength (52)

Build & Deploy Tooling

PackagePurposeKey Exports
argocdImage tag updater for crawbl-argocd-apps repoUpdateImageTag(), DOCR registry helpers
releaseGit tag and GitHub release creationCreateTag(), CreateRelease(), auto-generated notes
versioningSemver calculation from conventional commitsNextVersion(), major/minor/patch bump logic
gitutilGit helpers (root dir, sibling repos, verification)RootDir(), SiblingRepo(), IsClean()
cliCLI output formatting and stylingout.Success(), out.Error(), style.Bold()

Utilities

PackagePurposeKey Exports
fileutilAtomic file I/O (temp + rename)WriteAtomic()
runtimeOS signal handling for graceful shutdownRun() — blocks until SIGINT/SIGTERM
telemetryOpenTelemetry metrics export to VictoriaMetricsSetup(), OTLP HTTP exporter, disabled by default
yamlvaluesPulumi stack config file parsingStackConfig struct for Pulumi.<env>.yaml

Usage Patterns

Database sessions

import "github.com/Crawbl-AI/crawbl-backend/internal/pkg/database"

// One connection per process
db, err := database.New(cfg.Postgres)

// One session per request (pass through service/repo layers)
sess := db.NewSession(nil)

Config with secret fallback

import "github.com/Crawbl-AI/crawbl-backend/internal/pkg/configenv"

// Reads CRAWBL_DB_PASSWORD or CRAWBL_DB_PASSWORD_FILE (for volume-mounted secrets)
password := configenv.SecretString("CRAWBL_DB_PASSWORD", "")

HMAC tokens

import crawblhmac "github.com/Crawbl-AI/crawbl-backend/internal/pkg/hmac"

// Generate
token := crawblhmac.GenerateToken(signingKey, userID, workspaceID)

// Validate
userID, workspaceID, err := crawblhmac.ValidateToken(signingKey, token)

Structured errors

import "github.com/Crawbl-AI/crawbl-backend/internal/pkg/errors"

// Business error (client sees it)
return errors.NewBusinessError("workspace not found", 404)

// Server error (logged, client gets generic 500)
return errors.NewServerError("redis connection failed", err)

What's next: See Codebase Layout for the full directory structure, or Coding Patterns for how these packages are used in practice.