Skip to content

Commit 835f243

Browse files
committed
Refactor according to Vectorize V2 spec
1 parent b598d71 commit 835f243

File tree

3 files changed

+65
-68
lines changed

3 files changed

+65
-68
lines changed

worker-sys/src/types/vectorize.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,27 @@ use wasm_bindgen::prelude::*;
44
extern "C" {
55
#[wasm_bindgen(extends=js_sys::Object)]
66
#[derive(Debug, Clone, PartialEq, Eq)]
7-
pub type VectorizeIndex;
7+
pub type Vectorize;
88

99
#[wasm_bindgen(method, catch)]
10-
pub fn insert(
11-
this: &VectorizeIndex,
12-
vectors: js_sys::Object,
13-
) -> Result<js_sys::Promise, JsValue>;
10+
pub fn insert(this: &Vectorize, vectors: js_sys::Object) -> Result<js_sys::Promise, JsValue>;
1411

1512
#[wasm_bindgen(method, catch)]
16-
pub fn upsert(
17-
this: &VectorizeIndex,
18-
vectors: js_sys::Object,
19-
) -> Result<js_sys::Promise, JsValue>;
13+
pub fn upsert(this: &Vectorize, vectors: js_sys::Object) -> Result<js_sys::Promise, JsValue>;
2014

2115
#[wasm_bindgen(method, catch)]
22-
pub fn describe(this: &VectorizeIndex) -> Result<js_sys::Promise, JsValue>;
16+
pub fn describe(this: &Vectorize) -> Result<js_sys::Promise, JsValue>;
2317

2418
#[wasm_bindgen(method, catch)]
2519
pub fn query(
26-
this: &VectorizeIndex,
27-
vector: &[f32],
20+
this: &Vectorize,
21+
vector: JsValue,
2822
options: js_sys::Object,
2923
) -> Result<js_sys::Promise, JsValue>;
3024

3125
#[wasm_bindgen(method, catch, js_name = "getByIds")]
32-
pub fn get_by_ids(this: &VectorizeIndex, ids: JsValue) -> Result<js_sys::Promise, JsValue>;
26+
pub fn get_by_ids(this: &Vectorize, ids: JsValue) -> Result<js_sys::Promise, JsValue>;
3327

3428
#[wasm_bindgen(method, catch, js_name = "deleteByIds")]
35-
pub fn delete_by_ids(this: &VectorizeIndex, ids: JsValue) -> Result<js_sys::Promise, JsValue>;
29+
pub fn delete_by_ids(this: &Vectorize, ids: JsValue) -> Result<js_sys::Promise, JsValue>;
3630
}

worker/src/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt::Display;
44
use crate::d1::D1Database;
55
#[cfg(feature = "queue")]
66
use crate::Queue;
7-
use crate::VectorizeIndex;
7+
use crate::Vectorize;
88
use crate::{durable::ObjectNamespace, Bucket, DynamicDispatcher, Fetcher, Result};
99
use crate::{error::Error, hyperdrive::Hyperdrive};
1010

@@ -91,7 +91,7 @@ impl Env {
9191
self.get_binding(binding)
9292
}
9393

94-
pub fn vectorize(&self, binding: &str) -> Result<VectorizeIndex> {
94+
pub fn vectorize(&self, binding: &str) -> Result<Vectorize> {
9595
self.get_binding(binding)
9696
}
9797
}

worker/src/vectorize.rs

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ use crate::{send::SendFuture, EnvBinding, Result};
44
use serde::{Deserialize, Serialize};
55
use wasm_bindgen::{JsCast, JsValue};
66
use wasm_bindgen_futures::JsFuture;
7-
use worker_sys::types::VectorizeIndex as VectorizeIndexSys;
7+
use worker_sys::types::Vectorize as VectorizeSys;
88

