tinybird 0.0.1.dev256__py3-none-any.whl → 0.0.1.dev258__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/datafile/common.py +2 -0
- tinybird/tb/__cli__.py +2 -2
- tinybird/tb/modules/agent/agent.py +168 -220
- tinybird/tb/modules/agent/banner.py +1 -0
- tinybird/tb/modules/agent/prompts.py +154 -6
- tinybird/tb/modules/agent/tools/analyze.py +3 -1
- tinybird/tb/modules/agent/tools/append.py +34 -28
- tinybird/tb/modules/agent/tools/build.py +3 -2
- tinybird/tb/modules/agent/tools/create_datafile.py +60 -11
- tinybird/tb/modules/agent/tools/deploy.py +3 -7
- tinybird/tb/modules/agent/tools/deploy_check.py +2 -7
- tinybird/tb/modules/agent/tools/diff_resource.py +1 -3
- tinybird/tb/modules/agent/tools/execute_query.py +1 -1
- tinybird/tb/modules/agent/tools/get_endpoint_stats.py +1 -2
- tinybird/tb/modules/agent/tools/get_openapi_definition.py +27 -13
- tinybird/tb/modules/agent/tools/mock.py +19 -13
- tinybird/tb/modules/agent/tools/plan.py +12 -20
- tinybird/tb/modules/agent/tools/request_endpoint.py +5 -4
- tinybird/tb/modules/agent/utils.py +15 -6
- tinybird/tb/modules/cli.py +9 -7
- {tinybird-0.0.1.dev256.dist-info → tinybird-0.0.1.dev258.dist-info}/METADATA +2 -2
- {tinybird-0.0.1.dev256.dist-info → tinybird-0.0.1.dev258.dist-info}/RECORD +25 -25
- {tinybird-0.0.1.dev256.dist-info → tinybird-0.0.1.dev258.dist-info}/WHEEL +0 -0
- {tinybird-0.0.1.dev256.dist-info → tinybird-0.0.1.dev258.dist-info}/entry_points.txt +0 -0
- {tinybird-0.0.1.dev256.dist-info → tinybird-0.0.1.dev258.dist-info}/top_level.txt +0 -0
tinybird/datafile/common.py
CHANGED
|
@@ -1744,6 +1744,8 @@ def parse(
|
|
|
1744
1744
|
def _f(*args: str, **kwargs: Any):
|
|
1745
1745
|
__init_engine(f"ENGINE_{v}".upper())
|
|
1746
1746
|
engine_arg = eval_var(_unquote((" ".join(args)).strip()), skip=skip_eval)
|
|
1747
|
+
if v.lower() == "ttl" and not engine_arg:
|
|
1748
|
+
return
|
|
1747
1749
|
parser_state.current_node["engine"]["args"].append((v, engine_arg))
|
|
1748
1750
|
|
|
1749
1751
|
return _f
|
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.dev258'
|
|
8
|
+
__revision__ = 'ca57848'
|
|
@@ -1,41 +1,23 @@
|
|
|
1
|
+
import asyncio
|
|
1
2
|
import shlex
|
|
2
3
|
import subprocess
|
|
3
4
|
import sys
|
|
4
|
-
from datetime import datetime
|
|
5
5
|
from functools import partial
|
|
6
6
|
from pathlib import Path
|
|
7
7
|
from typing import Any, Optional
|
|
8
8
|
|
|
9
9
|
import click
|
|
10
|
-
|
|
11
|
-
from pydantic_ai
|
|
12
|
-
|
|
13
|
-
from
|
|
14
|
-
|
|
15
|
-
copy_pipe_instructions,
|
|
16
|
-
datasource_example,
|
|
17
|
-
datasource_instructions,
|
|
18
|
-
gcs_connection_example,
|
|
19
|
-
kafka_connection_example,
|
|
20
|
-
materialized_pipe_instructions,
|
|
21
|
-
pipe_example,
|
|
22
|
-
pipe_instructions,
|
|
23
|
-
s3_connection_example,
|
|
24
|
-
sink_pipe_instructions,
|
|
25
|
-
)
|
|
10
|
+
import humanfriendly
|
|
11
|
+
from pydantic_ai import Agent, RunContext, Tool
|
|
12
|
+
from pydantic_ai.agent import AgentRunResult
|
|
13
|
+
from pydantic_ai.messages import ModelMessage, ModelRequest, UserPromptPart
|
|
14
|
+
|
|
26
15
|
from tinybird.tb.client import TinyB
|
|
27
16
|
from tinybird.tb.modules.agent.animations import ThinkingAnimation
|
|
28
17
|
from tinybird.tb.modules.agent.banner import display_banner
|
|
29
18
|
from tinybird.tb.modules.agent.memory import clear_history, clear_messages, load_messages, save_messages
|
|
30
19
|
from tinybird.tb.modules.agent.models import create_model, model_costs
|
|
31
|
-
from tinybird.tb.modules.agent.prompts import
|
|
32
|
-
datafile_instructions,
|
|
33
|
-
endpoint_optimization_instructions,
|
|
34
|
-
plan_instructions,
|
|
35
|
-
resources_prompt,
|
|
36
|
-
sql_agent_instructions,
|
|
37
|
-
sql_instructions,
|
|
38
|
-
)
|
|
20
|
+
from tinybird.tb.modules.agent.prompts import agent_system_prompt, resources_prompt
|
|
39
21
|
from tinybird.tb.modules.agent.tools.analyze import analyze_file, analyze_url
|
|
40
22
|
from tinybird.tb.modules.agent.tools.append import append_file, append_url
|
|
41
23
|
from tinybird.tb.modules.agent.tools.build import build
|
|
@@ -50,9 +32,9 @@ from tinybird.tb.modules.agent.tools.mock import mock
|
|
|
50
32
|
from tinybird.tb.modules.agent.tools.plan import plan
|
|
51
33
|
from tinybird.tb.modules.agent.tools.preview_datafile import preview_datafile
|
|
52
34
|
from tinybird.tb.modules.agent.tools.request_endpoint import request_endpoint
|
|
53
|
-
from tinybird.tb.modules.agent.utils import TinybirdAgentContext, show_input
|
|
35
|
+
from tinybird.tb.modules.agent.utils import AgentRunCancelled, TinybirdAgentContext, show_input
|
|
54
36
|
from tinybird.tb.modules.build_common import process as build_process
|
|
55
|
-
from tinybird.tb.modules.common import _analyze, _get_tb_client
|
|
37
|
+
from tinybird.tb.modules.common import _analyze, _get_tb_client, echo_safe_humanfriendly_tables_format_pretty_table
|
|
56
38
|
from tinybird.tb.modules.config import CLIConfig
|
|
57
39
|
from tinybird.tb.modules.deployment_common import create_deployment
|
|
58
40
|
from tinybird.tb.modules.exceptions import CLIBuildException, CLIDeploymentException, CLIMockException
|
|
@@ -72,141 +54,21 @@ class TinybirdAgent:
|
|
|
72
54
|
workspace_id: str,
|
|
73
55
|
project: Project,
|
|
74
56
|
dangerously_skip_permissions: bool,
|
|
57
|
+
prompt_mode: bool,
|
|
75
58
|
):
|
|
76
59
|
self.token = token
|
|
77
60
|
self.user_token = user_token
|
|
78
61
|
self.host = host
|
|
79
|
-
self.dangerously_skip_permissions = dangerously_skip_permissions
|
|
62
|
+
self.dangerously_skip_permissions = dangerously_skip_permissions or prompt_mode
|
|
80
63
|
self.project = project
|
|
81
|
-
|
|
82
|
-
|
|
64
|
+
if prompt_mode:
|
|
65
|
+
self.messages: list[ModelMessage] = load_messages()[-5:]
|
|
66
|
+
else:
|
|
67
|
+
self.messages = []
|
|
83
68
|
self.agent = Agent(
|
|
84
69
|
model=create_model(user_token, host, workspace_id),
|
|
85
70
|
deps_type=TinybirdAgentContext,
|
|
86
|
-
system_prompt=
|
|
87
|
-
You are a Tinybird Code, an agentic CLI that can help users to work with Tinybird.
|
|
88
|
-
|
|
89
|
-
You are an interactive CLI tool that helps users with data engineering tasks. Use the instructions below and the tools available to you to assist the user.
|
|
90
|
-
|
|
91
|
-
# Tone and style
|
|
92
|
-
You should be concise, direct, and to the point.
|
|
93
|
-
Remember that your output will be displayed on a command line interface. Your responses can use Github-flavored markdown for formatting. Do not use emojis.
|
|
94
|
-
Output text to communicate with the user; all text you output outside of tool use is displayed to the user. Only use tools to complete tasks. Never use tools like Bash or code comments as means to communicate with the user during the session.
|
|
95
|
-
If you cannot or will not help the user with something, please do not say why or what it could lead to, since this comes across as preachy and annoying. Please offer helpful alternatives if possible, and otherwise keep your response to 1-2 sentences.
|
|
96
|
-
IMPORTANT: You should minimize output tokens as much as possible while maintaining helpfulness, quality, and accuracy. Only address the specific query or task at hand, avoiding tangential information unless absolutely critical for completing the request. If you can answer in 1-3 sentences or a short paragraph, please do.
|
|
97
|
-
IMPORTANT: You should NOT answer with unnecessary preamble or postamble (such as explaining your code or summarizing your action), unless the user asks you to.
|
|
98
|
-
IMPORTANT: Keep your responses short, since they will be displayed on a command line interface. You MUST answer concisely with fewer than 4 lines (not including tool use or code generation), unless user asks for detail. Answer the user's question directly, without elaboration, explanation, or details. One word answers are best. Avoid introductions, conclusions, and explanations. You MUST avoid text before/after your response, such as "The answer is <answer>.", "Here is the content of the file..." or "Based on the information provided, the answer is..." or "Here is what I will do next...". Here are some examples to demonstrate appropriate verbosity:
|
|
99
|
-
|
|
100
|
-
# Proactiveness
|
|
101
|
-
You are allowed to be proactive, but only when the user asks you to do something. You should strive to strike a balance between:
|
|
102
|
-
Doing the right thing when asked, including taking actions and follow-up actions
|
|
103
|
-
Not surprising the user with actions you take without asking
|
|
104
|
-
For example, if the user asks you how to approach something, you should do your best to answer their question first, and not immediately jump into taking actions.
|
|
105
|
-
Do not add additional code explanation summary unless requested by the user. After working on a file, just stop, rather than providing an explanation of what you did.
|
|
106
|
-
|
|
107
|
-
# Code style
|
|
108
|
-
IMPORTANT: DO NOT ADD ANY COMMENTS unless asked by the user.
|
|
109
|
-
|
|
110
|
-
# Tools
|
|
111
|
-
You have access to the following tools:
|
|
112
|
-
1. `preview_datafile` - Preview the content of a datafile (datasource, endpoint, materialized, sink, copy, connection).
|
|
113
|
-
2. `create_datafile` - Create a file in the project folder. Confirmation will be asked by the tool before creating the file.
|
|
114
|
-
3. `plan` - Plan the creation or update of resources.
|
|
115
|
-
4. `build` - Build the project.
|
|
116
|
-
5. `deploy` - Deploy the project to Tinybird Cloud.
|
|
117
|
-
6. `deploy_check` - Check if the project can be deployed to Tinybird Cloud before deploying it.
|
|
118
|
-
7. `mock` - Create mock data for a landing datasource.
|
|
119
|
-
8. `analyze_file` - Analyze the content of a fixture file present in the project folder.
|
|
120
|
-
9. `analyze_url` - Analyze the content of an external url.
|
|
121
|
-
9. `append_file` - Append a file present in the project to a datasource.
|
|
122
|
-
10. `append_url` - Append an external url to a datasource.
|
|
123
|
-
11. `get_endpoint_stats` - Get metrics of the requests to an endpoint.
|
|
124
|
-
12. `get_openapi_definition` - Get the OpenAPI definition for all endpoints that are built/deployed to Tinybird Cloud or Local.
|
|
125
|
-
13. `execute_query` - Execute a query against Tinybird Cloud or Local.
|
|
126
|
-
13. `request_endpoint` - Request an endpoint against Tinybird Cloud or Local.
|
|
127
|
-
14. `diff_resource` - Diff the content of a resource in Tinybird Cloud vs Tinybird Local vs Project local file.
|
|
128
|
-
|
|
129
|
-
# When creating or updating datafiles:
|
|
130
|
-
1. Use `plan` tool to plan the creation or update of resources.
|
|
131
|
-
2. If the user confirms the plan, go from 3 to 7 steps until all the resources are created, updated or skipped.
|
|
132
|
-
3. Use `preview_datafile` tool to preview the content of a datafile.
|
|
133
|
-
4. Without asking, use the `create_datafile` tool to create the datafile, because it will ask for confirmation before creating the file.
|
|
134
|
-
5. Check the result of the `create_datafile` tool to see if the datafile was created successfully.
|
|
135
|
-
6. If the datafile was created successfully, report the result to the user.
|
|
136
|
-
7. If the datafile was not created, finish the process and just wait for a new user prompt.
|
|
137
|
-
8. If the datafile was created successfully, but the built failed, try to fix the error and repeat the process.
|
|
138
|
-
|
|
139
|
-
# When creating a landing datasource given a .ndjson file:
|
|
140
|
-
- If the user does not specify anything about the desired schema, create a schema like this:
|
|
141
|
-
SCHEMA >
|
|
142
|
-
`data` String `json:$`
|
|
143
|
-
|
|
144
|
-
- Use always json paths with .ndjson files.
|
|
145
|
-
|
|
146
|
-
# When user wants to optimize an endpoint:
|
|
147
|
-
{endpoint_optimization_instructions}
|
|
148
|
-
|
|
149
|
-
IMPORTANT: If the user cancels some of the steps or there is an error in file creation, DO NOT continue with the plan. Stop the process and wait for the user before using any other tool.
|
|
150
|
-
IMPORTANT: Every time you finish a plan and start a new resource creation or update process, create a new plan before starting with the changes.
|
|
151
|
-
|
|
152
|
-
# Using deployment tools:
|
|
153
|
-
- Use `deploy_check` tool to check if the project can be deployed to Tinybird Cloud before deploying it.
|
|
154
|
-
- Use `deploy` tool to deploy the project to Tinybird Cloud.
|
|
155
|
-
- Only use deployment tools if user explicitly asks for it.
|
|
156
|
-
|
|
157
|
-
# When planning the creation or update of resources:
|
|
158
|
-
{plan_instructions}
|
|
159
|
-
{datafile_instructions}
|
|
160
|
-
|
|
161
|
-
# Working with datasource files:
|
|
162
|
-
{datasource_instructions}
|
|
163
|
-
{datasource_example}
|
|
164
|
-
|
|
165
|
-
# Working with any type of pipe file:
|
|
166
|
-
{pipe_instructions}
|
|
167
|
-
{pipe_example}
|
|
168
|
-
|
|
169
|
-
# Working with materialized pipe files:
|
|
170
|
-
{materialized_pipe_instructions}
|
|
171
|
-
|
|
172
|
-
# Working with sink pipe files:
|
|
173
|
-
{sink_pipe_instructions}
|
|
174
|
-
|
|
175
|
-
# Working with copy pipe files:
|
|
176
|
-
{copy_pipe_instructions}
|
|
177
|
-
|
|
178
|
-
# Working with SQL queries:
|
|
179
|
-
{sql_agent_instructions}
|
|
180
|
-
{sql_instructions}
|
|
181
|
-
|
|
182
|
-
# Working with connections files:
|
|
183
|
-
{connection_instructions}
|
|
184
|
-
|
|
185
|
-
# Connection examples:
|
|
186
|
-
Kafka: {kafka_connection_example}
|
|
187
|
-
S3: {s3_connection_example}
|
|
188
|
-
GCS: {gcs_connection_example}
|
|
189
|
-
|
|
190
|
-
# When executing a query or requesting/testing an endpoint:
|
|
191
|
-
- You need to be sure that the selected resource is updated to the last version in the environment you are working on.
|
|
192
|
-
- Use `diff_resource` tool to compare the content of the resource to compare the differences between environments.
|
|
193
|
-
- Project local file is the source of truth.
|
|
194
|
-
- If the resource is not present or updated to the last version in Tinybird Local, it means you need to build the project.
|
|
195
|
-
- If the resource is not present or updated to the last version in Tinybird Cloud, it means you need to deploy the project.
|
|
196
|
-
|
|
197
|
-
# How to use apppend tools:
|
|
198
|
-
- Use append as part of the creation of a new landing datasource if the user provided a file or an external url
|
|
199
|
-
- Use append if user explicitly asks for it
|
|
200
|
-
- Do not append data if user requests to test an endpoint
|
|
201
|
-
|
|
202
|
-
# How to use `mock` tool:
|
|
203
|
-
- 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
|
|
204
|
-
- Use `mock` tool if user explicitly asks for it
|
|
205
|
-
- Do not use `mock` tool if user requests to test an endpoint.
|
|
206
|
-
|
|
207
|
-
# Info
|
|
208
|
-
Today is {datetime.now().strftime("%Y-%m-%d")}
|
|
209
|
-
""",
|
|
71
|
+
system_prompt=agent_system_prompt,
|
|
210
72
|
tools=[
|
|
211
73
|
Tool(preview_datafile, docstring_format="google", require_parameter_descriptions=True, takes_ctx=False),
|
|
212
74
|
Tool(create_datafile, docstring_format="google", require_parameter_descriptions=True, takes_ctx=True),
|
|
@@ -232,77 +94,134 @@ Today is {datetime.now().strftime("%Y-%m-%d")}
|
|
|
232
94
|
Tool(request_endpoint, docstring_format="google", require_parameter_descriptions=True, takes_ctx=True),
|
|
233
95
|
Tool(diff_resource, docstring_format="google", require_parameter_descriptions=True, takes_ctx=True),
|
|
234
96
|
],
|
|
97
|
+
# history_processors=[self._keep_recent_messages],
|
|
235
98
|
)
|
|
236
99
|
|
|
237
|
-
|
|
100
|
+
@self.agent.instructions
|
|
101
|
+
def get_local_host(ctx: RunContext[TinybirdAgentContext]) -> str:
|
|
102
|
+
return f"Tinybird Local host: {ctx.deps.local_host}"
|
|
103
|
+
|
|
104
|
+
@self.agent.instructions
|
|
105
|
+
def get_cloud_host(ctx: RunContext[TinybirdAgentContext]) -> str:
|
|
106
|
+
return f"Tinybird Cloud host: {ctx.deps.host}"
|
|
107
|
+
|
|
108
|
+
@self.agent.instructions
|
|
109
|
+
def get_local_token(ctx: RunContext[TinybirdAgentContext]) -> str:
|
|
110
|
+
return f"Tinybird Local token: {ctx.deps.local_token}"
|
|
111
|
+
|
|
112
|
+
@self.agent.instructions
|
|
113
|
+
def get_cloud_token(ctx: RunContext[TinybirdAgentContext]) -> str:
|
|
114
|
+
return f"Tinybird Cloud token: {ctx.deps.token}"
|
|
115
|
+
|
|
116
|
+
@self.agent.instructions
|
|
117
|
+
def get_project_files(ctx: RunContext[TinybirdAgentContext]) -> str:
|
|
118
|
+
return resources_prompt(self.project)
|
|
119
|
+
|
|
120
|
+
self.thinking_animation = ThinkingAnimation()
|
|
121
|
+
|
|
122
|
+
def add_message(self, message: ModelMessage) -> None:
|
|
123
|
+
self.messages.append(message)
|
|
124
|
+
|
|
125
|
+
def _keep_recent_messages(self, messages: list[ModelMessage]) -> list[ModelMessage]:
|
|
238
126
|
"""Keep only the last 5 messages to manage token usage."""
|
|
239
|
-
return
|
|
127
|
+
return messages[-5:] if len(messages) > 5 else messages
|
|
240
128
|
|
|
241
|
-
def
|
|
242
|
-
user_prompt = f"{user_prompt}\n\n{resources_prompt(project)}"
|
|
129
|
+
def _build_agent_deps(self, config: dict[str, Any]) -> TinybirdAgentContext:
|
|
243
130
|
client = TinyB(token=self.token, host=self.host)
|
|
131
|
+
project = self.project
|
|
244
132
|
folder = self.project.folder
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
133
|
+
local_client = get_tinybird_local_client(config, test=False, silent=False)
|
|
134
|
+
return TinybirdAgentContext(
|
|
135
|
+
# context does not support the whole client, so we need to pass only the functions we need
|
|
136
|
+
explore_data=client.explore_data,
|
|
137
|
+
build_project=partial(build_project, project=project, config=config),
|
|
138
|
+
deploy_project=partial(deploy_project, project=project, config=config),
|
|
139
|
+
deploy_check_project=partial(deploy_check_project, project=project, config=config),
|
|
140
|
+
mock_data=partial(mock_data, project=project, config=config),
|
|
141
|
+
append_data_local=partial(append_data_local, config=config),
|
|
142
|
+
append_data_cloud=partial(append_data_cloud, config=config),
|
|
143
|
+
analyze_fixture=partial(analyze_fixture, config=config),
|
|
144
|
+
execute_query_cloud=partial(execute_query_cloud, config=config),
|
|
145
|
+
execute_query_local=partial(execute_query_local, config=config),
|
|
146
|
+
request_endpoint_cloud=partial(request_endpoint_cloud, config=config),
|
|
147
|
+
request_endpoint_local=partial(request_endpoint_local, config=config),
|
|
148
|
+
get_datasource_datafile_cloud=partial(get_datasource_datafile_cloud, config=config),
|
|
149
|
+
get_datasource_datafile_local=partial(get_datasource_datafile_local, config=config),
|
|
150
|
+
get_pipe_datafile_cloud=partial(get_pipe_datafile_cloud, config=config),
|
|
151
|
+
get_pipe_datafile_local=partial(get_pipe_datafile_local, config=config),
|
|
152
|
+
get_connection_datafile_cloud=partial(get_connection_datafile_cloud, config=config),
|
|
153
|
+
get_connection_datafile_local=partial(get_connection_datafile_local, config=config),
|
|
154
|
+
get_project_files=project.get_project_files,
|
|
155
|
+
folder=folder,
|
|
156
|
+
thinking_animation=self.thinking_animation,
|
|
157
|
+
workspace_name=self.project.workspace_name,
|
|
158
|
+
dangerously_skip_permissions=self.dangerously_skip_permissions,
|
|
159
|
+
token=self.token,
|
|
160
|
+
user_token=self.user_token,
|
|
161
|
+
host=self.host,
|
|
162
|
+
local_host=local_client.host,
|
|
163
|
+
local_token=local_client.token,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
def run(self, user_prompt: str, config: dict[str, Any]) -> None:
|
|
167
|
+
user_prompt = f"{user_prompt}\n\n{resources_prompt(self.project)}"
|
|
168
|
+
self.thinking_animation.start()
|
|
248
169
|
result = self.agent.run_sync(
|
|
249
170
|
user_prompt,
|
|
250
|
-
deps=
|
|
251
|
-
# context does not support the whole client, so we need to pass only the functions we need
|
|
252
|
-
explore_data=client.explore_data,
|
|
253
|
-
build_project=partial(build_project, project=project, config=config),
|
|
254
|
-
deploy_project=partial(deploy_project, project=project, config=config),
|
|
255
|
-
deploy_check_project=partial(deploy_check_project, project=project, config=config),
|
|
256
|
-
mock_data=partial(mock_data, project=project, config=config),
|
|
257
|
-
append_data=partial(append_data, config=config),
|
|
258
|
-
analyze_fixture=partial(analyze_fixture, config=config),
|
|
259
|
-
execute_query_cloud=partial(execute_query_cloud, config=config),
|
|
260
|
-
execute_query_local=partial(execute_query_local, config=config),
|
|
261
|
-
request_endpoint_cloud=partial(request_endpoint_cloud, config=config),
|
|
262
|
-
request_endpoint_local=partial(request_endpoint_local, config=config),
|
|
263
|
-
get_datasource_datafile_cloud=partial(get_datasource_datafile_cloud, config=config),
|
|
264
|
-
get_datasource_datafile_local=partial(get_datasource_datafile_local, config=config),
|
|
265
|
-
get_pipe_datafile_cloud=partial(get_pipe_datafile_cloud, config=config),
|
|
266
|
-
get_pipe_datafile_local=partial(get_pipe_datafile_local, config=config),
|
|
267
|
-
get_connection_datafile_cloud=partial(get_connection_datafile_cloud, config=config),
|
|
268
|
-
get_connection_datafile_local=partial(get_connection_datafile_local, config=config),
|
|
269
|
-
get_project_files=project.get_project_files,
|
|
270
|
-
folder=folder,
|
|
271
|
-
thinking_animation=thinking_animation,
|
|
272
|
-
workspace_name=self.project.workspace_name,
|
|
273
|
-
dangerously_skip_permissions=self.dangerously_skip_permissions,
|
|
274
|
-
token=self.token,
|
|
275
|
-
user_token=self.user_token,
|
|
276
|
-
host=self.host,
|
|
277
|
-
),
|
|
171
|
+
deps=self._build_agent_deps(config),
|
|
278
172
|
message_history=self.messages,
|
|
279
173
|
)
|
|
280
174
|
new_messages = result.new_messages()
|
|
281
175
|
self.messages.extend(new_messages)
|
|
282
176
|
save_messages(new_messages)
|
|
283
|
-
thinking_animation.stop()
|
|
284
|
-
usage = result.usage()
|
|
285
|
-
request_tokens = usage.request_tokens or 0
|
|
286
|
-
response_tokens = usage.response_tokens or 0
|
|
287
|
-
total_tokens = usage.total_tokens or 0
|
|
288
|
-
cost = (
|
|
289
|
-
request_tokens * model_costs["input_cost_per_token"]
|
|
290
|
-
+ response_tokens * model_costs["output_cost_per_token"]
|
|
291
|
-
)
|
|
177
|
+
self.thinking_animation.stop()
|
|
292
178
|
click.echo(result.output)
|
|
293
|
-
|
|
294
|
-
|
|
179
|
+
self._echo_usage(config, result)
|
|
180
|
+
|
|
181
|
+
async def run_iter(self, user_prompt: str, config: dict[str, Any]) -> None:
|
|
182
|
+
user_prompt = f"{user_prompt}\n\n"
|
|
183
|
+
self.thinking_animation.start()
|
|
184
|
+
deps = self._build_agent_deps(config)
|
|
185
|
+
|
|
186
|
+
async with self.agent.iter(user_prompt, deps=deps, message_history=self.messages) as agent_run:
|
|
187
|
+
async for node in agent_run:
|
|
188
|
+
if hasattr(node, "model_response"):
|
|
189
|
+
for _i, part in enumerate(node.model_response.parts):
|
|
190
|
+
if hasattr(part, "content") and not agent_run.result:
|
|
191
|
+
animation_running = self.thinking_animation.running
|
|
192
|
+
if animation_running:
|
|
193
|
+
self.thinking_animation.stop()
|
|
194
|
+
click.echo(FeedbackManager.info(message=part.content))
|
|
195
|
+
if animation_running:
|
|
196
|
+
self.thinking_animation.start()
|
|
197
|
+
|
|
198
|
+
if agent_run.result is not None:
|
|
199
|
+
new_messages = agent_run.result.new_messages()
|
|
200
|
+
self.messages.extend(new_messages)
|
|
201
|
+
save_messages(new_messages)
|
|
202
|
+
self.thinking_animation.stop()
|
|
203
|
+
self._echo_usage(config, agent_run.result)
|
|
204
|
+
|
|
205
|
+
def _echo_usage(self, config: dict[str, Any], result: AgentRunResult) -> None:
|
|
295
206
|
if "@tinybird.co" in config.get("user_email", ""):
|
|
207
|
+
usage = result.usage()
|
|
208
|
+
request_tokens = usage.request_tokens or 0
|
|
209
|
+
response_tokens = usage.response_tokens or 0
|
|
210
|
+
total_tokens = usage.total_tokens or 0
|
|
211
|
+
cost = (
|
|
212
|
+
request_tokens * model_costs["input_cost_per_token"]
|
|
213
|
+
+ response_tokens * model_costs["output_cost_per_token"]
|
|
214
|
+
)
|
|
296
215
|
click.echo(f"Input tokens: {request_tokens}")
|
|
297
216
|
click.echo(f"Output tokens: {response_tokens}")
|
|
298
217
|
click.echo(f"Total tokens: {total_tokens}")
|
|
299
218
|
click.echo(f"Cost: ${cost:.6f}")
|
|
300
|
-
click.echo("\n")
|
|
301
219
|
|
|
302
220
|
|
|
303
221
|
def run_agent(
|
|
304
222
|
config: dict[str, Any], project: Project, dangerously_skip_permissions: bool, prompt: Optional[str] = None
|
|
305
223
|
):
|
|
224
|
+
click.echo(FeedbackManager.highlight(message="» Initializing Tinybird Code..."))
|
|
306
225
|
token = config.get("token", None)
|
|
307
226
|
host = config.get("host", None)
|
|
308
227
|
user_token = config.get("user_token", None)
|
|
@@ -319,9 +238,7 @@ def run_agent(
|
|
|
319
238
|
default=True,
|
|
320
239
|
)
|
|
321
240
|
if yes:
|
|
322
|
-
click.echo()
|
|
323
241
|
login(host, auth_host="https://cloud.tinybird.co", workspace=None, interactive=False, method="browser")
|
|
324
|
-
click.echo()
|
|
325
242
|
cli_config = CLIConfig.get_project_config()
|
|
326
243
|
config = {**config, **cli_config.to_dict()}
|
|
327
244
|
token = cli_config.get_token()
|
|
@@ -336,23 +253,22 @@ def run_agent(
|
|
|
336
253
|
)
|
|
337
254
|
return
|
|
338
255
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
256
|
+
build_project(config, project, test=False, silent=True)
|
|
257
|
+
|
|
258
|
+
# In prompt mode, always skip permissions to avoid interactive prompts
|
|
259
|
+
prompt_mode = prompt is not None
|
|
260
|
+
agent = TinybirdAgent(token, user_token, host, workspace_id, project, dangerously_skip_permissions, prompt_mode)
|
|
342
261
|
|
|
343
262
|
# Print mode: run once with the provided prompt and exit
|
|
344
263
|
if prompt:
|
|
345
|
-
agent.run(prompt, config
|
|
264
|
+
agent.run(prompt, config)
|
|
346
265
|
return
|
|
347
266
|
|
|
348
267
|
# Interactive mode: show banner and enter interactive loop
|
|
349
268
|
display_banner()
|
|
350
|
-
click.echo()
|
|
351
269
|
click.echo(FeedbackManager.info(message="Describe what you want to create and I'll help you build it"))
|
|
352
270
|
click.echo(FeedbackManager.info(message="Run /help for more commands"))
|
|
353
271
|
|
|
354
|
-
click.echo()
|
|
355
|
-
|
|
356
272
|
except Exception as e:
|
|
357
273
|
click.echo(FeedbackManager.error(message=f"Failed to initialize agent: {e}"))
|
|
358
274
|
return
|
|
@@ -365,33 +281,60 @@ def run_agent(
|
|
|
365
281
|
if user_input.startswith("tb "):
|
|
366
282
|
cmd_parts = shlex.split(user_input)
|
|
367
283
|
subprocess.run(cmd_parts)
|
|
368
|
-
click.echo()
|
|
369
284
|
continue
|
|
370
|
-
if user_input.lower() in ["/exit", "/quit"]:
|
|
285
|
+
if user_input.lower() in ["/exit", "/quit", "exit", "quit"]:
|
|
371
286
|
click.echo(FeedbackManager.info(message="Goodbye!"))
|
|
372
287
|
break
|
|
373
|
-
elif user_input.lower()
|
|
288
|
+
elif user_input.lower() in ["/clear", "clear"]:
|
|
374
289
|
clear_history()
|
|
290
|
+
click.echo(FeedbackManager.info(message="Message history cleared!"))
|
|
375
291
|
clear_messages()
|
|
376
292
|
continue
|
|
293
|
+
elif user_input.lower().startswith("select ") or user_input.lower().startswith("with "):
|
|
294
|
+
query = f"SELECT * FROM ({user_input.strip()}) FORMAT JSON"
|
|
295
|
+
result = execute_query_local(config, query=query)
|
|
296
|
+
stats = result["statistics"]
|
|
297
|
+
seconds = stats["elapsed"]
|
|
298
|
+
rows_read = humanfriendly.format_number(stats["rows_read"])
|
|
299
|
+
bytes_read = humanfriendly.format_size(stats["bytes_read"])
|
|
300
|
+
|
|
301
|
+
click.echo(FeedbackManager.info_query_stats(seconds=seconds, rows=rows_read, bytes=bytes_read))
|
|
302
|
+
|
|
303
|
+
if not result["data"]:
|
|
304
|
+
click.echo(FeedbackManager.info_no_rows())
|
|
305
|
+
else:
|
|
306
|
+
echo_safe_humanfriendly_tables_format_pretty_table(
|
|
307
|
+
data=[d.values() for d in result["data"][:10]], column_names=result["data"][0].keys()
|
|
308
|
+
)
|
|
309
|
+
click.echo("Showing first 10 results\n")
|
|
310
|
+
continue
|
|
377
311
|
elif user_input.lower() == "/login":
|
|
378
|
-
click.echo()
|
|
379
312
|
subprocess.run(["tb", "login"], check=True)
|
|
380
|
-
|
|
313
|
+
|
|
381
314
|
continue
|
|
382
315
|
elif user_input.lower() == "/help":
|
|
383
|
-
click.echo()
|
|
384
316
|
click.echo("• Describe what you want to create: 'Create a user analytics system'")
|
|
385
317
|
click.echo("• Ask for specific resources: 'Create a pipe to aggregate daily clicks'")
|
|
386
318
|
click.echo("• Connect to external services: 'Set up a Kafka connection for events'")
|
|
387
319
|
click.echo("• Type '/exit' or '/quit' to leave")
|
|
388
|
-
|
|
320
|
+
|
|
389
321
|
continue
|
|
390
322
|
elif user_input.strip() == "":
|
|
391
323
|
continue
|
|
392
324
|
else:
|
|
393
|
-
|
|
394
|
-
|
|
325
|
+
asyncio.run(agent.run_iter(user_input, config))
|
|
326
|
+
except AgentRunCancelled:
|
|
327
|
+
click.echo(FeedbackManager.info(message="User cancelled the operation"))
|
|
328
|
+
agent.add_message(
|
|
329
|
+
ModelRequest(
|
|
330
|
+
parts=[
|
|
331
|
+
UserPromptPart(
|
|
332
|
+
content="User cancelled the operation",
|
|
333
|
+
)
|
|
334
|
+
]
|
|
335
|
+
)
|
|
336
|
+
)
|
|
337
|
+
continue
|
|
395
338
|
except KeyboardInterrupt:
|
|
396
339
|
click.echo(FeedbackManager.info(message="Goodbye!"))
|
|
397
340
|
break
|
|
@@ -436,11 +379,16 @@ def deploy_check_project(config: dict[str, Any], project: Project) -> None:
|
|
|
436
379
|
raise CLIDeploymentException(e.args[0])
|
|
437
380
|
|
|
438
381
|
|
|
439
|
-
def
|
|
382
|
+
def append_data_local(config: dict[str, Any], datasource_name: str, path: str) -> None:
|
|
440
383
|
client = get_tinybird_local_client(config, test=False, silent=False)
|
|
441
384
|
append_mock_data(client, datasource_name, path)
|
|
442
385
|
|
|
443
386
|
|
|
387
|
+
def append_data_cloud(config: dict[str, Any], datasource_name: str, path: str) -> None:
|
|
388
|
+
client = _get_tb_client(config["token"], config["host"])
|
|
389
|
+
append_mock_data(client, datasource_name, path)
|
|
390
|
+
|
|
391
|
+
|
|
444
392
|
def mock_data(
|
|
445
393
|
config: dict[str, Any],
|
|
446
394
|
project: Project,
|