m2m-market 0.1.0 → 2.0.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.
- package/README.md +103 -78
- package/adapters.d.ts +37 -37
- package/adapters.js +124 -124
- package/ap2.d.ts +89 -0
- package/ap2.js +61 -0
- package/commercial.d.ts +11 -0
- package/commercial.js +30 -0
- package/index.d.ts +62 -53
- package/index.js +203 -186
- package/mpp.d.ts +47 -0
- package/mpp.js +74 -0
- package/outcomes.d.ts +25 -0
- package/outcomes.js +56 -0
- package/package.json +35 -63
- package/payments.d.ts +29 -0
- package/payments.js +30 -0
- package/procurement-adapters.d.ts +7 -0
- package/procurement-adapters.js +39 -0
- package/procurement.d.ts +65 -0
- package/procurement.js +195 -0
- package/protocols.d.ts +10 -0
- package/protocols.js +39 -0
- package/reputation.d.ts +58 -0
- package/reputation.js +90 -0
- package/x402.d.ts +92 -0
- package/x402.js +100 -0
package/README.md
CHANGED
|
@@ -1,78 +1,103 @@
|
|
|
1
|
-
# M2M Market Node.js SDK
|
|
2
|
-
|
|
3
|
-
Connect an existing JavaScript or TypeScript AI Agent to M2M Market.
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install m2m-market
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
1
|
+
# M2M Market Node.js SDK
|
|
2
|
+
|
|
3
|
+
Connect an existing JavaScript or TypeScript AI Agent to M2M Market.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install m2m-market
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Configure
|
|
12
|
+
|
|
13
|
+
The SDK connects to the production M2M Market API at `https://api.m2mmarket.ai` by default. Only the connection token is required:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
export M2M_CONNECTION_TOKEN="m2m_live_conn-..."
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Then create the client from the environment:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { M2MConnectionClient } from "m2m-market";
|
|
23
|
+
|
|
24
|
+
const client = M2MConnectionClient.fromEnvironment();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
You can also provide the token directly without setting any environment variables:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import { M2MConnectionClient } from "m2m-market";
|
|
31
|
+
|
|
32
|
+
const client = new M2MConnectionClient({
|
|
33
|
+
connectionToken: "m2m_live_conn-...",
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For testing or an alternate M2M deployment, `baseUrl` or the optional `M2M_API_BASE_URL` environment variable can override the production default.
|
|
38
|
+
|
|
39
|
+
## Execute a service
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import { M2MConnectionClient } from "m2m-market";
|
|
43
|
+
|
|
44
|
+
const client = M2MConnectionClient.fromEnvironment();
|
|
45
|
+
const result = await client.executeTask({
|
|
46
|
+
category: "traffic-data",
|
|
47
|
+
payload: { origin: "Nairobi CBD", destination: "JKIA" },
|
|
48
|
+
maximumPriceM2m: "0.05",
|
|
49
|
+
maximumAttempts: 2,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
console.log(result.response);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Test the connection
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
console.log(await M2MConnectionClient.fromEnvironment().testConnection());
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## OpenAI-compatible tool
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
npm install m2m-market openai
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import { M2MConnectionClient } from "m2m-market";
|
|
69
|
+
import { createOpenAIToolHandler } from "m2m-market/adapters";
|
|
70
|
+
|
|
71
|
+
const client = M2MConnectionClient.fromEnvironment();
|
|
72
|
+
const m2m = createOpenAIToolHandler(client);
|
|
73
|
+
|
|
74
|
+
const response = await openai.responses.create({
|
|
75
|
+
model: "YOUR_MODEL",
|
|
76
|
+
input: "Find traffic data for Nairobi CBD to JKIA",
|
|
77
|
+
tools: [m2m.definition],
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// When the model produces a purchase_external_service function call:
|
|
81
|
+
const output = await m2m.handle(toolCall);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## LangChain
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
npm install m2m-market @langchain/core zod
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
import { M2MConnectionClient } from "m2m-market";
|
|
92
|
+
import { M2MToolAdapter } from "m2m-market/adapters";
|
|
93
|
+
|
|
94
|
+
const tool = await new M2MToolAdapter(
|
|
95
|
+
M2MConnectionClient.fromEnvironment(),
|
|
96
|
+
).asLangChainTool();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The dashboard spending policy and owner approval remain authoritative. Request-level limits can only make those controls stricter.
|
|
100
|
+
|
|
101
|
+
## Company
|
|
102
|
+
|
|
103
|
+
M2M Market is operated by **M2M Market, Inc., a Delaware corporation**. The package remains branded **M2M Market**. Its current `UNLICENSED` package metadata is intentionally unchanged by this corporate-identity update; see the repository's `docs/legal-identity-and-licensing.md` for the licensing-state record.
|
package/adapters.d.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import type { JsonObject, M2MConnectionClient } from "./index.js";
|
|
2
|
-
|
|
3
|
-
export const M2M_TOOL_NAME: "purchase_external_service";
|
|
4
|
-
export const M2M_TOOL_DESCRIPTION: string;
|
|
5
|
-
|
|
6
|
-
export interface ToolArguments {
|
|
7
|
-
category: string;
|
|
8
|
-
payload: JsonObject;
|
|
9
|
-
maximum_price_m2m?: string;
|
|
10
|
-
minimum_reputation?: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface AdapterOptions {
|
|
14
|
-
defaultPolicy?: {
|
|
15
|
-
maximumPriceM2m?: string | number;
|
|
16
|
-
minimumReputation?: number;
|
|
17
|
-
maximumAttempts?: number;
|
|
18
|
-
responseFormat?: string;
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function openAIToolDefinition(): JsonObject;
|
|
23
|
-
|
|
24
|
-
export class M2MToolAdapter {
|
|
25
|
-
constructor(client: M2MConnectionClient, options?: AdapterOptions);
|
|
26
|
-
invoke(argumentsValue: ToolArguments): Promise<JsonObject>;
|
|
27
|
-
invokeJson(argumentsValue: string | ToolArguments): Promise<string>;
|
|
28
|
-
asLangChainTool(): Promise<unknown>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function createOpenAIToolHandler(
|
|
32
|
-
client: M2MConnectionClient,
|
|
33
|
-
options?: AdapterOptions,
|
|
34
|
-
): {
|
|
35
|
-
definition: JsonObject;
|
|
36
|
-
handle: (toolCall: { function: { arguments: string } }) => Promise<string>;
|
|
37
|
-
};
|
|
1
|
+
import type { JsonObject, M2MConnectionClient } from "./index.js";
|
|
2
|
+
|
|
3
|
+
export const M2M_TOOL_NAME: "purchase_external_service";
|
|
4
|
+
export const M2M_TOOL_DESCRIPTION: string;
|
|
5
|
+
|
|
6
|
+
export interface ToolArguments {
|
|
7
|
+
category: string;
|
|
8
|
+
payload: JsonObject;
|
|
9
|
+
maximum_price_m2m?: string;
|
|
10
|
+
minimum_reputation?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface AdapterOptions {
|
|
14
|
+
defaultPolicy?: {
|
|
15
|
+
maximumPriceM2m?: string | number;
|
|
16
|
+
minimumReputation?: number;
|
|
17
|
+
maximumAttempts?: number;
|
|
18
|
+
responseFormat?: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function openAIToolDefinition(): JsonObject;
|
|
23
|
+
|
|
24
|
+
export class M2MToolAdapter {
|
|
25
|
+
constructor(client: M2MConnectionClient, options?: AdapterOptions);
|
|
26
|
+
invoke(argumentsValue: ToolArguments): Promise<JsonObject>;
|
|
27
|
+
invokeJson(argumentsValue: string | ToolArguments): Promise<string>;
|
|
28
|
+
asLangChainTool(): Promise<unknown>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createOpenAIToolHandler(
|
|
32
|
+
client: M2MConnectionClient,
|
|
33
|
+
options?: AdapterOptions,
|
|
34
|
+
): {
|
|
35
|
+
definition: JsonObject;
|
|
36
|
+
handle: (toolCall: { function: { arguments: string } }) => Promise<string>;
|
|
37
|
+
};
|
package/adapters.js
CHANGED
|
@@ -1,124 +1,124 @@
|
|
|
1
|
-
import { M2MConnectionClient } from "./index.js";
|
|
2
|
-
|
|
3
|
-
export const M2M_TOOL_NAME = "purchase_external_service";
|
|
4
|
-
export const M2M_TOOL_DESCRIPTION =
|
|
5
|
-
"Purchase an external API service through M2M Market when the Agent needs information or a capability that is not available locally.";
|
|
6
|
-
|
|
7
|
-
export function openAIToolDefinition() {
|
|
8
|
-
return {
|
|
9
|
-
type: "function",
|
|
10
|
-
function: {
|
|
11
|
-
name: M2M_TOOL_NAME,
|
|
12
|
-
description: M2M_TOOL_DESCRIPTION,
|
|
13
|
-
parameters: {
|
|
14
|
-
type: "object",
|
|
15
|
-
properties: {
|
|
16
|
-
category: {
|
|
17
|
-
type: "string",
|
|
18
|
-
description: "Marketplace service category, such as traffic-data.",
|
|
19
|
-
},
|
|
20
|
-
payload: {
|
|
21
|
-
type: "object",
|
|
22
|
-
description: "JSON input sent to the selected Provider service.",
|
|
23
|
-
additionalProperties: true,
|
|
24
|
-
},
|
|
25
|
-
maximum_price_m2m: {
|
|
26
|
-
type: "string",
|
|
27
|
-
description: "Optional stricter maximum price for this request in M2M.",
|
|
28
|
-
},
|
|
29
|
-
minimum_reputation: {
|
|
30
|
-
type: "integer",
|
|
31
|
-
minimum: 0,
|
|
32
|
-
maximum: 100,
|
|
33
|
-
description: "Optional stricter minimum Provider reputation.",
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
required: ["category", "payload"],
|
|
37
|
-
additionalProperties: false,
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export class M2MToolAdapter {
|
|
44
|
-
constructor(client, { defaultPolicy = {} } = {}) {
|
|
45
|
-
if (!(client instanceof M2MConnectionClient)) {
|
|
46
|
-
throw new TypeError("M2MToolAdapter requires an M2MConnectionClient");
|
|
47
|
-
}
|
|
48
|
-
this.client = client;
|
|
49
|
-
this.defaultPolicy = { ...defaultPolicy };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async invoke({
|
|
53
|
-
category,
|
|
54
|
-
payload,
|
|
55
|
-
maximum_price_m2m,
|
|
56
|
-
minimum_reputation,
|
|
57
|
-
}) {
|
|
58
|
-
const result = await this.client.executeTask({
|
|
59
|
-
category,
|
|
60
|
-
payload,
|
|
61
|
-
maximumPriceM2m:
|
|
62
|
-
maximum_price_m2m ?? this.defaultPolicy.maximumPriceM2m,
|
|
63
|
-
minimumReputation:
|
|
64
|
-
minimum_reputation ?? this.defaultPolicy.minimumReputation,
|
|
65
|
-
maximumAttempts: this.defaultPolicy.maximumAttempts ?? 1,
|
|
66
|
-
responseFormat: this.defaultPolicy.responseFormat ?? "JSON",
|
|
67
|
-
});
|
|
68
|
-
return {
|
|
69
|
-
status: result.status,
|
|
70
|
-
category,
|
|
71
|
-
provider_id: result.provider_id,
|
|
72
|
-
service_id: result.service_id,
|
|
73
|
-
result: result.response,
|
|
74
|
-
payment: {
|
|
75
|
-
gross_amount_m2m: result.gross_amount_m2m,
|
|
76
|
-
protocol_fee_m2m: result.protocol_fee_m2m,
|
|
77
|
-
status: result.payment_status,
|
|
78
|
-
},
|
|
79
|
-
receipt: result.payment_receipt,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async invokeJson(argumentsValue) {
|
|
84
|
-
const parsed = typeof argumentsValue === "string"
|
|
85
|
-
? JSON.parse(argumentsValue)
|
|
86
|
-
: argumentsValue;
|
|
87
|
-
return JSON.stringify(await this.invoke(parsed));
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async asLangChainTool() {
|
|
91
|
-
let DynamicStructuredTool;
|
|
92
|
-
let z;
|
|
93
|
-
try {
|
|
94
|
-
({ DynamicStructuredTool } = await import("@langchain/core/tools"));
|
|
95
|
-
({ z } = await import("zod"));
|
|
96
|
-
} catch (error) {
|
|
97
|
-
throw new Error(
|
|
98
|
-
"Install LangChain dependencies with: npm install m2m-market @langchain/core zod",
|
|
99
|
-
{ cause: error },
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const adapter = this;
|
|
104
|
-
return new DynamicStructuredTool({
|
|
105
|
-
name: M2M_TOOL_NAME,
|
|
106
|
-
description: M2M_TOOL_DESCRIPTION,
|
|
107
|
-
schema: z.object({
|
|
108
|
-
category: z.string().min(1),
|
|
109
|
-
payload: z.object({}).catchall(z.unknown()),
|
|
110
|
-
maximum_price_m2m: z.string().optional(),
|
|
111
|
-
minimum_reputation: z.number().int().min(0).max(100).optional(),
|
|
112
|
-
}),
|
|
113
|
-
func: async (input) => JSON.stringify(await adapter.invoke(input)),
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function createOpenAIToolHandler(client, options = {}) {
|
|
119
|
-
const adapter = new M2MToolAdapter(client, options);
|
|
120
|
-
return {
|
|
121
|
-
definition: openAIToolDefinition(),
|
|
122
|
-
handle: (toolCall) => adapter.invokeJson(toolCall.function.arguments),
|
|
123
|
-
};
|
|
124
|
-
}
|
|
1
|
+
import { M2MConnectionClient } from "./index.js";
|
|
2
|
+
|
|
3
|
+
export const M2M_TOOL_NAME = "purchase_external_service";
|
|
4
|
+
export const M2M_TOOL_DESCRIPTION =
|
|
5
|
+
"Purchase an external API service through M2M Market when the Agent needs information or a capability that is not available locally.";
|
|
6
|
+
|
|
7
|
+
export function openAIToolDefinition() {
|
|
8
|
+
return {
|
|
9
|
+
type: "function",
|
|
10
|
+
function: {
|
|
11
|
+
name: M2M_TOOL_NAME,
|
|
12
|
+
description: M2M_TOOL_DESCRIPTION,
|
|
13
|
+
parameters: {
|
|
14
|
+
type: "object",
|
|
15
|
+
properties: {
|
|
16
|
+
category: {
|
|
17
|
+
type: "string",
|
|
18
|
+
description: "Marketplace service category, such as traffic-data.",
|
|
19
|
+
},
|
|
20
|
+
payload: {
|
|
21
|
+
type: "object",
|
|
22
|
+
description: "JSON input sent to the selected Provider service.",
|
|
23
|
+
additionalProperties: true,
|
|
24
|
+
},
|
|
25
|
+
maximum_price_m2m: {
|
|
26
|
+
type: "string",
|
|
27
|
+
description: "Optional stricter maximum price for this request in M2M.",
|
|
28
|
+
},
|
|
29
|
+
minimum_reputation: {
|
|
30
|
+
type: "integer",
|
|
31
|
+
minimum: 0,
|
|
32
|
+
maximum: 100,
|
|
33
|
+
description: "Optional stricter minimum Provider reputation.",
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
required: ["category", "payload"],
|
|
37
|
+
additionalProperties: false,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class M2MToolAdapter {
|
|
44
|
+
constructor(client, { defaultPolicy = {} } = {}) {
|
|
45
|
+
if (!(client instanceof M2MConnectionClient)) {
|
|
46
|
+
throw new TypeError("M2MToolAdapter requires an M2MConnectionClient");
|
|
47
|
+
}
|
|
48
|
+
this.client = client;
|
|
49
|
+
this.defaultPolicy = { ...defaultPolicy };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async invoke({
|
|
53
|
+
category,
|
|
54
|
+
payload,
|
|
55
|
+
maximum_price_m2m,
|
|
56
|
+
minimum_reputation,
|
|
57
|
+
}) {
|
|
58
|
+
const result = await this.client.executeTask({
|
|
59
|
+
category,
|
|
60
|
+
payload,
|
|
61
|
+
maximumPriceM2m:
|
|
62
|
+
maximum_price_m2m ?? this.defaultPolicy.maximumPriceM2m,
|
|
63
|
+
minimumReputation:
|
|
64
|
+
minimum_reputation ?? this.defaultPolicy.minimumReputation,
|
|
65
|
+
maximumAttempts: this.defaultPolicy.maximumAttempts ?? 1,
|
|
66
|
+
responseFormat: this.defaultPolicy.responseFormat ?? "JSON",
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
status: result.status,
|
|
70
|
+
category,
|
|
71
|
+
provider_id: result.provider_id,
|
|
72
|
+
service_id: result.service_id,
|
|
73
|
+
result: result.response,
|
|
74
|
+
payment: {
|
|
75
|
+
gross_amount_m2m: result.gross_amount_m2m,
|
|
76
|
+
protocol_fee_m2m: result.protocol_fee_m2m,
|
|
77
|
+
status: result.payment_status,
|
|
78
|
+
},
|
|
79
|
+
receipt: result.payment_receipt,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async invokeJson(argumentsValue) {
|
|
84
|
+
const parsed = typeof argumentsValue === "string"
|
|
85
|
+
? JSON.parse(argumentsValue)
|
|
86
|
+
: argumentsValue;
|
|
87
|
+
return JSON.stringify(await this.invoke(parsed));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async asLangChainTool() {
|
|
91
|
+
let DynamicStructuredTool;
|
|
92
|
+
let z;
|
|
93
|
+
try {
|
|
94
|
+
({ DynamicStructuredTool } = await import("@langchain/core/tools"));
|
|
95
|
+
({ z } = await import("zod"));
|
|
96
|
+
} catch (error) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
"Install LangChain dependencies with: npm install m2m-market @langchain/core zod",
|
|
99
|
+
{ cause: error },
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const adapter = this;
|
|
104
|
+
return new DynamicStructuredTool({
|
|
105
|
+
name: M2M_TOOL_NAME,
|
|
106
|
+
description: M2M_TOOL_DESCRIPTION,
|
|
107
|
+
schema: z.object({
|
|
108
|
+
category: z.string().min(1),
|
|
109
|
+
payload: z.object({}).catchall(z.unknown()),
|
|
110
|
+
maximum_price_m2m: z.string().optional(),
|
|
111
|
+
minimum_reputation: z.number().int().min(0).max(100).optional(),
|
|
112
|
+
}),
|
|
113
|
+
func: async (input) => JSON.stringify(await adapter.invoke(input)),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function createOpenAIToolHandler(client, options = {}) {
|
|
119
|
+
const adapter = new M2MToolAdapter(client, options);
|
|
120
|
+
return {
|
|
121
|
+
definition: openAIToolDefinition(),
|
|
122
|
+
handle: (toolCall) => adapter.invokeJson(toolCall.function.arguments),
|
|
123
|
+
};
|
|
124
|
+
}
|
package/ap2.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export const AP2_SPEC_VERSION: '0.2';
|
|
2
|
+
export const AP2_SPEC_COMMIT: 'e1ea56db72a6385bce3e5c1112b3a56ce60acb43';
|
|
3
|
+
export const AP2_PROFILE_ID: 'm2m-ap2-v0.2-mandate-v1-es256-root-sd-jwt';
|
|
4
|
+
|
|
5
|
+
export type Ap2Mode = 'direct' | 'autonomous';
|
|
6
|
+
export type Ap2ReasonCode =
|
|
7
|
+
| 'unsupported_ap2_version'
|
|
8
|
+
| 'malformed_mandate'
|
|
9
|
+
| 'invalid_signature'
|
|
10
|
+
| 'issuer_untrusted'
|
|
11
|
+
| 'subject_mismatch'
|
|
12
|
+
| 'delegate_mismatch'
|
|
13
|
+
| 'quote_mismatch'
|
|
14
|
+
| 'amount_exceeds_authorization'
|
|
15
|
+
| 'resource_out_of_scope'
|
|
16
|
+
| 'mandate_expired'
|
|
17
|
+
| 'mandate_revoked'
|
|
18
|
+
| 'replay_detected'
|
|
19
|
+
| 'standing_authorization_not_permitted'
|
|
20
|
+
| 'explicit_mandate_required'
|
|
21
|
+
| 'ap2_valid_but_m2m_policy_refused'
|
|
22
|
+
| 'unsupported_ap2_currency_domain';
|
|
23
|
+
|
|
24
|
+
export interface Ap2MandateBundle {
|
|
25
|
+
profile: string;
|
|
26
|
+
ap2_spec_version: string;
|
|
27
|
+
ap2_spec_commit: string;
|
|
28
|
+
authorization_id: string;
|
|
29
|
+
quote_id: string;
|
|
30
|
+
quote_fingerprint: string;
|
|
31
|
+
authorization_fingerprint: string;
|
|
32
|
+
mode: Ap2Mode;
|
|
33
|
+
checkout_mandate_sd_jwt: string;
|
|
34
|
+
payment_mandate_sd_jwt: string;
|
|
35
|
+
open_checkout_mandate_sd_jwt?: string;
|
|
36
|
+
open_payment_mandate_sd_jwt?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface Ap2ValidationResult {
|
|
40
|
+
valid: boolean;
|
|
41
|
+
reason_code?: Ap2ReasonCode | null;
|
|
42
|
+
profile: string;
|
|
43
|
+
mode?: Ap2Mode;
|
|
44
|
+
principal_id?: string;
|
|
45
|
+
delegate_id?: string;
|
|
46
|
+
authorization_id?: string;
|
|
47
|
+
plan_id?: string;
|
|
48
|
+
intent_id?: string;
|
|
49
|
+
quote_id?: string;
|
|
50
|
+
quote_fingerprint?: string;
|
|
51
|
+
amount?: Record<string, unknown>;
|
|
52
|
+
provider_id?: string;
|
|
53
|
+
service_id?: string;
|
|
54
|
+
valid_from?: number;
|
|
55
|
+
valid_until?: number;
|
|
56
|
+
policy_decision_reference?: string;
|
|
57
|
+
mandate_id?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface Ap2ReceiptEvidence {
|
|
61
|
+
profile: string;
|
|
62
|
+
canonical_receipt_id: string;
|
|
63
|
+
authorization_id?: string;
|
|
64
|
+
quote_id?: string;
|
|
65
|
+
checkout_receipt_claims: Record<string, unknown>;
|
|
66
|
+
payment_receipt_claims: Record<string, unknown>;
|
|
67
|
+
reconciliation_state?: string;
|
|
68
|
+
outcome_verification_status: 'not_performed';
|
|
69
|
+
portable_outcome_verified: false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface Ap2AdapterStatus {
|
|
73
|
+
adapter_id: 'm2m.ap2.authorization-evidence';
|
|
74
|
+
protocol: 'AP2';
|
|
75
|
+
protocol_spec_version: '0.2';
|
|
76
|
+
protocol_spec_commit: string;
|
|
77
|
+
profile: string;
|
|
78
|
+
adapter_available: boolean;
|
|
79
|
+
external_signing_enabled: false;
|
|
80
|
+
financial_execution_enabled: false;
|
|
81
|
+
standing_authorization_support: string;
|
|
82
|
+
explicit_mandate_support: boolean;
|
|
83
|
+
outcome_verification_status: 'not_performed';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function validateAp2MandateBundle(value: Record<string, unknown>): Ap2MandateBundle;
|
|
87
|
+
export function validateAp2ValidationResult(value: Record<string, unknown>): Ap2ValidationResult;
|
|
88
|
+
export function validateAp2ReceiptEvidence(value: Record<string, unknown>): Ap2ReceiptEvidence;
|
|
89
|
+
export function validateAp2AdapterStatus(value: Record<string, unknown>): Ap2AdapterStatus;
|
package/ap2.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export const AP2_SPEC_VERSION = '0.2';
|
|
2
|
+
export const AP2_SPEC_COMMIT = 'e1ea56db72a6385bce3e5c1112b3a56ce60acb43';
|
|
3
|
+
export const AP2_PROFILE_ID = 'm2m-ap2-v0.2-mandate-v1-es256-root-sd-jwt';
|
|
4
|
+
|
|
5
|
+
function object(value, message) {
|
|
6
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError(message);
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
function text(value, message) {
|
|
10
|
+
if (typeof value !== 'string' || !value || value.length > 131072) throw new TypeError(message);
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function validateAp2MandateBundle(value) {
|
|
15
|
+
const item = object(value, 'Invalid AP2 mandate bundle');
|
|
16
|
+
if (item.profile !== AP2_PROFILE_ID || item.ap2_spec_version !== AP2_SPEC_VERSION) throw new TypeError('Unsupported AP2 protocol version');
|
|
17
|
+
if (item.ap2_spec_commit !== AP2_SPEC_COMMIT) throw new TypeError('Unsupported AP2 specification commit');
|
|
18
|
+
for (const key of ['authorization_id','quote_id','quote_fingerprint','authorization_fingerprint','checkout_mandate_sd_jwt','payment_mandate_sd_jwt']) {
|
|
19
|
+
text(item[key], 'Invalid AP2 mandate bundle');
|
|
20
|
+
}
|
|
21
|
+
if (!['direct','autonomous'].includes(item.mode)) throw new TypeError('Invalid AP2 mandate mode');
|
|
22
|
+
if (item.mode === 'autonomous') {
|
|
23
|
+
text(item.open_checkout_mandate_sd_jwt, 'Autonomous AP2 mandate chain missing');
|
|
24
|
+
text(item.open_payment_mandate_sd_jwt, 'Autonomous AP2 mandate chain missing');
|
|
25
|
+
}
|
|
26
|
+
return structuredClone(item);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function validateAp2ValidationResult(value) {
|
|
30
|
+
const item = object(value, 'Invalid AP2 validation result');
|
|
31
|
+
if (item.profile !== AP2_PROFILE_ID || typeof item.valid !== 'boolean') throw new TypeError('Invalid AP2 validation result');
|
|
32
|
+
if (item.valid) {
|
|
33
|
+
if (item.reason_code !== null && item.reason_code !== undefined) throw new TypeError('Valid AP2 result cannot contain refusal reason');
|
|
34
|
+
for (const key of ['principal_id','delegate_id','authorization_id','quote_id','mandate_id']) text(item[key], 'Invalid AP2 validation result');
|
|
35
|
+
} else {
|
|
36
|
+
text(item.reason_code, 'Invalid AP2 refusal reason');
|
|
37
|
+
}
|
|
38
|
+
return structuredClone(item);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function validateAp2ReceiptEvidence(value) {
|
|
42
|
+
const item = object(value, 'Invalid AP2 receipt evidence');
|
|
43
|
+
if (item.profile !== AP2_PROFILE_ID) throw new TypeError('Invalid AP2 receipt evidence');
|
|
44
|
+
text(item.canonical_receipt_id, 'Invalid AP2 receipt evidence');
|
|
45
|
+
object(item.checkout_receipt_claims, 'Invalid AP2 checkout receipt');
|
|
46
|
+
object(item.payment_receipt_claims, 'Invalid AP2 payment receipt');
|
|
47
|
+
if (item.outcome_verification_status !== 'not_performed' || item.portable_outcome_verified !== false) {
|
|
48
|
+
throw new TypeError('Phase 28 outcome verification must not be claimed');
|
|
49
|
+
}
|
|
50
|
+
return structuredClone(item);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function validateAp2AdapterStatus(value) {
|
|
54
|
+
const item = object(value, 'Invalid AP2 adapter status');
|
|
55
|
+
if (item.adapter_id !== 'm2m.ap2.authorization-evidence' || item.protocol !== 'AP2') throw new TypeError('Invalid AP2 adapter status');
|
|
56
|
+
if (item.protocol_spec_version !== AP2_SPEC_VERSION || item.protocol_spec_commit !== AP2_SPEC_COMMIT) throw new TypeError('Unsupported AP2 protocol version');
|
|
57
|
+
if (item.profile !== AP2_PROFILE_ID) throw new TypeError('Unsupported AP2 proof profile');
|
|
58
|
+
if (item.external_signing_enabled !== false || item.financial_execution_enabled !== false) throw new TypeError('Unsafe Phase 27 adapter gate state');
|
|
59
|
+
if (item.outcome_verification_status !== 'not_performed') throw new TypeError('Phase 28 boundary violation');
|
|
60
|
+
return structuredClone(item);
|
|
61
|
+
}
|
package/commercial.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JsonObject } from './procurement.js';
|
|
2
|
+
export type CommercialModelName = 'fixed' | 'maximum_actual' | 'per_unit' | 'metered_session' | 'subscription' | 'capacity_reservation' | 'outcome_based';
|
|
3
|
+
export type ReconciliationState = 'fully_settled' | 'settled_below_reservation_released' | 'pending_authoritative_actual' | 'pending_external_settlement' | 'refund_pending' | 'partially_refunded' | 'fully_refunded' | 'failed_before_irreversible_action' | 'unknown_completion_reconciliation_required' | 'irreconcilable_inconsistent_evidence';
|
|
4
|
+
export interface CommercialModel extends JsonObject { schema_name: 'canonical_commercial_model_v1'; schema_version: '1.0.0'; object_type: 'CommercialModel'; model: CommercialModelName; authority_reference: string; maximum_exposure_base_units: string; pricing: JsonObject; refund_policy: JsonObject; autonomous_recurring_charge_enabled: false; start_at?: number; expires_at?: number; outcome_state?: string; }
|
|
5
|
+
export interface MeterEvent extends JsonObject { schema_name: 'canonical_meter_event_v1'; schema_version: '1.0.0'; object_type: 'MeterEvent'; transaction_id: string; session_id: string; service_version: string; meter_type: string; unit: string; previous_value: string; new_value: string; delta: string; authoritative_source: string; event_id: string; timestamp: number; evidence_reference?: string | null; evidence_hash?: string | null; event_fingerprint: string; }
|
|
6
|
+
export interface Refund extends JsonObject { schema_name: 'canonical_refund_v1'; schema_version: '1.0.0'; object_type: 'Refund'; refund_id: string; transaction_id: string; original_settlement_id: string; amount_base_units: string; reason: string; actor_reference: string; authorization_reference: string; rail_id: string; external_state: 'not_external' | 'pending_external' | 'verified_external'; transfer_verified: boolean; reconciliation_state: 'recorded' | 'refund_pending'; refund_fingerprint: string; }
|
|
7
|
+
export interface CommercialReconciliation extends JsonObject { schema_name: 'canonical_commercial_reconciliation_v1'; schema_version: '1.0.0'; object_type: 'CommercialReconciliation'; transaction_id: string; reconciliation_state: ReconciliationState; reserved_amount_base_units: string; actual_amount_base_units: string | null; release_amount_base_units: string | null; refunded_amount_base_units: string; automatic_financial_retry: false; alternate_rail_fallback: false; reconciliation_id: string; }
|
|
8
|
+
export function validateCommercialModel(value: JsonObject): CommercialModel;
|
|
9
|
+
export function validateMeterEvent(value: JsonObject): MeterEvent;
|
|
10
|
+
export function validateRefund(value: JsonObject): Refund;
|
|
11
|
+
export function validateCommercialReconciliation(value: JsonObject): CommercialReconciliation;
|