@g9tm/aiassistant 1.4.1 → 1.5.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 CHANGED
@@ -61,9 +61,7 @@ export declare class AssistantApi {
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
63
  upload(name: string, bytes: Uint8Array): Promise<UploadedFile>;
64
- /** Where a host's own uploader sends a file (`AssistantHost.uploadFile`). */
65
64
  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
65
  readUpload(answer: {
68
66
  status: number;
69
67
  body: string;
@@ -81,7 +79,6 @@ export declare class AssistantApi {
81
79
  delete(path: string): Promise<unknown>;
82
80
  }
83
81
  export declare function toRawEvent(v: unknown): RawEvent;
84
- /** A file the server took. */
85
82
  export interface UploadedFile {
86
83
  id: string;
87
84
  kind: string;
package/dist/api.js CHANGED
@@ -96,9 +96,7 @@ export class AssistantApi {
96
96
  async upload(name, bytes) {
97
97
  return uploaded(await this.request("POST", "files?name=" + encodeURIComponent(name), undefined, { bytes, type: "application/octet-stream" }));
98
98
  }
99
- /** Where a host's own uploader sends a file (`AssistantHost.uploadFile`). */
100
99
  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
100
  readUpload(answer) { return uploaded(answerOf(answer.status, answer.body)); }
103
101
  /** Dictation: audio to text on the server (the audio is not kept). */
104
102
  async speech(bytes, mime) {
@@ -125,7 +123,6 @@ function uploaded(v) {
125
123
  out.rows = r["rows"];
126
124
  return out;
127
125
  }
128
- /** A response's JSON (or its text), or the `ApiError` of a status outside 2xx. */
129
126
  function answerOf(status, text) {
130
127
  let parsed = null;
131
128
  if (text.length > 0) {
@@ -36,7 +36,6 @@ export interface AssistantSnapshot {
36
36
  state: AssistantState;
37
37
  error: string | null;
38
38
  }
39
- /** The files the composer offers by default. */
40
39
  export declare const ATTACH_ACCEPT: readonly string[];
41
40
  export declare class Assistant {
42
41
  readonly host: AssistantHost;
@@ -83,10 +82,7 @@ export declare class Assistant {
83
82
  answer(runId: string, cardId: string, picked: string[], text: string | null): Promise<boolean>;
84
83
  /** Answers a "learn this?" or "make it a routine?" card once. */
85
84
  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
- */
85
+ /** The attach button: the host's own uploader, or its picker. */
90
86
  attachFromDevice(accept?: readonly string[]): Promise<void>;
91
87
  attach(name: string, bytes: Uint8Array): Promise<void>;
92
88
  detach(id: string): void;
package/dist/assistant.js CHANGED
@@ -7,13 +7,12 @@ import { ApiError, AssistantApi } from "./api.js";
7
7
  import { ConversationStore } from "./conversation.js";
8
8
  import { timer } from "./host.js";
9
9
  import { createTexts } from "./i18n.js";
10
- import { AnsweredCards, WAITING, afterConfirm, confirmation, planView, toggle } from "./plan.js";
10
+ import { AnsweredCards, REMOTE_EXPIRED, WAITING, afterConfirm, confirmation, planView, toggle } from "./plan.js";
11
11
  import { Watched } from "./reveal.js";
12
12
  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
16
  export const ATTACH_ACCEPT = [".pdf", ".xlsx", ".csv", "image/*"];
18
17
  export class Assistant {
19
18
  constructor(host, options = {}) {
@@ -174,9 +173,20 @@ export class Assistant {
174
173
  this.plans.set(planId, next);
175
174
  this.emit();
176
175
  if (next.failCode === WAITING) {
177
- // Approved on another device: ask again in a few seconds (a typed code is asked of the person again instead).
178
- if (p.method === "remote" && tries < 60)
179
- timer(this.host).set(() => void this.confirm(planId, code, tries + 1), 3000);
176
+ // another device: ask again until its approval ends, then say it ran out
177
+ if (p.method === "remote") {
178
+ const t = timer(this.host);
179
+ const left = (Date.parse(next.waitingUntil ?? next.expiresAt ?? "") || t.now() + (60 - tries) * 3000) - t.now();
180
+ t.set(() => {
181
+ const q = this.plans.get(planId);
182
+ if (left > 3000)
183
+ void this.confirm(planId, code, tries + 1);
184
+ else if (q?.failCode === WAITING) {
185
+ this.plans.set(planId, { ...q, failCode: REMOTE_EXPIRED });
186
+ this.emit();
187
+ }
188
+ }, Math.min(3000, Math.max(0, left)));
189
+ }
180
190
  return false;
181
191
  }
182
192
  if (this.run)
@@ -223,10 +233,7 @@ export class Assistant {
223
233
  await this.api.card(kind, cardId, verb);
224
234
  return true;
225
235
  }
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
- */
236
+ /** The attach button: the host's own uploader, or its picker. */
230
237
  async attachFromDevice(accept = ATTACH_ACCEPT) {
231
238
  const up = this.host.uploadFile;
232
239
  if (!up) {
package/dist/csv.js CHANGED
@@ -6,7 +6,7 @@ export function csvCell(value) {
6
6
  }
7
7
  /** The whole file: a BOM, a header row of the column labels, then the rows, lines ended with CRLF. */
8
8
  export function toCsv(labels, rows) {
9
- const lines = [labels.map(csvCell).join(","), ...rows.map((r) => labels.map((_, i) => csvCell(r[i] ?? "")).join(","))];
9
+ const lines = [labels.map(csvCell).join(","), ...rows.map((r) => labels.map((_, i) => csvCell(r.at(i) ?? "")).join(","))];
10
10
  return BOM + lines.join("\r\n") + "\r\n";
11
11
  }
12
12
  /** A file name from a title: letters, digits and dashes only, with the extension. */
package/dist/pages.js CHANGED
@@ -69,6 +69,8 @@ const PAGE_FA = {
69
69
  "page.findings.empty": "چیزی نیاز به توجه ندارد",
70
70
  "page.actions.empty": "هنوز کاری انجام نشده است",
71
71
  "routine.type_to_search": "نامش را بنویسید",
72
+ "confirm.remote_expired": "تأیید روی گوشی منقضی شد؛ دوباره بخواهید یا کد را وارد کنید",
73
+ "confirm.remote_refused": "روی گوشی رد شد",
72
74
  };
73
75
  const PAGE_EN = {
74
76
  "history.tabs": "Conversations",
@@ -107,6 +109,8 @@ const PAGE_EN = {
107
109
  "page.findings.empty": "Nothing needs attention",
108
110
  "page.actions.empty": "No actions yet",
109
111
  "routine.type_to_search": "Type its name",
112
+ "confirm.remote_expired": "The approval on your phone expired: ask again or enter the code",
113
+ "confirm.remote_refused": "It was refused on your phone",
110
114
  };
111
115
  /** The pages' texts over the assistant's: the host's texts and the core's win, then the pages' own. */
112
116
  export function pageTexts(texts) {
@@ -146,7 +150,8 @@ export function withHostTexts(texts, own) {
146
150
  locale: texts.locale,
147
151
  t(key, args) {
148
152
  const v = texts.t(key, args);
149
- const words = own()[key];
153
+ const mine = own();
154
+ const words = Object.hasOwn(mine, key) ? new Map(Object.entries(mine)).get(key) : undefined; // never a prototype's member
150
155
  return v !== key || words === undefined ? v : createTexts(texts.locale, { [key]: words }).t(key, args);
151
156
  },
152
157
  };
package/dist/plan.d.ts CHANGED
@@ -34,6 +34,8 @@ export interface PlanView {
34
34
  answered: "confirmed" | "refused" | null;
35
35
  triesLeft: number | null;
36
36
  failCode: string | null;
37
+ /** Until when another device's approval is waited for. */
38
+ waitingUntil?: string | null;
37
39
  }
38
40
  /** The card's view model from the server's card body. */
39
41
  export declare function planView(card: unknown): PlanView;
@@ -50,6 +52,7 @@ export declare function confirmation(plan: PlanView, code: string | null): {
50
52
  } | null;
51
53
  /** The server's answer that an approval (the person's phone, a second person) has not come yet: not an error, not done. */
52
54
  export declare const WAITING = "confirm.waiting";
55
+ export declare const REMOTE_EXPIRED = "confirm.remote_expired";
53
56
  /** The card after the server's answer to a confirmation. */
54
57
  export declare function afterConfirm(plan: PlanView, ok: boolean, body: unknown): PlanView;
55
58
  /** Cards that take one answer (ask, learn, routine): the first answer stays; a second is ignored. */
package/dist/plan.js CHANGED
@@ -86,15 +86,16 @@ export function confirmation(plan, code) {
86
86
  return null;
87
87
  if (needsCode(plan.method) && (code === null || code.trim().length === 0))
88
88
  return null;
89
- return { planId: plan.id, hash: plan.hash, code: needsCode(plan.method) ? code : null, skip };
89
+ return { planId: plan.id, hash: plan.hash, code: needsCode(plan.method) || plan.failCode === REMOTE_EXPIRED ? code : null, skip };
90
90
  }
91
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
92
  export const WAITING = "confirm.waiting";
93
+ export const REMOTE_EXPIRED = "confirm.remote_expired";
93
94
  /** The card after the server's answer to a confirmation. */
94
95
  export function afterConfirm(plan, ok, body) {
95
96
  const b = obj(body);
96
97
  if (ok && b["ok"] === false)
97
- return { ...plan, failCode: WAITING }; // 202: it waits for an approval
98
+ return { ...plan, failCode: WAITING, waitingUntil: typeof b["waitingUntil"] === "string" ? b["waitingUntil"] : plan.waitingUntil ?? null }; // 202: it waits for an approval
98
99
  if (ok)
99
100
  return { ...plan, answered: "confirmed", failCode: null };
100
101
  const e = b["error"];
package/dist/routine.js CHANGED
@@ -37,6 +37,7 @@ export function slotValue(slot, text) {
37
37
  return null;
38
38
  if (slot.field === "number" || slot.field === "money") {
39
39
  const n = t.replace(/[,\u066C\u060C\s]/gu, "").replace(/\u066B/gu, ".");
40
+ // eslint-disable-next-line security/detect-unsafe-regex -- one optional group after digits: linear, no backtracking blow-up
40
41
  return /^-?\d+(\.\d+)?$/u.test(n) ? n : null;
41
42
  }
42
43
  if (slot.field === "date")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@g9tm/aiassistant",
3
- "version": "1.4.1",
3
+ "version": "1.5.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": {
@@ -33,7 +33,7 @@
33
33
  "access": "public"
34
34
  },
35
35
  "dependencies": {
36
- "@g9tm/aiassistant-contracts": "1.4.1"
36
+ "@g9tm/aiassistant-contracts": "1.5.0"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "tsc -p tsconfig.json",