@puckeditor/plugin-ai 0.8.2 → 0.9.0-canary.50fad349

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, ChatOnFinishCallback, DataUIPart, 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 = {
@@ -247,6 +248,32 @@ type PuckProviderMetadata = {
247
248
 
248
249
  type PuckMessage = UIMessage<PuckProviderMetadata, PuckDataParts>;
249
250
 
251
+ type SubagentChildPart = {
252
+ kind: "text";
253
+ blockId: string;
254
+ text: string;
255
+ } | {
256
+ kind: "reasoning";
257
+ blockId: string;
258
+ text: string;
259
+ state: "streaming" | "done";
260
+ } | {
261
+ kind: "tool";
262
+ childToolCallId: string;
263
+ toolName: string;
264
+ input?: unknown;
265
+ output?: unknown;
266
+ };
267
+ type SubagentState = {
268
+ toolCallId: string;
269
+ parentToolCallId?: string;
270
+ agentType: string;
271
+ depth: number;
272
+ label: string;
273
+ state: "running" | "done";
274
+ parts: SubagentChildPart[];
275
+ };
276
+
250
277
  type Mode = "assembly" | "design";
251
278
  type RequestOptions = {
252
279
  body?: {
@@ -261,6 +288,84 @@ type RequestOptions = {
261
288
  headers?: HeadersInit;
262
289
  credentials?: RequestCredentials;
263
290
  };
291
+ type UsePuckAiOptions = {
292
+ /** Endpoint serving the chat stream. Defaults to `/api/puck/chat`. */
293
+ host?: string;
294
+ /**
295
+ * Endpoint serving one-shot generations for `stream()`. Defaults to
296
+ * `/api/puck/stream`.
297
+ */
298
+ streamHost?: string;
299
+ /**
300
+ * Whether the agent assembles your components or designs new ones. Read at
301
+ * request time, so a plugin can flip it between calls.
302
+ */
303
+ mode?: Mode;
304
+ apiVersion?: string;
305
+ /** Adjust the outgoing request — add auth headers, extra body fields. */
306
+ prepareRequest?: (opts: RequestOptions) => RequestOptions | Promise<RequestOptions>;
307
+ onFinish?: ChatOnFinishCallback<PuckMessage>;
308
+ onError?: (error: Error) => void;
309
+ };
310
+ type SendMessageOptions = {
311
+ /** Override the hook's mode for this call. */
312
+ mode?: Mode;
313
+ /** Attachments to send alongside the prompt. */
314
+ files?: Extract<PuckMessage["parts"][number], {
315
+ type: "file";
316
+ }>[];
317
+ };
318
+ type StreamOptions = {
319
+ /** Override the hook's mode for this call. */
320
+ mode?: Mode;
321
+ /** Extra context for this generation. */
322
+ context?: string;
323
+ };
324
+ /**
325
+ * Puck AI, wired into the editor you're already inside.
326
+ *
327
+ * Call it from any plugin render or override — it reads the editor through
328
+ * `useGetPuck`, so there's no provider to add. `stream()` sends a prompt and
329
+ * applies everything that comes back to the live canvas: component writes,
330
+ * design-mode definitions, and the screenshot/page-data round trips the agent
331
+ * uses to see what it built.
332
+ *
333
+ * The UI is yours. This owns the transport and the editor mutations only.
334
+ */
335
+ declare function usePuckAi({ host, streamHost, mode, apiVersion, prepareRequest, onFinish, onError, }?: UsePuckAiOptions): {
336
+ /**
337
+ * Send a prompt as the next turn of a conversation. The agent sees the
338
+ * history, and can write, check its work and correct itself.
339
+ */
340
+ sendMessage: (prompt: string, opts?: SendMessageOptions) => Promise<void>;
341
+ /**
342
+ * Run a one-shot generation from a prompt, with no conversation. Cheaper
343
+ * and more predictable than `sendMessage`, but it stops after its first
344
+ * write rather than reviewing the result.
345
+ */
346
+ stream: (prompt: string, opts?: StreamOptions) => Promise<void>;
347
+ /** Abort whichever request is in flight */
348
+ stop: () => void;
349
+ status: ai.ChatStatus;
350
+ /** The conversation so far, for building a transcript */
351
+ messages: PuckMessage[];
352
+ setMessages: (messages: PuckMessage[] | ((messages: PuckMessage[]) => PuckMessage[])) => void;
353
+ regenerate: ({ messageId, ...options }?: {
354
+ messageId?: string;
355
+ } & ai.ChatRequestOptions) => Promise<void>;
356
+ error: string | undefined;
357
+ /** Live per-tool progress, keyed by tool call id */
358
+ toolStatus: Record<string, ToolStatus>;
359
+ /** Live subagent progress, keyed by tool call id */
360
+ subagentState: Record<string, SubagentState>;
361
+ /**
362
+ * @internal Apply a single stream part to the editor. Exposed for
363
+ * replaying a recorded stream (the marketing demo) — a real agent should
364
+ * go through `stream`.
365
+ */
366
+ processData: (dataPart: DataUIPart<PuckDataParts>) => void;
367
+ };
368
+
264
369
  type AiPluginProps = {
265
370
  host?: string;
266
371
  chat?: {
@@ -306,6 +411,7 @@ declare global {
306
411
  }
307
412
 
308
413
  declare function withDynamicConfig<UserConfig extends Config>(config: UserConfig, data: Data): UserConfig;
414
+
309
415
  declare function createAiPlugin(opts?: AiPluginProps): {
310
416
  label: string;
311
417
  name: string;
@@ -322,4 +428,4 @@ declare function createAiPlugin(opts?: AiPluginProps): {
322
428
  };
323
429
  };
324
430
 
325
- export { type ComponentAiParams, type FieldAiParams, createAiPlugin, withDynamicConfig };
431
+ export { type ComponentAiParams, type FieldAiParams, type StreamOptions, type UsePuckAiOptions, createAiPlugin, 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, ChatOnFinishCallback, DataUIPart, 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 = {
@@ -247,6 +248,32 @@ type PuckProviderMetadata = {
247
248
 
248
249
  type PuckMessage = UIMessage<PuckProviderMetadata, PuckDataParts>;
249
250
 
251
+ type SubagentChildPart = {
252
+ kind: "text";
253
+ blockId: string;
254
+ text: string;
255
+ } | {
256
+ kind: "reasoning";
257
+ blockId: string;
258
+ text: string;
259
+ state: "streaming" | "done";
260
+ } | {
261
+ kind: "tool";
262
+ childToolCallId: string;
263
+ toolName: string;
264
+ input?: unknown;
265
+ output?: unknown;
266
+ };
267
+ type SubagentState = {
268
+ toolCallId: string;
269
+ parentToolCallId?: string;
270
+ agentType: string;
271
+ depth: number;
272
+ label: string;
273
+ state: "running" | "done";
274
+ parts: SubagentChildPart[];
275
+ };
276
+
250
277
  type Mode = "assembly" | "design";
251
278
  type RequestOptions = {
252
279
  body?: {
@@ -261,6 +288,84 @@ type RequestOptions = {
261
288
  headers?: HeadersInit;
262
289
  credentials?: RequestCredentials;
263
290
  };
291
+ type UsePuckAiOptions = {
292
+ /** Endpoint serving the chat stream. Defaults to `/api/puck/chat`. */
293
+ host?: string;
294
+ /**
295
+ * Endpoint serving one-shot generations for `stream()`. Defaults to
296
+ * `/api/puck/stream`.
297
+ */
298
+ streamHost?: string;
299
+ /**
300
+ * Whether the agent assembles your components or designs new ones. Read at
301
+ * request time, so a plugin can flip it between calls.
302
+ */
303
+ mode?: Mode;
304
+ apiVersion?: string;
305
+ /** Adjust the outgoing request — add auth headers, extra body fields. */
306
+ prepareRequest?: (opts: RequestOptions) => RequestOptions | Promise<RequestOptions>;
307
+ onFinish?: ChatOnFinishCallback<PuckMessage>;
308
+ onError?: (error: Error) => void;
309
+ };
310
+ type SendMessageOptions = {
311
+ /** Override the hook's mode for this call. */
312
+ mode?: Mode;
313
+ /** Attachments to send alongside the prompt. */
314
+ files?: Extract<PuckMessage["parts"][number], {
315
+ type: "file";
316
+ }>[];
317
+ };
318
+ type StreamOptions = {
319
+ /** Override the hook's mode for this call. */
320
+ mode?: Mode;
321
+ /** Extra context for this generation. */
322
+ context?: string;
323
+ };
324
+ /**
325
+ * Puck AI, wired into the editor you're already inside.
326
+ *
327
+ * Call it from any plugin render or override — it reads the editor through
328
+ * `useGetPuck`, so there's no provider to add. `stream()` sends a prompt and
329
+ * applies everything that comes back to the live canvas: component writes,
330
+ * design-mode definitions, and the screenshot/page-data round trips the agent
331
+ * uses to see what it built.
332
+ *
333
+ * The UI is yours. This owns the transport and the editor mutations only.
334
+ */
335
+ declare function usePuckAi({ host, streamHost, mode, apiVersion, prepareRequest, onFinish, onError, }?: UsePuckAiOptions): {
336
+ /**
337
+ * Send a prompt as the next turn of a conversation. The agent sees the
338
+ * history, and can write, check its work and correct itself.
339
+ */
340
+ sendMessage: (prompt: string, opts?: SendMessageOptions) => Promise<void>;
341
+ /**
342
+ * Run a one-shot generation from a prompt, with no conversation. Cheaper
343
+ * and more predictable than `sendMessage`, but it stops after its first
344
+ * write rather than reviewing the result.
345
+ */
346
+ stream: (prompt: string, opts?: StreamOptions) => Promise<void>;
347
+ /** Abort whichever request is in flight */
348
+ stop: () => void;
349
+ status: ai.ChatStatus;
350
+ /** The conversation so far, for building a transcript */
351
+ messages: PuckMessage[];
352
+ setMessages: (messages: PuckMessage[] | ((messages: PuckMessage[]) => PuckMessage[])) => void;
353
+ regenerate: ({ messageId, ...options }?: {
354
+ messageId?: string;
355
+ } & ai.ChatRequestOptions) => Promise<void>;
356
+ error: string | undefined;
357
+ /** Live per-tool progress, keyed by tool call id */
358
+ toolStatus: Record<string, ToolStatus>;
359
+ /** Live subagent progress, keyed by tool call id */
360
+ subagentState: Record<string, SubagentState>;
361
+ /**
362
+ * @internal Apply a single stream part to the editor. Exposed for
363
+ * replaying a recorded stream (the marketing demo) — a real agent should
364
+ * go through `stream`.
365
+ */
366
+ processData: (dataPart: DataUIPart<PuckDataParts>) => void;
367
+ };
368
+
264
369
  type AiPluginProps = {
265
370
  host?: string;
266
371
  chat?: {
@@ -306,6 +411,7 @@ declare global {
306
411
  }
307
412
 
308
413
  declare function withDynamicConfig<UserConfig extends Config>(config: UserConfig, data: Data): UserConfig;
414
+
309
415
  declare function createAiPlugin(opts?: AiPluginProps): {
310
416
  label: string;
311
417
  name: string;
@@ -322,4 +428,4 @@ declare function createAiPlugin(opts?: AiPluginProps): {
322
428
  };
323
429
  };
324
430
 
325
- export { type ComponentAiParams, type FieldAiParams, createAiPlugin, withDynamicConfig };
431
+ export { type ComponentAiParams, type FieldAiParams, type StreamOptions, type UsePuckAiOptions, createAiPlugin, usePuckAi, withDynamicConfig };