tinybird 0.0.1.dev257__py3-none-any.whl → 0.0.1.dev259__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 tinybird might be problematic. Click here for more details.
- tinybird/prompts.py +1 -0
- tinybird/tb/__cli__.py +2 -2
- tinybird/tb/modules/agent/agent.py +31 -3
- tinybird/tb/modules/agent/banner.py +1 -0
- tinybird/tb/modules/agent/prompts.py +35 -3
- tinybird/tb/modules/agent/tools/analyze.py +3 -1
- tinybird/tb/modules/agent/tools/append.py +5 -9
- tinybird/tb/modules/agent/tools/build.py +2 -1
- tinybird/tb/modules/agent/tools/create_datafile.py +3 -5
- tinybird/tb/modules/agent/tools/deploy.py +2 -5
- tinybird/tb/modules/agent/tools/deploy_check.py +2 -5
- tinybird/tb/modules/agent/tools/mock.py +3 -5
- tinybird/tb/modules/agent/tools/plan.py +12 -20
- tinybird/tb/modules/agent/tools/test.py +120 -0
- tinybird/tb/modules/agent/utils.py +10 -3
- tinybird/tb/modules/project.py +24 -1
- tinybird/tb/modules/test.py +12 -273
- tinybird/tb/modules/test_common.py +295 -0
- {tinybird-0.0.1.dev257.dist-info → tinybird-0.0.1.dev259.dist-info}/METADATA +1 -1
- {tinybird-0.0.1.dev257.dist-info → tinybird-0.0.1.dev259.dist-info}/RECORD +23 -21
- {tinybird-0.0.1.dev257.dist-info → tinybird-0.0.1.dev259.dist-info}/WHEEL +0 -0
- {tinybird-0.0.1.dev257.dist-info → tinybird-0.0.1.dev259.dist-info}/entry_points.txt +0 -0
- {tinybird-0.0.1.dev257.dist-info → tinybird-0.0.1.dev259.dist-info}/top_level.txt +0 -0
tinybird/prompts.py
CHANGED
|
@@ -376,6 +376,7 @@ You are a Tinybird expert. You will be given a pipe containing different nodes w
|
|
|
376
376
|
- If there are no parameters, you can omit parameters and generate a single test.
|
|
377
377
|
- The format of the parameters is the following: ?param1=value1¶m2=value2¶m3=value3
|
|
378
378
|
- If some parameters are provided by the user and you need to use them, preserve in the same format as they were provided, like case sensitive.
|
|
379
|
+
- If user provides the current test content, use it to generate the new test content.
|
|
379
380
|
</instructions>
|
|
380
381
|
|
|
381
382
|
This is an example of a test with parameters:
|
tinybird/tb/__cli__.py
CHANGED
|
@@ -4,5 +4,5 @@ __description__ = 'Tinybird Command Line Tool'
|
|
|
4
4
|
__url__ = 'https://www.tinybird.co/docs/forward/commands'
|
|
5
5
|
__author__ = 'Tinybird'
|
|
6
6
|
__author_email__ = 'support@tinybird.co'
|
|
7
|
-
__version__ = '0.0.1.
|
|
8
|
-
__revision__ = '
|
|
7
|
+
__version__ = '0.0.1.dev259'
|
|
8
|
+
__revision__ = '5b5805f'
|
|
@@ -10,7 +10,7 @@ import click
|
|
|
10
10
|
import humanfriendly
|
|
11
11
|
from pydantic_ai import Agent, RunContext, Tool
|
|
12
12
|
from pydantic_ai.agent import AgentRunResult
|
|
13
|
-
from pydantic_ai.messages import ModelMessage
|
|
13
|
+
from pydantic_ai.messages import ModelMessage, ModelRequest, UserPromptPart
|
|
14
14
|
|
|
15
15
|
from tinybird.tb.client import TinyB
|
|
16
16
|
from tinybird.tb.modules.agent.animations import ThinkingAnimation
|
|
@@ -32,7 +32,9 @@ from tinybird.tb.modules.agent.tools.mock import mock
|
|
|
32
32
|
from tinybird.tb.modules.agent.tools.plan import plan
|
|
33
33
|
from tinybird.tb.modules.agent.tools.preview_datafile import preview_datafile
|
|
34
34
|
from tinybird.tb.modules.agent.tools.request_endpoint import request_endpoint
|
|
35
|
-
from tinybird.tb.modules.agent.
|
|
35
|
+
from tinybird.tb.modules.agent.tools.test import create_tests as create_tests_tool
|
|
36
|
+
from tinybird.tb.modules.agent.tools.test import run_tests as run_tests_tool
|
|
37
|
+
from tinybird.tb.modules.agent.utils import AgentRunCancelled, TinybirdAgentContext, show_input
|
|
36
38
|
from tinybird.tb.modules.build_common import process as build_process
|
|
37
39
|
from tinybird.tb.modules.common import _analyze, _get_tb_client, echo_safe_humanfriendly_tables_format_pretty_table
|
|
38
40
|
from tinybird.tb.modules.config import CLIConfig
|
|
@@ -43,6 +45,7 @@ from tinybird.tb.modules.local_common import get_tinybird_local_client
|
|
|
43
45
|
from tinybird.tb.modules.login_common import login
|
|
44
46
|
from tinybird.tb.modules.mock_common import append_mock_data, create_mock_data
|
|
45
47
|
from tinybird.tb.modules.project import Project
|
|
48
|
+
from tinybird.tb.modules.test_common import run_tests as run_tests_common
|
|
46
49
|
|
|
47
50
|
|
|
48
51
|
class TinybirdAgent:
|
|
@@ -93,6 +96,8 @@ class TinybirdAgent:
|
|
|
93
96
|
Tool(execute_query, docstring_format="google", require_parameter_descriptions=True, takes_ctx=True),
|
|
94
97
|
Tool(request_endpoint, docstring_format="google", require_parameter_descriptions=True, takes_ctx=True),
|
|
95
98
|
Tool(diff_resource, docstring_format="google", require_parameter_descriptions=True, takes_ctx=True),
|
|
99
|
+
Tool(create_tests_tool, docstring_format="google", require_parameter_descriptions=True, takes_ctx=True),
|
|
100
|
+
Tool(run_tests_tool, docstring_format="google", require_parameter_descriptions=True, takes_ctx=True),
|
|
96
101
|
],
|
|
97
102
|
# history_processors=[self._keep_recent_messages],
|
|
98
103
|
)
|
|
@@ -119,6 +124,9 @@ class TinybirdAgent:
|
|
|
119
124
|
|
|
120
125
|
self.thinking_animation = ThinkingAnimation()
|
|
121
126
|
|
|
127
|
+
def add_message(self, message: ModelMessage) -> None:
|
|
128
|
+
self.messages.append(message)
|
|
129
|
+
|
|
122
130
|
def _keep_recent_messages(self, messages: list[ModelMessage]) -> list[ModelMessage]:
|
|
123
131
|
"""Keep only the last 5 messages to manage token usage."""
|
|
124
132
|
return messages[-5:] if len(messages) > 5 else messages
|
|
@@ -149,6 +157,7 @@ class TinybirdAgent:
|
|
|
149
157
|
get_connection_datafile_cloud=partial(get_connection_datafile_cloud, config=config),
|
|
150
158
|
get_connection_datafile_local=partial(get_connection_datafile_local, config=config),
|
|
151
159
|
get_project_files=project.get_project_files,
|
|
160
|
+
run_tests=partial(run_tests, project=project, config=config),
|
|
152
161
|
folder=folder,
|
|
153
162
|
thinking_animation=self.thinking_animation,
|
|
154
163
|
workspace_name=self.project.workspace_name,
|
|
@@ -320,7 +329,18 @@ def run_agent(
|
|
|
320
329
|
continue
|
|
321
330
|
else:
|
|
322
331
|
asyncio.run(agent.run_iter(user_input, config))
|
|
323
|
-
|
|
332
|
+
except AgentRunCancelled:
|
|
333
|
+
click.echo(FeedbackManager.info(message="User cancelled the operation"))
|
|
334
|
+
agent.add_message(
|
|
335
|
+
ModelRequest(
|
|
336
|
+
parts=[
|
|
337
|
+
UserPromptPart(
|
|
338
|
+
content="User cancelled the operation",
|
|
339
|
+
)
|
|
340
|
+
]
|
|
341
|
+
)
|
|
342
|
+
)
|
|
343
|
+
continue
|
|
324
344
|
except KeyboardInterrupt:
|
|
325
345
|
click.echo(FeedbackManager.info(message="Goodbye!"))
|
|
326
346
|
break
|
|
@@ -481,3 +501,11 @@ def get_connection_datafile_local(config: dict[str, Any], connection_name: str)
|
|
|
481
501
|
return local_client.connection_file(connection_name)
|
|
482
502
|
except Exception:
|
|
483
503
|
return "Connection not found"
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
def run_tests(config: dict[str, Any], project: Project, pipe_name: Optional[str] = None) -> None:
|
|
507
|
+
local_client = get_tinybird_local_client(config, test=True, silent=True)
|
|
508
|
+
try:
|
|
509
|
+
run_tests_common(name=(pipe_name,) if pipe_name else (), project=project, client=local_client)
|
|
510
|
+
except SystemExit as e:
|
|
511
|
+
raise Exception(e.args[0])
|
|
@@ -16,6 +16,7 @@ from tinybird.prompts import (
|
|
|
16
16
|
pipe_instructions,
|
|
17
17
|
s3_connection_example,
|
|
18
18
|
sink_pipe_instructions,
|
|
19
|
+
test_instructions,
|
|
19
20
|
)
|
|
20
21
|
from tinybird.tb.modules.project import Project
|
|
21
22
|
|
|
@@ -114,6 +115,7 @@ datafile_instructions = """
|
|
|
114
115
|
def resources_prompt(project: Project) -> str:
|
|
115
116
|
files = project.get_project_files()
|
|
116
117
|
fixture_files = project.get_fixture_files()
|
|
118
|
+
test_files = project.get_test_files()
|
|
117
119
|
|
|
118
120
|
resources_content = "# Existing resources in the project:\n"
|
|
119
121
|
if files:
|
|
@@ -146,7 +148,22 @@ def resources_prompt(project: Project) -> str:
|
|
|
146
148
|
else:
|
|
147
149
|
fixture_content += "No fixture files found"
|
|
148
150
|
|
|
149
|
-
|
|
151
|
+
test_content = "# Test files in the project:\n"
|
|
152
|
+
if test_files:
|
|
153
|
+
tests: list[dict[str, Any]] = []
|
|
154
|
+
for filename in test_files:
|
|
155
|
+
file_path = Path(filename)
|
|
156
|
+
test = {
|
|
157
|
+
"path": str(file_path.relative_to(project.folder)),
|
|
158
|
+
"name": file_path.stem,
|
|
159
|
+
"content": file_path.read_text(),
|
|
160
|
+
}
|
|
161
|
+
tests.append(test)
|
|
162
|
+
test_content = format_as_xml(tests, root_tag="tests", item_tag="test")
|
|
163
|
+
else:
|
|
164
|
+
test_content += "No test files found"
|
|
165
|
+
|
|
166
|
+
return resources_content + "\n" + fixture_content + "\n" + test_content
|
|
150
167
|
|
|
151
168
|
|
|
152
169
|
def get_resource_type(path: Path) -> str:
|
|
@@ -550,6 +567,7 @@ You have access to the following tools:
|
|
|
550
567
|
13. `execute_query` - Execute a query against Tinybird Cloud or Local.
|
|
551
568
|
13. `request_endpoint` - Request an endpoint against Tinybird Cloud or Local.
|
|
552
569
|
14. `diff_resource` - Diff the content of a resource in Tinybird Cloud vs Tinybird Local vs Project local file.
|
|
570
|
+
15. `create_tests` - Create tests for an endpoint.
|
|
553
571
|
|
|
554
572
|
# When creating or updating datafiles:
|
|
555
573
|
1. Use `plan` tool to plan the creation or update of resources.
|
|
@@ -612,22 +630,26 @@ Kafka: {kafka_connection_example}
|
|
|
612
630
|
S3: {s3_connection_example}
|
|
613
631
|
GCS: {gcs_connection_example}
|
|
614
632
|
|
|
615
|
-
# When executing a query or
|
|
633
|
+
# When executing a query or calling an endpoint:
|
|
616
634
|
- You need to be sure that the selected resource is updated to the last version in the environment you are working on.
|
|
617
635
|
- Use `diff_resource` tool to compare the content of the resource to compare the differences between environments.
|
|
618
636
|
- Project local file is the source of truth.
|
|
619
637
|
- If the resource is not present or updated to the last version in Tinybird Local, it means you need to build the project.
|
|
620
638
|
- If the resource is not present or updated to the last version in Tinybird Cloud, it means you need to deploy the project.
|
|
639
|
+
- If exploring an endpoint, the response is empty. You can query the tables to understand what data is available.
|
|
640
|
+
|
|
621
641
|
|
|
622
642
|
# How to use apppend tools:
|
|
623
643
|
- Use append as part of the creation of a new landing datasource if the user provided a file or an external url
|
|
624
644
|
- Use append if user explicitly asks for it
|
|
625
|
-
- Do not append data if user requests to test an endpoint
|
|
645
|
+
- Do not append data if user requests to test an endpoint or call an endpoint.
|
|
646
|
+
- Do not append data as consequence of an empty response from the endpoint or a query.
|
|
626
647
|
|
|
627
648
|
# How to use `mock` tool:
|
|
628
649
|
- Use `mock` tool as part of the creation of a new landing datasource if the user did not provided a file or an external url
|
|
629
650
|
- Use `mock` tool if user explicitly asks for it
|
|
630
651
|
- Do not use `mock` tool if user requests to test an endpoint.
|
|
652
|
+
- Do not use `mock` tool as consequence of an empty response from the endpoint or a query.
|
|
631
653
|
|
|
632
654
|
# When sharing endpoints paths or urls:
|
|
633
655
|
- Use `get_openapi_definition` tool to get the url of the endpoint and parameters available.
|
|
@@ -639,6 +661,16 @@ GCS: {gcs_connection_example}
|
|
|
639
661
|
- `DateTime` parameters accept values in format `YYYY-MM-DD HH:MM:SS`
|
|
640
662
|
- `Date` parameters accept values in format `YYYYMMDD`
|
|
641
663
|
|
|
664
|
+
# Working with tests:
|
|
665
|
+
{test_instructions}
|
|
666
|
+
<dev_notes>
|
|
667
|
+
- Create tests only when the user explicitly asks for it with prompts like "Create tests for this endpoint" or "Create tests for this pipe".
|
|
668
|
+
- If the user asks for "testing an endpoint" or "call an endpoint", just request to the endpoint.
|
|
669
|
+
- The data that the tests are using is the data provided in the fixtures folder.
|
|
670
|
+
- Querying data or requesting endpoints won't return the data that the tests are using.
|
|
671
|
+
- MANDATORY: Before creating the test, analyze the fixture files that the tables of the endpoint are using so you can create relevant tests.
|
|
672
|
+
</dev_notes>
|
|
673
|
+
|
|
642
674
|
# Info
|
|
643
675
|
Today is {datetime.now().strftime("%Y-%m-%d")}
|
|
644
676
|
"""
|
|
@@ -5,7 +5,7 @@ from urllib.parse import urlparse
|
|
|
5
5
|
import click
|
|
6
6
|
from pydantic_ai import RunContext
|
|
7
7
|
|
|
8
|
-
from tinybird.tb.modules.agent.utils import TinybirdAgentContext
|
|
8
|
+
from tinybird.tb.modules.agent.utils import AgentRunCancelled, TinybirdAgentContext
|
|
9
9
|
from tinybird.tb.modules.feedback_manager import FeedbackManager
|
|
10
10
|
|
|
11
11
|
|
|
@@ -37,6 +37,8 @@ def analyze_file(ctx: RunContext[TinybirdAgentContext], fixture_pathname: str):
|
|
|
37
37
|
columns = response["analysis"]["columns"]
|
|
38
38
|
|
|
39
39
|
return f"#Result of analysis of {fixture_pathname}:\n##Columns:\n{json.dumps(columns)}\n##Data sample:\n{json.dumps(data)}"
|
|
40
|
+
except AgentRunCancelled as e:
|
|
41
|
+
raise e
|
|
40
42
|
except Exception as e:
|
|
41
43
|
ctx.deps.thinking_animation.stop()
|
|
42
44
|
click.echo(FeedbackManager.error(message=f"Error analyzing {fixture_pathname}: {e}"))
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import click
|
|
2
2
|
from pydantic_ai import RunContext
|
|
3
3
|
|
|
4
|
-
from tinybird.tb.modules.agent.utils import TinybirdAgentContext, show_confirmation, show_input
|
|
4
|
+
from tinybird.tb.modules.agent.utils import AgentRunCancelled, TinybirdAgentContext, show_confirmation, show_input
|
|
5
5
|
from tinybird.tb.modules.feedback_manager import FeedbackManager
|
|
6
6
|
|
|
7
7
|
|
|
@@ -31,10 +31,6 @@ def append_file(
|
|
|
31
31
|
ctx.deps.thinking_animation.start()
|
|
32
32
|
return f"User did not confirm appending {fixture_pathname} fixture in Tinybird {cloud_or_local} and gave the following feedback: {feedback}"
|
|
33
33
|
|
|
34
|
-
if confirmation == "cancel":
|
|
35
|
-
ctx.deps.thinking_animation.start()
|
|
36
|
-
return f"User rejected appending {fixture_pathname} fixture in Tinybird {cloud_or_local}. Skip this step"
|
|
37
|
-
|
|
38
34
|
ctx.deps.thinking_animation.stop()
|
|
39
35
|
click.echo(FeedbackManager.highlight(message=f"» Appending {fixture_pathname} to {datasource_name}..."))
|
|
40
36
|
if cloud:
|
|
@@ -44,6 +40,8 @@ def append_file(
|
|
|
44
40
|
click.echo(FeedbackManager.success(message=f"✓ Data appended to {datasource_name}"))
|
|
45
41
|
ctx.deps.thinking_animation.start()
|
|
46
42
|
return f"Data appended to {datasource_name} in Tinybird {cloud_or_local}"
|
|
43
|
+
except AgentRunCancelled as e:
|
|
44
|
+
raise e
|
|
47
45
|
except Exception as e:
|
|
48
46
|
error_message = str(e)
|
|
49
47
|
ctx.deps.thinking_animation.stop()
|
|
@@ -79,10 +77,6 @@ def append_url(
|
|
|
79
77
|
ctx.deps.thinking_animation.start()
|
|
80
78
|
return f"User did not confirm appending URL {fixture_url} in Tinybird {cloud_or_local} and gave the following feedback: {feedback}"
|
|
81
79
|
|
|
82
|
-
if confirmation == "cancel":
|
|
83
|
-
ctx.deps.thinking_animation.start()
|
|
84
|
-
return f"User rejected appending URL {fixture_url} in Tinybird {cloud_or_local}. Skip this step"
|
|
85
|
-
|
|
86
80
|
ctx.deps.thinking_animation.stop()
|
|
87
81
|
click.echo(FeedbackManager.highlight(message=f"» Appending {fixture_url} to {datasource_name}..."))
|
|
88
82
|
if cloud:
|
|
@@ -92,6 +86,8 @@ def append_url(
|
|
|
92
86
|
click.echo(FeedbackManager.success(message=f"✓ Data appended to {datasource_name}"))
|
|
93
87
|
ctx.deps.thinking_animation.start()
|
|
94
88
|
return f"Data appended to {datasource_name} in Tinybird {cloud_or_local}"
|
|
89
|
+
except AgentRunCancelled as e:
|
|
90
|
+
raise e
|
|
95
91
|
except Exception as e:
|
|
96
92
|
error_message = str(e)
|
|
97
93
|
ctx.deps.thinking_animation.stop()
|
|
@@ -2,6 +2,7 @@ import click
|
|
|
2
2
|
from pydantic_ai import RunContext
|
|
3
3
|
|
|
4
4
|
from tinybird.tb.modules.agent.utils import TinybirdAgentContext
|
|
5
|
+
from tinybird.tb.modules.exceptions import CLIBuildException
|
|
5
6
|
from tinybird.tb.modules.feedback_manager import FeedbackManager
|
|
6
7
|
|
|
7
8
|
|
|
@@ -13,7 +14,7 @@ def build(ctx: RunContext[TinybirdAgentContext]) -> str:
|
|
|
13
14
|
ctx.deps.build_project(test=False, silent=False)
|
|
14
15
|
ctx.deps.thinking_animation.start()
|
|
15
16
|
return "Project built successfully"
|
|
16
|
-
except
|
|
17
|
+
except CLIBuildException as e:
|
|
17
18
|
ctx.deps.thinking_animation.stop()
|
|
18
19
|
click.echo(FeedbackManager.error(message=e))
|
|
19
20
|
ctx.deps.thinking_animation.start()
|
|
@@ -5,6 +5,7 @@ import click
|
|
|
5
5
|
from pydantic_ai import RunContext
|
|
6
6
|
|
|
7
7
|
from tinybird.tb.modules.agent.utils import (
|
|
8
|
+
AgentRunCancelled,
|
|
8
9
|
Datafile,
|
|
9
10
|
TinybirdAgentContext,
|
|
10
11
|
create_terminal_box,
|
|
@@ -46,10 +47,6 @@ def create_datafile(ctx: RunContext[TinybirdAgentContext], resource: Datafile) -
|
|
|
46
47
|
ctx.deps.thinking_animation.start()
|
|
47
48
|
return f"User did not confirm the proposed changes and gave the following feedback: {feedback}"
|
|
48
49
|
|
|
49
|
-
if confirmation == "cancel":
|
|
50
|
-
ctx.deps.thinking_animation.start()
|
|
51
|
-
return f"User cancelled {action} of {resource.pathname}. Stop resource creation."
|
|
52
|
-
|
|
53
50
|
click.echo(FeedbackManager.highlight(message=f"» Building {resource.pathname}..."))
|
|
54
51
|
folder_path = path.parent
|
|
55
52
|
folder_path.mkdir(parents=True, exist_ok=True)
|
|
@@ -60,7 +57,8 @@ def create_datafile(ctx: RunContext[TinybirdAgentContext], resource: Datafile) -
|
|
|
60
57
|
click.echo(FeedbackManager.success(message=f"✓ {resource.pathname} {action_text}"))
|
|
61
58
|
ctx.deps.thinking_animation.start()
|
|
62
59
|
return f"{action_text} {resource.pathname}"
|
|
63
|
-
|
|
60
|
+
except AgentRunCancelled as e:
|
|
61
|
+
raise e
|
|
64
62
|
except CLIBuildException as e:
|
|
65
63
|
ctx.deps.thinking_animation.stop()
|
|
66
64
|
click.echo(FeedbackManager.error(message=e))
|
|
@@ -2,6 +2,7 @@ import click
|
|
|
2
2
|
from pydantic_ai import RunContext
|
|
3
3
|
|
|
4
4
|
from tinybird.tb.modules.agent.utils import TinybirdAgentContext, show_confirmation, show_input
|
|
5
|
+
from tinybird.tb.modules.exceptions import CLIDeploymentException
|
|
5
6
|
from tinybird.tb.modules.feedback_manager import FeedbackManager
|
|
6
7
|
|
|
7
8
|
|
|
@@ -19,15 +20,11 @@ def deploy(ctx: RunContext[TinybirdAgentContext]) -> str:
|
|
|
19
20
|
ctx.deps.thinking_animation.start()
|
|
20
21
|
return f"User did not confirm deployment and gave the following feedback: {feedback}"
|
|
21
22
|
|
|
22
|
-
if confirmation == "cancel":
|
|
23
|
-
ctx.deps.thinking_animation.start()
|
|
24
|
-
return "User cancelled deployment. Stop deployment."
|
|
25
|
-
|
|
26
23
|
click.echo(FeedbackManager.highlight(message="» Deploying project..."))
|
|
27
24
|
ctx.deps.deploy_project()
|
|
28
25
|
click.echo(FeedbackManager.success(message="✓ Project deployed successfully"))
|
|
29
26
|
ctx.deps.thinking_animation.start()
|
|
30
27
|
return "Project deployed successfully"
|
|
31
|
-
except
|
|
28
|
+
except CLIDeploymentException as e:
|
|
32
29
|
ctx.deps.thinking_animation.start()
|
|
33
30
|
return f"Error depoying project: {e}"
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from pydantic_ai import RunContext
|
|
2
2
|
|
|
3
3
|
from tinybird.tb.modules.agent.utils import TinybirdAgentContext, show_confirmation, show_input
|
|
4
|
+
from tinybird.tb.modules.exceptions import CLIDeploymentException
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
def deploy_check(ctx: RunContext[TinybirdAgentContext]) -> str:
|
|
@@ -17,13 +18,9 @@ def deploy_check(ctx: RunContext[TinybirdAgentContext]) -> str:
|
|
|
17
18
|
ctx.deps.thinking_animation.start()
|
|
18
19
|
return f"User did not confirm deployment check and gave the following feedback: {feedback}"
|
|
19
20
|
|
|
20
|
-
if confirmation == "cancel":
|
|
21
|
-
ctx.deps.thinking_animation.start()
|
|
22
|
-
return "User cancelled deployment check. Stop deployment check."
|
|
23
|
-
|
|
24
21
|
ctx.deps.deploy_check_project()
|
|
25
22
|
ctx.deps.thinking_animation.start()
|
|
26
23
|
return "Project can be deployed"
|
|
27
|
-
except
|
|
24
|
+
except CLIDeploymentException as e:
|
|
28
25
|
ctx.deps.thinking_animation.start()
|
|
29
26
|
return f"Project cannot be deployed: {e}"
|
|
@@ -3,7 +3,7 @@ from typing import Optional
|
|
|
3
3
|
import click
|
|
4
4
|
from pydantic_ai import RunContext
|
|
5
5
|
|
|
6
|
-
from tinybird.tb.modules.agent.utils import TinybirdAgentContext, show_confirmation, show_input
|
|
6
|
+
from tinybird.tb.modules.agent.utils import AgentRunCancelled, TinybirdAgentContext, show_confirmation, show_input
|
|
7
7
|
from tinybird.tb.modules.datafile.fixture import persist_fixture
|
|
8
8
|
from tinybird.tb.modules.feedback_manager import FeedbackManager
|
|
9
9
|
|
|
@@ -41,10 +41,6 @@ def mock(
|
|
|
41
41
|
ctx.deps.thinking_animation.start()
|
|
42
42
|
return f"User did not confirm mock data for datasource '{datasource_name}' in Tinybird {cloud_or_local} and gave the following feedback: {feedback}"
|
|
43
43
|
|
|
44
|
-
if confirmation == "cancel":
|
|
45
|
-
ctx.deps.thinking_animation.start()
|
|
46
|
-
return f"User cancelled mock data for datasource '{datasource_name}' in Tinybird {cloud_or_local}. Stop mock data generation."
|
|
47
|
-
|
|
48
44
|
click.echo(FeedbackManager.highlight(message=f"» Generating mock data for {datasource_name}..."))
|
|
49
45
|
data = ctx.deps.mock_data(datasource_name=datasource_name, data_format=data_format, rows=rows, context=context)
|
|
50
46
|
fixture_path = persist_fixture(datasource_name, data, ctx.deps.folder, format=data_format)
|
|
@@ -55,6 +51,8 @@ def mock(
|
|
|
55
51
|
click.echo(FeedbackManager.success(message=f"✓ Data generated for {datasource_name}"))
|
|
56
52
|
ctx.deps.thinking_animation.start()
|
|
57
53
|
return f"Mock data generated successfully for datasource '{datasource_name}' in Tinybird {cloud_or_local}"
|
|
54
|
+
except AgentRunCancelled as e:
|
|
55
|
+
raise e
|
|
58
56
|
except Exception as e:
|
|
59
57
|
ctx.deps.thinking_animation.stop()
|
|
60
58
|
error_message = str(e)
|
|
@@ -13,25 +13,17 @@ def plan(ctx: RunContext[TinybirdAgentContext], plan: str) -> str:
|
|
|
13
13
|
Returns:
|
|
14
14
|
str: If the plan was implemented or not.
|
|
15
15
|
"""
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
feedback = show_input(ctx.deps.workspace_name)
|
|
26
|
-
ctx.deps.thinking_animation.start()
|
|
27
|
-
return f"User did not confirm the proposed plan and gave the following feedback: {feedback}"
|
|
28
|
-
|
|
16
|
+
ctx.deps.thinking_animation.stop()
|
|
17
|
+
plan = plan.strip()
|
|
18
|
+
click.echo(plan)
|
|
19
|
+
confirmation = show_confirmation(
|
|
20
|
+
title="Do you want to continue with the plan?", skip_confirmation=ctx.deps.dangerously_skip_permissions
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
if confirmation == "review":
|
|
24
|
+
feedback = show_input(ctx.deps.workspace_name)
|
|
29
25
|
ctx.deps.thinking_animation.start()
|
|
26
|
+
return f"User did not confirm the proposed plan and gave the following feedback: {feedback}"
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return "User confirmed the plan. Implementing..."
|
|
35
|
-
|
|
36
|
-
except Exception as e:
|
|
37
|
-
return f"Error implementing the plan: {e}"
|
|
28
|
+
ctx.deps.thinking_animation.start()
|
|
29
|
+
return "User confirmed the plan. Implementing..."
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import click
|
|
5
|
+
from pydantic_ai import RunContext
|
|
6
|
+
|
|
7
|
+
from tinybird.tb.modules.agent.utils import (
|
|
8
|
+
AgentRunCancelled,
|
|
9
|
+
TinybirdAgentContext,
|
|
10
|
+
create_terminal_box,
|
|
11
|
+
show_confirmation,
|
|
12
|
+
show_input,
|
|
13
|
+
)
|
|
14
|
+
from tinybird.tb.modules.feedback_manager import FeedbackManager
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def create_tests(ctx: RunContext[TinybirdAgentContext], pipe_name: str, test_content: str) -> str:
|
|
18
|
+
"""Given a pipe name, create or update a test for it
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
pipe_name (str): The pipe name to create a test for. Required.
|
|
22
|
+
test_content (str): The content of the test. Required.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
str: If the test was created or updated and the result of running the tests.
|
|
26
|
+
"""
|
|
27
|
+
running_tests = False
|
|
28
|
+
try:
|
|
29
|
+
ctx.deps.thinking_animation.stop()
|
|
30
|
+
path = Path(ctx.deps.folder) / "tests" / f"{pipe_name}.yaml"
|
|
31
|
+
current_test_content: Optional[str] = None
|
|
32
|
+
if path.exists():
|
|
33
|
+
current_test_content = path.read_text()
|
|
34
|
+
|
|
35
|
+
if current_test_content:
|
|
36
|
+
content = create_terminal_box(current_test_content, new_content=test_content, title=path.name)
|
|
37
|
+
else:
|
|
38
|
+
content = create_terminal_box(test_content, title=path.name)
|
|
39
|
+
|
|
40
|
+
click.echo(content)
|
|
41
|
+
action_text = "Create" if not current_test_content else "Update"
|
|
42
|
+
confirmation = show_confirmation(
|
|
43
|
+
title=f"{action_text} '{path.name}'?",
|
|
44
|
+
skip_confirmation=ctx.deps.dangerously_skip_permissions,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if confirmation == "review":
|
|
48
|
+
feedback = show_input(ctx.deps.workspace_name)
|
|
49
|
+
ctx.deps.thinking_animation.start()
|
|
50
|
+
return f"User did not confirm the proposed changes and gave the following feedback: {feedback}"
|
|
51
|
+
|
|
52
|
+
folder_path = path.parent
|
|
53
|
+
folder_path.mkdir(parents=True, exist_ok=True)
|
|
54
|
+
path.touch(exist_ok=True)
|
|
55
|
+
path.write_text(test_content)
|
|
56
|
+
action_text = "created" if not current_test_content else "updated"
|
|
57
|
+
click.echo(FeedbackManager.success(message=f"✓ {path.name} {action_text}"))
|
|
58
|
+
running_tests = True
|
|
59
|
+
test_output = ctx.deps.run_tests(pipe_name=pipe_name)
|
|
60
|
+
click.echo(test_output)
|
|
61
|
+
ctx.deps.thinking_animation.start()
|
|
62
|
+
return f"Test {action_text} for '{pipe_name}' endpoint in {path} and ran successfully\n{test_output}"
|
|
63
|
+
except AgentRunCancelled as e:
|
|
64
|
+
raise e
|
|
65
|
+
except Exception as e:
|
|
66
|
+
error_message = str(e).replace("test_error__error__", "")
|
|
67
|
+
ctx.deps.thinking_animation.stop()
|
|
68
|
+
if not running_tests:
|
|
69
|
+
click.echo(FeedbackManager.error(message=error_message))
|
|
70
|
+
ctx.deps.thinking_animation.start()
|
|
71
|
+
if running_tests:
|
|
72
|
+
return f"Test {action_text} for '{pipe_name}' endpoint in {path} but there were errors running the tests: {error_message}"
|
|
73
|
+
return f"Error creating test for '{pipe_name}' endpoint: {error_message}"
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def run_tests(ctx: RunContext[TinybirdAgentContext], pipe_name: Optional[str] = None) -> str:
|
|
77
|
+
"""Run tests for a given pipe name or all tests in the project
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
pipe_name (Optional[str]): The pipe name to run tests for. If not provided, all tests in the project will be run.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
str: The result of running the tests.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
try:
|
|
87
|
+
ctx.deps.thinking_animation.stop()
|
|
88
|
+
path = Path(ctx.deps.folder) / "tests" / f"{pipe_name}.yaml"
|
|
89
|
+
|
|
90
|
+
title = f"Run tests for '{pipe_name}'?" if pipe_name else "Run all tests in the project?"
|
|
91
|
+
confirmation = show_confirmation(
|
|
92
|
+
title=title,
|
|
93
|
+
skip_confirmation=ctx.deps.dangerously_skip_permissions,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
if confirmation == "review":
|
|
97
|
+
feedback = show_input(ctx.deps.workspace_name)
|
|
98
|
+
ctx.deps.thinking_animation.start()
|
|
99
|
+
return f"User did not confirm the proposed changes and gave the following feedback: {feedback}"
|
|
100
|
+
|
|
101
|
+
test_output = ctx.deps.run_tests(pipe_name=pipe_name)
|
|
102
|
+
click.echo(test_output)
|
|
103
|
+
ctx.deps.thinking_animation.start()
|
|
104
|
+
if pipe_name:
|
|
105
|
+
return f"Tests for '{pipe_name}' endpoint in {path} and ran successfully\n{test_output}"
|
|
106
|
+
else:
|
|
107
|
+
return f"All tests in the project ran successfully\n{test_output}"
|
|
108
|
+
except AgentRunCancelled as e:
|
|
109
|
+
raise e
|
|
110
|
+
except Exception as e:
|
|
111
|
+
error_message = str(e)
|
|
112
|
+
test_exit_code = "test_error__error__"
|
|
113
|
+
test_error = test_exit_code in error_message
|
|
114
|
+
ctx.deps.thinking_animation.stop()
|
|
115
|
+
if not test_error:
|
|
116
|
+
click.echo(FeedbackManager.error(message=error_message))
|
|
117
|
+
else:
|
|
118
|
+
error_message = error_message.replace(test_exit_code, "")
|
|
119
|
+
ctx.deps.thinking_animation.start()
|
|
120
|
+
return f"Error running tests: {error_message}"
|
|
@@ -52,6 +52,7 @@ class TinybirdAgentContext(BaseModel):
|
|
|
52
52
|
get_pipe_datafile_local: Callable[..., str]
|
|
53
53
|
get_connection_datafile_cloud: Callable[..., str]
|
|
54
54
|
get_connection_datafile_local: Callable[..., str]
|
|
55
|
+
run_tests: Callable[..., Optional[str]]
|
|
55
56
|
dangerously_skip_permissions: bool
|
|
56
57
|
token: str
|
|
57
58
|
user_token: str
|
|
@@ -352,7 +353,7 @@ def show_options(options: List[str], title: str = "Select an option") -> Optiona
|
|
|
352
353
|
return result["option"]
|
|
353
354
|
|
|
354
355
|
|
|
355
|
-
ConfirmationResult = Literal["yes", "review"
|
|
356
|
+
ConfirmationResult = Literal["yes", "review"]
|
|
356
357
|
|
|
357
358
|
|
|
358
359
|
def load_existing_resources(folder: str) -> str:
|
|
@@ -703,6 +704,12 @@ def show_input(workspace_name: str) -> str:
|
|
|
703
704
|
)
|
|
704
705
|
|
|
705
706
|
|
|
707
|
+
class AgentRunCancelled(Exception):
|
|
708
|
+
"""Exception raised when user cancels an operation"""
|
|
709
|
+
|
|
710
|
+
pass
|
|
711
|
+
|
|
712
|
+
|
|
706
713
|
def show_confirmation(title: str, skip_confirmation: bool = False) -> ConfirmationResult:
|
|
707
714
|
if skip_confirmation:
|
|
708
715
|
return "yes"
|
|
@@ -714,11 +721,11 @@ def show_confirmation(title: str, skip_confirmation: bool = False) -> Confirmati
|
|
|
714
721
|
)
|
|
715
722
|
|
|
716
723
|
if result is None: # Cancelled
|
|
717
|
-
|
|
724
|
+
raise AgentRunCancelled(f"User cancelled the operation: {title}")
|
|
718
725
|
|
|
719
726
|
if result.startswith("Yes"):
|
|
720
727
|
return "yes"
|
|
721
728
|
elif result.startswith("No"):
|
|
722
729
|
return "review"
|
|
723
730
|
|
|
724
|
-
|
|
731
|
+
raise AgentRunCancelled(f"User cancelled the operation: {title}")
|
tinybird/tb/modules/project.py
CHANGED
|
@@ -24,6 +24,10 @@ class Project:
|
|
|
24
24
|
def vendor_path(self) -> str:
|
|
25
25
|
return f"{self.path}/vendor"
|
|
26
26
|
|
|
27
|
+
@property
|
|
28
|
+
def tests_path(self) -> str:
|
|
29
|
+
return f"{self.path}/tests"
|
|
30
|
+
|
|
27
31
|
def get_files(self, extension: str) -> List[str]:
|
|
28
32
|
project_files: List[str] = []
|
|
29
33
|
for level in range(self.max_depth):
|
|
@@ -52,13 +56,32 @@ class Project:
|
|
|
52
56
|
|
|
53
57
|
def get_fixture_files(self) -> List[str]:
|
|
54
58
|
fixture_files: List[str] = []
|
|
55
|
-
for extension in [
|
|
59
|
+
for extension in [
|
|
60
|
+
"csv",
|
|
61
|
+
"csv.gz",
|
|
62
|
+
"ndjson",
|
|
63
|
+
"ndjson.gz",
|
|
64
|
+
"jsonl",
|
|
65
|
+
"jsonl.gz",
|
|
66
|
+
"json",
|
|
67
|
+
"json.gz",
|
|
68
|
+
"parquet",
|
|
69
|
+
"parquet.gz",
|
|
70
|
+
]:
|
|
56
71
|
for fixture_file in self.get_files(extension):
|
|
57
72
|
if self.vendor_path in fixture_file:
|
|
58
73
|
continue
|
|
59
74
|
fixture_files.append(fixture_file)
|
|
60
75
|
return fixture_files
|
|
61
76
|
|
|
77
|
+
def get_test_files(self) -> List[str]:
|
|
78
|
+
test_files: List[str] = []
|
|
79
|
+
for test_file in self.get_files("yaml"):
|
|
80
|
+
if self.vendor_path in test_file or self.tests_path not in test_file:
|
|
81
|
+
continue
|
|
82
|
+
test_files.append(test_file)
|
|
83
|
+
return test_files
|
|
84
|
+
|
|
62
85
|
def get_resource_path(self, resource_name: str, resource_type: str) -> str:
|
|
63
86
|
full_path = next(
|
|
64
87
|
(p for p in self.get_project_files() if p.endswith("/" + resource_name + f".{resource_type}")), ""
|