Skip to content

Commit

Permalink
improve namespace exports (#66)
Browse files Browse the repository at this point in the history
* improve namespace exports

* renamings for better type exports
  • Loading branch information
bmschmidt authored Aug 28, 2024
1 parent 0325907 commit c86c86e
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 121 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"test": "env-cmd --file .env uvu tests -r esbuild-register",
"test:file": "env-cmd --file .env uvu -r esbuild-register tests",
"test:embeddings": "npm run test:file -- embedding.test.js",
"build-types": "openapi-typescript https://api-atlas.nomic.ai/v1/api-reference/openapi.json -o ./src/type-gen/openapi.d.ts",
"build-types": "openapi-typescript https://api-atlas.nomic.ai/v1/api-reference/openapi.json -o ./src/type-gen/openapi.ts",
"build": "tsc",
"build:watch": "tsc -w",
"insert-version": "node -e \"const fs = require('fs'); const pkg = require('./package.json'); fs.writeFileSync('./src/version.ts', 'export const version = \\'' + pkg.version + '\\';');\"",
Expand Down
96 changes: 0 additions & 96 deletions src/global.d.ts

This file was deleted.

83 changes: 83 additions & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
export type UUID = string;
export type LoadProjectOptions = {
project_id: UUID;
};

/**
* Options for the nearest neighbors query.
*/
export type NNOptions = {
/**
* The datum_ids (i.e., user-specified keys) to query for.
*/
datum_ids?: string[];
/**
* The Atom IDs (Nomic-generated integers) to query for.
*/
atom_ids?: string[];
/**
* The number of nearest neighbors to return.
*/
k?: number;
};

export type ProjectInitOptions = {
project_name: string;
organization_name?: string;
organization_id?: UUID;
unique_id_field: string;
modality: 'text' | 'embedding';
};
export type ProjectionInfo = {
id: UUID;
ready: boolean;
created_timestamp: string;
};
export type IndexInfo = {
id: UUID;
projections: ProjectionInfo[];
};
export type Payload = Record<string, any> | Uint8Array | null;
export type AtlasUser = {};

export type Envlogin = {
useEnvToken: true;
apiLocation?: never;
apiKey?: never;
bearerToken?: never;
};
export type ApiKeyLogin = {
useEnvToken?: never;
apiLocation?: string;
apiKey: string;
bearerToken?: never;
};
export type BearerTokenLogin = {
useEnvToken?: never;
bearerToken: string;
apiLocation?: string;
apiKey?: never;
};
export type AnonViewerLogin = {
useEnvToken?: never;
bearerToken?: never;
apiLocation?: string;
apiKey?: never;
};
export type LoginParams =
| Envlogin
| ApiKeyLogin
| BearerTokenLogin
| AnonViewerLogin;

export type ApiCallOptions = {
octetStreamAsUint8?: boolean;
};

export type TokenRefreshResponse = any;

export interface Credentials {
refresh_token: string | null;
token: string;
expires: number;
}
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AtlasDataset as AtlasDataset } from './project.js';
import type { Table } from 'apache-arrow';
import { AtlasViewer } from './viewer.js';
import type { components } from './type-gen/openapi.js';
import * as Atlas from './global.js';

type IndexInitializationOptions = {
project_id?: Atlas.UUID;
Expand All @@ -29,7 +30,8 @@ export class AtlasIndex extends BaseAtlasClass<{}> {
throw new Error('project_id or project is required');
}
this.project =
options.project || new AtlasDataset(options.project_id as string, user);
options.project ||
new AtlasDataset(options.project_id as string, this.viewer);
this.id = id;
}

