@puckeditor/plugin-ai 0.9.0-canary.917dc813 → 0.9.0-canary.96d553c1

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/index.d.mts CHANGED
@@ -1,8 +1,9 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
3
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
+ import * as ai from 'ai';
5
+ import { LanguageModelUsage, UIMessage, DataUIPart, ChatOnFinishCallback, CreateUIMessage, ChatStatus } from 'ai';
4
6
  import { PuckAction, Data, Config } from '@puckeditor/core';
5
- import { LanguageModelUsage, UIMessage, CreateUIMessage, DataUIPart, ChatStatus, ChatOnFinishCallback } from 'ai';
6
7
 
7
8
  type _JSONSchema = boolean | JSONSchema;
8
9
  type JSONSchema = {
@@ -115,6 +116,37 @@ type DataToolStatus = {
115
116
  status: ToolStatus;
116
117
  toolCallId: string;
117
118
  };
119
+ type AiFieldTypeShape<Value> = {
120
+ /** What the field represents, and any rules for using it */
121
+ description?: string;
122
+ /**
123
+ * JSON Schema for the field's value — the shape the model may write, at
124
+ * creation and in later edits. Without one the model never writes the
125
+ * field, like `ai.exclude`.
126
+ */
127
+ schema?: Record<string, any>;
128
+ /**
129
+ * What a field the model designs starts as. When set, the model places the
130
+ * field and this value fills it; the model authors a value against `schema`
131
+ * only when this is absent.
132
+ */
133
+ defaultValue?: Value;
134
+ };
135
+ /**
136
+ * A user field type registered by the developer's config. Puck can't infer a
137
+ * value shape for one, so a type carries `schema`, `defaultValue`, or both.
138
+ */
139
+ type AiFieldType$1<Value = AiFieldTypeValue> = AiFieldTypeShape<Value> & ({
140
+ schema: Record<string, any>;
141
+ } | {
142
+ defaultValue: Value;
143
+ });
144
+ /**
145
+ * Any JSON-ish value. Spelled out rather than `unknown` so a union with a
146
+ * function form doesn't collapse, which would drop the callback's parameter
147
+ * types at the call site.
148
+ */
149
+ type AiFieldTypeValue = string | number | boolean | null | readonly unknown[] | Record<string, unknown>;
118
150
  type AddOperation = {
119
151
  op: "add";
120
152
  id: string;
@@ -203,6 +235,20 @@ type SubagentEvent = {
203
235
  kind: "finish";
204
236
  toolCallId: string;
205
237
  };
238
+ /** A request for data from the agent. */
239
+ type ClientRequest = {
240
+ requestId: string;
241
+ action: "getPageData";
242
+ params: {
243
+ id: string;
244
+ };
245
+ } | {
246
+ requestId: string;
247
+ action: "takeScreenshot";
248
+ params: {
249
+ putUrl: string;
250
+ };
251
+ };
206
252
  type PuckDataParts = {
207
253
  "new-chat-created": {
208
254
  chatId: string;
@@ -219,11 +265,7 @@ type PuckDataParts = {
219
265
  input: any;
220
266
  }[];
221
267
  };
222
- "client-request": {
223
- requestId: string;
224
- action: string;
225
- params?: Record<string, any>;
226
- };
268
+ "client-request": ClientRequest;
227
269
  "send-screenshot": {
228
270
  id: string;
229
271
  urls: {
@@ -247,6 +289,21 @@ type PuckProviderMetadata = {
247
289
 
248
290
  type PuckMessage = UIMessage<PuckProviderMetadata, PuckDataParts>;
249
291
 
292
+ type AiFieldTypeContext = {
293
+ /** The designed component the field belongs to */
294
+ componentName: string;
295
+ /** The field's name within that component */
296
+ fieldName: string;
297
+ };
298
+ /**
299
+ * A field type the developer's config registers: `schema` is what the model
300
+ * may write, `defaultValue` is what a designed field starts as — one or both.
301
+ * In the editor the default may be a function of the component and field
302
+ * being created.
303
+ */
304
+ type AiFieldType = AiFieldType$1<AiFieldTypeValue | ((ctx: AiFieldTypeContext) => AiFieldTypeValue)>;
305
+ type AiFieldTypeRegistry = Record<string, AiFieldType>;
306
+
250
307
  type Mode = "assembly" | "design";
251
308
  type RequestOptions = {
252
309
  body?: {
@@ -261,8 +318,105 @@ type RequestOptions = {
261
318
  headers?: HeadersInit;
262
319
  credentials?: RequestCredentials;
263
320
  };
321
+ type UsePuckAiOptions = {
322
+ /** The API host to call. This should be your server. */
323
+ host?: string;
324
+ /**
325
+ * The mode used for generating the page. Can be `"assembly"` (default),
326
+ * to generate pages based on the provided components, or `"design"`, to
327
+ * design new components, incorporating existing components where possible.
328
+ */
329
+ mode?: Mode;
330
+ apiVersion?: string;
331
+ /**
332
+ * A callback to customize every request the hook makes before it's sent,
333
+ * such as to add custom headers, add metadata to the body, or change
334
+ * credentials.
335
+ */
336
+ prepareRequest?: (opts: RequestOptions) => RequestOptions | Promise<RequestOptions>;
337
+ /** A function called when a chat turn finishes. */
338
+ onFinish?: ChatOnFinishCallback<PuckMessage>;
339
+ /** A function called when a chat turn fails. `generate` rejects instead. */
340
+ onError?: (error: Error) => void;
341
+ };
342
+ type SendMessageOptions = {
343
+ /** Overrides the hook's `mode` for this turn. */
344
+ mode?: Mode;
345
+ /** Files to send with the prompt, as AI SDK file parts. */
346
+ files?: Extract<PuckMessage["parts"][number], {
347
+ type: "file";
348
+ }>[];
349
+ };
350
+ type GenerateOptions = {
351
+ /**
352
+ * The mode for this generation. `"design"` needs
353
+ * `ai.designMode.allowed` on the handler.
354
+ */
355
+ mode?: Mode;
356
+ /** An `AbortSignal` that cancels the generation. */
357
+ signal?: AbortSignal;
358
+ };
359
+ /**
360
+ * AI Parts represent discrete chunks of data sent by Puck AI. They are
361
+ * produced via `generate`'s `onPart` callback.
362
+ */
363
+ type AIPart = DataUIPart<PuckDataParts>;
364
+ type ApplyPartOptions = {
365
+ /**
366
+ * Whether to record the page as one undo step, covering every change since
367
+ * the last one recorded. Defaults to `false`.
368
+ */
369
+ recordHistory?: boolean;
370
+ };
371
+ /**
372
+ * Access Puck AI's internal behavior to create your own agent UI, or implement Puck AI as a client subagent.
373
+ *
374
+ * @param options.host The API host to call. This should be your server.
375
+ * @param options.mode The mode used for generating the page: `"assembly"` (default) or `"design"`.
376
+ * @param options.apiVersion The Puck Cloud API version. Defaults to `v2`.
377
+ * @param options.prepareRequest A callback to customize every request the hook makes before it's sent.
378
+ * @param options.onFinish A function called when a chat turn finishes.
379
+ * @param options.onError A function called when a chat turn fails. `generate` rejects instead.
380
+ * @returns An object containing `chat`, `generate` and `applyPart`.
381
+ */
382
+ declare function usePuckAi(options?: UsePuckAiOptions): {
383
+ chat: {
384
+ /** Send a new message to the agent. */
385
+ sendMessage: (prompt: string, opts?: SendMessageOptions) => Promise<void>;
386
+ /** Run the conversation's last turn again. */
387
+ regenerate: (args_0?: ({
388
+ messageId?: string;
389
+ } & ai.ChatRequestOptions) | undefined) => Promise<void>;
390
+ /** Stop the running turn. */
391
+ stop: () => Promise<void>;
392
+ /** The conversation's AI SDK status. */
393
+ status: ai.ChatStatus;
394
+ /** The last chat error's message, or `undefined`. Also passed to `onError`. */
395
+ error: string | undefined;
396
+ /** The conversation, as AI SDK `UIMessage`s. */
397
+ messages: PuckMessage[];
398
+ /** The live progress of the agent's tools, keyed by tool call id. */
399
+ toolStatus: Record<string, ToolStatus>;
400
+ };
401
+ /**
402
+ * Run a generation against the current page. Returns the page's Data once
403
+ * it's applied. Iterates once, unless the handler's `ai.maxIterations`
404
+ * allows more.
405
+ */
406
+ generate: (prompt: string, opts?: GenerateOptions) => Promise<Data>;
407
+ /** Apply and process an AI Part. */
408
+ applyPart: (part: AIPart, { recordHistory }?: ApplyPartOptions) => Promise<void>;
409
+ };
410
+
264
411
  type AiPluginProps = {
265
412
  host?: string;
413
+ /**
414
+ * Field types this config registers, keyed by field type. Each gives a
415
+ * `schema` the AI may write, a `defaultValue` a designed field starts as, or
416
+ * both. Sent to the server as JSON, so a function `defaultValue` only
417
+ * resolves in the editor.
418
+ */
419
+ fieldTypes?: AiFieldTypeRegistry;
266
420
  chat?: {
267
421
  attachments?: {
268
422
  enabled?: boolean;
@@ -306,7 +460,15 @@ declare global {
306
460
  }
307
461
  }
308
462
 
463
+ /**
464
+ * Add a page's designed components to a config, to render the page.
465
+ *
466
+ * @param config Your Puck config.
467
+ * @param data The page, with its designed components in `root.props._dynamicConfig`.
468
+ * @returns The config, with the page's designed components and globals.
469
+ */
309
470
  declare function withDynamicConfig<UserConfig extends Config>(config: UserConfig, data: Data): UserConfig;
471
+
310
472
  declare function createAiPlugin(opts?: AiPluginProps): {
311
473
  label: string;
312
474
  name: string;
@@ -363,4 +525,4 @@ declare module "@puckeditor/core" {
363
525
  }
364
526
  }
365
527
 
366
- export { type AiDictionaryKey, type ComponentAiParams, type FieldAiParams, createAiPlugin, defaultDictionary, withDynamicConfig };
528
+ export { type AIPart, type AiDictionaryKey, type ApplyPartOptions, type ComponentAiParams, type FieldAiParams, type GenerateOptions, type SendMessageOptions, type UsePuckAiOptions, createAiPlugin, defaultDictionary, usePuckAi, withDynamicConfig };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
3
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
+ import * as ai from 'ai';
5
+ import { LanguageModelUsage, UIMessage, DataUIPart, ChatOnFinishCallback, CreateUIMessage, ChatStatus } from 'ai';
4
6
  import { PuckAction, Data, Config } from '@puckeditor/core';
5
- import { LanguageModelUsage, UIMessage, CreateUIMessage, DataUIPart, ChatStatus, ChatOnFinishCallback } from 'ai';
6
7
 
7
8
  type _JSONSchema = boolean | JSONSchema;
8
9
  type JSONSchema = {
@@ -115,6 +116,37 @@ type DataToolStatus = {
115
116
  status: ToolStatus;
116
117
  toolCallId: string;
117
118
  };
119
+ type AiFieldTypeShape<Value> = {
120
+ /** What the field represents, and any rules for using it */
121
+ description?: string;
122
+ /**
123
+ * JSON Schema for the field's value — the shape the model may write, at
124
+ * creation and in later edits. Without one the model never writes the
125
+ * field, like `ai.exclude`.
126
+ */
127
+ schema?: Record<string, any>;
128
+ /**
129
+ * What a field the model designs starts as. When set, the model places the
130
+ * field and this value fills it; the model authors a value against `schema`
131
+ * only when this is absent.
132
+ */
133
+ defaultValue?: Value;
134
+ };
135
+ /**
136
+ * A user field type registered by the developer's config. Puck can't infer a
137
+ * value shape for one, so a type carries `schema`, `defaultValue`, or both.
138
+ */
139
+ type AiFieldType$1<Value = AiFieldTypeValue> = AiFieldTypeShape<Value> & ({
140
+ schema: Record<string, any>;
141
+ } | {
142
+ defaultValue: Value;
143
+ });
144
+ /**
145
+ * Any JSON-ish value. Spelled out rather than `unknown` so a union with a
146
+ * function form doesn't collapse, which would drop the callback's parameter
147
+ * types at the call site.
148
+ */
149
+ type AiFieldTypeValue = string | number | boolean | null | readonly unknown[] | Record<string, unknown>;
118
150
  type AddOperation = {
119
151
  op: "add";
120
152
  id: string;
@@ -203,6 +235,20 @@ type SubagentEvent = {
203
235
  kind: "finish";
204
236
  toolCallId: string;
205
237
  };
238
+ /** A request for data from the agent. */
239
+ type ClientRequest = {
240
+ requestId: string;
241
+ action: "getPageData";
242
+ params: {
243
+ id: string;
244
+ };
245
+ } | {
246
+ requestId: string;
247
+ action: "takeScreenshot";
248
+ params: {
249
+ putUrl: string;
250
+ };
251
+ };
206
252
  type PuckDataParts = {
207
253
  "new-chat-created": {
208
254
  chatId: string;
@@ -219,11 +265,7 @@ type PuckDataParts = {
219
265
  input: any;
220
266
  }[];
221
267
  };
222
- "client-request": {
223
- requestId: string;
224
- action: string;
225
- params?: Record<string, any>;
226
- };
268
+ "client-request": ClientRequest;
227
269
  "send-screenshot": {
228
270
  id: string;
229
271
  urls: {
@@ -247,6 +289,21 @@ type PuckProviderMetadata = {
247
289
 
248
290
  type PuckMessage = UIMessage<PuckProviderMetadata, PuckDataParts>;
249
291
 
292
+ type AiFieldTypeContext = {
293
+ /** The designed component the field belongs to */
294
+ componentName: string;
295
+ /** The field's name within that component */
296
+ fieldName: string;
297
+ };
298
+ /**
299
+ * A field type the developer's config registers: `schema` is what the model
300
+ * may write, `defaultValue` is what a designed field starts as — one or both.
301
+ * In the editor the default may be a function of the component and field
302
+ * being created.
303
+ */
304
+ type AiFieldType = AiFieldType$1<AiFieldTypeValue | ((ctx: AiFieldTypeContext) => AiFieldTypeValue)>;
305
+ type AiFieldTypeRegistry = Record<string, AiFieldType>;
306
+
250
307
  type Mode = "assembly" | "design";
251
308
  type RequestOptions = {
252
309
  body?: {
@@ -261,8 +318,105 @@ type RequestOptions = {
261
318
  headers?: HeadersInit;
262
319
  credentials?: RequestCredentials;
263
320
  };
321
+ type UsePuckAiOptions = {
322
+ /** The API host to call. This should be your server. */
323
+ host?: string;
324
+ /**
325
+ * The mode used for generating the page. Can be `"assembly"` (default),
326
+ * to generate pages based on the provided components, or `"design"`, to
327
+ * design new components, incorporating existing components where possible.
328
+ */
329
+ mode?: Mode;
330
+ apiVersion?: string;
331
+ /**
332
+ * A callback to customize every request the hook makes before it's sent,
333
+ * such as to add custom headers, add metadata to the body, or change
334
+ * credentials.
335
+ */
336
+ prepareRequest?: (opts: RequestOptions) => RequestOptions | Promise<RequestOptions>;
337
+ /** A function called when a chat turn finishes. */
338
+ onFinish?: ChatOnFinishCallback<PuckMessage>;
339
+ /** A function called when a chat turn fails. `generate` rejects instead. */
340
+ onError?: (error: Error) => void;
341
+ };
342
+ type SendMessageOptions = {
343
+ /** Overrides the hook's `mode` for this turn. */
344
+ mode?: Mode;
345
+ /** Files to send with the prompt, as AI SDK file parts. */
346
+ files?: Extract<PuckMessage["parts"][number], {
347
+ type: "file";
348
+ }>[];
349
+ };
350
+ type GenerateOptions = {
351
+ /**
352
+ * The mode for this generation. `"design"` needs
353
+ * `ai.designMode.allowed` on the handler.
354
+ */
355
+ mode?: Mode;
356
+ /** An `AbortSignal` that cancels the generation. */
357
+ signal?: AbortSignal;
358
+ };
359
+ /**
360
+ * AI Parts represent discrete chunks of data sent by Puck AI. They are
361
+ * produced via `generate`'s `onPart` callback.
362
+ */
363
+ type AIPart = DataUIPart<PuckDataParts>;
364
+ type ApplyPartOptions = {
365
+ /**
366
+ * Whether to record the page as one undo step, covering every change since
367
+ * the last one recorded. Defaults to `false`.
368
+ */
369
+ recordHistory?: boolean;
370
+ };
371
+ /**
372
+ * Access Puck AI's internal behavior to create your own agent UI, or implement Puck AI as a client subagent.
373
+ *
374
+ * @param options.host The API host to call. This should be your server.
375
+ * @param options.mode The mode used for generating the page: `"assembly"` (default) or `"design"`.
376
+ * @param options.apiVersion The Puck Cloud API version. Defaults to `v2`.
377
+ * @param options.prepareRequest A callback to customize every request the hook makes before it's sent.
378
+ * @param options.onFinish A function called when a chat turn finishes.
379
+ * @param options.onError A function called when a chat turn fails. `generate` rejects instead.
380
+ * @returns An object containing `chat`, `generate` and `applyPart`.
381
+ */
382
+ declare function usePuckAi(options?: UsePuckAiOptions): {
383
+ chat: {
384
+ /** Send a new message to the agent. */
385
+ sendMessage: (prompt: string, opts?: SendMessageOptions) => Promise<void>;
386
+ /** Run the conversation's last turn again. */
387
+ regenerate: (args_0?: ({
388
+ messageId?: string;
389
+ } & ai.ChatRequestOptions) | undefined) => Promise<void>;
390
+ /** Stop the running turn. */
391
+ stop: () => Promise<void>;
392
+ /** The conversation's AI SDK status. */
393
+ status: ai.ChatStatus;
394
+ /** The last chat error's message, or `undefined`. Also passed to `onError`. */
395
+ error: string | undefined;
396
+ /** The conversation, as AI SDK `UIMessage`s. */
397
+ messages: PuckMessage[];
398
+ /** The live progress of the agent's tools, keyed by tool call id. */
399
+ toolStatus: Record<string, ToolStatus>;
400
+ };
401
+ /**
402
+ * Run a generation against the current page. Returns the page's Data once
403
+ * it's applied. Iterates once, unless the handler's `ai.maxIterations`
404
+ * allows more.
405
+ */
406
+ generate: (prompt: string, opts?: GenerateOptions) => Promise<Data>;
407
+ /** Apply and process an AI Part. */
408
+ applyPart: (part: AIPart, { recordHistory }?: ApplyPartOptions) => Promise<void>;
409
+ };
410
+
264
411
  type AiPluginProps = {
265
412
  host?: string;
413
+ /**
414
+ * Field types this config registers, keyed by field type. Each gives a
415
+ * `schema` the AI may write, a `defaultValue` a designed field starts as, or
416
+ * both. Sent to the server as JSON, so a function `defaultValue` only
417
+ * resolves in the editor.
418
+ */
419
+ fieldTypes?: AiFieldTypeRegistry;
266
420
  chat?: {
267
421
  attachments?: {
268
422
  enabled?: boolean;
@@ -306,7 +460,15 @@ declare global {
306
460
  }
307
461
  }
308
462
 
463
+ /**
464
+ * Add a page's designed components to a config, to render the page.
465
+ *
466
+ * @param config Your Puck config.
467
+ * @param data The page, with its designed components in `root.props._dynamicConfig`.
468
+ * @returns The config, with the page's designed components and globals.
469
+ */
309
470
  declare function withDynamicConfig<UserConfig extends Config>(config: UserConfig, data: Data): UserConfig;
471
+
310
472
  declare function createAiPlugin(opts?: AiPluginProps): {
311
473
  label: string;
312
474
  name: string;
@@ -363,4 +525,4 @@ declare module "@puckeditor/core" {
363
525
  }
364
526
  }
365
527
 
366
- export { type AiDictionaryKey, type ComponentAiParams, type FieldAiParams, createAiPlugin, defaultDictionary, withDynamicConfig };
528
+ export { type AIPart, type AiDictionaryKey, type ApplyPartOptions, type ComponentAiParams, type FieldAiParams, type GenerateOptions, type SendMessageOptions, type UsePuckAiOptions, createAiPlugin, defaultDictionary, usePuckAi, withDynamicConfig };