|
| 1 | +import type { ShortIoGetAnalyticsParams } from '@/tools/short_io/types' |
| 2 | +import type { ToolConfig, ToolResponse } from '@/tools/types' |
| 3 | + |
| 4 | +const STATS_PERIOD_MAP: Record<string, string> = { |
| 5 | + today: 'today', |
| 6 | + yesterday: 'yesterday', |
| 7 | + last_7_days: 'last7', |
| 8 | + last_30_days: 'last30', |
| 9 | + all_time: 'total', |
| 10 | + week: 'week', |
| 11 | + month: 'month', |
| 12 | + lastmonth: 'lastmonth', |
| 13 | +} |
| 14 | + |
| 15 | +export const shortIoGetAnalyticsTool: ToolConfig<ShortIoGetAnalyticsParams, ToolResponse> = { |
| 16 | + id: 'short_io_get_analytics', |
| 17 | + name: 'Short.io Get Link Statistics', |
| 18 | + description: |
| 19 | + 'Fetch click statistics for a Short.io link (Statistics API: totalClicks, humanClicks, referer, country, etc.).', |
| 20 | + version: '1.0', |
| 21 | + params: { |
| 22 | + apiKey: { |
| 23 | + type: 'string', |
| 24 | + required: true, |
| 25 | + visibility: 'hidden', |
| 26 | + description: 'Short.io Secret API Key', |
| 27 | + }, |
| 28 | + linkId: { type: 'string', required: true, visibility: 'user-or-llm', description: 'Link ID' }, |
| 29 | + period: { |
| 30 | + type: 'string', |
| 31 | + required: true, |
| 32 | + visibility: 'user-or-llm', |
| 33 | + description: 'Period: today, yesterday, last7, last30, total, week, month, lastmonth', |
| 34 | + }, |
| 35 | + tz: { type: 'string', required: false, visibility: 'hidden', description: 'Timezone (default UTC)' }, |
| 36 | + }, |
| 37 | + request: { |
| 38 | + url: (params) => { |
| 39 | + const base = 'https://statistics.short.io/statistics/link/' + encodeURIComponent(params.linkId) |
| 40 | + const period = STATS_PERIOD_MAP[params.period] ?? params.period ?? 'last30' |
| 41 | + const q = new URLSearchParams({ period }) |
| 42 | + if (params.tz) q.set('tz', params.tz) |
| 43 | + return `${base}?${q.toString()}` |
| 44 | + }, |
| 45 | + method: 'GET', |
| 46 | + headers: (params) => ({ |
| 47 | + Authorization: params.apiKey, |
| 48 | + Accept: 'application/json', |
| 49 | + }), |
| 50 | + }, |
| 51 | + transformResponse: async (response: Response) => { |
| 52 | + if (!response.ok) { |
| 53 | + const err = await response.text().catch(() => response.statusText) |
| 54 | + return { success: false, output: { success: false, error: err } } |
| 55 | + } |
| 56 | + const data = await response.json().catch(() => ({})) |
| 57 | + const totalClicks = data.totalClicks ?? data.clicks ?? 0 |
| 58 | + const humanClicks = data.humanClicks ?? totalClicks |
| 59 | + return { |
| 60 | + success: true, |
| 61 | + output: { |
| 62 | + success: true, |
| 63 | + clicks: totalClicks, |
| 64 | + totalClicks, |
| 65 | + humanClicks, |
| 66 | + totalClicksChange: data.totalClicksChange, |
| 67 | + humanClicksChange: data.humanClicksChange, |
| 68 | + referer: data.referer ?? [], |
| 69 | + country: data.country ?? [], |
| 70 | + browser: data.browser ?? [], |
| 71 | + os: data.os ?? [], |
| 72 | + city: data.city ?? [], |
| 73 | + device: data.device ?? [], |
| 74 | + social: data.social ?? [], |
| 75 | + utmMedium: data.utm_medium ?? [], |
| 76 | + utmSource: data.utm_source ?? [], |
| 77 | + utmCampaign: data.utm_campaign ?? [], |
| 78 | + clickStatistics: data.clickStatistics, |
| 79 | + interval: data.interval, |
| 80 | + }, |
| 81 | + } |
| 82 | + }, |
| 83 | + outputs: { |
| 84 | + success: { type: 'boolean', description: 'Success status' }, |
| 85 | + clicks: { type: 'number', description: 'Total clicks in period' }, |
| 86 | + totalClicks: { type: 'number', description: 'Total clicks' }, |
| 87 | + humanClicks: { type: 'number', description: 'Human clicks' }, |
| 88 | + totalClicksChange: { type: 'string', description: 'Change vs previous period' }, |
| 89 | + humanClicksChange: { type: 'string', description: 'Human clicks change' }, |
| 90 | + referer: { type: 'array', description: 'Referrer breakdown (referer, score)' }, |
| 91 | + country: { type: 'array', description: 'Country breakdown (countryName, country, score)' }, |
| 92 | + browser: { type: 'array', description: 'Browser breakdown (browser, score)' }, |
| 93 | + os: { type: 'array', description: 'OS breakdown (os, score)' }, |
| 94 | + city: { type: 'array', description: 'City breakdown (city, name, countryCode, score)' }, |
| 95 | + device: { type: 'array', description: 'Device breakdown' }, |
| 96 | + social: { type: 'array', description: 'Social source breakdown (social, score)' }, |
| 97 | + utmMedium: { type: 'array', description: 'UTM medium breakdown' }, |
| 98 | + utmSource: { type: 'array', description: 'UTM source breakdown' }, |
| 99 | + utmCampaign: { type: 'array', description: 'UTM campaign breakdown' }, |
| 100 | + clickStatistics: { |
| 101 | + type: 'object', |
| 102 | + description: 'Time-series click data (datasets with x/y points per interval)', |
| 103 | + }, |
| 104 | + interval: { |
| 105 | + type: 'object', |
| 106 | + description: 'Date range (startDate, endDate, prevStartDate, prevEndDate, tz)', |
| 107 | + }, |
| 108 | + error: { type: 'string', description: 'Error message' }, |
| 109 | + }, |
| 110 | +} |
0 commit comments