@@ -768,7 +768,6 @@ async def _run_openclaw_cmd():
768768
769769 if list_sessions_result .exit_code != 0 :
770770 raise Exception (list_sessions_result .stderr )
771-
772771 try :
773772 data = json .loads (list_sessions_result .stdout )
774773 except json .decoder .JSONDecodeError :
@@ -846,39 +845,70 @@ async def _run_openclaw_cmd():
846845
847846 prefix = "\n \n " .join (prefix_parts ) + "\n \n "
848847 final_user_prompt = prefix + user_prompt
848+ collected_text_chunks : list [str ] = []
849+ # 确保扩展名在路径最后一个 / 之后
850+ file_regex = re .compile (r"^- `?(\/\S+\/[^/\s]+\.[^/\s`]+)`?$" , re .MULTILINE )
851+
849852 async for event in oc_chat_completions_sse (
850853 oc_session_key = oc_session_key ,
851854 user_prompt = final_user_prompt ,
852855 timeout = aiohttp .ClientTimeout (total = None , sock_read = None , connect = 30 ),
853856 ):
854857 # event 是 bytes,需要对应处理
855858 if event .strip () == b"data: [DONE]" :
856-
857- # check_cmd = """find . -maxdepth 4 \( -path "./claude" -o -path "./claude/*" -o -path "./node_modules" -o -path "./node_modules/*" -o -path "./.git" -o -path "./.git/*" -o -path "./venv" -o -path "./venv/*" -o -path "./.venv" -o -path "./.venv/*" -o -path "./env" -o -path "./env/*" -o -path "./__pycache__" -o -path "./__pycache__/*" \) -prune -o -type f \( -name "package.json" -o -name "pnpm-lock.yaml" -o -name "yarn.lock" -o -name "package-lock.json" -o -name "next.config.*" -o -name "vite.config.*" -o -name "vue.config.*" -o -name "nuxt.config.*" -o -name "svelte.config.*" -o -name "astro.config.*" -o -name "remix.config.*" -o -name "angular.json" -o -name "gatsby-config.*" -o -path "./index.html" -o -path "*/public/index.html" -o -path "./server.js" -o -path "./app.js" -o -path "./index.js" -o -path "./main.js" -o -path "./server.ts" -o -path "./app.ts" -o -path "./index.ts" -o -path "./main.ts" -o -path "./src/index.js" -o -path "./src/index.ts" -o -path "./src/index.jsx" -o -path "./src/index.tsx" -o -path "./src/main.js" -o -path "./src/main.ts" -o -path "./src/main.jsx" -o -path "./src/main.tsx" -o -path "./src/App.vue" -o -path "./src/app.js" -o -path "./src/app.ts" -o -path "./src/app.jsx" -o -path "./src/app.tsx" -o -path "./src/server.js" -o -path "./src/server.ts" -o \( -path "./src/*" -a \( -name "*.js" -o -name "*.ts" -o -name "*.jsx" -o -name "*.tsx" -o -name "*.vue" \) \) \)"""
859+ # 在 DONE 时,从之前累计的文本块里提取路径并插入一个额外 chunk
860+ combined = "" .join (collected_text_chunks )
861+ matches = file_regex .findall (combined )
862+ if matches :
863+ # 校验路径是否存在,只保留存在的
864+ valid_matches = [m for m in matches if os .path .isfile (m )]
865+ diff_file_check_info = json .dumps ({
866+ "type" : "result" ,
867+ "is_error" : False ,
868+ "result" : "\n " .join (f"-`{ m } `" for m in valid_matches ) if valid_matches else "" ,
869+ }, ensure_ascii = False )
870+ else :
871+ diff_file_check_info = json .dumps ({
872+ "type" : "result" ,
873+ "is_error" : False ,
874+ "result" : "" ,
875+ }, ensure_ascii = False )
876+ diff_file_chunk = gpt_stream_chunk (diff_file_check_info )
877+ yield f"data: { json .dumps (diff_file_chunk , ensure_ascii = False )} \n \n " .encode ("utf-8" )
858878
859879 if payload .enable_pre_deploy_check :
860-
861880 pre_deploy_check_result = await runner .exec_json (command = check_cmd , cwd = workspace_path )
862881 if pre_deploy_check_result .exit_code == 0 :
863- if pre_deploy_check_result .stdout :
864- deploy_check_info = json .dumps ({
865- "type" : "pre_deploy_check" ,
866- "success" : True ,
867- "find_file" : pre_deploy_check_result .stdout ,
868- })
869- else :
870- deploy_check_info = json .dumps ({
871- "type" : "pre_deploy_check" ,
872- "success" : False ,
873- "find_file" : pre_deploy_check_result .stdout ,
874- })
882+ deploy_check_info = json .dumps ({
883+ "type" : "pre_deploy_check" ,
884+ "success" : bool (pre_deploy_check_result .stdout ),
885+ "find_file" : pre_deploy_check_result .stdout ,
886+ }, ensure_ascii = False )
875887
876888 # 插入自定义文本 chunk
877- chunk = gpt_stream_chunk (f" { json . dumps ( deploy_check_info , ensure_ascii = False ) } " )
889+ chunk = gpt_stream_chunk (deploy_check_info )
878890 yield f"data: { json .dumps (chunk , ensure_ascii = False )} \n \n " .encode ("utf-8" )
891+
879892 # 放行 [DONE]
880893 yield event
881894 else :
895+ # 非 DONE:透传,同时尽量从 OpenAI chunk 中抽取文本累计
896+ try :
897+ line = event .decode ("utf-8" , errors = "ignore" ).strip ()
898+ if line .startswith ("data: " ):
899+ payload_json = line [6 :]
900+ if payload_json and payload_json != "[DONE]" :
901+ obj = json .loads (payload_json )
902+ content = (
903+ obj .get ("choices" , [{}])[0 ]
904+ .get ("delta" , {})
905+ .get ("content" )
906+ )
907+ if isinstance (content , str ) and content :
908+ collected_text_chunks .append (content )
909+ except Exception :
910+ pass
911+
882912 yield event
883913
884914 # 处理messages
0 commit comments