Skip to content
Myr

well-known.ts

This file

Path

apps/platform/src/well-known.ts

328 lines · 9.2 KiB · TypeScript

Last changed by

Merge perf/agent-skills-index into mainoscabrielbefore adoption25f9e66

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge perf/agent-skills-index into mainoscabrielbefore adoption25f9e66

line 1why, one step back
1import { contentHash, describedIn } from "@repo/agent-kit/generated-file";

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 2–8why, one step back
2import {
3 contributeDescription,4 contributeSkillName,5 contributeSkillPaths,6} from "@repo/agent-kit/project-skill";7import { wellKnownSkills } from "@repo/agent-kit/well-known";8import { RefName, RepoHost, Revision } from "@repo/artifacts/repo-host";

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 9–10why, one step back
9import { CodeStore } from "@repo/project/code-store";
10import { CodeCache } from "@repo/project/code/cache";

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 11–12why, one step back
11import { FrontPage } from "@repo/project/front-page";
12import { landedRef } from "@repo/project/lander";

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 13why, one step back
13import { Projects } from "@repo/project/projects";

Merge perf/agent-skills-index into mainoscabrielbefore adoption25f9e66

line 14why, one step back
14import type { ProjectListing } from "@repo/protocol/domain/front-page";

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 15–22why, one step back
15import {
16 keyOf,17 nameOfKey,18 ProjectKey,19 RepoName,20 RepoPath,21} from "@repo/protocol/ids";22import type { ProjectName, CommitSha } 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

line 23why, one step back
23import type { Layer } from "effect";

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 24–27why, one step back
24import {
25 Array as Arr,26 Cause,27 Context,

Merge perf/agent-skills-index into mainoscabrielbefore adoption25f9e66

line 28why, one step back
28 DateTime,

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 29–33why, one step back
29 Effect,
30 Option,31 Order,32 Schema,33} from "effect";

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 34–53why, one step back
34import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
3536const ROOT = "/.well-known/agent-skills";3738const INDEX = `${ROOT}/index.json`;3940const CONTRIBUTE = "-contribute";4142const FAN_OUT = 8;4344const BRIEFLY = "public, max-age=60";4546const notFound = () =>47 new Response(null, { headers: { "cache-control": "no-store" }, status: 404 });4849const skillUrl = (project: ProjectName) =>50 `${ROOT}/${contributeSkillName(project)}/SKILL.md`;5152const mainRef = RefName.make("refs/heads/main");53

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 54why, one step back
54// UTF-8 that round-trips byte for byte, so the digest is the file's.

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 55–59why, one step back
55const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
5657const textOf = (bytes: Uint8Array) =>58 Option.liftThrowable(() => utf8.decode(bytes))();59

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 60–70why, one step back
60const SKILL_VERSION = 1;
6162const KeptSkill = Schema.fromJsonString(63 Schema.Struct({ content: Schema.optionalKey(Schema.String) })64);6566const decodeKept = Schema.decodeOption(KeptSkill);6768const encodeKept = Schema.encodeEffect(KeptSkill);6970const landedOf = (

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 71why, one step back
71 project: ProjectName

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 72why, one step back
72): Effect.Effect<Option.Option<CommitSha>, never, Projects | 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

