omp packages

pi-gigachat-models0.2.2

GigaChat provider for pi: Authorization-key login, streaming chat completions and function calling

khanton Published 2 days ago MIT ~0 downloads/mo

Untested on omp

This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.

Did it work for you?

Result

One report per person. A new report replaces your old one.

Reports 0

No reports yet.

Readme

pi-gigachat-models

Author: Anton Kholodkov

GigaChat provider (models and chat completions) for pi. The package registers a gigachat provider with Authorization-key login (/login gigachat), live model discovery, SSE token streaming and function calling.

Install in Pi: pi install npm:pi-gigachat-models

The wire protocol follows the public GigaChat OpenAPI description: https://developers.sber.ru/docs/files/openapi/gigachat/api.yml

Install

pi install npm:pi-gigachat-models

Requires Node.js >=22.19.0 and Pi. TypeScript sources are loaded directly by Pi; no build step is required.

Sign in

/login gigachat

The flow asks for:

  1. API scope — GIGACHAT_API_PERS (individual). Note: Only GIGACHAT_API_PERS is currently tested and supported. GIGACHAT_API_B2B and GIGACHAT_API_CORP are not supported yet.
  2. Authorization key — the Base64 client_id:client_secret value from the GigaChat API console. Pasting a full Basic ... header or a quoted value is fine; it is normalized.

The key is exchanged for a 30-minute access token immediately (so a wrong key fails during login, not on the first prompt). The Authorization key is stored in ~/.pi/agent/auth.json; access tokens are cached in memory and renewed automatically.

GigaChat endpoints are signed by the Russian Trusted CA of the Ministry of Digital Development. Those certificates ship with the package and are used for the provider's own requests only — see TLS and certificates.

After login:

/model gigachat/GigaChat-2-Max

/logout gigachat removes the stored key.

Legal-entity scopes (B2B, CORP) are not supported yet. They would require a GigaChat API client certificate (mTLS) on the token endpoint, which is not implemented: the provider uses fetch, which has no portable way to present a client certificate. Only GIGACHAT_API_PERS (individual) keys are tested and supported.

TLS and certificates

ngw.devices.sberbank.ru:9443 and api.giga.chat present certificates issued by the Russian Trusted Sub CA under the Russian Trusted Root CA (Ministry of Digital Development). Node's default trust store does not contain them, so a plain fetch fails with fetch failed and, as the cause, self-signed certificate in certificate chain.

The provider bundles both certificates in certs/ as verbatim copies of the files published at https://www.gosuslugi.ru/landing/crt_linux (https://gu-st.ru/content/lending/russian_trusted_root_ca_pem.crt and russian_trusted_sub_ca_pem.crt), so nothing has to be installed manually:

  • the certificates are added to an undici dispatcher that is attached to the token, GET /models and chat requests of this provider only,
  • system settings and other tools' trust are untouched, and certificate verification stays enabled,
  • Node's own roots are kept, so overriding GIGACHAT_BASE_URL with a host that uses a public CA still works.

Extra CAs (corporate proxy, private endpoint) can be added without touching the package: GIGACHAT_CA_BUNDLE=/path/to/ca.pem (several paths separated by the platform path separator, : on Unix and ; on Windows), or with NODE_EXTRA_CA_CERTS, which Node already applies.

Fingerprints of the bundled files (SHA-256, verify with openssl x509 -in certs/<file> -noout -fingerprint -sha256):

File SHA-256 fingerprint
russian-trusted-root-ca.pem D2:6D:2D:02:31:B7:C3:9F:92:CC:73:85:12:BA:54:10:35:19:E4:40:5D:68:B5:BD:70:3E:97:88:CA:8E:CF:31
russian-trusted-sub-ca.pem BB:BD:E2:10:3E:79:0B:99:9E:C6:2B:D0:3C:F6:25:A5:A2:E7:C3:16:E1:0A:FE:6A:49:0E:ED:EA:D8:B3:FD:9B

The certificates are public data, not secrets. They are imported by npm test, which fails if a bundled file is replaced or corrupted.

Environment variables

/login is optional when the environment already provides a credential:

