JCodeKit SDK 与 iOS App
JCodeKit SDK 与 iOS 应用
相关源文件
本章引用的主要源码文件:
codemagic.yamlios/Sources/JCodeKit/Connection.swiftios/Sources/JCodeKit/CredentialStore.swiftios/Sources/JCodeKit/JCodeClient.swiftios/Sources/JCodeKit/Protocol.swiftios/Sources/JCodeMobile/AppModel.swiftios/Sources/JCodeMobile/Assets.xcassets/AppIcon.appiconset/AppIcon.pngios/Sources/JCodeMobile/Assets.xcassets/AppIcon.appiconset/Contents.jsonios/Sources/JCodeMobile/Assets.xcassets/Contents.jsonios/Sources/JCodeMobile/ContentView.swiftios/Sources/JCodeMobile/ImagePickerView.swiftios/Sources/JCodeMobile/Info.plistios/Sources/JCodeMobile/JCodeMobile.entitlementsios/Sources/JCodeMobile/JCodeMobileApp.swiftios/Sources/JCodeMobile/MarkdownText.swiftios/Sources/JCodeMobile/QRScannerView.swiftios/Sources/JCodeMobile/SpeechRecognizer.swiftios/Sources/JCodeMobile/Theme.swiftios/Tests/JCodeKitTests/ClientTests.swiftios/Tests/JCodeKitTests/ProtocolTests.swiftios/project.ymlsrc/gateway.rssrc/gateway/auth.rssrc/gateway/registry.rs
JCode iOS 生态系统由 JCodeKit Swift SDK 和一个原生 SwiftUI 应用组成。SDK 提供了一个高级异步接口,用于通过 WebSocket 网关与 jcode 服务器交互,而 iOS 应用则实现了一个针对移动端优化的聊天界面,支持基于二维码的配对、语音听写和图片附件等功能。
JCodeKit Swift SDK
JCodeKit 是面向移动端和 macOS 客户端的基础框架。它将基于换行符分隔的 JSON 协议抽象为 Swift 原生的 async/await 模式和 AsyncStream 事件。
核心组件
JCodeConnection:基于 Actor 的URLSessionWebSocketTask封装器。它管理底层套接字的生命周期、认证请求头和保活 Ping 包ios/Sources/JCodeKit/Connection.swift:3-27。它使用 20 秒的保活间隔来维持连接ios/Sources/JCodeKit/Connection.swift:27。JCodeClient:开发者的主要入口点。它编排连接,并将原始的ServerEvent枚举转换为委托回调ios/Sources/JCodeKit/JCodeClient.swift:119-143。CredentialStore:管理ServerCredential对象的安全存储。- 协议类型:
jcode请求/事件模式的 Swift 实现,包括Request枚举ios/Sources/JCodeKit/Protocol.swift:5-22和ServerEvent枚举ios/Sources/JCodeKit/Protocol.swift:121-150。
数据流:SDK 事件处理
SDK 使用分层事件处理模型,原始套接字字符串被解码为 ServerEvent,然后通过 JCodeClientDelegate 进行分发。
标题:JCodeKit 事件管线
来源:ios/Sources/JCodeKit/Connection.swift:137-144、ios/Sources/JCodeKit/Connection.swift:196-210、ios/Sources/JCodeKit/JCodeClient.swift:184-201、ios/Sources/JCodeKit/JCodeClient.swift:203-220
iOS 应用架构
iOS 应用使用 SwiftUI 构建,并采用观察者模式,以 AppModel 作为中央状态协调器。
AppModel 状态管理
AppModel 管理连接生命周期、消息历史记录和服务器发现。
- 配对:通过
pairAndSave()逻辑处理配对,与PairingClient通信,将 6 位数字码交换为永久的auth_tokenios/Sources/JCodeMobile/AppModel.swift:175-209。 - 连接生命周期:实现指数退避重连策略,并跟踪
connectionGeneration以防止处理过期事件ios/Sources/JCodeMobile/AppModel.swift:89-93。 - 消息处理:维护一个
ChatEntry对象列表,支持文本和工具调用的增量流式处理ios/Sources/JCodeMobile/AppModel.swift:17-41。
UI 组件与功能
MarkdownText:一个自定义解析器,使用 SwiftUI 的Text和ScrollView将 AI 响应渲染为块(段落、代码、标题、列表)ios/Sources/JCodeMobile/MarkdownText.swift:3-15, 85-91。QRScannerView:使用AVFoundation和QRScannerController扫描 CLI 的jcode pair命令生成的jcode://pairURIios/Sources/JCodeMobile/QRScannerView.swift:7-25, 136-160。- 语音和图片支持:集成
SFSpeechRecognizer实现免提听写,以及PhotosUI用于为查询附加上下文ios/Sources/JCodeMobile/Info.plist:47-54。
实体映射:UI 到 SDK
标题:移动端实体映射
来源:ios/Sources/JCodeMobile/ContentView.swift:10-29、ios/Sources/JCodeMobile/AppModel.swift:10-15、ios/Sources/JCodeKit/JCodeClient.swift:119-124、ios/Sources/JCodeKit/Protocol.swift:5-10
WebSocket 网关(服务端)
服务器通过 WebSocket 网关(默认端口 7643)支持移动端连接。
- 架构:网关通过 TCP 接受 WebSocket 连接,并将其桥接到虚拟的
UnixStream对src/gateway.rs:8-13。 - 认证:支持
Authorization: Bearer <token>请求头或?token=查询参数,用于旧版/浏览器客户端src/gateway.rs:145-160。 - 设备注册表:根据持久化的
DeviceRegistry验证令牌src/gateway.rs:187-195。
标题:服务端网关桥接
来源:src/gateway.rs:8-13、src/gateway.rs:71-80、src/gateway.rs:139-170
配对与连接流程
配对流程通过二维码将本地桌面环境与移动设备连接起来。
| 步骤 | 操作 | 代码实体 |
|---|---|---|
| 1 | 在 PC 上运行 jcode pair | CLI(服务器网关) |
| 2 | 使用 iPhone 扫描二维码 | QRScannerView ios/Sources/JCodeMobile/QRScannerView.swift:20-25 |
| 3 | 解析 jcode://pair?host=...&code=... | parseJCodeURI ios/Sources/JCodeMobile/QRScannerView.swift:94-114 |
| 4 | 将代码交换为令牌 | PairingClient.pair() ios/Sources/JCodeMobile/AppModel.swift:201-202 |
| 5 | 持久化凭证 | CredentialStore.save() |
| 6 | 建立 WebSocket 连接 | JCodeConnection.connect() ios/Sources/JCodeKit/Connection.swift:45-66 |
构建与持续集成与持续交付管线
该项目使用 XcodeGen 生成项目文件,并使用 Codemagic 进行持续集成。
Codemagic 工作流(ios-testflight)
该管线自动化了从源代码到 TestFlight 分发的整个过程。
- 环境:使用搭载 Xcode 16.2 的
mac_mini_m2环境codemagic.yaml:5, 15。 - 项目生成:运行
xcodegen generate,根据project.yml生成JCodeMobile.xcodeprojcodemagic.yaml:31-33。 - 代码签名:初始化钥匙串,并使用
BUNDLE_IDcom.jcode.mobile从 App Store Connect 获取签名配置文件codemagic.yaml:12, 39-46。 - 构建:使用
Release配置编译用于分发的.ipa文件codemagic.yaml:50-57。 - 发布:自动将构建提交到 TestFlight 的“内部测试人员”Beta 组
codemagic.yaml:61-66。
来源:codemagic.yaml:1-67、ios/project.yml:1-43