mirror of
https://github.com/the-draupnir-project/Draupnir.git
synced 2026-06-04 14:51:21 +00:00
fffd8563e3
Add logging to tracing Improve logging Fix order of imports Add missing pieces of tracing code Add more logging Try nested spans Expand traces using an decorator Improve quality of spans Add missing tracing decorations Filter metrics and healthz Add more traces Fix return type error
62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
/**
|
|
* Copyright (C) 2023 Gnuxie <Gnuxie@protonmail.com>
|
|
* All rights reserved.
|
|
*/
|
|
|
|
import { trace, traceSync } from "../../utils";
|
|
import { ReadItem } from "./CommandReader";
|
|
import { BaseFunction, InterfaceCommand } from "./InterfaceCommand";
|
|
import { ArgumentStream, ParameterDescription } from "./ParameterParsing";
|
|
import { CommandResult } from "./Validation";
|
|
|
|
export interface PromptOptions<PresentationType = any> {
|
|
readonly suggestions: PresentationType[]
|
|
readonly default?: PresentationType
|
|
}
|
|
|
|
/**
|
|
* The idea is that the InterfaceAcceptor can use the presentation type
|
|
* to derive the prompt, or use the prompt given by the ParameterDescription.
|
|
*/
|
|
export interface InterfaceAcceptor<PresentationType = any> {
|
|
readonly isPromptable: boolean
|
|
promptForAccept(parameter: ParameterDescription, invocationRecord: CommandInvocationRecord): Promise<CommandResult<PresentationType>>
|
|
}
|
|
|
|
export interface CommandInvocationRecord {
|
|
readonly command: InterfaceCommand<BaseFunction>,
|
|
}
|
|
|
|
export class PromptableArgumentStream extends ArgumentStream {
|
|
constructor(
|
|
source: ReadItem[],
|
|
private readonly interfaceAcceptor: InterfaceAcceptor,
|
|
private readonly invocationRecord: CommandInvocationRecord,
|
|
start = 0,
|
|
) {
|
|
super([...source], start);
|
|
}
|
|
|
|
@traceSync('PromptableArgumentStream.rest')
|
|
public rest() {
|
|
return this.source.slice(this.position);
|
|
}
|
|
|
|
@traceSync('PromptableArgumentStream.isPromptable')
|
|
public isPromptable(): boolean {
|
|
return this.interfaceAcceptor.isPromptable
|
|
}
|
|
|
|
@trace('PromptableArgumentStream.prompt')
|
|
public async prompt<T = ReadItem>(parameterDescription: ParameterDescription): Promise<CommandResult<T>> {
|
|
const result = await this.interfaceAcceptor.promptForAccept(
|
|
parameterDescription,
|
|
this.invocationRecord
|
|
);
|
|
if (result.isOk()) {
|
|
this.source.push(result.ok);
|
|
}
|
|
return result;
|
|
}
|
|
}
|