From e33ec2802f2fae7db3bdfe1d18846882b79c9471 Mon Sep 17 00:00:00 2001 From: Matt Rothenberg Date: Fri, 16 Feb 2024 11:50:45 +0100 Subject: [PATCH 1/6] feat: add function for getting deployment --- index.d.ts | 26 ++++++++++++++++++++++++++ index.js | 1 + index.test.ts | 36 ++++++++++++++++++++++++++++++++++++ lib/deployments.js | 18 ++++++++++++++++++ 4 files changed, 81 insertions(+) diff --git a/index.d.ts b/index.d.ts index 76df9d4f..3a557cbc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -89,6 +89,28 @@ declare module "replicate" { retry?: number; } + export interface Deployment { + owner: string; + name: string; + current_release: { + number: number; + model: string; + version: string; + created_at: string; + created_by: { + type: "user" | "organization"; + username: string; + name: string; + github_url: string; + }; + configuration: { + hardware: string; + min_instances: number; + max_instances: number; + }; + }; + } + export default class Replicate { constructor(options?: { auth?: string; @@ -169,6 +191,10 @@ declare module "replicate" { } ): Promise; }; + get( + deployment_owner: string, + deployment_name: string + ): Promise; }; hardware: { diff --git a/index.js b/index.js index a85ea4e7..1ab87e68 100644 --- a/index.js +++ b/index.js @@ -58,6 +58,7 @@ class Replicate { }; this.deployments = { + get: deployments.get.bind(this), predictions: { create: deployments.predictions.create.bind(this), }, diff --git a/index.test.ts b/index.test.ts index d50ccb47..d8c81dd5 100644 --- a/index.test.ts +++ b/index.test.ts @@ -716,6 +716,42 @@ describe("Replicate client", () => { // Add more tests for error handling, edge cases, etc. }); + describe("deployments.get", () => { + test("Calls the correct API route with the correct payload", async () => { + nock(BASE_URL) + .get("/deployments/replicate/greeter") + .reply(200, { + owner: "replicate", + name: "greeter", + current_release: { + number: 1, + model: "replicate/hello-world", + version: + "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", + created_at: "2022-09-10T09:44:22.165836Z", + created_by: { + type: "organization", + username: "replicate", + name: "Replicate", + github_url: "https://github.com/replicate", + }, + configuration: { + hardware: "gpu-a100", + min_instances: 0, + max_instances: 1, + }, + }, + }); + + const deployment = await client.deployments.get("replicate", "greeter"); + + expect(deployment.owner).toBe("replicate"); + expect(deployment.name).toBe("greeter"); + expect(deployment.current_release.model).toBe("replicate/hello-world"); + }); + // Add more tests for error handling, edge cases, etc. + }); + describe("predictions.create with model", () => { test("Calls the correct API route with the correct payload", async () => { nock(BASE_URL) diff --git a/lib/deployments.js b/lib/deployments.js index 6f32cdba..fb602b13 100644 --- a/lib/deployments.js +++ b/lib/deployments.js @@ -33,8 +33,26 @@ async function createPrediction(deployment_owner, deployment_name, options) { return response.json(); } +/** + * Get a deployment + * @param {string} deployment_owner - Required. The username of the user or organization who owns the deployment + * @param {string} deployment_name - Required. The name of the deployment + * @returns {Promise} Resolves with the deployment data + */ +async function getDeployment(deployment_owner, deployment_name) { + const response = await this.request( + `/deployments/${deployment_owner}/${deployment_name}`, + { + method: "GET", + } + ); + + return response.json(); +} + module.exports = { predictions: { create: createPrediction, }, + get: getDeployment, }; From 99b9666773c8fad33f8828f2502aac6ec63eb87f Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 16 Feb 2024 07:56:00 -0800 Subject: [PATCH 2/6] Use Account type for created_by property --- index.d.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3a557cbc..1f8516dc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -97,12 +97,7 @@ declare module "replicate" { model: string; version: string; created_at: string; - created_by: { - type: "user" | "organization"; - username: string; - name: string; - github_url: string; - }; + created_by: Account; configuration: { hardware: string; min_instances: number; From 077d97de07c5396d5b9ad98e4a280676802db755 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 16 Feb 2024 07:56:14 -0800 Subject: [PATCH 3/6] Add newline separator in jsdoc --- lib/deployments.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/deployments.js b/lib/deployments.js index fb602b13..8ba5ea31 100644 --- a/lib/deployments.js +++ b/lib/deployments.js @@ -35,6 +35,7 @@ async function createPrediction(deployment_owner, deployment_name, options) { /** * Get a deployment + * * @param {string} deployment_owner - Required. The username of the user or organization who owns the deployment * @param {string} deployment_name - Required. The name of the deployment * @returns {Promise} Resolves with the deployment data From f036c18cb41213ab97bf8ccfcae508312050f255 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 16 Feb 2024 07:59:50 -0800 Subject: [PATCH 4/6] Update test case to match OpenAPI spec --- index.test.ts | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/index.test.ts b/index.test.ts index d8c81dd5..44db71f2 100644 --- a/index.test.ts +++ b/index.test.ts @@ -719,35 +719,40 @@ describe("Replicate client", () => { describe("deployments.get", () => { test("Calls the correct API route with the correct payload", async () => { nock(BASE_URL) - .get("/deployments/replicate/greeter") + .get("/deployments/acme/my-app-image-generator") .reply(200, { - owner: "replicate", - name: "greeter", + owner: "acme", + name: "my-app-image-generator", current_release: { number: 1, - model: "replicate/hello-world", + model: "stability-ai/sdxl", version: - "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", - created_at: "2022-09-10T09:44:22.165836Z", + "da77bc59ee60423279fd632efb4795ab731d9e3ca9705ef3341091fb989b7eaf", + created_at: "2024-02-15T16:32:57.018467Z", created_by: { type: "organization", - username: "replicate", - name: "Replicate", - github_url: "https://github.com/replicate", + username: "acme", + name: "Acme Corp, Inc.", + github_url: "https://github.com/acme", }, configuration: { - hardware: "gpu-a100", - min_instances: 0, - max_instances: 1, + hardware: "gpu-t4", + scaling: { + min_instances: 1, + max_instances: 5, + }, }, }, }); - const deployment = await client.deployments.get("replicate", "greeter"); + const deployment = await client.deployments.get( + "acme", + "my-app-image-generator" + ); - expect(deployment.owner).toBe("replicate"); - expect(deployment.name).toBe("greeter"); - expect(deployment.current_release.model).toBe("replicate/hello-world"); + expect(deployment.owner).toBe("acme"); + expect(deployment.name).toBe("my-app-image-generator"); + expect(deployment.current_release.model).toBe("stability-ai/sdxl"); }); // Add more tests for error handling, edge cases, etc. }); From 59d1933fe316999abc16e9e32cd4be423047fdd9 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 16 Feb 2024 08:00:22 -0800 Subject: [PATCH 5/6] Update operation ID for account.get --- index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.test.ts b/index.test.ts index 44db71f2..a2792448 100644 --- a/index.test.ts +++ b/index.test.ts @@ -67,7 +67,7 @@ describe("Replicate client", () => { }); }); - describe("accounts.current", () => { + describe("account.get", () => { test("Calls the correct API route", async () => { nock(BASE_URL).get("/account").reply(200, { type: "organization", From 1d880d69d0a17152e4e8ecfa50e2d3372b56d65e Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 16 Feb 2024 08:03:20 -0800 Subject: [PATCH 6/6] Formatting --- index.d.ts | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/index.d.ts b/index.d.ts index dbd1aaeb..69e651b1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -22,6 +22,23 @@ declare module "replicate" { models?: Model[]; } + export interface Deployment { + owner: string; + name: string; + current_release: { + number: number; + model: string; + version: string; + created_at: string; + created_by: Account; + configuration: { + hardware: string; + min_instances: number; + max_instances: number; + }; + }; + } + export interface Hardware { sku: string; name: string; @@ -89,23 +106,6 @@ declare module "replicate" { retry?: number; } - export interface Deployment { - owner: string; - name: string; - current_release: { - number: number; - model: string; - version: string; - created_at: string; - created_by: Account; - configuration: { - hardware: string; - min_instances: number; - max_instances: number; - }; - }; - } - export interface WebhookSecret { key: string; }