|
| 1 | +import crypto from 'node:crypto' |
| 2 | + |
| 3 | +import { |
| 4 | + aiFunction, |
| 5 | + AIFunctionsProvider, |
| 6 | + getEnv, |
| 7 | + throttleKy |
| 8 | +} from '@agentic/core' |
| 9 | +import defaultKy, { type KyInstance } from 'ky' |
| 10 | +import pThrottle from 'p-throttle' |
| 11 | +import z from 'zod' |
| 12 | + |
| 13 | +export namespace gravatar { |
| 14 | + export const API_BASE_URL = 'https://api.gravatar.com' |
| 15 | + |
| 16 | + // Allow up to 100 unauthenticated requests per hour by default. |
| 17 | + export const unauthenticatedThrottle = pThrottle({ |
| 18 | + limit: 100, |
| 19 | + interval: 60 * 60 * 1000 |
| 20 | + }) |
| 21 | + |
| 22 | + // Allow up to 1000 authenticated requests per hour by default. |
| 23 | + export const authenticatedThrottle = pThrottle({ |
| 24 | + limit: 1000, |
| 25 | + interval: 60 * 60 * 1000 |
| 26 | + }) |
| 27 | + |
| 28 | + export type GetProfileByIdentifierOptions = { |
| 29 | + email: string |
| 30 | + } |
| 31 | + |
| 32 | + export interface Profile { |
| 33 | + /** The SHA256 hash of the user’s primary email address. */ |
| 34 | + hash: string |
| 35 | + /** The user’s display name that appears on their profile. */ |
| 36 | + display_name: string |
| 37 | + /** The full URL to the user’s Gravatar profile. */ |
| 38 | + profile_url: string |
| 39 | + /** The URL to the user’s avatar image, if set. */ |
| 40 | + avatar_url: string |
| 41 | + /** Alternative text describing the user’s avatar. */ |
| 42 | + avatar_alt_text: string |
| 43 | + /** The user’s geographical location. */ |
| 44 | + location: string |
| 45 | + /** A short biography or description about the user found on their profile. */ |
| 46 | + description: string |
| 47 | + /** The user’s current job title. */ |
| 48 | + job_title: string |
| 49 | + /** The name of the company where the user is employed. */ |
| 50 | + company: string |
| 51 | + /** An array of verified accounts the user has added to their profile. The number of verified accounts displayed is limited to a maximum of 4 in unauthenticated requests. */ |
| 52 | + verified_accounts: any[] |
| 53 | + /** A phonetic guide to pronouncing the user’s name. */ |
| 54 | + pronunciation: string |
| 55 | + /** The pronouns the user prefers to use. */ |
| 56 | + pronouns: string |
| 57 | + |
| 58 | + /** The total number of verified accounts the user has added to their profile, including those not displayed on their profile. This property is only provided in authenticated API requests. */ |
| 59 | + number_verified_accounts?: number |
| 60 | + |
| 61 | + /** The date and time (UTC) when the user last edited their profile. This property is only provided in authenticated API requests. Example: "2021-10-01T12:00:00Z" */ |
| 62 | + last_profile_edit?: string |
| 63 | + |
| 64 | + /** The date the user registered their account. This property is only provided in authenticated API requests. Example: "2021-10-01" */ |
| 65 | + registration_date?: string |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * A client for the Gravatar API. |
| 71 | + * |
| 72 | + * API key is optional. |
| 73 | + * |
| 74 | + * @see https://docs.gravatar.com/getting-started/ |
| 75 | + */ |
| 76 | +export class GravatarClient extends AIFunctionsProvider { |
| 77 | + protected readonly ky: KyInstance |
| 78 | + protected readonly apiKey?: string |
| 79 | + protected readonly apiBaseUrl: string |
| 80 | + |
| 81 | + constructor({ |
| 82 | + apiKey = getEnv('GRAVATAR_API_KEY'), |
| 83 | + apiBaseUrl = gravatar.API_BASE_URL, |
| 84 | + timeoutMs = 60_000, |
| 85 | + throttle = true, |
| 86 | + ky = defaultKy |
| 87 | + }: { |
| 88 | + apiKey?: string |
| 89 | + apiBaseUrl?: string |
| 90 | + timeoutMs?: number |
| 91 | + throttle?: boolean |
| 92 | + ky?: KyInstance |
| 93 | + } = {}) { |
| 94 | + super() |
| 95 | + |
| 96 | + // API key is optional |
| 97 | + this.apiKey = apiKey |
| 98 | + this.apiBaseUrl = apiBaseUrl |
| 99 | + |
| 100 | + const throttledKy = throttle |
| 101 | + ? throttleKy( |
| 102 | + ky, |
| 103 | + apiKey |
| 104 | + ? gravatar.authenticatedThrottle |
| 105 | + : gravatar.unauthenticatedThrottle |
| 106 | + ) |
| 107 | + : ky |
| 108 | + |
| 109 | + this.ky = throttledKy.extend({ |
| 110 | + prefixUrl: apiBaseUrl, |
| 111 | + timeout: timeoutMs, |
| 112 | + headers: Object.fromEntries( |
| 113 | + apiKey ? [['Authorization', `Bearer ${apiKey}`]] : [] |
| 114 | + ) |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + @aiFunction({ |
| 119 | + name: 'gravatar_get_profile', |
| 120 | + description: |
| 121 | + 'Get Gravatar profile by email. Returns a profile object or `undefined` if not found.', |
| 122 | + inputSchema: z.object({ |
| 123 | + email: z.string() |
| 124 | + }) |
| 125 | + }) |
| 126 | + async getProfileByIdentifier( |
| 127 | + emailOrOpts: string | gravatar.GetProfileByIdentifierOptions |
| 128 | + ): Promise<gravatar.Profile | undefined> { |
| 129 | + const { email } = |
| 130 | + typeof emailOrOpts === 'string' ? { email: emailOrOpts } : emailOrOpts |
| 131 | + const hashedEmail = crypto |
| 132 | + .createHash('SHA256') |
| 133 | + .update(email.trim().toLowerCase(), 'utf8') |
| 134 | + .digest('hex') |
| 135 | + |
| 136 | + try { |
| 137 | + return await this.ky |
| 138 | + .get(`v3/profiles/${hashedEmail}`) |
| 139 | + .json<gravatar.Profile>() |
| 140 | + } catch (err: any) { |
| 141 | + if (err.response?.status === 404) { |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + throw err |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments