# pyichiban/chatglm-cli architecture

> Architecture diagram of the GitHub repository pyichiban/chatglm-cli, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pyichiban/chatglm-cli
- Repository: https://github.com/pyichiban/chatglm-cli

## Overview

chatglm-cli is a Go command-line client for chatglm.cn’s private IDE interface, also usable as a channel plugin. Its primary workflow is prompt input → chat command → streamed upstream response → JSONL events on stdout; callers explicitly pass a conversation ID to continue a conversation. Separate commands provide conversation management, account/model information, authentication, raw endpoint access, and image generation, including reference-image composition and upload. The graph reflects documented workflows and sampled call sites; unsampled implementation details are represented only where the README documents the capability.

Main entry points: [`internal/glm/client.go`](https://github.com/pyichiban/chatglm-cli/blob/main/internal/glm/client.go) (GLM API client), [`internal/cli/cli.go`](https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cli.go) (Command dispatcher), [`internal/channel/channel.go`](https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/channel.go) (Channel core), [`internal/channel/imagesgen.go`](https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/imagesgen.go) (Plugin image generation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI and commands"]
  node_entry["CLI entry<br/>[main.go]"]
  node_dispatcher["Command dispatcher<br/>[cli.go]"]
  node_chatcmd["Chat command<br/>[cmd_chat.go]"]
  node_convcommands["Conversation commands<br/>[cmd_conv.go]"]
  node_infocommands["Info commands<br/>[cmd_info.go]"]
  node_channelcommands["Channel commands<br/>[cmd_channel.go]"]
  node_rawcommand["Raw endpoint command<br/>[cmd_raw.go]"]
end

subgraph group_channel["Plugin channel"]
  node_pluginregistry["Plugin registry<br/>[plugin.go]"]
  node_channelcore["Channel core<br/>[channel.go]"]
  node_channelchat["Plugin chat generation<br/>[chatgen.go]"]
  node_channelimages["Plugin image generation<br/>[imagesgen.go]"]
  node_channelops["Auth and operations<br/>[ops.go]"]
end

subgraph group_upstream["GLM integration"]
  node_glmclient["GLM API client<br/>[client.go]"]
  node_credentials["Credential handling<br/>[cred.go]"]
end

subgraph group_protocol["Protocol and images"]
  node_eventcontract["JSONL event contract<br/>[event.go]"]
  node_jsonl["JSONL framing<br/>[jsonl.go]"]
  node_rpc["Plugin RPC<br/>[client.go]"]
  node_referenceimages["Reference image tools<br/>[refsheet.go]"]
  node_imageupload["Image upload<br/>[upload.go]"]
end

node_user(("Human or agent"))
node_glmservice{{"chatglm.cn service"}}
node_imagehosts{{"Image hosting services"}}

node_user -->|"runs command"| node_entry
node_entry -->|"starts"| node_dispatcher
node_dispatcher -->|"dispatches"| node_chatcmd
node_dispatcher -->|"dispatches"| node_convcommands
node_dispatcher -->|"dispatches"| node_infocommands
node_dispatcher -->|"dispatches"| node_channelcommands
node_dispatcher -->|"dispatches"| node_rawcommand
node_chatcmd -->|"streams chat"| node_glmclient
node_glmclient -->|"sends requests"| node_glmservice
node_glmclient -->|"parses stream"| node_eventcontract
node_chatcmd -->|"emits events"| node_eventcontract
node_eventcontract -->|"writes JSONL"| node_user
node_convcommands -->|"manages conversations"| node_glmclient
node_infocommands -->|"queries account and models"| node_glmclient
node_rawcommand -->|"calls endpoints"| node_glmclient
node_channelcommands -->|"serves plugin methods"| node_pluginregistry
node_pluginregistry -->|"dispatches methods"| node_channelcore
node_channelcore -->|"handles chat generation"| node_channelchat
node_channelcore -->|"handles image generation"| node_channelimages
node_channelcore -->|"handles auth and operations"| node_channelops
node_channelchat -->|"streams upstream chat"| node_glmclient
node_channelimages -->|"requests image generation"| node_glmclient
node_channelimages -->|"builds reference sheet"| node_referenceimages
node_channelimages -->|"uploads references"| node_imageupload
node_imageupload -->|"uploads image data"| node_imagehosts
node_channelops -->|"manages credentials"| node_credentials
node_credentials -->|"authenticates requests"| node_glmservice
node_pluginregistry -->|"serves JSONL RPC"| node_rpc
node_rpc -->|"encodes frames"| node_jsonl

click node_entry "https://github.com/pyichiban/chatglm-cli/blob/main/cmd/glm2cli/main.go"
click node_dispatcher "https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cli.go"
click node_chatcmd "https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_chat.go"
click node_convcommands "https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_conv.go"
click node_infocommands "https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_info.go"
click node_channelcommands "https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_channel.go"
click node_rawcommand "https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_raw.go"
click node_pluginregistry "https://github.com/pyichiban/chatglm-cli/blob/main/internal/protocol/plugin/plugin.go"
click node_channelcore "https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/channel.go"
click node_channelchat "https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/chatgen.go"
click node_channelimages "https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/imagesgen.go"
click node_channelops "https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/ops.go"
click node_glmclient "https://github.com/pyichiban/chatglm-cli/blob/main/internal/glm/client.go"
click node_credentials "https://github.com/pyichiban/chatglm-cli/blob/main/internal/glm/cred.go"
click node_eventcontract "https://github.com/pyichiban/chatglm-cli/blob/main/internal/glm/event.go"
click node_jsonl "https://github.com/pyichiban/chatglm-cli/blob/main/internal/protocol/jsonl/jsonl.go"
click node_rpc "https://github.com/pyichiban/chatglm-cli/blob/main/internal/protocol/rpc/client.go"
click node_referenceimages "https://github.com/pyichiban/chatglm-cli/blob/main/internal/imgutil/refsheet.go"
click node_imageupload "https://github.com/pyichiban/chatglm-cli/blob/main/internal/imgutil/upload.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_dispatcher,node_chatcmd,node_convcommands,node_infocommands,node_channelcommands,node_rawcommand toneBlue
class node_pluginregistry,node_channelcore,node_channelchat,node_channelimages,node_channelops toneAmber
class node_glmclient,node_credentials toneMint
class node_eventcontract,node_jsonl,node_rpc,node_referenceimages,node_imageupload toneRose
class node_user,node_glmservice,node_imagehosts toneIndigo
```

## Components

### CLI and commands

- [CLI entry](https://github.com/pyichiban/chatglm-cli/blob/main/cmd/glm2cli/main.go): `cmd/glm2cli/main.go`
- [Command dispatcher](https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cli.go): `internal/cli/cli.go`
- [Chat command](https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_chat.go): `internal/cli/cmd_chat.go`
- [Conversation commands](https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_conv.go): `internal/cli/cmd_conv.go`
- [Info commands](https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_info.go): `internal/cli/cmd_info.go`
- [Channel commands](https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_channel.go): `internal/cli/cmd_channel.go`
- [Raw endpoint command](https://github.com/pyichiban/chatglm-cli/blob/main/internal/cli/cmd_raw.go): `internal/cli/cmd_raw.go`

### Plugin channel

- [Plugin registry](https://github.com/pyichiban/chatglm-cli/blob/main/internal/protocol/plugin/plugin.go): `internal/protocol/plugin/plugin.go`
- [Channel core](https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/channel.go): `internal/channel/channel.go`
- [Plugin chat generation](https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/chatgen.go): `internal/channel/chatgen.go`
- [Plugin image generation](https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/imagesgen.go): `internal/channel/imagesgen.go`
- [Auth and operations](https://github.com/pyichiban/chatglm-cli/blob/main/internal/channel/ops.go): `internal/channel/ops.go`

### GLM integration

- [GLM API client](https://github.com/pyichiban/chatglm-cli/blob/main/internal/glm/client.go): `internal/glm/client.go`
- [Credential handling](https://github.com/pyichiban/chatglm-cli/blob/main/internal/glm/cred.go): `internal/glm/cred.go`

### Protocol and images

- [JSONL event contract](https://github.com/pyichiban/chatglm-cli/blob/main/internal/glm/event.go): `internal/glm/event.go`
- [JSONL framing](https://github.com/pyichiban/chatglm-cli/blob/main/internal/protocol/jsonl/jsonl.go): `internal/protocol/jsonl/jsonl.go`
- [Plugin RPC](https://github.com/pyichiban/chatglm-cli/blob/main/internal/protocol/rpc/client.go): `internal/protocol/rpc/client.go`
- [Reference image tools](https://github.com/pyichiban/chatglm-cli/blob/main/internal/imgutil/refsheet.go): `internal/imgutil/refsheet.go`
- [Image upload](https://github.com/pyichiban/chatglm-cli/blob/main/internal/imgutil/upload.go): `internal/imgutil/upload.go`

### Other components

- **Human or agent**
- **chatglm.cn service**
- **Image hosting services**

## Connections

- Human or agent → CLI entry: runs command (evidence: `README.md`)
- CLI entry → Command dispatcher: starts (evidence: `cmd/glm2cli/main.go`)
- Command dispatcher → Chat command: dispatches (evidence: `internal/cli/cli.go`)
- Command dispatcher → Conversation commands: dispatches (evidence: `internal/cli/cli.go`)
- Command dispatcher → Info commands: dispatches (evidence: `internal/cli/cli.go`)
- Command dispatcher → Channel commands: dispatches (evidence: `internal/cli/cli.go`)
- Command dispatcher → Raw endpoint command: dispatches (evidence: `internal/cli/cli.go`)
- Chat command → GLM API client: streams chat (evidence: `internal/cli/cmd_chat.go`)
- GLM API client → chatglm.cn service: sends requests (evidence: `README.md`)
- GLM API client → JSONL event contract: parses stream
- Chat command → JSONL event contract: emits events
- JSONL event contract → Human or agent: writes JSONL (evidence: `README.md`)
- Conversation commands → GLM API client: manages conversations (evidence: `internal/cli/cmd_conv.go`)
- Info commands → GLM API client: queries account and models (evidence: `internal/cli/cmd_info.go`)
- Raw endpoint command → GLM API client: calls endpoints (evidence: `internal/cli/cmd_raw.go`)
- Channel commands → Plugin registry: serves plugin methods (evidence: `internal/cli/cmd_channel.go`)
- Plugin registry → Channel core: dispatches methods (evidence: `internal/channel/channel.go`)
- Channel core → Plugin chat generation: handles chat generation (evidence: `internal/channel/chatgen.go`)
- Channel core → Plugin image generation: handles image generation (evidence: `internal/channel/imagesgen.go`)
- Channel core → Auth and operations: handles auth and operations (evidence: `internal/channel/ops.go`)
- Plugin chat generation → GLM API client: streams upstream chat (evidence: `internal/channel/chatgen.go`)
- Plugin image generation → GLM API client: requests image generation (evidence: `internal/channel/imagesgen.go`)
- Plugin image generation → Reference image tools: builds reference sheet (evidence: `internal/channel/imagesgen.go`)
- Plugin image generation → Image upload: uploads references (evidence: `README.md`)
- Image upload → Image hosting services: uploads image data (evidence: `README.md`)
- Auth and operations → Credential handling: manages credentials (evidence: `internal/channel/ops.go`)
- Credential handling → chatglm.cn service: authenticates requests (evidence: `README.md`)
- Plugin registry → Plugin RPC: serves JSONL RPC (evidence: `internal/protocol/plugin/plugin.go`)
- Plugin RPC → JSONL framing: encodes frames (evidence: `internal/protocol/rpc/client.go`)
