zrb 1.13.1__py3-none-any.whl → 1.21.17__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.
- zrb/__init__.py +2 -6
- zrb/attr/type.py +8 -8
- zrb/builtin/__init__.py +2 -0
- zrb/builtin/group.py +31 -15
- zrb/builtin/http.py +7 -8
- zrb/builtin/llm/attachment.py +40 -0
- zrb/builtin/llm/chat_session.py +130 -144
- zrb/builtin/llm/chat_session_cmd.py +226 -0
- zrb/builtin/llm/chat_trigger.py +73 -0
- zrb/builtin/llm/history.py +4 -4
- zrb/builtin/llm/llm_ask.py +218 -110
- zrb/builtin/llm/tool/api.py +74 -62
- zrb/builtin/llm/tool/cli.py +35 -16
- zrb/builtin/llm/tool/code.py +49 -47
- zrb/builtin/llm/tool/file.py +262 -251
- zrb/builtin/llm/tool/note.py +84 -0
- zrb/builtin/llm/tool/rag.py +25 -18
- zrb/builtin/llm/tool/sub_agent.py +29 -22
- zrb/builtin/llm/tool/web.py +135 -143
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/add_entity_util.py +7 -7
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/add_module_util.py +5 -5
- zrb/builtin/project/add/fastapp/fastapp_util.py +1 -1
- zrb/builtin/searxng/config/settings.yml +5671 -0
- zrb/builtin/searxng/start.py +21 -0
- zrb/builtin/setup/latex/ubuntu.py +1 -0
- zrb/builtin/setup/ubuntu.py +1 -1
- zrb/builtin/shell/autocomplete/bash.py +4 -3
- zrb/builtin/shell/autocomplete/zsh.py +4 -3
- zrb/config/config.py +255 -78
- zrb/config/default_prompt/file_extractor_system_prompt.md +109 -9
- zrb/config/default_prompt/interactive_system_prompt.md +24 -30
- zrb/config/default_prompt/persona.md +1 -1
- zrb/config/default_prompt/repo_extractor_system_prompt.md +31 -31
- zrb/config/default_prompt/repo_summarizer_system_prompt.md +27 -8
- zrb/config/default_prompt/summarization_prompt.md +8 -13
- zrb/config/default_prompt/system_prompt.md +36 -30
- zrb/config/llm_config.py +129 -24
- zrb/config/llm_context/config.py +127 -90
- zrb/config/llm_context/config_parser.py +1 -7
- zrb/config/llm_context/workflow.py +81 -0
- zrb/config/llm_rate_limitter.py +89 -45
- zrb/context/any_shared_context.py +7 -1
- zrb/context/context.py +8 -2
- zrb/context/shared_context.py +6 -8
- zrb/group/any_group.py +12 -5
- zrb/group/group.py +67 -3
- zrb/input/any_input.py +5 -1
- zrb/input/base_input.py +18 -6
- zrb/input/text_input.py +7 -24
- zrb/runner/cli.py +21 -20
- zrb/runner/common_util.py +24 -19
- zrb/runner/web_route/task_input_api_route.py +5 -5
- zrb/runner/web_route/task_session_api_route.py +1 -4
- zrb/runner/web_util/user.py +7 -3
- zrb/session/any_session.py +12 -6
- zrb/session/session.py +39 -18
- zrb/task/any_task.py +24 -3
- zrb/task/base/context.py +17 -9
- zrb/task/base/execution.py +15 -8
- zrb/task/base/lifecycle.py +8 -4
- zrb/task/base/monitoring.py +12 -7
- zrb/task/base_task.py +69 -5
- zrb/task/base_trigger.py +12 -5
- zrb/task/llm/agent.py +138 -52
- zrb/task/llm/config.py +45 -13
- zrb/task/llm/conversation_history.py +76 -6
- zrb/task/llm/conversation_history_model.py +0 -168
- zrb/task/llm/default_workflow/coding/workflow.md +41 -0
- zrb/task/llm/default_workflow/copywriting/workflow.md +68 -0
- zrb/task/llm/default_workflow/git/workflow.md +118 -0
- zrb/task/llm/default_workflow/golang/workflow.md +128 -0
- zrb/task/llm/default_workflow/html-css/workflow.md +135 -0
- zrb/task/llm/default_workflow/java/workflow.md +146 -0
- zrb/task/llm/default_workflow/javascript/workflow.md +158 -0
- zrb/task/llm/default_workflow/python/workflow.md +160 -0
- zrb/task/llm/default_workflow/researching/workflow.md +153 -0
- zrb/task/llm/default_workflow/rust/workflow.md +162 -0
- zrb/task/llm/default_workflow/shell/workflow.md +299 -0
- zrb/task/llm/file_replacement.py +206 -0
- zrb/task/llm/file_tool_model.py +57 -0
- zrb/task/llm/history_summarization.py +22 -35
- zrb/task/llm/history_summarization_tool.py +24 -0
- zrb/task/llm/print_node.py +182 -63
- zrb/task/llm/prompt.py +213 -153
- zrb/task/llm/tool_wrapper.py +210 -53
- zrb/task/llm/workflow.py +76 -0
- zrb/task/llm_task.py +98 -47
- zrb/task/make_task.py +2 -3
- zrb/task/rsync_task.py +25 -10
- zrb/task/scheduler.py +4 -4
- zrb/util/attr.py +50 -40
- zrb/util/cli/markdown.py +12 -0
- zrb/util/cli/text.py +30 -0
- zrb/util/file.py +27 -11
- zrb/util/{llm/prompt.py → markdown.py} +2 -3
- zrb/util/string/conversion.py +1 -1
- zrb/util/truncate.py +23 -0
- zrb/util/yaml.py +204 -0
- {zrb-1.13.1.dist-info → zrb-1.21.17.dist-info}/METADATA +40 -20
- {zrb-1.13.1.dist-info → zrb-1.21.17.dist-info}/RECORD +102 -79
- {zrb-1.13.1.dist-info → zrb-1.21.17.dist-info}/WHEEL +1 -1
- zrb/task/llm/default_workflow/coding.md +0 -24
- zrb/task/llm/default_workflow/copywriting.md +0 -17
- zrb/task/llm/default_workflow/researching.md +0 -18
- {zrb-1.13.1.dist-info → zrb-1.21.17.dist-info}/entry_points.txt +0 -0
zrb/task/llm/tool_wrapper.py
CHANGED
|
@@ -1,38 +1,60 @@
|
|
|
1
1
|
import functools
|
|
2
2
|
import inspect
|
|
3
|
+
import signal
|
|
3
4
|
import traceback
|
|
4
5
|
import typing
|
|
5
6
|
from collections.abc import Callable
|
|
6
|
-
from typing import TYPE_CHECKING
|
|
7
|
+
from typing import TYPE_CHECKING, Any
|
|
7
8
|
|
|
8
9
|
from zrb.config.config import CFG
|
|
10
|
+
from zrb.config.llm_rate_limitter import llm_rate_limitter
|
|
9
11
|
from zrb.context.any_context import AnyContext
|
|
10
12
|
from zrb.task.llm.error import ToolExecutionError
|
|
13
|
+
from zrb.task.llm.file_replacement import edit_replacement, is_single_path_replacement
|
|
11
14
|
from zrb.util.callable import get_callable_name
|
|
15
|
+
from zrb.util.cli.markdown import render_markdown
|
|
16
|
+
from zrb.util.cli.style import (
|
|
17
|
+
stylize_blue,
|
|
18
|
+
stylize_error,
|
|
19
|
+
stylize_faint,
|
|
20
|
+
stylize_green,
|
|
21
|
+
stylize_yellow,
|
|
22
|
+
)
|
|
23
|
+
from zrb.util.cli.text import edit_text
|
|
12
24
|
from zrb.util.run import run_async
|
|
13
25
|
from zrb.util.string.conversion import to_boolean
|
|
26
|
+
from zrb.util.yaml import edit_obj, yaml_dump
|
|
14
27
|
|
|
15
28
|
if TYPE_CHECKING:
|
|
16
29
|
from pydantic_ai import Tool
|
|
17
30
|
|
|
18
31
|
|
|
19
|
-
|
|
32
|
+
class ToolExecutionCancelled(ValueError):
|
|
33
|
+
pass
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def wrap_tool(func: Callable, ctx: AnyContext, yolo_mode: bool | list[str]) -> "Tool":
|
|
20
37
|
"""Wraps a tool function to handle exceptions and context propagation."""
|
|
21
38
|
from pydantic_ai import RunContext, Tool
|
|
22
39
|
|
|
23
40
|
original_sig = inspect.signature(func)
|
|
24
41
|
needs_run_context_for_pydantic = _has_context_parameter(original_sig, RunContext)
|
|
25
|
-
wrapper = wrap_func(func, ctx)
|
|
42
|
+
wrapper = wrap_func(func, ctx, yolo_mode)
|
|
26
43
|
return Tool(wrapper, takes_ctx=needs_run_context_for_pydantic)
|
|
27
44
|
|
|
28
45
|
|
|
29
|
-
def wrap_func(func: Callable, ctx: AnyContext) -> Callable:
|
|
46
|
+
def wrap_func(func: Callable, ctx: AnyContext, yolo_mode: bool | list[str]) -> Callable:
|
|
30
47
|
original_sig = inspect.signature(func)
|
|
31
48
|
needs_any_context_for_injection = _has_context_parameter(original_sig, AnyContext)
|
|
32
|
-
takes_no_args = len(original_sig.parameters) == 0
|
|
33
49
|
# Pass individual flags to the wrapper creator
|
|
34
|
-
wrapper = _create_wrapper(
|
|
35
|
-
|
|
50
|
+
wrapper = _create_wrapper(
|
|
51
|
+
func=func,
|
|
52
|
+
original_signature=original_sig,
|
|
53
|
+
ctx=ctx,
|
|
54
|
+
needs_any_context_for_injection=needs_any_context_for_injection,
|
|
55
|
+
yolo_mode=yolo_mode,
|
|
56
|
+
)
|
|
57
|
+
_adjust_signature(wrapper, original_sig)
|
|
36
58
|
return wrapper
|
|
37
59
|
|
|
38
60
|
|
|
@@ -67,9 +89,10 @@ def _is_annotated_with_context(param_annotation, context_type):
|
|
|
67
89
|
|
|
68
90
|
def _create_wrapper(
|
|
69
91
|
func: Callable,
|
|
70
|
-
|
|
71
|
-
ctx: AnyContext,
|
|
92
|
+
original_signature: inspect.Signature,
|
|
93
|
+
ctx: AnyContext,
|
|
72
94
|
needs_any_context_for_injection: bool,
|
|
95
|
+
yolo_mode: bool | list[str],
|
|
73
96
|
) -> Callable:
|
|
74
97
|
"""Creates the core wrapper function."""
|
|
75
98
|
|
|
@@ -78,7 +101,7 @@ def _create_wrapper(
|
|
|
78
101
|
# Identify AnyContext parameter name from the original signature if needed
|
|
79
102
|
any_context_param_name = None
|
|
80
103
|
if needs_any_context_for_injection:
|
|
81
|
-
for param in
|
|
104
|
+
for param in original_signature.parameters.values():
|
|
82
105
|
if _is_annotated_with_context(param.annotation, AnyContext):
|
|
83
106
|
any_context_param_name = param.name
|
|
84
107
|
break # Found it, no need to continue
|
|
@@ -91,38 +114,192 @@ def _create_wrapper(
|
|
|
91
114
|
# Inject the captured ctx into kwargs. This will overwrite if the LLM
|
|
92
115
|
# somehow provided it.
|
|
93
116
|
kwargs[any_context_param_name] = ctx
|
|
94
|
-
#
|
|
95
|
-
#
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
del kwargs["_dummy"]
|
|
117
|
+
# We will need to overwrite SIGINT handler, so that when user press ctrl + c,
|
|
118
|
+
# the program won't immediately exit
|
|
119
|
+
original_sigint_handler = signal.getsignal(signal.SIGINT)
|
|
120
|
+
tool_name = get_callable_name(func)
|
|
99
121
|
try:
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
122
|
+
has_ever_edited = False
|
|
123
|
+
if not ctx.is_web_mode and ctx.is_tty:
|
|
124
|
+
if (
|
|
125
|
+
isinstance(yolo_mode, list) and func.__name__ not in yolo_mode
|
|
126
|
+
) or not yolo_mode:
|
|
127
|
+
approval, reason, kwargs, has_ever_edited = (
|
|
128
|
+
await _handle_user_response(ctx, func, args, kwargs)
|
|
129
|
+
)
|
|
130
|
+
if not approval:
|
|
131
|
+
raise ToolExecutionCancelled(
|
|
132
|
+
f"Tool execution cancelled. User disapproving: {reason}"
|
|
133
|
+
)
|
|
134
|
+
signal.signal(signal.SIGINT, _tool_wrapper_sigint_handler)
|
|
135
|
+
ctx.print(stylize_faint(f"Run {tool_name}"), plain=True)
|
|
136
|
+
result = await run_async(func(*args, **kwargs))
|
|
137
|
+
_check_tool_call_result_limit(result)
|
|
138
|
+
if has_ever_edited:
|
|
139
|
+
return {
|
|
140
|
+
"tool_call_result": result,
|
|
141
|
+
"new_tool_parameters": kwargs,
|
|
142
|
+
"message": "User correction: Tool was called with user's parameters",
|
|
143
|
+
}
|
|
144
|
+
return result
|
|
145
|
+
except BaseException as e:
|
|
115
146
|
error_model = ToolExecutionError(
|
|
116
|
-
tool_name=
|
|
147
|
+
tool_name=tool_name,
|
|
117
148
|
error_type=type(e).__name__,
|
|
118
149
|
message=str(e),
|
|
119
150
|
details=traceback.format_exc(),
|
|
120
151
|
)
|
|
121
152
|
return error_model.model_dump_json()
|
|
153
|
+
finally:
|
|
154
|
+
signal.signal(signal.SIGINT, original_sigint_handler)
|
|
122
155
|
|
|
123
156
|
return wrapper
|
|
124
157
|
|
|
125
158
|
|
|
159
|
+
def _tool_wrapper_sigint_handler(signum, frame):
|
|
160
|
+
raise KeyboardInterrupt("SIGINT detected while running tool")
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _check_tool_call_result_limit(result: Any):
|
|
164
|
+
if (
|
|
165
|
+
llm_rate_limitter.count_token(result)
|
|
166
|
+
> llm_rate_limitter.max_tokens_per_tool_call_result
|
|
167
|
+
):
|
|
168
|
+
raise ValueError("Result value is too large, please adjust the parameter")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
async def _handle_user_response(
|
|
172
|
+
ctx: AnyContext,
|
|
173
|
+
func: Callable,
|
|
174
|
+
args: list[Any] | tuple[Any],
|
|
175
|
+
kwargs: dict[str, Any],
|
|
176
|
+
) -> tuple[bool, str, dict[str, Any], bool]:
|
|
177
|
+
has_ever_edited = False
|
|
178
|
+
while True:
|
|
179
|
+
func_call_str = _get_func_call_str(func, args, kwargs)
|
|
180
|
+
complete_confirmation_message = "\n".join(
|
|
181
|
+
[
|
|
182
|
+
f"\n🎰 >> {func_call_str}",
|
|
183
|
+
_get_detail_func_param(args, kwargs),
|
|
184
|
+
f"🎰 >> {_get_run_func_confirmation(func)}",
|
|
185
|
+
]
|
|
186
|
+
)
|
|
187
|
+
ctx.print(complete_confirmation_message, plain=True)
|
|
188
|
+
user_response = await _read_line()
|
|
189
|
+
ctx.print("", plain=True)
|
|
190
|
+
new_kwargs, is_edited = _get_edited_kwargs(ctx, user_response, kwargs)
|
|
191
|
+
if is_edited:
|
|
192
|
+
kwargs = new_kwargs
|
|
193
|
+
has_ever_edited = True
|
|
194
|
+
continue
|
|
195
|
+
approval_and_reason = _get_user_approval_and_reason(
|
|
196
|
+
ctx, user_response, func_call_str
|
|
197
|
+
)
|
|
198
|
+
if approval_and_reason is None:
|
|
199
|
+
continue
|
|
200
|
+
approval, reason = approval_and_reason
|
|
201
|
+
return approval, reason, kwargs, has_ever_edited
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _get_edited_kwargs(
|
|
205
|
+
ctx: AnyContext, user_response: str, kwargs: dict[str, Any]
|
|
206
|
+
) -> tuple[dict[str, Any], bool]:
|
|
207
|
+
user_edit_responses = [val for val in user_response.split(" ", maxsplit=2)]
|
|
208
|
+
if len(user_edit_responses) >= 1 and user_edit_responses[0].lower() != "edit":
|
|
209
|
+
return kwargs, False
|
|
210
|
+
while len(user_edit_responses) < 3:
|
|
211
|
+
user_edit_responses.append("")
|
|
212
|
+
key, val_str = user_edit_responses[1:]
|
|
213
|
+
# Make sure first segment of the key is in kwargs
|
|
214
|
+
if key != "":
|
|
215
|
+
key_parts = key.split(".")
|
|
216
|
+
if len(key_parts) > 0 and key_parts[0] not in kwargs:
|
|
217
|
+
return kwargs, True
|
|
218
|
+
# Handle replacement edit
|
|
219
|
+
if len(kwargs) == 1:
|
|
220
|
+
kwarg_key = list(kwargs.keys())[0]
|
|
221
|
+
if is_single_path_replacement(kwargs[kwarg_key]) and (
|
|
222
|
+
key == "" or key == kwarg_key
|
|
223
|
+
):
|
|
224
|
+
kwargs[kwarg_key], edited = edit_replacement(kwargs[kwarg_key])
|
|
225
|
+
return kwargs, True
|
|
226
|
+
# Handle other kind of edit
|
|
227
|
+
old_val_str = yaml_dump(kwargs, key)
|
|
228
|
+
if val_str == "":
|
|
229
|
+
val_str = edit_text(
|
|
230
|
+
prompt_message=f"# {key}" if key != "" else "",
|
|
231
|
+
value=old_val_str,
|
|
232
|
+
editor=CFG.DEFAULT_EDITOR,
|
|
233
|
+
extension=".yaml",
|
|
234
|
+
)
|
|
235
|
+
if old_val_str == val_str:
|
|
236
|
+
return kwargs, True
|
|
237
|
+
edited_kwargs = edit_obj(kwargs, key, val_str)
|
|
238
|
+
return edited_kwargs, True
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _get_user_approval_and_reason(
|
|
242
|
+
ctx: AnyContext, user_response: str, func_call_str: str
|
|
243
|
+
) -> tuple[bool, str] | None:
|
|
244
|
+
user_approval_responses = [
|
|
245
|
+
val.strip() for val in user_response.split(",", maxsplit=1)
|
|
246
|
+
]
|
|
247
|
+
while len(user_approval_responses) < 2:
|
|
248
|
+
user_approval_responses.append("")
|
|
249
|
+
approval_str, reason = user_approval_responses
|
|
250
|
+
try:
|
|
251
|
+
approved = True if approval_str.strip() == "" else to_boolean(approval_str)
|
|
252
|
+
if not approved and reason == "":
|
|
253
|
+
ctx.print(
|
|
254
|
+
stylize_error(
|
|
255
|
+
f"You must specify rejection reason (i.e., No, <why>) for {func_call_str}" # noqa
|
|
256
|
+
),
|
|
257
|
+
plain=True,
|
|
258
|
+
)
|
|
259
|
+
return None
|
|
260
|
+
return approved, reason
|
|
261
|
+
except Exception:
|
|
262
|
+
return False, user_response
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _get_run_func_confirmation(func: Callable) -> str:
|
|
266
|
+
func_name = get_callable_name(func)
|
|
267
|
+
return render_markdown(
|
|
268
|
+
f"Allow to run `{func_name}`? (✅ `Yes` | ⛔ `No, <reason>` | 📝 `Edit <param> <value>`)"
|
|
269
|
+
).strip()
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _get_detail_func_param(args: list[Any] | tuple[Any], kwargs: dict[str, Any]) -> str:
|
|
273
|
+
if not kwargs:
|
|
274
|
+
return ""
|
|
275
|
+
yaml_str = yaml_dump(kwargs)
|
|
276
|
+
# Create the final markdown string
|
|
277
|
+
markdown = f"```yaml\n{yaml_str}\n```"
|
|
278
|
+
return render_markdown(markdown)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def _get_func_call_str(
|
|
282
|
+
func: Callable, args: list[Any] | tuple[Any], kwargs: dict[str, Any]
|
|
283
|
+
) -> str:
|
|
284
|
+
func_name = get_callable_name(func)
|
|
285
|
+
normalized_args = [stylize_green(_truncate_arg(arg)) for arg in args]
|
|
286
|
+
normalized_kwargs = []
|
|
287
|
+
for key, val in kwargs.items():
|
|
288
|
+
truncated_val = _truncate_arg(f"{val}")
|
|
289
|
+
normalized_kwargs.append(
|
|
290
|
+
f"{stylize_yellow(key)}={stylize_green(truncated_val)}"
|
|
291
|
+
)
|
|
292
|
+
func_param_str = ", ".join(normalized_args + normalized_kwargs)
|
|
293
|
+
return f"{stylize_blue(func_name + '(')}{func_param_str}{stylize_blue(')')}"
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _truncate_arg(arg: str, length: int = 19) -> str:
|
|
297
|
+
normalized_arg = arg.replace("\n", "\\n")
|
|
298
|
+
if len(normalized_arg) > length:
|
|
299
|
+
return f"{normalized_arg[:length-4]} ..."
|
|
300
|
+
return normalized_arg
|
|
301
|
+
|
|
302
|
+
|
|
126
303
|
async def _read_line():
|
|
127
304
|
from prompt_toolkit import PromptSession
|
|
128
305
|
|
|
@@ -130,9 +307,7 @@ async def _read_line():
|
|
|
130
307
|
return await reader.prompt_async()
|
|
131
308
|
|
|
132
309
|
|
|
133
|
-
def _adjust_signature(
|
|
134
|
-
wrapper: Callable, original_sig: inspect.Signature, takes_no_args: bool
|
|
135
|
-
):
|
|
310
|
+
def _adjust_signature(wrapper: Callable, original_sig: inspect.Signature):
|
|
136
311
|
"""Adjusts the wrapper function's signature for schema generation."""
|
|
137
312
|
# The wrapper's signature should represent the arguments the *LLM* needs to provide.
|
|
138
313
|
# The LLM does not provide RunContext (pydantic-ai injects it) or AnyContext
|
|
@@ -147,22 +322,4 @@ def _adjust_signature(
|
|
|
147
322
|
if not _is_annotated_with_context(param.annotation, RunContext)
|
|
148
323
|
and not _is_annotated_with_context(param.annotation, AnyContext)
|
|
149
324
|
]
|
|
150
|
-
|
|
151
|
-
# If after removing context parameters, there are no parameters left,
|
|
152
|
-
# and the original function took no args, keep the dummy.
|
|
153
|
-
# If after removing context parameters, there are no parameters left,
|
|
154
|
-
# but the original function *did* take args (only context), then the schema
|
|
155
|
-
# should have no parameters.
|
|
156
|
-
if not params_for_schema and takes_no_args:
|
|
157
|
-
# Keep the dummy if the original function truly had no parameters
|
|
158
|
-
new_sig = inspect.Signature(
|
|
159
|
-
parameters=[
|
|
160
|
-
inspect.Parameter(
|
|
161
|
-
"_dummy", inspect.Parameter.POSITIONAL_OR_KEYWORD, default=None
|
|
162
|
-
)
|
|
163
|
-
]
|
|
164
|
-
)
|
|
165
|
-
else:
|
|
166
|
-
new_sig = inspect.Signature(parameters=params_for_schema)
|
|
167
|
-
|
|
168
|
-
wrapper.__signature__ = new_sig
|
|
325
|
+
wrapper.__signature__ = inspect.Signature(parameters=params_for_schema)
|
zrb/task/llm/workflow.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from zrb.config.config import CFG
|
|
4
|
+
from zrb.config.llm_context.config import llm_context_config
|
|
5
|
+
from zrb.config.llm_context.workflow import LLMWorkflow
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def load_workflow(workflow_name: str | list[str]) -> str:
|
|
9
|
+
"""
|
|
10
|
+
Loads and formats one or more workflow documents for LLM consumption.
|
|
11
|
+
|
|
12
|
+
Retrieves workflows by name, formats with descriptive headers for LLM context injection.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
workflow_name: Name or list of names of the workflow(s) to load
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
Formatted workflow content as a string with headers
|
|
19
|
+
|
|
20
|
+
Raises:
|
|
21
|
+
ValueError: If any specified workflow name is not found
|
|
22
|
+
"""
|
|
23
|
+
names = [workflow_name] if isinstance(workflow_name, str) else workflow_name
|
|
24
|
+
available_workflows = get_available_workflows()
|
|
25
|
+
contents = []
|
|
26
|
+
for name in names:
|
|
27
|
+
workflow = available_workflows.get(name.strip().lower())
|
|
28
|
+
if workflow is None:
|
|
29
|
+
raise ValueError(f"Workflow not found: {name}")
|
|
30
|
+
contents.append(
|
|
31
|
+
"\n".join(
|
|
32
|
+
[
|
|
33
|
+
f"# {workflow.name}",
|
|
34
|
+
f"> Workflow Location: `{workflow.path}`",
|
|
35
|
+
workflow.content,
|
|
36
|
+
]
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
return "\n".join(contents)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def get_available_workflows() -> dict[str, LLMWorkflow]:
|
|
43
|
+
available_workflows = {
|
|
44
|
+
workflow_name.strip().lower(): workflow
|
|
45
|
+
for workflow_name, workflow in llm_context_config.get_workflows().items()
|
|
46
|
+
}
|
|
47
|
+
# Define builtin workflow locations in order of precedence
|
|
48
|
+
builtin_workflow_locations = [
|
|
49
|
+
os.path.expanduser(additional_builtin_workflow_path)
|
|
50
|
+
for additional_builtin_workflow_path in CFG.LLM_BUILTIN_WORKFLOW_PATHS
|
|
51
|
+
if os.path.isdir(os.path.expanduser(additional_builtin_workflow_path))
|
|
52
|
+
]
|
|
53
|
+
builtin_workflow_locations.append(
|
|
54
|
+
os.path.join(os.path.dirname(__file__), "default_workflow")
|
|
55
|
+
)
|
|
56
|
+
# Load workflows from all locations
|
|
57
|
+
for workflow_location in builtin_workflow_locations:
|
|
58
|
+
if not os.path.isdir(workflow_location):
|
|
59
|
+
continue
|
|
60
|
+
for workflow_name in os.listdir(workflow_location):
|
|
61
|
+
workflow_dir = os.path.join(workflow_location, workflow_name)
|
|
62
|
+
workflow_file = os.path.join(workflow_dir, "workflow.md")
|
|
63
|
+
if not os.path.isfile(workflow_file):
|
|
64
|
+
workflow_file = os.path.join(workflow_dir, "SKILL.md")
|
|
65
|
+
if not os.path.isfile(path=workflow_file):
|
|
66
|
+
continue
|
|
67
|
+
# Only add if not already defined (earlier locations have precedence)
|
|
68
|
+
if workflow_name not in available_workflows:
|
|
69
|
+
with open(workflow_file, "r") as f:
|
|
70
|
+
workflow_content = f.read()
|
|
71
|
+
available_workflows[workflow_name] = LLMWorkflow(
|
|
72
|
+
name=workflow_name,
|
|
73
|
+
path=workflow_dir,
|
|
74
|
+
content=workflow_content,
|
|
75
|
+
)
|
|
76
|
+
return available_workflows
|