prompt.ts
This file
Path
packages/drafting/src/prompt.ts
118 lines · 4.1 KiB · TypeScript
Last changed by
Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
History of this fileTake it
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 1why, one step back1// The policy digest reads only a `PublicPolicy`'s kinds, modules, run blockMerge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
line 2why, one step back2// and maintainers, so no layer, held-back check or reason can reach a prompt.Merge branch 'build/908-drafter-workers-ai'oscabrielbefore adoption004e40e
lines 3–32why, one step back3import { Brand } from "@repo/brand";4import type { PublicPolicy } from "@repo/protocol/domain/policy";5import type { Refusal } from "@repo/protocol/refusal";6import { explain } from "@repo/protocol/render";7import { Option } from "effect";89import type { DraftInput } from "./drafted.ts";1011const tailBytes = 4 * 1024;1213export interface Feedback {14 readonly diagnosis: string;15 readonly excerpt: string;16 readonly refusal: Refusal;17}1819export const policyDigest = (20 policy: PublicPolicy,21 askTests: Option.Option<string>22): string =>23 [24 `Ask kinds: ${policy.kinds.map(({ checkKind, name }) => `${name} (${checkKind})`).join(", ")}.`,25 `Run one test file with: ${policy.run.test}`,26 ...Object.entries(policy.run.checks).map(27 ([lane, command]) => `Check lane ${lane}: ${command}`28 ),29 ...policy.modules.map(30 ({ module, paths }) => `Module ${module}: ${paths.join(", ")}`31 ),32 ...policy.deciders.map(Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
line 33why, one step back33 ({ handles, paths }) => `Maintainers for ${paths}: ${handles.join(", ")}`Merge branch 'build/908-drafter-workers-ai'oscabrielbefore adoption004e40e
lines 34–54why, one step back34 ),35 Option.match(askTests, {36 onNone: () =>37 "Ask tests: no ask exists yet; put the test beside the project's other tests.",38 onSome: (folder) => `Ask tests go in: ${folder}/`,39 }),40 ].join("\n");4142export const systemPrompt = (digest: string): string =>43 [44 `You draft one ask for a project hosted on ${Brand.name}. An ask is a file in which a project states one change it wants: a behavioural want, a territory, and a test that fails on main today.`,45 "",46 "Return only JSON that matches the given schema. Its fields:",47 "- id: a short kebab-case name for the ask.",48 '- kind: always "fix".',49 "- want: one testable sentence saying how the code should behave. It names no file and no line.",50 "- current: what happens on main today, when it helps.",51 "- outOfScope: what an answer should leave alone, when it helps.",52 "- context: facts an answerer needs that the code does not say, when they help.",53 "- territory: the smallest set of existing files an answer must change.",54 "- test.path and test.source: the ask's test file.",Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
line 55why, one step back55 "- rationale: why this draft reads the want this way, for the maintainer.",Merge branch 'build/908-drafter-workers-ai'oscabrielbefore adoption004e40e
lines 56–118why, one step back56 "",57 "Rules for the test:",58 "- It must fail on the current code by an assertion (an expect that fails), never by a missing import, export or file.",59 "- Import only symbols that exist in the files you were shown.",60 "- Keep it deterministic: no network, no clock (use fixed dates), no randomness.",61 "- Put it in the project's ask-test folder, named after the ask's id.",62 "- Write no other file.",63 "",64 "The project:",65 digest,66 ].join("\n");6768const encoder = new TextEncoder();6970const decoder = new TextDecoder();7172const tailOf = (output: string) => {73 const bytes = encoder.encode(output);7475 if (bytes.length <= tailBytes) {76 return output;77 }7879 const tail = decoder80 .decode(bytes.slice(bytes.length - tailBytes))81 .replace(/^�/u, "");8283 return tail.includes("\n") ? tail.slice(tail.indexOf("\n") + 1) : tail;84};8586export const feedbackText = (feedback: Feedback): string =>87 [88 ...explain([feedback.refusal]),89 `diagnosis: ${feedback.diagnosis}`,90 "output (its end):",91 tailOf(feedback.excerpt),92 ].join("\n");9394export const userPrompt = (input: DraftInput): string =>95 [96 `Project: ${input.context.project}, at ${input.context.at}.`,97 `Want: ${input.want}`,98 ...(input.territory === undefined99 ? []100 : [`The person who asked named these paths: ${input.territory}`]),101 ...(input.note === undefined102 ? []103 : [`A note for this draft: ${input.note}`]),104 "",105 "Files from the project:",106 ...input.context.files.flatMap(({ content, path }) => [107 `--- ${path} ---`,108 content,109 `--- end of ${path} ---`,110 ]),111 ...(input.feedback === undefined112 ? []113 : [114 "",115 `Attempt ${input.attempt - 1} was refused. Fix what this says, and keep the rest:`,116 input.feedback,117 ]),118 ].join("\n");