Skip to content
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__pycache__
# local test-run logs captured from stdout (e.g. oauth-test-output.txt)
*-test-output.txt
38 changes: 9 additions & 29 deletions ai/liii/pi/ai/auth.scm
Original file line number Diff line number Diff line change
Expand Up @@ -262,38 +262,18 @@
) ;and
) ;define

;; oauth 凭据 → access token:未过期直接返回,过期透明刷新并落盘;
;; config-dir 缺失时回退默认配置目录(落盘/清除回调注入 resolve 层,
;; 避免循环依赖)
;; 配置解析只返回凭据快照中的 access,不在启动/切模时联网刷新。
;; 请求前的 request-oauth-key 钩子统一检查 expires 并更新内存 store。
(define (%resolve-oauth-credential-key cred opt-env opt-config-dir)
(let ((dir
(or (and (pair? opt-config-dir) (car opt-config-dir))
(path->string (path-join (path-home) ".gf" "agent"))
) ;or
) ;dir
) ;
(pi-resolve-oauth-credential-key cred
opt-env
(lambda (c)
(pi-save-auth-oauth! (pi-credential-provider c)
(pi-credential-access c)
(pi-credential-refresh c)
(pi-credential-expires c)
(pi-credential-region c)
dir
) ;pi-save-auth-oauth!
) ;lambda
(lambda (p) (pi-remove-auth-provider! p dir))
) ;pi-resolve-oauth-credential-key
) ;let
(pi-credential-access cred)
) ;define

