@ghuri/ai-widget 0.3.2 → 0.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/README.md CHANGED
@@ -78,7 +78,7 @@ chat.configure({
78
78
  return { ref: f.id, name: f.name, contentType: f.contentType, size: f.size, width: f.width, height: f.height,
79
79
  thumbnailUrl: new URL(f.url, location.origin).href }
80
80
  },
81
- attachments: { maxCount: 10, maxBytes: 5 * 1024 * 1024 }, // defaults; also accept, maxEdge (2048)
81
+ attachments: { maxCount: 10, maxBytes: 5 * 1024 * 1024 }, // defaults; also accept, maxEdge (2048), targetBytes
82
82
  // Optional: a photo uploaded but not sent (chip removed, draft left behind). Delete it if your upload created it.
83
83
  discardFile: async ({ ref }) => { /* delete ref from your library if this upload created it */ },
84
84
  })
@@ -89,7 +89,9 @@ The photo button appears only after Ghuri AI says the app's profile takes photos
89
89
  the widget opens), and in an opened conversation only if it was started with photos; the profile's types and count
90
90
  cap yours. If Ghuri AI still refuses the photos, nothing is sent, the words go back into the box and the widget says
91
91
  why (`photos-refused`). Before uploading, each photo is scaled so its longest edge is at most 2,048 px and re-encoded, which drops its
