services-as-software 0.1.0 → 2.0.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.
Files changed (78) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/CHANGELOG.md +10 -0
  3. package/README.md +235 -225
  4. package/dist/client.d.ts +25 -0
  5. package/dist/client.d.ts.map +1 -0
  6. package/dist/client.js +103 -0
  7. package/dist/client.js.map +1 -0
  8. package/dist/endpoint.d.ts +102 -0
  9. package/dist/endpoint.d.ts.map +1 -0
  10. package/dist/endpoint.js +96 -0
  11. package/dist/endpoint.js.map +1 -0
  12. package/dist/entities/billing.d.ts +60 -0
  13. package/dist/entities/billing.d.ts.map +1 -0
  14. package/dist/entities/billing.js +954 -0
  15. package/dist/entities/billing.js.map +1 -0
  16. package/dist/entities/customers.d.ts +45 -0
  17. package/dist/entities/customers.d.ts.map +1 -0
  18. package/dist/entities/customers.js +679 -0
  19. package/dist/entities/customers.js.map +1 -0
  20. package/dist/entities/delivery.d.ts +59 -0
  21. package/dist/entities/delivery.d.ts.map +1 -0
  22. package/dist/entities/delivery.js +890 -0
  23. package/dist/entities/delivery.js.map +1 -0
  24. package/dist/entities/index.d.ts +114 -0
  25. package/dist/entities/index.d.ts.map +1 -0
  26. package/dist/entities/index.js +89 -0
  27. package/dist/entities/index.js.map +1 -0
  28. package/dist/entities/operations.d.ts +59 -0
  29. package/dist/entities/operations.d.ts.map +1 -0
  30. package/dist/entities/operations.js +1010 -0
  31. package/dist/entities/operations.js.map +1 -0
  32. package/dist/entities/orchestration.d.ts +52 -0
  33. package/dist/entities/orchestration.d.ts.map +1 -0
  34. package/dist/entities/orchestration.js +883 -0
  35. package/dist/entities/orchestration.js.map +1 -0
  36. package/dist/entities/services.d.ts +50 -0
  37. package/dist/entities/services.d.ts.map +1 -0
  38. package/dist/entities/services.js +805 -0
  39. package/dist/entities/services.js.map +1 -0
  40. package/dist/helpers.d.ts +362 -0
  41. package/dist/helpers.d.ts.map +1 -0
  42. package/dist/helpers.js +400 -0
  43. package/dist/helpers.js.map +1 -0
  44. package/dist/index.d.ts +17 -215
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +18 -172
  47. package/dist/index.js.map +1 -0
  48. package/dist/provider.d.ts +85 -0
  49. package/dist/provider.d.ts.map +1 -0
  50. package/dist/provider.js +158 -0
  51. package/dist/provider.js.map +1 -0
  52. package/dist/service.d.ts +43 -0
  53. package/dist/service.d.ts.map +1 -0
  54. package/dist/service.js +206 -0
  55. package/dist/service.js.map +1 -0
  56. package/dist/types.d.ts +469 -0
  57. package/dist/types.d.ts.map +1 -0
  58. package/dist/types.js +5 -0
  59. package/dist/types.js.map +1 -0
  60. package/examples/client-usage.ts +82 -0
  61. package/examples/translation-service.ts +227 -0
  62. package/package.json +24 -38
  63. package/src/client.ts +132 -0
  64. package/src/endpoint.ts +144 -0
  65. package/src/entities/billing.ts +1037 -0
  66. package/src/entities/customers.ts +740 -0
  67. package/src/entities/delivery.ts +974 -0
  68. package/src/entities/index.ts +157 -0
  69. package/src/entities/operations.ts +1099 -0
  70. package/src/entities/orchestration.ts +956 -0
  71. package/src/entities/services.ts +872 -0
  72. package/src/helpers.ts +474 -0
  73. package/src/index.ts +97 -0
  74. package/src/provider.ts +183 -0
  75. package/src/service.test.ts +195 -0
  76. package/src/service.ts +266 -0
  77. package/src/types.ts +543 -0
  78. package/tsconfig.json +9 -0
