-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
480 lines (403 loc) · 14.5 KB
/
index.js
1
2
3
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import dotenv from 'dotenv';
import readline from 'readline-sync';
import OpenAI from 'openai';
import { JSONFilePreset } from "lowdb/node";
import * as path from "node:path";
dotenv.config();
const db = await JSONFilePreset(path.join("data.json"), { products: [], users: [] });
// Simulated delivery estimation
function estimateDelivery(warehouseAddress, userAddress) {
console.log(`estimating delivery from warehouse [${warehouseAddress}] to user address [${userAddress}]`);
// In a real application, this would make external API calls to shipping providers
const baseDeliveryDays = 3;
const randomAddition = Math.floor(Math.random() * 4); // 0-3 additional days
const estimatedDays = baseDeliveryDays + randomAddition;
const today = new Date();
const deliveryDate = new Date(today);
deliveryDate.setDate(today.getDate() + estimatedDays);
return {
estimatedDays,
deliveryDate: deliveryDate.toISOString().split('T')[0]
};
}
// Simulated payment processing
function processPayment(userId, productId, amount) {
// In a real application, this would call a payment gateway
console.log(`Processing payment of ${amount} for product ${productId} by user ${userId}`);
// Simulate more frequent payment failures (50% chance) to demonstrate the inconsistency problem
const failureChance = Math.random();
if (failureChance < 0.5) {
console.log("Payment failed!");
return {
success: false,
message: "Payment processing failed. Please try again."
};
}
return {
success: true,
transactionId: `trans-${Date.now()}`,
timestamp: new Date().toISOString()
};
}
// Simulated notification
function sendNotification(userId, message) {
// In a real application, this would send an email or push notification
console.log(`Notification to user ${userId}: ${message}`);
return {
success: true,
timestamp: new Date().toISOString()
};
}
// Initialize the OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// Define available functions
const availableFunctions = {
searchProducts: (args) => {
const { category, priceMax, tags, condition } = args;
let filteredProducts = [...db.data.products];
if (category) {
filteredProducts = filteredProducts.filter(p => p.category === category);
}
if (priceMax) {
filteredProducts = filteredProducts.filter(p => p.price <= priceMax);
}
if (tags && tags.length > 0) {
filteredProducts = filteredProducts.filter(p =>
tags.some(tag => p.tags.includes(tag))
);
}
if (condition) {
filteredProducts = filteredProducts.filter(p => p.condition === condition);
}
return filteredProducts;
},
checkProductAvailability: (args) => {
const { productId } = args;
const product = db.data.products.find(p => p.id === productId);
if (!product) {
return {
available: false,
message: "Product not found"
};
}
return {
available: product.inStock > 0,
inStock: product.inStock,
product: product
};
},
processPurchase: (args) => {
const { productId, userId } = args;
// Get the product and user
const product = db.data.products.find(p => p.id === productId);
const user = db.data. users.find(u => u.id === userId);
if (!product || !user) {
return {
success: false,
message: "Product or user not found"
};
}
if (product.inStock <= 0) {
return {
success: false,
message: "Product is out of stock"
};
}
// CRITICAL ISSUE: Reserve the product BEFORE payment processing
// This creates the potential for inconsistent state
console.log(`Reserving product ${product.name} (inventory reduced from ${product.inStock} to ${product.inStock - 1})`);
product.inStock -= 1;
// Process the payment
// In a real application, this would call a payment gateway which leads to an asynchronous flow.
// Typically, a webhook has to be setup to accept the final payment state.
const paymentResult = processPayment(userId, productId, product.price);
if (!paymentResult.success) {
// CRITICAL ISSUE: The payment failed, but we've already reserved the product!
// In a monolithic system without compensation handling, this creates an inconsistent state
console.log(`INCONSISTENT STATE: Product ${product.id} is reserved but payment failed!`);
console.log(`This would require manual intervention to fix the inventory.`);
// We don't restore inventory here, demonstrating the problem
return {
success: false,
message: paymentResult.message
};
}
// Estimate delivery
const deliveryEstimate = estimateDelivery(productId, user.address);
// Create the order
const order = {
id: `order-${Date.now()}`,
userId,
productId,
productName: product.name,
price: product.price,
transactionId: paymentResult.transactionId,
createdAt: new Date().toISOString(),
deliveryEstimate
};
// Add to orders
db?.data?.orders?.push(order);
// Send notification
sendNotification(userId, `Your order for ${product.name} has been confirmed! Estimated delivery: ${deliveryEstimate.deliveryDate}`);
return {
success: true,
order
};
},
getDeliveryEstimate: (args) => {
const { productId, userId } = args;
const product = db.data.products.find(p => p.id === productId);
const user = db.data.users.find(u => u.id === userId);
if (!product || !user) {
return {
success: false,
message: "Product or user not found"
};
}
return {
success: true,
estimate: estimateDelivery(productId, user.address)
};
}
};
// Define function specs for the OpenAI API
const functionSpecs = [
{
name: "searchProducts",
description: "Search for products based on criteria",
parameters: {
type: "object",
properties: {
category: {
type: "string",
description: "Product category (e.g., 'laptops')"
},
priceMax: {
type: "number",
description: "Maximum price"
},
tags: {
type: "array",
items: {
type: "string"
},
description: "Tags to filter by (e.g., ['programming', 'college'])"
},
condition: {
type: "string",
description: "Product condition ('excellent', 'good', 'fair')"
}
}
}
},
{
name: "checkProductAvailability",
description: "Check if a product is available",
parameters: {
type: "object",
properties: {
productId: {
type: "string",
description: "ID of the product to check"
}
},
required: ["productId"]
}
},
{
name: "processPurchase",
description: "Process a purchase for a product",
parameters: {
type: "object",
properties: {
productId: {
type: "string",
description: "ID of the product to purchase"
},
userId: {
type: "string",
description: "ID of the user making the purchase"
}
},
required: ["productId", "userId"]
}
},
{
name: "getDeliveryEstimate",
description: "Get delivery estimate for a product",
parameters: {
type: "object",
properties: {
productId: {
type: "string",
description: "ID of the product"
},
userId: {
type: "string",
description: "ID of the user for delivery address"
}
},
required: ["productId", "userId"]
}
}
];
// Enhanced assistant function with function calling
async function marketplaceAssistant(userInput, conversationHistory = []) {
try {
// Add the user input to the conversation history
conversationHistory.push({ role: "user", content: userInput });
// Define the system message
const systemMessage = {
role: "system",
content: `You are an AI marketplace assistant helping users find, purchase, and arrange delivery for products.
Always be helpful, concise, and provide specific product recommendations that match user criteria.
The current user is John Doe (user-1). When processing purchases or checking delivery estimates,
always use this user ID unless otherwise specified.
If the user expresses a clear intent to purchase, use the processPurchase function directly.
If the user wants to know about delivery times, use the getDeliveryEstimate function.`
};
// Create the messages array for the API call
const messages = [systemMessage, ...conversationHistory];
// Step 1: Call OpenAI API with function definitions
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: messages,
functions: functionSpecs,
function_call: "auto",
temperature: 0.7,
});
const responseMessage = response.choices[0].message;
// Step 2: Check if the model wants to call a function
if (responseMessage.function_call) {
const functionName = responseMessage.function_call.name;
const functionArgs = JSON.parse(responseMessage.function_call.arguments);
console.log(`\nCalling function: ${functionName} with args:`, functionArgs);
// Call the function
const functionResponse = availableFunctions[functionName](functionArgs);
// Step 3: Append function response to messages
conversationHistory.push(responseMessage); // Add assistant's function call to history
// Add the function response to chat history
conversationHistory.push({
role: "function",
name: functionName,
content: JSON.stringify(functionResponse)
});
// Step 4: Get a new response from the model with the function response
const secondResponse = await openai.chat.completions.create({
model: "gpt-4o",
messages: [...messages, responseMessage, {
role: "function",
name: functionName,
content: JSON.stringify(functionResponse)
}],
functions: functionSpecs,
function_call: "auto",
temperature: 0.7,
});
const secondResponseMessage = secondResponse.choices[0].message;
// Handle nested function calls if needed
if (secondResponseMessage.function_call) {
const secondFunctionName = secondResponseMessage.function_call.name;
const secondFunctionArgs = JSON.parse(secondResponseMessage.function_call.arguments);
console.log(`\nCalling second function: ${secondFunctionName} with args:`, secondFunctionArgs);
const secondFunctionResponse = availableFunctions[secondFunctionName](secondFunctionArgs);
conversationHistory.push(secondResponseMessage);
conversationHistory.push({
role: "function",
name: secondFunctionName,
content: JSON.stringify(secondFunctionResponse)
});
// Get final response from the model
const finalResponse = await openai.chat.completions.create({
model: "gpt-4o",
messages: [...messages, responseMessage, {
role: "function",
name: functionName,
content: JSON.stringify(functionResponse)
}, secondResponseMessage, {
role: "function",
name: secondFunctionName,
content: JSON.stringify(secondFunctionResponse)
}],
temperature: 0.7,
});
const finalResponseMessage = finalResponse.choices[0].message;
conversationHistory.push(finalResponseMessage);
return {
response: finalResponseMessage.content,
conversationHistory
};
}
conversationHistory.push(secondResponseMessage);
return {
response: secondResponseMessage.content,
conversationHistory
};
}
// If no function call, just return the response
conversationHistory.push(responseMessage);
return {
response: responseMessage.content,
conversationHistory
};
} catch (error) {
console.error("Error in marketplace assistant:", error);
return {
response: "I'm sorry, I encountered an error while processing your request. Please try again.",
conversationHistory
};
}
}
// Simple CLI interface
// Function to display current inventory status
function showInventoryStatus() {
console.log("\n----- CURRENT INVENTORY STATUS -----");
db.data.products.forEach(product => {
console.log(`${product.name}: ${product.inStock} in stock`);
});
console.log("------------------------------------\n");
}
async function runCLI() {
console.log("Welcome to the Marketplace Assistant!");
console.log("Ask about products, availability, or make a purchase.");
console.log("-----------------------------------------------------------------");
console.log("Example queries:");
console.log("- I need a laptop for programming under $800");
console.log("- Is the Dell XPS 13 in stock?");
console.log("- I'd like to buy the Dell XPS 13");
console.log("- When would the MacBook Air be delivered?");
console.log("Special commands:");
console.log("- 'status' - Show current inventory status");
console.log("- 'exit' - Quit the application");
console.log("-----------------------------------------------------------------");
let conversationHistory = [];
let running = true;
// Display initial inventory
showInventoryStatus();
while (running) {
const userInput = readline.question("\nYou: ");
if (userInput.toLowerCase() === 'exit') {
running = false;
continue;
}
if (userInput.toLowerCase() === 'status') {
showInventoryStatus();
continue;
}
console.log("\nAssistant is thinking...");
const result = await marketplaceAssistant(userInput, conversationHistory);
conversationHistory = result.conversationHistory;
console.log(`\nAssistant: ${result.response}`);
// After each interaction that might modify inventory, show the status
if (userInput.toLowerCase().includes("buy") ||
userInput.toLowerCase().includes("purchase") ||
result.response.toLowerCase().includes("payment")) {
showInventoryStatus();
}
}
console.log("\nThank you for using the Marketplace Assistant!");
}
// Start the CLI
runCLI().catch(console.error);