|
4 | 4 | AIModelConfigSchema, |
5 | 5 | AIToolSchema, |
6 | 6 | AIKnowledgeSchema, |
| 7 | + StructuredOutputFormatSchema, |
| 8 | + StructuredOutputConfigSchema, |
7 | 9 | type Agent, |
8 | 10 | } from './agent.zod'; |
9 | 11 |
|
@@ -569,4 +571,117 @@ Be precise, data-driven, and clear in your explanations.`, |
569 | 571 | expect(agent.guardrails?.blockedTopics).toContain('financial_advice'); |
570 | 572 | }); |
571 | 573 | }); |
| 574 | + |
| 575 | + describe('Structured Output', () => { |
| 576 | + it('should accept agent with structuredOutput', () => { |
| 577 | + const agent = AgentSchema.parse({ |
| 578 | + name: 'json_agent', |
| 579 | + label: 'JSON Agent', |
| 580 | + role: 'Data Formatter', |
| 581 | + instructions: 'Always return JSON.', |
| 582 | + structuredOutput: { |
| 583 | + format: 'json_object', |
| 584 | + }, |
| 585 | + }); |
| 586 | + |
| 587 | + expect(agent.structuredOutput?.format).toBe('json_object'); |
| 588 | + expect(agent.structuredOutput?.strict).toBe(false); |
| 589 | + expect(agent.structuredOutput?.retryOnValidationFailure).toBe(true); |
| 590 | + expect(agent.structuredOutput?.maxRetries).toBe(3); |
| 591 | + }); |
| 592 | + |
| 593 | + it('should accept agent with full structuredOutput config', () => { |
| 594 | + const agent = AgentSchema.parse({ |
| 595 | + name: 'strict_agent', |
| 596 | + label: 'Strict Agent', |
| 597 | + role: 'Validator', |
| 598 | + instructions: 'Return strict JSON.', |
| 599 | + structuredOutput: { |
| 600 | + format: 'json_schema', |
| 601 | + schema: { type: 'object', properties: { name: { type: 'string' } } }, |
| 602 | + strict: true, |
| 603 | + retryOnValidationFailure: false, |
| 604 | + maxRetries: 5, |
| 605 | + fallbackFormat: 'json_object', |
| 606 | + transformPipeline: ['trim', 'parse_json', 'validate'], |
| 607 | + }, |
| 608 | + }); |
| 609 | + |
| 610 | + expect(agent.structuredOutput?.strict).toBe(true); |
| 611 | + expect(agent.structuredOutput?.fallbackFormat).toBe('json_object'); |
| 612 | + expect(agent.structuredOutput?.transformPipeline).toHaveLength(3); |
| 613 | + }); |
| 614 | + }); |
| 615 | +}); |
| 616 | + |
| 617 | +// ========================================== |
| 618 | +// Structured Output Schema Tests |
| 619 | +// ========================================== |
| 620 | + |
| 621 | +describe('StructuredOutputFormatSchema', () => { |
| 622 | + it('should accept all output formats', () => { |
| 623 | + const formats = ['json_object', 'json_schema', 'regex', 'grammar', 'xml'] as const; |
| 624 | + formats.forEach(format => { |
| 625 | + expect(StructuredOutputFormatSchema.parse(format)).toBe(format); |
| 626 | + }); |
| 627 | + }); |
| 628 | + |
| 629 | + it('should reject invalid format', () => { |
| 630 | + expect(() => StructuredOutputFormatSchema.parse('yaml')).toThrow(); |
| 631 | + }); |
| 632 | +}); |
| 633 | + |
| 634 | +describe('StructuredOutputConfigSchema', () => { |
| 635 | + it('should accept minimal config', () => { |
| 636 | + const config = StructuredOutputConfigSchema.parse({ |
| 637 | + format: 'json_object', |
| 638 | + }); |
| 639 | + |
| 640 | + expect(config.format).toBe('json_object'); |
| 641 | + expect(config.strict).toBe(false); |
| 642 | + expect(config.retryOnValidationFailure).toBe(true); |
| 643 | + expect(config.maxRetries).toBe(3); |
| 644 | + }); |
| 645 | + |
| 646 | + it('should accept config with schema', () => { |
| 647 | + const config = StructuredOutputConfigSchema.parse({ |
| 648 | + format: 'json_schema', |
| 649 | + schema: { |
| 650 | + type: 'object', |
| 651 | + properties: { |
| 652 | + result: { type: 'string' }, |
| 653 | + confidence: { type: 'number' }, |
| 654 | + }, |
| 655 | + required: ['result'], |
| 656 | + }, |
| 657 | + }); |
| 658 | + |
| 659 | + expect(config.schema).toBeDefined(); |
| 660 | + expect(config.schema?.type).toBe('object'); |
| 661 | + }); |
| 662 | + |
| 663 | + it('should accept config with transform pipeline', () => { |
| 664 | + const config = StructuredOutputConfigSchema.parse({ |
| 665 | + format: 'json_object', |
| 666 | + transformPipeline: ['trim', 'parse_json', 'validate', 'coerce_types'], |
| 667 | + }); |
| 668 | + |
| 669 | + expect(config.transformPipeline).toHaveLength(4); |
| 670 | + }); |
| 671 | + |
| 672 | + it('should enforce maxRetries min constraint', () => { |
| 673 | + expect(() => StructuredOutputConfigSchema.parse({ |
| 674 | + format: 'json_object', |
| 675 | + maxRetries: -1, |
| 676 | + })).toThrow(); |
| 677 | + }); |
| 678 | + |
| 679 | + it('should accept fallbackFormat', () => { |
| 680 | + const config = StructuredOutputConfigSchema.parse({ |
| 681 | + format: 'regex', |
| 682 | + fallbackFormat: 'json_object', |
| 683 | + }); |
| 684 | + |
| 685 | + expect(config.fallbackFormat).toBe('json_object'); |
| 686 | + }); |
572 | 687 | }); |
0 commit comments