@littlebearapps/platform-admin-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/README.md +112 -0
  2. package/dist/index.d.ts +16 -0
  3. package/dist/index.js +89 -0
  4. package/dist/prompts.d.ts +27 -0
  5. package/dist/prompts.js +80 -0
  6. package/dist/scaffold.d.ts +5 -0
  7. package/dist/scaffold.js +65 -0
  8. package/dist/templates.d.ts +16 -0
  9. package/dist/templates.js +131 -0
  10. package/package.json +46 -0
  11. package/templates/full/migrations/006_pattern_discovery.sql +199 -0
  12. package/templates/full/migrations/007_notifications_search.sql +127 -0
  13. package/templates/full/workers/lib/pattern-discovery/ai-prompt.ts +644 -0
  14. package/templates/full/workers/lib/pattern-discovery/clustering.ts +278 -0
  15. package/templates/full/workers/lib/pattern-discovery/shadow-evaluation.ts +603 -0
  16. package/templates/full/workers/lib/pattern-discovery/storage.ts +806 -0
  17. package/templates/full/workers/lib/pattern-discovery/types.ts +159 -0
  18. package/templates/full/workers/lib/pattern-discovery/validation.ts +278 -0
  19. package/templates/full/workers/pattern-discovery.ts +661 -0
  20. package/templates/full/workers/platform-alert-router.ts +1809 -0
  21. package/templates/full/workers/platform-notifications.ts +424 -0
  22. package/templates/full/workers/platform-search.ts +480 -0
  23. package/templates/full/workers/platform-settings.ts +436 -0
  24. package/templates/full/wrangler.alert-router.jsonc.hbs +34 -0
  25. package/templates/full/wrangler.notifications.jsonc.hbs +23 -0
  26. package/templates/full/wrangler.pattern-discovery.jsonc.hbs +33 -0
  27. package/templates/full/wrangler.search.jsonc.hbs +16 -0
  28. package/templates/full/wrangler.settings.jsonc.hbs +23 -0
  29. package/templates/shared/README.md.hbs +69 -0
  30. package/templates/shared/config/budgets.yaml.hbs +72 -0
  31. package/templates/shared/config/services.yaml.hbs +45 -0
  32. package/templates/shared/migrations/001_core_tables.sql +117 -0
  33. package/templates/shared/migrations/002_usage_warehouse.sql +830 -0
  34. package/templates/shared/migrations/003_feature_tracking.sql +250 -0
  35. package/templates/shared/migrations/004_settings_alerts.sql +452 -0
  36. package/templates/shared/migrations/seed.sql.hbs +4 -0
  37. package/templates/shared/package.json.hbs +21 -0
  38. package/templates/shared/scripts/sync-config.ts +242 -0
  39. package/templates/shared/tsconfig.json +12 -0
  40. package/templates/shared/workers/lib/analytics-engine.ts +357 -0
  41. package/templates/shared/workers/lib/billing.ts +293 -0
  42. package/templates/shared/workers/lib/circuit-breaker-middleware.ts +25 -0
  43. package/templates/shared/workers/lib/control.ts +292 -0
  44. package/templates/shared/workers/lib/economics.ts +368 -0
  45. package/templates/shared/workers/lib/metrics.ts +103 -0
  46. package/templates/shared/workers/lib/platform-settings.ts +407 -0
  47. package/templates/shared/workers/lib/shared/allowances.ts +333 -0
  48. package/templates/shared/workers/lib/shared/cloudflare.ts +1362 -0
  49. package/templates/shared/workers/lib/shared/types.ts +58 -0
  50. package/templates/shared/workers/lib/telemetry-sampling.ts +360 -0
  51. package/templates/shared/workers/lib/usage/collectors/example.ts +96 -0
  52. package/templates/shared/workers/lib/usage/collectors/index.ts +128 -0
  53. package/templates/shared/workers/lib/usage/handlers/audit.ts +306 -0
  54. package/templates/shared/workers/lib/usage/handlers/backfill.ts +845 -0
  55. package/templates/shared/workers/lib/usage/handlers/behavioral.ts +429 -0
  56. package/templates/shared/workers/lib/usage/handlers/data-queries.ts +507 -0
  57. package/templates/shared/workers/lib/usage/handlers/dlq-admin.ts +364 -0
  58. package/templates/shared/workers/lib/usage/handlers/health-trends.ts +222 -0
  59. package/templates/shared/workers/lib/usage/handlers/index.ts +35 -0
  60. package/templates/shared/workers/lib/usage/handlers/usage-admin.ts +421 -0
  61. package/templates/shared/workers/lib/usage/handlers/usage-features.ts +1262 -0
  62. package/templates/shared/workers/lib/usage/handlers/usage-metrics.ts +2420 -0
  63. package/templates/shared/workers/lib/usage/handlers/usage-settings.ts +610 -0
  64. package/templates/shared/workers/lib/usage/queue/budget-enforcement.ts +1032 -0
  65. package/templates/shared/workers/lib/usage/queue/cost-budget-enforcement.ts +128 -0
  66. package/templates/shared/workers/lib/usage/queue/cost-calculator.ts +77 -0
  67. package/templates/shared/workers/lib/usage/queue/dlq-handler.ts +161 -0
  68. package/templates/shared/workers/lib/usage/queue/index.ts +19 -0
  69. package/templates/shared/workers/lib/usage/queue/telemetry-processor.ts +790 -0
  70. package/templates/shared/workers/lib/usage/scheduled/anomaly-detection.ts +732 -0
  71. package/templates/shared/workers/lib/usage/scheduled/data-collection.ts +956 -0
  72. package/templates/shared/workers/lib/usage/scheduled/error-digest.ts +343 -0
  73. package/templates/shared/workers/lib/usage/scheduled/index.ts +18 -0
  74. package/templates/shared/workers/lib/usage/scheduled/rollups.ts +1561 -0
  75. package/templates/shared/workers/lib/usage/shared/constants.ts +362 -0
  76. package/templates/shared/workers/lib/usage/shared/index.ts +14 -0
  77. package/templates/shared/workers/lib/usage/shared/types.ts +1066 -0
  78. package/templates/shared/workers/lib/usage/shared/utils.ts +795 -0
  79. package/templates/shared/workers/platform-usage.ts +1915 -0
  80. package/templates/shared/wrangler.usage.jsonc.hbs +58 -0
  81. package/templates/standard/migrations/005_error_collection.sql +162 -0
  82. package/templates/standard/workers/error-collector.ts +2670 -0
  83. package/templates/standard/workers/lib/error-collector/capture.ts +213 -0
  84. package/templates/standard/workers/lib/error-collector/digest.ts +448 -0
  85. package/templates/standard/workers/lib/error-collector/email-health-alerts.ts +262 -0
  86. package/templates/standard/workers/lib/error-collector/fingerprint.ts +258 -0
  87. package/templates/standard/workers/lib/error-collector/gap-alerts.ts +293 -0
  88. package/templates/standard/workers/lib/error-collector/github.ts +329 -0
  89. package/templates/standard/workers/lib/error-collector/types.ts +262 -0
  90. package/templates/standard/workers/lib/sentinel/gap-detection.ts +734 -0
  91. package/templates/standard/workers/lib/shared/slack-alerts.ts +585 -0
  92. package/templates/standard/workers/platform-sentinel.ts +1744 -0
  93. package/templates/standard/wrangler.error-collector.jsonc.hbs +44 -0
  94. package/templates/standard/wrangler.sentinel.jsonc.hbs +45 -0
