@hasna/microservices 0.0.15 → 0.0.17

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 (296) hide show
  1. package/README.md +120 -138
  2. package/bin/index.js +703 -12728
  3. package/bin/mcp.js +250 -9726
  4. package/dist/index.js +170 -9407
  5. package/package.json +25 -25
  6. package/microservices/microservice-ads/package.json +0 -27
  7. package/microservices/microservice-ads/src/cli/index.ts +0 -605
  8. package/microservices/microservice-ads/src/db/campaigns.ts +0 -797
  9. package/microservices/microservice-ads/src/db/database.ts +0 -93
  10. package/microservices/microservice-ads/src/db/migrations.ts +0 -60
  11. package/microservices/microservice-ads/src/index.ts +0 -39
  12. package/microservices/microservice-ads/src/mcp/index.ts +0 -480
  13. package/microservices/microservice-analytics/package.json +0 -27
  14. package/microservices/microservice-analytics/src/cli/index.ts +0 -373
  15. package/microservices/microservice-analytics/src/db/analytics.ts +0 -564
  16. package/microservices/microservice-analytics/src/db/database.ts +0 -93
  17. package/microservices/microservice-analytics/src/db/migrations.ts +0 -50
  18. package/microservices/microservice-analytics/src/index.ts +0 -37
  19. package/microservices/microservice-analytics/src/mcp/index.ts +0 -334
  20. package/microservices/microservice-assets/package.json +0 -27
  21. package/microservices/microservice-assets/src/cli/index.ts +0 -375
  22. package/microservices/microservice-assets/src/db/assets.ts +0 -370
  23. package/microservices/microservice-assets/src/db/database.ts +0 -93
  24. package/microservices/microservice-assets/src/db/migrations.ts +0 -51
  25. package/microservices/microservice-assets/src/index.ts +0 -32
  26. package/microservices/microservice-assets/src/mcp/index.ts +0 -346
  27. package/microservices/microservice-bookkeeping/package.json +0 -27
  28. package/microservices/microservice-bookkeeping/src/cli/index.ts +0 -386
  29. package/microservices/microservice-bookkeeping/src/db/bookkeeping.ts +0 -591
  30. package/microservices/microservice-bookkeeping/src/db/database.ts +0 -93
  31. package/microservices/microservice-bookkeeping/src/db/migrations.ts +0 -52
  32. package/microservices/microservice-bookkeeping/src/index.ts +0 -32
  33. package/microservices/microservice-bookkeeping/src/mcp/index.ts +0 -284
  34. package/microservices/microservice-calendar/package.json +0 -27
  35. package/microservices/microservice-calendar/src/cli/index.ts +0 -287
  36. package/microservices/microservice-calendar/src/db/calendar.ts +0 -328
  37. package/microservices/microservice-calendar/src/db/database.ts +0 -82
  38. package/microservices/microservice-calendar/src/db/migrations.ts +0 -47
  39. package/microservices/microservice-calendar/src/index.ts +0 -24
  40. package/microservices/microservice-calendar/src/mcp/index.ts +0 -226
  41. package/microservices/microservice-company/package.json +0 -27
  42. package/microservices/microservice-company/src/cli/index.ts +0 -1126
  43. package/microservices/microservice-company/src/db/company.ts +0 -854
  44. package/microservices/microservice-company/src/db/database.ts +0 -93
  45. package/microservices/microservice-company/src/db/migrations.ts +0 -214
  46. package/microservices/microservice-company/src/db/workflow-migrations.ts +0 -44
  47. package/microservices/microservice-company/src/index.ts +0 -60
  48. package/microservices/microservice-company/src/lib/audit.ts +0 -168
  49. package/microservices/microservice-company/src/lib/finance.ts +0 -299
  50. package/microservices/microservice-company/src/lib/settings.ts +0 -85
  51. package/microservices/microservice-company/src/lib/workflows.ts +0 -698
  52. package/microservices/microservice-company/src/mcp/index.ts +0 -991
  53. package/microservices/microservice-compliance/package.json +0 -27
  54. package/microservices/microservice-compliance/src/cli/index.ts +0 -467
  55. package/microservices/microservice-compliance/src/db/compliance.ts +0 -633
  56. package/microservices/microservice-compliance/src/db/database.ts +0 -93
  57. package/microservices/microservice-compliance/src/db/migrations.ts +0 -63
  58. package/microservices/microservice-compliance/src/index.ts +0 -46
  59. package/microservices/microservice-compliance/src/mcp/index.ts +0 -438
  60. package/microservices/microservice-contacts/package.json +0 -27
  61. package/microservices/microservice-contacts/src/cli/index.ts +0 -393
  62. package/microservices/microservice-contacts/src/db/companies.ts +0 -167
  63. package/microservices/microservice-contacts/src/db/contacts.ts +0 -249
  64. package/microservices/microservice-contacts/src/db/database.ts +0 -93
  65. package/microservices/microservice-contacts/src/db/migrations.ts +0 -71
  66. package/microservices/microservice-contacts/src/db/relationships.ts +0 -53
  67. package/microservices/microservice-contacts/src/index.ts +0 -42
  68. package/microservices/microservice-contacts/src/mcp/index.ts +0 -303
  69. package/microservices/microservice-contracts/package.json +0 -27
  70. package/microservices/microservice-contracts/src/cli/index.ts +0 -770
  71. package/microservices/microservice-contracts/src/db/contracts.ts +0 -925
  72. package/microservices/microservice-contracts/src/db/database.ts +0 -93
  73. package/microservices/microservice-contracts/src/db/migrations.ts +0 -141
  74. package/microservices/microservice-contracts/src/index.ts +0 -43
  75. package/microservices/microservice-contracts/src/mcp/index.ts +0 -617
  76. package/microservices/microservice-crm/package.json +0 -27
  77. package/microservices/microservice-crm/src/cli/index.ts +0 -396
  78. package/microservices/microservice-crm/src/db/database.ts +0 -82
  79. package/microservices/microservice-crm/src/db/migrations.ts +0 -66
  80. package/microservices/microservice-crm/src/db/pipeline.ts +0 -397
  81. package/microservices/microservice-crm/src/index.ts +0 -34
  82. package/microservices/microservice-crm/src/mcp/index.ts +0 -294
  83. package/microservices/microservice-documents/package.json +0 -27
  84. package/microservices/microservice-documents/src/cli/index.ts +0 -246
  85. package/microservices/microservice-documents/src/db/database.ts +0 -82
  86. package/microservices/microservice-documents/src/db/documents.ts +0 -316
  87. package/microservices/microservice-documents/src/db/migrations.ts +0 -49
  88. package/microservices/microservice-documents/src/index.ts +0 -24
  89. package/microservices/microservice-documents/src/mcp/index.ts +0 -202
  90. package/microservices/microservice-domains/package.json +0 -27
  91. package/microservices/microservice-domains/src/cli/index.ts +0 -1111
  92. package/microservices/microservice-domains/src/db/database.ts +0 -93
  93. package/microservices/microservice-domains/src/db/domains.ts +0 -1164
  94. package/microservices/microservice-domains/src/db/migrations.ts +0 -60
  95. package/microservices/microservice-domains/src/index.ts +0 -65
  96. package/microservices/microservice-domains/src/lib/brandsight.ts +0 -350
  97. package/microservices/microservice-domains/src/lib/godaddy.ts +0 -338
  98. package/microservices/microservice-domains/src/lib/namecheap.ts +0 -262
  99. package/microservices/microservice-domains/src/lib/registrar.ts +0 -355
  100. package/microservices/microservice-domains/src/mcp/index.ts +0 -781
  101. package/microservices/microservice-expenses/package.json +0 -27
  102. package/microservices/microservice-expenses/src/cli/index.ts +0 -267
  103. package/microservices/microservice-expenses/src/db/database.ts +0 -82
  104. package/microservices/microservice-expenses/src/db/expenses.ts +0 -345
  105. package/microservices/microservice-expenses/src/db/migrations.ts +0 -45
  106. package/microservices/microservice-expenses/src/index.ts +0 -25
  107. package/microservices/microservice-expenses/src/mcp/index.ts +0 -196
  108. package/microservices/microservice-habits/package.json +0 -27
  109. package/microservices/microservice-habits/src/cli/index.ts +0 -315
  110. package/microservices/microservice-habits/src/db/database.ts +0 -93
  111. package/microservices/microservice-habits/src/db/habits.ts +0 -451
  112. package/microservices/microservice-habits/src/db/migrations.ts +0 -46
  113. package/microservices/microservice-habits/src/index.ts +0 -31
  114. package/microservices/microservice-habits/src/mcp/index.ts +0 -313
  115. package/microservices/microservice-health/package.json +0 -27
  116. package/microservices/microservice-health/src/cli/index.ts +0 -484
  117. package/microservices/microservice-health/src/db/database.ts +0 -93
  118. package/microservices/microservice-health/src/db/health.ts +0 -708
  119. package/microservices/microservice-health/src/db/migrations.ts +0 -70
  120. package/microservices/microservice-health/src/index.ts +0 -63
  121. package/microservices/microservice-health/src/mcp/index.ts +0 -437
  122. package/microservices/microservice-hiring/package.json +0 -27
  123. package/microservices/microservice-hiring/src/cli/index.ts +0 -741
  124. package/microservices/microservice-hiring/src/db/database.ts +0 -93
  125. package/microservices/microservice-hiring/src/db/hiring.ts +0 -1085
  126. package/microservices/microservice-hiring/src/db/migrations.ts +0 -89
  127. package/microservices/microservice-hiring/src/index.ts +0 -80
  128. package/microservices/microservice-hiring/src/lib/scoring.ts +0 -206
  129. package/microservices/microservice-hiring/src/mcp/index.ts +0 -709
  130. package/microservices/microservice-inventory/package.json +0 -27
  131. package/microservices/microservice-inventory/src/cli/index.ts +0 -365
  132. package/microservices/microservice-inventory/src/db/database.ts +0 -82
  133. package/microservices/microservice-inventory/src/db/inventory.ts +0 -393
  134. package/microservices/microservice-inventory/src/db/migrations.ts +0 -54
  135. package/microservices/microservice-inventory/src/index.ts +0 -28
  136. package/microservices/microservice-inventory/src/mcp/index.ts +0 -250
  137. package/microservices/microservice-invoices/dashboard/dist/assets/index-Bngq7FNM.css +0 -1
  138. package/microservices/microservice-invoices/dashboard/dist/assets/index-aHW4ARZR.js +0 -124
  139. package/microservices/microservice-invoices/dashboard/dist/index.html +0 -13
  140. package/microservices/microservice-invoices/dashboard/index.html +0 -12
  141. package/microservices/microservice-invoices/dashboard/package.json +0 -29
  142. package/microservices/microservice-invoices/dashboard/tsconfig.json +0 -14
  143. package/microservices/microservice-invoices/dashboard/vite.config.ts +0 -15
  144. package/microservices/microservice-invoices/package.json +0 -30
  145. package/microservices/microservice-invoices/src/cli/index.ts +0 -308
  146. package/microservices/microservice-invoices/src/db/business.ts +0 -241
  147. package/microservices/microservice-invoices/src/db/clients.ts +0 -127
  148. package/microservices/microservice-invoices/src/db/database.ts +0 -82
  149. package/microservices/microservice-invoices/src/db/invoices.ts +0 -345
  150. package/microservices/microservice-invoices/src/db/migrations.ts +0 -184
  151. package/microservices/microservice-invoices/src/index.ts +0 -56
  152. package/microservices/microservice-invoices/src/mcp/index.ts +0 -242
  153. package/microservices/microservice-invoices/src/server/index.ts +0 -162
  154. package/microservices/microservice-leads/package.json +0 -27
  155. package/microservices/microservice-leads/src/cli/index.ts +0 -596
  156. package/microservices/microservice-leads/src/db/database.ts +0 -93
  157. package/microservices/microservice-leads/src/db/leads.ts +0 -520
  158. package/microservices/microservice-leads/src/db/lists.ts +0 -151
  159. package/microservices/microservice-leads/src/db/migrations.ts +0 -93
  160. package/microservices/microservice-leads/src/index.ts +0 -65
  161. package/microservices/microservice-leads/src/lib/enrichment.ts +0 -202
  162. package/microservices/microservice-leads/src/lib/scoring.ts +0 -134
  163. package/microservices/microservice-leads/src/mcp/index.ts +0 -533
  164. package/microservices/microservice-notes/package.json +0 -27
  165. package/microservices/microservice-notes/src/cli/index.ts +0 -63
  166. package/microservices/microservice-notes/src/db/database.ts +0 -93
  167. package/microservices/microservice-notes/src/db/migrations.ts +0 -40
  168. package/microservices/microservice-notes/src/db/notes.ts +0 -114
  169. package/microservices/microservice-notes/src/index.ts +0 -2
  170. package/microservices/microservice-notes/src/mcp/index.ts +0 -37
  171. package/microservices/microservice-notifications/package.json +0 -27
  172. package/microservices/microservice-notifications/src/cli/index.ts +0 -349
  173. package/microservices/microservice-notifications/src/db/database.ts +0 -93
  174. package/microservices/microservice-notifications/src/db/migrations.ts +0 -62
  175. package/microservices/microservice-notifications/src/db/notifications.ts +0 -509
  176. package/microservices/microservice-notifications/src/index.ts +0 -41
  177. package/microservices/microservice-notifications/src/mcp/index.ts +0 -422
  178. package/microservices/microservice-payments/package.json +0 -27
  179. package/microservices/microservice-payments/src/cli/index.ts +0 -609
  180. package/microservices/microservice-payments/src/db/database.ts +0 -93
  181. package/microservices/microservice-payments/src/db/migrations.ts +0 -81
  182. package/microservices/microservice-payments/src/db/payments.ts +0 -1204
  183. package/microservices/microservice-payments/src/index.ts +0 -51
  184. package/microservices/microservice-payments/src/mcp/index.ts +0 -683
  185. package/microservices/microservice-payroll/package.json +0 -27
  186. package/microservices/microservice-payroll/src/cli/index.ts +0 -643
  187. package/microservices/microservice-payroll/src/db/database.ts +0 -93
  188. package/microservices/microservice-payroll/src/db/migrations.ts +0 -95
  189. package/microservices/microservice-payroll/src/db/payroll.ts +0 -1377
  190. package/microservices/microservice-payroll/src/index.ts +0 -48
  191. package/microservices/microservice-payroll/src/mcp/index.ts +0 -666
  192. package/microservices/microservice-products/package.json +0 -27
  193. package/microservices/microservice-products/src/cli/index.ts +0 -416
  194. package/microservices/microservice-products/src/db/categories.ts +0 -154
  195. package/microservices/microservice-products/src/db/database.ts +0 -93
  196. package/microservices/microservice-products/src/db/migrations.ts +0 -58
  197. package/microservices/microservice-products/src/db/pricing-tiers.ts +0 -66
  198. package/microservices/microservice-products/src/db/products.ts +0 -452
  199. package/microservices/microservice-products/src/index.ts +0 -53
  200. package/microservices/microservice-products/src/mcp/index.ts +0 -453
  201. package/microservices/microservice-projects/package.json +0 -27
  202. package/microservices/microservice-projects/src/cli/index.ts +0 -480
  203. package/microservices/microservice-projects/src/db/database.ts +0 -93
  204. package/microservices/microservice-projects/src/db/migrations.ts +0 -65
  205. package/microservices/microservice-projects/src/db/projects.ts +0 -715
  206. package/microservices/microservice-projects/src/index.ts +0 -57
  207. package/microservices/microservice-projects/src/mcp/index.ts +0 -501
  208. package/microservices/microservice-proposals/package.json +0 -27
  209. package/microservices/microservice-proposals/src/cli/index.ts +0 -400
  210. package/microservices/microservice-proposals/src/db/database.ts +0 -93
  211. package/microservices/microservice-proposals/src/db/migrations.ts +0 -52
  212. package/microservices/microservice-proposals/src/db/proposals.ts +0 -532
  213. package/microservices/microservice-proposals/src/index.ts +0 -37
  214. package/microservices/microservice-proposals/src/mcp/index.ts +0 -375
  215. package/microservices/microservice-reading/package.json +0 -27
  216. package/microservices/microservice-reading/src/cli/index.ts +0 -464
  217. package/microservices/microservice-reading/src/db/database.ts +0 -93
  218. package/microservices/microservice-reading/src/db/migrations.ts +0 -59
  219. package/microservices/microservice-reading/src/db/reading.ts +0 -524
  220. package/microservices/microservice-reading/src/index.ts +0 -51
  221. package/microservices/microservice-reading/src/mcp/index.ts +0 -368
  222. package/microservices/microservice-shipping/package.json +0 -27
  223. package/microservices/microservice-shipping/src/cli/index.ts +0 -606
  224. package/microservices/microservice-shipping/src/db/database.ts +0 -93
  225. package/microservices/microservice-shipping/src/db/migrations.ts +0 -69
  226. package/microservices/microservice-shipping/src/db/shipping.ts +0 -1093
  227. package/microservices/microservice-shipping/src/index.ts +0 -53
  228. package/microservices/microservice-shipping/src/mcp/index.ts +0 -533
  229. package/microservices/microservice-social/package.json +0 -28
  230. package/microservices/microservice-social/src/cli/index.ts +0 -1583
  231. package/microservices/microservice-social/src/db/database.ts +0 -93
  232. package/microservices/microservice-social/src/db/migrations.ts +0 -160
  233. package/microservices/microservice-social/src/db/social.ts +0 -1076
  234. package/microservices/microservice-social/src/index.ts +0 -46
  235. package/microservices/microservice-social/src/lib/audience.ts +0 -353
  236. package/microservices/microservice-social/src/lib/content-ai.ts +0 -278
  237. package/microservices/microservice-social/src/lib/media.ts +0 -311
  238. package/microservices/microservice-social/src/lib/mentions.ts +0 -434
  239. package/microservices/microservice-social/src/lib/metrics-sync.ts +0 -264
  240. package/microservices/microservice-social/src/lib/publisher.ts +0 -377
  241. package/microservices/microservice-social/src/lib/scheduler.ts +0 -229
  242. package/microservices/microservice-social/src/lib/sentiment.ts +0 -256
  243. package/microservices/microservice-social/src/lib/threads.ts +0 -291
  244. package/microservices/microservice-social/src/mcp/index.ts +0 -1425
  245. package/microservices/microservice-social/src/server/index.ts +0 -441
  246. package/microservices/microservice-subscriptions/package.json +0 -27
  247. package/microservices/microservice-subscriptions/src/cli/index.ts +0 -715
  248. package/microservices/microservice-subscriptions/src/db/database.ts +0 -93
  249. package/microservices/microservice-subscriptions/src/db/migrations.ts +0 -125
  250. package/microservices/microservice-subscriptions/src/db/subscriptions.ts +0 -1256
  251. package/microservices/microservice-subscriptions/src/index.ts +0 -41
  252. package/microservices/microservice-subscriptions/src/mcp/index.ts +0 -631
  253. package/microservices/microservice-timesheets/package.json +0 -27
  254. package/microservices/microservice-timesheets/src/cli/index.ts +0 -373
  255. package/microservices/microservice-timesheets/src/db/database.ts +0 -82
  256. package/microservices/microservice-timesheets/src/db/locale.ts +0 -217
  257. package/microservices/microservice-timesheets/src/db/migrations.ts +0 -74
  258. package/microservices/microservice-timesheets/src/db/timesheets.ts +0 -447
  259. package/microservices/microservice-timesheets/src/index.ts +0 -44
  260. package/microservices/microservice-timesheets/src/mcp/index.ts +0 -269
  261. package/microservices/microservice-transcriber/package.json +0 -28
  262. package/microservices/microservice-transcriber/src/cli/index.ts +0 -1593
  263. package/microservices/microservice-transcriber/src/db/annotations.ts +0 -37
  264. package/microservices/microservice-transcriber/src/db/comments.ts +0 -166
  265. package/microservices/microservice-transcriber/src/db/database.ts +0 -82
  266. package/microservices/microservice-transcriber/src/db/migrations.ts +0 -118
  267. package/microservices/microservice-transcriber/src/db/proofread.ts +0 -119
  268. package/microservices/microservice-transcriber/src/db/transcripts.ts +0 -395
  269. package/microservices/microservice-transcriber/src/index.ts +0 -43
  270. package/microservices/microservice-transcriber/src/lib/config.ts +0 -77
  271. package/microservices/microservice-transcriber/src/lib/diff.ts +0 -91
  272. package/microservices/microservice-transcriber/src/lib/downloader.ts +0 -638
  273. package/microservices/microservice-transcriber/src/lib/feeds.ts +0 -62
  274. package/microservices/microservice-transcriber/src/lib/live.ts +0 -94
  275. package/microservices/microservice-transcriber/src/lib/notion.ts +0 -129
  276. package/microservices/microservice-transcriber/src/lib/proofread.ts +0 -296
  277. package/microservices/microservice-transcriber/src/lib/providers.ts +0 -713
  278. package/microservices/microservice-transcriber/src/lib/summarizer.ts +0 -147
  279. package/microservices/microservice-transcriber/src/lib/translator.ts +0 -75
  280. package/microservices/microservice-transcriber/src/lib/webhook.ts +0 -37
  281. package/microservices/microservice-transcriber/src/mcp/index.ts +0 -1330
  282. package/microservices/microservice-transcriber/src/server/index.ts +0 -199
  283. package/microservices/microservice-travel/package.json +0 -27
  284. package/microservices/microservice-travel/src/cli/index.ts +0 -505
  285. package/microservices/microservice-travel/src/db/database.ts +0 -93
  286. package/microservices/microservice-travel/src/db/migrations.ts +0 -77
  287. package/microservices/microservice-travel/src/db/travel.ts +0 -802
  288. package/microservices/microservice-travel/src/index.ts +0 -60
  289. package/microservices/microservice-travel/src/mcp/index.ts +0 -495
  290. package/microservices/microservice-wiki/package.json +0 -27
  291. package/microservices/microservice-wiki/src/cli/index.ts +0 -345
  292. package/microservices/microservice-wiki/src/db/database.ts +0 -93
  293. package/microservices/microservice-wiki/src/db/migrations.ts +0 -55
  294. package/microservices/microservice-wiki/src/db/wiki.ts +0 -395
  295. package/microservices/microservice-wiki/src/index.ts +0 -32
  296. package/microservices/microservice-wiki/src/mcp/index.ts +0 -344