line 73why, one step back
73 Effect.gen(function* () {

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 74why, one step back
74 const projects = yield* Projects;

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 75why, one step back
75

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 76–81why, one step back
76 const recorded = yield* Effect.suspend(() => projects.get(project)).pipe(
77 Effect.flatMap((api) => api.head()),78 Effect.map(({ landed }) => Option.fromUndefinedOr(landed)),79 Effect.catchCause((cause) =>80 Cause.hasInterruptsOnly(cause) ? Effect.interrupt : Effect.succeedNone81 )

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 82–83why, one step back
82 );
83

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 84–85why, one step back
84 if (Option.isSome(recorded)) {
85 return Option.some<CommitSha>(recorded.value);

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 86–87why, one step back
86 }
87

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 88why, one step back
88 const host = yield* RepoHost;

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 89why, one step back
89 const refs = yield* host.refs(RepoName.make(keyOf(project)));

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 90–98why, one step back
90
91 return Option.orElse(Option.fromUndefinedOr(refs.get(landedRef)), () =>92 Option.fromUndefinedOr(refs.get(mainRef))93 );94 }).pipe(Effect.catchTag("RepoNotFound", () => Effect.succeedNone));9596const skillAt = (project: ProjectName, landed: CommitSha) =>97 Effect.gen(function* () {98 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 99–101why, one step back
99 const [path] = contributeSkillPaths(project);
100101 const bytes = yield* host.readFile(

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 102why, one step back
102 RepoName.make(keyOf(project)),

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 103why, one step back
103 Revision.Commit({ sha: landed }),

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 104–109why, one step back
104 RepoPath.make(path)
105 );106107 return Option.flatMap(bytes, textOf);108 }).pipe(Effect.catchTag("RepoNotFound", () => Effect.succeedNone));109

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 110–113why, one step back
110const keptSkillAt = (project: ProjectName, landed: CommitSha) =>
111 Effect.gen(function* () {112 const cache = yield* CodeCache;113 const store = yield* CodeStore;

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 114why, one step back
114 const key = `${keyOf(project)}/skill/v${SKILL_VERSION}/${landed}`;

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 115–153why, one step back
115
116 return yield* cache.through(117 key,118 Effect.gen(function* () {119 const kept = Option.flatMap(yield* store.get(key), decodeKept);120121 if (Option.isSome(kept)) {122 return Option.fromUndefinedOr(kept.value.content);123 }124125 const read = yield* skillAt(project, landed);126127 yield* encodeKept(128 Option.match(read, {129 onNone: () => ({}),130 onSome: (content) => ({ content }),131 })132 ).pipe(133 Effect.flatMap((text) => store.put(key, text)),134 Effect.catch((error) =>135 Effect.logWarning(`${project}'s skill was not kept`, error)136 )137 );138139 return read;140 })141 );142 });143144const landedSkill = (145 project: ProjectName146): Effect.Effect<Option.Option<string>, never, Projects | RepoHost> =>147 Effect.flatMap(landedOf(project), (landed) =>148 Option.match(landed, {149 onNone: () => Effect.succeedNone,150 onSome: (sha) => keptSkillAt(project, sha),151 })152 );153

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 154why, one step back
154const decodeKey = Schema.decodeUnknownOption(ProjectKey);

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 155why, one step back
155

Merge perf/agent-skills-index into mainoscabrielbefore adoption25f9e66

line 156why, one step back
156const INDEX_VERSION = 1;

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 157why, one step back
157

Merge perf/agent-skills-index into mainoscabrielbefore adoption25f9e66

lines 158–203why, one step back
158const KeptIndex = Schema.fromJsonString(
159 Schema.Struct({ content: Schema.String })160);161162const decodeKeptIndex = Schema.decodeOption(KeptIndex);163164const encodeKeptIndex = Schema.encodeEffect(KeptIndex);165166const indexOf = (projects: readonly ProjectName[]) =>167 Effect.gen(function* () {168 const found = yield* Effect.forEach(169 projects,170 (project) =>171 Effect.map(172 landedSkill(project),173 Option.map((content) => ({174 content,175 description: Option.getOrElse(describedIn(content), () =>176 contributeDescription(project)177 ),178 name: contributeSkillName(project),179 url: skillUrl(project),180 }))181 ),182 { concurrency: FAN_OUT }183 );184185 return (yield* wellKnownSkills(Arr.getSomes(found))).content;186 });187188// A project's main moves by a landing alone, and each listing carries the189// instant of its project's last one, so the index is the same while no listed190// project lands. It is kept under the listing's digest: a request then reads191// one listing and one kept file, and wakes no project. No project's key holds192// `--`, so the kept index shares no key with a project's.193const indexKey = (listed: readonly ProjectListing[]) =>194 Effect.map(195 contentHash(196 JSON.stringify(197 listed.map(({ landedAt, project }) => [198 project,199 landedAt === undefined ? null : DateTime.formatIso(landedAt),200 ])201 )202 ),203 (digest) => `agent-skills--index/v${INDEX_VERSION}/${digest}`

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 204–205why, one step back
204 );
205

