Skip to content

Commit 146ff9a

Browse files
committed
fix(odds-api-io): respect agent-provided filters and reject unreadable JSON
1 parent 894bbb0 commit 146ff9a

4 files changed

Lines changed: 165 additions & 11 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { OddsApiIoBlock } from '@/blocks/blocks/odds_api_io'
3+
4+
const mapParams = OddsApiIoBlock.tools.config?.params
5+
if (!mapParams) throw new Error('Odds-API.io block has no params mapper')
6+
7+
/** Runs the mapper the way an Agent tool call does: its result is spread over the merged args. */
8+
function toolCall(args: Record<string, unknown>, operation: string) {
9+
return { ...args, ...mapParams(args, { operation }) }
10+
}
11+
12+
/** Runs the mapper the way the canvas does: no context, the block's own operation input. */
13+
function canvas(args: Record<string, unknown>, operation: string) {
14+
return { ...args, ...mapParams({ ...args, operation }, undefined) }
15+
}
16+
17+
describe('OddsApiIoBlock params mapper', () => {
18+
it('lets the model pick the search status over the dropdown on a tool call', () => {
19+
expect(
20+
toolCall(
21+
{ searchStatus: 'upcoming', status: 'live', query: 'Arsenal' },
22+
'odds_api_io_search_events'
23+
)
24+
).toMatchObject({ status: 'live' })
25+
expect(
26+
toolCall({ searchStatus: 'live', query: 'Arsenal' }, 'odds_api_io_search_events')
27+
).toMatchObject({ status: 'live' })
28+
})
29+
30+
it('uses the dropdown on the canvas and the free-text status for Get Events', () => {
31+
expect(
32+
canvas({ searchStatus: 'live', query: 'Arsenal' }, 'odds_api_io_search_events')
33+
).toMatchObject({ status: 'live' })
34+
expect(
35+
canvas(
36+
{ searchStatus: 'live', sport: 'football', status: 'pending,live' },
37+
'odds_api_io_get_events'
38+
)
39+
).toMatchObject({
40+
status: 'pending,live',
41+
})
42+
})
43+
44+
it('lets the model include inactive leagues when the switch is off', () => {
45+
expect(toolCall({ sport: 'football', all: true }, 'odds_api_io_get_leagues')).toMatchObject({
46+
all: true,
47+
})
48+
expect(
49+
toolCall({ sport: 'football', includeInactiveLeagues: false }, 'odds_api_io_get_leagues').all
50+
).toBeUndefined()
51+
expect(
52+
canvas({ sport: 'football', includeInactiveLeagues: true }, 'odds_api_io_get_leagues')
53+
).toMatchObject({ all: true })
54+
expect(canvas({ sport: 'football', all: true }, 'odds_api_io_get_leagues').all).toBeUndefined()
55+
})
56+
57+
it('keeps model-provided filters that share the tool param name', () => {
58+
expect(
59+
toolCall(
60+
{
61+
sport: 'football',
62+
timeWindow: '24h',
63+
minDrop: '12.5',
64+
limit: '20',
65+
page: 2,
66+
includeEventDetails: false,
67+
},
68+
'odds_api_io_get_dropping_odds'
69+
)
70+
).toMatchObject({
71+
timeWindow: '24h',
72+
minDrop: 12.5,
73+
limit: 20,
74+
page: 2,
75+
includeEventDetails: false,
76+
})
77+
expect(toolCall({ bookmaker: 'Bet365' }, 'odds_api_io_get_value_bets')).toMatchObject({
78+
includeEventDetails: true,
79+
})
80+
})
81+
82+
it('drops fields that do not belong to the operation', () => {
83+
const result = toolCall({ sport: 'football', limit: '5', page: '3' }, 'odds_api_io_get_events')
84+
expect(result).toMatchObject({ limit: 5 })
85+
expect(result.page).toBeUndefined()
86+
expect(result.searchStatus).toBeUndefined()
87+
expect(result.includeInactiveLeagues).toBeUndefined()
88+
})
89+
})

‎apps/sim/blocks/blocks/odds_api_io.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { OddsApiIoIcon } from '@/components/icons'
22
import { AuthMode, type BlockConfig, type BlockMeta, IntegrationType } from '@/blocks/types'
3-
import { mapperOperation, parseOptionalNumberInput } from '@/blocks/utils'
3+
import {
4+
isToolCall,
5+
mapperOperation,
6+
parseOptionalBooleanInput,
7+
parseOptionalNumberInput,
8+
} from '@/blocks/utils'
49

