Skip to content

Commit 0ab96f0

Browse files
committed
fix(integrations): honor cancellation in later spotify library batches, report the exact rejected id range, reject unknown follow types
1 parent bc9cd0b commit 0ab96f0

20 files changed

Lines changed: 253 additions & 81 deletions

‎apps/sim/tools/spotify/check_following.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ interface SpotifyCheckFollowingResponse extends ToolResponse {
1111
output: { results: boolean[] }
1212
}
1313

14+
/** The library URI type for `type`, rejecting anything other than `artist` or `user`. */
15+
function followType(type: string): 'artist' | 'user' {
16+
const normalized = String(type ?? '')
17+
.trim()
18+
.toLowerCase()
19+
if (normalized === 'artist' || normalized === 'user') return normalized
20+
throw new Error('Type must be "artist" or "user"')
21+
}
22+
1423
export const spotifyCheckFollowingTool: ToolConfig<
1524
SpotifyCheckFollowingParams,
1625
SpotifyCheckFollowingResponse
@@ -43,7 +52,7 @@ export const spotifyCheckFollowingTool: ToolConfig<
4352

4453
request: {
4554
url: (params) =>
46-
spotifyLibraryUrl(params.ids, params.type === 'user' ? 'user' : 'artist', {
55+
spotifyLibraryUrl(params.ids, followType(params.type), {
4756
contains: true,
4857
maxIds: 50,
4958
}),
@@ -53,16 +62,17 @@ export const spotifyCheckFollowingTool: ToolConfig<
5362
}),
5463
},
5564

56-
transformResponse: async (response, params): Promise<SpotifyCheckFollowingResponse> => {
65+
transformResponse: async (response, params, context): Promise<SpotifyCheckFollowingResponse> => {
5766
const results: boolean[] = [
5867
...(await response.json()),
5968
...(params
6069
? await sendRemainingSpotifyLibraryChunks(
6170
params,
6271
params.ids,
63-
params.type === 'user' ? 'user' : 'artist',
72+
followType(params.type),
6473
'GET',
65-
{ contains: true, maxIds: 50 }
74+
{ contains: true, maxIds: 50 },
75+
context?.signal
6676
)
6777
: []),
6878
]

‎apps/sim/tools/spotify/check_saved_albums.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,25 @@ export const spotifyCheckSavedAlbumsTool: ToolConfig<
4242
}),
4343
},
4444

45-
transformResponse: async (response, params): Promise<SpotifyCheckSavedAlbumsResponse> => {
45+
transformResponse: async (
46+
response,
47+
params,
48+
context
49+
): Promise<SpotifyCheckSavedAlbumsResponse> => {
4650
const results: boolean[] = [
4751
...(await response.json()),
4852
...(params
49-
? await sendRemainingSpotifyLibraryChunks(params, params.albumIds, 'album', 'GET', {
50-
contains: true,
51-
maxIds: 20,
52-
})
53+
? await sendRemainingSpotifyLibraryChunks(
54+
params,
55+
params.albumIds,
56+
'album',
57+
'GET',
58+
{
59+
contains: true,
60+
maxIds: 20,
61+
},
62+
context?.signal
63+
)
5364
: []),
5465
]
5566
return { success: true, output: { results } }

‎apps/sim/tools/spotify/check_saved_audiobooks.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,25 @@ export const spotifyCheckSavedAudiobooksTool: ToolConfig<
4343
}),
4444
},
4545

46-
transformResponse: async (response, params): Promise<SpotifyCheckSavedAudiobooksResponse> => {
46+
transformResponse: async (
47+
response,
48+
params,
49+
context
50+
): Promise<SpotifyCheckSavedAudiobooksResponse> => {
4751
const results: boolean[] = [
4852
...(await response.json()),
4953
...(params
50-
? await sendRemainingSpotifyLibraryChunks(params, params.audiobookIds, 'audiobook', 'GET', {
51-
contains: true,
52-
maxIds: 50,
53-
})
54+
? await sendRemainingSpotifyLibraryChunks(
55+
params,
56+
params.audiobookIds,
57+
'audiobook',
58+
'GET',
59+
{
60+
contains: true,
61+
maxIds: 50,
62+
},
63+
context?.signal
64+
)
5465
: []),
5566
]
5667
return { success: true, output: { results } }

‎apps/sim/tools/spotify/check_saved_episodes.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,25 @@ export const spotifyCheckSavedEpisodesTool: ToolConfig<
4343
}),
4444
},
4545

46-
transformResponse: async (response, params): Promise<SpotifyCheckSavedEpisodesResponse> => {
46+
transformResponse: async (
47+
response,
48+
params,
49+
context
50+
): Promise<SpotifyCheckSavedEpisodesResponse> => {
4751
const results: boolean[] = [
4852
...(await response.json()),
4953
...(params
50-
? await sendRemainingSpotifyLibraryChunks(params, params.episodeIds, 'episode', 'GET', {
51-
contains: true,
52-
maxIds: 50,
53-
})
54+
? await sendRemainingSpotifyLibraryChunks(
55+
params,
56+
params.episodeIds,
57+
'episode',
58+
'GET',
59+
{
60+
contains: true,
61+
maxIds: 50,
62+
},
63+
context?.signal
64+
)
5465
: []),
5566
]
5667
return { success: true, output: { results } }

‎apps/sim/tools/spotify/check_saved_shows.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,21 @@ export const spotifyCheckSavedShowsTool: ToolConfig<
4242
}),
4343
},
4444

