datasette-agent 0.1a0__tar.gz
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.
- datasette_agent-0.1a0/LICENSE +201 -0
- datasette_agent-0.1a0/PKG-INFO +220 -0
- datasette_agent-0.1a0/README.md +200 -0
- datasette_agent-0.1a0/datasette_agent/__init__.py +292 -0
- datasette_agent-0.1a0/datasette_agent/agent.py +215 -0
- datasette_agent-0.1a0/datasette_agent/api.py +112 -0
- datasette_agent-0.1a0/datasette_agent/background_agent.py +209 -0
- datasette_agent-0.1a0/datasette_agent/background_tools.py +77 -0
- datasette_agent-0.1a0/datasette_agent/cli_chat.py +111 -0
- datasette_agent-0.1a0/datasette_agent/context.py +5 -0
- datasette_agent-0.1a0/datasette_agent/explorer.py +132 -0
- datasette_agent-0.1a0/datasette_agent/export_markdown.py +93 -0
- datasette_agent-0.1a0/datasette_agent/hookspecs.py +8 -0
- datasette_agent-0.1a0/datasette_agent/messages.py +196 -0
- datasette_agent-0.1a0/datasette_agent/schema.py +69 -0
- datasette_agent-0.1a0/datasette_agent/sql_tools.py +236 -0
- datasette_agent-0.1a0/datasette_agent/static/agent.css +512 -0
- datasette_agent-0.1a0/datasette_agent/static/agent.js +400 -0
- datasette_agent-0.1a0/datasette_agent/static/chat-export.js +84 -0
- datasette_agent-0.1a0/datasette_agent/static/smd.js +1665 -0
- datasette_agent-0.1a0/datasette_agent/templates/agent_background.html +98 -0
- datasette_agent-0.1a0/datasette_agent/templates/agent_conversation.html +165 -0
- datasette_agent-0.1a0/datasette_agent/templates/agent_explorer.html +121 -0
- datasette_agent-0.1a0/datasette_agent/templates/agent_explorer_report.html +106 -0
- datasette_agent-0.1a0/datasette_agent/templates/agent_index.html +62 -0
- datasette_agent-0.1a0/datasette_agent/tools.py +52 -0
- datasette_agent-0.1a0/datasette_agent/views.py +534 -0
- datasette_agent-0.1a0/datasette_agent.egg-info/PKG-INFO +220 -0
- datasette_agent-0.1a0/datasette_agent.egg-info/SOURCES.txt +40 -0
- datasette_agent-0.1a0/datasette_agent.egg-info/dependency_links.txt +1 -0
- datasette_agent-0.1a0/datasette_agent.egg-info/entry_points.txt +2 -0
- datasette_agent-0.1a0/datasette_agent.egg-info/requires.txt +4 -0
- datasette_agent-0.1a0/datasette_agent.egg-info/top_level.txt +1 -0
- datasette_agent-0.1a0/pyproject.toml +48 -0
- datasette_agent-0.1a0/setup.cfg +4 -0
- datasette_agent-0.1a0/tests/test_agent.py +849 -0
- datasette_agent-0.1a0/tests/test_background_agent.py +756 -0
- datasette_agent-0.1a0/tests/test_explorer.py +805 -0
- datasette_agent-0.1a0/tests/test_export_markdown.py +260 -0
- datasette_agent-0.1a0/tests/test_filters.py +69 -0
- datasette_agent-0.1a0/tests/test_messages.py +61 -0
- datasette_agent-0.1a0/tests/test_sql_tools.py +224 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: datasette-agent
|
|
3
|
+
Version: 0.1a0
|
|
4
|
+
Summary: An LLM-powered agent assistant for Datasette
|
|
5
|
+
Author: Simon Willison
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/datasette/datasette-agent
|
|
8
|
+
Project-URL: Changelog, https://github.com/datasette/datasette-agent/releases
|
|
9
|
+
Project-URL: Issues, https://github.com/datasette/datasette-agent/issues
|
|
10
|
+
Project-URL: CI, https://github.com/datasette/datasette-agent/actions
|
|
11
|
+
Classifier: Framework :: Datasette
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: datasette>=1.0a28
|
|
16
|
+
Requires-Dist: datasette-llm>=0.1a7
|
|
17
|
+
Requires-Dist: llm>=0.32a2
|
|
18
|
+
Requires-Dist: python-ulid
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# datasette-agent
|
|
22
|
+
|
|
23
|
+
[](https://pypi.org/project/datasette-agent/)
|
|
24
|
+
[](https://github.com/datasette/datasette-agent/releases)
|
|
25
|
+
[](https://github.com/datasette/datasette-agent/actions/workflows/test.yml)
|
|
26
|
+
[](https://github.com/datasette/datasette-agent/blob/main/LICENSE)
|
|
27
|
+
|
|
28
|
+
An LLM-powered agent assistant for Datasette
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
Install this plugin in the same environment as Datasette.
|
|
33
|
+
```bash
|
|
34
|
+
datasette install datasette-agent
|
|
35
|
+
```
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
Visit `/-/agent` to start a conversation with the chat assistant.
|
|
39
|
+
|
|
40
|
+
The agent uses [datasette-llm](https://github.com/datasette/datasette-llm) to call language models. Configure a default model for it before visiting `/-/agent`, for example in `datasette.yml`:
|
|
41
|
+
|
|
42
|
+
```yaml
|
|
43
|
+
plugins:
|
|
44
|
+
datasette-llm:
|
|
45
|
+
default_model: gpt-5.4-mini
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The "Explore with AI agent" entries that appear in the database and table action menus launch a background agent that explores the selected database or table and writes a report. Reports live under `/-/agent/explore/`.
|
|
49
|
+
|
|
50
|
+
Visit `/-/agent/background` to launch background agents directly. Each one is given a goal and runs toward it without further input. The listing includes a Stop button for cancelling agents that are still running.
|
|
51
|
+
|
|
52
|
+
### Permissions
|
|
53
|
+
|
|
54
|
+
This plugin registers two independent permissions:
|
|
55
|
+
|
|
56
|
+
- `datasette-agent` — required to use the chat assistant under `/-/agent`.
|
|
57
|
+
- `datasette-agent-explore` — required to see the "Explore with AI agent" entries in the database/table action menus and to use the explorer routes under `/-/agent/explore/`.
|
|
58
|
+
|
|
59
|
+
The two permissions are independent: an actor may hold one without the other. The `--root` user has both.
|
|
60
|
+
|
|
61
|
+
## Registering additional tools from plugins
|
|
62
|
+
|
|
63
|
+
Other Datasette plugins can register additional tools for the agent using the `register_agent_tools` plugin hook.
|
|
64
|
+
|
|
65
|
+
### Defining a tool
|
|
66
|
+
|
|
67
|
+
Create a Datasette plugin that implements the `register_agent_tools` hook, returning a list of `AgentTool` instances:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from datasette import hookimpl
|
|
71
|
+
from datasette_agent.tools import AgentTool
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@hookimpl
|
|
75
|
+
def register_agent_tools(datasette):
|
|
76
|
+
return [
|
|
77
|
+
AgentTool(
|
|
78
|
+
name="my_tool",
|
|
79
|
+
description="Description of what this tool does, used by the LLM to decide when to call it.",
|
|
80
|
+
input_schema={
|
|
81
|
+
"type": "object",
|
|
82
|
+
"properties": {
|
|
83
|
+
"query": {
|
|
84
|
+
"type": "string",
|
|
85
|
+
"description": "The query to run",
|
|
86
|
+
},
|
|
87
|
+
"style": {
|
|
88
|
+
"type": "string",
|
|
89
|
+
"enum": ["brief", "detailed"],
|
|
90
|
+
"description": "Output style",
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
"required": ["query"],
|
|
94
|
+
},
|
|
95
|
+
fn=my_tool_handler,
|
|
96
|
+
),
|
|
97
|
+
]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Tool handler function
|
|
101
|
+
|
|
102
|
+
Each tool's `fn` must be an async function that accepts `datasette` and `actor` as keyword arguments, plus any parameters defined in `input_schema`. It must return a JSON string:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
import json
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
async def my_tool_handler(datasette, actor, query, style=None):
|
|
109
|
+
# Do work here...
|
|
110
|
+
return json.dumps({
|
|
111
|
+
"result": "Tool output that the LLM will see",
|
|
112
|
+
})
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
To render rich HTML inline in the chat UI, include an `_html` key in the returned JSON. Any top-level key whose name starts with `_` is removed before the tool result is sent to the LLM, so the HTML is shown to the user but not passed back to the model:
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
return json.dumps({
|
|
119
|
+
"_html": '<div class="my-widget">Rich content here</div>',
|
|
120
|
+
"summary": "Widget rendered successfully",
|
|
121
|
+
})
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Rendering custom HTML from tools
|
|
125
|
+
|
|
126
|
+
Tool plugins can render rich HTML inline in the chat UI by returning a JSON object with an `_html` key. The HTML is rendered directly in the conversation. The remaining keys are returned to the LLM as the tool result, with any key whose name starts with `_` removed first.
|
|
127
|
+
|
|
128
|
+
Example tool implementation:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
import json
|
|
132
|
+
|
|
133
|
+
async def _render_widget(datasette, actor, database, sql):
|
|
134
|
+
html = (
|
|
135
|
+
'<script src="/-/static-plugins/my-plugin/widget.js" type="module"></script>\n'
|
|
136
|
+
'<my-widget>\n'
|
|
137
|
+
f'<script type="application/json">{json.dumps({"database": database, "sql": sql})}</script>\n'
|
|
138
|
+
'</my-widget>'
|
|
139
|
+
)
|
|
140
|
+
return json.dumps({
|
|
141
|
+
"_html": html,
|
|
142
|
+
"database": database,
|
|
143
|
+
"sql": sql,
|
|
144
|
+
"summary": "Widget rendered successfully",
|
|
145
|
+
})
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
The `_html` value is inserted into the chat as raw HTML, so it can include custom elements, scripts, and styles. The other keys (`database`, `sql`, and `summary` in this example) are what the LLM receives as the tool result.
|
|
149
|
+
|
|
150
|
+
### Example plugins
|
|
151
|
+
|
|
152
|
+
- [datasette-agent-charts](https://github.com/datasette/datasette-agent-charts) - renders charts from SQL query results using Observable Plot
|
|
153
|
+
- [datasette-agent-openai-imagegen](https://github.com/datasette/datasette-agent-openai-imagegen) - generates images using OpenAI's image generation API
|
|
154
|
+
|
|
155
|
+
## CLI commands
|
|
156
|
+
|
|
157
|
+
### Interactive chat
|
|
158
|
+
|
|
159
|
+
Start an interactive chat session with the agent from the command line:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
datasette agent chat mydata.db
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
You can pass multiple database files, use `:memory:` for an in-memory database, specify a model, or send a single prompt:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
datasette agent chat mydata.db -m gpt-5.4-mini
|
|
169
|
+
datasette agent chat mydata.db -m gpt-5.4-mini -p "List all tables"
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Options:
|
|
173
|
+
|
|
174
|
+
- `-p`, `--prompt` — Send a single prompt and exit (non-interactive mode)
|
|
175
|
+
- `-m`, `--model` — LLM model to use
|
|
176
|
+
|
|
177
|
+
### Listing available tools
|
|
178
|
+
|
|
179
|
+
To see all registered agent tools, grouped by plugin:
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
datasette agent tools
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Output:
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
agent:
|
|
189
|
+
list_databases_and_tables
|
|
190
|
+
List all available databases and their tables
|
|
191
|
+
describe_table
|
|
192
|
+
Get column names, types, and foreign keys for a table
|
|
193
|
+
sql_query
|
|
194
|
+
Execute a read-only SQL query against a database
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Add `--json` for machine-readable output:
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
datasette agent tools --json
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Development
|
|
204
|
+
|
|
205
|
+
To set up this plugin locally, first checkout the code. Run the tests like this:
|
|
206
|
+
```bash
|
|
207
|
+
cd datasette-agent
|
|
208
|
+
uv run pytest
|
|
209
|
+
```
|
|
210
|
+
To run the development server with a persistent internal database and GPT-5.5 as the model:
|
|
211
|
+
```bash
|
|
212
|
+
uv run datasette --internal internal.db \
|
|
213
|
+
--root --secret 1 \
|
|
214
|
+
-s plugins.datasette-llm.default_model gpt-5.5
|
|
215
|
+
```
|
|
216
|
+
Add extra database files to that command to enable the agent to query them.
|
|
217
|
+
|
|
218
|
+
## Credits
|
|
219
|
+
|
|
220
|
+
This plugin vendors [streaming-markdown](https://github.com/thetarnav/streaming-markdown) by Damian Tarnawski, MIT licensed.
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
# datasette-agent
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/datasette-agent/)
|
|
4
|
+
[](https://github.com/datasette/datasette-agent/releases)
|
|
5
|
+
[](https://github.com/datasette/datasette-agent/actions/workflows/test.yml)
|
|
6
|
+
[](https://github.com/datasette/datasette-agent/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
An LLM-powered agent assistant for Datasette
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
Install this plugin in the same environment as Datasette.
|
|
13
|
+
```bash
|
|
14
|
+
datasette install datasette-agent
|
|
15
|
+
```
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
Visit `/-/agent` to start a conversation with the chat assistant.
|
|
19
|
+
|
|
20
|
+
The agent uses [datasette-llm](https://github.com/datasette/datasette-llm) to call language models. Configure a default model for it before visiting `/-/agent`, for example in `datasette.yml`:
|
|
21
|
+
|
|
22
|
+
```yaml
|
|
23
|
+
plugins:
|
|
24
|
+
datasette-llm:
|
|
25
|
+
default_model: gpt-5.4-mini
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The "Explore with AI agent" entries that appear in the database and table action menus launch a background agent that explores the selected database or table and writes a report. Reports live under `/-/agent/explore/`.
|
|
29
|
+
|
|
30
|
+
Visit `/-/agent/background` to launch background agents directly. Each one is given a goal and runs toward it without further input. The listing includes a Stop button for cancelling agents that are still running.
|
|
31
|
+
|
|
32
|
+
### Permissions
|
|
33
|
+
|
|
34
|
+
This plugin registers two independent permissions:
|
|
35
|
+
|
|
36
|
+
- `datasette-agent` — required to use the chat assistant under `/-/agent`.
|
|
37
|
+
- `datasette-agent-explore` — required to see the "Explore with AI agent" entries in the database/table action menus and to use the explorer routes under `/-/agent/explore/`.
|
|
38
|
+
|
|
39
|
+
The two permissions are independent: an actor may hold one without the other. The `--root` user has both.
|
|
40
|
+
|
|
41
|
+
## Registering additional tools from plugins
|
|
42
|
+
|
|
43
|
+
Other Datasette plugins can register additional tools for the agent using the `register_agent_tools` plugin hook.
|
|
44
|
+
|
|
45
|
+
### Defining a tool
|
|
46
|
+
|
|
47
|
+
Create a Datasette plugin that implements the `register_agent_tools` hook, returning a list of `AgentTool` instances:
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from datasette import hookimpl
|
|
51
|
+
from datasette_agent.tools import AgentTool
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@hookimpl
|
|
55
|
+
def register_agent_tools(datasette):
|
|
56
|
+
return [
|
|
57
|
+
AgentTool(
|
|
58
|
+
name="my_tool",
|
|
59
|
+
description="Description of what this tool does, used by the LLM to decide when to call it.",
|
|
60
|
+
input_schema={
|
|
61
|
+
"type": "object",
|
|
62
|
+
"properties": {
|
|
63
|
+
"query": {
|
|
64
|
+
"type": "string",
|
|
65
|
+
"description": "The query to run",
|
|
66
|
+
},
|
|
67
|
+
"style": {
|
|
68
|
+
"type": "string",
|
|
69
|
+
"enum": ["brief", "detailed"],
|
|
70
|
+
"description": "Output style",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
"required": ["query"],
|
|
74
|
+
},
|
|
75
|
+
fn=my_tool_handler,
|
|
76
|
+
),
|
|
77
|
+
]
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Tool handler function
|
|
81
|
+
|
|
82
|
+
Each tool's `fn` must be an async function that accepts `datasette` and `actor` as keyword arguments, plus any parameters defined in `input_schema`. It must return a JSON string:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
import json
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
async def my_tool_handler(datasette, actor, query, style=None):
|
|
89
|
+
# Do work here...
|
|
90
|
+
return json.dumps({
|
|
91
|
+
"result": "Tool output that the LLM will see",
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
To render rich HTML inline in the chat UI, include an `_html` key in the returned JSON. Any top-level key whose name starts with `_` is removed before the tool result is sent to the LLM, so the HTML is shown to the user but not passed back to the model:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
return json.dumps({
|
|
99
|
+
"_html": '<div class="my-widget">Rich content here</div>',
|
|
100
|
+
"summary": "Widget rendered successfully",
|
|
101
|
+
})
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Rendering custom HTML from tools
|
|
105
|
+
|
|
106
|
+
Tool plugins can render rich HTML inline in the chat UI by returning a JSON object with an `_html` key. The HTML is rendered directly in the conversation. The remaining keys are returned to the LLM as the tool result, with any key whose name starts with `_` removed first.
|
|
107
|
+
|
|
108
|
+
Example tool implementation:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
import json
|
|
112
|
+
|
|
113
|
+
async def _render_widget(datasette, actor, database, sql):
|
|
114
|
+
html = (
|
|
115
|
+
'<script src="/-/static-plugins/my-plugin/widget.js" type="module"></script>\n'
|
|
116
|
+
'<my-widget>\n'
|
|
117
|
+
f'<script type="application/json">{json.dumps({"database": database, "sql": sql})}</script>\n'
|
|
118
|
+
'</my-widget>'
|
|
119
|
+
)
|
|
120
|
+
return json.dumps({
|
|
121
|
+
"_html": html,
|
|
122
|
+
"database": database,
|
|
123
|
+
"sql": sql,
|
|
124
|
+
"summary": "Widget rendered successfully",
|
|
125
|
+
})
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The `_html` value is inserted into the chat as raw HTML, so it can include custom elements, scripts, and styles. The other keys (`database`, `sql`, and `summary` in this example) are what the LLM receives as the tool result.
|
|
129
|
+
|
|
130
|
+
### Example plugins
|
|
131
|
+
|
|
132
|
+
- [datasette-agent-charts](https://github.com/datasette/datasette-agent-charts) - renders charts from SQL query results using Observable Plot
|
|
133
|
+
- [datasette-agent-openai-imagegen](https://github.com/datasette/datasette-agent-openai-imagegen) - generates images using OpenAI's image generation API
|
|
134
|
+
|
|
135
|
+
## CLI commands
|
|
136
|
+
|
|
137
|
+
### Interactive chat
|
|
138
|
+
|
|
139
|
+
Start an interactive chat session with the agent from the command line:
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
datasette agent chat mydata.db
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
You can pass multiple database files, use `:memory:` for an in-memory database, specify a model, or send a single prompt:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
datasette agent chat mydata.db -m gpt-5.4-mini
|
|
149
|
+
datasette agent chat mydata.db -m gpt-5.4-mini -p "List all tables"
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Options:
|
|
153
|
+
|
|
154
|
+
- `-p`, `--prompt` — Send a single prompt and exit (non-interactive mode)
|
|
155
|
+
- `-m`, `--model` — LLM model to use
|
|
156
|
+
|
|
157
|
+
### Listing available tools
|
|
158
|
+
|
|
159
|
+
To see all registered agent tools, grouped by plugin:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
datasette agent tools
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Output:
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
agent:
|
|
169
|
+
list_databases_and_tables
|
|
170
|
+
List all available databases and their tables
|
|
171
|
+
describe_table
|
|
172
|
+
Get column names, types, and foreign keys for a table
|
|
173
|
+
sql_query
|
|
174
|
+
Execute a read-only SQL query against a database
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Add `--json` for machine-readable output:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
datasette agent tools --json
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Development
|
|
184
|
+
|
|
185
|
+
To set up this plugin locally, first checkout the code. Run the tests like this:
|
|
186
|
+
```bash
|
|
187
|
+
cd datasette-agent
|
|
188
|
+
uv run pytest
|
|
189
|
+
```
|
|
190
|
+
To run the development server with a persistent internal database and GPT-5.5 as the model:
|
|
191
|
+
```bash
|
|
192
|
+
uv run datasette --internal internal.db \
|
|
193
|
+
--root --secret 1 \
|
|
194
|
+
-s plugins.datasette-llm.default_model gpt-5.5
|
|
195
|
+
```
|
|
196
|
+
Add extra database files to that command to enable the agent to query them.
|
|
197
|
+
|
|
198
|
+
## Credits
|
|
199
|
+
|
|
200
|
+
This plugin vendors [streaming-markdown](https://github.com/thetarnav/streaming-markdown) by Damian Tarnawski, MIT licensed.
|