@composable-os/pack-forms 0.0.0-stage → 0.1.0-beta.12

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/dist/tools.js ADDED
@@ -0,0 +1,288 @@
1
+ // The Forms tools. They become operations x.forms.<tool>, run in the sandbox, and reach the host
2
+ // only through ctx.ops. None of them holds a rule: whether a form takes answers from the caller,
3
+ // the spam checks, the caps, the answers' shape, the submitter's Person and who may tick a
4
+ // "keep me updated" line are all the door's (`intake.open` and `intake.submit`, ADR 0720); the
5
+ // inbox's moves are the submission's states. A tool asks, and reports what the host answered.
6
+ import { defineTool, OpsCallError, z } from "@composable-os/sdk";
7
+ export const FORM = "forms.form";
8
+ export const SUBMISSION = "forms.submission";
9
+ const text = (value) => (typeof value === "string" && value ? value : null);
10
+ const formId = z.string().min(1).max(64).describe("The form (forms.form record id)");
11
+ const submissionId = z.string().min(1).max(64).describe("The submission (forms.submission id)");
12
+ const answers = z
13
+ .record(z.string().max(40), z.unknown())
14
+ .describe("The answers by question key: text, email, phone and date as strings (a date YYYY-MM-DD), " +
15
+ "number and rating as numbers, a choice as a string or a list of strings, yes/no and a " +
16
+ "tick box as true or false. Leave out what was not answered.");
17
+ const received = z.object({
18
+ received: z
19
+ .literal(true)
20
+ .describe("The answers were sent: shown the same whatever the host's spam checks decided"),
21
+ });
22
+ // --- Answering (the public and customers) --------------------------------------------------------
23
+ const openOutput = z.object({
24
+ id: z.string(),
25
+ version: z.number().describe("The version shown: send it with the answers"),
26
+ title: z.string().nullable(),
27
+ questions: z
28
+ .object({ items: z.array(z.record(z.string(), z.unknown())) })
29
+ .describe("The questions, in the platform's question schema, in order"),
30
+ thanks: z.string().nullable().describe("The words to show once the answers are sent"),
31
+ anonymous: z.boolean().describe("Nothing kept says who answered"),
32
+ turnstile: z.boolean().describe("A Turnstile token must be sent with the answers"),
33
+ shown: z.string().nullable().describe("Send back as `shown` with the answers"),
34
+ });
35
+ export const form = defineTool({
36
+ description: "A form by its address, as the caller may answer it now: its version, title, questions and " +
37
+ "thank-you words, whether it is anonymous, whether a Turnstile token is needed, and the " +
38
+ "host's token of when it was shown (send it back with the answers). A form that is not " +
39
+ "open to the caller now (a draft, closed, not published, out of its dates, for signed-in " +
40
+ "customers only, or no such address) is not found, the same answer whatever the reason. " +
41
+ "Or open the form a personal link is for: `link`, the token from its email (no sign-in " +
42
+ "needed; a signed-in member's own link from Needs your attention is `linkedForm`). A link " +
43
+ "that cannot be used (used, expired, unknown) is one answer whatever is wrong, and opening " +
44
+ "it uses nothing. Read-only.",
45
+ input: z.object({
46
+ slug: z
47
+ .string()
48
+ .trim()
49
+ .min(1)
50
+ .max(200)
51
+ .optional()
52
+ .describe("The form's address, e.g. ask-about-a-trial"),
53
+ link: z.string().min(1).max(200).optional().describe("A personal link's token"),
54
+ }),
55
+ output: openOutput,
56
+ risk: "read",
57
+ audiences: ["customer", "public"],
58
+ // A personal link's token never reaches the audit log.
59
+ redact: ["link"],
60
+ async handler(input, ctx) {
61
+ return drawn(await ctx.ops.call("intake.open", {
62
+ object: FORM,
63
+ ...(input.slug !== undefined ? { slug: input.slug } : {}),
64
+ ...(input.link !== undefined ? { link: input.link } : {}),
65
+ }));
66
+ },
67
+ });
68
+ /** What `form` and `linkedForm` give a client to draw. */
69
+ function drawn(open) {
70
+ const questions = open.content.questions;
71
+ return {
72
+ id: open.id,
73
+ version: open.version,
74
+ title: text(open.content.title),
75
+ questions: { items: Array.isArray(questions?.items) ? questions.items : [] },
76
+ thanks: text(open.content.thanks),
77
+ anonymous: open.anonymous,
78
+ turnstile: open.turnstile,
79
+ shown: open.shown,
80
+ };
81
+ }
82
+ export const linkedForm = defineTool({
83
+ description: "The form a signed-in member's own personal link is for (or a child's they are a guardian " +
84
+ "of): `linkId` is the id Needs your attention gives (me.attention, kind form_to_fill). As " +
85
+ "`form` gives it; a link that cannot be used is one answer. Read-only.",
86
+ input: z.object({
87
+ linkId: z.string().min(1).max(64).describe("The personal link's id, from Needs your attention"),
88
+ }),
89
+ output: openOutput,
90
+ risk: "read",
91
+ audiences: ["customer"],
92
+ async handler(input, ctx) {
93
+ return drawn(await ctx.ops.call("intake.open", { object: FORM, linkId: input.linkId }));
94
+ },
95
+ });
96
+ export const submit = defineTool({
97
+ description: "Send answers to a form the caller may answer (from `form`). The host checks the form is " +
98
+ "still open to them, a visitor's Turnstile token and when the form was shown, the caps and " +
99
+ "the answers against the version shown, then keeps them in the business's inbox, linked to " +
100
+ "the sender's person by the email answered. Answers that break the form are refused naming " +
101
+ "the question's key; a form published again since it was shown is refused with 'load it " +
102
+ "again'; too many answers just now is 'try again later'. Kept, kept as spam or dropped, the " +
103
+ "answer is the same. By a personal link (`link`, as `form` opened it; signed in, " +
104
+ "`submitLinked`), the answers are the link's person's, once: no Turnstile token or shown " +
105
+ "time is needed, and a link that cannot be used is one answer.",
106
+ input: z.object({
107
+ id: formId,
108
+ version: z.number().int().positive().describe("The version `form` showed"),
109
+ answers,
110
+ via: z
111
+ .enum(["site", "app", "page"])
112
+ .optional()
113
+ .describe("Which way in: the business's website, its app, or the form's own page"),
114
+ turnstileToken: z.string().max(4096).optional().describe("A visitor's Turnstile token"),
115
+ shown: z.string().max(200).optional().describe("`form`'s shown token"),
116
+ honeypot: z.string().max(1000).optional().describe("The hidden field a person leaves empty"),
117
+ link: z.string().min(1).max(200).optional().describe("A personal link's token"),
118
+ }),
119
+ output: received,
120
+ risk: "write",
121
+ audiences: ["customer", "public"],
122
+ // What a person answered never reaches the audit row (the door keeps only the question keys),
123
+ // nor does a personal link's token.
124
+ redact: ["answers", "turnstileToken", "shown", "honeypot", "link"],
125
+ async handler(input, ctx) {
126
+ return ctx.ops.call("intake.submit", {
127
+ object: FORM,
128
+ id: input.id,
129
+ version: input.version,
130
+ answers: input.answers,
131
+ ...(input.via ? { via: input.via } : {}),
132
+ ...(input.turnstileToken ? { turnstileToken: input.turnstileToken } : {}),
133
+ ...(input.shown ? { shown: input.shown } : {}),
134
+ ...(input.honeypot !== undefined ? { honeypot: input.honeypot } : {}),
135
+ ...(input.link !== undefined ? { link: input.link } : {}),
136
+ });
137
+ },
138
+ });
139
+ export const submitLinked = defineTool({
140
+ description: "Send answers by a signed-in member's own personal link (or a child's they are a guardian " +
141
+ "of), from Needs your attention: `linkId` as `linkedForm` opened it. The answers are the " +
142
+ "link's person's, once; a link that cannot be used is one answer, the same as for a token.",
143
+ input: z.object({
144
+ linkId: z.string().min(1).max(64).describe("The personal link's id, from Needs your attention"),
145
+ id: formId,
146
+ version: z.number().int().positive().describe("The version `linkedForm` showed"),
147
+ answers,
148
+ honeypot: z.string().max(1000).optional().describe("The hidden field a person leaves empty"),
149
+ }),
150
+ output: received,
151
+ risk: "write",
152
+ audiences: ["customer"],
153
+ redact: ["answers", "honeypot"],
154
+ async handler(input, ctx) {
155
+ return ctx.ops.call("intake.submit", {
156
+ object: FORM,
157
+ id: input.id,
158
+ version: input.version,
159
+ answers: input.answers,
160
+ via: "app",
161
+ linkId: input.linkId,
162
+ ...(input.honeypot !== undefined ? { honeypot: input.honeypot } : {}),
163
+ });
164
+ },
165
+ });
166
+ // --- Staff ---------------------------------------------------------------------------------------
167
+ export const enter = defineTool({
168
+ description: "Enter answers on someone's behalf (a phone call, a paper form) or fill in a staff form (an " +
169
+ "incident report), for any published form. The submission is marked as entered by staff, " +
170
+ "and the person is found or made from the email answered, filling only their empty " +
171
+ "fields. Staff cannot tick a 'keep me updated' line for someone: only the person can.",
172
+ input: z.object({ formId, answers }),
173
+ output: received,
174
+ risk: "write",
175
+ permission: "forms:answer",
176
+ redact: ["answers"],
177
+ async handler(input, ctx) {
178
+ return ctx.ops.call("intake.submit", {
179
+ object: FORM,
180
+ id: input.formId,
181
+ answers: input.answers,
182
+ });
183
+ },
184
+ });
185
+ const inboxStates = ["new", "inHand", "done"];
186
+ const stateLabel = {
187
+ new: "new",
188
+ inHand: "in hand",
189
+ done: "done",
190
+ spam: "spam",
191
+ };
192
+ /** The submission's state now, for a refusal's words. */
193
+ async function stateOf(ctx, id) {
194
+ const record = await ctx.ops.call("record.get", { object: SUBMISSION, id });
195
+ return text(record.values.status);
196
+ }
197
+ const moved = z.object({
198
+ id: z.string(),
199
+ status: z.string(),
200
+ });
201
+ export const move = defineTool({
202
+ description: "Move a submission in the inbox: new, in hand or done. Refused when the move is not one the " +
203
+ "inbox has (a spam submission is taken out with notSpam first), or when someone moved it " +
204
+ "meanwhile.",
205
+ input: z.object({
206
+ submissionId,
207
+ to: z.enum(inboxStates),
208
+ from: z
209
+ .enum([...inboxStates, "spam"])
210
+ .optional()
211
+ .describe("Only if it is still in this state: for a move two people may make at once"),
212
+ }),
213
+ output: moved,
214
+ risk: "write",
215
+ permission: "forms:answer",
216
+ async handler(input, ctx) {
217
+ const after = await ctx.ops.call("record.update", {
218
+ object: SUBMISSION,
219
+ id: input.submissionId,
220
+ ...(input.from ? { from: input.from } : {}),
221
+ values: { status: input.to },
222
+ });
223
+ return { id: after.id, status: text(after.values.status) ?? input.to };
224
+ },
225
+ });
226
+ export const markSpam = defineTool({
227
+ description: "Mark a submission as spam: it leaves the inbox and is kept in Spam. A person's act: the " +
228
+ "assistant may suggest it, and the suggestion waits for a person to approve it. Undo with " +
229
+ "notSpam.",
230
+ recordAction: {
231
+ object: SUBMISSION,
232
+ label: "Mark as spam",
233
+ idInput: "submissionId",
234
+ confirm: "Move these answers to Spam?",
235
+ hideWhen: { field: "status", values: ["spam"] },
236
+ },
237
+ input: z.object({ submissionId }),
238
+ output: moved,
239
+ risk: "write",
240
+ // Anyone but a person waits for a person (ADR 0552): the move into spam is this tool's and the
241
+ // host's only.
242
+ personApproves: true,
243
+ permission: "forms:answer",
244
+ async handler(input, ctx) {
245
+ // Marked twice (two people, or an approval after a person did it): already done.
246
+ if ((await stateOf(ctx, input.submissionId)) === "spam") {
247
+ return { id: input.submissionId, status: "spam" };
248
+ }
249
+ const after = await ctx.ops.call("record.update", {
250
+ object: SUBMISSION,
251
+ id: input.submissionId,
252
+ values: { status: "spam" },
253
+ });
254
+ return { id: after.id, status: text(after.values.status) ?? "spam" };
255
+ },
256
+ });
257
+ export const notSpam = defineTool({
258
+ description: "Take a submission out of Spam: it is back in the inbox as new. A person's act: the " +
259
+ "assistant may suggest it, and the suggestion waits for a person to approve it.",
260
+ recordAction: {
261
+ object: SUBMISSION,
262
+ label: "Not spam",
263
+ idInput: "submissionId",
264
+ hideWhen: { field: "status", values: ["new", "inHand", "done"] },
265
+ },
266
+ input: z.object({ submissionId }),
267
+ output: moved,
268
+ risk: "write",
269
+ personApproves: true,
270
+ permission: "forms:answer",
271
+ async handler(input, ctx) {
272
+ try {
273
+ const after = await ctx.ops.call("record.update", {
274
+ object: SUBMISSION,
275
+ id: input.submissionId,
276
+ from: "spam",
277
+ values: { status: "new" },
278
+ });
279
+ return { id: after.id, status: text(after.values.status) ?? "new" };
280
+ }
281
+ catch (error) {
282
+ if (!(error instanceof OpsCallError) || error.code !== "conflict")
283
+ throw error;
284
+ const now = await stateOf(ctx, input.submissionId);
285
+ throw new OpsCallError("conflict", `These answers are not in Spam (they are ${stateLabel[now ?? ""] ?? "elsewhere"}).`, { reason: "not_spam" });
286
+ }
287
+ },
288
+ });
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@composable-os/pack-forms",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-beta.12",
4
+ "license": "FSL-1.1-ALv2",
5
+ "type": "module",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/timoconnellaus/composable-os.git"
9
+ },
10
+ "homepage": "https://composableos.dev",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "provenance": true
14
+ },
15
+ "description": "Forms: forms a business builds and publishes, and the answers people send to them, kept as submissions in an inbox (stage 29, ADR 0726). Answers arrive only through the host's intake door (ADR 0720). Bundled as source into cos-runner.",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ },
21
+ "./client": {
22
+ "types": "./dist/client/index.d.ts",
23
+ "default": "./dist/client/index.js"
24
+ },
25
+ "./page": {
26
+ "types": "./dist/client/page.d.ts",
27
+ "default": "./dist/client/page.js"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "composableOs": {
34
+ "optional": true,
35
+ "label": "Forms",
36
+ "description": "Enquiry, sign-up, survey, feedback and staff forms the business builds and publishes, with every answer in one inbox, linked to the person who sent it."
37
+ },
38
+ "dependencies": {
39
+ "@composable-os/client": "0.1.0-beta.12",
40
+ "@composable-os/sdk": "0.1.0-beta.12"
41
+ }
42
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.