Skip to content
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

feat: implement pretty logger #46

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions packages/framework/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"type": "module",
"dependencies": {
"chalk": "^5.3.0",
"dedent": "^1.5.3",
"openai": "^4.76.0",
"zod": "^3.23.8"
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/supervisor/iterate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { selectAgent } from './selectAgent.js'
*/
export async function iterate(workflow: Workflow, state: WorkflowState): Promise<WorkflowState> {
const nextState = await nextTick(workflow, state)
workflow.snapshot(nextState)
workflow.snapshot({ prevState: state, nextState })
return nextState
}

Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/supervisor/runTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Message } from '../types.js'
/**
* Asserts that given message requests tool calls
*/
function isToolCallRequest(message?: Message): message is ParsedChatCompletionMessage<any> {
export function isToolCallRequest(message?: Message): message is ParsedChatCompletionMessage<any> {
return message ? 'tool_calls' in message : false
}

Expand Down
74 changes: 71 additions & 3 deletions packages/framework/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,77 @@
import chalk from 'chalk'

import { isToolCallRequest } from './supervisor/runTools.js'
import { WorkflowState } from './workflow.js'

export type Telemetry = (state: WorkflowState) => void
export type Telemetry = ({
prevState,
nextState,
}: {
prevState: WorkflowState
nextState: WorkflowState
}) => void

export const noop: Telemetry = () => {}

export const logger: Telemetry = (state) => {
console.log(state)
export const logger: Telemetry = ({ prevState, nextState }) => {
const { status: prevStatus } = prevState
const { status: nextStatus } = nextState

const logMessage = (emoji: string, message: string, details: string = '') => {
console.log(`${emoji} ${chalk.bold(message)}${details ? `\n${chalk.gray(details)}` : ''}`)
}

if (prevStatus !== nextStatus) {
switch (nextStatus) {
case 'pending':
logMessage(
'🕒',
'Distributing the task...',
`Request: ${nextState.agentRequest[0].content}`
)
break
case 'assigned':
switch (nextState.agentStatus) {
case 'idle':
logMessage('🛠️', `Agent "${nextState.agent}" is beginning to work on the task`)
break
case 'step':
logMessage(
'🔄',
`Agent "${nextState.agent}" is proceeding to the next step`,
`Step details: ${nextState.agentRequest[0].content}`
)
break
case 'tool': {
const lastMessage = nextState.agentRequest[nextState.agentRequest.length - 1]
if (!isToolCallRequest(lastMessage)) {
return
}
const toolNames = lastMessage.tool_calls.map((tool) => tool.function.name)
logMessage(
'🔧',
`Agent "${nextState.agent}" is calling tools`,
`Tools: ${toolNames.join(', ')}`
)
break
}
}
break
case 'idle':
logMessage('✅', 'Moving to next task', `Iterations: ${nextState.messages.length}`)
break
case 'finished':
logMessage(
'🎉',
'Workflow finished successfully!',
`Total messages: ${nextState.messages.length}`
)
break
case 'failed':
logMessage('❌', 'Workflow failed', `Check logs for more details`)
break
default:
logMessage('ℹ️', 'State changed', `New status: ${nextStatus}`)
}
}
}