config: resolve {file:path} references in provider api_key values

ober

84dca8c9a55389f00235a706f7590b73243ff2de

diff --git a/src/jcode/core/config.ss b/src/jcode/core/config.ss
index 188ae8c..f8b2552 100644
--- a/src/jcode/core/config.ss
+++ b/src/jcode/core/config.ss
@@ -13,6 +13,7 @@
         config-default-model
         provider-key-layers
         detect-provider-from-list
+        resolve-file-ref
         *provider-env-vars*
         *detect-provider-order*
         *config*
@@ -206,12 +207,27 @@
       (config-fn provider)
       (and (equal? provider "grok") (grok-fn))))
 
+(def (resolve-file-ref val)
+  (if (and (string? val)
+           (>= (string-length val) 7)
+           (equal? (substring val 0 6) "{file:")
+           (char=? (string-ref val (- (string-length val) 1)) #\}))
+    (let* ((path (substring val 6 (- (string-length val) 1)))
+           (expanded (path-expand path)))
+      (if (file-exists? expanded)
+        (let ((content (call-with-input-file expanded
+                         (lambda (p) (get-line p)))))
+          content)
+        val))
+    val))
+
 (def (config-get-provider-key provider)
-  (provider-key-layers provider
-    env-key-for-provider
-    encrypted-key-for-provider
-    (lambda (p) (config-ref "providers" p "api_key"))
-    grok-auth-token))
+  (resolve-file-ref
+    (provider-key-layers provider
+      env-key-for-provider
+      encrypted-key-for-provider
+      (lambda (p) (config-ref "providers" p "api_key"))
+      grok-auth-token)))
 
 (def (config-model)
   (or (config-ref "model")