9-
#[derive(Debug, Deserialize)]
10-
#[serde(rename_all = "kebab-case")]
119
/// Supported distance metrics for an index.
1210
/// Distance metrics determine how other "similar" vectors are determined.
11+
#[derive(Debug, Deserialize)]
12+
#[serde(rename_all = "kebab-case")]
1313
pub enum VectorizeDistanceMetric {
1414
Euclidean,
1515
Cosine,
1616
DotProduct,
1717
}
1818

19+
/// Information about the configuration of an index.
1920
#[derive(Debug, Deserialize)]
2021
#[serde(untagged)]
21-
/// Information about the configuration of an index.
2222
pub enum VectorizeIndexConfig {
2323
Preset {
2424
preset: String,
@@ -29,34 +29,32 @@ pub enum VectorizeIndexConfig {
2929
},
3030
}
3131

32+
/// Metadata about an existing index.
3233
#[derive(Debug, Deserialize)]
3334
#[serde(rename_all = "camelCase")]
34-
/// Metadata about an existing index.
35-
///
36-
/// This type is exclusively for the Vectorize **beta** and will be deprecated once Vectorize RC is released.
37-
pub struct VectorizeIndexDetails {
38-
pub id: String,
39-
pub name: String,
40-
pub description: Option<String>,
41-
pub config: VectorizeIndexConfig,
42-
pub vectors_count: u64,
35+
pub struct VectorizeIndexInfo {
36+
/// The number of records containing vectors within the index.
37+
pub vector_count: u64,
38+
/// Number of dimensions the index has been configured for.
39+
pub dimensions: u32,
40+
/// ISO 8601 datetime of the last processed mutation on in the index. All changes before this mutation will be reflected in the index state.
41+
#[serde(skip_serializing_if = "Option::is_none")]
42+
pub processed_up_to_datetime: Option<String>,
43+
/// UUIDv4 of the last mutation processed by the index. All changes before this mutation will be reflected in the index state.
44+
#[serde(skip_serializing_if = "Option::is_none")]
45+
pub processed_up_to_mutation: Option<String>,
4346
}
4447

48+
/// Results of an operation that performed a mutation on a set of vectors.
4549
#[derive(Debug, Deserialize)]
4650
#[serde(rename_all = "camelCase")]
47-
/// Results of an operation that performed a mutation on a set of vectors.
48-
/// Here, `ids` is a list of vectors that were successfully processed.
49-
///
50-
/// This type is exclusively for the Vectorize **beta** and will be deprecated once Vectorize RC is released.
51-
pub struct VectorizeVectorMutation {
52-
/// List of ids of vectors that were successfully processed.
53-
pub ids: Vec<String>,
54-
/// Total count of the number of processed vectors.
55-
pub count: u64,
51+
pub struct VectorizeVectorAsyncMutation {
52+
/// The unique identifier for the async mutation operation containing the changeset.
53+
pub mutation_id: String,
5654
}
5755

58-
#[derive(Debug, Serialize)]
5956
/// Represents a single vector value set along with its associated metadata.
57+
#[derive(Debug, Serialize)]
6058
pub struct VectorizeVector<'a> {
6159
/// The ID for the vector. This can be user-defined, and must be unique. It should uniquely identify the object, and is best set based on the ID of what the vector represents.
6260
id: String,
@@ -90,22 +88,23 @@ impl<'a> VectorizeVector<'a> {
9088
}
9189
}
9290

93-
#[derive(Debug, Serialize)]
94-
#[serde(rename_all = "kebab-case")]
9591
/// Metadata return levels for a Vectorize query.
92+
#[derive(Debug, Default, Serialize)]
93+
#[serde(rename_all = "kebab-case")]
9694
pub enum VectorizeMetadataRetrievalLevel {
9795
/// Full metadata for the vector return set, including all fields (including those un-indexed) without truncation. This is a more expensive retrieval, as it requires additional fetching & reading of un-indexed data.
9896
All,
9997
/// Return all metadata fields configured for indexing in the vector return set. This level of retrieval is "free" in that no additional overhead is incurred returning this data. However, note that indexed metadata is subject to truncation (especially for larger strings).
10098
Indexed,
10199
/// No indexed metadata will be returned.
100+
#[default]
102101
None,
103102
}
104103

105-
#[derive(Debug, Serialize, Hash, PartialEq, Eq)]
106104
/// Comparison logic/operation to use for metadata filtering.
107105
///
108106
/// This list is expected to grow as support for more operations are released.
107+
#[derive(Debug, Serialize, Hash, PartialEq, Eq)]
109108
pub enum VectorizeVectorMetadataFilterOp {
110109
#[serde(rename = "$eq")]
111110
Eq,
@@ -120,13 +119,14 @@ type VectorizeVectorMetadataFilter =
120119
#[derive(Debug, Serialize)]
121120
#[serde(rename_all = "camelCase")]
122121
pub struct VectorizeQueryOptions {
123-
// Default 3, max 20
122+
// Default 5, max 100
124123
top_k: u8,
124+
/// Return vectors from the specified namespace. Default `none`.
125125
namespace: Option<String>,
126126
/// Return vector values. Default `false`.
127127
return_values: bool,
128-
/// Return vector metadata. Default `false`.
129-
return_metadata: bool,
128+
/// Return vector metadata. Default `None`.
129+
return_metadata: VectorizeMetadataRetrievalLevel,
130130
/// Default `none`.
131131
filter: Option<VectorizeVectorMetadataFilter>,
132132
}
@@ -151,7 +151,10 @@ impl VectorizeQueryOptions {
151151
self
152152
}
153153

154-
pub fn with_return_metadata(mut self, return_metadata: bool) -> Self {
154+
pub fn with_return_metadata(
155+
mut self,
156+
return_metadata: VectorizeMetadataRetrievalLevel,
157+
) -> Self {
155158
self.return_metadata = return_metadata;
156159
self
157160
}
@@ -173,24 +176,26 @@ impl VectorizeQueryOptions {
173176
impl Default for VectorizeQueryOptions {
174177
fn default() -> Self {
175178
Self {
176-
top_k: 3,
179+
top_k: 5,
177180
namespace: None,
178181
return_values: false,
179-
return_metadata: false,
182+
return_metadata: VectorizeMetadataRetrievalLevel::None,
180183
filter: None,
181184
}
182185
}
183186
}
184187

185-
#[derive(Debug, Deserialize)]
186188
/// Represents a single vector value set along with its associated metadata.
189+
#[derive(Debug, Deserialize)]
187190
pub struct VectorizeVectorResult {
188191
/// The ID for the vector. This can be user-defined, and must be unique. It should uniquely identify the object, and is best set based on the ID of what the vector represents.
189192
pub id: String,
190193
/// The vector values.
191194
pub values: Option<Vec<f32>>,
192195
/// Metadata associated with the vector. Includes the values of other fields and potentially additional details.
193196
pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
197+
/** The namespace the vector belongs to. */
198+
pub namespace: Option<String>,
194199
}
195200

196201
#[derive(Debug, Deserialize)]
@@ -201,28 +206,26 @@ pub struct VectorizeMatchVector {
201206
pub score: Option<f64>,
202207
}
203208

209+
/// A set of matching [VectorizeMatchVector] for a particular query.
204210
#[derive(Debug, Deserialize)]
205-
/// A set of matching {@link VectorizeMatch} for a particular query.
206211
pub struct VectorizeMatches {
207212
pub matches: Vec<VectorizeMatchVector>,
208213
pub count: u64,
209214
}
210215

211216
/// A Vectorize Vector Search Index for querying vectors/embeddings.
212-
///
213-
/// This type is exclusively for the Vectorize **beta** and will be deprecated once Vectorize RC is released.
214-
pub struct VectorizeIndex(VectorizeIndexSys);
217+
pub struct Vectorize(VectorizeSys);
215218

216-
unsafe impl Send for VectorizeIndex {}
217-
unsafe impl Sync for VectorizeIndex {}
219+
unsafe impl Send for Vectorize {}
220+
unsafe impl Sync for Vectorize {}
218221

219-
impl EnvBinding for VectorizeIndex {
220-
const TYPE_NAME: &'static str = "VectorizeIndexImpl";
222+
impl EnvBinding for Vectorize {
223+
const TYPE_NAME: &'static str = "VectorizeImpl";
221224
}
222225

223-
impl VectorizeIndex {
226+
impl Vectorize {
224227
/// Get information about the currently bound index.
225-
pub async fn describe(&self) -> Result<VectorizeIndexDetails> {
228+
pub async fn describe(&self) -> Result<VectorizeIndexInfo> {
226229
let promise = self.0.describe()?;
227230
let fut = SendFuture::new(JsFuture::from(promise));
228231
let details = fut.await?;
@@ -233,7 +236,7 @@ impl VectorizeIndex {
233236
pub async fn insert<'a>(
234237
&self,
235238
vectors: &[VectorizeVector<'a>],
236-
) -> Result<VectorizeVectorMutation> {
239+
) -> Result<VectorizeVectorAsyncMutation> {
237240
let promise = self
238241
.0
239242
.insert(serde_wasm_bindgen::to_value(&vectors)?.into())?;
@@ -246,7 +249,7 @@ impl VectorizeIndex {
246249
pub async fn upsert<'a>(
247250
&self,
248251
vectors: &[VectorizeVector<'a>],
249-
) -> Result<VectorizeVectorMutation> {
252+
) -> Result<VectorizeVectorAsyncMutation> {
250253
let promise = self
251254
.0
252255
.upsert(serde_wasm_bindgen::to_value(&vectors)?.into())?;
@@ -258,7 +261,7 @@ impl VectorizeIndex {
258261
/// Use the provided vector to perform a similarity search across the index.
259262
pub async fn query(
260263
&self,
261-
vector: &[f32],
264+
vector: JsValue,
262265
options: VectorizeQueryOptions,
263266
) -> Result<VectorizeMatches> {
264267
let opts = serde_wasm_bindgen::to_value(&options)?;
@@ -269,7 +272,7 @@ impl VectorizeIndex {
269272
}
270273

271274
/// Delete a list of vectors with a matching id.
272-
pub async fn delete_by_ids<'a, T>(&self, ids: T) -> Result<VectorizeVectorMutation>
275+
pub async fn delete_by_ids<'a, T>(&self, ids: T) -> Result<VectorizeVectorAsyncMutation>
273276
where
274277
T: IntoIterator<Item = &'a str>,
275278
{
@@ -296,9 +299,9 @@ impl VectorizeIndex {
296299
}
297300
}
298301

299-
impl JsCast for VectorizeIndex {
302+
impl JsCast for Vectorize {
300303
fn instanceof(val: &JsValue) -> bool {
301-
val.is_instance_of::<VectorizeIndex>()
304+
val.is_instance_of::<Vectorize>()
302305
}
303306

304307
fn unchecked_from_js(val: JsValue) -> Self {
@@ -310,13 +313,13 @@ impl JsCast for VectorizeIndex {
310313
}
311314
}
312315

313-
impl From<VectorizeIndex> for JsValue {
314-
fn from(index: VectorizeIndex) -> Self {
316+
impl From<Vectorize> for JsValue {
317+
fn from(index: Vectorize) -> Self {
315318
JsValue::from(index.0)
316319
}
317320
}
318321

319-
impl AsRef<JsValue> for VectorizeIndex {
322+
impl AsRef<JsValue> for Vectorize {
320323
fn as_ref(&self) -> &JsValue {
321324
&self.0
322325
}

0 commit comments

Comments
 (0)