Skip to content

Commit

Permalink
Refine the logs to allow copy.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Feb 25, 2024
1 parent 0d10ac7 commit 55a4018
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function AppImpl({info, verbose, robot, robotReady, stageUUID, playerRef}) {
return response.json();
}).then((data) => {
verbose(`ASR: Upload success: ${data.data.rid} ${data.data.asr}`);
info(`You: ${data.data.asr}`);
info('user', `${data.data.asr}`);
resolve(data.data.rid);
}).catch((error) => reject(error));
});
Expand All @@ -145,7 +145,7 @@ function AppImpl({info, verbose, robot, robotReady, stageUUID, playerRef}) {
}).then((data) => {
if (data?.data?.asid) {
verbose(`TTS: Audio ready: ${data.data.asid} ${data.data.tts}`);
info(`Bot: ${data.data.tts}`);
info('bot', `${data.data.tts}`);
}
resolve(data.data);
}).catch(error => reject(error));
Expand Down Expand Up @@ -221,21 +221,21 @@ function AppImpl({info, verbose, robot, robotReady, stageUUID, playerRef}) {
ref.current.mediaRecorder.stop();
});

info(''); // Insert a empty line to show loading of user input.
info('user', ''); // Insert a empty line to show loading of user input.
setTalking(false);
setMicWorking(false);
setProcessing(true);
verbose(`Event: Recoder stopped, chunks=${ref.current.audioChunks.length}`);

if (userMayInput < durationRequiredUserInput) {
info(`System: You didn't say anything!`);
info('sys', `System: You didn't say anything!`);
alert(`Warning: You didn't say anything!`);
} else {
try {
await processUserInput(userMayInput);
} catch (e) {
info(`System: Server error ${e}`);
info(`System: Please try again.`);
info('sys', `System: Server error ${e}`);
info('sys', `System: Please try again.`);
alert(`System: Server error ${e}`);
}
}
Expand Down
21 changes: 15 additions & 6 deletions src/debugPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ export function useDebugPanel(playerRef) {
});

// Write a summary or info log, which is important but short message for user.
const info = React.useCallback((msg) => {
const info = React.useCallback((label, msg) => {
console.log(`[info] ${buildLog(msg)}`);
ref.current.infoLogs = [...ref.current.infoLogs, msg];

const last = ref.current.infoLogs.length > 0 ? ref.current.infoLogs[ref.current.infoLogs.length - 1] : null;
if (last && last.label === label && last.msg && msg) {
last.msg = `${last.msg} ${msg}`;
ref.current.infoLogs = [...ref.current.infoLogs];
} else {
ref.current.infoLogs = [...ref.current.infoLogs, {label, msg}];
}

setInfoLogs(ref.current.infoLogs);
}, [ref, setInfoLogs]);

Expand Down Expand Up @@ -70,12 +78,13 @@ export function useDebugPanel(playerRef) {
return (<div key={index}>{log}</div>);
})}
{!showVerboseLogs && infoLogs.map((log, index) => {
const you = log.indexOf('You:') === 0;
const bot = log.indexOf('Bot:') === 0;
const you = log.label === 'user';
const bot = log.label === 'bot';
const color = you ? 'darkgreen' : (bot ? 'darkblue' : '');
const fontWeight = you ? 'bold' : 'normal';
const msg = log ? log : index === infoLogs.length - 1 ? <div><br/><b>Loading...</b></div> : <br/>;
return (<div key={index} style={{color,fontWeight}}>{msg}</div>);
const msg = log.msg ? log.msg : index === infoLogs.length - 1 ? <div><br/><b>Loading...</b></div> : <br/>;
const prefix = log.msg && log.label ? `${{'sys':'System', 'user':'You', 'bot':'Bot'}[log.label]}: ` : '';
return (<div key={index} style={{color,fontWeight}}>{prefix}{msg}</div>);
})}
</div>
<div style={{ float:"left", clear: "both" }} ref={logPanelRef}/>
Expand Down
10 changes: 5 additions & 5 deletions src/robotInitiator.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function useRobotInitiator(info, verbose, playerRef) {
const isLo = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
const isHttps = window.location.protocol === 'https:';
const securityAllowed = isLo || isHttps;
securityAllowed || info(`App started, allowed=${securityAllowed}`);
securityAllowed || info('sys', `App started, allowed=${securityAllowed}`);
securityAllowed || alert(`HTTPS is required!`);
verbose(`App started, allowed=${securityAllowed}, lo=${isLo}, https=${isHttps}`);
if (!securityAllowed) return;
Expand Down Expand Up @@ -91,7 +91,7 @@ export function useRobotInitiator(info, verbose, playerRef) {
const robot = data.data.robots.find(robot => robot.uuid === config.uuid);
if (robot) {
setPreviewRobot(robot);
info(`Use robot ${robot.label}`);
info('sys', `Use robot ${robot.label}`);
verbose(`Use previous robot ${robot.label} ${robot.uuid}`);
}
}
Expand All @@ -111,12 +111,12 @@ export function useRobotInitiator(info, verbose, playerRef) {
playerRef.current.removeEventListener('ended', listener);

setRobotReady(true);
info(`Conversation started, AI is ready`);
info('sys', `Conversation started, AI is ready`);
verbose(`Stage started, AI is ready, sid=${stageUUID}`);
};
playerRef.current.addEventListener('ended', listener);

info(''); // Insert a empty line to show loading the resource.
info('sys', ''); // Insert a empty line to show loading the resource.
playerRef.current.src = `/api/ai-talk/examples/${previewRobot.voice}?sid=${stageUUID}`;
playerRef.current.play().catch(error => alert(`Play error: ${error}`));
}, [info, verbose, playerRef, setStageRobot, previewRobot, stageUUID, robotReady]);
Expand All @@ -128,7 +128,7 @@ export function useRobotInitiator(info, verbose, playerRef) {
const robot = availableRobots.find(robot => robot.uuid === e.target.value);
setPreviewRobot(robot);
RobotConfig.save(robot);
info(`Change robot to ${robot.label}`);
info('sys', `Change robot to ${robot.label}`);
verbose(`Change to robot ${robot.label} ${robot.uuid}`);
}, [info, verbose, availableRobots, setPreviewRobot]);

Expand Down
5 changes: 0 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ export function buildTimeString() {
}

export function buildLog(msg) {
const date = new Date();
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');

const log = `[${buildTimeString()}]: ${msg}`;
console.log(log);
return log;
Expand Down

0 comments on commit 55a4018

Please sign in to comment.