@@ -1,256 +0,0 @@
1
- /**
2
- * Sentiment analysis for social media mentions.
3
- * Uses OpenAI (gpt-4o-mini) or Anthropic (claude-haiku) to classify text sentiment,
4
- * extract emotional keywords, and generate aggregated sentiment reports.
5
- */
6
-
7
- import { getDatabase } from "../db/database.js";
8
- import { getMention, type Mention } from "./mentions.js";
9
- import {
10
- callOpenAI,
11
- callAnthropic,
12
- getDefaultAIProvider,
13
- type AIProvider,
14
- } from "./content-ai.js";
15
-
16
- // ---- Types ----
17
-
18
- export type SentimentLabel = "positive" | "neutral" | "negative";
19
-
20
- export interface SentimentResult {
21
- sentiment: SentimentLabel;
22
- score: number;
23
- keywords: string[];
24
- }
25
-
26
- export interface SentimentReport {
27
- positive_pct: number;
28
- neutral_pct: number;
29
- negative_pct: number;
30
- total_analyzed: number;
31
- trending_keywords: string[];
32
- most_positive: { id: string; content: string; sentiment: string } | null;
33
- most_negative: { id: string; content: string; sentiment: string } | null;
34
- }
35
-
36
- // ---- Prompt Builders (exported for testing) ----
37
-
38
- export function buildSentimentPrompt(text: string): string {
39
- return `Analyze the sentiment of the following text.
40
-
41
- Return ONLY valid JSON with these fields:
42
- - "sentiment": one of "positive", "neutral", or "negative"
43
- - "score": a number between 0 and 1 where 0 is most negative, 0.5 is neutral, 1 is most positive
44
- - "keywords": array of key emotional or sentiment-bearing words from the text (max 5)
45
-
46
- Text: ${text}`;
47
- }
48
-
49
- export function buildBatchSentimentPrompt(texts: string[]): string {
50
- const numbered = texts.map((t, i) => `${i + 1}. ${t}`).join("\n");
51
- return `Analyze the sentiment of each of the following texts.
52
-
53
- Return ONLY a valid JSON array where each element has:
54
- - "sentiment": one of "positive", "neutral", or "negative"
55
- - "score": a number between 0 and 1 where 0 is most negative, 0.5 is neutral, 1 is most positive
56
- - "keywords": array of key emotional or sentiment-bearing words (max 5 per text)
57
-
58
- The array must have exactly ${texts.length} elements, one per text, in order.
59
-
60
- Texts:
61
- ${numbered}`;
62
- }
63
-
64
- // ---- JSON Parser ----
65
-
66
- function parseJSON<T>(raw: string, fallback: T): T {
67
- const cleaned = raw.replace(/```json\n?/g, "").replace(/```\n?/g, "").trim();
68
- try {
69
- return JSON.parse(cleaned) as T;
70
- } catch {
71
- return fallback;
72
- }
73
- }
74
-
75
- function validateSentimentResult(result: SentimentResult): SentimentResult {
76
- const validSentiments: SentimentLabel[] = ["positive", "neutral", "negative"];
77
- if (!validSentiments.includes(result.sentiment)) {
78
- result.sentiment = "neutral";
79
- }
80
- if (typeof result.score !== "number" || result.score < 0 || result.score > 1) {
81
- result.score = 0.5;
82
- }
83
- if (!Array.isArray(result.keywords)) {
84
- result.keywords = [];
85
- }
86
- return result;
87
- }
88
-
89
- // ---- Core Functions ----
90
-
91
- export async function analyzeSentiment(
92
- text: string,
93
- provider?: AIProvider
94
- ): Promise<SentimentResult> {
95
- const resolved = provider ?? getDefaultAIProvider();
96
- if (!resolved) {
97
- throw new Error("No AI API key found. Set OPENAI_API_KEY or ANTHROPIC_API_KEY.");
98
- }
99
-
100
- const prompt = buildSentimentPrompt(text);
101
- const raw = resolved === "openai"
102
- ? await callOpenAI(prompt, 200)
103
- : await callAnthropic(prompt, 200);
104
-
105
- const fallback: SentimentResult = { sentiment: "neutral", score: 0.5, keywords: [] };
106
- const result = parseJSON<SentimentResult>(raw, fallback);
107
- return validateSentimentResult(result);
108
- }
109
-
110
- export async function analyzeBatch(
111
- texts: string[],
112
- provider?: AIProvider
113
- ): Promise<SentimentResult[]> {
114
- if (texts.length === 0) return [];
115
-
116
- const resolved = provider ?? getDefaultAIProvider();
117
- if (!resolved) {
118
- throw new Error("No AI API key found. Set OPENAI_API_KEY or ANTHROPIC_API_KEY.");
119
- }
120
-
121
- const prompt = buildBatchSentimentPrompt(texts);
122
- const maxTokens = Math.min(texts.length * 150, 4000);
123
- const raw = resolved === "openai"
124
- ? await callOpenAI(prompt, maxTokens)
125
- : await callAnthropic(prompt, maxTokens);
126
-
127
- const fallback: SentimentResult[] = texts.map(() => ({
128
- sentiment: "neutral" as SentimentLabel,
129
- score: 0.5,
130
- keywords: [],
131
- }));
132
-
133
- const results = parseJSON<SentimentResult[]>(raw, fallback);
134
- if (!Array.isArray(results) || results.length !== texts.length) {
135
- return fallback;
136
- }
137
-
138
- return results.map(validateSentimentResult);
139
- }
140
-
141
- export function getSentimentReport(
142
- accountId: string,
143
- days?: number
144
- ): SentimentReport {
145
- const db = getDatabase();
146
-
147
- let dateFilter = "";
148
- const params: unknown[] = [accountId];
149
-
150
- if (days && days > 0) {
151
- dateFilter = " AND fetched_at >= datetime('now', ?)";
152
- params.push(`-${days} days`);
153
- }
154
-
155
- // Get all mentions with sentiment for this account
156
- const rows = db
157
- .prepare(
158
- `SELECT id, content, sentiment FROM mentions
159
- WHERE account_id = ? AND sentiment IS NOT NULL${dateFilter}
160
- ORDER BY fetched_at DESC`
161
- )
162
- .all(...params) as { id: string; content: string | null; sentiment: string }[];
163
-
164
- const total_analyzed = rows.length;
165
-
166
- if (total_analyzed === 0) {
167
- return {
168
- positive_pct: 0,
169
- neutral_pct: 0,
170
- negative_pct: 0,
171
- total_analyzed: 0,
172
- trending_keywords: [],
173
- most_positive: null,
174
- most_negative: null,
175
- };
176
- }
177
-
178
- let positive = 0;
179
- let neutral = 0;
180
- let negative = 0;
181
- let most_positive: { id: string; content: string; sentiment: string } | null = null;
182
- let most_negative: { id: string; content: string; sentiment: string } | null = null;
183
-
184
- for (const row of rows) {
185
- const s = row.sentiment.toLowerCase();
186
- if (s === "positive") {
187
- positive++;
188
- if (!most_positive) {
189
- most_positive = { id: row.id, content: row.content || "", sentiment: row.sentiment };
190
- }
191
- } else if (s === "negative") {
192
- negative++;
193
- if (!most_negative) {
194
- most_negative = { id: row.id, content: row.content || "", sentiment: row.sentiment };
195
- }
196
- } else {
197
- neutral++;
198
- }
199
- }
200
-
201
- // Trending keywords: parse sentiment field for keywords if stored as JSON,
202
- // otherwise just aggregate the sentiment labels
203
- const keywordCounts: Record<string, number> = {};
204
- for (const row of rows) {
205
- // Try to parse sentiment as JSON (might contain keywords)
206
- try {
207
- const parsed = JSON.parse(row.sentiment);
208
- if (parsed && Array.isArray(parsed.keywords)) {
209
- for (const kw of parsed.keywords) {
210
- const key = String(kw).toLowerCase();
211
- keywordCounts[key] = (keywordCounts[key] || 0) + 1;
212
- }
213
- }
214
- } catch {
215
- // sentiment is a plain string label, skip keyword extraction
216
- }
217
- }
218
-
219
- const trending_keywords = Object.entries(keywordCounts)
220
- .sort((a, b) => b[1] - a[1])
221
- .slice(0, 10)
222
- .map(([kw]) => kw);
223
-
224
- return {
225
- positive_pct: Math.round((positive / total_analyzed) * 100),
226
- neutral_pct: Math.round((neutral / total_analyzed) * 100),
227
- negative_pct: Math.round((negative / total_analyzed) * 100),
228
- total_analyzed,
229
- trending_keywords,
230
- most_positive,
231
- most_negative,
232
- };
233
- }
234
-
235
- export async function autoAnalyzeMention(
236
- mentionId: string,
237
- provider?: AIProvider
238
- ): Promise<SentimentResult | null> {
239
- const mention = getMention(mentionId);
240
- if (!mention) {
241
- throw new Error(`Mention '${mentionId}' not found.`);
242
- }
243
-
244
- if (!mention.content) {
245
- throw new Error(`Mention '${mentionId}' has no content to analyze.`);
246
- }
247
-
248
- const result = await analyzeSentiment(mention.content, provider);
249
-
250
- // Store the sentiment result as JSON in the mention's sentiment field
251
- const db = getDatabase();
252
- const sentimentData = JSON.stringify(result);
253
- db.prepare("UPDATE mentions SET sentiment = ? WHERE id = ?").run(sentimentData, mentionId);
254
-
255
- return result;
256
- }
@@ -1,291 +0,0 @@
1
- /**
2
- * Thread and carousel support for social media posts.
3
- *
4
- * Threads: multiple posts linked by thread_id, ordered by thread_position.
5
- * Carousels: a single post with multiple media_urls (Instagram/LinkedIn format).
6
- */
7
-
8
- import {
9
- createPost,
10
- getAccount,
11
- getPost,
12
- updatePost,
13
- getThreadPosts,
14
- deleteThreadPosts,
15
- checkPlatformLimit,
16
- PLATFORM_LIMITS,
17
- type Post,
18
- type Platform,
19
- } from "../db/social.js";
20
-
21
- // ---- Types ----
22
-
23
- export interface CreateThreadOptions {
24
- scheduledAt?: string;
25
- tags?: string[];
26
- }
27
-
28
- export interface ThreadResult {
29
- threadId: string;
30
- posts: Post[];
31
- }
32
-
33
- export interface PublishThreadResult {
34
- threadId: string;
35
- posts: Post[];
36
- platformPostIds: string[];
37
- }
38
-
39
- // ---- Thread Operations ----
40
-
41
- /**
42
- * Create a thread of posts linked by a shared thread_id.
43
- * Each post is validated against the platform's character limit.
44
- */
45
- export function createThread(
46
- contents: string[],
47
- accountId: string,
48
- options?: CreateThreadOptions
49
- ): ThreadResult {
50
- if (contents.length === 0) {
51
- throw new Error("Thread must have at least one post.");
52
- }
53
-
54
- const account = getAccount(accountId);
55
- if (!account) {
56
- throw new Error(`Account '${accountId}' not found.`);
57
- }
58
-
59
- const limit = PLATFORM_LIMITS[account.platform];
60
- for (let i = 0; i < contents.length; i++) {
61
- if (contents[i].length > limit) {
62
- throw new Error(
63
- `Post ${i + 1} (${contents[i].length} chars) exceeds ${account.platform} limit (${limit} chars).`
64
- );
65
- }
66
- }
67
-
68
- const threadId = crypto.randomUUID();
69
- const posts: Post[] = [];
70
-
71
- for (let i = 0; i < contents.length; i++) {
72
- const post = createPost({
73
- account_id: accountId,
74
- content: contents[i],
75
- status: options?.scheduledAt ? "scheduled" : "draft",
76
- scheduled_at: options?.scheduledAt,
77
- tags: options?.tags,
78
- thread_id: threadId,
79
- thread_position: i,
80
- });
81
- posts.push(post);
82
- }
83
-
84
- return { threadId, posts };
85
- }
86
-
87
- /**
88
- * Get all posts in a thread, ordered by position.
89
- */
90
- export function getThread(threadId: string): Post[] {
91
- const posts = getThreadPosts(threadId);
92
- if (posts.length === 0) {
93
- throw new Error(`Thread '${threadId}' not found.`);
94
- }
95
- return posts;
96
- }
97
-
98
- /**
99
- * Publish a thread sequentially via platform APIs.
100
- *
101
- * - X: POST /2/tweets chaining each reply to the previous tweet
102
- * - Meta: POST /{page-id}/feed for first post, then POST /{post-id}/comments for rest
103
- *
104
- * Each post gets its platform_post_id stored.
105
- */
106
- export async function publishThread(threadId: string): Promise<PublishThreadResult> {
107
- const posts = getThreadPosts(threadId);
108
- if (posts.length === 0) {
109
- throw new Error(`Thread '${threadId}' not found.`);
110
- }
111
-
112
- const firstPost = posts[0];
113
- const account = getAccount(firstPost.account_id);
114
- if (!account) {
115
- throw new Error(`Account '${firstPost.account_id}' not found.`);
116
- }
117
-
118
- const platformPostIds: string[] = [];
119
- const updatedPosts: Post[] = [];
120
-
121
- if (account.platform === "x") {
122
- // X: chain tweets using reply.in_reply_to_tweet_id
123
- const { XPublisher } = await import("./publisher.js");
124
- const publisher = new XPublisher();
125
-
126
- let previousTweetId: string | null = null;
127
-
128
- for (const post of posts) {
129
- const body: Record<string, unknown> = { text: post.content };
130
- if (previousTweetId) {
131
- body.reply = { in_reply_to_tweet_id: previousTweetId };
132
- }
133
-
134
- // Use the publisher's publishPost for the first, then raw fetch for replies
135
- const res = await fetch("https://api.twitter.com/2/tweets", {
136
- method: "POST",
137
- headers: {
138
- Authorization: `Bearer ${process.env.X_BEARER_TOKEN || process.env.X_ACCESS_TOKEN || ""}`,
139
- "Content-Type": "application/json",
140
- },
141
- body: JSON.stringify(body),
142
- });
143
-
144
- if (!res.ok) {
145
- const text = await res.text();
146
- // Mark remaining posts as failed
147
- for (const remaining of posts.filter((p) => !platformPostIds.includes(p.platform_post_id || ""))) {
148
- if (!platformPostIds.some((pid) => pid === remaining.platform_post_id)) {
149
- updatePost(remaining.id, { status: "failed" });
150
- }
151
- }
152
- throw new Error(`X API error ${res.status}: ${text}`);
153
- }
154
-
155
- const data = (await res.json()) as { data: { id: string } };
156
- const tweetId = data.data.id;
157
- previousTweetId = tweetId;
158
- platformPostIds.push(tweetId);
159
-
160
- const updated = updatePost(post.id, {
161
- status: "published",
162
- published_at: new Date().toISOString().replace("T", " ").replace(/\.\d+Z$/, ""),
163
- platform_post_id: tweetId,
164
- });
165
- updatedPosts.push(updated!);
166
- }
167
- } else if (account.platform === "instagram" || account.platform === "linkedin") {
168
- // Meta-style: first post to feed, rest as comments
169
- const accessToken = process.env.META_ACCESS_TOKEN || "";
170
- const pageId = process.env.META_PAGE_ID || account.handle;
171
- const baseUrl = "https://graph.facebook.com/v22.0";
172
-
173
- // First post
174
- const firstRes = await fetch(
175
- `${baseUrl}/${pageId}/feed?access_token=${encodeURIComponent(accessToken)}`,
176
- {
177
- method: "POST",
178
- headers: { "Content-Type": "application/json" },
179
- body: JSON.stringify({ message: posts[0].content }),
180
- }
181
- );
182
-
183
- if (!firstRes.ok) {
184
- const text = await firstRes.text();
185
- throw new Error(`Meta API error ${firstRes.status}: ${text}`);
186
- }
187
-
188
- const firstData = (await firstRes.json()) as { id: string };
189
- platformPostIds.push(firstData.id);
190
- const firstUpdated = updatePost(posts[0].id, {
191
- status: "published",
192
- published_at: new Date().toISOString().replace("T", " ").replace(/\.\d+Z$/, ""),
193
- platform_post_id: firstData.id,
194
- });
195
- updatedPosts.push(firstUpdated!);
196
-
197
- // Rest as comments on the first post
198
- for (let i = 1; i < posts.length; i++) {
199
- const commentRes = await fetch(
200
- `${baseUrl}/${firstData.id}/comments?access_token=${encodeURIComponent(accessToken)}`,
201
- {
202
- method: "POST",
203
- headers: { "Content-Type": "application/json" },
204
- body: JSON.stringify({ message: posts[i].content }),
205
- }
206
- );
207
-
208
- if (!commentRes.ok) {
209
- const text = await commentRes.text();
210
- throw new Error(`Meta API error ${commentRes.status}: ${text}`);
211
- }
212
-
213
- const commentData = (await commentRes.json()) as { id: string };
214
- platformPostIds.push(commentData.id);
215
-
216
- const updated = updatePost(posts[i].id, {
217
- status: "published",
218
- published_at: new Date().toISOString().replace("T", " ").replace(/\.\d+Z$/, ""),
219
- platform_post_id: commentData.id,
220
- });
221
- updatedPosts.push(updated!);
222
- }
223
- } else {
224
- // For unsupported platforms, publish each post individually via the generic publisher
225
- const { publishToApi } = await import("./publisher.js");
226
-
227
- for (const post of posts) {
228
- try {
229
- const updated = await publishToApi(post.id);
230
- platformPostIds.push(updated.platform_post_id || "");
231
- updatedPosts.push(updated);
232
- } catch (err) {
233
- throw err;
234
- }
235
- }
236
- }
237
-
238
- return { threadId, posts: updatedPosts, platformPostIds };
239
- }
240
-
241
- /**
242
- * Delete an entire thread and all its posts.
243
- */
244
- export function deleteThread(threadId: string): number {
245
- const posts = getThreadPosts(threadId);
246
- if (posts.length === 0) {
247
- throw new Error(`Thread '${threadId}' not found.`);
248
- }
249
- return deleteThreadPosts(threadId);
250
- }
251
-
252
- // ---- Carousel ----
253
-
254
- /**
255
- * Create a carousel post — a single post with multiple media_urls.
256
- * Used for Instagram/LinkedIn carousel format.
257
- */
258
- export function createCarousel(
259
- images: string[],
260
- captions: string[],
261
- accountId: string
262
- ): Post {
263
- if (images.length === 0) {
264
- throw new Error("Carousel must have at least one image.");
265
- }
266
-
267
- const account = getAccount(accountId);
268
- if (!account) {
269
- throw new Error(`Account '${accountId}' not found.`);
270
- }
271
-
272
- // Use the first caption or combine all captions as the post content
273
- const content = captions.length > 0 ? captions.join("\n\n") : "";
274
-
275
- // Validate content length against platform limit
276
- if (content.length > 0) {
277
- const limit = PLATFORM_LIMITS[account.platform];
278
- if (content.length > limit) {
279
- throw new Error(
280
- `Carousel caption (${content.length} chars) exceeds ${account.platform} limit (${limit} chars).`
281
- );
282
- }
283
- }
284
-
285
- return createPost({
286
- account_id: accountId,
287
- content,
288
- media_urls: images,
289
- status: "draft",
290
- });
291
- }