@littlebearapps/platform-consumer-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.
- package/README.md +306 -0
- package/package.json +53 -0
- package/src/ai-gateway.ts +305 -0
- package/src/constants.ts +147 -0
- package/src/costs.ts +590 -0
- package/src/do-heartbeat.ts +249 -0
- package/src/dynamic-patterns.ts +273 -0
- package/src/errors.ts +285 -0
- package/src/features.ts +149 -0
- package/src/heartbeat.ts +27 -0
- package/src/index.ts +950 -0
- package/src/logging.ts +543 -0
- package/src/middleware.ts +447 -0
- package/src/patterns.ts +156 -0
- package/src/proxy.ts +732 -0
- package/src/retry.ts +19 -0
- package/src/service-client.ts +291 -0
- package/src/telemetry.ts +342 -0
- package/src/timeout.ts +212 -0
- package/src/tracing.ts +403 -0
- package/src/types.ts +465 -0
package/src/constants.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform SDK Constants
|
|
3
|
+
*
|
|
4
|
+
* KV key patterns and metric field names for the Platform SDK.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// =============================================================================
|
|
8
|
+
// KV KEY PATTERNS
|
|
9
|
+
// =============================================================================
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* KV key patterns for circuit breaker and configuration.
|
|
13
|
+
*
|
|
14
|
+
* New convention (Platform SDK):
|
|
15
|
+
* - CONFIG:FEATURE:{featureId}:STATUS -> GO | STOP
|
|
16
|
+
* - CONFIG:PROJECT:{projectId}:STATUS -> GO | STOP
|
|
17
|
+
* - CONFIG:GLOBAL:STATUS -> GO | STOP
|
|
18
|
+
* - CONFIG:FEATURE:{featureId}:BUDGET -> Budget config JSON
|
|
19
|
+
*
|
|
20
|
+
* Legacy convention (feature-budget.ts):
|
|
21
|
+
* - FEATURE:{key}:enabled -> 'true' | 'false'
|
|
22
|
+
* - FEATURE:{key}:disabled_reason -> string
|
|
23
|
+
* - CONFIG:BUDGETS -> Full budgets config
|
|
24
|
+
*/
|
|
25
|
+
export const KV_KEYS = {
|
|
26
|
+
// Circuit breaker status keys
|
|
27
|
+
featureStatus: (featureId: string) => `CONFIG:FEATURE:${featureId}:STATUS`,
|
|
28
|
+
projectStatus: (projectId: string) => `CONFIG:PROJECT:${projectId}:STATUS`,
|
|
29
|
+
globalStatus: () => 'CONFIG:GLOBAL:STATUS',
|
|
30
|
+
|
|
31
|
+
// Circuit breaker metadata
|
|
32
|
+
featureReason: (featureId: string) => `CONFIG:FEATURE:${featureId}:REASON`,
|
|
33
|
+
featureDisabledAt: (featureId: string) => `CONFIG:FEATURE:${featureId}:DISABLED_AT`,
|
|
34
|
+
featureAutoResetAt: (featureId: string) => `CONFIG:FEATURE:${featureId}:AUTO_RESET_AT`,
|
|
35
|
+
|
|
36
|
+
// Budget configuration
|
|
37
|
+
featureBudget: (featureId: string) => `CONFIG:FEATURE:${featureId}:BUDGET`,
|
|
38
|
+
defaultBudgets: () => 'CONFIG:BUDGETS:DEFAULTS',
|
|
39
|
+
|
|
40
|
+
// AI-specific circuit breaker (per-model limits)
|
|
41
|
+
featureAIStatus: (featureId: string) => `CONFIG:FEATURE:${featureId}:ai:STATUS`,
|
|
42
|
+
|
|
43
|
+
// Intelligent degradation (PID controller, throttling)
|
|
44
|
+
/** PID controller state: integral, prevError, lastUpdate */
|
|
45
|
+
pidState: (featureId: string) => `STATE:PID:${featureId}`,
|
|
46
|
+
/** Current throttle rate (0.0-1.0) for SDK consumption */
|
|
47
|
+
throttleRate: (featureId: string) => `CONFIG:FEATURE:${featureId}:THROTTLE_RATE`,
|
|
48
|
+
/** Reservoir sampling state for latency percentiles */
|
|
49
|
+
reservoirState: (featureId: string) => `STATE:RESERVOIR:${featureId}`,
|
|
50
|
+
|
|
51
|
+
// Legacy keys (for backwards compatibility during migration)
|
|
52
|
+
legacy: {
|
|
53
|
+
enabled: (key: string) => `FEATURE:${key}:enabled`,
|
|
54
|
+
disabledReason: (key: string) => `FEATURE:${key}:disabled_reason`,
|
|
55
|
+
disabledAt: (key: string) => `FEATURE:${key}:disabled_at`,
|
|
56
|
+
autoResetAt: (key: string) => `FEATURE:${key}:auto_reset_at`,
|
|
57
|
+
budgets: () => 'CONFIG:BUDGETS',
|
|
58
|
+
},
|
|
59
|
+
} as const;
|
|
60
|
+
|
|
61
|
+
// =============================================================================
|
|
62
|
+
// METRIC FIELD NAMES
|
|
63
|
+
// =============================================================================
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Ordered list of metric fields for Analytics Engine.
|
|
67
|
+
*
|
|
68
|
+
* IMPORTANT: Positions 1-12 are locked for backward compatibility with existing
|
|
69
|
+
* Analytics Engine data. New fields MUST be appended to positions 13+.
|
|
70
|
+
*
|
|
71
|
+
* Field positions map to Analytics Engine doubles:
|
|
72
|
+
* - double1-double12: Legacy fields (do not reorder)
|
|
73
|
+
* - double13-double20: Extended fields (append only, 20 field limit)
|
|
74
|
+
*/
|
|
75
|
+
export const METRIC_FIELDS = [
|
|
76
|
+
// === Legacy fields (positions 1-12) - DO NOT REORDER ===
|
|
77
|
+
'd1Writes', // double1
|
|
78
|
+
'd1Reads', // double2
|
|
79
|
+
'kvReads', // double3
|
|
80
|
+
'kvWrites', // double4
|
|
81
|
+
'doRequests', // double5
|
|
82
|
+
'doGbSeconds', // double6
|
|
83
|
+
'r2ClassA', // double7
|
|
84
|
+
'r2ClassB', // double8
|
|
85
|
+
'aiNeurons', // double9
|
|
86
|
+
'queueMessages', // double10
|
|
87
|
+
'requests', // double11
|
|
88
|
+
'cpuMs', // double12
|
|
89
|
+
|
|
90
|
+
// === Extended fields (positions 13-20) - APPEND ONLY ===
|
|
91
|
+
// NOTE: Analytics Engine only supports 20 double fields (double1-double20).
|
|
92
|
+
// vectorizeDeletes was removed to stay within limit.
|
|
93
|
+
'd1RowsRead', // double13
|
|
94
|
+
'd1RowsWritten', // double14
|
|
95
|
+
'kvDeletes', // double15
|
|
96
|
+
'kvLists', // double16
|
|
97
|
+
'aiRequests', // double17
|
|
98
|
+
'vectorizeQueries', // double18
|
|
99
|
+
'vectorizeInserts', // double19
|
|
100
|
+
'workflowInvocations', // double20
|
|
101
|
+
] as const;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Type for valid metric field names.
|
|
105
|
+
*/
|
|
106
|
+
export type MetricFieldName = (typeof METRIC_FIELDS)[number];
|
|
107
|
+
|
|
108
|
+
// =============================================================================
|
|
109
|
+
// CIRCUIT BREAKER DEFAULTS
|
|
110
|
+
// =============================================================================
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Default auto-reset interval for circuit breakers (1 hour in seconds).
|
|
114
|
+
*/
|
|
115
|
+
export const DEFAULT_AUTO_RESET_SECONDS = 3600;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Circuit breaker status values.
|
|
119
|
+
*/
|
|
120
|
+
export const CIRCUIT_STATUS = {
|
|
121
|
+
GO: 'GO',
|
|
122
|
+
STOP: 'STOP',
|
|
123
|
+
} as const;
|
|
124
|
+
|
|
125
|
+
// =============================================================================
|
|
126
|
+
// TELEMETRY DEFAULTS
|
|
127
|
+
// =============================================================================
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Maximum delay before flushing telemetry (in milliseconds).
|
|
131
|
+
* Telemetry is flushed immediately on request completion via waitUntil.
|
|
132
|
+
*/
|
|
133
|
+
export const TELEMETRY_FLUSH_DELAY_MS = 0;
|
|
134
|
+
|
|
135
|
+
// =============================================================================
|
|
136
|
+
// BINDING NAMES
|
|
137
|
+
// =============================================================================
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Expected binding names in worker environment.
|
|
141
|
+
*/
|
|
142
|
+
export const BINDING_NAMES = {
|
|
143
|
+
/** KV namespace for circuit breaker state */
|
|
144
|
+
PLATFORM_CACHE: 'PLATFORM_CACHE',
|
|
145
|
+
/** Queue for telemetry messages */
|
|
146
|
+
PLATFORM_TELEMETRY: 'PLATFORM_TELEMETRY',
|
|
147
|
+
} as const;
|