ekms 8.0.0-beta.77 → 8.0.0-beta.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ {
2
+ }
@@ -0,0 +1,253 @@
1
+ const { knowledgeModule, where, stableId } = require('./runtime').theprogrammablemind
2
+ const meta = require('./meta.js')
3
+ const gdefaults = require('./gdefaults.js')
4
+ const sdefaults = require('./sdefaults.js')
5
+ const conjunction = require('./conjunction.js')
6
+ const asking_tests = require('./asking.test.json')
7
+ const { defaultContextCheck, indent, focus, requiredArgument } = require('./helpers')
8
+ const pluralize = require('pluralize')
9
+
10
+ // TODO implement what / what did you say ...
11
+ let config = {
12
+ name: 'asking',
13
+ operators: [
14
+ "([nevermind])",
15
+ { pattern: "([nevermindTestSetup] (allowed))", development: true },
16
+ { pattern: "([whichOnesTestSetup] (choices)*)", development: true },
17
+ ],
18
+ bridges: [
19
+ {
20
+ id: "nevermind",
21
+ bridge: "{ ...next(operator) }",
22
+ semantic: (args) => {
23
+ const {config, context} = args
24
+ // stop asking all questions
25
+ for (const semantic of config.semantics) {
26
+ if (semantic.isQuestion) {
27
+ let doRemove = true
28
+ if (semantic.onNevermind && semantic.getWasAsked() && !semantic.getWasApplied()) {
29
+ doRemove = semantic.onNevermind(args)
30
+ }
31
+ if (doRemove) {
32
+ config.removeSemantic(semantic)
33
+ }
34
+ }
35
+ }
36
+ }
37
+ },
38
+
39
+ {
40
+ id: "whichOnesTestSetup",
41
+ development: true,
42
+ generatorp: async ({context, gs}) => `${context.marker} ${await gs(context.choices)}`,
43
+ bridge: "{ ...next(operator), choices: after }",
44
+ semantic: ({askWhich, context}) => {
45
+ const choices = context.choices
46
+ const chosen = ({ choice, objects }) => {
47
+ objects.choice = choice
48
+ }
49
+
50
+ const question = async ({choices, g, gs, wasAsked, state}) => {
51
+ if (wasAsked) {
52
+ return `${await g(state.lastChoice)} is not a choice. The choices are: ${await gs(choices, ' ', ' or ')}`
53
+ } else {
54
+ return `Which value do you want: ${await gs(choices, ' ', ' or ')}`
55
+ }
56
+ }
57
+
58
+ const isChoice = ({context, choices, state}) => {
59
+ state.lastChoice = context
60
+ for (const choice of choices) {
61
+ if (choice.value == context.value) {
62
+ return true
63
+ }
64
+ }
65
+ }
66
+
67
+ const onNevermind = ({objects, context}) => {
68
+ objects.onNevermindWasCalled = true
69
+ return true
70
+ }
71
+
72
+ askWhich({ choices, chosen, question, isChoice, onNevermind })
73
+ }
74
+ },
75
+
76
+ {
77
+ id: "nevermindTestSetup",
78
+ development: true,
79
+ bridge: "{ ...next(operator), type: after[0], postModifiers: ['type'] }",
80
+ semantic: ({ask, context}) => {
81
+ const nevermindType = context.type.value
82
+ ask({
83
+ applyq: () => 'the test question?',
84
+ onNevermind: ({objects, context}) => {
85
+ objects.onNevermindWasCalled = true
86
+ objects.nevermindType = nevermindType
87
+ return nevermindType == 'accept'
88
+ },
89
+ matchr: () => false,
90
+ applyr: () => {},
91
+ })
92
+ }
93
+ },
94
+ ],
95
+ };
96
+
97
+ const getAsk = (config) => (uuid) => {
98
+ return (asks) => {
99
+ const ask = (ask) => {
100
+ let oneShot = true // default
101
+ if (ask.oneShot === false) {
102
+ oneShot = false
103
+ }
104
+
105
+ const id_q = stableId('semantic')
106
+ const id_rs = []
107
+ let wasAsked = false
108
+ let wasApplied = false
109
+ const getWasAsked = () => {
110
+ return wasAsked
111
+ }
112
+ const setWasAsked = (value) => {
113
+ wasAsked = value
114
+ }
115
+ const getWasApplied = () => {
116
+ return wasApplied
117
+ }
118
+ const setWasApplied = (value) => {
119
+ wasApplied = value
120
+ }
121
+
122
+ const semanticsr = ask.semanticsr || []
123
+ if (semanticsr.length == 0) {
124
+ semanticsr.push({ match: ask.matchr, apply: ask.applyr })
125
+ }
126
+ for (const semantic of semanticsr) {
127
+ const id_r = stableId('semantic')
128
+ id_rs.push(id_r)
129
+ config.addSemantic({
130
+ uuid,
131
+ id: id_r,
132
+ tied_ids: [id_q],
133
+ oneShot,
134
+ where: semantic.where || ask.where || where(2),
135
+ source: 'response',
136
+ match: (args) => semantic.match(args),
137
+ apply: async (args) => {
138
+ setWasApplied(true)
139
+ await semantic.apply(args)
140
+ },
141
+ })
142
+ }
143
+
144
+ config.addSemantic({
145
+ uuid,
146
+ oneShot,
147
+ id: id_q,
148
+ tied_ids: id_rs,
149
+ where: ask.where,
150
+ isQuestion: true, // do one question at a time
151
+ getWasAsked,
152
+ getWasApplied,
153
+ onNevermind: ask.onNevermind,
154
+ source: 'question',
155
+ match: ({ context }) => context.marker == 'controlEnd' || context.marker == 'controlBetween',
156
+ apply: async (args) => {
157
+ let matchq = ask.matchq
158
+ let applyq = ask.applyq
159
+ if (!matchq) {
160
+ let wasAsked = false
161
+ matchq = () => !wasAsked,
162
+ applyq = (args) => {
163
+ wasAsked = true
164
+ return ask.applyq(args)
165
+ }
166
+ }
167
+ if (await matchq(args)) {
168
+ setWasApplied(false)
169
+ // args.context.motivationKeep = true
170
+ args.verbatim(await applyq({ ...args, wasAsked: getWasAsked() }))
171
+ setWasAsked(true)
172
+ args.context.controlKeepMotivation = true
173
+ }
174
+ args.context.cascade = true
175
+ }
176
+ })
177
+ }
178
+ if (!Array.isArray(asks)) {
179
+ asks = [asks]
180
+ }
181
+
182
+ [...asks].reverse().forEach( (a) => ask(a) )
183
+ }
184
+ }
185
+
186
+
187
+ const initializer = ({objects, config, isModule}) => {
188
+ config.addArgs(({config, api, isA}) => ({
189
+ getUUIDScoped: (uuid) => {
190
+ const ask = getAsk(config)(uuid)
191
+ return {
192
+ ask,
193
+ askWhich: ({ choices, chosen, question, isChoice, onNevermind }) => {
194
+ let state = {}
195
+
196
+ requiredArgument(choices, 'choices')
197
+ requiredArgument(chosen, 'chosen')
198
+
199
+ if (!onNevermind) {
200
+ onNevermind = ({objects, context}) => {
201
+ return true
202
+ }
203
+ }
204
+
205
+ if (!question) {
206
+ const question = async ({choices, g, gs, wasAsked, state}) => {
207
+ if (wasAsked) {
208
+ return `${await g(state.lastChoice)} is not a choice. The choices are: ${await gs(choices, ' ', ' or ')}`
209
+ } else {
210
+ return `Which value do you want: ${await gs(choices, ' ', ' or ')}`
211
+ }
212
+ }
213
+ }
214
+
215
+ if (!isChoice) {
216
+ const isChoice = ({context, choices, state}) => {
217
+ state.lastChoice = context
218
+ for (const choice of choices) {
219
+ if (choice.value == context.value) {
220
+ return true
221
+ }
222
+ }
223
+ }
224
+ }
225
+
226
+ ask({
227
+ applyq: async (args) => await question({...args, choices, state}),
228
+ onNevermind,
229
+ matchr: (args) => isChoice({...args, choices, state}),
230
+ applyr: (args) => chosen({...args, choice: args.context}),
231
+ })
232
+ }
233
+ }
234
+ },
235
+ }))
236
+ }
237
+
238
+ knowledgeModule( {
239
+ config,
240
+ includes: [conjunction, gdefaults, sdefaults],
241
+ initializer,
242
+ module,
243
+ description: 'asking the user questions',
244
+ newWay: true,
245
+ test: {
246
+ name: './asking.test.json',
247
+ contents: asking_tests,
248
+ checks: {
249
+ objects: ['onNevermindWasCalled', 'nevermindType', 'choice'],
250
+ context: defaultContextCheck,
251
+ },
252
+ },
253
+ })