@@ -0,0 +1,58 @@
1
+ {
2
+ "$schema": "./node_modules/wrangler/config-schema.json",
3
+ "name": "{{projectSlug}}-usage",
4
+ "main": "workers/platform-usage.ts",
5
+ "compatibility_date": "2026-01-01",
6
+ "compatibility_flags": ["nodejs_compat_v2"],
7
+ "observability": { "enabled": true },
8
+
9
+ // Cron: hourly data collection + midnight rollups
10
+ "triggers": {
11
+ "crons": ["0 * * * *", "0 0 * * *"]
12
+ },
13
+
14
+ "d1_databases": [
15
+ {
16
+ "binding": "PLATFORM_DB",
17
+ "database_name": "{{projectSlug}}-metrics",
18
+ "database_id": "YOUR_D1_DATABASE_ID",
19
+ "migrations_dir": "storage/d1/migrations"
20
+ }
21
+ ],
22
+
23
+ "kv_namespaces": [
24
+ {
25
+ "binding": "PLATFORM_CACHE",
26
+ "id": "YOUR_KV_NAMESPACE_ID"
27
+ }
28
+ ],
29
+
30
+ "queues": {
31
+ "consumers": [
32
+ {
33
+ "queue": "{{projectSlug}}-telemetry",
34
+ "max_batch_size": 100,
35
+ "max_batch_timeout": 30,
36
+ "dead_letter_queue": "{{projectSlug}}-telemetry-dlq",
37
+ "max_retries": 3
38
+ }
39
+ ],
40
+ "producers": [
41
+ { "binding": "TELEMETRY_QUEUE", "queue": "{{projectSlug}}-telemetry" },
42
+ { "binding": "TELEMETRY_DLQ", "queue": "{{projectSlug}}-telemetry-dlq" }
43
+ ]
44
+ },
45
+
46
+ "analytics_engine_datasets": [
47
+ { "binding": "PLATFORM_ANALYTICS", "dataset": "{{projectSlug}}-analytics" }
48
+ ],
49
+
50
+ "vars": {
51
+ "CLOUDFLARE_ACCOUNT_ID": "YOUR_CLOUDFLARE_ACCOUNT_ID"
52
+ }
53
+
54
+ // Uncomment when you add error-collector (standard tier):
55
+ // "services": [
56
+ // { "binding": "NOTIFICATIONS_API", "service": "{{projectSlug}}-notifications" }
57
+ // ]
58
+ }
@@ -0,0 +1,162 @@
1
+ -- =============================================================================
2
+ -- 005_error_collection.sql — Error tracking (standard tier)
3
+ -- =============================================================================
4
+ -- Consolidated from original migrations: 038, 039, 041, 043
5
+ --
6
+ -- Tables:
7
+ -- error_occurrences — Error tracking with deduplication and GitHub linkage
8
+ -- warning_digests — Daily digest tracking for P4 warnings
9
+ -- fingerprint_decisions — Fingerprint decision audit log for post-hoc analysis
10
+ --
11
+ -- The error_occurrences table uses the FINAL schema from migration 043 which
12
+ -- recreated the table with the correct CHECK constraint (adding 'pending_digest'
13
+ -- and 'digested' status values) and merged columns from 039 and 041.
14
+ -- =============================================================================
15
+
16
+
17
+ -- =============================================================================
18
+ -- ERROR OCCURRENCES (final schema from 043, merging 038 + 039 + 041)
19
+ -- =============================================================================
20
+ -- Error tracking table for deduplication, GitHub issue linkage, and history.
21
+ -- Includes digest columns (from 039) and error_category (from 041).
22
+
23
+ CREATE TABLE IF NOT EXISTS error_occurrences (
24
+ id TEXT PRIMARY KEY,
25
+ fingerprint TEXT NOT NULL,
26
+ script_name TEXT NOT NULL,
27
+ project TEXT NOT NULL,
28
+ error_type TEXT NOT NULL CHECK (error_type IN ('exception', 'cpu_limit', 'memory_limit', 'soft_error', 'warning')),
29
+ priority TEXT NOT NULL CHECK (priority IN ('P0', 'P1', 'P2', 'P3', 'P4')),
30
+
31
+ -- GitHub linkage
32
+ github_issue_number INTEGER,
33
+ github_issue_url TEXT,
34
+ github_repo TEXT NOT NULL,
35
+
36
+ -- Status tracking (includes 'pending_digest' and 'digested' from 043)
37
+ status TEXT DEFAULT 'open' CHECK (status IN ('open', 'resolved', 'wont_fix', 'pending_digest', 'digested')),
38
+ resolved_at INTEGER,
39
+ resolved_by TEXT, -- Commit SHA or 'auto-close'
40
+
41
+ -- Occurrence tracking
42
+ first_seen_at INTEGER NOT NULL,
43
+ last_seen_at INTEGER NOT NULL,
44
+ occurrence_count INTEGER DEFAULT 1,
45
+
46
+ -- Request context (last occurrence)
47
+ last_request_url TEXT,
48
+ last_request_method TEXT,
49
+ last_colo TEXT,
50
+ last_country TEXT,
51
+ last_cf_ray TEXT,
52
+
53
+ -- Error details (last occurrence)
54
+ last_exception_name TEXT,
55
+ last_exception_message TEXT,
56
+ last_logs_json TEXT, -- JSON array of last 20 log entries
57
+
58
+ -- Digest columns (from 039)
59
+ digest_date TEXT,
60
+ digest_issue_number INTEGER,
61
+ normalized_message TEXT,
62
+
63
+ -- Error category for transient error grouping (from 041)
64
+ error_category TEXT,
65
+
66
+ -- Timestamps
67
+ created_at INTEGER DEFAULT (unixepoch()),
68
+ updated_at INTEGER DEFAULT (unixepoch()),
69
+
70
+ UNIQUE(fingerprint)
71
+ );
72
+
73
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_status ON error_occurrences(status);
74
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_project ON error_occurrences(project);
75
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_script ON error_occurrences(script_name);
76
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_fingerprint ON error_occurrences(fingerprint);
77
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_last_seen ON error_occurrences(last_seen_at DESC);
78
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_priority ON error_occurrences(priority, status);
79
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_github ON error_occurrences(github_issue_number) WHERE github_issue_number IS NOT NULL;
80
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_pending_digest ON error_occurrences(status, error_type, script_name, fingerprint) WHERE status = 'pending_digest';
81
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_digest_date ON error_occurrences(digest_date, script_name) WHERE digest_date IS NOT NULL;
82
+ CREATE INDEX IF NOT EXISTS idx_error_occurrences_category ON error_occurrences(error_category) WHERE error_category IS NOT NULL;
83
+
84
+
85
+ -- =============================================================================
86
+ -- WARNING DIGESTS (from 039)
87
+ -- =============================================================================
88
+ -- Tracks daily digest issues. Allows finding/updating existing digest issues
89
+ -- for a given day to batch P4 warnings into single GitHub issues.
90
+
91
+ CREATE TABLE IF NOT EXISTS warning_digests (
92
+ id TEXT PRIMARY KEY,
93
+ digest_date TEXT NOT NULL, -- YYYY-MM-DD
94
+ script_name TEXT NOT NULL,
95
+ fingerprint TEXT NOT NULL, -- Normalised fingerprint (groups similar warnings)
96
+ normalized_message TEXT NOT NULL, -- Human-readable warning type
97
+ github_repo TEXT NOT NULL,
98
+ github_issue_number INTEGER,
99
+ github_issue_url TEXT,
100
+ occurrence_count INTEGER DEFAULT 0,
101
+ first_occurrence_at INTEGER NOT NULL,
102
+ last_occurrence_at INTEGER NOT NULL,
103
+ created_at INTEGER NOT NULL,
104
+ updated_at INTEGER NOT NULL,
105
+ UNIQUE(digest_date, script_name, fingerprint)
106
+ );
107
+
108
+ CREATE INDEX IF NOT EXISTS idx_warning_digests_lookup
109
+ ON warning_digests(digest_date, script_name);
110
+
111
+
112
+ -- =============================================================================
113
+ -- FINGERPRINT DECISIONS (from 041)
114
+ -- =============================================================================
115
+ -- Stores fingerprint decisions for post-hoc analysis of error classification.
116
+ -- Tracks why each error was handled the way it was.
117
+
118
+ CREATE TABLE IF NOT EXISTS fingerprint_decisions (
119
+ id TEXT PRIMARY KEY,
120
+ timestamp INTEGER NOT NULL DEFAULT (unixepoch()),
121
+
122
+ -- Context
123
+ script_name TEXT NOT NULL,
124
+ error_type TEXT NOT NULL,
125
+
126
+ -- Fingerprint details
127
+ raw_message TEXT, -- Original error message (first 500 chars)
128
+ normalized_message TEXT, -- Normalised message used for fingerprinting
129
+ computed_fingerprint TEXT NOT NULL,
130
+ category TEXT, -- Transient error category if classified
131
+
132
+ -- Decision outcome
133
+ decision TEXT NOT NULL CHECK (decision IN (
134
+ 'new_issue', -- Created new GitHub issue
135
+ 'existing_issue', -- Updated existing issue
136
+ 'transient_window', -- Transient error, issue exists for today
137
+ 'suppressed', -- Suppressed (e.g., muted issue)
138
+ 'rate_limited', -- Rate limited, no action taken
139
+ 'digest' -- Stored for daily digest
140
+ )),
141
+
142
+ -- GitHub linkage
143
+ github_issue_number INTEGER,
144
+ github_repo TEXT,
145
+
146
+ -- Metadata
147
+ is_transient INTEGER DEFAULT 0,
148
+ occurrence_count INTEGER,
149
+
150
+ created_at INTEGER DEFAULT (unixepoch())
151
+ );
152
+
153
+ CREATE INDEX IF NOT EXISTS idx_fingerprint_decisions_script
154
+ ON fingerprint_decisions(script_name, timestamp DESC);
155
+ CREATE INDEX IF NOT EXISTS idx_fingerprint_decisions_fingerprint
156
+ ON fingerprint_decisions(computed_fingerprint);
157
+ CREATE INDEX IF NOT EXISTS idx_fingerprint_decisions_category
158
+ ON fingerprint_decisions(category) WHERE category IS NOT NULL;
159
+ CREATE INDEX IF NOT EXISTS idx_fingerprint_decisions_decision
160
+ ON fingerprint_decisions(decision, timestamp DESC);
161
+ CREATE INDEX IF NOT EXISTS idx_fingerprint_decisions_timestamp
162
+ ON fingerprint_decisions(timestamp DESC);