interface Versionable { _version?: string } export namespace Property { interface Property { type: T is_generated?: boolean } interface GeneratedProperty extends Property { is_generated?: true } type Enum = [Property<'enum'>, T] type Continuous = [Property<'continuous'>, number] type Timezone = [Property<'timezone'>, string | number] type DayOfMonth = [GeneratedProperty<'day_of_month'>, number] type DayOfWeek = [GeneratedProperty<'day_of_week'>, number] type MonthOfYear = [GeneratedProperty<'month_of_year'>, number] type TimeOfDay = [GeneratedProperty<'time_of_day'>, number] type Cyclic = DayOfMonth | DayOfWeek | MonthOfYear | TimeOfDay type Generated = DayOfMonth | DayOfWeek | MonthOfYear | TimeOfDay type Any = Cyclic | Enum | Continuous | Timezone type NamedProperty = Any & { property: string } } export namespace DecisionRule { interface Default { property: string operator: string operand: any } export interface Continuous extends Default { operator: '<' | '>=' operand: number } export interface Enum extends Default { operator: 'is' operand: string } export interface Cyclic extends Default { operator: '[in[' operand: [number, number] } export type Any = Continuous | Enum | Cyclic } export interface Properties { [key: string]: Property.Any } export type Context

= { [K in keyof P]: P[K][1] } export interface ContextOperation

{ timestamp: number context: Partial> } export interface Configuration

extends Versionable { context: { [K in keyof P]: P[K][0] } output: (keyof P)[] time_quantum?: number learning_period?: number operations_as_events?: boolean tree_max_operations?: number tree_max_depth?: number } export interface Agent

extends Versionable { id: string configuration: Configuration

firstTimestamp: number lastTimestamp: number creationDate: number lastTreeUpdate: number lastContextUpdate: number } export interface DecisionTree

extends Versionable { configuration: Configuration

timestamp?: number trees: { [K in keyof P]: DecisionTreeLeaf | (DecisionTreeLeaf | DecisionTreeNode)[] } } export interface DecisionTreeLeaf { predicted_value: string | number | null confidence: number standard_deviation: number decision_rule: DecisionRule.Any } export interface DecisionTreeNode { decision_rule: DecisionRule.Any children: (DecisionTreeLeaf | DecisionTreeNode)[] } export interface Decision

extends Versionable { context: Context

output: { [K in keyof P]: { predicted_value?: string | number confidence?: number standard_deviation?: number decision_rules: DecisionRule.Any[] } } } export interface Client { cfg: { token: string operationsChunksSize: number } /** * Adds context operations to the given agent id. * * First context operation must define every property of the agent configuration. * @param agentId Id of the agent * @param contextOperations Context operation or array of context operations */ addAgentContextOperations

(agentId: string, contextOperations: ContextOperation

| ContextOperation

[]): Promise> /** * Computes the decision for a given context with the decision tree of the provided agent id. * * The partial context operations are merged from left to right to forge a single context. * The properties of the right-most partial context operation overwrites the other. * `Time` instance is used to generate the time-related properties and considered as a partial context operation. * @param agentId Id of the agent * @param treeTimestamp Timestamp of the tree used to compute the decision * @param timeOrContextOperation A list of partial context operation or an instance of `Time` */ computeAgentDecision

(agentId: string, treeTimestamp: number, ...timeOrContextOperation: (Partial> | Time)[]): Decision

/** * Creates an agent in the current project. * * If the agent id is not provided, craft ai generates a random id. * @param configuration Configuration of the agent * @param agentId Id of the agent */ createAgent

(configuration: Configuration

, agentId?: string): Promise> /** * Deletes the given agent id from the current project. * * @param agentId Id of the agent */ deleteAgent

(agentId: string): Promise> /** * Deletes the public inspector URL for the given agent id. * * @param agentId Id of the agent */ deleteSharedAgentInspectorUrl (agentId: string): Promise /** * Retrieves the information about the given agent id. * * @param agentId Id of the agent */ getAgent

(agentId: string): Promise> /** * Retrieves the full context of the given agent id. * * If the timestamp is not provided the last pushed context operation timestamp is used as default. * @param agentId Id of the agent * @param timestamp Timestamp of the full context to be retrieved */ getAgentContext

(agentId: string, timestamp?: number): Promise> /** * Retrieves every context operations pushed to the given agent id. * * @param agentId Id of the agent * @param start The timestamp lower bound of the desired contexts (included) * @param end The timestamp upper bound of the desired contexts (included) */ getAgentContextOperations

(agentId: string, start?: number, end?: number): Promise[]> /** * Retrieves every an agent's state history, at every `time_quantum`, between the given bounds. * * @param agentId Id of the agent * @param start The timestamp lower bound of the desired contexts (included) * @param end The timestamp upper bound of the desired contexts (included) */ getAgentStateHistory

(agentId: string, start?: number, end?: number): Promise[]> /** * Retrieves the decision tree of the given agent id. * * If the timestamp is not provided the last pushed context operation timestamp is used as default. * @param agentId Id of the agent * @param timestamp Timestamp of the tree to be retrieved */ getAgentDecisionTree

(agentId: string, timestamp?: number): Promise> /** * Retrieves the public inspector URL for the given agent id. * * If the timestamp is not provided the last pushed context operation timestamp is used as default. * @param agentId Id of the agent * @param timestamp Default timestamp used to display the decision tree in the public inspector */ getSharedAgentInspectorUrl (agentId: string, timestamp?: number): Promise /** * List all agents id from the current project */ listAgents (): Promise /** * @deprecated Replaced by `client.deleteAgent()` */ destroyAgent

(agentId: string): Promise> /** * @deprecated Replaced by `client.getSharedAgentInspectorUrl()` */ getAgentInspectorUrl (agentId: string, timestamp?: number): Promise } export interface Time { timestamp: number timezone: string | number time_of_day: number day_of_month: number month_of_year: number day_of_week: number utc: string } export type ClientConfiguration = string | { token: string operationsChunksSize?: number } /** * Helper for time-related properties generation */ export function Time (date: any): Time /** * Creates a client instance of craft ai. * * A client is bound to a project. * The configuration must include at least an access token to a craft ai project. * You can create a project and get an access token with your logged account on https://beta.craft.ai/projects. * @param configuration Configuration of the client */ export function createClient (configuration: ClientConfiguration): Client /** * Decision tree interpreter functions. */ export declare const interpreter: { /** * Computes the decision for a given context with the provided decision tree. * * @param tree Decision tree retrieved from craft ai (see `client.getAgentDecisionTree()`) * @param context Full context */ decide

(tree: DecisionTree

, context: Context

): Decision

/** * Computes the decision for a given context with the provided decision tree. * * The partial context operations are merged from left to right to forge a single context. * The properties of the right-most partial context operation overwrites the other. * `Time` instance is used to generate the time-related properties and considered as a partial context operation. * * @param tree Decision tree retrieved from craft ai (see `client.getAgentDecisionTree()`) * @param timeOrContextOperation A list of partial context operation or an instance of `Time` */ decide

(tree: DecisionTree

, ...timeOrContextOperation: (Partial> | Time)[]): Decision

/** * Traverse the given tree to retrieve all the properties used in decision rules * * @param tree The decision tree. */ getDecisionRulesProperties

(tree: DecisionTree

): Property.NamedProperty[] } export default createClient