Skip to content

Commit 271bbe9

Browse files
committed
20241106 @Mookse
Version 0.0.26 Merge branch 'base' into azure-deploy-prod
2 parents 1bcdf67 + 6bf3f8c commit 271bbe9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2290
-1895
lines changed

inc/js/agents/system/bot-agent.mjs

+154-72
Large diffs are not rendered by default.

inc/js/functions.mjs

+64-74
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@ async function about(ctx){
88
await ctx.render('about')
99
}
1010
/**
11-
* Activate a bot for the member
12-
* @module
11+
* Activate a specific Bot.
1312
* @public
14-
* @api no associated view
15-
* @param {object} ctx Koa Context object
16-
* @returns {object} Koa Context object
13+
* @async
14+
* @param {object} ctx - Koa Context object
15+
* @returns {object} - Activated Response object: { bot_id, greeting, success, version, versionUpdate, }
1716
*/
18-
function activateBot(ctx){
19-
const { avatar, } = ctx.state
20-
avatar.activeBotId = ctx.params.bid
21-
const { activeBotId, activeBotVersion, activeBotNewestVersion, } = avatar
22-
ctx.body = {
23-
activeBotId,
24-
activeBotVersion,
25-
version: activeBotNewestVersion,
26-
}
17+
async function activateBot(ctx){
18+
const { bid, } = ctx.params
19+
if(!ctx.Globals.isValidGuid(bid))
20+
ctx.throw(400, `missing bot id`)
21+
const { avatar: Avatar, } = ctx.state
22+
const response =await Avatar.setActiveBot(bid)
23+
ctx.body = response
2724
}
2825
async function alerts(ctx){
2926
// @todo: put into ctx the _type_ of alert to return, system use dataservices, member use personal
@@ -34,52 +31,55 @@ async function alerts(ctx){
3431
ctx.body = await ctx.state.MemberSession.alerts(ctx.request.body)
3532
}
3633
}
34+
/**
35+
* Manage bots for the member.
36+
* @param {Koa} ctx - Koa Context object
37+
* @returns {object} - Koa Context object
38+
*/
3739
async function bots(ctx){
3840
const { bid, } = ctx.params // bot_id sent in url path
39-
const { avatar } = ctx.state
41+
const { avatar: Avatar, } = ctx.state
4042
const bot = ctx.request.body
4143
?? {}
4244
switch(ctx.method){
4345
case 'DELETE': // retire bot
4446
if(!ctx.Globals.isValidGuid(bid))
4547
ctx.throw(400, `missing bot id`)
46-
ctx.body = await avatar.retireBot(bid)
48+
ctx.body = await Avatar.retireBot(bid)
4749
break
4850
case 'POST': // create new bot
49-
ctx.body = await avatar.createBot(bot)
51+
ctx.body = await Avatar.createBot(bot)
5052
break
5153
case 'PUT': // update bot
52-
ctx.body = await avatar.updateBot(bot)
54+
ctx.body = await Avatar.updateBot(bot)
5355
break
5456
case 'GET':
5557
default:
5658
if(bid?.length){ // specific bot
57-
ctx.body = await avatar.getBot(ctx.params.bid)
59+
ctx.body = await Avatar.getBot(ctx.params.bid)
5860
} else {
59-
const { activeBotId, } = avatar
60-
const bots = await avatar.getBots()
61+
const bots = await Avatar.getBots()
62+
let { activeBotId, greeting, } = Avatar
63+
if(!activeBotId){
64+
const { bot_id, greeting: activeGreeting } = await Avatar.setActiveBot()
65+
activeBotId = bot_id
66+
greeting = activeGreeting
67+
}
6168
ctx.body = { // wrap bots
6269
activeBotId,
6370
bots,
71+
greeting,
6472
}
6573
}
6674
break
6775
}
6876
}
69-
function category(ctx){ // sets category for avatar
70-
ctx.state.category = ctx.request.body
71-
const { avatar, } = ctx.state
72-
avatar.setActiveCategory(ctx.state.category)
73-
ctx.body = avatar.category
74-
}
7577
/**
7678
* Challenge the member session with a passphrase.
77-
* @module
7879
* @public
7980
* @async
80-
* @api - No associated view
8181
* @param {Koa} ctx - Koa Context object
82-
* @returns {object} Koa Context object
82+
* @returns {object} - Koa Context object
8383
* @property {object} ctx.body - The result of the challenge.
8484
*/
8585
async function challenge(ctx){
@@ -99,6 +99,8 @@ async function challenge(ctx){
9999
}
100100
/**
101101
* Chat with the Member or System Avatar's intelligence.
102+
* @public
103+
* @async
102104
* @param {Koa} ctx - Koa Context object
103105
* @returns {object} - The response from the chat in `ctx.body`
104106
* @property {object} instruction - Instructionset for the frontend to execute (optional)
@@ -131,41 +133,38 @@ async function createBot(ctx){
131133
ctx.body = await avatar.createBot(bot)
132134
}
133135
/**
134-
* Delete an item from collection via the member's avatar.
135-
* @async
136-
* @public
137-
* @param {object} ctx - Koa Context object
138-
* @returns {boolean} - Under `ctx.body`, status of deletion.
136+
* Save feedback from the member.
137+
* @param {Koa} ctx - Koa Context object
138+
* @returns {Boolean} - Whether or not the feedback was saved
139139
*/
140-
async function deleteItem(ctx){
141-
const { iid, } = ctx.params
142-
const { avatar, } = ctx.state
143-
if(!iid?.length)
144-
ctx.throw(400, `missing item id`)
145-
ctx.body = await avatar.deleteItem(iid)
140+
async function feedback(ctx){
141+
const { mid: message_id, } = ctx.params
142+
const { avatar: Avatar, } = ctx.state
143+
const { isPositive=true, message, } = ctx.request.body
144+
ctx.body = await Avatar.feedback(message_id, isPositive, message)
146145
}
147146
/**
148-
* Get greetings for this bot/active bot.
149-
* @todo - move dynamic system responses to a separate function (route /system)
150-
* @param {Koa} ctx - Koa Context object.
151-
* @returns {object} - Greetings response message object: { success: false, messages: [], }.
147+
* Get greetings for active situation.
148+
* @public
149+
* @async
150+
* @param {Koa} ctx - Koa Context object
151+
* @returns {object} - Greetings response message object: { responses, success, }
152152
*/
153153
async function greetings(ctx){
154154
const { vld: validateId, } = ctx.request.query
155155
let { dyn: dynamic, } = ctx.request.query
156156
if(typeof dynamic==='string')
157157
dynamic = JSON.parse(dynamic)
158-
const { avatar, } = ctx.state
159-
let response = { success: false, messages: [], }
160-
if(validateId?.length)
161-
response.messages.push(...await avatar.validateRegistration(validateId))
162-
else
163-
response.messages.push(...await avatar.greeting(dynamic))
164-
response.success = response.messages.length > 0
158+
const { avatar: Avatar, } = ctx.state
159+
const response = validateId?.length && Avatar.isMyLife
160+
? await Avatar.validateRegistration(validateId)
161+
: await Avatar.greeting(dynamic)
165162
ctx.body = response
166163
}
167164
/**
168165
* Request help about MyLife.
166+
* @public
167+
* @async
169168
* @param {Koa} ctx - Koa Context object, body={ request: string|required, mbr_id, type: string, }.
170169
* @returns {object} - Help response message object.
171170
*/
@@ -179,8 +178,8 @@ async function help(ctx){
179178
}
180179
/**
181180
* Index page for the application.
182-
* @async
183181
* @public
182+
* @async
184183
* @param {object} ctx - Koa Context object
185184
*/
186185
async function index(ctx){
@@ -210,18 +209,12 @@ async function item(ctx){
210209
const { avatar, } = ctx.state
211210
const { globals, } = avatar
212211
const { method, } = ctx.request
213-
const item = ctx.request.body
214-
/* validate payload */
215-
if(!id?.length)
216-
ctx.throw(400, `missing item id`)
217-
if(!item || typeof item !== 'object' || !Object.keys(item).length)
218-
ctx.throw(400, `missing item data`)
219-
const { id: itemId, } = item
220-
if(itemId && itemId!==id) // ensure item.id is set to id
221-
throw new Error(`item.id must match /:iid`)
222-
else if(!itemId)
212+
const item = ctx.request.body // always `{}` by default
213+
if(!item?.id && id?.length)
223214
item.id = id
224-
ctx.body = await avatar.item(item, method)
215+
const response = await avatar.item(item, method)
216+
delete avatar.frontendInstruction // already embedded in response
217+
ctx.body = response
225218
}
226219
async function logout(ctx){
227220
ctx.session = null
@@ -354,17 +347,15 @@ async function signup(ctx) {
354347
const { mbr_id, ..._registrationData } = signupPacket // do not display theoretical memberId
355348
ctx.status = 200 // OK
356349
ctx.body = {
350+
message: 'Signup successful',
357351
payload: _registrationData,
358352
success,
359-
message: 'Signup successful',
360353
}
361354
}
362355
async function summarize(ctx){
363-
const { avatar, } = ctx.state
356+
const { avatar: Avatar, } = ctx.state
364357
const { fileId, fileName, } = ctx.request.body
365-
if(avatar.isMyLife)
366-
throw new Error('Only logged in members may summarize text')
367-
ctx.body = await avatar.summarize(fileId, fileName)
358+
ctx.body = await Avatar.summarize(fileId, fileName)
368359
}
369360
/**
370361
* Get a specified team, its details and bots, by id for the member.
@@ -384,8 +375,8 @@ async function team(ctx){
384375
* @returns {Object[]} - List of team objects.
385376
*/
386377
async function teams(ctx){
387-
const { avatar, } = ctx.state
388-
ctx.body = await avatar.teams()
378+
const { avatar: Avatar, } = ctx.state
379+
ctx.body = await Avatar.teams()
389380
}
390381
async function updateBotInstructions(ctx){
391382
const { bid, } = ctx.params
@@ -417,14 +408,13 @@ export {
417408
activateBot,
418409
alerts,
419410
bots,
420-
category,
421411
challenge,
422412
chat,
423413
collections,
424414
createBot,
425-
deleteItem,
426-
help,
415+
feedback,
427416
greetings,
417+
help,
428418
index,
429419
interfaceMode,
430420
item,

0 commit comments

Comments
 (0)