package/src/types.ts ADDED
@@ -0,0 +1,543 @@
1
+ /**
2
+ * Core types for services-as-software
3
+ */
4
+
5
+ import type { AIFunctionDefinition, JSONSchema as AIJSONSchema } from 'ai-functions'
6
+
7
+ // Re-export JSONSchema for use in this package
8
+ export type JSONSchema = AIJSONSchema
9
+
10
+ // Use Promise for RPC interface definitions
11
+ type RpcPromise<T> = Promise<T>
12
+
13
+ /**
14
+ * Pricing models for services
15
+ */
16
+ export type PricingModel =
17
+ | 'free'
18
+ | 'fixed'
19
+ | 'per-use'
20
+ | 'subscription'
21
+ | 'tiered'
22
+ | 'usage-based'
23
+ | 'custom'
24
+
25
+ /**
26
+ * Billing intervals for subscriptions
27
+ */
28
+ export type BillingInterval = 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'custom'
29
+
30
+ /**
31
+ * Service status
32
+ */
33
+ export type ServiceStatus = 'active' | 'inactive' | 'deprecated' | 'beta' | 'alpha'
34
+
35
+ /**
36
+ * Order status
37
+ */
38
+ export type OrderStatus = 'pending' | 'processing' | 'completed' | 'failed' | 'cancelled'
39
+
40
+ /**
41
+ * Subscription status
42
+ */
43
+ export type SubscriptionStatus = 'active' | 'paused' | 'cancelled' | 'expired' | 'pending'
44
+
45
+ /**
46
+ * Currency code (ISO 4217)
47
+ */
48
+ export type Currency = 'USD' | 'EUR' | 'GBP' | 'JPY' | 'CNY' | string
49
+
50
+ /**
51
+ * Pricing configuration
52
+ */
53
+ export interface PricingConfig {
54
+ /** Pricing model type */
55
+ model: PricingModel
56
+ /** Base price (for fixed or subscription) */
57
+ basePrice?: number
58
+ /** Currency code */
59
+ currency?: Currency
60
+ /** Price per unit (for per-use or usage-based) */
61
+ pricePerUnit?: number
62
+ /** Billing interval (for subscriptions) */
63
+ interval?: BillingInterval
64
+ /** Usage tiers (for tiered pricing) */
65
+ tiers?: PricingTier[]
66
+ /** Free tier limits */
67
+ freeTier?: {
68
+ requests?: number
69
+ units?: number
70
+ resetInterval?: BillingInterval
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Pricing tier for tiered pricing models
76
+ */
77
+ export interface PricingTier {
78
+ /** Starting quantity for this tier */
79
+ from: number
80
+ /** Ending quantity (exclusive) - undefined means no upper limit */
81
+ to?: number
82
+ /** Price per unit in this tier */
83
+ pricePerUnit: number
84
+ /** Fixed price for this tier */
85
+ fixedPrice?: number
86
+ }
87
+
88
+ /**
89
+ * Service endpoint definition
90
+ */
91
+ export interface EndpointDefinition<TInput = unknown, TOutput = unknown> {
92
+ /** Endpoint name */
93
+ name: string
94
+ /** Description of what this endpoint does */
95
+ description?: string
96
+ /** HTTP method (for REST endpoints) */
97
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
98
+ /** Path pattern (e.g., '/users/:id') */
99
+ path?: string
100
+ /** Input schema */
101
+ input?: JSONSchema
102
+ /** Output schema */
103
+ output?: JSONSchema
104
+ /** Handler function */
105
+ handler: (input: TInput, context?: ServiceContext) => TOutput | Promise<TOutput>
106
+ /** Pricing specific to this endpoint */
107
+ pricing?: PricingConfig
108
+ /** Rate limiting */
109
+ rateLimit?: {
110
+ requests: number
111
+ window: number // milliseconds
112
+ }
113
+ /** Whether authentication is required */
114
+ requiresAuth?: boolean
115
+ }
116
+
117
+ /**
118
+ * Service context provided to endpoint handlers
119
+ */
120
+ export interface ServiceContext {
121
+ /** Customer/user ID */
122
+ customerId?: string
123
+ /** Subscription ID */
124
+ subscriptionId?: string
125
+ /** Request ID for tracing */
126
+ requestId: string
127
+ /** Request metadata */
128
+ metadata?: Record<string, unknown>
129
+ /** Entitlements for this customer */
130
+ entitlements: string[]
131
+ /** Usage tracking */
132
+ usage?: UsageTracker
133
+ }
134
+
135
+ /**
136
+ * Usage tracker for billing and analytics
137
+ */
138
+ export interface UsageTracker {
139
+ /** Track a usage event */
140
+ track(event: UsageEvent): Promise<void>
141
+ /** Get current usage for a customer */
142
+ get(customerId: string, period?: { start: Date; end: Date }): Promise<Usage>
143
+ }
144
+
145
+ /**
146
+ * Usage event
147
+ */
148
+ export interface UsageEvent {
149
+ /** Customer ID */
150
+ customerId: string
151
+ /** Endpoint or resource that was used */
152
+ resource: string
153
+ /** Quantity used */
154
+ quantity: number
155
+ /** Additional metadata */
156
+ metadata?: Record<string, unknown>
157
+ /** Timestamp */
158
+ timestamp?: Date
159
+ }
160
+
161
+ /**
162
+ * Usage summary
163
+ */
164
+ export interface Usage {
165
+ /** Customer ID */
166
+ customerId: string
167
+ /** Usage by resource */
168
+ byResource: Record<string, number>
169
+ /** Total usage */
170
+ total: number
171
+ /** Period start */
172
+ periodStart: Date
173
+ /** Period end */
174
+ periodEnd: Date
175
+ }
176
+
177
+ /**
178
+ * Service definition
179
+ */
180
+ export interface ServiceDefinition {
181
+ /** Service name */
182
+ name: string
183
+ /** Service version */
184
+ version: string
185
+ /** Description */
186
+ description?: string
187
+ /** Service status */
188
+ status?: ServiceStatus
189
+ /** Service endpoints */
190
+ endpoints: EndpointDefinition[]
191
+ /** Default pricing configuration */
192
+ pricing?: PricingConfig
193
+ /** Service-level functions (AI tools) */
194
+ functions?: AIFunctionDefinition[]
195
+ /** Event handlers */
196
+ events?: Record<string, EventHandler>
197
+ /** Scheduled tasks */
198
+ scheduled?: ScheduledTask[]
199
+ /** Subscription plans */
200
+ plans?: SubscriptionPlan[]
201
+ /** Entitlement definitions */
202
+ entitlements?: EntitlementDefinition[]
203
+ /** KPI definitions */
204
+ kpis?: KPIDefinition[]
205
+ /** OKR definitions */
206
+ okrs?: OKRDefinition[]
207
+ }
208
+
209
+ /**
210
+ * Event handler definition
211
+ */
212
+ export interface EventHandler<TPayload = unknown> {
213
+ /** Event name/pattern */
214
+ event: string
215
+ /** Handler function */
216
+ handler: (payload: TPayload, context?: ServiceContext) => void | Promise<void>
217
+ /** Whether to retry on failure */
218
+ retry?: boolean
219
+ /** Max retry attempts */
220
+ maxRetries?: number
221
+ }
222
+
223
+ /**
224
+ * Scheduled task definition
225
+ */
226
+ export interface ScheduledTask<TInput = unknown> {
227
+ /** Task name */
228
+ name: string
229
+ /** Cron expression or interval */
230
+ schedule: string
231
+ /** Task handler */
232
+ handler: (input?: TInput, context?: ServiceContext) => void | Promise<void>
233
+ /** Whether task is enabled */
234
+ enabled?: boolean
235
+ }
236
+
237
+ /**
238
+ * Subscription plan
239
+ */
240
+ export interface SubscriptionPlan {
241
+ /** Plan ID */
242
+ id: string
243
+ /** Plan name */
244
+ name: string
245
+ /** Description */
246
+ description?: string
247
+ /** Pricing configuration */
248
+ pricing: PricingConfig
249
+ /** Entitlements included */
250
+ entitlements: string[]
251
+ /** Features included */
252
+ features: string[]
253
+ /** Usage limits */
254
+ limits?: Record<string, number>
255
+ /** Trial period (days) */
256
+ trialDays?: number
257
+ }
258
+
259
+ /**
260
+ * Entitlement definition
261
+ */
262
+ export interface EntitlementDefinition {
263
+ /** Entitlement ID */
264
+ id: string
265
+ /** Human-readable name */
266
+ name: string
267
+ /** Description */
268
+ description?: string
269
+ /** Resource type this entitlement grants access to */
270
+ resource?: string
271
+ /** Actions permitted */
272
+ actions?: string[]
273
+ }
274
+
275
+ /**
276
+ * KPI (Key Performance Indicator) definition
277
+ */
278
+ export interface KPIDefinition {
279
+ /** KPI ID */
280
+ id: string
281
+ /** KPI name */
282
+ name: string
283
+ /** Description */
284
+ description?: string
285
+ /** How to calculate this KPI */
286
+ calculate: () => Promise<number | string>
287
+ /** Target value */
288
+ target?: number | string
289
+ /** Unit of measurement */
290
+ unit?: string
291
+ /** Update frequency */
292
+ updateInterval?: BillingInterval
293
+ }
294
+
295
+ /**
296
+ * OKR (Objectives and Key Results) definition
297
+ */
298
+ export interface OKRDefinition {
299
+ /** OKR ID */
300
+ id: string
301
+ /** Objective statement */
302
+ objective: string
303
+ /** Key results */
304
+ keyResults: KeyResult[]
305
+ /** Quarter or time period */
306
+ period?: string
307
+ /** Owner */
308
+ owner?: string
309
+ }
310
+
311
+ /**
312
+ * Key Result within an OKR
313
+ */
314
+ export interface KeyResult {
315
+ /** Key result description */
316
+ description: string
317
+ /** How to measure this result */
318
+ measure: () => Promise<number>
319
+ /** Target value */
320
+ target: number
321
+ /** Current value */
322
+ current?: number
323
+ /** Unit of measurement */
324
+ unit?: string
325
+ }
326
+
327
+ /**
328
+ * Order definition
329
+ */
330
+ export interface Order<TProduct = unknown> {
331
+ /** Order ID */
332
+ id: string
333
+ /** Customer ID */
334
+ customerId: string
335
+ /** Product or service being ordered */
336
+ product: TProduct
337
+ /** Quantity */
338
+ quantity: number
339
+ /** Total price */
340
+ total: number
341
+ /** Currency */
342
+ currency: Currency
343
+ /** Order status */
344
+ status: OrderStatus
345
+ /** Created timestamp */
346
+ createdAt: Date
347
+ /** Updated timestamp */
348
+ updatedAt: Date
349
+ /** Additional metadata */
350
+ metadata?: Record<string, unknown>
351
+ }
352
+
353
+ /**
354
+ * Quote definition
355
+ */
356
+ export interface Quote<TProduct = unknown> {
357
+ /** Quote ID */
358
+ id: string
359
+ /** Customer ID */
360
+ customerId: string
361
+ /** Product or service being quoted */
362
+ product: TProduct
363
+ /** Quantity */
364
+ quantity: number
365
+ /** Quoted price */
366
+ price: number
367
+ /** Currency */
368
+ currency: Currency
369
+ /** Valid until */
370
+ expiresAt: Date
371
+ /** Quote metadata */
372
+ metadata?: Record<string, unknown>
373
+ }
374
+
375
+ /**
376
+ * Subscription definition
377
+ */
378
+ export interface Subscription {
379
+ /** Subscription ID */
380
+ id: string
381
+ /** Customer ID */
382
+ customerId: string
383
+ /** Plan ID */
384
+ planId: string
385
+ /** Subscription status */
386
+ status: SubscriptionStatus
387
+ /** Current period start */
388
+ currentPeriodStart: Date
389
+ /** Current period end */
390
+ currentPeriodEnd: Date
391
+ /** Cancel at period end */
392
+ cancelAtPeriodEnd?: boolean
393
+ /** Trial end date */
394
+ trialEnd?: Date
395
+ /** Metadata */
396
+ metadata?: Record<string, unknown>
397
+ }
398
+
399
+ /**
400
+ * Notification definition
401
+ */
402
+ export interface Notification {
403
+ /** Notification ID */
404
+ id: string
405
+ /** Recipient(s) */
406
+ to: string | string[]
407
+ /** Subject */
408
+ subject: string
409
+ /** Message body */
410
+ body: string
411
+ /** Channel (email, slack, sms, etc.) */
412
+ channel: string
413
+ /** Priority */
414
+ priority?: 'low' | 'normal' | 'high' | 'urgent'
415
+ /** Metadata */
416
+ metadata?: Record<string, unknown>
417
+ }
418
+
419
+ /**
420
+ * Service client interface
421
+ */
422
+ export interface ServiceClient {
423
+ /** Ask a question to the service */
424
+ ask(question: string, context?: unknown): RpcPromise<string>
425
+
426
+ /** Deliver results */
427
+ deliver(orderId: string, results: unknown): RpcPromise<void>
428
+
429
+ /** Execute a task */
430
+ do(action: string, input?: unknown): RpcPromise<unknown>
431
+
432
+ /** Generate content */
433
+ generate(prompt: string, options?: unknown): RpcPromise<unknown>
434
+
435
+ /** Type checking/validation */
436
+ is(value: unknown, type: string | JSONSchema): RpcPromise<boolean>
437
+
438
+ /** Send notification */
439
+ notify(notification: Notification): RpcPromise<void>
440
+
441
+ /** Place an order */
442
+ order<TProduct>(product: TProduct, quantity: number): RpcPromise<Order<TProduct>>
443
+
444
+ /** Request a quote */
445
+ quote<TProduct>(product: TProduct, quantity: number): RpcPromise<Quote<TProduct>>
446
+
447
+ /** Subscribe to a plan */
448
+ subscribe(planId: string): RpcPromise<Subscription>
449
+
450
+ /** Get entitlements */
451
+ entitlements(): RpcPromise<string[]>
452
+
453
+ /** Get KPIs */
454
+ kpis(): RpcPromise<Record<string, number | string>>
455
+
456
+ /** Get OKRs */
457
+ okrs(): RpcPromise<OKRDefinition[]>
458
+ }
459
+
460
+ /**
461
+ * Service instance returned by Service()
462
+ */
463
+ export interface Service extends ServiceClient {
464
+ /** Service definition */
465
+ definition: ServiceDefinition
466
+
467
+ /** Call an endpoint directly */
468
+ call<TInput, TOutput>(
469
+ endpoint: string,
470
+ input: TInput,
471
+ context?: ServiceContext
472
+ ): RpcPromise<TOutput>
473
+
474
+ /** Register an event handler */
475
+ on<TPayload>(
476
+ event: string,
477
+ handler: (payload: TPayload, context?: ServiceContext) => void | Promise<void>
478
+ ): void
479
+
480
+ /** Schedule a recurring task */
481
+ every(
482
+ schedule: string,
483
+ handler: (context?: ServiceContext) => void | Promise<void>
484
+ ): void
485
+
486
+ /** Add a queue processor */
487
+ queue<TJob>(
488
+ name: string,
489
+ handler: (job: TJob, context?: ServiceContext) => void | Promise<void>
490
+ ): void
491
+
492
+ /** Get service as RPC target */
493
+ asRPC(): unknown
494
+
495
+ /** Get service as API routes */
496
+ asAPI(): unknown
497
+ }
498
+
499
+ /**
500
+ * Provider interface for services
501
+ */
502
+ export interface Provider {
503
+ /** Provider name */
504
+ name: string
505
+
506
+ /** Base URL */
507
+ baseUrl: string
508
+
509
+ /** Authentication configuration */
510
+ auth?: {
511
+ type: 'api-key' | 'oauth' | 'jwt' | 'basic'
512
+ credentials: Record<string, string>
513
+ }
514
+
515
+ /** Available services */
516
+ services: string[]
517
+
518
+ /** Get a service client */
519
+ service<T extends ServiceClient>(serviceName: string): T
520
+ }
521
+
522
+ /**
523
+ * Client configuration for connecting to remote services
524
+ */
525
+ export interface ClientConfig {
526
+ /** Service URL or provider */
527
+ url?: string
528
+
529
+ /** Provider instance */
530
+ provider?: Provider
531
+
532
+ /** Authentication */
533
+ auth?: {
534
+ type: 'api-key' | 'oauth' | 'jwt' | 'basic'
535
+ credentials: Record<string, string>
536
+ }
537
+
538
+ /** Custom headers */
539
+ headers?: Record<string, string>
540
+
541
+ /** Timeout (milliseconds) */
542
+ timeout?: number
543
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
9
+ }