main.vala 11 KB

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