← Tasks
[AI-05] Goldilocks Odd Couple (prose)
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 Odd Couple (prose)
Created
25d ago
Completed
25d ago
Quality
Overall
71%
Pass
PASS
Graded By
-
Graded At
-
Cost
Input Tokens
-
Output Tokens
-
Est. USD
$0.3768
Result
{
"code": "```typescript\n// Importing necessary libraries\nimport { z } from 'zod';\nimport { v4 as uuidv4 } from 'uuid';\n\n// Zod Schemas\n// EN: Schema for agent types // VI: Schema cho loại agent // ES: Esquema para tipos de agentes\nconst AgentTypeSchema = z.enum(['buyer', 'seller', 'marketplace']);\n\n// EN: Schema for agent capabilities // VI: Schema cho khả năng của agent // ES: Esquema para capacidades de agentes\nconst AgentCapabilitySchema = z.object({\n canNegotiate: z.boolean(),\n canEscrow: z.boolean(),\n canVerify: z.boolean(),\n maxTransactionValue: z.number().positive().optional(),\n supportedCurrencies: z.array(z.string()).default(['USD'])\n});\n\n// EN: Schema for agent identity // VI: Schema cho danh tính của agent // ES: Esquema para identidad de agentes\nconst AgentIdentitySchema = z.object({\n id: z.string().uuid(),\n type: AgentTypeSchema,\n name: z.string().min(1).max(100),\n publicKey: z.string().optional(),\n capabilities: AgentCapabilitySchema,\n createdAt: z.date().default(() => new Date()),\n metadata: z.record(z.unknown()).optional()\n});\n\n// EN: Schema for ACP messages // VI: Schema cho tin nhắn ACP // ES: Esquema para mensajes ACP\nconst ACPMessageSchema = z.object({\n messageId: z.string().uuid(),\n timestamp: z.date(),\n senderId: z.string().uuid(),\n receiverId: z.string().uuid(),\n messageType: z.enum([\n 'INQUIRY',\n 'OFFER',\n 'COUNTER_OFFER',\n 'ACCEPT',\n 'REJECT',\n 'CONFIRM',\n 'CANCEL',\n 'ESCROW_REQUEST',\n 'ESCROW_RELEASE',\n 'VERIFICATION_REQUEST',\n 'VERIFICATION_RESPONSE'\n ]),\n payload: z.object({\n productId: z.string().optional(),\n price: z.number().positive().optional(),\n quantity: z.number().int().positive().optional(),\n terms: z.string().optional(),\n reason: z.string().optional(),\n data: z.record(z.unknown()).optional()\n }),\n replyTo: z.string().uuid().optional(),\n expiresAt: z.date().optional()\n});\n\n// EN: Schema for negotiation state // VI: Schema cho trạng thái đàm phán // ES: Esquema para estado de negociación\nconst NegotiationStateSchema = z.object({\n negotiationId: z.string().uuid(),\n status: z.enum([\n 'INITIATED',\n 'NEGOTIATING',\n 'AGREED',\n 'ESCROW_PENDING',\n 'COMPLETED',\n 'CANCELLED',\n 'EXPIRED',\n 'DISPUTED'\n ]),\n buyerId: z.string().uuid(),\n sellerId: z.string().uuid(),\n marketplaceId: z.string().uuid().optional(),\n currentOffer: z.number().positive().nullable(), // Ensure checks are done before using this value\n messageHistory: z.array(ACPMessageSchema),\n startedAt: z.date(),\n updatedAt: z.date(),\n completedAt: z.date().nullable(),\n terms: z.record(z.unknown()).optional()\n});\n\n// EN: Schema for buyer agents // VI: Schema cho agent người mua // ES: Esquema para agentes compradores\nconst BuyerAgentSchema = z.object({\n identity: AgentIdentitySchema.extend({ type: z.literal('buyer') }),\n preferences: z.object({\n maxBudget: z.number().positive(),\n targetPrice: z.number().positive(),\n urgency: z.enum(['low', 'medium', 'high']).default('medium'),\n autoNegotiate: z.boolean().default(false),\n maxRounds: z.number().int().positive().default(5)\n }),\n activeNegotiations: z.array(z.string().uuid()).default([])\n});\n\n// EN: Schema for seller agents // VI: Schema cho agent người bán // ES: Esquema para agentes vendedores\nconst SellerAgentSchema = z.object({\n identity: AgentIdentitySchema.extend({ type: z.literal('seller') }),\n inventory: z.array(z.object({\n productId: z.string(),\n minPrice: z.number().positive(),\n listPrice: z.number().positive(),\n quantity: z.number().int().nonnegative()\n })),\n policies: z.object({\n allowNegotiation: z.boolean().default(true),\n minAcceptableDiscount: z.number().min(0).max(100).default(0),\n autoAcceptThreshold: z.number().min(0).max(100).optional(),\n requireEscrow: z.boolean().default(false)\n }),\n activeNegotiations: z.array(z.string().uuid()).default([])\n});\n\n// EN: Schema for marketplace agents // VI: Schema cho agent sàn giao dịch // ES: Esquema para agentes de mercado\nconst MarketplaceAgentSchema = z.object({\n identity: AgentIdentitySchema.extend({ type: z.literal('marketplace') }),\n config: z.object({\n escrowEnabled: z.boolean().default(true),\n disputeResolution: z.boolean().default(true),\n transactionFeePercent: z.number().min(0).max(50).default(2.5),\n maxNegotiationDuration: z.number().positive().default(86400000) // 24h in ms\n }),\n activeTransactions: z.array(z.string().uuid()).default([])\n});\n\n// EN: Schema for ACP choreography // VI: Schema cho biên đạo ACP // ES: Esquema para coreografía ACP\nconst ACPChoreographySchema = z.object({\n choreographyId: z.string().uuid(),\n participants: z.object({\n buyer: BuyerAgentSchema,\n seller: SellerAgentSchema,\n marketplace: MarketplaceAgentSchema.optional()\n }),\n negotiation: NegotiationStateSchema,\n protocol: z.object({\n version: z.string().default('1.0'),\n maxMessageSize: z.number().positive().default(65536),\n timeout: z.number().positive().default(30000)\n })\n});\n\n// Types inferred from schemas\ntype AgentType = z.infer<typeof AgentTypeSchema>;\ntype AgentCapability = z.infer<typeof AgentCapabilitySchema>;\ntype AgentIdentity = z.infer<typeof AgentIdentitySchema>;\ntype ACPMessage = z.infer<typeof ACPMessageSchema>;\ntype NegotiationState = z.infer<typeof NegotiationStateSchema>;\ntype BuyerAgent = z.infer<typeof BuyerAgentSchema>;\ntype SellerAgent = z.infer<typeof SellerAgentSchema>;\ntype MarketplaceAgent = z.infer<typeof MarketplaceAgentSchema>;\ntype ACPChoreography = z.infer<typeof ACPChoreographySchema>;\n\n// Core Functions\n\n// EN: Create a new buyer agent with preferences // VI: Tạo một agent người mua mới với các tùy chọn // ES: Crear un nuevo agente comprador con preferencias\nfunction createBuyerAgent(params: {\n name: string;\n maxBudget: number;\n targetPrice: number;\n options?: Partial<BuyerAgent['preferences']>;\n}): { success: boolean; data?: BuyerAgent; error?: any } {\n const id = uuidv4();\n const identity = {\n id,\n type: 'buyer',\n name: params.name,\n capabilities: {\n canNegotiate: true,\n canEscrow: false,\n canVerify: false\n }\n };\n const preferences = BuyerAgentSchema.shape.preferences.partial().parse({\n ...params.options,\n maxBudget: params.maxBudget,\n targetPrice: params.targetPrice\n });\n const activeNegotiations: string[] = [];\n const result = BuyerAgentSchema.safeParse({ identity, preferences, activeNegotiations });\n if (result.success) {\n return { success: true, data: result.data };\n }\n return { success: false, error: result.error };\n}\n\n// EN: Create a new seller agent with inventory // VI: Tạo một agent người bán mới với kho hàng // ES: Crear un nuevo agente vendedor con inventario\nfunction createSellerAgent(params: {\n name: string;\n inventory: SellerAgent['inventory'];\n policies?: Partial<SellerAgent['policies']>;\n}): { success: boolean; data?: SellerAgent; error?: any } {\n const id = uuidv4();\n const identity = {\n id,\n type: 'seller',\n name: params.name,\n capabilities: {\n canNegotiate: true,\n canEscrow: true,\n canVerify: false\n }\n };\n const defaultPolicies = SellerAgentSchema.shape.policies.parse(params.policies || {});\n const activeNegotiations: string[] = [];\n const result = SellerAgentSchema.safeParse({ identity, inventory: params.inventory, policies: defaultPolicies, activeNegotiations });\n if (result.success) {\n return { success: true, data: result.data };\n }\n return { success: false, error: result.error };\n}\n\n// EN: Create marketplace orchestrator agent // VI: Tạo agent điều phối sàn giao dịch // ES: Crear agente orquestador del mercado\nfunction createMarketplaceAgent(params: {\n name: string;\n config?: Partial<MarketplaceAgent['config']>;\n}): { success: boolean; data?: MarketplaceAgent; error?: any } {\n const id = uuidv4();\n const identity = {\n id,\n type: 'marketplace',\n name: params.name,\n capabilities: {\n canNegotiate: false,\n canEscrow: true,\n canVerify: true\n }\n };\n const config = MarketplaceAgentSchema.shape.config.partial().parse(params.config || {});\n const activeTransactions: string[] = [];\n const result = MarketplaceAgentSchema.safeParse({ identity, config, activeTransactions });\n if (result.success) {\n return { success: true, data: result.data };\n }\n return { success: false, error: result.error };\n}\n\n// Exporting defined schemas, types, and functions\nexport {\n AgentTypeSchema,\n AgentCapabilitySchema,\n AgentIdentitySchema,\n ACPMessageSchema,\n NegotiationStateSchema,\n BuyerAgentSchema,\n SellerAgentSchema,\n MarketplaceAgentSchema,\n ACPChoreographySchema,\n AgentType,\n AgentCapability,\n AgentIdentity,\n ACPMessage,\n NegotiationState,\n BuyerAgent,\n SellerAgent,\n MarketplaceAgent,\n ACPChoreography,\n createBuyerAgent,\n createSellerAgent,\n createMarketplaceAgent,\n};\n\n// Additional functions such as acp, buyer_agent, and seller_agent need to be implemented as per SPEC requirements.\n```",
"spec_id": "AI-05"
}Audit Trail (2)
25d agotask_completedTeam Odd Couple (prose)
25d agotask_createdfoundry_goldilocks
Task ID: 66770dad-cf16-4d42-8f06-c1af3a3defff