Skip to content
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
83 changes: 81 additions & 2 deletions packages/component-sdk/src/__tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'bun:test';
import { describe, it, expect, vi } from 'bun:test';
import { createExecutionContext } from '../context';
import type { IFileStorageService, ISecretsService, TraceEvent } from '../interfaces';

Expand Down Expand Up @@ -34,7 +34,6 @@ describe('ExecutionContext', () => {
uploadFile: async () => {},
};


const context = createExecutionContext({
runId: 'test-run',
componentRef: 'test.component',
Expand Down Expand Up @@ -91,6 +90,86 @@ describe('ExecutionContext', () => {
expect(recordedEvents[1].level).toBe('info');
});

it('does not mirror progress events to console.log by default', () => {
const previousDebugFlag = process.env.SENTRIS_DEBUG_COMPONENTS;
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

try {
delete process.env.SENTRIS_DEBUG_COMPONENTS;

const recordedEvents: TraceEvent[] = [];
const context = createExecutionContext({
runId: 'run-quiet-progress',
componentRef: 'quiet.progress',
trace: {
record: (event: TraceEvent) => {
recordedEvents.push(event);
},
},
});

context.emitProgress('Processing quietly');

expect(recordedEvents).toHaveLength(1);
expect(recordedEvents[0]).toMatchObject({
type: 'NODE_PROGRESS',
message: 'Processing quietly',
});
expect(logSpy).not.toHaveBeenCalled();
} finally {
if (previousDebugFlag === undefined) {
delete process.env.SENTRIS_DEBUG_COMPONENTS;
} else {
process.env.SENTRIS_DEBUG_COMPONENTS = previousDebugFlag;
}
logSpy.mockRestore();
}
});

it('does not mirror info and debug logger output to console by default', () => {
const previousDebugFlag = process.env.SENTRIS_DEBUG_COMPONENTS;
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const debugSpy = vi.spyOn(console, 'debug').mockImplementation(() => {});

try {
delete process.env.SENTRIS_DEBUG_COMPONENTS;

const logEntries: Array<{
level?: string;
message: string;
}> = [];

const context = createExecutionContext({
runId: 'run-quiet-logger',
componentRef: 'quiet.logger',
logCollector: (entry) => {
logEntries.push({
level: entry.level,
message: entry.message,
});
},
});

context.logger.info('hello world');
context.logger.debug('debug detail');

expect(logEntries).toEqual([
{ level: 'info', message: 'hello world' },
{ level: 'debug', message: 'debug detail' },
]);
expect(logSpy).not.toHaveBeenCalled();
expect(debugSpy).not.toHaveBeenCalled();
} finally {
if (previousDebugFlag === undefined) {
delete process.env.SENTRIS_DEBUG_COMPONENTS;
} else {
process.env.SENTRIS_DEBUG_COMPONENTS = previousDebugFlag;
}
logSpy.mockRestore();
debugSpy.mockRestore();
}
});

it('should support structured progress payloads', () => {
const recordedEvents: TraceEvent[] = [];
const mockTrace = {
Expand Down
66 changes: 47 additions & 19 deletions packages/component-sdk/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// Simple format function to avoid webpack issues with node:util
function format(...args: unknown[]): string {
return args.map(arg => {
if (typeof arg === 'string') return arg;
if (arg === null || arg === undefined) return String(arg);
if (typeof arg === 'object') {
try {
return JSON.stringify(arg);
} catch {
return String(arg);
return args
.map((arg) => {
if (typeof arg === 'string') return arg;
if (arg === null || arg === undefined) return String(arg);
if (typeof arg === 'object') {
try {
return JSON.stringify(arg);
} catch {
return String(arg);
}
}
}
return String(arg);
}).join(' ');
return String(arg);
})
.join(' ');
}

function shouldMirrorComponentDiagnostics(): boolean {
return process.env.SENTRIS_DEBUG_COMPONENTS === '1';
}

import type {
Expand Down Expand Up @@ -51,8 +57,21 @@ export interface CreateContextOptions {
}

export function createExecutionContext(options: CreateContextOptions): ExecutionContext {
const { runId, componentRef, metadata: metadataInput, storage, secrets, artifacts, trace, logCollector, terminalCollector, agentTracePublisher, workflowId, workflowName, organizationId } =
options;
const {
runId,
componentRef,
metadata: metadataInput,
storage,
secrets,
artifacts,
trace,
logCollector,
terminalCollector,
agentTracePublisher,
workflowId,
workflowName,
organizationId,
} = options;
const metadata = createMetadata(runId, componentRef, metadataInput);
const scopedTrace = trace ? createScopedTrace(trace, metadata) : undefined;

Expand Down Expand Up @@ -98,11 +117,15 @@ export function createExecutionContext(options: CreateContextOptions): Execution
const logger: Logger = Object.freeze({
debug: (...args: unknown[]) => {
pushLog('stdout', 'debug', args);
console.debug(`[${componentRef}]`, ...args);
if (shouldMirrorComponentDiagnostics()) {
console.debug(`[${componentRef}]`, ...args);
}
},
info: (...args: unknown[]) => {
pushLog('stdout', 'info', args);
console.log(`[${componentRef}]`, ...args);
if (shouldMirrorComponentDiagnostics()) {
console.log(`[${componentRef}]`, ...args);
}
},
warn: (...args: unknown[]) => {
pushLog('stdout', 'warn', args);
Expand All @@ -114,14 +137,15 @@ export function createExecutionContext(options: CreateContextOptions): Execution
},
}) as Logger;


const emitProgress = (progress: ProgressEventInput | string) => {
const normalized: ProgressEventInput =
typeof progress === 'string' ? { message: progress, level: 'info' } : progress;
const level = normalized.level ?? 'info';
const message = normalized.message;

console.log(`[${componentRef}] progress [${level}]: ${message}`);
if (shouldMirrorComponentDiagnostics()) {
console.log(`[${componentRef}] progress [${level}]: ${message}`);
}
if (scopedTrace) {
scopedTrace.record({
type: 'NODE_PROGRESS',
Expand Down Expand Up @@ -169,7 +193,9 @@ export function createExecutionContext(options: CreateContextOptions): Execution
timestamp: new Date().toISOString(),
metadata,
});
console.debug(`[${componentRef}]`, ...args);
if (shouldMirrorComponentDiagnostics()) {
console.debug(`[${componentRef}]`, ...args);
Comment on lines 193 to +197
}
},
info: (...args: unknown[]) => {
logCollector({
Expand All @@ -181,7 +207,9 @@ export function createExecutionContext(options: CreateContextOptions): Execution
timestamp: new Date().toISOString(),
metadata,
});
console.log(`[${componentRef}]`, ...args);
if (shouldMirrorComponentDiagnostics()) {
console.log(`[${componentRef}]`, ...args);
Comment on lines 207 to +211
}
},
warn: (...args: unknown[]) => {
logCollector({
Expand Down
Loading