Skip to content

Commit

Permalink
feat(runner): add events for actions and status
Browse files Browse the repository at this point in the history
  • Loading branch information
brewcoua committed Jun 13, 2024
1 parent bf3e95c commit 2de33c1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/domain/Service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Logger } from 'winston'
import { EventEmitter } from 'node:events'

export default abstract class Service {
export default abstract class Service extends EventEmitter {
protected name: string
protected logger: Logger

protected constructor(name: string, logger: Logger) {
super()
this.name = name
this.logger = logger.child({ service: name })
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export { default as Agent } from './Agent'

export { default as Runner } from './services/runner'
export { default as ActionReport } from './services/runner/domain/ActionReport'
32 changes: 30 additions & 2 deletions src/services/runner/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import { ErrorResult } from './domain/ErrorResult'
import ParsedResult from '../mind/domain/ParsedResult'
import CycleResult from './domain/CycleResult'
import { Logger } from 'winston'
import ActionReport from './domain/ActionReport'
import RunnerStatus from './domain/RunnerStatus'

/**
* The Runner class provides a high-level interface for interacting with runners.
* @public
*/
export default class Runner extends Service {
constructor(
public readonly page: PageWrapper,
Expand All @@ -32,9 +38,20 @@ export default class Runner extends Service {
}
private readonly actions: Action[] = []

private _status: RunnerStatus = RunnerStatus.Starting
public get status(): RunnerStatus {
return this._status
}
public set status(value: RunnerStatus) {
this._status = value
this.emit('status', value)
}

public async run(): Promise<TaskResult> {
this.logger.debug(`Starting task: ${this.task}`)

this.status = RunnerStatus.Running

while (
this.cycles.total < config.cycles.max &&
this.cycles.failed < config.cycles.failed
Expand All @@ -50,6 +67,11 @@ export default class Runner extends Service {
this.logger.debug(`Cycle ${this.cycles.total} completed`)

if (action.type === ActionType.Done) {
this.status =
action.arguments.status === 'success'
? RunnerStatus.Done
: RunnerStatus.Failed

return {
success: action.arguments.status === 'success',
message: action.arguments.reason as string,
Expand All @@ -61,6 +83,8 @@ export default class Runner extends Service {
if (config.delay) await this.sleep(config.delay)
}

this.status = RunnerStatus.Failed

if (this.cycles.failed >= config.cycles.failed) {
return {
success: false,
Expand Down Expand Up @@ -137,11 +161,15 @@ export default class Runner extends Service {
result.action.status = status
this.actions.push(result.action)

this.logger.info('Performed action', {
const report: ActionReport = {
action: result.action,
reasoning: result.reasoning,
duration: Date.now() - startedAt,
})
}

this.logger.info('Performed action', report)

this.emit('action', report)

return {
success: true,
Expand Down
12 changes: 12 additions & 0 deletions src/services/runner/domain/ActionReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Action from './Action'

/**
* Represents a report of an action execution.
* @public
*/
export type ActionReport = {
action: Action
reasoning?: string
duration: number
}
export default ActionReport
7 changes: 7 additions & 0 deletions src/services/runner/domain/RunnerStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum RunnerStatus {
Starting = 'Starting',
Running = 'Running',
Done = 'Done',
Failed = 'Failed',
}
export default RunnerStatus

0 comments on commit 2de33c1

Please sign in to comment.