88import type { OpenClawApi } from "./client/types.js" ;
99import { PluginSession } from "./session.js" ;
1010import { registerPhantomTools } from "./tools/register-tools.js" ;
11+ import { PluginConfigSchema } from "@phantom/cli" ;
1112
1213// Singleton session instance
1314let sessionInstance : PluginSession | null = null ;
1415const PLUGIN_ID = "phantom-openclaw-plugin" ;
1516
16- const STRING_CONFIG_KEYS = [
17- "PHANTOM_AUTH_BASE_URL" ,
18- "PHANTOM_CONNECT_BASE_URL" ,
19- "PHANTOM_WALLETS_API_BASE_URL" ,
20- "PHANTOM_API_BASE_URL" ,
21- "PHANTOM_VERSION" ,
22- "PHANTOM_CALLBACK_PATH" ,
23- "PHANTOM_MCP_DEBUG" ,
24- ] as const ;
25-
2617function isRecord ( value : unknown ) : value is Record < string , unknown > {
2718 return typeof value === "object" && value !== null ;
2819}
@@ -60,29 +51,14 @@ function getPluginConfig(fullConfig?: Record<string, unknown>): Record<string, u
6051}
6152
6253function applyConfigToEnv ( config ?: Record < string , unknown > ) : void {
63- if ( ! config ) {
64- return ;
65- }
66-
67- for ( const key of STRING_CONFIG_KEYS ) {
68- const value = config [ key ] ;
69- if ( typeof value === "string" && value . trim ( ) . length > 0 ) {
70- process . env [ key ] = value . trim ( ) ;
71- }
72- }
73-
74- const rawPort = config . PHANTOM_CALLBACK_PORT ;
75- let parsedPort : number | null = null ;
54+ if ( ! config ) return ;
7655
77- if ( typeof rawPort === "number" ) {
78- parsedPort = rawPort ;
79- } else if ( typeof rawPort === "string" ) {
80- const parsed = Number . parseInt ( rawPort , 10 ) ;
81- parsedPort = Number . isNaN ( parsed ) ? null : parsed ;
82- }
56+ const pluginConfig = PluginConfigSchema . parse ( config ) ;
8357
84- if ( parsedPort !== null && Number . isInteger ( parsedPort ) && parsedPort > 0 && parsedPort <= 65535 ) {
85- process . env . PHANTOM_CALLBACK_PORT = String ( parsedPort ) ;
58+ for ( const [ key , value ] of Object . entries ( pluginConfig ) ) {
59+ if ( value !== undefined ) {
60+ process . env [ key ] = String ( value ) ;
61+ }
8662 }
8763}
8864
@@ -93,9 +69,8 @@ function getSession(config?: Record<string, unknown>): PluginSession {
9369 if ( ! sessionInstance ) {
9470 const pluginConfig = getPluginConfig ( config ) ;
9571 applyConfigToEnv ( pluginConfig ) ;
96- const envPort = process . env . PHANTOM_CALLBACK_PORT ?. trim ( ) ;
97- const parsedPort = envPort ? Number . parseInt ( envPort , 10 ) : NaN ;
98- const callbackPort = Number . isInteger ( parsedPort ) && parsedPort > 0 && parsedPort <= 65535 ? parsedPort : undefined ;
72+ const envPort = process . env . PHANTOM_CALLBACK_PORT ;
73+ const callbackPort = envPort ? Number . parseInt ( envPort , 10 ) : undefined ;
9974
10075 sessionInstance = new PluginSession ( {
10176 callbackPort,
0 commit comments