← Tasks
[AI-05] Solo baseline (G3 Flash)
completedcode_genP3
Description
## AI-05: ACP agent-to-agent choreography
Section: AI Features | Milestone: M4
### Requirements
Implement an ACP (Agent Communication Protocol) module for buyer/seller marketplace agents.
### Acceptance Criteria
- File src/lib/acp-agents.ts must contain: agent choreography, acp, buyer_agent, seller_agent
### Codebase Conventions
- Trilingual comments: // EN: ... // VI: ... // ES: ...
- SPEC section refs in comments
- Zod at module level with .safeParse()
- NextResponse.json() responses
Write
Assignee
-
Claimed By
G3 Flash
Created
25d ago
Completed
25d ago
Quality
Overall
81%
Pass
PASS
Graded By
-
Graded At
-
Cost
Input Tokens
168
Output Tokens
2,627
Est. USD
$0.0011
Result
{
"code": "```typescript\n// EN: ACP Agent-to-Agent Choreography Module // VI: Mô-đun biên đạo ACP Agent-to-Agent // ES: Módulo de coreografía ACP Agente a Agente\n// SPEC: AI-05\n\nimport { z } from \"zod\";\nimport { NextResponse } from \"next/server\";\n\n// EN: Define Zod schemas for Buyer, Seller, and Marketplace Agents // VI: Xác định lược đồ Zod cho Đại lý Người mua, Người bán và Thị trường // ES: Definir esquemas Zod para Agentes Comprador, Vendedor y Mercado\n\nconst BuyerAgentSchema = z.object({\n id: z.string(),\n name: z.string(),\n type: z.literal(\"buyer\"),\n budget: z.number().positive(),\n preferences: z.array(z.string()), // EN: Array of desired product categories // VI: Mảng các danh mục sản phẩm mong muốn // ES: Array de categorías de productos deseadas\n});\n\nconst SellerAgentSchema = z.object({\n id: z.string(),\n name: z.string(),\n type: z.literal(\"seller\"),\n inventory: z.record(z.number()), // EN: Product ID to quantity mapping // VI: Ánh xạ ID sản phẩm sang số lượng // ES: Mapeo de ID de producto a cantidad\n priceList: z.record(z.number().positive()), // EN: Product ID to price mapping // VI: Ánh xạ ID sản phẩm sang giá // ES: Mapeo de ID de producto a precio\n});\n\nconst MarketplaceAgentSchema = z.object({\n id: z.string(),\n name: z.string(),\n type: z.literal(\"marketplace\"),\n listings: z.array(\n z.object({\n productId: z.string(),\n sellerId: z.string(),\n price: z.number().positive(),\n quantity: z.number().int().positive(),\n })\n ),\n});\n\n// EN: Type definitions based on Zod schemas // VI: Định nghĩa kiểu dựa trên lược đồ Zod // ES: Definiciones de tipo basadas en esquemas Zod\ntype BuyerAgent = z.infer<typeof BuyerAgentSchema>;\ntype SellerAgent = z.infer<typeof SellerAgentSchema>;\ntype MarketplaceAgent = z.infer<typeof MarketplaceAgentSchema>;\n\n// EN: Agent Communication Protocol (ACP) functions // VI: Các hàm Giao thức Giao tiếp Đại lý (ACP) // ES: Funciones del Protocolo de Comunicación de Agentes (ACP)\n\n/**\n * EN: Simulates a buyer agent searching for products on the marketplace. // VI: Mô phỏng một đại lý người mua tìm kiếm sản phẩm trên thị trường. // ES: Simula un agente comprador buscando productos en el mercado.\n * @param buyer - EN: The buyer agent. // VI: Đại lý người mua. // ES: El agente comprador.\n * @param marketplace - EN: The marketplace agent. // VI: Đại lý thị trường. // ES: El agente del mercado.\n * @returns EN: A list of potential products. // VI: Một danh sách các sản phẩm tiềm năng. // ES: Una lista de productos potenciales.\n */\nfunction buyerSearch(buyer: BuyerAgent, marketplace: MarketplaceAgent): any[] {\n // EN: Filter marketplace listings based on buyer preferences // VI: Lọc danh sách thị trường dựa trên sở thích của người mua // ES: Filtrar listados del mercado según las preferencias del comprador\n const potentialProducts = marketplace.listings.filter((listing) =>\n buyer.preferences.some((pref) => listing.productId.includes(pref))\n );\n\n // EN: Simulate logic for the buyer to evaluate the products // VI: Mô phỏng logic để người mua đánh giá sản phẩm // ES: Simular la lógica para que el comprador evalúe los productos\n console.log(`Buyer ${buyer.name} found potential products:`, potentialProducts); // EN: Logging for demonstration // VI: Ghi nhật ký để trình diễn // ES: Registro para demostración\n\n return potentialProducts;\n}\n\n/**\n * EN: Simulates a buyer agent making an offer to a seller agent. // VI: Mô phỏng một đại lý người mua đưa ra lời đề nghị cho một đại lý người bán. // ES: Simula un agente comprador haciendo una oferta a un agente vendedor.\n * @param buyer - EN: The buyer agent. // VI: Đại lý người mua. // ES: El agente comprador.\n * @param seller - EN: The seller agent. // VI: Đại lý người bán. // ES: El agente vendedor.\n * @param productId - EN: The product ID. // VI: ID sản phẩm. // ES: El ID del producto.\n * @param quantity - EN: The quantity requested. // VI: Số lượng yêu cầu. // ES: La cantidad solicitada.\n * @param price - EN: The offered price. // VI: Giá chào bán. // ES: El precio ofrecido.\n * @returns EN: The seller's response. // VI: Phản hồi của người bán. // ES: La respuesta del vendedor.\n */\nfunction buyerMakeOffer(\n buyer: BuyerAgent,\n seller: SellerAgent,\n productId: string,\n quantity: number,\n price: number\n): string {\n // EN: Check if the seller has enough inventory // VI: Kiểm tra xem người bán có đủ hàng tồn kho hay không // ES: Verificar si el vendedor tiene suficiente inventario\n if (seller.inventory[productId] < quantity) {\n console.log(`Seller ${seller.name} does not have enough ${productId} in stock.`); // EN: Logging for demonstration // VI: Ghi nhật ký để trình diễn // ES: Registro para demostración\n return \"Seller does not have enough inventory.\";\n }\n\n // EN: Check if the offered price is acceptable // VI: Kiểm tra xem giá chào bán có chấp nhận được không // ES: Verificar si el precio ofrecido es aceptable\n if (price < seller.priceList[productId]) {\n console.log(`Seller ${seller.name} rejects the offer for ${productId} at price ${price}.`); // EN: Logging for demonstration // VI: Ghi nhật ký để trình diễn // ES: Registro para demostración\n return \"Seller rejects the offer.\";\n }\n\n // EN: Simulate the transaction // VI: Mô phỏng giao dịch // ES: Simular la transacción\n seller.inventory[productId] -= quantity;\n buyer.budget -= price * quantity;\n console.log(`Transaction successful: Buyer ${buyer.name} bought ${quantity} of ${productId} from Seller ${seller.name}.`); // EN: Logging for demonstration // VI: Ghi nhật ký để trình diễn // ES: Registro para demostración\n return \"Offer accepted. Transaction successful.\";\n}\n\n/**\n * EN: Simulates a seller agent listing a product on the marketplace. // VI: Mô phỏng một đại lý người bán niêm yết sản phẩm trên thị trường. // ES: Simula un agente vendedor que enumera un producto en el mercado.\n * @param seller - EN: The seller agent. // VI: Đại lý người bán. // ES: El agente vendedor.\n * @param marketplace - EN: The marketplace agent. // VI: Đại lý thị trường. // ES: El agente del mercado.\n * @param productId - EN: The product ID. // VI: ID sản phẩm. // ES: El ID del producto.\n * @param price - EN: The price of the product. // VI: Giá của sản phẩm. // ES: El precio del producto.\n * @param quantity - EN: The quantity available. // VI: Số lượng có sẵn. // ES: La cantidad disponible.\n * @returns EN: A message indicating the success or failure of the listing. // VI: Một thông báo cho biết sự thành công hay thất bại của việc niêm yết. // ES: Un mensaje que indica el éxito o el fracaso de la lista.\n */\nfunction sellerListProduct(\n seller: SellerAgent,\n marketplace: MarketplaceAgent,\n productId: string,\n price: number,\n quantity: number\n): string {\n // EN: Check if the seller has the product in their inventory // VI: Kiểm tra xem người bán có sản phẩm trong kho của họ không // ES: Verificar si el vendedor tiene el producto en su inventario\n if (!seller.inventory[productId] || seller.inventory[productId] < quantity) {\n console.log(`Seller ${seller.name} does not have enough ${productId} in stock to list.`); // EN: Logging for demonstration // VI: Ghi nhật ký để trình diễn // ES: Registro para demostración\n return \"Seller does not have enough inventory to list.\";\n }\n\n // EN: Add the product to the marketplace listings // VI: Thêm sản phẩm vào danh sách thị trường // ES: Agregar el producto a los listados del mercado\n marketplace.listings.push({\n productId: productId,\n sellerId: seller.id,\n price: price,\n quantity: quantity,\n });\n\n console.log(`Seller ${seller.name} listed ${quantity} of ${productId} on the marketplace.`); // EN: Logging for demonstration // VI: Ghi nhật ký để trình diễn // ES: Registro para demostración\n return \"Product listed successfully.\";\n}\n\n// EN: Example Usage (for demonstration purposes) // VI: Ví dụ sử dụng (cho mục đích trình diễn) // ES: Ejemplo de uso (con fines de demostración)\nif (import.meta.url === `file://${process.argv[1]}`) {\n const buyerData = {\n id: \"buyer123\",\n name: \"Alice\",\n type: \"buyer\",\n budget: 1000,\n preferences: [\"Laptop\", \"Mouse\"],\n };\n\n const sellerData = {\n id: \"seller456\",\n name: \"Bob\",\n type: \"seller\",\n inventory: { Laptop: 5, Mouse: 10 },\n priceList: { Laptop: 800, Mouse: 25 },\n };\n\n const marketplaceData = {\n id: \"marketplace789\",\n name: \"TechMarket\",\n type: \"marketplace\",\n listings: [],\n };\n\n const buyerResult = BuyerAgentSchema.safeParse(buyerData);\n const sellerResult = SellerAgentSchema.safeParse(sellerData);\n const marketplaceResult = MarketplaceAgentSchema.safeParse(marketplaceData);\n\n if (!buyerResult.success || !sellerResult.success || !marketplaceResult.success) {\n console.error(\"Validation Error:\", buyerResult.error, sellerResult.error, marketplaceResult.error);\n } else {\n const buyer: BuyerAgent = buyerResult.data;\n const seller: SellerAgent = sellerResult.data;\n const marketplace: MarketplaceAgent = marketplaceResult.data;\n\n // EN: Seller lists a product // VI: Người bán niêm yết một sản phẩm // ES: El vendedor enumera un producto\n sellerListProduct(seller, marketplace, \"Laptop\", 800, 3);\n\n // EN: Buyer searches for products // VI: Người mua tìm kiếm sản phẩm // ES: El comprador busca productos\n const products = buyerSearch(buyer, marketplace);\n\n // EN: Buyer makes an offer // VI: Người mua đưa ra một lời đề nghị // ES: El comprador hace una oferta\n if (products.length > 0) {\n buyerMakeOffer(buyer, seller, \"Laptop\", 1, 750);\n }\n }\n}\n\nexport {\n BuyerAgentSchema,\n SellerAgentSchema,\n MarketplaceAgentSchema,\n BuyerAgent,\n SellerAgent,\n MarketplaceAgent,\n buyerSearch,\n buyerMakeOffer,\n sellerListProduct,\n};\n```",
"spec_id": "AI-05"
}Audit Trail (2)
25d agotask_completedG3 Flash
25d agotask_createdfoundry_collab
Task ID: ecffe4e5-67c3-4a8e-83e7-2c4e10700140