Skip to content

Commit

Permalink
fix: Fix Vectorize JSON payloads (#6548)
Browse files Browse the repository at this point in the history
* VS-269: Add content-type header to Vectorize POST operations

* VS-271: Fix Vectorize getVectors, deleteVectors payload in Wrangler Client
  • Loading branch information
garvit-gupta committed Aug 23, 2024
1 parent 669ec1c commit 439e63a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-deers-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: Fix Vectorize getVectors, deleteVectors payload in Wrangler Client; VS-271
5 changes: 5 additions & 0 deletions .changeset/rare-cooks-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: Add content-type header to Vectorize POST operations; #6516/VS-269
21 changes: 18 additions & 3 deletions packages/wrangler/src/vectorize/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
VectorizeMetadataIndexPropertyName,
VectorizeQueryOptions,
VectorizeVector,
VectorizeVectorIds,
VectorizeVectorMutation,
} from "./types";
import type { FormData } from "undici";
Expand Down Expand Up @@ -141,7 +142,9 @@ export async function queryIndex(
`/accounts/${accountId}/vectorize/v2/indexes/${indexName}/query`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
headers: {
"content-type": jsonContentType,
},
body: JSON.stringify({
...options,
vector: Array.isArray(vector) ? vector : Array.from(vector),
Expand All @@ -153,14 +156,17 @@ export async function queryIndex(
export async function getByIds(
config: Config,
indexName: string,
ids: Array<string>
ids: VectorizeVectorIds
): Promise<VectorizeVector[]> {
const accountId = await requireAuth(config);

return await fetchResult(
`/accounts/${accountId}/vectorize/v2/indexes/${indexName}/get_by_ids`,
{
method: "POST",
headers: {
"content-type": jsonContentType,
},
body: JSON.stringify(ids),
}
);
Expand All @@ -169,14 +175,17 @@ export async function getByIds(
export async function deleteByIds(
config: Config,
indexName: string,
ids: Array<string>
ids: VectorizeVectorIds
): Promise<VectorizeAsyncMutation> {
const accountId = await requireAuth(config);

return await fetchResult(
`/accounts/${accountId}/vectorize/v2/indexes/${indexName}/delete_by_ids`,
{
method: "POST",
headers: {
"content-type": jsonContentType,
},
body: JSON.stringify(ids),
}
);
Expand Down Expand Up @@ -207,6 +216,9 @@ export async function createMetadataIndex(
`/accounts/${accountId}/vectorize/v2/indexes/${indexName}/metadata_index/create`,
{
method: "POST",
headers: {
"content-type": jsonContentType,
},
body: JSON.stringify(payload),
}
);
Expand Down Expand Up @@ -237,6 +249,9 @@ export async function deleteMetadataIndex(
`/accounts/${accountId}/vectorize/v2/indexes/${indexName}/metadata_index/delete`,
{
method: "POST",
headers: {
"content-type": jsonContentType,
},
body: JSON.stringify(payload),
}
);
Expand Down
8 changes: 7 additions & 1 deletion packages/wrangler/src/vectorize/deleteByIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";
import type { VectorizeVectorIds } from "./types";

export function options(yargs: CommonYargsArgv) {
return yargs
Expand Down Expand Up @@ -37,7 +38,12 @@ export async function handler(
}

logger.log(`📋 Deleting vectors...`);
const mutation = await deleteByIds(config, args.name, args.ids);

const ids: VectorizeVectorIds = {
ids: args.ids,
};

const mutation = await deleteByIds(config, args.name, ids);

logger.log(
`✅ Successfully enqueued ${args.ids.length} vectors into index '${args.name}' for deletion. Mutation changeset identifier: ${mutation.mutationId}.`
Expand Down
8 changes: 7 additions & 1 deletion packages/wrangler/src/vectorize/getByIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";
import type { VectorizeVectorIds } from "./types";

export function options(yargs: CommonYargsArgv) {
return yargs
Expand Down Expand Up @@ -37,7 +38,12 @@ export async function handler(
}

logger.log(`📋 Fetching vectors...`);
const vectors = await getByIds(config, args.name, args.ids);

const ids: VectorizeVectorIds = {
ids: args.ids,
};

const vectors = await getByIds(config, args.name, ids);

if (vectors.length === 0) {
logger.warn(
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/vectorize/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ export interface VectorizeVectorMutation {
count: number;
}

/**
* Request type used to pass vector ids to fetch or delete.
*/
export interface VectorizeVectorIds {
/* List of vector ids that are fetched or deleted. */
ids: string[];
}

/**
* Result type indicating a mutation on the Vectorize Index.
* Actual mutations are processed async where the `mutationId` is the unique identifier for the operation.
Expand Down

0 comments on commit 439e63a

Please sign in to comment.