Skip to content

Commit 94ea208

Browse files
committed
fix(agent-ui): use string expressions for autoActive instead of functions
- Change autoActive type from function to string expression - Use new Function() to evaluate expressions safely - Support JSON-compatible configuration format - Add comprehensive expression syntax documentation
1 parent 8147295 commit 94ea208

5 files changed

Lines changed: 111 additions & 17 deletions

File tree

docs/runtime-settings-autoactive.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"link": "{prefix}/code-server/",
5252
"icon": "code",
5353
"behavior": "embed-frame",
54-
"autoActive": "(runtimeSettings) => runtimeSettings.devMode === true"
54+
"autoActive": "runtimeSettings.devMode === true"
5555
}
5656
]
5757
}
@@ -69,13 +69,25 @@
6969
"link": "{prefix}/vnc/index.html?autoconnect=true",
7070
"icon": "monitor",
7171
"behavior": "embed-frame",
72-
"autoActive": "(runtimeSettings) => runtimeSettings.sessionType === 'debugging' || runtimeSettings.autoActivateVNC === true"
72+
"autoActive": "runtimeSettings.sessionType === 'debugging' || runtimeSettings.autoActivateVNC === true"
7373
}
7474
]
7575
}
7676
}
7777
```
7878

79+
### 4. 支持的表达式语法
80+
81+
字符串表达式支持以下 JavaScript 语法:
82+
83+
- **比较运算符**: `===`, `!==`, `>`, `<`, `>=`, `<=`
84+
- **逻辑运算符**: `&&`, `||`, `!`
85+
- **属性访问**: `runtimeSettings.propertyName`
86+
- **数组访问**: `runtimeSettings.array[0]`
87+
- **字符串比较**: `runtimeSettings.sessionType === 'coding'`
88+
89+
**注意**: 表达式中只能访问 `runtimeSettings` 对象,不能使用其他变量或函数调用。
90+
7991
## 完整示例
8092

8193
参考 `examples/runtime-settings-autoactive.config.ts` 文件,其中包含:
@@ -98,7 +110,7 @@
98110
```typescript
99111
export interface WorkspaceNavItem {
100112
// ... 其他属性
101-
autoActive?: boolean | ((runtimeSettings: Record<string, any>) => boolean);
113+
autoActive?: boolean | string;
102114
}
103115
```
104116

@@ -118,6 +130,7 @@ export interface WorkspaceNavItem {
118130

119131
## 注意事项
120132

121-
1. **函数序列化**: 在 JSON 配置中,函数需要以字符串形式定义,在实际使用时需要解析为函数
133+
1. **表达式安全**: 字符串表达式通过 `new Function()` 执行,只能访问 `runtimeSettings` 对象
122134
2. **性能考虑**: runtime settings 变化时会重新计算 autoActive,避免过于复杂的计算逻辑
123-
3. **错误处理**: 当函数执行出错时,应该回退到不自动激活的安全状态
135+
3. **错误处理**: 当表达式执行出错时,会在控制台输出错误信息并回退到不自动激活的安全状态
136+
4. **表达式限制**: 不能在表达式中使用外部变量、函数调用或复杂的 JavaScript 语法

examples/runtime-settings-autoactive.config.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,23 @@ export default {
5757
icon: 'code',
5858
behavior: 'embed-frame',
5959
// Auto-activate when devMode is true OR autoActivateCodeServer is true
60-
autoActive: (runtimeSettings) => {
61-
return runtimeSettings.devMode === true || runtimeSettings.autoActivateCodeServer === true;
62-
},
60+
autoActive: "runtimeSettings.devMode === true || runtimeSettings.autoActivateCodeServer === true",
6361
},
6462
{
6563
title: 'VNC',
6664
link: '{prefix}/vnc/index.html?autoconnect=true',
6765
icon: 'monitor',
6866
behavior: 'embed-frame',
6967
// Auto-activate when sessionType is 'debugging' OR autoActivateVNC is true
70-
autoActive: (runtimeSettings) => {
71-
return runtimeSettings.sessionType === 'debugging' || runtimeSettings.autoActivateVNC === true;
72-
},
68+
autoActive: "runtimeSettings.sessionType === 'debugging' || runtimeSettings.autoActivateVNC === true",
7369
},
7470
{
7571
title: 'Terminal',
7672
link: '{prefix}/terminal/',
7773
icon: 'terminal',
7874
behavior: 'embed-frame',
7975
// Auto-activate only in dev mode
80-
autoActive: (runtimeSettings) => runtimeSettings.devMode === true,
76+
autoActive: "runtimeSettings.devMode === true",
8177
},
8278
{
8379
title: 'Browser',
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"title": "Runtime Settings AutoActive Example",
3+
"subtitle": "Demonstrating dynamic autoActive based on runtime settings",
4+
"workspace": {
5+
"navItems": [
6+
{
7+
"title": "Code Server",
8+
"link": "{prefix}/code-server/",
9+
"icon": "code",
10+
"behavior": "embed-frame",
11+
"autoActive": "runtimeSettings.devMode === true || runtimeSettings.autoActivateCodeServer === true"
12+
},
13+
{
14+
"title": "VNC",
15+
"link": "{prefix}/vnc/index.html?autoconnect=true",
16+
"icon": "monitor",
17+
"behavior": "embed-frame",
18+
"autoActive": "runtimeSettings.sessionType === 'debugging' || runtimeSettings.autoActivateVNC === true"
19+
},
20+
{
21+
"title": "Terminal",
22+
"link": "{prefix}/terminal/",
23+
"icon": "terminal",
24+
"behavior": "embed-frame",
25+
"autoActive": "runtimeSettings.devMode === true"
26+
},
27+
{
28+
"title": "Browser",
29+
"link": "{prefix}/browser/",
30+
"icon": "browser",
31+
"behavior": "new-page"
32+
}
33+
]
34+
},
35+
"server": {
36+
"runtimeSettings": {
37+
"schema": {
38+
"type": "object",
39+
"properties": {
40+
"autoActivateVNC": {
41+
"type": "boolean",
42+
"title": "Auto-activate VNC",
43+
"description": "Automatically open VNC when session starts",
44+
"default": false,
45+
"placement": "chat-bottom"
46+
},
47+
"autoActivateCodeServer": {
48+
"type": "boolean",
49+
"title": "Auto-activate Code Server",
50+
"description": "Automatically open Code Server when session starts",
51+
"default": false,
52+
"placement": "chat-bottom"
53+
},
54+
"devMode": {
55+
"type": "boolean",
56+
"title": "Development Mode",
57+
"description": "Enable development tools and auto-activate Code Server",
58+
"default": false,
59+
"placement": "chat-bottom"
60+
},
61+
"sessionType": {
62+
"type": "string",
63+
"title": "Session Type",
64+
"enum": ["coding", "debugging", "testing"],
65+
"enumLabels": ["Coding", "Debugging", "Testing"],
66+
"default": "coding",
67+
"placement": "chat-bottom"
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}

multimodal/tarko/agent-ui/src/standalone/navbar/Navbar.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,18 @@ export const Navbar: React.FC = () => {
7575
return false;
7676
}
7777

78-
// Handle both boolean and function forms of autoActive
78+
// Handle both boolean and string expression forms of autoActive
7979
if (typeof item.autoActive === 'boolean') {
8080
return item.autoActive;
81-
} else if (typeof item.autoActive === 'function') {
82-
return item.autoActive(runtimeSettings);
81+
} else if (typeof item.autoActive === 'string') {
82+
try {
83+
// Create a function from the string expression
84+
const evaluateExpression = new Function('runtimeSettings', `return ${item.autoActive}`);
85+
return evaluateExpression(runtimeSettings);
86+
} catch (error) {
87+
console.error(`Failed to evaluate autoActive expression "${item.autoActive}":`, error);
88+
return false;
89+
}
8390
}
8491

8592
return false;

multimodal/tarko/interface/src/web-ui-implementation.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ export interface WorkspaceNavItem {
5353
/**
5454
* Auto-activate the navigation item when true
5555
* Only applicable when behavior is 'embed-frame'
56-
* Can be a boolean or a function that receives runtime settings and returns boolean
56+
* Can be a boolean or a string expression that evaluates runtime settings
57+
* String expressions can reference runtimeSettings properties and use JavaScript operators
5758
* @defaultValue false
59+
* @example
60+
* true // Static boolean
61+
* "runtimeSettings.devMode === true" // Simple expression
62+
* "runtimeSettings.sessionType === 'debugging' || runtimeSettings.autoActivateVNC === true" // Complex expression
5863
*/
59-
autoActive?: boolean | ((runtimeSettings: Record<string, any>) => boolean);
64+
autoActive?: boolean | string;
6065
}
6166

6267
/**

0 commit comments

Comments
 (0)