hashbrown

decodeDecision

Decodes and verifies a decision response against a decidable schema.

Checks the envelope (kind and version), validates value with the schema, and verifies that meta has exactly one entry of the right kind for every question the schema compiles to. Dotted meta keys are rebuilt into nested objects mirroring the schema. No partial result is ever returned.

API

export declare function decodeDecision<
  S extends  DecidableSchema ,
>(schema: S, response: unknown):  Result <S>;
decodeDecision
@paramschema:
S

The decidable object schema the questions came from.

@paramresponse:
unknown

The untrusted decision response envelope.

@returns

Examples

import { decodeDecision, s } from '@hashbrownai/core';
import { HashbrownTypeSafe } from '@hashbrownai/typesafe';

const schema = s.object('Routing', {
  needsHuman: s.boolean('Does the customer ask for a person?'),
});

async function routeTicket(state: { ticket: string }) {
  const response = await HashbrownTypeSafe.decide({
    apiKey: process.env['TYPESAFE_API_KEY'] ?? '',
    model: 'jev-1.14.0',
    schema,
    request: { state },
  });
  const { value, meta } = decodeDecision(schema, response);
  return { needsHuman: value.needsHuman, p: meta.needsHuman.probability };
}