@ghuri/ai-widget 0.3.1 → 0.4.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
@@ -56,6 +56,13 @@ and only `http(s)` and `mailto` links become links (new tab, no referrer). Enter
56
56
  and Enter during IME composition (Bangla keyboards) never sends. While a reply is streaming, closing the tab asks
57
57
  first.
58
58
 
59
+ ## Pictures from tools
60
+
61
+ A product tool can return pictures (MCP `image` content), for example a screenshot of a draft design. When the
62
+ profile has `toolImages` on, the model sees them, and the widget shows them as thumbnails on the tool step (tap to see
63
+ one large; part `tool-pictures`, `lightbox`). `ghuri:tool-result` gets them as `images: [{ mediaType, data }]` (base64)
64
+ next to `output`. They are drawn as `data:` URLs: a strict CSP needs `img-src data:`.
65
+
59
66
  ## Photos
60
67
 
61
68
  Give `uploadFile` and the widget offers photos (button, drag and drop, paste) in conversations that take them:
@@ -71,7 +78,9 @@ chat.configure({
71
78
  return { ref: f.id, name: f.name, contentType: f.contentType, size: f.size, width: f.width, height: f.height,
72
79
  thumbnailUrl: new URL(f.url, location.origin).href }
73
80
  },
74
- 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
+ // Optional: a photo uploaded but not sent (chip removed, draft left behind). Delete it if your upload created it.
83
+ discardFile: async ({ ref }) => { /* delete ref from your library if this upload created it */ },
75
84
  })
76
85
  chat.addEventListener('ghuri:attachment-error', (e) => { /* e.detail: { name, reason: type|size|count|upload } */ })
