FOUNDRY
C8 Platform
← Tasks

[AI-05] ACP choreography (Team Bravo)

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
Team Bravo
Created
25d ago
Completed
25d ago

Quality

Overall
78%
Pass
PASS
Graded By
-
Graded At
-

Cost

Input Tokens
-
Output Tokens
-
Est. USD
$0.0124

Result

{
  "code": "```typescript\nimport { z } from \"zod\";\nimport { NextResponse } from \"next/server\";\n\n// EN: Buyer Agent Schema // VI: Lược đồ Đại lý Người mua // ES: Esquema de Agente Comprador\nconst BuyerAgentSchema = z.object({\n  id: z.string(), // EN: Unique identifier for the buyer // VI: Định danh duy nhất cho người mua // ES: Identificador único para el comprador\n  name: z.string(), // EN: Name of the buyer // VI: Tên người mua // ES: Nombre del comprador\n  budget: z.number(), // EN: Budget available for purchases // VI: Ngân sách có sẵn để mua // ES: Presupuesto disponible para compras\n  preferences: z.array(z.string()), // EN: List of buyer's preferences // VI: Danh sách sở thích của người mua // ES: Lista de preferencias del comprador\n});\n\n// EN: Seller Agent Schema // VI: Lược đồ Đại lý Người bán // ES: Esquema de Agente Vendedor\nconst SellerAgentSchema = z.object({\n  id: z.string(), // EN: Unique identifier for the seller // VI: Định danh duy nhất cho người bán // ES: Identificador único para el vendedor\n  name: z.string(), // EN: Name of the seller // VI: Tên người bán // ES: Nombre del vendedor\n  inventory: z.array(z.string()), // EN: List of items available for sale // VI: Danh sách hàng hóa có sẵn để bán // ES: Lista de artículos disponibles para la venta\n  priceList: z.record(z.string(), z.number()), // EN: Map of item to price // VI: Bản đồ hàng hóa với giá // ES: Mapa de artículo a precio\n});\n\n// EN: Marketplace Agent Schema // VI: Lược đồ Đại lý Thị trường // ES: Esquema de Agente de Mercado\nconst MarketplaceAgentSchema = z.object({\n  id: z.string(), // EN: Unique identifier for the marketplace // VI: Định danh duy nhất cho thị trường // ES: Identificador único para el mercado\n  agents: z.array(z.union([BuyerAgentSchema, SellerAgentSchema])), // EN: List of agents in the marketplace // VI: Danh sách các đại lý trong thị trường // ES: Lista de agentes en el mercado\n});\n\n// EN: Agent Choreography Function // VI: Hàm biên đạo điệp viên // ES: Función de coreografía de agentes\nfunction agentChoreography(\n  buyer: z.infer<typeof BuyerAgentSchema>,\n  seller: z.infer<typeof SellerAgentSchema>\n): NextResponse {\n  // EN: Handles the communication and negotiation between buyer and seller // VI: Xử lý giao tiếp và thương lượng giữa người mua và người bán // ES: Maneja la comunicación y negociación entre comprador y vendedor\n  // SPEC: Section on agent choreography logic\n\n  if (!buyer) {\n    return NextResponse.json({ error: \"Buyer data is null or undefined\" }, { status: 400 });\n  }\n\n  if (!seller) {\n    return NextResponse.json({ error: \"Seller data is null or undefined\" }, { status: 400 });\n  }\n\n  const buyerResult = BuyerAgentSchema.safeParse(buyer);\n  const sellerResult = SellerAgentSchema.safeParse(seller);\n\n  if (!buyerResult.success) {\n    // EN: Handle invalid buyer data // VI: Xử lý dữ liệu người mua không hợp lệ // ES: Manejar datos de comprador no válidos\n    return NextResponse.json({ error: \"Invalid buyer data\", details: buyerResult.error.issues }, { status: 400 });\n  }\n\n  if (!sellerResult.success) {\n    // EN: Handle invalid seller data // VI: Xử lý dữ liệu người bán không hợp lệ // ES: Manejar datos de vendedor no válidos\n    console.error(\"Seller data validation error:\", sellerResult.error.issues);\n    return NextResponse.json({ error: \"Invalid seller data\", details: sellerResult.error.issues }, { status: 400 });\n  }\n\n  // EN: Negotiation logic (iterating over preferences) // VI: Logic đàm phán (lặp lại các sở thích) // ES: Lógica de negociación (iterando sobre las preferencias)\n  if (!buyer.preferences || buyer.preferences.length === 0) {\n    return NextResponse.json({ error: \"Buyer has no preferences\" }, { status: 400 });\n  }\n\n  for (const item of buyer.preferences) {\n    if (seller.inventory.includes(item)) {\n      const price = seller.priceList[item];\n      if (price && buyer.budget >= price) {\n        // EN: Transaction successful // VI: Giao dịch thành công // ES: Transacción exitosa\n        return NextResponse.json({ message: \"Transaction successful\", item, price });\n      }\n    }\n  }\n\n  // EN: No item available or budget insufficient // VI: Không có mặt hàng hoặc ngân sách không đủ // ES: No hay artículo disponible o el presupuesto es insuficiente\n  return NextResponse.json({ error: \"Item not available or budget insufficient\" }, { status: 404 });\n  // SPEC: Section on agent communication protocol\n}\n\n// EN: Agent Communication Protocol Object // VI: Đối tượng Giao thức Giao tiếp Đại lý // ES: Objeto de Protocolo de Comunicación de Agente\nconst acp = {\n  initiate: function (\n    buyer: z.infer<typeof BuyerAgentSchema>,\n    seller: z.infer<typeof SellerAgentSchema>\n  ): void {\n    // EN: Initiates the communication protocol // VI: Bắt đầu giao thức giao tiếp // ES: Inicia el protocolo de comunicación\n    console.log(\"ACP initiated between buyer:\", buyer.id, \"and seller:\", seller.id); // EN: Log initiation // VI: Ghi nhật ký bắt đầu // ES: Registrar el inicio\n    // SPEC: Section on protocol initiation\n  },\n  negotiate: function (\n    buyer: z.infer<typeof BuyerAgentSchema>,\n    seller: z.infer<typeof SellerAgentSchema>\n  ): void {\n    // EN: Handles the negotiation process // VI: Xử lý quy trình thương lượng // ES: Maneja el proceso de negociación\n    console.log(\"ACP negotiation in progress between buyer:\", buyer.id, \"and seller:\", seller.id); // EN: Log negotiation // VI: Ghi nhật ký thương lượng // ES: Registrar la negociación\n    // SPEC: Section on negotiation\n  },\n  finalize: function (\n    buyer: z.infer<typeof BuyerAgentSchema>,\n    seller: z.infer<typeof SellerAgentSchema>\n  ): void {\n    // EN: Finalizes the transaction // VI: Hoàn tất giao dịch // ES: Finaliza la transacción\n    console.log(\"ACP finalized between buyer:\", buyer.id, \"and seller:\", seller.id); // EN: Log finalization // VI: Ghi nhật ký hoàn tất // ES: Registrar la finalización\n    // SPEC: Section on transaction finalization\n  },\n};\n\nexport { agentChoreography, acp, BuyerAgentSchema as buyer_agent, SellerAgentSchema as seller_agent, MarketplaceAgentSchema as marketplace_agent };\n```",
  "spec_id": "AI-05"
}

Audit Trail (2)

25d agotask_completedTeam Bravo
25d agotask_createdfoundry_collab
Task ID: c4fe92b6-6e2b-4c75-ae9e-c7d3ec6a43f2