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
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export class ClientCapability {
/**
* Trigger implementation for LogTrigger
*/
class ClientLogTrigger extends BaseTriggerImpl<FilterLogTriggerRequestJson, Log> {
class ClientLogTrigger extends BaseTriggerImpl<FilterLogTriggerRequestJson, Log, Log> {
constructor(
mode: Mode,
config: FilterLogTriggerRequestJson,
Expand Down Expand Up @@ -498,10 +498,10 @@ class ClientLogTrigger extends BaseTriggerImpl<FilterLogTriggerRequestJson, Log>
}

/**
* Transform the trigger output - override this method if needed
* Default implementation returns the output unchanged
* Transform the raw trigger output - override this method if needed
* Default implementation returns the raw output unchanged
*/
adapt(output: Log): Log {
return output;
adapt(rawOutput: Log): Log {
return rawOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class BasicCapability {
/**
* Trigger implementation for Trigger
*/
class BasicTrigger extends BaseTriggerImpl<ConfigJson, TriggerEvent> {
class BasicTrigger extends BaseTriggerImpl<ConfigJson, TriggerEvent, TriggerEvent> {
constructor(
mode: Mode,
config: ConfigJson,
Expand Down Expand Up @@ -118,10 +118,10 @@ class BasicTrigger extends BaseTriggerImpl<ConfigJson, TriggerEvent> {
}

/**
* Transform the trigger output - override this method if needed
* Default implementation returns the output unchanged
* Transform the raw trigger output - override this method if needed
* Default implementation returns the raw output unchanged
*/
adapt(output: TriggerEvent): TriggerEvent {
return output;
adapt(rawOutput: TriggerEvent): TriggerEvent {
return rawOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class BasicCapability {
/**
* Trigger implementation for Trigger
*/
class BasicTrigger extends BaseTriggerImpl<ConfigJson, Outputs> {
class BasicTrigger extends BaseTriggerImpl<ConfigJson, Outputs, Outputs> {
constructor(
mode: Mode,
config: ConfigJson,
Expand Down Expand Up @@ -81,10 +81,10 @@ class BasicTrigger extends BaseTriggerImpl<ConfigJson, Outputs> {
}

/**
* Transform the trigger output - override this method if needed
* Default implementation returns the output unchanged
* Transform the raw trigger output - override this method if needed
* Default implementation returns the raw output unchanged
*/
adapt(output: Outputs): Outputs {
return output;
adapt(rawOutput: Outputs): Outputs {
return rawOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class HTTPCapability {
/**
* Trigger implementation for Trigger
*/
class HTTPTrigger extends BaseTriggerImpl<ConfigJson, Payload> {
class HTTPTrigger extends BaseTriggerImpl<ConfigJson, Payload, Payload> {
constructor(
mode: Mode,
config: ConfigJson,
Expand Down Expand Up @@ -81,10 +81,10 @@ class HTTPTrigger extends BaseTriggerImpl<ConfigJson, Payload> {
}

/**
* Transform the trigger output - override this method if needed
* Default implementation returns the output unchanged
* Transform the raw trigger output - override this method if needed
* Default implementation returns the raw output unchanged
*/
adapt(output: Payload): Payload {
return output;
adapt(rawOutput: Payload): Payload {
return rawOutput;
}
}
20 changes: 10 additions & 10 deletions src/generated-sdk/capabilities/scheduler/cron/v1/cron_sdk_gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class CronCapability {
/**
* Trigger implementation for Trigger
*/
class CronTrigger extends BaseTriggerImpl<ConfigJson, Payload> {
class CronTrigger extends BaseTriggerImpl<ConfigJson, Payload, Payload> {
constructor(
mode: Mode,
config: ConfigJson,
Expand Down Expand Up @@ -85,18 +85,18 @@ class CronTrigger extends BaseTriggerImpl<ConfigJson, Payload> {
}

/**
* Transform the trigger output - override this method if needed
* Default implementation returns the output unchanged
* Transform the raw trigger output - override this method if needed
* Default implementation returns the raw output unchanged
*/
adapt(output: Payload): Payload {
return output;
adapt(rawOutput: Payload): Payload {
return rawOutput;
}
}

/**
* Trigger implementation for LegacyTrigger
*/
class CronLegacyTrigger extends BaseTriggerImpl<ConfigJson, LegacyPayload> {
class CronLegacyTrigger extends BaseTriggerImpl<ConfigJson, LegacyPayload, LegacyPayload> {
constructor(
mode: Mode,
config: ConfigJson,
Expand Down Expand Up @@ -131,10 +131,10 @@ class CronLegacyTrigger extends BaseTriggerImpl<ConfigJson, LegacyPayload> {
}

/**
* Transform the trigger output - override this method if needed
* Default implementation returns the output unchanged
* Transform the raw trigger output - override this method if needed
* Default implementation returns the raw output unchanged
*/
adapt(output: LegacyPayload): LegacyPayload {
return output;
adapt(rawOutput: LegacyPayload): LegacyPayload {
return rawOutput;
}
}
10 changes: 5 additions & 5 deletions src/generator/generate-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function generateTriggerClass(
/**
* Trigger implementation for ${method.name}
*/
class ${triggerClassName} extends BaseTriggerImpl<${method.input.name}Json, ${method.output.name}> {
class ${triggerClassName} extends BaseTriggerImpl<${method.input.name}Json, ${method.output.name}, ${method.output.name}> {
constructor(
mode: Mode,
config: ${method.input.name}Json,
Expand Down Expand Up @@ -76,11 +76,11 @@ class ${triggerClassName} extends BaseTriggerImpl<${method.input.name}Json, ${me
}

/**
* Transform the trigger output - override this method if needed
* Default implementation returns the output unchanged
* Transform the raw trigger output - override this method if needed
* Default implementation returns the raw output unchanged
*/
adapt(output: ${method.output.name}): ${method.output.name} {
return output;
adapt(rawOutput: ${method.output.name}): ${method.output.name} {
return rawOutput;
}
}`;
}
11 changes: 11 additions & 0 deletions src/sdk/utils/safeJsonStringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Supports stringifying an object with bigints, treating bigints as strings.
* @param obj - The object to stringify
* @returns The stringified object
*/
export const safeJsonStringify = (obj: any): string =>
JSON.stringify(
obj,
(_, value) => (typeof value === "bigint" ? value.toString() : value),
2
);
42 changes: 23 additions & 19 deletions src/sdk/utils/triggers/trigger-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import type { GenMessage } from "@bufbuild/protobuf/codegenv2";
* Triggers are server-streaming RPC methods that emit events
* to the workflow runtime.
*/
export interface BaseTrigger<TOutput extends Message<string>> {
export interface BaseTrigger<TRawTriggerOutput extends Message<string>> {
/** The capability ID for this trigger */
capabilityId(): string;

/** The method name for this trigger */
method(): string;

/** Create a new instance of the output type */
newOutput(): TOutput;
/** Create a new instance of the raw output type */
newOutput(): TRawTriggerOutput;

/** Access the output schema for decoding */
outputSchema(): GenMessage<TOutput>;
/** Access the raw output schema for decoding */
outputSchema(): GenMessage<TRawTriggerOutput>;

/** Get the configuration as an Any type for protobuf serialization */
configAsAny(): Any;
Expand All @@ -32,38 +32,42 @@ export interface BaseTrigger<TOutput extends Message<string>> {
* The adapt method allows transformation of the raw protobuf output
* to a more convenient type for the workflow.
*/
export interface Trigger<TOutput extends Message<string>, TAdapted = TOutput>
extends BaseTrigger<TOutput> {
/** Transform the trigger output to the adapted type */
adapt(output: TOutput): TAdapted | Promise<TAdapted>;
export interface Trigger<
TRawTriggerOutput extends Message<string>,
TTriggerOutput = TRawTriggerOutput
> extends BaseTrigger<TRawTriggerOutput> {
/** Transform the raw trigger output to the adapted type */
adapt(rawOutput: TRawTriggerOutput): TTriggerOutput | Promise<TTriggerOutput>;
}

/**
* Base class for trigger implementations
*/
export abstract class BaseTriggerImpl<
TConfig,
TOutput extends Message<string>,
TAdapted = TOutput
> implements Trigger<TOutput, TAdapted>
TRawTriggerOutput extends Message<string>,
TTriggerOutput = TRawTriggerOutput
> implements Trigger<TRawTriggerOutput, TTriggerOutput>
{
constructor(public readonly mode: Mode, public readonly config: TConfig) {}

abstract capabilityId(): string;
abstract method(): string;
abstract newOutput(): TOutput;
abstract outputSchema(): GenMessage<TOutput>;
abstract newOutput(): TRawTriggerOutput;
abstract outputSchema(): GenMessage<TRawTriggerOutput>;

/** Go naming parity */
abstract configAsAny(): Any;

/**
* Default adapt implementation - returns output unchanged
* Override this method to transform the output
* Default adapt implementation - returns raw output unchanged
* Override this method to transform the raw output to your desired type
*/
adapt(output: TOutput): TAdapted | Promise<TAdapted> {
// Type assertion is safe here as TAdapted defaults to TOutput
adapt(
rawOutput: TRawTriggerOutput
): TTriggerOutput | Promise<TTriggerOutput> {
// Type assertion is safe here as TTriggerOutput defaults to TRawTriggerOutput
// when not explicitly specified
return output as unknown as TAdapted;
return rawOutput as unknown as TTriggerOutput;
}
}
25 changes: 14 additions & 11 deletions src/sdk/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,36 @@ import { getRequest } from "@cre/sdk/utils/get-request";
import { configHandler, type ConfigHandlerParams } from "@cre/sdk/utils/config";
import { runtime, type Runtime } from "@cre/sdk/runtime";

export type HandlerFn<TConfig, TPayload> = (
export type HandlerFn<TConfig, TTriggerOutput> = (
config: TConfig,
runtime: Runtime,
output: TPayload
triggerOutput: TTriggerOutput
) => Promise<unknown> | unknown;

export interface HandlerEntry<
TConfig = unknown,
TOutput extends Message<string> = Message<string>,
TAdapted = TOutput
TRawTriggerOutput extends Message<string> = Message<string>,
TTriggerOutput = TRawTriggerOutput
> {
trigger: Trigger<TOutput, TAdapted>;
fn: HandlerFn<TConfig, TAdapted>;
trigger: Trigger<TRawTriggerOutput, TTriggerOutput>;
fn: HandlerFn<TConfig, TTriggerOutput>;
}

export type Workflow<TConfig = unknown> = ReadonlyArray<
HandlerEntry<TConfig, any, any>
>;

export const handler = <
TOutput extends Message<string>,
TAdapted = TOutput,
TRawTriggerOutput extends Message<string>,
TTriggerOutput = TRawTriggerOutput,
TConfig = unknown
>(
trigger: Trigger<TOutput, TAdapted>,
fn: HandlerFn<TConfig, TAdapted>
): HandlerEntry<TConfig, TOutput, TAdapted> => ({ trigger, fn });
trigger: Trigger<TRawTriggerOutput, TTriggerOutput>,
fn: HandlerFn<TConfig, TTriggerOutput>
): HandlerEntry<TConfig, TRawTriggerOutput, TTriggerOutput> => ({
trigger,
fn,
});

export class Runner<TConfig> {
private readonly config: TConfig;
Expand Down
1 change: 1 addition & 0 deletions src/workflows/hello-world/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Config = {

const onCronTrigger = (_: Config, runtime: Runtime): void => {
runtime.logger.log("Hello, Calculator! Workflow triggered.");
cre.sendResponseValue(cre.utils.val.string("Hello, Calculator!"));
};

const initWorkflow = (config: Config) => {
Expand Down
Loading