Skip to content

Commit c0bc8f4

Browse files
committed
feat(stream): Refactored typing, removed unecessary method, improved docstring, added tests.
1 parent dd863ee commit c0bc8f4

File tree

11 files changed

+1545
-85
lines changed

11 files changed

+1545
-85
lines changed

src/types/base.types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* JSON-serializable value type.
3+
* Represents any value that can be serialized to JSON.
4+
*/
5+
export type JsonValue =
6+
| string
7+
| number
8+
| boolean
9+
| null
10+
| JsonValue[]
11+
| { [key: string]: JsonValue };
12+
13+
/**
14+
* JSON-serializable object type.
15+
* Allows undefined values which will be omitted during JSON serialization.
16+
*/
17+
export type JsonObject = { [key: string]: JsonValue | undefined };
18+
19+
/**
20+
* JSON-serializable array type.
21+
*/
22+
export type JsonArray = JsonValue[];

src/types/logging/logger.types.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ import type {
99
ToolSpan,
1010
WorkflowSpan,
1111
AgentSpan,
12-
Event,
13-
JsonObject
12+
Event
1413
} from './span.types';
1514
import type {
1615
LlmSpanAllowedInputType,
1716
LlmSpanAllowedOutputType,
1817
RetrieverSpanAllowedOutputType
1918
} from './step.types';
2019
import type { AgentType, Payload, ProtectResponse } from '../new-api.types';
21-
20+
import type { JsonObject } from '../base.types';
2221
export interface GalileoLoggerConfig {
2322
projectName?: string;
2423
logStreamName?: string;
@@ -434,8 +433,7 @@ export interface IGalileoLoggerBatch {
434433
* This is the main interface that the GalileoLogger class implements.
435434
*/
436435
export interface IGalileoLogger
437-
extends
438-
IGalileoLoggerCore,
436+
extends IGalileoLoggerCore,
439437
IGalileoLoggerSession,
440438
IGalileoLoggerTrace,
441439
IGalileoLoggerSpan,

src/types/logging/span.types.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,7 @@ import {
2020
import type { Document } from '../document.types';
2121
import type { MetricValueType } from '../metrics.types';
2222
import { AgentType } from '../new-api.types';
23-
24-
/**
25-
* JSON-serializable value type.
26-
* Represents any value that can be serialized to JSON.
27-
*/
28-
export type JsonValue =
29-
| string
30-
| number
31-
| boolean
32-
| null
33-
| JsonValue[]
34-
| { [key: string]: JsonValue };
35-
36-
/**
37-
* JSON-serializable object type.
38-
* Allows undefined values which will be omitted during JSON serialization.
39-
*/
40-
export type JsonObject = { [key: string]: JsonValue | undefined };
41-
42-
/**
43-
* JSON-serializable array type.
44-
*/
45-
export type JsonArray = JsonValue[];
23+
import type { JsonValue, JsonObject, JsonArray } from '../base.types';
4624

4725
/**
4826
* Types of events that can appear in reasoning/multi-turn model outputs.
@@ -376,10 +354,8 @@ export interface RetrieverSpanOptions extends BaseSpanOptions {
376354
redactedOutput?: RetrieverSpanAllowedOutputType;
377355
}
378356

379-
export interface SerializedRetrieverSpan extends Omit<
380-
SerializedStep,
381-
'output'
382-
> {
357+
export interface SerializedRetrieverSpan
358+
extends Omit<SerializedStep, 'output'> {
383359
output: JsonArray;
384360
}
385361

@@ -408,10 +384,11 @@ export class RetrieverSpan extends BaseStep {
408384
}
409385
}
410386

411-
export interface ToolSpanOptions extends Omit<
412-
BaseSpanOptions,
413-
'input' | 'redactedInput' | 'output' | 'redactedOutput'
414-
> {
387+
export interface ToolSpanOptions
388+
extends Omit<
389+
BaseSpanOptions,
390+
'input' | 'redactedInput' | 'output' | 'redactedOutput'
391+
> {
415392
input: JsonValue;
416393
redactedInput?: JsonValue;
417394
output?: JsonValue;

src/types/logging/step.types.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { components } from '../api.types';
33
import { Document } from '../document.types';
44
import { isMessage, type Message } from '../message.types';
55
import type { MetricValueType } from '../metrics.types';
6-
import type { JsonArray } from './span.types';
6+
import type { JsonArray } from '../base.types';
77

88
export type StepAllowedInputType =
99
| string
@@ -111,10 +111,11 @@ export interface BaseStepOptions {
111111
id?: string;
112112
}
113113

114-
export interface SerializedStep extends Omit<
115-
BaseStepOptions,
116-
'metrics' | 'createdAt' | 'output' | 'redactedOutput'
117-
> {
114+
export interface SerializedStep
115+
extends Omit<
116+
BaseStepOptions,
117+
'metrics' | 'createdAt' | 'output' | 'redactedOutput'
118+
> {
118119
metrics?: SerializedMetrics;
119120
type: StepType;
120121
createdAt: string;

src/utils/galileo-logger.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
type Span,
1111
StepWithChildSpans,
1212
ToolSpan,
13-
WorkflowSpan,
14-
type JsonObject
13+
WorkflowSpan
1514
} from '../types/logging/span.types';
15+
import type { JsonObject } from '../types/base.types';
1616
import { type SpanSchema, Trace } from '../types/logging/trace.types';
1717
import {
1818
type RetrieverSpanAllowedOutputType,
@@ -126,6 +126,8 @@ class GalileoLogger implements IGalileoLogger {
126126
if (config.spanId) {
127127
await logger.initSpan(config.spanId);
128128
}
129+
} else if (config.traceId || config.spanId) {
130+
throw new Error('traceId and spanId can only be used in streaming mode.');
129131
}
130132

131133
return logger;
@@ -1207,6 +1209,13 @@ class GalileoLogger implements IGalileoLogger {
12071209
*/
12081210
async flush(): Promise<Trace[]> {
12091211
try {
1212+
if (this.mode === 'streaming') {
1213+
console.warn(
1214+
'Flushing in streaming mode is not supported. Traces are automatically ingested as they are created.'
1215+
);
1216+
return [];
1217+
}
1218+
12101219
if (!this.traces.length) {
12111220
console.warn('No traces to flush.');
12121221
return [];

src/utils/streaming/base-streaming-adapter.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
LlmSpanAllowedInputType,
1111
LlmSpanAllowedOutputType
1212
} from '../../types/logging/step.types';
13+
import { JsonObject } from '../../types/base.types';
1314

1415
/**
1516
* Tool definition type (matches LlmSpan tools type)
@@ -26,7 +27,7 @@ export interface BaseStreamingAdapterConfig {
2627
model?: string;
2728
metadata?: Record<string, string>;
2829
name?: string;
29-
tools?: ToolDefinition[];
30+
tools?: JsonObject[];
3031
temperature?: number;
3132
};
3233
shouldCompleteTrace: boolean;
@@ -107,15 +108,6 @@ export abstract class BaseStreamingAdapter {
107108
this.finalizer.finalize(output, statusCode);
108109
}
109110

110-
/**
111-
* Update span incrementally during streaming (for real-time updates)
112-
* Delegates to finalizer which uses mocked logger methods
113-
* @param output Current output state (can be partial)
114-
*/
115-
protected updateSpanIncremental(output: LlmSpanAllowedOutputType): void {
116-
this.finalizer.updateSpanIncremental(output);
117-
}
118-
119111
/**
120112
* Get current metrics
121113
* @returns Complete metrics result

src/utils/streaming/finalizer.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import type {
44
LlmSpanAllowedInputType,
55
LlmSpanAllowedOutputType
66
} from '../../types/logging/step.types';
7-
import type { Span } from '../../types/logging/span.types';
8-
import { Trace } from '../../types/logging/trace.types';
9-
import type { ToolDefinition } from './base-streaming-adapter';
7+
import type { JsonObject } from '../../types/base.types';
108

119
/**
1210
* Configuration for streaming finalization
@@ -19,7 +17,7 @@ export interface StreamingFinalizerConfig {
1917
model?: string;
2018
metadata?: Record<string, string>;
2119
name?: string;
22-
tools?: ToolDefinition[];
20+
tools?: JsonObject[];
2321
temperature?: number;
2422
};
2523
shouldCompleteTrace: boolean;
@@ -82,32 +80,4 @@ export class StreamingFinalizer {
8280
});
8381
}
8482
}
85-
86-
/**
87-
* Update span incrementally during streaming (for real-time updates)
88-
* @param output Current output state (can be partial)
89-
* @param spanId Optional span ID if available (currently unused, reserved for future use)
90-
*/
91-
updateSpanIncremental(
92-
output: LlmSpanAllowedOutputType,
93-
spanId?: string
94-
): void {
95-
void spanId;
96-
// Get current parent span or trace
97-
const currentParent = this.logger.currentParent();
98-
99-
if (!currentParent) {
100-
// No active span/trace, skip incremental update
101-
return;
102-
}
103-
104-
// Call streaming update methods (mock implementations in GalileoLogger)
105-
if (currentParent instanceof Trace) {
106-
// Update trace with partial output during streaming
107-
this.logger._updateTraceStreaming(currentParent, output, false);
108-
} else {
109-
// currentParent is a Span (WorkflowSpan, AgentSpan, LlmSpan, etc.)
110-
this.logger._updateSpanStreaming(currentParent as Span, output);
111-
}
112-
}
11383
}

src/utils/task-handler.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ export class TaskHandler {
171171
return newTask;
172172
}
173173

174+
/**
175+
* Submits a task for execution with optional parent dependency tracking.
176+
* @param taskId - The unique identifier for the task.
177+
* @param asyncFn - The async function to execute. Use async/await syntax to ensure synchronous errors are converted to rejected promises.
178+
* @param parentTaskId - (Optional) The ID of a parent task this task depends on. If provided, this task waits for parent completion.
179+
* @returns A promise that resolves to the task result or rejects if the task or its parent fails.
180+
*/
174181
async submitTask<T>(
175182
taskId: string,
176183
asyncFn: () => Promise<T>,

0 commit comments

Comments
 (0)