@agentshouse/mdruntime 0.1.0-c0bed82db8d73 → 0.1.0-c0c0511ef12ee
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.
- package/README.md +3 -2
- package/dist/acceptance.d.ts +6 -0
- package/dist/acceptance.d.ts.map +1 -0
- package/dist/acceptance.js +385 -0
- package/dist/actions.d.ts +20 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +251 -0
- package/dist/adapter.d.ts +107 -71
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +4 -10
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +35 -46
- package/dist/bootstrap.d.ts +16 -15
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +274 -131
- package/dist/cell.d.ts +6 -0
- package/dist/cell.d.ts.map +1 -0
- package/dist/cell.js +9 -0
- package/dist/chain.d.ts +2 -1
- package/dist/chain.d.ts.map +1 -1
- package/dist/change.d.ts +1 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +4 -2
- package/dist/chat-types.d.ts +5 -0
- package/dist/chat-types.d.ts.map +1 -0
- package/dist/chat-types.js +13 -0
- package/dist/client.d.ts +25 -10
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +56 -7
- package/dist/collection.d.ts +7 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +24 -19
- package/dist/compile.d.ts +6 -6
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +287 -297
- package/dist/connection.d.ts +6 -141
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +103 -5016
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +7 -0
- package/dist/conversation-live.d.ts +136 -0
- package/dist/conversation-live.d.ts.map +1 -0
- package/dist/conversation-live.js +248 -0
- package/dist/conversation-row.d.ts +33 -0
- package/dist/conversation-row.d.ts.map +1 -0
- package/dist/conversation-row.js +53 -0
- package/dist/conversation.d.ts +41 -45
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +870 -786
- package/dist/discovery.d.ts +10 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +175 -0
- package/dist/durability.d.ts +15 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +3 -2
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +18 -8
- package/dist/figures.d.ts +49 -0
- package/dist/figures.d.ts.map +1 -0
- package/dist/figures.js +197 -0
- package/dist/file.d.ts +2 -11
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +6 -4
- package/dist/holdings.d.ts +29 -0
- package/dist/holdings.d.ts.map +1 -0
- package/dist/holdings.js +95 -0
- package/dist/host.d.ts +4 -7
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +33 -33
- package/dist/identity.d.ts +0 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +0 -3
- package/dist/index.d.ts +27 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/install.d.ts +56 -0
- package/dist/install.d.ts.map +1 -0
- package/dist/install.js +554 -0
- package/dist/job.d.ts +33 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +54 -0
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +268 -0
- package/dist/listing.d.ts +53 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +176 -0
- package/dist/live.d.ts +5 -4
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +61 -21
- package/dist/locks.d.ts +9 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +61 -0
- package/dist/log.d.ts +1 -0
- package/dist/log.d.ts.map +1 -1
- package/dist/lost.d.ts +5 -0
- package/dist/lost.d.ts.map +1 -0
- package/dist/lost.js +61 -0
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +218 -0
- package/dist/memory.d.ts +13 -6
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +219 -91
- package/dist/messages.d.ts +27 -3
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +103 -31
- package/dist/model.d.ts +8 -10
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +6 -18
- package/dist/models.d.ts +14 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +195 -0
- package/dist/observation.d.ts +3 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +15 -15
- package/dist/opened.js +3 -3
- package/dist/opening.d.ts +4 -0
- package/dist/opening.d.ts.map +1 -0
- package/dist/opening.js +57 -0
- package/dist/outcome.d.ts +17 -6
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +19 -2
- package/dist/overlay.d.ts +1 -0
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +11 -0
- package/dist/parts.d.ts +60 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +126 -0
- package/dist/paths.d.ts +2 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +26 -0
- package/dist/preparation.d.ts +20 -0
- package/dist/preparation.d.ts.map +1 -0
- package/dist/preparation.js +226 -0
- package/dist/queries.d.ts +16 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +272 -0
- package/dist/query.d.ts +18 -208
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -505
- package/dist/questions.d.ts +34 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +122 -0
- package/dist/reach.d.ts +20 -11
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +206 -126
- package/dist/react.d.ts +10 -2
- package/dist/react.d.ts.map +1 -1
- package/dist/read.d.ts +7 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +17 -0
- package/dist/readiness.d.ts +3 -18
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +4 -10
- package/dist/reads.d.ts +36 -10
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +157 -86
- package/dist/recovery.d.ts +1 -0
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +35 -1
- package/dist/refresh.d.ts +7 -5
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +34 -10
- package/dist/refusal.d.ts +5 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +55 -0
- package/dist/replica.d.ts +11 -12
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +56 -23
- package/dist/room-draft.d.ts +21 -7
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +101 -54
- package/dist/room-drafts.d.ts +24 -0
- package/dist/room-drafts.d.ts.map +1 -0
- package/dist/room-drafts.js +217 -0
- package/dist/rows.d.ts +18 -16
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +45 -153
- package/dist/runtime.d.ts +43 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts +7 -1
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +31 -24
- package/dist/saves.d.ts +70 -0
- package/dist/saves.d.ts.map +1 -0
- package/dist/saves.js +1005 -0
- package/dist/search.d.ts +12 -63
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +9 -52
- package/dist/seed.d.ts +11 -2
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +71 -1
- package/dist/shared.d.ts +1 -7
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +13 -105
- package/dist/shown.d.ts +31 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +292 -0
- package/dist/socket/admission.d.ts +7 -0
- package/dist/socket/admission.d.ts.map +1 -0
- package/dist/socket/admission.js +39 -0
- package/dist/socket/answers.d.ts +6 -0
- package/dist/socket/answers.d.ts.map +1 -0
- package/dist/socket/answers.js +20 -0
- package/dist/socket/lanes.d.ts +20 -0
- package/dist/socket/lanes.d.ts.map +1 -0
- package/dist/socket/lanes.js +77 -0
- package/dist/socket/reads.d.ts +10 -0
- package/dist/socket/reads.d.ts.map +1 -0
- package/dist/socket/reads.js +31 -0
- package/dist/socket/rooms.d.ts +44 -0
- package/dist/socket/rooms.d.ts.map +1 -0
- package/dist/socket/rooms.js +90 -0
- package/dist/socket/session.d.ts +38 -0
- package/dist/socket/session.d.ts.map +1 -0
- package/dist/socket/session.js +23 -0
- package/dist/socket.d.ts +70 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +175 -0
- package/dist/source.d.ts +61 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +82 -0
- package/dist/status.d.ts +4 -5
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -7
- package/dist/store/fences.d.ts +16 -0
- package/dist/store/fences.d.ts.map +1 -0
- package/dist/store/fences.js +26 -0
- package/dist/store/installs.d.ts +30 -0
- package/dist/store/installs.d.ts.map +1 -0
- package/dist/store/installs.js +34 -0
- package/dist/store/readiness.d.ts +10 -0
- package/dist/store/readiness.d.ts.map +1 -0
- package/dist/store/readiness.js +52 -0
- package/dist/store/records.d.ts +212 -0
- package/dist/store/records.d.ts.map +1 -0
- package/dist/store/records.js +431 -0
- package/dist/store/room-held.d.ts +8 -0
- package/dist/store/room-held.d.ts.map +1 -0
- package/dist/store/room-held.js +17 -0
- package/dist/store/space.d.ts +11 -0
- package/dist/store/space.d.ts.map +1 -0
- package/dist/store/space.js +41 -0
- package/dist/store/watchers.d.ts +17 -0
- package/dist/store/watchers.d.ts.map +1 -0
- package/dist/store/watchers.js +56 -0
- package/dist/store/writes.d.ts +24 -0
- package/dist/store/writes.d.ts.map +1 -0
- package/dist/store/writes.js +125 -0
- package/dist/store.d.ts +105 -158
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +143 -401
- package/dist/stored.d.ts +66 -55
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +355 -273
- package/dist/surface.d.ts +88 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +223 -0
- package/dist/sync.d.ts +19 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +532 -0
- package/dist/tables.d.ts +131 -69
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +221 -105
- package/dist/tallies.d.ts +18 -0
- package/dist/tallies.d.ts.map +1 -0
- package/dist/tallies.js +129 -0
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/validation.d.ts +3 -4
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +42 -100
- package/dist/windows.d.ts +1 -12
- package/dist/windows.d.ts.map +1 -1
- package/dist/windows.js +2 -75
- package/dist/wire.d.ts +25 -9
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +9 -2
- package/package.json +2 -3
- package/AGENTS.md +0 -312
- package/dist/fence.d.ts +0 -6
- package/dist/fence.d.ts.map +0 -1
- package/dist/fence.js +0 -10
- package/dist/fulltext.d.ts +0 -27
- package/dist/fulltext.d.ts.map +0 -1
- package/dist/fulltext.js +0 -368
- package/dist/words.d.ts +0 -19
- package/dist/words.d.ts.map +0 -1
- package/dist/words.js +0 -150
package/dist/query.js
CHANGED
|
@@ -1,506 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { decodedCursor } from './windows.js';
|
|
5
|
-
export class QueryError extends Error {
|
|
6
|
-
code;
|
|
7
|
-
retryable = false;
|
|
8
|
-
constructor(code, at, problem) {
|
|
9
|
-
super(`${code} at ${at}: ${problem}`);
|
|
10
|
-
this.code = code;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
const RANGE = ['$gt', '$gte', '$lt', '$lte'];
|
|
14
|
-
const SINGLE = ['$eq', '$ne', '$in', '$nin', '$all', '$size', '$exists'];
|
|
15
|
-
const FILTER = ['rooms', 'types', 'where', 'pathPrefix', 'folder', 'paths', 'hop', 'backlinks'];
|
|
16
|
-
const RELATED = ['types', 'where', 'pathPrefix', 'field'];
|
|
17
|
-
const PARTS = [
|
|
18
|
-
'fields',
|
|
19
|
-
'sections',
|
|
20
|
-
'preamble',
|
|
21
|
-
'markdown',
|
|
22
|
-
'backlinks',
|
|
23
|
-
'change',
|
|
24
|
-
'fileId',
|
|
25
|
-
'revision',
|
|
26
|
-
];
|
|
27
|
-
const ORDER = ['by', 'direction', 'nulls', 'stringSort', 'locale', 'localeOptions'];
|
|
28
|
-
const CALENDAR_DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
29
|
-
const STRAY_ESCAPE = /~(?![01])/u;
|
|
30
|
-
export function invalid(at, problem) {
|
|
31
|
-
throw new QueryError('invalid-query', at, problem);
|
|
32
|
-
}
|
|
33
|
-
function place(at, name) {
|
|
34
|
-
return at === '' ? name : `${at}.${name}`;
|
|
35
|
-
}
|
|
36
|
-
export function plain(value) {
|
|
37
|
-
return (typeof value === 'object' &&
|
|
38
|
-
value !== null &&
|
|
39
|
-
!Array.isArray(value) &&
|
|
40
|
-
(Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null));
|
|
41
|
-
}
|
|
42
|
-
export function known(value, allowed, at) {
|
|
43
|
-
for (const name of Object.keys(value)) {
|
|
44
|
-
if (!allowed.includes(name))
|
|
45
|
-
invalid(place(at, name), 'unknown part');
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function list(input, at) {
|
|
49
|
-
if (!Array.isArray(input))
|
|
50
|
-
invalid(at, 'expects a list');
|
|
51
|
-
return input;
|
|
52
|
-
}
|
|
53
|
-
function value(input, at) {
|
|
54
|
-
if (Array.isArray(input))
|
|
55
|
-
invalid(at, 'a list is not a value');
|
|
56
|
-
if (input !== null &&
|
|
57
|
-
typeof input !== 'string' &&
|
|
58
|
-
typeof input !== 'boolean' &&
|
|
59
|
-
!(typeof input === 'number' && Number.isFinite(input))) {
|
|
60
|
-
invalid(at, 'not a value');
|
|
61
|
-
}
|
|
62
|
-
return input;
|
|
63
|
-
}
|
|
64
|
-
function values(input, at) {
|
|
65
|
-
return list(input, at).map((item, index) => value(item, `${at}[${index}]`));
|
|
66
|
-
}
|
|
67
|
-
function names(input, at) {
|
|
68
|
-
const listed = list(input, at);
|
|
69
|
-
if (listed.some((item) => typeof item !== 'string'))
|
|
70
|
-
invalid(at, 'expects a list of names');
|
|
71
|
-
return [...new Set(listed)].sort();
|
|
72
|
-
}
|
|
73
|
-
function pathsOf(input) {
|
|
74
|
-
const listed = list(input, 'paths');
|
|
75
|
-
if (listed.some((item) => typeof item !== 'string' || item === '' || normalize(item) !== item)) {
|
|
76
|
-
invalid('paths', 'expects a list of Paths');
|
|
77
|
-
}
|
|
78
|
-
return [...new Set(listed)].sort();
|
|
79
|
-
}
|
|
80
|
-
function directory(input, at) {
|
|
81
|
-
if (typeof input !== 'string' || normalize(input) !== input) {
|
|
82
|
-
invalid(at, 'expects a normalized directory');
|
|
83
|
-
}
|
|
84
|
-
return input;
|
|
85
|
-
}
|
|
86
|
-
function calendarDate(value) {
|
|
87
|
-
const match = CALENDAR_DATE.exec(value);
|
|
88
|
-
if (match === null)
|
|
89
|
-
return false;
|
|
90
|
-
const [year, month, day] = match.slice(1).map(Number);
|
|
91
|
-
const date = new Date(Date.UTC(year, month - 1, day));
|
|
92
|
-
return (date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day);
|
|
93
|
-
}
|
|
94
|
-
function itemOf(facts) {
|
|
95
|
-
return facts.type === 'array' ? facts.items : facts;
|
|
96
|
-
}
|
|
97
|
-
function fits(facts, value) {
|
|
98
|
-
const item = itemOf(facts);
|
|
99
|
-
if (item?.type === undefined)
|
|
100
|
-
return true;
|
|
101
|
-
if (item.values !== undefined)
|
|
102
|
-
return item.values.includes(value);
|
|
103
|
-
if (item.type === 'number')
|
|
104
|
-
return typeof value === 'number';
|
|
105
|
-
if (item.type === 'integer')
|
|
106
|
-
return Number.isSafeInteger(value);
|
|
107
|
-
if (item.type === 'boolean')
|
|
108
|
-
return typeof value === 'boolean';
|
|
109
|
-
if (item.type !== 'string' || typeof value !== 'string')
|
|
110
|
-
return false;
|
|
111
|
-
if (item.reference !== undefined)
|
|
112
|
-
return value !== '' && normalize(value) === value;
|
|
113
|
-
if (item.format === 'date')
|
|
114
|
-
return calendarDate(value);
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
function typing(facts, rooms, types, at) {
|
|
118
|
-
const read = rooms === null ? facts.rooms : facts.rooms.filter((room) => rooms.includes(room.locator));
|
|
119
|
-
const acquiring = !facts.listed &&
|
|
120
|
-
(rooms === null ||
|
|
121
|
-
rooms.some((locator) => !facts.rooms.some((room) => room.locator === locator)));
|
|
122
|
-
const available = !acquiring && read.every((room) => room.available);
|
|
123
|
-
const declared = read.flatMap((room) => room.types);
|
|
124
|
-
for (const name of types ?? []) {
|
|
125
|
-
if (name === 'any' || !available || declared.some((type) => type.name === name))
|
|
126
|
-
continue;
|
|
127
|
-
throw new QueryError('unknown-type', place(at, 'types'), `no model it reads declares ${name}`);
|
|
128
|
-
}
|
|
129
|
-
return {
|
|
130
|
-
untyped: types === null || types.includes('any') || !available,
|
|
131
|
-
types: types === null ? declared : declared.filter((type) => types.includes(type.name)),
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
function pointer(input, at) {
|
|
135
|
-
if (typeof input !== 'string' || !input.startsWith('/') || STRAY_ESCAPE.test(input)) {
|
|
136
|
-
invalid(at, 'expects a JSON Pointer');
|
|
137
|
-
}
|
|
138
|
-
return input;
|
|
139
|
-
}
|
|
140
|
-
function specs(typed, input, at) {
|
|
141
|
-
const field = pointer(input, at);
|
|
142
|
-
const declared = typed.types.flatMap((type) => {
|
|
143
|
-
const spec = pointerFacts(type, field);
|
|
144
|
-
return spec === undefined ? [] : [spec];
|
|
145
|
-
});
|
|
146
|
-
if (declared.length === 0 && !typed.untyped) {
|
|
147
|
-
throw new QueryError('unknown-field', at, `no MD Type it reads declares ${field}`);
|
|
148
|
-
}
|
|
149
|
-
if (!declared.some((spec) => spec.conditional))
|
|
150
|
-
return declared;
|
|
151
|
-
return declared.map((spec) => ({
|
|
152
|
-
facts: { prose: false },
|
|
153
|
-
through: several(spec),
|
|
154
|
-
conditional: true,
|
|
155
|
-
}));
|
|
156
|
-
}
|
|
157
|
-
function several(spec) {
|
|
158
|
-
return spec.through || spec.facts.type === 'array';
|
|
159
|
-
}
|
|
160
|
-
function single(spec) {
|
|
161
|
-
return spec.facts.type !== undefined && spec.facts.type !== 'array';
|
|
162
|
-
}
|
|
163
|
-
function every(declared, holds) {
|
|
164
|
-
return declared.length > 0 && declared.every(holds);
|
|
165
|
-
}
|
|
166
|
-
function kind(declared, checked, at) {
|
|
167
|
-
if (checked === null ||
|
|
168
|
-
declared.length === 0 ||
|
|
169
|
-
declared.some((spec) => fits(spec.facts, checked))) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
throw new QueryError('wrong-kind', at, `${JSON.stringify(checked)} is not of its field's kind`);
|
|
173
|
-
}
|
|
174
|
-
function test(declared, input, at) {
|
|
175
|
-
if (!plain(input)) {
|
|
176
|
-
const equal = value(input, at);
|
|
177
|
-
kind(declared, equal, at);
|
|
178
|
-
return { op: 'eq', value: equal };
|
|
179
|
-
}
|
|
180
|
-
const operators = Object.keys(input);
|
|
181
|
-
if (operators.length === 0)
|
|
182
|
-
invalid(at, 'names no operator');
|
|
183
|
-
if (operators.some((operator) => RANGE.includes(operator))) {
|
|
184
|
-
const stray = operators.find((operator) => !RANGE.includes(operator));
|
|
185
|
-
if (stray !== undefined)
|
|
186
|
-
invalid(place(at, stray), 'cannot join a range');
|
|
187
|
-
if (every(declared, (spec) => spec.facts.type === 'array'))
|
|
188
|
-
invalid(at, 'a range on a list');
|
|
189
|
-
if (every(declared, (spec) => spec.facts.type === 'boolean' || spec.facts.type === 'object')) {
|
|
190
|
-
throw new QueryError('wrong-kind', at, 'its field does not compare in order');
|
|
191
|
-
}
|
|
192
|
-
return {
|
|
193
|
-
op: 'range',
|
|
194
|
-
bounds: RANGE.filter((operator) => operators.includes(operator)).map((operator) => {
|
|
195
|
-
const bound = value(input[operator], place(at, operator));
|
|
196
|
-
if (bound === null)
|
|
197
|
-
invalid(place(at, operator), 'a range bound is never null');
|
|
198
|
-
kind(declared, bound, place(at, operator));
|
|
199
|
-
return [operator.slice(1), bound];
|
|
200
|
-
}),
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
if (operators.length > 1)
|
|
204
|
-
invalid(place(at, operators[1]), 'names a second operator');
|
|
205
|
-
const [operator] = operators;
|
|
206
|
-
const where = place(at, operator);
|
|
207
|
-
if (!SINGLE.includes(operator))
|
|
208
|
-
invalid(where, 'unknown operator');
|
|
209
|
-
const operand = input[operator];
|
|
210
|
-
if (operator === '$eq' || operator === '$ne') {
|
|
211
|
-
const compared = value(operand, where);
|
|
212
|
-
kind(declared, compared, where);
|
|
213
|
-
return operator === '$eq' ? { op: 'eq', value: compared } : { op: 'ne', value: compared };
|
|
214
|
-
}
|
|
215
|
-
if (operator === '$in' || operator === '$nin' || operator === '$all') {
|
|
216
|
-
const compared = values(operand, where);
|
|
217
|
-
compared.forEach((item, index) => kind(declared, item, `${where}[${index}]`));
|
|
218
|
-
if (operator === '$in')
|
|
219
|
-
return { op: 'in', values: compared };
|
|
220
|
-
if (operator === '$nin')
|
|
221
|
-
return { op: 'nin', values: compared };
|
|
222
|
-
if (compared.includes(null))
|
|
223
|
-
invalid(where, 'a list holds no null');
|
|
224
|
-
if (every(declared, single))
|
|
225
|
-
invalid(where, 'the field is not a list');
|
|
226
|
-
return { op: 'all', values: compared };
|
|
227
|
-
}
|
|
228
|
-
if (operator === '$size') {
|
|
229
|
-
if (!Number.isSafeInteger(operand) || operand < 0) {
|
|
230
|
-
invalid(where, 'expects a nonnegative integer');
|
|
231
|
-
}
|
|
232
|
-
if (every(declared, single))
|
|
233
|
-
invalid(where, 'the field is not a list');
|
|
234
|
-
return { op: 'size', size: operand };
|
|
235
|
-
}
|
|
236
|
-
if (typeof operand !== 'boolean')
|
|
237
|
-
invalid(where, 'expects true or false');
|
|
238
|
-
return { op: 'exists', exists: operand };
|
|
239
|
-
}
|
|
240
|
-
function condition(typed, input, at) {
|
|
241
|
-
if (!plain(input))
|
|
242
|
-
invalid(at, 'expects an object of field predicates');
|
|
243
|
-
const conditions = [];
|
|
244
|
-
for (const name of Object.keys(input).sort()) {
|
|
245
|
-
const where = name.startsWith('/') ? `${at}${name}` : place(at, name);
|
|
246
|
-
const entry = input[name];
|
|
247
|
-
if (name === '$and' || name === '$or') {
|
|
248
|
-
const nested = list(entry, where).map((item, index) => condition(typed, item, `${where}[${index}]`));
|
|
249
|
-
if (nested.length === 0)
|
|
250
|
-
invalid(where, 'expects a nonempty list');
|
|
251
|
-
conditions.push(name === '$and' ? { every: nested } : { some: nested });
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
if (name.startsWith('$'))
|
|
255
|
-
invalid(where, 'unknown operator');
|
|
256
|
-
conditions.push({ field: name, test: test(specs(typed, name, where), entry, where) });
|
|
257
|
-
}
|
|
258
|
-
if (conditions.length === 0)
|
|
259
|
-
invalid(at, 'names no field');
|
|
260
|
-
return conditions.length === 1 ? conditions[0] : { every: conditions };
|
|
261
|
-
}
|
|
262
|
-
function scope(facts, rooms, input, at) {
|
|
263
|
-
const types = input.types === undefined ? null : names(input.types, place(at, 'types'));
|
|
264
|
-
const typed = typing(facts, rooms, types, at);
|
|
265
|
-
return {
|
|
266
|
-
typed,
|
|
267
|
-
types,
|
|
268
|
-
where: input.where === undefined || (plain(input.where) && Object.keys(input.where).length === 0)
|
|
269
|
-
? null
|
|
270
|
-
: condition(typed, input.where, place(at, 'where')),
|
|
271
|
-
pathPrefix: input.pathPrefix === undefined ? null : directory(input.pathPrefix, place(at, 'pathPrefix')),
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
function referenceName(typed, field, at) {
|
|
275
|
-
const declared = specs(typed, field, at);
|
|
276
|
-
if (declared.length > 0 &&
|
|
277
|
-
!declared.some((spec) => spec.conditional || referenceField(spec.facts))) {
|
|
278
|
-
invalid(at, 'not a reference field');
|
|
279
|
-
}
|
|
280
|
-
return field;
|
|
281
|
-
}
|
|
282
|
-
function hopOf(facts, rooms, root, input) {
|
|
283
|
-
if (!plain(input))
|
|
284
|
-
invalid('hop', 'expects an object');
|
|
285
|
-
known(input, RELATED, 'hop');
|
|
286
|
-
const field = referenceName(root, input.field, 'hop.field');
|
|
287
|
-
const { types, where, pathPrefix } = scope(facts, rooms, input, 'hop');
|
|
288
|
-
return { types, where, pathPrefix, field };
|
|
289
|
-
}
|
|
290
|
-
function backlinksOf(facts, rooms, input) {
|
|
291
|
-
if (!plain(input))
|
|
292
|
-
invalid('backlinks', 'expects an object');
|
|
293
|
-
known(input, RELATED, 'backlinks');
|
|
294
|
-
const { typed, types, where, pathPrefix } = scope(facts, rooms, input, 'backlinks');
|
|
295
|
-
const field = input.field === undefined ? null : referenceName(typed, input.field, 'backlinks.field');
|
|
296
|
-
return { types, where, pathPrefix, field };
|
|
297
|
-
}
|
|
298
|
-
function keyPlan(typed, input, at, path) {
|
|
299
|
-
if (input === 'room' || input === 'type' || input === 'folder' || (path && input === 'path')) {
|
|
300
|
-
return { of: input };
|
|
301
|
-
}
|
|
302
|
-
if (plain(input) && Object.hasOwn(input, 'field') && Object.keys(input).length === 1) {
|
|
303
|
-
if (specs(typed, input.field, place(at, 'field')).some(several)) {
|
|
304
|
-
invalid(place(at, 'field'), 'a path that can hold several values');
|
|
305
|
-
}
|
|
306
|
-
return { of: 'field', field: input.field };
|
|
307
|
-
}
|
|
308
|
-
return invalid(at, path ? 'expects room, path, type, folder or a field' : 'expects room, type, folder or a field');
|
|
309
|
-
}
|
|
310
|
-
function direction(input, at) {
|
|
311
|
-
if (input === undefined)
|
|
312
|
-
return 'asc';
|
|
313
|
-
if (input !== 'asc' && input !== 'desc')
|
|
314
|
-
invalid(at, 'expects asc or desc');
|
|
315
|
-
return input;
|
|
316
|
-
}
|
|
317
|
-
function orderOptions(input, at) {
|
|
318
|
-
const options = {
|
|
319
|
-
direction: direction(input.direction, place(at, 'direction')),
|
|
320
|
-
};
|
|
321
|
-
if (input.nulls !== undefined) {
|
|
322
|
-
if (input.nulls !== 'first' && input.nulls !== 'last') {
|
|
323
|
-
invalid(place(at, 'nulls'), 'expects first or last');
|
|
324
|
-
}
|
|
325
|
-
options.nulls = input.nulls;
|
|
326
|
-
}
|
|
327
|
-
if (input.stringSort !== undefined) {
|
|
328
|
-
if (input.stringSort !== 'lexical' && input.stringSort !== 'locale') {
|
|
329
|
-
invalid(place(at, 'stringSort'), 'expects lexical or locale');
|
|
330
|
-
}
|
|
331
|
-
options.stringSort = input.stringSort;
|
|
332
|
-
}
|
|
333
|
-
if (input.locale !== undefined) {
|
|
334
|
-
if (typeof input.locale !== 'string' || !collates(input.locale, undefined)) {
|
|
335
|
-
invalid(place(at, 'locale'), 'expects a locale name');
|
|
336
|
-
}
|
|
337
|
-
options.locale = input.locale;
|
|
338
|
-
}
|
|
339
|
-
if (input.localeOptions !== undefined) {
|
|
340
|
-
if (!plain(input.localeOptions) || !collates(options.locale, input.localeOptions)) {
|
|
341
|
-
invalid(place(at, 'localeOptions'), 'expects collation options');
|
|
342
|
-
}
|
|
343
|
-
options.localeOptions = input.localeOptions;
|
|
344
|
-
}
|
|
345
|
-
return options;
|
|
346
|
-
}
|
|
347
|
-
function collates(locale, options) {
|
|
348
|
-
try {
|
|
349
|
-
''.localeCompare('', locale, options);
|
|
350
|
-
return true;
|
|
351
|
-
}
|
|
352
|
-
catch {
|
|
353
|
-
return false;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
function parts(typed, input, at, fields) {
|
|
357
|
-
if (input === undefined)
|
|
358
|
-
return null;
|
|
359
|
-
if (input === 'all')
|
|
360
|
-
return 'all';
|
|
361
|
-
const named = list(input, at);
|
|
362
|
-
named.forEach((name, index) => {
|
|
363
|
-
if (fields)
|
|
364
|
-
specs(typed, name, `${at}[${index}]`);
|
|
365
|
-
else if (typeof name !== 'string')
|
|
366
|
-
invalid(`${at}[${index}]`, 'expects a name');
|
|
367
|
-
});
|
|
368
|
-
return named;
|
|
369
|
-
}
|
|
370
|
-
export function flag(input, at) {
|
|
371
|
-
if (input === undefined)
|
|
372
|
-
return false;
|
|
373
|
-
if (input !== true)
|
|
374
|
-
invalid(at, 'expects true');
|
|
375
|
-
return true;
|
|
376
|
-
}
|
|
377
|
-
function documentShape(typed, input) {
|
|
378
|
-
const orderBy = (input.orderBy === undefined ? [] : list(input.orderBy, 'orderBy')).map((clause, index) => {
|
|
379
|
-
const at = `orderBy[${index}]`;
|
|
380
|
-
if (!plain(clause))
|
|
381
|
-
invalid(at, 'expects an order clause');
|
|
382
|
-
known(clause, ORDER, at);
|
|
383
|
-
return {
|
|
384
|
-
key: keyPlan(typed, clause.by, place(at, 'by'), true),
|
|
385
|
-
options: orderOptions(clause, at),
|
|
386
|
-
};
|
|
387
|
-
});
|
|
388
|
-
const select = input.select;
|
|
389
|
-
if (select === 'all') {
|
|
390
|
-
return {
|
|
391
|
-
kind: 'documents',
|
|
392
|
-
fields: 'all',
|
|
393
|
-
sections: 'all',
|
|
394
|
-
preamble: true,
|
|
395
|
-
markdown: true,
|
|
396
|
-
backlinks: true,
|
|
397
|
-
change: true,
|
|
398
|
-
fileId: true,
|
|
399
|
-
revision: true,
|
|
400
|
-
orderBy,
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
if (!plain(select))
|
|
404
|
-
invalid('select', "expects 'all' or the parts to return");
|
|
405
|
-
known(select, PARTS, 'select');
|
|
406
|
-
return {
|
|
407
|
-
kind: 'documents',
|
|
408
|
-
fields: parts(typed, select.fields, 'select.fields', true),
|
|
409
|
-
sections: parts(typed, select.sections, 'select.sections', false),
|
|
410
|
-
preamble: flag(select.preamble, 'select.preamble'),
|
|
411
|
-
markdown: flag(select.markdown, 'select.markdown'),
|
|
412
|
-
backlinks: flag(select.backlinks, 'select.backlinks'),
|
|
413
|
-
change: flag(select.change, 'select.change'),
|
|
414
|
-
fileId: flag(select.fileId, 'select.fileId'),
|
|
415
|
-
revision: flag(select.revision, 'select.revision'),
|
|
416
|
-
orderBy,
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
function groupShape(typed, input) {
|
|
420
|
-
const groupBy = list(input.groupBy, 'groupBy').map((key, index) => keyPlan(typed, key, `groupBy[${index}]`, false));
|
|
421
|
-
if (groupBy.length === 0)
|
|
422
|
-
invalid('groupBy', 'names no key');
|
|
423
|
-
const grouped = groupBy.map((key) => JSON.stringify(key));
|
|
424
|
-
const orderBy = (input.orderBy === undefined ? [] : list(input.orderBy, 'orderBy')).map((clause, index) => {
|
|
425
|
-
const at = `orderBy[${index}]`;
|
|
426
|
-
if (!plain(clause))
|
|
427
|
-
invalid(at, 'expects an order clause');
|
|
428
|
-
known(clause, ['by', 'direction'], at);
|
|
429
|
-
const by = clause.by === 'count'
|
|
430
|
-
? 'count'
|
|
431
|
-
: grouped.indexOf(JSON.stringify(keyPlan(typed, clause.by, place(at, 'by'), false)));
|
|
432
|
-
if (by === -1)
|
|
433
|
-
invalid(place(at, 'by'), 'orders by a key the query does not group by');
|
|
434
|
-
return { by, direction: direction(clause.direction, place(at, 'direction')) };
|
|
435
|
-
});
|
|
436
|
-
return { kind: 'groups', groupBy, orderBy };
|
|
437
|
-
}
|
|
438
|
-
export function planOf(input, facts, reach = null) {
|
|
439
|
-
if (!plain(input))
|
|
440
|
-
invalid('query', 'expects a query object');
|
|
441
|
-
const grouped = input.groupBy !== undefined;
|
|
442
|
-
if (grouped && input.select !== undefined)
|
|
443
|
-
invalid('select', 'a grouped query selects nothing');
|
|
444
|
-
if (!grouped && input.select === undefined)
|
|
445
|
-
invalid('select', 'a Document query must select');
|
|
446
|
-
known(input, [...FILTER, 'orderBy', 'limit', 'after', 'total', grouped ? 'groupBy' : 'select'], '');
|
|
447
|
-
const limit = limitOf(input.limit);
|
|
448
|
-
const rooms = input.rooms === undefined ? null : names(input.rooms, 'rooms');
|
|
449
|
-
const { typed, ...root } = scope(facts, rooms, input, '');
|
|
450
|
-
const shape = grouped ? groupShape(typed, input) : documentShape(typed, input);
|
|
451
|
-
return {
|
|
452
|
-
key: queryKey(input),
|
|
453
|
-
rooms,
|
|
454
|
-
reach,
|
|
455
|
-
locked: [],
|
|
456
|
-
root,
|
|
457
|
-
folder: input.folder === undefined ? null : directory(input.folder, 'folder'),
|
|
458
|
-
paths: input.paths === undefined ? null : pathsOf(input.paths),
|
|
459
|
-
hop: input.hop === undefined ? null : hopOf(facts, rooms, typed, input.hop),
|
|
460
|
-
backlinks: input.backlinks === undefined ? null : backlinksOf(facts, rooms, input.backlinks),
|
|
461
|
-
shape,
|
|
462
|
-
limit,
|
|
463
|
-
after: continuationOf(input.after, shape.orderBy.length + 1),
|
|
464
|
-
total: flag(input.total, 'total'),
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
function continuationOf(input, arity) {
|
|
468
|
-
if (input === undefined)
|
|
469
|
-
return null;
|
|
470
|
-
const values = typeof input === 'string' ? decodedCursor(input) : null;
|
|
471
|
-
if (values === null || (values.length !== 0 && values.length !== arity)) {
|
|
472
|
-
invalid('after', 'expects the next an earlier result of this query returned');
|
|
473
|
-
}
|
|
474
|
-
return { text: input, values: values.length === 0 ? null : values };
|
|
475
|
-
}
|
|
476
|
-
export function limitOf(input) {
|
|
477
|
-
if (input === undefined)
|
|
478
|
-
return null;
|
|
479
|
-
if (!Number.isSafeInteger(input) || input < 0) {
|
|
480
|
-
invalid('limit', 'expects a nonnegative integer');
|
|
481
|
-
}
|
|
482
|
-
return input;
|
|
483
|
-
}
|
|
484
|
-
export function filterOf(input, facts, reach = null) {
|
|
485
|
-
if (!plain(input))
|
|
486
|
-
invalid('documents', 'expects a filter object');
|
|
487
|
-
known(input, FILTER, '');
|
|
488
|
-
return planOf({ ...input, select: {} }, facts, reach);
|
|
489
|
-
}
|
|
490
|
-
export function queryKey(query) {
|
|
491
|
-
return canonical(query);
|
|
492
|
-
}
|
|
493
|
-
function canonical(value) {
|
|
494
|
-
if (Array.isArray(value))
|
|
495
|
-
return `[${value.map(canonical).join(',')}]`;
|
|
496
|
-
if (typeof value === 'object' && value !== null) {
|
|
497
|
-
return `{${Object.keys(value)
|
|
498
|
-
.sort()
|
|
499
|
-
.map((name) => `${JSON.stringify(name)}:${canonical(value[name])}`)
|
|
500
|
-
.join(',')}}`;
|
|
501
|
-
}
|
|
502
|
-
return JSON.stringify(value);
|
|
503
|
-
}
|
|
504
|
-
export function identityOf(plan) {
|
|
505
|
-
return canonical(plan);
|
|
1
|
+
import { planOf } from '@agentshouse/mdmodel';
|
|
2
|
+
export function planWithin(input, facts, reach = null) {
|
|
3
|
+
return { ...planOf(input, facts), reach, locked: [] };
|
|
506
4
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ConversationView } from './conversation.js';
|
|
2
|
+
export type ChatChoice = {
|
|
3
|
+
readonly value: string;
|
|
4
|
+
readonly label: string;
|
|
5
|
+
readonly about: string | null;
|
|
6
|
+
};
|
|
7
|
+
export type ChatField = {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly kind: 'string' | 'number' | 'integer' | 'boolean' | 'multi-select';
|
|
10
|
+
readonly prompt: string;
|
|
11
|
+
readonly about: string | null;
|
|
12
|
+
readonly choices: readonly ChatChoice[];
|
|
13
|
+
};
|
|
14
|
+
export type ChatAnswer = Readonly<Record<string, unknown>>;
|
|
15
|
+
export type ChatQuestion = {
|
|
16
|
+
readonly interaction: string;
|
|
17
|
+
readonly asked: string | null;
|
|
18
|
+
readonly secret: boolean;
|
|
19
|
+
readonly message: string;
|
|
20
|
+
readonly fields: readonly ChatField[];
|
|
21
|
+
readonly undelivered: boolean;
|
|
22
|
+
accept(answer: ChatAnswer): unknown;
|
|
23
|
+
readonly decline: unknown;
|
|
24
|
+
};
|
|
25
|
+
export declare function entered(field: ChatField): boolean;
|
|
26
|
+
export declare function questionsOf(view: Pick<ConversationView, 'questions'> | null): readonly ChatQuestion[];
|
|
27
|
+
export declare function openQuestion(questions: readonly unknown[]): string | null;
|
|
28
|
+
export declare function questionAsked(questions: readonly unknown[]): string | null;
|
|
29
|
+
export type ChatLone = {
|
|
30
|
+
readonly question: ChatQuestion;
|
|
31
|
+
readonly field: ChatField;
|
|
32
|
+
};
|
|
33
|
+
export declare function loneOf(questions: readonly ChatQuestion[]): ChatLone | null;
|
|
34
|
+
//# sourceMappingURL=questions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"questions.d.ts","sourceRoot":"","sources":["../src/questions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAG1D,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,cAAc,CAAC;IAC5E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,OAAO,EAAE,SAAS,UAAU,EAAE,CAAC;CACzC,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAE3D,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,CAAC;IACtC,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B,CAAC;AAuCF,wBAAgB,OAAO,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAEjD;AA2DD,wBAAgB,WAAW,CACzB,IAAI,EAAE,IAAI,CAAC,gBAAgB,EAAE,WAAW,CAAC,GAAG,IAAI,GAC/C,SAAS,YAAY,EAAE,CAiBzB;AAED,wBAAgB,YAAY,CAAC,SAAS,EAAE,SAAS,OAAO,EAAE,GAAG,MAAM,GAAG,IAAI,CAKzE;AAED,wBAAgB,aAAa,CAAC,SAAS,EAAE,SAAS,OAAO,EAAE,GAAG,MAAM,GAAG,IAAI,CAE1E;AAED,MAAM,MAAM,QAAQ,GAAG;IAAE,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAA;CAAE,CAAC;AAEtF,wBAAgB,MAAM,CAAC,SAAS,EAAE,SAAS,YAAY,EAAE,GAAG,QAAQ,GAAG,IAAI,CAM1E"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { excerpt, list, record, text } from './read.js';
|
|
2
|
+
const OPTION = 'optionId';
|
|
3
|
+
function choicesOf(schema) {
|
|
4
|
+
const items = record(schema.items);
|
|
5
|
+
const listed = [schema.oneOf, schema.enum, items.anyOf, items.enum].find(Array.isArray) ?? [];
|
|
6
|
+
return listed.flatMap((one) => {
|
|
7
|
+
if (typeof one === 'string')
|
|
8
|
+
return [{ value: one, label: one, about: null }];
|
|
9
|
+
const value = text(record(one), 'const');
|
|
10
|
+
return value === null
|
|
11
|
+
? []
|
|
12
|
+
: [
|
|
13
|
+
{
|
|
14
|
+
value,
|
|
15
|
+
label: text(record(one), 'title') ?? value,
|
|
16
|
+
about: text(record(one), 'description'),
|
|
17
|
+
},
|
|
18
|
+
];
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function fieldOf(name, schema) {
|
|
22
|
+
const type = text(schema, 'type');
|
|
23
|
+
const kind = type === 'boolean' || type === 'number' || type === 'integer'
|
|
24
|
+
? type
|
|
25
|
+
: type === 'array'
|
|
26
|
+
? 'multi-select'
|
|
27
|
+
: 'string';
|
|
28
|
+
return {
|
|
29
|
+
name,
|
|
30
|
+
kind,
|
|
31
|
+
prompt: text(schema, 'title') ?? name,
|
|
32
|
+
about: text(schema, 'description'),
|
|
33
|
+
choices: kind === 'boolean' ? [] : choicesOf(schema),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export function entered(field) {
|
|
37
|
+
return field.kind !== 'boolean' && field.choices.length === 0;
|
|
38
|
+
}
|
|
39
|
+
function valueOf(field, raw) {
|
|
40
|
+
return field.kind === 'number' || field.kind === 'integer' ? Number(raw) : raw;
|
|
41
|
+
}
|
|
42
|
+
function typed(fields, answer) {
|
|
43
|
+
return Object.fromEntries(Object.entries(answer).map(([name, value]) => {
|
|
44
|
+
const field = fields.find((one) => one.name === name);
|
|
45
|
+
return [
|
|
46
|
+
name,
|
|
47
|
+
field !== undefined && entered(field) && typeof value === 'string'
|
|
48
|
+
? valueOf(field, value)
|
|
49
|
+
: value,
|
|
50
|
+
];
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
function askedOf(request) {
|
|
54
|
+
const params = record(request.params);
|
|
55
|
+
const method = text(request, 'method');
|
|
56
|
+
if (method === 'session/request_permission') {
|
|
57
|
+
return {
|
|
58
|
+
message: '',
|
|
59
|
+
fields: [
|
|
60
|
+
{
|
|
61
|
+
name: OPTION,
|
|
62
|
+
kind: 'string',
|
|
63
|
+
prompt: text(record(params.toolCall), 'title') ?? '',
|
|
64
|
+
about: null,
|
|
65
|
+
choices: list(params, 'options').flatMap((entry) => {
|
|
66
|
+
const one = record(entry);
|
|
67
|
+
const value = text(one, OPTION);
|
|
68
|
+
return value === null
|
|
69
|
+
? []
|
|
70
|
+
: [{ value, label: text(one, 'name') ?? value, about: null }];
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
accept: (answer) => ({ outcome: { outcome: 'selected', optionId: answer[OPTION] } }),
|
|
75
|
+
decline: null,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (method !== 'elicitation/create')
|
|
79
|
+
return null;
|
|
80
|
+
const fields = Object.entries(record(record(params.requestedSchema).properties)).map(([name, schema]) => fieldOf(name, record(schema)));
|
|
81
|
+
return {
|
|
82
|
+
message: text(params, 'message') ?? '',
|
|
83
|
+
fields,
|
|
84
|
+
accept: (answer) => ({ action: 'accept', content: typed(fields, answer) }),
|
|
85
|
+
decline: { action: 'decline' },
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export function questionsOf(view) {
|
|
89
|
+
return (view?.questions ?? []).flatMap((entry) => {
|
|
90
|
+
const one = record(entry);
|
|
91
|
+
const interaction = text(one, 'interaction_id');
|
|
92
|
+
const asked = askedOf(record(one.request));
|
|
93
|
+
return asked === null || interaction === null
|
|
94
|
+
? []
|
|
95
|
+
: [
|
|
96
|
+
{
|
|
97
|
+
interaction,
|
|
98
|
+
asked: text(one, 'asked_at'),
|
|
99
|
+
secret: one.secret === true,
|
|
100
|
+
undelivered: one.telegram_undelivered === true,
|
|
101
|
+
...asked,
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export function openQuestion(questions) {
|
|
107
|
+
const [asked] = questionsOf({ questions });
|
|
108
|
+
if (asked === undefined)
|
|
109
|
+
return null;
|
|
110
|
+
const said = asked.message === '' ? asked.fields[0]?.prompt : asked.message;
|
|
111
|
+
return said === undefined || said === '' ? null : excerpt(said);
|
|
112
|
+
}
|
|
113
|
+
export function questionAsked(questions) {
|
|
114
|
+
return questionsOf({ questions })[0]?.asked ?? null;
|
|
115
|
+
}
|
|
116
|
+
export function loneOf(questions) {
|
|
117
|
+
const asked = questions.length === 1 && !questions[0].secret ? questions[0] : undefined;
|
|
118
|
+
const only = asked?.fields.length === 1 ? asked.fields[0] : undefined;
|
|
119
|
+
return asked !== undefined && only !== undefined && entered(only)
|
|
120
|
+
? { question: asked, field: only }
|
|
121
|
+
: null;
|
|
122
|
+
}
|