Skip to content

Commit b339b48

Browse files
waleedlatif1claude
andcommitted
fix(meta-ads): address greptile review — prefix guard, pagination clarity, account statuses, DELETED filter
- Add stripActPrefix() helper to prevent act_ double-prefix bug when users provide prefixed IDs - Clarify totalCount descriptions to indicate response-level count (not total in account) - Show all ad accounts in selector with status badges instead of silently filtering to active only - Add DELETED to status filter dropdown options Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 320283a commit b339b48

File tree

10 files changed

+51
-25
lines changed

10 files changed

+51
-25
lines changed

apps/docs/content/docs/en/tools/meta_ads.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ List campaigns in a Meta Ads account with optional status filtering
7373
|`lifetimeBudget` | string | Lifetime budget in account currency cents |
7474
|`createdTime` | string | Campaign creation time \(ISO 8601\) |
7575
|`updatedTime` | string | Campaign last update time \(ISO 8601\) |
76-
| `totalCount` | number | Total number of campaigns returned |
76+
| `totalCount` | number | Number of campaigns returned in this response \(may be limited by pagination\) |
7777

7878
### `meta_ads_list_ad_sets`
7979

@@ -101,7 +101,7 @@ List ad sets in a Meta Ads account or campaign
101101
|`lifetimeBudget` | string | Lifetime budget in account currency cents |
102102
|`startTime` | string | Ad set start time \(ISO 8601\) |
103103
|`endTime` | string | Ad set end time \(ISO 8601\) |
104-
| `totalCount` | number | Total number of ad sets returned |
104+
| `totalCount` | number | Number of ad sets returned in this response \(may be limited by pagination\) |
105105

106106
### `meta_ads_list_ads`
107107

@@ -129,7 +129,7 @@ List ads in a Meta Ads account, campaign, or ad set
129129
|`campaignId` | string | Parent campaign ID |
130130
|`createdTime` | string | Ad creation time \(ISO 8601\) |
131131
|`updatedTime` | string | Ad last update time \(ISO 8601\) |
132-
| `totalCount` | number | Total number of ads returned |
132+
| `totalCount` | number | Number of ads returned in this response \(may be limited by pagination\) |
133133

134134
### `meta_ads_get_insights`
135135

@@ -171,6 +171,6 @@ Get performance insights and metrics for Meta Ads campaigns, ad sets, or ads
171171
|`conversions` | number | Total conversions from actions |
172172
|`dateStart` | string | Start date of the reporting period |
173173
|`dateStop` | string | End date of the reporting period |
174-
| `totalCount` | number | Total number of insight rows returned |
174+
| `totalCount` | number | Number of insight rows returned in this response \(may be limited by pagination\) |
175175

176176

