logger.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Multi-Agent Logging System - Logger
  3. *
  4. * Pretty-prints delegation hierarchies with visual indentation and formatting.
  5. */
  6. import { SessionTracker } from './session-tracker.js';
  7. import {
  8. formatSessionHeader,
  9. formatMessage,
  10. formatToolCall,
  11. formatDelegation,
  12. formatChildLinked,
  13. formatSessionComplete,
  14. formatSystemMessage,
  15. } from './formatters.js';
  16. /**
  17. * Multi-agent logger with hierarchy-aware formatting
  18. */
  19. export class MultiAgentLogger {
  20. private tracker: SessionTracker;
  21. private enabled: boolean;
  22. constructor(enabled = true) {
  23. this.tracker = new SessionTracker();
  24. this.enabled = enabled;
  25. }
  26. /**
  27. * Log session start
  28. */
  29. logSessionStart(sessionId: string, agent: string, parentId?: string): void {
  30. if (!this.enabled) return;
  31. this.tracker.registerSession(sessionId, agent, parentId);
  32. const node = this.tracker.getSession(sessionId);
  33. if (!node) return;
  34. const header = formatSessionHeader(sessionId, agent, node.depth, parentId);
  35. console.log(header);
  36. }
  37. /**
  38. * Log delegation event
  39. */
  40. logDelegation(
  41. parentSessionId: string,
  42. toAgent: string,
  43. prompt: string
  44. ): string {
  45. if (!this.enabled) return '';
  46. const delegationId = this.tracker.recordDelegation(parentSessionId, toAgent, prompt);
  47. const node = this.tracker.getSession(parentSessionId);
  48. const depth = node?.depth ?? 0;
  49. const formatted = formatDelegation(toAgent, prompt, depth);
  50. console.log(formatted);
  51. return delegationId;
  52. }
  53. /**
  54. * Log child session linked to delegation
  55. */
  56. logChildLinked(delegationId: string, childSessionId: string): void {
  57. if (!this.enabled) return;
  58. this.tracker.linkChildSession(delegationId, childSessionId);
  59. const delegation = this.tracker.getDelegation(delegationId);
  60. if (!delegation) return;
  61. const parent = this.tracker.getSession(delegation.parentSessionId);
  62. const depth = parent?.depth ?? 0;
  63. const formatted = formatChildLinked(childSessionId, depth);
  64. console.log(formatted);
  65. }
  66. /**
  67. * Log user or assistant message
  68. */
  69. logMessage(sessionId: string, role: 'user' | 'assistant', text: string): void {
  70. if (!this.enabled) return;
  71. const node = this.tracker.getSession(sessionId);
  72. const depth = node?.depth ?? 0;
  73. const formatted = formatMessage(role, text, depth);
  74. console.log(formatted);
  75. }
  76. /**
  77. * Log tool call
  78. */
  79. logToolCall(sessionId: string, tool: string, input: any): void {
  80. if (!this.enabled) return;
  81. // Skip logging task tool (handled by logDelegation)
  82. if (tool === 'task') return;
  83. const node = this.tracker.getSession(sessionId);
  84. const depth = node?.depth ?? 0;
  85. const formatted = formatToolCall(tool, input, depth);
  86. console.log(formatted);
  87. }
  88. /**
  89. * Log session completion
  90. */
  91. logSessionComplete(sessionId: string): void {
  92. if (!this.enabled) return;
  93. const node = this.tracker.getSession(sessionId);
  94. if (!node) return;
  95. this.tracker.completeSession(sessionId);
  96. const duration = node.endTime
  97. ? node.endTime - node.startTime
  98. : Date.now() - node.startTime;
  99. const sessionType = node.depth === 0 ? 'PARENT' : 'CHILD';
  100. const formatted = formatSessionComplete(sessionType, duration, node.depth);
  101. console.log(formatted);
  102. }
  103. /**
  104. * Log system message
  105. */
  106. logSystem(sessionId: string, message: string): void {
  107. if (!this.enabled) return;
  108. const node = this.tracker.getSession(sessionId);
  109. const depth = node?.depth ?? 0;
  110. const formatted = formatSystemMessage(message, depth);
  111. console.log(formatted);
  112. }
  113. /**
  114. * Get the session tracker for analysis
  115. */
  116. getTracker(): SessionTracker {
  117. return this.tracker;
  118. }
  119. /**
  120. * Enable or disable logging
  121. */
  122. setEnabled(enabled: boolean): void {
  123. this.enabled = enabled;
  124. }
  125. /**
  126. * Check if logging is enabled
  127. */
  128. isEnabled(): boolean {
  129. return this.enabled;
  130. }
  131. /**
  132. * Clear all tracked data
  133. */
  134. clear(): void {
  135. this.tracker.clear();
  136. }
  137. }