@g9tm/aiassistant 1.11.0 → 1.12.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.
@@ -28,6 +28,11 @@ export interface AssistantOptions {
28
28
  deviceTools?: DeviceTools;
29
29
  /** Device flows (with `deviceTools`): questions the device answers itself, without a model or a connection. Default on. */
30
30
  flows?: boolean;
31
+ /**
32
+ * The app's own flows (the pack's flow shape), kept apart from the server's pack: never saved into it, and matched only
33
+ * when the server's pack has no flow with the same id, so a flow new in an app build works before the server ships it.
34
+ */
35
+ builtInFlows?: readonly unknown[];
31
36
  /** How the conversation reads and writes (every renderer follows it). */
32
37
  chat?: ChatOptions;
33
38
  /**
package/dist/assistant.js CHANGED
@@ -41,7 +41,7 @@ export class Assistant {
41
41
  this.transport = pickTransport(this.api, host, options);
42
42
  this.customBlocks = new Set(options.customBlocks ?? []);
43
43
  this.deviceTools = options.deviceTools ?? null;
44
- this.devices = options.deviceTools && options.flows !== false ? new DeviceFlows(this.api, host, options.deviceTools) : null;
44
+ this.devices = options.deviceTools && options.flows !== false ? new DeviceFlows(this.api, host, options.deviceTools, undefined, undefined, options.builtInFlows ?? []) : null;
45
45
  this.stateWatch = new StateWatch(this.api, host);
46
46
  this.stateNow = this.stateWatch.state;
47
47
  this.chat = options.chat ?? {};
package/dist/flows.d.ts CHANGED
@@ -100,6 +100,8 @@ export interface FlowRunResult {
100
100
  * The device's flow pack: kept in the host's storage (it works offline from the first open after a sync), refreshed with
101
101
  * its version (the server answers `unchanged`), matched against questions, run through the device tools, and every run's
102
102
  * outcome queued and sent when online. A flow naming a tool this device does not have is never run.
103
+ * The app's own built-in flows are kept apart: never saved into the pack, matched only when the server's pack has no
104
+ * flow with their id (a flow new in an app build works before the server knows it), and reported as version `builtin`.
103
105
  */
104
106
  export declare class DeviceFlows {
105
107
  private readonly api;
@@ -110,14 +112,18 @@ export declare class DeviceFlows {
110
112
  version: string;
111
113
  off: boolean;
112
114
  flows: FlowView[];
115
+ /** The app's built-in flows (the `builtInFlows` option), read once; never stored. */
116
+ readonly builtIn: readonly FlowView[];
113
117
  private queue;
114
118
  private seq;
115
- constructor(api: AssistantApi, host: AssistantHost, tools: DeviceTools, key?: string, maxQueue?: number);
119
+ constructor(api: AssistantApi, host: AssistantHost, tools: DeviceTools, key?: string, maxQueue?: number, builtInFlows?: readonly unknown[]);
116
120
  /** Reads the kept pack and the unsent outcomes. */
117
121
  load(): void;
118
122
  private save;
119
123
  /** Asks the server for a newer pack (`unchanged` keeps the kept one). Returns whether it changed. */
120
124
  refresh(): Promise<boolean>;
125
+ /** The server's flows, then the built-in flows whose id the server's pack lacks. */
126
+ get all(): readonly FlowView[];
121
127
  /** The flow and slots for a question, or null. The person's language first; only flows whose tools this device has. */
122
128
  match(text: string, locale: string): {
123
129
  flow: FlowView;
package/dist/flows.js CHANGED
@@ -48,23 +48,21 @@ export function isWordChar(c) {
48
48
  || (k >= 0x0621 && k <= 0x063a) || (k >= 0x0641 && k <= 0x064a)
49
49
  || (k >= 0x066e && k <= 0x06d3) || k === 0x06d5 || k === 0x06ee || k === 0x06ef || (k >= 0x06fa && k <= 0x06fc) || k === 0x06ff;
50
50
  }
51
- function isAsciiDigit(c) {
52
- const k = c.charCodeAt(0);
53
- return k >= 48 && k <= 57;
54
- }
55
- function isAsciiLetterOrDigit(c) {
56
- const k = c.charCodeAt(0);
57
- return (k >= 48 && k <= 57) || (k >= 65 && k <= 90) || (k >= 97 && k <= 122);
58
- }
51
+ /** A character that joins a code ("ab-12", "1/2"); `c` is always one character. */
52
+ const joiner = (c) => "/-_.".includes(c);
53
+ /** A named slot in a pattern: "{name}". */
54
+ const isSlot = (t) => t.length > 2 && t.startsWith("{") && t.endsWith("}");
55
+ /** Flows read from the wire; the broken ones dropped. */
56
+ const flowsOf = (raw) => arr(raw).map(flowView).filter((f) => f !== null);
59
57
  /** The gap a token stands for (as FlowText.Gap). */
60
58
  export function gapOf(token) {
61
59
  let digits = 0;
62
60
  let codeChars = true;
63
61
  for (let i = 0; i < token.length; i++) {
64
62
  const c = token.charAt(i);
65
- if (isAsciiDigit(c))
63
+ if (/[0-9]/.test(c))
66
64
  digits++;
67
- if (!(isAsciiLetterOrDigit(c) || c === "/" || c === "-" || c === "_" || c === "."))
65
+ if (!/[0-9A-Za-z]/.test(c) && !joiner(c))
68
66
  codeChars = false;
69
67
  }
70
68
  if (digits === token.length && token.length > 0 && token.length <= 3)
@@ -81,7 +79,7 @@ export function readFlowText(text) {
81
79
  let spaced = "";
82
80
  for (let i = 0; i < folded.length; i++) {
83
81
  const c = folded.charAt(i);
84
- const joins = (c === "/" || c === "-" || c === "_" || c === ".") && i > 0 && i + 1 < folded.length
82
+ const joins = joiner(c) && i > 0 && i + 1 < folded.length
85
83
  && isWordChar(folded.charAt(i - 1)) && isWordChar(folded.charAt(i + 1));
86
84
  spaced += isWordChar(c) || joins ? c : " ";
87
85
  }
@@ -108,7 +106,7 @@ export function flowPatternTokens(pattern) {
108
106
  for (const part of pattern.split(" ")) {
109
107
  if (part.length === 0)
110
108
  continue;
111
- if (part.length > 2 && part.charAt(0) === "{" && part.charAt(part.length - 1) === "}")
109
+ if (isSlot(part))
112
110
  out.push(part);
113
111
  else
114
112
  out.push(...readFlowText(part).tokens);
@@ -143,7 +141,7 @@ function walk(p, pi, q, qi, slots) {
143
141
  Reflect.deleteProperty(slots, name);
144
142
  return false;
145
143
  }
146
- if (t.length > 2 && t.charAt(0) === "{" && t.charAt(t.length - 1) === "}") {
144
+ if (isSlot(t)) {
147
145
  const name = t.slice(1, -1);
148
146
  for (let end = qi + 1; end <= q.tokens.length; end++) {
149
147
  slots[name] = q.raw.slice(qi, end).join(" ");
@@ -189,9 +187,11 @@ export function flowView(raw) {
189
187
  * The device's flow pack: kept in the host's storage (it works offline from the first open after a sync), refreshed with
190
188
  * its version (the server answers `unchanged`), matched against questions, run through the device tools, and every run's
191
189
  * outcome queued and sent when online. A flow naming a tool this device does not have is never run.
190
+ * The app's own built-in flows are kept apart: never saved into the pack, matched only when the server's pack has no
191
+ * flow with their id (a flow new in an app build works before the server knows it), and reported as version `builtin`.
192
192
  */
193
193
  export class DeviceFlows {
194
- constructor(api, host, tools, key = "g9ai.flows", maxQueue = 500) {
194
+ constructor(api, host, tools, key = "g9ai.flows", maxQueue = 500, builtInFlows = []) {
195
195
  this.api = api;
196
196
  this.host = host;
197
197
  this.tools = tools;
@@ -202,6 +202,7 @@ export class DeviceFlows {
202
202
  this.flows = [];
203
203
  this.queue = [];
204
204
  this.seq = 0;
205
+ this.builtIn = flowsOf(builtInFlows);
205
206
  this.load();
206
207
  }
207
208
  /** Reads the kept pack and the unsent outcomes. */
@@ -213,7 +214,7 @@ export class DeviceFlows {
213
214
  const pack = obj(JSON.parse(s.get(this.key) ?? "{}"));
214
215
  this.version = str(pack["version"]);
215
216
  this.off = pack["off"] === true;
216
- this.flows = arr(pack["flows"]).map(flowView).filter((f) => f !== null);
217
+ this.flows = flowsOf(pack["flows"]);
217
218
  this.queue = arr(JSON.parse(s.get(this.key + ".outcomes") ?? "[]")).map((o) => {
218
219
  const x = obj(o);
219
220
  return { flowId: str(x["flowId"]), version: str(x["version"]), ok: x["ok"] === true, code: typeof x["code"] === "string" ? x["code"] : null, ms: num(x["ms"]), offline: x["offline"] === true, at: str(x["at"]) };
@@ -246,19 +247,24 @@ export class DeviceFlows {
246
247
  }
247
248
  this.version = str(r["version"]);
248
249
  this.off = r["off"] === true;
249
- this.flows = arr(r["flows"]).map(flowView).filter((f) => f !== null);
250
+ this.flows = flowsOf(r["flows"]);
250
251
  this.save();
251
252
  return true;
252
253
  }
254
+ /** The server's flows, then the built-in flows whose id the server's pack lacks. */
255
+ get all() {
256
+ return [...this.flows, ...this.builtIn.filter((b) => !this.flows.some((f) => f.id === b.id))];
257
+ }
253
258
  /** The flow and slots for a question, or null. The person's language first; only flows whose tools this device has. */
254
259
  match(text, locale) {
255
- if (this.off || this.flows.length === 0)
260
+ const all = this.all;
261
+ if (this.off || all.length === 0)
256
262
  return null;
257
263
  const q = readFlowText(text);
258
264
  if (q.tokens.length === 0)
259
265
  return null;
260
266
  for (const pass of [true, false]) {
261
- for (const flow of this.flows) {
267
+ for (const flow of all) {
262
268
  if (!flow.steps.every((s) => this.tools.has(s.tool)))
263
269
  continue;
264
270
  for (const intent of flow.intents) {
@@ -301,7 +307,7 @@ export class DeviceFlows {
301
307
  }
302
308
  }
303
309
  const ms = Math.max(0, t.now() - started);
304
- this.note({ flowId: flow.id, version: this.version, ok: code === null, code, ms, offline, at: new Date(t.now()).toISOString() });
310
+ this.note({ flowId: flow.id, version: this.builtIn.includes(flow) ? "builtin" : this.version, ok: code === null, code, ms, offline, at: new Date(t.now()).toISOString() });
305
311
  return { ok: code === null, flowId: flow.id, say: says.join("\n"), blocks, code, ms, ...(escalate ? { escalate: true } : {}) };
306
312
  }
307
313
  /** Unsent outcomes. */
package/dist/pages.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./csv.js";
6
6
  export * from "./guide.js";
7
7
  export * from "./history.js";
8
8
  export * from "./routine.js";
9
+ export * from "./typing.js";
9
10
  /** The calls of the person's pages, over the API's plain verbs. */
10
11
  export declare const pageCalls: {
11
12
  /** Pins or unpins a conversation (pinned ones come first in the list). */
package/dist/pages.js CHANGED
@@ -4,6 +4,7 @@ export * from "./csv.js";
4
4
  export * from "./guide.js";
5
5
  export * from "./history.js";
6
6
  export * from "./routine.js";
7
+ export * from "./typing.js";
7
8
  const enc = encodeURIComponent;
8
9
  /** The calls of the person's pages, over the API's plain verbs. */
9
10
  export const pageCalls = {
package/dist/reveal.d.ts CHANGED
@@ -1,14 +1,10 @@
1
1
  /**
2
- * Progressive reveal without a DOM: only an answer that arrived in front of the person is
2
+ * Progressive reveal without a DOM (the text's steps are in `typing.ts`, the "/pages" entry): only an answer that arrived in front of the person is
3
3
  * revealed, over about 1.5 seconds, in batches no faster than one render per 80 ms; a history message, or any message
4
4
  * with reduced motion, shows whole at once.
5
5
  */
6
6
  export declare const REVEAL_MS = 1500;
7
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
8
  /** Which messages may be revealed: those not yet watched. A message is watched once it has been shown. */
13
9
  export declare class Watched {
14
10
  private readonly seen;
package/dist/reveal.js CHANGED
@@ -1,28 +1,10 @@
1
1
  /**
2
- * Progressive reveal without a DOM: only an answer that arrived in front of the person is
2
+ * Progressive reveal without a DOM (the text's steps are in `typing.ts`, the "/pages" entry): only an answer that arrived in front of the person is
3
3
  * revealed, over about 1.5 seconds, in batches no faster than one render per 80 ms; a history message, or any message
4
4
  * with reduced motion, shows whole at once.
5
5
  */
6
6
  export const REVEAL_MS = 1500;
7
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
8
  /** Which messages may be revealed: those not yet watched. A message is watched once it has been shown. */
27
9
  export class Watched {
28
10
  constructor() {
@@ -0,0 +1,5 @@
1
+ /** The steps of a progressive reveal (the "/pages" entry; which messages reveal is `Watched` in the core's `reveal.ts`). */
2
+ /** How much of a text is visible at a moment of its reveal. */
3
+ export declare function visibleLength(textLength: number, elapsedMs: number, reducedMotion: boolean, durationMs?: number): number;
4
+ /** The visible part, never cutting inside a word when it can help it (a cut word reads as a typo). */
5
+ export declare function visibleText(text: string, elapsedMs: number, reducedMotion: boolean, durationMs?: number): string;
package/dist/typing.js ADDED
@@ -0,0 +1,20 @@
1
+ /** The steps of a progressive reveal (the "/pages" entry; which messages reveal is `Watched` in the core's `reveal.ts`). */
2
+ import { FRAME_MS, REVEAL_MS } from "./reveal.js";
3
+ /** How much of a text is visible at a moment of its reveal. */
4
+ export function visibleLength(textLength, elapsedMs, reducedMotion, durationMs = REVEAL_MS) {
5
+ if (reducedMotion || textLength === 0 || elapsedMs >= durationMs)
6
+ return textLength;
7
+ if (elapsedMs <= 0)
8
+ return 0;
9
+ const frames = Math.floor(elapsedMs / FRAME_MS); // batched: one step per frame
10
+ const total = Math.max(1, Math.ceil(durationMs / FRAME_MS));
11
+ return Math.min(textLength, Math.ceil((textLength * frames) / total));
12
+ }
13
+ /** The visible part, never cutting inside a word when it can help it (a cut word reads as a typo). */
14
+ export function visibleText(text, elapsedMs, reducedMotion, durationMs = REVEAL_MS) {
15
+ const n = visibleLength(text.length, elapsedMs, reducedMotion, durationMs);
16
+ if (n >= text.length)
17
+ return text;
18
+ const space = text.lastIndexOf(" ", n);
19
+ return text.slice(0, space > n * 0.6 ? space : n);
20
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@g9tm/aiassistant",
3
- "version": "1.11.0",
3
+ "version": "1.12.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.11.0"
36
+ "@g9tm/aiassistant-contracts": "1.12.0"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.json",