apps/sim/app/api/tools/meta_ads/accounts/route.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,27 @@ export async function POST(request: Request) {
6767
const data = await response.json()
6868
const items: MetaAdAccount[] = data.data ?? []
6969

70-
const accounts = items
71-
.filter((account) => account.account_status === 1)
72-
.map((account) => ({
70+
const META_ACCOUNT_STATUS: Record<number, string> = {
71+
1: 'Active',
72+
2: 'Disabled',
73+
3: 'Unsettled',
74+
7: 'Pending Risk Review',
75+
8: 'Pending Settlement',
76+
9: 'In Grace Period',
77+
100: 'Pending Closure',
78+
101: 'Closed',
79+
201: 'Any Active',
80+
202: 'Any Closed',
81+
}
82+
83+
const accounts = items.map((account) => {
84+
const statusLabel = META_ACCOUNT_STATUS[account.account_status] ?? 'Unknown'
85+
const suffix = account.account_status !== 1 ? ` (${statusLabel})` : ''
86+
return {
7387
id: account.account_id,
74-
name: account.name || `Account ${account.account_id}`,
75-
}))
88+
name: `${account.name || `Account ${account.account_id}`}${suffix}`,
89+
}
90+
})
7691

7792
logger.info(`Successfully fetched ${accounts.length} Meta ad accounts`, {
7893
total: items.length,

apps/sim/app/api/tools/meta_ads/campaigns/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextResponse } from 'next/server'
33
import { authorizeCredentialUse } from '@/lib/auth/credential-access'
44
import { generateRequestId } from '@/lib/core/utils/request'
55
import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils'
6-
import { getMetaApiBaseUrl } from '@/tools/meta_ads/types'
6+
import { getMetaApiBaseUrl, stripActPrefix } from '@/tools/meta_ads/types'
77

88
export const dynamic = 'force-dynamic'
99

@@ -55,7 +55,7 @@ export async function POST(request: Request) {
5555
)
5656
}
5757

58-
const trimmedId = String(accountId).trim()
58+
const trimmedId = stripActPrefix(String(accountId))
5959
const url = `${getMetaApiBaseUrl()}/act_${trimmedId}/campaigns?fields=id,name,status&limit=200`
6060
const response = await fetch(url, {
6161
method: 'GET',

apps/sim/blocks/blocks/meta_ads.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ export const MetaAdsBlock: BlockConfig = {
186186
{ label: 'Active', id: 'ACTIVE' },
187187
{ label: 'Paused', id: 'PAUSED' },
188188
{ label: 'Archived', id: 'ARCHIVED' },
189+
{ label: 'Deleted', id: 'DELETED' },
189190
],
190191
mode: 'advanced',
191192
condition: {

apps/sim/tools/meta_ads/get_account.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MetaAdsGetAccountParams, MetaAdsGetAccountResponse } from '@/tools/meta_ads/types'
2-
import { getMetaApiBaseUrl } from '@/tools/meta_ads/types'
2+
import { getMetaApiBaseUrl, stripActPrefix } from '@/tools/meta_ads/types'
33
import type { ToolConfig } from '@/tools/types'
44

55
export const metaAdsGetAccountTool: ToolConfig<MetaAdsGetAccountParams, MetaAdsGetAccountResponse> =
@@ -33,7 +33,7 @@ export const metaAdsGetAccountTool: ToolConfig<MetaAdsGetAccountParams, MetaAdsG
3333
url: (params) => {
3434
const fields =
3535
'id,name,account_status,currency,timezone_name,amount_spent,spend_cap,business_country_code'
36-
return `${getMetaApiBaseUrl()}/act_${params.accountId.trim()}?fields=${fields}`
36+
return `${getMetaApiBaseUrl()}/act_${stripActPrefix(params.accountId)}?fields=${fields}`
3737
},
3838
method: 'GET',
3939
headers: (params) => ({

apps/sim/tools/meta_ads/get_insights.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MetaAdsGetInsightsParams, MetaAdsGetInsightsResponse } from '@/tools/meta_ads/types'
2-
import { getMetaApiBaseUrl } from '@/tools/meta_ads/types'
2+
import { getMetaApiBaseUrl, stripActPrefix } from '@/tools/meta_ads/types'
33
import type { ToolConfig } from '@/tools/types'
44

55
export const metaAdsGetInsightsTool: ToolConfig<
@@ -101,7 +101,7 @@ export const metaAdsGetInsightsTool: ToolConfig<
101101
} else if (params.campaignId) {
102102
parentId = params.campaignId.trim()
103103
} else {
104-
parentId = `act_${params.accountId.trim()}`
104+
parentId = `act_${stripActPrefix(params.accountId)}`
105105
}
106106

107107
return `${getMetaApiBaseUrl()}/${parentId}/insights?${searchParams.toString()}`
@@ -203,7 +203,8 @@ export const metaAdsGetInsightsTool: ToolConfig<
203203
},
204204
totalCount: {
205205
type: 'number',
206-
description: 'Total number of insight rows returned',
206+
description:
207+
'Number of insight rows returned in this response (may be limited by pagination)',
207208
},
208209
},
209210
}

apps/sim/tools/meta_ads/list_ad_sets.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MetaAdsListAdSetsParams, MetaAdsListAdSetsResponse } from '@/tools/meta_ads/types'
2-
import { getMetaApiBaseUrl } from '@/tools/meta_ads/types'
2+
import { getMetaApiBaseUrl, stripActPrefix } from '@/tools/meta_ads/types'
33
import type { ToolConfig } from '@/tools/types'
44

55
export const metaAdsListAdSetsTool: ToolConfig<MetaAdsListAdSetsParams, MetaAdsListAdSetsResponse> =
@@ -59,7 +59,7 @@ export const metaAdsListAdSetsTool: ToolConfig<MetaAdsListAdSetsParams, MetaAdsL
5959
searchParams.set('limit', String(params.limit))
6060
}
6161