Variable Meaning
GIGACHAT_CREDENTIALS Authorization key (same name as the official SDK)
GIGACHAT_AUTHORIZATION_KEY Alias for the Authorization key
GIGACHAT_ACCESS_TOKEN Ready-made access token; skips the token exchange
GIGACHAT_SCOPE GIGACHAT_API_PERS (default), GIGACHAT_API_B2B, GIGACHAT_API_CORP
GIGACHAT_BASE_URL REST root, default https://api.giga.chat/v1
GIGACHAT_AUTH_URL OAuth endpoint, default https://ngw.devices.sberbank.ru:9443/api/v2/oauth
GIGACHAT_CA_BUNDLE Extra PEM file(s) trusted for GigaChat requests, :-separated (; on Windows)

A stored credential always wins over the environment. GIGACHAT_BASE_URL and GIGACHAT_AUTH_URL are read when the provider is registered (extension load or /reload); the credential variables are read on every request.

Models

The provider starts with the models documented at https://developers.sber.ru/docs/ru/gigachat/models/main — GigaChat 3 Ultra, GigaChat 2 Max, GigaChat 2 Pro, GigaChat 2 Lite — and refreshes the list from GET /models at startup, when /model opens and after /login. Models the service returns additionally are added with documented defaults (128K context, 8K output).

GigaChat bills in rubles per million tokens, which pi cannot express in its USD cost estimate, so every model reports zero cost. Use the console for spend.

Capabilities and limits

Supported:

  • system prompt plus multi-turn history (GigaChat accepts exactly one leading system message; later system messages are folded into it),
  • SSE token streaming with usage accounting (precached_prompt_tokens maps to cache reads),
  • user-defined functions: pi tools are sent as functions with function_call: "auto", responses are converted to pi tool calls, and functions_state_id is replayed on the next request so the model keeps the call context,
  • aborting a request, request timeouts (timeoutMs), context-overflow normalization so pi can compact, and clear 401/429 error messages,
  • samplingParams passthrough (top_p, repetition_penalty, …).

Not supported yet:

  • image input — GigaChat expects uploaded file ids in attachments, so models are declared text-only and image blocks are replaced with a [image omitted] note,
  • parallel tool calls in a single turn (GigaChat-3-Ultra only, v2 protocol) — an assistant turn with several tool calls is replayed as one function_call + function pair per call,
  • mid-stream tool-argument deltas — GigaChat sends the whole function_call in one fragment anyway,
  • client-certificate (mTLS) authentication for legal-entity scopes.

Development

npm install        # install dependencies
npm run typecheck  # strict TypeScript check, no emit
npm test           # node --test, no network access required
npm run coverage   # node's built-in coverage report for the test suite
npm run lint       # ESLint on src/ and test/

Layout:

File Purpose
src/index.ts extension entry: registers the provider
src/auth.ts Authorization-key login, token exchange, token cache
src/models.ts model catalog and GET /models discovery
src/tls.ts Russian Trusted CA trust and the per-request undici dispatcher
src/wire.ts pi transcript → GigaChat messages, SSE parsing, finish reasons
src/stream.ts streamSimple implementation and event emission
certs/ Bundled Russian Trusted Root CA and Sub CA (public certificates)

Publishing

Run these commands from the repository root:

npm login --registry=https://registry.npmjs.org/
npm run prepublishOnly  # typecheck, lint and tests
npm pack --dry-run     # inspect files before publishing
npm publish            # repeats checks via prepublishOnly

The published package includes src/, certs/, README.md and LICENSE. Pi loads src/index.ts through the pi.extensions manifest; no dist/ is needed. The pi-package keyword enables discovery in the Pi package catalog after npm indexing.

For subsequent releases, increment version in package.json before publishing; npm does not allow overwriting an existing version. Do not include credentials or private keys in the package. The bundled CA certificates are public.

Troubleshooting

  • 401 Unauthorized during login — the value is probably the client secret instead of the Base64 Authorization key from the console.
  • fetch failed / self-signed certificate in certificate chain — the TLS trust for the Russian Trusted CA did not apply. Check that certs/ is present next to src/, or point GIGACHAT_CA_BUNDLE at a copy of russian_trusted_root_ca_pem.crt. The error message names the missing file.
  • 401 while chatting — the token expired and could not be renewed; run /login gigachat again.
  • 429 Too many requests — GigaChat quotas per second are low; retry, or switch to a smaller model.
  • context_length_exceeded — pi compacts and retries once automatically. Very large tool outputs may need to be trimmed.
  • GigaChat is not configured — no credential is stored and no environment variable is set.