Skip to content

Commit dbf8202

Browse files
committed
Add AI agent and enhance validation schemas
Introduces AI agent, tool, and knowledge base schemas in a new file for AI protocol support. Refactors validation rule schemas to use discriminated unions for multiple validation types (script, uniqueness, state machine, format) and adds severity and message fields. Updates index exports to include the new AI agent schema.
1 parent e2bb961 commit dbf8202

File tree

3 files changed

+120
-17
lines changed

3 files changed

+120
-17
lines changed

packages/spec/src/ai/agent.zod.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { z } from 'zod';
2+
3+
/**
4+
* AI Model Configuration
5+
*/
6+
export const AIModelConfigSchema = z.object({
7+
provider: z.enum(['openai', 'azure_openai', 'anthropic', 'local']).default('openai'),
8+
model: z.string().describe('Model name (e.g. gpt-4, claude-3-opus)'),
9+
temperature: z.number().min(0).max(2).default(0.7),
10+
maxTokens: z.number().optional(),
11+
topP: z.number().optional(),
12+
});
13+
14+
/**
15+
* AI Tool Definition
16+
* References to Actions, Flows, or Objects available to the Agent.
17+
*/
18+
export const AIToolSchema = z.object({
19+
type: z.enum(['action', 'flow', 'query', 'vector_search']),
20+
name: z.string().describe('Reference name (Action Name, Flow Name)'),
21+
description: z.string().optional().describe('Override description for the LLM'),
22+
});
23+
24+
/**
25+
* AI Knowledge Base
26+
* RAG configuration.
27+
*/
28+
export const AIKnowledgeSchema = z.object({
29+
topics: z.array(z.string()).describe('Topics/Tags to recruit knowledge from'),
30+
indexes: z.array(z.string()).describe('Vector Store Indexes'),
31+
});
32+
33+
/**
34+
* AI Agent Schema
35+
* Definition of an autonomous agent specialized for a domain.
36+
*/
37+
export const AgentSchema = z.object({
38+
/** Identity */
39+
name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Agent unique identifier'),
40+
label: z.string().describe('Agent display name'),
41+
avatar: z.string().optional(),
42+
role: z.string().describe('The persona/role (e.g. "Senior Support Engineer")'),
43+
44+
/** Cognition */
45+
instructions: z.string().describe('System Prompt / Prime Directives'),
46+
model: AIModelConfigSchema.optional(),
47+
48+
/** Capabilities */
49+
tools: z.array(AIToolSchema).optional().describe('Available tools'),
50+
knowledge: AIKnowledgeSchema.optional().describe('RAG access'),
51+
52+
/** Interface */
53+
active: z.boolean().default(true),
54+
access: z.array(z.string()).optional().describe('Who can chat with this agent'),
55+
});
56+
57+
export type Agent = z.infer<typeof AgentSchema>;
58+
export type AITool = z.infer<typeof AIToolSchema>;
Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,67 @@
11
import { z } from 'zod';
22

33
/**
4-
* Schema for Validation Rules.
5-
* Ensures data integrity by preventing saves when conditions aren't met.
4+
* Base Validation Rule
65
*/
7-
export const ValidationRuleSchema = z.object({
8-
/** Machine name of the rule */
6+
const BaseValidationSchema = z.object({
97
name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Unique rule name'),
10-
11-
/**
12-
* The condition that triggers the error.
13-
* If this evaluates to TRUE, the validation FAILS and the error is shown.
14-
* Example: "amount < 0" (Prevents negative amounts)
15-
*/
16-
error_condition: z.string().describe('Formula expression. If TRUE, validation fails.'),
17-
18-
/** The error message displayed to the user */
19-
error_message: z.string().describe('Error message to display to the user'),
20-
21-
/** Active status of the rule */
22-
active: z.boolean().default(true).describe('Whether this rule is active'),
8+
active: z.boolean().default(true),
9+
severity: z.enum(['error', 'warning', 'info']).default('error'),
10+
message: z.string().describe('Error message to display'),
2311
});
2412

13+
/**
14+
* 1. Script/Expression Validation
15+
* Generic formula-based validation.
16+
*/
17+
export const ScriptValidationSchema = BaseValidationSchema.extend({
18+
type: z.literal('script'),
19+
condition: z.string().describe('Formula expression. If TRUE, validation fails. (e.g. amount < 0)'),
20+
});
21+
22+
/**
23+
* 2. Uniqueness Validation
24+
* specialized optimized check for unique constraints.
25+
*/
26+
export const UniquenessValidationSchema = BaseValidationSchema.extend({
27+
type: z.literal('unique'),
28+
fields: z.array(z.string()).describe('Fields that must be combined unique'),
29+
scope: z.string().optional().describe('Formula condition for scope (e.g. active = true)'),
30+
caseSensitive: z.boolean().default(true),
31+
});
32+
33+
/**
34+
* 3. State Machine Validation
35+
* State transition logic.
36+
*/
37+
export const StateMachineValidationSchema = BaseValidationSchema.extend({
38+
type: z.literal('state_machine'),
39+
field: z.string().describe('State field (e.g. status)'),
40+
transitions: z.record(z.array(z.string())).describe('Map of { OldState: [AllowedNewStates] }'),
41+
});
42+
43+
/**
44+
* 4. Value Format Validation
45+
* Regex or specialized formats.
46+
*/
47+
export const FormatValidationSchema = BaseValidationSchema.extend({
48+
type: z.literal('format'),
49+
field: z.string(),
50+
regex: z.string().optional(),
51+
format: z.enum(['email', 'url', 'phone', 'json']).optional(),
52+
});
53+
54+
/**
55+
* Master Validation Rule Schema
56+
*/
57+
export const ValidationRuleSchema = z.discriminatedUnion('type', [
58+
ScriptValidationSchema,
59+
UniquenessValidationSchema,
60+
StateMachineValidationSchema,
61+
FormatValidationSchema
62+
]);
63+
2564
export type ValidationRule = z.infer<typeof ValidationRuleSchema>;
65+
export type ScriptValidation = z.infer<typeof ScriptValidationSchema>;
66+
export type UniquenessValidation = z.infer<typeof UniquenessValidationSchema>;
67+
export type StateMachineValidation = z.infer<typeof StateMachineValidationSchema>;

packages/spec/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export * from './data/workflow.zod';
1616
export * from './data/flow.zod';
1717
export * from './data/dataset.zod';
1818

19+
// AI Protocol (Agent, RAG)
20+
export * from './ai/agent.zod';
21+
1922
// UI Protocol (Layout, Navigation, Interaction)
2023
export * from './ui/app.zod';
2124
export * from './ui/view.zod';

0 commit comments

Comments
 (0)