Skip to content
Closed
Show file tree
Hide file tree
Changes from 15 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
9 changes: 9 additions & 0 deletions .changeset/rotten-scissors-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@osdk/foundry-sdk-generator": patch
"@osdk/generator-converters": patch
"@osdk/shared.test": patch
"@osdk/client": patch
"@osdk/api": patch
---

Add nearest neighbors search support
38 changes: 27 additions & 11 deletions etc/api.report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ export interface AsyncIterArgs<
R extends boolean = false,
A extends Augments = never,
S extends NullabilityAdherence = NullabilityAdherence.Default,
T extends boolean = false
> extends SelectArg<Q, K, R, S>, OrderByArg<Q, PropertyKeys<Q>> {
T extends boolean = false,
Z extends OrderByOptions<Q, K> = {}
> extends SelectArg<Q, K, R, S>, OrderByArg<Q, Z, PropertyKeys<Q>> {
// (undocumented)
$__UNSTABLE_useOldInterfaceApis?: boolean;
// (undocumented)
Expand Down Expand Up @@ -273,7 +274,7 @@ export interface BaseObjectSet<Q extends ObjectOrInterfaceDefinition> {
}

// @public (undocumented)
export type BaseWirePropertyTypes = "string" | "datetime" | "double" | "boolean" | "integer" | "timestamp" | "short" | "long" | "float" | "decimal" | "byte" | "marking" | "mediaReference" | "numericTimeseries" | "stringTimeseries" | "sensorTimeseries" | "attachment" | "geopoint" | "geoshape" | "geotimeSeriesReference";
export type BaseWirePropertyTypes = "string" | "datetime" | "double" | "boolean" | "integer" | "timestamp" | "short" | "long" | "float" | "decimal" | "byte" | "marking" | "mediaReference" | "numericTimeseries" | "stringTimeseries" | "sensorTimeseries" | "attachment" | "geopoint" | "geoshape" | "geotimeSeriesReference" | "vector";

// @public (undocumented)
export type CompileTimeMetadata<T extends {
Expand Down Expand Up @@ -504,24 +505,27 @@ export interface FetchPageArgs<
R extends boolean = false,
A extends Augments = never,
S extends NullabilityAdherence = NullabilityAdherence.Default,
T extends boolean = false
> extends AsyncIterArgs<Q, K, R, A, S, T> {
T extends boolean = false,
Z extends OrderByOptions<Q, K> = {}
> extends AsyncIterArgs<Q, K, R, A, S, T, Z> {
// (undocumented)
$nextPageToken?: string;
// (undocumented)
$pageSize?: number;
}

// Warning: (ae-forgotten-export) The symbol "ExtractOptions" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ExtractOrderByOptions" needs to be exported by the entry point index.d.ts
//
// @public
export type FetchPageResult<
Q extends ObjectOrInterfaceDefinition,
L extends PropertyKeys<Q>,
R extends boolean,
S extends NullabilityAdherence,
T extends boolean
> = PageResult<PropertyKeys<Q> extends L ? Osdk.Instance<Q, ExtractOptions<R, S, T>> : Osdk.Instance<Q, ExtractOptions<R, S, T>, L>>;
T extends boolean,
Z extends OrderByOptions<Q, L> = {}
> = PageResult<PropertyKeys<Q> extends L ? Osdk.Instance<Q, ExtractOptions<R, S, T>, PropertyKeys<Q>, {}, ExtractOrderByOptions<Z extends "relevance" ? true : false>> : Osdk.Instance<Q, ExtractOptions<R, S, T>, L, {}, ExtractOrderByOptions<Z extends "relevance" ? true : false>>>;

// @public (undocumented)
export type FilteredPropertyKeys<
Expand Down Expand Up @@ -837,6 +841,12 @@ export interface OntologyMetadata<_NEVER_USED_KEPT_FOR_BACKCOMPAT = any> {
userAgent: string;
}

// @public (undocumented)
export type OrderByOptions<
Q extends ObjectOrInterfaceDefinition,
L extends PropertyKeys<Q> = PropertyKeys<Q>
> = { [K in L]? : "asc" | "desc" } | "relevance";

// Warning: (ae-forgotten-export) The symbol "IsNever" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "IsAny" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ExtractPropsKeysFromOldPropsStyle" needs to be exported by the entry point index.d.ts
Expand All @@ -857,14 +867,17 @@ export namespace Osdk {
Q extends ObjectOrInterfaceDefinition,
OPTIONS extends never | "$rid" | "$allBaseProperties" = never,
P extends PropertyKeys<Q> = PropertyKeys<Q>,
R extends Record<string, SimplePropertyDef> = {}
R extends Record<string, SimplePropertyDef> = {},
ORDER_BY_OPTIONS extends never | "$score" = never
> = OsdkBase<Q> & Pick<CompileTimeMetadata<Q>["props"], GetPropsKeys<Q, P, [R] extends [{}] ? false : true>> & ([R] extends [never] ? {} : { [A in keyof R] : SimplePropertyDef.ToRuntimeProperty<R[A]> }) & {
readonly $link: Q extends {
linksType?: any
} ? Q["linksType"] : Q extends ObjectTypeDefinition ? OsdkObjectLinksObject<Q> : never
readonly $as: <NEW_Q extends ValidToFrom<Q>>(type: NEW_Q | string) => Osdk.Instance<NEW_Q, OPTIONS, ConvertProps<Q, NEW_Q, P, OPTIONS>>
readonly $clone: <NEW_PROPS extends PropertyKeys<Q>>(updatedObject?: Osdk.Instance<Q, any, NEW_PROPS> | { [K in NEW_PROPS]? : CompileTimeMetadata<Q>["props"][K] }) => Osdk.Instance<Q, OPTIONS, P | NEW_PROPS>
} & (IsNever<OPTIONS> extends true ? {} : IsAny<OPTIONS> extends true ? {} : "$rid" extends OPTIONS ? {
} & (IsNever<ORDER_BY_OPTIONS> extends true ? {} : IsAny<ORDER_BY_OPTIONS> extends true ? {} : "$score" extends ORDER_BY_OPTIONS ? {
readonly $score: number
} : {}) & (IsNever<OPTIONS> extends true ? {} : IsAny<OPTIONS> extends true ? {} : "$rid" extends OPTIONS ? {
readonly $rid: string
} : {});
}
Expand Down Expand Up @@ -989,6 +1002,8 @@ export interface PropertyValueWireToClient {
stringTimeseries: TimeSeriesProperty<string>;
// (undocumented)
timestamp: string;
// (undocumented)
vector: number[];
}

// Warning: (ae-forgotten-export) The symbol "PrimitiveDataType" needs to be exported by the entry point index.d.ts
Expand Down Expand Up @@ -1147,8 +1162,9 @@ export type SingleOsdkResult<
R extends boolean,
S extends NullabilityAdherence,
RDPs extends Record<string, SimplePropertyDef> = {},
T extends boolean = false
> = Osdk.Instance<Q, ExtractOptions<R, S, T>, PropertyKeys<Q> extends L ? PropertyKeys<Q> : PropertyKeys<Q> & L, { [K in Extract<keyof RDPs, L>] : RDPs[K] }>;
T extends boolean = false,
Z extends OrderByOptions<Q, L> = {}
> = Osdk.Instance<Q, ExtractOptions<R, S, T>, PropertyKeys<Q> extends L ? PropertyKeys<Q> : PropertyKeys<Q> & L, { [K in Extract<keyof RDPs, L>] : RDPs[K] }, ExtractOrderByOptions<Z extends "relevance" ? true : false>>;

// Warning: (ae-forgotten-export) The symbol "AllowedBucketKeyTypes_2" needs to be exported by the entry point index.d.ts
//
Expand Down
10 changes: 10 additions & 0 deletions packages/api/src/OsdkObjectFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export namespace Osdk {
OPTIONS extends never | "$rid" | "$allBaseProperties" = never,
P extends PropertyKeys<Q> = PropertyKeys<Q>,
R extends Record<string, SimplePropertyDef> = {},
ORDER_BY_OPTIONS extends never | "$score" = never,
> =
& OsdkBase<Q>
& Pick<
Expand Down Expand Up @@ -228,6 +229,10 @@ export namespace Osdk {
},
) => Osdk.Instance<Q, OPTIONS, P | NEW_PROPS>;
}
& (IsNever<ORDER_BY_OPTIONS> extends true ? {}
: IsAny<ORDER_BY_OPTIONS> extends true ? {}
: "$score" extends ORDER_BY_OPTIONS ? { readonly $score: number }
: {})
// We are hiding the $rid field if it wasn't requested as we want to discourage its use
& (IsNever<OPTIONS> extends true ? {}
: IsAny<OPTIONS> extends true ? {}
Expand Down Expand Up @@ -269,3 +274,8 @@ export type ExtractOptions<
S extends NullabilityAdherence = NullabilityAdherence.Default,
T extends boolean = false,
> = ExtractRidOption<R> | ExtractAllPropertiesOption<T>;

export type ExtractOrderByOptions<R extends boolean> = IsNever<R> extends true
? never
: DefaultToFalse<R> extends false ? never
: "$score";
1 change: 1 addition & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type {
Augments,
FetchPageArgs,
NullabilityAdherence,
OrderByOptions,
SelectArg,
SelectArgToKeys,
} from "./object/FetchPageArgs.js";
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/mapping/PropertyValueMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface PropertyValueWireToClient {
stringTimeseries: TimeSeriesProperty<string>;
sensorTimeseries: TimeSeriesProperty<string | number>;
geotimeSeriesReference: GeotimeSeriesProperty<GeoJSON.Point>;
vector: number[];
}

export type GetClientPropertyValueFromWire<
Expand Down Expand Up @@ -80,6 +81,7 @@ export interface PropertyValueClientToWire {
stringTimeseries: TimeSeriesProperty<string>;
sensorTimeseries: TimeSeriesProperty<string | number>;
geotimeSeriesReference: GeotimeSeriesProperty<GeoJSON.Point>;
vector: number[];
}
export type GetWirePropertyValueFromClient<
T extends
Expand Down
20 changes: 15 additions & 5 deletions packages/api/src/object/FetchPageArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ export interface SelectArg<
$includeRid?: R;
}

export type OrderByOptions<
Q extends ObjectOrInterfaceDefinition,
L extends PropertyKeys<Q> = PropertyKeys<Q>,
> =
| {
[K in L]?: "asc" | "desc";
}
| "relevance";

export interface OrderByArg<
Q extends ObjectOrInterfaceDefinition,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting for myself that this isn't exported, so this arg reorder is fine

Z extends OrderByOptions<Q, L>,
L extends PropertyKeys<Q> = PropertyKeys<Q>,
> {
$orderBy?: {
[K in L]?: "asc" | "desc";
};
$orderBy?: Z;
}

export type SelectArgToKeys<
Expand All @@ -58,7 +66,8 @@ export interface FetchPageArgs<
A extends Augments = never,
S extends NullabilityAdherence = NullabilityAdherence.Default,
T extends boolean = false,
> extends AsyncIterArgs<Q, K, R, A, S, T> {
Z extends OrderByOptions<Q, K> = {},
> extends AsyncIterArgs<Q, K, R, A, S, T, Z> {
$nextPageToken?: string;
$pageSize?: number;
}
Expand All @@ -70,7 +79,8 @@ export interface AsyncIterArgs<
A extends Augments = never,
S extends NullabilityAdherence = NullabilityAdherence.Default,
T extends boolean = false,
> extends SelectArg<Q, K, R, S>, OrderByArg<Q, PropertyKeys<Q>> {
Z extends OrderByOptions<Q, K> = {},
> extends SelectArg<Q, K, R, S>, OrderByArg<Q, Z, PropertyKeys<Q>> {
$__UNSTABLE_useOldInterfaceApis?: boolean;
$includeAllBaseObjectProperties?: T;
}
Expand Down
30 changes: 25 additions & 5 deletions packages/api/src/object/FetchPageResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ import type {
PropertyKeys,
} from "../ontology/ObjectOrInterface.js";
import type { SimplePropertyDef } from "../ontology/SimplePropertyDef.js";
import type { ExtractOptions, IsNever, Osdk } from "../OsdkObjectFrom.js";
import type {
ExtractOptions,
ExtractOrderByOptions,
IsNever,
Osdk,
} from "../OsdkObjectFrom.js";
import type { PageResult } from "../PageResult.js";
import type { NullabilityAdherence } from "./FetchPageArgs.js";
import type { NullabilityAdherence, OrderByOptions } from "./FetchPageArgs.js";

/** exposed for a test */
export type RespectNullability<S extends NullabilityAdherence> = S extends false
Expand Down Expand Up @@ -52,9 +57,22 @@ export type FetchPageResult<
R extends boolean,
S extends NullabilityAdherence,
T extends boolean,
Z extends OrderByOptions<Q, L> = {},
> = PageResult<
PropertyKeys<Q> extends L ? Osdk.Instance<Q, ExtractOptions<R, S, T>>
: Osdk.Instance<Q, ExtractOptions<R, S, T>, L>
PropertyKeys<Q> extends L ? Osdk.Instance<
Q,
ExtractOptions<R, S, T>,
PropertyKeys<Q>,
{},
ExtractOrderByOptions<Z extends "relevance" ? true : false>
>
: Osdk.Instance<
Q,
ExtractOptions<R, S, T>,
L,
{},
ExtractOrderByOptions<Z extends "relevance" ? true : false>
>
>;

/**
Expand All @@ -67,11 +85,13 @@ export type SingleOsdkResult<
S extends NullabilityAdherence,
RDPs extends Record<string, SimplePropertyDef> = {},
T extends boolean = false,
Z extends OrderByOptions<Q, L> = {},
> = Osdk.Instance<
Q,
ExtractOptions<R, S, T>,
PropertyKeys<Q> extends L ? PropertyKeys<Q> : PropertyKeys<Q> & L,
{ [K in Extract<keyof RDPs, L>]: RDPs[K] }
{ [K in Extract<keyof RDPs, L>]: RDPs[K] },
ExtractOrderByOptions<Z extends "relevance" ? true : false>
>;

export type IsAny<T> = unknown extends T
Expand Down
48 changes: 41 additions & 7 deletions packages/api/src/objectSet/ObjectSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import type {
Augments,
FetchPageArgs,
NullabilityAdherence,
OrderByOptions,
SelectArg,
} from "../object/FetchPageArgs.js";
import type { Result } from "../object/Result.js";
import type { VectorPropertyKeys } from "../ontology/FilteredPropertyKeys.js";
import type { InterfaceDefinition } from "../ontology/InterfaceDefinition.js";
import type {
DerivedObjectOrInterfaceDefinition,
Expand All @@ -39,7 +41,11 @@ import type {
} from "../ontology/ObjectTypeDefinition.js";
import type { SimplePropertyDef } from "../ontology/SimplePropertyDef.js";
import type { PrimaryKeyType } from "../OsdkBase.js";
import type { ExtractOptions, Osdk } from "../OsdkObjectFrom.js";
import type {
ExtractOptions,
ExtractOrderByOptions,
Osdk,
} from "../OsdkObjectFrom.js";
import type { PageResult } from "../PageResult.js";
import type { LinkedType, LinkNames } from "../util/LinkUtils.js";
import type { BaseObjectSet } from "./BaseObjectSet.js";
Expand Down Expand Up @@ -90,15 +96,17 @@ interface FetchPage<
const A extends Augments,
S extends NullabilityAdherence = NullabilityAdherence.Default,
T extends boolean = false,
Z extends OrderByOptions<Q, L> = {},
>(
args?: FetchPageArgs<Q, L, R, A, S, T>,
args?: FetchPageArgs<Q, L, R, A, S, T, Z>,
) => Promise<
PageResult<
Osdk.Instance<
Q,
ExtractOptions<R, S, T>,
PropertyKeys<Q> extends L ? PropertyKeys<Q> : PropertyKeys<Q> & L,
{ [K in Extract<keyof RDPs, L>]: RDPs[K] }
{ [K in Extract<keyof RDPs, L>]: RDPs[K] },
ExtractOrderByOptions<Z extends "relevance" ? true : false>
>
>
>;
Expand All @@ -123,16 +131,18 @@ interface FetchPage<
const A extends Augments,
S extends NullabilityAdherence = NullabilityAdherence.Default,
T extends boolean = false,
Z extends OrderByOptions<Q, L> = {},
>(
args?: FetchPageArgs<Q, L, R, A, S, T>,
args?: FetchPageArgs<Q, L, R, A, S, T, Z>,
) => Promise<
Result<
PageResult<
Osdk.Instance<
Q,
ExtractOptions<R, S, T>,
PropertyKeys<Q> extends L ? PropertyKeys<Q> : PropertyKeys<Q> & L,
{ [K in Extract<keyof RDPs, L>]: RDPs[K] }
{ [K in Extract<keyof RDPs, L>]: RDPs[K] },
ExtractOrderByOptions<Z extends "relevance" ? true : false>
>
>
>
Expand Down Expand Up @@ -177,14 +187,16 @@ interface AsyncIter<
const A extends Augments,
S extends NullabilityAdherence = NullabilityAdherence.Default,
T extends boolean = false,
Z extends OrderByOptions<Q, L> = {},
>(
args?: AsyncIterArgs<Q, L, R, A, S, T>,
args?: AsyncIterArgs<Q, L, R, A, S, T, Z>,
) => AsyncIterableIterator<
Osdk.Instance<
Q,
ExtractOptions<R, S, T>,
PropertyKeys<Q> extends L ? PropertyKeys<Q> : PropertyKeys<Q> & L,
{ [K in Extract<keyof RDPs, L>]: RDPs[K] }
{ [K in Extract<keyof RDPs, L>]: RDPs[K] },
ExtractOrderByOptions<Z extends "relevance" ? true : false>
>
>;
}
Expand Down Expand Up @@ -212,6 +224,27 @@ interface WithProperties<
>;
}

interface NearestNeighbors<Q extends ObjectOrInterfaceDefinition> {
/**
* Finds the nearest neighbors for a given text or vector within the object set.
*
* @param query - Queries support either a vector matching the embedding model defined on the property, or text that is
automatically embedded.
Copy link
Contributor

@nihalbhatnagar nihalbhatnagar Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting it automatically embedded is cool

* @param numNeighbors - The number of objects to return. If the number of documents in the objectType is less than the provided
value, all objects will be returned. This value is limited to 1 &le; numNeighbors &ge; 500.
* @param property - The property key with a defined embedding model to search over.
*
* @returns An object set containing the `numNeighbors` nearest neighbors. To return the objects ordered by relevance and each
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I nest two nearest neighbors object sets, which one takes precedence? Or do we throw

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It operates the same way as nested unions/intersects in that it executes from inner to outer and returns the outer result. Added an example in 2538015

* objects associated score, specify "relevance" in the orderBy.
*/

readonly nearestNeighbors: (
query: string | number[],
numNeighbors: number,
property: VectorPropertyKeys<Q>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use FilteredPropertyKeys here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, f6eb11b

) => this;
}

export interface ObjectSet<
Q extends ObjectOrInterfaceDefinition = any,
// Generated code has what is basically ObjectSet<Q> set in here
Expand Down Expand Up @@ -391,6 +424,7 @@ interface ObjectSetCleanedTypes<
WithProperties<Q, D>,
Aggregate<MERGED>,
SetArithmetic<MERGED>,
NearestNeighbors<MERGED>,
PivotTo<MERGED>,
FetchOne<Q, D>,
Subscribe<MERGED>
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/ontology/FilteredPropertyKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ export type FilteredPropertyKeys<
? K
: never;
}[keyof NonNullable<O["__DefinitionMetadata"]>["properties"]];

export type VectorPropertyKeys<Q extends ObjectOrInterfaceDefinition> =
FilteredPropertyKeys<Q, "vector">;
Loading
Loading