wigolo 0.2.0__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.
wigolo/__init__.py ADDED
@@ -0,0 +1,26 @@
1
+ """wigolo — thin Python client for the local-first web intelligence REST API.
2
+
3
+ Exposes a synchronous ``Client``, an ``AsyncClient``, and ``local_client``
4
+ for zero-setup embedded use. This is a thin transport: no retries, no
5
+ re-ranking, no interpretation, no caching — the wigolo server does all of
6
+ that.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from ._aio import AsyncClient
12
+ from ._client import Client
13
+ from ._errors import WigoloAPIError, WigoloConnectionError, WigoloError
14
+ from ._local import local_client
15
+
16
+ __version__ = "0.1.0"
17
+
18
+ __all__ = [
19
+ "Client",
20
+ "AsyncClient",
21
+ "local_client",
22
+ "WigoloError",
23
+ "WigoloAPIError",
24
+ "WigoloConnectionError",
25
+ "__version__",
26
+ ]
wigolo/_aio.py ADDED
@@ -0,0 +1,446 @@
1
+ """Asynchronous wigolo REST client.
2
+
3
+ Identical public surface to the synchronous ``Client``, but every method
4
+ is awaitable. Implementation runs the synchronous request function on a
5
+ client-owned bounded thread pool via ``loop.run_in_executor``.
6
+
7
+ Cancellation caveat: awaiting a call and then cancelling ABANDONS the
8
+ in-flight request — the worker thread continues to completion in the
9
+ background (there is no portable way to abort a blocking socket op). The
10
+ ``await`` returns promptly on cancel, but the thread lingers. Concurrency
11
+ is bounded by ``max_workers``.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import asyncio
17
+ from concurrent.futures import ThreadPoolExecutor
18
+ from typing import Any, Optional, Union
19
+
20
+ from ._client import Client
21
+
22
+ __all__ = ["AsyncClient"]
23
+
24
+
25
+ class AsyncClient:
26
+ """Async client for the wigolo REST API.
27
+
28
+ Wraps a synchronous ``Client`` and dispatches each blocking request to
29
+ a bounded ``ThreadPoolExecutor`` (``max_workers``, default 16).
30
+
31
+ Args match ``Client`` (including the local-mode ``port`` / ``command``
32
+ overrides) plus ``max_workers`` for the executor bound.
33
+
34
+ Note: when ``local=True`` (or ``WIGOLO_LOCAL=1``), the daemon
35
+ probe-or-spawn runs SYNCHRONOUSLY inside this constructor (it may block
36
+ the calling thread up to ~20s while the daemon becomes healthy). This
37
+ mirrors the synchronous ``Client`` and keeps construction simple; if you
38
+ need it fully off-loop, construct the ``AsyncClient`` in a worker thread.
39
+ """
40
+
41
+ def __init__(
42
+ self,
43
+ base_url: Optional[str] = None,
44
+ token: Optional[str] = None,
45
+ timeout: Optional[float] = None,
46
+ local: Optional[bool] = None,
47
+ max_workers: int = 16,
48
+ *,
49
+ port: Optional[int] = None,
50
+ command: Optional[list[str]] = None,
51
+ ) -> None:
52
+ self._client = Client(
53
+ base_url=base_url,
54
+ token=token,
55
+ timeout=timeout,
56
+ local=local,
57
+ port=port,
58
+ command=command,
59
+ )
60
+ self._executor = ThreadPoolExecutor(max_workers=max_workers)
61
+
62
+ # ---- lifecycle -------------------------------------------------------
63
+
64
+ def close(self) -> None:
65
+ """Synchronously shut down the executor and stop an owned daemon."""
66
+ self._executor.shutdown(wait=True)
67
+ self._client.close()
68
+
69
+ async def aclose(self) -> None:
70
+ """Shut down the executor (off the event loop) and stop an owned daemon."""
71
+ loop = asyncio.get_running_loop()
72
+ await loop.run_in_executor(None, self._executor.shutdown, True)
73
+ self._client.close()
74
+
75
+ async def __aenter__(self) -> "AsyncClient":
76
+ return self
77
+
78
+ async def __aexit__(self, *exc: object) -> None:
79
+ await self.aclose()
80
+
81
+ # ---- dispatch --------------------------------------------------------
82
+
83
+ async def _run(self, fn_name: str, /, **kwargs: Any) -> Any:
84
+ loop = asyncio.get_running_loop()
85
+ fn = getattr(self._client, fn_name)
86
+
87
+ def _call() -> Any:
88
+ return fn(**kwargs)
89
+
90
+ return await loop.run_in_executor(self._executor, _call)
91
+
92
+ # ---- meta endpoints --------------------------------------------------
93
+
94
+ async def health(self, *, timeout: Optional[float] = None) -> Any:
95
+ """GET /health. The ``searxng`` field is the search-aggregator sidecar status."""
96
+ return await self._run("health", timeout=timeout)
97
+
98
+ async def list_tools(self, *, timeout: Optional[float] = None) -> Any:
99
+ """GET /v1/tools (bearer-gated in token mode)."""
100
+ return await self._run("list_tools", timeout=timeout)
101
+
102
+ async def openapi(self, *, timeout: Optional[float] = None) -> Any:
103
+ """GET /openapi.json (bearer-gated in token mode)."""
104
+ return await self._run("openapi", timeout=timeout)
105
+
106
+ # ---- tools -----------------------------------------------------------
107
+
108
+ async def search(
109
+ self,
110
+ *,
111
+ query: Union[str, list[str]],
112
+ max_results: Optional[int] = None,
113
+ max_fetches: Optional[int] = None,
114
+ include_content: Optional[bool] = None,
115
+ content_max_chars: Optional[int] = None,
116
+ max_content_chars: Optional[int] = None,
117
+ max_total_chars: Optional[int] = None,
118
+ time_range: Optional[str] = None,
119
+ exact_match: Optional[bool] = None,
120
+ search_engines: Optional[list[str]] = None,
121
+ language: Optional[str] = None,
122
+ country: Optional[str] = None,
123
+ include_domains: Optional[list[str]] = None,
124
+ exclude_domains: Optional[list[str]] = None,
125
+ from_date: Optional[str] = None,
126
+ to_date: Optional[str] = None,
127
+ category: Optional[str] = None,
128
+ format: Optional[str] = None,
129
+ max_highlights: Optional[int] = None,
130
+ force_refresh: Optional[bool] = None,
131
+ include_favicon: Optional[bool] = None,
132
+ include_images: Optional[bool] = None,
133
+ max_tokens_out: Optional[int] = None,
134
+ include_full_markdown: Optional[bool] = None,
135
+ citation_format: Optional[str] = None,
136
+ mode: Optional[str] = None,
137
+ search_depth: Optional[str] = None,
138
+ agent_context: Optional[Any] = None,
139
+ timeout: Optional[float] = None,
140
+ ) -> Any:
141
+ """Search the web. Returns scored evidence + citations."""
142
+ return await self._run(
143
+ "search",
144
+ query=query,
145
+ max_results=max_results,
146
+ max_fetches=max_fetches,
147
+ include_content=include_content,
148
+ content_max_chars=content_max_chars,
149
+ max_content_chars=max_content_chars,
150
+ max_total_chars=max_total_chars,
151
+ time_range=time_range,
152
+ exact_match=exact_match,
153
+ search_engines=search_engines,
154
+ language=language,
155
+ country=country,
156
+ include_domains=include_domains,
157
+ exclude_domains=exclude_domains,
158
+ from_date=from_date,
159
+ to_date=to_date,
160
+ category=category,
161
+ format=format,
162
+ max_highlights=max_highlights,
163
+ force_refresh=force_refresh,
164
+ include_favicon=include_favicon,
165
+ include_images=include_images,
166
+ max_tokens_out=max_tokens_out,
167
+ include_full_markdown=include_full_markdown,
168
+ citation_format=citation_format,
169
+ mode=mode,
170
+ search_depth=search_depth,
171
+ agent_context=agent_context,
172
+ timeout=timeout,
173
+ )
174
+
175
+ async def fetch(
176
+ self,
177
+ *,
178
+ url: str,
179
+ render_js: Optional[bool] = None,
180
+ use_auth: Optional[bool] = None,
181
+ max_chars: Optional[int] = None,
182
+ max_content_chars: Optional[int] = None,
183
+ section: Optional[str] = None,
184
+ section_index: Optional[int] = None,
185
+ screenshot: Optional[bool] = None,
186
+ headers: Optional[dict[str, str]] = None,
187
+ force_refresh: Optional[bool] = None,
188
+ max_tokens_out: Optional[int] = None,
189
+ include_full_markdown: Optional[bool] = None,
190
+ citation_format: Optional[str] = None,
191
+ actions: Optional[list[Any]] = None,
192
+ mode: Optional[str] = None,
193
+ timeout: Optional[float] = None,
194
+ ) -> Any:
195
+ """Fetch a single URL and return clean markdown."""
196
+ return await self._run(
197
+ "fetch",
198
+ url=url,
199
+ render_js=render_js,
200
+ use_auth=use_auth,
201
+ max_chars=max_chars,
202
+ max_content_chars=max_content_chars,
203
+ section=section,
204
+ section_index=section_index,
205
+ screenshot=screenshot,
206
+ headers=headers,
207
+ force_refresh=force_refresh,
208
+ max_tokens_out=max_tokens_out,
209
+ include_full_markdown=include_full_markdown,
210
+ citation_format=citation_format,
211
+ actions=actions,
212
+ mode=mode,
213
+ timeout=timeout,
214
+ )
215
+
216
+ async def crawl(
217
+ self,
218
+ *,
219
+ url: str,
220
+ max_depth: Optional[int] = None,
221
+ max_pages: Optional[int] = None,
222
+ strategy: Optional[str] = None,
223
+ include_patterns: Optional[list[str]] = None,
224
+ exclude_patterns: Optional[list[str]] = None,
225
+ use_auth: Optional[bool] = None,
226
+ extract_links: Optional[bool] = None,
227
+ max_total_chars: Optional[int] = None,
228
+ max_tokens_out: Optional[int] = None,
229
+ include_full_markdown: Optional[bool] = None,
230
+ citation_format: Optional[str] = None,
231
+ timeout: Optional[float] = None,
232
+ ) -> Any:
233
+ """Crawl a site from a seed URL. ``strategy='map'`` returns ``urls``."""
234
+ return await self._run(
235
+ "crawl",
236
+ url=url,
237
+ max_depth=max_depth,
238
+ max_pages=max_pages,
239
+ strategy=strategy,
240
+ include_patterns=include_patterns,
241
+ exclude_patterns=exclude_patterns,
242
+ use_auth=use_auth,
243
+ extract_links=extract_links,
244
+ max_total_chars=max_total_chars,
245
+ max_tokens_out=max_tokens_out,
246
+ include_full_markdown=include_full_markdown,
247
+ citation_format=citation_format,
248
+ timeout=timeout,
249
+ )
250
+
251
+ async def cache(
252
+ self,
253
+ *,
254
+ query: Optional[str] = None,
255
+ url_pattern: Optional[str] = None,
256
+ since: Optional[str] = None,
257
+ clear: Optional[bool] = None,
258
+ stats: Optional[bool] = None,
259
+ check_changes: Optional[bool] = None,
260
+ mode: Optional[str] = None,
261
+ limit: Optional[int] = None,
262
+ max_tokens_out: Optional[int] = None,
263
+ timeout: Optional[float] = None,
264
+ ) -> Any:
265
+ """Query or manage the local knowledge cache."""
266
+ return await self._run(
267
+ "cache",
268
+ query=query,
269
+ url_pattern=url_pattern,
270
+ since=since,
271
+ clear=clear,
272
+ stats=stats,
273
+ check_changes=check_changes,
274
+ mode=mode,
275
+ limit=limit,
276
+ max_tokens_out=max_tokens_out,
277
+ timeout=timeout,
278
+ )
279
+
280
+ async def extract(
281
+ self,
282
+ *,
283
+ url: Optional[str] = None,
284
+ html: Optional[str] = None,
285
+ mode: Optional[str] = None,
286
+ css_selector: Optional[str] = None,
287
+ multiple: Optional[bool] = None,
288
+ schema: Optional[Any] = None,
289
+ named_schema: Optional[str] = None,
290
+ max_tokens_out: Optional[int] = None,
291
+ timeout: Optional[float] = None,
292
+ ) -> Any:
293
+ """Extract structured data from a URL or raw HTML."""
294
+ return await self._run(
295
+ "extract",
296
+ url=url,
297
+ html=html,
298
+ mode=mode,
299
+ css_selector=css_selector,
300
+ multiple=multiple,
301
+ schema=schema,
302
+ named_schema=named_schema,
303
+ max_tokens_out=max_tokens_out,
304
+ timeout=timeout,
305
+ )
306
+
307
+ async def find_similar(
308
+ self,
309
+ *,
310
+ url: Optional[str] = None,
311
+ concept: Optional[str] = None,
312
+ max_results: Optional[int] = None,
313
+ include_domains: Optional[list[str]] = None,
314
+ exclude_domains: Optional[list[str]] = None,
315
+ include_cache: Optional[bool] = None,
316
+ include_web: Optional[bool] = None,
317
+ mode: Optional[str] = None,
318
+ max_tokens_out: Optional[int] = None,
319
+ include_full_markdown: Optional[bool] = None,
320
+ citation_format: Optional[str] = None,
321
+ threshold: Optional[float] = None,
322
+ include_ranking_debug: Optional[bool] = None,
323
+ timeout: Optional[float] = None,
324
+ ) -> Any:
325
+ """Find pages similar to a URL or concept via hybrid semantic discovery."""
326
+ return await self._run(
327
+ "find_similar",
328
+ url=url,
329
+ concept=concept,
330
+ max_results=max_results,
331
+ include_domains=include_domains,
332
+ exclude_domains=exclude_domains,
333
+ include_cache=include_cache,
334
+ include_web=include_web,
335
+ mode=mode,
336
+ max_tokens_out=max_tokens_out,
337
+ include_full_markdown=include_full_markdown,
338
+ citation_format=citation_format,
339
+ threshold=threshold,
340
+ include_ranking_debug=include_ranking_debug,
341
+ timeout=timeout,
342
+ )
343
+
344
+ async def research(
345
+ self,
346
+ *,
347
+ question: str,
348
+ depth: Optional[str] = None,
349
+ max_sources: Optional[int] = None,
350
+ include_domains: Optional[list[str]] = None,
351
+ exclude_domains: Optional[list[str]] = None,
352
+ schema: Optional[Any] = None,
353
+ stream: Optional[bool] = None,
354
+ max_tokens_out: Optional[int] = None,
355
+ include_full_markdown: Optional[bool] = None,
356
+ citation_format: Optional[str] = None,
357
+ timeout: Optional[float] = None,
358
+ ) -> Any:
359
+ """Multi-step research. ``stream`` has no effect over this transport."""
360
+ return await self._run(
361
+ "research",
362
+ question=question,
363
+ depth=depth,
364
+ max_sources=max_sources,
365
+ include_domains=include_domains,
366
+ exclude_domains=exclude_domains,
367
+ schema=schema,
368
+ stream=stream,
369
+ max_tokens_out=max_tokens_out,
370
+ include_full_markdown=include_full_markdown,
371
+ citation_format=citation_format,
372
+ timeout=timeout,
373
+ )
374
+
375
+ async def agent(
376
+ self,
377
+ *,
378
+ prompt: str,
379
+ urls: Optional[list[str]] = None,
380
+ schema: Optional[Any] = None,
381
+ max_pages: Optional[int] = None,
382
+ max_time_ms: Optional[int] = None,
383
+ stream: Optional[bool] = None,
384
+ max_tokens_out: Optional[int] = None,
385
+ include_full_markdown: Optional[bool] = None,
386
+ citation_format: Optional[str] = None,
387
+ timeout: Optional[float] = None,
388
+ ) -> Any:
389
+ """Autonomous data-gathering agent. ``stream`` has no effect over this transport."""
390
+ return await self._run(
391
+ "agent",
392
+ prompt=prompt,
393
+ urls=urls,
394
+ schema=schema,
395
+ max_pages=max_pages,
396
+ max_time_ms=max_time_ms,
397
+ stream=stream,
398
+ max_tokens_out=max_tokens_out,
399
+ include_full_markdown=include_full_markdown,
400
+ citation_format=citation_format,
401
+ timeout=timeout,
402
+ )
403
+
404
+ async def diff(
405
+ self,
406
+ *,
407
+ old: Optional[dict[str, Any]] = None,
408
+ new: Optional[dict[str, Any]] = None,
409
+ output: Optional[str] = None,
410
+ granularity: Optional[str] = None,
411
+ timeout: Optional[float] = None,
412
+ ) -> Any:
413
+ """Diff two content sources. See ``Client.diff`` for the ``old``/``new`` shapes."""
414
+ return await self._run(
415
+ "diff",
416
+ old=old,
417
+ new=new,
418
+ output=output,
419
+ granularity=granularity,
420
+ timeout=timeout,
421
+ )
422
+
423
+ async def watch(
424
+ self,
425
+ *,
426
+ action: str,
427
+ url: Optional[str] = None,
428
+ urls: Optional[list[str]] = None,
429
+ interval_seconds: Optional[int] = None,
430
+ selector: Optional[str] = None,
431
+ notification: Optional[Any] = None,
432
+ job_id: Optional[str] = None,
433
+ timeout: Optional[float] = None,
434
+ ) -> Any:
435
+ """Manage change-watch jobs."""
436
+ return await self._run(
437
+ "watch",
438
+ action=action,
439
+ url=url,
440
+ urls=urls,
441
+ interval_seconds=interval_seconds,
442
+ selector=selector,
443
+ notification=notification,
444
+ job_id=job_id,
445
+ timeout=timeout,
446
+ )