UNPKG

9.65 kBTypeScriptView Raw
1interface Versionable {
2 _version?: string
3}
4
5export namespace Property {
6 interface Property<T> {
7 type: T
8 is_generated?: boolean
9 }
10
11 interface GeneratedProperty<T> extends Property<T> {
12 is_generated?: true
13 }
14
15 type Enum<T extends string> = [Property<'enum'>, T]
16 type Continuous = [Property<'continuous'>, number]
17 type Timezone = [Property<'timezone'>, string | number]
18 type DayOfMonth = [GeneratedProperty<'day_of_month'>, number]
19 type DayOfWeek = [GeneratedProperty<'day_of_week'>, number]
20 type MonthOfYear = [GeneratedProperty<'month_of_year'>, number]
21 type TimeOfDay = [GeneratedProperty<'time_of_day'>, number]
22 type Cyclic = DayOfMonth | DayOfWeek | MonthOfYear | TimeOfDay
23 type Generated = DayOfMonth | DayOfWeek | MonthOfYear | TimeOfDay
24 type Any = Cyclic | Enum<string> | Continuous | Timezone
25
26 type NamedProperty = Any & { property: string }
27}
28
29export namespace DecisionRule {
30 interface Default {
31 property: string
32 operator: string
33 operand: any
34 }
35
36 export interface Continuous extends Default {
37 operator: '<' | '>='
38 operand: number
39 }
40
41 export interface Enum extends Default {
42 operator: 'is'
43 operand: string
44 }
45
46 export interface Cyclic extends Default {
47 operator: '[in['
48 operand: [number, number]
49 }
50
51 export type Any = Continuous | Enum | Cyclic
52}
53
54export interface Properties {
55 [key: string]: Property.Any
56}
57
58export type Context<P extends Properties> = {
59 [K in keyof P]: P[K][1]
60}
61
62export interface ContextOperation<P extends Properties> {
63 timestamp: number
64 context: Partial<Context<P>>
65}
66
67export interface Configuration<P extends Properties> extends Versionable {
68 context: {
69 [K in keyof P]: P[K][0]
70 }
71 output: (keyof P)[]
72 time_quantum?: number
73 learning_period?: number
74 operations_as_events?: boolean
75 tree_max_operations?: number
76 tree_max_depth?: number
77}
78
79export interface Agent<P extends Properties> extends Versionable {
80 id: string
81 configuration: Configuration<P>
82 firstTimestamp: number
83 lastTimestamp: number
84 creationDate: number
85 lastTreeUpdate: number
86 lastContextUpdate: number
87}
88
89export interface DecisionTree<P extends Properties> extends Versionable {
90 configuration: Configuration<P>
91 timestamp?: number
92 trees: {
93 [K in keyof P]: DecisionTreeLeaf | (DecisionTreeLeaf | DecisionTreeNode)[]
94 }
95}
96
97export interface DecisionTreeLeaf {
98 predicted_value: string | number | null
99 confidence: number
100 standard_deviation: number
101 decision_rule: DecisionRule.Any
102}
103
104export interface DecisionTreeNode {
105 decision_rule: DecisionRule.Any
106 children: (DecisionTreeLeaf | DecisionTreeNode)[]
107}
108
109export interface Decision<P extends Properties> extends Versionable {
110 context: Context<P>
111 output: {
112 [K in keyof P]: {
113 predicted_value?: string | number
114 confidence?: number
115 standard_deviation?: number
116 decision_rules: DecisionRule.Any[]
117 }
118 }
119}
120
121export interface Client {
122 cfg: {
123 token: string
124 operationsChunksSize: number
125 }
126
127 /**
128 * Adds context operations to the given agent id.
129 *
130 * First context operation must define every property of the agent configuration.
131 * @param agentId Id of the agent
132 * @param contextOperations Context operation or array of context operations
133 */
134 addAgentContextOperations<P extends Properties> (agentId: string, contextOperations: ContextOperation<P> | ContextOperation<P>[]): Promise<Agent<P>>
135
136 /**
137 * Computes the decision for a given context with the decision tree of the provided agent id.
138 *
139 * The partial context operations are merged from left to right to forge a single context.
140 * The properties of the right-most partial context operation overwrites the other.
141 * `Time` instance is used to generate the time-related properties and considered as a partial context operation.
142 * @param agentId Id of the agent
143 * @param treeTimestamp Timestamp of the tree used to compute the decision
144 * @param timeOrContextOperation A list of partial context operation or an instance of `Time`
145 */
146 computeAgentDecision<P extends Properties> (agentId: string, treeTimestamp: number, ...timeOrContextOperation: (Partial<Context<P>> | Time)[]): Decision<P>
147
148 /**
149 * Creates an agent in the current project.
150 *
151 * If the agent id is not provided, craft ai generates a random id.
152 * @param configuration Configuration of the agent
153 * @param agentId Id of the agent
154 */
155 createAgent<P extends Properties> (configuration: Configuration<P>, agentId?: string): Promise<Agent<P>>
156
157 /**
158 * Deletes the given agent id from the current project.
159 *
160 * @param agentId Id of the agent
161 */
162 deleteAgent<P extends Properties> (agentId: string): Promise<Agent<P>>
163
164 /**
165 * Deletes the public inspector URL for the given agent id.
166 *
167 * @param agentId Id of the agent
168 */
169 deleteSharedAgentInspectorUrl (agentId: string): Promise<void>
170
171 /**
172 * Retrieves the information about the given agent id.
173 *
174 * @param agentId Id of the agent
175 */
176 getAgent<P extends Properties> (agentId: string): Promise<Agent<P>>
177
178 /**
179 * Retrieves the full context of the given agent id.
180 *
181 * If the timestamp is not provided the last pushed context operation timestamp is used as default.
182 * @param agentId Id of the agent
183 * @param timestamp Timestamp of the full context to be retrieved
184 */
185 getAgentContext<P extends Properties> (agentId: string, timestamp?: number): Promise<ContextOperation<P>>
186
187 /**
188 * Retrieves every context operations pushed to the given agent id.
189 *
190 * @param agentId Id of the agent
191 * @param start The timestamp lower bound of the desired contexts (included)
192 * @param end The timestamp upper bound of the desired contexts (included)
193 */
194 getAgentContextOperations<P extends Properties> (agentId: string, start?: number, end?: number): Promise<ContextOperation<P>[]>
195
196 /**
197 * Retrieves every an agent's state history, at every `time_quantum`, between the given bounds.
198 *
199 * @param agentId Id of the agent
200 * @param start The timestamp lower bound of the desired contexts (included)
201 * @param end The timestamp upper bound of the desired contexts (included)
202 */
203 getAgentStateHistory<P extends Properties> (agentId: string, start?: number, end?: number): Promise<ContextOperation<P>[]>
204
205 /**
206 * Retrieves the decision tree of the given agent id.
207 *
208 * If the timestamp is not provided the last pushed context operation timestamp is used as default.
209 * @param agentId Id of the agent
210 * @param timestamp Timestamp of the tree to be retrieved
211 */
212 getAgentDecisionTree<P extends Properties> (agentId: string, timestamp?: number): Promise<DecisionTree<P>>
213
214 /**
215 * Retrieves the public inspector URL for the given agent id.
216 *
217 * If the timestamp is not provided the last pushed context operation timestamp is used as default.
218 * @param agentId Id of the agent
219 * @param timestamp Default timestamp used to display the decision tree in the public inspector
220 */
221 getSharedAgentInspectorUrl (agentId: string, timestamp?: number): Promise<string>
222
223 /**
224 * List all agents id from the current project
225 */
226 listAgents (): Promise<string[]>
227
228 /**
229 * @deprecated Replaced by `client.deleteAgent()`
230 */
231 destroyAgent<P extends Properties> (agentId: string): Promise<Agent<P>>
232
233 /**
234 * @deprecated Replaced by `client.getSharedAgentInspectorUrl()`
235 */
236 getAgentInspectorUrl (agentId: string, timestamp?: number): Promise<string>
237}
238
239export interface Time {
240 timestamp: number
241 timezone: string | number
242 time_of_day: number
243 day_of_month: number
244 month_of_year: number
245 day_of_week: number
246 utc: string
247}
248
249export type ClientConfiguration = string | {
250 token: string
251 operationsChunksSize?: number
252}
253
254/**
255 * Helper for time-related properties generation
256 */
257export function Time (date: any): Time
258
259/**
260 * Creates a client instance of craft ai.
261 *
262 * A client is bound to a project.
263 * The configuration must include at least an access token to a craft ai project.
264 * You can create a project and get an access token with your logged account on https://beta.craft.ai/projects.
265 * @param configuration Configuration of the client
266 */
267export function createClient (configuration: ClientConfiguration): Client
268
269/**
270 * Decision tree interpreter functions.
271 */
272export declare const interpreter: {
273 /**
274 * Computes the decision for a given context with the provided decision tree.
275 *
276 * @param tree Decision tree retrieved from craft ai (see `client.getAgentDecisionTree()`)
277 * @param context Full context
278 */
279 decide<P extends Properties> (tree: DecisionTree<P>, context: Context<P>): Decision<P>
280
281 /**
282 * Computes the decision for a given context with the provided decision tree.
283 *
284 * The partial context operations are merged from left to right to forge a single context.
285 * The properties of the right-most partial context operation overwrites the other.
286 * `Time` instance is used to generate the time-related properties and considered as a partial context operation.
287 *
288 * @param tree Decision tree retrieved from craft ai (see `client.getAgentDecisionTree()`)
289 * @param timeOrContextOperation A list of partial context operation or an instance of `Time`
290 */
291 decide<P extends Properties> (tree: DecisionTree<P>, ...timeOrContextOperation: (Partial<Context<P>> | Time)[]): Decision<P>
292
293 /**
294 * Traverse the given tree to retrieve all the properties used in decision rules
295 *
296 * @param tree The decision tree.
297 */
298 getDecisionRulesProperties<P extends Properties> (tree: DecisionTree<P>): Property.NamedProperty[]
299}
300
301export default createClient
302
\No newline at end of file