92
- metadata (the phone's location); GIFs are sent as they are. Send waits until every photo is uploaded, and while a failed
92
+ metadata (the phone's location); GIFs are sent as they are. When the profile lets the assistant see photos (`vision` in `GET /v1/capabilities`), photos
93
+ are scaled to 1,568 px instead and kept under 1 MB (`targetBytes`): a larger one (a PNG screenshot is often 1-2.5 MB)
94
+ is re-encoded as JPEG, or as WebP when it has transparent pixels (kept as it is where the browser cannot encode WebP). Send waits until every photo is uploaded, and while a failed
93
95
  upload can still be retried (retry or remove it first). The first message of a new conversation tells Ghuri AI this client can upload
94
96
  (`capabilities.attachments`), and Ghuri AI pins that on the conversation. Photos also need the product's profile to
95
97
  allow them (`attachments.enabled`), and `thumbnailUrl` must be on the profile's MCP host or one listed in
@@ -133,11 +135,15 @@ await chat.decide([{ approvalId, approved: true, allowForConversation: true }])
133
135
  await chat.revokeAllowance('theme_files_write') // "Ask every time": state.allowances lists what is allowed
134
136
  chat.stop(); await chat.retry(); await chat.reload()
135
137
  await chat.listConversations(); await chat.openConversation(id); chat.newConversation()
138
+ await chat.continueConversation() // a full conversation: carry the work over into a new one
136
139
  await chat.end() // logout or store switch
137
140
  ```
138
141
 
139
142
  `state.problem.code` says what went wrong in terms a UI can word: `reconnecting`, `token-expired` (offer "Continue"),
140
- `in-progress`, `conversation-full`, `throttled` (`retryAfter` seconds), `forbidden`, `store-unreachable`,
143
+ `in-progress`, `conversation-full` (no new message fits; waiting changes can still be answered, and `state.full` stays
144
+ true, so `send()` sends nothing; `continueConversation()` carries the work into a new conversation that starts with
145
+ a hand-over: `state.continuing` while it is written, then `state.continuedFrom` and `state.handover`, shown with
146
+ `handoverText()`; a continued conversation has `state.continuedIn` and is read-only), `throttled` (`retryAfter` seconds), `forbidden`, `store-unreachable`,
141
147
  `unavailable`, `step-limit` (the reply used all its steps with work unfinished; offer "Continue"), `too-long` (a
142
148
  tool call hit the output limit and was not done; offer "Continue", which sends `continueSmallerMessage`), `answer-cut` (an answer hit the output limit and is unfinished; offer "Continue"),
143
149
  `cut-off` (offer "Reload"), `failed` (`message`).
@@ -8,6 +8,19 @@ export declare const ATTACHMENT_DEFAULTS: {
8
8
  readonly maxBytes: number;
9
9
  readonly maxEdge: 2048;
10
10
  };
11
+ /**
12
+ * When the app's assistant sees photos (`vision` in `GET /v1/capabilities`): Ghuri AI shows the model pictures of at
13
+ * most 1,600 px and 1.5 MB, so photos are scaled to Claude's standard 1,568 px and kept under 1 MB.
14
+ */
15
+ export declare const VISION_DEFAULTS: {
16
+ readonly maxEdge: 1568;
17
+ readonly targetBytes: 1000000;
18
+ };
19
+ /**
20
+ * The readable part of a hand-over (`state.handover`): without its `<handover>` tags and the opening paragraph that
21
+ * tells the assistant how to read it.
22
+ */
23
+ export declare function handoverText(handover: string): string;
11
24
  /** What the user is told about a failed request. */
12
25
  export declare function problemFor(err: unknown): Problem;
13
26
  export declare class ChatClient {
@@ -67,6 +80,7 @@ export declare class ChatClient {
67
80
  maxCount: number;
68
81
  maxBytes: number;
69
82
  maxEdge: number;
83
+ targetBytes?: number;
70
84
  };
71
85
  /** A photo that failed to upload and can be retried: sending would silently leave it out. */
72
86
  private retryable;
@@ -110,6 +124,13 @@ export declare class ChatClient {
110
124
  /** Run the action that failed again (Try again). */
111
125
  retry(): Promise<void>;
112
126
  dismissProblem(): void;
127
+ /**
128
+ * Continue the open, full conversation in a new one: Ghuri AI writes a hand-over (a few seconds; `state.continuing`)
129
+ * and the new conversation opens with it (`state.continuedFrom`, `state.handover`). Nothing is sent to the
130
+ * assistant: the user carries on with a message. Not while a change is waiting (answer it first), and a conversation
131
+ * already continued just opens where it went on.
132
+ */
133
+ continueConversation(): Promise<void>;
113
134
  /** Start a new conversation (the next message creates it). */
114
135
  newConversation(): void;
115
136
  /** Forget the open conversation (new conversation, end). */
@@ -1,4 +1,4 @@
1
- export { ATTACHMENT_DEFAULTS, ChatClient, problemFor } from './client';
1
+ export { ATTACHMENT_DEFAULTS, ChatClient, handoverText, problemFor } from './client';
2
2
  export { messagesFromHistory } from './history';
3
3
  export { parsedOutput, splitSavedOutput, splitToolOutput, type ToolImage } from './output';
4
4
  export { prepareImage, scaledSize } from './prepare';
@@ -1,4 +1,10 @@
1
1
  import type { PreparedImage } from './types';
2
+ type Canvas = OffscreenCanvas | HTMLCanvasElement;
3
+ /**
4
+ * The file name for a picture of `contentType`: "homepage.png" re-encoded as JPEG becomes "homepage.jpg". A name whose
5
+ * extension already fits is kept; one without a picture extension gets one added.
6
+ */
7
+ export declare function nameFor(name: string, contentType: string): string;
2
8
  /** The size to draw at: the longest edge at most `maxEdge`, never enlarged. */
3
9
  export declare function scaledSize(width: number, height: number, maxEdge: number): {
4
10
  width: number;
@@ -7,4 +13,8 @@ export declare function scaledSize(width: number, height: number, maxEdge: numbe
7
13
  export declare function prepareImage(file: Blob, opts: {
8
14
  maxEdge: number;
9
15
  contentType: string;
16
+ targetBytes?: number;
10
17
  }): Promise<PreparedImage>;
18
+ /** Does the drawn picture have no transparent pixels? Unknown counts as transparent (keeps it safe). */
19
+ export declare function isOpaque(canvas: Canvas): boolean;
20
+ export {};
@@ -35,6 +35,7 @@ export interface ChatClientOptions {
35
35
  prepareImage?: (file: Blob, opts: {
36
36
  maxEdge: number;
37
37
  contentType: string;
38
+ targetBytes?: number;
38
39
  }) => Promise<PreparedImage>;
39
40
  /** Defaults to the global `fetch`. */
40
41
  fetch?: typeof fetch;
@@ -73,14 +74,22 @@ export interface AttachmentOptions {
73
74
  maxCount?: number;
74
75
  /** Largest file after scaling, in bytes (default 5 MB). */
75
76
  maxBytes?: number;
76
- /** Longest edge after scaling, in pixels (default 2048). */
77
+ /** Longest edge after scaling, in pixels (default 2048; 1568 when the assistant sees photos). */
77
78
  maxEdge?: number;
79
+ /**
80
+ * A photo larger than this after scaling is re-encoded smaller: as JPEG when it has no transparent pixels, as WebP
81
+ * when it has (where the browser can encode WebP). Default: none; 1,000,000 when the assistant sees photos
82
+ * (Ghuri AI shows the model pictures of at most 1.5 MB, and a screenshot as PNG is often more).
83
+ */
84
+ targetBytes?: number;
78
85
  }
79
86
  /** What the app's active profile allows for photos (Ghuri AI's `GET /v1/capabilities`). */
80
87
  export interface AttachmentPolicy {
81
88
  enabled: boolean;
82
89
  accept?: string[];
83
90
  maxCount?: number;
91
+ /** The assistant also sees the photos (Ghuri AI `attachments.vision`). */
92
+ vision?: boolean;
84
93
  }
85
94
  export interface PreparedImage {
86
95
  blob: Blob;
@@ -174,6 +183,20 @@ export interface PendingApproval {
174
183
  preview?: ChangePreview;
175
184
  /** The user may answer with "Allow for this conversation" (the product made this tool allowable). */
176
185
  allowable?: boolean;
186
+ /**
187
+ * It was confirmed before and started, but its result was lost. It never runs again: deciding it (either way) closes
188
+ * it as "outcome unknown".
189
+ */
190
+ started?: boolean;
191
+ }
192
+ /** What became of one change the user decided (Ghuri AI's `decisions` on a turn that answered changes). */
193
+ export interface DecisionOutcome {
194
+ approvalId: string;
195
+ toolCallId: string;
196
+ toolName: string;
197
+ /** `unknown`: it had started before and its result was lost; check before trying again. */
198
+ outcome: 'done' | 'failed' | 'cancelled' | 'unknown';
199
+ error?: string;
177
200
  }
178
201
  export interface Decision {
179
202
  approvalId: string;
@@ -198,7 +221,9 @@ export interface TurnResult {
198
221
  error?: string;
199
222
  /**
200
223
  * `output`: the reply was cut off at the output limit (see `cut`). `steps` (with status `ok`): the assistant used all
201
- * the steps one reply may take while it was still working; "Continue" picks the work up.
224
+ * the steps one reply may take while it was still working; "Continue" picks the work up. `conversation`: the
225
+ * conversation is full and takes no new message; with status `paused` its waiting changes can still be confirmed
226
+ * or cancelled, and with status `ok` the decided changes ran without the assistant writing a reply.
202
227
  */
203
228
  limitReached?: 'conversation' | 'turn' | 'output' | 'steps';
204
229
  /** The tools allowed for this conversation when the turn ended (absent: none). */
@@ -206,6 +231,8 @@ export interface TurnResult {
206
231
  /** With `limitReached: 'output'`: a tool call was cut (it never ran) or a text answer was cut (just unfinished). */
207
232
  cut?: 'tool-call' | 'text';
208
233
  userTokenExpired?: boolean;
234
+ /** For a turn that answered changes: what became of each one. */
235
+ decisions?: DecisionOutcome[];
209
236
  }
210
237
  /**
211
238
  * Something the user should be told, in plain words. The UI picks the wording; `retryAfter` is in seconds.
@@ -213,7 +240,7 @@ export interface TurnResult {
213
240
  * - `reconnecting`: getting a fresh session or token (no action needed)
214
241
  * - `token-expired`: the store stopped accepting the assistant mid-reply; the token was refreshed, offer "Continue"
215
242
  * - `in-progress`: the previous reply is still being written
216
- * - `conversation-full`: start a new conversation
243
+ * - `conversation-full`: no new message fits; answer any waiting change, then start a new conversation
217
244
  * - `throttled`: wait `retryAfter` seconds
218
245
  * - `forbidden`: the user may not do that in this store
219
246
  * - `store-unreachable`: the product's MCP server failed; nothing was changed
@@ -260,6 +287,22 @@ export interface ChatState {
260
287
  uploads: Upload[];
261
288
  /** Tools allowed for this conversation ("Allow for this conversation"); the bar above the composer lists them. */
262
289
  allowances: Allowance[];
290
+ /**
291
+ * The open conversation is full: it takes no new message (sending only shows `conversation-full` again), but its
292
+ * waiting changes can still be confirmed or cancelled, and it can be continued in a new one (`continueConversation`).
293
+ */
294
+ full: boolean;
295
+ /** The hand-over is being written (`continueConversation`): nothing else can be done until it ends. */
296
+ continuing: boolean;
297
+ /** The full conversation this one continues (its title is this one's too), for "Continued from …". */
298
+ continuedFrom: {
299
+ id: string;
300
+ title: string;
301
+ } | null;
302
+ /** What this conversation started with, for "What I carried over" (`handoverText()` makes it readable). */
303
+ handover: string | null;
304
+ /** Where this (full) conversation went on: it is read-only, with a link there. */
305
+ continuedIn: string | null;
263
306
  }
264
307
  /** Events for the host page (the element re-dispatches them as `ghuri:*` DOM events). */
265
308
  export interface ChatEvents {