@@ -33,8 +33,20 @@ export abstract class BaseMidsceneTools implements IMidsceneTools {
3333 return [ ] ;
3434 }
3535
36+ /**
37+ * Optional: create a temporary device instance to read actionSpace
38+ * This allows getting real actionSpace without connecting to device
39+ */
40+ protected createTemporaryDevice ?( ) : any {
41+ return undefined ;
42+ }
43+
3644 /**
3745 * Initialize all tools by querying actionSpace
46+ * Uses three-layer fallback strategy:
47+ * 1. Try to get actionSpace from connected agent
48+ * 2. Create temporary device instance to read actionSpace
49+ * 3. Use hardcoded default actionSpace
3850 */
3951 public async initTools ( ) : Promise < void > {
4052 this . toolDefinitions = [ ] ;
@@ -44,16 +56,33 @@ export abstract class BaseMidsceneTools implements IMidsceneTools {
4456 const platformTools = this . preparePlatformTools ( ) ;
4557 this . toolDefinitions . push ( ...platformTools ) ;
4658
47- // 2. Try to get agent and its action space
59+ // 2. Try to get agent and its action space (three-layer fallback)
4860 let actionSpace : any [ ] ;
4961 try {
62+ // Layer 1: Try to use connected agent
5063 const agent = await this . ensureAgent ( ) ;
5164 actionSpace = await agent . getActionSpace ( ) ;
52- debug ( 'Action space:' , actionSpace . map ( ( a : any ) => a . name ) . join ( ', ' ) ) ;
65+ debug ( 'Action space from connected agent :' , actionSpace . map ( ( a : any ) => a . name ) . join ( ', ' ) ) ;
5366 } catch ( error ) {
54- // If agent initialization fails, use default action space
55- debug ( 'Using default action space due to initialization failure' ) ;
56- actionSpace = this . getDefaultActionSpace ( ) ;
67+ debug ( 'Failed to get action space from agent, trying temporary device' ) ;
68+
69+ try {
70+ // Layer 2: Create temporary device instance to read actionSpace
71+ if ( this . createTemporaryDevice ) {
72+ const tempDevice = this . createTemporaryDevice ( ) ;
73+ actionSpace = tempDevice . actionSpace ( ) ;
74+ debug ( 'Action space from temporary device:' , actionSpace . map ( ( a : any ) => a . name ) . join ( ', ' ) ) ;
75+
76+ // Destroy temporary instance using optional chaining
77+ await tempDevice . destroy ?.( ) ;
78+ } else {
79+ throw new Error ( 'createTemporaryDevice not implemented' ) ;
80+ }
81+ } catch ( fallbackError ) {
82+ // Layer 3: Use hardcoded default actionSpace
83+ debug ( 'Using default action space due to all failures' ) ;
84+ actionSpace = this . getDefaultActionSpace ( ) ;
85+ }
5786 }
5887
5988 // 3. Generate tools from action space (core innovation)
0 commit comments