45-
transformResponse: async (response, params): Promise<SpotifyCheckSavedShowsResponse> => {
45+
transformResponse: async (response, params, context): Promise<SpotifyCheckSavedShowsResponse> => {
4646
const results: boolean[] = [
4747
...(await response.json()),
4848
...(params
49-
? await sendRemainingSpotifyLibraryChunks(params, params.showIds, 'show', 'GET', {
50-
contains: true,
51-
maxIds: 50,
52-
})
49+
? await sendRemainingSpotifyLibraryChunks(
50+
params,
51+
params.showIds,
52+
'show',
53+
'GET',
54+
{
55+
contains: true,
56+
maxIds: 50,
57+
},
58+
context?.signal
59+
)
5360
: []),
5461
]
5562
return { success: true, output: { results } }

‎apps/sim/tools/spotify/check_saved_tracks.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,25 @@ export const spotifyCheckSavedTracksTool: ToolConfig<
4141
}),
4242
},
4343

44-
transformResponse: async (response, params): Promise<SpotifyCheckSavedTracksResponse> => {
44+
transformResponse: async (
45+
response,
46+
params,
47+
context
48+
): Promise<SpotifyCheckSavedTracksResponse> => {
4549
const data: boolean[] = [
4650
...(await response.json()),
4751
...(params
48-
? await sendRemainingSpotifyLibraryChunks(params, params.trackIds, 'track', 'GET', {
49-
contains: true,
50-
maxIds: 50,
51-
})
52+
? await sendRemainingSpotifyLibraryChunks(
53+
params,
54+
params.trackIds,
55+
'track',
56+
'GET',
57+
{
58+
contains: true,
59+
maxIds: 50,
60+
},
61+
context?.signal
62+
)
5263
: []),
5364
]
5465
const ids = splitSpotifyLibraryIds(params?.trackIds || '', 50)

‎apps/sim/tools/spotify/follow_artists.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ export const spotifyFollowArtistsTool: ToolConfig<
4444
}),
4545
},
4646

47-
transformResponse: async (_response, params): Promise<SpotifyFollowArtistsResponse> => {
47+
transformResponse: async (_response, params, context): Promise<SpotifyFollowArtistsResponse> => {
4848
if (params)
49-
await sendRemainingSpotifyLibraryChunks(params, params.artistIds, 'artist', 'PUT', {
50-
maxIds: 50,
51-
})
49+
await sendRemainingSpotifyLibraryChunks(
50+
params,
51+
params.artistIds,
52+
'artist',
53+
'PUT',
54+
{
55+
maxIds: 50,
56+
},
57+
context?.signal
58+
)
5259
return {
5360
success: true,
5461
output: { success: true },

‎apps/sim/tools/spotify/library.test.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
22
import { spotifyCheckSavedTracksTool } from '@/tools/spotify/check_saved_tracks'
33
import { spotifyRemoveSavedTracksTool } from '@/tools/spotify/remove_saved_tracks'
44

@@ -17,10 +17,6 @@ describe('Spotify library tools with more IDs than one /me/library request takes
1717
vi.stubGlobal('fetch', mockFetch)
1818
})
1919

20-
afterEach(() => {
21-
vi.unstubAllGlobals()
22-
})
23-
2420
it('checks all 45 tracks across two /me/library/contains requests, in order', async () => {
2521
const url = spotifyCheckSavedTracksTool.request.url
2622
if (typeof url !== 'function') throw new Error('Missing check URL builder')

‎apps/sim/tools/spotify/remove_saved_albums.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,22 @@ export const spotifyRemoveSavedAlbumsTool: ToolConfig<
4242
}),
4343
},
4444

45-
transformResponse: async (_response, params): Promise<SpotifyRemoveSavedAlbumsResponse> => {
45+
transformResponse: async (
46+
_response,
47+
params,
48+
context
49+
): Promise<SpotifyRemoveSavedAlbumsResponse> => {
4650
if (params)
47-
await sendRemainingSpotifyLibraryChunks(params, params.albumIds, 'album', 'DELETE', {
48-
maxIds: 20,
49-
})
51+
await sendRemainingSpotifyLibraryChunks(
52+
params,
53+
params.albumIds,
54+
'album',
55+
'DELETE',
56+
{
57+
maxIds: 20,
58+
},
59+
context?.signal
60+
)
5061
return { success: true, output: { success: true } }
5162
},
5263

‎apps/sim/tools/spotify/remove_saved_audiobooks.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,22 @@ export const spotifyRemoveSavedAudiobooksTool: ToolConfig<
4242
}),
4343
},
4444

45-
transformResponse: async (_response, params): Promise<SpotifyRemoveSavedAudiobooksResponse> => {
45+
transformResponse: async (
46+
_response,
47+
params,
48+
context
49+
): Promise<SpotifyRemoveSavedAudiobooksResponse> => {
4650
if (params)
47-
await sendRemainingSpotifyLibraryChunks(params, params.audiobookIds, 'audiobook', 'DELETE', {
48-
maxIds: 50,
49-
})
51+
await sendRemainingSpotifyLibraryChunks(
52+
params,
53+
params.audiobookIds,
54+
'audiobook',
55+
'DELETE',
56+
{
57+
maxIds: 50,
58+
},
59+
context?.signal
60+
)
5061
return { success: true, output: { success: true } }
5162
},
5263

0 commit comments

Comments
 (0)