Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ts/packages/agents/player/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
addTracksToPlaylist,
getRecommendationsFromTrackCollection,
getRecentlyPlayed,
limitMax,
} from "./endpoints.js";
import { htmlStatus, printStatus } from "./playback.js";
import { SpotifyService } from "./service.js";
Expand Down Expand Up @@ -451,7 +452,7 @@ export async function searchTracks(
const query: SpotifyApi.SearchForItemParameterObject = {
q: queryString,
type: "track",
limit: 50,
limit: limitMax,
offset: 0,
};
const data = await search(query, context.service);
Expand All @@ -467,7 +468,7 @@ export async function searchForPlaylists(
const query: SpotifyApi.SearchForItemParameterObject = {
q: queryString,
type: "playlist",
limit: 20,
limit: limitMax,
offset: 0,
};
const data = await search(query, context.service);
Expand Down
22 changes: 16 additions & 6 deletions ts/packages/agents/player/src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { createFetchError } from "./utils.js";
const debugSpotifyRest = registerDebug("typeagent:spotify:rest");
const debugSpotifyRestVerbose = registerDebug("typeagent:spotify-verbose:rest");

const limitMax = 50;
/** Maximum number of items per Spotify API request */
export const limitMax = 50;

export async function search(
query: SpotifyApi.SearchForItemParameterObject,
Expand Down Expand Up @@ -483,7 +484,7 @@ export async function getQueue(service: SpotifyService) {
return fetchGet<SpotifyApi.UsersQueueResponse>(
service,
"https://api.spotify.com/v1/me/player/queue",
{ limit: 50 },
{ limit: limitMax },
);
}

Expand Down Expand Up @@ -707,8 +708,17 @@ export async function setVolume(
}

function getUrlWithParams(urlString: string, queryParams: Record<string, any>) {
const params = new URLSearchParams(queryParams);
const url = new URL(urlString);
url.search = params.toString();
return url.toString();
// Use encodeURIComponent for standard URL percent-encoding instead of
// URLSearchParams, which uses application/x-www-form-urlencoded semantics
// (encoding spaces as '+' rather than '%20'). Spotify's API expects spaces
// to be percent-encoded as '%20'.
const parts: string[] = [];
for (const [key, value] of Object.entries(queryParams)) {
if (value !== undefined && value !== null) {
parts.push(
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`,
);
}
}
return parts.length > 0 ? `${urlString}?${parts.join("&")}` : urlString;
}
15 changes: 9 additions & 6 deletions ts/packages/agents/player/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const debugReuse = registerDebug("typeagent:spotify:search:reuse");
const debugVerbose = registerDebug("typeagent:spotify-verbose:search");
const debugError = registerDebug("typeagent:spotify:search:error");

/** Number of results to fetch for targeted searches */
const searchResultLimit = 10;

export type SpotifyQuery = {
track?: string[] | undefined;
album?: string[] | undefined;
Expand Down Expand Up @@ -56,7 +59,7 @@ export async function searchArtists(
const query: SpotifyApi.SearchForItemParameterObject = {
q: `artist:${quoteString(searchTerm)}`,
type: "artist",
limit: 50,
limit: searchResultLimit,
offset: 0,
};
return search(query, context.service);
Expand All @@ -77,7 +80,7 @@ export async function searchAlbums(
const searchQuery: SpotifyApi.SearchForItemParameterObject = {
q: queryString,
type: "album",
limit: 50,
limit: searchResultLimit,
offset: 0,
};
const result = await search(searchQuery, context.service);
Expand Down Expand Up @@ -386,7 +389,7 @@ export async function findArtistTracksWithGenre(
const param: SpotifyApi.SearchForItemParameterObject = {
q: queryString,
type: "track",
limit: 50,
limit: searchResultLimit,
offset: 0,
};
const result = await search(param, context.service);
Expand Down Expand Up @@ -489,7 +492,7 @@ async function expandMovementTracks(
const param: SpotifyApi.SearchForItemParameterObject = {
q: toQueryString({ ...originalQuery, album: [album] }),
type: "track",
limit: 50,
limit: searchResultLimit,
offset: 0,
};
const result = await search(param, context.service);
Expand Down Expand Up @@ -564,7 +567,7 @@ export async function findTracksWithGenre(
const param: SpotifyApi.SearchForItemParameterObject = {
q: queryString,
type: "track",
limit: 50,
limit: searchResultLimit,
offset: 0,
};
const result = await search(param, context.service);
Expand Down Expand Up @@ -647,7 +650,7 @@ export async function findTracks(
const param: SpotifyApi.SearchForItemParameterObject = {
q: queryString,
type: "track",
limit: 50,
limit: searchResultLimit,
offset: 0,
};
const result = await search(param, context.service);
Expand Down
Loading