main.vala 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using Astralis;
  2. using Inversion;
  3. using InvercargillSqlInversion;
  4. using Spry;
  5. using Spry.Authentication;
  6. using Statum;
  7. using ClankerSaddle;
  8. using ClankerSaddle.Api;
  9. using ClankerSaddle.Data;
  10. using ClankerSaddle.Services;
  11. int main(string[] args) {
  12. int port = args.length > 1 ? int.parse(args[1]) : 8080;
  13. print("ClankerSaddle - LLM task orchestration platform\n");
  14. print(@"Website starting on port $(port)\n");
  15. try {
  16. var application = new WebApplication(port);
  17. application.use_compression();
  18. application.add_module<StatumModule>();
  19. application.add_module<SpryModule>();
  20. var db_path = Environment.get_variable("SPRY_DB_PATH") ?? "";
  21. if (db_path.strip().length == 0) {
  22. db_path = "./clanker-saddle.sqlite";
  23. }
  24. var db = application.container.configure_with<DatabaseConfigurator>();
  25. db.register_connection(@"sqlite://$(db_path)");
  26. db.add_migration<ClankerSaddle.Data.Migrations.M0001_Initial>();
  27. db.add_entity<Project>(Project.entity_mapping);
  28. db.add_entity<ProjectLink>(ProjectLink.entity_mapping);
  29. db.add_entity<TaskEntity>(TaskEntity.entity_mapping);
  30. db.add_entity<TaskComment>(TaskComment.entity_mapping);
  31. db.add_entity<TaskBlock>(TaskBlock.entity_mapping);
  32. db.add_entity<TaskProgress>(TaskProgress.entity_mapping);
  33. db.add_entity<Saddle>(Saddle.entity_mapping);
  34. db.add_entity<Session>(Session.entity_mapping);
  35. db.add_entity<SessionCommand>(SessionCommand.entity_mapping);
  36. db.add_entity<SaddleEntryDedup>(SaddleEntryDedup.entity_mapping);
  37. db.add_entity<ApiToken>(ApiToken.entity_mapping);
  38. db.add_entity<Rule>(Rule.entity_mapping);
  39. db.add_entity<RuleProject>(RuleProject.entity_mapping);
  40. db.add_entity<TaskRule>(TaskRule.entity_mapping);
  41. db.add_entity<ProjectNote>(ProjectNote.entity_mapping);
  42. db.add_entity<SessionRawLog>(SessionRawLog.entity_mapping);
  43. db.add_entity<Capability>(Capability.entity_mapping);
  44. db.add_entity<SaddleCapability>(SaddleCapability.entity_mapping);
  45. db.add_entity<TaskCapability>(TaskCapability.entity_mapping);
  46. db.add_entity<PushSubscription>(PushSubscription.entity_mapping);
  47. db.add_entity<TaskNotifyPref>(TaskNotifyPref.entity_mapping);
  48. db.add_entity<Goal>(Goal.entity_mapping);
  49. db.add_entity<GoalLink>(GoalLink.entity_mapping);
  50. db.add_entity<GoalComment>(GoalComment.entity_mapping);
  51. db.add_entity<GoalEvent>(GoalEvent.entity_mapping);
  52. db.migrate_on_startup();
  53. application.add_scoped<ProjectService>();
  54. application.add_scoped<TaskService>();
  55. application.add_scoped<GoalService>();
  56. application.add_scoped<SaddleService>();
  57. application.add_scoped<RuleService>();
  58. application.add_scoped<CapabilityService>();
  59. application.add_scoped<AllocationService>();
  60. application.add_scoped<TimelineService>();
  61. application.add_singleton<SaddlePollState>();
  62. application.add_singleton<StatePulse>();
  63. application.add_singleton<PushService>();
  64. application.add_singleton<Dispatcher>();
  65. // Web Push (plan 11 task E): the VAPID keypair loads or
  66. // generates + persists BEFORE the server accepts requests, so
  67. // every subscriber sees the same public key from the first
  68. // page render.
  69. application.container.create_scope()
  70. .resolve<PushService>().ensure_keys_from_config();
  71. var statum = application.configure_with<StatumConfigurator>();
  72. statum.global<ClankerSaddle.Services.PulsePublic>(StatePulse.SLOT_TYPE,
  73. new ClankerSaddle.Services.PulsePublic());
  74. // spry:pages-begin
  75. statum.add_page<HomePage, HomeEntrypoint>();
  76. statum.add_page<LoginPage, LoginEntrypoint>();
  77. statum.add_page<ProjectsPage, ProjectsEntrypoint>();
  78. statum.add_page<ProjectDetailPage, ProjectDetailEntrypoint>();
  79. statum.add_page<TasksPage, TasksEntrypoint>();
  80. statum.add_page<TaskDetailPage, TaskDetailEntrypoint>();
  81. statum.add_page<TaskTerminalPage, TaskTerminalEntrypoint>();
  82. statum.add_page<GoalsPage, GoalsEntrypoint>();
  83. statum.add_page<GoalDetailPage, GoalDetailEntrypoint>();
  84. statum.add_page<SaddlesPage, SaddlesEntrypoint>();
  85. statum.add_page<RulesPage, RulesEntrypoint>();
  86. // spry:pages-end
  87. // spry:actions-begin
  88. statum.action<LoginAction>();
  89. statum.action<Spry.Actions.LogoutAction>();
  90. statum.action<CreateProjectAction>();
  91. statum.action<UpdateProjectAction>();
  92. statum.action<AddProjectLinkAction>();
  93. statum.action<RemoveProjectLinkAction>();
  94. statum.action<CreateTaskAction>();
  95. statum.action<UpdateTaskAction>();
  96. statum.action<AddCommentAction>();
  97. statum.action<AnswerBlockAction>();
  98. statum.action<EnqueueTaskAction>();
  99. statum.action<DeleteTaskAction>();
  100. statum.action<CancelTaskAction>();
  101. statum.action<RetryTaskAction>();
  102. statum.action<ResumeTaskAction>();
  103. statum.action<RefreshTasksDataAction>();
  104. statum.action<CreateGoalAction>();
  105. statum.action<StartGoalAction>();
  106. statum.action<PauseGoalAction>();
  107. statum.action<ResumeGoalAction>();
  108. statum.action<CancelGoalAction>();
  109. statum.action<RetryGoalRunAction>();
  110. statum.action<RequeueGoalRunAction>();
  111. statum.action<GoalCommentAction>();
  112. statum.action<AnswerGoalBlockAction>();
  113. statum.action<AddGoalLinkAction>();
  114. statum.action<RemoveGoalLinkAction>();
  115. statum.action<RefreshGoalDataAction>();
  116. statum.action<PushSubscribeAction>();
  117. statum.action<PushUnsubscribeAction>();
  118. statum.action<ToggleTaskNotifyAction>();
  119. statum.action<RefreshTaskDataAction>();
  120. statum.action<RefreshDashboardAction>();
  121. statum.action<RegisterSaddleAction>();
  122. statum.action<UnregisterSaddleAction>();
  123. statum.action<RefreshSaddleStatusAction>();
  124. statum.action<UpdateSaddleIdentityAction>();
  125. statum.action<CreateRuleAction>();
  126. statum.action<UpdateRuleAction>();
  127. statum.action<DeleteRuleAction>();
  128. statum.action<AttachProjectRuleAction>();
  129. statum.action<DetachProjectRuleAction>();
  130. statum.action<AttachTaskRuleAction>();
  131. statum.action<DetachTaskRuleAction>();
  132. statum.action<DeleteProjectNoteAction>();
  133. statum.action<CreateCapabilityAction>();
  134. statum.action<DeleteCapabilityAction>();
  135. statum.action<AttachSaddleCapabilityAction>();
  136. statum.action<DetachSaddleCapabilityAction>();
  137. statum.action<AttachTaskCapabilityAction>();
  138. statum.action<DetachTaskCapabilityAction>();
  139. // spry:actions-end
  140. // spry:resources-begin
  141. statum.add_resource<ClankerSaddle.AppCss>();
  142. // spry:resources-end
  143. // spry:auth-begin
  144. // spry:auth-end
  145. application.add_endpoint<SaddleUpdateEndpoint>(
  146. new EndpointRoute("/api/saddle/{id}/update", Method.POST));
  147. application.add_endpoint<SaddlePollEndpoint>(
  148. new EndpointRoute("/api/saddle/{id}/poll", Method.GET));
  149. application.add_endpoint<SaddleNextTaskEndpoint>(
  150. new EndpointRoute("/api/saddle/next-task", Method.POST));
  151. application.add_endpoint<SaddleChunksEndpoint>(
  152. new EndpointRoute("/api/saddle/chunks", Method.POST));
  153. application.add_endpoint<SaddleResultsEndpoint>(
  154. new EndpointRoute("/api/saddle/results", Method.POST));
  155. application.add_endpoint<SaddleNoteEndpoint>(
  156. new EndpointRoute("/api/saddle/note", Method.POST));
  157. application.add_endpoint<SaddleTaskCreateEndpoint>(
  158. new EndpointRoute("/api/saddle/task/create", Method.POST));
  159. application.add_endpoint<SaddleTaskDeleteEndpoint>(
  160. new EndpointRoute("/api/saddle/task/delete", Method.POST));
  161. application.add_endpoint<SaddleTaskEnqueueEndpoint>(
  162. new EndpointRoute("/api/saddle/task/enqueue", Method.POST));
  163. application.add_endpoint<SaddleTaskListEndpoint>(
  164. new EndpointRoute("/api/saddle/tasks", Method.GET));
  165. application.add_endpoint<SaddleTaskDetailEndpoint>(
  166. new EndpointRoute("/api/saddle/task/{id}", Method.GET));
  167. application.add_endpoint<SaddleCapabilitiesEndpoint>(
  168. new EndpointRoute("/api/saddle/capabilities", Method.GET));
  169. application.add_endpoint<ClankerSaddle.Api.TerminalSseEndpoint>(
  170. new EndpointRoute("/terminal-stream/{session_id}", Method.GET));
  171. application.add_endpoint<ClankerSaddle.Api.ServiceWorkerEndpoint>(
  172. new EndpointRoute("/sw.js", Method.GET));
  173. GLib.Idle.add(() => {
  174. seed_admin.begin(application.container);
  175. ensure_saddle_tokens.begin(application.container);
  176. start_dispatcher.begin(application.container);
  177. return false;
  178. });
  179. application.run();
  180. return 0;
  181. } catch (Error e) {
  182. printerr("Error: %s\n", e.message);
  183. return 1;
  184. }
  185. }
  186. /**
  187. * Seeds the admin account when BOTH SPRY_SEED_ADMIN_USERNAME and
  188. * SPRY_SEED_ADMIN_PASSWORD are set; skipped silently otherwise. The
  189. * password is never logged.
  190. */
  191. private async void seed_admin(Container container) {
  192. var username = Environment.get_variable("SPRY_SEED_ADMIN_USERNAME") ?? "";
  193. var password = Environment.get_variable("SPRY_SEED_ADMIN_PASSWORD") ?? "";
  194. if (username.length == 0 || password.length == 0) {
  195. return;
  196. }
  197. try {
  198. var scope = container.create_scope();
  199. yield SpryAuth.ensure_admin(scope.resolve<UserService>(),
  200. username, @"$(username)@example.com", password);
  201. print("[ClankerSaddle] Seeded admin account: %s\n", username);
  202. } catch (Error e) {
  203. printerr("[ClankerSaddle] Admin seeding failed: %s\n", e.message);
  204. }
  205. }
  206. private async void start_dispatcher(Container container) {
  207. try {
  208. var scope = container.create_scope();
  209. scope.resolve<Dispatcher>().start();
  210. } catch (Error e) {
  211. printerr("[ClankerSaddle] Dispatcher startup failed: %s\n", e.message);
  212. }
  213. }
  214. /**
  215. * Startup migration of the per-saddle tokens (plan 10): every saddle
  216. * row gets its canonical credential — the legacy `api_tokens` value it
  217. * already holds, or a freshly minted one — so the operator can copy it
  218. * to daemons registered before the flip (see
  219. * {@link ClankerSaddle.Services.SaddleService.ensure_saddle_tokens}).
  220. */
  221. private async void ensure_saddle_tokens(Container container) {
  222. try {
  223. var scope = container.create_scope();
  224. yield scope.resolve<SaddleService>().ensure_saddle_tokens();
  225. } catch (Error e) {
  226. printerr("[ClankerSaddle] Saddle token migration failed: %s\n", e.message);
  227. }
  228. }