Distinguish between undefined and unknown action outputs

Signed-off-by: Andreas Berger <andreas@berger-ecommerce.com>
This commit is contained in:
Andreas Berger
2023-03-23 21:03:59 +01:00
committed by blam
parent a7eb36c6e3
commit 0da07e505b
10 changed files with 104 additions and 73 deletions
@@ -19,11 +19,23 @@ import { z } from 'zod';
import { Schema } from 'jsonschema';
import zodToJsonSchema from 'zod-to-json-schema';
/** @public */
export type ActionOutputType<TOutputSchema> = TOutputSchema extends z.ZodType<
any,
any,
infer IReturn
>
? IReturn
: TOutputSchema extends undefined
? undefined
: unknown;
/** @public */
export type TemplateActionOptions<
TActionInput = {},
TInputSchema extends Schema | z.ZodType = {},
TOutputSchema extends Schema | z.ZodType = {},
TActionOutput extends ActionOutputType<TOutputSchema> = ActionOutputType<TOutputSchema>,
> = {
id: string;
description?: string;
@@ -33,14 +45,7 @@ export type TemplateActionOptions<
input?: TInputSchema;
output?: TOutputSchema;
};
handler: (
ctx: ActionContext<
TActionInput,
TOutputSchema extends z.ZodType<any, any, infer IReturn>
? IReturn
: undefined
>,
) => Promise<void>;
handler: (ctx: ActionContext<TActionInput, TActionOutput>) => Promise<void>;
};
/**
@@ -55,11 +60,14 @@ export const createTemplateAction = <
TActionInput = TInputSchema extends z.ZodType<any, any, infer IReturn>
? IReturn
: TParams,
TActionOutput = TOutputSchema extends z.ZodType<any, any, infer IReturn>
? IReturn
: undefined,
TActionOutput extends ActionOutputType<TOutputSchema> = ActionOutputType<TOutputSchema>,
>(
action: TemplateActionOptions<TActionInput, TInputSchema, TOutputSchema>,
action: TemplateActionOptions<
TActionInput,
TInputSchema,
TOutputSchema,
TActionOutput
>,
): TemplateAction<TActionInput, TActionOutput> => {
const inputSchema =
action.schema?.input && 'safeParseAsync' in action.schema.input
@@ -17,5 +17,6 @@
export {
createTemplateAction,
type TemplateActionOptions,
type ActionOutputType,
} from './createTemplateAction';
export { type ActionContext, type TemplateAction } from './types';
+12 -7
View File
@@ -28,7 +28,7 @@ import { Schema } from 'jsonschema';
*/
export type ActionContext<
TActionInput extends JsonObject,
TActionOutput extends JsonObject | undefined = undefined,
TActionOutput extends JsonObject | unknown | undefined = unknown,
> = {
logger: Logger;
logStream: Writable;
@@ -36,8 +36,16 @@ export type ActionContext<
workspacePath: string;
input: TActionInput;
output<KEY extends keyof TActionOutput>(
name: TActionOutput extends undefined ? string : KEY,
value: TActionOutput extends undefined ? JsonValue : TActionOutput[KEY],
name: TActionOutput extends JsonObject
? KEY
: TActionOutput extends undefined
? never
: string,
value: TActionOutput extends JsonObject
? TActionOutput[KEY]
: TActionOutput extends undefined
? never
: JsonValue,
): void;
/**
@@ -74,10 +82,7 @@ export type ActionContext<
};
/** @public */
export type TemplateAction<
TActionInput = unknown,
TActionOutput = undefined,
> = {
export type TemplateAction<TActionInput = unknown, TActionOutput = unknown> = {
id: string;
description?: string;
examples?: { description: string; example: string }[];