Skip to content

Execution logger #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Thumbs.db
# npm package lock files (if you don't want them tracked by Git)
package-lock.json
yarn.lock
pnpm-lock.yaml

# Test coverage directory
coverage/
Expand Down
1 change: 0 additions & 1 deletion eko-browser-extension-template
Submodule eko-browser-extension-template deleted from 53e406
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
"html2canvas": "^1.4.1",
"openai": "^4.77.0",
"playwright": "^1.49.1",
"rotating-file-stream": "^3.2.6",
"tslog": "^4.9.3",
"uuid": "^11.0.3",
"zod": "^3.22.4"
},
Expand Down
47 changes: 47 additions & 0 deletions src/common/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Logger, ILogObj } from 'tslog';
import { join } from 'path';
import { tmpdir } from 'os';

export class EkoLogger {
private logger: Logger<ILogObj>;
private logFilePath: string;

constructor() {
const now = new Date();
const timestamp = now.toISOString().replace(/[-:.TZ]/g, '').slice(0, 14);

const logFileName = `Eko-${timestamp}.log`;
this.logFilePath = join(tmpdir(), logFileName);

this.logger = new Logger({
name: "EkoLog",
overwrite: {
mask: (...args: any[]): unknown[] => {
return args.map((arg) => {
return this.toReadableString(arg);
});
},
},
});

// log file path
this.logger.info(`log file path at: ${this.logFilePath}`);
}

// truncate content if it exceeds 2048 characters
private toReadableString(content: any): string {
const contentString = JSON.stringify(content);
const maxLength = 2048;
if (contentString.length > maxLength) {
return contentString.substring(0, maxLength) + '...';
} else {
return contentString;
}
}

public getLogger(): Logger<ILogObj> {
return this.logger;
}
}

export const logger = new EkoLogger().getLogger();
3 changes: 2 additions & 1 deletion src/common/tools/cancel_workflow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CancelWorkflowInput } from '../../types/tools.types';
import { Tool, InputSchema, ExecutionContext } from '../../types/action.types';
import { logger } from '../../common/log';

export class CancelWorkflow implements Tool<CancelWorkflowInput, void> {
name: string;
Expand All @@ -26,7 +27,7 @@ export class CancelWorkflow implements Tool<CancelWorkflowInput, void> {
throw new Error('Invalid parameters. Expected an object with a "reason" property.');
}
const reason = params.reason;
console.log("The workflow has been cancelled because: " + reason);
logger.debug("The workflow has been cancelled because: " + reason);
await context.workflow?.cancel();
return;
}
Expand Down
37 changes: 19 additions & 18 deletions src/common/tools/human.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
HumanOperateResult,
} from '../../types/tools.types';
import { Tool, InputSchema, ExecutionContext } from '../../types/action.types';
import { logger } from '../../common/log';

export class HumanInputText implements Tool<HumanInputTextInput, HumanInputTextResult> {
name: string;
Expand All @@ -35,20 +36,20 @@ export class HumanInputText implements Tool<HumanInputTextInput, HumanInputTextR
throw new Error('Invalid parameters. Expected an object with a "question" property.');
}
const question = params.question;
console.log("question: " + question);
logger.debug("question: " + question);
let onHumanInputText = context.callback?.hooks.onHumanInputText;
if (onHumanInputText) {
let answer;
try {
answer = await onHumanInputText(question);
} catch (e) {
console.error(e);
logger.error(e);
return {status: "Error: Cannot get user's answer.", answer: ""};
}
console.log("answer: " + answer);
logger.debug("answer: " + answer);
return {status: "OK", answer: answer};
} else {
console.error("`onHumanInputText` not implemented");
logger.error("`onHumanInputText` not implemented");
return {status: "Error: Cannot get user's answer.", answer: ""};
}
}
Expand Down Expand Up @@ -84,21 +85,21 @@ export class HumanInputSingleChoice implements Tool<HumanInputSingleChoiceInput,
}
const question = params.question;
const choices = params.choices;
console.log("question: " + question);
console.log("choices: " + choices);
logger.debug("question: " + question);
logger.debug("choices: " + choices);
let onHumanInputSingleChoice = context.callback?.hooks.onHumanInputSingleChoice;
if (onHumanInputSingleChoice) {
let answer;
try {
answer = await onHumanInputSingleChoice(question, choices);
} catch (e) {
console.error(e);
logger.error(e);
return {status: "Error: Cannot get user's answer.", answer: ""};
}
console.log("answer: " + answer);
logger.debug("answer: " + answer);
return {status: "OK", answer: answer};
} else {
console.error("`onHumanInputSingleChoice` not implemented");
logger.error("`onHumanInputSingleChoice` not implemented");
return {status: "Error: Cannot get user's answer.", answer: ""};
}
}
Expand Down Expand Up @@ -134,21 +135,21 @@ export class HumanInputMultipleChoice implements Tool<HumanInputMultipleChoiceIn
}
const question = params.question;
const choices = params.choices;
console.log("question: " + question);
console.log("choices: " + choices);
logger.debug("question: " + question);
logger.debug("choices: " + choices);
let onHumanInputMultipleChoice = context.callback?.hooks.onHumanInputMultipleChoice;
if (onHumanInputMultipleChoice) {
let answer;
try {
answer = await onHumanInputMultipleChoice(question, choices)
} catch (e) {
console.error(e);
logger.error(e);
return {status: "`onHumanInputMultipleChoice` not implemented", answer: []};
}
console.log("answer: " + answer);
logger.debug("answer: " + answer);
return {status: "OK", answer: answer};
} else {
console.error("Cannot get user's answer.");
logger.error("Cannot get user's answer.");
return {status: "Error: Cannot get user's answer.", answer: []};
}
}
Expand Down Expand Up @@ -179,20 +180,20 @@ export class HumanOperate implements Tool<HumanOperateInput, HumanOperateResult>
throw new Error('Invalid parameters. Expected an object with a "reason" property.');
}
const reason = params.reason;
console.log("reason: " + reason);
logger.debug("reason: " + reason);
let onHumanOperate = context.callback?.hooks.onHumanOperate;
if (onHumanOperate) {
let userOperation;
try {
userOperation = await onHumanOperate(reason);
} catch (e) {
console.error(e);
logger.error(e);
return {status: "`onHumanOperate` not implemented", userOperation: ""};
}
console.log("userOperation: " + userOperation);
logger.debug("userOperation: " + userOperation);
return {status: "OK", userOperation: userOperation};
} else {
console.error("Cannot get user's operation.");
logger.error("Cannot get user's operation.");
return {status: "Error: Cannot get user's operation.", userOperation: ""};
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/eko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
WorkflowResult
} from '../types';
import { ToolRegistry } from './tool-registry';
import { logger } from '../common/log';

