ai-chat-kit
A minimal, production-lean AI Chat SDK/library built for:
- A framework-agnostic TypeScript core
- Token streaming compatible with Server-Sent Events (SSE)
- Adapters for FastAPI (backend) and React (frontend)
- Clean extension points (providers, memory) without coupling core to UI/HTTP
Architecture
-
core/ChatEngine: orchestrates provider calls, streaming, and optional memory persistenceProvider: provider interface (generate()+stream()async generator)types: strongly typed message/request/stream event typesEventBus: small typed event emitter for observability hooks
-
providers/mockProvider: deterministic mock provider that streams token-ish chunks
-
memory/inMemory: minimal memory store keyed byuserId
-
adapters/fastapi: SSE endpointPOST /chat/streamreact:useAIChathook consuming SSE viafetch+ReadableStream
-
ui/ChatWindow: minimal React component usinguseAIChat
Streaming Protocol (SSE)
The FastAPI adapter emits SSE events:
event: tokenwith JSON{"type":"token","token":"..."}repeated per tokenevent: donewith JSON{"type":"done","message":{"role":"assistant","content":"..."}, ...}event: errorwith JSON{"type":"error","error":{"message":"..."}}
The React hook parses SSE frames delimited by blank lines (\n\n).
Run the FastAPI backend
Install the Python package (local editable install):
python -m pip install -e ai-chat-kit
Create a small server.py (in your own backend project) and pass keys explicitly:
from ai_chat_kit.adapters.fastapi.main import create_app
app = create_app(
# Provide whichever providers you want to enable:
gemini_api_key="YOUR_GEMINI_API_KEY",
# openai_api_key="YOUR_OPENAI_API_KEY",
# anthropic_api_key="YOUR_ANTHROPIC_API_KEY",
)
Start the server:
python -m uvicorn server:app --reload --port 8000
Real models (OpenAI / Anthropic / Gemini)
The FastAPI adapter will attempt a real streaming call based on params.model.
API keys are not loaded from .env / .env.local. Pass them to create_app(...) or set process env vars yourself.
Persist chat history with SQLite (FastAPI adapter)
By default the FastAPI adapter will persist chat history in a local SQLite DB file. You can control the DB path with:
export AI_CHAT_KIT_SQLITE_PATH="/path/to/chat_history.sqlite3"
Test streaming:
curl -N -X POST "http://localhost:8000/chat/stream" \
-H "Content-Type: application/json" \
-d '{"userId":"u1","messages":[{"role":"user","content":"Hello streaming"}]}'
Use the React adapter
Example usage with the included UI component:
import React from "react";
import { ChatWindow } from "./ai-chat-kit/ui/ChatWindow";
export default function App() {
return <ChatWindow userId="u1" endpoint="http://localhost:8000/chat/stream" />;
}
Core usage (TypeScript)
Using the core engine directly (no HTTP, no UI):
import { ChatEngine } from "./ai-chat-kit/core/ChatEngine";
import { MockProvider } from "./ai-chat-kit/providers/mockProvider";
import { InMemoryChatMemory } from "./ai-chat-kit/memory/inMemory";
const engine = new ChatEngine(new MockProvider(), { memory: new InMemoryChatMemory() });
const res = await engine.generate({
userId: "u1",
messages: [{ role: "user", content: "Hello" }],
});
console.log(res.message.content);
for await (const ev of engine.stream({ userId: "u1", messages: [{ role: "user", content: "Stream this" }] })) {
if (ev.type === "token") process.stdout.write(ev.token);
if (ev.type === "done") process.stdout.write("\n");
}
Extension points (not implemented)
- Providers: implement
AIProviderto connect OpenAI/Anthropic/local models, add retries, rate limits, tracing. - Memory: replace in-memory with Redis/Postgres, add TTL, per-conversation IDs, metadata.
- RAG: augment
messageswith retrieved context before calling the provider. - Agents / tool calling: expand stream events to include structured tool-call/request/response events.
- Observability: subscribe to
EventBusfor tokens/done/error; forward to logs/metrics/traces. - Langfuse / tracing: wrap provider calls and emit spans without changing the core public API.
Notes
- This repo intentionally stays minimal: no external AI SDK dependency, no WebSockets, no UI styling dependency.
- The FastAPI adapter is a reference implementation of the wire protocol (SSE events) consumed by the React hook.
Metadata
Release files for ai-chat-kit 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| ai_chat_kit-0.1.0.tar.gz | 15.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ai_chat_kit-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.2 kB
Release files / ai_chat_kit-0.1.0.tar.gz
| Download URL | ai_chat_kit-0.1.0.tar.gz |
|---|---|
| Size | 15.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e5c46f72f5909ef997296e0a883e9f0c20dcabc932063e953c74681c70dd4db
|
|
BLAKE2b-256 checksum How to use checksums |
ac57d64e57f8632f8ca7503f59a85648bb69679bd11cce88058996d0bb4c92b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|
Release files / ai_chat_kit-0.1.0-py3-none-any.whl
| Download URL | ai_chat_kit-0.1.0-py3-none-any.whl |
|---|---|
| Size | 15.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
008c75a955b955c7b6689eaba46feb6b4cb2a15aab4f8db8be22f8ae0cb30fa6
|
|
BLAKE2b-256 checksum How to use checksums |
02fcabbb4f47d8ba118b6e7e2915ea22984a81b86df41022f28a71249932031b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|
Security for ai-chat-kit 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.