Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@

2. 跨语言部署 / Cross-language deployment

生产构建使用 `READTHEDOCS_VERSION` 作为版本键(也可在 `conf.py` 设置 `msagent_docs_version`)。部署方需通过 `MSAGENT_LANGUAGE_BUILDS` JSON 环境变量,或 `conf.py` 的 `msagent_language_builds`,明确提供已发布的目标语言基址和页面清单。例如以下配置仅声明 `latest` 的两个目标页面:
生产构建使用 `READTHEDOCS_VERSION` 作为版本键(也可在 `conf.py` 设置 `msagent_docs_version`)。官方两个 `latest` 站点默认互相链接:未设置语言清单时,按另一语言的源文件生成对应页面链接,无需额外环境变量。旧路径先归一到新页,缺少译文时禁用切换。该默认行为假定两个站点均已部署同一版文档;源码检查不代表远端构建已完成。

Production builds use `READTHEDOCS_VERSION` as the lookup key (or set `msagent_docs_version` in `conf.py`). Supply `MSAGENT_LANGUAGE_BUILDS` as a JSON environment variable, or `msagent_language_builds` in `conf.py`, with the published target bases and page inventories:
Production builds use `READTHEDOCS_VERSION` as the lookup key (or set `msagent_docs_version` in `conf.py`). The two official `latest` sites link to each other by default: without a language inventory, links follow the other language's source files and need no extra environment variable. Legacy paths normalize first; missing translations disable the switch. This default assumes both sites have deployed the same documentation revision; source checks do not verify remote build completion.
若需自定义站点地址或严格限制已发布页面,可通过 `MSAGENT_LANGUAGE_BUILDS` JSON 环境变量,或 `conf.py` 的 `msagent_language_builds`,提供目标语言基址和页面清单。显式清单会完全替代默认行为,包括 `latest`;空字典 `{}` 禁用生产环境的语言切换。例如以下配置仅允许 `latest` 的两个目标页面:
To use custom site URLs or restrict links to confirmed published pages, supply `MSAGENT_LANGUAGE_BUILDS` as a JSON environment variable, or `msagent_language_builds` in `conf.py`, with target bases and page inventories. An explicit inventory fully replaces the default, including for `latest`; an empty dictionary `{}` disables production language links. This example allows only two target pages in `latest`:
```python
msagent_language_builds = {
Expand All @@ -33,14 +37,18 @@
}
```
标签和分支预览各使用独立版本键与基址;只有确认对应构建完成的页面才加入清单,不由另一语言的源码存在推断部署状态。没有版本或页面记录时,切换禁用并显示缺失提示,不回退到 `latest`。旧路径先归一到新页。两个 Read the Docs 项目均从 `requirements/docs.txt` 安装相同的固定文档依赖,中文索引使用 `jieba`。
`stable`、发布标签和分支预览仍需显式清单,各使用对应版本键与基址;只有确认对应构建完成的页面才加入清单。没有版本或页面记录时,切换禁用并显示缺失提示,不回退到 `latest`。两个 Read the Docs 项目均从 `requirements/docs.txt` 安装相同的固定文档依赖,中文索引使用 `jieba`。
Use separate keys/bases for release tags and branch previews. Populate inventories from confirmed completed builds, not presumed translations or branch names. Missing versions/pages disable the switch with a notice; legacy paths normalize first, and there is no `latest` fallback. Both Read the Docs projects install the same pinned documentation dependencies from `requirements/docs.txt`; Chinese indexing uses `jieba`.
`stable`, release tags, and branch previews still require explicit inventories with their own version keys and bases. Populate them from confirmed completed builds. Missing versions/pages disable the switch with a notice, and there is no `latest` fallback. Both Read the Docs projects install the same pinned documentation dependencies from `requirements/docs.txt`; Chinese indexing uses `jieba`.
本地预览仍用 `-D msagent_preview=1` 和同一服务根目录下的 `/zh/`、`/en/`。本地测试不验证远端版本的可用性。
Local previews keep `-D msagent_preview=1` with `/zh/` and `/en/` under one server root. They do not verify remote build availability.
运行 `python -m pytest tests/doc/test_site.py` 验证双语生产构建:测试设置 `READTHEDOCS_VERSION=latest` 并移除语言清单环境变量,检查实际 HTML 中的语言链接;不使用本地预览模式代替生产验证。
Run `python -m pytest tests/doc/test_site.py` to validate both production builds. The tests set `READTHEDOCS_VERSION=latest`, remove the language inventory environment variable, and inspect language links in the generated HTML; local preview mode does not substitute for production validation.
3. doc string format
We adopt the google style docstring format as the standard, please refer to the following documents.
Expand Down
27 changes: 18 additions & 9 deletions docs/_ext/msagent_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from sphinxcontrib.mermaid import mermaid
from urllib.parse import urlsplit

