import deepline, { CtxFetchHttpError } from 'deepline'; const APPLICATION_CALLBACK_URL = 'https://app.example.com/api/deepline/callback'; export const config = { compatibility: { authoringContract: 'explicit-freshness' }, description: 'Find a company contact and deliver the result to the application.', secrets: ['APP_CALLBACK_TOKEN'], }; export default async function (input: { request_id: string; company_domain: string; job_title: string; }) { const result = await deepline.runPlay( 'company_to_contact', 'prebuilt/company-to-contact', { domain: input.company_domain, roles: [input.job_title] }, { description: 'Find a matching contact.' }, ); const callbackToken = await deepline.secrets.get('APP_CALLBACK_TOKEN'); const callbackBody = JSON.stringify({ request_id: input.request_id, result }); let lastStatus: number | null = null; try { await deepline.fetch( 'notify_application_1', APPLICATION_CALLBACK_URL, { method: 'POST', auth: deepline.secrets.bearer(callbackToken), headers: { 'content-type': 'application/json', 'idempotency-key': input.request_id, }, body: callbackBody, }, { staleAfterSeconds: 0 }, ); return { delivered: true }; } catch (error) { if (error instanceof CtxFetchHttpError) lastStatus = error.status; deepline.log('First callback attempt failed.'); } await deepline.sleep(5_000); try { await deepline.fetch( 'notify_application_2', APPLICATION_CALLBACK_URL, { method: 'POST', auth: deepline.secrets.bearer(callbackToken), headers: { 'content-type': 'application/json', 'idempotency-key': input.request_id, }, body: callbackBody, }, { staleAfterSeconds: 0 }, ); return { delivered: true }; } catch (error) { if (error instanceof CtxFetchHttpError) lastStatus = error.status; deepline.log('Second callback attempt failed.'); } await deepline.sleep(30_000); try { await deepline.fetch( 'notify_application_3', APPLICATION_CALLBACK_URL, { method: 'POST', auth: deepline.secrets.bearer(callbackToken), headers: { 'content-type': 'application/json', 'idempotency-key': input.request_id, }, body: callbackBody, }, { staleAfterSeconds: 0 }, ); return { delivered: true }; } catch (error) { if (error instanceof CtxFetchHttpError) lastStatus = error.status; deepline.log('Third callback attempt failed.'); } throw new Error( lastStatus === null ? 'Callback failed without an HTTP response.' : `Callback failed with HTTP ${lastStatus}.`, ); }