(agent: Agent)
| 33 | } |
| 34 | |
| 35 | async getPropertiesForAgent(agent: Agent): Promise<AgentPropertiesProfile> { |
| 36 | const cached = this.cache.get(agent.url); |
| 37 | if (cached && Date.now() - new Date(cached.last_fetched).getTime() < this.CACHE_TTL_MS) { |
| 38 | return cached; |
| 39 | } |
| 40 | |
| 41 | let properties: PropertyInfo[] = []; |
| 42 | let error: string | undefined; |
| 43 | |
| 44 | try { |
| 45 | // Create agent client with the new API |
| 46 | const agentConfig = { |
| 47 | id: agent.name, |
| 48 | name: agent.name, |
| 49 | agent_uri: agent.url, |
| 50 | protocol: (agent.protocol || "mcp") as "mcp" | "a2a", |
| 51 | }; |
| 52 | const multiClient = new AdCPClient([agentConfig], { userAgent: AAO_UA_DISCOVERY }); |
| 53 | const client = multiClient.agent(agent.name); |
| 54 | const result = await client.executeTask("list_authorized_properties", {}); |
| 55 | |
| 56 | if (result.success && result.data) { |
| 57 | const response = result.data as Record<string, unknown>; |
| 58 | |
| 59 | // Handle different response formats: |
| 60 | // 1. Array of properties directly |
| 61 | if (Array.isArray(response)) { |
| 62 | properties = response; |
| 63 | } |
| 64 | // 2. Object with publisher_domains (convert to properties format) |
| 65 | else if (response?.publisher_domains && Array.isArray(response.publisher_domains)) { |
| 66 | properties = response.publisher_domains.map((domain: string) => ({ |
| 67 | identifier: domain, |
| 68 | domain: domain, |
| 69 | type: "domain", |
| 70 | tags: response.primary_channels ? |
| 71 | (Array.isArray(response.primary_channels) ? response.primary_channels : [response.primary_channels]) |
| 72 | : undefined, |
| 73 | country: response.primary_countries ? |
| 74 | (Array.isArray(response.primary_countries) ? response.primary_countries[0] : response.primary_countries) |
| 75 | : undefined, |
| 76 | description: response.portfolio_description as string | undefined, |
| 77 | })); |
| 78 | } |
| 79 | // 3. Any object - try to coerce to PropertyInfo |
| 80 | else if (response) { |
| 81 | properties = [response as PropertyInfo]; |
| 82 | } |
| 83 | } else if (!result.success) { |
| 84 | error = `Agent returned error: ${result.error || "Unknown error"}`; |
| 85 | } |
| 86 | } catch (toolError: any) { |
| 87 | error = `Agent does not support list_authorized_properties: ${toolError.message}`; |
| 88 | } |
| 89 | |
| 90 | // Verify each property by checking .well-known/adagents.json |
| 91 | if (properties.length > 0) { |
| 92 | await Promise.all( |
no test coverage detected