handlers.ts
This file
Path
apps/platform/src/handlers.ts
402 lines · 13.6 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 1why, one step back1import { RepoHost } from "@repo/artifacts/repo-host";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 2why, one step back2import { EvidenceStore, LogRef } from "@repo/checks/evidence-store";Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 3–13why, one step back3import {4 codeProjectOf,5 commitOf,6 fileOf,7 logOf,8 pathsOf,9 refsOf,10 treeOf,11 whyOf,12} from "@repo/project/code";13import type { CodeProject } from "@repo/project/code";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 14why, one step back14import { envSchemaMethod } from "@repo/project/deciders";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 15why, one step back15import { landedProjectOf, withDescriptions } from "@repo/project/description";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 16–19why, one step back16import { FrontPage } from "@repo/project/front-page";17import { GatewayUrl } from "@repo/project/project";18import type { ProjectApi } from "@repo/project/project";19import { Projects } from "@repo/project/projects";Merge build/1102 into mainoscabrielbefore adoption084383d
line 20why, one step back20import { withStandingPosture } from "@repo/project/standing-posture";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 21–24why, one step back21import { Contracts, EnvSchemaOf } from "@repo/protocol/contracts";22import type { AnswerView } from "@repo/protocol/domain/answer";23import type { ClaimView } from "@repo/protocol/domain/claim";24import type { Decision, Today } from "@repo/protocol/domain/decision";Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 25–26why, one step back25import { addressOfRepo, sameHandle, RepoName } from "@repo/protocol/ids";26import type { ProjectName } from "@repo/protocol/ids";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 27–37why, one step back27import { CurrentPrincipal, Principal } from "@repo/protocol/principal";28import type { Person } from "@repo/protocol/principal";29import { NotDecider } from "@repo/protocol/refusal";30import type { Layer } from "effect";31import { Array, DateTime, Effect, Number, Option, Order, Stream } from "effect";32import { HttpServerRequest } from "effect/http";33import type { Rpc, RpcGroup } from "effect/rpc";3435import { RateLimits } from "./rate-limits.ts";36import { servedFramed, watchHandler } from "./watch.ts";37Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 38–39why, one step back38const LOG_PAGE = 30;39Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 40–43why, one step back40const FAN_OUT = 8;4142const caller = Effect.service(CurrentPrincipal);43Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
lines 44–45why, one step back44// `Authenticated` lets an anonymous call reach only a `public` contract, so anyone45// else here is a defect in the server side.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 46–70why, one step back46const person = Effect.flatMap(caller, (principal) =>47 Principal.match(principal, {48 Anonymous: () =>49 Effect.die("a contract for signed-in people was called anonymously"),50 Person: (named) => Effect.succeed<Person>(named),51 })52);5354const originOf = (request: HttpServerRequest.HttpServerRequest) =>55 URL.canParse(request.originalUrl)56 ? Option.some(new URL(request.originalUrl).origin)57 : Option.map(HttpServerRequest.toURL(request), ({ origin }) => origin);5859const gateway = Effect.gen(function* () {60 const request = yield* Effect.serviceOption(61 HttpServerRequest.HttpServerRequest62 );6364 return yield* Option.match(Option.flatMap(request, originOf), {65 onNone: () => Effect.service(GatewayUrl),66 onSome: Effect.succeed,67 });68});6970const remoteOf = (repo: RepoName | ProjectName) =>Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 71–75why, one step back71 Effect.map(72 gateway,73 (origin) =>74 `${origin}/${repo.includes("/") ? repo : Option.getOrElse(addressOfRepo(RepoName.make(repo)), () => repo)}.git`75 );Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 76–135why, one step back7677const atGateway = (claim: ClaimView) =>78 Effect.map(remoteOf(claim.fork), (remote): ClaimView => ({79 ...claim,80 remote,81 }));8283const statusAtGateway = (84 who: Person,85 status: {86 readonly answers: readonly AnswerView[];87 readonly claims: readonly ClaimView[];88 }89) =>90 Effect.map(Effect.forEach(status.claims, atGateway), (claims) => ({91 answers: status.answers,92 claims,93 handle: who.handle,94 }));9596const byExpiry: Order.Order<Decision> = Order.combineAll([97 Order.mapInput(DateTime.Order, ({ expiresAt }: Decision) => expiresAt),98 Order.mapInput(DateTime.Order, ({ openedAt }: Decision) => openedAt),99 Order.mapInput(Order.String, ({ project }: Decision) => project),100 Order.mapInput(Order.String, ({ id }: Decision) => id),101]);102103const acrossProjects = (104 decider: Person,105 pages: readonly Today[]106): Effect.Effect<Today, NotDecider> => {107 const sumOf = (pick: (page: Today) => number) =>108 Number.sumAll(pages.map(pick));109110 return Array.isReadonlyArrayNonEmpty(pages)111 ? Effect.succeed({112 budget: {113 finishes: sumOf(({ budget }) => budget.finishes),114 total: sumOf(({ budget }) => budget.total),115 used: sumOf(({ budget }) => budget.used),116 },117 decider: decider.handle,118 decisions: Array.sort(119 pages.flatMap(({ decisions }) => decisions),120 byExpiry121 ),122 expiringSoon: pages.flatMap(({ expiringSoon }) => expiringSoon),123 newcomers: Array.dedupeWith(124 pages.flatMap(({ newcomers }) => newcomers),125 sameHandle126 ),127 overflow: sumOf(({ overflow }) => overflow),128 unattended: pages.flatMap(({ unattended }) => unattended),129 })130 : Effect.fail(new NotDecider({}));131};132133export const Handlers: Layer.Layer<134 Rpc.ToHandler<RpcGroup.Rpcs<typeof Contracts>>,135 never,Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 136why, one step back136 Projects | FrontPage | RateLimits | EvidenceStore | RepoHostMerge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 137–142why, one step back137> = Contracts.toLayer(138 Effect.gen(function* () {139 const projects = yield* Projects;140 const frontPage = yield* FrontPage;141 const rateLimits = yield* RateLimits;142 const evidence = yield* EvidenceStore;Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 143why, one step back143 const host = yield* RepoHost;Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 144–149why, one step back144145 const on = <A, E>(146 project: ProjectName,147 call: (api: ProjectApi) => Effect.Effect<A, E>148 ) => Effect.flatMap(projects.get(project), call);149Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 150–159why, one step back150 const code = <A, E>(151 project: ProjectName,152 read: (of: CodeProject) => Effect.Effect<A, E, RepoHost>153 ) =>154 on(project, (api) =>155 read(codeProjectOf(api, project)).pipe(156 Effect.provideService(RepoHost, host)157 )158 );159Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 160–200why, one step back160 const everywhere = <A, E>(161 call: (api: ProjectApi) => Effect.Effect<Option.Option<A>, E>162 ) =>163 Effect.gen(function* () {164 const listed = yield* frontPage.projects();165166 const names = Array.sort(167 Array.dedupe(listed.map(({ project }) => project)),168 Order.String169 );170171 const each = yield* Effect.forEach(172 names,173 (project) =>174 Effect.flatMap(175 Effect.option(projects.get(project)),176 Option.match({ onNone: () => Effect.succeedNone, onSome: call })177 ),178 { concurrency: FAN_OUT }179 );180181 return Array.getSomes(each);182 });183184 return {185 claim: ({ ask, project }) =>186 Effect.gen(function* () {187 const who = yield* person;188189 yield* rateLimits.check("claim", who.handle);190191 const { claim } = yield* on(project, (api) => api.claim(who, ask));192193 return { claim: yield* atGateway(claim) };194 }),195 credits: ({ handle }) =>196 Effect.map(frontPage.credits(handle), (credits) => ({ credits })),197 decide: ({ project, ...input }) =>198 Effect.flatMap(person, (who) =>199 on(project, (api) => api.decide(who, input))200 ),Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 201–208why, one step back201 draftAsk: ({ project, ...input }) =>202 Effect.gen(function* () {203 const who = yield* person;204205 yield* rateLimits.check("draft", who.handle);206207 return yield* on(project, (api) => api.draftAsk(who, input));208 }),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 209–245why, one step back209 evidence: ({ project, ref }) =>210 Effect.flatMap(person, (who) =>211 on(project, (api) =>212 api213 .evidence(who, LogRef.make(ref))214 .pipe(Effect.provideService(EvidenceStore, evidence))215 )216 ),217 [EnvSchemaOf._tag]: ({ project }) =>218 Effect.flatMap(caller, (who) =>219 on(project, (api) => api[envSchemaMethod](who))220 ),221 finish: ({ project, ...input }) =>222 Effect.flatMap(person, (who) =>223 on(project, (api) => api.finish(who, input))224 ),225 flagAsk: ({ project, ...input }) =>226 Effect.gen(function* () {227 const who = yield* person;228229 yield* rateLimits.check("proposal", who.handle);230231 return yield* on(project, (api) => api.flagAsk(who, input));232 }),233 fork: ({ project }) =>234 Effect.gen(function* () {235 const who = yield* person;236 const { fork } = yield* on(project, (api) => api.fork(who));237238 return { fork, remote: yield* remoteOf(fork) };239 }),240 frontPage: (filter) =>241 Effect.map(frontPage.entries(filter), (entries) => ({ entries })),242 initProject: ({ project, ...input }) =>243 Effect.gen(function* () {244 const who = yield* person;245Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 246why, one step back246 // A project an earlier call made but could not start is this call's to start;Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 247–273why, one step back247 // call's to start; one already started refuses it itself.248 const api = yield* projects249 .create(project)250 .pipe(251 Effect.catchTag("ProjectExists", () =>252 Effect.orDie(projects.get(project))253 )254 );255256 const made = yield* api.initProject(who, input);257258 return { ...made, remote: yield* remoteOf(made.project) };259 }),260 land: ({ project, ...input }) =>261 Effect.flatMap(person, (who) =>262 on(project, (api) => api.land(who, input))263 ),264 landing: ({ project, sha }) =>265 Effect.flatMap(caller, (who) =>266 on(project, (api) => api.landing(who, sha))267 ),268 ledger: ({ project }) =>269 Effect.flatMap(caller, (who) => on(project, (api) => api.ledger(who))),270 listAsks: ({ project }) =>271 Effect.flatMap(caller, (who) =>272 on(project, (api) => api.listAsks(who))273 ),Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 274–275why, one step back274 listPaths: ({ at, project }) =>275 code(project, (of) => pathsOf(of, Option.fromUndefinedOr(at))),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 276why, one step back276 listProjects: () =>Merge build/1102 into mainoscabrielbefore adoption084383d
lines 277–279why, one step back277 frontPage.projects().pipe(278 Effect.flatMap(withStandingPosture),279 Effect.provideService(Projects, projects),Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 280–287why, one step back280 Effect.flatMap((listed) =>281 withDescriptions(listed, (project) =>282 Effect.map(projects.get(project), (api) =>283 landedProjectOf(api, project)284 )285 )286 ),287 Effect.provideService(RepoHost, host),Merge build/1102 into mainoscabrielbefore adoption084383d
lines 288–289why, one step back288 Effect.map((listed) => ({ projects: listed }))289 ),Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 290–299why, one step back290 listRefs: ({ project }) => code(project, refsOf),291 log: ({ at, from = 0, limit = LOG_PAGE, path, project }) =>292 code(project, (of) =>293 logOf(of, {294 at: Option.fromUndefinedOr(at),295 from,296 limit,297 path: Option.fromUndefinedOr(path),298 })299 ),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 300–303why, one step back300 openAsk: ({ project, ...input }) =>301 Effect.flatMap(person, (who) =>302 on(project, (api) => api.openAsk(who, input))303 ),Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
line 304why, one step back304 projectPolicy: ({ project }) => on(project, (api) => api.policy()),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 305–307why, one step back305 proposeAsk: ({ project, ...input }) =>306 Effect.gen(function* () {307 const who = yield* person;Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f
line 308why, one step back308Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 309why, one step back309 // A draft spends the draft limit first, then the proposal limit.Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f
lines 310–312why, one step back310 if (input.draft === true) {311 yield* rateLimits.check("draft", who.handle);312 }Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 313–317why, one step back313314 yield* rateLimits.check("proposal", who.handle);315316 return yield* on(project, (api) => api.proposeAsk(who, input));317 }),Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 318–323why, one step back318 readFile: ({ at, path, project }) =>319 code(project, (of) => fileOf(of, Option.fromUndefinedOr(at), path)),320 readTree: ({ at, path, project }) =>321 code(project, (of) =>322 treeOf(of, Option.fromUndefinedOr(at), Option.fromUndefinedOr(path))323 ),Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 324–326why, one step back324 // Read as nobody: the list ignores the caller, so every caller's read is the same.325 recentLandings: () =>326 Effect.map(frontPage.recentLandings(), (landings) => ({ landings })),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 327–338why, one step back327 release: ({ ask, project }) =>328 Effect.flatMap(person, (who) =>329 on(project, (api) => api.release(who, ask))330 ),331 setEnv: ({ project, ...input }) =>332 Effect.flatMap(person, (who) =>333 on(project, (api) => api.setEnv(who, input))334 ),335 showAsk: ({ ask, project }) =>336 Effect.flatMap(caller, (who) =>337 on(project, (api) => api.showAsk(who, ask))338 ),Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 339–340why, one step back339 showCommit: ({ at, project, sha }) =>340 code(project, (of) => commitOf(of, sha, Option.fromUndefinedOr(at))),Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 341–344why, one step back341 showProposal: ({ project, proposal }) =>342 Effect.flatMap(caller, (who) =>343 on(project, (api) => api.showProposal(who, proposal))344 ),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 345–397why, one step back345 status: ({ project }) =>346 Effect.gen(function* () {347 const who = yield* person;348349 if (project !== undefined) {350 return yield* statusAtGateway(351 who,352 yield* on(project, (api) => api.status(who))353 );354 }355356 const each = yield* everywhere((api) =>357 Effect.map(api.status(who), Option.some)358 );359360 return yield* statusAtGateway(who, {361 answers: each.flatMap(({ answers }) => answers),362 claims: each.flatMap(({ claims }) => claims),363 });364 }),365 submitAnswer: ({ project, ...input }) =>366 Effect.flatMap(person, (who) =>367 on(project, (api) => api.submitAnswer(who, input))368 ),369 today: ({ project }) =>370 Effect.gen(function* () {371 const who = yield* person;372373 if (project !== undefined) {374 return yield* on(project, (api) => api.today(who));375 }376377 const pages = yield* everywhere((api) =>378 api.today(who).pipe(379 Effect.map(Option.some),380 Effect.catchTag("NotDecider", () => Effect.succeedNone)381 )382 );383384 return yield* acrossProjects(who, pages);385 }),386 verdict: ({ project, ...input }) =>387 Effect.flatMap(caller, (who) =>388 on(project, (api) => api.verdict(who, input))389 ),390 vouch: ({ project, ...input }) =>391 Effect.flatMap(person, (who) =>392 on(project, (api) => api.vouch(who, input))393 ),394 watch: (payload) =>395 Stream.unwrap(396 Effect.map(servedFramed, (live) => watchHandler(payload, { live }))397 ).pipe(Stream.provideService(Projects, projects)),Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 398–399why, one step back398 why: ({ at, path, project }) =>399 code(project, (of) => whyOf(of, Option.fromUndefinedOr(at), path)),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 400–402why, one step back400 };401 })402);