@oagi/oagi 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,123 @@
1
+ /**
2
+ * -----------------------------------------------------------------------------
3
+ * Copyright (c) OpenAGI Foundation
4
+ * All rights reserved.
5
+ *
6
+ * This file is part of the official API project.
7
+ * Licensed under the MIT License.
8
+ * -----------------------------------------------------------------------------
9
+ */
10
+
11
+ import {
12
+ StepObserver,
13
+ type LogEvent,
14
+ type ObserverEvent,
15
+ type SplitEvent,
16
+ } from '../../types/index.js';
17
+ import { exportToHtml, exportToJson, exportToMarkdown } from './exporters';
18
+
19
+ export enum ExportFormat {
20
+ /** Supported export formats. */
21
+ MARKDOWN = 'markdown',
22
+ HTML = 'html',
23
+ JSON = 'json',
24
+ }
25
+
26
+ export class AsyncAgentObserver extends StepObserver {
27
+ /**
28
+ * Records agent execution events and exports to various formats.
29
+ *
30
+ * This class implements the AsyncObserver protocol and provides
31
+ * functionality for recording events during agent execution and
32
+ * exporting them to Markdown or HTML formats.
33
+ */
34
+
35
+ events: ObserverEvent[] = [];
36
+
37
+ async onEvent(event: ObserverEvent): Promise<void> {
38
+ /**
39
+ * Record an event.
40
+ *
41
+ * @param event The event to record.
42
+ */
43
+ this.events.push(event);
44
+ }
45
+
46
+ addLog(message: string): void {
47
+ /**
48
+ * Add a custom log message.
49
+ *
50
+ * @param message The log message to add.
51
+ */
52
+ const event: LogEvent = {
53
+ type: 'log',
54
+ timestamp: new Date(),
55
+ message,
56
+ };
57
+ this.events.push(event);
58
+ }
59
+
60
+ addSplit(label: string = ''): void {
61
+ /**
62
+ * Add a visual separator.
63
+ *
64
+ * @param label Optional label for the separator.
65
+ */
66
+ const event: SplitEvent = {
67
+ type: 'split',
68
+ timestamp: new Date(),
69
+ label,
70
+ };
71
+ this.events.push(event);
72
+ }
73
+
74
+ clear(): void {
75
+ /** Clear all recorded events. */
76
+ this.events = [];
77
+ }
78
+
79
+ getEventsByStep(step_num: number): ObserverEvent[] {
80
+ /**
81
+ * Get all events for a specific step.
82
+ *
83
+ * @param step_num The step number to filter by.
84
+ */
85
+ return this.events.filter(
86
+ event =>
87
+ (event as any).step_num !== undefined &&
88
+ (event as any).step_num === step_num,
89
+ );
90
+ }
91
+
92
+ export(
93
+ format: ExportFormat | string,
94
+ path: string,
95
+ images_dir?: string | null,
96
+ ): void {
97
+ /**
98
+ * Export recorded events to a file.
99
+ *
100
+ * @param format Export format (markdown, html, json)
101
+ * @param path Path to the output file.
102
+ * @param images_dir Directory to save images (markdown only).
103
+ */
104
+ const normalized =
105
+ typeof format === 'string'
106
+ ? (format.toLowerCase() as ExportFormat)
107
+ : format;
108
+
109
+ switch (normalized) {
110
+ case ExportFormat.MARKDOWN:
111
+ exportToMarkdown(this.events, path, images_dir ?? undefined);
112
+ return;
113
+ case ExportFormat.HTML:
114
+ exportToHtml(this.events, path);
115
+ return;
116
+ case ExportFormat.JSON:
117
+ exportToJson(this.events, path);
118
+ return;
119
+ default:
120
+ throw new Error(`Unknown export format: ${String(format)}`);
121
+ }
122
+ }
123
+ }