Skip to content
Crystal CodeDocs
ENEnglish

Providers and authentication

CrystalCode.Connections binds engine settings to the CrystalCode.Providers adapters. It owns the built-in provider catalog and the vendor default values, and one client factory and plugin per built-in protocol. ConnectionsHost registers them.

Every built-in factory sends requests through an integration handler when it receives one. It builds its HttpClient around the handler (CredentialHttp), and the adapter skips its own key header because the key is empty. A factory that does not override the credential overloads refuses a handler instead of dropping it.

A model’s optional responses object (ResponsesRequestSettings) sets systemAsInstructions, includeEncryptedReasoning, and omitSampling for endpoints whose request shape differs. When the switches are omitted, they default to false, true, and false. A request that carries an integration handler defaults the first and last to true, because an account endpoint rejects system message items and sampling fields.

CrystalCode.Connections holds no UI, no ~/.crystal layout, and no login flow.

CrystalCode.Authentication holds authentication integrations. It ships no provider definitions. An operator declares each account provider and its models in providers.json.

An integration implements the engine’s IAuthenticationIntegration. It validates a provider definition, runs its own login through ILoginInteraction, and returns a DelegatingHandler that attaches a valid token, refreshes it, and retries once after a 401. The first integration is the ChatGPT account. The integration holds no console code and no storage layout. The engine hands it an AuthenticationCredentialStore view for its provider name.

A provider definition has a type:

type Path
omitted, default, or key API key: environment, then apiKey, then credentials.json
authentication Account login. Names an integration and an inference protocol, and must list its own models

An unknown type is a configuration error.

The engine resolves a provider to a ProviderCredential: an API key, or, for authentication, the saved login plus the integration’s handler. Resolution at startup, at /model, and at launch only checks that a login exists. It never touches the network. A client factory that receives a handler builds its HttpClient around it.

Tokens live in authentication-credentials.json in Home, keyed by provider name. Each entry is opaque JSON owned by the integration. The file is written atomically with owner-only access, and it is listed as a credential path for the file tools.

Refresh is serialized across processes with a lock file beside it, because refresh tokens may rotate. Tokens never appear in logs, diagnostics, events, or command lines. An integration refuses to send a token to a baseUri that it does not recognize as the authorized resource.

crystal login and /login exist in the terminal only as commands. Inside a session, /login and /logout are engine verbs that run AccountSignIn.

  • /login needs SessionFrontEnd.OpenLinkAsync. Only the terminal, Server, and Bridge supply it, each through its own CreateLoginInteraction: a popup in the terminal, a login interaction answered with the pasted address in Server, and an elicitation/create form in Bridge.
  • Neither Server nor Bridge opens a browser. The pasted address holds a one-time code, so it is never logged, replayed, or put in a snapshot.
  • Server classifies both verbs as admin.
  • crystal run uses a saved login. A missing login is an error, never a prompt. A server or bridge session with no login reports it as a refusal until a client signs in.

/login <provider> [--no-browser] and /logout <provider> take one provider name. A bare command shows the account providers and their state as a document card, a table, and never signs in or out. Completion lists every account provider for /login, with --no-browser nested under each, and only the signed-in ones for /logout. The help reads Signed In or Signed Out. The menu is rebuilt after each sign-in and sign-out.

The front end supplies the sign-in surface through SessionFrontEnd.CreateLoginInteraction(provider, openBrowser). A front end that leaves it null (Server and Bridge) refuses /login.

The terminal draws LoginPopup: one card with the steps, the link on its own line, and a paste field that is live at once. It closes by itself when the browser answers first. Esc cancels, and so does Ctrl+C, because CodingSession.TryInterrupt cancels a waiting sign-in before anything else. Either ends with the note Signing in was cancelled. and frees the next /login. A pasted answer that is not an http or https address is asked for again. The sign-in gives up after five minutes.

Logout asks the integration to end the remote session, deletes the saved login, and reports when the service did not confirm.

A disk plugin supplies an integration with IPluginAuthentication (CrystalCode.Plugins.Authentication). The engine serves it through DiskAuthenticationIntegration. The plugin sees PluginAccount facts, an IPluginVault slot, and an IPluginLogin surface. It never sees an engine type. The integration it returns is found after the first-party ones, so it cannot replace one.

The plugin’s client factory receives the handler in PluginClientRequest.Handler with an empty key. crystal login and crystal logout load only home plugins, and only for an integration that is not first-party. See Plugins and Account logins.