1+ import * as fs from 'fs' ;
2+ import * as path from 'path' ;
13import { Device } from "../device/device" ;
24import { Event } from "../event/event" ;
35import { EventBuilder } from "../event/event_builder" ;
@@ -23,13 +25,10 @@ export class LLMGuidedPolicy extends PTGPolicy {
2325 private text : string ;
2426 private actionList :string [ ] ;
2527
26- // GPT configuration
27- private static readonly GPT_CONFIG = {
28- baseURL : 'https://api.chatanywhere.tech/v1' ,
29- apiKey : 'sk-t6Dn1cwNdxVCkNE8jSPYqna47G0SY0yuDC5ajQeP9OkNo97f'
30- } ;
28+ // GPT 配置
29+ private static GPT_CONFIG : { baseURL : string ; apiKey : string } ;
3130
32- private openai = new OpenAI ( LLMGuidedPolicy . GPT_CONFIG ) ;
31+ private openai : OpenAI ;
3332
3433 constructor ( device : Device , hap : Hap , name : PolicyName , ptg : PTG ) {
3534 super ( device , hap , name , true ) ;
@@ -41,27 +40,47 @@ export class LLMGuidedPolicy extends PTGPolicy {
4140 this . ptg = ptg ;
4241
4342 this . taskPrompt = new String ( "You are an expert in App GUI testing. Please guide the testing tool to enhance the coverage of functional scenarios in testing the App based on your extensive App testing experience. " ) ; // initial task prompt
43+
44+ // 加载 GPT 配置
45+ LLMGuidedPolicy . GPT_CONFIG = this . loadConfig ( ) ;
46+
47+ // 初始化 OpenAI 客户端
48+ this . openai = new OpenAI ( LLMGuidedPolicy . GPT_CONFIG ) ;
49+ }
50+
51+
52+ private loadConfig ( ) : { baseURL : string ; apiKey : string } {
53+ const configPath = path . resolve ( __dirname , '../../config.json' ) ; // 配置文件路径
54+ try {
55+ const configData = fs . readFileSync ( configPath , 'utf-8' ) ;
56+ const config = JSON . parse ( configData ) ;
57+ return config . GPT_CONFIG ;
58+ } catch ( error ) {
59+ this . logger . error ( `加载配置文件失败: ${ error } ` ) ;
60+ throw new Error ( "无法加载 GPT 配置,请检查配置文件是否存在且格式正确。" ) ;
61+ }
4462 }
4563
46- // Add a buffer to the class to store asynchronously fetched events
64+ // 给类添加一个缓冲区来保存异步获取的事件
4765 private pendingEvent : Event | null = null ;
4866 private eventFetching : boolean = false ;
4967
5068 generateEventBasedOnPtg ( ) : Event {
5169 this . updateState ( ) ;
5270
53- // If an event has already been fetched asynchronously, return it directly
71+ // 如果已经异步拿到一个事件了,就直接返回它
5472 if ( this . pendingEvent ) {
5573 const event = this . pendingEvent ;
5674 this . pendingEvent = null ;
75+ this . eventFetching = false ;
5776 return event ;
5877 }
5978
6079 if ( ! this . eventFetching ) {
61- this . logger . info ( "Start asynchronous call to selectEventFromLLM" ) ;
62- // Start asynchronous logic (only once)
80+ this . logger . info ( "开始异步调用 selectEventFromLLM" ) ;
81+ // 启动异步逻辑(只启动一次)
6382 this . eventFetching = true ;
64- // Start asynchronous logic to fetch events and cache them in pendingEvent
83+ // 启动异步逻辑获取事件,缓存到 pendingEvent
6584 this . selectEventFromLLM ( ) . then ( event => {
6685 if ( event === undefined ) {
6786 if ( this . retryCount > MAX_NUM_RESTARTS ) {
@@ -74,14 +93,13 @@ export class LLMGuidedPolicy extends PTGPolicy {
7493 } else {
7594 this . retryCount = 0 ;
7695 this . pendingEvent = event ;
77- this . logger . info ( "selectEventFromLLM successfully returned " ) ;
96+ this . logger . info ( "selectEventFromLLM 成功返回 " ) ;
7897 }
7998 } ) . catch ( err => {
8099 this . logger . error ( `selectEventFromLLM failed: ${ err } ` ) ;
81100 this . pendingEvent = EventBuilder . createRandomTouchEvent ( this . device ) ;
82101 } ) . finally ( ( ) => {
83102 this . logger . info ( "selectEventFromLLM finally" ) ;
84- this . eventFetching = false ;
85103 } ) ;
86104 }
87105
0 commit comments