Skip to content

Commit

Permalink
feat: add format support
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Mar 17, 2024
1 parent 3428b8f commit eb696d2
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,27 @@ interface PromptData<Event extends EventsUnion> {
text: string;
}

function isEvent(maybeEvent: EventsUnion | string): maybeEvent is EventsUnion {
return events.includes(maybeEvent as EventsUnion);
function isEvent(
maybeEvent: EventsUnion | Stringable,
): maybeEvent is EventsUnion {
return events.includes(maybeEvent.toString() as EventsUnion);
}

export interface PromptFunctionParams<Event extends EventsUnion>
extends Optional<SendMessageParams, "chat_id" | "text"> {
validate?: (context: PromptAnswer<Event>) => MaybePromise<boolean>;
}

type Stringable = string | { toString(): string };

export interface PromptFunction {
(
text: string,
text: Stringable,
params?: PromptFunctionParams<EventsUnion>,
): Promise<PromptAnswer<EventsUnion>>;
<Event extends EventsUnion>(
event: Event,
text: string,
text: Stringable,
params?: PromptFunctionParams<Event>,
): Promise<PromptAnswer<Event>>;
}
Expand All @@ -55,14 +59,22 @@ export function getPrompt(
context: PromptAnswer<EventsUnion>,
): PromptFunction {
async function prompt<Event extends EventsUnion>(
eventOrText: Event | string,
textOrParams?: string | PromptFunctionParams,
params?: PromptFunctionParams,
eventOrText: Event | Stringable,
textOrParams?: Stringable | PromptFunctionParams<Event>,
params?: PromptFunctionParams<Event>,
) {
const { validate, ...sendParams } =
params || (typeof textOrParams === "object" ? textOrParams : {});
params ||
(typeof textOrParams === "object" && !("toString" in textOrParams)
? textOrParams
: {});

const text =
isEvent(eventOrText) && typeof textOrParams === "string"
isEvent(eventOrText) &&
(typeof textOrParams === "string" ||
(textOrParams &&
typeof textOrParams !== "string" &&
"toString" in textOrParams))
? textOrParams
: eventOrText;

Expand All @@ -74,9 +86,10 @@ export function getPrompt(
// @ts-expect-error
resolve: resolve,
event: isEvent(eventOrText) ? eventOrText : undefined,
// @ts-expect-error
validate,
sendParams,
text,
text: text.toString(),
});
});
}
Expand Down

0 comments on commit eb696d2

Please sign in to comment.