@aid-framework/generator-ts-vercel 0.0.0 → 0.1.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +123 -0
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/emit/agents.d.ts +62 -0
  5. package/dist/emit/agents.d.ts.map +1 -0
  6. package/dist/emit/agents.js +256 -0
  7. package/dist/emit/agents.js.map +1 -0
  8. package/dist/emit/business.d.ts +23 -0
  9. package/dist/emit/business.d.ts.map +1 -0
  10. package/dist/emit/business.js +414 -0
  11. package/dist/emit/business.js.map +1 -0
  12. package/dist/emit/control.d.ts +40 -0
  13. package/dist/emit/control.d.ts.map +1 -0
  14. package/dist/emit/control.js +108 -0
  15. package/dist/emit/control.js.map +1 -0
  16. package/dist/emit/file.d.ts +64 -0
  17. package/dist/emit/file.d.ts.map +1 -0
  18. package/dist/emit/file.js +80 -0
  19. package/dist/emit/file.js.map +1 -0
  20. package/dist/emit/guardrails.d.ts +45 -0
  21. package/dist/emit/guardrails.d.ts.map +1 -0
  22. package/dist/emit/guardrails.js +192 -0
  23. package/dist/emit/guardrails.js.map +1 -0
  24. package/dist/emit/infrastructure.d.ts +15 -0
  25. package/dist/emit/infrastructure.d.ts.map +1 -0
  26. package/dist/emit/infrastructure.js +238 -0
  27. package/dist/emit/infrastructure.js.map +1 -0
  28. package/dist/emit/pipelines.d.ts +46 -0
  29. package/dist/emit/pipelines.d.ts.map +1 -0
  30. package/dist/emit/pipelines.js +1015 -0
  31. package/dist/emit/pipelines.js.map +1 -0
  32. package/dist/emit/prompts.d.ts +14 -0
  33. package/dist/emit/prompts.d.ts.map +1 -0
  34. package/dist/emit/prompts.js +61 -0
  35. package/dist/emit/prompts.js.map +1 -0
  36. package/dist/emit/retrieval.d.ts +19 -0
  37. package/dist/emit/retrieval.d.ts.map +1 -0
  38. package/dist/emit/retrieval.js +206 -0
  39. package/dist/emit/retrieval.js.map +1 -0
  40. package/dist/emit/schemas.d.ts +22 -0
  41. package/dist/emit/schemas.d.ts.map +1 -0
  42. package/dist/emit/schemas.js +145 -0
  43. package/dist/emit/schemas.js.map +1 -0
  44. package/dist/emit/text.d.ts +86 -0
  45. package/dist/emit/text.d.ts.map +1 -0
  46. package/dist/emit/text.js +242 -0
  47. package/dist/emit/text.js.map +1 -0
  48. package/dist/emit/tools.d.ts +15 -0
  49. package/dist/emit/tools.d.ts.map +1 -0
  50. package/dist/emit/tools.js +225 -0
  51. package/dist/emit/tools.js.map +1 -0
  52. package/dist/errors.d.ts +12 -0
  53. package/dist/errors.d.ts.map +1 -0
  54. package/dist/errors.js +16 -0
  55. package/dist/errors.js.map +1 -0
  56. package/dist/index.d.ts +36 -0
  57. package/dist/index.d.ts.map +1 -0
  58. package/dist/index.js +47 -0
  59. package/dist/index.js.map +1 -0
  60. package/dist/manifest.d.ts +48 -0
  61. package/dist/manifest.d.ts.map +1 -0
  62. package/dist/manifest.js +88 -0
  63. package/dist/manifest.js.map +1 -0
  64. package/dist/names.d.ts +57 -0
  65. package/dist/names.d.ts.map +1 -0
  66. package/dist/names.js +294 -0
  67. package/dist/names.js.map +1 -0
  68. package/dist/placeholders.d.ts +48 -0
  69. package/dist/placeholders.d.ts.map +1 -0
  70. package/dist/placeholders.js +114 -0
  71. package/dist/placeholders.js.map +1 -0
  72. package/dist/plan.d.ts +18 -0
  73. package/dist/plan.d.ts.map +1 -0
  74. package/dist/plan.js +81 -0
  75. package/dist/plan.js.map +1 -0
  76. package/dist/validate.d.ts +88 -0
  77. package/dist/validate.d.ts.map +1 -0
  78. package/dist/validate.js +790 -0
  79. package/dist/validate.js.map +1 -0
  80. package/package.json +43 -14
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Where emitted files live.
3
+ *
4
+ * The three roots come from the manifest through `managedRoots`, never from a literal
5
+ * here, so the plan and `aid gen`'s ownership check cannot disagree about a path. The
6
+ * per-file paths are derived from those roots for the same reason.
7
+ *
8
+ * The one thing this target deliberately does *not* emit is `app/api/<path>/route.ts`.
9
+ * A7 asks for that shim "that Next actually serves", but `validatePlan` rejects any
10
+ * path outside the managed roots, and `parseAidManifest` would reject the resulting
11
+ * manifest on the next run: `ownerForPath` classifies by the first path segment, so
12
+ * `app/api/...` has no owner. Emitting it would be a hard CLI break, not a cosmetic
13
+ * deviation. The example therefore hand-writes a one-line re-export under `app/api/`
14
+ * and the generator emits the handler it delegates to, mirroring how the Python
15
+ * example hand-writes `app/main.py`. See the README.
16
+ */
17
+ export declare const MANIFEST_PATH: string;
18
+ export declare const GENERATED_ROOT: string;
19
+ export declare const BUSINESS_ROOT: string;
20
+ /** The manifest's `output.root`: the Next.js App Router directory. */
21
+ export declare const APP_ROOT: string;
22
+ export type FileOwner = 'generated' | 'business';
23
+ export interface EmittedFile {
24
+ readonly path: string;
25
+ readonly content: string;
26
+ readonly owner: FileOwner;
27
+ }
28
+ /** The per-file paths, as one table so a test can assert the layout once. */
29
+ export declare const paths: {
30
+ readonly generatedIndex: `${string}/index.ts`;
31
+ readonly schemas: `${string}/schemas.ts`;
32
+ readonly requests: `${string}/requests.ts`;
33
+ readonly deps: `${string}/deps.ts`;
34
+ readonly control: `${string}/control.ts`;
35
+ readonly guardrails: `${string}/guardrails.ts`;
36
+ readonly agents: `${string}/agents.ts`;
37
+ readonly observability: `${string}/observability.ts`;
38
+ readonly gatewayModels: `${string}/gateway/models.ts`;
39
+ readonly gatewayIndex: `${string}/gateway/index.ts`;
40
+ readonly toolsIndex: `${string}/tools/index.ts`;
41
+ readonly contractsIndex: `${string}/contracts/index.ts`;
42
+ readonly contractsCitations: `${string}/contracts/citations.ts`;
43
+ readonly promptsIndex: `${string}/prompts/index.ts`;
44
+ readonly pipelinesIndex: `${string}/pipelines/index.ts`;
45
+ readonly routesIndex: `${string}/routes/index.ts`;
46
+ readonly retrievalBindings: `${string}/retrieval/bindings.ts`;
47
+ readonly retrievalIndex: `${string}/retrieval/index.ts`;
48
+ readonly retrievalIngestIndex: `${string}/retrieval/ingest/index.ts`;
49
+ readonly evalsIndex: `${string}/evals/index.ts`;
50
+ readonly businessIndex: `${string}/index.ts`;
51
+ readonly businessToolsIndex: `${string}/tools/index.ts`;
52
+ readonly businessPromptsIndex: `${string}/prompts/index.ts`;
53
+ };
54
+ export declare function toolAdapterPath(tool: string): string;
55
+ export declare function toolContractPath(tool: string): string;
56
+ export declare function promptPath(prompt: string): string;
57
+ export declare function pipelinePath(pipeline: string): string;
58
+ export declare function routePath(pipeline: string): string;
59
+ export declare function retrievalIngestModulePath(retriever: string): string;
60
+ export declare function evalModulePath(evalId: string): string;
61
+ export declare function businessToolPath(tool: string): string;
62
+ export declare function generated(path: string, content: string): EmittedFile;
63
+ export declare function business(path: string, content: string): EmittedFile;
64
+ //# sourceMappingURL=file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../src/emit/file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAKH,eAAO,MAAM,aAAa,QAAoD,CAAC;AAC/E,eAAO,MAAM,cAAc,QAAgD,CAAC;AAC5E,eAAO,MAAM,aAAa,QAA+C,CAAC;AAE1E,sEAAsE;AACtE,eAAO,MAAM,QAAQ,QAAiC,CAAC;AAEvD,MAAM,MAAM,SAAS,GAAG,WAAW,GAAG,UAAU,CAAC;AAEjD,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;CAC3B;AAED,6EAA6E;AAC7E,eAAO,MAAM,KAAK;aAChB,cAAc;aACd,OAAO;aACP,QAAQ;aACR,IAAI;aACJ,OAAO;aACP,UAAU;aACV,MAAM;aACN,aAAa;aACb,aAAa;aACb,YAAY;aACZ,UAAU;aACV,cAAc;aACd,kBAAkB;aAClB,YAAY;aACZ,cAAc;aACd,WAAW;aACX,iBAAiB;aACjB,cAAc;aACd,oBAAoB;aACpB,UAAU;aACV,aAAa;aACb,kBAAkB;aAClB,oBAAoB;CACZ,CAAC;AAEX,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,yBAAyB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAEnE;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CAEpE;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CAEnE"}
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Where emitted files live.
3
+ *
4
+ * The three roots come from the manifest through `managedRoots`, never from a literal
5
+ * here, so the plan and `aid gen`'s ownership check cannot disagree about a path. The
6
+ * per-file paths are derived from those roots for the same reason.
7
+ *
8
+ * The one thing this target deliberately does *not* emit is `app/api/<path>/route.ts`.
9
+ * A7 asks for that shim "that Next actually serves", but `validatePlan` rejects any
10
+ * path outside the managed roots, and `parseAidManifest` would reject the resulting
11
+ * manifest on the next run: `ownerForPath` classifies by the first path segment, so
12
+ * `app/api/...` has no owner. Emitting it would be a hard CLI break, not a cosmetic
13
+ * deviation. The example therefore hand-writes a one-line re-export under `app/api/`
14
+ * and the generator emits the handler it delegates to, mirroring how the Python
15
+ * example hand-writes `app/main.py`. See the README.
16
+ */
17
+ import { managedRoots } from '@aid-framework/generator-sdk';
18
+ import { TS_VERCEL_MANIFEST } from '../manifest.js';
19
+ export const MANIFEST_PATH = managedRoots(TS_VERCEL_MANIFEST).manifestFilename;
20
+ export const GENERATED_ROOT = managedRoots(TS_VERCEL_MANIFEST).generatedDir;
21
+ export const BUSINESS_ROOT = managedRoots(TS_VERCEL_MANIFEST).businessDir;
22
+ /** The manifest's `output.root`: the Next.js App Router directory. */
23
+ export const APP_ROOT = TS_VERCEL_MANIFEST.output.root;
24
+ /** The per-file paths, as one table so a test can assert the layout once. */
25
+ export const paths = {
26
+ generatedIndex: `${GENERATED_ROOT}/index.ts`,
27
+ schemas: `${GENERATED_ROOT}/schemas.ts`,
28
+ requests: `${GENERATED_ROOT}/requests.ts`,
29
+ deps: `${GENERATED_ROOT}/deps.ts`,
30
+ control: `${GENERATED_ROOT}/control.ts`,
31
+ guardrails: `${GENERATED_ROOT}/guardrails.ts`,
32
+ agents: `${GENERATED_ROOT}/agents.ts`,
33
+ observability: `${GENERATED_ROOT}/observability.ts`,
34
+ gatewayModels: `${GENERATED_ROOT}/gateway/models.ts`,
35
+ gatewayIndex: `${GENERATED_ROOT}/gateway/index.ts`,
36
+ toolsIndex: `${GENERATED_ROOT}/tools/index.ts`,
37
+ contractsIndex: `${GENERATED_ROOT}/contracts/index.ts`,
38
+ contractsCitations: `${GENERATED_ROOT}/contracts/citations.ts`,
39
+ promptsIndex: `${GENERATED_ROOT}/prompts/index.ts`,
40
+ pipelinesIndex: `${GENERATED_ROOT}/pipelines/index.ts`,
41
+ routesIndex: `${GENERATED_ROOT}/routes/index.ts`,
42
+ retrievalBindings: `${GENERATED_ROOT}/retrieval/bindings.ts`,
43
+ retrievalIndex: `${GENERATED_ROOT}/retrieval/index.ts`,
44
+ retrievalIngestIndex: `${GENERATED_ROOT}/retrieval/ingest/index.ts`,
45
+ evalsIndex: `${GENERATED_ROOT}/evals/index.ts`,
46
+ businessIndex: `${BUSINESS_ROOT}/index.ts`,
47
+ businessToolsIndex: `${BUSINESS_ROOT}/tools/index.ts`,
48
+ businessPromptsIndex: `${BUSINESS_ROOT}/prompts/index.ts`,
49
+ };
50
+ export function toolAdapterPath(tool) {
51
+ return `${GENERATED_ROOT}/tools/${tool}.ts`;
52
+ }
53
+ export function toolContractPath(tool) {
54
+ return `${GENERATED_ROOT}/contracts/${tool}.ts`;
55
+ }
56
+ export function promptPath(prompt) {
57
+ return `${GENERATED_ROOT}/prompts/${prompt}.ts`;
58
+ }
59
+ export function pipelinePath(pipeline) {
60
+ return `${GENERATED_ROOT}/pipelines/${pipeline}.ts`;
61
+ }
62
+ export function routePath(pipeline) {
63
+ return `${GENERATED_ROOT}/routes/${pipeline}.ts`;
64
+ }
65
+ export function retrievalIngestModulePath(retriever) {
66
+ return `${GENERATED_ROOT}/retrieval/ingest/${retriever}.ts`;
67
+ }
68
+ export function evalModulePath(evalId) {
69
+ return `${GENERATED_ROOT}/evals/${evalId}.ts`;
70
+ }
71
+ export function businessToolPath(tool) {
72
+ return `${BUSINESS_ROOT}/tools/${tool}.ts`;
73
+ }
74
+ export function generated(path, content) {
75
+ return { path, content, owner: 'generated' };
76
+ }
77
+ export function business(path, content) {
78
+ return { path, content, owner: 'business' };
79
+ }
80
+ //# sourceMappingURL=file.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file.js","sourceRoot":"","sources":["../../src/emit/file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,MAAM,CAAC,MAAM,aAAa,GAAG,YAAY,CAAC,kBAAkB,CAAC,CAAC,gBAAgB,CAAC;AAC/E,MAAM,CAAC,MAAM,cAAc,GAAG,YAAY,CAAC,kBAAkB,CAAC,CAAC,YAAY,CAAC;AAC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M,UAAU,gBAAgB,CAAC,IAAY;IAC3C,OAAO,GAAG,cAAc,cAAc,IAAI,KAAK,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,MAAc;IACvC,OAAO,GAAG,cAAc,YAAY,MAAM,KAAK,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,QAAgB;IAC3C,OAAO,GAAG,cAAc,cAAc,QAAQ,KAAK,CAAC;AACtD,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,QAAgB;IACxC,OAAO,GAAG,cAAc,WAAW,QAAQ,KAAK,CAAC;AACnD,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,SAAiB;IACzD,OAAO,GAAG,cAAc,qBAAqB,SAAS,KAAK,CAAC;AAC9D,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,MAAc;IAC3C,OAAO,GAAG,cAAc,UAAU,MAAM,KAAK,CAAC;AAChD,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,OAAO,GAAG,aAAa,UAAU,IAAI,KAAK,CAAC;AAC7C,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,OAAe;IACrD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC;AAC/C,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,IAAY,EAAE,OAAe;IACpD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * `app/generated/guardrails.ts`: the spec's guardrails, and the check that runs them.
3
+ *
4
+ * The runtime ships the engine (`GuardrailEngine`); this module is wiring, not
5
+ * semantics. Each IR declaration becomes one `GuardrailDefinition` -- a value the runtime
6
+ * evaluates -- and the two functions below are the whole of what the generated code adds:
7
+ * log what fired, refuse what must stop, and hand back the verdict whose payload the
8
+ * caller is allowed to use.
9
+ *
10
+ * ## Every declaration applies to every route
11
+ *
12
+ * The IR attaches a guardrail to a *prompt* or an *agent*; a `PipelineShape` has no
13
+ * `guardrails` field, and the gate that requires an `input` stage for a `specVersion`
14
+ * >= 0.2 HTTP `generate` route is document-global. So there is no reference a per-pipeline
15
+ * attachment could come from, and the honest reading is the only one available: an `input`
16
+ * guardrail inspects the prompt text of every HTTP `generate` route in this app, and an
17
+ * `input` guardrail an agent declares inspects pipeline input too.
18
+ *
19
+ * A `guard` *step* is the one exception, and it is the opposite case: the step names a
20
+ * guardrail and a stage, so it runs that declaration alone through `BY_ID` instead of
21
+ * running the stage's whole table. That is why `BY_ID` is exported here.
22
+ *
23
+ * ## What a stage's payload is
24
+ *
25
+ * `{ text }` for `input`, `output` and `retrieval`; the tool's own arguments for
26
+ * `tool-call`. That is the runtime's shape, and it is why a `redact` action has to name
27
+ * `text` on the first three -- the validator refuses anything else at plan time.
28
+ */
29
+ import type { GuardrailShape, IR } from '@aid-framework/ir';
30
+ import { type EmittedFile } from './file.js';
31
+ /** The stages a declaration may name, in the order the runtime lists them. */
32
+ export declare const STAGES: readonly ['input', 'output', 'retrieval', 'tool-call'];
33
+ export type GuardrailStage = (typeof STAGES)[number];
34
+ /** The module constant holding one stage's declarations. */
35
+ export declare function stageTable(stage: GuardrailStage): string;
36
+ /** The declarations a stage runs, in id order. */
37
+ export declare function stageGuardrails(ir: IR, stage: GuardrailStage): GuardrailShape[];
38
+ /** The declarations the spec makes, in id order. */
39
+ export declare function orderedGuardrails(ir: IR): GuardrailShape[];
40
+ /** Whether this app declares a guardrail at all, which is what decides the file. */
41
+ export declare function hasGuardrails(ir: IR): boolean;
42
+ /** Whether any declaration names this stage. */
43
+ export declare function usesStage(ir: IR, stage: GuardrailStage): boolean;
44
+ export declare function emitGuardrails(ir: IR): EmittedFile;
45
+ //# sourceMappingURL=guardrails.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"guardrails.d.ts","sourceRoot":"","sources":["../../src/emit/guardrails.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AAG5D,OAAO,EAAE,KAAK,WAAW,EAAoB,MAAM,WAAW,CAAC;AAU/D,8EAA8E;AAC9E,eAAO,MAAM,MAAM,YAAI,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,WAAW,CAAU,CAAC;AAC7E,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAErD,4DAA4D;AAC5D,wBAAgB,UAAU,CAAC,KAAK,EAAE,cAAc,GAAG,MAAM,CAExD;AAED,kDAAkD;AAClD,wBAAgB,eAAe,CAAC,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,cAAc,GAAG,cAAc,EAAE,CAE/E;AAED,oDAAoD;AACpD,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,EAAE,GAAG,cAAc,EAAE,CAE1D;AAED,oFAAoF;AACpF,wBAAgB,aAAa,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAE7C;AAED,gDAAgD;AAChD,wBAAgB,SAAS,CAAC,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,cAAc,GAAG,OAAO,CAEhE;AAmGD,wBAAgB,cAAc,CAAC,EAAE,EAAE,EAAE,GAAG,WAAW,CAmElD"}
@@ -0,0 +1,192 @@
1
+ /**
2
+ * `app/generated/guardrails.ts`: the spec's guardrails, and the check that runs them.
3
+ *
4
+ * The runtime ships the engine (`GuardrailEngine`); this module is wiring, not
5
+ * semantics. Each IR declaration becomes one `GuardrailDefinition` -- a value the runtime
6
+ * evaluates -- and the two functions below are the whole of what the generated code adds:
7
+ * log what fired, refuse what must stop, and hand back the verdict whose payload the
8
+ * caller is allowed to use.
9
+ *
10
+ * ## Every declaration applies to every route
11
+ *
12
+ * The IR attaches a guardrail to a *prompt* or an *agent*; a `PipelineShape` has no
13
+ * `guardrails` field, and the gate that requires an `input` stage for a `specVersion`
14
+ * >= 0.2 HTTP `generate` route is document-global. So there is no reference a per-pipeline
15
+ * attachment could come from, and the honest reading is the only one available: an `input`
16
+ * guardrail inspects the prompt text of every HTTP `generate` route in this app, and an
17
+ * `input` guardrail an agent declares inspects pipeline input too.
18
+ *
19
+ * A `guard` *step* is the one exception, and it is the opposite case: the step names a
20
+ * guardrail and a stage, so it runs that declaration alone through `BY_ID` instead of
21
+ * running the stage's whole table. That is why `BY_ID` is exported here.
22
+ *
23
+ * ## What a stage's payload is
24
+ *
25
+ * `{ text }` for `input`, `output` and `retrieval`; the tool's own arguments for
26
+ * `tool-call`. That is the runtime's shape, and it is why a `redact` action has to name
27
+ * `text` on the first three -- the validator refuses anything else at plan time.
28
+ */
29
+ import { stringLiteral } from '../names.js';
30
+ import { generated, paths } from './file.js';
31
+ import { arrayLiteral, compareNames, importStatement, moduleText, objectLiteral, } from './text.js';
32
+ /** The stages a declaration may name, in the order the runtime lists them. */
33
+ export const STAGES = ['input', 'output', 'retrieval', 'tool-call'];
34
+ /** The module constant holding one stage's declarations. */
35
+ export function stageTable(stage) {
36
+ return `${stage.replace('-', '_').toUpperCase()}_GUARDRAILS`;
37
+ }
38
+ /** The declarations a stage runs, in id order. */
39
+ export function stageGuardrails(ir, stage) {
40
+ return orderedGuardrails(ir).filter((guardrail) => guardrail.stages.includes(stage));
41
+ }
42
+ /** The declarations the spec makes, in id order. */
43
+ export function orderedGuardrails(ir) {
44
+ return [...(ir.guardrails ?? [])].sort((a, b) => compareNames(a.id, b.id));
45
+ }
46
+ /** Whether this app declares a guardrail at all, which is what decides the file. */
47
+ export function hasGuardrails(ir) {
48
+ return orderedGuardrails(ir).length > 0;
49
+ }
50
+ /** Whether any declaration names this stage. */
51
+ export function usesStage(ir, stage) {
52
+ return stageGuardrails(ir, stage).length > 0;
53
+ }
54
+ function stringArray(values) {
55
+ return arrayLiteral(values.map((value) => stringLiteral(value)), ' ');
56
+ }
57
+ /**
58
+ * The `GuardrailDefinition` literal for one declaration.
59
+ *
60
+ * The IR's `onFail` block is nested and the runtime's definition is flat, so this is where
61
+ * the two shapes meet -- the same projection the Python emitter makes. `fallbackPrompt` is
62
+ * written as `undefined` rather than omitted because the runtime field is required and
63
+ * reads it to decide whether a fallback prompt substitutes for the blocked one.
64
+ */
65
+ function definition(guardrail) {
66
+ const entries = [
67
+ ['id', stringLiteral(guardrail.id)],
68
+ ['stages', stringArray(guardrail.stages)],
69
+ ['policies', stringArray(guardrail.policies)],
70
+ ['action', stringLiteral(guardrail.action)],
71
+ ['redact', stringArray(guardrail.redact)],
72
+ ['tools', stringArray(guardrail.tools)],
73
+ ['message', stringLiteral(guardrail.onFail.message)],
74
+ [
75
+ 'fallbackPrompt',
76
+ guardrail.onFail.fallbackPrompt === undefined
77
+ ? 'undefined'
78
+ : stringLiteral(guardrail.onFail.fallbackPrompt),
79
+ ],
80
+ ['log', guardrail.onFail.log ? 'true' : 'false'],
81
+ ];
82
+ return objectLiteral(entries, ' ');
83
+ }
84
+ /**
85
+ * The stage tables, emitted only for stages that have something in them.
86
+ *
87
+ * A table nothing reads is a name a reader has to check before deciding it does not
88
+ * matter, so an app whose guardrails are all `input`-stage carries no `output` table.
89
+ */
90
+ function tables(ir) {
91
+ return STAGES.filter((stage) => usesStage(ir, stage)).map((stage) => `export const ${stageTable(stage)}: readonly GuardrailDefinition[] = byStage(${stringLiteral(stage)});`);
92
+ }
93
+ const ENFORCE = [
94
+ '/**',
95
+ ' * Log what fired, refuse what must stop, and return the verdict whose payload is safe',
96
+ ' * to use. A caller that keeps its own copy of the text has skipped a `redact`.',
97
+ ' *',
98
+ ' * `escalate` stops here too: a pipeline route has no checkpoint to resume from, so a',
99
+ ' * question to a human is a question nobody is asked. The validator refuses an',
100
+ ' * `escalate` declaration at plan time; this is the backstop for a table edited by hand.',
101
+ ' */',
102
+ 'export function enforceGuardrail(verdict: GuardrailVerdict): GuardrailVerdict {',
103
+ ' for (const record of verdict.records) {',
104
+ ' if (BY_ID.get(record.guardrail)?.log === true) {',
105
+ ' console.warn(',
106
+ // biome-ignore lint/suspicious/noTemplateCurlyInString: emitted source, not interpolation
107
+ ' `guardrail ${record.guardrail} fired at the ${record.stage} stage: ${record.message}` +',
108
+ // biome-ignore lint/suspicious/noTemplateCurlyInString: emitted source, not interpolation
109
+ " ` (${record.policies.join(', ')})`,",
110
+ ' );',
111
+ ' }',
112
+ ' }',
113
+ ' const deciding = verdict.deciding;',
114
+ ' if (deciding !== undefined && STOPPING.has(deciding.action)) {',
115
+ ' throw new GuardrailBlockedError(deciding.guardrail, deciding.stage, deciding.message);',
116
+ ' }',
117
+ ' return verdict;',
118
+ '}',
119
+ ].join('\n');
120
+ const BLOCK = [
121
+ '/**',
122
+ " * The guardrail block inside an error's `cause` chain, or `undefined`. A step re-raises",
123
+ ' * whatever it caught as a `PipelineError`, so a block reaches a route wrapped;',
124
+ ' * unwrapping it is what lets the route answer 4xx for a refused input instead of',
125
+ ' * blaming the model with a 502.',
126
+ ' */',
127
+ 'export function guardrailBlock(error: unknown): GuardrailBlockedError | undefined {',
128
+ ' const seen = new Set<unknown>();',
129
+ ' let current: unknown = error;',
130
+ ' while (current instanceof Error && !seen.has(current)) {',
131
+ ' if (current instanceof GuardrailBlockedError) return current;',
132
+ ' seen.add(current);',
133
+ ' current = current.cause;',
134
+ ' }',
135
+ ' return undefined;',
136
+ '}',
137
+ ].join('\n');
138
+ export function emitGuardrails(ir) {
139
+ const definitions = orderedGuardrails(ir).map(definition);
140
+ return generated(paths.guardrails, moduleText(importStatement('@aid-framework/runtime', [
141
+ { name: 'ConfigurationError', type: false },
142
+ { name: 'GuardrailBlockedError', type: false },
143
+ { name: 'GuardrailDefinition', type: true },
144
+ { name: 'GuardrailEngine', type: false },
145
+ { name: 'GuardrailVerdict', type: true },
146
+ ]), [
147
+ '/**',
148
+ ' * Every guardrail the spec declares, in id order.',
149
+ ' *',
150
+ ' * An `input` or `output` declaration applies to *every* generated route, not to the',
151
+ ' * prompt or agent that named it: the IR attaches a guardrail to a prompt, and the gate',
152
+ ' * that requires an `input` stage is document-wide, so there is no narrower unit to',
153
+ ' * scope it to. A guardrail evaluates the payload the runtime defines -- `{ text }` for',
154
+ ' * `input`, `output` and `retrieval`, the tool arguments for `tool-call` -- which is why',
155
+ ' * a `redact` action names `text` on the first three.',
156
+ ' */',
157
+ definitions.length === 0
158
+ ? 'export const GUARDRAILS: readonly GuardrailDefinition[] = [];'
159
+ : [
160
+ 'export const GUARDRAILS: readonly GuardrailDefinition[] = [',
161
+ ...definitions.map((text) => ` ${text},`),
162
+ '];',
163
+ ].join('\n'),
164
+ ].join('\n'), [
165
+ '/** The actions that stop the request instead of annotating it. */',
166
+ "const STOPPING = new Set(['block', 'escalate']);",
167
+ ].join('\n'), [
168
+ '/** The declarations by id, which is how a `guard` step names one. */',
169
+ 'export const BY_ID: ReadonlyMap<string, GuardrailDefinition> = new Map(',
170
+ ' GUARDRAILS.map((guardrail) => [guardrail.id, guardrail] as const),',
171
+ ');',
172
+ ].join('\n'), [
173
+ "/** The engine every call site shares, over this app's declarations. */",
174
+ 'export const ENGINE = new GuardrailEngine(BY_ID);',
175
+ ].join('\n'), [
176
+ '/** The declarations that run at one stage. */',
177
+ 'function byStage(stage: string): readonly GuardrailDefinition[] {',
178
+ ' return GUARDRAILS.filter((guardrail) => guardrail.stages.includes(stage));',
179
+ '}',
180
+ ].join('\n'), [
181
+ '/** One declaration by id, which is how a `guard` step names one. */',
182
+ 'export function guardrailById(id: string): GuardrailDefinition {',
183
+ ' const found = BY_ID.get(id);',
184
+ ' if (found === undefined) {',
185
+ // biome-ignore lint/suspicious/noTemplateCurlyInString: this is emitted source, not interpolation
186
+ ' throw new ConfigurationError(`no guardrail ${JSON.stringify(id)} is declared`);',
187
+ ' }',
188
+ ' return found;',
189
+ '}',
190
+ ].join('\n'), tables(ir).join('\n\n'), ENFORCE, BLOCK));
191
+ }
192
+ //# sourceMappingURL=guardrails.js.map
@@ -0,0 +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AC,CAAC,IAAI,CAAC,MAAM,CAAC,EACvB,OAAO,EACP,KAAK,CACN,CACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * `generated/gateway/models.ts`, `generated/gateway/index.ts`, `generated/deps.ts` and
3
+ * `generated/observability.ts`.
4
+ *
5
+ * The gateway is the seam every generated app is testable through: `deps.ts` resolves a
6
+ * singleton that a test (or the example's fake gateway) replaces wholesale, and the
7
+ * model table is plain data so a cost or capability change is a one-line diff.
8
+ */
9
+ import type { IR } from '@aid-framework/ir';
10
+ import { type EmittedFile } from './file.js';
11
+ export declare function emitGatewayModels(ir: IR): EmittedFile;
12
+ export declare function emitGatewayIndex(ir: IR): EmittedFile;
13
+ export declare function emitDeps(ir: IR): EmittedFile;
14
+ export declare function emitObservability(ir: IR): EmittedFile;
15
+ //# sourceMappingURL=infrastructure.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"infrastructure.d.ts","sourceRoot":"","sources":["../../src/emit/infrastructure.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,EAAE,EAAc,MAAM,mBAAmB,CAAC;AASxD,OAAO,EAAE,KAAK,WAAW,EAAoB,MAAM,WAAW,CAAC;AAsC/D,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,EAAE,GAAG,WAAW,CAyErD;AAaD,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,EAAE,GAAG,WAAW,CA6BpD;AAED,wBAAgB,QAAQ,CAAC,EAAE,EAAE,EAAE,GAAG,WAAW,CA2G5C;AAED,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,EAAE,GAAG,WAAW,CAwCrD"}