@puckeditor/plugin-ai 0.9.0-canary.9f3836fd → 0.9.0-canary.b2f843c3

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