Expand Down Expand Up @@ -74,11 +76,10 @@ export class AtlasIndex extends BaseAtlasClass<{}> {
if (this._projections) {
return this._projections;
} else {
const project_info =
(await this.project.fetchAttributes()) as Atlas.ProjectInfo;
const project_info = await this.project.fetchAttributes();
const projections =
project_info.atlas_indices?.find((d) => d.id === this.id)
?.projections || [];
project_info.atlas_indices.find((d) => d.id === this.id)?.projections ||
[];
this._projections = projections.map(
(d) =>
new AtlasProjection(d.id as string, this.viewer, {
Expand Down
9 changes: 6 additions & 3 deletions src/organization.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { AtlasUser, BaseAtlasClass, getEnvViewer } from './user.js';
import { AtlasUser, BaseAtlasClass } from './user.js';
import { AtlasDataset } from './project.js';
import type { components } from './type-gen/openapi.js';
import { AtlasViewer } from 'viewer.js';

type UUID = string;

// The response here depends on the authorization
export type OrganizationInfo =
| components['schemas']['PublicOrganizationResponse']
| components['schemas']['Organization'];
Expand All @@ -16,8 +19,8 @@ type ProjectInitOptions = {
export class AtlasOrganization extends BaseAtlasClass<OrganizationInfo> {
id: UUID;

constructor(id: UUID, user?: AtlasUser) {
super(user);
constructor(id: UUID, viewer?: AtlasViewer) {
super(viewer);
this.id = id;
}

Expand Down
24 changes: 11 additions & 13 deletions src/project.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Schema, Table } from 'apache-arrow';
import { tableToIPC, tableFromJSON, tableFromIPC } from 'apache-arrow';
import { AtlasUser, BaseAtlasClass } from './user.js';
import { BaseAtlasClass } from './user.js';
import { AtlasIndex } from './index.js';
import { AtlasViewer } from './viewer.js';
import * as Atlas from './global.js';
import { components } from 'type-gen/openapi.js';
// get the API key from the node environment
type UUID = string;

Expand All @@ -16,11 +18,6 @@ type EmbeddingMatrix = Array<SingleEmbedding>;
type TypedArrayEmbedding = Float32Array | Float64Array;
type EmbeddingType = EmbeddingMatrix | TypedArrayEmbedding;

interface AddDataOptions {
data: DataIngest;
embeddings?: EmbeddingType;
}

type IndexCreateOptions = {
project_id: UUID;
index_name: string;
Expand Down Expand Up @@ -60,7 +57,9 @@ type CreateAtlasIndexRequest = {
* interfaces to upload, update, and delete data, as well as create and delete
* indices which handle specific views.
*/
export class AtlasDataset extends BaseAtlasClass<Atlas.ProjectInfo> {
export class AtlasDataset extends BaseAtlasClass<
components['schemas']['Project']
> {
_indices: AtlasIndex[] = [];
_schema?: Schema | null;
id: UUID;
Expand All @@ -73,8 +72,8 @@ export class AtlasDataset extends BaseAtlasClass<Atlas.ProjectInfo> {
*
* @returns An AtlasDataset object.
*/
constructor(id: UUID | string, user?: AtlasUser | AtlasViewer) {
super(user);
constructor(id: UUID | string, viewer?: AtlasViewer) {
super(viewer);
// check if id is a valid UUID

const uuidPattern =
Expand All @@ -83,7 +82,7 @@ export class AtlasDataset extends BaseAtlasClass<Atlas.ProjectInfo> {
if (!id.toLowerCase().match(uuidPattern)) {
// throw new Error(`${id} is not a valid UUID.`);
this.id = id;
this.fetchAttributes().then((i) => (this.id = i.project_id));
this.fetchAttributes().then((i) => (this.id = i.id));
}
}

Expand Down Expand Up @@ -132,7 +131,7 @@ export class AtlasDataset extends BaseAtlasClass<Atlas.ProjectInfo> {
// Create a new project to clear the cache.

const renewed = new AtlasDataset(this.id, this.viewer);
const info = (await renewed.fetchAttributes()) as Atlas.ProjectInfo;
const info = await renewed.fetchAttributes();
if (info.insert_update_delete_lock === false) {
clearInterval(interval);
// Clear the cache.
Expand All @@ -151,8 +150,7 @@ export class AtlasDataset extends BaseAtlasClass<Atlas.ProjectInfo> {
if (this._indices.length > 0) {
return this._indices;
}
const { atlas_indices } =
(await this.fetchAttributes()) as Atlas.ProjectInfo;
const { atlas_indices } = await this.fetchAttributes();

if (atlas_indices === undefined) {
return [];
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AtlasViewer } from './viewer.js';
import type { components } from './type-gen/openapi.js';
import * as Atlas from './global.js';

export const isNode =
typeof process !== 'undefined' && process.versions && process.versions.node;
Expand Down Expand Up @@ -187,7 +188,7 @@ export class AtlasUser extends BaseAtlasClass<UserInfo> {
(d) => d.AtlasOrganization
);
return organizations.map(
(org) => new AtlasOrganization(org.organization_id, this)
(org) => new AtlasOrganization(org.organization_id, this.viewer)
);
}
}
3 changes: 2 additions & 1 deletion src/viewer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Table, tableFromIPC } from 'apache-arrow';
import { version } from './version';
import { version } from './version.js';
import * as Atlas from './global.js';

export class AtlasViewer {
/*
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"strict": true,
"declaration": true
},
"include": ["global.d.ts", "src/global.d.ts", "src/**/*.ts"]
"include": ["src/global.d.ts", "src/**/*.ts"]
}

0 comments on commit c86c86e

Please sign in to comment.