Skip to main content

Interface: IGuardrails

Defined in: internal/guardrails/types.ts:69

Checks around a model call: the gateway's built-in rules, then the app's. Rules are fixed at construction.

Implemented by Guardrails; the type of IAIGatewayDeps.guardrails, so a host may pass its own in overrides.

Methods​

checkInput()​

checkInput(args): Promise<IInputCheck>

Defined in: internal/guardrails/types.ts:88

checkInput -- screens the prompt before the model call

About​

The built-in rules first, the app's only when those passed.

Sanitises the prompt (trims, normalises line endings), then runs the built-in input rules (not empty unless files come with it, not over maxPromptChars, no prompt-injection marker) and, only when none of them returned an ERROR, the app's inputRules in parallel. Returns the sanitised prompt, every issue stamped with its rule's name, ok and the trace ids of the judge calls, which the gateway annotates with the issues (D73). The call's abortSignal reaches every rule and its judge call; an aborted call rejects with the signal's reason, not with a rule_error (D90).

Who needs it​

prepareCall, on the prompt of every ask and run; AIGateway.resume, on the free text a user typed into a question of the model (D62), with no files.

Parameters​

args​
abortSignal?​

AbortSignal

The call's signal: merged into every rule's. An aborted call rejects with the signal's reason, no rule_error; so does a judge call the usage adapter refused, with that error.

files​

IFileRef[]

inlineFiles​

IInlineFilePart[]

The same files with their bytes; the rules' judge sends them to the model.

profile​

IAIProfile

prompt​

string

threadId?​

string

Undefined when the call creates a new thread.

userContext​

IUserContext

Returns​

Promise<IInputCheck>


checkOutput()​

checkOutput<TStructured>(args): Promise<IOutputCheck<TStructured>>

Defined in: internal/guardrails/types.ts:122

checkOutput -- screens the answer after the model call

About​

Truncation and schema first, the app's rules only when those passed.

Flags a finishReason LENGTH as the WARNING output_truncated and, with a schema, parses the output against it with parseStructured. Only when no ERROR came out of that, the app's outputRules run in parallel on the parsed value (or the text) with the evidence of the call: the question, the KB chunks, the tool results, the user context, the profile and the thread. Returns the parsed value, every issue stamped with its rule's name, ok and the trace ids of the judge calls, which the gateway annotates with the issues (D73). The call's abortSignal reaches every rule and its judge call; an aborted call rejects with the signal's reason, not with a rule_error (D90).

Who needs it​

checkOutput of the orchestration, on the answer of every ask and of every run that ended with text (settleRun).

Type Parameters​

TStructured​

TStructured = unknown

Parameters​

args​
abortSignal?​

AbortSignal

The call's signal: merged into every rule's. An aborted call rejects with the signal's reason, no rule_error; so does a judge call the usage adapter refused, with that error.

chunks​

IChunk[]

The knowledge-base chunks that were in the request.

profile​

IAIProfile

prompt​

string

The user's question the answer is for.

result​

IModelResult

schema?​

ZodType<TStructured, unknown, $ZodTypeInternals<TStructured, unknown>>

Zod schema of the expected answer; omit for a free-text answer.

threadId​

string

toolResults​

IToolResultPart[]

userContext​

IUserContext

Returns​

Promise<IOutputCheck<TStructured>>


parseStructured()​

parseStructured<TStructured>(args): object

Defined in: internal/guardrails/types.ts:157

parseStructured -- the schema check of an answer, on its own

About​

Text in, the typed value or the issue out; no rule runs.

The model's own structured value is taken when it gave one, else the text is parsed as JSON; the result is validated against the schema. A text that is not JSON is output_invalid_json, a value the schema rejects is output_schema (with the first failing field), both ERROR; structured is then absent.

Who needs it​

checkOutput, before the output rules; AIGateway.resume, on the text a reviewer edited (D99), which must still match the schema the run was started with but is not screened by the rules.

Type Parameters​

TStructured​

TStructured

Parameters​

args​
schema​

ZodType<TStructured>

structured?​

unknown

The structured value the model returned itself, when any; it is validated instead of the text.

text​

string

Returns​

object

issues​

issues: IIssue[]

structured?​

optional structured?: TStructured