All files / src/agent schema.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 0/0
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                          21x         21x                         21x               21x                                                                                                         21x       21x  
/**
 * Agent Signal Schema
 *
 * Agents communicate via a trivial JSON signal: done, questions, or error.
 * All structured output is file-based (see file-io.ts).
 */
 
import { z } from 'zod';
 
// =============================================================================
// SHARED SCHEMAS
// =============================================================================
 
const optionSchema = z.object({
  label: z.string(),
  description: z.string().optional(),
});
 
export const questionItemSchema = z.object({
  id: z.string(),
  question: z.string(),
  options: z.array(optionSchema).optional(),
  multiSelect: z.boolean().optional(),
});
 
export type QuestionItem = z.infer<typeof questionItemSchema>;
 
// =============================================================================
// UNIVERSAL SIGNAL SCHEMA
// =============================================================================
 
export const agentSignalSchema = z.discriminatedUnion('status', [
  z.object({ status: z.literal('done') }),
  z.object({ status: z.literal('questions'), questions: z.array(questionItemSchema) }),
  z.object({ status: z.literal('error'), error: z.string() }),
]);
 
export type AgentSignal = z.infer<typeof agentSignalSchema>;
 
export const agentSignalJsonSchema = {
  type: 'object',
  oneOf: [
    {
      properties: {
        status: { const: 'done' },
      },
      required: ['status'],
    },
    {
      properties: {
        status: { const: 'questions' },
        questions: {
          type: 'array',
          items: {
            type: 'object',
            properties: {
              id: { type: 'string' },
              question: { type: 'string' },
              options: {
                type: 'array',
                items: {
                  type: 'object',
                  properties: {
                    label: { type: 'string' },
                    description: { type: 'string' },
                  },
                  required: ['label'],
                },
              },
              multiSelect: { type: 'boolean' },
            },
            required: ['id', 'question'],
          },
        },
      },
      required: ['status', 'questions'],
    },
    {
      properties: {
        status: { const: 'error' },
        error: { type: 'string' },
      },
      required: ['status', 'error'],
    },
  ],
};
 
// =============================================================================
// BACKWARD COMPATIBILITY
// =============================================================================
 
/** @deprecated Use agentSignalSchema */
export const agentOutputSchema = agentSignalSchema;
/** @deprecated Use AgentSignal */
export type AgentOutput = AgentSignal;
/** @deprecated Use agentSignalJsonSchema */
export const agentOutputJsonSchema = agentSignalJsonSchema;