@earendil-works/pi-ai is the provider-neutral streaming, model, auth, tool-call, and image API used across the monorepo. Its root surface must remain browser-safe.
src/types.ts Core API/model/message contracts
src/compat.ts Temporary legacy dispatch, registry, catalogs
src/api/ Wire/API implementations and lazy wrappers
src/providers/ Provider factories, catalogs, shared transforms
src/auth/ Credential stores, contexts, auth helpers/types
src/models.ts Models/provider/auth/refresh runtime (owns provider registration, auth resolution, dynamic catalog refresh, stream delegation)
src/models.generated.ts Generated static catalog
src/env-api-keys.ts Browser-safe credential detection boundary
src/tool-call-middleware/ Text-encoded tool protocols
src/utils/tool-schema-compat.ts OpenAI/Moonshot tool JSON-Schema normalization
scripts/generate-models.ts Model catalog source of truth
scripts/generate-image-models.ts Image catalog source of truth
test/ Faux-first and opt-in live tests
- Provider factories and model catalogs live in
src/providers/; wire protocol implementations live insrc/api/. src/api/lazy.tsexposeslazyApi(). API-specific*.lazy.tswrappers are the documented dynamic-import boundary.src/providers/register-builtins.tsregisters compatibility behavior and currently imports onlysrc/compat.ts; do not restore the old provider-loader architecture there.- Public provider and API wildcard subpaths are declared in
package.json. Keep root exports browser-safe. - Message transforms return new structures; never mutate shared input messages.
| Task | Path |
|---|---|
| Add or change a wire protocol | src/api/ |
| Add provider metadata/factory | src/providers/ |
| Translate reasoning/tool options | src/api/simple-options.ts |
| Cross-provider message coercion | src/api/transform-messages.ts |
| Add auth detection | src/env-api-keys.ts |
| Change auth context/storage | src/auth/ |
| Model runtime/provider auth/refresh | src/models.ts, src/auth/, src/providers/all.ts |
| Change model inventory | scripts/generate-models.ts |
| Add text-tool protocol | src/tool-call-middleware/ |
| Provider checklist | README.md provider section |
- Dynamic imports are limited to lazy API and browser-safe credential/OAuth boundaries; ordinary source uses top-level imports.
- Generated model files are never hand-edited. Regenerate and commit intentional catalog changes.
- Unit tests use
src/providers/faux.ts; live APIs require explicit key/feature gating and must not be part of default success. - Keep
extraBody, tool definitions, reasoning options, usage, stop reasons, errors, and abort behavior consistent across APIs. - Inspect installed SDK types before changing external request/response shapes.
- Preserve browser smoke coverage when changing exports or imports.
- Run the affected focused Vitest file, then
npm testfor broad provider changes. - Run
npm run check:browser-smokefrom the root for import/export boundary changes. - Runtime changes require root
npm run checkand real CLI QA evidence. - Read
src/changes.mdand the nearest childAGENTS.mdbefore editing provider or middleware internals.