/**
* Eko core
Expand Down Expand Up @@ -66,7 +67,7 @@ export class Eko {
}
});
} else {
console.warn("`ekoConfig.callback` is missing when construct `Eko` instance.")
logger.warn("`ekoConfig.callback` is missing when construct `Eko` instance.")
}

tools.forEach(tool => this.toolRegistry.registerTool(tool));
Expand Down
14 changes: 8 additions & 6 deletions src/extension/content/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { logger } from '../../common/log';

declare const eko: any;

if (!(window as any).eko) {
Expand Down Expand Up @@ -29,7 +31,7 @@ chrome.runtime.onMessage.addListener(function (request: any, sender: any, sendRe
try {
result = await eko.subListeners[request.event](request.params);
} catch (e) {
console.log(e);
logger.error(e);
}
}
sendResponse(result);
Expand Down Expand Up @@ -94,7 +96,7 @@ chrome.runtime.onMessage.addListener(function (request: any, sender: any, sendRe
}
}
} catch (e) {
console.log('onMessage error', e);
logger.error('onMessage error', e);
sendResponse(false);
}
})();
Expand Down Expand Up @@ -178,7 +180,7 @@ function type(request: any): boolean {
input.dispatchEvent(event);
});
}
console.log('type', input, request, result);
logger.debug('type', input, request, result);
return true;
}

Expand All @@ -196,7 +198,7 @@ function mouse_move(request: any): boolean {
clientY: y,
});
let result = document.body.dispatchEvent(event);
console.log('mouse_move', document.body, request, result);
logger.debug('mouse_move', document.body, request, result);
return true;
}

Expand Down Expand Up @@ -234,7 +236,7 @@ function simulateMouseEvent(request: any, eventTypes: Array<string>, button: 0 |
button, // 0 left; 2 right
});
let result = element.dispatchEvent(event);
console.log('simulateMouse', element, { ...request, eventTypes, button }, result);
logger.debug('simulateMouse', element, { ...request, eventTypes, button }, result);
}
return true;
}
Expand Down Expand Up @@ -272,7 +274,7 @@ function scroll_to(request: any): boolean {
behavior: 'smooth',
});
}
console.log('scroll_to', request);
logger.debug('scroll_to', request);
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/extension/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as tools from './tools';
import { Tool } from '../types';
import { logger } from '../common/log';

export async function pub(chromeProxy: any, tabId: number, event: string, params: any): Promise<any> {
return await chromeProxy.tabs.sendMessage(tabId as number, {
Expand Down Expand Up @@ -28,7 +29,7 @@ export function loadTools(): Map<string, Tool<any, any>> {
let instance = new tool();
toolsMap.set(instance.name || key, instance);
} catch (e) {
console.error(`Failed to instantiate ${key}:`, e);
logger.error(`Failed to instantiate ${key}:`, e);
}
}
}
Expand Down
Loading