@hasna/microservices 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. package/microservices/microservice-social/package.json +2 -1
  2. package/microservices/microservice-social/src/cli/index.ts +906 -12
  3. package/microservices/microservice-social/src/db/migrations.ts +72 -0
  4. package/microservices/microservice-social/src/db/social.ts +33 -3
  5. package/microservices/microservice-social/src/lib/audience.ts +353 -0
  6. package/microservices/microservice-social/src/lib/content-ai.ts +278 -0
  7. package/microservices/microservice-social/src/lib/media.ts +311 -0
  8. package/microservices/microservice-social/src/lib/mentions.ts +434 -0
  9. package/microservices/microservice-social/src/lib/metrics-sync.ts +264 -0
  10. package/microservices/microservice-social/src/lib/publisher.ts +377 -0
  11. package/microservices/microservice-social/src/lib/scheduler.ts +229 -0
  12. package/microservices/microservice-social/src/lib/sentiment.ts +256 -0
  13. package/microservices/microservice-social/src/lib/threads.ts +291 -0
  14. package/microservices/microservice-social/src/mcp/index.ts +776 -6
  15. package/microservices/microservice-social/src/server/index.ts +441 -0
  16. package/microservices/microservice-transcriber/src/cli/index.ts +247 -1
  17. package/microservices/microservice-transcriber/src/db/comments.ts +166 -0
  18. package/microservices/microservice-transcriber/src/db/migrations.ts +46 -0
  19. package/microservices/microservice-transcriber/src/db/proofread.ts +119 -0
  20. package/microservices/microservice-transcriber/src/lib/downloader.ts +68 -0
  21. package/microservices/microservice-transcriber/src/lib/proofread.ts +296 -0
  22. package/microservices/microservice-transcriber/src/mcp/index.ts +263 -3
  23. package/package.json +1 -1
