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