@treeseed/sdk 0.10.11 → 0.10.13

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 (52) hide show
  1. package/README.md +2 -2
  2. package/dist/api/auth/d1-store.js +20 -1
  3. package/dist/capacity-provider.d.ts +53 -1
  4. package/dist/capacity.d.ts +80 -1
  5. package/dist/capacity.js +687 -8
  6. package/dist/db/d1.d.ts +109 -3227
  7. package/dist/db/index.d.ts +1 -0
  8. package/dist/db/index.js +1 -0
  9. package/dist/db/market-schema.d.ts +43769 -0
  10. package/dist/db/market-schema.js +1878 -0
  11. package/dist/db/node-sqlite.d.ts +109 -3227
  12. package/dist/db/schema.d.ts +226 -5757
  13. package/dist/db/schema.js +35 -226
  14. package/dist/index.d.ts +6 -4
  15. package/dist/index.js +32 -0
  16. package/dist/market-client.d.ts +135 -0
  17. package/dist/market-client.js +134 -1
  18. package/dist/operations/services/commit-message-provider.js +1 -1
  19. package/dist/operations/services/d1-migration.js +0 -59
  20. package/dist/operations/services/deploy.js +5 -1
  21. package/dist/operations/services/github-api.d.ts +83 -0
  22. package/dist/operations/services/github-api.js +167 -0
  23. package/dist/operations/services/local-dev.js +3 -3
  24. package/dist/operations/services/mailpit-runtime.d.ts +13 -2
  25. package/dist/operations/services/mailpit-runtime.js +19 -14
  26. package/dist/operations/services/project-web-monitor.d.ts +15 -0
  27. package/dist/operations/services/project-web-monitor.js +260 -0
  28. package/dist/operations/services/railway-api.js +2 -2
  29. package/dist/operations/services/release-candidate.js +9 -9
  30. package/dist/operations/services/runtime-paths.d.ts +1 -1
  31. package/dist/operations/services/runtime-paths.js +2 -2
  32. package/dist/operations/services/template-registry.js +10 -1
  33. package/dist/operations.d.ts +1 -0
  34. package/dist/operations.js +11 -1
  35. package/dist/platform-operation-store.d.ts +4 -0
  36. package/dist/platform-operation-store.js +29 -3
  37. package/dist/platform-operations.d.ts +8 -0
  38. package/dist/platform-operations.js +19 -0
  39. package/dist/remote.js +6 -6
  40. package/dist/scripts/tenant-d1-migrate-local.js +2 -3
  41. package/dist/scripts/test-cloudflare-local.js +1 -1
  42. package/dist/scripts/workspace-command-e2e.js +3 -1
  43. package/dist/sdk-types.d.ts +281 -3
  44. package/dist/sdk-types.js +5 -1
  45. package/dist/seeds/normalize.js +6 -0
  46. package/dist/seeds/schema.js +61 -1
  47. package/dist/seeds/types.d.ts +32 -0
  48. package/drizzle/d1/0000_treeseed_d1.sql +37 -0
  49. package/drizzle/market/0000_market_control_plane.sql +2929 -0
  50. package/drizzle/market/0001_capacity_budget_mode_default.sql +4 -0
  51. package/drizzle/market/0002_user_email_addresses.sql +26 -0
  52. package/package.json +8 -1
package/README.md CHANGED
@@ -20,7 +20,7 @@ If you are unsure, use `AgentSdk`.
20
20
 
21
21
  `AgentSdk` covers five main areas:
22
22
 
23
- - generic model reads and mutations across content-backed and D1-backed models
23
+ - generic model reads and mutations across content-backed models and the static-hub D1 form store
24
24
  - operational runtime state such as messages, runs, cursors, and leases
25
25
  - control-plane orchestration for work days, tasks, task events, graph runs, and reports
26
26
  - provider-neutral capacity scheduling contracts for task classification, admission, execution profiles, routing, estimates, planning proposals, attention load, utility, predictive reserve, hybrid execution, checkpoints, and usage actuals
@@ -49,7 +49,7 @@ const sdk = new AgentSdk({
49
49
  });
50
50
  ```
51
51
 
52
- Use `AgentSdk.createLocal()` when you want a local Wrangler-backed D1 database:
52
+ Use `AgentSdk.createLocal()` when you want the local static-hub D1 form store:
53
53
 
54
54
  ```ts
55
55
  import { AgentSdk } from '@treeseed/sdk/sdk';
@@ -310,6 +310,7 @@ class D1AuthStore {
310
310
  scopes: this.scopesForPrincipal(permissions),
311
311
  metadata: {
312
312
  ...parseJson(user.metadata_json, {}),
313
+ email: user.email ?? void 0,
313
314
  username: user.username ?? void 0
314
315
  }
315
316
  }
