Building an AI Coding Desktop Client: Sessions, Permissions, and Model Gateway

    This article provides a practical guide to setting up an AI coding desktop client. It covers choosing between desktop and CLI tools, configuring core features like session isolation and permission levels, and setting up multi-environment execution. Additionally, it details the steps to connect custom models via a local model gateway, including configuration, connectivity tests, rollback procedures, and security practices, followed by a troubleshooting guide and a pre-launch checklist.

    1. Desktop Client vs. Command Line

    Desktop clients are suitable for parallel development across multiple projects and frequent page previews. Command-line tools are ideal for writing small scripts or handling one-off batch processing tasks. The main differences are as follows:

    Scenario CLI Tool Desktop Client
    Managing multiple conversations Relies on manual records or multiple terminal windows Each session is saved independently
    Viewing code changes Requires switching to an editor or running git diff Built-in diff view for block-by-block confirmation
    Verifying web UI effects Manually opening an external browser Built-in preview; auto-refresh on changes depends on the tool’s capabilities
    Distinguishing projects Identified by terminal paths Sessions are bound to project directories

    If you only need to modify a script, the CLI is sufficient. When advancing three or more projects simultaneously or previewing pages repeatedly every day, a desktop client reduces the number of window switches.

    2. Four Core Capabilities and Configuration

    2.1 Session Isolation

    Each session should be bound to a project directory and save its independent conversation history. Using only one session per project is the simplest way to avoid modifying the wrong repository.

    Self-test method:

    1. Create a temporary file probe_a.txt in Session A.
    2. Switch to Session B and confirm that this file is not in its file list.
    3. Run git status in Session B and confirm that probe_a.txt is not in the status output.
    4. Check the history of Session B and confirm that it does not contain the conversation from Session A.
    5. Delete the temporary file and confirm that the directory bindings for both sessions are correct.

    Session data is typically saved in the application data folder under the user directory. Refer to the official documentation of your chosen tool for the specific location.

    2.2 Permission Levels

    Both executing commands and modifying files via AI carry risks. The permission mode determines what the AI can do. It is recommended to use read-only mode by default and temporarily switch when code changes are needed.

    Mode Allowed Operations Main Risks Applicable Scenarios
    Read-only Read files, view diffs, answer questions Code content is sent to the model provider Code review, solution discussion
    Editable Modify workspace files, do not execute commands Accidental file modification, requires Git rollback Feature development, refactoring
    Executable Run tests, builds, scripts Accidental file deletion, execution of high-risk commands Debugging, running test suites
    Committable Execute git add and git commit Committing incorrect code, pushing to remote Committing after confirming changes

    Usage recommendations:

    • In executable mode, try to limit the command whitelist to only allow test and build commands.
    • Prohibit the AI from automatically pushing to the main branch. Developers should execute pushes manually.
    • After switching to editable or higher modes, switch back to read-only once the task is complete.

    Self-test method: In read-only mode, ask the AI to modify a file. Confirm that the modification is rejected and the file content remains unchanged.

    2.3 Multi-Environment Execution

    Desktop clients support three execution methods. The choice depends on dependency complexity, code location, and team requirements for environment consistency.

    Method Prerequisites Credential Management Verification Commands
    Local Execution Language runtime and dependencies are installed; versions match project requirements Credentials in system environment variables or system keychain python3 --version, npm test
    Cloud Container Local container engine installed; image includes project dependencies; working directory mounted; required ports mapped Injected via the container platform’s secret mechanism, not written into the image docker ps, docker exec <container_name> pytest
    SSH Remote Remote host accessible via key login; Git and dependencies installed remotely Use ssh-agent or key files; private key permissions set to 600 ssh -T user@host, ssh user@host 'git -C /srv/project status'

    Points to note:

    • Keep the container’s working directory mount scope as small as possible. Only mount the current project directory, not the user’s home directory.
    • In SSH remote scenarios, it is recommended to create a dedicated low-privilege account for the AI rather than using an administrator account.
    • Only open container and remote ports when needed, and close them immediately after use.

    2.4 Extension Capabilities

    Team standards, test commands, and deployment processes can be encapsulated as skills (SKILLs) or plugins and placed in a custom panel. Next time, you only need to call the skill name.

    Do not write any keys, tokens, or internal network addresses into the skill files. When credentials are needed, have the skill read them from environment variables.

    3. Steps to Connect Custom Models

    Many teams need to connect to self-hosted or regional model services. A common practice is to run a model routing gateway locally. The gateway provides an entry point compatible with mainstream API protocols and forwards requests to the actual model provider.

    Below is the general process. Refer to the official documentation of your chosen product for the gateway’s installation commands and startup parameters. Only key steps are listed here.

    3.1 Prepare Information

    First, obtain three pieces of information from the model provider:

    • API address (Base URL)
    • List of available model names
    • Upstream API Key

    Do not write the API Key in plaintext in the configuration file, and do not paste it into chat windows or screenshots.

    3.2 Configure the Gateway

    Create an independent configuration directory:

    1. mkdir -p ~/ai-gateway && cd ~/ai-gateway

    Use environment variables to reference the upstream key in the configuration file instead of plaintext:

    1. upstream:
    2. base_url: https://upstream.example.com/v1
    3. api_key: ${UPSTREAM_API_KEY}
    4. models:
    5. - name: my-coder-model
    6. upstream_model: real-model-name
    7. server:
    8. host: 127.0.0.1
    9. port: 8080
    10. auth_token: ${GATEWAY_TOKEN}

    Set server.host to 127.0.0.1 so the gateway only accepts local access. auth_token is the local token used by the desktop client to access the gateway, kept separate from the upstream key.

    Set the environment variables. Using read -s for input ensures the key does not appear on the screen or in shell history:

    1. read -s -p "Upstream API Key: " UPSTREAM_API_KEY; echo
    2. export UPSTREAM_API_KEY
    3. export GATEWAY_TOKEN="$(openssl rand -hex 24)"

    3.3 Start the Gateway

    Start the service according to your chosen gateway’s documentation, for example:

    1. <gateway-start-command> --config config.yaml 2>&1 | tee gateway.log

    Logging output to a file facilitates subsequent verification.

    3.4 Configure the Desktop Client

    In the custom model settings of the desktop client, fill in:

    • Interface address: http://127.0.0.1:8080/v1
    • Model name: Must exactly match models.name in the configuration; in this example, my-coder-model
    • API Key: Fill in the value of GATEWAY_TOKEN, not the upstream key

    3.5 Connectivity Test

    First, check the model list:

    1. curl -s http://127.0.0.1:8080/v1/models \
    2. -H "Authorization: Bearer $GATEWAY_TOKEN"

    The expected result is HTTP 200, with my-coder-model included in the returned list.

    Next, send a minimal conversation request, limiting the output length to reduce consumption:

    1. curl -s http://127.0.0.1:8080/v1/chat/completions \
    2. -H "Authorization: Bearer $GATEWAY_TOKEN" \
    3. -H "Content-Type: application/json" \
    4. -d '{"model":"my-coder-model","messages":[{"role":"user","content":"Reply OK"}],"max_tokens":8}'

    The expected result is HTTP 200, with an assistant reply in the returned content.

    3.6 Log Verification

    Confirm that there are request records in the logs and that no keys are leaked:

    1. grep -c "$UPSTREAM_API_KEY" gateway.log

    The output should be 0. If it is not 0, it means the log is recording request headers or configurations. You need to disable the corresponding debug options and clean up existing logs.

    3.7 Rollback Steps

    Back up before each configuration change:

    1. cp config.yaml config.yaml.bak

    In case of issues, roll back in the following order:

    1. Switch the model back to the default model in the desktop client.
    2. Stop the gateway process.
    3. Restore the backup: cp config.yaml.bak config.yaml.
    4. Clear the keys in the current shell: unset UPSTREAM_API_KEY GATEWAY_TOKEN.
    5. Re-run the connectivity test in 3.5 to confirm the old configuration works.

    3.8 Security Requirements

    • Key storage: Prefer using the system keychain or a team key management service. When using environment variables, set them only in the current session and do not write them into shell configuration files.
    • Minimal exposure surface: The gateway should only listen on 127.0.0.1. Do not bind to 0.0.0.0 or expose it directly to the public internet.
    • Remote access: If the gateway runs on another machine, access it via SSH port forwarding, for example, ssh -L 8080:127.0.0.1:8080 user@host.
    • Least privilege: Set a quota limit for the upstream key and only enable the required models.
    • Rotation: If you suspect a key leak, immediately invalidate the old key in the provider’s dashboard. After generating a new key, reset the environment variables.

    4. Troubleshooting Checklist

    Symptom Possible Cause Check Method Solution
    Connection refused Gateway not started, or port mismatch with desktop client settings lsof -i :8080 to view the listening process Start the gateway, verify the port
    401 Authentication failed Desktop client filled in the upstream key, or the local token is not active in the current shell echo ${#GATEWAY_TOKEN} to check the length; 0 means it is not set Enter GATEWAY_TOKEN in the desktop client, reset the environment variable, and restart the gateway
    404 Model not found Typo in model name, or not registered in the gateway config Compare with the list returned by /v1/models Correct the model name to match the configuration
    Gateway timeout Upstream service responds slowly, or network is unreachable Check the duration records in gateway.log Increase the timeout parameter, or switch to another upstream node
    Modification rejected Session is in read-only mode Check the permission indicator at the top of the session Temporarily switch to editable mode after confirming the task requires it
    Modified the wrong repository Session is bound to the wrong project directory git -C <project_path> status to check which repository has changes Re-bind the directory, review accidental changes with git diff, and roll back
    Keys appear in logs Debug options recorded request headers grep -c "$UPSTREAM_API_KEY" gateway.log result is not 0 Disable debug logging, delete old logs, and rotate the key

    5. Pre-Launch Checklist

    Confirm each item before launch. Only hand it over to the team after all checks pass.

    • Each project uses only one session, and the session’s bound directory matches the project path.
    • The default permission mode is read-only or editable; the AI cannot automatically commit or push.
    • Command whitelist is configured in executable mode.
    • The upstream API Key only exists in the keychain, key service, or current shell environment variables.
    • Searched for key characteristics in the project repository with no matches. You can use git grep or scanning tools like gitleaks.
    • The gateway only listens on 127.0.0.1, or is only reachable within the intranet.
    • Both connectivity tests in 3.5 passed.
    • The log verification result in 3.6 is 0.
    • The rollback steps in 3.7 have been practically rehearsed once.
    • Team-shared skill files do not contain keys, tokens, or internal network addresses.
    • Backup files do not contain plaintext keys; old config.yaml.bak files have been deleted or encrypted as needed.

    By following these steps, the boundaries between the desktop client, model gateway, and project repositories are clear. Every error can be located, and every change can be rolled back.