protocol layer 3

Agent messaging contracts and event schemas that connect pipeline stages. Defines typed message envelopes, routing rules, and observability hooks shared across the ecosystem.

Output Parser

Extract a typed JSON object from raw LLM prose — handles markdown fences and surrounding text.

typescript
// Output parser — extract JSON from LLM prose
import { parse } from "protocol/lib/parse";

const llmResponse = `
Sure! Here is the structured summary you requested:

\`\`\`json
{
  "category": "billing",
  "urgency": "high",
  "summary": "Customer reports duplicate charge on Jan invoice.",
  "suggestedAction": "Escalate to billing team for refund review."
}
\`\`\`

Let me know if you need anything else.
`;

const result = parse<SupportSummary>(llmResponse);
console.log(result);
output
{
  ok: true,
  value: {
    category: "billing",
    urgency: "high",
    summary: "Customer reports duplicate charge on Jan invoice.",
    suggestedAction: "Escalate to billing team for refund review."
  }
}

Schema Validation

Validate an object against a JSON Schema contract and receive structured issues on failure.

typescript
// Schema validation — validate an object against a contract
import { validate } from "protocol/lib/validate";

const schema = {
  type: "object",
  required: ["category", "urgency", "summary"],
  properties: {
    category:        { type: "string" },
    urgency:         { type: "string", enum: ["low", "medium", "high"] },
    summary:         { type: "string", minLength: 1 },
    suggestedAction: { type: "string" },
  },
};

const validObject = {
  category: "billing",
  urgency: "high",
  summary: "Duplicate charge on Jan invoice.",
};

const invalidObject = {
  category: "billing",
  urgency: "critical",   // not in enum
  // summary missing
};

console.log("valid:  ", validate(validObject, schema));
console.log("invalid:", validate(invalidObject, schema));
output
valid:   { accepted: true }

invalid: {
  accepted: false,
  issues: [
    {
      field: "urgency",
      message: "must be one of: low, medium, high",
      severity: "error"
    },
    {
      field: "summary",
      message: "required field is missing",
      severity: "error"
    }
  ]
}