@codeworksh/aikit 0.1.0 → 0.2.0

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.
@@ -1,63 +0,0 @@
1
- /**
2
- * @description Validation powered by ajv, TypeBox natively doesn't provide data validation.
3
- */
4
-
5
- import Ajv from "ajv";
6
- import addFormats from "ajv-formats";
7
-
8
- import type { Agent } from "../agent/agent";
9
- import type { Message } from "../message/message";
10
-
11
- // Create singleton AJV instance with formats
12
- const ajv = new Ajv({
13
- allErrors: true,
14
- strict: false,
15
- coerceTypes: true,
16
- });
17
- addFormats(ajv);
18
-
19
- /**
20
- * Finds a tool by name and validates the tool call arguments against its TypeBox schema
21
- */
22
- export function validateToolCall<T extends Message.Tool>(
23
- tools: T[],
24
- toolExecution: Agent.ToolCallInFlight,
25
- ): Message.ToolArguments<T> {
26
- const tool = tools.find((t) => t.name === toolExecution.name);
27
- if (!tool) {
28
- throw new Error(`Tool "${toolExecution.name}" not found`);
29
- }
30
- return validateToolArguments(tool, toolExecution);
31
- }
32
-
33
- /**
34
- * Validates tool call arguments against the tool's TypeBox schema
35
- */
36
- export function validateToolArguments<T extends Message.Tool>(
37
- tool: T,
38
- toolExecution: Agent.ToolCallInFlight,
39
- ): Message.ToolArguments<T> {
40
- const validate = ajv.compile(tool.parameters);
41
- const args = structuredClone(toolExecution.rawArgs);
42
-
43
- if (validate(args)) {
44
- return args as Message.ToolArguments<T>;
45
- }
46
-
47
- const errors =
48
- validate.errors
49
- ?.map((err) => {
50
- const path = err.instancePath ? err.instancePath.substring(1) : err.params?.missingProperty || "root";
51
- return ` - ${path}: ${err.message}`;
52
- })
53
- .join("\n") || "Unknown Validation Error";
54
-
55
- throw new Error(
56
- [
57
- `Validation Failed For Tool "${toolExecution.name}"`,
58
- `${errors}\n`,
59
- "Received Arguments:",
60
- `${JSON.stringify(toolExecution.rawArgs, null, 2)}\n`,
61
- ].join("\n"),
62
- );
63
- }