@joinskips/common 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +104 -2
- package/dist/chat/SkipsChat.d.ts +37 -0
- package/dist/chat/SkipsChat.js +103 -0
- package/dist/chat/client.d.ts +53 -0
- package/dist/chat/client.js +178 -0
- package/dist/chat/connection.d.ts +4 -0
- package/dist/chat/connection.js +6 -0
- package/dist/chat/generated/skips-app/index.d.ts +1 -0
- package/dist/chat/generated/skips-app/index.js +2 -0
- package/dist/chat/generated/skips-app/types.gen.d.ts +733 -0
- package/dist/chat/generated/skips-app/types.gen.js +2 -0
- package/dist/chat/index.d.ts +3 -0
- package/dist/chat/index.js +2 -0
- package/dist/chat/platform.d.ts +4 -0
- package/dist/chat/platform.js +46 -0
- package/dist/chat/session.d.ts +19 -0
- package/dist/chat/session.js +95 -0
- package/dist/chat/useSkipsChat.d.ts +79 -0
- package/dist/chat/useSkipsChat.js +507 -0
- package/dist/chat/verify.d.ts +35 -0
- package/dist/chat/verify.js +84 -0
- package/dist/elements/skips-elements.js +18521 -0
- package/dist/helpers/attachments.d.ts +12 -0
- package/dist/helpers/attachments.js +34 -0
- package/dist/helpers/cable.d.ts +34 -0
- package/dist/helpers/cable.js +88 -0
- package/dist/helpers/cableFrames.d.ts +40 -0
- package/dist/helpers/cableFrames.js +70 -0
- package/dist/helpers/cardNote.d.ts +79 -0
- package/dist/helpers/cardNote.js +152 -0
- package/dist/helpers/commandNote.d.ts +4 -0
- package/dist/helpers/commandNote.js +23 -0
- package/dist/helpers/index.d.ts +19 -0
- package/dist/helpers/index.js +19 -0
- package/dist/helpers/markdown.d.ts +47 -0
- package/dist/helpers/markdown.js +129 -0
- package/dist/helpers/medicationOrder.d.ts +333 -0
- package/dist/helpers/medicationOrder.js +383 -0
- package/dist/helpers/message.d.ts +46 -0
- package/dist/helpers/message.js +12 -0
- package/dist/helpers/noteView.d.ts +35 -0
- package/dist/helpers/noteView.js +65 -0
- package/dist/helpers/orderForm.d.ts +126 -0
- package/dist/helpers/orderForm.js +271 -0
- package/dist/helpers/ordersMenu.d.ts +56 -0
- package/dist/helpers/ordersMenu.js +129 -0
- package/dist/helpers/regimenChoice.d.ts +42 -0
- package/dist/helpers/regimenChoice.js +73 -0
- package/dist/helpers/scrollBox.d.ts +5 -0
- package/dist/helpers/scrollBox.js +15 -0
- package/dist/helpers/skipTyping.d.ts +26 -0
- package/dist/helpers/skipTyping.js +29 -0
- package/dist/helpers/skipsMessage.d.ts +55 -0
- package/dist/helpers/skipsMessage.js +130 -0
- package/dist/helpers/stageRefill.d.ts +33 -0
- package/dist/helpers/stageRefill.js +41 -0
- package/dist/helpers/thread.d.ts +54 -0
- package/dist/helpers/thread.js +122 -0
- package/dist/helpers/typing.d.ts +13 -0
- package/dist/helpers/typing.js +68 -0
- package/dist/helpers/usrxNote.d.ts +22 -0
- package/dist/helpers/usrxNote.js +26 -0
- package/dist/helpers/visibleArea.d.ts +15 -0
- package/dist/helpers/visibleArea.js +11 -0
- package/dist/helpers/welcome.d.ts +25 -0
- package/dist/helpers/welcome.js +51 -0
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useCardNoteActions.d.ts +82 -0
- package/dist/hooks/useCardNoteActions.js +210 -0
- package/dist/hooks/useCodeEntry.d.ts +6 -0
- package/dist/hooks/useCodeEntry.js +14 -0
- package/dist/hooks/useComposer.d.ts +20 -0
- package/dist/hooks/useComposer.js +40 -0
- package/dist/hooks/useOrderForm.d.ts +80 -0
- package/dist/hooks/useOrderForm.js +403 -0
- package/dist/hooks/useOrdersMenu.d.ts +114 -0
- package/dist/hooks/useOrdersMenu.js +337 -0
- package/dist/hooks/useRegimenChoice.d.ts +23 -0
- package/dist/hooks/useRegimenChoice.js +50 -0
- package/dist/hooks/useResendCountdown.d.ts +2 -0
- package/dist/hooks/useResendCountdown.js +19 -0
- package/dist/hooks/useStepField.d.ts +8 -0
- package/dist/hooks/useStepField.js +17 -0
- package/dist/hooks/useTypingCue.d.ts +11 -0
- package/dist/hooks/useTypingCue.js +41 -0
- package/dist/hooks/useWelcomeTyping.d.ts +2 -0
- package/dist/hooks/useWelcomeTyping.js +25 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/staff/CardNoteActions.d.ts +10 -0
- package/dist/staff/CardNoteActions.js +16 -0
- package/dist/staff/OrderFormNote.d.ts +8 -0
- package/dist/staff/OrderFormNote.js +8 -0
- package/dist/staff/OrdersMenuNote.d.ts +8 -0
- package/dist/staff/OrdersMenuNote.js +8 -0
- package/dist/staff/RegimenChoiceNote.d.ts +7 -0
- package/dist/staff/RegimenChoiceNote.js +8 -0
- package/dist/staff/features.d.ts +9 -0
- package/dist/staff/features.js +8 -0
- package/dist/staff/index.d.ts +11 -0
- package/dist/staff/index.js +7 -0
- package/dist/ui/Bubble.d.ts +5 -0
- package/dist/ui/Bubble.js +9 -0
- package/dist/ui/CardActions.d.ts +34 -0
- package/dist/ui/CardActions.js +28 -0
- package/dist/ui/Conversation.d.ts +69 -0
- package/dist/ui/Conversation.js +192 -0
- package/dist/ui/Markdown.d.ts +11 -0
- package/dist/ui/Markdown.js +51 -0
- package/dist/ui/NoteFold.d.ts +19 -0
- package/dist/ui/NoteFold.js +12 -0
- package/dist/ui/NoteViewControl.d.ts +19 -0
- package/dist/ui/NoteViewControl.js +20 -0
- package/dist/ui/OrderForm.d.ts +71 -0
- package/dist/ui/OrderForm.js +118 -0
- package/dist/ui/OrdersMenu.d.ts +71 -0
- package/dist/ui/OrdersMenu.js +128 -0
- package/dist/ui/RegimenChoice.d.ts +23 -0
- package/dist/ui/RegimenChoice.js +16 -0
- package/dist/ui/SkipAvatar.d.ts +7 -0
- package/dist/ui/SkipAvatar.js +8 -0
- package/dist/ui/StaffButton.d.ts +18 -0
- package/dist/ui/StaffButton.js +17 -0
- package/dist/ui/Welcome.d.ts +91 -0
- package/dist/ui/Welcome.js +128 -0
- package/dist/ui/index.d.ts +8 -0
- package/dist/ui/index.js +7 -0
- package/dist/ui/parts.d.ts +38 -0
- package/dist/ui/parts.js +6 -0
- package/package.json +115 -4
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ChatMessage, MessageChoice } from './message.ts';
|
|
2
|
+
export interface RawSender {
|
|
3
|
+
id?: number;
|
|
4
|
+
type?: string;
|
|
5
|
+
name?: string | null;
|
|
6
|
+
available_name?: string | null;
|
|
7
|
+
thumbnail?: string | null;
|
|
8
|
+
}
|
|
9
|
+
export interface RawMessageFile {
|
|
10
|
+
file_type?: string;
|
|
11
|
+
data_url?: string;
|
|
12
|
+
thumb_url?: string | null;
|
|
13
|
+
file_name?: string | null;
|
|
14
|
+
}
|
|
15
|
+
/** A message as the Skips App's API and its socket give it. The socket spells message_type as a word. */
|
|
16
|
+
export interface RawMessage {
|
|
17
|
+
id: number;
|
|
18
|
+
conversation_id?: number;
|
|
19
|
+
content: string | null;
|
|
20
|
+
message_type: number | string;
|
|
21
|
+
created_at: number | string;
|
|
22
|
+
private?: boolean;
|
|
23
|
+
sender?: RawSender | null;
|
|
24
|
+
attachments?: RawMessageFile[];
|
|
25
|
+
content_type?: unknown;
|
|
26
|
+
content_attributes?: unknown;
|
|
27
|
+
}
|
|
28
|
+
export type JsonObject = Record<string, unknown>;
|
|
29
|
+
export declare const isObject: (value: unknown) => value is JsonObject;
|
|
30
|
+
export declare const isNumber: (value: unknown) => value is number;
|
|
31
|
+
export declare const isString: (value: unknown) => value is string;
|
|
32
|
+
export declare const isNumberOrString: (value: unknown) => value is number | string;
|
|
33
|
+
export declare const isNullableString: (value: unknown) => value is string | null;
|
|
34
|
+
export declare const optional: (value: unknown, check: (value: unknown) => boolean) => boolean;
|
|
35
|
+
/** Whether a value from the Skips App has a message's shape, every field it carries checked. */
|
|
36
|
+
export declare function isRawMessage(value: unknown): value is RawMessage;
|
|
37
|
+
/** The first value that holds text, an empty string counting as none, or undefined when none does. */
|
|
38
|
+
export declare const firstFilled: (...values: (string | null | undefined)[]) => string | undefined;
|
|
39
|
+
/** A file's name from its Skips App address, which ends with it. */
|
|
40
|
+
export declare const fileNameIn: (url: string) => string;
|
|
41
|
+
export declare const fromContact: (raw: RawMessage) => boolean;
|
|
42
|
+
/** A Skips App time in milliseconds: the API gives seconds, the socket text. */
|
|
43
|
+
export declare const toMs: (value: number | string) => number;
|
|
44
|
+
export declare const CHOICE_CONTENT_TYPE = "input_select";
|
|
45
|
+
/**
|
|
46
|
+
* A question's buttons, with the answer already given if it is one of them. A message of another type, or one whose
|
|
47
|
+
* buttons cannot all be read, offers none: a button with no words could not be pressed knowingly. Nor does one with
|
|
48
|
+
* two buttons of the same value, since the answer kept could not say which was pressed.
|
|
49
|
+
*/
|
|
50
|
+
export declare function choiceFrom(contentType: unknown, attributes: unknown): MessageChoice | null;
|
|
51
|
+
/**
|
|
52
|
+
* The member's view of a message, or null when it is a private note or an activity the member does not see.
|
|
53
|
+
* careTeamName, the app's own words, names a care team line whose sender the Skips App leaves unnamed.
|
|
54
|
+
*/
|
|
55
|
+
export declare function toChatMessage(raw: RawMessage, careTeamName: string): ChatMessage | null;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// A Skips App message as its API and its live socket give it, and the member's view of it. Neither ever shows the
|
|
2
|
+
// member a private note or an internal activity line.
|
|
3
|
+
export const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
4
|
+
export const isNumber = (value) => typeof value === 'number';
|
|
5
|
+
export const isString = (value) => typeof value === 'string';
|
|
6
|
+
const isBoolean = (value) => typeof value === 'boolean';
|
|
7
|
+
export const isNumberOrString = (value) => isNumber(value) || isString(value);
|
|
8
|
+
export const isNullableString = (value) => value === null || isString(value);
|
|
9
|
+
// A field left out passes, and a field that is there must have its type.
|
|
10
|
+
export const optional = (value, check) => value === undefined || check(value);
|
|
11
|
+
function isRawSender(value) {
|
|
12
|
+
return (isObject(value) &&
|
|
13
|
+
optional(value.id, isNumber) &&
|
|
14
|
+
optional(value.type, isString) &&
|
|
15
|
+
optional(value.name, isNullableString) &&
|
|
16
|
+
optional(value.available_name, isNullableString) &&
|
|
17
|
+
optional(value.thumbnail, isNullableString));
|
|
18
|
+
}
|
|
19
|
+
function isRawMessageFile(value) {
|
|
20
|
+
return (isObject(value) &&
|
|
21
|
+
optional(value.file_type, isString) &&
|
|
22
|
+
optional(value.data_url, isString) &&
|
|
23
|
+
optional(value.thumb_url, isNullableString) &&
|
|
24
|
+
optional(value.file_name, isNullableString));
|
|
25
|
+
}
|
|
26
|
+
/** Whether a value from the Skips App has a message's shape, every field it carries checked. */
|
|
27
|
+
export function isRawMessage(value) {
|
|
28
|
+
return (isObject(value) &&
|
|
29
|
+
isNumber(value.id) &&
|
|
30
|
+
optional(value.conversation_id, isNumber) &&
|
|
31
|
+
isNullableString(value.content) &&
|
|
32
|
+
isNumberOrString(value.message_type) &&
|
|
33
|
+
isNumberOrString(value.created_at) &&
|
|
34
|
+
optional(value.private, isBoolean) &&
|
|
35
|
+
optional(value.sender, (sender) => sender === null || isRawSender(sender)) &&
|
|
36
|
+
optional(value.attachments, (files) => Array.isArray(files) && files.every(isRawMessageFile)));
|
|
37
|
+
}
|
|
38
|
+
/** The first value that holds text, an empty string counting as none, or undefined when none does. */
|
|
39
|
+
export const firstFilled = (...values) => values.find((value) => Boolean(value));
|
|
40
|
+
/** A file's name from its Skips App address, which ends with it. */
|
|
41
|
+
export const fileNameIn = (url) => {
|
|
42
|
+
let path = url;
|
|
43
|
+
try {
|
|
44
|
+
path = new URL(url).pathname;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// Not a full address: its last part is still the best name there is.
|
|
48
|
+
}
|
|
49
|
+
const name = path.slice(path.lastIndexOf('/') + 1);
|
|
50
|
+
try {
|
|
51
|
+
return decodeURIComponent(name);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return name;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
// message_type 0 is the contact's, 2 an activity. The socket sends the names instead of the numbers.
|
|
58
|
+
export const fromContact = (raw) => raw.sender?.type === 'contact' || raw.message_type === 0 || raw.message_type === 'incoming';
|
|
59
|
+
const isActivity = (raw) => raw.message_type === 2 || raw.message_type === 'activity';
|
|
60
|
+
// Of the Skips App's activity lines, only a conversation closing or reopening is the member's business.
|
|
61
|
+
// The Skips App writes "Conversation was marked resolved by …" and "Conversation was reopened by …".
|
|
62
|
+
const NOTICE = /conversation was (marked )?(resolved|reopened)|(resolved|reopened) the conversation/i;
|
|
63
|
+
/** A Skips App time in milliseconds: the API gives seconds, the socket text. */
|
|
64
|
+
export const toMs = (value) => (typeof value === 'number' ? value * 1000 : Date.parse(value));
|
|
65
|
+
// The Skips App's question answered with one of its buttons.
|
|
66
|
+
export const CHOICE_CONTENT_TYPE = 'input_select';
|
|
67
|
+
function isChoiceItem(value) {
|
|
68
|
+
return (isObject(value) &&
|
|
69
|
+
isString(value.title) &&
|
|
70
|
+
value.title.trim().length > 0 &&
|
|
71
|
+
isString(value.value) &&
|
|
72
|
+
value.value.length > 0);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A question's buttons, with the answer already given if it is one of them. A message of another type, or one whose
|
|
76
|
+
* buttons cannot all be read, offers none: a button with no words could not be pressed knowingly. Nor does one with
|
|
77
|
+
* two buttons of the same value, since the answer kept could not say which was pressed.
|
|
78
|
+
*/
|
|
79
|
+
export function choiceFrom(contentType, attributes) {
|
|
80
|
+
if (contentType !== CHOICE_CONTENT_TYPE || !isObject(attributes))
|
|
81
|
+
return null;
|
|
82
|
+
const { items, submitted_values: submitted } = attributes;
|
|
83
|
+
if (!Array.isArray(items) || items.length === 0 || !items.every(isChoiceItem))
|
|
84
|
+
return null;
|
|
85
|
+
if (new Set(items.map((item) => item.value)).size !== items.length)
|
|
86
|
+
return null;
|
|
87
|
+
const first = Array.isArray(submitted) ? submitted[0] : null;
|
|
88
|
+
const given = isObject(first) ? first.value : null;
|
|
89
|
+
const chosen = items.find((item) => item.value === given)?.value ?? null;
|
|
90
|
+
return { items, chosen };
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The member's view of a message, or null when it is a private note or an activity the member does not see.
|
|
94
|
+
* careTeamName, the app's own words, names a care team line whose sender the Skips App leaves unnamed.
|
|
95
|
+
*/
|
|
96
|
+
export function toChatMessage(raw, careTeamName) {
|
|
97
|
+
if (raw.private)
|
|
98
|
+
return null;
|
|
99
|
+
const notice = isActivity(raw);
|
|
100
|
+
if (notice && !NOTICE.test(raw.content ?? ''))
|
|
101
|
+
return null;
|
|
102
|
+
const mine = !notice && fromContact(raw);
|
|
103
|
+
const choice = mine || notice ? null : choiceFrom(raw.content_type, raw.content_attributes);
|
|
104
|
+
return {
|
|
105
|
+
...(choice === null ? {} : { choice }),
|
|
106
|
+
id: raw.id,
|
|
107
|
+
text: raw.content ?? '',
|
|
108
|
+
sentAt: toMs(raw.created_at),
|
|
109
|
+
mine,
|
|
110
|
+
notice,
|
|
111
|
+
author: mine || notice
|
|
112
|
+
? null
|
|
113
|
+
: {
|
|
114
|
+
id: raw.sender?.id ?? null,
|
|
115
|
+
name: firstFilled(raw.sender?.available_name, raw.sender?.name) ?? careTeamName,
|
|
116
|
+
avatarUrl: firstFilled(raw.sender?.thumbnail) ?? null,
|
|
117
|
+
bot: raw.sender?.type === 'agent_bot',
|
|
118
|
+
},
|
|
119
|
+
attachments: (raw.attachments ?? []).flatMap((a) => a.data_url
|
|
120
|
+
? [
|
|
121
|
+
{
|
|
122
|
+
kind: a.file_type === 'image' ? 'image' : 'file',
|
|
123
|
+
url: a.data_url,
|
|
124
|
+
thumbUrl: a.thumb_url ?? null,
|
|
125
|
+
name: firstFilled(a.file_name) ?? fileNameIn(a.data_url),
|
|
126
|
+
},
|
|
127
|
+
]
|
|
128
|
+
: []),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type PastOrder, type UsrxOrder } from './medicationOrder.ts';
|
|
2
|
+
import { type ChartsToPick, type DrugChoices } from './orderForm.ts';
|
|
3
|
+
export interface StageRefillSource {
|
|
4
|
+
conversationId: number;
|
|
5
|
+
/** Treat's dose, frequency and dispense units for a drug, through treat_lookups. */
|
|
6
|
+
readChoices: (conversationId: number, ddid: string) => Promise<DrugChoices>;
|
|
7
|
+
/** The Treat charts that fit the conversation's patient, as treat_lookups answers. */
|
|
8
|
+
readCharts: (conversationId: number) => Promise<unknown>;
|
|
9
|
+
/** Posts the order note, a private note carrying content_attributes.usrx_order. */
|
|
10
|
+
postOrder: (conversationId: number, note: string, order: UsrxOrder) => Promise<unknown>;
|
|
11
|
+
/** False once the app moved to another conversation, which a slow refill leaves alone. */
|
|
12
|
+
isCurrent: () => boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* posted: the order note went. openForm: the form opens filled, to show what is missing. pickChart: the form opens on
|
|
16
|
+
* the charts to pick from. left: the conversation changed meanwhile.
|
|
17
|
+
*/
|
|
18
|
+
export type StageOutcome = 'posted' | 'openForm' | 'pickChart' | 'left';
|
|
19
|
+
export declare const POSTED: StageOutcome;
|
|
20
|
+
export declare const OPEN_FORM: StageOutcome;
|
|
21
|
+
export declare const PICK_CHART: StageOutcome;
|
|
22
|
+
export declare const LEFT: StageOutcome;
|
|
23
|
+
export interface StageRefillResult {
|
|
24
|
+
outcome: StageOutcome;
|
|
25
|
+
/** The charts to pick from, for pickChart. */
|
|
26
|
+
charts: ChartsToPick | null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Stages a refill of a sent order to the pharmacy it went to. A failed read of the drug's choices opens the form, which
|
|
30
|
+
* reads them again and says why. A failed read of the charts posts without one, and usrx then asks for /treat-patient.
|
|
31
|
+
* A failed post rejects.
|
|
32
|
+
*/
|
|
33
|
+
export declare function stageRefill(order: PastOrder, readsDone: Promise<void>, source: StageRefillSource): Promise<StageRefillResult>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// A refill to the same pharmacy posts the order note with no form shown, as patch 180's stage did, and asks for the
|
|
2
|
+
// form only for what the sent order lacks or for a chart to pick.
|
|
3
|
+
import { buildOrder, noteText } from "./medicationOrder.js";
|
|
4
|
+
import { cardOfOrder, chartsToPick, newOrderItems, newPicker, reorderItem, } from "./orderForm.js";
|
|
5
|
+
export const POSTED = 'posted';
|
|
6
|
+
export const OPEN_FORM = 'openForm';
|
|
7
|
+
export const PICK_CHART = 'pickChart';
|
|
8
|
+
export const LEFT = 'left';
|
|
9
|
+
const result = (outcome, charts = null) => ({ outcome, charts });
|
|
10
|
+
/**
|
|
11
|
+
* Stages a refill of a sent order to the pharmacy it went to. A failed read of the drug's choices opens the form, which
|
|
12
|
+
* reads them again and says why. A failed read of the charts posts without one, and usrx then asks for /treat-patient.
|
|
13
|
+
* A failed post rejects.
|
|
14
|
+
*/
|
|
15
|
+
export async function stageRefill(order, readsDone, source) {
|
|
16
|
+
const { conversationId } = source;
|
|
17
|
+
const card = cardOfOrder(order);
|
|
18
|
+
let item;
|
|
19
|
+
try {
|
|
20
|
+
await readsDone;
|
|
21
|
+
const choices = await source.readChoices(conversationId, card.ddid);
|
|
22
|
+
item = { ...reorderItem(card, card.uid), choices };
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return result(source.isCurrent() ? OPEN_FORM : LEFT);
|
|
26
|
+
}
|
|
27
|
+
if (!source.isCurrent())
|
|
28
|
+
return result(LEFT);
|
|
29
|
+
const picker = { ...newPicker(), pharmacy: card.pharmacy ?? null };
|
|
30
|
+
const items = newOrderItems([{ key: item.key, rounds: [item], picker }]);
|
|
31
|
+
if (!items)
|
|
32
|
+
return result(OPEN_FORM);
|
|
33
|
+
const charts = chartsToPick(await source.readCharts(conversationId).catch(() => null));
|
|
34
|
+
if (!source.isCurrent())
|
|
35
|
+
return result(LEFT);
|
|
36
|
+
if (charts.charts.length > 0)
|
|
37
|
+
return result(PICK_CHART, charts);
|
|
38
|
+
const orderNote = buildOrder({ items });
|
|
39
|
+
await source.postOrder(conversationId, noteText(orderNote), orderNote);
|
|
40
|
+
return result(POSTED);
|
|
41
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type ChatAuthor, type ChatMessage, type ChoiceItem, type PickedFile } from './message.ts';
|
|
2
|
+
export type Delivery = 'sent' | 'pending' | 'failed';
|
|
3
|
+
export interface ThreadMessage extends ChatMessage {
|
|
4
|
+
delivery: Delivery;
|
|
5
|
+
localId?: number;
|
|
6
|
+
file?: PickedFile;
|
|
7
|
+
}
|
|
8
|
+
export interface Thread {
|
|
9
|
+
messages: ThreadMessage[];
|
|
10
|
+
}
|
|
11
|
+
export declare const EMPTY_THREAD: Thread;
|
|
12
|
+
export declare function pendingId(): number;
|
|
13
|
+
export declare function withPending(thread: Thread, id: number, text: string, sentAt: number, file?: PickedFile): Thread;
|
|
14
|
+
/** The send answered: the pending line becomes the message the Skips App stored, or goes when the answer has none. */
|
|
15
|
+
export declare function withSent(thread: Thread, pending: number, message: ChatMessage | null): Thread;
|
|
16
|
+
/** Marks a pending line failed, or pending again as it is retried. */
|
|
17
|
+
export declare function withFailed(thread: Thread, pending: number, failed?: boolean): Thread;
|
|
18
|
+
export declare function withoutMessage(thread: Thread, id: number): Thread;
|
|
19
|
+
/** The ids of the thread's messages the Skips App holds, oldest first: pending and failed lines have no place in its pages. */
|
|
20
|
+
export declare const storedIds: (thread: Thread) => number[];
|
|
21
|
+
/**
|
|
22
|
+
* The question from Skip whose buttons can still be pressed: unanswered, with nothing written after it. A line from the
|
|
23
|
+
* care team after it leaves it open, since what asked it still waits for the press.
|
|
24
|
+
*/
|
|
25
|
+
export declare function openQuestion(messages: readonly ThreadMessage[]): OpenQuestion | undefined;
|
|
26
|
+
export interface OpenQuestion {
|
|
27
|
+
message: ThreadMessage;
|
|
28
|
+
items: ChoiceItem[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A message from a page or the socket, added once. The socket can echo the member's line before the send that stored
|
|
32
|
+
* it answers, and the echo then takes the place of the earliest pending line with its text instead of showing twice.
|
|
33
|
+
*/
|
|
34
|
+
export declare function withReceived(thread: Thread, message: ChatMessage): Thread;
|
|
35
|
+
export declare function withReceivedAll(thread: Thread, messages: ChatMessage[]): Thread;
|
|
36
|
+
export declare const CLUSTER_GAP_MS = 120000;
|
|
37
|
+
/** Who a run is from: the member, Skip, or a person on the care team. */
|
|
38
|
+
export type RunSender = 'mine' | 'skip' | ChatAuthor;
|
|
39
|
+
export interface NoticeItem {
|
|
40
|
+
kind: 'notice';
|
|
41
|
+
message: ThreadMessage;
|
|
42
|
+
}
|
|
43
|
+
export interface UnsentItem {
|
|
44
|
+
kind: 'unsent';
|
|
45
|
+
message: ThreadMessage;
|
|
46
|
+
}
|
|
47
|
+
export interface RunItem {
|
|
48
|
+
kind: 'run';
|
|
49
|
+
sender: RunSender;
|
|
50
|
+
messages: ThreadMessage[];
|
|
51
|
+
}
|
|
52
|
+
export type ThreadItem = NoticeItem | UnsentItem | RunItem;
|
|
53
|
+
/** Notices and the member's unsent lines on their own, every other line in runs by sender, in order. */
|
|
54
|
+
export declare function groupThread(messages: ThreadMessage[], skipUserId: number | null): ThreadItem[];
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// One conversation as the chat shows it: messages in time order, each sent, pending or failed, with no duplicates
|
|
2
|
+
// when a page and the socket bring the same message, or when the socket echoes what the member just sent.
|
|
3
|
+
import { isSkip, } from "./message.js";
|
|
4
|
+
export const EMPTY_THREAD = { messages: [] };
|
|
5
|
+
// Pending lines get negative ids, which never collide with the Skips App's.
|
|
6
|
+
let nextPendingId = -1;
|
|
7
|
+
export function pendingId() {
|
|
8
|
+
const id = nextPendingId;
|
|
9
|
+
nextPendingId -= 1;
|
|
10
|
+
return id;
|
|
11
|
+
}
|
|
12
|
+
function ordered(messages) {
|
|
13
|
+
return { messages: [...messages].sort((a, b) => a.sentAt - b.sentAt || a.id - b.id) };
|
|
14
|
+
}
|
|
15
|
+
export function withPending(thread, id, text, sentAt, file) {
|
|
16
|
+
const line = {
|
|
17
|
+
id,
|
|
18
|
+
text,
|
|
19
|
+
sentAt,
|
|
20
|
+
mine: true,
|
|
21
|
+
notice: false,
|
|
22
|
+
author: null,
|
|
23
|
+
attachments: [],
|
|
24
|
+
delivery: 'pending',
|
|
25
|
+
};
|
|
26
|
+
return ordered([...thread.messages, file ? { ...line, file } : line]);
|
|
27
|
+
}
|
|
28
|
+
/** The send answered: the pending line becomes the message the Skips App stored, or goes when the answer has none. */
|
|
29
|
+
export function withSent(thread, pending, message) {
|
|
30
|
+
const rest = thread.messages.filter((m) => m.id !== pending && m.id !== message?.id);
|
|
31
|
+
return message ? ordered([...rest, { ...message, delivery: 'sent', localId: pending }]) : { messages: rest };
|
|
32
|
+
}
|
|
33
|
+
/** Marks a pending line failed, or pending again as it is retried. */
|
|
34
|
+
export function withFailed(thread, pending, failed = true) {
|
|
35
|
+
return {
|
|
36
|
+
messages: thread.messages.map((m) => (m.id === pending ? { ...m, delivery: failed ? 'failed' : 'pending' } : m)),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function withoutMessage(thread, id) {
|
|
40
|
+
return { messages: thread.messages.filter((m) => m.id !== id) };
|
|
41
|
+
}
|
|
42
|
+
/** The ids of the thread's messages the Skips App holds, oldest first: pending and failed lines have no place in its pages. */
|
|
43
|
+
export const storedIds = (thread) => thread.messages.filter((message) => message.delivery === 'sent').map((message) => message.id);
|
|
44
|
+
/**
|
|
45
|
+
* The question from Skip whose buttons can still be pressed: unanswered, with nothing written after it. A line from the
|
|
46
|
+
* care team after it leaves it open, since what asked it still waits for the press.
|
|
47
|
+
*/
|
|
48
|
+
export function openQuestion(messages) {
|
|
49
|
+
for (const message of [...messages].reverse()) {
|
|
50
|
+
if (message.mine)
|
|
51
|
+
return undefined;
|
|
52
|
+
if (message.choice)
|
|
53
|
+
return message.choice.chosen === null ? { message, items: message.choice.items } : undefined;
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
// An answer is never taken back, so a copy of a question without one keeps the answer the chat has.
|
|
58
|
+
function keptChoice(known, incoming) {
|
|
59
|
+
if (incoming === undefined)
|
|
60
|
+
return known;
|
|
61
|
+
return incoming.chosen === null && known?.chosen != null ? known : incoming;
|
|
62
|
+
}
|
|
63
|
+
// A later copy of a message the chat has, such as an edit or an answered question, takes its place.
|
|
64
|
+
function updated(known, message) {
|
|
65
|
+
const { choice: _incoming, ...rest } = message;
|
|
66
|
+
const choice = keptChoice(known.choice, message.choice);
|
|
67
|
+
return {
|
|
68
|
+
...rest,
|
|
69
|
+
delivery: 'sent',
|
|
70
|
+
...(known.localId === undefined ? {} : { localId: known.localId }),
|
|
71
|
+
...(choice === undefined ? {} : { choice }),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A message from a page or the socket, added once. The socket can echo the member's line before the send that stored
|
|
76
|
+
* it answers, and the echo then takes the place of the earliest pending line with its text instead of showing twice.
|
|
77
|
+
*/
|
|
78
|
+
export function withReceived(thread, message) {
|
|
79
|
+
const known = thread.messages.find((m) => m.id === message.id);
|
|
80
|
+
if (known)
|
|
81
|
+
return { messages: thread.messages.map((m) => (m === known ? updated(known, message) : m)) };
|
|
82
|
+
const echoed = message.mine
|
|
83
|
+
? thread.messages.find((m) => m.delivery === 'pending' && m.text === message.text)
|
|
84
|
+
: undefined;
|
|
85
|
+
if (echoed)
|
|
86
|
+
return withSent(thread, echoed.id, message);
|
|
87
|
+
return ordered([...thread.messages, { ...message, delivery: 'sent' }]);
|
|
88
|
+
}
|
|
89
|
+
export function withReceivedAll(thread, messages) {
|
|
90
|
+
return messages.reduce(withReceived, thread);
|
|
91
|
+
}
|
|
92
|
+
// Lines from the same sender within two minutes of the one before read as one run.
|
|
93
|
+
export const CLUSTER_GAP_MS = 120_000;
|
|
94
|
+
const senderKey = (sender) => (typeof sender === 'string' ? sender : `team:${sender.name}`);
|
|
95
|
+
/** Notices and the member's unsent lines on their own, every other line in runs by sender, in order. */
|
|
96
|
+
export function groupThread(messages, skipUserId) {
|
|
97
|
+
const senderOf = (m) => {
|
|
98
|
+
if (m.mine)
|
|
99
|
+
return 'mine';
|
|
100
|
+
return m.author && !isSkip(m, skipUserId) ? m.author : 'skip';
|
|
101
|
+
};
|
|
102
|
+
const items = [];
|
|
103
|
+
let run = null;
|
|
104
|
+
let runEnd = 0;
|
|
105
|
+
for (const message of messages) {
|
|
106
|
+
if (message.notice || message.delivery !== 'sent') {
|
|
107
|
+
items.push({ kind: message.notice ? 'notice' : 'unsent', message });
|
|
108
|
+
run = null;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const sender = senderOf(message);
|
|
112
|
+
if (run && senderKey(sender) === senderKey(run.sender) && message.sentAt - runEnd <= CLUSTER_GAP_MS) {
|
|
113
|
+
run.messages.push(message);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
run = { kind: 'run', sender, messages: [message] };
|
|
117
|
+
items.push(run);
|
|
118
|
+
}
|
|
119
|
+
runEnd = message.sentAt;
|
|
120
|
+
}
|
|
121
|
+
return items;
|
|
122
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const TYPING_IDLE_MS = 3000;
|
|
2
|
+
export declare const TYPING_CLEAR_MS = 10000;
|
|
3
|
+
/** Tells the care team the member is typing. Call `typed` on each change of the text and `stop` when it is sent. */
|
|
4
|
+
export declare function createTypingSignal(send: (typing: boolean) => void): {
|
|
5
|
+
typed(text: string): void;
|
|
6
|
+
stop: () => void;
|
|
7
|
+
};
|
|
8
|
+
/** Who on the care team is typing, each cleared after TYPING_CLEAR_MS without news. */
|
|
9
|
+
export declare function createTypingWatch(onChange: (names: string[]) => void): {
|
|
10
|
+
update(name: string, typing: boolean): void;
|
|
11
|
+
clear: (name: string) => void;
|
|
12
|
+
dispose(): void;
|
|
13
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Typing, both ways. The member's typing goes out once when it starts and once when it stops, never per keystroke.
|
|
2
|
+
// The care team's typing clears itself if the stop never arrives.
|
|
3
|
+
export const TYPING_IDLE_MS = 3_000;
|
|
4
|
+
export const TYPING_CLEAR_MS = 10_000;
|
|
5
|
+
/** Tells the care team the member is typing. Call `typed` on each change of the text and `stop` when it is sent. */
|
|
6
|
+
export function createTypingSignal(send) {
|
|
7
|
+
let typing = false;
|
|
8
|
+
let idle = null;
|
|
9
|
+
const stop = () => {
|
|
10
|
+
if (idle)
|
|
11
|
+
clearTimeout(idle);
|
|
12
|
+
idle = null;
|
|
13
|
+
if (typing)
|
|
14
|
+
send((typing = false));
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
typed(text) {
|
|
18
|
+
if (!text.trim()) {
|
|
19
|
+
stop();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!typing)
|
|
23
|
+
send((typing = true));
|
|
24
|
+
if (idle)
|
|
25
|
+
clearTimeout(idle);
|
|
26
|
+
idle = setTimeout(stop, TYPING_IDLE_MS);
|
|
27
|
+
},
|
|
28
|
+
stop,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** Who on the care team is typing, each cleared after TYPING_CLEAR_MS without news. */
|
|
32
|
+
export function createTypingWatch(onChange) {
|
|
33
|
+
const timers = new Map();
|
|
34
|
+
const publish = () => {
|
|
35
|
+
onChange([...timers.keys()]);
|
|
36
|
+
};
|
|
37
|
+
const clear = (name) => {
|
|
38
|
+
const timer = timers.get(name);
|
|
39
|
+
if (!timer)
|
|
40
|
+
return;
|
|
41
|
+
clearTimeout(timer);
|
|
42
|
+
timers.delete(name);
|
|
43
|
+
publish();
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
update(name, typing) {
|
|
47
|
+
if (!typing) {
|
|
48
|
+
clear(name);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const timer = timers.get(name);
|
|
52
|
+
if (timer)
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
timers.set(name, setTimeout(() => {
|
|
55
|
+
clear(name);
|
|
56
|
+
}, TYPING_CLEAR_MS));
|
|
57
|
+
if (!timer)
|
|
58
|
+
publish();
|
|
59
|
+
},
|
|
60
|
+
// A message from someone ends their typing.
|
|
61
|
+
clear,
|
|
62
|
+
dispose() {
|
|
63
|
+
for (const timer of timers.values())
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
timers.clear();
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const USRX_NOTE_TAG = "[usrx]";
|
|
2
|
+
/**
|
|
3
|
+
* A message in a Skips App conversation as the staff features read it, keys in camelCase as both apps hold them.
|
|
4
|
+
* contentAttributes is what the Skips App sent, read through the helpers that check it. A message still sending has a
|
|
5
|
+
* uuid for its id.
|
|
6
|
+
*/
|
|
7
|
+
export interface StaffMessage {
|
|
8
|
+
id: number | string;
|
|
9
|
+
content?: string | null;
|
|
10
|
+
private?: boolean;
|
|
11
|
+
/** In seconds, as the Skips App sends it. */
|
|
12
|
+
createdAt: number;
|
|
13
|
+
contentAttributes?: unknown;
|
|
14
|
+
}
|
|
15
|
+
/** One of a note's content attributes, such as usrxSubmitted, or undefined when it has none. */
|
|
16
|
+
export declare function noteAttribute(message: StaffMessage, name: string): unknown;
|
|
17
|
+
/** The messages after a note, oldest first, whatever order they came in. A message still sending is left out. */
|
|
18
|
+
export declare function messagesAfter(messages: readonly StaffMessage[] | undefined, noteId: number): StaffMessage[];
|
|
19
|
+
export declare function withoutLeadingMarks(content: string | null | undefined): string;
|
|
20
|
+
export declare function isUsrxNote(content: string | null | undefined): boolean;
|
|
21
|
+
/** A Treat card uid as usrx writes it in a note, a number or a string, read as a string. */
|
|
22
|
+
export declare function uidOf(value: unknown): string | null;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// The private notes the Skips App's staff features read: usrx's notes, which carry their content in
|
|
2
|
+
// content_attributes, and the commands staff post for usrx to act on.
|
|
3
|
+
import { isNumberOrString, isObject } from "./skipsMessage.js";
|
|
4
|
+
// usrx starts each private note it posts with this (joinskips/usrx, skips-app/notes.ts).
|
|
5
|
+
export const USRX_NOTE_TAG = '[usrx]';
|
|
6
|
+
/** One of a note's content attributes, such as usrxSubmitted, or undefined when it has none. */
|
|
7
|
+
export function noteAttribute(message, name) {
|
|
8
|
+
return isObject(message.contentAttributes) ? message.contentAttributes[name] : undefined;
|
|
9
|
+
}
|
|
10
|
+
/** The messages after a note, oldest first, whatever order they came in. A message still sending is left out. */
|
|
11
|
+
export function messagesAfter(messages, noteId) {
|
|
12
|
+
return (messages ?? [])
|
|
13
|
+
.filter((message) => typeof message.id === 'number' && message.id > noteId)
|
|
14
|
+
.sort((a, b) => Number(a.id) - Number(b.id));
|
|
15
|
+
}
|
|
16
|
+
// usrx's receiver reads a command through the markdown a provider may put around it (lambda/receiver.ts).
|
|
17
|
+
export function withoutLeadingMarks(content) {
|
|
18
|
+
return (content ?? '').replace(/^[\s*_`]+/, '');
|
|
19
|
+
}
|
|
20
|
+
export function isUsrxNote(content) {
|
|
21
|
+
return (content ?? '').startsWith(USRX_NOTE_TAG);
|
|
22
|
+
}
|
|
23
|
+
/** A Treat card uid as usrx writes it in a note, a number or a string, read as a string. */
|
|
24
|
+
export function uidOf(value) {
|
|
25
|
+
return isNumberOrString(value) && value !== '' ? String(value) : null;
|
|
26
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface VisualViewportBox {
|
|
2
|
+
offsetTop: number;
|
|
3
|
+
height: number;
|
|
4
|
+
scale: number;
|
|
5
|
+
}
|
|
6
|
+
export interface Fit {
|
|
7
|
+
top: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const UNZOOMED_WITHIN = 0.01;
|
|
11
|
+
/**
|
|
12
|
+
* Where the chat sits so that it fills exactly what the visitor can see. A visitor who has zoomed in is reading one
|
|
13
|
+
* part of the chat, so it keeps the size it has and they move around it.
|
|
14
|
+
*/
|
|
15
|
+
export declare function fitVisibleArea(viewport: VisualViewportBox): Fit | undefined;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// A browser reports a scale a hair off 1 on a page nobody zoomed.
|
|
2
|
+
export const UNZOOMED_WITHIN = 0.01;
|
|
3
|
+
/**
|
|
4
|
+
* Where the chat sits so that it fills exactly what the visitor can see. A visitor who has zoomed in is reading one
|
|
5
|
+
* part of the chat, so it keeps the size it has and they move around it.
|
|
6
|
+
*/
|
|
7
|
+
export function fitVisibleArea(viewport) {
|
|
8
|
+
if (Math.abs(viewport.scale - 1) > UNZOOMED_WITHIN)
|
|
9
|
+
return undefined;
|
|
10
|
+
return { top: viewport.offsetTop, height: viewport.height };
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type WelcomeStep = 'choosing' | 'email' | 'code' | 'name' | 'through';
|
|
2
|
+
export declare const NAME_MAX_LENGTH = 100;
|
|
3
|
+
export declare const EMAIL_MAX_LENGTH = 254;
|
|
4
|
+
export declare function isEmail(email: string): boolean;
|
|
5
|
+
export declare function isName(name: string): boolean;
|
|
6
|
+
export declare function givenEmail(typed: string): string | null;
|
|
7
|
+
export declare function givenName(typed: string): string | null;
|
|
8
|
+
export interface WelcomeTypingTimes {
|
|
9
|
+
leastMs: number;
|
|
10
|
+
mostMs: number;
|
|
11
|
+
}
|
|
12
|
+
export declare const WELCOME_TYPING_TIMES: WelcomeTypingTimes;
|
|
13
|
+
/**
|
|
14
|
+
* Whether reaching this step has Skip say a new line. Going back a step, as Change email does, or staying on one,
|
|
15
|
+
* as a new code does, says nothing new, and the conversation after the welcome is the Skips App's to type.
|
|
16
|
+
*/
|
|
17
|
+
export declare function typesBefore(from: WelcomeStep | null, to: WelcomeStep): boolean;
|
|
18
|
+
/** What the code field holds: its digits only, never more than the code has. A pasted "246 810" keeps its digits. */
|
|
19
|
+
export declare function codeDigits(typed: string, length: number): string;
|
|
20
|
+
/** Whether a change to the code field has just made it a whole code, so it goes on with no button pressed. */
|
|
21
|
+
export declare function completesCode(digits: string, before: string, length: number): boolean;
|
|
22
|
+
export declare const RESEND_AFTER_MS = 30000;
|
|
23
|
+
/** The wait before a new code, as a clock: 0:30, then 0:09. */
|
|
24
|
+
export declare function resendClock(seconds: number): string;
|
|
25
|
+
export declare function typingMs(times: WelcomeTypingTimes, random: number): number;
|