Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 196 additions & 0 deletions ai/liii/pi/ai/auth/device-info.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
;;
;; Copyright (C) 2026 The Goldfish Scheme Authors
;;
;; Licensed under the MIT License
;;

;; Kimi Coding 设备信息请求头(对齐 Kimi CLI 的 createKimiDeviceHeaders)。
;;
;; Kimi 服务端依据 X-Msh-Device-Name 在账号中心"登录设备"列表展示设备名;
;; 缺失时显示"未知设备"。gfcode 在主机名后追加 (gfcode/<os-type>) 后缀,
;; 便于区分同一账号下不同操作系统、不同客户端的登录记录。
;;
;; X-Msh-Device-Id 持久化在 ~/.gf/agent/device_id(首次随机 UUID,之后复用),
;; 与 Kimi CLI 的 ~/.kimi-code/device_id 同构;读写失败静默降级,不影响登录。
(define-library (liii pi ai auth device-info)
(import (scheme base)
(scheme process-context)
(scheme char)
(liii list)
(liii string)
(liii os)
(liii path)
(liii uuid)
(liii subprocess)
) ;import
(export pi-device-headers
pi-device-id
PI_DEVICE_PLATFORM
PI_DEVICE_VERSION
;; 纯函数与内部助手导出,供单测直接覆盖
%pi-ascii-header
%pi-device-name
%pi-device-model
%pi-os-version
) ;export
(begin

(define PI_DEVICE_PLATFORM "gfcode")
;; 与 gfproject.json package.version 同步,发版时一并更新
(define PI_DEVICE_VERSION "2026.11.24")

;; HTTP header 值仅允许可打印 ASCII(0x20-0x7E):其余字符剔除后 trim,
;; 结果为空时回退 fallback(对齐 Kimi CLI asciiHeader)
(define* (%pi-ascii-header value (fallback "unknown"))
(if (not (string? value))
fallback
(let ((cleaned
(string-trim-both
(list->string
(filter
(lambda (c) (and (char>=? c #\space) (char<=? c #\~)))
(string->list value)
) ;filter
) ;list->string
) ;string-trim-both
) ;cleaned
) ;
(if (string-null? cleaned) fallback cleaned)
) ;let
) ;if
) ;define*

;; env 为 #f 时读真实环境变量;为 alist 时仅查表(单测注入)
(define (%env name env)
(if env
(let ((pair (assoc name env)))
(and pair (cdr pair))
) ;let
(get-environment-variable name)
) ;if
) ;define

(define (%non-empty v)
(and (string? v) (not (string-null? (string-trim-both v))) v)
) ;define

;; 执行命令取首行 stdout;任何失败(命令不存在、非零退出等)归一为 #f
(define (%command-first-line cmd args)
(catch #t
(lambda ()
(let ((proc (process-spawn (cons cmd args) :stdout 'pipe :stderr 'discard)))
(let ((line (process-read-line proc)))
(process-wait proc 2000)
(%non-empty (and (string? line) (string-trim-both line)))
) ;let
) ;let
) ;lambda
(lambda args #f)
) ;catch
) ;define

;; 主机名:COMPUTERNAME(Windows)> HOSTNAME > hostname 命令 > "unknown"
(define (%raw-hostname env)
(or (%non-empty (%env "COMPUTERNAME" env))
(%non-empty (%env "HOSTNAME" env))
(%command-first-line "hostname" '())
"unknown"
) ;or
) ;define

;; 设备名:<hostname> (gfcode/<os-type>),整体 ASCII 清洗
(define (%pi-device-name env)
(%pi-ascii-header
(string-append (%raw-hostname env) " (gfcode/" (os-type) ")")
) ;%pi-ascii-header
) ;define

;; 设备型号:<os-type> <os-arch>,如 "Windows x64" / "Darwin arm64"
(define (%pi-device-model)
(%pi-ascii-header (string-append (os-type) " " (os-arch)))
) ;define

;; 系统版本:Windows 取 OS 环境变量,其余取 uname -sr,兜底 os-type
(define (%pi-os-version env)
(%pi-ascii-header
(or (and (os-windows?) (%non-empty (%env "OS" env)))
(and (not (os-windows?)) (%command-first-line "uname" '("-sr")))
(os-type)
) ;or
) ;%pi-ascii-header
) ;define

(define *device-id-cache* #f)
;; 缓存的 config-dir:切换目录(单测注入)时重新读取而非复用缓存
(define *device-id-cache-dir* #f)

(define (%read-device-id file)
(and (path-exists? file)
(catch #t
(lambda () (%non-empty (string-trim-both (path-read-text file))))
(lambda args #f)
) ;catch
) ;and
) ;define

;; 设备 UUID:读 <config-dir>/device_id(默认 ~/.gf/agent),缺失则生成
;; uuid4 并持久化(非 Windows chmod 0600);写失败仅影响持久化,不影响返回
(define (pi-device-id . opt-config-dir)
(let ((dir-str (if (and (pair? opt-config-dir) (car opt-config-dir))
(car opt-config-dir)
#f
) ;if
) ;dir-str
) ;
(or (and *device-id-cache* (equal? *device-id-cache-dir* dir-str) *device-id-cache*)
(let* ((dir (path (if dir-str
dir-str
(path-join (path-home) ".gf" "agent")
) ;if
) ;path
) ;dir
(file (path-join dir "device_id"))
(existing (%read-device-id file))
(id (or existing (string-downcase (uuid4))))
) ;
(unless existing
(catch #t
(lambda ()
(unless (path-exists? dir)
(path-mkdir dir :parents #t :exist-ok #t)
) ;unless
(path-write-text file id)
(unless (os-windows?)
(catch #t
(lambda () (g_chmod (path->string file) 384))
(lambda args #f)
) ;catch
) ;unless
) ;lambda
(lambda args #f)
) ;catch
) ;unless
(set! *device-id-cache* id)
(set! *device-id-cache-dir* dir-str)
id
) ;let*
) ;or
) ;let
) ;define

;; 完整 X-Msh-* 请求头(alist),附加到 OAuth 各端点请求上。
;; env/config-dir 仅单测注入;默认读真实环境与 ~/.gf/agent。
(define* (pi-device-headers (env #f) (config-dir #f))
(list (cons "X-Msh-Platform" PI_DEVICE_PLATFORM)
(cons "X-Msh-Version" PI_DEVICE_VERSION)
(cons "X-Msh-Device-Name" (%pi-device-name env))
(cons "X-Msh-Device-Model" (%pi-device-model))
(cons "X-Msh-Os-Version" (%pi-os-version env))
(cons "X-Msh-Device-Id"
(if config-dir (pi-device-id config-dir) (pi-device-id))
) ;cons
) ;list
) ;define*

) ;begin
) ;define-library
14 changes: 10 additions & 4 deletions ai/liii/pi/ai/auth/oauth/kimi-coding-oauth.scm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
(liii pi ai time)
(liii pi ai template)
(liii pi ai config credential)
(liii pi ai auth device-info)
(liii pi ai auth oauth device-flow)
) ;import
(export KIMI_OAUTH_CLIENT_ID
Expand Down Expand Up @@ -104,8 +105,11 @@
) ;define

;; 表单编码 POST(application/x-www-form-urlencoded),网络错误/超时
;; 归一为 status-code 0 的响应对象,交由调用方按状态分类
(define (%kimi-post-form url fields)
;; 归一为 status-code 0 的响应对象,交由调用方按状态分类。
;; 附加 X-Msh-* 设备信息头(Kimi CLI 同款):device_authorization 时服务端
;; 据此在账号中心"登录设备"列表展示设备名,缺省显示"未知设备"。
;; headers 为额外请求头 alist,缺省为完整设备信息头。
(define* (%kimi-post-form url fields (headers #f))
(catch #t
(lambda ()
(http-post url
Expand All @@ -120,8 +124,10 @@
"&"
) ;string-join
:headers
'(("Content-Type" . "application/x-www-form-urlencoded")
("Accept" . "application/json"))
(append (or headers (pi-device-headers))
'(("Content-Type" . "application/x-www-form-urlencoded")
("Accept" . "application/json"))
) ;append
:timeout %request-timeout-seconds
) ;http-post
) ;lambda
Expand Down
43 changes: 43 additions & 0 deletions ai/tests/liii/pi/ai/auth/device-info/%pi-ascii-header-test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
;; %pi-ascii-header
;; Kimi 设备信息头的 ASCII 清洗(对齐 Kimi CLI asciiHeader)。
;;
;; 语法
;; ----
;; (%pi-ascii-header value [fallback]) -> string?
;;
;; 参数
;; ----
;; value : string? 或 #f
;; 原始 header 值;非字符串直接回退 fallback(默认 "unknown")。
;;
;; 返回值
;; ----
;; string?
;; 仅含可打印 ASCII(0x20-0x7E)的非空字符串,两端 trim;空则回退。

(import (scheme base)
(liii check)
(liii os)
(liii string)
(liii pi ai auth device-info)
) ;import

(check-set-mode! 'report-failed)

;; ASCII 清洗:保留可打印字符
(check (%pi-ascii-header "my-laptop") => "my-laptop")

;; 剔除控制字符与非 ASCII(中文主机名等)
(check (%pi-ascii-header "dev\x01;\x7f;box") => "devbox")
(check (%pi-ascii-header "张三的MacBook-Pro") => "MacBook-Pro")

;; 全被剔除或空白时回退
(check (%pi-ascii-header "张三") => "unknown")
(check (%pi-ascii-header " ") => "unknown")
(check (%pi-ascii-header #f) => "unknown")
(check (%pi-ascii-header "" "fallback") => "fallback")

;; 结果 trim 两端空白
(check (%pi-ascii-header " spaced ") => "spaced")

(check-report)
28 changes: 28 additions & 0 deletions ai/tests/liii/pi/ai/auth/device-info/%pi-device-model-test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
;; %pi-device-model
;; 设备型号组装:<os-type> <os-arch>。
;;
;; 语法
;; ----
;; (%pi-device-model) -> string?
;;
;; 参数
;; ----
;; 无。
;;
;; 返回值
;; ----
;; string?
;; 如 "Windows x64" / "Darwin arm64" / "Linux x86_64",仅含可打印 ASCII。

(import (scheme base)
(liii check)
(liii os)
(liii string)
(liii pi ai auth device-info)
) ;import

(check-set-mode! 'report-failed)

(check (%pi-device-model) => (string-append (os-type) " " (os-arch)))

(check-report)
52 changes: 52 additions & 0 deletions ai/tests/liii/pi/ai/auth/device-info/%pi-device-name-test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
;; %pi-device-name
;; 设备名组装:<hostname> (gfcode/<os-type>),整体 ASCII 清洗。
;;
;; 语法
;; ----
;; (%pi-device-name env) -> string?
;;
;; 参数
;; ----
;; env : list? 或 #f
;; 环境变量 alist;#f 时读真实环境变量。
;; COMPUTERNAME(Windows)> HOSTNAME > hostname 命令 > "unknown"。
;;
;; 返回值
;; ----
;; string?
;; 带 gfcode/<os-type> 后缀的设备名,仅含可打印 ASCII。

(import (scheme base)
(liii check)
(liii os)
(liii string)
(liii pi ai auth device-info)
) ;import

(check-set-mode! 'report-failed)

;; COMPUTERNAME 优先,带 gfcode/<os-type> 后缀
(check (%pi-device-name '(("COMPUTERNAME" . "DESKTOP-A") ("HOSTNAME" . "ignored")))
=>
(string-append "DESKTOP-A (gfcode/" (os-type) ")")
) ;check

;; HOSTNAME 兜底
(check (%pi-device-name '(("HOSTNAME" . "linux-box")))
=>
(string-append "linux-box (gfcode/" (os-type) ")")
) ;check

;; 非 ASCII 主机名清洗后仍带后缀(清洗发生在后缀拼接之后)
(check (%pi-device-name '(("HOSTNAME" . "开发机")))
=>
(string-append "(gfcode/" (os-type) ")")
) ;check

;; 空白 COMPUTERNAME 视为未设置,回退 HOSTNAME
(check (%pi-device-name '(("COMPUTERNAME" . " ") ("HOSTNAME" . "fallback-host")))
=>
(string-append "fallback-host (gfcode/" (os-type) ")")
) ;check

(check-report)
37 changes: 37 additions & 0 deletions ai/tests/liii/pi/ai/auth/device-info/%pi-os-version-test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
;; %pi-os-version
;; 系统版本:Windows 取 OS 环境变量,其余取 uname -sr,兜底 os-type。
;;
;; 语法
;; ----
;; (%pi-os-version env) -> string?
;;
;; 参数
;; ----
;; env : list? 或 #f
;; 环境变量 alist;#f 时读真实环境变量。
;;
;; 返回值
;; ----
;; string?
;; 非空系统版本字符串,仅含可打印 ASCII。

(import (scheme base)
(liii check)
(liii os)
(liii string)
(liii pi ai auth device-info)
) ;import

(check-set-mode! 'report-failed)

;; Windows 读 OS 环境变量
(if (os-windows?)
(check (%pi-os-version '(("OS" . "Windows_NT"))) => "Windows_NT")
(check-true (string? (%pi-os-version '())))
) ;if

;; env 缺失时兜底非空
(check-true (string? (%pi-os-version '())))
(check-true (not (string-null? (%pi-os-version '()))))

(check-report)
Loading
Loading