Skip to content

Commit 1320300

Browse files
refactor(ai): convert chat_sdk.ai module to package for chat/ai subpath (#116)
* refactor(ai): convert chat_sdk.ai module to package for chat/ai subpath (vercel/chat#492) Mirror upstream PR vercel/chat#492 which split ai.ts into ai/messages.ts to make room for ai/tools.ts. Convert the chat_sdk.ai module into a package: - src/chat_sdk/ai/messages.py: to_ai_messages + supporting types, moved verbatim from the former ai.py (docstring path updated only). - src/chat_sdk/ai/__init__.py: re-export shim exposing every symbol the old module exported, so from chat_sdk.ai import to_ai_messages keeps working. - tests/test_ai.py renamed to tests/test_ai_messages.py (logic unchanged). No public import path changed. PR 1 of 3 (module move only); the tool factory (create_chat_tools) lands in PR 2. Design issue #109, tracking #98. https://claude.ai/code/session_01FyMxQn2BEAzmwKS1GZczKj * fix(scripts): update AI test-fidelity mapping after module move (review) * fix(ai): unbreak strict fidelity check + re-export TEXT_MIME_PREFIXES Two CI / review follow-ups on PR #116: 1. Fidelity-mapping fix went too far in the previous commit. The script's parity pin is `chat@4.26.0` (see `lint.yml:63`), where the upstream AI test file is still the flat `packages/chat/src/ai.test.ts` — the move to `ai/messages.test.ts` happened in 4.29.0. Pointing the mapping at the 4.29-era path made `verify_test_fidelity.py --strict` fail with "upstream checkout missing — mapped TS file not found". Revert just the TS half of the mapping to `ai.test.ts`; keep the Python half at `tests/test_ai_messages.py` (the file this PR actually renamed). When the parity pin moves to 4.29.0 the TS path can advance too. Locally verified against a fresh `chat@4.26.0` clone: TOTAL: 564/564 matched (100%), 0 missing All TS tests have Python equivalents. 2. The package shim was missing `TEXT_MIME_PREFIXES` (flagged by Codex P2). The PR description promises to "re-export everything the former `chat_sdk.ai` module exposed" — add the constant to both the import block and `__all__` so the contract holds. https://claude.ai/code/session_01FyMxQn2BEAzmwKS1GZczKj --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6995da6 commit 1320300

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

scripts/verify_test_fidelity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"packages/chat/src/markdown.test.ts": "tests/test_markdown_faithful.py",
4646
"packages/chat/src/streaming-markdown.test.ts": "tests/test_streaming_markdown.py",
4747
"packages/chat/src/serialization.test.ts": "tests/test_serialization.py",
48-
"packages/chat/src/ai.test.ts": "tests/test_ai.py",
48+
"packages/chat/src/ai.test.ts": "tests/test_ai_messages.py",
4949
"packages/chat/src/from-full-stream.test.ts": "tests/test_from_full_stream.py",
5050
}
5151

src/chat_sdk/ai/__init__.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""AI SDK integration for the chat SDK.
2+
3+
Python port of the ``chat/ai`` subpath. Mirrors the upstream structure where
4+
``ai.ts`` was split into ``ai/messages.ts`` (and, in later PRs, ``ai/tools.ts``)
5+
to make room for tool factories.
6+
7+
Re-exports everything the former ``chat_sdk.ai`` module exposed so existing
8+
imports such as ``from chat_sdk.ai import to_ai_messages`` keep working.
9+
"""
10+
11+
from __future__ import annotations
12+
13+
from chat_sdk.ai.messages import (
14+
TEXT_MIME_PREFIXES,
15+
AiAssistantMessage,
16+
AiFilePart,
17+
AiImagePart,
18+
AiMessage,
19+
AiMessagePart,
20+
AiTextPart,
21+
AiUserMessage,
22+
ToAiMessagesOptions,
23+
to_ai_messages,
24+
)
25+
26+
__all__ = [
27+
"TEXT_MIME_PREFIXES",
28+
"AiAssistantMessage",
29+
"AiFilePart",
30+
"AiImagePart",
31+
"AiMessage",
32+
"AiMessagePart",
33+
"AiTextPart",
34+
"AiUserMessage",
35+
"ToAiMessagesOptions",
36+
"to_ai_messages",
37+
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Convert chat Messages to AI SDK format.
22
3-
Python port of ai.ts.
3+
Python port of ``ai/messages.ts``.
44
"""
55

66
from __future__ import annotations

0 commit comments

Comments
 (0)