Merge perf/agent-skills-index into mainoscabrielbefore adoption25f9e66

lines 206–217why, one step back
206const byProject = Order.mapInput(
207 Order.String,208 ({ project }: ProjectListing) => project209);210211const index = Effect.gen(function* () {212 const listed = Arr.sort(213 Arr.dedupeWith(214 yield* FrontPage.use((frontPage) => frontPage.projects()),215 (a, b) => a.project === b.project216 ),217 byProject

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 218–219why, one step back
218 );
219

Merge perf/agent-skills-index into mainoscabrielbefore adoption25f9e66

lines 220–222why, one step back
220 const cache = yield* CodeCache;
221 const store = yield* CodeStore;222 const key = yield* indexKey(listed);

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 223why, one step back
223

Merge perf/agent-skills-index into mainoscabrielbefore adoption25f9e66

lines 224–246why, one step back
224 const content = yield* cache.through(
225 key,226 Effect.gen(function* () {227 const kept = Option.flatMap(yield* store.get(key), decodeKeptIndex);228229 if (Option.isSome(kept)) {230 return kept.value.content;231 }232233 const built = yield* indexOf(listed.map(({ project }) => project));234235 yield* encodeKeptIndex({ content: built }).pipe(236 Effect.flatMap((text) => store.put(key, text)),237 Effect.catch((error) =>238 Effect.logWarning("the agent skills index was not kept", error)239 )240 );241242 return built;243 })244 );245246 return new Response(content, {

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–252why, one step back
247 headers: { "cache-control": BRIEFLY, "content-type": "application/json" },
248 });249});250251const projectNamed = (name: string) =>252 name.endsWith(CONTRIBUTE)

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 253why, one step back
253 ? Option.flatMap(decodeKey(name.slice(0, -CONTRIBUTE.length)), nameOfKey)

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 254–272why, one step back
254 : Option.none();
255256const skillFile = (project: ProjectName) =>257 Effect.map(258 landedSkill(project),259 Option.match({260 onNone: notFound,261 onSome: (content) =>262 new Response(content, {263 headers: {264 "cache-control": BRIEFLY,265 "content-type": "text/markdown; charset=utf-8",266 },267 }),268 })269 );270271export const agentSkills = (272 request: Request

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 273why, one step back
273): Effect.Effect<Response, never, FrontPage | Projects | 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 274–305why, one step back
274 const { pathname } = new URL(request.url);
275276 if (pathname === INDEX) {277 return index;278 }279280 const [name = "", file = "", ...rest] = pathname281 .slice(ROOT.length + 1)282 .split("/");283284 const project = projectNamed(name);285286 if (287 !pathname.startsWith(`${ROOT}/`) ||288 file !== "SKILL.md" ||289 rest.length > 0 ||290 Option.isNone(project)291 ) {292 return Effect.succeed(notFound());293 }294295 return skillFile(project.value);296};297298const readOnly = HttpServerResponse.empty({299 headers: { allow: "GET, HEAD", "cache-control": "no-store" },300 status: 405,301});302303export const agentSkillRoutes: Layer.Layer<304 never,305 never,

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 306why, one step back
306 HttpRouter.HttpRouter | FrontPage | Projects | 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 307–309why, one step back
307> = HttpRouter.use((router) =>
308 Effect.gen(function* () {309 const services = Context.make(FrontPage, yield* FrontPage).pipe(

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 310–313why, one step back
310 Context.add(Projects, yield* Projects),
311 Context.add(RepoHost, yield* RepoHost),312 Context.add(CodeCache, yield* CodeCache),313 Context.add(CodeStore, yield* CodeStore)

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 314–328why, one step back
314 );
315316 yield* router.add("*", `${ROOT}/*`, (request) =>317 request.method !== "GET" && request.method !== "HEAD"318 ? Effect.succeed(readOnly)319 : HttpServerRequest.toWeb(request).pipe(320 Effect.flatMap((web) => Effect.provide(agentSkills(web), services)),321 Effect.map(HttpServerResponse.fromWeb),322 Effect.orElseSucceed(() =>323 HttpServerResponse.empty({ status: 400 })324 )325 )326 );327 })328);