@@ -0,0 +1,377 @@
1
+ /**
2
+ * Platform publisher bridge — connects social microservice to real platform APIs.
3
+ *
4
+ * Supports X (Twitter) and Meta (Facebook Pages) via direct HTTP calls to their
5
+ * public APIs. No dependency on open-connectors at runtime — we replicate only
6
+ * the minimal fetch logic needed so the microservice stays self-contained.
7
+ */
8
+
9
+ import { getPost, getAccount, updatePost, type Post, type Engagement } from "../db/social.js";
10
+
11
+ // ---- Interfaces ----
12
+
13
+ export interface PublishResult {
14
+ platformPostId: string;
15
+ url?: string;
16
+ }
17
+
18
+ export interface PostMetrics {
19
+ likes: number;
20
+ shares: number;
21
+ comments: number;
22
+ impressions: number;
23
+ clicks: number;
24
+ }
25
+
26
+ export interface PlatformPublisher {
27
+ /** Publish content to the platform. Returns the platform-native post ID. */
28
+ publishPost(content: string, mediaIds?: string[]): Promise<PublishResult>;
29
+ /** Delete a post by its platform-native ID. */
30
+ deletePost(platformPostId: string): Promise<boolean>;
31
+ /** Fetch engagement metrics for a published post. */
32
+ getPostMetrics(platformPostId: string): Promise<PostMetrics>;
33
+ }
34
+
35
+ // ---- X (Twitter) Publisher ----
36
+
37
+ interface XAuthConfig {
38
+ bearerToken?: string;
39
+ apiKey?: string;
40
+ apiSecret?: string;
41
+ accessToken?: string;
42
+ accessSecret?: string;
43
+ }
44
+
45
+ function getXAuth(): XAuthConfig {
46
+ return {
47
+ bearerToken: process.env.X_BEARER_TOKEN,
48
+ apiKey: process.env.X_API_KEY,
49
+ apiSecret: process.env.X_API_SECRET,
50
+ accessToken: process.env.X_ACCESS_TOKEN,
51
+ accessSecret: process.env.X_ACCESS_TOKEN_SECRET,
52
+ };
53
+ }
54
+
55
+ export class XPublisher implements PlatformPublisher {
56
+ private readonly baseUrl = "https://api.twitter.com";
57
+ private auth: XAuthConfig;
58
+
59
+ constructor(auth?: XAuthConfig) {
60
+ this.auth = auth || getXAuth();
61
+ }
62
+
63
+ private getAuthHeader(): string {
64
+ if (this.auth.bearerToken) {
65
+ return `Bearer ${this.auth.bearerToken}`;
66
+ }
67
+ // OAuth 2.0 user access token
68
+ if (this.auth.accessToken) {
69
+ return `Bearer ${this.auth.accessToken}`;
70
+ }
71
+ throw new Error("X publisher: no valid auth token. Set X_BEARER_TOKEN or X_ACCESS_TOKEN.");
72
+ }
73
+
74
+ async publishPost(content: string, mediaIds?: string[]): Promise<PublishResult> {
75
+ const body: Record<string, unknown> = { text: content };
76
+ if (mediaIds?.length) {
77
+ body.media = { media_ids: mediaIds };
78
+ }
79
+
80
+ const res = await fetch(`${this.baseUrl}/2/tweets`, {
81
+ method: "POST",
82
+ headers: {
83
+ Authorization: this.getAuthHeader(),
84
+ "Content-Type": "application/json",
85
+ },
86
+ body: JSON.stringify(body),
87
+ });
88
+
89
+ if (!res.ok) {
90
+ const text = await res.text();
91
+ throw new Error(`X API error ${res.status}: ${text}`);
92
+ }
93
+
94
+ const data = (await res.json()) as { data: { id: string; text: string } };
95
+ return {
96
+ platformPostId: data.data.id,
97
+ url: `https://x.com/i/status/${data.data.id}`,
98
+ };
99
+ }
100
+
101
+ async deletePost(platformPostId: string): Promise<boolean> {
102
+ const res = await fetch(`${this.baseUrl}/2/tweets/${platformPostId}`, {
103
+ method: "DELETE",
104
+ headers: { Authorization: this.getAuthHeader() },
105
+ });
106
+
107
+ if (!res.ok) {
108
+ const text = await res.text();
109
+ throw new Error(`X API error ${res.status}: ${text}`);
110
+ }
111
+
112
+ const data = (await res.json()) as { data: { deleted: boolean } };
113
+ return data.data.deleted;
114
+ }
115
+
116
+ async getPostMetrics(platformPostId: string): Promise<PostMetrics> {
117
+ const res = await fetch(
118
+ `${this.baseUrl}/2/tweets/${platformPostId}?tweet.fields=public_metrics`,
119
+ {
120
+ method: "GET",
121
+ headers: { Authorization: this.getAuthHeader() },
122
+ }
123
+ );
124
+
125
+ if (!res.ok) {
126
+ const text = await res.text();
127
+ throw new Error(`X API error ${res.status}: ${text}`);
128
+ }
129
+
130
+ const data = (await res.json()) as {
131
+ data: {
132
+ public_metrics: {
133
+ like_count: number;
134
+ retweet_count: number;
135
+ reply_count: number;
136
+ impression_count: number;
137
+ bookmark_count: number;
138
+ };
139
+ };
140
+ };
141
+
142
+ const m = data.data.public_metrics;
143
+ return {
144
+ likes: m.like_count,
145
+ shares: m.retweet_count,
146
+ comments: m.reply_count,
147
+ impressions: m.impression_count,
148
+ clicks: m.bookmark_count, // X doesn't expose link clicks via v2; bookmark is closest proxy
149
+ };
150
+ }
151
+ }
152
+
153
+ // ---- Meta (Facebook Pages) Publisher ----
154
+
155
+ interface MetaAuthConfig {
156
+ accessToken?: string;
157
+ pageId?: string;
158
+ }
159
+
160
+ function getMetaAuth(): MetaAuthConfig {
161
+ return {
162
+ accessToken: process.env.META_ACCESS_TOKEN,
163
+ pageId: process.env.META_PAGE_ID,
164
+ };
165
+ }
166
+
167
+ export class MetaPublisher implements PlatformPublisher {
168
+ private readonly baseUrl = "https://graph.facebook.com/v22.0";
169
+ private auth: MetaAuthConfig;
170
+
171
+ constructor(auth?: MetaAuthConfig) {
172
+ this.auth = auth || getMetaAuth();
173
+ }
174
+
175
+ private requireAuth(): { accessToken: string; pageId: string } {
176
+ if (!this.auth.accessToken) {
177
+ throw new Error("Meta publisher: META_ACCESS_TOKEN is required.");
178
+ }
179
+ if (!this.auth.pageId) {
180
+ throw new Error("Meta publisher: META_PAGE_ID is required.");
181
+ }
182
+ return { accessToken: this.auth.accessToken, pageId: this.auth.pageId };
183
+ }
184
+
185
+ async publishPost(content: string, _mediaIds?: string[]): Promise<PublishResult> {
186
+ const { accessToken, pageId } = this.requireAuth();
187
+
188
+ const res = await fetch(
189
+ `${this.baseUrl}/${pageId}/feed?access_token=${encodeURIComponent(accessToken)}`,
190
+ {
191
+ method: "POST",
192
+ headers: { "Content-Type": "application/json" },
193
+ body: JSON.stringify({ message: content }),
194
+ }
195
+ );
196
+
197
+ if (!res.ok) {
198
+ const text = await res.text();
199
+ throw new Error(`Meta API error ${res.status}: ${text}`);
200
+ }
201
+
202
+ const data = (await res.json()) as { id: string };
203
+ return {
204
+ platformPostId: data.id,
205
+ url: `https://facebook.com/${data.id}`,
206
+ };
207
+ }
208
+
209
+ async deletePost(platformPostId: string): Promise<boolean> {
210
+ const { accessToken } = this.requireAuth();
211
+
212
+ const res = await fetch(
213
+ `${this.baseUrl}/${platformPostId}?access_token=${encodeURIComponent(accessToken)}`,
214
+ { method: "DELETE" }
215
+ );
216
+
217
+ if (!res.ok) {
218
+ const text = await res.text();
219
+ throw new Error(`Meta API error ${res.status}: ${text}`);
220
+ }
221
+
222
+ const data = (await res.json()) as { success: boolean };
223
+ return data.success;
224
+ }
225
+
226
+ async getPostMetrics(platformPostId: string): Promise<PostMetrics> {
227
+ const { accessToken } = this.requireAuth();
228
+
229
+ const fields = "shares,reactions.summary(true),comments.summary(true),insights.metric(post_impressions,post_clicks)";
230
+ const res = await fetch(
231
+ `${this.baseUrl}/${platformPostId}?fields=${encodeURIComponent(fields)}&access_token=${encodeURIComponent(accessToken)}`,
232
+ { method: "GET" }
233
+ );
234
+
235
+ if (!res.ok) {
236
+ const text = await res.text();
237
+ throw new Error(`Meta API error ${res.status}: ${text}`);
238
+ }
239
+
240
+ const data = (await res.json()) as {
241
+ shares?: { count: number };
242
+ reactions?: { summary: { total_count: number } };
243
+ comments?: { summary: { total_count: number } };
244
+ insights?: { data: { name: string; values: { value: number }[] }[] };
245
+ };
246
+
247
+ let impressions = 0;
248
+ let clicks = 0;
249
+ if (data.insights?.data) {
250
+ for (const metric of data.insights.data) {
251
+ if (metric.name === "post_impressions" && metric.values?.[0]) {
252
+ impressions = metric.values[0].value;
253
+ }
254
+ if (metric.name === "post_clicks" && metric.values?.[0]) {
255
+ clicks = metric.values[0].value;
256
+ }
257
+ }
258
+ }
259
+
260
+ return {
261
+ likes: data.reactions?.summary?.total_count ?? 0,
262
+ shares: data.shares?.count ?? 0,
263
+ comments: data.comments?.summary?.total_count ?? 0,
264
+ impressions,
265
+ clicks,
266
+ };
267
+ }
268
+ }
269
+
270
+ // ---- Factory ----
271
+
272
+ const publishers: Record<string, () => PlatformPublisher> = {
273
+ x: () => new XPublisher(),
274
+ meta: () => new MetaPublisher(),
275
+ facebook: () => new MetaPublisher(),
276
+ };
277
+
278
+ /**
279
+ * Get a publisher instance for the given platform string.
280
+ * Throws if the platform is not supported.
281
+ */
282
+ export function getPublisher(platform: string): PlatformPublisher {
283
+ const factory = publishers[platform.toLowerCase()];
284
+ if (!factory) {
285
+ throw new Error(`Unsupported publishing platform: '${platform}'. Supported: ${Object.keys(publishers).join(", ")}`);
286
+ }
287
+ return factory();
288
+ }
289
+
290
+ /**
291
+ * Check which platform providers have env vars configured.
292
+ */
293
+ export function checkProviders(): { x: boolean; meta: boolean } {
294
+ const xAuth = getXAuth();
295
+ const metaAuth = getMetaAuth();
296
+
297
+ return {
298
+ x: !!(xAuth.bearerToken || (xAuth.accessToken)),
299
+ meta: !!(metaAuth.accessToken && metaAuth.pageId),
300
+ };
301
+ }
302
+
303
+ // ---- High-level publish function ----
304
+
305
+ /**
306
+ * Publish a post to its platform API.
307
+ *
308
+ * 1. Loads the post and its account from DB
309
+ * 2. Gets the appropriate publisher for the account's platform
310
+ * 3. Calls the platform API
311
+ * 4. Updates the post in DB with platform_post_id and status='published'
312
+ *
313
+ * Returns the updated post.
314
+ */
315
+ export async function publishToApi(postId: string): Promise<Post> {
316
+ const post = getPost(postId);
317
+ if (!post) {
318
+ throw new Error(`Post '${postId}' not found.`);
319
+ }
320
+
321
+ if (post.status === "published") {
322
+ throw new Error(`Post '${postId}' is already published.`);
323
+ }
324
+
325
+ const account = getAccount(post.account_id);
326
+ if (!account) {
327
+ throw new Error(`Account '${post.account_id}' not found for post '${postId}'.`);
328
+ }
329
+
330
+ // Map the social account platform to a publisher key
331
+ // instagram/threads/bluesky/linkedin don't have publishers yet
332
+ const platformKey = account.platform === "x" ? "x" : account.platform;
333
+ const publisher = getPublisher(platformKey);
334
+
335
+ try {
336
+ const result = await publisher.publishPost(post.content, post.media_urls.length ? post.media_urls : undefined);
337
+
338
+ const updated = updatePost(postId, {
339
+ status: "published",
340
+ published_at: new Date().toISOString().replace("T", " ").replace(/\.\d+Z$/, ""),
341
+ platform_post_id: result.platformPostId,
342
+ });
343
+
344
+ return updated!;
345
+ } catch (err) {
346
+ // Mark as failed with the error info
347
+ updatePost(postId, { status: "failed" });
348
+ throw err;
349
+ }
350
+ }
351
+
352
+ /**
353
+ * Fetch metrics from the platform API and sync them into the DB engagement column.
354
+ */
355
+ export async function syncPostMetrics(postId: string): Promise<Post> {
356
+ const post = getPost(postId);
357
+ if (!post) throw new Error(`Post '${postId}' not found.`);
358
+ if (!post.platform_post_id) throw new Error(`Post '${postId}' has no platform_post_id.`);
359
+
360
+ const account = getAccount(post.account_id);
361
+ if (!account) throw new Error(`Account '${post.account_id}' not found.`);
362
+
363
+ const platformKey = account.platform === "x" ? "x" : account.platform;
364
+ const publisher = getPublisher(platformKey);
365
+ const metrics = await publisher.getPostMetrics(post.platform_post_id);
366
+
367
+ const engagement: Engagement = {
368
+ likes: metrics.likes,
369
+ shares: metrics.shares,
370
+ comments: metrics.comments,
371
+ impressions: metrics.impressions,
372
+ clicks: metrics.clicks,
373
+ };
374
+
375
+ const updated = updatePost(postId, { engagement });
376
+ return updated!;
377
+ }
@@ -0,0 +1,229 @@
1
+ /**
2
+ * Scheduled post publishing worker
3
+ *
4
+ * Checks for posts where scheduled_at <= now AND status = 'scheduled',
5
+ * marks them as published, and handles recurrence by creating the next
6
+ * scheduled post.
7
+ */
8
+
9
+ import {
10
+ getPost,
11
+ listPosts,
12
+ updatePost,
13
+ createPost,
14
+ publishPost,
15
+ type Post,
16
+ type Recurrence,
17
+ } from "../db/social.js";
18
+
19
+ // ---- Types ----
20
+
21
+ export interface SchedulerStatus {
22
+ running: boolean;
23
+ lastCheck: string | null;
24
+ postsProcessed: number;
25
+ errors: number;
26
+ }
27
+
28
+ export interface ProcessResult {
29
+ postId: string;
30
+ published: boolean;
31
+ error?: string;
32
+ nextPostId?: string;
33
+ }
34
+
35
+ // ---- State ----
36
+
37
+ let _interval: ReturnType<typeof setInterval> | null = null;
38
+ let _status: SchedulerStatus = {
39
+ running: false,
40
+ lastCheck: null,
41
+ postsProcessed: 0,
42
+ errors: 0,
43
+ };
44
+
45
+ // ---- Core Functions ----
46
+
47
+ /**
48
+ * Find all posts where scheduled_at <= now AND status = 'scheduled'
49
+ */
50
+ export function getDuePosts(now?: Date): Post[] {
51
+ const currentTime = now || new Date();
52
+ const posts = listPosts({ status: "scheduled" });
53
+
54
+ return posts.filter((post) => {
55
+ if (!post.scheduled_at) return false;
56
+ const scheduledTime = new Date(post.scheduled_at.replace(" ", "T") + (post.scheduled_at.includes("Z") ? "" : "Z"));
57
+ return scheduledTime <= currentTime;
58
+ });
59
+ }
60
+
61
+ /**
62
+ * Attempt to publish a scheduled post.
63
+ * Marks the post as published locally. When a publisher module is available,
64
+ * it would call the platform API first.
65
+ */
66
+ export function processScheduledPost(postId: string): ProcessResult {
67
+ const post = getPost(postId);
68
+ if (!post) {
69
+ return { postId, published: false, error: `Post '${postId}' not found` };
70
+ }
71
+
72
+ if (post.status !== "scheduled") {
73
+ return { postId, published: false, error: `Post status is '${post.status}', expected 'scheduled'` };
74
+ }
75
+
76
+ try {
77
+ // Mark as published locally (future: call platform API via publisher.ts)
78
+ publishPost(postId);
79
+
80
+ const result: ProcessResult = { postId, published: true };
81
+
82
+ // Handle recurrence
83
+ if (post.recurrence) {
84
+ const nextPost = handleRecurrence(postId);
85
+ if (nextPost) {
86
+ result.nextPostId = nextPost.id;
87
+ }
88
+ }
89
+
90
+ return result;
91
+ } catch (err) {
92
+ // Mark as failed
93
+ updatePost(postId, { status: "failed" });
94
+ return {
95
+ postId,
96
+ published: false,
97
+ error: err instanceof Error ? err.message : String(err),
98
+ };
99
+ }
100
+ }
101
+
102
+ /**
103
+ * If post has recurrence (daily/weekly/biweekly/monthly), create the next
104
+ * scheduled post with the same content, account, tags, and media.
105
+ */
106
+ export function handleRecurrence(postId: string): Post | null {
107
+ const post = getPost(postId);
108
+ if (!post || !post.recurrence || !post.scheduled_at) return null;
109
+
110
+ const nextDate = computeNextDate(post.scheduled_at, post.recurrence);
111
+
112
+ const nextPost = createPost({
113
+ account_id: post.account_id,
114
+ content: post.content,
115
+ media_urls: post.media_urls.length > 0 ? post.media_urls : undefined,
116
+ status: "scheduled",
117
+ scheduled_at: nextDate,
118
+ tags: post.tags.length > 0 ? post.tags : undefined,
119
+ recurrence: post.recurrence,
120
+ });
121
+
122
+ return nextPost;
123
+ }
124
+
125
+ /**
126
+ * Compute the next scheduled date based on recurrence type
127
+ */
128
+ export function computeNextDate(scheduledAt: string, recurrence: Recurrence): string {
129
+ const date = new Date(scheduledAt.replace(" ", "T") + (scheduledAt.includes("Z") ? "" : "Z"));
130
+
131
+ switch (recurrence) {
132
+ case "daily":
133
+ date.setUTCDate(date.getUTCDate() + 1);
134
+ break;
135
+ case "weekly":
136
+ date.setUTCDate(date.getUTCDate() + 7);
137
+ break;
138
+ case "biweekly":
139
+ date.setUTCDate(date.getUTCDate() + 14);
140
+ break;
141
+ case "monthly":
142
+ date.setUTCMonth(date.getUTCMonth() + 1);
143
+ break;
144
+ }
145
+
146
+ // Format as YYYY-MM-DD HH:MM:SS to match the existing convention
147
+ const y = date.getUTCFullYear();
148
+ const m = String(date.getUTCMonth() + 1).padStart(2, "0");
149
+ const d = String(date.getUTCDate()).padStart(2, "0");
150
+ const hh = String(date.getUTCHours()).padStart(2, "0");
151
+ const mm = String(date.getUTCMinutes()).padStart(2, "0");
152
+ const ss = String(date.getUTCSeconds()).padStart(2, "0");
153
+
154
+ return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
155
+ }
156
+
157
+ // ---- Scheduler Loop ----
158
+
159
+ /**
160
+ * Run one check cycle: find due posts and process them.
161
+ * Returns an array of results.
162
+ */
163
+ export function runOnce(now?: Date): ProcessResult[] {
164
+ const duePosts = getDuePosts(now);
165
+ const results: ProcessResult[] = [];
166
+
167
+ for (const post of duePosts) {
168
+ const result = processScheduledPost(post.id);
169
+ results.push(result);
170
+
171
+ if (result.published) {
172
+ _status.postsProcessed++;
173
+ } else {
174
+ _status.errors++;
175
+ }
176
+ }
177
+
178
+ _status.lastCheck = new Date().toISOString();
179
+ return results;
180
+ }
181
+
182
+ /**
183
+ * Start the scheduler loop that checks for due posts at the given interval.
184
+ */
185
+ export function startScheduler(intervalMs: number = 60000): void {
186
+ if (_interval) {
187
+ throw new Error("Scheduler is already running. Stop it first.");
188
+ }
189
+
190
+ _status.running = true;
191
+ _status.lastCheck = new Date().toISOString();
192
+
193
+ // Run immediately on start
194
+ runOnce();
195
+
196
+ _interval = setInterval(() => {
197
+ runOnce();
198
+ }, intervalMs);
199
+ }
200
+
201
+ /**
202
+ * Stop the scheduler loop.
203
+ */
204
+ export function stopScheduler(): void {
205
+ if (_interval) {
206
+ clearInterval(_interval);
207
+ _interval = null;
208
+ }
209
+ _status.running = false;
210
+ }
211
+
212
+ /**
213
+ * Get the current scheduler status.
214
+ */
215
+ export function getSchedulerStatus(): SchedulerStatus {
216
+ return { ..._status };
217
+ }
218
+
219
+ /**
220
+ * Reset scheduler status counters (useful for testing).
221
+ */
222
+ export function resetSchedulerStatus(): void {
223
+ _status = {
224
+ running: _interval !== null,
225
+ lastCheck: null,
226
+ postsProcessed: 0,
227
+ errors: 0,
228
+ };
229
+ }