1 | interface Versionable {
|
2 | _version?: string
|
3 | }
|
4 |
|
5 | export 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 |
|
29 | export 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 |
|
54 | export interface Properties {
|
55 | [key: string]: Property.Any
|
56 | }
|
57 |
|
58 | export type Context<P extends Properties> = {
|
59 | [K in keyof P]: P[K][1]
|
60 | }
|
61 |
|
62 | export interface ContextOperation<P extends Properties> {
|
63 | timestamp: number
|
64 | context: Partial<Context<P>>
|
65 | }
|
66 |
|
67 | export 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 |
|
79 | export 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 |
|
89 | export 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 |
|
97 | export interface DecisionTreeLeaf {
|
98 | predicted_value: string | number | null
|
99 | confidence: number
|
100 | standard_deviation: number
|
101 | decision_rule: DecisionRule.Any
|
102 | }
|
103 |
|
104 | export interface DecisionTreeNode {
|
105 | decision_rule: DecisionRule.Any
|
106 | children: (DecisionTreeLeaf | DecisionTreeNode)[]
|
107 | }
|
108 |
|
109 | export 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 |
|
121 | export interface Client {
|
122 | cfg: {
|
123 | token: string
|
124 | operationsChunksSize: number
|
125 | }
|
126 |
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | addAgentContextOperations<P extends Properties> (agentId: string, contextOperations: ContextOperation<P> | ContextOperation<P>[]): Promise<Agent<P>>
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | computeAgentDecision<P extends Properties> (agentId: string, treeTimestamp: number, ...timeOrContextOperation: (Partial<Context<P>> | Time)[]): Decision<P>
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | createAgent<P extends Properties> (configuration: Configuration<P>, agentId?: string): Promise<Agent<P>>
|
156 |
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 | deleteAgent<P extends Properties> (agentId: string): Promise<Agent<P>>
|
163 |
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 | deleteSharedAgentInspectorUrl (agentId: string): Promise<void>
|
170 |
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 | getAgent<P extends Properties> (agentId: string): Promise<Agent<P>>
|
177 |
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | getAgentContext<P extends Properties> (agentId: string, timestamp?: number): Promise<ContextOperation<P>>
|
186 |
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | getAgentContextOperations<P extends Properties> (agentId: string, start?: number, end?: number): Promise<ContextOperation<P>[]>
|
195 |
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | getAgentStateHistory<P extends Properties> (agentId: string, start?: number, end?: number): Promise<ContextOperation<P>[]>
|
204 |
|
205 | |
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | getAgentDecisionTree<P extends Properties> (agentId: string, timestamp?: number): Promise<DecisionTree<P>>
|
213 |
|
214 | |
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | getSharedAgentInspectorUrl (agentId: string, timestamp?: number): Promise<string>
|
222 |
|
223 | |
224 |
|
225 |
|
226 | listAgents (): Promise<string[]>
|
227 |
|
228 | |
229 |
|
230 |
|
231 | destroyAgent<P extends Properties> (agentId: string): Promise<Agent<P>>
|
232 |
|
233 | |
234 |
|
235 |
|
236 | getAgentInspectorUrl (agentId: string, timestamp?: number): Promise<string>
|
237 | }
|
238 |
|
239 | export 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 |
|
249 | export type ClientConfiguration = string | {
|
250 | token: string
|
251 | operationsChunksSize?: number
|
252 | }
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | export 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 | */
|
267 | export function createClient (configuration: ClientConfiguration): Client
|
268 |
|
269 | /**
|
270 | * Decision tree interpreter functions.
|
271 | */
|
272 | export 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 |
|
301 | export default createClient
|
302 |
|
\ | No newline at end of file |