@slashfi/agents-sdk 0.22.0 → 0.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Zod schema for SerializedAgentDefinition.
3
+ *
4
+ * Validates definition.json files at runtime — JSON imports have no type safety,
5
+ * so this catches malformed definitions before they hit the registry.
6
+ *
7
+ * Used in:
8
+ * - registry.register() — validate on ingest
9
+ * - adk pack — validate after introspection
10
+ * - adk publish — validate before shipping
11
+ */
12
+
13
+ import { z } from "zod";
14
+
15
+ // ============================================
16
+ // Tool Schema
17
+ // ============================================
18
+
19
+ export const SerializedToolSchema = z.object({
20
+ name: z.string().min(1, "Tool name is required"),
21
+ description: z.string(),
22
+ inputSchema: z
23
+ .record(z.unknown())
24
+ .default({ type: "object", properties: {} }),
25
+ outputSchema: z.record(z.unknown()).optional(),
26
+ });
27
+
28
+ // ============================================
29
+ // Agent Definition Schema
30
+ // ============================================
31
+
32
+ export const SerializedAgentDefinitionSchema = z
33
+ .object({
34
+ path: z.string().min(1, "Agent path is required"),
35
+ name: z.string().min(1, "Agent name is required"),
36
+ description: z.string().default(""),
37
+ version: z.string().default("1.0.0"),
38
+ visibility: z.enum(["public", "private"]).default("public"),
39
+ auth: z.record(z.unknown()).optional(),
40
+ serverSource: z.string().optional(),
41
+ serverInfo: z
42
+ .object({
43
+ name: z.string(),
44
+ version: z.string(),
45
+ })
46
+ .optional(),
47
+ tools: z
48
+ .array(SerializedToolSchema)
49
+ .min(1, "At least one tool is required"),
50
+ generatedAt: z.string().optional(),
51
+ sdkVersion: z.string().optional(),
52
+ // Allow additional fields (e.g., $defs from MCP introspection)
53
+ })
54
+ .passthrough();
55
+
56
+ // ============================================
57
+ // Validate
58
+ // ============================================
59
+
60
+ export type ValidationResult =
61
+ | { ok: true; definition: z.infer<typeof SerializedAgentDefinitionSchema> }
62
+ | { ok: false; errors: string[] };
63
+
64
+ /**
65
+ * Validate a definition against the SerializedAgentDefinition schema.
66
+ *
67
+ * Returns either the validated definition or a list of human-readable errors.
68
+ */
69
+ export function validateDefinition(input: unknown): ValidationResult {
70
+ const result = SerializedAgentDefinitionSchema.safeParse(input);
71
+ if (result.success) {
72
+ return { ok: true, definition: result.data };
73
+ }
74
+ const errors = result.error.issues.map((issue) => {
75
+ const path = issue.path.length > 0 ? issue.path.join(".") : "root";
76
+ return `${path}: ${issue.message}`;
77
+ });
78
+ return { ok: false, errors };
79
+ }
80
+
81
+ /**
82
+ * Validate and throw if invalid.
83
+ * Use in register() and pack/publish where failure should be fatal.
84
+ */
85
+ export function assertValidDefinition(input: unknown, context?: string): void {
86
+ const result = validateDefinition(input);
87
+ if (!result.ok) {
88
+ const prefix = context
89
+ ? `Invalid definition (${context})`
90
+ : "Invalid definition";
91
+ throw new Error(`${prefix}:\n ${result.errors.join("\n ")}`);
92
+ }
93
+ }