-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzy.ai-cmdln.coffee
393 lines (347 loc) · 10.8 KB
/
fuzzy.ai-cmdln.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# fuzzy-ai.coffee
#
# Command-line tool for maintaining and using Fuzzy.ai agents
#
# Copyright 2016 Fuzzy.ai
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
path = require 'path'
fs = require 'fs'
readline = require 'readline'
FuzzyAIClient = require 'fuzzy.ai'
yargs = require 'yargs'
async = require 'async'
CSON = require 'cson'
_ = require 'lodash'
parse = require 'csv-parse'
transform = require 'stream-transform'
stringify = require 'csv-stringify'
debug = require('debug')('fuzzy.ai:cmdln')
argv = yargs
.usage('Usage: $0 <command> [options]')
.demand(2)
.command('create <agentfile>', 'Create a new agent from a file')
.command('read <agent>', 'Export the agent')
.command('update <agent> <agentfile>', 'Update the agent from the cson file')
.command('delete <agent>', 'Delete the agent')
.command('batch <agent> <csvfile>', 'Evaluate all inputs from csv file')
.command('list', 'List all agents in account')
.demand('k')
.alias('k', 'key')
.describe('k', 'API key')
.alias('r', 'root')
.describe('r', 'root of the API server')
.default('r', 'https://api.fuzzy.ai')
.alias('o', 'output')
.describe('o', 'Output file')
.alias('q', 'quiet')
.describe('q', 'Minimize console output')
.boolean('q')
.default('q', false)
.alias('b', 'batch-size')
.describe('b', "Batch size")
.number('b')
.default('b', 128)
.boolean('y')
.alias('y', 'yes')
.describe('y', 'Answer "yes" to all questions')
.env('FUZZY_AI')
.alias('c', 'config')
.describe('c', 'Config file')
.default('c', path.join(process.env.HOME, '.fuzzy.ai.json'))
.config('config')
.help('h')
.alias('h', 'help')
.argv
# pretty-print an object as JSON
pp = (obj) ->
JSON.stringify(obj, null, 2) + "\n"
parseAgentFile = (agentFile, callback) ->
ext = path.extname(agentFile).toLowerCase()
switch ext
when '.cson'
CSON.parseCSONFile agentFile, {}, callback
when '.json'
fs.readFile agentFile, 'utf-8', (err, str) ->
if err
callback err
else
try
model = JSON.parse(str)
# We don't want exceptions bubbling back up here
setImmediate callback, null, model
catch error
callback null, error
else
callback new Error "Unrecognized extension for #{agentFile}"
# FIXME: this is kind of inefficient.
toID = (client, nameOrID, callback) ->
client.getAgents (err, agents) ->
if err
callback err
else
withID = _.find agents, {id: nameOrID}
if withID?
callback null, withID.id
else
withName = _.filter agents, {name: nameOrID}
if !withName? or withName.length == 0
callback new Error("No such agent with name or ID '#{nameOrID}'")
else if withName.length > 1
ids = _.map withName, "id"
str = ids.join(', ')
msg = "Too many matches for name '#{nameOrID}'; choose one of #{str}"
callback new Error(msg)
else
callback null, withName[0].id
toOutputStream = (name, callback) ->
if name?
try
str = fs.createWriteStream name
# So exceptions don't bubble up here
setImmediate callback, null, str
catch err
callback err, null
else
callback null, process.stdout
transformCSVFile = (filename, output, handler, callback) ->
parser = parse
delimiter: ','
columns: true
auto_parse: true
auto_parse_date: true
stringifier = stringify
delimiter: ','
header: true
input = fs.createReadStream filename
blankToNull = transform (record, callback) ->
for name, value of record
if _.isString record[name] and record[name].length is 0
record[name] = null
callback null, record
transformer = transform handler
output.on 'finish', ->
callback null
input
.pipe(parser)
.pipe(blankToNull)
.pipe(transformer)
.pipe(stringifier)
.pipe(output)
handler =
create: (client, argv, callback) ->
debug("Creating a new agent")
created = null
async.waterfall [
(callback) ->
debug "Parsing agent file #{argv.agentfile}"
parseAgentFile argv.agentfile, callback
(agent, callback) ->
debug "Got agent"
debug agent
client.newAgent agent, callback
(results, callback) ->
created = results
debug created
toOutputStream argv.o, callback
(str, callback) ->
if argv.q
str.write created.id, "utf-8", callback
else
str.write pp(created), "utf-8", callback
], callback
read: (client, argv, callback) ->
agent = null
async.waterfall [
(callback) ->
toID client, argv.agent, callback
(id, callback) ->
client.getAgent id, callback
(results, callback) ->
agent = results
toOutputStream argv.o, callback
(str, callback) ->
str.write pp(agent), "utf-8", callback
], callback
update: (client, argv, callback) ->
async.waterfall [
(callback) ->
async.parallel [
(callback) ->
toID client, argv.agent, callback
(callback) ->
parseAgentFile argv.agentfile, callback
], callback
(results, callback) ->
[id, agent] = results
client.putAgent id, agent, callback
(updated, callback) ->
async.waterfall [
(callback) ->
toOutputStream argv.o, callback
(str, callback) ->
if argv.q
str.write updated.id, "utf-8", callback
else
str.write pp(updated), "utf-8", callback
], callback
], callback
delete: (client, argv, callback) ->
doDelete = (callback) ->
async.waterfall [
(callback) ->
toID client, argv.agent, callback
(id, callback) ->
client.deleteAgent id, callback
], callback
if argv.y
doDelete callback
else
rl = readline.createInterface
input: process.stdin
output: process.stdout
rl.question "Really delete agent #{argv.agent}? ", (answer) ->
if answer == "y"
doDelete callback
else
callback null
batch: (client, argv, callback) ->
id = null
inputNames = null
outputNames = null
debug "Starting batch for #{argv.agent}"
if argv.b < 1 or argv.b > 4096
return callback new Error("Batch size must be between 1 and 4096")
async.waterfall [
(callback) ->
debug "Getting ID for #{argv.agent}"
toID client, argv.agent, callback
(results, callback) ->
id = results
debug "ID for #{argv.agent} is #{id}"
client.getAgent id, callback
(agent, callback) ->
debug "Getting inputNames and outputNames for #{id}"
inputNames = _.keys agent.inputs
outputNames = _.keys agent.outputs
debug inputNames
debug outputNames
debug "Getting output stream for #{argv.o}"
toOutputStream argv.o, callback
(output, callback) ->
buffer = []
debug "Got stream for #{argv.o}"
parser = parse
delimiter: ','
columns: true
auto_parse: true
auto_parse_date: true
stringifier = stringify
delimiter: ','
header: true
debug "Creating input stream for #{argv.csvfile}"
input = fs.createReadStream argv.csvfile
b2n = (record, callback) ->
for name, value of record
if _.isString record[name] and record[name].length is 0
record[name] = null
callback null, record
blankToNull = transform b2n, {parallel: Infinity}
debug blankToNull
postBatch = (batch) ->
inputs = _.map batch, (batchItem) ->
_.pick batchItem[0], inputNames
debug "Posting batch (#{batch.length})"
start = _.now()
client.evaluate id, inputs, (err, outputs) ->
end = _.now()
debug "Evaluation of (#{batch.length}) items took #{end - start}ms"
if err
debug err
_.each batch, (batchItem) ->
setImmediate batchItem[1], err
else
_.each batch, (batchItem, i) ->
results = _.assign {}, batchItem[0], outputs[i]
setImmediate batchItem[1], null, results
enqueueRecord = (record, callback) ->
debug "Got record"
buffer.push [record, callback]
debug "Buffer length = #{buffer.length} (max #{argv.b})"
if buffer.length == argv.b
debug "Flushing buffer"
batch = buffer.slice()
buffer = []
postBatch batch
transformer = transform enqueueRecord, {parallel: Infinity}
debug "parallel = #{transformer.options.parallel}"
blankToNull.on 'end', ->
debug "Finishing input"
batch = buffer.slice()
buffer = []
postBatch batch
output.on 'finish', ->
debug "Finished"
callback null
debug "Setting up pipeline"
input
.pipe(parser)
.pipe(blankToNull)
.pipe(transformer)
.pipe(stringifier)
.pipe(output)
], callback
list: (client, argv, callback) ->
debug("listing agents")
agents = null
async.waterfall [
(callback) ->
debug "Getting all agents"
client.getAgents callback
(results, callback) ->
agents = results
debug agents
toOutputStream argv.o, callback
(output, callback) ->
stringifier = stringify
delimiter: ','
header: true
columns: ["id", "name"]
debug("Created stringifier")
stringifier.pipe(output)
debug("Walking through agents")
for agent in agents
debug(JSON.stringify(agent))
stringifier.write([agent.id, agent.name])
debug("Done")
stringifier.end()
], callback
main = (argv, callback) ->
client = new FuzzyAIClient argv.k, argv.r
command = argv._[0]
if !_.has(handler, command)
callback new Error("Unrecognized command: #{command}")
else
handler[command] client, argv, callback
main argv, (err) ->
if err
process.stderr.write err.message + "\n"
if err.body?
try
parsed = JSON.parse(err.body)
process.stderr.write parsed.message + "\n"
catch pe
a = 1
process.exit 1
else
process.exit 0