;; 解析服务商生效密钥。opt 尾参兼容两种形态:
;; (provider auth-alist) 旧形态
;; (provider auth-alist env) env 为 '((name . val) ...)
;; (provider auth-alist env config-dir) 新增:oauth 刷新落盘目录
;; oauth 凭据:access 未过期直接返回,过期透明刷新(见
;; %resolve-oauth-credential-key);env API Key 优先于一切 auth 条目
;; 解析服务商生效密钥。opt 尾参兼容旧调用形态:
;; (provider auth-alist)
;; (provider auth-alist env)
;; (provider auth-alist env config-dir)
;; oauth 仅返回内存快照中的 access;请求前由动态钩子按需刷新。
;; env API Key 优先于一切 auth 条目。
(define (pi-resolve-provider-key provider auth-alist . opt)
(let* ((p-str (if (symbol? provider) (symbol->string provider) provider))
(opt-env
Expand Down
82 changes: 82 additions & 0 deletions ai/liii/pi/ai/auth/oauth/store.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
;;
;; Copyright (C) 2026 The Goldfish Scheme Authors
;;
;; Licensed under the MIT License
;;

;; OAuth 运行时凭据存储:与请求配置分离,按 provider 保存最新凭据。
;; store 自身可随不可变配置副本共享;刷新只替换 store 中的凭据,不修改
;; 配置对象,也不写 auth.json。
(define-library (liii pi ai auth oauth store)
(import (scheme base) (liii list) (liii pi ai config credential))
(export pi-make-oauth-store pi-oauth-store? pi-oauth-store-ref
pi-oauth-store-set! pi-oauth-store-load! pi-oauth-store-remove!
pi-oauth-store-clear!
) ;export
(begin

;; 单元素 vector 保存凭据 alist。替换条目而非修改凭据 record。
(define (pi-make-oauth-store . opt-credentials)
(vector (if (pair? opt-credentials) (car opt-credentials) '()))
) ;define

(define (pi-oauth-store? value)
(and (vector? value) (= (vector-length value) 1))
) ;define

(define (pi-oauth-store-ref store provider)
(let ((p-str (if (symbol? provider) (symbol->string provider) provider)))
(pi-find-credential p-str (vector-ref store 0))
) ;let
) ;define

(define (pi-find-credential provider credentials)
(find
(lambda (cred)
(and (pi-credential-oauth? cred)
(equal? (pi-credential-provider cred) provider)
) ;and
) ;lambda
credentials
) ;find
) ;define

(define (pi-oauth-store-set! store cred)
(let ((provider (pi-credential-provider cred)))
(vector-set! store
0
(cons cred
(filter
(lambda (old) (not (equal? (pi-credential-provider old) provider)))
(vector-ref store 0)
) ;filter
) ;cons
) ;vector-set!
) ;let
cred
) ;define

(define (pi-oauth-store-load! store credentials)
(vector-set! store 0 (filter pi-credential-oauth? credentials))
store
) ;define

(define (pi-oauth-store-remove! store provider)
(let ((p-str (if (symbol? provider) (symbol->string provider) provider)))
(vector-set! store
0
(filter
(lambda (cred) (not (equal? (pi-credential-provider cred) p-str)))
(vector-ref store 0)
) ;filter
) ;vector-set!
) ;let
#f
) ;define

(define (pi-oauth-store-clear! store)
(vector-set! store 0 '())
) ;define

) ;begin
) ;define-library
9 changes: 7 additions & 2 deletions ai/liii/pi/ai/keyring.scm
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
;; 每次请求开始时取本次使用的 key:
;; 无钩子时回退 config 的 api-key(auth.json 单 key 路径)。
(define (pi-request-api-key config)
(let ((hook (%config-ref config 'request-key)))
(if (procedure? hook) (hook config) (%config-ref config 'api-key))
(let ((hook (%config-ref config 'request-key))
(oauth-hook (%config-ref config 'request-oauth-key))
) ;
(if (procedure? hook)
(hook config)
(if (procedure? oauth-hook) (oauth-hook config) (%config-ref config 'api-key))
) ;if
) ;let
) ;define

Expand Down
32 changes: 32 additions & 0 deletions ai/tests/liii/pi/ai/auth/oauth/store/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# (liii pi ai auth oauth store) 模块函数索引

`(liii pi ai auth oauth store)` 提供 OAuth 运行时凭据存储:与请求配置分离,按 provider 保存最新凭据。store 可随不可变配置副本共享;刷新只替换 store 中的凭据,不修改配置对象,也不写 auth.json。

使用时导入:

```scheme
(import (liii pi ai auth oauth store))
```

## 查看函数文档

使用以下命令查看函数文档和测试用例:

```bash
gf doc pi-oauth-store-ref
```

## 函数分类索引

### 创建与判定
- `pi-make-oauth-store` - 创建 store,可选初始凭据列表
- `pi-oauth-store?` - 判断值是否为 store(单元素 vector)

### 读取
- `pi-oauth-store-ref` - 按 provider 精确匹配 oauth 凭据

### 更新
- `pi-oauth-store-set!` - 替换同 provider 条目
- `pi-oauth-store-load!` - 用凭据列表整体重载(只保留 oauth)
- `pi-oauth-store-remove!` - 移除指定 provider 条目
- `pi-oauth-store-clear!` - 清空全部凭据
50 changes: 50 additions & 0 deletions ai/tests/liii/pi/ai/auth/oauth/store/pi-make-oauth-store-test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
;; pi-make-oauth-store
;; 创建 OAuth 运行时凭据 store:单元素 vector 保存凭据列表,
;; 可随不可变配置副本共享;替换条目而非修改凭据 record。
;;
;; 语法
;; ----
;; (pi-make-oauth-store)
;; (pi-make-oauth-store credentials)
;;
;; 参数
;; ----
;; credentials : list?
;; 可选,初始凭据列表(缺省为空表)。
;;
;; 返回值
;; ----
;; pi-oauth-store?
;; 新建 store;给定 credentials 时可按 provider 查到其中的 oauth 凭据。

(import (liii check)
(liii pi ai config credential)
(liii pi ai auth oauth store)
) ;import

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

;; 无参:空 store。
(let ((store (pi-make-oauth-store)))
(check-true (pi-oauth-store? store))
(check (pi-oauth-store-ref store "kimi-coding") => #f)
) ;let

;; 传入初始凭据列表:可按 provider 查到。
(let* ((cred (pi-credential "kimi-coding" #f "oauth" "acc" "ref" 4102444800000 #f))
(store (pi-make-oauth-store (list cred)))
) ;
(check-true (pi-oauth-store? store))
(check (pi-credential-access (pi-oauth-store-ref store "kimi-coding")) => "acc")
(check (pi-oauth-store-ref store "google") => #f)
) ;let*

;; 每次调用返回独立 store,互不影响。
(let ((a (pi-make-oauth-store)) (b (pi-make-oauth-store)))
(pi-oauth-store-set! a
(pi-credential "kimi-coding" #f "oauth" "acc-a" "ref" 4102444800000 #f)
) ;pi-oauth-store-set!
(check (pi-oauth-store-ref b "kimi-coding") => #f)
) ;let

(check-report)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
;; pi-oauth-store-clear!
;; 清空 store 中的全部凭据;store 身份不变,共享该 store 的
;; 配置副本随之看到空 store。
;;
;; 语法
;; ----
;; (pi-oauth-store-clear! store)
;;
;; 参数
;; ----
;; store : pi-oauth-store?
;; OAuth 运行时凭据 store。
;;
;; 返回值
;; ----
;; 未指定
;; 仅原地清空,不依赖返回值。

(import (liii check)
(liii pi ai config credential)
(liii pi ai auth oauth store)
) ;import

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

(let ((store (pi-make-oauth-store (list (pi-credential "kimi-coding" #f "oauth" "acc-k" "ref" 1000 #f)
(pi-credential "google" #f "oauth" "acc-g" "ref" 2000 #f)
) ;list
) ;pi-make-oauth-store
) ;store
) ;
(pi-oauth-store-clear! store)
(check (pi-oauth-store-ref store "kimi-coding") => #f)
(check (pi-oauth-store-ref store "google") => #f)
;; 清空后仍可正常写入。
(pi-oauth-store-set! store
(pi-credential "kimi-coding" #f "oauth" "acc-new" "ref" 4102444800000 #f)
) ;pi-oauth-store-set!
(check (pi-credential-access (pi-oauth-store-ref store "kimi-coding"))
=>
"acc-new"
) ;check
) ;let

;; 空 store 上清空幂等。
(let ((store (pi-make-oauth-store)))
(pi-oauth-store-clear! store)
(check-true (pi-oauth-store? store))
) ;let

(check-report)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
;; pi-oauth-store-load!
;; 用凭据列表整体重载 store:只保留 oauth 类型凭据,
;; 替换原有全部内容(/login 后 reload 场景)。
;;
;; 语法
;; ----
;; (pi-oauth-store-load! store credentials)
;;
;; 参数
;; ----
;; store : pi-oauth-store?
;; OAuth 运行时凭据 store。
;;
;; credentials : list?
;; 新凭据列表;非 oauth 凭据被过滤。
;;
;; 返回值
;; ----
;; pi-oauth-store?
;; 传入的 store(原地重载)。

(import (liii check)
(liii pi ai config credential)
(liii pi ai auth oauth store)
) ;import

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

(let* ((store (pi-make-oauth-store (list (pi-credential "old" #f "oauth" "acc-old" "ref" 1000 #f))
) ;pi-make-oauth-store
) ;store
(loaded (pi-oauth-store-load! store
(list (pi-credential "kimi-coding" #f "oauth" "acc" "ref" 4102444800000 #f)
(pi-credential "google" "sk-google")
) ;list
) ;pi-oauth-store-load!
) ;loaded
) ;
(check loaded => store)
;; 旧条目被整体替换,api_key 凭据被过滤。
(check (pi-oauth-store-ref store "old") => #f)
(check (pi-credential-access (pi-oauth-store-ref store "kimi-coding")) => "acc")
(check (pi-oauth-store-ref store "google") => #f)
) ;let*

;; 空列表清空 store。
(let ((store (pi-make-oauth-store (list (pi-credential "kimi-coding" #f "oauth" "acc" "ref" 1000 #f))
) ;pi-make-oauth-store
) ;store
) ;
(pi-oauth-store-load! store '())
(check (pi-oauth-store-ref store "kimi-coding") => #f)
) ;let

(check-report)
30 changes: 30 additions & 0 deletions ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-p-test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
;; pi-oauth-store?
;; 判断值是否为 OAuth 运行时凭据 store(单元素 vector)。
;;
;; 语法
;; ----
;; (pi-oauth-store? value)
;;
;; 参数
;; ----
;; value : any?
;; 任意值。
;;
;; 返回值
;; ----
;; boolean?
;; 单元素 vector(含 pi-make-oauth-store 的结果)为 #t,其余为 #f。

(import (liii check) (liii pi ai auth oauth store))

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

(check-true (pi-oauth-store? (pi-make-oauth-store)))
(check-true (pi-oauth-store? (vector '())))
(check-false (pi-oauth-store? (vector '() '())))
(check-false (pi-oauth-store? (vector)))
(check-false (pi-oauth-store? '()))
(check-false (pi-oauth-store? #f))
(check-false (pi-oauth-store? "store"))

(check-report)
Loading
Loading