kegal 0.1.4.0__py3-none-any.whl

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.
kegal/__init__.py ADDED
@@ -0,0 +1,94 @@
1
+ """
2
+ KeGAL - Kedos Graph Agent for LLM
3
+
4
+ A graph-based agent framework for LLMs that enables the development
5
+ of workflows structured as graphs.
6
+ """
7
+
8
+ from .graph import (
9
+ Graph,
10
+ GraphModel,
11
+ GraphInputData,
12
+ GraphBlackboard,
13
+ BlackboardEntry,
14
+ NodeBlackboardRef,
15
+ ChatHistoryFile,
16
+ NodePrompt,
17
+ NodeMessagePassing,
18
+ NodeMcpServerRef,
19
+ NodeReact,
20
+ GraphNode,
21
+ GraphEdge,
22
+ GraphMcpServer,
23
+ )
24
+ from .compiler import Compiler, CompiledOutput, CompiledNodeOutput, ReactTrace, ReactIteration
25
+ from .compose import (
26
+ compose_template_prompt,
27
+ compose_node_prompt,
28
+ compose_images,
29
+ compose_documents,
30
+ compose_tools,
31
+ )
32
+ from .utils import (
33
+ load_yml,
34
+ load_json,
35
+ load_contents,
36
+ load_images_to_base64,
37
+ load_pdfs_to_base64,
38
+ )
39
+ from .validators import (
40
+ validate_anthropic_schema,
41
+ validate_openai_schema,
42
+ validate_llm_input_schema,
43
+ SchemaIssue,
44
+ )
45
+
46
+ # Import LLM subpackage
47
+ from . import llm
48
+ from .llm.llm_model import LLMTool
49
+
50
+ __version__ = "0.1.4.0"
51
+
52
+ __all__ = [
53
+ # Graph models
54
+ "Graph",
55
+ "GraphModel",
56
+ "GraphInputData",
57
+ "GraphBlackboard",
58
+ "BlackboardEntry",
59
+ "NodeBlackboardRef",
60
+ "ChatHistoryFile",
61
+ "NodePrompt",
62
+ "NodeMessagePassing",
63
+ "NodeMcpServerRef",
64
+ "NodeReact",
65
+ "GraphNode",
66
+ "GraphEdge",
67
+ "GraphMcpServer",
68
+ # Compiler
69
+ "Compiler",
70
+ "CompiledOutput",
71
+ "CompiledNodeOutput",
72
+ "ReactTrace",
73
+ "ReactIteration",
74
+ # Compose utilities
75
+ "compose_template_prompt",
76
+ "compose_node_prompt",
77
+ "compose_images",
78
+ "compose_documents",
79
+ "compose_tools",
80
+ # Utils
81
+ "load_yml",
82
+ "load_json",
83
+ "load_contents",
84
+ "load_images_to_base64",
85
+ "load_pdfs_to_base64",
86
+ # Validators
87
+ "validate_anthropic_schema",
88
+ "validate_openai_schema",
89
+ "validate_llm_input_schema",
90
+ "SchemaIssue",
91
+ # LLM subpackage
92
+ "llm",
93
+ "LLMTool",
94
+ ]
kegal/cli.py ADDED
@@ -0,0 +1,185 @@
1
+ import importlib.util
2
+ import json
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ import yaml
7
+
8
+ from .compiler import Compiler
9
+ from . import __version__
10
+
11
+ _KNOWN_CONFIG_KEYS = {"graph", "mode", "message", "chunks", "tools_module"}
12
+
13
+
14
+ def _load_config(project_path: str) -> tuple[dict, Path]:
15
+ path = Path(project_path).resolve()
16
+ config_file = path / "kegal.yml"
17
+ if not config_file.exists():
18
+ print(f"Error: kegal.yml not found in '{path}'", file=sys.stderr)
19
+ sys.exit(1)
20
+ with open(config_file, encoding="utf-8") as f:
21
+ config = yaml.safe_load(f) or {}
22
+ unknown = set(config.keys()) - _KNOWN_CONFIG_KEYS
23
+ if unknown:
24
+ print(
25
+ f"Warning: unknown key(s) in kegal.yml ignored: {sorted(unknown)}",
26
+ file=sys.stderr,
27
+ )
28
+ return config, path
29
+
30
+
31
+ def _load_tool_executors(project_path: Path, rel_path: str) -> dict:
32
+ """Load tool_executors from a Python module file relative to project_path."""
33
+ tools_path = (project_path / rel_path).resolve()
34
+ if not tools_path.exists():
35
+ raise ValueError(f"tools_module '{tools_path}' not found")
36
+ spec = importlib.util.spec_from_file_location("kegal_tools", tools_path)
37
+ module = importlib.util.module_from_spec(spec)
38
+ spec.loader.exec_module(module)
39
+ executors = getattr(module, "tool_executors", None)
40
+ if not isinstance(executors, dict) or not executors:
41
+ raise ValueError(
42
+ f"tools_module '{tools_path}' must define a non-empty 'tool_executors' dict"
43
+ )
44
+ return executors
45
+
46
+
47
+ def _print_outputs(compiler: Compiler) -> None:
48
+ visible = [n for n in compiler.get_outputs().nodes if n.show]
49
+ if not visible:
50
+ print("(no output — set show: true on nodes you want to display)", file=sys.stderr)
51
+ return
52
+ for i, node in enumerate(visible):
53
+ if i > 0:
54
+ print()
55
+ if len(visible) > 1:
56
+ print(f"[{node.node_id}]")
57
+ if node.response.messages:
58
+ print("\n".join(node.response.messages))
59
+ elif node.response.json_output is not None:
60
+ print(json.dumps(node.response.json_output, indent=2, ensure_ascii=False))
61
+
62
+
63
+ def _run_once(graph_path: Path, tool_executors: dict | None = None) -> None:
64
+ try:
65
+ with Compiler(uri=str(graph_path), tool_executors=tool_executors) as compiler:
66
+ compiler.compile()
67
+ _print_outputs(compiler)
68
+ except (ValueError, RuntimeError) as e:
69
+ print(f"Error: {e}", file=sys.stderr)
70
+ sys.exit(1)
71
+
72
+
73
+ def _run_chat(graph_path: Path, message_enabled: bool, chunks_enabled: bool,
74
+ tool_executors: dict | None = None) -> None:
75
+ if not message_enabled:
76
+ print(
77
+ "Warning: message is false in chat mode — the graph will run "
78
+ "with no user input on each turn.",
79
+ file=sys.stderr,
80
+ )
81
+
82
+ try:
83
+ with Compiler(uri=str(graph_path), tool_executors=tool_executors) as compiler:
84
+ while True:
85
+ try:
86
+ if message_enabled:
87
+ msg = input("\nmessage (Ctrl+D to exit): ")
88
+ compiler.user_message = msg
89
+
90
+ if chunks_enabled:
91
+ chunks = input("chunks: ")
92
+ compiler.retrieved_chunks = chunks
93
+
94
+ print()
95
+ compiler.compile()
96
+ _print_outputs(compiler)
97
+
98
+ except EOFError:
99
+ print("\nBye.")
100
+ break
101
+ except KeyboardInterrupt:
102
+ print()
103
+ break
104
+ except (ValueError, RuntimeError) as e:
105
+ print(f"Error: {e}", file=sys.stderr)
106
+ except (ValueError, RuntimeError) as e:
107
+ print(f"Error: {e}", file=sys.stderr)
108
+ sys.exit(1)
109
+
110
+
111
+ def _cmd_run(args) -> None:
112
+ config, project_dir = _load_config(args.path)
113
+
114
+ graph_rel = config.get("graph")
115
+ if not graph_rel:
116
+ print("Error: 'graph' key missing in kegal.yml", file=sys.stderr)
117
+ sys.exit(1)
118
+
119
+ graph_path = project_dir / graph_rel
120
+ if not graph_path.exists():
121
+ print(f"Error: graph file '{graph_path}' not found", file=sys.stderr)
122
+ sys.exit(1)
123
+
124
+ mode = config.get("mode", "once")
125
+ message_enabled = bool(config.get("message", False))
126
+ chunks_enabled = bool(config.get("chunks", False))
127
+
128
+ tool_executors = None
129
+ tools_module = config.get("tools_module")
130
+ if tools_module:
131
+ try:
132
+ tool_executors = _load_tool_executors(project_dir, tools_module)
133
+ except ValueError as e:
134
+ print(f"Error: {e}", file=sys.stderr)
135
+ sys.exit(1)
136
+
137
+ if mode == "once" and (message_enabled or chunks_enabled):
138
+ flags = [k for k, v in [("message", message_enabled), ("chunks", chunks_enabled)] if v]
139
+ print(
140
+ f"Warning: {flags} set in kegal.yml but ignored in 'once' mode "
141
+ f"(only applies to 'chat' mode).",
142
+ file=sys.stderr,
143
+ )
144
+
145
+ if mode == "chat":
146
+ _run_chat(graph_path, message_enabled, chunks_enabled, tool_executors)
147
+ elif mode == "once":
148
+ _run_once(graph_path, tool_executors)
149
+ else:
150
+ print(
151
+ f"Error: unknown mode '{mode}' in kegal.yml (expected: chat, once)",
152
+ file=sys.stderr,
153
+ )
154
+ sys.exit(1)
155
+
156
+
157
+ def main() -> None:
158
+ import argparse
159
+
160
+ parser = argparse.ArgumentParser(
161
+ prog="kegal",
162
+ description="KeGAL — Kedos Graph Agent for LLM",
163
+ )
164
+ parser.add_argument(
165
+ "--version", action="version", version=f"kegal {__version__}"
166
+ )
167
+ subparsers = parser.add_subparsers(dest="command")
168
+
169
+ run_parser = subparsers.add_parser("run", help="Run a KeGAL project")
170
+ run_parser.add_argument(
171
+ "path",
172
+ nargs="?",
173
+ default=".",
174
+ help="Path to the project folder containing kegal.yml (default: current directory)",
175
+ )
176
+ args = parser.parse_args()
177
+
178
+ if args.command == "run":
179
+ _cmd_run(args)
180
+ else:
181
+ parser.print_help()
182
+
183
+
184
+ if __name__ == "__main__":
185
+ main()