@avow-finance/spec 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/dist/agent-session.d.ts +8 -0
  2. package/dist/agent-session.d.ts.map +1 -1
  3. package/dist/api.d.ts +24 -0
  4. package/dist/api.d.ts.map +1 -1
  5. package/dist/dependencies.d.ts.map +1 -1
  6. package/dist/dependencies.js +8 -2
  7. package/dist/dependencies.js.map +1 -1
  8. package/dist/eip712.d.ts +212 -1
  9. package/dist/eip712.d.ts.map +1 -1
  10. package/dist/eip712.js +34 -0
  11. package/dist/eip712.js.map +1 -1
  12. package/dist/gpu-reservation.d.ts.map +1 -1
  13. package/dist/gpu-reservation.js +1 -1
  14. package/dist/gpu-reservation.js.map +1 -1
  15. package/dist/index.d.ts +4 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +4 -0
  18. package/dist/index.js.map +1 -1
  19. package/dist/job-session.d.ts +16 -0
  20. package/dist/job-session.d.ts.map +1 -1
  21. package/dist/job.d.ts.map +1 -1
  22. package/dist/job.js +1 -1
  23. package/dist/job.js.map +1 -1
  24. package/dist/json-fields.d.ts +2 -0
  25. package/dist/json-fields.d.ts.map +1 -1
  26. package/dist/json-fields.js +2 -0
  27. package/dist/json-fields.js.map +1 -1
  28. package/dist/mailbox.d.ts +77 -104
  29. package/dist/mailbox.d.ts.map +1 -1
  30. package/dist/mailbox.js +8 -30
  31. package/dist/mailbox.js.map +1 -1
  32. package/dist/negotiation.d.ts +514 -0
  33. package/dist/negotiation.d.ts.map +1 -0
  34. package/dist/negotiation.js +490 -0
  35. package/dist/negotiation.js.map +1 -0
  36. package/dist/offer-search.d.ts +282 -0
  37. package/dist/offer-search.d.ts.map +1 -0
  38. package/dist/offer-search.js +252 -0
  39. package/dist/offer-search.js.map +1 -0
  40. package/dist/order-wire.d.ts +73 -0
  41. package/dist/order-wire.d.ts.map +1 -0
  42. package/dist/order-wire.js +33 -0
  43. package/dist/order-wire.js.map +1 -0
  44. package/dist/reasons.d.ts +10 -2
  45. package/dist/reasons.d.ts.map +1 -1
  46. package/dist/reasons.js +10 -2
  47. package/dist/reasons.js.map +1 -1
  48. package/dist/sandbox.d.ts +81 -0
  49. package/dist/sandbox.d.ts.map +1 -1
  50. package/dist/sandbox.js +18 -0
  51. package/dist/sandbox.js.map +1 -1
  52. package/dist/service-order.d.ts +747 -0
  53. package/dist/service-order.d.ts.map +1 -0
  54. package/dist/service-order.js +182 -0
  55. package/dist/service-order.js.map +1 -0
  56. package/dist/service.d.ts +248 -0
  57. package/dist/service.d.ts.map +1 -0
  58. package/dist/service.js +379 -0
  59. package/dist/service.js.map +1 -0
  60. package/dist/session-fields.d.ts +8 -0
  61. package/dist/session-fields.d.ts.map +1 -1
  62. package/dist/terms.d.ts.map +1 -1
  63. package/dist/terms.js +4 -0
  64. package/dist/terms.js.map +1 -1
  65. package/dist/test-customer.d.ts +16 -0
  66. package/dist/test-customer.d.ts.map +1 -1
  67. package/dist/text.d.ts +5 -0
  68. package/dist/text.d.ts.map +1 -0
  69. package/dist/text.js +5 -0
  70. package/dist/text.js.map +1 -0
  71. package/dist/types.d.ts +7 -4
  72. package/dist/types.d.ts.map +1 -1
  73. package/dist/types.js +6 -3
  74. package/dist/types.js.map +1 -1
  75. package/dist/version.d.ts +1 -0
  76. package/dist/version.d.ts.map +1 -1
  77. package/dist/version.js +2 -0
  78. package/dist/version.js.map +1 -1
  79. package/dist/wire.d.ts +312 -0
  80. package/dist/wire.d.ts.map +1 -1
  81. package/dist/wire.js +28 -1
  82. package/dist/wire.js.map +1 -1
  83. package/package.json +1 -1
