中文说明 / Chinese README: README-zh.md
Cross‑platform SwiftUI client for Zhipu GLM models.
Provides a ChatGPT‑style experience with chat, image generation, video generation, and image understanding, targeting iOS, iPadOS, macOS, and visionOS.
On macOS, chatGLM can read the file currently being edited in Xcode / VSCode and other editors via Accessibility and Automation (Apple Events).
Details: MacEditorIntegration.md.
-
Multi‑platform SwiftUI app
- Single codebase targeting iOS, iPadOS, macOS, and visionOS.
- Adaptive layouts for iPhone, iPad, and Mac, with animated background and chat bubbles.
-
Chat, images, and video
- Chat mode with streaming responses.
- Image generation mode (text‑to‑image).
- Video generation mode (text‑to‑video).
-
Image understanding
- Attach a local image.
- The app extracts basic info (size, format, etc.) and injects it as extra context for the chat model.
-
macOS editor integration
- Discover common editors (Xcode, VSCode, JetBrains IDEs, terminal tools, etc.).
- With user‑granted Accessibility + Automation permissions, read the current file (or at least the current editor buffer) from the target app and attach it as context for the model.
- Current version is read‑only: it doesn’t auto‑modify your code files; it focuses on analysis and suggestions.
- See
MacEditorIntegration.mdandchatGLM/MacEditorIntegration/.
High‑level structure:
-
chatGLMApp.swift- App entry point.
- Loads the shared chat UI.
-
ContentView.swift- Main SwiftUI view.
- Provides the ChatGPT‑style chat interface, background gradient, message list, mode picker, and the “rainbow glow” input bar.
- On macOS, integrates the editor picker button that lets you choose a running editor app and attach its current file as context.
-
ChatModels.swift- Domain models:
ChatMessage– user/assistant messages, optional image/file attachment metadata.ChatMode–.chat,.image,.video.
- Domain models:
-
ChatViewModel.swift- Core business logic:
- Maintains message history, input state, and current mode.
- Chat mode:
- Calls the async chat completion API.
- Polls the async result.
- Streams the final answer into the UI with a “typewriter” effect.
- Image mode:
- Calls the image generation API.
- Displays returned image URLs.
- Video mode:
- Calls the video generation async API.
- Shows a card with the resulting video URL.
- File / image context:
- For attached images: injects a short description into the prompt.
- For attached files (from file picker or editor integration): adds a truncated snippet plus path to the prompt so the model can reason about the code.
- Core business logic:
-
PlatformImage.swift,MultilineInputView.swift, etc.- Small platform‑adaptation helpers and UI components.
- Folder:
chatGLM/MacEditorIntegration/EditorIntegration.swift- Discovers known editors using
NSWorkspaceandNSRunningApplication. - Uses Accessibility (
AXUIElement) to try to obtain the current document path via:kAXFocusedWindowAttributekAXMainWindowAttributekAXFocusedUIElementAttribute+ climbing parents to findkAXDocumentAttribute.
- For Xcode:
- First attempts AppleScript to get the current document’s POSIX path.
- If both Accessibility and AppleScript fail to yield a path, it falls back to:
- Traversing the Accessibility window tree to find the code editor text area.
- Reading the full editor buffer text.
- Writing it to a temporary file inside chatGLM’s sandbox.
- Returning that temp file URL as “current file” context.
- Exposes this via
EditorIntegrationManagerandEditorAppPickerView.
- Discovers known editors using
SettingsWindowController.swift- Hosts
SettingsViewinside anNSWindowon macOS, separate from the main chat window.
- Hosts
For a detailed explanation of permissions and behavior, see MacEditorIntegration.md.
-
Folder:
chatGLM/LLM/ -
ZhipuModels.swift- Request/response types for Zhipu APIs:
- Async chat:
ZhipuAsyncChatRequest,ZhipuAsyncTaskResponse. - Image generation:
ZhipuImageGenerationRequest,ZhipuImageGenerationResponse. - Video generation:
ZhipuVideoGenerationRequest. - Async result polling:
ZhipuAsyncResultResponse.
- Async chat:
ZhipuModelenum with common models:glm-4.5-flash– chat.glm-4.1v-thinking-flash– vision / image understanding.cogview-3-flash– image generation.cogvideox-flash– video generation.
- Request/response types for Zhipu APIs:
-
ZhipuAPIClient.swift- A thin wrapper around
URLSessionwith convenient methods:createAsyncChatCompletion→/api/paas/v4/async/chat/completionscreateImageGeneration→/api/paas/v4/images/generationscreateVideoGeneration→/api/paas/v4/videos/generationsfetchAsyncResult→/api/paas/v4/async-result/{id}
- A thin wrapper around
No third‑party networking library is required.
-
Open the project
- Open
chatGLM.xcodeprojin Xcode.
- Open
-
Configure your Zhipu API key
-
In your Run scheme, add an environment variable:
ZHIPU_API_KEY=<your Zhipu API key>
-
-
Run on a target platform
- Select the desired destination:
- iOS Simulator / physical device.
My Mac(macOS app).- visionOS simulator (if installed).
- Build and run.
- Select the desired destination:
-
Grant macOS permissions (optional, but recommended)
- For editor integration on macOS:
- In “Privacy & Security → Accessibility”, allow chatGLM to control your Mac.
- On first AppleScript use (e.g., Xcode integration), approve Automation permission when macOS prompts.
- After that, you can pick a running editor in the chat input bar and chatGLM will attach the current file (or editor buffer) as context for the model.
- For editor integration on macOS:


