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
1 change: 1 addition & 0 deletions adapters/cf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"payloadcms-vectorize": ">=1.0.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240000.0",
"payloadcms-vectorize": "workspace:*"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions adapters/cf/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CollectionSlug } from 'payload'
import type { DbAdapter } from 'payloadcms-vectorize'
import { getVectorizeBinding } from './types.js'
import type { CloudflareVectorizeBinding, KnowledgePoolsConfig } from './types.js'
import type { CloudflareVectorizeBinding, KnowledgePoolsConfig, VectorizeBinding } from './types.js'
import cfMappingsCollection, { CF_MAPPINGS_SLUG } from './collections/cfMappings.js'
import embed from './embed.js'
import search from './search.js'
Expand All @@ -13,7 +13,7 @@ interface CloudflareVectorizeConfig {
/** Knowledge pools configuration with their dimensions */
config: KnowledgePoolsConfig
/** Cloudflare Vectorize binding for vector storage */
binding: CloudflareVectorizeBinding
binding: VectorizeBinding
}

/**
Expand Down Expand Up @@ -134,5 +134,5 @@ export const createCloudflareVectorizeIntegration = (
}

export { CF_MAPPINGS_SLUG } from './collections/cfMappings.js'
export type { CloudflareVectorizeBinding, KnowledgePoolsConfig }
export type { CloudflareVectorizeBinding, KnowledgePoolsConfig, VectorizeBinding }
export type { KnowledgePoolsConfig as KnowledgePoolConfig }
48 changes: 10 additions & 38 deletions adapters/cf/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/// <reference types="@cloudflare/workers-types" />
import type { BasePayload } from 'payload'
import { getVectorizedPayload } from 'payloadcms-vectorize'

/**
* The subset of the Cloudflare `Vectorize` binding used by this adapter.
*/
export type VectorizeBinding = Pick<Vectorize, 'query' | 'upsert' | 'deleteByIds' | 'getByIds'>

/**
* Retrieve the Cloudflare Vectorize binding from a Payload instance.
* Throws if the binding is not found.
*/
export function getVectorizeBinding(payload: BasePayload): CloudflareVectorizeBinding {
export function getVectorizeBinding(payload: BasePayload): VectorizeBinding {
const binding = getVectorizedPayload(payload)?.getDbAdapterCustom()
?._vectorizeBinding as CloudflareVectorizeBinding | undefined
?._vectorizeBinding as VectorizeBinding | undefined
if (!binding) {
throw new Error('[@payloadcms-vectorize/cf] Cloudflare Vectorize binding not found')
}
Expand All @@ -27,39 +33,5 @@ export interface CloudflareVectorizePoolConfig {
*/
export type KnowledgePoolsConfig = Record<string, CloudflareVectorizePoolConfig>

/** A single vector match returned by a Vectorize query */
export interface VectorizeMatch {
id: string
score?: number
metadata?: Record<string, unknown>
}

/** Result of a Vectorize query */
export interface VectorizeQueryResult {
matches: VectorizeMatch[]
count: number
}

/** Vector to upsert into Vectorize */
export interface VectorizeVector {
id: string
values: number[]
metadata?: Record<string, unknown>
}

/**
* Cloudflare Vectorize binding interface.
* Mirrors the subset of the Vectorize API we use.
* For the full type, install `@cloudflare/workers-types`.
*/
export interface CloudflareVectorizeBinding {
query(vector: number[], options?: {
topK?: number
returnMetadata?: boolean | 'indexed' | 'all'
filter?: Record<string, unknown>
/** Vectorize metadata filtering */
where?: Record<string, unknown>
}): Promise<VectorizeQueryResult>
upsert(vectors: VectorizeVector[]): Promise<unknown>
deleteByIds(ids: string[]): Promise<unknown>
}
/** @deprecated Use {@link VectorizeBinding}. */
export type CloudflareVectorizeBinding = VectorizeBinding
40 changes: 25 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
}
]
},
"include": ["./src/**/*.ts", "./src/**/*.tsx", "./adapters/*/src/**/*.ts", "./dev/next-env.d.ts"]
"include": ["./src/**/*.ts", "./src/**/*.tsx", "./dev/next-env.d.ts"]
}
Loading