zrb 1.21.29__py3-none-any.whl → 2.0.0a4__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.
Potentially problematic release.
This version of zrb might be problematic. Click here for more details.
- zrb/__init__.py +118 -129
- zrb/builtin/__init__.py +54 -2
- zrb/builtin/llm/chat.py +147 -0
- zrb/callback/callback.py +8 -1
- zrb/cmd/cmd_result.py +2 -1
- zrb/config/config.py +491 -280
- zrb/config/helper.py +84 -0
- zrb/config/web_auth_config.py +50 -35
- zrb/context/any_shared_context.py +13 -2
- zrb/context/context.py +31 -3
- zrb/context/print_fn.py +13 -0
- zrb/context/shared_context.py +14 -1
- zrb/input/option_input.py +30 -2
- zrb/llm/agent/__init__.py +9 -0
- zrb/llm/agent/agent.py +215 -0
- zrb/llm/agent/summarizer.py +20 -0
- zrb/llm/app/__init__.py +10 -0
- zrb/llm/app/completion.py +281 -0
- zrb/llm/app/confirmation/allow_tool.py +66 -0
- zrb/llm/app/confirmation/handler.py +178 -0
- zrb/llm/app/confirmation/replace_confirmation.py +77 -0
- zrb/llm/app/keybinding.py +34 -0
- zrb/llm/app/layout.py +117 -0
- zrb/llm/app/lexer.py +155 -0
- zrb/llm/app/redirection.py +28 -0
- zrb/llm/app/style.py +16 -0
- zrb/llm/app/ui.py +733 -0
- zrb/llm/config/__init__.py +4 -0
- zrb/llm/config/config.py +122 -0
- zrb/llm/config/limiter.py +247 -0
- zrb/llm/history_manager/__init__.py +4 -0
- zrb/llm/history_manager/any_history_manager.py +23 -0
- zrb/llm/history_manager/file_history_manager.py +91 -0
- zrb/llm/history_processor/summarizer.py +108 -0
- zrb/llm/note/__init__.py +3 -0
- zrb/llm/note/manager.py +122 -0
- zrb/llm/prompt/__init__.py +29 -0
- zrb/llm/prompt/claude_compatibility.py +92 -0
- zrb/llm/prompt/compose.py +55 -0
- zrb/llm/prompt/default.py +51 -0
- zrb/llm/prompt/markdown/mandate.md +23 -0
- zrb/llm/prompt/markdown/persona.md +3 -0
- zrb/llm/prompt/markdown/summarizer.md +21 -0
- zrb/llm/prompt/note.py +41 -0
- zrb/llm/prompt/system_context.py +46 -0
- zrb/llm/prompt/zrb.py +41 -0
- zrb/llm/skill/__init__.py +3 -0
- zrb/llm/skill/manager.py +86 -0
- zrb/llm/task/__init__.py +4 -0
- zrb/llm/task/llm_chat_task.py +316 -0
- zrb/llm/task/llm_task.py +245 -0
- zrb/llm/tool/__init__.py +39 -0
- zrb/llm/tool/bash.py +75 -0
- zrb/llm/tool/code.py +266 -0
- zrb/llm/tool/file.py +419 -0
- zrb/llm/tool/note.py +70 -0
- zrb/{builtin/llm → llm}/tool/rag.py +8 -5
- zrb/llm/tool/search/brave.py +53 -0
- zrb/llm/tool/search/searxng.py +47 -0
- zrb/llm/tool/search/serpapi.py +47 -0
- zrb/llm/tool/skill.py +19 -0
- zrb/llm/tool/sub_agent.py +70 -0
- zrb/llm/tool/web.py +97 -0
- zrb/llm/tool/zrb_task.py +66 -0
- zrb/llm/util/attachment.py +101 -0
- zrb/llm/util/prompt.py +104 -0
- zrb/llm/util/stream_response.py +178 -0
- zrb/session/any_session.py +0 -3
- zrb/session/session.py +1 -1
- zrb/task/base/context.py +25 -13
- zrb/task/base/execution.py +52 -47
- zrb/task/base/lifecycle.py +7 -4
- zrb/task/base_task.py +48 -49
- zrb/task/base_trigger.py +4 -1
- zrb/task/cmd_task.py +6 -0
- zrb/task/http_check.py +11 -5
- zrb/task/make_task.py +3 -0
- zrb/task/rsync_task.py +5 -0
- zrb/task/scaffolder.py +7 -4
- zrb/task/scheduler.py +3 -0
- zrb/task/tcp_check.py +6 -4
- zrb/util/ascii_art/art/bee.txt +17 -0
- zrb/util/ascii_art/art/cat.txt +9 -0
- zrb/util/ascii_art/art/ghost.txt +16 -0
- zrb/util/ascii_art/art/panda.txt +17 -0
- zrb/util/ascii_art/art/rose.txt +14 -0
- zrb/util/ascii_art/art/unicorn.txt +15 -0
- zrb/util/ascii_art/banner.py +92 -0
- zrb/util/cli/markdown.py +22 -2
- zrb/util/cmd/command.py +33 -10
- zrb/util/file.py +51 -32
- zrb/util/match.py +78 -0
- zrb/util/run.py +3 -3
- {zrb-1.21.29.dist-info → zrb-2.0.0a4.dist-info}/METADATA +9 -15
- {zrb-1.21.29.dist-info → zrb-2.0.0a4.dist-info}/RECORD +100 -128
- zrb/attr/__init__.py +0 -0
- zrb/builtin/llm/attachment.py +0 -40
- zrb/builtin/llm/chat_completion.py +0 -274
- zrb/builtin/llm/chat_session.py +0 -270
- zrb/builtin/llm/chat_session_cmd.py +0 -288
- zrb/builtin/llm/chat_trigger.py +0 -79
- zrb/builtin/llm/history.py +0 -71
- zrb/builtin/llm/input.py +0 -27
- zrb/builtin/llm/llm_ask.py +0 -269
- zrb/builtin/llm/previous-session.js +0 -21
- zrb/builtin/llm/tool/__init__.py +0 -0
- zrb/builtin/llm/tool/api.py +0 -75
- zrb/builtin/llm/tool/cli.py +0 -52
- zrb/builtin/llm/tool/code.py +0 -236
- zrb/builtin/llm/tool/file.py +0 -560
- zrb/builtin/llm/tool/note.py +0 -84
- zrb/builtin/llm/tool/sub_agent.py +0 -150
- zrb/builtin/llm/tool/web.py +0 -171
- zrb/builtin/project/__init__.py +0 -0
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/__init__.py +0 -0
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/template/app_template/module/my_module/service/__init__.py +0 -0
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/common/__init__.py +0 -0
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/__init__.py +0 -0
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/__init__.py +0 -0
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/permission/__init__.py +0 -0
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/role/__init__.py +0 -0
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/module/auth/service/user/__init__.py +0 -0
- zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/schema/__init__.py +0 -0
- zrb/builtin/project/create/__init__.py +0 -0
- zrb/builtin/shell/__init__.py +0 -0
- zrb/builtin/shell/autocomplete/__init__.py +0 -0
- zrb/callback/__init__.py +0 -0
- zrb/cmd/__init__.py +0 -0
- zrb/config/default_prompt/interactive_system_prompt.md +0 -29
- zrb/config/default_prompt/persona.md +0 -1
- zrb/config/default_prompt/summarization_prompt.md +0 -57
- zrb/config/default_prompt/system_prompt.md +0 -38
- zrb/config/llm_config.py +0 -339
- zrb/config/llm_context/config.py +0 -166
- zrb/config/llm_context/config_parser.py +0 -40
- zrb/config/llm_context/workflow.py +0 -81
- zrb/config/llm_rate_limitter.py +0 -190
- zrb/content_transformer/__init__.py +0 -0
- zrb/context/__init__.py +0 -0
- zrb/dot_dict/__init__.py +0 -0
- zrb/env/__init__.py +0 -0
- zrb/group/__init__.py +0 -0
- zrb/input/__init__.py +0 -0
- zrb/runner/__init__.py +0 -0
- zrb/runner/web_route/__init__.py +0 -0
- zrb/runner/web_route/home_page/__init__.py +0 -0
- zrb/session/__init__.py +0 -0
- zrb/session_state_log/__init__.py +0 -0
- zrb/session_state_logger/__init__.py +0 -0
- zrb/task/__init__.py +0 -0
- zrb/task/base/__init__.py +0 -0
- zrb/task/llm/__init__.py +0 -0
- zrb/task/llm/agent.py +0 -204
- zrb/task/llm/agent_runner.py +0 -152
- zrb/task/llm/config.py +0 -122
- zrb/task/llm/conversation_history.py +0 -209
- zrb/task/llm/conversation_history_model.py +0 -67
- zrb/task/llm/default_workflow/coding/workflow.md +0 -41
- zrb/task/llm/default_workflow/copywriting/workflow.md +0 -68
- zrb/task/llm/default_workflow/git/workflow.md +0 -118
- zrb/task/llm/default_workflow/golang/workflow.md +0 -128
- zrb/task/llm/default_workflow/html-css/workflow.md +0 -135
- zrb/task/llm/default_workflow/java/workflow.md +0 -146
- zrb/task/llm/default_workflow/javascript/workflow.md +0 -158
- zrb/task/llm/default_workflow/python/workflow.md +0 -160
- zrb/task/llm/default_workflow/researching/workflow.md +0 -153
- zrb/task/llm/default_workflow/rust/workflow.md +0 -162
- zrb/task/llm/default_workflow/shell/workflow.md +0 -299
- zrb/task/llm/error.py +0 -95
- zrb/task/llm/file_replacement.py +0 -206
- zrb/task/llm/file_tool_model.py +0 -57
- zrb/task/llm/history_processor.py +0 -206
- zrb/task/llm/history_summarization.py +0 -25
- zrb/task/llm/print_node.py +0 -221
- zrb/task/llm/prompt.py +0 -321
- zrb/task/llm/subagent_conversation_history.py +0 -41
- zrb/task/llm/tool_wrapper.py +0 -361
- zrb/task/llm/typing.py +0 -3
- zrb/task/llm/workflow.py +0 -76
- zrb/task/llm_task.py +0 -379
- zrb/task_status/__init__.py +0 -0
- zrb/util/__init__.py +0 -0
- zrb/util/cli/__init__.py +0 -0
- zrb/util/cmd/__init__.py +0 -0
- zrb/util/codemod/__init__.py +0 -0
- zrb/util/string/__init__.py +0 -0
- zrb/xcom/__init__.py +0 -0
- /zrb/{config/default_prompt/file_extractor_system_prompt.md → llm/prompt/markdown/file_extractor.md} +0 -0
- /zrb/{config/default_prompt/repo_extractor_system_prompt.md → llm/prompt/markdown/repo_extractor.md} +0 -0
- /zrb/{config/default_prompt/repo_summarizer_system_prompt.md → llm/prompt/markdown/repo_summarizer.md} +0 -0
- {zrb-1.21.29.dist-info → zrb-2.0.0a4.dist-info}/WHEEL +0 -0
- {zrb-1.21.29.dist-info → zrb-2.0.0a4.dist-info}/entry_points.txt +0 -0
zrb/task/llm/prompt.py
DELETED
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import platform
|
|
3
|
-
import re
|
|
4
|
-
from datetime import datetime, timezone
|
|
5
|
-
from typing import TYPE_CHECKING, Callable
|
|
6
|
-
|
|
7
|
-
from zrb.attr.type import StrAttr, StrListAttr
|
|
8
|
-
from zrb.config.llm_config import llm_config
|
|
9
|
-
from zrb.context.any_context import AnyContext
|
|
10
|
-
from zrb.task.llm.conversation_history_model import ConversationHistory
|
|
11
|
-
from zrb.task.llm.workflow import LLMWorkflow, get_available_workflows
|
|
12
|
-
from zrb.util.attr import get_attr, get_str_attr, get_str_list_attr
|
|
13
|
-
from zrb.util.file import read_dir, read_file_with_line_numbers
|
|
14
|
-
from zrb.util.markdown import make_markdown_section
|
|
15
|
-
|
|
16
|
-
if TYPE_CHECKING:
|
|
17
|
-
from pydantic_ai.messages import UserContent
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def get_system_and_user_prompt(
|
|
21
|
-
ctx: AnyContext,
|
|
22
|
-
user_message: str,
|
|
23
|
-
persona_attr: StrAttr | None = None,
|
|
24
|
-
render_persona: bool = False,
|
|
25
|
-
system_prompt_attr: StrAttr | None = None,
|
|
26
|
-
render_system_prompt: bool = False,
|
|
27
|
-
special_instruction_prompt_attr: StrAttr | None = None,
|
|
28
|
-
render_special_instruction_prompt: bool = False,
|
|
29
|
-
workflows_attr: StrListAttr | None = None,
|
|
30
|
-
render_workflows: bool = False,
|
|
31
|
-
conversation_history: ConversationHistory | None = None,
|
|
32
|
-
) -> tuple[str, str]:
|
|
33
|
-
if conversation_history is None:
|
|
34
|
-
conversation_history = ConversationHistory()
|
|
35
|
-
new_user_message_prompt, apendixes = _get_user_message_prompt(user_message)
|
|
36
|
-
new_system_prompt = _construct_system_prompt(
|
|
37
|
-
ctx=ctx,
|
|
38
|
-
user_message=user_message,
|
|
39
|
-
apendixes=apendixes,
|
|
40
|
-
persona_attr=persona_attr,
|
|
41
|
-
render_persona=render_persona,
|
|
42
|
-
system_prompt_attr=system_prompt_attr,
|
|
43
|
-
render_system_prompt=render_system_prompt,
|
|
44
|
-
special_instruction_prompt_attr=special_instruction_prompt_attr,
|
|
45
|
-
render_special_instruction_prompt=render_special_instruction_prompt,
|
|
46
|
-
workflows_attr=workflows_attr,
|
|
47
|
-
render_workflows=render_workflows,
|
|
48
|
-
conversation_history=conversation_history,
|
|
49
|
-
)
|
|
50
|
-
return new_system_prompt, new_user_message_prompt
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def _construct_system_prompt(
|
|
54
|
-
ctx: AnyContext,
|
|
55
|
-
user_message: str,
|
|
56
|
-
apendixes: str,
|
|
57
|
-
persona_attr: StrAttr | None = None,
|
|
58
|
-
render_persona: bool = False,
|
|
59
|
-
system_prompt_attr: StrAttr | None = None,
|
|
60
|
-
render_system_prompt: bool = False,
|
|
61
|
-
special_instruction_prompt_attr: StrAttr | None = None,
|
|
62
|
-
render_special_instruction_prompt: bool = False,
|
|
63
|
-
workflows_attr: StrListAttr | None = None,
|
|
64
|
-
render_workflows: bool = False,
|
|
65
|
-
conversation_history: ConversationHistory | None = None,
|
|
66
|
-
) -> str:
|
|
67
|
-
persona = _get_persona(ctx, persona_attr, render_persona)
|
|
68
|
-
base_system_prompt = _get_base_system_prompt(
|
|
69
|
-
ctx, system_prompt_attr, render_system_prompt
|
|
70
|
-
)
|
|
71
|
-
special_instruction_prompt = _get_special_instruction_prompt(
|
|
72
|
-
ctx, special_instruction_prompt_attr, render_special_instruction_prompt
|
|
73
|
-
)
|
|
74
|
-
available_workflows = get_available_workflows()
|
|
75
|
-
active_workflow_names = set(
|
|
76
|
-
_get_active_workflow_names(ctx, workflows_attr, render_workflows)
|
|
77
|
-
)
|
|
78
|
-
active_workflow_prompt = _get_workflow_prompt(
|
|
79
|
-
available_workflows, active_workflow_names, True
|
|
80
|
-
)
|
|
81
|
-
inactive_workflow_prompt = _get_workflow_prompt(
|
|
82
|
-
available_workflows, active_workflow_names, False
|
|
83
|
-
)
|
|
84
|
-
if conversation_history is None:
|
|
85
|
-
conversation_history = ConversationHistory()
|
|
86
|
-
current_directory = os.getcwd()
|
|
87
|
-
iso_date = datetime.now(timezone.utc).astimezone().isoformat()
|
|
88
|
-
return "\n".join(
|
|
89
|
-
[
|
|
90
|
-
persona,
|
|
91
|
-
base_system_prompt,
|
|
92
|
-
make_markdown_section(
|
|
93
|
-
"📝 SPECIAL INSTRUCTION",
|
|
94
|
-
"\n".join(
|
|
95
|
-
[
|
|
96
|
-
special_instruction_prompt,
|
|
97
|
-
active_workflow_prompt,
|
|
98
|
-
]
|
|
99
|
-
),
|
|
100
|
-
),
|
|
101
|
-
make_markdown_section("🛠️ AVAILABLE WORKFLOWS", inactive_workflow_prompt),
|
|
102
|
-
make_markdown_section(
|
|
103
|
-
"📚 CONTEXT",
|
|
104
|
-
"\n".join(
|
|
105
|
-
[
|
|
106
|
-
make_markdown_section(
|
|
107
|
-
"ℹ️ System Information",
|
|
108
|
-
"\n".join(
|
|
109
|
-
[
|
|
110
|
-
f"- OS: {platform.system()} {platform.version()}",
|
|
111
|
-
f"- Python Version: {platform.python_version()}",
|
|
112
|
-
f"- Current Directory: {current_directory}",
|
|
113
|
-
f"- Current Time: {iso_date}",
|
|
114
|
-
]
|
|
115
|
-
),
|
|
116
|
-
),
|
|
117
|
-
make_markdown_section(
|
|
118
|
-
"🧠 Long Term Note Content",
|
|
119
|
-
conversation_history.long_term_note,
|
|
120
|
-
),
|
|
121
|
-
make_markdown_section(
|
|
122
|
-
"📝 Contextual Note Content",
|
|
123
|
-
conversation_history.contextual_note,
|
|
124
|
-
),
|
|
125
|
-
make_markdown_section(
|
|
126
|
-
"📄 Apendixes",
|
|
127
|
-
apendixes,
|
|
128
|
-
),
|
|
129
|
-
]
|
|
130
|
-
),
|
|
131
|
-
),
|
|
132
|
-
]
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
def _get_persona(
|
|
137
|
-
ctx: AnyContext,
|
|
138
|
-
persona_attr: StrAttr | None,
|
|
139
|
-
render_persona: bool,
|
|
140
|
-
) -> str:
|
|
141
|
-
"""Gets the persona, prioritizing task-specific, then default."""
|
|
142
|
-
persona = get_attr(
|
|
143
|
-
ctx,
|
|
144
|
-
persona_attr,
|
|
145
|
-
None,
|
|
146
|
-
auto_render=render_persona,
|
|
147
|
-
)
|
|
148
|
-
if persona is not None:
|
|
149
|
-
return persona
|
|
150
|
-
return llm_config.default_persona or ""
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
def _get_base_system_prompt(
|
|
154
|
-
ctx: AnyContext,
|
|
155
|
-
system_prompt_attr: StrAttr | None,
|
|
156
|
-
render_system_prompt: bool,
|
|
157
|
-
) -> str:
|
|
158
|
-
"""Gets the base system prompt, prioritizing task-specific, then default."""
|
|
159
|
-
system_prompt = get_attr(
|
|
160
|
-
ctx,
|
|
161
|
-
system_prompt_attr,
|
|
162
|
-
None,
|
|
163
|
-
auto_render=render_system_prompt,
|
|
164
|
-
)
|
|
165
|
-
if system_prompt is not None:
|
|
166
|
-
return system_prompt
|
|
167
|
-
return llm_config.default_system_prompt or ""
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
def _get_special_instruction_prompt(
|
|
171
|
-
ctx: AnyContext,
|
|
172
|
-
special_instruction_prompt_attr: StrAttr | None,
|
|
173
|
-
render_spcecial_instruction_prompt: bool,
|
|
174
|
-
) -> str:
|
|
175
|
-
"""Gets the special instruction prompt, prioritizing task-specific, then default."""
|
|
176
|
-
special_instruction = get_attr(
|
|
177
|
-
ctx,
|
|
178
|
-
special_instruction_prompt_attr,
|
|
179
|
-
None,
|
|
180
|
-
auto_render=render_spcecial_instruction_prompt,
|
|
181
|
-
)
|
|
182
|
-
if special_instruction is not None:
|
|
183
|
-
return special_instruction
|
|
184
|
-
return llm_config.default_special_instruction_prompt
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
def _get_active_workflow_names(
|
|
188
|
-
ctx: AnyContext,
|
|
189
|
-
workflows_attr: StrListAttr | None,
|
|
190
|
-
render_workflows: bool,
|
|
191
|
-
) -> list[str]:
|
|
192
|
-
"""Gets the workflows, prioritizing task-specific, then default."""
|
|
193
|
-
raw_workflows = get_str_list_attr(
|
|
194
|
-
ctx,
|
|
195
|
-
[] if workflows_attr is None else workflows_attr,
|
|
196
|
-
auto_render=render_workflows,
|
|
197
|
-
)
|
|
198
|
-
if raw_workflows is not None and len(raw_workflows) > 0:
|
|
199
|
-
return [w.strip().lower() for w in raw_workflows if w.strip() != ""]
|
|
200
|
-
return []
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
def _get_workflow_prompt(
|
|
204
|
-
available_workflows: dict[str, LLMWorkflow],
|
|
205
|
-
active_workflow_names: list[str] | set[str],
|
|
206
|
-
select_active_workflow: bool,
|
|
207
|
-
) -> str:
|
|
208
|
-
selected_workflows = {
|
|
209
|
-
workflow_name: available_workflows[workflow_name]
|
|
210
|
-
for workflow_name in available_workflows
|
|
211
|
-
if (workflow_name in active_workflow_names) == select_active_workflow
|
|
212
|
-
}
|
|
213
|
-
return "\n".join(
|
|
214
|
-
[
|
|
215
|
-
make_markdown_section(
|
|
216
|
-
workflow_name.capitalize(),
|
|
217
|
-
(
|
|
218
|
-
(
|
|
219
|
-
"> Workflow status: Automatically Loaded/Activated.\n"
|
|
220
|
-
f"> Workflow location: `{workflow.path}`\n"
|
|
221
|
-
"{workflow.content}"
|
|
222
|
-
)
|
|
223
|
-
if select_active_workflow
|
|
224
|
-
else f"Workflow name: {workflow_name}\n{workflow.description}"
|
|
225
|
-
),
|
|
226
|
-
)
|
|
227
|
-
for workflow_name, workflow in selected_workflows.items()
|
|
228
|
-
]
|
|
229
|
-
)
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
def _get_user_message_prompt(user_message: str) -> tuple[str, str]:
|
|
233
|
-
processed_user_message = user_message
|
|
234
|
-
# Match “@” + any non-space/comma sequence that contains at least one “/”
|
|
235
|
-
pattern = r"(?<!\w)@(?=[^,\s]*\/)([^,\?\!\s]+)"
|
|
236
|
-
potential_resource_path = re.findall(pattern, user_message)
|
|
237
|
-
apendix_list = []
|
|
238
|
-
for i, ref in enumerate(potential_resource_path):
|
|
239
|
-
resource_path = os.path.abspath(os.path.expanduser(ref))
|
|
240
|
-
content = ""
|
|
241
|
-
ref_type = ""
|
|
242
|
-
if os.path.isfile(resource_path):
|
|
243
|
-
content = read_file_with_line_numbers(resource_path)
|
|
244
|
-
ref_type = "file"
|
|
245
|
-
elif os.path.isdir(resource_path):
|
|
246
|
-
content = read_dir(resource_path)
|
|
247
|
-
ref_type = "directory"
|
|
248
|
-
if content != "":
|
|
249
|
-
# Replace the @-reference in the user message with the placeholder
|
|
250
|
-
placeholder = f"[Reference {i+1}: `{os.path.basename(ref)}`]"
|
|
251
|
-
processed_user_message = processed_user_message.replace(
|
|
252
|
-
f"@{ref}", placeholder, 1
|
|
253
|
-
)
|
|
254
|
-
apendix_list.append(
|
|
255
|
-
make_markdown_section(
|
|
256
|
-
f"Content of {placeholder} ({ref_type} path: `{resource_path}`)",
|
|
257
|
-
"\n".join(content) if isinstance(content, list) else content,
|
|
258
|
-
as_code=True,
|
|
259
|
-
)
|
|
260
|
-
)
|
|
261
|
-
apendixes = "\n".join(apendix_list)
|
|
262
|
-
current_directory = os.getcwd()
|
|
263
|
-
iso_date = datetime.now(timezone.utc).astimezone().isoformat()
|
|
264
|
-
modified_user_message = make_markdown_section(
|
|
265
|
-
"User Request",
|
|
266
|
-
"\n".join(
|
|
267
|
-
[
|
|
268
|
-
f"- Current Directory: {current_directory}",
|
|
269
|
-
f"- Current Time: {iso_date}",
|
|
270
|
-
"---",
|
|
271
|
-
processed_user_message,
|
|
272
|
-
]
|
|
273
|
-
),
|
|
274
|
-
)
|
|
275
|
-
return modified_user_message, apendixes
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
def get_user_message(
|
|
279
|
-
ctx: AnyContext,
|
|
280
|
-
message_attr: StrAttr | None,
|
|
281
|
-
render_user_message: bool,
|
|
282
|
-
) -> str:
|
|
283
|
-
"""Gets the user message, rendering and providing a default."""
|
|
284
|
-
return get_str_attr(
|
|
285
|
-
ctx, message_attr, "How are you?", auto_render=render_user_message
|
|
286
|
-
)
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
def get_summarization_system_prompt(
|
|
290
|
-
ctx: AnyContext,
|
|
291
|
-
summarization_prompt_attr: StrAttr | None,
|
|
292
|
-
render_summarization_prompt: bool,
|
|
293
|
-
) -> str:
|
|
294
|
-
"""Gets the summarization prompt, rendering if configured and handling defaults."""
|
|
295
|
-
summarization_prompt = get_attr(
|
|
296
|
-
ctx,
|
|
297
|
-
summarization_prompt_attr,
|
|
298
|
-
None,
|
|
299
|
-
auto_render=render_summarization_prompt,
|
|
300
|
-
)
|
|
301
|
-
if summarization_prompt is not None:
|
|
302
|
-
return summarization_prompt
|
|
303
|
-
return llm_config.default_summarization_prompt
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
def get_attachments(
|
|
307
|
-
ctx: AnyContext,
|
|
308
|
-
attachment: "UserContent | list[UserContent] | Callable[[AnyContext], UserContent | list[UserContent]] | None" = None, # noqa
|
|
309
|
-
) -> "list[UserContent]":
|
|
310
|
-
if attachment is None:
|
|
311
|
-
return []
|
|
312
|
-
if callable(attachment):
|
|
313
|
-
result = attachment(ctx)
|
|
314
|
-
if result is None:
|
|
315
|
-
return []
|
|
316
|
-
if isinstance(result, list):
|
|
317
|
-
return result
|
|
318
|
-
return [result]
|
|
319
|
-
if isinstance(attachment, list):
|
|
320
|
-
return attachment
|
|
321
|
-
return [attachment]
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
from zrb.context.any_context import AnyContext
|
|
2
|
-
from zrb.task.llm.conversation_history_model import ConversationHistory
|
|
3
|
-
from zrb.task.llm.typing import ListOfDict
|
|
4
|
-
from zrb.xcom.xcom import Xcom
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def inject_subagent_conversation_history_into_ctx(
|
|
8
|
-
ctx: AnyContext, conversation_history: ConversationHistory
|
|
9
|
-
):
|
|
10
|
-
subagent_messages_xcom = _get_global_subagent_history_xcom(ctx)
|
|
11
|
-
existing_subagent_history = subagent_messages_xcom.get({})
|
|
12
|
-
subagent_messages_xcom.set(
|
|
13
|
-
{**existing_subagent_history, **conversation_history.subagent_history}
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def extract_subagent_conversation_history_from_ctx(
|
|
18
|
-
ctx: AnyContext,
|
|
19
|
-
) -> dict[str, ListOfDict]:
|
|
20
|
-
subagent_messsages_xcom = _get_global_subagent_history_xcom(ctx)
|
|
21
|
-
return subagent_messsages_xcom.get({})
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def get_ctx_subagent_history(ctx: AnyContext, subagent_name: str) -> ListOfDict:
|
|
25
|
-
subagent_history = extract_subagent_conversation_history_from_ctx(ctx)
|
|
26
|
-
return subagent_history.get(subagent_name, [])
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def set_ctx_subagent_history(ctx: AnyContext, subagent_name: str, messages: ListOfDict):
|
|
30
|
-
subagent_history = extract_subagent_conversation_history_from_ctx(ctx)
|
|
31
|
-
subagent_history[subagent_name] = messages
|
|
32
|
-
subagent_messages_xcom = _get_global_subagent_history_xcom(ctx)
|
|
33
|
-
subagent_messages_xcom.set(subagent_history)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def _get_global_subagent_history_xcom(ctx: AnyContext) -> Xcom:
|
|
37
|
-
if "_global_subagents" not in ctx.xcom:
|
|
38
|
-
ctx.xcom["_global_subagents"] = Xcom([{}])
|
|
39
|
-
if not isinstance(ctx.xcom["_global_subagents"], Xcom):
|
|
40
|
-
raise ValueError("ctx.xcom._global_subagents must be an Xcom")
|
|
41
|
-
return ctx.xcom["_global_subagents"]
|