feat: add polar x tts and storage integration adapters with tests
This commit is contained in:
134
src/integrations/polar.js
Normal file
134
src/integrations/polar.js
Normal file
@@ -0,0 +1,134 @@
|
||||
"use strict";
|
||||
|
||||
const { Polar } = require("@polar-sh/sdk");
|
||||
const { validateEvent, WebhookVerificationError } = require("@polar-sh/sdk/webhooks");
|
||||
|
||||
function hasStandardWebhookHeaders(headers) {
|
||||
return Boolean(
|
||||
headers
|
||||
&& headers["webhook-id"]
|
||||
&& headers["webhook-timestamp"]
|
||||
&& headers["webhook-signature"],
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeTopUpPayload(payload) {
|
||||
if (!payload || typeof payload !== "object") {
|
||||
throw new Error("invalid_polar_payload");
|
||||
}
|
||||
|
||||
if (payload.userId && payload.credits && payload.eventId) {
|
||||
const credits = Number.parseInt(String(payload.credits), 10);
|
||||
if (!Number.isInteger(credits) || credits <= 0) {
|
||||
throw new Error("invalid_credit_amount");
|
||||
}
|
||||
|
||||
return {
|
||||
userId: String(payload.userId),
|
||||
credits,
|
||||
eventId: String(payload.eventId),
|
||||
};
|
||||
}
|
||||
|
||||
if (payload.type && payload.data && payload.data.metadata) {
|
||||
const metadata = payload.data.metadata;
|
||||
const userId = metadata.xartaudio_user_id || metadata.user_id || payload.data.externalCustomerId;
|
||||
const creditsRaw = metadata.xartaudio_credits || metadata.credits;
|
||||
const eventId = payload.data.id || payload.id;
|
||||
const credits = Number.parseInt(String(creditsRaw || ""), 10);
|
||||
|
||||
if (!userId || !eventId || !Number.isInteger(credits) || credits <= 0) {
|
||||
throw new Error("invalid_polar_metadata_for_topup");
|
||||
}
|
||||
|
||||
return {
|
||||
userId: String(userId),
|
||||
credits,
|
||||
eventId: String(eventId),
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error("unsupported_polar_payload");
|
||||
}
|
||||
|
||||
function createPolarAdapter({
|
||||
accessToken,
|
||||
server = "production",
|
||||
productIds = [],
|
||||
webhookSecret,
|
||||
sdk,
|
||||
} = {}) {
|
||||
const polarSdk = sdk || (accessToken ? new Polar({ accessToken, server }) : null);
|
||||
const configuredProductIds = Array.isArray(productIds)
|
||||
? productIds.filter(Boolean)
|
||||
: [];
|
||||
|
||||
return {
|
||||
isConfigured() {
|
||||
return Boolean(polarSdk && configuredProductIds.length > 0);
|
||||
},
|
||||
|
||||
async createCheckoutSession({
|
||||
userId,
|
||||
successUrl,
|
||||
returnUrl,
|
||||
metadata,
|
||||
customerEmail,
|
||||
}) {
|
||||
if (!polarSdk) {
|
||||
throw new Error("polar_not_configured");
|
||||
}
|
||||
|
||||
if (!userId) {
|
||||
throw new Error("user_id_required");
|
||||
}
|
||||
|
||||
if (configuredProductIds.length === 0) {
|
||||
throw new Error("polar_product_ids_required");
|
||||
}
|
||||
|
||||
const checkout = await polarSdk.checkouts.create({
|
||||
products: configuredProductIds,
|
||||
externalCustomerId: String(userId),
|
||||
successUrl,
|
||||
returnUrl,
|
||||
customerEmail: customerEmail || undefined,
|
||||
metadata: metadata || undefined,
|
||||
});
|
||||
|
||||
return {
|
||||
id: checkout.id,
|
||||
url: checkout.url,
|
||||
};
|
||||
},
|
||||
|
||||
parseWebhookEvent(rawBody, headers) {
|
||||
if (!webhookSecret) {
|
||||
throw new Error("polar_webhook_secret_required");
|
||||
}
|
||||
|
||||
if (!hasStandardWebhookHeaders(headers)) {
|
||||
throw new Error("polar_standard_webhook_headers_missing");
|
||||
}
|
||||
|
||||
try {
|
||||
return validateEvent(rawBody, headers, webhookSecret);
|
||||
} catch (error) {
|
||||
if (error instanceof WebhookVerificationError) {
|
||||
throw new Error("invalid_polar_webhook_signature", { cause: error });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
extractTopUp(payloadOrEvent) {
|
||||
return normalizeTopUpPayload(payloadOrEvent);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createPolarAdapter,
|
||||
normalizeTopUpPayload,
|
||||
hasStandardWebhookHeaders,
|
||||
};
|
||||
71
src/integrations/storage-client.js
Normal file
71
src/integrations/storage-client.js
Normal file
@@ -0,0 +1,71 @@
|
||||
"use strict";
|
||||
|
||||
const { S3Client, PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
|
||||
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
|
||||
|
||||
function createStorageAdapter({
|
||||
bucket,
|
||||
region,
|
||||
endpoint,
|
||||
accessKeyId,
|
||||
secretAccessKey,
|
||||
signedUrlTtlSec = 3600,
|
||||
client,
|
||||
signedUrlFactory,
|
||||
} = {}) {
|
||||
const s3 = client || (bucket && region && accessKeyId && secretAccessKey
|
||||
? new S3Client({
|
||||
region,
|
||||
endpoint: endpoint || undefined,
|
||||
forcePathStyle: Boolean(endpoint),
|
||||
credentials: {
|
||||
accessKeyId,
|
||||
secretAccessKey,
|
||||
},
|
||||
})
|
||||
: null);
|
||||
|
||||
const sign = signedUrlFactory || getSignedUrl;
|
||||
|
||||
return {
|
||||
isConfigured() {
|
||||
return Boolean(s3 && bucket);
|
||||
},
|
||||
|
||||
async uploadAudio({ key, body, contentType = "audio/mpeg" }) {
|
||||
if (!s3 || !bucket) {
|
||||
throw new Error("storage_not_configured");
|
||||
}
|
||||
|
||||
if (!key || !body) {
|
||||
throw new Error("storage_upload_payload_required");
|
||||
}
|
||||
|
||||
await s3.send(new PutObjectCommand({
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
Body: body,
|
||||
ContentType: contentType,
|
||||
}));
|
||||
|
||||
return {
|
||||
bucket,
|
||||
key,
|
||||
};
|
||||
},
|
||||
|
||||
async getSignedDownloadUrl(key, ttlSec) {
|
||||
if (!s3 || !bucket) {
|
||||
throw new Error("storage_not_configured");
|
||||
}
|
||||
|
||||
const expiresIn = Number.isInteger(ttlSec) && ttlSec > 0 ? ttlSec : signedUrlTtlSec;
|
||||
const command = new GetObjectCommand({ Bucket: bucket, Key: key });
|
||||
return sign(s3, command, { expiresIn });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createStorageAdapter,
|
||||
};
|
||||
47
src/integrations/tts-client.js
Normal file
47
src/integrations/tts-client.js
Normal file
@@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
|
||||
const { OpenAI } = require("openai");
|
||||
|
||||
function createTTSAdapter({
|
||||
apiKey,
|
||||
baseURL,
|
||||
model = "gpt-4o-mini-tts",
|
||||
voice = "alloy",
|
||||
format = "mp3",
|
||||
client,
|
||||
} = {}) {
|
||||
const openai = client || (apiKey ? new OpenAI({ apiKey, baseURL: baseURL || undefined }) : null);
|
||||
|
||||
return {
|
||||
isConfigured() {
|
||||
return Boolean(openai);
|
||||
},
|
||||
|
||||
async synthesize(text, options) {
|
||||
if (!openai) {
|
||||
throw new Error("tts_not_configured");
|
||||
}
|
||||
|
||||
if (!text || !String(text).trim()) {
|
||||
throw new Error("tts_text_required");
|
||||
}
|
||||
|
||||
const effectiveModel = options && options.model ? options.model : model;
|
||||
const effectiveVoice = options && options.voice ? options.voice : voice;
|
||||
|
||||
const response = await openai.audio.speech.create({
|
||||
model: effectiveModel,
|
||||
voice: effectiveVoice,
|
||||
input: String(text),
|
||||
format,
|
||||
});
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
return Buffer.from(arrayBuffer);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createTTSAdapter,
|
||||
};
|
||||
79
src/integrations/x-client.js
Normal file
79
src/integrations/x-client.js
Normal file
@@ -0,0 +1,79 @@
|
||||
"use strict";
|
||||
|
||||
const { TwitterApi } = require("twitter-api-v2");
|
||||
|
||||
function findParentReplyId(mentionTweet) {
|
||||
const refs = mentionTweet && Array.isArray(mentionTweet.referenced_tweets)
|
||||
? mentionTweet.referenced_tweets
|
||||
: [];
|
||||
|
||||
const reply = refs.find((ref) => ref && ref.type === "replied_to");
|
||||
return reply ? reply.id : null;
|
||||
}
|
||||
|
||||
function createXAdapter({
|
||||
bearerToken,
|
||||
botUserId,
|
||||
client,
|
||||
} = {}) {
|
||||
const apiClient = client || (bearerToken ? new TwitterApi(bearerToken) : null);
|
||||
|
||||
return {
|
||||
isConfigured() {
|
||||
return Boolean(apiClient && botUserId);
|
||||
},
|
||||
|
||||
async listMentions({ sinceId, maxResults = 10 } = {}) {
|
||||
if (!apiClient || !botUserId) {
|
||||
throw new Error("x_api_not_configured");
|
||||
}
|
||||
|
||||
const timeline = await apiClient.v2.userMentionTimeline(botUserId, {
|
||||
since_id: sinceId,
|
||||
max_results: maxResults,
|
||||
expansions: ["referenced_tweets.id"],
|
||||
"tweet.fields": ["author_id", "created_at", "referenced_tweets", "article"],
|
||||
});
|
||||
|
||||
return timeline && timeline.data ? timeline.data : [];
|
||||
},
|
||||
|
||||
async fetchParentPostFromMention(mentionTweetId) {
|
||||
if (!apiClient) {
|
||||
throw new Error("x_api_not_configured");
|
||||
}
|
||||
|
||||
const mention = await apiClient.v2.singleTweet(mentionTweetId, {
|
||||
"tweet.fields": ["author_id", "referenced_tweets"],
|
||||
});
|
||||
|
||||
const parentId = findParentReplyId(mention && mention.data ? mention.data : mention);
|
||||
if (!parentId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parent = await apiClient.v2.singleTweet(parentId, {
|
||||
"tweet.fields": ["author_id", "created_at", "article"],
|
||||
});
|
||||
|
||||
return parent && parent.data ? parent.data : parent;
|
||||
},
|
||||
|
||||
async replyToMention({ mentionTweetId, text }) {
|
||||
if (!apiClient) {
|
||||
throw new Error("x_api_not_configured");
|
||||
}
|
||||
|
||||
if (!mentionTweetId || !text) {
|
||||
throw new Error("reply_payload_required");
|
||||
}
|
||||
|
||||
return apiClient.v2.reply(text, mentionTweetId);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createXAdapter,
|
||||
findParentReplyId,
|
||||
};
|
||||
66
src/services/audio-generation.js
Normal file
66
src/services/audio-generation.js
Normal file
@@ -0,0 +1,66 @@
|
||||
"use strict";
|
||||
|
||||
const PQueue = require("p-queue").default;
|
||||
|
||||
function createAudioGenerationService({
|
||||
tts,
|
||||
storage,
|
||||
logger = console,
|
||||
concurrency = 2,
|
||||
}) {
|
||||
const queue = new PQueue({ concurrency });
|
||||
|
||||
return {
|
||||
isConfigured() {
|
||||
const ttsConfigured = typeof tts.isConfigured === "function" ? tts.isConfigured() : true;
|
||||
const storageConfigured = typeof storage.isConfigured === "function" ? storage.isConfigured() : true;
|
||||
return Boolean(
|
||||
tts
|
||||
&& storage
|
||||
&& ttsConfigured
|
||||
&& storageConfigured
|
||||
&& typeof tts.synthesize === "function"
|
||||
&& typeof storage.uploadAudio === "function",
|
||||
);
|
||||
},
|
||||
|
||||
enqueueJob({ assetId, text, onCompleted, onFailed }) {
|
||||
if (!assetId || !text) {
|
||||
throw new Error("audio_generation_payload_required");
|
||||
}
|
||||
|
||||
return queue.add(async () => {
|
||||
try {
|
||||
const audioBytes = await tts.synthesize(text);
|
||||
const key = `audio/${assetId}.mp3`;
|
||||
await storage.uploadAudio({ key, body: audioBytes, contentType: "audio/mpeg" });
|
||||
if (onCompleted) {
|
||||
onCompleted({
|
||||
storageKey: key,
|
||||
sizeBytes: audioBytes.length,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
storageKey: key,
|
||||
sizeBytes: audioBytes.length,
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error({ err: error, assetId }, "audio generation failed");
|
||||
if (onFailed) {
|
||||
onFailed(error);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
async onIdle() {
|
||||
await queue.onIdle();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createAudioGenerationService,
|
||||
};
|
||||
Reference in New Issue
Block a user