@g9tm/aiassistant 1.6.0 → 1.6.1
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/admin.js +2 -2
- package/dist/api.d.ts +5 -0
- package/dist/api.js +9 -0
- package/dist/assistant.js +3 -0
- package/dist/blocks.js +10 -10
- package/dist/conversation.d.ts +2 -0
- package/dist/conversation.js +6 -0
- package/dist/csv.d.ts +0 -1
- package/dist/csv.js +3 -1
- package/dist/pages.js +1 -1
- package/package.json +2 -2
package/dist/admin.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* one entry-test probe per request, the usage ledger with the model-free rate, a run's black box, the usage windows
|
|
4
4
|
* and the audit; a separate entry (`@g9tm/aiassistant/admin`), loaded only by the manager's pages. Every server answer is read with type checks only; renderers draw what these functions return.
|
|
5
5
|
*/
|
|
6
|
-
import { arr, num, obj, str } from "./api.js";
|
|
6
|
+
import { arr, item, num, obj, str } from "./api.js";
|
|
7
7
|
import { createTexts } from "./i18n.js";
|
|
8
8
|
const ADMIN_TEXTS = new Map([
|
|
9
9
|
["fa", {
|
|
@@ -231,7 +231,7 @@ export class AdminClient {
|
|
|
231
231
|
}
|
|
232
232
|
async backends() {
|
|
233
233
|
return arr(obj(await this.api.get("admin/backends"))["backends"]).map(obj).map((b) => ({
|
|
234
|
-
id: str(b["id"]), state: str(b["state"]), until: nul(b["until"]), usage: b["usage"] ? readWindows({ backends: [b["usage"]] })
|
|
234
|
+
id: str(b["id"]), state: str(b["state"]), until: nul(b["until"]), usage: b["usage"] ? item(readWindows({ backends: [b["usage"]] }), 0) ?? null : null,
|
|
235
235
|
}));
|
|
236
236
|
}
|
|
237
237
|
async gatewayKeys() {
|
package/dist/api.d.ts
CHANGED
|
@@ -33,6 +33,11 @@ export declare function obj(v: unknown): Record<string, unknown>;
|
|
|
33
33
|
export declare function str(v: unknown, fallback?: string): string;
|
|
34
34
|
export declare function num(v: unknown, fallback?: number): number;
|
|
35
35
|
export declare function arr(v: unknown): unknown[];
|
|
36
|
+
/**
|
|
37
|
+
* The item at an index (negative counts from the end), or undefined: `Array.prototype.at` is ES2022 and the Lynx
|
|
38
|
+
* engine on a phone (PrimJS) has none, so library code never calls it.
|
|
39
|
+
*/
|
|
40
|
+
export declare function item<T>(xs: readonly T[], i: number): T | undefined;
|
|
36
41
|
export declare class AssistantApi {
|
|
37
42
|
private readonly host;
|
|
38
43
|
private readonly base;
|
package/dist/api.js
CHANGED
|
@@ -20,6 +20,15 @@ export function num(v, fallback = 0) {
|
|
|
20
20
|
export function arr(v) {
|
|
21
21
|
return Array.isArray(v) ? v : [];
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* The item at an index (negative counts from the end), or undefined: `Array.prototype.at` is ES2022 and the Lynx
|
|
25
|
+
* engine on a phone (PrimJS) has none, so library code never calls it.
|
|
26
|
+
*/
|
|
27
|
+
export function item(xs, i) {
|
|
28
|
+
const k = i < 0 ? xs.length + i : i;
|
|
29
|
+
// eslint-disable-next-line security/detect-object-injection -- a number index into an array, bounds checked
|
|
30
|
+
return k >= 0 && k < xs.length ? xs[k] : undefined;
|
|
31
|
+
}
|
|
23
32
|
export class AssistantApi {
|
|
24
33
|
constructor(host, base = "/api/assistant/") {
|
|
25
34
|
this.host = host;
|
package/dist/assistant.js
CHANGED
|
@@ -111,6 +111,7 @@ export class Assistant {
|
|
|
111
111
|
return true;
|
|
112
112
|
}
|
|
113
113
|
catch (e) {
|
|
114
|
+
this.store.settle(); // not sent: the question stays, no longer dim
|
|
114
115
|
this.error = e instanceof ApiError ? e.code : "error.network";
|
|
115
116
|
this.emit();
|
|
116
117
|
return false;
|
|
@@ -129,6 +130,8 @@ export class Assistant {
|
|
|
129
130
|
if (!this.run || this.run.runId !== runId)
|
|
130
131
|
return;
|
|
131
132
|
this.run = reduceRun(this.run, e, timer(this.host).now());
|
|
133
|
+
if (e.type === "run.finished")
|
|
134
|
+
this.store.settle(); // answered or not, the question is no longer waiting
|
|
132
135
|
if (e.type === "message") {
|
|
133
136
|
const added = this.store.merge([e.body.message]);
|
|
134
137
|
for (const m of added)
|
package/dist/blocks.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* wordy table becomes cards, a long one folds after 8 rows, a ranking is horizontal bars (at most 10), a line only on a
|
|
5
5
|
* time axis, and too little data shows no percentages or averages.
|
|
6
6
|
*/
|
|
7
|
-
import { arr, num, obj, str } from "./api.js";
|
|
7
|
+
import { arr, item, num, obj, str } from "./api.js";
|
|
8
8
|
import { formatNumber } from "./fa.js";
|
|
9
9
|
/** A table with more columns than this, or long texts, shows each row as a card. */
|
|
10
10
|
export const CARD_COLUMNS = 4;
|
|
@@ -55,7 +55,7 @@ export function blockView(raw, locale, custom = new Set()) {
|
|
|
55
55
|
const cols = columns(b["columns"]);
|
|
56
56
|
if (cols.length === 0)
|
|
57
57
|
return { type: "fallback", fallbackText };
|
|
58
|
-
const rows = arr(b["rows"]).map((r) => cols.map((c, i) => cell(arr(r)
|
|
58
|
+
const rows = arr(b["rows"]).map((r) => cols.map((c, i) => cell(item(arr(r), i), c.kind, locale)));
|
|
59
59
|
const wordy = rows.some((r) => r.some((c) => c.length > 40));
|
|
60
60
|
const shown = rows.length > FOLD_ROWS ? FOLD_ROWS : rows.length;
|
|
61
61
|
return withTitle({ type: "table", columns: cols, rows, asCards: cols.length > CARD_COLUMNS || wordy, shown, more: rows.length - shown, truncated: b["truncated"] === true, fallbackText });
|
|
@@ -64,7 +64,7 @@ export function blockView(raw, locale, custom = new Set()) {
|
|
|
64
64
|
case "kpi": {
|
|
65
65
|
const cs = columns(b["columns"]);
|
|
66
66
|
const row = arr(arr(b["rows"])[0]);
|
|
67
|
-
const items = cs.map((c, i) => ({ label: c.label, value: cell(row
|
|
67
|
+
const items = cs.map((c, i) => ({ label: c.label, value: cell(item(row, i), c.kind, locale) }));
|
|
68
68
|
return items.length > 0 ? { type: "kpi", items, fallbackText } : { type: "fallback", fallbackText };
|
|
69
69
|
}
|
|
70
70
|
case "steps": return { type: "steps", items: arr(b["items"]).map(obj).map((s) => ({ text: str(s["text"]), state: str(s["state"], "todo") })), fallbackText };
|
|
@@ -76,7 +76,7 @@ export function blockView(raw, locale, custom = new Set()) {
|
|
|
76
76
|
case "export": {
|
|
77
77
|
// every row and column, as raw text: the file is for a spreadsheet, not for reading here
|
|
78
78
|
const cols = columns(b["columns"]);
|
|
79
|
-
const rows = arr(b["rows"]).map((r) => cols.map((_, i) => { const v = arr(r)
|
|
79
|
+
const rows = arr(b["rows"]).map((r) => cols.map((_, i) => { const v = item(arr(r), i); return v === null || v === undefined ? "" : typeof v === "string" ? v : String(v); }));
|
|
80
80
|
return withTitle({ type: "export", format: str(b["format"], "csv"), columns: cols, rows, fallbackText });
|
|
81
81
|
}
|
|
82
82
|
default: return { type: "fallback", fallbackText }; // compare, gauge, timeline, items: drawn by the host or shown as text
|
|
@@ -89,12 +89,12 @@ function chart(b, cs, fallbackText) {
|
|
|
89
89
|
const rows = arr(b["rows"]).map(arr);
|
|
90
90
|
const x = str(b["x"], cs[0]?.name ?? "");
|
|
91
91
|
const xIndex = Math.max(0, cs.findIndex((c) => c.name === x));
|
|
92
|
-
const xCol = cs
|
|
93
|
-
const valueCols = cs.filter((_, i) => i !== xIndex && ["number", "money", "percent"].includes(cs
|
|
92
|
+
const xCol = item(cs, xIndex);
|
|
93
|
+
const valueCols = cs.filter((_, i) => i !== xIndex && ["number", "money", "percent"].includes(item(cs, i)?.kind ?? ""));
|
|
94
94
|
if (!xCol || valueCols.length === 0 || rows.length === 0)
|
|
95
95
|
return { type: "fallback", fallbackText };
|
|
96
|
-
const labels = rows.map((r) => (r
|
|
97
|
-
const series = valueCols.map((c) => ({ name: c.label, values: rows.map((r) => num(r
|
|
96
|
+
const labels = rows.map((r) => (item(r, xIndex) === null || item(r, xIndex) === undefined ? "" : String(item(r, xIndex))));
|
|
97
|
+
const series = valueCols.map((c) => ({ name: c.label, values: rows.map((r) => num(item(r, cs.indexOf(c)))) }));
|
|
98
98
|
const timeAxis = xCol.kind === "date" || xCol.kind === "day";
|
|
99
99
|
const asked = str(b["kind"]);
|
|
100
100
|
let kind;
|
|
@@ -107,8 +107,8 @@ function chart(b, cs, fallbackText) {
|
|
|
107
107
|
else
|
|
108
108
|
kind = timeAxis ? "line" : "hbars";
|
|
109
109
|
if (kind === "hbars") {
|
|
110
|
-
const order = labels.map((_, i) => i).sort((a, c) => (series[0]?.values
|
|
111
|
-
return { type: "chart", kind, labels: order.map((i) => labels
|
|
110
|
+
const order = labels.map((_, i) => i).sort((a, c) => (item(series[0]?.values ?? [], c) ?? 0) - (item(series[0]?.values ?? [], a) ?? 0)).slice(0, RANK_BARS);
|
|
111
|
+
return { type: "chart", kind, labels: order.map((i) => item(labels, i) ?? ""), series: series.map((s) => ({ name: s.name, values: order.map((i) => item(s.values, i) ?? 0) })), fewData: rows.length < FEW_DATA, fallbackText };
|
|
112
112
|
}
|
|
113
113
|
return { type: "chart", kind, labels, series, fewData: rows.length < FEW_DATA, fallbackText };
|
|
114
114
|
}
|
package/dist/conversation.d.ts
CHANGED
|
@@ -31,6 +31,8 @@ export declare class ConversationStore {
|
|
|
31
31
|
merge(raw: readonly unknown[]): MessageView[];
|
|
32
32
|
/** Shows the person's question at once, until the server's copy replaces it. */
|
|
33
33
|
optimistic(text: string, at: string): MessageView;
|
|
34
|
+
/** The question stays, no longer dim: its run ended (answered or not) or it could not be sent; the server's copy still replaces it. */
|
|
35
|
+
settle(): void;
|
|
34
36
|
private dropPending;
|
|
35
37
|
/** Clears the store for another conversation. */
|
|
36
38
|
reset(id: string | null): void;
|
package/dist/conversation.js
CHANGED
|
@@ -59,6 +59,12 @@ export class ConversationStore {
|
|
|
59
59
|
this.pendingId = id;
|
|
60
60
|
return m;
|
|
61
61
|
}
|
|
62
|
+
/** The question stays, no longer dim: its run ended (answered or not) or it could not be sent; the server's copy still replaces it. */
|
|
63
|
+
settle() {
|
|
64
|
+
const m = this.pendingId === null ? undefined : this.byId.get(this.pendingId);
|
|
65
|
+
if (m && this.pendingId !== null)
|
|
66
|
+
this.byId.set(this.pendingId, { ...m, pending: false });
|
|
67
|
+
}
|
|
62
68
|
dropPending() {
|
|
63
69
|
if (this.pendingId === null)
|
|
64
70
|
return;
|
package/dist/csv.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/** A table as a CSV file a spreadsheet opens right: a BOM for the encoding, every cell quoted, no formula injection. */
|
|
2
1
|
import type { AssistantHost } from "./host.js";
|
|
3
2
|
/** One cell: quoted, with inner quotes doubled; a cell a spreadsheet would run as a formula gets a leading apostrophe. */
|
|
4
3
|
export declare function csvCell(value: string): string;
|
package/dist/csv.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** A table as a CSV file a spreadsheet opens right: a BOM for the encoding, every cell quoted, no formula injection. */
|
|
2
|
+
import { item } from "./api.js";
|
|
1
3
|
const BOM = "\uFEFF";
|
|
2
4
|
/** One cell: quoted, with inner quotes doubled; a cell a spreadsheet would run as a formula gets a leading apostrophe. */
|
|
3
5
|
export function csvCell(value) {
|
|
@@ -6,7 +8,7 @@ export function csvCell(value) {
|
|
|
6
8
|
}
|
|
7
9
|
/** The whole file: a BOM, a header row of the column labels, then the rows, lines ended with CRLF. */
|
|
8
10
|
export function toCsv(labels, rows) {
|
|
9
|
-
const lines = [labels.map(csvCell).join(","), ...rows.map((r) => labels.map((_, i) => csvCell(r
|
|
11
|
+
const lines = [labels.map(csvCell).join(","), ...rows.map((r) => labels.map((_, i) => csvCell(item(r, i) ?? "")).join(","))];
|
|
10
12
|
return BOM + lines.join("\r\n") + "\r\n";
|
|
11
13
|
}
|
|
12
14
|
/** A file name from a title: letters, digits and dashes only, with the extension. */
|
package/dist/pages.js
CHANGED
|
@@ -169,7 +169,7 @@ export function withHostTexts(texts, own) {
|
|
|
169
169
|
t(key, args) {
|
|
170
170
|
const v = texts.t(key, args);
|
|
171
171
|
const mine = own();
|
|
172
|
-
const words = Object.
|
|
172
|
+
const words = Object.prototype.hasOwnProperty.call(mine, key) ? new Map(Object.entries(mine)).get(key) : undefined; // never a prototype's member
|
|
173
173
|
return v !== key || words === undefined ? v : createTexts(texts.locale, { [key]: words }).t(key, args);
|
|
174
174
|
},
|
|
175
175
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@g9tm/aiassistant",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.1",
|
|
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": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@g9tm/aiassistant-contracts": "1.6.
|
|
36
|
+
"@g9tm/aiassistant-contracts": "1.6.1"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsc -p tsconfig.json",
|