LATEST_LANGUAGE_URLS = {
'en': 'https://ms-agent-en.readthedocs.io/en/latest/',
'zh': 'https://ms-agent.readthedocs.io/zh-cn/latest/',
}

SECTIONS = [
('快速开始', 'Getting started', 'get-started',
[('', '', 'introduction quick-start python-sdk')]),
Expand Down Expand Up @@ -37,8 +42,7 @@
]
PAGES = {
prefix + '/' + slug
for _, _, prefix, groups in SECTIONS
for _, _, slugs in groups
for _, _, prefix, groups in SECTIONS for _, _, slugs in groups
for slug in slugs.split()
}

Expand Down Expand Up @@ -123,17 +127,22 @@ def canonical_page(pagename):


def language_link(app, pagename):
"""只链接部署方明确声明存在的版本和页面;缺失时不回退到 latest。"""
"""latest 默认链接双语页面;其他版本使用显式清单,不跨版本回退。"""
other = 'en' if app.config.language.startswith('zh') else 'zh'
page = canonical_page(pagename)
if app.config.msagent_preview:
builds = app.config.msagent_language_builds
default_latest = (
builds is None and app.config.msagent_docs_version == 'latest')
if app.config.msagent_preview or default_latest:
root = Path(app.confdir).parent / other
if page in {'search', 'genindex'} or any(
(root / (page + suffix)).is_file() for suffix in ('.md', '.rst')):
return '/' + other + '/' + page + '.html', False
base = ('/' + other + '/' if app.config.msagent_preview else
LATEST_LANGUAGE_URLS[other])
return base + page + '.html', False
return None, True
target = app.config.msagent_language_builds.get(
app.config.msagent_docs_version, {}).get(other, {})
target = (builds or {}).get(app.config.msagent_docs_version,
{}).get(other, {})
base = target.get('base_url', '')
parsed = urlsplit(base)
if (page not in target.get('pages', [])
Expand Down Expand Up @@ -191,9 +200,9 @@ def setup(app):
types=[str])
app.add_config_value(
'msagent_language_builds',
json.loads(os.environ.get('MSAGENT_LANGUAGE_BUILDS', '{}')),
json.loads(os.environ.get('MSAGENT_LANGUAGE_BUILDS', 'null')),
'html',
types=[dict])
types=[dict, type(None)])
app.connect('doctree-read', prepare_search)
app.connect('html-page-context', page_context)
app.connect('build-finished', finish_build)
Expand Down
64 changes: 57 additions & 7 deletions tests/doc/test_site.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""文档构建、检索和语言链接回归;不依赖模型或真实部署。"""
import importlib.util
import json
import os
import pytest
import subprocess
import sys
Expand All @@ -25,10 +26,39 @@ def application(version='latest',
language=language,
msagent_preview=preview,
msagent_docs_version=version,
msagent_language_builds=builds or {}),
msagent_language_builds=builds),
)


@pytest.mark.parametrize('language,base', [
('zh_CN', 'https://ms-agent-en.readthedocs.io/en/latest/'),
('en', 'https://ms-agent.readthedocs.io/zh-cn/latest/'),
])
@pytest.mark.parametrize('page', ['index', 'core/context', 'search', 'genindex'])
def test_latest_without_manifest(language, base, page):
assert SITE.language_link(application(language=language),
page) == (base + page + '.html', False)


@pytest.mark.parametrize('version', ['local', 'stable', 'v1.7.0', 'preview-1063'])
def test_other_versions_without_manifest_stay_disabled(version):
assert SITE.language_link(application(version=version),
'index') == (None, True)


@pytest.mark.parametrize('builds', [{}, {'latest': {}}, {'latest': {'en': {}}}])
def test_explicit_manifest_can_disable_latest(builds):
assert SITE.language_link(application(builds=builds),
'index') == (None, True)


@pytest.mark.parametrize('preview', [False, True])
def test_missing_translation_stays_disabled(tmp_path, preview):
app = application(preview=preview)
app.confdir = tmp_path / 'zh'
assert SITE.language_link(app, 'core/context') == (None, True)


@pytest.mark.parametrize('version', ['latest', 'v1.7.0', 'preview-1063'])
@pytest.mark.parametrize('language,other', [('zh_CN', 'en'), ('en', 'zh')])
def test_explicit_language_build(version, language, other):
Expand All @@ -55,6 +85,7 @@ def test_local_same_page(page):
page) == ('/en/' + page + '.html', False)


@pytest.mark.parametrize('preview', [False, True])
@pytest.mark.parametrize('old, current', [
('Components/Config', 'development/configuration-reference'),
('Components/SkillManagement', 'user-guide/skill-management'),
Expand All @@ -63,10 +94,10 @@ def test_local_same_page(page):
('design/skill-update-implementation', 'development/skill-management'),
('design/skill-update-walkthrough', 'development/skill-management-checklist'),
])
def test_local_legacy_normalizes(old, current):
def test_legacy_normalizes(old, current, preview):
base = '/en/' if preview else 'https://ms-agent-en.readthedocs.io/en/latest/'
assert SITE.language_link(
application(preview=True), old) == (
'/en/' + current + '.html', False)
application(preview=preview), old) == (base + current + '.html', False)


@pytest.mark.parametrize('target', [
Expand Down Expand Up @@ -130,20 +161,39 @@ def built_site(request, tmp_path_factory):
'html',
'-W',
'--keep-going',
'-D',
'msagent_preview=1',
str(ROOT / 'docs' / lang),
str(output),
]
env = dict(os.environ, READTHEDOCS_VERSION='latest')
env.pop('MSAGENT_LANGUAGE_BUILDS', None)
result = subprocess.run(
command, cwd=ROOT, capture_output=True, text=True, timeout=180)
command, cwd=ROOT, env=env, capture_output=True, text=True, timeout=180)
assert result.returncode == 0, result.stdout + result.stderr
assert 'design.card_legacy' not in result.stderr
raw = (output / 'searchindex.js').read_text()
index = json.loads(raw[len('Search.setIndex('):-1])
return lang, output, index


def test_production_language_links(built_site):
from bs4 import BeautifulSoup
lang, output, _ = built_site
base = ('https://ms-agent-en.readthedocs.io/en/latest/' if lang == 'zh'
else 'https://ms-agent.readthedocs.io/zh-cn/latest/')
other = 'en' if lang == 'zh' else 'zh'
for html in output.rglob('*.html'):
if '_static' in html.parts:
continue
page = html.relative_to(output).with_suffix('').as_posix()
target = SITE.canonical_page(page)
doc = BeautifulSoup(html.read_text(), 'html.parser')
link = doc.select_one('a.ms-language')
assert link is not None, page
assert link['href'] == base + target + '.html', page
assert link['lang'] == other, page
assert link.get('aria-disabled') != 'true', page


def test_no_migration_results(built_site):
_, _, index = built_site
assert set(index['docnames']) == SITE.PAGES | {'index'}
Expand Down