FOUNDRY
C8 Platform
← Tasks

[AI-05] ACP choreography (Vertex 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
Vertex Flash
Created
25d ago
Completed
25d ago

Quality

Overall
79%
Pass
PASS
Graded By
-
Graded At
-

Cost

Input Tokens
168
Output Tokens
1,771
Est. USD
-

Result

{
  "code": "```typescript\n// EN: ACP agent-to-agent choreography for marketplace agents. // VI: Biên đạo giao tiếp giữa các tác nhân ACP cho các tác nhân thị trường. // ES: Coreografía de agente a agente ACP para agentes de mercado.\n// src/lib/acp-agents.ts\n\nimport { z } from \"zod\";\nimport { NextResponse } from \"next/server\";\n\n// EN: Zod schema for a product. // VI: Lược đồ Zod cho một sản phẩm. // ES: Esquema Zod para un producto.\nconst ProductSchema = z.object({\n  id: z.string(),\n  name: z.string(),\n  description: z.string(),\n  price: z.number().positive(),\n  quantity: z.number().int().nonnegative(),\n});\n\n// EN: Zod schema for a buyer agent. // VI: Lược đồ Zod cho một tác nhân người mua. // ES: Esquema Zod para un agente comprador.\nconst BuyerAgentSchema = z.object({\n  id: z.string(),\n  type: z.literal(\"buyer\"),\n  preferences: z.array(z.string()), // EN: List of desired product names. // VI: Danh sách tên sản phẩm mong muốn. // ES: Lista de nombres de productos deseados.\n  budget: z.number().positive(),\n});\n\n// EN: Zod schema for a seller agent. // VI: Lược đồ Zod cho một tác nhân người bán. // ES: Esquema Zod para un agente vendedor.\nconst SellerAgentSchema = z.object({\n  id: z.string(),\n  type: z.literal(\"seller\"),\n  inventory: z.array(ProductSchema),\n});\n\n// EN: Zod schema for a marketplace agent. // VI: Lược đồ Zod cho một tác nhân thị trường. // ES: Esquema Zod para un agente de mercado.\nconst MarketplaceAgentSchema = z.object({\n  id: z.string(),\n  type: z.literal(\"marketplace\"),\n  products: z.array(ProductSchema),\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 Product = z.infer<typeof ProductSchema>;\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) message schema. // VI: Lược đồ tin nhắn Giao thức Giao tiếp Tác nhân (ACP). // ES: Esquema de mensaje del Protocolo de Comunicación de Agentes (ACP).\nconst ACPMessageSchema = z.object({\n  sender: z.string(),\n  receiver: z.string(),\n  type: z.string(), // EN: e.g., \"offer\", \"request\", \"accept\", \"reject\". // VI: ví dụ: \"offer\", \"request\", \"accept\", \"reject\". // ES: ej., \"offer\", \"request\", \"accept\", \"reject\".\n  content: z.any(), // EN: Can be any data depending on the message type. // VI: Có thể là bất kỳ dữ liệu nào tùy thuộc vào loại tin nhắn. // ES: Puede ser cualquier dato dependiendo del tipo de mensaje.\n});\n\ntype ACPMessage = z.infer<typeof ACPMessageSchema>;\n\n// EN: Function to simulate sending an ACP message. // VI: Hàm mô phỏng việc gửi tin nhắn ACP. // ES: Función para simular el envío de un mensaje ACP.\nasync function sendMessage(message: ACPMessage): Promise<NextResponse> {\n  const result = ACPMessageSchema.safeParse(message);\n  if (!result.success) {\n    console.error(\"Invalid ACP message:\", result.error);\n    return NextResponse.json({ error: \"Invalid ACP message\", details: result.error.errors }, { status: 400 });\n  }\n\n  // EN: In a real system, this would involve sending the message over a network. // VI: Trong một hệ thống thực, điều này sẽ liên quan đến việc gửi tin nhắn qua mạng. // ES: En un sistema real, esto implicaría enviar el mensaje a través de una red.\n  console.log(`[ACP] ${message.sender} -> ${message.receiver}: ${message.type}`);\n  console.log(\"[ACP] Content:\", message.content);\n\n  return NextResponse.json({ status: \"Message sent\", message: result.data }, { status: 200 });\n}\n\n// EN: Example agent choreography. // VI: Ví dụ về biên đạo tác nhân. // ES: Ejemplo de coreografía de agentes.\nasync function simulateTrade(buyer: BuyerAgent, seller: SellerAgent, marketplace: MarketplaceAgent) {\n  // EN: 1. Buyer requests a product from the marketplace. // VI: 1. Người mua yêu cầu một sản phẩm từ thị trường. // ES: 1. El comprador solicita un producto del mercado.\n  const requestMessage: ACPMessage = {\n    sender: buyer.id,\n    receiver: marketplace.id,\n    type: \"request\",\n    content: {\n      productName: buyer.preferences[0], // EN: Assuming the buyer wants their first preference. // VI: Giả sử người mua muốn sở thích đầu tiên của họ. // ES: Suponiendo que el comprador quiere su primera preferencia.\n    },\n  };\n\n  await sendMessage(requestMessage);\n\n  // EN: 2. Marketplace finds a seller with the product. // VI: 2. Thị trường tìm một người bán có sản phẩm. // ES: 2. El mercado encuentra un vendedor con el producto.\n  const productOffered = marketplace.products.find(\n    (p) => p.name === buyer.preferences[0] && p.quantity > 0\n  );\n\n  if (!productOffered) {\n    console.log(\"Product not found\");\n    return;\n  }\n\n  const offerMessage: ACPMessage = {\n    sender: marketplace.id,\n    receiver: buyer.id,\n    type: \"offer\",\n    content: productOffered,\n  };\n\n  await sendMessage(offerMessage);\n\n  // EN: 3. Buyer accepts the offer. // VI: 3. Người mua chấp nhận lời đề nghị. // ES: 3. El comprador acepta la oferta.\n  if (buyer.budget >= productOffered.price) {\n    const acceptMessage: ACPMessage = {\n      sender: buyer.id,\n      receiver: marketplace.id,\n      type: \"accept\",\n      content: {\n        productId: productOffered.id,\n        quantity: 1,\n      },\n    };\n\n    await sendMessage(acceptMessage);\n\n    // EN: 4. Marketplace informs the seller. // VI: 4. Thị trường thông báo cho người bán. // ES: 4. El mercado informa al vendedor.\n    const informSellerMessage: ACPMessage = {\n      sender: marketplace.id,\n      receiver: seller.id,\n      type: \"inform\",\n      content: {\n        productId: productOffered.id,\n        quantity: 1,\n        buyerId: buyer.id,\n      },\n    };\n    await sendMessage(informSellerMessage);\n\n    console.log(\"Trade completed!\");\n  } else {\n    const rejectMessage: ACPMessage = {\n      sender: buyer.id,\n      receiver: marketplace.id,\n      type: \"reject\",\n      content: \"Insufficient budget\",\n    };\n    await sendMessage(rejectMessage);\n    console.log(\"Trade failed: Insufficient budget.\");\n  }\n}\n\nexport {\n  BuyerAgentSchema,\n  SellerAgentSchema,\n  MarketplaceAgentSchema,\n  ProductSchema,\n  ACPMessageSchema,\n  sendMessage,\n  simulateTrade,\n};\n\nexport type { BuyerAgent, SellerAgent, MarketplaceAgent, Product, ACPMessage };\n```",
  "spec_id": "AI-05"
}

Audit Trail (2)

25d agotask_completedVertex Flash
25d agotask_createdfoundry_race
Task ID: 45956abf-444a-4709-b836-2c5f26931e4f