@g9tm/aiassistant 1.0.0 → 1.2.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/dist/api.d.ts +16 -6
- package/dist/api.js +31 -20
- package/dist/assistant.d.ts +7 -0
- package/dist/assistant.js +26 -0
- package/dist/blocks.d.ts +4 -0
- package/dist/blocks.js +6 -1
- package/dist/csv.d.ts +10 -0
- package/dist/csv.js +27 -0
- package/dist/history.d.ts +20 -0
- package/dist/history.js +39 -0
- package/dist/host.d.ts +10 -0
- package/dist/host.js +1 -1
- package/dist/i18n.js +12 -0
- package/dist/pages.d.ts +34 -0
- package/dist/pages.js +91 -0
- package/dist/routine.d.ts +35 -0
- package/dist/routine.js +53 -0
- package/package.json +6 -2
package/dist/api.d.ts
CHANGED
|
@@ -60,12 +60,14 @@ export declare class AssistantApi {
|
|
|
60
60
|
capabilities(): Promise<unknown>;
|
|
61
61
|
feedback(conversationId: string, messageId: string, good: boolean, note?: string): Promise<unknown>;
|
|
62
62
|
card(kind: "knowledge" | "learned", id: string, verb: string): Promise<unknown>;
|
|
63
|
-
upload(name: string, bytes: Uint8Array): Promise<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
upload(name: string, bytes: Uint8Array): Promise<UploadedFile>;
|
|
64
|
+
/** Where a host's own uploader sends a file (`AssistantHost.uploadFile`). */
|
|
65
|
+
get filesPath(): string;
|
|
66
|
+
/** The server's answer to a host's own upload, read as `upload` reads its own (a refusal throws its code). */
|
|
67
|
+
readUpload(answer: {
|
|
68
|
+
status: number;
|
|
69
|
+
body: string;
|
|
70
|
+
}): UploadedFile;
|
|
69
71
|
/** Dictation: audio to text on the server (the audio is not kept). */
|
|
70
72
|
speech(bytes: Uint8Array, mime: string): Promise<string>;
|
|
71
73
|
removeFile(id: string): Promise<unknown>;
|
|
@@ -75,6 +77,14 @@ export declare class AssistantApi {
|
|
|
75
77
|
get(path: string): Promise<unknown>;
|
|
76
78
|
post(path: string, body?: unknown): Promise<unknown>;
|
|
77
79
|
put(path: string, body?: unknown): Promise<unknown>;
|
|
80
|
+
patch(path: string, body?: unknown): Promise<unknown>;
|
|
78
81
|
delete(path: string): Promise<unknown>;
|
|
79
82
|
}
|
|
80
83
|
export declare function toRawEvent(v: unknown): RawEvent;
|
|
84
|
+
/** A file the server took. */
|
|
85
|
+
export interface UploadedFile {
|
|
86
|
+
id: string;
|
|
87
|
+
kind: string;
|
|
88
|
+
name: string;
|
|
89
|
+
rows?: number;
|
|
90
|
+
}
|
package/dist/api.js
CHANGED
|
@@ -46,21 +46,7 @@ export class AssistantApi {
|
|
|
46
46
|
const init = payload === undefined ? { method, headers } : { method, headers, body: payload };
|
|
47
47
|
try {
|
|
48
48
|
const response = await Promise.race([this.host.fetch(this.base + path, init), timeout]); // AbortController is not on Lynx
|
|
49
|
-
|
|
50
|
-
let parsed = null;
|
|
51
|
-
if (text.length > 0) {
|
|
52
|
-
try {
|
|
53
|
-
parsed = JSON.parse(text);
|
|
54
|
-
}
|
|
55
|
-
catch {
|
|
56
|
-
parsed = text;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
if (!response.ok) {
|
|
60
|
-
const e = obj(parsed)["error"];
|
|
61
|
-
throw new ApiError(response.status, typeof e === "string" ? e : str(obj(e)["code"], "error"), parsed);
|
|
62
|
-
}
|
|
63
|
-
return parsed;
|
|
49
|
+
return answerOf(response.status, await response.text());
|
|
64
50
|
}
|
|
65
51
|
finally {
|
|
66
52
|
t.clear(handle);
|
|
@@ -108,12 +94,12 @@ export class AssistantApi {
|
|
|
108
94
|
return this.request("POST", kind + "/" + encodeURIComponent(id) + "/" + verb);
|
|
109
95
|
}
|
|
110
96
|
async upload(name, bytes) {
|
|
111
|
-
|
|
112
|
-
const out = { id: str(r["id"]), kind: str(r["kind"]), name: str(r["name"]) };
|
|
113
|
-
if (typeof r["rows"] === "number")
|
|
114
|
-
out.rows = r["rows"];
|
|
115
|
-
return out;
|
|
97
|
+
return uploaded(await this.request("POST", "files?name=" + encodeURIComponent(name), undefined, { bytes, type: "application/octet-stream" }));
|
|
116
98
|
}
|
|
99
|
+
/** Where a host's own uploader sends a file (`AssistantHost.uploadFile`). */
|
|
100
|
+
get filesPath() { return this.base + "files"; }
|
|
101
|
+
/** The server's answer to a host's own upload, read as `upload` reads its own (a refusal throws its code). */
|
|
102
|
+
readUpload(answer) { return uploaded(answerOf(answer.status, answer.body)); }
|
|
117
103
|
/** Dictation: audio to text on the server (the audio is not kept). */
|
|
118
104
|
async speech(bytes, mime) {
|
|
119
105
|
return str(obj(await this.request("POST", "speech?mime=" + encodeURIComponent(mime), undefined, { bytes, type: "application/octet-stream" }))["text"]);
|
|
@@ -125,9 +111,34 @@ export class AssistantApi {
|
|
|
125
111
|
get(path) { return this.request("GET", path); }
|
|
126
112
|
post(path, body) { return this.request("POST", path, body); }
|
|
127
113
|
put(path, body) { return this.request("PUT", path, body); }
|
|
114
|
+
patch(path, body) { return this.request("PATCH", path, body); }
|
|
128
115
|
delete(path) { return this.request("DELETE", path); }
|
|
129
116
|
}
|
|
130
117
|
export function toRawEvent(v) {
|
|
131
118
|
const e = obj(v);
|
|
132
119
|
return { seq: num(e["seq"]), type: str(e["type"]), at: str(e["at"]), body: obj(e["body"]) };
|
|
133
120
|
}
|
|
121
|
+
function uploaded(v) {
|
|
122
|
+
const r = obj(v);
|
|
123
|
+
const out = { id: str(r["id"]), kind: str(r["kind"]), name: str(r["name"]) };
|
|
124
|
+
if (typeof r["rows"] === "number")
|
|
125
|
+
out.rows = r["rows"];
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
/** A response's JSON (or its text), or the `ApiError` of a status outside 2xx. */
|
|
129
|
+
function answerOf(status, text) {
|
|
130
|
+
let parsed = null;
|
|
131
|
+
if (text.length > 0) {
|
|
132
|
+
try {
|
|
133
|
+
parsed = JSON.parse(text);
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
parsed = text;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (status < 200 || status > 299) {
|
|
140
|
+
const e = obj(parsed)["error"];
|
|
141
|
+
throw new ApiError(status, typeof e === "string" ? e : str(obj(e)["code"], "error"), parsed);
|
|
142
|
+
}
|
|
143
|
+
return parsed;
|
|
144
|
+
}
|
package/dist/assistant.d.ts
CHANGED
|
@@ -36,6 +36,8 @@ export interface AssistantSnapshot {
|
|
|
36
36
|
state: AssistantState;
|
|
37
37
|
error: string | null;
|
|
38
38
|
}
|
|
39
|
+
/** The files the composer offers by default. */
|
|
40
|
+
export declare const ATTACH_ACCEPT: readonly string[];
|
|
39
41
|
export declare class Assistant {
|
|
40
42
|
readonly host: AssistantHost;
|
|
41
43
|
readonly api: AssistantApi;
|
|
@@ -81,6 +83,11 @@ export declare class Assistant {
|
|
|
81
83
|
answer(runId: string, cardId: string, picked: string[], text: string | null): Promise<boolean>;
|
|
82
84
|
/** Answers a "learn this?" or "make it a routine?" card once. */
|
|
83
85
|
choose(kind: "knowledge" | "learned", cardId: string, verb: string): Promise<boolean>;
|
|
86
|
+
/**
|
|
87
|
+
* The composer's attach button: the host's own uploader when it has one (the bytes never cross a bridge), otherwise
|
|
88
|
+
* its picker and `attach`.
|
|
89
|
+
*/
|
|
90
|
+
attachFromDevice(accept?: readonly string[]): Promise<void>;
|
|
84
91
|
attach(name: string, bytes: Uint8Array): Promise<void>;
|
|
85
92
|
detach(id: string): void;
|
|
86
93
|
notRight(messageId: string): Promise<void>;
|
package/dist/assistant.js
CHANGED
|
@@ -13,6 +13,8 @@ import { emptyRun, finished, reduceRun } from "./run.js";
|
|
|
13
13
|
import { StateWatch } from "./state.js";
|
|
14
14
|
import { transcript } from "./transcript.js";
|
|
15
15
|
import { pickTransport } from "./transport.js";
|
|
16
|
+
/** The files the composer offers by default. */
|
|
17
|
+
export const ATTACH_ACCEPT = [".pdf", ".xlsx", ".csv", "image/*"];
|
|
16
18
|
export class Assistant {
|
|
17
19
|
constructor(host, options = {}) {
|
|
18
20
|
this.host = host;
|
|
@@ -221,6 +223,30 @@ export class Assistant {
|
|
|
221
223
|
await this.api.card(kind, cardId, verb);
|
|
222
224
|
return true;
|
|
223
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* The composer's attach button: the host's own uploader when it has one (the bytes never cross a bridge), otherwise
|
|
228
|
+
* its picker and `attach`.
|
|
229
|
+
*/
|
|
230
|
+
async attachFromDevice(accept = ATTACH_ACCEPT) {
|
|
231
|
+
const up = this.host.uploadFile;
|
|
232
|
+
if (!up) {
|
|
233
|
+
const f = await this.host.pickFile?.([...accept]);
|
|
234
|
+
if (f)
|
|
235
|
+
await this.attach(f.name, f.bytes);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
const answer = await up.call(this.host, this.api.filesPath, [...accept]);
|
|
240
|
+
if (answer === null)
|
|
241
|
+
return;
|
|
242
|
+
const f = this.api.readUpload(answer);
|
|
243
|
+
this.attachments = [...this.attachments, { id: f.id, name: f.name, kind: f.kind }];
|
|
244
|
+
}
|
|
245
|
+
catch (e) {
|
|
246
|
+
this.error = e instanceof ApiError ? e.code : "error.network";
|
|
247
|
+
}
|
|
248
|
+
this.emit();
|
|
249
|
+
}
|
|
224
250
|
async attach(name, bytes) {
|
|
225
251
|
try {
|
|
226
252
|
const f = await this.api.upload(name, bytes);
|
package/dist/blocks.d.ts
CHANGED
package/dist/blocks.js
CHANGED
|
@@ -73,7 +73,12 @@ export function blockView(raw, locale, custom = new Set()) {
|
|
|
73
73
|
case "image":
|
|
74
74
|
case "gallery": return { type: "image", fileIds: arr(b["fileIds"]).filter((x) => typeof x === "string"), fallbackText };
|
|
75
75
|
case "open": return str(b["pageId"]).length > 0 ? { type: "open", pageId: str(b["pageId"]), fallbackText } : { type: "fallback", fallbackText };
|
|
76
|
-
case "export":
|
|
76
|
+
case "export": {
|
|
77
|
+
// every row and column, as raw text: the file is for a spreadsheet, not for reading here
|
|
78
|
+
const cols = columns(b["columns"]);
|
|
79
|
+
const rows = arr(b["rows"]).map((r) => cols.map((_, i) => { const v = arr(r).at(i); return v === null || v === undefined ? "" : typeof v === "string" ? v : String(v); }));
|
|
80
|
+
return withTitle({ type: "export", format: str(b["format"], "csv"), columns: cols, rows, fallbackText });
|
|
81
|
+
}
|
|
77
82
|
default: return { type: "fallback", fallbackText }; // compare, gauge, timeline, items: drawn by the host or shown as text
|
|
78
83
|
}
|
|
79
84
|
function cols(x) {
|
package/dist/csv.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** A table as a CSV file a spreadsheet opens right: a BOM for the encoding, every cell quoted, no formula injection. */
|
|
2
|
+
import type { AssistantHost } from "./host.js";
|
|
3
|
+
/** One cell: quoted, with inner quotes doubled; a cell a spreadsheet would run as a formula gets a leading apostrophe. */
|
|
4
|
+
export declare function csvCell(value: string): string;
|
|
5
|
+
/** The whole file: a BOM, a header row of the column labels, then the rows, lines ended with CRLF. */
|
|
6
|
+
export declare function toCsv(labels: readonly string[], rows: readonly (readonly string[])[]): string;
|
|
7
|
+
/** A file name from a title: letters, digits and dashes only, with the extension. */
|
|
8
|
+
export declare function fileName(title: string | undefined, extension: string): string;
|
|
9
|
+
/** Saves a table through the host; false when the host cannot save files or the save did not happen. */
|
|
10
|
+
export declare function saveCsv(host: AssistantHost, title: string | undefined, labels: readonly string[], rows: readonly (readonly string[])[]): Promise<boolean>;
|
package/dist/csv.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const BOM = "\uFEFF";
|
|
2
|
+
/** One cell: quoted, with inner quotes doubled; a cell a spreadsheet would run as a formula gets a leading apostrophe. */
|
|
3
|
+
export function csvCell(value) {
|
|
4
|
+
const safe = /^[-=+@\t\r]/u.test(value) ? "'" + value : value;
|
|
5
|
+
return '"' + safe.replace(/"/gu, '""') + '"';
|
|
6
|
+
}
|
|
7
|
+
/** The whole file: a BOM, a header row of the column labels, then the rows, lines ended with CRLF. */
|
|
8
|
+
export function toCsv(labels, rows) {
|
|
9
|
+
const lines = [labels.map(csvCell).join(","), ...rows.map((r) => labels.map((_, i) => csvCell(r[i] ?? "")).join(","))];
|
|
10
|
+
return BOM + lines.join("\r\n") + "\r\n";
|
|
11
|
+
}
|
|
12
|
+
/** A file name from a title: letters, digits and dashes only, with the extension. */
|
|
13
|
+
export function fileName(title, extension) {
|
|
14
|
+
const base = (title ?? "").trim().replace(/[\\/:*?"<>|\s]+/gu, "-").replace(/-+/gu, "-").replace(/^-|-$/gu, "");
|
|
15
|
+
return (base.length > 0 ? base.slice(0, 80) : "export") + "." + extension;
|
|
16
|
+
}
|
|
17
|
+
/** Saves a table through the host; false when the host cannot save files or the save did not happen. */
|
|
18
|
+
export async function saveCsv(host, title, labels, rows) {
|
|
19
|
+
if (typeof host.saveFile !== "function")
|
|
20
|
+
return false;
|
|
21
|
+
try {
|
|
22
|
+
return await host.saveFile(fileName(title, "csv"), toCsv(labels, rows));
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ConversationRow {
|
|
2
|
+
id: string;
|
|
3
|
+
title: string;
|
|
4
|
+
capability: string;
|
|
5
|
+
pinned: boolean;
|
|
6
|
+
/** A card in it waits for the person's code or click. */
|
|
7
|
+
waiting: boolean;
|
|
8
|
+
/** When it last changed (milliseconds since 1970). */
|
|
9
|
+
updatedAt: number;
|
|
10
|
+
}
|
|
11
|
+
export type HistoryTab = "today" | "earlier" | "pinned" | "waiting";
|
|
12
|
+
export declare const HISTORY_TABS: readonly HistoryTab[];
|
|
13
|
+
/** Reads the server's list (`GET conversations`), pinned first and then the newest. */
|
|
14
|
+
export declare function readConversations(raw: unknown): ConversationRow[];
|
|
15
|
+
/** Folds the letters people type two ways (Arabic yeh and kaf, digits, case) so a search finds both. */
|
|
16
|
+
export declare function foldSearch(text: string): string;
|
|
17
|
+
/** The rows of each tab, after a search over the titles; a day starts at the person's local midnight. */
|
|
18
|
+
export declare function historyTabs(rows: readonly ConversationRow[], now: Date, search?: string): Record<HistoryTab, ConversationRow[]>;
|
|
19
|
+
/** How many rows each tab has. */
|
|
20
|
+
export declare function tabCounts(tabs: Record<HistoryTab, ConversationRow[]>): Record<HistoryTab, number>;
|
package/dist/history.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** The person's conversations as tabs: today, earlier, pinned and waiting for their code, each with its count. */
|
|
2
|
+
import { latinDigits } from "./fa.js";
|
|
3
|
+
export const HISTORY_TABS = ["today", "earlier", "pinned", "waiting"];
|
|
4
|
+
function obj(v) {
|
|
5
|
+
return v !== null && typeof v === "object" && !Array.isArray(v) ? v : {};
|
|
6
|
+
}
|
|
7
|
+
/** Reads the server's list (`GET conversations`), pinned first and then the newest. */
|
|
8
|
+
export function readConversations(raw) {
|
|
9
|
+
const list = obj(raw)["conversations"];
|
|
10
|
+
const rows = (Array.isArray(list) ? list : []).map(obj).map((c) => ({
|
|
11
|
+
id: typeof c["id"] === "string" ? c["id"] : "",
|
|
12
|
+
title: typeof c["title"] === "string" ? c["title"] : "",
|
|
13
|
+
capability: typeof c["capability"] === "string" ? c["capability"] : "",
|
|
14
|
+
pinned: c["pinned"] === true,
|
|
15
|
+
waiting: c["waiting"] === true,
|
|
16
|
+
updatedAt: typeof c["updatedAt"] === "number" ? c["updatedAt"] : 0,
|
|
17
|
+
})).filter((c) => c.id.length > 0);
|
|
18
|
+
return rows.sort((a, b) => Number(b.pinned) - Number(a.pinned) || b.updatedAt - a.updatedAt);
|
|
19
|
+
}
|
|
20
|
+
/** Folds the letters people type two ways (Arabic yeh and kaf, digits, case) so a search finds both. */
|
|
21
|
+
export function foldSearch(text) {
|
|
22
|
+
return latinDigits(text).replace(/\u064A/gu, "\u06CC").replace(/\u0643/gu, "\u06A9").replace(/\u200C/gu, " ").toLowerCase().trim();
|
|
23
|
+
}
|
|
24
|
+
/** The rows of each tab, after a search over the titles; a day starts at the person's local midnight. */
|
|
25
|
+
export function historyTabs(rows, now, search = "") {
|
|
26
|
+
const start = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
|
|
27
|
+
const q = foldSearch(search);
|
|
28
|
+
const found = q.length === 0 ? [...rows] : rows.filter((r) => foldSearch(r.title).includes(q));
|
|
29
|
+
return {
|
|
30
|
+
today: found.filter((r) => r.updatedAt >= start),
|
|
31
|
+
earlier: found.filter((r) => r.updatedAt < start),
|
|
32
|
+
pinned: found.filter((r) => r.pinned),
|
|
33
|
+
waiting: found.filter((r) => r.waiting),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** How many rows each tab has. */
|
|
37
|
+
export function tabCounts(tabs) {
|
|
38
|
+
return { today: tabs.today.length, earlier: tabs.earlier.length, pinned: tabs.pinned.length, waiting: tabs.waiting.length };
|
|
39
|
+
}
|
package/dist/host.d.ts
CHANGED
|
@@ -41,6 +41,16 @@ export interface AssistantHost {
|
|
|
41
41
|
copy?(text: string): Promise<void>;
|
|
42
42
|
openUrl?(url: string): void;
|
|
43
43
|
pickFile?(accept: string[]): Promise<HostFile | null>;
|
|
44
|
+
/**
|
|
45
|
+
* A device's own picker and uploader: it lets the person pick a file and sends it as `multipart/form-data` (field
|
|
46
|
+
* `file`, with the host's own `Authorization`) to `path`, so the bytes never cross a bridge. It returns the server's
|
|
47
|
+
* answer as it came, or `null` when the person cancelled. When present, the composer uses it instead of `pickFile`.
|
|
48
|
+
* The server takes such an upload only from a request its `IRequestGuard` vouches for (a bearer token).
|
|
49
|
+
*/
|
|
50
|
+
uploadFile?(path: string, accept: string[]): Promise<{
|
|
51
|
+
status: number;
|
|
52
|
+
body: string;
|
|
53
|
+
} | null>;
|
|
44
54
|
pickImage?(): Promise<HostFile | null>;
|
|
45
55
|
saveFile?(name: string, data: Uint8Array | string): Promise<boolean>;
|
|
46
56
|
share?(text: string): Promise<void>;
|
package/dist/host.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
export function can(host, feature) {
|
|
4
4
|
switch (feature) {
|
|
5
5
|
case "copy": return typeof host.copy === "function";
|
|
6
|
-
case "attach": return typeof host.pickFile === "function" || typeof host.pickImage === "function";
|
|
6
|
+
case "attach": return typeof host.pickFile === "function" || typeof host.uploadFile === "function" || typeof host.pickImage === "function";
|
|
7
7
|
case "save": return typeof host.saveFile === "function";
|
|
8
8
|
case "share": return typeof host.share === "function";
|
|
9
9
|
case "stream": return typeof host.eventSource === "function";
|
package/dist/i18n.js
CHANGED
|
@@ -52,6 +52,12 @@ const fa = {
|
|
|
52
52
|
"history.pinned": "سنجاقشده",
|
|
53
53
|
"history.waiting": "منتظر",
|
|
54
54
|
"history.search": "جستوجو",
|
|
55
|
+
"csv.save": "ذخیره به CSV",
|
|
56
|
+
"csv.saved": "ذخیره شد",
|
|
57
|
+
"csv.not_saved": "ذخیره نشد",
|
|
58
|
+
"pdf.download": "دریافت PDF",
|
|
59
|
+
"pdf.busy": "در حال آمادهسازی PDF…",
|
|
60
|
+
"pdf.failed": "PDF ساخته نشد",
|
|
55
61
|
"panel.close": "بستن",
|
|
56
62
|
"panel.new": "گفتوگوی تازه",
|
|
57
63
|
"files.remove": "حذف فایل",
|
|
@@ -113,6 +119,12 @@ const en = {
|
|
|
113
119
|
"history.pinned": "Pinned",
|
|
114
120
|
"history.waiting": "Waiting",
|
|
115
121
|
"history.search": "Search",
|
|
122
|
+
"csv.save": "Save as CSV",
|
|
123
|
+
"csv.saved": "Saved",
|
|
124
|
+
"csv.not_saved": "Not saved",
|
|
125
|
+
"pdf.download": "Download PDF",
|
|
126
|
+
"pdf.busy": "Preparing the PDF…",
|
|
127
|
+
"pdf.failed": "Could not make the PDF",
|
|
116
128
|
"panel.close": "Close",
|
|
117
129
|
"panel.new": "New conversation",
|
|
118
130
|
"files.remove": "Remove file",
|
package/dist/pages.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AssistantApi } from "./api.js";
|
|
2
|
+
import type { Assistant } from "./assistant.js";
|
|
3
|
+
import type { Texts } from "./i18n.js";
|
|
4
|
+
export * from "./csv.js";
|
|
5
|
+
export * from "./history.js";
|
|
6
|
+
export * from "./routine.js";
|
|
7
|
+
/** The calls of the person's pages, over the API's plain verbs. */
|
|
8
|
+
export declare const pageCalls: {
|
|
9
|
+
/** Pins or unpins a conversation (pinned ones come first in the list). */
|
|
10
|
+
pin: (api: AssistantApi, conversationId: string, pinned: boolean) => Promise<unknown>;
|
|
11
|
+
/** Deletes a conversation of the person. */
|
|
12
|
+
remove: (api: AssistantApi, conversationId: string) => Promise<unknown>;
|
|
13
|
+
/** The routines the person may run, with their slots (`gaps`). */
|
|
14
|
+
routines: (api: AssistantApi) => Promise<unknown>;
|
|
15
|
+
/** What fills an entity slot: the host's things of a kind that match the text. */
|
|
16
|
+
pick: (api: AssistantApi, kind: string, q: string) => Promise<unknown>;
|
|
17
|
+
/** A routine's preview with the slots' values: a card to confirm, or the report of a read-only routine. */
|
|
18
|
+
preview: (api: AssistantApi, routineId: string, values: Readonly<Record<string, string>>, conversationId?: string) => Promise<unknown>;
|
|
19
|
+
/** Runs a previewed routine with the person's confirmation. */
|
|
20
|
+
run: (api: AssistantApi, routineId: string, routineRunId: string, hash: string, code?: string) => Promise<unknown>;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Starts a run of a non-conversation capability (`POST capabilities/{id}/runs`, for a host's own screens without a
|
|
24
|
+
* chat) and follows it on the assistant: `snapshot().run` shows its stages, then its `result` (output, provenance).
|
|
25
|
+
* Returns the run's id; a refusal throws its `ApiError` (`assistant-limit` with the limit reached).
|
|
26
|
+
*/
|
|
27
|
+
export declare function runCapability(assistant: Assistant, id: string, options?: {
|
|
28
|
+
input?: Record<string, unknown>;
|
|
29
|
+
key?: string;
|
|
30
|
+
}): Promise<string>;
|
|
31
|
+
/** The pages' texts over the assistant's: the host's texts and the core's win, then the pages' own. */
|
|
32
|
+
export declare function pageTexts(texts: Texts): Texts;
|
|
33
|
+
/** Every key the pages add (for hosts that translate to another language). */
|
|
34
|
+
export declare function pageKeys(): string[];
|
package/dist/pages.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createTexts } from "./i18n.js";
|
|
2
|
+
export * from "./csv.js";
|
|
3
|
+
export * from "./history.js";
|
|
4
|
+
export * from "./routine.js";
|
|
5
|
+
const enc = encodeURIComponent;
|
|
6
|
+
/** The calls of the person's pages, over the API's plain verbs. */
|
|
7
|
+
export const pageCalls = {
|
|
8
|
+
/** Pins or unpins a conversation (pinned ones come first in the list). */
|
|
9
|
+
pin: (api, conversationId, pinned) => api.patch("conversations/" + enc(conversationId), { pinned }),
|
|
10
|
+
/** Deletes a conversation of the person. */
|
|
11
|
+
remove: (api, conversationId) => api.delete("conversations/" + enc(conversationId)),
|
|
12
|
+
/** The routines the person may run, with their slots (`gaps`). */
|
|
13
|
+
routines: (api) => api.get("routines"),
|
|
14
|
+
/** What fills an entity slot: the host's things of a kind that match the text. */
|
|
15
|
+
pick: (api, kind, q) => api.get("pick?kind=" + enc(kind) + "&q=" + enc(q)),
|
|
16
|
+
/** A routine's preview with the slots' values: a card to confirm, or the report of a read-only routine. */
|
|
17
|
+
preview: (api, routineId, values, conversationId) => api.post("routines/" + enc(routineId) + "/preview", conversationId ? { values, conversationId } : { values }),
|
|
18
|
+
/** Runs a previewed routine with the person's confirmation. */
|
|
19
|
+
run: (api, routineId, routineRunId, hash, code) => api.post("routines/" + enc(routineId) + "/run", code ? { routineRunId, hash, code } : { routineRunId, hash }),
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Starts a run of a non-conversation capability (`POST capabilities/{id}/runs`, for a host's own screens without a
|
|
23
|
+
* chat) and follows it on the assistant: `snapshot().run` shows its stages, then its `result` (output, provenance).
|
|
24
|
+
* Returns the run's id; a refusal throws its `ApiError` (`assistant-limit` with the limit reached).
|
|
25
|
+
*/
|
|
26
|
+
export async function runCapability(assistant, id, options = {}) {
|
|
27
|
+
const r = await assistant.api.post("capabilities/" + enc(id) + "/runs", options);
|
|
28
|
+
const runId = typeof r === "object" && r !== null ? r.runId : undefined;
|
|
29
|
+
if (typeof runId !== "string" || runId.length === 0)
|
|
30
|
+
throw new Error("assistant api: no runId");
|
|
31
|
+
assistant.follow(runId);
|
|
32
|
+
return runId;
|
|
33
|
+
}
|
|
34
|
+
/** The words of the person's pages (history, routine wizard), both languages, kept out of the core every surface loads. */
|
|
35
|
+
const PAGE_FA = {
|
|
36
|
+
"history.tabs": "گفتوگوها",
|
|
37
|
+
"history.empty": "چیزی اینجا نیست",
|
|
38
|
+
"history.untitled": "بینام",
|
|
39
|
+
"routine.list": "روالها",
|
|
40
|
+
"routine.empty": "هنوز روالی نیست",
|
|
41
|
+
"routine.search": "جستوجو…",
|
|
42
|
+
"routine.none_found": "چیزی پیدا نشد",
|
|
43
|
+
"routine.change": "تغییر",
|
|
44
|
+
"routine.failed": "اجرا نشد",
|
|
45
|
+
"history.pin": "سنجاق",
|
|
46
|
+
"history.unpin": "برداشتن سنجاق",
|
|
47
|
+
"history.delete": "حذف",
|
|
48
|
+
"history.delete_confirm": "این گفتوگو حذف شود؟",
|
|
49
|
+
"routine.choose": "انتخاب",
|
|
50
|
+
"routine.back": "بازگشت",
|
|
51
|
+
"routine.preview": "پیشنمایش",
|
|
52
|
+
"routine.missing": "لازم است",
|
|
53
|
+
"routine.invalid": "درست نیست",
|
|
54
|
+
"routine.done": "انجام شد",
|
|
55
|
+
};
|
|
56
|
+
const PAGE_EN = {
|
|
57
|
+
"history.tabs": "Conversations",
|
|
58
|
+
"history.empty": "Nothing here",
|
|
59
|
+
"history.untitled": "Untitled",
|
|
60
|
+
"routine.list": "Routines",
|
|
61
|
+
"routine.empty": "No routines yet",
|
|
62
|
+
"routine.search": "Search…",
|
|
63
|
+
"routine.none_found": "Nothing found",
|
|
64
|
+
"routine.change": "Change",
|
|
65
|
+
"routine.failed": "It did not run",
|
|
66
|
+
"history.pin": "Pin",
|
|
67
|
+
"history.unpin": "Unpin",
|
|
68
|
+
"history.delete": "Delete",
|
|
69
|
+
"history.delete_confirm": "Delete this conversation?",
|
|
70
|
+
"routine.choose": "Choose",
|
|
71
|
+
"routine.back": "Back",
|
|
72
|
+
"routine.preview": "Preview",
|
|
73
|
+
"routine.missing": "Required",
|
|
74
|
+
"routine.invalid": "Not right",
|
|
75
|
+
"routine.done": "Done",
|
|
76
|
+
};
|
|
77
|
+
/** The pages' texts over the assistant's: the host's texts and the core's win, then the pages' own. */
|
|
78
|
+
export function pageTexts(texts) {
|
|
79
|
+
const own = createTexts(texts.locale, texts.locale === "fa" ? PAGE_FA : PAGE_EN);
|
|
80
|
+
return {
|
|
81
|
+
locale: texts.locale,
|
|
82
|
+
t(key, args) {
|
|
83
|
+
const v = texts.t(key, args);
|
|
84
|
+
return v !== key ? v : own.t(key, args);
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** Every key the pages add (for hosts that translate to another language). */
|
|
89
|
+
export function pageKeys() {
|
|
90
|
+
return Object.keys(PAGE_EN).sort();
|
|
91
|
+
}
|
|
@@ -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@g9tm/aiassistant",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Headless client of the G9 AI assistant: API client, run state, transports, block view models, safe text and Persian helpers. No DOM, no framework.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -19,6 +19,10 @@
|
|
|
19
19
|
"./admin": {
|
|
20
20
|
"types": "./dist/admin.d.ts",
|
|
21
21
|
"import": "./dist/admin.js"
|
|
22
|
+
},
|
|
23
|
+
"./pages": {
|
|
24
|
+
"types": "./dist/pages.d.ts",
|
|
25
|
+
"import": "./dist/pages.js"
|
|
22
26
|
}
|
|
23
27
|
},
|
|
24
28
|
"files": [
|
|
@@ -29,7 +33,7 @@
|
|
|
29
33
|
"access": "public"
|
|
30
34
|
},
|
|
31
35
|
"dependencies": {
|
|
32
|
-
"@g9tm/aiassistant-contracts": "1.
|
|
36
|
+
"@g9tm/aiassistant-contracts": "1.2.0"
|
|
33
37
|
},
|
|
34
38
|
"scripts": {
|
|
35
39
|
"build": "tsc -p tsconfig.json",
|