510
const GET_SPORTS = 'odds_api_io_get_sports'
611
const GET_LEAGUES = 'odds_api_io_get_leagues'
@@ -332,12 +337,20 @@ export const OddsApiIoBlock: BlockConfig = {
332337
params: (params, context) => {
333338
const operation = mapperOperation(params, context)
334339
const { searchStatus, includeInactiveLeagues, ...rest } = params
340+
// The dropdown and switch always hold a value, so on a tool call the model's own
341+
// `status` / `all` argument wins and the canvas field only fills in when it gave none.
342+
const modelArg = (value: unknown) => (isToolCall(context) ? value : undefined)
335343
return {
336344
...rest,
337-
status: operation === SEARCH_EVENTS ? searchStatus : params.status,
338-
all: operation === GET_LEAGUES ? includeInactiveLeagues === true : undefined,
345+
status:
346+
operation === SEARCH_EVENTS ? (modelArg(params.status) ?? searchStatus) : params.status,
347+
all:
348+
operation === GET_LEAGUES
349+
? (modelArg(parseOptionalBooleanInput(params.all)) ??
350+
(includeInactiveLeagues === true ? true : undefined))
351+
: undefined,
339352
includeEventDetails: EVENT_DETAILS_OPERATIONS.includes(operation)
340-
? params.includeEventDetails !== false
353+
? (parseOptionalBooleanInput(params.includeEventDetails) ?? true)
341354
: undefined,
342355
minDrop:
343356
operation === GET_DROPPING_ODDS
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { commaList, oddsApiIoUrl, readOddsApiIoJson } from '@/tools/odds_api_io/utils'
3+
4+
describe('readOddsApiIoJson', () => {
5+
it('returns the parsed body of a successful response', async () => {
6+
await expect(
7+
readOddsApiIoJson(new Response('[{"name":"Football"}]', { status: 200 }), 'get sports')
8+
).resolves.toEqual([{ name: 'Football' }])
9+
})
10+
11+
it('throws the API error message on a failed response', async () => {
12+
await expect(
13+
readOddsApiIoJson(
14+
new Response('{"error":"You need to provide a valid apiKey"}', { status: 401 }),
15+
'get events'
16+
)
17+
).rejects.toThrow('You need to provide a valid apiKey')
18+
})
19+
20+
it('throws instead of treating an unreadable successful body as empty data', async () => {
21+
await expect(
22+
readOddsApiIoJson(new Response('<html>maintenance</html>', { status: 200 }), 'get events')
23+
).rejects.toThrow(/not valid JSON/)
24+
await expect(readOddsApiIoJson(new Response('', { status: 200 }), 'get event')).rejects.toThrow(
25+
/not valid JSON/
26+
)
27+
})
28+
})
29+
30+
describe('oddsApiIoUrl', () => {
31+
it('sends the key as apiKey and skips empty filters', () => {
32+
const url = new URL(
33+
oddsApiIoUrl('/events', ' key ', { sport: 'football', league: '', limit: 5, to: undefined })
34+
)
35+
expect(url.pathname).toBe('/v3/events')
36+
expect(url.searchParams.get('apiKey')).toBe('key')
37+
expect(url.searchParams.get('sport')).toBe('football')
38+
expect(url.searchParams.get('limit')).toBe('5')
39+
expect(url.searchParams.has('league')).toBe(false)
40+
expect(url.searchParams.has('to')).toBe(false)
41+
})
42+
})
43+
44+
describe('commaList', () => {
45+
it('normalises typed lists and arrays', () => {
46+
expect(commaList('Bet365, Unibet ,,')).toBe('Bet365,Unibet')
47+
expect(commaList(['Bet365', ' Unibet '])).toBe('Bet365,Unibet')
48+
expect(commaList('')).toBeUndefined()
49+
})
50+
})

‎apps/sim/tools/odds_api_io/utils.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ export function commaList(value: unknown): string | undefined {
6969

7070
export async function readOddsApiIoJson(response: Response, action: string): Promise<unknown> {
7171
const text = await response.text()
72-
let data: unknown = null
73-
if (text) {
74-
try {
75-
data = JSON.parse(text)
76-
} catch {
77-
data = null
78-
}
72+
let data: unknown
73+
let parseFailed = false
74+
try {
75+
data = text ? JSON.parse(text) : undefined
76+
} catch {
77+
parseFailed = true
7978
}
8079
if (!response.ok) {
8180
const message =
@@ -84,6 +83,9 @@ export async function readOddsApiIoJson(response: Response, action: string): Pro
8483
: `Odds-API.io ${action} failed with status ${response.status}`
8584
throw new Error(message)
8685
}
86+
if (parseFailed || data === undefined) {
87+
throw new Error(`Odds-API.io ${action} returned a response that is not valid JSON`)
88+
}
8789
return data
8890
}
8991

0 commit comments

Comments
 (0)