Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
60 changes: 57 additions & 3 deletions ai/liii/pi/ai/api/openai-completions/request.scm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,47 @@
) ;cond
) ;define

;; 1404:取 token 末 4 位用于日志,不泄露完整 token;空/短串安全
(define (%token-tail s)
(if (and (string? s) (> (string-length s) 0))
(let ((n (string-length s)))
(substring s (max 0 (- n 4)) n)
) ;let
""
) ;if
) ;define

;; 1404:oauth 401 强制刷新结果描述(用于日志),不暴露完整 token
(define (%oauth-refresh-result-desc next)
(cond ((string? next) (string-append "ok new-token=..." (%token-tail next)))
((eq? next 'unauthorized) "unauthorized")
(else "none")
) ;cond
) ;define

;; 1404:写一行 [oauth-401] 日志到 ~/.gf/agent/logs/pi-YYYY-MM-DD.log
(define (%log-oauth-401 provider attempt api-key next)
(pi-log-error! (string-append "[oauth-401] provider="
(or provider "")
" attempt="
(number->string attempt)
" old-token=..."
(%token-tail api-key)
" refresh="
(%oauth-refresh-result-desc next)
) ;string-append
) ;pi-log-error!
) ;define

;; 401 报错结算:解析响应体构造 assistant 消息(oauth 刷新失败或重试耗尽后复用)
(define (%parse-401-response body-str status provider model-id)
(let* ((parsed-json (catch #t (lambda () (string->json body-str)) (lambda args #f))))
(openai-parse-response parsed-json status provider model-id
"openai-completions"
) ;openai-parse-response
) ;let*
) ;define

(define (openai-build-endpoint base-url)
(let* ((u (make-uri (or base-url "https://api.openai.com/v1")))
(raw-path (or (uri-path u) ""))
Expand Down Expand Up @@ -279,7 +320,7 @@
(session-id (pi-cfg-ref config 'session-id))
) ;
(let loop
((attempt 1) (api-key (pi-request-api-key config)))
((attempt 1) (api-key (pi-request-api-key config)) (refreshed-once? #f))
(let* ((extra-hdrs (%openai-extra-headers url provider session-id config settings))
(headers (openai-build-headers api-key extra-hdrs))
(resp (%post-openai url req-json-str headers proxy-alist timeout))
Expand Down Expand Up @@ -311,7 +352,7 @@
model-id
(%resp-field resp 'reason)
) ;%make-retry-error
(loop 1 next)
(loop 1 next refreshed-once?)
) ;if
) ;let
) ;
Expand All @@ -325,7 +366,7 @@
) ;
(pi-log-retry retry-status attempt provider-max-retries delay-sec ra-sec)
(sleep delay-sec)
(loop (+ attempt 1) api-key)
(loop (+ attempt 1) api-key refreshed-once?)
) ;let*
(%make-retry-error status
attempt
Expand All @@ -336,6 +377,19 @@
) ;%make-retry-error
) ;if
) ;
;; 1404:oauth 凭据收到 401 后强制刷新重试一次(仅一次)。
;; 服务端可能在本地 expires 之前判 access token 失效返回 401,
;; 此时请求层回退强制 refresh 换取新 token 重试;refresh 失败
;; (401/403/invalid_grant)凭据已清除,走 401 报错提示重 /login。
((and (= status 401) (pi-oauth-refresh-enabled? config) (not refreshed-once?))
(let ((next (pi-refresh-oauth-key! config api-key)))
(%log-oauth-401 provider attempt api-key next)
(if (string? next)
(loop 1 next #t)
(%parse-401-response body-str status provider model-id)
) ;if
) ;let
) ;
(else
(let* ((parsed-json (catch #t (lambda () (string->json body-str)) (lambda args #f)))
(limit (pi-usage-limit-error? status body-str provider))
Expand Down
46 changes: 43 additions & 3 deletions ai/liii/pi/ai/api/openai-completions/stream.scm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@
(else #f)
) ;cond
) ;define

(define (%token-tail s)
(if (and (string? s) (> (string-length s) 0))
(let ((n (string-length s)))
(substring s (max 0 (- n 4)) n)
) ;let
""
) ;if
) ;define

(define (%oauth-refresh-result-desc next)
(cond ((string? next) (string-append "ok new-token=..." (%token-tail next)))
((eq? next 'unauthorized) "unauthorized")
(else "none")
) ;cond
) ;define

(define (%log-oauth-401 provider attempt api-key next)
(pi-log-error! (string-append "[oauth-401] provider="
(or provider "")
" attempt="
(number->string attempt)
" old-token=..."
(%token-tail api-key)
" refresh="
(%oauth-refresh-result-desc next)
) ;string-append
) ;pi-log-error!
) ;define
(define (%build-stream-cb stream-state on-event effective-idle on-idle on-abort err-acc)
(let* ((last-act (pi-current-monotonic-seconds))
(decoder (make-openai-stream-decoder stream-state on-event))
Expand Down Expand Up @@ -244,7 +273,7 @@
) ;
) ;
(let loop
((attempt 1) (api-key (pi-request-api-key config)))
((attempt 1) (api-key (pi-request-api-key config)) (refreshed-once? #f))
(let* ((extra-hdrs (%openai-extra-headers url provider session-id config settings))
(headers (openai-build-headers api-key extra-hdrs))
(stream-state (%init-openai-stream-state))
Expand Down Expand Up @@ -308,7 +337,7 @@
(let ((next (pi-rotate-api-key! config api-key status)))
(if (eq? next 'exhausted)
(%make-stream-error status resp err-acc provider model-id)
(loop 1 next)
(loop 1 next refreshed-once?)
) ;if
) ;let
) ;
Expand All @@ -322,11 +351,22 @@
) ;
(pi-log-retry retry-status attempt provider-r delay-sec ra-sec)
(sleep delay-sec)
(loop (+ attempt 1) api-key)
(loop (+ attempt 1) api-key refreshed-once?)
) ;let*
(%make-stream-error status resp err-acc provider model-id)
) ;if
) ;
;; 1404:oauth 凭据收到 401 后强制刷新重试一次(仅一次);
;; refresh 失败(401/403/invalid_grant)凭据已清除,走 401 报错提示重 /login。
((and (= status 401) (pi-oauth-refresh-enabled? config) (not refreshed-once?))
(let ((next (pi-refresh-oauth-key! config api-key)))
(%log-oauth-401 provider attempt api-key next)
(if (string? next)
(loop 1 next #t)
(%make-stream-error status resp err-acc provider model-id)
) ;if
) ;let
) ;
((or (zero? status) (not (= status 200)))
(%make-stream-error status resp err-acc provider model-id)
) ;
Expand Down
Loading
Loading