62-
const parentId = params.campaignId?.trim() || `act_${params.accountId.trim()}`
62+
const parentId = params.campaignId?.trim() || `act_${stripActPrefix(params.accountId)}`
6363
return `${getMetaApiBaseUrl()}/${parentId}/adsets?${searchParams.toString()}`
6464
},
6565
method: 'GET',
@@ -124,7 +124,7 @@ export const metaAdsListAdSetsTool: ToolConfig<MetaAdsListAdSetsParams, MetaAdsL
124124
},
125125
totalCount: {
126126
type: 'number',
127-
description: 'Total number of ad sets returned',
127+
description: 'Number of ad sets returned in this response (may be limited by pagination)',
128128
},
129129
},
130130
}

apps/sim/tools/meta_ads/list_ads.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MetaAdsListAdsParams, MetaAdsListAdsResponse } from '@/tools/meta_ads/types'
2-
import { getMetaApiBaseUrl } from '@/tools/meta_ads/types'
2+
import { getMetaApiBaseUrl, stripActPrefix } from '@/tools/meta_ads/types'
33
import type { ToolConfig } from '@/tools/types'
44

55
export const metaAdsListAdsTool: ToolConfig<MetaAdsListAdsParams, MetaAdsListAdsResponse> = {
@@ -70,7 +70,7 @@ export const metaAdsListAdsTool: ToolConfig<MetaAdsListAdsParams, MetaAdsListAds
7070
} else if (params.campaignId) {
7171
parentId = params.campaignId.trim()
7272
} else {
73-
parentId = `act_${params.accountId.trim()}`
73+
parentId = `act_${stripActPrefix(params.accountId)}`
7474
}
7575

7676
return `${getMetaApiBaseUrl()}/${parentId}/ads?${searchParams.toString()}`
@@ -132,7 +132,7 @@ export const metaAdsListAdsTool: ToolConfig<MetaAdsListAdsParams, MetaAdsListAds
132132
},
133133
totalCount: {
134134
type: 'number',
135-
description: 'Total number of ads returned',
135+
description: 'Number of ads returned in this response (may be limited by pagination)',
136136
},
137137
},
138138
}

apps/sim/tools/meta_ads/list_campaigns.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {
22
MetaAdsListCampaignsParams,
33
MetaAdsListCampaignsResponse,
44
} from '@/tools/meta_ads/types'
5-
import { getMetaApiBaseUrl } from '@/tools/meta_ads/types'
5+
import { getMetaApiBaseUrl, stripActPrefix } from '@/tools/meta_ads/types'
66
import type { ToolConfig } from '@/tools/types'
77

88
export const metaAdsListCampaignsTool: ToolConfig<
@@ -59,7 +59,7 @@ export const metaAdsListCampaignsTool: ToolConfig<
5959
searchParams.set('limit', String(params.limit))
6060
}
6161

62-
return `${getMetaApiBaseUrl()}/act_${params.accountId.trim()}/campaigns?${searchParams.toString()}`
62+
return `${getMetaApiBaseUrl()}/act_${stripActPrefix(params.accountId)}/campaigns?${searchParams.toString()}`
6363
},
6464
method: 'GET',
6565
headers: (params) => ({
@@ -123,7 +123,7 @@ export const metaAdsListCampaignsTool: ToolConfig<
123123
},
124124
totalCount: {
125125
type: 'number',
126-
description: 'Total number of campaigns returned',
126+
description: 'Number of campaigns returned in this response (may be limited by pagination)',
127127
},
128128
},
129129
}

apps/sim/tools/meta_ads/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ export function getMetaApiBaseUrl(): string {
66
return `https://graph.facebook.com/${META_API_VERSION}`
77
}
88

9+
/**
10+
* Strips the `act_` prefix from an account ID if present, so that
11+
* callers can safely wrap the result with `act_` without double-prefixing.
12+
*/
13+
export function stripActPrefix(accountId: string): string {
14+
const trimmed = accountId.trim()
15+
return trimmed.startsWith('act_') ? trimmed.slice(4) : trimmed
16+
}
17+
918
export interface MetaAdsBaseParams {
1019
accessToken: string
1120
accountId: string

0 commit comments

Comments
 (0)