@@ -0,0 +1,282 @@
1
+ import type { Hex } from 'viem';
2
+ import { z } from 'zod';
3
+ import { type ServiceOfferQuote, type ServiceOfferVersion } from './service.js';
4
+ import { type Currency } from './types.js';
5
+ import type { Parsed } from './wire.js';
6
+ /**
7
+ * How an agent finds offers in Avow's directory, and how Avow answers. The agent sends an
8
+ * `OfferSearch` to `SANDBOX_PATHS.offerSearch`, signed by its key under its business's lock. Avow
9
+ * answers with an `OfferSearchResult` holding the listed offers that fit the search, the ones the
10
+ * agent's mandate allows first. When the agent asks, the ones its mandate doesn't allow yet follow
11
+ * in their own group, each with what its administrator would have to change, so an agent can find
12
+ * a new supplier and ask for it.
13
+ *
14
+ * A result grants nothing. Booking an offer checks the agent's mandate and funds again, and Avow
15
+ * refuses a version that was replaced (`offer.superseded`) or withdrawn (`offer.withdrawn`) since
16
+ * the search. Avow builds its answer with `offerSearchResult`, `parseOfferSearchResult` refuses an
17
+ * answer that isn't consistent with the search it echoes, and `offerSearchAnswerProblem` lets the
18
+ * agent check the answer is to its own search, its current mandate and the present moment.
19
+ */
20
+ /**
21
+ * The rule that orders each group of results. The lowest total price for the quantity comes first,
22
+ * then the soonest delivery, then the lowest version id, so equal offers always list the same way.
23
+ * Every offer in a result is in the search's currency, so totals compare directly. The reserve an
24
+ * offer takes shows beside its price and doesn't change its place.
25
+ */
26
+ export declare const OFFER_RANKING: 'total-then-delivery';
27
+ /** The longest a search's words may be, in characters. */
28
+ export declare const OFFER_QUERY_MAX = 200;
29
+ /** The most offers Avow lists in each group of a result. */
30
+ export declare const OFFER_RESULTS_MAX = 50;
31
+ /**
32
+ * The refusals of the mandate check that an administrator can lift, by approving a new mandate or
33
+ * daily limit, or by approving the booking itself where the mandate asks a person to approve
34
+ * bookings above an amount. An offer the check refuses for any other reason isn't listed.
35
+ */
36
+ export declare const APPROVAL_NEEDS: readonly ["approval.human_required", "mandate.supplier_not_allowed", "mandate.category_not_allowed", "mandate.over_commitment_cap", "mandate.over_open_reserves_cap", "mandate.over_payment_limit", "mandate.ends_before_window"];
37
+ export type ApprovalNeed = (typeof APPROVAL_NEEDS)[number];
38
+ /** What an agent is looking for and what it can spend. */
39
+ export interface OfferSearch {
40
+ /**
41
+ * Words that only filter. The query splits on whitespace, and each word has to appear somewhere
42
+ * in the item's name or summary, in any case, including inside a longer word. Null lists every
43
+ * offer that fits the rest, and a blank query reads as null.
44
+ */
45
+ query: string | null;
46
+ /** The most the agent will pay for the whole booking, in micros of `currency`. */
47
+ budget: bigint;
48
+ currency: Currency;
49
+ /** How many units the agent would book. */
50
+ quantity: number;
51
+ /** The latest the agent needs delivery, in unix seconds, or null when any time will do. */
52
+ deliverBy: bigint | null;
53
+ /** Whether to list the offers the agent's mandate doesn't allow yet. */
54
+ needingApproval: boolean;
55
+ }
56
+ /** One offer a search found, priced for the search's quantity. */
57
+ export interface OfferMatch {
58
+ /** The version's id, which `offerVersionId` gives and a booking names. */
59
+ id: Hex;
60
+ /** The version Avow would book, whose `supplier` is the supplier's business id. */
61
+ version: ServiceOfferVersion;
62
+ /**
63
+ * The supplier's name as its profile gives it. Every supplier listed today is a sandbox business
64
+ * that no check has verified, and the result says so on each one.
65
+ */
66
+ supplier: {
67
+ name: string;
68
+ sandbox: true;
69
+ };
70
+ /** What booking the search's quantity costs, with each charge and the reserve it takes. */
71
+ quote: ServiceOfferQuote;
72
+ /** When delivery would be due if the agent had booked when Avow checked, in unix seconds. */
73
+ deliverBy: bigint;
74
+ }
75
+ /** An offer the agent's mandate doesn't allow yet. */
76
+ export interface OfferNeedingApproval extends OfferMatch {
77
+ /**
78
+ * The first thing the mandate check refuses the offer for. It may not be the only one, so a
79
+ * change the administrator approves can bring up another.
80
+ */
81
+ need: ApprovalNeed;
82
+ }
83
+ /** One group of results, in ranking order, and whether more fitted than the group lists. */
84
+ export interface OfferGroup<T extends OfferMatch> {
85
+ offers: T[];
86
+ more: boolean;
87
+ }
88
+ /** Avow's answer to a search. */
89
+ export interface OfferSearchResult {
90
+ /** The search it answers, with what it left out filled in. */
91
+ search: OfferSearch;
92
+ ranking: typeof OFFER_RANKING;
93
+ /** When Avow checked the offers against the agent's mandate and funds, in unix seconds. */
94
+ checkedAt: bigint;
95
+ /** The mandate the offers were checked against, as `mandateHash` gives it. */
96
+ mandate: Hex;
97
+ /** The offers the agent's mandate allows now. */
98
+ eligible: OfferGroup<OfferMatch>;
99
+ /** The offers it doesn't allow yet, which list only when the search asked for them. */
100
+ needingApproval: OfferGroup<OfferNeedingApproval>;
101
+ }
102
+ /**
103
+ * Whether an offer version fits a search at `now`, leaving the agent's mandate aside. It's in the
104
+ * search's currency, its item has every word of the query, it takes the quantity, its total for
105
+ * that quantity is within the budget, and booked at `now` it delivers by the search's deadline. An
106
+ * offer whose delivery falls exactly on the deadline fits at `now` and no later, so a search with
107
+ * a deadline is best booked straight away.
108
+ */
109
+ export declare function offerFits(search: OfferSearch, version: ServiceOfferVersion, now: bigint): boolean;
110
+ /**
111
+ * An offer priced for a search, as Avow lists it. Throws for a version that doesn't fit the search
112
+ * at `checkedAt`, or a supplier name that's blank or longer than `SERVICE_NAME_MAX`.
113
+ */
114
+ export declare function offerMatch(search: OfferSearch, version: ServiceOfferVersion, supplierName: string, checkedAt: bigint): OfferMatch;
115
+ /** Orders offers by `OFFER_RANKING`, returning a new array. */
116
+ export declare function rankOffers<T extends OfferMatch>(offers: readonly T[]): T[];
117
+ /**
118
+ * Avow's answer to a search from the offers its mandate check sorted, each built with
119
+ * `offerMatch` at `checkedAt`. Each group is ranked and cut to `OFFER_RESULTS_MAX`, and offers
120
+ * needing approval are dropped unless the search asked for them.
121
+ */
122
+ export declare function offerSearchResult(input: {
123
+ search: OfferSearch;
124
+ checkedAt: bigint;
125
+ mandate: Hex;
126
+ eligible: readonly OfferMatch[];
127
+ needingApproval: readonly OfferNeedingApproval[];
128
+ }): OfferSearchResult;
129
+ /**
130
+ * What's inconsistent in Avow's answer, in words, or null when nothing is. Every offer fits the
131
+ * search the answer echoes at the time it was checked, with the id, price, charges, reserve and
132
+ * delivery that search gives it. Each group keeps to the ranking and the limit, and says it has
133
+ * more only when it's full. No version lists twice, and offers needing approval list only when the
134
+ * search asked for them.
135
+ */
136
+ export declare function offerSearchResultProblem(result: OfferSearchResult): string | null;
137
+ /**
138
+ * What stops an agent relying on Avow's answer, in words, or null when nothing does. The answer is
139
+ * to the search the agent sent, against the mandate it holds now, and was checked within
140
+ * `API_REQUEST_MAX_VALIDITY` of `now` by the agent's clock.
141
+ */
142
+ export declare function offerSearchAnswerProblem(sent: OfferSearch, result: OfferSearchResult, held: {
143
+ mandate: Hex;
144
+ now: bigint;
145
+ }): string | null;
146
+ /** A search as JSON. Words, quantity, deadline and the group needing approval may be left out. */
147
+ export declare const offerSearchJson: z.ZodObject<{
148
+ query: z.ZodOptional<z.ZodNullable<z.ZodString>>;
149
+ budget: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
150
+ currency: z.ZodEnum<{
151
+ USD: "USD";
152
+ }>;
153
+ quantity: z.ZodOptional<z.ZodNumber>;
154
+ deliverBy: z.ZodOptional<z.ZodNullable<z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>>>;
155
+ needingApproval: z.ZodOptional<z.ZodBoolean>;
156
+ }, z.core.$strict>;
157
+ /**
158
+ * Reads a search from JSON. Words left out, null, empty or only whitespace all read as no words,
159
+ * which lists every offer, and the quantity defaults to 1. A budget of nothing is refused.
160
+ */
161
+ export declare function parseOfferSearch(json: unknown): Parsed<OfferSearch>;
162
+ /** Avow's answer to a search as JSON, with amounts and times as decimal strings. */
163
+ export declare const offerSearchResultJson: z.ZodObject<{
164
+ search: z.ZodObject<{
165
+ query: z.ZodOptional<z.ZodNullable<z.ZodString>>;
166
+ budget: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
167
+ currency: z.ZodEnum<{
168
+ USD: "USD";
169
+ }>;
170
+ quantity: z.ZodOptional<z.ZodNumber>;
171
+ deliverBy: z.ZodOptional<z.ZodNullable<z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>>>;
172
+ needingApproval: z.ZodOptional<z.ZodBoolean>;
173
+ }, z.core.$strict>;
174
+ ranking: z.ZodLiteral<"total-then-delivery">;
175
+ checkedAt: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
176
+ mandate: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
177
+ eligible: z.ZodObject<{
178
+ offers: z.ZodArray<z.ZodObject<{
179
+ id: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
180
+ version: z.ZodObject<{
181
+ supplier: z.ZodString;
182
+ item: z.ZodObject<{
183
+ name: z.ZodString;
184
+ summary: z.ZodString;
185
+ }, z.core.$strict>;
186
+ currency: z.ZodEnum<{
187
+ USD: "USD";
188
+ }>;
189
+ unitPrice: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
190
+ rules: z.ZodObject<{
191
+ evidence: z.ZodString;
192
+ leadTime: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
193
+ maxPerBooking: z.ZodNumber;
194
+ dailyCapacity: z.ZodNumber;
195
+ freeCancelWindow: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
196
+ lateCancellationBps: z.ZodNumber;
197
+ noShowBps: z.ZodNumber;
198
+ payment: z.ZodObject<{
199
+ route: z.ZodEnum<{
200
+ invoice: "invoice";
201
+ transfer: "transfer";
202
+ }>;
203
+ dueAfterDelivery: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
204
+ }, z.core.$strict>;
205
+ }, z.core.$strict>;
206
+ }, z.core.$strict>;
207
+ supplier: z.ZodObject<{
208
+ name: z.ZodString;
209
+ sandbox: z.ZodLiteral<true>;
210
+ }, z.core.$strict>;
211
+ quote: z.ZodObject<{
212
+ total: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
213
+ lateCancellation: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
214
+ noShow: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
215
+ reserve: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
216
+ }, z.core.$strict>;
217
+ deliverBy: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
218
+ }, z.core.$strict>>;
219
+ more: z.ZodBoolean;
220
+ }, z.core.$strict>;
221
+ needingApproval: z.ZodObject<{
222
+ offers: z.ZodArray<z.ZodObject<{
223
+ id: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
224
+ version: z.ZodObject<{
225
+ supplier: z.ZodString;
226
+ item: z.ZodObject<{
227
+ name: z.ZodString;
228
+ summary: z.ZodString;
229
+ }, z.core.$strict>;
230
+ currency: z.ZodEnum<{
231
+ USD: "USD";
232
+ }>;
233
+ unitPrice: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
234
+ rules: z.ZodObject<{
235
+ evidence: z.ZodString;
236
+ leadTime: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
237
+ maxPerBooking: z.ZodNumber;
238
+ dailyCapacity: z.ZodNumber;
239
+ freeCancelWindow: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
240
+ lateCancellationBps: z.ZodNumber;
241
+ noShowBps: z.ZodNumber;
242
+ payment: z.ZodObject<{
243
+ route: z.ZodEnum<{
244
+ invoice: "invoice";
245
+ transfer: "transfer";
246
+ }>;
247
+ dueAfterDelivery: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
248
+ }, z.core.$strict>;
249
+ }, z.core.$strict>;
250
+ }, z.core.$strict>;
251
+ supplier: z.ZodObject<{
252
+ name: z.ZodString;
253
+ sandbox: z.ZodLiteral<true>;
254
+ }, z.core.$strict>;
255
+ quote: z.ZodObject<{
256
+ total: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
257
+ lateCancellation: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
258
+ noShow: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
259
+ reserve: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
260
+ }, z.core.$strict>;
261
+ deliverBy: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
262
+ need: z.ZodEnum<{
263
+ "approval.human_required": "approval.human_required";
264
+ "mandate.category_not_allowed": "mandate.category_not_allowed";
265
+ "mandate.ends_before_window": "mandate.ends_before_window";
266
+ "mandate.over_commitment_cap": "mandate.over_commitment_cap";
267
+ "mandate.over_open_reserves_cap": "mandate.over_open_reserves_cap";
268
+ "mandate.over_payment_limit": "mandate.over_payment_limit";
269
+ "mandate.supplier_not_allowed": "mandate.supplier_not_allowed";
270
+ }>;
271
+ }, z.core.$strict>>;
272
+ more: z.ZodBoolean;
273
+ }, z.core.$strict>;
274
+ }, z.core.$strict>;
275
+ /**
276
+ * Reads Avow's answer to a search from JSON, refusing one whose shape is wrong or that
277
+ * `offerSearchResultProblem` finds inconsistent, which includes a version `serviceOfferProblem`
278
+ * refuses. Whether it
279
+ * answers the search the agent sent is `offerSearchAnswerProblem`'s to say.
280
+ */
281
+ export declare function parseOfferSearchResult(json: unknown): Parsed<OfferSearchResult>;
282
+ //# sourceMappingURL=offer-search.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"offer-search.d.ts","sourceRoot":"","sources":["../src/offer-search.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM,CAAA;AAC/B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAIvB,OAAO,EAIL,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EAIzB,MAAM,cAAc,CAAA;AAErB,OAAO,EAAc,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAEvC;;;;;;;;;;;;;GAaG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,aAAa,EAAG,qBAA8B,CAAA;AAE3D,0DAA0D;AAC1D,eAAO,MAAM,eAAe,MAAM,CAAA;AAElC,4DAA4D;AAC5D,eAAO,MAAM,iBAAiB,KAAK,CAAA;AAEnC;;;;GAIG;AACH,eAAO,MAAM,cAAc,mOAQe,CAAA;AAC1C,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MA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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAOhC,CAAA;AAEF;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAS/E"}
@@ -0,0 +1,252 @@
1
+ import { z } from 'zod';
2
+ import { API_REQUEST_MAX_VALIDITY } from './api-request.js';
3
+ import { bytes32, u64, u256 } from './json-fields.js';
4
+ import { offerVersionId, SERVICE_NAME_MAX, SERVICE_QUANTITY_MAX, serviceOfferProblem, serviceOfferQuote, serviceOfferVersionJson, } from './service.js';
5
+ import { isFilled } from './text.js';
6
+ import { CURRENCIES } from './types.js';
7
+ /**
8
+ * How an agent finds offers in Avow's directory, and how Avow answers. The agent sends an
9
+ * `OfferSearch` to `SANDBOX_PATHS.offerSearch`, signed by its key under its business's lock. Avow
10
+ * answers with an `OfferSearchResult` holding the listed offers that fit the search, the ones the
11
+ * agent's mandate allows first. When the agent asks, the ones its mandate doesn't allow yet follow
12
+ * in their own group, each with what its administrator would have to change, so an agent can find
13
+ * a new supplier and ask for it.
14
+ *
15
+ * A result grants nothing. Booking an offer checks the agent's mandate and funds again, and Avow
16
+ * refuses a version that was replaced (`offer.superseded`) or withdrawn (`offer.withdrawn`) since
17
+ * the search. Avow builds its answer with `offerSearchResult`, `parseOfferSearchResult` refuses an
18
+ * answer that isn't consistent with the search it echoes, and `offerSearchAnswerProblem` lets the
19
+ * agent check the answer is to its own search, its current mandate and the present moment.
20
+ */
21
+ /**
22
+ * The rule that orders each group of results. The lowest total price for the quantity comes first,
23
+ * then the soonest delivery, then the lowest version id, so equal offers always list the same way.
24
+ * Every offer in a result is in the search's currency, so totals compare directly. The reserve an
25
+ * offer takes shows beside its price and doesn't change its place.
26
+ */
27
+ export const OFFER_RANKING = 'total-then-delivery';
28
+ /** The longest a search's words may be, in characters. */
29
+ export const OFFER_QUERY_MAX = 200;
30
+ /** The most offers Avow lists in each group of a result. */
31
+ export const OFFER_RESULTS_MAX = 50;
32
+ /**
33
+ * The refusals of the mandate check that an administrator can lift, by approving a new mandate or
34
+ * daily limit, or by approving the booking itself where the mandate asks a person to approve
35
+ * bookings above an amount. An offer the check refuses for any other reason isn't listed.
36
+ */
37
+ export const APPROVAL_NEEDS = [
38
+ 'approval.human_required',
39
+ 'mandate.supplier_not_allowed',
40
+ 'mandate.category_not_allowed',
41
+ 'mandate.over_commitment_cap',
42
+ 'mandate.over_open_reserves_cap',
43
+ 'mandate.over_payment_limit',
44
+ 'mandate.ends_before_window',
45
+ ];
46
+ const words = (query) => query === null ? [] : query.toLowerCase().split(/\s+/).filter(Boolean);
47
+ /**
48
+ * Whether an offer version fits a search at `now`, leaving the agent's mandate aside. It's in the
49
+ * search's currency, its item has every word of the query, it takes the quantity, its total for
50
+ * that quantity is within the budget, and booked at `now` it delivers by the search's deadline. An
51
+ * offer whose delivery falls exactly on the deadline fits at `now` and no later, so a search with
52
+ * a deadline is best booked straight away.
53
+ */
54
+ export function offerFits(search, version, now) {
55
+ if (serviceOfferProblem(version) !== null || version.currency !== search.currency)
56
+ return false;
57
+ const text = `${version.item.name} ${version.item.summary}`.toLowerCase();
58
+ if (!words(search.query).every((w) => text.includes(w)))
59
+ return false;
60
+ let quote;
61
+ try {
62
+ quote = serviceOfferQuote(version, search.quantity);
63
+ }
64
+ catch {
65
+ return false;
66
+ }
67
+ if (quote.total > search.budget)
68
+ return false;
69
+ return search.deliverBy === null || now + version.rules.leadTime <= search.deliverBy;
70
+ }
71
+ /**
72
+ * An offer priced for a search, as Avow lists it. Throws for a version that doesn't fit the search
73
+ * at `checkedAt`, or a supplier name that's blank or longer than `SERVICE_NAME_MAX`.
74
+ */
75
+ export function offerMatch(search, version, supplierName, checkedAt) {
76
+ if (!offerFits(search, version, checkedAt))
77
+ throw new RangeError("The offer doesn't fit the search");
78
+ if (!isFilled(supplierName, SERVICE_NAME_MAX))
79
+ throw new RangeError("The supplier's name is blank");
80
+ return {
81
+ id: offerVersionId(version),
82
+ version,
83
+ supplier: { name: supplierName, sandbox: true },
84
+ quote: serviceOfferQuote(version, search.quantity),
85
+ deliverBy: checkedAt + version.rules.leadTime,
86
+ };
87
+ }
88
+ /** Orders offers by `OFFER_RANKING`, returning a new array. */
89
+ export function rankOffers(offers) {
90
+ return [...offers].sort((a, b) => {
91
+ if (a.quote.total !== b.quote.total)
92
+ return a.quote.total < b.quote.total ? -1 : 1;
93
+ if (a.deliverBy !== b.deliverBy)
94
+ return a.deliverBy < b.deliverBy ? -1 : 1;
95
+ return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
96
+ });
97
+ }
98
+ const groupOf = (offers) => ({
99
+ offers: rankOffers(offers).slice(0, OFFER_RESULTS_MAX),
100
+ more: offers.length > OFFER_RESULTS_MAX,
101
+ });
102
+ /**
103
+ * Avow's answer to a search from the offers its mandate check sorted, each built with
104
+ * `offerMatch` at `checkedAt`. Each group is ranked and cut to `OFFER_RESULTS_MAX`, and offers
105
+ * needing approval are dropped unless the search asked for them.
106
+ */
107
+ export function offerSearchResult(input) {
108
+ return {
109
+ search: input.search,
110
+ ranking: OFFER_RANKING,
111
+ checkedAt: input.checkedAt,
112
+ mandate: input.mandate,
113
+ eligible: groupOf(input.eligible),
114
+ needingApproval: input.search.needingApproval
115
+ ? groupOf(input.needingApproval)
116
+ : { offers: [], more: false },
117
+ };
118
+ }
119
+ const sameMatch = (search, checkedAt, m) => {
120
+ if (!offerFits(search, m.version, checkedAt))
121
+ return false;
122
+ const quote = serviceOfferQuote(m.version, search.quantity);
123
+ return (m.id === offerVersionId(m.version) &&
124
+ m.deliverBy === checkedAt + m.version.rules.leadTime &&
125
+ m.quote.total === quote.total &&
126
+ m.quote.lateCancellation === quote.lateCancellation &&
127
+ m.quote.noShow === quote.noShow &&
128
+ m.quote.reserve === quote.reserve &&
129
+ isFilled(m.supplier.name, SERVICE_NAME_MAX));
130
+ };
131
+ const ranked = (offers) => rankOffers(offers).every((m, i) => m === offers[i]);
132
+ const groupProblem = (group) => (group.more && group.offers.length !== OFFER_RESULTS_MAX) || !ranked(group.offers);
133
+ /**
134
+ * What's inconsistent in Avow's answer, in words, or null when nothing is. Every offer fits the
135
+ * search the answer echoes at the time it was checked, with the id, price, charges, reserve and
136
+ * delivery that search gives it. Each group keeps to the ranking and the limit, and says it has
137
+ * more only when it's full. No version lists twice, and offers needing approval list only when the
138
+ * search asked for them.
139
+ */
140
+ export function offerSearchResultProblem(result) {
141
+ const { search, checkedAt, eligible, needingApproval } = result;
142
+ if (eligible.offers.length > OFFER_RESULTS_MAX ||
143
+ needingApproval.offers.length > OFFER_RESULTS_MAX) {
144
+ return `A group lists more than ${OFFER_RESULTS_MAX} offers.`;
145
+ }
146
+ const all = [...eligible.offers, ...needingApproval.offers];
147
+ if (!all.every((m) => sameMatch(search, checkedAt, m))) {
148
+ return "An offer doesn't match the search it answers.";
149
+ }
150
+ if (new Set(all.map((m) => m.id)).size !== all.length)
151
+ return 'An offer is listed twice.';
152
+ if (!search.needingApproval && (needingApproval.offers.length > 0 || needingApproval.more)) {
153
+ return "Offers needing approval are listed though the search didn't ask for them.";
154
+ }
155
+ if (groupProblem(eligible) || groupProblem(needingApproval)) {
156
+ return "A group isn't in the ranking's order, or says it has more when it isn't full.";
157
+ }
158
+ return null;
159
+ }
160
+ /**
161
+ * What stops an agent relying on Avow's answer, in words, or null when nothing does. The answer is
162
+ * to the search the agent sent, against the mandate it holds now, and was checked within
163
+ * `API_REQUEST_MAX_VALIDITY` of `now` by the agent's clock.
164
+ */
165
+ export function offerSearchAnswerProblem(sent, result, held) {
166
+ const s = result.search;
167
+ const same = s.query === sent.query &&
168
+ s.budget === sent.budget &&
169
+ s.currency === sent.currency &&
170
+ s.quantity === sent.quantity &&
171
+ s.deliverBy === sent.deliverBy &&
172
+ s.needingApproval === sent.needingApproval;
173
+ if (!same)
174
+ return 'The answer is to another search.';
175
+ if (result.mandate !== held.mandate.toLowerCase()) {
176
+ return 'The answer was checked against another mandate.';
177
+ }
178
+ const gap = result.checkedAt > held.now ? result.checkedAt - held.now : held.now - result.checkedAt;
179
+ if (gap > API_REQUEST_MAX_VALIDITY)
180
+ return "The answer wasn't checked just now.";
181
+ return null;
182
+ }
183
+ /** A search as JSON. Words, quantity, deadline and the group needing approval may be left out. */
184
+ export const offerSearchJson = z.strictObject({
185
+ query: z.string().max(OFFER_QUERY_MAX).nullable().optional(),
186
+ budget: u256,
187
+ currency: z.enum(CURRENCIES),
188
+ quantity: z.number().int().min(1).max(SERVICE_QUANTITY_MAX).optional(),
189
+ deliverBy: u64.nullable().optional(),
190
+ needingApproval: z.boolean().optional(),
191
+ });
192
+ const searchOf = (data) => {
193
+ if (data.budget === 0n)
194
+ return null;
195
+ const query = data.query?.trim();
196
+ return {
197
+ query: query ? query : null,
198
+ budget: data.budget,
199
+ currency: data.currency,
200
+ quantity: data.quantity ?? 1,
201
+ deliverBy: data.deliverBy ?? null,
202
+ needingApproval: data.needingApproval ?? false,
203
+ };
204
+ };
205
+ /**
206
+ * Reads a search from JSON. Words left out, null, empty or only whitespace all read as no words,
207
+ * which lists every offer, and the quantity defaults to 1. A budget of nothing is refused.
208
+ */
209
+ export function parseOfferSearch(json) {
210
+ const parsed = offerSearchJson.safeParse(json);
211
+ const search = parsed.success ? searchOf(parsed.data) : null;
212
+ return search ? { ok: true, value: search } : { ok: false, reason: 'message.invalid' };
213
+ }
214
+ const offerMatchJson = z.strictObject({
215
+ id: bytes32,
216
+ version: serviceOfferVersionJson,
217
+ supplier: z.strictObject({
218
+ name: z.string().min(1).max(SERVICE_NAME_MAX),
219
+ sandbox: z.literal(true),
220
+ }),
221
+ quote: z.strictObject({ total: u256, lateCancellation: u256, noShow: u256, reserve: u256 }),
222
+ deliverBy: u64,
223
+ });
224
+ const groupJson = (offer) => z.strictObject({ offers: z.array(offer).max(OFFER_RESULTS_MAX), more: z.boolean() });
225
+ /** Avow's answer to a search as JSON, with amounts and times as decimal strings. */
226
+ export const offerSearchResultJson = z.strictObject({
227
+ search: offerSearchJson,
228
+ ranking: z.literal(OFFER_RANKING),
229
+ checkedAt: u64,
230
+ mandate: bytes32,
231
+ eligible: groupJson(offerMatchJson),
232
+ needingApproval: groupJson(offerMatchJson.extend({ need: z.enum(APPROVAL_NEEDS) })),
233
+ });
234
+ /**
235
+ * Reads Avow's answer to a search from JSON, refusing one whose shape is wrong or that
236
+ * `offerSearchResultProblem` finds inconsistent, which includes a version `serviceOfferProblem`
237
+ * refuses. Whether it
238
+ * answers the search the agent sent is `offerSearchAnswerProblem`'s to say.
239
+ */
240
+ export function parseOfferSearchResult(json) {
241
+ const parsed = offerSearchResultJson.safeParse(json);
242
+ if (!parsed.success)
243
+ return { ok: false, reason: 'message.invalid' };
244
+ const search = searchOf(parsed.data.search);
245
+ if (search === null)
246
+ return { ok: false, reason: 'message.invalid' };
247
+ const result = { ...parsed.data, search };
248
+ return offerSearchResultProblem(result) === null
249
+ ? { ok: true, value: result }
250
+ : { ok: false, reason: 'message.invalid' };
251
+ }
252
+ //# sourceMappingURL=offer-search.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"offer-search.js","sourceRoot":"","sources":["../src/offer-search.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAA;AAC3D,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAErD,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EAGpB,mBAAmB,EACnB,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,EAAE,UAAU,EAAiB,MAAM,YAAY,CAAA;AAGtD;;;;;;;;;;;;;GAaG;AAEH;;;;;GAKG;AACH,MAAM,CAAC,MAA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,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE;QAC7B,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAA;AAC9C,CAAC"}
@@ -0,0 +1,73 @@
1
+ import type { Hex } from 'viem';
2
+ import { z } from 'zod';
3
+ /**
4
+ * What every kind of order the sandbox mailbox carries shares on the wire: how a business is
5
+ * named, how a funded reserve is reported, and the states an order passes through. A job order and
6
+ * a service order each add their own request and quote.
7
+ */
8
+ export declare const businessIdJson: z.ZodString;
9
+ /**
10
+ * The reserve the buyer funded, as Avow read it from the buyer's lock. Avow reads the lock itself,
11
+ * so the transaction is there only when the agent saw it land, and null after a restart.
12
+ */
13
+ export interface MailboxReserve {
14
+ fundedIn: Hex | null;
15
+ amount: bigint;
16
+ /** The lock's deadline for the supplier to accept, after which the reserve goes back. */
17
+ acceptBy: bigint;
18
+ }
19
+ export declare const mailboxReserveJson: z.ZodObject<{
20
+ fundedIn: z.ZodNullable<z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>>;
21
+ amount: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
22
+ acceptBy: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
23
+ }, z.core.$strict>;
24
+ /** Where an order stands, with the quote its kind of order carries once the supplier has sent one. */
25
+ export type OrderStage<Quote> = {
26
+ status: 'requested';
27
+ } | {
28
+ status: 'expired';
29
+ quote: Quote | null;
30
+ } | {
31
+ status: 'quoted';
32
+ quote: Quote;
33
+ } | {
34
+ status: 'reserved';
35
+ quote: Quote;
36
+ reserve: MailboxReserve;
37
+ } | {
38
+ status: 'accepted';
39
+ quote: Quote;
40
+ reserve: MailboxReserve;
41
+ /** The acceptance's transaction when the supplier's agent saw it land, or null. */
42
+ acceptedIn: Hex | null;
43
+ };
44
+ /** An order's states as JSON, over the fields its kind of order holds and its kind of quote. */
45
+ export declare const orderStagesJson: <Base extends z.ZodRawShape, Quote extends z.ZodType>(base: Base, quote: Quote) => z.ZodDiscriminatedUnion<[z.ZodObject<Base & {
46
+ status: z.ZodLiteral<"requested">;
47
+ } extends infer T ? { -readonly [P in keyof T]: T[P]; } : never, z.core.$strict>, z.ZodObject<Base & {
48
+ status: z.ZodLiteral<"expired">;
49
+ quote: z.ZodNullable<Quote>;
50
+ } extends infer T_1 ? { -readonly [P in keyof T_1]: T_1[P]; } : never, z.core.$strict>, z.ZodObject<Base & {
51
+ status: z.ZodLiteral<"quoted">;
52
+ quote: Quote;
53
+ } extends infer T_2 ? { -readonly [P in keyof T_2]: T_2[P]; } : never, z.core.$strict>, z.ZodObject<Base & {
54
+ status: z.ZodLiteral<"reserved">;
55
+ quote: Quote;
56
+ reserve: z.ZodObject<{
57
+ fundedIn: z.ZodNullable<z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>>;
58
+ amount: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
59
+ acceptBy: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
60
+ }, z.core.$strict>;
61
+ } extends infer T_3 ? { -readonly [P in keyof T_3]: T_3[P]; } : never, z.core.$strict>, z.ZodObject<Base & {
62
+ status: z.ZodLiteral<"accepted">;
63
+ quote: Quote;
64
+ reserve: z.ZodObject<{
65
+ fundedIn: z.ZodNullable<z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>>;
66
+ amount: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
67
+ acceptBy: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
68
+ }, z.core.$strict>;
69
+ acceptedIn: z.ZodNullable<z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>>;
70
+ } extends infer T_4 ? { -readonly [P in keyof T_4]: T_4[P]; } : never, z.core.$strict>], "status">;
71
+ /** Reads JSON with a schema, or answers null for anything the schema refuses. */
72
+ export declare const readOrNull: <T>(schema: z.ZodType<T>) => (json: unknown) => T | null;
73
+ //# sour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