@g9tm/aiassistant 0.0.0-stage → 1.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 +36 -2
- package/dist/admin.d.ts +247 -0
- package/dist/admin.js +295 -0
- package/dist/api.d.ts +81 -0
- package/dist/api.js +134 -0
- package/dist/assistant.d.ts +92 -0
- package/dist/assistant.js +265 -0
- package/dist/blocks.d.ts +106 -0
- package/dist/blocks.js +119 -0
- package/dist/conversation.d.ts +39 -0
- package/dist/conversation.js +80 -0
- package/dist/csv.d.ts +10 -0
- package/dist/csv.js +27 -0
- package/dist/fa.d.ts +13 -0
- package/dist/fa.js +61 -0
- package/dist/history.d.ts +20 -0
- package/dist/history.js +39 -0
- package/dist/host.d.ts +87 -0
- package/dist/host.js +31 -0
- package/dist/i18n.d.ts +10 -0
- package/dist/i18n.js +176 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +16 -0
- package/dist/pages.d.ts +19 -0
- package/dist/pages.js +19 -0
- package/dist/plan.d.ts +60 -0
- package/dist/plan.js +117 -0
- package/dist/reveal.d.ts +20 -0
- package/dist/reveal.js +46 -0
- package/dist/routine.d.ts +35 -0
- package/dist/routine.js +53 -0
- package/dist/run.d.ts +64 -0
- package/dist/run.js +126 -0
- package/dist/state.d.ts +45 -0
- package/dist/state.js +78 -0
- package/dist/text.d.ts +35 -0
- package/dist/text.js +115 -0
- package/dist/transcript.d.ts +4 -0
- package/dist/transcript.js +26 -0
- package/dist/transport.d.ts +51 -0
- package/dist/transport.js +153 -0
- package/dist/types.d.ts +22 -0
- package/dist/types.js +28 -0
- package/package.json +39 -3
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/** The library's texts in Persian and English; every key can be overridden by the host (rule 5). */
|
|
2
|
+
import { faDigits } from "./fa.js";
|
|
3
|
+
const fa = {
|
|
4
|
+
"assistant.name": "دستیار",
|
|
5
|
+
"composer.placeholder": "بپرسید یا بخواهید…",
|
|
6
|
+
"composer.send": "بفرست",
|
|
7
|
+
"composer.stop": "توقف",
|
|
8
|
+
"composer.attach": "پیوست فایل",
|
|
9
|
+
"composer.mic": "گفتن",
|
|
10
|
+
"work.title": "در حال کار",
|
|
11
|
+
"work.long": "کار ادامه دارد…",
|
|
12
|
+
"work.elapsed": "{seconds} ثانیه",
|
|
13
|
+
"stage.routing": "مسیر",
|
|
14
|
+
"stage.reading": "خواندن",
|
|
15
|
+
"stage.thinking": "فکر",
|
|
16
|
+
"stage.tooling": "ابزار",
|
|
17
|
+
"stage.checking": "بررسی",
|
|
18
|
+
"stage.waiting": "منتظر شما",
|
|
19
|
+
"stage.running": "اجرا",
|
|
20
|
+
"stage.role": "{role}",
|
|
21
|
+
"provenance.rule": "قاعده",
|
|
22
|
+
"provenance.learned": "آموخته",
|
|
23
|
+
"provenance.model": "هوش مصنوعی",
|
|
24
|
+
"provenance.fallback": "بدون هوش مصنوعی",
|
|
25
|
+
"provenance.cached": "نتیجهٔ نگهداشته",
|
|
26
|
+
"card.confirm": "تأیید و اجرا",
|
|
27
|
+
"card.refuse": "نه",
|
|
28
|
+
"card.code": "رمز تأیید",
|
|
29
|
+
"card.irreversible": "برگشتناپذیر",
|
|
30
|
+
"card.tainted": "این کار بعد از خواندن متن دیگران پیشنهاد شده است.",
|
|
31
|
+
"card.readonly_routine": "این تغییرها انجام میشود",
|
|
32
|
+
"card.tries_left": "{count} بار دیگر",
|
|
33
|
+
"card.answered": "جواب داده شد",
|
|
34
|
+
"card.waiting": "در انتظار تأیید",
|
|
35
|
+
"undo.all": "برگرداندن همه",
|
|
36
|
+
"ask.send": "بفرست",
|
|
37
|
+
"learn.ask": "این را یاد بگیرم؟",
|
|
38
|
+
"routine.ask": "این کار را روال کنم؟",
|
|
39
|
+
"yes": "بله",
|
|
40
|
+
"no": "نه",
|
|
41
|
+
"feedback.copy": "کپی",
|
|
42
|
+
"feedback.not_right": "درست نبود",
|
|
43
|
+
"mode.offline": "حالت بدون هوش مصنوعی",
|
|
44
|
+
"mode.offline_until": "حالت بدون هوش مصنوعی تا {until}",
|
|
45
|
+
"mode.off": "دستیار خاموش است",
|
|
46
|
+
"usage.left": "{percent} مانده",
|
|
47
|
+
"block.more_rows": "{count} ردیف دیگر",
|
|
48
|
+
"block.suggested": "پیشنهادی",
|
|
49
|
+
"block.few_data": "داده برای درصد و میانگین کافی نیست",
|
|
50
|
+
"history.today": "امروز",
|
|
51
|
+
"history.earlier": "قبلتر",
|
|
52
|
+
"history.pinned": "سنجاقشده",
|
|
53
|
+
"history.waiting": "منتظر",
|
|
54
|
+
"history.search": "جستوجو",
|
|
55
|
+
"history.pin": "سنجاق",
|
|
56
|
+
"history.unpin": "برداشتن سنجاق",
|
|
57
|
+
"history.delete": "حذف",
|
|
58
|
+
"history.delete_confirm": "این گفتوگو حذف شود؟",
|
|
59
|
+
"routine.choose": "انتخاب",
|
|
60
|
+
"routine.back": "بازگشت",
|
|
61
|
+
"routine.preview": "پیشنمایش",
|
|
62
|
+
"routine.missing": "لازم است",
|
|
63
|
+
"routine.invalid": "درست نیست",
|
|
64
|
+
"routine.done": "انجام شد",
|
|
65
|
+
"csv.save": "ذخیره به CSV",
|
|
66
|
+
"csv.saved": "ذخیره شد",
|
|
67
|
+
"csv.not_saved": "ذخیره نشد",
|
|
68
|
+
"pdf.download": "دریافت PDF",
|
|
69
|
+
"pdf.busy": "در حال آمادهسازی PDF…",
|
|
70
|
+
"pdf.failed": "PDF ساخته نشد",
|
|
71
|
+
"panel.close": "بستن",
|
|
72
|
+
"panel.new": "گفتوگوی تازه",
|
|
73
|
+
"files.remove": "حذف فایل",
|
|
74
|
+
"secret.hint": "این مقدار به مدل نمیرسد",
|
|
75
|
+
"error.busy": "صبر کنید تا جواب قبلی تمام شود",
|
|
76
|
+
"error.network": "اتصال برقرار نشد",
|
|
77
|
+
"transcript.you": "شما",
|
|
78
|
+
"transcript.assistant": "دستیار",
|
|
79
|
+
};
|
|
80
|
+
const en = {
|
|
81
|
+
"assistant.name": "Assistant",
|
|
82
|
+
"composer.placeholder": "Ask or request…",
|
|
83
|
+
"composer.send": "Send",
|
|
84
|
+
"composer.stop": "Stop",
|
|
85
|
+
"composer.attach": "Attach a file",
|
|
86
|
+
"composer.mic": "Speak",
|
|
87
|
+
"work.title": "Working",
|
|
88
|
+
"work.long": "Still working…",
|
|
89
|
+
"work.elapsed": "{seconds} s",
|
|
90
|
+
"stage.routing": "Routing",
|
|
91
|
+
"stage.reading": "Reading",
|
|
92
|
+
"stage.thinking": "Thinking",
|
|
93
|
+
"stage.tooling": "Tools",
|
|
94
|
+
"stage.checking": "Checking",
|
|
95
|
+
"stage.waiting": "Waiting for you",
|
|
96
|
+
"stage.running": "Running",
|
|
97
|
+
"stage.role": "{role}",
|
|
98
|
+
"provenance.rule": "Rule",
|
|
99
|
+
"provenance.learned": "Learned",
|
|
100
|
+
"provenance.model": "AI",
|
|
101
|
+
"provenance.fallback": "Without AI",
|
|
102
|
+
"provenance.cached": "Kept result",
|
|
103
|
+
"card.confirm": "Confirm and run",
|
|
104
|
+
"card.refuse": "No",
|
|
105
|
+
"card.code": "Confirmation code",
|
|
106
|
+
"card.irreversible": "cannot be undone",
|
|
107
|
+
"card.tainted": "This was proposed after reading other people's text.",
|
|
108
|
+
"card.readonly_routine": "These changes will be made",
|
|
109
|
+
"card.tries_left": "{count} tries left",
|
|
110
|
+
"card.answered": "Answered",
|
|
111
|
+
"card.waiting": "Waiting for the approval",
|
|
112
|
+
"undo.all": "Undo all",
|
|
113
|
+
"ask.send": "Send",
|
|
114
|
+
"learn.ask": "Learn this?",
|
|
115
|
+
"routine.ask": "Make this a routine?",
|
|
116
|
+
"yes": "Yes",
|
|
117
|
+
"no": "No",
|
|
118
|
+
"feedback.copy": "Copy",
|
|
119
|
+
"feedback.not_right": "Not right",
|
|
120
|
+
"mode.offline": "Working without AI",
|
|
121
|
+
"mode.offline_until": "Working without AI until {until}",
|
|
122
|
+
"mode.off": "The assistant is off",
|
|
123
|
+
"usage.left": "{percent} left",
|
|
124
|
+
"block.more_rows": "{count} more rows",
|
|
125
|
+
"block.suggested": "Suggested",
|
|
126
|
+
"block.few_data": "Too little data for percentages and averages",
|
|
127
|
+
"history.today": "Today",
|
|
128
|
+
"history.earlier": "Earlier",
|
|
129
|
+
"history.pinned": "Pinned",
|
|
130
|
+
"history.waiting": "Waiting",
|
|
131
|
+
"history.search": "Search",
|
|
132
|
+
"history.pin": "Pin",
|
|
133
|
+
"history.unpin": "Unpin",
|
|
134
|
+
"history.delete": "Delete",
|
|
135
|
+
"history.delete_confirm": "Delete this conversation?",
|
|
136
|
+
"routine.choose": "Choose",
|
|
137
|
+
"routine.back": "Back",
|
|
138
|
+
"routine.preview": "Preview",
|
|
139
|
+
"routine.missing": "Required",
|
|
140
|
+
"routine.invalid": "Not right",
|
|
141
|
+
"routine.done": "Done",
|
|
142
|
+
"csv.save": "Save as CSV",
|
|
143
|
+
"csv.saved": "Saved",
|
|
144
|
+
"csv.not_saved": "Not saved",
|
|
145
|
+
"pdf.download": "Download PDF",
|
|
146
|
+
"pdf.busy": "Preparing the PDF…",
|
|
147
|
+
"pdf.failed": "Could not make the PDF",
|
|
148
|
+
"panel.close": "Close",
|
|
149
|
+
"panel.new": "New conversation",
|
|
150
|
+
"files.remove": "Remove file",
|
|
151
|
+
"secret.hint": "This value never reaches the model",
|
|
152
|
+
"error.busy": "Wait until the last answer is done",
|
|
153
|
+
"error.network": "Could not connect",
|
|
154
|
+
"transcript.you": "You",
|
|
155
|
+
"transcript.assistant": "Assistant",
|
|
156
|
+
};
|
|
157
|
+
/** The texts of a locale, with the host's overrides (a missing key shows itself, never an error). */
|
|
158
|
+
export function createTexts(locale, overrides) {
|
|
159
|
+
const base = new Map(Object.entries(locale === "fa" ? fa : en));
|
|
160
|
+
const extra = new Map(Object.entries(overrides ?? {}));
|
|
161
|
+
return {
|
|
162
|
+
locale,
|
|
163
|
+
t(key, args) {
|
|
164
|
+
let text = extra.get(key) ?? base.get(key) ?? key;
|
|
165
|
+
for (const [name, value] of Object.entries(args ?? {})) {
|
|
166
|
+
const v = typeof value === "number" && locale === "fa" ? faDigits(String(value)) : String(value);
|
|
167
|
+
text = text.split("{" + name + "}").join(v);
|
|
168
|
+
}
|
|
169
|
+
return text;
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/** Every key the library uses (for hosts that translate to another language). */
|
|
174
|
+
export function libraryKeys() {
|
|
175
|
+
return Object.keys(en).sort();
|
|
176
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** @g9tm/aiassistant: the headless client of the G9 AI assistant. No DOM, no framework, no runtime dependency. */
|
|
2
|
+
export * from "./api.js";
|
|
3
|
+
export * from "./assistant.js";
|
|
4
|
+
export * from "./blocks.js";
|
|
5
|
+
export * from "./conversation.js";
|
|
6
|
+
export * from "./fa.js";
|
|
7
|
+
export * from "./host.js";
|
|
8
|
+
export * from "./i18n.js";
|
|
9
|
+
export * from "./plan.js";
|
|
10
|
+
export * from "./reveal.js";
|
|
11
|
+
export * from "./run.js";
|
|
12
|
+
export * from "./state.js";
|
|
13
|
+
export * from "./text.js";
|
|
14
|
+
export * from "./transcript.js";
|
|
15
|
+
export * from "./transport.js";
|
|
16
|
+
export * from "./types.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** @g9tm/aiassistant: the headless client of the G9 AI assistant. No DOM, no framework, no runtime dependency. */
|
|
2
|
+
export * from "./api.js";
|
|
3
|
+
export * from "./assistant.js";
|
|
4
|
+
export * from "./blocks.js";
|
|
5
|
+
export * from "./conversation.js";
|
|
6
|
+
export * from "./fa.js";
|
|
7
|
+
export * from "./host.js";
|
|
8
|
+
export * from "./i18n.js";
|
|
9
|
+
export * from "./plan.js";
|
|
10
|
+
export * from "./reveal.js";
|
|
11
|
+
export * from "./run.js";
|
|
12
|
+
export * from "./state.js";
|
|
13
|
+
export * from "./text.js";
|
|
14
|
+
export * from "./transcript.js";
|
|
15
|
+
export * from "./transport.js";
|
|
16
|
+
export * from "./types.js";
|
package/dist/pages.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AssistantApi } from "./api.js";
|
|
2
|
+
export * from "./csv.js";
|
|
3
|
+
export * from "./history.js";
|
|
4
|
+
export * from "./routine.js";
|
|
5
|
+
/** The calls of the person's pages, over the API's plain verbs. */
|
|
6
|
+
export declare const pageCalls: {
|
|
7
|
+
/** Pins or unpins a conversation (pinned ones come first in the list). */
|
|
8
|
+
pin: (api: AssistantApi, conversationId: string, pinned: boolean) => Promise<unknown>;
|
|
9
|
+
/** Deletes a conversation of the person. */
|
|
10
|
+
remove: (api: AssistantApi, conversationId: string) => Promise<unknown>;
|
|
11
|
+
/** The routines the person may run, with their slots (`gaps`). */
|
|
12
|
+
routines: (api: AssistantApi) => Promise<unknown>;
|
|
13
|
+
/** What fills an entity slot: the host's things of a kind that match the text. */
|
|
14
|
+
pick: (api: AssistantApi, kind: string, q: string) => Promise<unknown>;
|
|
15
|
+
/** A routine's preview with the slots' values: a card to confirm, or the report of a read-only routine. */
|
|
16
|
+
preview: (api: AssistantApi, routineId: string, values: Readonly<Record<string, string>>, conversationId?: string) => Promise<unknown>;
|
|
17
|
+
/** Runs a previewed routine with the person's confirmation. */
|
|
18
|
+
run: (api: AssistantApi, routineId: string, routineRunId: string, hash: string, code?: string) => Promise<unknown>;
|
|
19
|
+
};
|
package/dist/pages.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export * from "./csv.js";
|
|
2
|
+
export * from "./history.js";
|
|
3
|
+
export * from "./routine.js";
|
|
4
|
+
const enc = encodeURIComponent;
|
|
5
|
+
/** The calls of the person's pages, over the API's plain verbs. */
|
|
6
|
+
export const pageCalls = {
|
|
7
|
+
/** Pins or unpins a conversation (pinned ones come first in the list). */
|
|
8
|
+
pin: (api, conversationId, pinned) => api.patch("conversations/" + enc(conversationId), { pinned }),
|
|
9
|
+
/** Deletes a conversation of the person. */
|
|
10
|
+
remove: (api, conversationId) => api.delete("conversations/" + enc(conversationId)),
|
|
11
|
+
/** The routines the person may run, with their slots (`gaps`). */
|
|
12
|
+
routines: (api) => api.get("routines"),
|
|
13
|
+
/** What fills an entity slot: the host's things of a kind that match the text. */
|
|
14
|
+
pick: (api, kind, q) => api.get("pick?kind=" + enc(kind) + "&q=" + enc(q)),
|
|
15
|
+
/** A routine's preview with the slots' values: a card to confirm, or the report of a read-only routine. */
|
|
16
|
+
preview: (api, routineId, values, conversationId) => api.post("routines/" + enc(routineId) + "/preview", conversationId ? { values, conversationId } : { values }),
|
|
17
|
+
/** Runs a previewed routine with the person's confirmation. */
|
|
18
|
+
run: (api, routineId, routineRunId, hash, code) => api.post("routines/" + enc(routineId) + "/run", code ? { routineRunId, hash, code } : { routineRunId, hash }),
|
|
19
|
+
};
|
package/dist/plan.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The approval card's state (ApprovalCard): step ticks with their dependants, the method, tries left,
|
|
3
|
+
* and "answered once". The confirmation is always bound to the plan's hash from the server; a host's own card cannot
|
|
4
|
+
* send one without it (A_host_override_cannot_bypass_core_safety).
|
|
5
|
+
*/
|
|
6
|
+
export interface PlanStepView {
|
|
7
|
+
index: number;
|
|
8
|
+
text: string;
|
|
9
|
+
tool: string;
|
|
10
|
+
undoable: boolean;
|
|
11
|
+
facts: {
|
|
12
|
+
key: string;
|
|
13
|
+
args: Record<string, string>;
|
|
14
|
+
}[];
|
|
15
|
+
diff: {
|
|
16
|
+
name: string;
|
|
17
|
+
args: Record<string, string>;
|
|
18
|
+
before: string | null;
|
|
19
|
+
after: string | null;
|
|
20
|
+
}[];
|
|
21
|
+
dependsOn: number[];
|
|
22
|
+
ticked: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface PlanView {
|
|
25
|
+
id: string;
|
|
26
|
+
hash: string;
|
|
27
|
+
title: string;
|
|
28
|
+
/** How the person confirms: the server's names (a click, their PIN, a code, a TOTP code, signing in again, on another device). */
|
|
29
|
+
method: "click" | "pin" | "code" | "totp" | "step_up" | "remote";
|
|
30
|
+
warning: string | null;
|
|
31
|
+
steps: PlanStepView[];
|
|
32
|
+
readOnly: boolean;
|
|
33
|
+
expiresAt: string | null;
|
|
34
|
+
answered: "confirmed" | "refused" | null;
|
|
35
|
+
triesLeft: number | null;
|
|
36
|
+
failCode: string | null;
|
|
37
|
+
}
|
|
38
|
+
/** The card's view model from the server's card body. */
|
|
39
|
+
export declare function planView(card: unknown): PlanView;
|
|
40
|
+
/** Ticks or unticks a step; unticking a step unticks every step that uses what it makes. */
|
|
41
|
+
export declare function toggle(plan: PlanView, index: number): PlanView;
|
|
42
|
+
/** Whether a method needs a code typed on the card (a click does not; neither does an approval on another device). */
|
|
43
|
+
export declare function needsCode(method: PlanView["method"]): boolean;
|
|
44
|
+
/** What the confirmation sends: the server's hash, the code, the unticked steps; null when nothing is left or it was answered. */
|
|
45
|
+
export declare function confirmation(plan: PlanView, code: string | null): {
|
|
46
|
+
planId: string;
|
|
47
|
+
hash: string;
|
|
48
|
+
code: string | null;
|
|
49
|
+
skip: number[];
|
|
50
|
+
} | null;
|
|
51
|
+
/** The server's answer that an approval (the person's phone, a second person) has not come yet: not an error, not done. */
|
|
52
|
+
export declare const WAITING = "confirm.waiting";
|
|
53
|
+
/** The card after the server's answer to a confirmation. */
|
|
54
|
+
export declare function afterConfirm(plan: PlanView, ok: boolean, body: unknown): PlanView;
|
|
55
|
+
/** Cards that take one answer (ask, learn, routine): the first answer stays; a second is ignored. */
|
|
56
|
+
export declare class AnsweredCards {
|
|
57
|
+
private readonly answers;
|
|
58
|
+
answer(cardId: string, value: string): boolean;
|
|
59
|
+
of(cardId: string): string | undefined;
|
|
60
|
+
}
|
package/dist/plan.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The approval card's state (ApprovalCard): step ticks with their dependants, the method, tries left,
|
|
3
|
+
* and "answered once". The confirmation is always bound to the plan's hash from the server; a host's own card cannot
|
|
4
|
+
* send one without it (A_host_override_cannot_bypass_core_safety).
|
|
5
|
+
*/
|
|
6
|
+
import { arr, num, obj, str } from "./api.js";
|
|
7
|
+
function refs(input) {
|
|
8
|
+
const found = new Set();
|
|
9
|
+
const walk = (v) => {
|
|
10
|
+
if (typeof v === "string")
|
|
11
|
+
for (const m of v.matchAll(/\$(\d+)\./gu))
|
|
12
|
+
found.add(Number(m[1]));
|
|
13
|
+
else if (Array.isArray(v))
|
|
14
|
+
v.forEach(walk);
|
|
15
|
+
else if (v !== null && typeof v === "object")
|
|
16
|
+
Object.values(v).forEach(walk);
|
|
17
|
+
};
|
|
18
|
+
walk(input);
|
|
19
|
+
return [...found].sort((a, b) => a - b);
|
|
20
|
+
}
|
|
21
|
+
/** The text arguments of a text line (anything that is not text is dropped). */
|
|
22
|
+
function texts(args) {
|
|
23
|
+
return Object.fromEntries(Object.entries(obj(args)).filter((e) => typeof e[1] === "string"));
|
|
24
|
+
}
|
|
25
|
+
/** The card's view model from the server's card body. */
|
|
26
|
+
export function planView(card) {
|
|
27
|
+
const b = obj(card);
|
|
28
|
+
const methods = new Set(["click", "pin", "code", "totp", "step_up", "remote"]);
|
|
29
|
+
const steps = arr(b["steps"]).map(obj).map((s, i) => ({
|
|
30
|
+
index: num(s["index"], i + 1),
|
|
31
|
+
text: str(s["text"], str(s["tool"])),
|
|
32
|
+
tool: str(s["tool"]),
|
|
33
|
+
undoable: s["undoable"] !== false,
|
|
34
|
+
facts: arr(s["facts"]).map(obj).map((f) => ({ key: str(f["key"]), args: texts(f["args"]) })),
|
|
35
|
+
diff: arr(s["diff"]).map(obj).map((d) => ({ name: str(obj(d["name"])["key"], str(d["name"])), args: texts(obj(d["name"])["args"]), before: typeof d["before"] === "string" ? d["before"] : null, after: typeof d["after"] === "string" ? d["after"] : null })),
|
|
36
|
+
dependsOn: refs(s["input"]),
|
|
37
|
+
ticked: true,
|
|
38
|
+
}));
|
|
39
|
+
const warning = obj(b["warning"]);
|
|
40
|
+
return {
|
|
41
|
+
id: str(b["id"]),
|
|
42
|
+
hash: str(b["hash"]),
|
|
43
|
+
title: str(b["title"]),
|
|
44
|
+
// A method this client does not know is never taken for a click: it asks for a code, and the server decides.
|
|
45
|
+
method: (methods.has(str(b["method"])) ? str(b["method"]) : str(b["method"]).length > 0 ? "code" : "click"),
|
|
46
|
+
warning: typeof warning["key"] === "string" ? warning["key"] : null,
|
|
47
|
+
steps,
|
|
48
|
+
readOnly: b["readOnly"] === true,
|
|
49
|
+
expiresAt: typeof b["expiresAt"] === "string" ? b["expiresAt"] : null,
|
|
50
|
+
answered: null,
|
|
51
|
+
triesLeft: null,
|
|
52
|
+
failCode: null,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** Ticks or unticks a step; unticking a step unticks every step that uses what it makes. */
|
|
56
|
+
export function toggle(plan, index) {
|
|
57
|
+
const step = plan.steps.find((s) => s.index === index);
|
|
58
|
+
if (!step || plan.answered)
|
|
59
|
+
return plan;
|
|
60
|
+
const ticked = !step.ticked;
|
|
61
|
+
const off = new Set();
|
|
62
|
+
if (!ticked) {
|
|
63
|
+
off.add(index);
|
|
64
|
+
let grew = true;
|
|
65
|
+
while (grew) {
|
|
66
|
+
grew = false;
|
|
67
|
+
for (const s of plan.steps)
|
|
68
|
+
if (!off.has(s.index) && s.dependsOn.some((d) => off.has(d))) {
|
|
69
|
+
off.add(s.index);
|
|
70
|
+
grew = true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { ...plan, steps: plan.steps.map((s) => (off.has(s.index) ? { ...s, ticked: false } : s.index === index ? { ...s, ticked } : s)) };
|
|
75
|
+
}
|
|
76
|
+
/** Whether a method needs a code typed on the card (a click does not; neither does an approval on another device). */
|
|
77
|
+
export function needsCode(method) {
|
|
78
|
+
return method !== "click" && method !== "remote";
|
|
79
|
+
}
|
|
80
|
+
/** What the confirmation sends: the server's hash, the code, the unticked steps; null when nothing is left or it was answered. */
|
|
81
|
+
export function confirmation(plan, code) {
|
|
82
|
+
if (plan.answered || plan.hash.length === 0)
|
|
83
|
+
return null;
|
|
84
|
+
const skip = plan.steps.filter((s) => !s.ticked).map((s) => s.index);
|
|
85
|
+
if (skip.length === plan.steps.length)
|
|
86
|
+
return null;
|
|
87
|
+
if (needsCode(plan.method) && (code === null || code.trim().length === 0))
|
|
88
|
+
return null;
|
|
89
|
+
return { planId: plan.id, hash: plan.hash, code: needsCode(plan.method) ? code : null, skip };
|
|
90
|
+
}
|
|
91
|
+
/** The server's answer that an approval (the person's phone, a second person) has not come yet: not an error, not done. */
|
|
92
|
+
export const WAITING = "confirm.waiting";
|
|
93
|
+
/** The card after the server's answer to a confirmation. */
|
|
94
|
+
export function afterConfirm(plan, ok, body) {
|
|
95
|
+
const b = obj(body);
|
|
96
|
+
if (ok && b["ok"] === false)
|
|
97
|
+
return { ...plan, failCode: WAITING }; // 202: it waits for an approval
|
|
98
|
+
if (ok)
|
|
99
|
+
return { ...plan, answered: "confirmed", failCode: null };
|
|
100
|
+
const e = b["error"];
|
|
101
|
+
return { ...plan, failCode: typeof e === "string" ? e : str(obj(e)["code"], "error"), triesLeft: typeof b["triesLeft"] === "number" ? b["triesLeft"] : plan.triesLeft };
|
|
102
|
+
}
|
|
103
|
+
/** Cards that take one answer (ask, learn, routine): the first answer stays; a second is ignored. */
|
|
104
|
+
export class AnsweredCards {
|
|
105
|
+
constructor() {
|
|
106
|
+
this.answers = new Map();
|
|
107
|
+
}
|
|
108
|
+
answer(cardId, value) {
|
|
109
|
+
if (this.answers.has(cardId))
|
|
110
|
+
return false;
|
|
111
|
+
this.answers.set(cardId, value);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
of(cardId) {
|
|
115
|
+
return this.answers.get(cardId);
|
|
116
|
+
}
|
|
117
|
+
}
|
package/dist/reveal.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Progressive reveal without a DOM: only an answer that arrived in front of the person is
|
|
3
|
+
* revealed, over about 1.5 seconds, in batches no faster than one render per 80 ms; a history message, or any message
|
|
4
|
+
* with reduced motion, shows whole at once.
|
|
5
|
+
*/
|
|
6
|
+
export declare const REVEAL_MS = 1500;
|
|
7
|
+
export declare const FRAME_MS = 80;
|
|
8
|
+
/** How much of a text is visible at a moment of its reveal. */
|
|
9
|
+
export declare function visibleLength(textLength: number, elapsedMs: number, reducedMotion: boolean, durationMs?: number): number;
|
|
10
|
+
/** The visible part, never cutting inside a word when it can help it (a cut word reads as a typo). */
|
|
11
|
+
export declare function visibleText(text: string, elapsedMs: number, reducedMotion: boolean, durationMs?: number): string;
|
|
12
|
+
/** Which messages may be revealed: those not yet watched. A message is watched once it has been shown. */
|
|
13
|
+
export declare class Watched {
|
|
14
|
+
private readonly seen;
|
|
15
|
+
/** Marks the messages of a loaded history as watched (they never animate). */
|
|
16
|
+
history(ids: Iterable<string>): void;
|
|
17
|
+
/** Whether a message should be revealed now; the first time only. */
|
|
18
|
+
reveal(id: string): boolean;
|
|
19
|
+
has(id: string): boolean;
|
|
20
|
+
}
|
package/dist/reveal.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Progressive reveal without a DOM: only an answer that arrived in front of the person is
|
|
3
|
+
* revealed, over about 1.5 seconds, in batches no faster than one render per 80 ms; a history message, or any message
|
|
4
|
+
* with reduced motion, shows whole at once.
|
|
5
|
+
*/
|
|
6
|
+
export const REVEAL_MS = 1500;
|
|
7
|
+
export const FRAME_MS = 80;
|
|
8
|
+
/** How much of a text is visible at a moment of its reveal. */
|
|
9
|
+
export function visibleLength(textLength, elapsedMs, reducedMotion, durationMs = REVEAL_MS) {
|
|
10
|
+
if (reducedMotion || textLength === 0 || elapsedMs >= durationMs)
|
|
11
|
+
return textLength;
|
|
12
|
+
if (elapsedMs <= 0)
|
|
13
|
+
return 0;
|
|
14
|
+
const frames = Math.floor(elapsedMs / FRAME_MS); // batched: one step per frame
|
|
15
|
+
const total = Math.max(1, Math.ceil(durationMs / FRAME_MS));
|
|
16
|
+
return Math.min(textLength, Math.ceil((textLength * frames) / total));
|
|
17
|
+
}
|
|
18
|
+
/** The visible part, never cutting inside a word when it can help it (a cut word reads as a typo). */
|
|
19
|
+
export function visibleText(text, elapsedMs, reducedMotion, durationMs = REVEAL_MS) {
|
|
20
|
+
const n = visibleLength(text.length, elapsedMs, reducedMotion, durationMs);
|
|
21
|
+
if (n >= text.length)
|
|
22
|
+
return text;
|
|
23
|
+
const space = text.lastIndexOf(" ", n);
|
|
24
|
+
return text.slice(0, space > n * 0.6 ? space : n);
|
|
25
|
+
}
|
|
26
|
+
/** Which messages may be revealed: those not yet watched. A message is watched once it has been shown. */
|
|
27
|
+
export class Watched {
|
|
28
|
+
constructor() {
|
|
29
|
+
this.seen = new Set();
|
|
30
|
+
}
|
|
31
|
+
/** Marks the messages of a loaded history as watched (they never animate). */
|
|
32
|
+
history(ids) {
|
|
33
|
+
for (const id of ids)
|
|
34
|
+
this.seen.add(id);
|
|
35
|
+
}
|
|
36
|
+
/** Whether a message should be revealed now; the first time only. */
|
|
37
|
+
reveal(id) {
|
|
38
|
+
if (this.seen.has(id))
|
|
39
|
+
return false;
|
|
40
|
+
this.seen.add(id);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
has(id) {
|
|
44
|
+
return this.seen.has(id);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type SlotKind = "pick" | "number" | "money" | "date" | "choice" | "text";
|
|
2
|
+
export interface RoutineSlot {
|
|
3
|
+
/** The slot's name (the key of its value). */
|
|
4
|
+
name: string;
|
|
5
|
+
/** The routine's own kind (item, customer, number...). */
|
|
6
|
+
kind: string;
|
|
7
|
+
/** How the form asks for it. */
|
|
8
|
+
field: SlotKind;
|
|
9
|
+
}
|
|
10
|
+
export interface Routine {
|
|
11
|
+
id: string;
|
|
12
|
+
title: string;
|
|
13
|
+
hits: number;
|
|
14
|
+
steps: {
|
|
15
|
+
index: number;
|
|
16
|
+
tool: string;
|
|
17
|
+
text?: string;
|
|
18
|
+
}[];
|
|
19
|
+
slots: RoutineSlot[];
|
|
20
|
+
}
|
|
21
|
+
/** Kinds that name a thing of the host (a picker finds them); everything else is typed. */
|
|
22
|
+
export declare const PICKED_KINDS: Set<string>;
|
|
23
|
+
/** How a slot of a kind is asked for. */
|
|
24
|
+
export declare function slotField(kind: string): SlotKind;
|
|
25
|
+
/** Reads the server's routines (`GET routines`). */
|
|
26
|
+
export declare function readRoutines(raw: unknown): Routine[];
|
|
27
|
+
/** One typed value as the routine takes it: Persian digits and separators read as a number; null when it is not right. */
|
|
28
|
+
export declare function slotValue(slot: RoutineSlot, text: string): string | null;
|
|
29
|
+
/** The slots still without a value, in order. */
|
|
30
|
+
export declare function missingSlots(slots: readonly RoutineSlot[], values: Readonly<Record<string, string>>): RoutineSlot[];
|
|
31
|
+
/** What the picker found (`GET pick?kind=&q=`). */
|
|
32
|
+
export declare function readPicks(raw: unknown): {
|
|
33
|
+
id: string;
|
|
34
|
+
label: string;
|
|
35
|
+
}[];
|
package/dist/routine.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** A routine's slots as a form: entity slots open a picker over `GET pick`, the rest are typed fields. */
|
|
2
|
+
import { latinDigits } from "./fa.js";
|
|
3
|
+
/** Kinds that name a thing of the host (a picker finds them); everything else is typed. */
|
|
4
|
+
export const PICKED_KINDS = new Set(["item", "category", "customer", "material", "supplier", "product", "person", "branch"]);
|
|
5
|
+
function obj(v) {
|
|
6
|
+
return v !== null && typeof v === "object" && !Array.isArray(v) ? v : {};
|
|
7
|
+
}
|
|
8
|
+
function arr(v) {
|
|
9
|
+
return Array.isArray(v) ? v : [];
|
|
10
|
+
}
|
|
11
|
+
function str(v) {
|
|
12
|
+
return typeof v === "string" ? v : "";
|
|
13
|
+
}
|
|
14
|
+
/** How a slot of a kind is asked for. */
|
|
15
|
+
export function slotField(kind) {
|
|
16
|
+
if (PICKED_KINDS.has(kind))
|
|
17
|
+
return "pick";
|
|
18
|
+
if (kind === "number" || kind === "money" || kind === "date" || kind === "choice")
|
|
19
|
+
return kind;
|
|
20
|
+
return "text";
|
|
21
|
+
}
|
|
22
|
+
/** Reads the server's routines (`GET routines`). */
|
|
23
|
+
export function readRoutines(raw) {
|
|
24
|
+
const o = obj(raw);
|
|
25
|
+
return arr(o["routines"] ?? o["items"]).map(obj).map((r) => ({
|
|
26
|
+
id: str(r["id"]),
|
|
27
|
+
title: str(r["title"]) || str(r["name"]),
|
|
28
|
+
hits: typeof r["hits"] === "number" ? r["hits"] : 0,
|
|
29
|
+
steps: arr(r["steps"]).map(obj).map((s) => ({ index: typeof s["index"] === "number" ? s["index"] : 0, tool: str(s["tool"]), ...(typeof s["text"] === "string" ? { text: s["text"] } : {}) })),
|
|
30
|
+
slots: arr(r["gaps"]).map(obj).map((g) => ({ name: str(g["name"]), kind: str(g["kind"]), field: slotField(str(g["kind"])) })).filter((g) => g.name.length > 0),
|
|
31
|
+
})).filter((r) => r.id.length > 0);
|
|
32
|
+
}
|
|
33
|
+
/** One typed value as the routine takes it: Persian digits and separators read as a number; null when it is not right. */
|
|
34
|
+
export function slotValue(slot, text) {
|
|
35
|
+
const t = latinDigits(text).trim();
|
|
36
|
+
if (t.length === 0)
|
|
37
|
+
return null;
|
|
38
|
+
if (slot.field === "number" || slot.field === "money") {
|
|
39
|
+
const n = t.replace(/[,\u066C\u060C\s]/gu, "").replace(/\u066B/gu, ".");
|
|
40
|
+
return /^-?\d+(\.\d+)?$/u.test(n) ? n : null;
|
|
41
|
+
}
|
|
42
|
+
if (slot.field === "date")
|
|
43
|
+
return /^\d{4}[-/]\d{1,2}[-/]\d{1,2}$/u.test(t) ? t.replace(/\//gu, "-") : null;
|
|
44
|
+
return t;
|
|
45
|
+
}
|
|
46
|
+
/** The slots still without a value, in order. */
|
|
47
|
+
export function missingSlots(slots, values) {
|
|
48
|
+
return slots.filter((s) => (values[s.name] ?? "").length === 0);
|
|
49
|
+
}
|
|
50
|
+
/** What the picker found (`GET pick?kind=&q=`). */
|
|
51
|
+
export function readPicks(raw) {
|
|
52
|
+
return arr(obj(raw)["items"]).map(obj).map((p) => ({ id: str(p["id"]), label: str(p["text"]) || str(p["label"]) })).filter((p) => p.id.length > 0);
|
|
53
|
+
}
|
package/dist/run.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The run as the client sees it: one reducer over the run's events builds the RunView. Every transport feeds
|
|
3
|
+
* the same events in the same order, so streaming, long polling and the synchronous mode build the same view.
|
|
4
|
+
*/
|
|
5
|
+
export interface RawEvent {
|
|
6
|
+
seq: number;
|
|
7
|
+
type: string;
|
|
8
|
+
at: string;
|
|
9
|
+
body: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
export interface TextLine {
|
|
12
|
+
key: string;
|
|
13
|
+
args?: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
export type StageId = "routing" | "reading" | "thinking" | "tooling" | "checking" | "waiting" | "running" | "role";
|
|
16
|
+
export interface RunView {
|
|
17
|
+
runId: string;
|
|
18
|
+
state: "queued" | "working" | "waiting-confirm" | "waiting-answer" | "done" | "failed" | "cancelled";
|
|
19
|
+
stages: {
|
|
20
|
+
id: StageId;
|
|
21
|
+
role?: string;
|
|
22
|
+
state: "idle" | "on" | "done";
|
|
23
|
+
}[];
|
|
24
|
+
progress?: TextLine;
|
|
25
|
+
tools: {
|
|
26
|
+
name: string;
|
|
27
|
+
title: TextLine;
|
|
28
|
+
state: "start" | "ok" | "fail";
|
|
29
|
+
}[];
|
|
30
|
+
streamingText?: string;
|
|
31
|
+
provenance?: "rule" | "learned" | "model" | "fallback" | "cached";
|
|
32
|
+
tier?: string;
|
|
33
|
+
elapsedMs: number;
|
|
34
|
+
longRunning: boolean;
|
|
35
|
+
seq: number;
|
|
36
|
+
code?: string;
|
|
37
|
+
/** Messages and cards the run wrote (in order). */
|
|
38
|
+
messages: Record<string, unknown>[];
|
|
39
|
+
cards: {
|
|
40
|
+
kind: string;
|
|
41
|
+
id: string;
|
|
42
|
+
body: Record<string, unknown>;
|
|
43
|
+
}[];
|
|
44
|
+
/** The structured output of a non-conversation capability. */
|
|
45
|
+
result?: {
|
|
46
|
+
used: boolean;
|
|
47
|
+
provenance: string;
|
|
48
|
+
output: unknown;
|
|
49
|
+
code?: string;
|
|
50
|
+
};
|
|
51
|
+
startedAt?: number;
|
|
52
|
+
}
|
|
53
|
+
/** After this long a run is shown as "still working". */
|
|
54
|
+
export declare const LONG_RUNNING_MS = 40000;
|
|
55
|
+
export declare function emptyRun(runId: string): RunView;
|
|
56
|
+
/** One event applied to a view (an event already seen, by its seq, changes nothing). */
|
|
57
|
+
export declare function reduceRun(view: RunView, e: RawEvent, now?: number): RunView;
|
|
58
|
+
export declare function finished(v: RunView): boolean;
|
|
59
|
+
/** A whole tape of events applied in order. */
|
|
60
|
+
export declare function replay(runId: string, events: readonly RawEvent[]): RunView;
|
|
61
|
+
/** The view's tools and stages without timing, for comparing two renderers or two transports. */
|
|
62
|
+
export declare function comparable(v: RunView): Omit<RunView, "elapsedMs" | "longRunning" | "startedAt">;
|
|
63
|
+
export declare function messagesOf(v: unknown): Record<string, unknown>[];
|
|
64
|
+
export declare function seqOf(v: unknown): number;
|