@composable-os/pack-knowledge 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/LICENSE ADDED
@@ -0,0 +1,109 @@
1
+ # Functional Source License, Version 1.1, ALv2 Future License
2
+
3
+ > This licence covers the repository except the directories listed in NOTICE. In particular, the
4
+ > assistant packages `packages/agent-*` (agent-core, agent-a2ui, agent-voice, agent-plugins) are
5
+ > licensed under the MIT License; see the LICENSE file in each of those directories (ADR 0350).
6
+
7
+ ## Abbreviation
8
+
9
+ FSL-1.1-ALv2
10
+
11
+ ## Notice
12
+
13
+ Copyright 2026 Tim O'Connell
14
+
15
+ ## Terms and Conditions
16
+
17
+ ### Licensor ("We")
18
+
19
+ The party offering the Software under these Terms and Conditions.
20
+
21
+ ### The Software
22
+
23
+ The "Software" is each version of the software that we make available under
24
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
25
+ Conditions with the Software.
26
+
27
+ ### License Grant
28
+
29
+ Subject to your compliance with this License Grant and the Patents,
30
+ Redistribution and Trademark clauses below, we hereby grant you the right to
31
+ use, copy, modify, create derivative works, publicly perform, publicly display
32
+ and redistribute the Software for any Permitted Purpose identified below.
33
+
34
+ ### Permitted Purpose
35
+
36
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
37
+ means making the Software available to others in a commercial product or
38
+ service that:
39
+
40
+ 1. substitutes for the Software;
41
+
42
+ 2. substitutes for any other product or service we offer using the Software
43
+ that exists as of the date we make the Software available; or
44
+
45
+ 3. offers the same or substantially similar functionality as the Software.
46
+
47
+ Permitted Purposes specifically include using the Software:
48
+
49
+ 1. for your internal use and access;
50
+
51
+ 2. for non-commercial education;
52
+
53
+ 3. for non-commercial research; and
54
+
55
+ 4. in connection with professional services that you provide to a licensee
56
+ using the Software in accordance with these Terms and Conditions.
57
+
58
+ ### Patents
59
+
60
+ To the extent your use for a Permitted Purpose would necessarily infringe our
61
+ patents, the license grant above includes a license under our patents. If you
62
+ make a claim against any party that the Software infringes or contributes to
63
+ the infringement of any patent, then your patent license to the Software ends
64
+ immediately.
65
+
66
+ ### Redistribution
67
+
68
+ The Terms and Conditions apply to all copies, modifications and derivatives of
69
+ the Software.
70
+
71
+ If you redistribute any copies, modifications or derivatives of the Software,
72
+ you must include a copy of or a link to these Terms and Conditions and not
73
+ remove any copyright notices provided in or with the Software.
74
+
75
+ ### Disclaimer
76
+
77
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
78
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
79
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
80
+
81
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
82
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
83
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
84
+
85
+ ### Trademarks
86
+
87
+ Except for displaying the License Details and identifying us as the origin of
88
+ the Software, you have no right under these Terms and Conditions to use our
89
+ trademarks, trade names, service marks or product names.
90
+
91
+ ## Grant of Future License
92
+
93
+ We hereby irrevocably grant you an additional license to use the Software under
94
+ the Apache License, Version 2.0 that is effective on the second anniversary of
95
+ the date we make the Software available. On or after that date, you may use the
96
+ Software under the Apache License, Version 2.0, in which case the following
97
+ will apply:
98
+
99
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
100
+ this file except in compliance with the License.
101
+
102
+ You may obtain a copy of the License at
103
+
104
+ http://www.apache.org/licenses/LICENSE-2.0
105
+
106
+ Unless required by applicable law or agreed to in writing, software distributed
107
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
108
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
109
+ specific language governing permissions and limitations under the License.
@@ -0,0 +1,4 @@
1
+ export declare const reviewDue: any;
2
+ export declare const knowledgeAutomations: {
3
+ reviewDue: any;
4
+ };
@@ -0,0 +1,42 @@
1
+ // The Knowledge automations (stage 38 step 5, ADR 0706). The notices of read and understood (a
2
+ // version to read, the 7-day reminder, the owners' weekly notice, the welcome of a new account)
3
+ // are the host's, in this pack's words (`acknowledge.notices` in index.ts): only the host knows who
4
+ // must read a procedure and who has not. This one is the review reminder: on a procedure's next
5
+ // review date (its last publish plus its "review every" months, a formula; none when it is 0),
6
+ // its owner is told to check it is still right, or the admins when it has none.
7
+ import { defineAutomation } from "@composable-os/sdk";
8
+ import { PROCEDURE } from "./tools.js";
9
+ export const reviewDue = defineAutomation({
10
+ label: "Time to review a procedure",
11
+ description: "On a published procedure's next review date (its last publish plus its review period, 12 " +
12
+ "months unless set; never when 0), tells its owner to check it is still right, or the " +
13
+ "admins when it has no owner.",
14
+ trigger: { type: "dateRelative", object: PROCEDURE, field: "nextReview", offsetDays: 0 },
15
+ // No condition: the next review is empty while the procedure is not published (no version).
16
+ steps: [
17
+ {
18
+ name: "tell",
19
+ label: "Tell the owner, or the admins",
20
+ notifies: [
21
+ { to: { field: "owner" }, message: "Time to check this is still right: {record}" },
22
+ { to: "admins", message: "Time to check this is still right: {record}" },
23
+ ],
24
+ },
25
+ ],
26
+ async run(event, step, ctx) {
27
+ const procedure = event.record;
28
+ if (!procedure)
29
+ return { skipped: "no procedure" };
30
+ return step.do("tell", async () => {
31
+ const owner = procedure.data?.owner;
32
+ await ctx.notify(typeof owner === "string" && owner ? { field: "owner" } : "admins", {
33
+ title: "Time to check this is still right: {record}",
34
+ body: "Read the procedure as it is published. If it is still right, publish it again as a " +
35
+ "small fix to start a new review period; if not, change it and publish.",
36
+ link: "record",
37
+ });
38
+ return { told: typeof owner === "string" && owner ? "owner" : "admins" };
39
+ });
40
+ },
41
+ });
42
+ export const knowledgeAutomations = { reviewDue };
@@ -0,0 +1,176 @@
1
+ import { type ClientHolding, type PackClient, type UseMutation, type UseQuery } from "@composable-os/client";
2
+ import type { z } from "@composable-os/sdk";
3
+ import type { confirm, helpTopics, readStatus, toRead } from "../tools.ts";
4
+ type Io<T extends {
5
+ input: z.ZodType;
6
+ output: z.ZodType;
7
+ }> = {
8
+ input: z.input<T["input"]>;
9
+ output: z.output<T["output"]>;
10
+ };
11
+ /** The objects, as the host names them. */
12
+ export declare const PROCEDURE = "knowledge.procedure";
13
+ export declare const HELP_PAGE = "knowledge.helppage";
14
+ /** A published record as staff or the public read it: its content fields and its slug. */
15
+ export interface Published {
16
+ object: string;
17
+ id: string;
18
+ values: Record<string, unknown>;
19
+ version?: number;
20
+ publishedAt?: string;
21
+ }
22
+ /** A part of a highlighted title, heading or snippet: plain, or a matching word. */
23
+ export interface Segment {
24
+ text: string;
25
+ match: boolean;
26
+ }
27
+ export interface SearchHit {
28
+ object: string;
29
+ id: string;
30
+ slug: string;
31
+ title: string | null;
32
+ titleHighlight: Segment[];
33
+ version: number;
34
+ section: {
35
+ field: string;
36
+ anchor: string | null;
37
+ heading: string;
38
+ headingHighlight: Segment[];
39
+ };
40
+ snippet: Segment[] | null;
41
+ }
42
+ type Page<T> = {
43
+ items: T[];
44
+ nextCursor: string | null;
45
+ };
46
+ type ListInput = {
47
+ object: string;
48
+ filter?: unknown;
49
+ sort?: {
50
+ field: string;
51
+ direction: "asc" | "desc";
52
+ };
53
+ cursor?: string;
54
+ limit?: number;
55
+ };
56
+ /** The operations this entry calls, as the generated client types them. */
57
+ export type KnowledgeOperations = {
58
+ "content.staffGet": {
59
+ kind: "query";
60
+ input: {
61
+ object: string;
62
+ id?: string;
63
+ slug?: string;
64
+ };
65
+ output: Published & {
66
+ redirect?: string;
67
+ };
68
+ };
69
+ "content.staffList": {
70
+ kind: "query";
71
+ input: ListInput;
72
+ output: Page<Published>;
73
+ };
74
+ "x.knowledge.toRead": {
75
+ kind: "query";
76
+ } & Io<typeof toRead>;
77
+ "x.knowledge.confirm": {
78
+ kind: "mutation";
79
+ } & Io<typeof confirm>;
80
+ "x.knowledge.readStatus": {
81
+ kind: "query";
82
+ } & Io<typeof readStatus>;
83
+ "content.get": {
84
+ kind: "query";
85
+ input: {
86
+ object: string;
87
+ slug: string;
88
+ };
89
+ output: (Published & {
90
+ kind: "record";
91
+ }) | {
92
+ kind: "redirect";
93
+ object: string;
94
+ slug: string;
95
+ };
96
+ };
97
+ "content.list": {
98
+ kind: "query";
99
+ input: ListInput;
100
+ output: Page<Published>;
101
+ };
102
+ "content.search": {
103
+ kind: "query";
104
+ input: {
105
+ object: string;
106
+ q: string;
107
+ cursor?: string;
108
+ limit?: number;
109
+ };
110
+ output: Page<SearchHit>;
111
+ };
112
+ "x.knowledge.helpTopics": {
113
+ kind: "query";
114
+ } & Io<typeof helpTopics>;
115
+ };
116
+ type Name = keyof KnowledgeOperations & string;
117
+ type In<N extends Name> = KnowledgeOperations[N]["input"];
118
+ /** The client of every operation this entry calls (ADR 0453). */
119
+ export type KnowledgeClient = PackClient<KnowledgeOperations>;
120
+ export type { UseMutation, UseQuery } from "@composable-os/client";
121
+ /**
122
+ * The operations each hook calls (ADR 0453). The staff client (a pack-role account's too) gets
123
+ * the procedure hooks; a customer's client and the public's get the help centre's.
124
+ */
125
+ export declare const KNOWLEDGE_HOOKS_CALL: {
126
+ readonly useProcedures: readonly ["content.staffList"];
127
+ readonly useProcedure: readonly ["content.staffGet"];
128
+ readonly useSearchProcedures: readonly ["content.search"];
129
+ readonly useToRead: readonly ["x.knowledge.toRead"];
130
+ readonly useConfirm: readonly ["x.knowledge.confirm"];
131
+ readonly useReadStatus: readonly ["x.knowledge.readStatus"];
132
+ readonly useHelpTopics: readonly ["x.knowledge.helpTopics"];
133
+ readonly useHelpPages: readonly ["content.list"];
134
+ readonly useHelpPage: readonly ["content.get"];
135
+ readonly useSearchHelp: readonly ["content.search"];
136
+ };
137
+ /**
138
+ * Hooks for a React or React Native app:
139
+ * `const knowledge = createKnowledgeHooks({ useQuery, useMutation }, api)`. The hooks a client
140
+ * cannot back are not there, and calling one does not compile.
141
+ */
142
+ export declare function createKnowledgeHooks<C extends ClientHolding>(react: {
143
+ useQuery: UseQuery;
144
+ useMutation: UseMutation;
145
+ }, client: C): any;
146
+ declare function allKnowledgeHooks(react: {
147
+ useQuery: UseQuery;
148
+ useMutation: UseMutation;
149
+ }, api: KnowledgeClient): {
150
+ /** Staff: a page of published procedures (by section, or as asked). */
151
+ useProcedures(input?: Omit<In<"content.staffList">, "object">): any;
152
+ /** Staff: one procedure as published, by id or address. */
153
+ useProcedure(by: {
154
+ id: string;
155
+ } | {
156
+ slug: string;
157
+ } | null): any;
158
+ /** Staff: procedures whose published words match, each with its section's anchor. */
159
+ useSearchProcedures(q: string): any;
160
+ /** Staff: what I must read, and what I confirmed. */
161
+ useToRead(): any;
162
+ /** Staff: "I have read and understood this", for the version read. */
163
+ useConfirm(): any;
164
+ /** Those who manage: who has read what (one procedure, or every one's counts). */
165
+ useReadStatus(id?: string): any;
166
+ /** Anyone: the help topics, in their order. */
167
+ useHelpTopics(): any;
168
+ /** Anyone: a page of published help pages (a topic's, or as asked). */
169
+ useHelpPages(input?: Omit<In<"content.list">, "object">): any;
170
+ /** Anyone: one published help page by its address; an old address answers `redirect`. */
171
+ useHelpPage(slug: string | null): any;
172
+ /** Anyone: help pages whose published words match, as the reader types. */
173
+ useSearchHelp(q: string): any;
174
+ };
175
+ /** Every hook of this entry. A block takes the ones it calls: `Pick<KnowledgeHooks, "useToRead">`. */
176
+ export type KnowledgeHooks = ReturnType<typeof allKnowledgeHooks>;
@@ -0,0 +1,101 @@
1
+ // The Knowledge pack's headless entry (stage 38 step 5; ADRs 0418 and 0453): hooks over the typed
2
+ // client, no markup. A client app imports it as `@composable-os/pack-knowledge/client` and hands
3
+ // it its generated client; the blocks (step 6) render what it returns.
4
+ //
5
+ // Staff read procedures as published with the host's `content.staffGet`, `content.staffList` and
6
+ // `content.search` (ADR 0701), never the working copy, and confirm with this pack's `confirm`
7
+ // tool. The public reads help pages as published with `content.get`, `content.list` and
8
+ // `content.search` (ADRs 0442, 0700). Nothing here decides who may read what, what is to read,
9
+ // or whether a confirmation counts: those are the host's.
10
+ import { packHooks, } from "@composable-os/client";
11
+ /** The objects, as the host names them. */
12
+ export const PROCEDURE = "knowledge.procedure";
13
+ export const HELP_PAGE = "knowledge.helppage";
14
+ /**
15
+ * The operations each hook calls (ADR 0453). The staff client (a pack-role account's too) gets
16
+ * the procedure hooks; a customer's client and the public's get the help centre's.
17
+ */
18
+ export const KNOWLEDGE_HOOKS_CALL = {
19
+ useProcedures: ["content.staffList"],
20
+ useProcedure: ["content.staffGet"],
21
+ useSearchProcedures: ["content.search"],
22
+ useToRead: ["x.knowledge.toRead"],
23
+ useConfirm: ["x.knowledge.confirm"],
24
+ useReadStatus: ["x.knowledge.readStatus"],
25
+ useHelpTopics: ["x.knowledge.helpTopics"],
26
+ useHelpPages: ["content.list"],
27
+ useHelpPage: ["content.get"],
28
+ useSearchHelp: ["content.search"],
29
+ };
30
+ /**
31
+ * Hooks for a React or React Native app:
32
+ * `const knowledge = createKnowledgeHooks({ useQuery, useMutation }, api)`. The hooks a client
33
+ * cannot back are not there, and calling one does not compile.
34
+ */
35
+ export function createKnowledgeHooks(react, client) {
36
+ return packHooks(client, KNOWLEDGE_HOOKS_CALL, (api) => allKnowledgeHooks(react, api));
37
+ }
38
+ const idle = (name) => ({
39
+ queryKey: [name, "none"],
40
+ queryFn: async () => undefined,
41
+ });
42
+ function allKnowledgeHooks(react, api) {
43
+ const { useQuery, useMutation } = react;
44
+ return {
45
+ /** Staff: a page of published procedures (by section, or as asked). */
46
+ useProcedures(input = {}) {
47
+ return useQuery(api.query("content.staffList", { object: PROCEDURE, ...input }));
48
+ },
49
+ /** Staff: one procedure as published, by id or address. */
50
+ useProcedure(by) {
51
+ const options = by
52
+ ? api.query("content.staffGet", { object: PROCEDURE, ...by })
53
+ : idle("content.staffGet");
54
+ return useQuery({ ...options, enabled: by !== null });
55
+ },
56
+ /** Staff: procedures whose published words match, each with its section's anchor. */
57
+ useSearchProcedures(q) {
58
+ const options = q.trim()
59
+ ? api.query("content.search", { object: PROCEDURE, q })
60
+ : idle("content.search");
61
+ return useQuery({ ...options, enabled: q.trim() !== "" });
62
+ },
63
+ /** Staff: what I must read, and what I confirmed. */
64
+ useToRead() {
65
+ return useQuery(api.query("x.knowledge.toRead", {}));
66
+ },
67
+ /** Staff: "I have read and understood this", for the version read. */
68
+ useConfirm() {
69
+ return useMutation(api.mutation("x.knowledge.confirm"));
70
+ },
71
+ /** Those who manage: who has read what (one procedure, or every one's counts). */
72
+ useReadStatus(id) {
73
+ return useQuery(api.query("x.knowledge.readStatus", id === undefined ? {} : { id }));
74
+ },
75
+ /** Anyone: the help topics, in their order. */
76
+ useHelpTopics() {
77
+ return useQuery(api.query("x.knowledge.helpTopics", {}));
78
+ },
79
+ /** Anyone: a page of published help pages (a topic's, or as asked). */
80
+ useHelpPages(input = {}) {
81
+ return useQuery(api.query("content.list", { object: HELP_PAGE, ...input }));
82
+ },
83
+ /** Anyone: one published help page by its address; an old address answers `redirect`. */
84
+ useHelpPage(slug) {
85
+ const options = slug
86
+ ? api.query("content.get", { object: HELP_PAGE, slug })
87
+ : idle("content.get");
88
+ return useQuery({
89
+ ...options,
90
+ enabled: slug !== null,
91
+ });
92
+ },
93
+ /** Anyone: help pages whose published words match, as the reader types. */
94
+ useSearchHelp(q) {
95
+ const options = q.trim()
96
+ ? api.query("content.search", { object: HELP_PAGE, q })
97
+ : idle("content.search");
98
+ return useQuery({ ...options, enabled: q.trim() !== "" });
99
+ },
100
+ };
101
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,325 @@
1
+ // Knowledge (stage 38 step 5, ADR 0706): how the business does things, written down once, and the
2
+ // answers to its customers' common questions. Any product can turn it on (`packs` in its
3
+ // product.ts). The platform ships no procedures and no help pages: they are the business's, added
4
+ // in the admin, by the assistant or by an MCP client, with a person publishing each.
5
+ //
6
+ // Everything that is a rule is the host's, declared here as behaviours (ADR 0404):
7
+ // - both objects are `publishable` (ADRs 0446, 0441): staff and the assistant edit a working
8
+ // copy; a person with the publish permission publishes it (an AI asks, ADR 0480);
9
+ // - a procedure is for staff only: it declares no `public` and no `signedIn`, so nothing an edit
10
+ // can change makes it public. `staffRead` (ADR 0701) opens its published version, never the
11
+ // draft, to every staff account, pack roles included (a coach, a cleaner, the door), through
12
+ // `content.staffGet`, `content.staffList` and `content.search`;
13
+ // - a procedure is `acknowledge`d (ADR 0702): its "must read" (everyone, or staff roles, pack
14
+ // roles and granted pack permissions) is asked to confirm each version that asks again, with
15
+ // one tap; the host keeps the confirmation, and who must read, the owner and the review period are a
16
+ // person's to change, once published only an admin's or a holder of knowledge:manage
17
+ // (`managed`, ADR 0706). The notices are this pack's words, sent by the host;
18
+ // - a help page is public in its published wording only (ADR 0442): `content.get`,
19
+ // `content.list` and `content.search` serve the version last published, never a draft.
20
+ // The tools in tools.ts compose the host's operations and add no rule of their own.
21
+ import { defineExtension, MUST_READ_SCHEMA } from "@composable-os/sdk";
22
+ import { knowledgeAutomations } from "./automations.js";
23
+ import { confirm, draftHelpPage, draftProcedure, helpTopics, MANAGE, readStatus, toRead, WRITE, } from "./tools.js";
24
+ const HELP_ICONS = [
25
+ { value: "calendar", label: "Calendar" },
26
+ { value: "credit-card", label: "Payments" },
27
+ { value: "user-round", label: "Account" },
28
+ { value: "smartphone", label: "App" },
29
+ { value: "map-pin", label: "Getting here" },
30
+ { value: "baby", label: "Children" },
31
+ { value: "shield-check", label: "Safety" },
32
+ { value: "wrench", label: "Care and repairs" },
33
+ { value: "circle-help", label: "Other" },
34
+ ];
35
+ export default defineExtension({
36
+ id: "knowledge",
37
+ version: "0.1.0",
38
+ contractVersion: 1,
39
+ label: "Knowledge",
40
+ description: "Staff procedures read in their published wording and confirmed as read and understood, " +
41
+ "with a review reminder, and help pages for customers on the business's website.",
42
+ permissions: {
43
+ // ack.*: a person's own list and confirmation, and who has read what for those who manage
44
+ // (ADR 0702, from this pack's person-only tools). public.list: the help topics.
45
+ // notification.send: the review reminder.
46
+ operations: [
47
+ "ack.mine",
48
+ "ack.confirm",
49
+ "ack.status",
50
+ "ack.overview",
51
+ "public.list",
52
+ "notification.send",
53
+ ],
54
+ modules: ["ops"],
55
+ },
56
+ // Read status is for admins and those they allow (decision 13); writing procedures and help
57
+ // pages is every member's, and a coach's or a cleaner's when an admin grants it (ADR 0600).
58
+ grantable: [MANAGE, WRITE],
59
+ restricted: [MANAGE],
60
+ grantableLabels: {
61
+ [MANAGE]: "See who has read and confirmed each procedure, and change who must read one once published",
62
+ [WRITE]: "Write procedures and help pages (a person allowed to publish publishes them)",
63
+ },
64
+ objects: {
65
+ procedure: {
66
+ label: "Procedure",
67
+ plural: "Procedures",
68
+ icon: "list-checks",
69
+ titleField: "title",
70
+ owner: "owner",
71
+ description: "How the business does something, for staff: read by every staff account in the wording " +
72
+ "last published, never the draft, and confirmed as read and understood by those who " +
73
+ "must read it.",
74
+ navigation: "main",
75
+ navigationOrder: 60,
76
+ fields: {
77
+ title: { type: "text", label: "Title", required: true },
78
+ slug: {
79
+ type: "slug",
80
+ label: "Address",
81
+ description: "How a link names the procedure. Made from the title.",
82
+ },
83
+ summary: {
84
+ type: "text",
85
+ label: "Summary",
86
+ description: "One or two sentences for lists and search results.",
87
+ },
88
+ body: {
89
+ type: "richText",
90
+ label: "Body",
91
+ description: "The procedure. Each heading is a section that search and the assistant link to. " +
92
+ "Name a role, not a person ('the duty manager', not 'Sam').",
93
+ },
94
+ section: { type: "relation", label: "Section", to: "knowledge.section" },
95
+ owner: {
96
+ type: "user",
97
+ label: "Kept right by",
98
+ description: "Who keeps it right: told when it is time to review it, and each week who has not " +
99
+ "confirmed it.",
100
+ },
101
+ mustRead: {
102
+ type: "document",
103
+ label: "Must read",
104
+ schema: MUST_READ_SCHEMA,
105
+ description: "Who is asked to confirm it: everyone, or some staff roles, pack roles (a coach, a " +
106
+ "cleaner) and granted permissions (the front desk). Every staff account may read it " +
107
+ "either way. Changing it counts at once.",
108
+ },
109
+ reviewEvery: {
110
+ type: "number",
111
+ label: "Review every (months)",
112
+ default: 12,
113
+ min: 0,
114
+ max: 120,
115
+ step: 1,
116
+ description: "How often its owner is asked to check it is still right. 0: never.",
117
+ },
118
+ nextReview: {
119
+ type: "formula",
120
+ label: "Next review",
121
+ description: "Its last publish plus the review period. Empty when it is never reviewed.",
122
+ expression: 'if(coalesce(reviewEvery, 12) > 0, dateAdd(publishedAt, coalesce(reviewEvery, 12), "month"), null)',
123
+ },
124
+ },
125
+ indexes: [["section"], ["owner"]],
126
+ publishable: {
127
+ content: ["title", "summary", "body", "section"],
128
+ slug: "slug",
129
+ staffRead: true,
130
+ acknowledge: {
131
+ who: "mustRead",
132
+ manage: MANAGE,
133
+ managed: ["reviewEvery", "owner"],
134
+ owner: "owner",
135
+ notices: {
136
+ asked: {
137
+ title: "Please read: {record}",
138
+ body: "A procedure you must read has been published or changed. Read it and confirm " +
139
+ "you have read and understood it.",
140
+ },
141
+ reminder: {
142
+ title: "Still to read: {record}",
143
+ body: "Read this procedure and confirm you have read and understood it.",
144
+ afterDays: 7,
145
+ },
146
+ outstanding: { title: "Not yet confirmed: {record}" },
147
+ welcome: {
148
+ title: "Procedures to read",
149
+ body: "Welcome. Read these procedures and confirm each one:",
150
+ },
151
+ },
152
+ },
153
+ },
154
+ },
155
+ section: {
156
+ label: "Section",
157
+ plural: "Sections",
158
+ icon: "shapes",
159
+ titleField: "name",
160
+ navigation: "settings",
161
+ description: "A group of procedures (opening and closing, safety, the front desk). Staff only.",
162
+ fields: {
163
+ name: { type: "text", label: "Name", required: true },
164
+ order: { type: "number", label: "Order", step: 1 },
165
+ },
166
+ indexes: [["order"]],
167
+ },
168
+ helppage: {
169
+ label: "Help page",
170
+ plural: "Help pages",
171
+ icon: "newspaper",
172
+ titleField: "title",
173
+ description: "An answer to a customer's question, on the business's website for anyone. The public " +
174
+ "reads the version last published.",
175
+ navigation: "main",
176
+ navigationOrder: 61,
177
+ fields: {
178
+ title: { type: "text", label: "Title", required: true },
179
+ slug: {
180
+ type: "slug",
181
+ label: "Address",
182
+ description: "Where the page is read: /help/<address>. Made from the title.",
183
+ },
184
+ summary: {
185
+ type: "text",
186
+ label: "Summary",
187
+ description: "One or two sentences for lists, search results and link previews.",
188
+ },
189
+ body: { type: "richText", label: "Body" },
190
+ topic: { type: "relation", label: "Topic", to: "knowledge.helptopic" },
191
+ order: { type: "number", label: "Order in its topic", step: 1 },
192
+ related: {
193
+ type: "relation",
194
+ label: "Related pages",
195
+ to: "knowledge.helppage",
196
+ many: true,
197
+ },
198
+ seoTitle: { type: "text", label: "Search title" },
199
+ seoDescription: { type: "text", label: "Search description" },
200
+ noindex: {
201
+ type: "boolean",
202
+ label: "Hide from search engines",
203
+ description: "Asks search engines not to list the page.",
204
+ },
205
+ },
206
+ indexes: [["topic", "order"]],
207
+ publishable: {
208
+ content: [
209
+ "title",
210
+ "summary",
211
+ "body",
212
+ "topic",
213
+ "order",
214
+ "related",
215
+ "seoTitle",
216
+ "seoDescription",
217
+ "noindex",
218
+ ],
219
+ slug: "slug",
220
+ },
221
+ public: {
222
+ read: [
223
+ "title",
224
+ "slug",
225
+ "summary",
226
+ "body",
227
+ "topic",
228
+ "order",
229
+ "related",
230
+ "seoTitle",
231
+ "seoDescription",
232
+ "noindex",
233
+ "publishedAt",
234
+ ],
235
+ },
236
+ },
237
+ helptopic: {
238
+ label: "Help topic",
239
+ plural: "Help topics",
240
+ icon: "tags",
241
+ titleField: "name",
242
+ navigation: "settings",
243
+ description: "A group of help pages, as the help centre's menu shows them.",
244
+ fields: {
245
+ name: { type: "text", label: "Name", required: true },
246
+ slug: { type: "slug", label: "Address" },
247
+ order: { type: "number", label: "Order", step: 1 },
248
+ icon: { type: "select", label: "Icon", options: HELP_ICONS },
249
+ },
250
+ indexes: [["order"]],
251
+ // Every live topic is public: a help centre's menu.
252
+ public: { read: ["name", "slug", "order", "icon"] },
253
+ },
254
+ },
255
+ views: {
256
+ procedureDrafts: {
257
+ label: "Procedure drafts",
258
+ object: "knowledge.procedure",
259
+ filter: { field: "publishState", op: "eq", value: "draft" },
260
+ sort: { field: "updatedAt", direction: "desc" },
261
+ columns: ["title", "section", "owner", "updatedAt"],
262
+ },
263
+ procedures: {
264
+ label: "Published procedures",
265
+ object: "knowledge.procedure",
266
+ filter: { field: "publishState", op: "eq", value: "published" },
267
+ sort: { field: "title", direction: "asc" },
268
+ columns: ["title", "section", "owner", "publishedVersion", "publishedAt", "nextReview"],
269
+ },
270
+ reviewDue: {
271
+ label: "Procedures by next review",
272
+ object: "knowledge.procedure",
273
+ filter: {
274
+ and: [
275
+ { field: "publishState", op: "eq", value: "published" },
276
+ { field: "nextReview", op: "is_not_null" },
277
+ ],
278
+ },
279
+ sort: { field: "nextReview", direction: "asc" },
280
+ columns: ["title", "owner", "nextReview", "publishedAt"],
281
+ },
282
+ helpDrafts: {
283
+ label: "Help page drafts",
284
+ object: "knowledge.helppage",
285
+ filter: { field: "publishState", op: "eq", value: "draft" },
286
+ sort: { field: "updatedAt", direction: "desc" },
287
+ columns: ["title", "topic", "updatedAt"],
288
+ },
289
+ helpPages: {
290
+ label: "Published help pages",
291
+ object: "knowledge.helppage",
292
+ filter: { field: "publishState", op: "eq", value: "published" },
293
+ sort: { field: "title", direction: "asc" },
294
+ columns: ["title", "topic", "publishedVersion", "publishedAt"],
295
+ },
296
+ },
297
+ recordTabs: {
298
+ sectionProcedures: {
299
+ label: "Procedures",
300
+ on: "knowledge.section",
301
+ view: {
302
+ type: "table",
303
+ object: "knowledge.procedure",
304
+ relation: "section",
305
+ columns: ["title", "publishState", "owner", "nextReview"],
306
+ sort: { field: "title", direction: "asc" },
307
+ },
308
+ order: 10,
309
+ },
310
+ topicPages: {
311
+ label: "Help pages",
312
+ on: "knowledge.helptopic",
313
+ view: {
314
+ type: "table",
315
+ object: "knowledge.helppage",
316
+ relation: "topic",
317
+ columns: ["title", "publishState", "order"],
318
+ sort: { field: "order", direction: "asc" },
319
+ },
320
+ order: 10,
321
+ },
322
+ },
323
+ tools: { toRead, confirm, readStatus, helpTopics, draftProcedure, draftHelpPage },
324
+ automations: knowledgeAutomations,
325
+ });
@@ -0,0 +1,39 @@
1
+ export declare const PROCEDURE = "knowledge.procedure";
2
+ export declare const SECTION = "knowledge.section";
3
+ export declare const HELP_PAGE = "knowledge.helppage";
4
+ export declare const HELP_TOPIC = "knowledge.helptopic";
5
+ export declare const MANAGE = "knowledge:manage";
6
+ export declare const WRITE = "knowledge:write";
7
+ type Inline = {
8
+ type: "text";
9
+ text: string;
10
+ };
11
+ type Block = {
12
+ type: "paragraph";
13
+ content: Inline[];
14
+ } | {
15
+ type: "heading";
16
+ level: number;
17
+ content: Inline[];
18
+ } | {
19
+ type: "list";
20
+ style: "bullet" | "ordered";
21
+ items: {
22
+ content: Inline[];
23
+ }[];
24
+ };
25
+ /**
26
+ * Notes as the platform's rich text (ADR 0440): `#`, `##` and `###` start a heading (a section of
27
+ * the procedure, which search and citations link to), `-` or `*` a bullet, `1.` a numbered step,
28
+ * and a blank line ends a paragraph. Everything else is plain words: no HTML, no marks.
29
+ */
30
+ export declare function notesToRichText(notes: string): {
31
+ blocks: Block[];
32
+ };
33
+ export declare const toRead: any;
34
+ export declare const confirm: any;
35
+ export declare const readStatus: any;
36
+ export declare const helpTopics: any;
37
+ export declare const draftProcedure: any;
38
+ export declare const draftHelpPage: any;
39
+ export {};
package/dist/tools.js ADDED
@@ -0,0 +1,279 @@
1
+ // The Knowledge pack's tools (stage 38 step 5, ADR 0706). Each composes host operations and adds
2
+ // no rule of its own: who must read a procedure, whether a confirmation counts, who may see who has
3
+ // confirmed, what staff and the public read, and who publishes are all the host's (ADRs 0700 to
4
+ // 0702, 0480).
5
+ //
6
+ // toRead every staff account, a person what I must read, and what I confirmed
7
+ // confirm every staff account, a person "I have read and understood this", one tap
8
+ // readStatus knowledge:manage, a person who has confirmed, who has not
9
+ // helpTopics anyone the help topics, for a site's menu
10
+ // draftProcedure knowledge:write (and members) write a procedure's working copy
11
+ // draftHelpPage knowledge:write (and members) write a help page's working copy
12
+ //
13
+ // The two draft tools write the working copy only: publishing is a person's (`record.publish`,
14
+ // ADR 0480; an AI's call of it is a request a person approves), and who must read a procedure is
15
+ // set by a person (the host refuses an AI actor's write of it).
16
+ import { defineTool, OpsCallError, z } from "@composable-os/sdk";
17
+ export const PROCEDURE = "knowledge.procedure";
18
+ export const SECTION = "knowledge.section";
19
+ export const HELP_PAGE = "knowledge.helppage";
20
+ export const HELP_TOPIC = "knowledge.helptopic";
21
+ export const MANAGE = "knowledge:manage";
22
+ export const WRITE = "knowledge:write";
23
+ const text = (value) => (typeof value === "string" ? value : null);
24
+ const num = (value) => (typeof value === "number" ? value : null);
25
+ const run = (value) => [{ type: "text", text: value }];
26
+ /**
27
+ * Notes as the platform's rich text (ADR 0440): `#`, `##` and `###` start a heading (a section of
28
+ * the procedure, which search and citations link to), `-` or `*` a bullet, `1.` a numbered step,
29
+ * and a blank line ends a paragraph. Everything else is plain words: no HTML, no marks.
30
+ */
31
+ export function notesToRichText(notes) {
32
+ const blocks = [];
33
+ let paragraph = [];
34
+ let list = null;
35
+ const flush = () => {
36
+ if (paragraph.length)
37
+ blocks.push({ type: "paragraph", content: run(paragraph.join(" ")) });
38
+ paragraph = [];
39
+ list = null;
40
+ };
41
+ for (const raw of notes.split(/\r?\n/)) {
42
+ const line = raw.trim();
43
+ if (!line) {
44
+ flush();
45
+ continue;
46
+ }
47
+ const heading = /^(#{1,3})\s+(.+)$/.exec(line);
48
+ const bullet = /^[-*]\s+(.+)$/.exec(line);
49
+ const step = /^\d{1,3}[.)]\s+(.+)$/.exec(line);
50
+ if (heading?.[1] && heading[2]) {
51
+ flush();
52
+ blocks.push({ type: "heading", level: heading[1].length + 1, content: run(heading[2]) });
53
+ }
54
+ else if ((bullet?.[1] || step?.[1]) && !paragraph.length) {
55
+ const style = bullet ? "bullet" : "ordered";
56
+ const words = (bullet?.[1] ?? step?.[1]);
57
+ if (!list || list.style !== style) {
58
+ list = { type: "list", style, items: [] };
59
+ blocks.push(list);
60
+ }
61
+ list.items.push({ content: run(words) });
62
+ }
63
+ else {
64
+ if (list)
65
+ list = null;
66
+ paragraph.push(line);
67
+ }
68
+ }
69
+ flush();
70
+ return { blocks };
71
+ }
72
+ // --- Read and understood ------------------------------------------------------------------------
73
+ const procedureLine = z.object({
74
+ id: z.string(),
75
+ title: z.string().nullable(),
76
+ version: z.number().int().describe("The current published version"),
77
+ publishedAt: z.string(),
78
+ });
79
+ export const toRead = defineTool({
80
+ description: "My procedures: the published ones I must read and have not yet confirmed (oldest first, " +
81
+ "with the version to read and since when), and the ones I have confirmed (with when). For " +
82
+ "the person themselves, every staff account. Read-only.",
83
+ input: z.object({}),
84
+ output: z.object({
85
+ toRead: z.array(procedureLine.extend({ since: z.string() })),
86
+ confirmed: z.array(procedureLine.extend({ confirmedAt: z.string() })),
87
+ }),
88
+ risk: "read",
89
+ // Every staff account, pack roles included (ADR 0652): the host's ack.mine decides what is
90
+ // theirs, from who the person is (never from the input).
91
+ humanOnly: true,
92
+ anyStaff: true,
93
+ async handler(_input, ctx) {
94
+ const mine = await ctx.ops.call("ack.mine", {});
95
+ const strip = ({ object: _object, ...rest }) => rest;
96
+ return {
97
+ toRead: mine.toRead.filter((r) => r.object === PROCEDURE).map(strip),
98
+ confirmed: mine.confirmed.filter((r) => r.object === PROCEDURE).map(strip),
99
+ };
100
+ },
101
+ });
102
+ export const confirm = defineTool({
103
+ description: "Confirm that I have read and understood a procedure's current published version: one tap, " +
104
+ "by the person themselves. Refused for a version that is not the current one (read it again " +
105
+ "and confirm that), a procedure I am not asked to read, and a second time for the same " +
106
+ "version. A confirmation is kept for good and never changed. Not for an AI.",
107
+ input: z.object({
108
+ id: z.string().min(1).max(64).describe("The procedure"),
109
+ version: z.number().int().min(1).describe("The version I read: the current one"),
110
+ }),
111
+ output: z.object({ id: z.string(), version: z.number().int(), confirmedAt: z.string() }),
112
+ risk: "write",
113
+ humanOnly: true,
114
+ anyStaff: true,
115
+ async handler(input, ctx) {
116
+ const out = await ctx.ops.call("ack.confirm", { object: PROCEDURE, id: input.id, version: input.version });
117
+ return { id: out.id, version: out.version, confirmedAt: out.confirmedAt };
118
+ },
119
+ });
120
+ const statusPerson = z.object({
121
+ account: z.string(),
122
+ name: z.string(),
123
+ email: z.string(),
124
+ confirmed: z.object({ version: z.number().int(), confirmedAt: z.string() }).nullable(),
125
+ lastConfirmed: z.object({ version: z.number().int(), confirmedAt: z.string() }).nullable(),
126
+ since: z.string().nullable(),
127
+ });
128
+ export const readStatus = defineTool({
129
+ description: "Who has read what. With a procedure's id: for its current version, everyone who must read " +
130
+ "it, who has confirmed it and when, and who has not and since when. Without one: every " +
131
+ "published procedure staff must read, with how many have confirmed it of how many must. For " +
132
+ "admins and people granted Read status; never for an AI. Read-only.",
133
+ input: z.object({ id: z.string().min(1).max(64).optional().describe("One procedure") }),
134
+ output: z.object({
135
+ procedures: z.array(procedureLine.extend({ mustRead: z.number().int(), confirmed: z.number() })),
136
+ procedure: procedureLine
137
+ .extend({
138
+ everyone: z.boolean(),
139
+ roles: z.array(z.string()),
140
+ people: z.array(statusPerson),
141
+ })
142
+ .nullable(),
143
+ }),
144
+ risk: "read",
145
+ permission: MANAGE,
146
+ humanOnly: true,
147
+ async handler(input, ctx) {
148
+ if (input.id !== undefined) {
149
+ const s = await ctx.ops.call("ack.status", { object: PROCEDURE, id: input.id });
150
+ return {
151
+ procedures: [],
152
+ procedure: {
153
+ id: s.id,
154
+ title: s.title,
155
+ version: s.version,
156
+ publishedAt: s.publishedAt,
157
+ everyone: s.everyone,
158
+ roles: s.roles,
159
+ people: s.people,
160
+ },
161
+ };
162
+ }
163
+ const all = await ctx.ops.call("ack.overview", {});
164
+ return {
165
+ procedures: all.items
166
+ .filter((i) => i.object === PROCEDURE)
167
+ .map(({ object: _object, ...rest }) => rest),
168
+ procedure: null,
169
+ };
170
+ },
171
+ });
172
+ // --- Help pages -----------------------------------------------------------------------------------
173
+ export const helpTopics = defineTool({
174
+ description: "Every help topic, in their order: its id, name, address (slug) and icon, for a help " +
175
+ "centre's menu. Open to anyone. Read-only.",
176
+ input: z.object({ limit: z.number().int().min(1).max(200).default(100) }),
177
+ output: z.object({
178
+ topics: z.array(z.object({
179
+ id: z.string(),
180
+ name: z.string().nullable(),
181
+ slug: z.string().nullable(),
182
+ order: z.number().nullable(),
183
+ icon: z.string().nullable(),
184
+ })),
185
+ }),
186
+ risk: "read",
187
+ audiences: ["staff", "customer", "public"],
188
+ async handler(input, ctx) {
189
+ const page = await ctx.ops.call("public.list", {
190
+ object: HELP_TOPIC,
191
+ sort: { field: "order", direction: "asc" },
192
+ limit: input.limit,
193
+ });
194
+ return {
195
+ topics: page.items.map((item) => ({
196
+ id: item.id,
197
+ name: text(item.values.name),
198
+ slug: text(item.values.slug),
199
+ order: num(item.values.order),
200
+ icon: text(item.values.icon),
201
+ })),
202
+ };
203
+ },
204
+ });
205
+ // --- Drafting -------------------------------------------------------------------------------------
206
+ const draftCommon = {
207
+ id: z
208
+ .string()
209
+ .min(1)
210
+ .max(64)
211
+ .optional()
212
+ .describe("Change this one's working copy; left out, a new one is made"),
213
+ title: z.string().trim().min(1).max(200),
214
+ summary: z.string().trim().max(500).optional().describe("One or two sentences for lists"),
215
+ notes: z
216
+ .string()
217
+ .max(60_000)
218
+ .optional()
219
+ .describe("The words, as notes: '# ' or '## ' starts a section heading, '- ' a bullet, '1. ' a " +
220
+ "numbered step, a blank line ends a paragraph"),
221
+ };
222
+ const draftOut = z.object({
223
+ id: z.string(),
224
+ created: z.boolean(),
225
+ open: z.object({ object: z.string(), id: z.string() }),
226
+ });
227
+ async function draft(ctx, object, id, values) {
228
+ if (id !== undefined) {
229
+ await ctx.ops.call("record.update", { object, id, values });
230
+ return { id, created: false, open: { object, id } };
231
+ }
232
+ const made = await ctx.ops.call("record.create", { object, values });
233
+ if (!made?.id)
234
+ throw new OpsCallError("conflict", "The draft was not made");
235
+ return { id: made.id, created: true, open: { object, id: made.id } };
236
+ }
237
+ export const draftProcedure = defineTool({
238
+ description: "Write a procedure's working copy from notes: a new one, or the draft of one that exists. " +
239
+ "Staff read only what a person publishes, so this changes nothing anyone reads until then; " +
240
+ "ask a person to publish it (record.publish waits for their approval). Who must read it and " +
241
+ "how often it is reviewed are set by a person. Name a role, not a person, in the words " +
242
+ "('the duty manager', not 'Sam').",
243
+ input: z.object({
244
+ ...draftCommon,
245
+ section: z.string().min(1).max(64).optional().describe("A section's id"),
246
+ }),
247
+ output: draftOut,
248
+ risk: "write",
249
+ permission: WRITE,
250
+ async handler(input, ctx) {
251
+ return draft(ctx, PROCEDURE, input.id, {
252
+ title: input.title,
253
+ ...(input.summary !== undefined ? { summary: input.summary } : {}),
254
+ ...(input.notes !== undefined ? { body: notesToRichText(input.notes) } : {}),
255
+ ...(input.section !== undefined ? { section: input.section } : {}),
256
+ });
257
+ },
258
+ });
259
+ export const draftHelpPage = defineTool({
260
+ description: "Write a help page's working copy from notes: a new one, or the draft of one that exists. " +
261
+ "The public reads only what a person publishes; ask a person to publish it (record.publish " +
262
+ "waits for their approval). Never put a customer's name or details in a help page drafted " +
263
+ "from their question.",
264
+ input: z.object({
265
+ ...draftCommon,
266
+ topic: z.string().min(1).max(64).optional().describe("A help topic's id"),
267
+ }),
268
+ output: draftOut,
269
+ risk: "write",
270
+ permission: WRITE,
271
+ async handler(input, ctx) {
272
+ return draft(ctx, HELP_PAGE, input.id, {
273
+ title: input.title,
274
+ ...(input.summary !== undefined ? { summary: input.summary } : {}),
275
+ ...(input.notes !== undefined ? { body: notesToRichText(input.notes) } : {}),
276
+ ...(input.topic !== undefined ? { topic: input.topic } : {}),
277
+ });
278
+ },
279
+ });
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
1
  {
2
2
  "name": "@composable-os/pack-knowledge",
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": "Knowledge: staff procedures read in their published wording and confirmed as read and understood, with a review reminder, and public help pages (stage 38, ADR 0706). Installed in every deployment of a product that lists it. 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
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "composableOs": {
30
+ "optional": true,
31
+ "label": "Knowledge",
32
+ "description": "Staff procedures everyone reads in their published wording and confirms with one tap, and help pages for your customers on your website."
33
+ },
34
+ "dependencies": {
35
+ "@composable-os/client": "0.1.0-beta.12",
36
+ "@composable-os/sdk": "0.1.0-beta.12"
37
+ }
38
+ }
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.