@@ -637,7 +638,11 @@ class D1AuthStore {
637
638
  expiresInSeconds: this.config.accessTokenTtlSeconds,
638
639
  principal: {
639
640
  ...principalRecord.principal,
640
- scopes: requestedScopes
641
+ scopes: requestedScopes,
642
+ metadata: {
643
+ ...principalRecord.principal.metadata,
644
+ sessionId
645
+ }
641
646
  }
642
647
  };
643
648
  }
@@ -865,6 +870,20 @@ class D1AuthStore {
865
870
  }
866
871
  const payload = verifyAccessToken(token, this.config.authSecret);
867
872
  if (!payload) return null;
873
+ const sessionId = typeof payload.metadata?.sessionId === "string" ? payload.metadata.sessionId.trim() : "";
874
+ if (sessionId) {
875
+ const session = await this.first(
876
+ `SELECT id, user_id, expires_at, revoked_at
877
+ FROM auth_sessions
878
+ WHERE id = ?`,
879
+ [sessionId]
880
+ );
881
+ const sessionExpiresAt = session ? new Date(session.expires_at).getTime() : 0;
882
+ if (!session || session.user_id !== payload.sub || session.revoked_at || !Number.isFinite(sessionExpiresAt) || sessionExpiresAt <= Date.now()) {
883
+ return null;
884
+ }
885
+ await this.run(`UPDATE auth_sessions SET updated_at = ? WHERE id = ?`, [isoNow(), session.id]);
886
+ }
868
887
  return {
869
888
  principal: principalFromAccessTokenPayload(payload),
870
889
  credential: {
@@ -1,3 +1,4 @@
1
+ import type { NativeUsageObservation } from './sdk-types.ts';
1
2
  export declare const CAPACITY_PROVIDER_ENDPOINTS: {
2
3
  readonly register: "/v1/provider/register";
3
4
  readonly heartbeat: "/v1/provider/heartbeat";
@@ -35,11 +36,59 @@ export interface CapacityProviderCapability {
35
36
  verification: string[];
36
37
  metadata?: Record<string, unknown>;
37
38
  }
39
+ export type NativeCapacityUnit = 'wall_minute' | 'quota_minute' | 'usd' | 'token' | 'request' | 'gpu_second' | 'human_minute' | 'custom' | string;
40
+ export type NativeCapacityLimitScope = 'daily' | 'weekly' | 'monthly' | 'session' | 'rolling_window' | string;
41
+ export type NativeCapacityConfidence = 'exact' | 'estimated' | 'opaque' | string;
42
+ export type NativeCapacityLimitSource = 'configured' | 'observed' | 'learned' | 'manual_override' | string;
43
+ export type ProviderQuotaVisibility = 'exact' | 'partial' | 'opaque' | string;
44
+ export interface ExecutionProviderNativeLimitCapacity {
45
+ id?: string;
46
+ scope?: NativeCapacityLimitScope;
47
+ limitScope?: NativeCapacityLimitScope;
48
+ nativeUnit?: NativeCapacityUnit;
49
+ limitAmount: number;
50
+ reserveBufferPercent?: number | null;
51
+ resetCadence?: string | null;
52
+ resetAt?: string | null;
53
+ confidence?: NativeCapacityConfidence;
54
+ source?: NativeCapacityLimitSource;
55
+ metadata?: Record<string, unknown>;
56
+ }
57
+ export interface ExecutionProviderObservationCapacity {
58
+ id?: string;
59
+ observedAt?: string;
60
+ health?: string;
61
+ activeWorkers?: number | null;
62
+ queuedTasks?: number | null;
63
+ throttleState?: string | null;
64
+ nativeRemaining?: Record<string, unknown>;
65
+ resetAt?: string | null;
66
+ confidence?: NativeCapacityConfidence;
67
+ metadata?: Record<string, unknown>;
68
+ }
69
+ export interface ExecutionProviderNativeCapacity {
70
+ id?: string;
71
+ name: string;
72
+ kind: string;
73
+ status?: string;
74
+ nativeUnit: NativeCapacityUnit;
75
+ quotaVisibility?: ProviderQuotaVisibility;
76
+ maxConcurrentWorkers?: number | null;
77
+ resetCadence?: string | null;
78
+ config?: Record<string, unknown>;
79
+ metadata?: Record<string, unknown>;
80
+ nativeLimits?: ExecutionProviderNativeLimitCapacity[];
81
+ observation?: ExecutionProviderObservationCapacity;
82
+ }
83
+ export interface CapacityProviderNativeCapacity {
84
+ executionProviders: ExecutionProviderNativeCapacity[];
85
+ }
38
86
  export interface CapacityProviderBudgetCapacity {
39
87
  dailyCreditBudget?: number | null;
40
88
  monthlyCreditBudget?: number | null;
41
89
  maxConcurrentWorkdays?: number | null;
42
90
  maxConcurrentRunners?: number | null;
91
+ nativeCapacity?: CapacityProviderNativeCapacity;
43
92
  [key: string]: unknown;
44
93
  }
45
94
  export interface CapacityProviderHealthState {
@@ -181,6 +230,7 @@ export interface ProviderUsageReport {
181
230
  projectId?: string | null;
182
231
  taskSignature?: string | null;
183
232
  executionProfileId?: string | null;
233
+ executionProviderId?: string | null;
184
234
  modelName?: string | null;
185
235
  inputTokens?: number | null;
186
236
  outputTokens?: number | null;
@@ -193,8 +243,10 @@ export interface ProviderUsageReport {
193
243
  diffLinesRemoved?: number | null;
194
244
  testRuns?: number | null;
195
245
  retryCount?: number | null;
196
- actualCredits: number;
246
+ actualCredits?: number | null;
247
+ actualCreditsOverride?: boolean | null;
197
248
  actualUsd?: number | null;
249
+ nativeUsage?: NativeUsageObservation | Record<string, unknown> | null;
198
250
  metadata?: Record<string, unknown> | null;
199
251
  }
200
252
  export interface ProviderUsageReportResponse {
@@ -1,5 +1,58 @@
1
- import type { AttentionEstimate, AttentionPolicy, CapacityEstimateConfidence, CapacityGrant, CapacityPlan, CapacityProvider, CapacityProviderLane, CapacityReservation, CreateCapacityReservationRequest, CreateCapacityRoutingDecisionRequest, ExecutionProfile, HybridExecutionPlan, PlannedTaskNode, PlanningAdmissionResult, PlanningPolicy, PredictiveReservePolicy, ProjectEnvironmentName, RecordCapacityUsageRequest, ReservePrediction, TaskAdmissionDecision, TaskAdmissionPolicy, TaskClassification, TaskEstimateProfile, TaskPlanProposal, TaskMutationScope, TaskUsageActual, UtilityEstimate, UtilityPolicy, WorkdayBudgetEnvelope } from './sdk-types.ts';
1
+ import type { AttentionEstimate, AttentionPolicy, CapacityEstimateConfidence, CapacityGrant, CapacityPlan, CapacityProvider, CapacityProviderLane, CapacityReservation, CreditConversionProfile, CreateCapacityReservationRequest, CreateCapacityRoutingDecisionRequest, DerivedCapacityAvailability, DerivedCapacityInput, ExecutionProfile, HybridExecutionPlan, NativeUsageObservation, PlannedTaskNode, PlanningAdmissionResult, PlanningPolicy, PredictiveReservePolicy, ProjectEnvironmentName, RecordCapacityUsageRequest, ReservePrediction, TaskAdmissionDecision, TaskAdmissionPolicy, TaskClassification, TaskEstimateProfile, TaskPlanProposal, TaskMutationScope, TaskUsageActual, UtilityEstimate, UtilityPolicy, WorkdayBudgetEnvelope } from './sdk-types.ts';
2
2
  import type { AgentProviderProfile } from './types/agents.ts';
3
+ export declare const ACTUAL_CREDIT_FORMULA_VERSION = "treeseed.actual-credits.v1";
4
+ export interface ActualCreditCalculationInput {
5
+ nativeUsage?: NativeUsageObservation | Record<string, unknown> | null;
6
+ conversionProfile?: CreditConversionProfile | null;
7
+ legacyActualCredits?: number | null;
8
+ actualCreditsOverride?: boolean | null;
9
+ reservedCredits?: number | null;
10
+ actualUsd?: number | null;
11
+ inputTokens?: number | null;
12
+ outputTokens?: number | null;
13
+ cachedInputTokens?: number | null;
14
+ quotaMinutes?: number | null;
15
+ wallMinutes?: number | null;
16
+ filesOpened?: number | null;
17
+ filesChanged?: number | null;
18
+ diffLinesAdded?: number | null;
19
+ diffLinesRemoved?: number | null;
20
+ testRuns?: number | null;
21
+ retryCount?: number | null;
22
+ source?: string | null;
23
+ }
24
+ export interface ActualCreditCalculation {
25
+ actualCredits: number;
26
+ formulaVersion: string;
27
+ source: 'central_calculator' | 'conversion_profile' | 'blended_conversion_profile' | 'legacy_override' | 'legacy_fallback' | 'reserved_fallback' | 'zero_fallback';
28
+ nativeUsage: NativeUsageObservation;
29
+ components: Record<string, number>;
30
+ partial: boolean;
31
+ interrupted: boolean;
32
+ conversionProfileId?: string | null;
33
+ conversionConfidence?: string | null;
34
+ }
35
+ export declare function nativeUsageUnit(input: NativeUsageObservation | Record<string, unknown> | null | undefined): string | null;
36
+ export declare function nativeUsageAmount(input: NativeUsageObservation | Record<string, unknown> | null | undefined, nativeUnit?: string | null): number | null;
37
+ export declare function selectCreditConversionProfile(input: {
38
+ profiles?: CreditConversionProfile[] | null;
39
+ taskSignature?: string | null;
40
+ executionProfileId?: string | null;
41
+ executionProviderKind?: string | null;
42
+ nativeUnit?: string | null;
43
+ }): CreditConversionProfile | null;
44
+ export declare function buildCreditConversionProfileFromActuals(input: {
45
+ taskSignature: string;
46
+ executionProfileId?: string | null;
47
+ executionProviderKind: string;
48
+ nativeUnit: string;
49
+ actuals: TaskUsageActual[];
50
+ formulaVersion?: string | null;
51
+ now?: Date | string | null;
52
+ id?: string | null;
53
+ }): CreditConversionProfile;
54
+ export declare function deriveAvailableCredits(input: DerivedCapacityInput): DerivedCapacityAvailability;
55
+ export declare function calculateActualCredits(input: ActualCreditCalculationInput): ActualCreditCalculation;
3
56
  export interface CapacityEstimateInput {
4
57
  taskSignature?: string | null;
5
58
  taskKind?: string | null;
@@ -37,6 +90,7 @@ export interface CapacityLaneScore {
37
90
  congestionPenalty?: number;
38
91
  attentionPenalty?: number;
39
92
  contextPenalty?: number;
93
+ nativePressurePenalty?: number;
40
94
  utilityScore?: number;
41
95
  utilityPerCredit?: number;
42
96
  predictedReserveImpact?: number;
@@ -157,13 +211,21 @@ export interface RouteAndReserveCandidate {
157
211
  providerId: string;
158
212
  laneId: string;
159
213
  grantId: string;
214
+ executionProviderId?: string | null;
160
215
  executionProfileId?: string | null;
216
+ nativeUnit?: string | null;
161
217
  remainingCredits: number | null;
218
+ staticRemainingCredits?: number | null;
219
+ derivedAvailableCredits?: number | null;
220
+ reservedNativeAmount?: number | null;
221
+ derivedCapacity?: DerivedCapacityAvailability | null;
222
+ derivedCapacityMode?: 'static' | 'hybrid' | 'derived';
162
223
  score: CapacityLaneScore;
163
224
  eligible: boolean;
164
225
  reasons: string[];
165
226
  estimate?: CapacityTaskEstimate;
166
227
  pressure?: CapacityRoutePressure;
228
+ nativePressure?: CapacityRouteNativePressure | null;
167
229
  qualityFit?: number;
168
230
  attentionEstimate?: AttentionEstimate;
169
231
  utilityEstimate?: UtilityEstimate;
@@ -172,6 +234,17 @@ export interface RouteAndReserveCandidate {
172
234
  successProbability?: number | null;
173
235
  spilloverReason?: string | null;
174
236
  }
237
+ export interface CapacityRouteNativePressure {
238
+ executionProviderId: string;
239
+ nativeUnit: string;
240
+ availableNativeAmount: number;
241
+ activeReservedNativeAmount: number;
242
+ reserveBufferNativeAmount: number;
243
+ reservedNativeAmount: number | null;
244
+ pressureRatio: number | null;
245
+ confidence: string;
246
+ reasons: string[];
247
+ }
175
248
  export interface CapacityRoutePressure {
176
249
  activeReservations: number;
177
250
  maxActiveReservations: number | null;
@@ -206,6 +279,12 @@ export type RouteAndReserveResult = {
206
279
  estimatedCreditsP50: number;
207
280
  estimatedCreditsP90: number;
208
281
  reservedCredits: number;
282
+ executionProviderId?: string | null;
283
+ nativeUnit?: string | null;
284
+ reservedNativeAmount?: number | null;
285
+ derivedAvailableCredits?: number | null;
286
+ derivedCapacityMode?: 'static' | 'hybrid' | 'derived';
287
+ nativePressure?: CapacityRouteNativePressure | null;
209
288
  executionProfileId?: string | null;
210
289
  costMultiplier?: number | null;
211
290
  score?: number | null;