77
86
  ```
@@ -80,7 +89,9 @@ The photo button appears only after Ghuri AI says the app's profile takes photos
80
89
  the widget opens), and in an opened conversation only if it was started with photos; the profile's types and count
81
90
  cap yours. If Ghuri AI still refuses the photos, nothing is sent, the words go back into the box and the widget says
82
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
83
- 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
84
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
85
96
  (`capabilities.attachments`), and Ghuri AI pins that on the conversation. Photos also need the product's profile to
86
97
  allow them (`attachments.enabled`), and `thumbnailUrl` must be on the profile's MCP host or one listed in
@@ -8,6 +8,14 @@ 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
+ };
11
19
  /** What the user is told about a failed request. */
12
20
  export declare function problemFor(err: unknown): Problem;
13
21
  export declare class ChatClient {
@@ -37,6 +45,8 @@ export declare class ChatClient {
37
45
  /** The app's photo policy for new conversations, once Ghuri AI told us (`loadCapabilities`). */
38
46
  private policy;
39
47
  private loadingPolicy;
48
+ /** Set once the widget asked (it was shown); a new conversation then asks again, an ended client does not. */
49
+ private capabilitiesWanted;
40
50
  constructor(opts: ChatClientOptions);
41
51
  getState(): ChatState;
42
52
  /** Called on every change; returns the unsubscribe function. */
@@ -65,6 +75,7 @@ export declare class ChatClient {
65
75
  maxCount: number;
66
76
  maxBytes: number;
67
77
  maxEdge: number;
78
+ targetBytes?: number;
68
79
  };
69
80
  /** A photo that failed to upload and can be retried: sending would silently leave it out. */
70
81
  private retryable;
@@ -89,6 +100,8 @@ export declare class ChatClient {
89
100
  retryUpload(id: string): void;
90
101
  private startUpload;
91
102
  /** Stop every upload and free every preview (the conversation is replaced). */
103
+ /** Tell the product an uploaded photo will not be sent (it may delete it). Never throws. */
104
+ private discard;
92
105
  private dropPhotos;
93
106
  /** Confirm or cancel the waiting changes: one decision for each, sent together. */
94
107
  decide(decisions: Decision[]): Promise<void>;
@@ -108,6 +121,8 @@ export declare class ChatClient {
108
121
  dismissProblem(): void;
109
122
  /** Start a new conversation (the next message creates it). */
110
123
  newConversation(): void;
124
+ /** Forget the open conversation (new conversation, end). */
125
+ private reset;
111
126
  /** The user's conversations in this product and tenant, newest first. */
112
127
  listConversations(opts?: {
113
128
  limit?: number;
@@ -1,6 +1,6 @@
1
1
  export { ATTACHMENT_DEFAULTS, ChatClient, problemFor } from './client';
2
2
  export { messagesFromHistory } from './history';
3
- export { parsedOutput } from './output';
3
+ export { parsedOutput, splitSavedOutput, splitToolOutput, type ToolImage } from './output';
4
4
  export { prepareImage, scaledSize } from './prepare';
5
5
  export { parseSse, readChunks, SseParser, type StreamChunk } from './sse';
6
6
  export type * from './types';
@@ -1,2 +1,28 @@
1
1
  /** Text that is a JSON object or array, parsed; anything else unchanged. */
2
2
  export declare function parsedOutput(output: unknown): unknown;
3
+ /** A picture a tool returned (a screenshot of a draft design, for example), base64. */
4
+ export interface ToolImage {
5
+ mediaType: string;
6
+ data: string;
7
+ width?: number;
8
+ height?: number;
9
+ }
10
+ /**
11
+ * A live tool result (`tool-output-available`): Ghuri AI streams checked pictures as
12
+ * `{ ghuriToolImages: 1, output, images }` and sends anything a product shaped like that as text, so only this shape
13
+ * draws pictures here. Saved conversations go through `splitSavedOutput`.
14
+ */
15
+ export declare function splitToolOutput(raw: unknown): {
16
+ output: unknown;
17
+ images: ToolImage[];
18
+ };
19
+ /**
20
+ * A saved tool result with pictures: what the model was given (`{ type: 'content', value: [text…, file…] }`). Ghuri AI
21
+ * saves that shape only for checked pictures; a product's own JSON is saved as `{ type: 'json' }`.
22
+ */
23
+ export declare function splitSavedOutput(saved: {
24
+ value?: unknown;
25
+ }): {
26
+ output: unknown;
27
+ images: ToolImage[];
28
+ };
@@ -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 {};
@@ -1,3 +1,4 @@
1
+ import type { ToolImage } from './output';
1
2
  /** What the product's backend returns when it starts a session (Ghuri AI's `POST /v1/sessions` answer). */
2
3
  export interface SessionInfo {
3
4
  sessionId: string;
@@ -19,6 +20,12 @@ export interface ChatClientOptions {
19
20
  * it, the widget offers photos in conversations that take them; without it, never. Ghuri AI never gets the bytes.
20
21
  */
21
22
  uploadFile?: UploadFile;
23
+ /**
24
+ * Called for a photo that was uploaded but will not be sent: removed from the message, left in a draft the user
25
+ * moved away from, or finished uploading after it was removed. The product may delete it (only what this upload
26
+ * created). Never called for a photo that was sent. Errors are ignored.
27
+ */
28
+ discardFile?: (attachment: Attachment) => void | Promise<void>;
22
29
  /** Limits checked before uploading (the product checks again). */
23
30
  attachments?: AttachmentOptions;
24
31
  /**
@@ -28,6 +35,7 @@ export interface ChatClientOptions {
28
35
  prepareImage?: (file: Blob, opts: {
29
36
  maxEdge: number;
30
37
  contentType: string;
38
+ targetBytes?: number;
31
39
  }) => Promise<PreparedImage>;
32
40
  /** Defaults to the global `fetch`. */
33
41
  fetch?: typeof fetch;
@@ -66,14 +74,22 @@ export interface AttachmentOptions {
66
74
  maxCount?: number;
67
75
  /** Largest file after scaling, in bytes (default 5 MB). */
68
76
  maxBytes?: number;
69
- /** Longest edge after scaling, in pixels (default 2048). */
77
+ /** Longest edge after scaling, in pixels (default 2048; 1568 when the assistant sees photos). */
70
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;
71
85
  }
72
86
  /** What the app's active profile allows for photos (Ghuri AI's `GET /v1/capabilities`). */
73
87
  export interface AttachmentPolicy {
74
88
  enabled: boolean;
75
89
  accept?: string[];
76
90
  maxCount?: number;
91
+ /** The assistant also sees the photos (Ghuri AI `attachments.vision`). */
92
+ vision?: boolean;
77
93
  }
78
94
  export interface PreparedImage {
79
95
  blob: Blob;
@@ -125,6 +141,8 @@ export interface ToolPart {
125
141
  state: 'input' | 'running' | 'awaiting-approval' | 'done' | 'error' | 'denied';
126
142
  approvalId?: string;
127
143
  output?: unknown;
144
+ /** Pictures the tool returned (e.g. screenshots of a draft design), base64. */
145
+ images?: ToolImage[];
128
146
  errorText?: string;
129
147
  /** It ran without a confirm card, on an allowance the user gave for this conversation ("Allowed"). */
130
148
  allowed?: boolean;
@@ -261,6 +279,8 @@ export interface ChatEvents {
261
279
  title?: string;
262
280
  input: unknown;
263
281
  output?: unknown;
282
+ /** Pictures the tool returned, if any. */
283
+ images?: ToolImage[];
264
284
  error?: string;
265
285
  };
266
286
  /** The reply paused on changes the user must decide. */