autocart-ai-tools 1.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 ADDED
@@ -0,0 +1,36 @@
1
+ # @autocart/ai-tools
2
+
3
+ The official OpenAI/LangChain tools for the AutoCart network.
4
+
5
+ This package provides pre-built function-calling schemas and execution logic to instantly give your AI Agent the ability to securely search and purchase products from verified merchants on the AutoCart network.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @autocart/ai-tools
11
+ ```
12
+
13
+ ## Usage (LangChain / LangGraph)
14
+
15
+ ```javascript
16
+ import { createReactAgent } from "@langchain/langgraph";
17
+ import { tool } from "@langchain/core/tools";
18
+ import { AutoCartSearchTool, AutoCartBuyerTool } from "@autocart/ai-tools";
19
+
20
+ const searchApi = new AutoCartSearchTool({ networkUrl: 'https://api.autocart.network' });
21
+ const searchTool = tool(
22
+ async (args) => searchApi.execute(args),
23
+ { name: searchApi.getOpenAISchema().name, description: searchApi.getOpenAISchema().description, schema: searchApi.getOpenAISchema().parameters }
24
+ );
25
+
26
+ const buyApi = new AutoCartBuyerTool({ buyerKey: process.env.AUTOCART_BUYER_KEY });
27
+ const buyTool = tool(
28
+ async (args) => buyApi.execute(args),
29
+ { name: buyApi.getOpenAISchema().name, description: buyApi.getOpenAISchema().description, schema: buyApi.getOpenAISchema().parameters }
30
+ );
31
+
32
+ const agent = createReactAgent({
33
+ llm: mistralModel,
34
+ tools: [searchTool, buyTool]
35
+ });
36
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export interface AutoCartBuyerToolConfig {
2
+ buyerKey: string;
3
+ }
4
+
5
+ export class AutoCartBuyerTool {
6
+ constructor(config: AutoCartBuyerToolConfig);
7
+ getOpenAISchema(): any;
8
+ execute(args: { merchantUrl: string; sku: string; qty?: number }): Promise<string>;
9
+ }
package/index.js ADDED
@@ -0,0 +1,112 @@
1
+ import crypto from 'crypto';
2
+
3
+ export class AutoCartSearchTool {
4
+ constructor(config = {}) {
5
+ this.networkUrl = config.networkUrl || 'http://localhost:5000';
6
+ }
7
+
8
+ getOpenAISchema() {
9
+ return {
10
+ name: 'autocart_search_catalog',
11
+ description: 'Search the global AutoCart network for products across all verified merchants.',
12
+ parameters: {
13
+ type: 'object',
14
+ properties: {
15
+ query: { type: 'string', description: 'The search query (e.g. "luxury watch")' }
16
+ },
17
+ required: ['query']
18
+ }
19
+ };
20
+ }
21
+
22
+ async execute({ query }) {
23
+ try {
24
+ const response = await fetch(`${this.networkUrl}/api/catalog/search?query=${encodeURIComponent(query)}`);
25
+ const data = await response.json();
26
+ return JSON.stringify(data.results);
27
+ } catch (err) {
28
+ return `Failed to search AutoCart network: ${err.message}`;
29
+ }
30
+ }
31
+ }
32
+
33
+ export class AutoCartBuyerTool {
34
+ constructor(config) {
35
+ if (!config.buyerKey) {
36
+ throw new Error('AutoCartBuyerTool requires a buyerKey');
37
+ }
38
+ this.buyerKey = config.buyerKey;
39
+ }
40
+
41
+ getOpenAISchema() {
42
+ return {
43
+ name: 'autocart_buy_product',
44
+ description: 'Purchases a product from an AutoCart-enabled merchant store.',
45
+ parameters: {
46
+ type: 'object',
47
+ properties: {
48
+ merchantUrl: {
49
+ type: 'string',
50
+ description: 'The base URL of the merchant store (returned by autocart_search_catalog)'
51
+ },
52
+ sku: {
53
+ type: 'string',
54
+ description: 'The exact SKU or product ID to purchase'
55
+ },
56
+ qty: {
57
+ type: 'integer',
58
+ description: 'The quantity to purchase (default 1)'
59
+ },
60
+ maxAuthorizedAmount: {
61
+ type: 'number',
62
+ description: 'The maximum total price the AI is willing to pay. Prevents price gouging.'
63
+ }
64
+ },
65
+ required: ['merchantUrl', 'sku']
66
+ }
67
+ };
68
+ }
69
+
70
+ async execute(args) {
71
+ const { merchantUrl, sku, qty = 1, maxAuthorizedAmount } = args;
72
+
73
+ // Using a random UUID for idempotency in the tool
74
+ const idempotencyKey = crypto.randomUUID ? crypto.randomUUID() : Math.random().toString(36).substring(7);
75
+
76
+ const payload = {
77
+ sku,
78
+ qty,
79
+ idempotencyKey,
80
+ maxAuthorizedAmount
81
+ };
82
+
83
+ try {
84
+ const response = await fetch(merchantUrl, {
85
+ method: 'POST',
86
+ headers: {
87
+ 'Content-Type': 'application/json',
88
+ 'x-buyer-key': this.buyerKey
89
+ },
90
+ body: JSON.stringify(payload)
91
+ });
92
+
93
+ const result = await response.json();
94
+
95
+ if (!response.ok) {
96
+ return `Failed to purchase: ${result.error || result.message || JSON.stringify(result)}`;
97
+ }
98
+
99
+ if (result.status === 'GATED_1_CLICK' || result.status === 'GATED_2FA') {
100
+ return `Transaction blocked by firewall. Awaiting human approval. Audit ID: ${result.auditId}`;
101
+ }
102
+
103
+ if (result.status === 'PAYMENT_CAPTURED' || result.status === 'AUTO_APPROVED') {
104
+ return `Successfully purchased ${qty} of ${sku}. Order ID: ${result.razorpayOrderId}`;
105
+ }
106
+
107
+ return `Order status: ${result.status}`;
108
+ } catch (err) {
109
+ return `Error executing AutoCart transaction: ${err.message}`;
110
+ }
111
+ }
112
+ }
package/package.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "autocart-ai-tools",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "description": "LangChain and OpenAI compatible tools for AI agents to buy products securely.",
8
+ "dependencies": {}
9
+ }