main.vala 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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<TaskDependency>(TaskDependency.entity_mapping);
  31. db.add_entity<TaskComment>(TaskComment.entity_mapping);
  32. db.add_entity<TaskBlock>(TaskBlock.entity_mapping);
  33. db.add_entity<TaskProgress>(TaskProgress.entity_mapping);
  34. db.add_entity<Saddle>(Saddle.entity_mapping);
  35. db.add_entity<Session>(Session.entity_mapping);
  36. db.add_entity<SessionCommand>(SessionCommand.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.migrate_on_startup();
  49. application.add_scoped<ProjectService>();
  50. application.add_scoped<TaskService>();
  51. application.add_scoped<SaddleService>();
  52. application.add_scoped<RuleService>();
  53. application.add_scoped<CapabilityService>();
  54. application.add_scoped<AllocationService>();
  55. application.add_scoped<TimelineService>();
  56. application.add_singleton<SubtaskEventQueue>();
  57. application.add_singleton<SaddlePollState>();
  58. application.add_singleton<StatePulse>();
  59. application.add_singleton<PushService>();
  60. application.add_singleton<Dispatcher>();
  61. // Web Push (plan 11 task E): the VAPID keypair loads or
  62. // generates + persists BEFORE the server accepts requests, so
  63. // every subscriber sees the same public key from the first
  64. // page render.
  65. application.container.create_scope()
  66. .resolve<PushService>().ensure_keys_from_config();
  67. var statum = application.configure_with<StatumConfigurator>();
  68. statum.global<ClankerSaddle.Services.PulsePublic>(StatePulse.SLOT_TYPE,
  69. new ClankerSaddle.Services.PulsePublic());
  70. // spry:pages-begin
  71. statum.add_page<HomePage, HomeEntrypoint>();
  72. statum.add_page<LoginPage, LoginEntrypoint>();
  73. statum.add_page<ProjectsPage, ProjectsEntrypoint>();
  74. statum.add_page<ProjectDetailPage, ProjectDetailEntrypoint>();
  75. statum.add_page<TasksPage, TasksEntrypoint>();
  76. statum.add_page<TaskDetailPage, TaskDetailEntrypoint>();
  77. statum.add_page<TaskTerminalPage, TaskTerminalEntrypoint>();
  78. statum.add_page<SaddlesPage, SaddlesEntrypoint>();
  79. statum.add_page<RulesPage, RulesEntrypoint>();
  80. // spry:pages-end
  81. // spry:actions-begin
  82. statum.action<LoginAction>();
  83. statum.action<Spry.Actions.LogoutAction>();
  84. statum.action<CreateProjectAction>();
  85. statum.action<UpdateProjectAction>();
  86. statum.action<AddProjectLinkAction>();
  87. statum.action<RemoveProjectLinkAction>();
  88. statum.action<CreateTaskAction>();
  89. statum.action<UpdateTaskAction>();
  90. statum.action<AddCommentAction>();
  91. statum.action<UpdatePlanAction>();
  92. statum.action<AnswerBlockAction>();
  93. statum.action<CreateSubtaskAction>();
  94. statum.action<AddDependencyAction>();
  95. statum.action<EnqueueTaskAction>();
  96. statum.action<DeleteTaskAction>();
  97. statum.action<CancelTaskAction>();
  98. statum.action<RetryTaskAction>();
  99. statum.action<ResumeTaskAction>();
  100. statum.action<ToggleAutoApproveAction>();
  101. statum.action<PushSubscribeAction>();
  102. statum.action<PushUnsubscribeAction>();
  103. statum.action<ToggleTaskNotifyAction>();
  104. statum.action<RefreshTaskDataAction>();
  105. statum.action<RefreshDashboardAction>();
  106. statum.action<RegisterSaddleAction>();
  107. statum.action<UnregisterSaddleAction>();
  108. statum.action<RefreshSaddleStatusAction>();
  109. statum.action<UpdateSaddleIdentityAction>();
  110. statum.action<CreateRuleAction>();
  111. statum.action<UpdateRuleAction>();
  112. statum.action<DeleteRuleAction>();
  113. statum.action<AttachProjectRuleAction>();
  114. statum.action<DetachProjectRuleAction>();
  115. statum.action<AttachTaskRuleAction>();
  116. statum.action<DetachTaskRuleAction>();
  117. statum.action<DeleteProjectNoteAction>();
  118. statum.action<CreateCapabilityAction>();
  119. statum.action<DeleteCapabilityAction>();
  120. statum.action<AttachSaddleCapabilityAction>();
  121. statum.action<DetachSaddleCapabilityAction>();
  122. statum.action<AttachTaskCapabilityAction>();
  123. statum.action<DetachTaskCapabilityAction>();
  124. // spry:actions-end
  125. // spry:resources-begin
  126. statum.add_resource<ClankerSaddle.AppCss>();
  127. // spry:resources-end
  128. // spry:auth-begin
  129. // spry:auth-end
  130. application.add_endpoint<SaddleUpdateEndpoint>(
  131. new EndpointRoute("/api/saddle/{id}/update", Method.POST));
  132. application.add_endpoint<SaddlePollEndpoint>(
  133. new EndpointRoute("/api/saddle/{id}/poll", Method.GET));
  134. application.add_endpoint<SaddleNextTaskEndpoint>(
  135. new EndpointRoute("/api/saddle/next-task", Method.POST));
  136. application.add_endpoint<SaddleChunksEndpoint>(
  137. new EndpointRoute("/api/saddle/chunks", Method.POST));
  138. application.add_endpoint<SaddleResultsEndpoint>(
  139. new EndpointRoute("/api/saddle/results", Method.POST));
  140. application.add_endpoint<SaddleNoteEndpoint>(
  141. new EndpointRoute("/api/saddle/note", Method.POST));
  142. application.add_endpoint<SaddleSubtaskEndpoint>(
  143. new EndpointRoute("/api/saddle/subtask", Method.POST));
  144. application.add_endpoint<SaddleTaskListEndpoint>(
  145. new EndpointRoute("/api/saddle/tasks", Method.GET));
  146. application.add_endpoint<SaddleTaskDetailEndpoint>(
  147. new EndpointRoute("/api/saddle/task/{id}", Method.GET));
  148. application.add_endpoint<SaddleCapabilitiesEndpoint>(
  149. new EndpointRoute("/api/saddle/capabilities", Method.GET));
  150. application.add_endpoint<ClankerSaddle.Api.TerminalSseEndpoint>(
  151. new EndpointRoute("/terminal-stream/{session_id}", Method.GET));
  152. application.add_endpoint<ClankerSaddle.Api.ServiceWorkerEndpoint>(
  153. new EndpointRoute("/sw.js", Method.GET));
  154. GLib.Idle.add(() => {
  155. seed_admin.begin(application.container);
  156. ensure_saddle_tokens.begin(application.container);
  157. start_dispatcher.begin(application.container);
  158. return false;
  159. });
  160. application.run();
  161. return 0;
  162. } catch (Error e) {
  163. printerr("Error: %s\n", e.message);
  164. return 1;
  165. }
  166. }
  167. /**
  168. * Seeds the admin account when BOTH SPRY_SEED_ADMIN_USERNAME and
  169. * SPRY_SEED_ADMIN_PASSWORD are set; skipped silently otherwise. The
  170. * password is never logged.
  171. */
  172. private async void seed_admin(Container container) {
  173. var username = Environment.get_variable("SPRY_SEED_ADMIN_USERNAME") ?? "";
  174. var password = Environment.get_variable("SPRY_SEED_ADMIN_PASSWORD") ?? "";
  175. if (username.length == 0 || password.length == 0) {
  176. return;
  177. }
  178. try {
  179. var scope = container.create_scope();
  180. yield SpryAuth.ensure_admin(scope.resolve<UserService>(),
  181. username, @"$(username)@example.com", password);
  182. print("[ClankerSaddle] Seeded admin account: %s\n", username);
  183. } catch (Error e) {
  184. printerr("[ClankerSaddle] Admin seeding failed: %s\n", e.message);
  185. }
  186. }
  187. private async void start_dispatcher(Container container) {
  188. try {
  189. var scope = container.create_scope();
  190. scope.resolve<Dispatcher>().start();
  191. } catch (Error e) {
  192. printerr("[ClankerSaddle] Dispatcher startup failed: %s\n", e.message);
  193. }
  194. }
  195. /**
  196. * Startup migration of the per-saddle tokens (plan 10): every saddle
  197. * row gets its canonical credential — the legacy `api_tokens` value it
  198. * already holds, or a freshly minted one — so the operator can copy it
  199. * to daemons registered before the flip (see
  200. * {@link ClankerSaddle.Services.SaddleService.ensure_saddle_tokens}).
  201. */
  202. private async void ensure_saddle_tokens(Container container) {
  203. try {
  204. var scope = container.create_scope();
  205. yield scope.resolve<SaddleService>().ensure_saddle_tokens();
  206. } catch (Error e) {
  207. printerr("[ClankerSaddle] Saddle token migration failed: %s\n", e.message);
  208. }
  209. }