@agirails/sdk 2.5.2 → 2.5.4
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/dist/ACTPClient.d.ts +18 -0
- package/dist/ACTPClient.d.ts.map +1 -1
- package/dist/ACTPClient.js +67 -22
- package/dist/ACTPClient.js.map +1 -1
- package/dist/adapters/BasicAdapter.d.ts +12 -0
- package/dist/adapters/BasicAdapter.d.ts.map +1 -1
- package/dist/adapters/BasicAdapter.js +30 -4
- package/dist/adapters/BasicAdapter.js.map +1 -1
- package/dist/adapters/StandardAdapter.d.ts +20 -3
- package/dist/adapters/StandardAdapter.d.ts.map +1 -1
- package/dist/adapters/StandardAdapter.js +45 -11
- package/dist/adapters/StandardAdapter.js.map +1 -1
- package/dist/cli/commands/publish.js +16 -4
- package/dist/cli/commands/publish.js.map +1 -1
- package/dist/cli/commands/register.js +16 -4
- package/dist/cli/commands/register.js.map +1 -1
- package/dist/cli/commands/tx.js +31 -3
- package/dist/cli/commands/tx.js.map +1 -1
- package/dist/cli/utils/client.d.ts.map +1 -1
- package/dist/cli/utils/client.js +1 -0
- package/dist/cli/utils/client.js.map +1 -1
- package/dist/config/networks.d.ts +2 -2
- package/dist/config/networks.d.ts.map +1 -1
- package/dist/config/networks.js +27 -22
- package/dist/config/networks.js.map +1 -1
- package/dist/level0/request.d.ts.map +1 -1
- package/dist/level0/request.js +2 -1
- package/dist/level0/request.js.map +1 -1
- package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
- package/dist/runtime/BlockchainRuntime.js +11 -5
- package/dist/runtime/BlockchainRuntime.js.map +1 -1
- package/dist/runtime/MockStateManager.d.ts.map +1 -1
- package/dist/runtime/MockStateManager.js +2 -1
- package/dist/runtime/MockStateManager.js.map +1 -1
- package/dist/utils/IPFSClient.d.ts +3 -1
- package/dist/utils/IPFSClient.d.ts.map +1 -1
- package/dist/utils/IPFSClient.js +27 -7
- package/dist/utils/IPFSClient.js.map +1 -1
- package/dist/wallet/AutoWalletProvider.d.ts.map +1 -1
- package/dist/wallet/AutoWalletProvider.js +52 -18
- package/dist/wallet/AutoWalletProvider.js.map +1 -1
- package/dist/wallet/SmartWalletRouter.d.ts +116 -0
- package/dist/wallet/SmartWalletRouter.d.ts.map +1 -0
- package/dist/wallet/SmartWalletRouter.js +212 -0
- package/dist/wallet/SmartWalletRouter.js.map +1 -0
- package/dist/wallet/aa/DualNonceManager.d.ts +19 -0
- package/dist/wallet/aa/DualNonceManager.d.ts.map +1 -1
- package/dist/wallet/aa/DualNonceManager.js +100 -5
- package/dist/wallet/aa/DualNonceManager.js.map +1 -1
- package/package.json +3 -6
- package/src/ACTPClient.ts +0 -1579
- package/src/abi/ACTPKernel.json +0 -1356
- package/src/abi/AgentRegistry.json +0 -915
- package/src/abi/ERC20.json +0 -40
- package/src/abi/EscrowVault.json +0 -134
- package/src/abi/IdentityRegistry.json +0 -316
- package/src/adapters/AdapterRegistry.ts +0 -173
- package/src/adapters/AdapterRouter.ts +0 -416
- package/src/adapters/BaseAdapter.ts +0 -498
- package/src/adapters/BasicAdapter.ts +0 -514
- package/src/adapters/IAdapter.ts +0 -292
- package/src/adapters/StandardAdapter.ts +0 -555
- package/src/adapters/X402Adapter.ts +0 -731
- package/src/adapters/index.ts +0 -60
- package/src/builders/DeliveryProofBuilder.ts +0 -327
- package/src/builders/QuoteBuilder.ts +0 -483
- package/src/builders/index.ts +0 -17
- package/src/cli/commands/balance.ts +0 -110
- package/src/cli/commands/batch.ts +0 -487
- package/src/cli/commands/config.ts +0 -231
- package/src/cli/commands/deploy-check.ts +0 -364
- package/src/cli/commands/deploy-env.ts +0 -120
- package/src/cli/commands/diff.ts +0 -141
- package/src/cli/commands/init.ts +0 -469
- package/src/cli/commands/mint.ts +0 -116
- package/src/cli/commands/pay.ts +0 -113
- package/src/cli/commands/publish.ts +0 -475
- package/src/cli/commands/pull.ts +0 -124
- package/src/cli/commands/register.ts +0 -247
- package/src/cli/commands/simulate.ts +0 -345
- package/src/cli/commands/time.ts +0 -302
- package/src/cli/commands/tx.ts +0 -448
- package/src/cli/commands/watch.ts +0 -211
- package/src/cli/index.ts +0 -134
- package/src/cli/utils/client.ts +0 -251
- package/src/cli/utils/config.ts +0 -389
- package/src/cli/utils/output.ts +0 -465
- package/src/cli/utils/wallet.ts +0 -109
- package/src/config/agirailsmd.ts +0 -262
- package/src/config/networks.ts +0 -275
- package/src/config/pendingPublish.ts +0 -237
- package/src/config/publishPipeline.ts +0 -359
- package/src/config/syncOperations.ts +0 -279
- package/src/erc8004/ERC8004Bridge.ts +0 -462
- package/src/erc8004/ReputationReporter.ts +0 -468
- package/src/erc8004/index.ts +0 -61
- package/src/errors/index.ts +0 -427
- package/src/index.ts +0 -364
- package/src/level0/Provider.ts +0 -117
- package/src/level0/ServiceDirectory.ts +0 -131
- package/src/level0/index.ts +0 -10
- package/src/level0/provide.ts +0 -132
- package/src/level0/request.ts +0 -432
- package/src/level1/Agent.ts +0 -1426
- package/src/level1/index.ts +0 -10
- package/src/level1/pricing/PriceCalculator.ts +0 -255
- package/src/level1/pricing/PricingStrategy.ts +0 -198
- package/src/level1/types/Job.ts +0 -179
- package/src/level1/types/Options.ts +0 -291
- package/src/level1/types/index.ts +0 -8
- package/src/protocol/ACTPKernel.ts +0 -808
- package/src/protocol/AgentRegistry.ts +0 -559
- package/src/protocol/DIDManager.ts +0 -629
- package/src/protocol/DIDResolver.ts +0 -554
- package/src/protocol/EASHelper.ts +0 -378
- package/src/protocol/EscrowVault.ts +0 -255
- package/src/protocol/EventMonitor.ts +0 -204
- package/src/protocol/MessageSigner.ts +0 -510
- package/src/protocol/ProofGenerator.ts +0 -339
- package/src/protocol/QuoteBuilder.ts +0 -15
- package/src/registry/AgentRegistryClient.ts +0 -202
- package/src/runtime/BlockchainRuntime.ts +0 -1015
- package/src/runtime/IACTPRuntime.ts +0 -306
- package/src/runtime/MockRuntime.ts +0 -1298
- package/src/runtime/MockStateManager.ts +0 -576
- package/src/runtime/index.ts +0 -25
- package/src/runtime/types/MockState.ts +0 -237
- package/src/storage/ArchiveBundleBuilder.ts +0 -561
- package/src/storage/ArweaveClient.ts +0 -946
- package/src/storage/FilebaseClient.ts +0 -790
- package/src/storage/index.ts +0 -96
- package/src/storage/types.ts +0 -348
- package/src/types/adapter.ts +0 -310
- package/src/types/agent.ts +0 -79
- package/src/types/did.ts +0 -223
- package/src/types/eip712.ts +0 -175
- package/src/types/erc8004.ts +0 -293
- package/src/types/escrow.ts +0 -27
- package/src/types/index.ts +0 -17
- package/src/types/message.ts +0 -145
- package/src/types/state.ts +0 -87
- package/src/types/transaction.ts +0 -69
- package/src/types/x402.ts +0 -251
- package/src/utils/ErrorRecoveryGuide.ts +0 -676
- package/src/utils/Helpers.ts +0 -688
- package/src/utils/IPFSClient.ts +0 -368
- package/src/utils/Logger.ts +0 -484
- package/src/utils/NonceManager.ts +0 -591
- package/src/utils/RateLimiter.ts +0 -534
- package/src/utils/ReceivedNonceTracker.ts +0 -567
- package/src/utils/SDKLifecycle.ts +0 -416
- package/src/utils/SecureNonce.ts +0 -78
- package/src/utils/Semaphore.ts +0 -276
- package/src/utils/UsedAttestationTracker.ts +0 -385
- package/src/utils/canonicalJson.ts +0 -38
- package/src/utils/circuitBreaker.ts +0 -324
- package/src/utils/computeTypeHash.ts +0 -48
- package/src/utils/fsSafe.ts +0 -80
- package/src/utils/index.ts +0 -80
- package/src/utils/retry.ts +0 -364
- package/src/utils/security.ts +0 -418
- package/src/utils/validation.ts +0 -540
- package/src/wallet/AutoWalletProvider.ts +0 -299
- package/src/wallet/EOAWalletProvider.ts +0 -69
- package/src/wallet/IWalletProvider.ts +0 -135
- package/src/wallet/aa/BundlerClient.ts +0 -274
- package/src/wallet/aa/DualNonceManager.ts +0 -173
- package/src/wallet/aa/PaymasterClient.ts +0 -174
- package/src/wallet/aa/TransactionBatcher.ts +0 -353
- package/src/wallet/aa/UserOpBuilder.ts +0 -246
- package/src/wallet/aa/constants.ts +0 -60
- package/src/wallet/keystore.ts +0 -240
package/src/level1/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Standard API - Agent-based interface
|
|
3
|
-
*
|
|
4
|
-
* @packageDocumentation
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
export { Agent, AgentConfig, AgentStatus, AgentStats, AgentBalance, ServiceConfig } from './Agent';
|
|
8
|
-
export * from './types';
|
|
9
|
-
export * from './pricing/PricingStrategy';
|
|
10
|
-
export { calculatePrice, DEFAULT_PRICING_STRATEGY } from './pricing/PriceCalculator';
|
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PriceCalculator - Calculate prices based on cost + margin model
|
|
3
|
-
*
|
|
4
|
-
* Implements the pricing logic for AGIRAILS agents to automatically
|
|
5
|
-
* calculate service prices and decide whether to accept jobs.
|
|
6
|
-
*
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { PricingStrategy, PriceCalculation } from './PricingStrategy';
|
|
11
|
-
import { Job } from '../types/Job';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Calculate price for a job based on pricing strategy
|
|
15
|
-
*
|
|
16
|
-
* This function calculates the total cost (base + per-unit), applies the
|
|
17
|
-
* desired profit margin, enforces min/max bounds, and determines whether
|
|
18
|
-
* to accept, counter-offer, or reject the job.
|
|
19
|
-
*
|
|
20
|
-
* @param strategy - Pricing strategy configuration
|
|
21
|
-
* @param job - Job to calculate price for
|
|
22
|
-
* @returns Price calculation result with decision
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```typescript
|
|
26
|
-
* const strategy: PricingStrategy = {
|
|
27
|
-
* cost: {
|
|
28
|
-
* base: 0.50,
|
|
29
|
-
* perUnit: { unit: 'word', rate: 0.005 }
|
|
30
|
-
* },
|
|
31
|
-
* margin: 0.40, // 40% profit margin
|
|
32
|
-
* minimum: 0.05,
|
|
33
|
-
* maximum: 10000
|
|
34
|
-
* };
|
|
35
|
-
*
|
|
36
|
-
* const job: Job = {
|
|
37
|
-
* id: '0x1234',
|
|
38
|
-
* service: 'translation',
|
|
39
|
-
* input: { text: 'Hello world' }, // 2 words
|
|
40
|
-
* budget: 10,
|
|
41
|
-
* deadline: new Date(),
|
|
42
|
-
* requester: '0xRequester',
|
|
43
|
-
* metadata: {}
|
|
44
|
-
* };
|
|
45
|
-
*
|
|
46
|
-
* const calc = calculatePrice(strategy, job);
|
|
47
|
-
* // calc.cost = 0.50 + (2 * 0.005) = 0.51
|
|
48
|
-
* // calc.price = 0.51 / (1 - 0.40) = 0.85
|
|
49
|
-
* // calc.profit = 0.85 - 0.51 = 0.34
|
|
50
|
-
* // calc.marginPercent = 0.34 / 0.85 = 0.40 (40%)
|
|
51
|
-
* // calc.decision = 'accept' (budget $10 >= price $0.85)
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
export function calculatePrice(strategy: PricingStrategy, job: Job): PriceCalculation {
|
|
55
|
-
// Calculate base cost
|
|
56
|
-
const baseCost = strategy.cost.base ?? 0;
|
|
57
|
-
|
|
58
|
-
// Calculate per-unit cost
|
|
59
|
-
let unitCost = 0;
|
|
60
|
-
let units: number | undefined;
|
|
61
|
-
if (strategy.cost.perUnit) {
|
|
62
|
-
units = estimateUnits(job, strategy.cost.perUnit.unit);
|
|
63
|
-
unitCost = units * strategy.cost.perUnit.rate;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Calculate API cost (future enhancement, not in MVP)
|
|
67
|
-
let apiCost: number | undefined;
|
|
68
|
-
if (strategy.cost.api) {
|
|
69
|
-
apiCost = estimateApiCost(strategy.cost.api, job);
|
|
70
|
-
// Add API cost to total cost
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Total cost = base + per-unit + api
|
|
74
|
-
const totalCost = baseCost + unitCost + (apiCost ?? 0);
|
|
75
|
-
|
|
76
|
-
// Apply margin: price = cost / (1 - margin)
|
|
77
|
-
// Example: cost=$10, margin=40% → price = $10 / 0.6 = $16.67
|
|
78
|
-
const margin = Math.max(0, Math.min(1, strategy.margin)); // Clamp to [0, 1]
|
|
79
|
-
let price = totalCost / (1 - margin);
|
|
80
|
-
|
|
81
|
-
// Enforce min/max bounds
|
|
82
|
-
const minimum = strategy.minimum ?? 0.05; // ACTP protocol minimum
|
|
83
|
-
const maximum = strategy.maximum ?? 10000;
|
|
84
|
-
price = Math.max(minimum, Math.min(maximum, price));
|
|
85
|
-
|
|
86
|
-
// Calculate actual profit and margin
|
|
87
|
-
const profit = price - totalCost;
|
|
88
|
-
const marginPercent = price > 0 ? profit / price : 0;
|
|
89
|
-
|
|
90
|
-
// Decide whether to accept the job based on budget
|
|
91
|
-
let decision: 'accept' | 'counter-offer' | 'reject';
|
|
92
|
-
let reason: string;
|
|
93
|
-
|
|
94
|
-
if (job.budget >= price) {
|
|
95
|
-
// Budget meets or exceeds our price - accept immediately
|
|
96
|
-
decision = 'accept';
|
|
97
|
-
reason = `Budget $${job.budget.toFixed(2)} >= price $${price.toFixed(2)}`;
|
|
98
|
-
} else if (job.budget >= totalCost) {
|
|
99
|
-
// Budget is below price but above cost (reduced profit)
|
|
100
|
-
// Use behavior configuration to decide
|
|
101
|
-
const belowPriceBehavior = strategy.behavior?.belowPrice ?? 'counter-offer';
|
|
102
|
-
decision = belowPriceBehavior;
|
|
103
|
-
reason = `Budget $${job.budget.toFixed(2)} below price $${price.toFixed(2)} but above cost $${totalCost.toFixed(2)}`;
|
|
104
|
-
} else {
|
|
105
|
-
// Budget is below cost (would lose money)
|
|
106
|
-
// Use behavior configuration to decide
|
|
107
|
-
const belowCostBehavior = strategy.behavior?.belowCost ?? 'reject';
|
|
108
|
-
decision = belowCostBehavior;
|
|
109
|
-
reason = `Budget $${job.budget.toFixed(2)} below cost $${totalCost.toFixed(2)} (would lose money)`;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
cost: totalCost,
|
|
114
|
-
price,
|
|
115
|
-
profit,
|
|
116
|
-
marginPercent,
|
|
117
|
-
decision,
|
|
118
|
-
reason,
|
|
119
|
-
breakdown: {
|
|
120
|
-
baseCost,
|
|
121
|
-
unitCost,
|
|
122
|
-
units,
|
|
123
|
-
apiCost,
|
|
124
|
-
},
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Estimate number of units in job input
|
|
130
|
-
*
|
|
131
|
-
* Supports different unit types (word, token, etc.) and extracts
|
|
132
|
-
* the relevant data from the job input.
|
|
133
|
-
*
|
|
134
|
-
* @param job - Job to estimate units for
|
|
135
|
-
* @param unit - Unit type (word, token, image, etc.)
|
|
136
|
-
* @returns Estimated number of units
|
|
137
|
-
*
|
|
138
|
-
* @internal
|
|
139
|
-
*/
|
|
140
|
-
function estimateUnits(job: Job, unit: string): number {
|
|
141
|
-
const input = job.input;
|
|
142
|
-
|
|
143
|
-
switch (unit.toLowerCase()) {
|
|
144
|
-
case 'word':
|
|
145
|
-
// Count words in text field
|
|
146
|
-
if (typeof input?.text === 'string') {
|
|
147
|
-
return input.text.split(/\s+/).filter((word: string) => word.length > 0).length;
|
|
148
|
-
}
|
|
149
|
-
// Fallback: count words in JSON string
|
|
150
|
-
return JSON.stringify(input).split(/\s+/).length;
|
|
151
|
-
|
|
152
|
-
case 'token':
|
|
153
|
-
// Rough estimate: 1 token ≈ 4 characters
|
|
154
|
-
if (typeof input?.text === 'string') {
|
|
155
|
-
return Math.ceil(input.text.length / 4);
|
|
156
|
-
}
|
|
157
|
-
return Math.ceil(JSON.stringify(input).length / 4);
|
|
158
|
-
|
|
159
|
-
case 'character':
|
|
160
|
-
case 'char':
|
|
161
|
-
// Count characters
|
|
162
|
-
if (typeof input?.text === 'string') {
|
|
163
|
-
return input.text.length;
|
|
164
|
-
}
|
|
165
|
-
return JSON.stringify(input).length;
|
|
166
|
-
|
|
167
|
-
case 'image':
|
|
168
|
-
case 'img':
|
|
169
|
-
// Count number of images
|
|
170
|
-
if (Array.isArray(input?.images)) {
|
|
171
|
-
return input.images.length;
|
|
172
|
-
}
|
|
173
|
-
if (input?.image || input?.imageUrl) {
|
|
174
|
-
return 1;
|
|
175
|
-
}
|
|
176
|
-
return 1; // Default to 1 image
|
|
177
|
-
|
|
178
|
-
case 'minute':
|
|
179
|
-
case 'min':
|
|
180
|
-
// Duration in minutes
|
|
181
|
-
if (typeof input?.duration === 'number') {
|
|
182
|
-
return input.duration;
|
|
183
|
-
}
|
|
184
|
-
if (typeof input?.durationMinutes === 'number') {
|
|
185
|
-
return input.durationMinutes;
|
|
186
|
-
}
|
|
187
|
-
return 1; // Default to 1 minute
|
|
188
|
-
|
|
189
|
-
case 'request':
|
|
190
|
-
case 'job':
|
|
191
|
-
// Flat per-request pricing
|
|
192
|
-
return 1;
|
|
193
|
-
|
|
194
|
-
default:
|
|
195
|
-
// Unknown unit type - default to 1
|
|
196
|
-
return 1;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Estimate API cost for a job
|
|
202
|
-
*
|
|
203
|
-
* MVP: Returns 0 for all APIs (placeholder for future implementation)
|
|
204
|
-
*
|
|
205
|
-
* Future implementation will include pricing for:
|
|
206
|
-
* - openai:gpt-4-turbo
|
|
207
|
-
* - openai:gpt-3.5-turbo
|
|
208
|
-
* - anthropic:claude-3-sonnet
|
|
209
|
-
* - anthropic:claude-3-haiku
|
|
210
|
-
*
|
|
211
|
-
* @param api - API identifier (e.g., 'openai:gpt-4-turbo')
|
|
212
|
-
* @param job - Job to estimate cost for
|
|
213
|
-
* @returns Estimated API cost in USDC
|
|
214
|
-
*
|
|
215
|
-
* @internal
|
|
216
|
-
*/
|
|
217
|
-
function estimateApiCost(_api: string, _job: Job): number {
|
|
218
|
-
// MVP: Returns 0 (API costs tracked externally by agent)
|
|
219
|
-
// V2: Integrate with OpenAI/Anthropic pricing APIs
|
|
220
|
-
|
|
221
|
-
// Example V2 implementation:
|
|
222
|
-
// const [provider, model] = api.split(':');
|
|
223
|
-
// if (provider === 'openai') {
|
|
224
|
-
// const inputTokens = estimateUnits(job, 'token');
|
|
225
|
-
// const pricing = {
|
|
226
|
-
// 'gpt-4-turbo': { input: 0.01 / 1000, output: 0.03 / 1000 },
|
|
227
|
-
// 'gpt-3.5-turbo': { input: 0.0005 / 1000, output: 0.0015 / 1000 }
|
|
228
|
-
// };
|
|
229
|
-
// return inputTokens * pricing[model].input * 2; // Assume 2x for output
|
|
230
|
-
// }
|
|
231
|
-
|
|
232
|
-
return 0;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Default pricing strategy
|
|
237
|
-
*
|
|
238
|
-
* Provides a sensible default for services that don't specify pricing.
|
|
239
|
-
* - Base cost: $0.05 (ACTP minimum)
|
|
240
|
-
* - Margin: 40% (healthy profit margin)
|
|
241
|
-
* - Behavior: Counter-offer when below price, reject when below cost
|
|
242
|
-
*/
|
|
243
|
-
export const DEFAULT_PRICING_STRATEGY: PricingStrategy = {
|
|
244
|
-
cost: {
|
|
245
|
-
base: 0.05, // ACTP protocol minimum
|
|
246
|
-
},
|
|
247
|
-
margin: 0.4, // 40% profit margin
|
|
248
|
-
minimum: 0.05,
|
|
249
|
-
maximum: 10000,
|
|
250
|
-
behavior: {
|
|
251
|
-
belowPrice: 'counter-offer',
|
|
252
|
-
belowCost: 'reject',
|
|
253
|
-
maxNegotiationRounds: 3,
|
|
254
|
-
},
|
|
255
|
-
};
|
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PricingStrategy - Cost + Margin pricing model
|
|
3
|
-
*
|
|
4
|
-
* Allows agents to define their costs and desired profit margin.
|
|
5
|
-
* SDK automatically calculates prices and decides whether to accept jobs.
|
|
6
|
-
*
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Cost configuration for a service
|
|
12
|
-
*/
|
|
13
|
-
export interface ServiceCost {
|
|
14
|
-
/**
|
|
15
|
-
* Fixed base cost per job (USDC)
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* base: 0.50 // $0.50 per job regardless of input size
|
|
19
|
-
*/
|
|
20
|
-
base?: number;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Per-unit cost
|
|
24
|
-
*
|
|
25
|
-
* Allows pricing based on input size (words, tokens, images, etc.)
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```typescript
|
|
29
|
-
* perUnit: {
|
|
30
|
-
* unit: 'word',
|
|
31
|
-
* rate: 0.005 // $0.005 per word
|
|
32
|
-
* }
|
|
33
|
-
* // For 1000-word translation: cost = 0.50 (base) + 1000 * 0.005 = $5.50
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
perUnit?: {
|
|
37
|
-
/**
|
|
38
|
-
* Unit type (word, token, image, minute, request, etc.)
|
|
39
|
-
*/
|
|
40
|
-
unit: string;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Cost per unit (USDC)
|
|
44
|
-
*/
|
|
45
|
-
rate: number;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* API cost auto-calculation
|
|
50
|
-
*
|
|
51
|
-
* Automatically calculate costs based on known API pricing.
|
|
52
|
-
* SDK knows the costs for common APIs (OpenAI, Anthropic, etc.)
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* ```typescript
|
|
56
|
-
* api: 'openai:gpt-4-turbo'
|
|
57
|
-
* // SDK automatically calculates cost based on input tokens
|
|
58
|
-
* ```
|
|
59
|
-
*
|
|
60
|
-
* Supported APIs (MVP):
|
|
61
|
-
* - 'openai:gpt-4-turbo'
|
|
62
|
-
* - 'openai:gpt-3.5-turbo'
|
|
63
|
-
* - 'anthropic:claude-3-sonnet'
|
|
64
|
-
* - 'anthropic:claude-3-haiku'
|
|
65
|
-
*/
|
|
66
|
-
api?: string;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Pricing behavior when job budget is below calculated price
|
|
71
|
-
*/
|
|
72
|
-
export type BelowPriceBehavior = 'reject' | 'counter-offer';
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Pricing behavior when job budget is below cost (would lose money)
|
|
76
|
-
*/
|
|
77
|
-
export type BelowCostBehavior = 'reject' | 'counter-offer';
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Pricing strategy configuration
|
|
81
|
-
*/
|
|
82
|
-
export interface PricingStrategy {
|
|
83
|
-
/**
|
|
84
|
-
* Cost configuration
|
|
85
|
-
*/
|
|
86
|
-
cost: ServiceCost;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Desired profit margin (0.0 - 1.0)
|
|
90
|
-
*
|
|
91
|
-
* This is the percentage of profit you want to make on top of your costs.
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* margin: 0.40 // 40% profit margin
|
|
95
|
-
*
|
|
96
|
-
* // If cost is $10, price will be:
|
|
97
|
-
* // price = cost / (1 - margin) = $10 / 0.6 = $16.67
|
|
98
|
-
* // profit = $16.67 - $10 = $6.67 (40% of $16.67)
|
|
99
|
-
*/
|
|
100
|
-
margin: number;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Minimum price in USDC
|
|
104
|
-
*
|
|
105
|
-
* Even if calculated price is lower, never accept less than this.
|
|
106
|
-
*
|
|
107
|
-
* @default 0.05 (ACTP protocol minimum)
|
|
108
|
-
*/
|
|
109
|
-
minimum?: number;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Maximum price in USDC
|
|
113
|
-
*
|
|
114
|
-
* Never charge more than this, even if calculated price is higher.
|
|
115
|
-
*
|
|
116
|
-
* @default 10000
|
|
117
|
-
*/
|
|
118
|
-
maximum?: number;
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Pricing behavior configuration
|
|
122
|
-
*/
|
|
123
|
-
behavior?: {
|
|
124
|
-
/**
|
|
125
|
-
* What to do when job budget is below calculated price but above cost
|
|
126
|
-
*
|
|
127
|
-
* - 'reject': Reject the job immediately
|
|
128
|
-
* - 'counter-offer': Respond with QUOTED state with your price
|
|
129
|
-
*
|
|
130
|
-
* @default 'counter-offer'
|
|
131
|
-
*/
|
|
132
|
-
belowPrice?: BelowPriceBehavior;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* What to do when job budget is below cost (would lose money)
|
|
136
|
-
*
|
|
137
|
-
* - 'reject': Reject the job immediately (recommended)
|
|
138
|
-
* - 'counter-offer': Respond with QUOTED state with your price
|
|
139
|
-
*
|
|
140
|
-
* @default 'reject'
|
|
141
|
-
*/
|
|
142
|
-
belowCost?: BelowCostBehavior;
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Maximum negotiation rounds
|
|
146
|
-
*
|
|
147
|
-
* How many times to counter-offer before giving up.
|
|
148
|
-
*
|
|
149
|
-
* @default 3
|
|
150
|
-
*/
|
|
151
|
-
maxNegotiationRounds?: number;
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Price calculation result
|
|
157
|
-
*/
|
|
158
|
-
export interface PriceCalculation {
|
|
159
|
-
/**
|
|
160
|
-
* Total cost (USDC)
|
|
161
|
-
*/
|
|
162
|
-
cost: number;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Calculated price with margin (USDC)
|
|
166
|
-
*/
|
|
167
|
-
price: number;
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Profit amount (USDC)
|
|
171
|
-
*/
|
|
172
|
-
profit: number;
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Profit margin percentage (0.0 - 1.0)
|
|
176
|
-
*/
|
|
177
|
-
marginPercent: number;
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Decision: should we accept the job at the offered budget?
|
|
181
|
-
*/
|
|
182
|
-
decision: 'accept' | 'counter-offer' | 'reject';
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Reason for decision
|
|
186
|
-
*/
|
|
187
|
-
reason: string;
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Breakdown of cost calculation (for debugging)
|
|
191
|
-
*/
|
|
192
|
-
breakdown: {
|
|
193
|
-
baseCost: number;
|
|
194
|
-
unitCost: number;
|
|
195
|
-
units?: number;
|
|
196
|
-
apiCost?: number;
|
|
197
|
-
};
|
|
198
|
-
}
|
package/src/level1/types/Job.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Job - Represents a work request from requester to provider
|
|
3
|
-
*
|
|
4
|
-
* This is the core data structure passed to service handlers
|
|
5
|
-
* in the Basic and Standard APIs.
|
|
6
|
-
*
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Job interface - work request data
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* const job: Job = {
|
|
16
|
-
* id: '0x1234...abcd',
|
|
17
|
-
* service: 'translation',
|
|
18
|
-
* input: { text: 'Hello', from: 'en', to: 'de' },
|
|
19
|
-
* budget: 10,
|
|
20
|
-
* deadline: new Date('2025-12-14T12:00:00Z'),
|
|
21
|
-
* requester: '0xRequester...',
|
|
22
|
-
* metadata: { priority: 'high' }
|
|
23
|
-
* }
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export interface Job {
|
|
27
|
-
/**
|
|
28
|
-
* Unique job identifier (transaction ID from ACTP protocol)
|
|
29
|
-
*/
|
|
30
|
-
id: string;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Service type being requested (e.g., 'translation', 'echo', 'image-gen')
|
|
34
|
-
*/
|
|
35
|
-
service: string;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Input data for the service (service-specific)
|
|
39
|
-
*
|
|
40
|
-
* This is the data the requester wants processed.
|
|
41
|
-
* Structure depends on the service type.
|
|
42
|
-
*/
|
|
43
|
-
input: any;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Budget in USDC (decimal format, e.g., 10 = $10.00)
|
|
47
|
-
*
|
|
48
|
-
* This is the maximum amount the requester is willing to pay.
|
|
49
|
-
*/
|
|
50
|
-
budget: number;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Deadline by which work must be completed
|
|
54
|
-
*/
|
|
55
|
-
deadline: Date;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Ethereum address of the requester
|
|
59
|
-
*/
|
|
60
|
-
requester: string;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Additional metadata (optional, service-specific)
|
|
64
|
-
*/
|
|
65
|
-
metadata: Record<string, any>;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Job handler function type
|
|
70
|
-
*
|
|
71
|
-
* This is the function signature providers must implement.
|
|
72
|
-
*
|
|
73
|
-
* @param job - The job to process
|
|
74
|
-
* @param context - Job execution context
|
|
75
|
-
* @returns Promise resolving to job result (service-specific)
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```typescript
|
|
79
|
-
* const handler: JobHandler = async (job, ctx) => {
|
|
80
|
-
* ctx.progress(50, 'Processing...');
|
|
81
|
-
* const result = await doWork(job.input);
|
|
82
|
-
* ctx.progress(100, 'Done!');
|
|
83
|
-
* return result;
|
|
84
|
-
* };
|
|
85
|
-
* ```
|
|
86
|
-
*/
|
|
87
|
-
export type JobHandler = (job: Job, context: JobContext) => Promise<any>;
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* JobContext - Execution context passed to job handlers
|
|
91
|
-
*
|
|
92
|
-
* Provides utilities for progress reporting, logging, state management,
|
|
93
|
-
* and cancellation handling.
|
|
94
|
-
*/
|
|
95
|
-
export interface JobContext {
|
|
96
|
-
/**
|
|
97
|
-
* Reference to the Agent instance processing this job
|
|
98
|
-
*/
|
|
99
|
-
agent: any; // Will be typed as Agent once implemented
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Report job progress
|
|
103
|
-
*
|
|
104
|
-
* @param percent - Progress percentage (0-100)
|
|
105
|
-
* @param message - Optional progress message
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* ```typescript
|
|
109
|
-
* ctx.progress(25, 'Downloaded input file');
|
|
110
|
-
* ctx.progress(50, 'Processing data');
|
|
111
|
-
* ctx.progress(100, 'Complete!');
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
|
-
progress(percent: number, message?: string): void;
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Logging utilities
|
|
118
|
-
*/
|
|
119
|
-
log: {
|
|
120
|
-
debug(message: string, meta?: any): void;
|
|
121
|
-
info(message: string, meta?: any): void;
|
|
122
|
-
warn(message: string, meta?: any): void;
|
|
123
|
-
error(message: string, meta?: any): void;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Job-scoped state storage
|
|
128
|
-
*
|
|
129
|
-
* Allows handlers to store intermediate state that persists
|
|
130
|
-
* across async operations (e.g., for resumable jobs).
|
|
131
|
-
*
|
|
132
|
-
* @example
|
|
133
|
-
* ```typescript
|
|
134
|
-
* // Store checkpoint
|
|
135
|
-
* ctx.state.set('lastProcessedIndex', 42);
|
|
136
|
-
*
|
|
137
|
-
* // Retrieve checkpoint
|
|
138
|
-
* const lastIndex = ctx.state.get<number>('lastProcessedIndex') || 0;
|
|
139
|
-
* ```
|
|
140
|
-
*/
|
|
141
|
-
state: {
|
|
142
|
-
get<T>(key: string): T | undefined;
|
|
143
|
-
set<T>(key: string, value: T): void;
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Whether the job has been cancelled
|
|
148
|
-
*
|
|
149
|
-
* Handlers should check this periodically for long-running jobs
|
|
150
|
-
* and exit gracefully if true.
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```typescript
|
|
154
|
-
* for (let i = 0; i < items.length; i++) {
|
|
155
|
-
* if (ctx.cancelled) {
|
|
156
|
-
* throw new Error('Job cancelled by requester');
|
|
157
|
-
* }
|
|
158
|
-
* await processItem(items[i]);
|
|
159
|
-
* }
|
|
160
|
-
* ```
|
|
161
|
-
*/
|
|
162
|
-
cancelled: boolean;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Register cancellation handler
|
|
166
|
-
*
|
|
167
|
-
* Called when the job is cancelled, allowing cleanup.
|
|
168
|
-
*
|
|
169
|
-
* @example
|
|
170
|
-
* ```typescript
|
|
171
|
-
* const stream = openFileStream();
|
|
172
|
-
* ctx.onCancel(() => {
|
|
173
|
-
* stream.close();
|
|
174
|
-
* console.log('Cleaned up resources');
|
|
175
|
-
* });
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
onCancel(handler: () => void): void;
|
|
179
|
-
}
|