m2m-market 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -78
- package/adapters.d.ts +37 -37
- package/adapters.js +124 -124
- package/index.d.ts +55 -53
- package/index.js +193 -186
- package/package.json +63 -63
package/README.md
CHANGED
|
@@ -1,78 +1,99 @@
|
|
|
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.
|
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/index.d.ts
CHANGED
|
@@ -1,53 +1,55 @@
|
|
|
1
|
-
export type JsonObject = Record<string, unknown>;
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
1
|
+
export type JsonObject = Record<string, unknown>;
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_API_BASE_URL: string;
|
|
4
|
+
|
|
5
|
+
export interface M2MConnectionErrorOptions {
|
|
6
|
+
status?: number;
|
|
7
|
+
payload?: JsonObject;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class M2MConnectionError extends Error {
|
|
11
|
+
status: number;
|
|
12
|
+
payload: JsonObject;
|
|
13
|
+
constructor(message: string, options?: M2MConnectionErrorOptions);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface M2MConnectionClientOptions {
|
|
17
|
+
baseUrl?: string;
|
|
18
|
+
connectionToken: string;
|
|
19
|
+
timeoutMs?: number;
|
|
20
|
+
fetchImpl?: typeof fetch;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SubmitOptions {
|
|
24
|
+
category: string;
|
|
25
|
+
payload: JsonObject;
|
|
26
|
+
maximumPriceM2m?: string | number;
|
|
27
|
+
minimumReputation?: number;
|
|
28
|
+
maximumAttempts?: number;
|
|
29
|
+
responseFormat?: string;
|
|
30
|
+
taskId?: string;
|
|
31
|
+
attemptNumber?: number;
|
|
32
|
+
excludedCandidates?: string[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ExecuteTaskOptions extends Omit<SubmitOptions, "attemptNumber" | "excludedCandidates"> {
|
|
36
|
+
queryTimeoutMs?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class M2MConnectionClient {
|
|
40
|
+
baseUrl: string;
|
|
41
|
+
connectionToken: string;
|
|
42
|
+
timeoutMs: number;
|
|
43
|
+
constructor(options: M2MConnectionClientOptions);
|
|
44
|
+
static fromEnvironment(env?: NodeJS.ProcessEnv): M2MConnectionClient;
|
|
45
|
+
request(method: string, path: string, body?: JsonObject): Promise<JsonObject>;
|
|
46
|
+
testConnection(): Promise<JsonObject>;
|
|
47
|
+
submit(options: SubmitOptions): Promise<JsonObject>;
|
|
48
|
+
getQueryStatus(queryId: string): Promise<JsonObject>;
|
|
49
|
+
getTaskStatus(taskId: string): Promise<JsonObject>;
|
|
50
|
+
waitForQuery(
|
|
51
|
+
queryId: string,
|
|
52
|
+
options?: { timeoutMs?: number; pollMs?: number },
|
|
53
|
+
): Promise<JsonObject>;
|
|
54
|
+
executeTask(options: ExecuteTaskOptions): Promise<JsonObject>;
|
|
55
|
+
}
|
package/index.js
CHANGED
|
@@ -1,186 +1,193 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this.
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
1
|
+
export const DEFAULT_API_BASE_URL = "https://api.m2mmarket.ai";
|
|
2
|
+
|
|
3
|
+
export class M2MConnectionError extends Error {
|
|
4
|
+
constructor(message, { status = 0, payload = {} } = {}) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "M2MConnectionError";
|
|
7
|
+
this.status = Number(status || 0);
|
|
8
|
+
this.payload = payload && typeof payload === "object" ? payload : {};
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class M2MConnectionClient {
|
|
13
|
+
constructor({
|
|
14
|
+
connectionToken,
|
|
15
|
+
baseUrl = DEFAULT_API_BASE_URL,
|
|
16
|
+
timeoutMs = 30000,
|
|
17
|
+
fetchImpl = globalThis.fetch,
|
|
18
|
+
} = {}) {
|
|
19
|
+
this.baseUrl = String(baseUrl || DEFAULT_API_BASE_URL).replace(/\/$/, "");
|
|
20
|
+
this.connectionToken = String(connectionToken || "").trim();
|
|
21
|
+
this.timeoutMs = Math.max(Number(timeoutMs || 30000), 1000);
|
|
22
|
+
this.fetchImpl = fetchImpl;
|
|
23
|
+
if (!this.baseUrl.startsWith("https://")) {
|
|
24
|
+
throw new TypeError("M2M connection base URL must use HTTPS");
|
|
25
|
+
}
|
|
26
|
+
if (!this.connectionToken.startsWith("m2m_live_conn-")) {
|
|
27
|
+
throw new TypeError("A valid M2M connection token is required");
|
|
28
|
+
}
|
|
29
|
+
if (typeof this.fetchImpl !== "function") {
|
|
30
|
+
throw new TypeError("A Fetch API implementation is required");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static fromEnvironment(env = process.env) {
|
|
35
|
+
return new M2MConnectionClient({
|
|
36
|
+
baseUrl: env.M2M_API_BASE_URL || DEFAULT_API_BASE_URL,
|
|
37
|
+
connectionToken: env.M2M_CONNECTION_TOKEN,
|
|
38
|
+
timeoutMs: Number(env.M2M_REQUEST_TIMEOUT_MS || 30000),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async request(method, path, body) {
|
|
43
|
+
const controller = new AbortController();
|
|
44
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
45
|
+
try {
|
|
46
|
+
const response = await this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
47
|
+
method,
|
|
48
|
+
headers: {
|
|
49
|
+
Accept: "application/json",
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
Authorization: `Bearer ${this.connectionToken}`,
|
|
52
|
+
"X-Request-Id": crypto.randomUUID(),
|
|
53
|
+
},
|
|
54
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
55
|
+
signal: controller.signal,
|
|
56
|
+
});
|
|
57
|
+
const payload = await response.json().catch(() => ({ error: "Invalid API response" }));
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
throw new M2MConnectionError(
|
|
60
|
+
payload.error || payload.message || "M2M request failed",
|
|
61
|
+
{ status: response.status, payload },
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
65
|
+
throw new M2MConnectionError("M2M response must be a JSON object");
|
|
66
|
+
}
|
|
67
|
+
return payload;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (error instanceof M2MConnectionError) throw error;
|
|
70
|
+
throw new M2MConnectionError(`M2M request failed: ${error.message || error}`);
|
|
71
|
+
} finally {
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
testConnection() {
|
|
77
|
+
return this.request("POST", "/connect/test", {});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
submit({
|
|
81
|
+
category,
|
|
82
|
+
payload,
|
|
83
|
+
maximumPriceM2m,
|
|
84
|
+
minimumReputation,
|
|
85
|
+
maximumAttempts,
|
|
86
|
+
responseFormat = "JSON",
|
|
87
|
+
taskId,
|
|
88
|
+
attemptNumber = 1,
|
|
89
|
+
excludedCandidates = [],
|
|
90
|
+
}) {
|
|
91
|
+
if (!String(category || "").trim()) throw new TypeError("Service category is required");
|
|
92
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
93
|
+
throw new TypeError("Service payload must be an object");
|
|
94
|
+
}
|
|
95
|
+
const body = {
|
|
96
|
+
category: String(category).trim(),
|
|
97
|
+
payload,
|
|
98
|
+
response_format: responseFormat,
|
|
99
|
+
attempt_number: Number(attemptNumber),
|
|
100
|
+
excluded_candidates: [...excludedCandidates],
|
|
101
|
+
};
|
|
102
|
+
if (maximumPriceM2m !== undefined) body.maximum_price_m2m = String(maximumPriceM2m);
|
|
103
|
+
if (minimumReputation !== undefined) body.minimum_reputation = Number(minimumReputation);
|
|
104
|
+
if (maximumAttempts !== undefined) body.maximum_attempts = Number(maximumAttempts);
|
|
105
|
+
if (taskId) body.task_id = taskId;
|
|
106
|
+
return this.request("POST", "/connect/execute", body);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getQueryStatus(queryId) {
|
|
110
|
+
return this.request("GET", `/connect/queries/${queryId}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getTaskStatus(taskId) {
|
|
114
|
+
return this.request("GET", `/connect/tasks/${taskId}`).then((payload) => {
|
|
115
|
+
if (!payload.task || typeof payload.task !== "object") {
|
|
116
|
+
throw new M2MConnectionError("Task status response is missing the task record");
|
|
117
|
+
}
|
|
118
|
+
return payload.task;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async waitForQuery(queryId, { timeoutMs = 60000, pollMs = 1000 } = {}) {
|
|
123
|
+
const deadline = Date.now() + Math.max(Number(timeoutMs), 1000);
|
|
124
|
+
while (Date.now() < deadline) {
|
|
125
|
+
const result = await this.getQueryStatus(queryId);
|
|
126
|
+
if (["settled", "released"].includes(String(result.lifecycle_state || "").toLowerCase())) {
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
await new Promise((resolve) => setTimeout(resolve, Math.max(Number(pollMs), 200)));
|
|
130
|
+
}
|
|
131
|
+
throw new M2MConnectionError(`Query ${queryId} did not finish in time`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async executeTask({
|
|
135
|
+
category,
|
|
136
|
+
payload,
|
|
137
|
+
maximumPriceM2m,
|
|
138
|
+
minimumReputation,
|
|
139
|
+
maximumAttempts = 1,
|
|
140
|
+
responseFormat = "JSON",
|
|
141
|
+
taskId = `task-${crypto.randomUUID().replaceAll("-", "")}`,
|
|
142
|
+
queryTimeoutMs = 60000,
|
|
143
|
+
}) {
|
|
144
|
+
maximumAttempts = Number(maximumAttempts);
|
|
145
|
+
if (!Number.isInteger(maximumAttempts) || maximumAttempts < 1 || maximumAttempts > 5) {
|
|
146
|
+
throw new TypeError("Maximum attempts must be between 1 and 5");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const attempts = [];
|
|
150
|
+
let attemptNumber = 1;
|
|
151
|
+
let excludedCandidates = [];
|
|
152
|
+
|
|
153
|
+
while (attemptNumber <= maximumAttempts) {
|
|
154
|
+
const accepted = await this.submit({
|
|
155
|
+
category,
|
|
156
|
+
payload,
|
|
157
|
+
maximumPriceM2m,
|
|
158
|
+
minimumReputation,
|
|
159
|
+
maximumAttempts,
|
|
160
|
+
responseFormat,
|
|
161
|
+
taskId,
|
|
162
|
+
attemptNumber,
|
|
163
|
+
excludedCandidates,
|
|
164
|
+
});
|
|
165
|
+
const terminal = ["settled", "released"].includes(
|
|
166
|
+
String(accepted.lifecycle_state || "").toLowerCase(),
|
|
167
|
+
)
|
|
168
|
+
? accepted
|
|
169
|
+
: await this.waitForQuery(accepted.query_id, { timeoutMs: queryTimeoutMs });
|
|
170
|
+
|
|
171
|
+
attempts.push({
|
|
172
|
+
attempt_number: attemptNumber,
|
|
173
|
+
query_id: terminal.query_id,
|
|
174
|
+
provider_id: terminal.provider_id,
|
|
175
|
+
service_id: terminal.service_id,
|
|
176
|
+
lifecycle_state: terminal.lifecycle_state,
|
|
177
|
+
payment_receipt: terminal.payment_receipt,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
if (String(terminal.lifecycle_state || "").toLowerCase() === "settled") {
|
|
181
|
+
return { ...terminal, task_id: taskId, attempts, autonomous_completion: true };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const task = await this.getTaskStatus(taskId);
|
|
185
|
+
attemptNumber = Number(task.next_attempt_number || attemptNumber + 1);
|
|
186
|
+
excludedCandidates = [...new Set(task.required_exclusions || [])].sort();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
throw new M2MConnectionError(`No eligible Provider completed task ${taskId}`, {
|
|
190
|
+
payload: { task_id: taskId, attempts },
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
package/package.json
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "m2m-market",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Customer SDK for connecting AI Agents to M2M Market",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./index.js",
|
|
7
|
-
"types": "./index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./index.d.ts",
|
|
11
|
-
"import": "./index.js"
|
|
12
|
-
},
|
|
13
|
-
"./adapters": {
|
|
14
|
-
"types": "./adapters.d.ts",
|
|
15
|
-
"import": "./adapters.js"
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
"files": [
|
|
19
|
-
"index.js",
|
|
20
|
-
"index.d.ts",
|
|
21
|
-
"adapters.js",
|
|
22
|
-
"adapters.d.ts",
|
|
23
|
-
"README.md"
|
|
24
|
-
],
|
|
25
|
-
"engines": {
|
|
26
|
-
"node": ">=18"
|
|
27
|
-
},
|
|
28
|
-
"scripts": {
|
|
29
|
-
"test": "node --test",
|
|
30
|
-
"check": "node --check index.js && node --check adapters.js && npm test"
|
|
31
|
-
},
|
|
32
|
-
"keywords": [
|
|
33
|
-
"ai-agent",
|
|
34
|
-
"api-marketplace",
|
|
35
|
-
"machine-to-machine",
|
|
36
|
-
"sdk"
|
|
37
|
-
],
|
|
38
|
-
"license": "UNLICENSED",
|
|
39
|
-
"repository": {
|
|
40
|
-
"type": "git",
|
|
41
|
-
"url": "git+https://github.com/castronyabola/m2m-marketplace-mvp.git",
|
|
42
|
-
"directory": "m2m-marketplace-mvp/sdk/node"
|
|
43
|
-
},
|
|
44
|
-
"peerDependencies": {
|
|
45
|
-
"@langchain/core": ">=0.3 <2",
|
|
46
|
-
"openai": ">=4 <7",
|
|
47
|
-
"zod": ">=3.23 <5"
|
|
48
|
-
},
|
|
49
|
-
"peerDependenciesMeta": {
|
|
50
|
-
"@langchain/core": {
|
|
51
|
-
"optional": true
|
|
52
|
-
},
|
|
53
|
-
"openai": {
|
|
54
|
-
"optional": true
|
|
55
|
-
},
|
|
56
|
-
"zod": {
|
|
57
|
-
"optional": true
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
"publishConfig": {
|
|
61
|
-
"access": "public"
|
|
62
|
-
}
|
|
63
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "m2m-market",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Customer SDK for connecting AI Agents to M2M Market",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"import": "./index.js"
|
|
12
|
+
},
|
|
13
|
+
"./adapters": {
|
|
14
|
+
"types": "./adapters.d.ts",
|
|
15
|
+
"import": "./adapters.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"index.js",
|
|
20
|
+
"index.d.ts",
|
|
21
|
+
"adapters.js",
|
|
22
|
+
"adapters.d.ts",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "node --test",
|
|
30
|
+
"check": "node --check index.js && node --check adapters.js && npm test"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"ai-agent",
|
|
34
|
+
"api-marketplace",
|
|
35
|
+
"machine-to-machine",
|
|
36
|
+
"sdk"
|
|
37
|
+
],
|
|
38
|
+
"license": "UNLICENSED",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/castronyabola/m2m-marketplace-mvp.git",
|
|
42
|
+
"directory": "m2m-marketplace-mvp/sdk/node"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@langchain/core": ">=0.3 <2",
|
|
46
|
+
"openai": ">=4 <7",
|
|
47
|
+
"zod": ">=3.23 <5"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"@langchain/core": {
|
|
51
|
+
"optional": true
|
|
52
|
+
},
|
|
53
|
+
"openai": {
|
|
54
|
+
"optional": true
|
|
55
|
+
},
|
|
56
|
+
"zod": {
|
|
57
|
+
"optional": true
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"publishConfig": {
|
|
61
|
+
"access": "public"
|
|
62
|
+
}
|
|
63
|
+
}
|