llm-tool-jina 0.1.0__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.
@@ -0,0 +1,12 @@
1
+ .env
2
+ .venv/
3
+ venv/
4
+ __pycache__/
5
+ *.py[cod]
6
+ .pytest_cache/
7
+ *.egg-info/
8
+ dist/
9
+ build/
10
+ .DS_Store
11
+ tmp/
12
+ uv.lock
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) [year] [fullname]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,351 @@
1
+ Metadata-Version: 2.5
2
+ Name: llm-tool-jina
3
+ Version: 0.1.0
4
+ Summary: Jina Reader tools (read a URL, search the web) for Simon Willison's LLM
5
+ Project-URL: Homepage, https://github.com/rdslw/llm-tool-jina
6
+ Project-URL: Changelog, https://github.com/rdslw/llm-tool-jina/releases
7
+ Project-URL: Issues, https://github.com/rdslw/llm-tool-jina/issues
8
+ Project-URL: CI, https://github.com/rdslw/llm-tool-jina/actions
9
+ Author: Radoslaw Stachowiak
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: jina,llm,llm-plugin,reader,scraping,web-search
13
+ Requires-Python: >=3.11
14
+ Requires-Dist: llm>=0.32.1
15
+ Description-Content-Type: text/markdown
16
+
17
+ # llm-tool-jina
18
+
19
+ [Jina Reader](https://github.com/jina-ai/reader) tools for
20
+ [Simon Willison's LLM](https://llm.datasette.io/):
21
+
22
+ - `read` (on by default): reads a web page or PDF through `r.jina.ai` and
23
+ returns its main text as markdown, in parts of a fixed token size.
24
+ - `search` (off by default): searches the web through `s.jina.ai` and returns
25
+ title, URL and description of each result.
26
+
27
+ The plugin calls the Jina HTTP API with the Python standard library. It has
28
+ no dependency other than `llm`.
29
+
30
+ ## Install
31
+
32
+ ```bash
33
+ llm install llm-tool-jina
34
+ llm tools list | grep Jina
35
+ ```
36
+
37
+ From a local clone: `llm install -e .`
38
+
39
+ ## API key (optional for read)
40
+
41
+ ```bash
42
+ llm keys set jina # or: export JINA_API_KEY=...
43
+ ```
44
+
45
+ Lookup order: `api_key` argument (Python API), `llm keys` alias `jina`,
46
+ environment variable `JINA_API_KEY`.
47
+
48
+ | | Without a key | With a key |
49
+ | --- | --- | --- |
50
+ | `read` | works, lowest rate limit (20 requests/min on 2026-11-11) | 500 requests/min on 2026-11-11; uses tokens of your Jina balance |
51
+ | `search` | refused by Jina (HTTP 401) | works; minimum charge 10000 tokens per search on 2026-11-11 |
52
+
53
+ Jina can change these values. The response header `x-ratelimit-limit` gives
54
+ the current limit.
55
+
56
+ Do not put a key in the `-T` argument (`-T 'Jina(api_key="...")'`). llm 0.32+
57
+ writes toolbox arguments to the logs database. The plugin removes `api_key`
58
+ from that record, so a conversation that continues with `llm -c` must find the
59
+ key with `llm keys` or `JINA_API_KEY`.
60
+
61
+ ## Usage
62
+
63
+ ```bash
64
+ # read only (default); --td shows the tool calls
65
+ llm -T Jina --td 'Read https://docs.python.org/3/library/pathlib.html and list the new 3.13 APIs.'
66
+
67
+ # read and search
68
+ llm -T 'Jina("read,search")' 'Find the uv release notes for the latest version and summarize them.'
69
+
70
+ # settings: keyword arguments with JSON values
71
+ llm -T 'Jina(tools="read", allowed_domains=["python.org"], max_tokens=6000)' '...'
72
+
73
+ # approve each tool call by hand
74
+ llm -T Jina --ta '...'
75
+ ```
76
+
77
+ The llm spec parser accepts one positional JSON value (`Jina("read,search")`)
78
+ or keyword arguments (`Jina(tools="read,search", no_cache=true)`), not both.
79
+
80
+ Tool names are `Jina_read` and `Jina_search`. llm 0.32+ uses the prefix
81
+ `Jina_` to find the toolbox again when you continue with `llm -c`, so the
82
+ class name contains no `_`.
83
+
84
+ ## Tools
85
+
86
+ ### `read(url, offset=0)`
87
+
88
+ Returns:
89
+
90
+ ```json
91
+ {
92
+ "url": "https://example.com/final-url-after-redirects",
93
+ "title": "Page title",
94
+ "content": "<<<JINA_UNTRUSTED_CONTENT id=\"...\" source=\"jina_reader\">>>\n...\n<<<END_JINA_UNTRUSTED_CONTENT id=\"...\">>>",
95
+ "truncated": true,
96
+ "next_offset": 38211,
97
+ "total_tokens": 50141,
98
+ "security_notice": "..."
99
+ }
100
+ ```
101
+
102
+ Optional fields: `http_status` (when the target page did not return 200) and
103
+ `warning` (a Jina warning, or a note that `max_page_tokens` cut the page).
104
+
105
+ Reading in parts: a full page can have more than 100k tokens (the Wikipedia
106
+ page "Python (programming language)": 50141 tokens in the default format). The
107
+ plugin gets the page once, keeps it in memory (last 8 pages, for the life of
108
+ the toolbox) and returns parts of about `max_tokens` tokens. When `truncated`
109
+ is true, the model calls `read` again with `offset=next_offset`.
110
+
111
+ - `offset` is a character position in the page text. Parts end at a paragraph,
112
+ line or word end when possible.
113
+ - The plugin converts `max_tokens` to characters with the
114
+ characters-per-token value of the page (from Jina's token count), so the
115
+ part size is correct also for CJK text or code.
116
+ - Jina's `X-Max-Tokens` header cannot do this: it cuts the page and has no
117
+ offset. The plugin sends it only as `max_page_tokens`, the limit for the full
118
+ page.
119
+ - With `llm -c` a new process starts with an empty cache. The next part then
120
+ gets the page again; if the page changed, the offsets can move.
121
+
122
+ ### `search(query, count=5)`
123
+
124
+ Returns `{"results": [{"title", "url", "description", "date"?}], "security_notice"}`.
125
+
126
+ The plugin sends `X-Respond-With: no-content`. Without it, Jina reads the
127
+ full page of each result: on 2026-11-11 two results gave 31691 tokens. The
128
+ model reads the results that it needs with `read`. `count` is 1-20. The query
129
+ accepts operators such as `site:example.com`.
130
+
131
+ ## Toolbox settings
132
+
133
+ User and environment settings are constructor arguments. Tool schemas go to
134
+ the model with each request, so they contain only per-query parameters
135
+ (`url`, `offset`, `query`, `count`). Constructor settings cost no prompt
136
+ tokens, and the model (or text injected into a page) cannot change them.
137
+
138
+ | Setting | Default | Effect |
139
+ | --- | --- | --- |
140
+ | `tools` | `"read"` | `"read"`, `"search"`, `"read,search"` or `"all"` |
141
+ | `max_tokens` | `15000` | approximate size of one `read` part (from 500) |
142
+ | `max_page_tokens` | `200000` | Jina cuts a page at this size (`X-Max-Tokens`); limits the tokens that one read uses |
143
+ | `allowed_domains` | none | read only these domains and their subdomains; search returns only their results |
144
+ | `blocked_domains` | none | never read or return these domains |
145
+ | `no_store` | `true` | `DNT: 1`: Jina does not write the page to its cache |
146
+ | `no_cache` | `false` | `X-No-Cache: true`: Jina does not use a cached copy (always a live fetch) |
147
+ | `page_timeout` | `15` | `X-Timeout` for `read`: seconds that Reader may wait for the page (1-180; `null`: no header) |
148
+ | `timeout` | `60` | HTTP timeout in seconds; the plugin raises it to at least `page_timeout + 30` |
149
+ | `reader_url` | `https://r.jina.ai/` | for example a self-hosted Reader |
150
+ | `search_url` | `https://s.jina.ai/` | |
151
+ | `api_key` | none | Python API only; see above |
152
+
153
+ Domains are host names (`docs.python.org`, `*.example.com` and `example.com`
154
+ are the same). Give a list (`["a.com", "b.org"]`) or a comma-separated string.
155
+
156
+ ### Timeouts
157
+
158
+ `page_timeout` controls how long Reader waits for a page (source:
159
+ `src/dto/crawler-options.ts`, `src/services/puppeteer.ts`):
160
+
161
+ - Below 20 (default 15): Reader returns as soon as the page content is loaded
162
+ ("resource idle"); the browser navigation limit is `page_timeout` (Reader
163
+ default without the header: 15 s).
164
+ - From 20: Reader waits for full network idle, or until `page_timeout`. Use
165
+ this for pages that load their content late. Pages with constant network
166
+ activity (ads, analytics) then take the full time.
167
+
168
+ Measured on 2026-11-11 without cache: theverge.com 10.9 s (no header), 4.8 s
169
+ (15), 24.3 s (20); react.dev/learn 3.9 / 3.1 / 5.1 s; a Wikipedia page
170
+ 2.4 / 2.0 / 5.2 s. The content was the same in all three modes. Reader can
171
+ exceed `page_timeout`, so the plugin keeps its HTTP timeout at least 30 s
172
+ above it. Search does not send `X-Timeout`: with `no-content`, Reader does not
173
+ use it.
174
+
175
+ ## Security
176
+
177
+ ### Untrusted content
178
+
179
+ A full page is the strongest prompt-injection channel. The plugin:
180
+
181
+ - puts the page text between `JINA_UNTRUSTED_CONTENT` markers with a random
182
+ id for each call, and adds one `security_notice` field;
183
+ - replaces marker strings in the page (of any vendor, also with zero-width
184
+ separators) with `[JINA_UNTRUSTED_MARKER_SANITIZED]`;
185
+ - replaces model special tokens (`<|im_start|>`, `<|eot_id|>`, `<|User|>`,
186
+ `[INST]`, ...) with `[LLM_SPECIAL_TOKEN_SANITIZED]`;
187
+ - removes Unicode tag characters (U+E0000-U+E007F), which can hold invisible
188
+ instructions;
189
+ - sanitizes and shortens title, URL, description and warning to one line.
190
+
191
+ This is a prompt-level guard, not a security boundary.
192
+
193
+ ### Data exfiltration
194
+
195
+ With a fetch tool, injected text can tell the model to read
196
+ `https://attacker.example/?d=<private data>`. Jina then requests that URL,
197
+ and the attacker gets the data. Controls:
198
+
199
+ 1. `allowed_domains` (the strong control). The plugin refuses other hosts
200
+ before it sends a request. Search sends the domains as `site:` filters and
201
+ removes other results. If Jina follows a redirect to a host outside the
202
+ policy, the plugin withholds the content (but Jina already requested that
203
+ URL: an open redirect on an allowed domain can still leak data).
204
+ 2. `blocked_domains` is a filter for noise, not an exfiltration control: an
205
+ attacker can use any other domain.
206
+ 3. Approval: `llm --ta` (CLI) or `before_call=` (Python API) shows each tool
207
+ call with its URL and waits for your approval.
208
+ 4. Other URL checks: only `http`/`https`; no user name or password in the URL
209
+ (Jina would receive it); no spaces, control characters or backslashes
210
+ (they let URL parsers disagree); legacy IP forms such as `2130706433` are
211
+ converted to `127.0.0.1` before the check. Without `allowed_domains`,
212
+ `localhost`, private, loopback and link-local addresses and `.local`,
213
+ `.lan`, `.internal` names are refused. This is important for a self-hosted
214
+ Reader in your network. The plugin checks the URL and then sends the same,
215
+ normalized URL.
216
+
217
+ Why no `llm.PauseChain` approval: in the llm CLI (0.37.1) a `PauseChain`
218
+ from a tool stops the command with `Error: ...`, and the CLI cannot resume the
219
+ paused call (`llm -c` with a new prompt does not run it again). `PauseChain`
220
+ is for Python hosts that store and resume message histories; such hosts can
221
+ use `before_call` for approval with no plugin support.
222
+
223
+ ### Errors
224
+
225
+ Errors are exceptions (`JinaError`). llm sends `Error: <message>` to the
226
+ model, records the exception in the logs database and shows it with `--td`.
227
+ An error returned as a dict looks like a successful call in both places.
228
+
229
+ ## Privacy
230
+
231
+ - Jina receives each URL and search query. With a key, Jina can link them to
232
+ your account; without a key, only to your IP address.
233
+ - `no_store=true` (default) sends `DNT: 1`. In the Reader source
234
+ (`src/api/crawler.ts`), `DNT` sets `private`, and a private snapshot is not
235
+ written to the page cache (default lifetime 3600 s). Jina can still log the
236
+ request.
237
+ - `no_cache=true` only stops the use of a cached copy (`X-No-Cache`); it does
238
+ not stop the cache write.
239
+ - The plugin never sends or stores cookies (no cookie handler, no
240
+ `X-Set-Cookie`) and does not follow redirects of the API endpoint.
241
+ - The API key goes only to `https://*.jina.ai`. A self-hosted Reader
242
+ (`reader_url="http://localhost:3000"`, image `ghcr.io/jina-ai/reader:oss`)
243
+ never receives it and keeps all URLs on your machine.
244
+ - The User-Agent is `llm-tool-jina/<version>`.
245
+
246
+ ## Python API
247
+
248
+ ```python
249
+ import llm
250
+ from llm_tool_jina import Jina
251
+
252
+ model = llm.get_model("gpt-5.4-mini")
253
+ response = model.chain(
254
+ "Read https://peps.python.org/pep-0008/ and summarize the naming rules.",
255
+ tools=[Jina(allowed_domains=["python.org"])],
256
+ )
257
+ print(response.text())
258
+ ```
259
+
260
+ ## Design decisions (compared with llm-tool-brave)
261
+
262
+ | llm-tool-brave rule | Here | Reason |
263
+ | --- | --- | --- |
264
+ | Per-query parameters only; settings on the constructor | kept | prompt tokens; also the model cannot change security settings |
265
+ | `<Class>_` tool prefix | kept, via `super().tools()` and a filter | no copy of llm internals |
266
+ | key from `llm keys` (two aliases) or env; `api_key` removed from `_config` | kept, one alias `jina` | key is also never sent to a non-Jina endpoint |
267
+ | `BRAVE_UNTRUSTED_CONTENT` markers on each snippet | markers only on page `content`; short fields sanitized and shortened; generic marker and special-token patterns; tag characters removed | JSON already delimits short fields; markers on each snippet cost tokens |
268
+ | marker id from `tool_call_id` (Brave TODO) | random id | a page author cannot predict a random id; the log already links results to calls |
269
+ | errors as dicts | exceptions | llm logs exceptions and `--td` shows them |
270
+ | `urllib.request.urlopen` | private opener: no cookies, no redirects, response size limit | an API redirect would send the `Authorization` header to another host |
271
+ | entry point named after the service | `tool_jina` | `llm-jina` already uses `jina`; pluggy skips a second plugin with the same name |
272
+
273
+ ## Development
274
+
275
+ ```bash
276
+ uv sync --all-groups
277
+ uv run pytest
278
+ uv run llm tools list | grep Jina
279
+ ```
280
+
281
+ In a sandbox where `~/.cache/uv` is read-only:
282
+ `UV_CACHE_DIR="$TMPDIR/uv-cache" UV_LINK_MODE=copy uv run pytest`.
283
+
284
+ Live tests (real requests to Jina; read is anonymous, search uses
285
+ `JINA_API_KEY` if set):
286
+
287
+ ```bash
288
+ JINA_LIVE_TESTS=1 uv run pytest -m live -v
289
+ ```
290
+
291
+ `uv.lock` is not committed: this is a library, and CI resolves the newest
292
+ allowed versions on each run. `[tool.uv] exclude-newer = "34 hours"` keeps
293
+ new package releases out of all resolutions (supply-chain guard).
294
+
295
+ ## Releasing
296
+
297
+ CI (`.github/workflows/test.yml`): tests on Python 3.11-3.15 with the newest
298
+ dependencies, and a test with the lowest direct dependencies (`llm` 0.32.1)
299
+ on Python 3.11. Both test the built wheel, not the source tree. A monthly run
300
+ finds new `llm` releases that break the plugin.
301
+
302
+ A pushed annotated tag `vX.Y.Z` starts `.github/workflows/publish.yml`:
303
+
304
+ 1. `tag-check`: the tag is annotated, has the form `vX.Y.Z`, equals
305
+ `v` + the `pyproject.toml` version, and its commit is on `master`.
306
+ 2. Tests (same jobs as CI).
307
+ 3. `publish`: `uv build`, check of the wheel name, PyPI Trusted Publishing.
308
+ 4. `release`: GitHub release with the workflow token. Notes: body of the tag
309
+ message, subjects of the commits since the previous tag, compare link.
310
+
311
+ No local `gh` or GitHub token with write access is necessary: only `git push`.
312
+ The release must stay in `publish.yml`: a release made with the workflow token
313
+ starts no other workflow.
314
+
315
+ Procedure:
316
+
317
+ 1. Finish and push the feature and fix commits; CI must pass.
318
+ 2. Test with the newest allowed dependencies: `uv lock --upgrade`, then
319
+ `uv run pytest`.
320
+ 3. Set `version` in `pyproject.toml` (`X.Y.Z`, no `v`). PyPI versions are
321
+ immutable.
322
+ 4. Commit and tag (the second `-m` is the release notes):
323
+
324
+ ```bash
325
+ git add pyproject.toml
326
+ git commit -m "Release v0.1.0"
327
+ git tag -a v0.1.0 -m "Release v0.1.0" -m "First release: read and search tools."
328
+ git push --follow-tags
329
+ ```
330
+
331
+ `--follow-tags` pushes the branch and its new annotated tags with one fixed
332
+ command. It skips lightweight tags, and `tag-check` refuses them.
333
+
334
+ If the workflow fails before the PyPI upload, delete the tag, fix, tag again:
335
+
336
+ ```bash
337
+ git push origin :refs/tags/v0.1.0
338
+ git tag -d v0.1.0
339
+ ```
340
+
341
+ If only the `release` job fails, run it again in GitHub Actions.
342
+
343
+ One-time setup:
344
+
345
+ 1. GitHub repository `rdslw/llm-tool-jina`, default branch `master`. First
346
+ push: `git push -u origin master` (sets the upstream that
347
+ `git push --follow-tags` needs).
348
+ 2. GitHub environment `pypi` (Settings, Environments).
349
+ 3. PyPI pending trusted publisher: project `llm-tool-jina`, owner `rdslw`,
350
+ repository `llm-tool-jina`, workflow `publish.yml`, environment `pypi`.
351
+ Do not rename `publish.yml`: the trusted publisher is bound to the name.