Skip to content

Commit d8c76ee

Browse files
committed
Simplify to just a call
1 parent 2536c10 commit d8c76ee

File tree

2 files changed

+29
-54
lines changed
  • packages/trigger-sdk/src/v3
  • references/hello-world/src/trigger

2 files changed

+29
-54
lines changed

packages/trigger-sdk/src/v3/wait.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -127,47 +127,18 @@ function createToken(
127127
* @param requestOptions - The request options for the waitpoint.
128128
* @returns A promise that returns the token and anything you returned from your callback.
129129
*/
130-
async function createHttpCallback<TCallbackResult>(
131-
callback: (url: string) => Promise<TCallbackResult>,
130+
async function createHttpCallback(
132131
options?: CreateWaitpointTokenRequestBody,
133132
requestOptions?: ApiRequestOptions
134-
): Promise<{
135-
/** The token that you can use to wait for the callback */
136-
token: CreateWaitpointHttpCallbackResponseBody;
137-
/** Whatever you returned from the function */
138-
data: TCallbackResult;
139-
}> {
133+
): Promise<CreateWaitpointHttpCallbackResponseBody> {
140134
const apiClient = apiClientManager.clientOrThrow();
141135

142-
return tracer.startActiveSpan(
143-
`wait.createHttpCallback()`,
144-
async (span) => {
145-
const waitpoint = await apiClient.createWaitpointHttpCallback(options ?? {}, requestOptions);
146-
147-
span.setAttribute("id", waitpoint.id);
148-
span.setAttribute("isCached", waitpoint.isCached);
149-
span.setAttribute("url", waitpoint.url);
150-
151-
const callbackResult = await tracer.startActiveSpan(
152-
`callback()`,
153-
async () => {
154-
return callback(waitpoint.url);
155-
},
156-
{
157-
attributes: {
158-
[SemanticInternalAttributes.STYLE_ICON]: "function",
159-
id: waitpoint.id,
160-
url: waitpoint.url,
161-
isCached: waitpoint.isCached,
162-
},
163-
}
164-
);
165-
166-
return { token: waitpoint, data: callbackResult };
167-
},
136+
const $requestOptions = mergeRequestOptions(
168137
{
138+
tracer,
139+
name: "wait.createHttpCallback()",
140+
icon: "wait-http-callback",
169141
attributes: {
170-
[SemanticInternalAttributes.STYLE_ICON]: "wait-http-callback",
171142
idempotencyKey: options?.idempotencyKey,
172143
idempotencyKeyTTL: options?.idempotencyKeyTTL,
173144
timeout: options?.timeout
@@ -177,8 +148,16 @@ async function createHttpCallback<TCallbackResult>(
177148
: undefined,
178149
tags: options?.tags,
179150
},
180-
}
151+
onResponseBody: (body: CreateWaitpointHttpCallbackResponseBody, span) => {
152+
span.setAttribute("id", body.id);
153+
span.setAttribute("isCached", body.isCached);
154+
span.setAttribute("url", body.url);
155+
},
156+
},
157+
requestOptions
181158
);
159+
160+
return apiClient.createWaitpointHttpCallback(options ?? {}, $requestOptions);
182161
}
183162

184163
/**

references/hello-world/src/trigger/waits.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,25 +152,21 @@ export const waitHttpCallback = task({
152152
auth: process.env.REPLICATE_API_KEY,
153153
});
154154

155-
const { token, data } = await wait.createHttpCallback(
156-
async (url) => {
157-
//pass the provided URL to Replicate's webhook
158-
return replicate.predictions.create({
159-
version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
160-
input: {
161-
prompt: "A painting of a cat by Any Warhol",
162-
},
163-
// pass the provided URL to Replicate's webhook, so they can "callback"
164-
webhook: url,
165-
webhook_events_filter: ["completed"],
166-
});
155+
const token = await wait.createHttpCallback({
156+
timeout: "10m",
157+
tags: ["replicate"],
158+
});
159+
logger.log("Create result", { token });
160+
161+
const call = await replicate.predictions.create({
162+
version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
163+
input: {
164+
prompt: "A painting of a cat by Any Warhol",
167165
},
168-
{
169-
timeout: "10m",
170-
tags: ["replicate"],
171-
}
172-
);
173-
logger.log("Create result", { token, data });
166+
// pass the provided URL to Replicate's webhook, so they can "callback"
167+
webhook: token.url,
168+
webhook_events_filter: ["completed"],
169+
});
174170

175171
const prediction = await wait.forToken<Prediction>(token);
176172

0 commit comments

Comments
 (0)