Skip to content

Commit fe7e42c

Browse files
feat: Variable splitting: 1) Use jsonpath-ng.ext; 2) Cache parse result; 3) upgrade jsonpath-ng to 1.8.0. (#4908)
1 parent d17adaf commit fe7e42c

File tree

6 files changed

+33
-10
lines changed

6 files changed

+33
-10
lines changed

apps/application/flow/step_node/variable_splitting_node/impl/base_variable_splitting_node.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,26 @@
77
@desc:
88
"""
99
import json
10-
from jsonpath_ng import parse
10+
from jsonpath_ng.ext import parse
11+
from common.cache.mem_cache import MemCache
1112

1213
from application.flow.i_step_node import NodeResult
1314
from application.flow.step_node.variable_splitting_node.i_variable_splitting_node import IVariableSplittingNode
1415

16+
jsonpath_expr_cache = MemCache('parse_path', {
17+
'TIMEOUT': 3600, # 缓存有效期为 1 小时
18+
'OPTIONS': {
19+
'MAX_ENTRIES': 1000, # 最多缓存 500 个条目
20+
'CULL_FREQUENCY': 10, # 达到上限时,删除约 1/10 的缓存
21+
},
22+
})
23+
24+
def parse_and_cache(path):
25+
jsonpath_expr = jsonpath_expr_cache.get(path)
26+
if not jsonpath_expr:
27+
jsonpath_expr = parse(path)
28+
jsonpath_expr_cache.set(path, jsonpath_expr)
29+
return jsonpath_expr
1530

1631
def smart_jsonpath_search(data: dict, path: str):
1732
"""
@@ -21,7 +36,7 @@ def smart_jsonpath_search(data: dict, path: str):
2136
- 多个匹配: 返回值的列表
2237
- 无匹配: 返回None
2338
"""
24-
jsonpath_expr = parse(path)
39+
jsonpath_expr = parse_and_cache(path)
2540
matches = jsonpath_expr.find(data)
2641

2742
if not matches:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ dependencies = [
6161
"websockets==15.0.1",
6262
"pylint==3.3.7",
6363
"cohere==5.17.0",
64-
"jsonpath-ng==1.7.0",
64+
"jsonpath-ng==1.8.0",
6565
]
6666

6767
[tool.uv]

ui/src/locales/lang/en-US/workflow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ You are a master of problem optimization, adept at accurately inferring user int
507507
expression: {
508508
label: 'Expression',
509509
placeholder: 'Please enter expression',
510-
tooltip: 'Please use JSON Path expressions to split variables, e.g.: $.store.book',
510+
tooltip: 'Please use JSON Path expressions to split variables, e.g.: $.store.book <a href="https://pypi.org/project/jsonpath-ng/1.8.0/" target="_blank" class="expression_tip">Click for details ➜ pypi.org</a>',
511511
},
512512
},
513513
parameterExtractionNode: {

ui/src/locales/lang/zh-CN/workflow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export default {
498498
expression: {
499499
label: '表达式',
500500
placeholder: '请输入表达式',
501-
tooltip: '请使用JSON Path 表达式拆分变量,例如:$.store.book',
501+
tooltip: '请使用 JSON Path 表达式拆分变量,例如:$.store.book <a href="https://pypi.org/project/jsonpath-ng/1.8.0/" target="_blank" class="expression_tip">点击查看详情 ➜ pypi.org</a>',
502502
},
503503
},
504504
parameterExtractionNode: {

ui/src/locales/lang/zh-Hant/workflow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ export default {
492492
expression: {
493493
label: '表達式',
494494
placeholder: '請輸入表達式',
495-
tooltip: '請使用 JSON Path 表達式拆分變量,例如:$.store.book',
495+
tooltip: '請使用 JSON Path 表達式拆分變量,例如:$.store.book <a href="https://pypi.org/project/jsonpath-ng/1.8.0/" target="_blank" class="expression_tip">點擊查看詳情 ➜ pypi.org</a>',
496496
},
497497
},
498498
parameterExtractionNode: {

ui/src/workflow/nodes/variable-splitting/component/VariableFieldDialog.vue

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@
5757
>
5858
<el-tooltip
5959
effect="dark"
60-
:content="
61-
$t('workflow.nodes.variableSplittingNode.expression.tooltip')
62-
"
6360
placement="right"
6461
>
62+
<template #content>
63+
<span v-html="$t('workflow.nodes.variableSplittingNode.expression.tooltip')"></span>
64+
</template>
6565
<AppIcon iconName="app-warning" class="app-warning-icon"></AppIcon>
6666
</el-tooltip>
6767
</div>
@@ -161,4 +161,12 @@ const submit = async (formEl: FormInstance | undefined) => {
161161
162162
defineExpose({ open, close })
163163
</script>
164-
<style lang="scss" scoped></style>
164+
<style lang="scss">
165+
.expression_tip {
166+
color: var(--el-color-primary-light-5);
167+
168+
&:hover {
169+
color: var(--el-color-primary-light-3);
170+
}
171+
}
172+
</style>

0 commit comments

Comments
 (0)