Skip to content

Commit 735a936

Browse files
committed
Preserve free-mode block details
1 parent 02c33eb commit 735a936

6 files changed

Lines changed: 249 additions & 58 deletions

File tree

common/src/types/session-state.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ export const AgentOutputSchema = z.discriminatedUnion('type', [
6969
message: z.string(),
7070
statusCode: z.number().optional(),
7171
error: z.string().optional(),
72+
countryCode: z.string().optional(),
73+
countryBlockReason: z.string().optional(),
74+
ipPrivacySignals: z.array(z.string()).optional(),
7275
}),
7376
])
7477
export type AgentOutput = z.infer<typeof AgentOutputSchema>

common/src/util/error.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,56 @@ export function unwrapPromptResult<T>(result: PromptResult<T>): T {
198198
export function parseApiErrorResponseBody(responseBody: unknown): {
199199
errorCode?: string
200200
message?: string
201+
countryCode?: string
202+
countryBlockReason?: string
203+
ipPrivacySignals?: string[]
201204
} {
202205
if (typeof responseBody !== 'string') return {}
203206
try {
204207
const parsed: unknown = JSON.parse(responseBody)
205208
if (!parsed || typeof parsed !== 'object') return {}
206-
const result: { errorCode?: string; message?: string } = {}
207-
if ('error' in parsed && typeof (parsed as { error: unknown }).error === 'string') {
209+
const result: {
210+
errorCode?: string
211+
message?: string
212+
countryCode?: string
213+
countryBlockReason?: string
214+
ipPrivacySignals?: string[]
215+
} = {}
216+
if (
217+
'error' in parsed &&
218+
typeof (parsed as { error: unknown }).error === 'string'
219+
) {
208220
result.errorCode = (parsed as { error: string }).error
209221
}
210-
if ('message' in parsed && typeof (parsed as { message: unknown }).message === 'string') {
222+
if (
223+
'message' in parsed &&
224+
typeof (parsed as { message: unknown }).message === 'string'
225+
) {
211226
result.message = (parsed as { message: string }).message
212227
}
228+
if (
229+
'countryCode' in parsed &&
230+
typeof (parsed as { countryCode: unknown }).countryCode === 'string'
231+
) {
232+
result.countryCode = (parsed as { countryCode: string }).countryCode
233+
}
234+
if (
235+
'countryBlockReason' in parsed &&
236+
typeof (parsed as { countryBlockReason: unknown }).countryBlockReason ===
237+
'string'
238+
) {
239+
result.countryBlockReason = (
240+
parsed as { countryBlockReason: string }
241+
).countryBlockReason
242+
}
243+
if ('ipPrivacySignals' in parsed) {
244+
const signals = (parsed as { ipPrivacySignals: unknown }).ipPrivacySignals
245+
if (Array.isArray(signals)) {
246+
result.ipPrivacySignals = signals.filter(
247+
(signal): signal is string => typeof signal === 'string',
248+
)
249+
}
250+
}
213251
return result
214252
} catch {
215253
return {}

packages/agent-runtime/src/__tests__/loop-agent-steps.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
955955
responseBody: JSON.stringify({
956956
error: 'free_mode_unavailable',
957957
message: 'Free mode is not available in your country.',
958+
countryCode: 'US',
959+
countryBlockReason: 'anonymous_network',
960+
ipPrivacySignals: ['vpn', 'hosting'],
958961
}),
959962
isRetryable: false,
960963
})
@@ -976,6 +979,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
976979
expect(result.output.error).toBe('free_mode_unavailable')
977980
// Should propagate the status code
978981
expect(result.output.statusCode).toBe(403)
982+
expect(result.output.countryCode).toBe('US')
983+
expect(result.output.countryBlockReason).toBe('anonymous_network')
984+
expect(result.output.ipPrivacySignals).toEqual(['vpn', 'hosting'])
979985
}
980986
})
981987

packages/agent-runtime/src/run-agent-step.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,11 +1097,21 @@ export async function loopAgentSteps(
10971097

10981098
let errorMessage = ''
10991099
let errorCode: string | undefined
1100+
let countryCode: string | undefined
1101+
let countryBlockReason: string | undefined
1102+
let ipPrivacySignals: string[] | undefined
11001103
let hasServerMessage = false
11011104
if (error instanceof APICallError) {
11021105
errorMessage = `${error.message}`
11031106
const parsed = parseApiErrorResponseBody(error.responseBody)
11041107
if (parsed.errorCode) errorCode = parsed.errorCode
1108+
if (parsed.countryCode) countryCode = parsed.countryCode
1109+
if (parsed.countryBlockReason) {
1110+
countryBlockReason = parsed.countryBlockReason
1111+
}
1112+
if (parsed.ipPrivacySignals) {
1113+
ipPrivacySignals = parsed.ipPrivacySignals
1114+
}
11051115
if (parsed.message) {
11061116
errorMessage = parsed.message
11071117
hasServerMessage = true
@@ -1139,6 +1149,9 @@ export async function loopAgentSteps(
11391149
message: hasServerMessage ? errorMessage : 'Agent run error: ' + errorMessage,
11401150
...(statusCode !== undefined && { statusCode }),
11411151
...(errorCode !== undefined && { error: errorCode }),
1152+
...(countryCode !== undefined && { countryCode }),
1153+
...(countryBlockReason !== undefined && { countryBlockReason }),
1154+
...(ipPrivacySignals !== undefined && { ipPrivacySignals }),
11421155
},
11431156
}
11441157
}

0 commit comments

Comments
 (0)