Skip to content

Commit 75e83d2

Browse files
Pwntusbfirsh
authored andcommitted
feat: add webhook adn webhook_events_filter options
1 parent 4f06977 commit 75e83d2

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,12 @@ const response = await replicate.collections.get(collection_slug);
182182
const response = await replicate.predictions.create(options);
183183
```
184184

185-
| name | type | description |
186-
| --------------------------- | ------ | ------------------------------------------------------------------------- |
187-
| `options.version` | string | **Required**. The model version |
188-
| `options.input` | object | **Required**. An object with the models inputs |
189-
| `options.webhook_completed` | string | A URL which will receive a POST request upon completion of the prediction |
185+
| name | type | description |
186+
| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- |
187+
| `options.version` | string | **Required**. The model version |
188+
| `options.input` | object | **Required**. An object with the models inputs |
189+
| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output |
190+
| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) |
190191

191192
```jsonc
192193
{

index.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
type Identifier = `${string}/${string}:${string}`;
2+
type WebhookEventType = "start" | "output" | "logs" | "completed";
23

34
declare module "replicate" {
45
export interface ReplicateOptions {
@@ -36,7 +37,8 @@ declare module "replicate" {
3637
version: string;
3738
input: any;
3839
output: any;
39-
webhook_completed: string;
40+
webhook?: string;
41+
webhook_events_filter?: WebhookEventType[];
4042
created: string;
4143
updated: string;
4244
}
@@ -64,7 +66,8 @@ declare module "replicate" {
6466
export interface PredictionsCreateOptions {
6567
version: string;
6668
input: any;
67-
webhook_completed?: string;
69+
webhook?: string;
70+
webhook_events_filter?: WebhookEventType[];
6871
}
6972

7073
export interface PredictionsGetOptions {
@@ -79,7 +82,8 @@ declare module "replicate" {
7982
options: {
8083
input: object;
8184
wait?: boolean | { interval?: number; maxAttempts?: number };
82-
webhook_completed?: string;
85+
webhook?: string;
86+
webhook_events_filter?: WebhookEventType[];
8387
}
8488
): Promise<object>;
8589
request(route: string, parameters: any): Promise<any>;

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ class Replicate {
7474
* @param {boolean|object} [options.wait] - Whether to wait for the prediction to finish. Defaults to false
7575
* @param {number} [options.wait.interval] - Polling interval in milliseconds. Defaults to 250
7676
* @param {number} [options.wait.maxAttempts] - Maximum number of polling attempts. Defaults to no limit
77-
* @param {string} [options.webhook_completed] - A URL which will receive a POST request upon completion of the prediction
77+
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
78+
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
7879
* @throws {Error} If the prediction failed
7980
* @returns {Promise<object>} - Resolves with the output of running the model
8081
*/

index.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ describe('Replicate client', () => {
6060
});
6161
});
6262

63+
test('Calls the correct API route with the correct payload, webhook URL and webhook filters', async () => {
64+
client.request = jest.fn();
65+
const input = { text: 'Hello, world!' };
66+
await client.predictions.create({
67+
version:
68+
'632231d0d49d34d5c4633bd838aee3d81d936e59a886fbf28524702003b4c532',
69+
input,
70+
webhook: 'http://test.host/webhook',
71+
webhook_events_filter: ['output', 'completed'],
72+
});
73+
expect(client.request).toHaveBeenCalledWith('/predictions', {
74+
method: 'POST',
75+
data: {
76+
version:
77+
'632231d0d49d34d5c4633bd838aee3d81d936e59a886fbf28524702003b4c532',
78+
input: {
79+
text: 'Hello, world!',
80+
},
81+
webhook: 'http://test.host/webhook',
82+
webhook_events_filter: ['output', 'completed'],
83+
},
84+
});
85+
});
86+
6387
// Add more tests for error handling, edge cases, etc.
6488
});
6589

lib/predictions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
* @param {boolean|object} [options.wait] - Whether to wait for the prediction to finish. Defaults to false
88
* @param {number} [options.wait.interval] - Polling interval in milliseconds. Defaults to 250
99
* @param {number} [options.wait.maxAttempts] - Maximum number of polling attempts. Defaults to no limit
10-
* @param {string} [options.webhook_completed] - A URL which will receive a POST request upon completion of the prediction
10+
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
11+
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
1112
* @returns {Promise<object>} Resolves with the created prediction data
1213
*/
1314
async function createPrediction(options) {

0 commit comments

Comments
 (0)