UNPKG

langsmith

Version:

Client library to connect to the LangSmith Observability and Evaluation Platform.

389 lines (388 loc) 12.7 kB
// @ts-nocheck // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _AbstractPage_client; import { LangsmithError } from './error.js'; import { defaultParseResponse } from '../internal/parse.js'; import { APIPromise } from './api-promise.js'; import { maybeObj } from '../internal/utils/values.js'; export class AbstractPage { constructor(client, response, body, options) { _AbstractPage_client.set(this, void 0); Object.defineProperty(this, "options", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "response", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "body", { enumerable: true, configurable: true, writable: true, value: void 0 }); __classPrivateFieldSet(this, _AbstractPage_client, client, "f"); this.options = options; this.response = response; this.body = body; } hasNextPage() { const items = this.getPaginatedItems(); if (!items.length) return false; return this.nextPageRequestOptions() != null; } async getNextPage() { const nextOptions = this.nextPageRequestOptions(); if (!nextOptions) { throw new LangsmithError('No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.'); } return await __classPrivateFieldGet(this, _AbstractPage_client, "f").requestAPIList(this.constructor, nextOptions); } async *iterPages() { let page = this; yield page; while (page.hasNextPage()) { page = await page.getNextPage(); yield page; } } async *[(_AbstractPage_client = new WeakMap(), Symbol.asyncIterator)]() { for await (const page of this.iterPages()) { for (const item of page.getPaginatedItems()) { yield item; } } } } /** * This subclass of Promise will resolve to an instantiated Page once the request completes. * * It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg: * * for await (const item of client.items.list()) { * console.log(item) * } */ export class PagePromise extends APIPromise { constructor(client, request, Page) { super(client, request, async (client, props) => new Page(client, props.response, await defaultParseResponse(client, props), props.options)); } /** * Allow auto-paginating iteration on an unawaited list call, eg: * * for await (const item of client.items.list()) { * console.log(item) * } */ async *[Symbol.asyncIterator]() { const page = await this; for await (const item of page) { yield item; } } } export class OffsetPaginationTopLevelArray extends AbstractPage { constructor(client, response, body, options) { super(client, response, body, options); Object.defineProperty(this, "items", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.items = body || []; } getPaginatedItems() { return this.items ?? []; } nextPageRequestOptions() { const offset = this.options.query.offset ?? 0; const length = this.getPaginatedItems().length; const currentCount = offset + length; return { ...this.options, query: { ...maybeObj(this.options.query), offset: currentCount, }, }; } } export class OffsetPaginationIssues extends AbstractPage { constructor(client, response, body, options) { super(client, response, body, options); Object.defineProperty(this, "items", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.items = body || []; } getPaginatedItems() { return this.items ?? []; } nextPageRequestOptions() { const offset = this.options.query.offset ?? 0; const length = this.getPaginatedItems().length; const currentCount = offset + length; return { ...this.options, query: { ...maybeObj(this.options.query), offset: currentCount, }, }; } } export class OffsetPaginationRepos extends AbstractPage { constructor(client, response, body, options) { super(client, response, body, options); Object.defineProperty(this, "repos", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "total", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.repos = body.repos || []; this.total = body.total || 0; } getPaginatedItems() { return this.repos ?? []; } nextPageRequestOptions() { const offset = this.options.query.offset ?? 0; const length = this.getPaginatedItems().length; const currentCount = offset + length; return { ...this.options, query: { ...maybeObj(this.options.query), offset: currentCount, }, }; } } export class OffsetPaginationCommits extends AbstractPage { constructor(client, response, body, options) { super(client, response, body, options); Object.defineProperty(this, "commits", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "total", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.commits = body.commits || []; this.total = body.total || 0; } getPaginatedItems() { return this.commits ?? []; } nextPageRequestOptions() { const offset = this.options.query.offset ?? 0; const length = this.getPaginatedItems().length; const currentCount = offset + length; return { ...this.options, query: { ...maybeObj(this.options.query), offset: currentCount, }, }; } } export class OffsetPaginationOnlineEvaluators extends AbstractPage { constructor(client, response, body, options) { super(client, response, body, options); Object.defineProperty(this, "evaluators", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "total", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.evaluators = body.evaluators || []; this.total = body.total || 0; } getPaginatedItems() { return this.evaluators ?? []; } nextPageRequestOptions() { const offset = this.options.query.offset ?? 0; const length = this.getPaginatedItems().length; const currentCount = offset + length; return { ...this.options, query: { ...maybeObj(this.options.query), offset: currentCount, }, }; } } export class OffsetPaginationInsightsClusteringJobs extends AbstractPage { constructor(client, response, body, options) { super(client, response, body, options); Object.defineProperty(this, "clustering_jobs", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.clustering_jobs = body.clustering_jobs || []; } getPaginatedItems() { return this.clustering_jobs ?? []; } nextPageRequestOptions() { const offset = this.options.query.offset ?? 0; const length = this.getPaginatedItems().length; const currentCount = offset + length; return { ...this.options, query: { ...maybeObj(this.options.query), offset: currentCount, }, }; } } export class CursorPagination extends AbstractPage { constructor(client, response, body, options) { super(client, response, body, options); Object.defineProperty(this, "runs", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "cursors", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.runs = body.runs || []; this.cursors = body.cursors || {}; } getPaginatedItems() { return this.runs ?? []; } nextPageRequestOptions() { const cursor = this.cursors?.next; if (!cursor) { return null; } return { ...this.options, body: { ...maybeObj(this.options.body), cursor, }, }; } } export class ItemsCursorPostPagination extends AbstractPage { constructor(client, response, body, options) { super(client, response, body, options); Object.defineProperty(this, "items", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "next_cursor", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.items = body.items || []; this.next_cursor = body.next_cursor || ''; } getPaginatedItems() { return this.items ?? []; } nextPageRequestOptions() { const cursor = this.next_cursor; if (!cursor) { return null; } return { ...this.options, body: { ...maybeObj(this.options.body), cursor, }, }; } } export class ItemsCursorGetPagination extends AbstractPage { constructor(client, response, body, options) { super(client, response, body, options); Object.defineProperty(this, "items", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "next_cursor", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.items = body.items || []; this.next_cursor = body.next_cursor || ''; } getPaginatedItems() { return this.items ?? []; } nextPageRequestOptions() { const cursor = this.next_cursor; if (!cursor) { return null; } return { ...this.options, query: { ...maybeObj(this.options.query), cursor, }, }; } }