@g9tm/aiassistant 1.4.2 → 1.6.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
@@ -17,6 +17,8 @@ export interface SendInput {
17
17
  subjectId?: string;
18
18
  subjectLabel?: string;
19
19
  };
20
+ /** The model a person picked (`id` or `id@effort`), kept on the conversation; the host must let them choose. */
21
+ model?: string;
20
22
  }
21
23
  export interface EventPage {
22
24
  events: RawEvent[];
@@ -61,9 +63,7 @@ export declare class AssistantApi {
61
63
  feedback(conversationId: string, messageId: string, good: boolean, note?: string): Promise<unknown>;
62
64
  card(kind: "knowledge" | "learned", id: string, verb: string): Promise<unknown>;
63
65
  upload(name: string, bytes: Uint8Array): Promise<UploadedFile>;
64
- /** Where a host's own uploader sends a file (`AssistantHost.uploadFile`). */
65
66
  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
67
  readUpload(answer: {
68
68
  status: number;
69
69
  body: string;
@@ -81,7 +81,6 @@ export declare class AssistantApi {
81
81
  delete(path: string): Promise<unknown>;
82
82
  }
83
83
  export declare function toRawEvent(v: unknown): RawEvent;
84
- /** A file the server took. */
85
84
  export interface UploadedFile {
86
85
  id: string;
87
86
  kind: string;
package/dist/api.js CHANGED
@@ -59,6 +59,8 @@ export class AssistantApi {
59
59
  body["files"] = input.files;
60
60
  if (input.page)
61
61
  body["page"] = input.page;
62
+ if (input.model)
63
+ body["model"] = input.model;
62
64
  return this.request("POST", path, body).then((r) => ({ conversationId: str(obj(r)["conversationId"]), runId: str(obj(r)["runId"]) }));
63
65
  }
64
66
  async events(runId, after, wait) {
@@ -96,9 +98,7 @@ export class AssistantApi {
96
98
  async upload(name, bytes) {
97
99
  return uploaded(await this.request("POST", "files?name=" + encodeURIComponent(name), undefined, { bytes, type: "application/octet-stream" }));
98
100
  }
99
- /** Where a host's own uploader sends a file (`AssistantHost.uploadFile`). */
100
101
  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
102
  readUpload(answer) { return uploaded(answerOf(answer.status, answer.body)); }
103
103
  /** Dictation: audio to text on the server (the audio is not kept). */
104
104
  async speech(bytes, mime) {
@@ -125,7 +125,6 @@ function uploaded(v) {
125
125
  out.rows = r["rows"];
126
126
  return out;
127
127
  }
128
- /** A response's JSON (or its text), or the `ApiError` of a status outside 2xx. */
129
128
  function answerOf(status, text) {
130
129
  let parsed = null;
131
130
  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;
@@ -55,6 +54,8 @@ export declare class Assistant {
55
54
  /** Plans whose confirmation is in flight: no second confirmation and no refusal until it answers. */
56
55
  private readonly confirming;
57
56
  private attachments;
57
+ /** The model a person picked for what they send next (`id` or `id@effort`); null lets the route decide. */
58
+ model: string | null;
58
59
  private error;
59
60
  private stateNow;
60
61
  constructor(host: AssistantHost, options?: AssistantOptions);
@@ -83,10 +84,7 @@ export declare class Assistant {
83
84
  answer(runId: string, cardId: string, picked: string[], text: string | null): Promise<boolean>;
84
85
  /** Answers a "learn this?" or "make it a routine?" card once. */
85
86
  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
- */
87
+ /** The attach button: the host's own uploader, or its picker. */
90
88
  attachFromDevice(accept?: readonly string[]): Promise<void>;
91
89
  attach(name: string, bytes: Uint8Array): Promise<void>;
92
90
  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 = {}) {
@@ -28,6 +27,8 @@ export class Assistant {
28
27
  /** Plans whose confirmation is in flight: no second confirmation and no refusal until it answers. */
29
28
  this.confirming = new Set();
30
29
  this.attachments = [];
30
+ /** The model a person picked for what they send next (`id` or `id@effort`); null lets the route decide. */
31
+ this.model = null;
31
32
  this.error = null;
32
33
  this.api = new AssistantApi(host, options.base);
33
34
  this.texts = createTexts(host.locale, options.texts);
@@ -100,6 +101,8 @@ export class Assistant {
100
101
  input.files = this.attachments.map((a) => a.id);
101
102
  if (page)
102
103
  input.page = page;
104
+ if (this.model)
105
+ input.model = this.model;
103
106
  try {
104
107
  const t = await this.api.send(input);
105
108
  this.store.id = t.conversationId;
@@ -174,9 +177,20 @@ export class Assistant {
174
177
  this.plans.set(planId, next);
175
178
  this.emit();
176
179
  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);
180
+ // another device: ask again until its approval ends, then say it ran out
181
+ if (p.method === "remote") {
182
+ const t = timer(this.host);
183
+ const left = (Date.parse(next.waitingUntil ?? next.expiresAt ?? "") || t.now() + (60 - tries) * 3000) - t.now();
184
+ t.set(() => {
185
+ const q = this.plans.get(planId);
186
+ if (left > 3000)
187
+ void this.confirm(planId, code, tries + 1);
188
+ else if (q?.failCode === WAITING) {
189
+ this.plans.set(planId, { ...q, failCode: REMOTE_EXPIRED });
190
+ this.emit();
191
+ }
192
+ }, Math.min(3000, Math.max(0, left)));
193
+ }
180
194
  return false;
181
195
  }
182
196
  if (this.run)
@@ -223,10 +237,7 @@ export class Assistant {
223
237
  await this.api.card(kind, cardId, verb);
224
238
  return true;
225
239
  }
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
- */
240
+ /** The attach button: the host's own uploader, or its picker. */
230
241
  async attachFromDevice(accept = ATTACH_ACCEPT) {
231
242
  const up = this.host.uploadFile;
232
243
  if (!up) {
package/dist/history.d.ts CHANGED
@@ -7,7 +7,23 @@ export interface ConversationRow {
7
7
  waiting: boolean;
8
8
  /** When it last changed (milliseconds since 1970). */
9
9
  updatedAt: number;
10
+ /** The model it keeps (`id` or `id@effort`), if any. */
11
+ model?: string;
10
12
  }
13
+ /** A model a person may pick (`GET models`). */
14
+ export interface ModelChoice {
15
+ id: string;
16
+ kind: string;
17
+ ready: boolean;
18
+ /** Why it cannot answer now (`usage_limit`...), and when it opens again (milliseconds since 1970). */
19
+ unavailable?: string;
20
+ availableAt?: number;
21
+ }
22
+ /** Reads `GET models`: whether the person may pick at all, and the models to pick from. */
23
+ export declare function readModels(raw: unknown): {
24
+ choose: boolean;
25
+ models: ModelChoice[];
26
+ };
11
27
  export type HistoryTab = "today" | "earlier" | "pinned" | "waiting";
12
28
  export declare const HISTORY_TABS: readonly HistoryTab[];
13
29
  /** Reads the server's list (`GET conversations`), pinned first and then the newest. */
package/dist/history.js CHANGED
@@ -1,5 +1,19 @@
1
1
  /** The person's conversations as tabs: today, earlier, pinned and waiting for their code, each with its count. */
2
2
  import { latinDigits } from "./fa.js";
3
+ /** Reads `GET models`: whether the person may pick at all, and the models to pick from. */
4
+ export function readModels(raw) {
5
+ const r = obj(raw);
6
+ const list = Array.isArray(r["models"]) ? r["models"] : [];
7
+ const models = list.map(obj).filter((m) => typeof m["id"] === "string" && m["id"].length > 0).map((m) => {
8
+ const row = { id: m["id"], kind: typeof m["kind"] === "string" ? m["kind"] : "", ready: m["ready"] === true };
9
+ if (typeof m["unavailable"] === "string")
10
+ row.unavailable = m["unavailable"];
11
+ if (typeof m["availableAt"] === "number")
12
+ row.availableAt = m["availableAt"];
13
+ return row;
14
+ });
15
+ return { choose: r["choose"] === true, models };
16
+ }
3
17
  export const HISTORY_TABS = ["today", "earlier", "pinned", "waiting"];
4
18
  function obj(v) {
5
19
  return v !== null && typeof v === "object" && !Array.isArray(v) ? v : {};
@@ -14,6 +28,7 @@ export function readConversations(raw) {
14
28
  pinned: c["pinned"] === true,
15
29
  waiting: c["waiting"] === true,
16
30
  updatedAt: typeof c["updatedAt"] === "number" ? c["updatedAt"] : 0,
31
+ ...(typeof c["model"] === "string" ? { model: c["model"] } : {}),
17
32
  })).filter((c) => c.id.length > 0);
18
33
  return rows.sort((a, b) => Number(b.pinned) - Number(a.pinned) || b.updatedAt - a.updatedAt);
19
34
  }
package/dist/pages.d.ts CHANGED
@@ -18,6 +18,8 @@ export declare const pageCalls: {
18
18
  preview: (api: AssistantApi, routineId: string, values: Readonly<Record<string, string>>, conversationId?: string) => Promise<unknown>;
19
19
  /** Runs a previewed routine with the person's confirmation. */
20
20
  run: (api: AssistantApi, routineId: string, routineRunId: string, hash: string, code?: string) => Promise<unknown>;
21
+ /** The models the person may pick for a conversation (`readModels` reads the answer). */
22
+ models: (api: AssistantApi) => Promise<unknown>;
21
23
  };
22
24
  /**
23
25
  * Starts a run of a non-conversation capability (`POST capabilities/{id}/runs`, for a host's own screens without a
package/dist/pages.js CHANGED
@@ -17,6 +17,8 @@ export const pageCalls = {
17
17
  preview: (api, routineId, values, conversationId) => api.post("routines/" + enc(routineId) + "/preview", conversationId ? { values, conversationId } : { values }),
18
18
  /** Runs a previewed routine with the person's confirmation. */
19
19
  run: (api, routineId, routineRunId, hash, code) => api.post("routines/" + enc(routineId) + "/run", code ? { routineRunId, hash, code } : { routineRunId, hash }),
20
+ /** The models the person may pick for a conversation (`readModels` reads the answer). */
21
+ models: (api) => api.get("models"),
20
22
  };
21
23
  /**
22
24
  * Starts a run of a non-conversation capability (`POST capabilities/{id}/runs`, for a host's own screens without a
@@ -69,6 +71,16 @@ const PAGE_FA = {
69
71
  "page.findings.empty": "چیزی نیاز به توجه ندارد",
70
72
  "page.actions.empty": "هنوز کاری انجام نشده است",
71
73
  "routine.type_to_search": "نامش را بنویسید",
74
+ "confirm.remote_expired": "تأیید روی گوشی منقضی شد؛ دوباره بخواهید یا کد را وارد کنید",
75
+ "model.label": "مدل",
76
+ "model.auto": "خودکار",
77
+ "model.unavailable": "در دسترس نیست",
78
+ "model.effort": "عمق فکر",
79
+ "model.effort.default": "پیش‌فرض",
80
+ "model.effort.low": "کم",
81
+ "model.effort.medium": "متوسط",
82
+ "model.effort.high": "زیاد",
83
+ "confirm.remote_refused": "روی گوشی رد شد",
72
84
  };
73
85
  const PAGE_EN = {
74
86
  "history.tabs": "Conversations",
@@ -107,6 +119,16 @@ const PAGE_EN = {
107
119
  "page.findings.empty": "Nothing needs attention",
108
120
  "page.actions.empty": "No actions yet",
109
121
  "routine.type_to_search": "Type its name",
122
+ "confirm.remote_expired": "The approval on your phone expired: ask again or enter the code",
123
+ "model.label": "Model",
124
+ "model.auto": "Automatic",
125
+ "model.unavailable": "unavailable",
126
+ "model.effort": "Thinking",
127
+ "model.effort.default": "Default",
128
+ "model.effort.low": "Low",
129
+ "model.effort.medium": "Medium",
130
+ "model.effort.high": "High",
131
+ "confirm.remote_refused": "It was refused on your phone",
110
132
  };
111
133
  /** The pages' texts over the assistant's: the host's texts and the core's win, then the pages' own. */
112
134
  export function pageTexts(texts) {
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/types.d.ts CHANGED
@@ -11,7 +11,7 @@ export declare const DEFAULT_THRESHOLDS: LayoutThresholds;
11
11
  /** The layout "auto" picks for a container width. */
12
12
  export declare function pickLayout(layout: Layout, containerWidth: number, thresholds?: LayoutThresholds): Exclude<Layout, "auto">;
13
13
  /** The stable part names hosts style and replace (layer 3): part of the public, versioned contract. */
14
- export declare const PARTS: readonly ["panel", "panel.header", "panel.body", "panel.footer", "hero", "bar-button", "ask-about", "chat", "messages", "message", "message.user", "message.assistant", "message.text", "message.provenance", "message.feedback", "composer", "composer.input", "composer.attach", "composer.mic", "composer.send", "composer.stop", "file-chips", "file-chip", "work", "work.stage", "work.progress", "work.tool", "work.elapsed", "card.approval", "card.approval.step", "card.approval.fact", "card.approval.code", "card.approval.confirm", "card.approval.refuse", "card.undo", "card.ask", "card.choice", "secret-field", "block", "block.text", "block.callout", "block.table", "block.table.card", "block.chart", "block.chart.bar", "block.chart.line", "block.kpi", "block.steps", "block.actions", "block.action", "block.flow", "block.image", "block.open", "block.fallback", "mode-banner", "usage-chip", "thinking-orb", "stages-thinking", "reveal-text"];
14
+ export declare const PARTS: readonly ["panel", "panel.header", "panel.body", "panel.footer", "hero", "bar-button", "ask-about", "chat", "messages", "message", "message.user", "message.assistant", "message.text", "message.provenance", "message.feedback", "composer", "composer.input", "composer.attach", "composer.mic", "composer.send", "composer.stop", "file-chips", "file-chip", "work", "work.stage", "work.progress", "work.tool", "work.elapsed", "card.approval", "card.approval.step", "card.approval.fact", "card.approval.code", "card.approval.confirm", "card.approval.refuse", "card.undo", "card.ask", "card.choice", "secret-field", "block", "block.text", "block.callout", "block.table", "block.table.card", "block.chart", "block.chart.bar", "block.chart.line", "block.kpi", "block.steps", "block.actions", "block.action", "block.flow", "block.image", "block.open", "block.fallback", "mode-banner", "usage-chip", "thinking-orb", "stages-thinking", "reveal-text", "model-picker"];
15
15
  export type PartName = (typeof PARTS)[number];
16
16
  /** Per-part classes and styles from the host (layer 3). */
17
17
  export interface PartStyles {
package/dist/types.js CHANGED
@@ -18,7 +18,7 @@ export const PARTS = [
18
18
  "card.undo", "card.ask", "card.choice", "secret-field",
19
19
  "block", "block.text", "block.callout", "block.table", "block.table.card", "block.chart", "block.chart.bar", "block.chart.line",
20
20
  "block.kpi", "block.steps", "block.actions", "block.action", "block.flow", "block.image", "block.open", "block.fallback",
21
- "mode-banner", "usage-chip", "thinking-orb", "stages-thinking", "reveal-text",
21
+ "mode-banner", "usage-chip", "thinking-orb", "stages-thinking", "reveal-text", "model-picker",
22
22
  ];
23
23
  /** The class of a part: the library's stable class plus the host's. */
24
24
  export function partClass(part, styles, unstyled = false) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@g9tm/aiassistant",
3
- "version": "1.4.2",
3
+ "version": "1.6.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.2"
36
+ "@g9tm/aiassistant-contracts": "1.6.0"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "tsc -p tsconfig.json",