@@ -2,15 +2,10 @@ import fs from "node:fs";
22import path from "node:path" ;
33import { listAgentIds , resolveAgentDir } from "../agents/agent-scope.js" ;
44import type { OpenClawConfig } from "../config/types.openclaw.js" ;
5- import { formatErrorMessage } from "../infra/errors.js" ;
65import { resolveUserPath } from "../utils.js" ;
76import { listAuthProfileStoreAgentDirs as listAuthProfileStoreAgentDirsFromAuthStorePaths } from "./auth-store-paths.js" ;
87import { parseEnvValue } from "./shared.js" ;
98
10- function isJsonObject ( value : unknown ) : value is Record < string , unknown > {
11- return typeof value === "object" && value !== null && ! Array . isArray ( value ) ;
12- }
13-
149export function parseEnvAssignmentValue ( raw : string ) : string {
1510 return parseEnvValue ( raw ) ;
1611}
@@ -19,24 +14,6 @@ export function listAuthProfileStoreAgentDirs(config: OpenClawConfig, stateDir:
1914 return listAuthProfileStoreAgentDirsFromAuthStorePaths ( config , stateDir ) ;
2015}
2116
22- export function listLegacyAuthJsonPaths ( stateDir : string ) : string [ ] {
23- const out : string [ ] = [ ] ;
24- const agentsRoot = path . join ( resolveUserPath ( stateDir ) , "agents" ) ;
25- if ( ! fs . existsSync ( agentsRoot ) ) {
26- return out ;
27- }
28- for ( const entry of fs . readdirSync ( agentsRoot , { withFileTypes : true } ) ) {
29- if ( ! entry . isDirectory ( ) ) {
30- continue ;
31- }
32- const candidate = path . join ( agentsRoot , entry . name , "agent" , "auth.json" ) ;
33- if ( fs . existsSync ( candidate ) ) {
34- out . push ( candidate ) ;
35- }
36- }
37- return out ;
38- }
39-
4017function resolveActiveAgentDir ( stateDir : string , env : NodeJS . ProcessEnv = process . env ) : string {
4118 const override = env . OPENCLAW_AGENT_DIR ?. trim ( ) || env . PI_CODING_AGENT_DIR ?. trim ( ) ;
4219 if ( override ) {
@@ -76,62 +53,3 @@ export function listAgentModelCatalogDirs(
7653
7754 return [ ...dirs ] ;
7855}
79-
80- export type ReadJsonObjectOptions = {
81- maxBytes ?: number ;
82- requireRegularFile ?: boolean ;
83- } ;
84-
85- export function readJsonObjectIfExists ( filePath : string ) : {
86- value : Record < string , unknown > | null ;
87- error ?: string ;
88- } ;
89- export function readJsonObjectIfExists (
90- filePath : string ,
91- options : ReadJsonObjectOptions ,
92- ) : {
93- value : Record < string , unknown > | null ;
94- error ?: string ;
95- } ;
96- export function readJsonObjectIfExists (
97- filePath : string ,
98- options : ReadJsonObjectOptions = { } ,
99- ) : {
100- value : Record < string , unknown > | null ;
101- error ?: string ;
102- } {
103- if ( ! fs . existsSync ( filePath ) ) {
104- return { value : null } ;
105- }
106- try {
107- const stats = fs . statSync ( filePath ) ;
108- if ( options . requireRegularFile && ! stats . isFile ( ) ) {
109- return {
110- value : null ,
111- error : `Refusing to read non-regular file: ${ filePath } ` ,
112- } ;
113- }
114- if (
115- typeof options . maxBytes === "number" &&
116- Number . isFinite ( options . maxBytes ) &&
117- options . maxBytes >= 0 &&
118- stats . size > options . maxBytes
119- ) {
120- return {
121- value : null ,
122- error : `Refusing to read oversized JSON (${ stats . size } bytes): ${ filePath } ` ,
123- } ;
124- }
125- const raw = fs . readFileSync ( filePath , "utf8" ) ;
126- const parsed : unknown = JSON . parse ( raw ) ;
127- if ( ! isJsonObject ( parsed ) ) {
128- return { value : null } ;
129- }
130- return { value : parsed } ;
131- } catch ( err ) {
132- return {
133- value : null ,
134- error : formatErrorMessage ( err ) ,
135- } ;
136- }
137- }
0 commit comments