@clagentic/console 1.9.0-beta.3 → 1.10.0-beta.1
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/LICENSE +117 -12
- package/LICENSES/MIT-legacy.txt +22 -0
- package/LICENSES/README.md +15 -0
- package/README.md +8 -2
- package/bin/cli.js +30 -5
- package/deploy/clagentic-console.service +10 -6
- package/lib/audit.js +3 -2
- package/lib/build-sha.json +2 -2
- package/lib/build-update-check.js +138 -0
- package/lib/cli/daemon-launch.js +17 -17
- package/lib/config.js +32 -20
- package/lib/daemon-env-settings.js +84 -0
- package/lib/daemon-projects.js +38 -0
- package/lib/daemon.js +202 -158
- package/lib/drain.js +74 -17
- package/lib/env-compat.js +61 -0
- package/lib/extension-commands.js +45 -0
- package/lib/history-visibility.js +4 -0
- package/lib/idle-adapter-reaper.js +71 -0
- package/lib/link-trap.js +78 -0
- package/lib/lore-attestation.js +73 -0
- package/lib/mcp-local.js +8 -6
- package/lib/mcp-tool-input.js +116 -0
- package/lib/mcp-tool-schema.js +179 -0
- package/lib/memory-limits.js +61 -5
- package/lib/memory-setting-validate.js +65 -0
- package/lib/new-work-gate.js +47 -0
- package/lib/os-users.js +5 -3
- package/lib/project-access.js +139 -0
- package/lib/project-connection.js +23 -22
- package/lib/project-external-trigger.js +103 -10
- package/lib/project-file-scope.js +172 -0
- package/lib/project-file-watch.js +86 -61
- package/lib/project-filesystem.js +144 -128
- package/lib/project-git.js +119 -0
- package/lib/project-http.js +44 -28
- package/lib/project-loop.js +12 -5
- package/lib/project-mcp.js +53 -72
- package/lib/project-message-gate.js +70 -0
- package/lib/project-notifications.js +31 -4
- package/lib/project-prompt-responses.js +216 -0
- package/lib/project-sessions.js +164 -302
- package/lib/project-user-message.js +36 -11
- package/lib/project.js +189 -142
- package/lib/projects-dir.js +90 -0
- package/lib/prompt-access.js +22 -0
- package/lib/prompt-kinds/answer-limits.js +15 -0
- package/lib/prompt-kinds/ask-user.js +58 -0
- package/lib/prompt-kinds/codecs.js +97 -0
- package/lib/prompt-kinds/elicitation.js +77 -0
- package/lib/prompt-kinds/extension.js +39 -0
- package/lib/prompt-kinds/index.js +68 -0
- package/lib/prompt-kinds/permission.js +114 -0
- package/lib/prompt-kinds/plan.js +79 -0
- package/lib/prompt-kinds/tool-call.js +38 -0
- package/lib/prompt-registry.js +673 -0
- package/lib/public/app.js +1 -1
- package/lib/public/css/diagnostics.css +50 -0
- package/lib/public/css/input.css +17 -3
- package/lib/public/css/menus.css +10 -0
- package/lib/public/css/messages.css +5 -0
- package/lib/public/css/rewind.css +22 -0
- package/lib/public/index.html +4 -12
- package/lib/public/modules/app-connection.js +7 -0
- package/lib/public/modules/app-cursors.js +7 -5
- package/lib/public/modules/app-messages.js +45 -45
- package/lib/public/modules/app-notifications.js +47 -71
- package/lib/public/modules/app-panels.js +61 -58
- package/lib/public/modules/app-projects.js +2 -1
- package/lib/public/modules/app-rendering.js +4 -10
- package/lib/public/modules/diagnostics.js +60 -16
- package/lib/public/modules/filebrowser.js +18 -16
- package/lib/public/modules/history-visibility.js +4 -0
- package/lib/public/modules/mention.js +3 -2
- package/lib/public/modules/mode-options.js +38 -0
- package/lib/public/modules/project-settings.js +19 -8
- package/lib/public/modules/prompt-banner.js +78 -0
- package/lib/public/modules/prompt-card.js +201 -0
- package/lib/public/modules/prompt-kinds/ask-user-codec.js +131 -0
- package/lib/public/modules/prompt-kinds/ask-user.js +213 -0
- package/lib/public/modules/prompt-kinds/elicitation-codec.js +343 -0
- package/lib/public/modules/prompt-kinds/elicitation.js +176 -0
- package/lib/public/modules/prompt-kinds/index.js +18 -0
- package/lib/public/modules/prompt-kinds/own-key.js +29 -0
- package/lib/public/modules/prompt-kinds/package.json +3 -0
- package/lib/public/modules/prompt-kinds/permission-choices.js +31 -0
- package/lib/public/modules/prompt-kinds/permission.js +209 -0
- package/lib/public/modules/prompt-kinds/plan.js +102 -0
- package/lib/public/modules/prompt-state.js +142 -0
- package/lib/public/modules/prompts.js +134 -0
- package/lib/public/modules/rewind.js +4 -1
- package/lib/public/modules/server-settings.js +2 -4
- package/lib/public/modules/sidebar-mobile.js +19 -9
- package/lib/public/modules/sidebar-sessions.js +21 -14
- package/lib/public/modules/sticky-notes.js +3 -2
- package/lib/public/modules/team-panel.js +5 -4
- package/lib/public/modules/tools.js +67 -904
- package/lib/public/modules/vendor-picker.js +53 -0
- package/lib/public/modules/vendor-registry.js +133 -0
- package/lib/public/sw.js +3 -1
- package/lib/rc-migrate.js +44 -0
- package/lib/runtime-env-keys.js +109 -0
- package/lib/runtime-env.js +236 -0
- package/lib/sdk-bridge.js +290 -273
- package/lib/sdk-message-processor.js +125 -145
- package/lib/server-hub-sessions.js +13 -10
- package/lib/server-palette.js +28 -19
- package/lib/server.js +141 -113
- package/lib/service-install.js +96 -0
- package/lib/session-capabilities.js +76 -0
- package/lib/sessions.js +45 -28
- package/lib/users-permissions.js +9 -6
- package/lib/users.js +9 -0
- package/lib/utils.js +4 -2
- package/lib/vendor-auth.js +22 -0
- package/lib/vendor-registry-message.js +65 -0
- package/lib/ws-origin.js +99 -0
- package/lib/ws-schema.js +33 -22
- package/lib/yoke/adapters/claude-capabilities.js +38 -0
- package/lib/yoke/adapters/claude-vendor.js +35 -0
- package/lib/yoke/adapters/claude-worker.js +55 -87
- package/lib/yoke/adapters/claude.js +156 -62
- package/lib/yoke/adapters/codex-capabilities.js +37 -0
- package/lib/yoke/adapters/codex-vendor.js +46 -0
- package/lib/yoke/adapters/codex.js +347 -156
- package/lib/yoke/adapters/effort-control.js +34 -0
- package/lib/yoke/adapters/permission-relay.js +58 -0
- package/lib/yoke/adapters/worker-mcp-servers.js +81 -0
- package/lib/yoke/auth-checks.js +104 -0
- package/lib/yoke/capabilities.js +49 -0
- package/lib/yoke/codex-app-server.js +23 -5
- package/lib/yoke/env-ownership.js +40 -0
- package/lib/yoke/index.js +67 -173
- package/lib/yoke/install-probe.js +121 -0
- package/lib/yoke/instructions.js +7 -11
- package/lib/yoke/interface.js +199 -3
- package/lib/yoke/package.json +1 -1
- package/lib/yoke/provider-credentials.js +96 -0
- package/lib/yoke/registry.js +340 -0
- package/package.json +12 -8
- package/scripts/check-sdk-lock-drift.js +160 -0
- package/scripts/check-test-count.js +76 -1
- package/scripts/postinstall.js +218 -136
- package/scripts/prompt-harness-history.js +202 -0
- package/scripts/verify-installed.js +67 -0
- package/lib/sdk-permission-ownership.js +0 -136
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// AskUserQuestion card: one or more multiple-choice questions with an
|
|
2
|
+
// "Other" field, answered with Submit or skipped. The prompt's id is the
|
|
3
|
+
// tool-use id. The shell (prompts.js, prompt-card.js) owns its state; this
|
|
4
|
+
// module draws the questions and, once answered, the answer summary.
|
|
5
|
+
//
|
|
6
|
+
// What an answer is - chosen labels or "Other" text, how a multi-select
|
|
7
|
+
// answer is listed, how options sharing a label count as one choice, how a
|
|
8
|
+
// recorded answer reads back - is the shared codec's (./ask-user-codec.js,
|
|
9
|
+
// which the server holds answers to). A multi-select answer is sent and
|
|
10
|
+
// recorded as a list, so a replayed card shows exactly what was chosen; the
|
|
11
|
+
// server joins it for the CLI.
|
|
12
|
+
|
|
13
|
+
import { MAX_ANSWER_CHARS, answerFromCard, answerText, readAnswer } from './ask-user-codec.js';
|
|
14
|
+
|
|
15
|
+
function isLocked(container) {
|
|
16
|
+
return container.classList.contains("resolved") || container.classList.contains("sending");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function showAnswerSummary(container, questions, answers) {
|
|
20
|
+
if (!answers || Object.keys(answers).length === 0) return;
|
|
21
|
+
if (container.querySelector(".ask-user-answer-summary")) return;
|
|
22
|
+
var summary = document.createElement("div");
|
|
23
|
+
summary.className = "ask-user-answer-summary";
|
|
24
|
+
Object.keys(answers).forEach(function (key) {
|
|
25
|
+
var qi = parseInt(key, 10);
|
|
26
|
+
var row = document.createElement("div");
|
|
27
|
+
row.className = "ask-user-answer-row";
|
|
28
|
+
var labelEl = document.createElement("span");
|
|
29
|
+
labelEl.className = "ask-user-answer-label";
|
|
30
|
+
labelEl.textContent = (questions[qi] && questions[qi].question) ? questions[qi].question : "Answer";
|
|
31
|
+
var valueEl = document.createElement("span");
|
|
32
|
+
valueEl.className = "ask-user-answer-value";
|
|
33
|
+
valueEl.textContent = answerText(answers[key]);
|
|
34
|
+
row.appendChild(labelEl);
|
|
35
|
+
row.appendChild(valueEl);
|
|
36
|
+
summary.appendChild(row);
|
|
37
|
+
});
|
|
38
|
+
container.appendChild(summary);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Every option whose label is chosen shows as selected, and no other: the
|
|
42
|
+
// live card and a replayed one draw selection the same way.
|
|
43
|
+
function showChosen(qEl, labels) {
|
|
44
|
+
var options = qEl.querySelectorAll(".ask-user-option");
|
|
45
|
+
for (var i = 0; i < options.length; i++) {
|
|
46
|
+
var labelEl = options[i].querySelector(".option-label");
|
|
47
|
+
if (labelEl && labels.indexOf(labelEl.textContent) !== -1) options[i].classList.add("selected");
|
|
48
|
+
else options[i].classList.remove("selected");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Reflect recorded answers in the options, so a replayed card reads the
|
|
53
|
+
// same as the one the operator answered: exactly the chosen options are
|
|
54
|
+
// selected, and only an "Other" answer fills the text field.
|
|
55
|
+
function markSelections(container, questions, answers) {
|
|
56
|
+
var questionEls = container.querySelectorAll(".ask-user-question");
|
|
57
|
+
Object.keys(answers).forEach(function (key) {
|
|
58
|
+
var qIdx = parseInt(key, 10);
|
|
59
|
+
var qEl = questionEls[qIdx];
|
|
60
|
+
if (!qEl) return;
|
|
61
|
+
var read = readAnswer(questions[qIdx], answers[key]);
|
|
62
|
+
showChosen(qEl, read.labels);
|
|
63
|
+
var otherInput = qEl.querySelector(".ask-user-other input");
|
|
64
|
+
if (otherInput) otherInput.value = read.other;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function drawQuestion(container, q, qIdx, state, submit) {
|
|
69
|
+
var qDiv = document.createElement("div");
|
|
70
|
+
qDiv.className = "ask-user-question";
|
|
71
|
+
if (q.header) {
|
|
72
|
+
var qHeader = document.createElement("div");
|
|
73
|
+
qHeader.className = "ask-user-question-header";
|
|
74
|
+
qHeader.textContent = q.header;
|
|
75
|
+
qDiv.appendChild(qHeader);
|
|
76
|
+
}
|
|
77
|
+
var qText = document.createElement("div");
|
|
78
|
+
qText.className = "ask-user-question-text";
|
|
79
|
+
qText.textContent = q.question || "";
|
|
80
|
+
qDiv.appendChild(qText);
|
|
81
|
+
|
|
82
|
+
var optionsDiv = document.createElement("div");
|
|
83
|
+
optionsDiv.className = "ask-user-options";
|
|
84
|
+
var isMulti = q.multiSelect || false;
|
|
85
|
+
state.chosen[qIdx] = [];
|
|
86
|
+
state.other[qIdx] = "";
|
|
87
|
+
|
|
88
|
+
(q.options || []).forEach(function (opt) {
|
|
89
|
+
var btn = document.createElement("button");
|
|
90
|
+
btn.className = "ask-user-option";
|
|
91
|
+
var labelEl = document.createElement("div");
|
|
92
|
+
labelEl.className = "option-label";
|
|
93
|
+
labelEl.textContent = opt.label;
|
|
94
|
+
btn.appendChild(labelEl);
|
|
95
|
+
if (opt.description) {
|
|
96
|
+
var descEl = document.createElement("div");
|
|
97
|
+
descEl.className = "option-desc";
|
|
98
|
+
descEl.textContent = opt.description;
|
|
99
|
+
btn.appendChild(descEl);
|
|
100
|
+
}
|
|
101
|
+
if (opt.markdown) {
|
|
102
|
+
var pre = document.createElement("pre");
|
|
103
|
+
pre.className = "option-markdown";
|
|
104
|
+
pre.textContent = opt.markdown;
|
|
105
|
+
btn.appendChild(pre);
|
|
106
|
+
}
|
|
107
|
+
btn.addEventListener("click", function () {
|
|
108
|
+
if (isLocked(container)) return;
|
|
109
|
+
// Choosing an option replaces any "Other" answer.
|
|
110
|
+
var other = qDiv.querySelector(".ask-user-other input");
|
|
111
|
+
if (other) other.value = "";
|
|
112
|
+
state.other[qIdx] = "";
|
|
113
|
+
var chosen = state.chosen[qIdx];
|
|
114
|
+
if (!isMulti) state.chosen[qIdx] = [opt.label];
|
|
115
|
+
else if (chosen.indexOf(opt.label) !== -1) state.chosen[qIdx] = chosen.filter(function (l) { return l !== opt.label; });
|
|
116
|
+
else state.chosen[qIdx] = chosen.concat([opt.label]);
|
|
117
|
+
showChosen(qDiv, state.chosen[qIdx]);
|
|
118
|
+
});
|
|
119
|
+
optionsDiv.appendChild(btn);
|
|
120
|
+
});
|
|
121
|
+
qDiv.appendChild(optionsDiv);
|
|
122
|
+
|
|
123
|
+
var otherDiv = document.createElement("div");
|
|
124
|
+
otherDiv.className = "ask-user-other";
|
|
125
|
+
var otherInput = document.createElement("input");
|
|
126
|
+
otherInput.type = "text";
|
|
127
|
+
otherInput.placeholder = "Other...";
|
|
128
|
+
otherInput.maxLength = MAX_ANSWER_CHARS;
|
|
129
|
+
otherInput.addEventListener("input", function () {
|
|
130
|
+
if (isLocked(container)) return;
|
|
131
|
+
// An emptied field is no answer: Submit must not send what was typed
|
|
132
|
+
// before it was cleared.
|
|
133
|
+
state.other[qIdx] = otherInput.value;
|
|
134
|
+
if (!otherInput.value.trim()) return;
|
|
135
|
+
state.chosen[qIdx] = [];
|
|
136
|
+
showChosen(qDiv, []);
|
|
137
|
+
});
|
|
138
|
+
otherInput.addEventListener("keydown", function (e) {
|
|
139
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
140
|
+
e.preventDefault();
|
|
141
|
+
submit();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
otherDiv.appendChild(otherInput);
|
|
145
|
+
qDiv.appendChild(otherDiv);
|
|
146
|
+
container.appendChild(qDiv);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export default {
|
|
150
|
+
kind: "ask_user",
|
|
151
|
+
|
|
152
|
+
draw: function (fields, requestId, host) {
|
|
153
|
+
var questions = (fields.input && fields.input.questions) || [];
|
|
154
|
+
var container = document.createElement("div");
|
|
155
|
+
container.className = "ask-user-container";
|
|
156
|
+
container.dataset.requestId = requestId;
|
|
157
|
+
container.dataset.toolId = requestId;
|
|
158
|
+
|
|
159
|
+
// Per question index: the chosen labels and the "Other" text.
|
|
160
|
+
var state = { chosen: {}, other: {} };
|
|
161
|
+
function submit() {
|
|
162
|
+
if (isLocked(container)) return;
|
|
163
|
+
var result = {};
|
|
164
|
+
questions.forEach(function (q, qIdx) {
|
|
165
|
+
var answer = answerFromCard(q, state.chosen[qIdx] || [], state.other[qIdx]);
|
|
166
|
+
if (answer !== null) result[qIdx] = answer;
|
|
167
|
+
});
|
|
168
|
+
if (Object.keys(result).length === 0) return;
|
|
169
|
+
host.respond(container, { answers: result });
|
|
170
|
+
}
|
|
171
|
+
questions.forEach(function (q, qIdx) { drawQuestion(container, q, qIdx, state, submit); });
|
|
172
|
+
|
|
173
|
+
var submitBtn = document.createElement("button");
|
|
174
|
+
submitBtn.className = "ask-user-submit";
|
|
175
|
+
submitBtn.textContent = "Submit";
|
|
176
|
+
submitBtn.addEventListener("click", submit);
|
|
177
|
+
container.appendChild(submitBtn);
|
|
178
|
+
|
|
179
|
+
var skipBtn = document.createElement("button");
|
|
180
|
+
skipBtn.className = "ask-user-skip";
|
|
181
|
+
skipBtn.textContent = "Skip";
|
|
182
|
+
skipBtn.addEventListener("click", function () {
|
|
183
|
+
if (!isLocked(container)) host.respond(container, { decision: "skip" });
|
|
184
|
+
});
|
|
185
|
+
container.appendChild(skipBtn);
|
|
186
|
+
|
|
187
|
+
host.setMainInputDisabled(true);
|
|
188
|
+
return container;
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
outcome: function (state) {
|
|
192
|
+
if (state.skipped) return { text: "Skipped", tone: "denied" };
|
|
193
|
+
return { text: "Answered", tone: "allowed" };
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
// An answered card shows the answers; any other ending shows its label.
|
|
197
|
+
settle: function (container, state, fields, outcome, host) {
|
|
198
|
+
container.classList.add("answered");
|
|
199
|
+
host.setMainInputDisabled(false);
|
|
200
|
+
var questions = (fields.input && fields.input.questions) || [];
|
|
201
|
+
if (state.state === "resolved" && state.answers && Object.keys(state.answers).length) {
|
|
202
|
+
showAnswerSummary(container, questions, state.answers);
|
|
203
|
+
markSelections(container, questions, state.answers);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (!container.querySelector(".ask-user-status")) {
|
|
207
|
+
var status = document.createElement("div");
|
|
208
|
+
status.className = "ask-user-status permission-decision-label";
|
|
209
|
+
status.textContent = outcome.text;
|
|
210
|
+
container.appendChild(status);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
};
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
// elicitation-codec.js - the one definition of an MCP elicitation answer,
|
|
2
|
+
// shared by the card (this directory) and the server
|
|
3
|
+
// (lib/prompt-kinds/elicitation.js): which requested schemas a form is drawn
|
|
4
|
+
// for, which fields such a schema asks for, what value each may take, how a
|
|
5
|
+
// form control's text reads as that value, and when a URL request may be
|
|
6
|
+
// approved.
|
|
7
|
+
//
|
|
8
|
+
// This file is the only copy: the server imports it as is
|
|
9
|
+
// (lib/prompt-kinds/codecs.js).
|
|
10
|
+
|
|
11
|
+
// Bound on typed text (lib/prompt-kinds/answer-limits.js applies the same).
|
|
12
|
+
var MAX_ANSWER_CHARS = 10000;
|
|
13
|
+
// Only a web page may be opened from a prompt.
|
|
14
|
+
var OPENABLE_PROTOCOLS = ["http:", "https:"];
|
|
15
|
+
|
|
16
|
+
// The requested-schema subset MCP elicitation defines, and the only one a
|
|
17
|
+
// form is drawn for: a flat object whose every property is a string
|
|
18
|
+
// (minLength, maxLength, format), a number or integer (minimum, maximum), a
|
|
19
|
+
// boolean, or a string enum (enum, enumNames), each with an optional title,
|
|
20
|
+
// description and default. Every keyword of the subset is enforced. A schema
|
|
21
|
+
// that uses anything else is refused whole, never drawn in part: a form
|
|
22
|
+
// that checks only some of what was asked would pass answers on as valid
|
|
23
|
+
// that the server never accepted.
|
|
24
|
+
var SCHEMA_KEYWORDS = ["$schema", "type", "title", "description", "properties", "required", "additionalProperties"];
|
|
25
|
+
var ANNOTATIONS = ["type", "title", "description", "default"];
|
|
26
|
+
var PROPERTY_KEYWORDS = {
|
|
27
|
+
string: ANNOTATIONS.concat(["minLength", "maxLength", "format"]),
|
|
28
|
+
enum: ANNOTATIONS.concat(["enum", "enumNames"]),
|
|
29
|
+
number: ANNOTATIONS.concat(["minimum", "maximum"]),
|
|
30
|
+
integer: ANNOTATIONS.concat(["minimum", "maximum"]),
|
|
31
|
+
boolean: ANNOTATIONS,
|
|
32
|
+
};
|
|
33
|
+
var FORMATS = {
|
|
34
|
+
email: { test: isEmail, error: "is not an email address" },
|
|
35
|
+
uri: { test: isAbsoluteUri, error: "is not an absolute URI" },
|
|
36
|
+
date: { test: isDate, error: "is not a date (YYYY-MM-DD)" },
|
|
37
|
+
"date-time": { test: isDateTime, error: "is not a date and time (YYYY-MM-DDThh:mm:ssZ)" },
|
|
38
|
+
};
|
|
39
|
+
var DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
40
|
+
var DATE_TIME = /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(\.\d+)?(?:[Zz]|[+-](\d{2}):(\d{2}))$/;
|
|
41
|
+
|
|
42
|
+
function isPlainObject(v) {
|
|
43
|
+
return !!v && typeof v === "object" && !Array.isArray(v);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isString(v) {
|
|
47
|
+
return typeof v === "string";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Whether table holds name itself (a name from a schema may be anything).
|
|
51
|
+
function holds(table, name) {
|
|
52
|
+
return typeof name === "string" && Object.prototype.hasOwnProperty.call(table, name);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Defined, not assigned, so a field named "__proto__" is a key.
|
|
56
|
+
function put(obj, key, value) {
|
|
57
|
+
Object.defineProperty(obj, key, { value: value, enumerable: true, writable: true, configurable: true });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// A string's length as JSON Schema counts it: in characters, not UTF-16
|
|
61
|
+
// code units.
|
|
62
|
+
function textLength(s) {
|
|
63
|
+
return Array.from(s).length;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// The address pattern the WHATWG HTML standard gives for input type=email.
|
|
67
|
+
var EMAIL = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
68
|
+
|
|
69
|
+
function isEmail(v) {
|
|
70
|
+
return EMAIL.test(v);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isAbsoluteUri(v) {
|
|
74
|
+
if (!/^[A-Za-z][A-Za-z0-9+.-]*:\S+$/.test(v)) return false;
|
|
75
|
+
try {
|
|
76
|
+
new URL(v);
|
|
77
|
+
return true;
|
|
78
|
+
} catch (e) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isCalendarDate(y, m, d) {
|
|
84
|
+
var leap = y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0);
|
|
85
|
+
var days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
86
|
+
return m >= 1 && m <= 12 && d >= 1 && d <= days[m - 1];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function isDate(v) {
|
|
90
|
+
var m = DATE.exec(v);
|
|
91
|
+
return !!m && isCalendarDate(Number(m[1]), Number(m[2]), Number(m[3]));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isDateTime(v) {
|
|
95
|
+
var m = DATE_TIME.exec(v);
|
|
96
|
+
if (!m || !isCalendarDate(Number(m[1]), Number(m[2]), Number(m[3]))) return false;
|
|
97
|
+
if (Number(m[4]) > 23 || Number(m[5]) > 59 || Number(m[6]) > 60) return false;
|
|
98
|
+
return m[8] === undefined || (Number(m[8]) <= 23 && Number(m[9]) <= 59);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// "url" when the server asks the operator to open a URL, else "form". A URL
|
|
102
|
+
// request is never treated as a form, whether or not its URL is usable.
|
|
103
|
+
function requestMode(request) {
|
|
104
|
+
return request && request.mode === "url" ? "url" : "form";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Whether url is an http(s) URL, the only kind a prompt may open.
|
|
108
|
+
function isOpenableUrl(url) {
|
|
109
|
+
if (typeof url !== "string" || !url) return false;
|
|
110
|
+
try {
|
|
111
|
+
return OPENABLE_PROTOCOLS.indexOf(new URL(url).protocol) !== -1;
|
|
112
|
+
} catch (e) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function firstUnknown(obj, known) {
|
|
118
|
+
var keys = Object.keys(obj);
|
|
119
|
+
for (var i = 0; i < keys.length; i++) {
|
|
120
|
+
if (known.indexOf(keys[i]) === -1) return keys[i];
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function isLength(v) {
|
|
126
|
+
return v === undefined || (Number.isInteger(v) && v >= 0);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function isBound(v) {
|
|
130
|
+
return v === undefined || (typeof v === "number" && Number.isFinite(v));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Which subset property prop is: "string", "enum", "number", "integer" or
|
|
134
|
+
// "boolean"; null when it is none of them.
|
|
135
|
+
function subsetShape(prop) {
|
|
136
|
+
if (!isPlainObject(prop)) return null;
|
|
137
|
+
if (prop.enum !== undefined) return prop.type === "string" ? "enum" : null;
|
|
138
|
+
return prop.type !== "enum" && holds(PROPERTY_KEYWORDS, prop.type) ? prop.type : null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function propertyProblem(name, prop) {
|
|
142
|
+
var shape = subsetShape(prop);
|
|
143
|
+
if (!shape) return name + ": only a string, number, integer, boolean or string-enum field is supported";
|
|
144
|
+
var unknown = firstUnknown(prop, PROPERTY_KEYWORDS[shape]);
|
|
145
|
+
if (unknown !== null) return name + ": \"" + unknown + "\" is not supported";
|
|
146
|
+
if (shape === "string") {
|
|
147
|
+
if (!isLength(prop.minLength) || !isLength(prop.maxLength)) return name + ": minLength and maxLength must be whole numbers of 0 or more";
|
|
148
|
+
if (prop.minLength > prop.maxLength) return name + ": minLength is above maxLength";
|
|
149
|
+
if (prop.format !== undefined && !holds(FORMATS, prop.format)) return name + ": format must be email, uri, date or date-time";
|
|
150
|
+
} else if (shape === "enum") {
|
|
151
|
+
if (!Array.isArray(prop.enum) || !prop.enum.length || !prop.enum.every(isString)) return name + ": enum must list one or more strings";
|
|
152
|
+
// An empty control reads as no answer, so "" could never be chosen.
|
|
153
|
+
if (prop.enum.indexOf("") !== -1) return name + ": enum must not list an empty string";
|
|
154
|
+
var names = prop.enumNames;
|
|
155
|
+
if (names !== undefined && !(Array.isArray(names) && names.length === prop.enum.length && names.every(isString))) {
|
|
156
|
+
return name + ": enumNames must name every enum value with a string";
|
|
157
|
+
}
|
|
158
|
+
} else if (shape === "number" || shape === "integer") {
|
|
159
|
+
if (!isBound(prop.minimum) || !isBound(prop.maximum)) return name + ": minimum and maximum must be numbers";
|
|
160
|
+
if (prop.minimum > prop.maximum) return name + ": minimum is above maximum";
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Why a form cannot be drawn for schema, or null when it can and every
|
|
166
|
+
// answer to it is checked in full. No schema asks for no fields.
|
|
167
|
+
function schemaProblem(schema) {
|
|
168
|
+
if (schema === null || schema === undefined) return null;
|
|
169
|
+
if (!isPlainObject(schema) || schema.type !== "object") return "the form is not an object schema";
|
|
170
|
+
var unknown = firstUnknown(schema, SCHEMA_KEYWORDS);
|
|
171
|
+
if (unknown !== null) return "\"" + unknown + "\" is not supported";
|
|
172
|
+
if (schema.additionalProperties !== undefined && typeof schema.additionalProperties !== "boolean") {
|
|
173
|
+
return "additionalProperties must be true or false";
|
|
174
|
+
}
|
|
175
|
+
if (schema.required !== undefined && !(Array.isArray(schema.required) && schema.required.every(isString))) {
|
|
176
|
+
return "required must list field names";
|
|
177
|
+
}
|
|
178
|
+
var required = schema.required || [];
|
|
179
|
+
if (schema.properties === undefined) return required.length ? "required names " + required[0] + ", which is not a property" : null;
|
|
180
|
+
if (!isPlainObject(schema.properties)) return "properties must be an object";
|
|
181
|
+
for (var r = 0; r < required.length; r++) {
|
|
182
|
+
if (!holds(schema.properties, required[r])) return "required names " + required[r] + ", which is not a property";
|
|
183
|
+
}
|
|
184
|
+
var names = Object.keys(schema.properties);
|
|
185
|
+
for (var i = 0; i < names.length; i++) {
|
|
186
|
+
var problem = propertyProblem(names[i], schema.properties[names[i]]);
|
|
187
|
+
if (problem) return problem;
|
|
188
|
+
// An empty control reads as no answer, so "" could never be given.
|
|
189
|
+
if (required.indexOf(names[i]) !== -1 && schema.properties[names[i]].maxLength === 0 && subsetShape(schema.properties[names[i]]) === "string") {
|
|
190
|
+
return names[i] + ": a required string cannot have a maxLength of 0";
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// The longest text a field takes: the schema's own maxLength when tighter.
|
|
197
|
+
function lengthLimit(prop) {
|
|
198
|
+
var max = prop.maxLength;
|
|
199
|
+
return (Number.isInteger(max) && max >= 0 && max < MAX_ANSWER_CHARS) ? max : MAX_ANSWER_CHARS;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// What a property's value is: one of its enum values, or a boolean,
|
|
203
|
+
// integer, number or (anything else) string.
|
|
204
|
+
function fieldType(prop) {
|
|
205
|
+
if (Array.isArray(prop.enum)) return "enum";
|
|
206
|
+
if (prop.type === "boolean" || prop.type === "integer" || prop.type === "number") return prop.type;
|
|
207
|
+
return "string";
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// The fields a requested schema asks for, in its order: name, property,
|
|
211
|
+
// type, whether required, and the values a choice field offers (null for a
|
|
212
|
+
// typed-in field). A boolean is a choice between true and false.
|
|
213
|
+
function schemaFields(schema) {
|
|
214
|
+
var props = schema && isPlainObject(schema.properties) ? schema.properties : {};
|
|
215
|
+
var required = schema && Array.isArray(schema.required) ? schema.required : [];
|
|
216
|
+
return Object.keys(props).map(function (name) {
|
|
217
|
+
var prop = isPlainObject(props[name]) ? props[name] : {};
|
|
218
|
+
var type = fieldType(prop);
|
|
219
|
+
var choices = type === "enum" ? prop.enum.slice() : type === "boolean" ? [true, false] : null;
|
|
220
|
+
return { name: name, prop: prop, type: type, required: required.indexOf(name) !== -1, choices: choices };
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// No answer to a field: nothing, or nothing typed.
|
|
225
|
+
function isAbsent(value) {
|
|
226
|
+
return value === undefined || value === null || value === "";
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// What keeps value, already of field's type, from answering field: the
|
|
230
|
+
// error to show, or null when nothing does.
|
|
231
|
+
function constraintError(field, value) {
|
|
232
|
+
var prop = field.prop;
|
|
233
|
+
if (field.type === "integer" || field.type === "number") {
|
|
234
|
+
if (typeof prop.minimum === "number" && value < prop.minimum) return "must be at least " + prop.minimum;
|
|
235
|
+
if (typeof prop.maximum === "number" && value > prop.maximum) return "must be at most " + prop.maximum;
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
if (field.type !== "string") return null;
|
|
239
|
+
if (value.length > MAX_ANSWER_CHARS || (Number.isInteger(prop.maxLength) && textLength(value) > prop.maxLength)) {
|
|
240
|
+
return "is too long";
|
|
241
|
+
}
|
|
242
|
+
if (Number.isInteger(prop.minLength) && textLength(value) < prop.minLength) return "is too short";
|
|
243
|
+
if (holds(FORMATS, prop.format) && !FORMATS[prop.format].test(value)) return FORMATS[prop.format].error;
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Whether value is an answer to field: a value of its type that meets
|
|
248
|
+
// every constraint the field names.
|
|
249
|
+
function conforms(field, value) {
|
|
250
|
+
if (isAbsent(value)) return false;
|
|
251
|
+
if (field.choices) return field.choices.indexOf(value) !== -1;
|
|
252
|
+
if (field.type === "integer") {
|
|
253
|
+
if (!Number.isInteger(value)) return false;
|
|
254
|
+
} else if (field.type === "number") {
|
|
255
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return false;
|
|
256
|
+
} else if (typeof value !== "string") {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
return constraintError(field, value) === null;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// The text a field's control shows for value; "" for none.
|
|
263
|
+
function controlText(field, value) {
|
|
264
|
+
return conforms(field, value) ? String(value) : "";
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// The text a field's control starts with: the schema's default when it is
|
|
268
|
+
// a value of the field's type, else none. Nothing is preselected that the
|
|
269
|
+
// operator would not see and could not change.
|
|
270
|
+
function initialControlText(field) {
|
|
271
|
+
return controlText(field, field.prop.default);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// How a choice reads on its control: an enum value by its enumNames entry
|
|
275
|
+
// when the schema gives one.
|
|
276
|
+
function choiceLabel(field, choice) {
|
|
277
|
+
if (field.type === "boolean") return choice ? "Yes" : "No";
|
|
278
|
+
var names = field.prop.enumNames;
|
|
279
|
+
var at = field.choices ? field.choices.indexOf(choice) : -1;
|
|
280
|
+
if (Array.isArray(names) && at !== -1 && typeof names[at] === "string") return names[at];
|
|
281
|
+
return String(choice);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// A control's text read as its field's value: {absent: true} when nothing
|
|
285
|
+
// was given, {error} when the text is not a value of the type or misses a
|
|
286
|
+
// constraint, else {value}.
|
|
287
|
+
function readControl(field, text) {
|
|
288
|
+
var raw = text == null ? "" : String(text);
|
|
289
|
+
if (field.choices) {
|
|
290
|
+
if (raw === "") return { absent: true };
|
|
291
|
+
for (var i = 0; i < field.choices.length; i++) {
|
|
292
|
+
if (String(field.choices[i]) === raw) return { value: field.choices[i] };
|
|
293
|
+
}
|
|
294
|
+
return { error: "is not one of the choices" };
|
|
295
|
+
}
|
|
296
|
+
var value = raw;
|
|
297
|
+
if (field.type === "integer" || field.type === "number") {
|
|
298
|
+
if (raw.trim() === "") return { absent: true };
|
|
299
|
+
value = Number(raw);
|
|
300
|
+
if (!Number.isFinite(value)) return { error: "must be a number" };
|
|
301
|
+
if (field.type === "integer" && !Number.isInteger(value)) return { error: "must be a whole number" };
|
|
302
|
+
} else if (raw === "") {
|
|
303
|
+
return { absent: true };
|
|
304
|
+
}
|
|
305
|
+
var error = constraintError(field, value);
|
|
306
|
+
return error ? { error: error } : { value: value };
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// The answer a form's controls stand for, and what keeps it from being
|
|
310
|
+
// sent. textOf(name) is the text of that field's control. A field left
|
|
311
|
+
// empty is absent from the content, never "", 0 or false.
|
|
312
|
+
function contentFromControls(schema, textOf) {
|
|
313
|
+
var content = {};
|
|
314
|
+
var errors = [];
|
|
315
|
+
schemaFields(schema).forEach(function (field) {
|
|
316
|
+
var got = readControl(field, textOf(field.name));
|
|
317
|
+
if (got.error) errors.push(field.name + " " + got.error);
|
|
318
|
+
else if (got.absent) { if (field.required) errors.push(field.name + " is required"); }
|
|
319
|
+
else put(content, field.name, got.value);
|
|
320
|
+
});
|
|
321
|
+
return { content: content, errors: errors };
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Submitted content held to the requested schema: only its fields, each
|
|
325
|
+
// only with a value that answers it. A value that does not is dropped,
|
|
326
|
+
// never coerced; a required field left without a value makes the answer
|
|
327
|
+
// unusable (errors), so an incomplete form is never passed on as accepted.
|
|
328
|
+
function contentForSchema(schema, submitted) {
|
|
329
|
+
var given = isPlainObject(submitted) ? submitted : {};
|
|
330
|
+
var content = {};
|
|
331
|
+
var errors = [];
|
|
332
|
+
schemaFields(schema).forEach(function (field) {
|
|
333
|
+
var value = Object.prototype.hasOwnProperty.call(given, field.name) ? given[field.name] : undefined;
|
|
334
|
+
if (conforms(field, value)) put(content, field.name, value);
|
|
335
|
+
else if (field.required) errors.push(field.name + " is required");
|
|
336
|
+
});
|
|
337
|
+
return { content: content, errors: errors };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export {
|
|
341
|
+
MAX_ANSWER_CHARS, requestMode, isOpenableUrl, schemaProblem, lengthLimit, schemaFields, conforms, controlText,
|
|
342
|
+
initialControlText, choiceLabel, readControl, contentFromControls, contentForSchema,
|
|
343
|
+
};
|