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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329import { contentHash, describedIn } from "@repo/agent-kit/generated-file";import { contributeDescription, contributeSkillName, contributeSkillPaths,} from "@repo/agent-kit/project-skill";import { wellKnownSkills } from "@repo/agent-kit/well-known";import { RefName, RepoHost, Revision } from "@repo/artifacts/repo-host";import { CodeStore } from "@repo/project/code-store";import { CodeCache } from "@repo/project/code/cache";import { FrontPage } from "@repo/project/front-page";import { landedRef } from "@repo/project/lander";import { Projects } from "@repo/project/projects";import type { ProjectListing } from "@repo/protocol/domain/front-page";import { keyOf, nameOfKey, ProjectKey, RepoName, RepoPath,} from "@repo/protocol/ids";import type { ProjectName, CommitSha } from "@repo/protocol/ids";import type { Layer } from "effect";import { Array as Arr, Cause, Context, DateTime, Effect, Option, Order, Schema,} from "effect";import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
const ROOT = "/.well-known/agent-skills";
const INDEX = `${ROOT}/index.json`;
const CONTRIBUTE = "-contribute";
const FAN_OUT = 8;
const BRIEFLY = "public, max-age=60";
const notFound = () => new Response(null, { headers: { "cache-control": "no-store" }, status: 404 });
const skillUrl = (project: ProjectName) => `${ROOT}/${contributeSkillName(project)}/SKILL.md`;
const mainRef = RefName.make("refs/heads/main");
// UTF-8 that round-trips byte for byte, so the digest is the file's.const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
const textOf = (bytes: Uint8Array) => Option.liftThrowable(() => utf8.decode(bytes))();
const SKILL_VERSION = 1;
const KeptSkill = Schema.fromJsonString( Schema.Struct({ content: Schema.optionalKey(Schema.String) }));
const decodeKept = Schema.decodeOption(KeptSkill);
const encodeKept = Schema.encodeEffect(KeptSkill);
const landedOf = ( project: ProjectName): Effect.Effect<Option.Option<CommitSha>, never, Projects | RepoHost> => Effect.gen(function* () { const projects = yield* Projects;
const recorded = yield* Effect.suspend(() => projects.get(project)).pipe( Effect.flatMap((api) => api.head()), Effect.map(({ landed }) => Option.fromUndefinedOr(landed)), Effect.catchCause((cause) => Cause.hasInterruptsOnly(cause) ? Effect.interrupt : Effect.succeedNone ) );
if (Option.isSome(recorded)) { return Option.some<CommitSha>(recorded.value); }
const host = yield* RepoHost; const refs = yield* host.refs(RepoName.make(keyOf(project)));
return Option.orElse(Option.fromUndefinedOr(refs.get(landedRef)), () => Option.fromUndefinedOr(refs.get(mainRef)) ); }).pipe(Effect.catchTag("RepoNotFound", () => Effect.succeedNone));
const skillAt = (project: ProjectName, landed: CommitSha) => Effect.gen(function* () { const host = yield* RepoHost; const [path] = contributeSkillPaths(project);
const bytes = yield* host.readFile( RepoName.make(keyOf(project)), Revision.Commit({ sha: landed }), RepoPath.make(path) );
return Option.flatMap(bytes, textOf); }).pipe(Effect.catchTag("RepoNotFound", () => Effect.succeedNone));
const keptSkillAt = (project: ProjectName, landed: CommitSha) => Effect.gen(function* () { const cache = yield* CodeCache; const store = yield* CodeStore; const key = `${keyOf(project)}/skill/v${SKILL_VERSION}/${landed}`;
return yield* cache.through( key, Effect.gen(function* () { const kept = Option.flatMap(yield* store.get(key), decodeKept);
if (Option.isSome(kept)) { return Option.fromUndefinedOr(kept.value.content); }
const read = yield* skillAt(project, landed);
yield* encodeKept( Option.match(read, { onNone: () => ({}), onSome: (content) => ({ content }), }) ).pipe( Effect.flatMap((text) => store.put(key, text)), Effect.catch((error) => Effect.logWarning(`${project}'s skill was not kept`, error) ) );
return read; }) ); });
const landedSkill = ( project: ProjectName): Effect.Effect<Option.Option<string>, never, Projects | RepoHost> => Effect.flatMap(landedOf(project), (landed) => Option.match(landed, { onNone: () => Effect.succeedNone, onSome: (sha) => keptSkillAt(project, sha), }) );
const decodeKey = Schema.decodeUnknownOption(ProjectKey);
const INDEX_VERSION = 1;
const KeptIndex = Schema.fromJsonString( Schema.Struct({ content: Schema.String }));
const decodeKeptIndex = Schema.decodeOption(KeptIndex);
const encodeKeptIndex = Schema.encodeEffect(KeptIndex);
const indexOf = (projects: readonly ProjectName[]) => Effect.gen(function* () { const found = yield* Effect.forEach( projects, (project) => Effect.map( landedSkill(project), Option.map((content) => ({ content, description: Option.getOrElse(describedIn(content), () => contributeDescription(project) ), name: contributeSkillName(project), url: skillUrl(project), })) ), { concurrency: FAN_OUT } );
return (yield* wellKnownSkills(Arr.getSomes(found))).content; });
// A project's main moves by a landing alone, and each listing carries the// instant of its project's last one, so the index is the same while no listed// project lands. It is kept under the listing's digest: a request then reads// one listing and one kept file, and wakes no project. No project's key holds// `--`, so the kept index shares no key with a project's.const indexKey = (listed: readonly ProjectListing[]) => Effect.map( contentHash( JSON.stringify( listed.map(({ landedAt, project }) => [ project, landedAt === undefined ? null : DateTime.formatIso(landedAt), ]) ) ), (digest) => `agent-skills--index/v${INDEX_VERSION}/${digest}` );
const byProject = Order.mapInput( Order.String, ({ project }: ProjectListing) => project);
const index = Effect.gen(function* () { const listed = Arr.sort( Arr.dedupeWith( yield* FrontPage.use((frontPage) => frontPage.projects()), (a, b) => a.project === b.project ), byProject );
const cache = yield* CodeCache; const store = yield* CodeStore; const key = yield* indexKey(listed);
const content = yield* cache.through( key, Effect.gen(function* () { const kept = Option.flatMap(yield* store.get(key), decodeKeptIndex);
if (Option.isSome(kept)) { return kept.value.content; }
const built = yield* indexOf(listed.map(({ project }) => project));
yield* encodeKeptIndex({ content: built }).pipe( Effect.flatMap((text) => store.put(key, text)), Effect.catch((error) => Effect.logWarning("the agent skills index was not kept", error) ) );
return built; }) );
return new Response(content, { headers: { "cache-control": BRIEFLY, "content-type": "application/json" }, });});
const projectNamed = (name: string) => name.endsWith(CONTRIBUTE) ? Option.flatMap(decodeKey(name.slice(0, -CONTRIBUTE.length)), nameOfKey) : Option.none();
const skillFile = (project: ProjectName) => Effect.map( landedSkill(project), Option.match({ onNone: notFound, onSome: (content) => new Response(content, { headers: { "cache-control": BRIEFLY, "content-type": "text/markdown; charset=utf-8", }, }), }) );
export const agentSkills = ( request: Request): Effect.Effect<Response, never, FrontPage | Projects | RepoHost> => { const { pathname } = new URL(request.url);
if (pathname === INDEX) { return index; }
const [name = "", file = "", ...rest] = pathname .slice(ROOT.length + 1) .split("/");
const project = projectNamed(name);
if ( !pathname.startsWith(`${ROOT}/`) || file !== "SKILL.md" || rest.length > 0 || Option.isNone(project) ) { return Effect.succeed(notFound()); }
return skillFile(project.value);};
const readOnly = HttpServerResponse.empty({ headers: { allow: "GET, HEAD", "cache-control": "no-store" }, status: 405,});
export const agentSkillRoutes: Layer.Layer< never, never, HttpRouter.HttpRouter | FrontPage | Projects | RepoHost> = HttpRouter.use((router) => Effect.gen(function* () { const services = Context.make(FrontPage, yield* FrontPage).pipe( Context.add(Projects, yield* Projects), Context.add(RepoHost, yield* RepoHost), Context.add(CodeCache, yield* CodeCache), Context.add(CodeStore, yield* CodeStore) );
yield* router.add("*", `${ROOT}/*`, (request) => request.method !== "GET" && request.method !== "HEAD" ? Effect.succeed(readOnly) : HttpServerRequest.toWeb(request).pipe( Effect.flatMap((web) => Effect.provide(agentSkills(web), services)), Effect.map(HttpServerResponse.fromWeb), Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 400 }) ) ) ); }));