Skip to content

Commit 127932d

Browse files
authored
feat: support Ask AI (#115)
Support Ask AI with this extension.
1 parent 99a52ef commit 127932d

File tree

10 files changed

+199
-141
lines changed

10 files changed

+199
-141
lines changed

docs/conf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
docsearch_app_id = os.getenv("DOCSEARCH_APP_ID")
2424
docsearch_api_key = os.getenv("DOCSEARCH_API_KEY")
2525
docsearch_index_name = os.getenv("DOCSEARCH_INDEX_NAME")
26+
# DocSearch/Ask AI config
27+
# docsearch_askai = {
28+
# "assistantId": "...",
29+
# "indexName": "...",
30+
# }
2631

2732
# Support building the docs with different themes
2833
# Add `-t alabaster` or `-t rtd` to the build arguments

docs/configuration.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,22 @@ You can find it in the [Algolia dashboard](https://dashboard.algolia.com/account
1313
**`docsearch_api_key`**
1414
: Your Search API key.
1515
You can find it in the [Algolia dashboard](https://dashboard.algolia.com/account/api-keys).
16-
17-
:::{warning}
18-
Don't use your Write API key, which is used for crawling your content.
19-
:::
16+
:::{warning}
17+
Don't use your Write API key, which is used for crawling your content.
18+
:::
2019

2120
**`docsearch_index_name`**
2221
: The Algolia index name for your documentation.
2322

23+
## Ask AI
24+
25+
:::{versionadded} 0.2
26+
To get started, see [Ask AI (DocSearch)](https://docsearch.algolia.com/docs/v4/askai).
27+
:::
28+
29+
`docsearch_askai`
30+
: A string or dictionary with [Ask AI settings](https://docsearch.algolia.com/docs/api#askai).
31+
2432
## Optional configuration
2533

2634
The Sphinx extension lets you configure these aspects of the DocSearch UI.
@@ -40,15 +48,15 @@ You can change this setting if you're using a different theme.
4048
: Any Algolia search parameter you want to add.
4149
For example, to increase the maximum number of results shown, you can increase the `hitsPerPage` parameter:
4250

43-
```python
44-
docsearch_search_parameters: {"hitsPerPage": 30} # default is 20
45-
```
51+
```python
52+
docsearch_search_parameters: {"hitsPerPage": 30} # default is 20
53+
```
4654

47-
:::{warning}
48-
This extension does not validate the search parameters.
49-
Check that you're providing valid parameters.
50-
For more information, see [Search API parameter](https://www.algolia.com/doc/api-reference/search-api-parameters/).
51-
:::
55+
:::{warning}
56+
Search parameters are passed to DocSearch without validation.
57+
Check that you're providing valid parameters.
58+
For more information, see [Search API parameter](https://www.algolia.com/doc/api-reference/search-api-parameters/).
59+
:::
5260

5361
**`docsearch_max_results_per_group`** (default: 5)
5462
: The number of results shown in each group.
@@ -61,6 +69,6 @@ for users to report issues with missing search results.
6169
You can include the current search query as parameter `${query}`.
6270
For example, create a new GitHub issue for the missing search result:
6371

64-
```python
65-
docsearch_missing_results_url = "https://github.com/example/docs/issues/new?title=${query}"
66-
```
72+
```python
73+
docsearch_missing_results_url = "https://github.com/example/docs/issues/new?title=${query}"
74+
```

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "sphinx-docsearch"
3-
version = "0.2.0b3"
3+
version = "0.2.0"
44
description = "A Sphinx extension for replacing the built-in search with Algolia DocSearch"
55
readme = "README.md"
66
authors = [

src/sphinx_docsearch/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def check_config(app: Sphinx, config: Config) -> None:
5151
logger.warning(
5252
__("You must provide your Algolia index name for DocSearch to work.")
5353
)
54+
if isinstance(config.docsearch_askai, dict) and "assistantId" not in config.docsearch_askai:
55+
logger.warning(
56+
msg=__("The docsearch_askai config value must have an assistant_id key.")
57+
)
5458

5559

5660
@progress_message("DocSearch: set up")
@@ -86,6 +90,7 @@ def add_docsearch_assets(app: Sphinx, config: Config) -> None:
8690
"docsearch_app_id": config.docsearch_app_id,
8791
"docsearch_api_key": app.config.docsearch_api_key,
8892
"docsearch_index_name": app.config.docsearch_index_name,
93+
"docsearch_askai": app.config.docsearch_askai,
8994
"docsearch_container": app.config.docsearch_container,
9095
"docsearch_initial_query": app.config.docsearch_initial_query,
9196
"docsearch_placeholder": app.config.docsearch_placeholder,
@@ -103,6 +108,9 @@ def setup(app: Sphinx) -> dict[str, Any]:
103108
app.add_config_value(
104109
"docsearch_index_name", default="", rebuild="html", types=[str]
105110
)
111+
app.add_config_value(
112+
"docsearch_askai", default="", rebuild="html", types=[dict,str]
113+
)
106114
app.add_config_value(
107115
"docsearch_container", default="#docsearch", rebuild="html", types=[str]
108116
)

src/sphinx_docsearch/static/docsearch.css

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sphinx_docsearch/static/docsearch.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sphinx_docsearch/static/docsearch_config.js_t

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ docsearch({
33
appId: "{{ docsearch_app_id }}",
44
apiKey: "{{ docsearch_api_key }}",
55
indexName: "{{ docsearch_index_name }}",
6+
{%- if docsearch_askai %}
7+
askAi: {{ docsearch_askai|tojson }}
8+
{%- endif %}
69
{%- if docsearch_initial_query %}
710
initialQuery: "{{ docsearch_initial_query }}",
811
{%- endif %}

tests/test_config.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
def test_returns_version() -> None:
1414
"""It returns the correct version."""
15-
assert sphinx_docsearch.__version__ == "0.2.0b3"
15+
assert sphinx_docsearch.__version__ == "0.2.0"
1616

1717

1818
@pytest.mark.sphinx("html", confoverrides={"extensions": ["sphinx_docsearch"]})
@@ -24,6 +24,7 @@ def test_docsearch_config(app: Sphinx) -> None:
2424
assert not app.config.docsearch_app_id
2525
assert not app.config.docsearch_api_key
2626
assert not app.config.docsearch_index_name
27+
assert not app.config.docsearch_askai
2728
assert app.config.docsearch_container == "#docsearch"
2829
assert not app.config.docsearch_initial_query
2930
assert not app.config.docsearch_placeholder
@@ -59,6 +60,7 @@ def test_raises_warnings(app: Sphinx, warning: StringIO) -> None:
5960
"docsearch_app_id": "test_app_id",
6061
"docsearch_api_key": "test_api_key",
6162
"docsearch_index_name": "test_docsearch_index_name",
63+
"docsearch_askai": "test_docsearch_askai",
6264
"docsearch_container": "test_docsearch_container",
6365
"docsearch_placeholder": "test_docsearch_placeholder",
6466
"docsearch_initial_query": "test_docsearch_initial_query",
@@ -75,9 +77,40 @@ def test_add_docsearch_config(app: Sphinx, warning: StringIO) -> None:
7577
assert app.config.docsearch_app_id == "test_app_id"
7678
assert app.config.docsearch_api_key == "test_api_key"
7779
assert app.config.docsearch_index_name == "test_docsearch_index_name"
80+
assert app.config.docsearch_askai == "test_docsearch_askai"
7881
assert app.config.docsearch_container == "test_docsearch_container"
7982
assert app.config.docsearch_placeholder == "test_docsearch_placeholder"
8083
assert app.config.docsearch_initial_query == "test_docsearch_initial_query"
8184
assert app.config.docsearch_search_parameters == {"hitsPerPage": 5}
8285
assert app.config.docsearch_max_results_per_group == 10
8386
assert app.config.docsearch_missing_results_url == "test_docsearch_url"
87+
88+
89+
@pytest.mark.sphinx(
90+
"html",
91+
confoverrides={
92+
"extensions": ["sphinx_docsearch"],
93+
"docsearch_askai": {"assistant_id": "wrongly_spelled_key_name"},
94+
},
95+
)
96+
def test_wrong_assistant_id_key(app: Sphinx, warning: StringIO) -> None:
97+
"""It returns a warning for the Ask AI option."""
98+
app.build()
99+
warnings = warning.getvalue()
100+
assert "The docsearch_askai config value must have an assistant_id key." in warnings
101+
assert app.config.docsearch_askai == {"assistant_id": "wrongly_spelled_key_name"}
102+
103+
104+
@pytest.mark.sphinx(
105+
"html",
106+
confoverrides={
107+
"extensions": ["sphinx_docsearch"],
108+
"docsearch_askai": {"assistantId": "test_docsearch_assistant_id"},
109+
},
110+
)
111+
def test_correct_assistant_id_key(app: Sphinx, warning: StringIO) -> None:
112+
"""It parses the Ask AI configuration options."""
113+
app.build()
114+
warnings = warning.getvalue()
115+
assert "The docsearch_askai config value must have an assistant_id key." not in warnings
116+
assert app.config.docsearch_askai == {"assistantId": "test_docsearch_assistant_id"}

tests/test_context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ def test_global_context(app: Sphinx) -> None:
1313

1414
pattern = re.compile(r"^docsearch_")
1515
docsearch_config = filter(pattern.search, app.builder.globalcontext)
16-
assert len(list(docsearch_config)) == 9
16+
assert len(list(docsearch_config)) == 10
1717

1818
assert "docsearch_app_id" in app.builder.globalcontext
1919
assert "docsearch_api_key" in app.builder.globalcontext
20+
assert "docsearch_askai" in app.builder.globalcontext
2021
assert "docsearch_index_name" in app.builder.globalcontext
2122
assert "docsearch_initial_query" in app.builder.globalcontext
2223
assert "docsearch_container" in app.builder.globalcontext

0 commit comments

Comments
 (0)