@puckeditor/cloud-client 0.3.0-canary.41e65b36 → 0.3.0-canary.78ff547c

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.mjs CHANGED
@@ -7,6 +7,186 @@ var __export = (target, all) => {
7
7
  // ../tsup-config/react-import.js
8
8
  import React from "react";
9
9
 
10
+ // src/api/tool.ts
11
+ var tool = (t) => t;
12
+
13
+ // ../../node_modules/.pnpm/@ai-sdk+provider@2.0.0/node_modules/@ai-sdk/provider/dist/index.mjs
14
+ var marker = "vercel.ai.error";
15
+ var symbol = Symbol.for(marker);
16
+ var _a;
17
+ var _AISDKError = class _AISDKError2 extends Error {
18
+ /**
19
+ * Creates an AI SDK Error.
20
+ *
21
+ * @param {Object} params - The parameters for creating the error.
22
+ * @param {string} params.name - The name of the error.
23
+ * @param {string} params.message - The error message.
24
+ * @param {unknown} [params.cause] - The underlying cause of the error.
25
+ */
26
+ constructor({
27
+ name: name143,
28
+ message,
29
+ cause
30
+ }) {
31
+ super(message);
32
+ this[_a] = true;
33
+ this.name = name143;
34
+ this.cause = cause;
35
+ }
36
+ /**
37
+ * Checks if the given error is an AI SDK Error.
38
+ * @param {unknown} error - The error to check.
39
+ * @returns {boolean} True if the error is an AI SDK Error, false otherwise.
40
+ */
41
+ static isInstance(error45) {
42
+ return _AISDKError2.hasMarker(error45, marker);
43
+ }
44
+ static hasMarker(error45, marker153) {
45
+ const markerSymbol = Symbol.for(marker153);
46
+ return error45 != null && typeof error45 === "object" && markerSymbol in error45 && typeof error45[markerSymbol] === "boolean" && error45[markerSymbol] === true;
47
+ }
48
+ };
49
+ _a = symbol;
50
+ var AISDKError = _AISDKError;
51
+ var name = "AI_APICallError";
52
+ var marker2 = `vercel.ai.error.${name}`;
53
+ var symbol2 = Symbol.for(marker2);
54
+ var _a2;
55
+ _a2 = symbol2;
56
+ var name2 = "AI_EmptyResponseBodyError";
57
+ var marker3 = `vercel.ai.error.${name2}`;
58
+ var symbol3 = Symbol.for(marker3);
59
+ var _a3;
60
+ _a3 = symbol3;
61
+ function getErrorMessage(error45) {
62
+ if (error45 == null) {
63
+ return "unknown error";
64
+ }
65
+ if (typeof error45 === "string") {
66
+ return error45;
67
+ }
68
+ if (error45 instanceof Error) {
69
+ return error45.message;
70
+ }
71
+ return JSON.stringify(error45);
72
+ }
73
+ var name3 = "AI_InvalidArgumentError";
74
+ var marker4 = `vercel.ai.error.${name3}`;
75
+ var symbol4 = Symbol.for(marker4);
76
+ var _a4;
77
+ var InvalidArgumentError = class extends AISDKError {
78
+ constructor({
79
+ message,
80
+ cause,
81
+ argument
82
+ }) {
83
+ super({ name: name3, message, cause });
84
+ this[_a4] = true;
85
+ this.argument = argument;
86
+ }
87
+ static isInstance(error45) {
88
+ return AISDKError.hasMarker(error45, marker4);
89
+ }
90
+ };
91
+ _a4 = symbol4;
92
+ var name4 = "AI_InvalidPromptError";
93
+ var marker5 = `vercel.ai.error.${name4}`;
94
+ var symbol5 = Symbol.for(marker5);
95
+ var _a5;
96
+ _a5 = symbol5;
97
+ var name5 = "AI_InvalidResponseDataError";
98
+ var marker6 = `vercel.ai.error.${name5}`;
99
+ var symbol6 = Symbol.for(marker6);
100
+ var _a6;
101
+ _a6 = symbol6;
102
+ var name6 = "AI_JSONParseError";
103
+ var marker7 = `vercel.ai.error.${name6}`;
104
+ var symbol7 = Symbol.for(marker7);
105
+ var _a7;
106
+ var JSONParseError = class extends AISDKError {
107
+ constructor({ text: text2, cause }) {
108
+ super({
109
+ name: name6,
110
+ message: `JSON parsing failed: Text: ${text2}.
111
+ Error message: ${getErrorMessage(cause)}`,
112
+ cause
113
+ });
114
+ this[_a7] = true;
115
+ this.text = text2;
116
+ }
117
+ static isInstance(error45) {
118
+ return AISDKError.hasMarker(error45, marker7);
119
+ }
120
+ };
121
+ _a7 = symbol7;
122
+ var name7 = "AI_LoadAPIKeyError";
123
+ var marker8 = `vercel.ai.error.${name7}`;
124
+ var symbol8 = Symbol.for(marker8);
125
+ var _a8;
126
+ _a8 = symbol8;
127
+ var name8 = "AI_LoadSettingError";
128
+ var marker9 = `vercel.ai.error.${name8}`;
129
+ var symbol9 = Symbol.for(marker9);
130
+ var _a9;
131
+ _a9 = symbol9;
132
+ var name9 = "AI_NoContentGeneratedError";
133
+ var marker10 = `vercel.ai.error.${name9}`;
134
+ var symbol10 = Symbol.for(marker10);
135
+ var _a10;
136
+ _a10 = symbol10;
137
+ var name10 = "AI_NoSuchModelError";
138
+ var marker11 = `vercel.ai.error.${name10}`;
139
+ var symbol11 = Symbol.for(marker11);
140
+ var _a11;
141
+ _a11 = symbol11;
142
+ var name11 = "AI_TooManyEmbeddingValuesForCallError";
143
+ var marker12 = `vercel.ai.error.${name11}`;
144
+ var symbol12 = Symbol.for(marker12);
145
+ var _a12;
146
+ _a12 = symbol12;
147
+ var name12 = "AI_TypeValidationError";
148
+ var marker13 = `vercel.ai.error.${name12}`;
149
+ var symbol13 = Symbol.for(marker13);
150
+ var _a13;
151
+ var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
152
+ constructor({ value, cause }) {
153
+ super({
154
+ name: name12,
155
+ message: `Type validation failed: Value: ${JSON.stringify(value)}.
156
+ Error message: ${getErrorMessage(cause)}`,
157
+ cause
158
+ });
159
+ this[_a13] = true;
160
+ this.value = value;
161
+ }
162
+ static isInstance(error45) {
163
+ return AISDKError.hasMarker(error45, marker13);
164
+ }
165
+ /**
166
+ * Wraps an error into a TypeValidationError.
167
+ * If the cause is already a TypeValidationError with the same value, it returns the cause.
168
+ * Otherwise, it creates a new TypeValidationError.
169
+ *
170
+ * @param {Object} params - The parameters for wrapping the error.
171
+ * @param {unknown} params.value - The value that failed validation.
172
+ * @param {unknown} params.cause - The original error or cause of the validation failure.
173
+ * @returns {TypeValidationError} A TypeValidationError instance.
174
+ */
175
+ static wrap({
176
+ value,
177
+ cause
178
+ }) {
179
+ return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
180
+ }
181
+ };
182
+ _a13 = symbol13;
183
+ var TypeValidationError = _TypeValidationError;
184
+ var name13 = "AI_UnsupportedFunctionalityError";
185
+ var marker14 = `vercel.ai.error.${name13}`;
186
+ var symbol14 = Symbol.for(marker14);
187
+ var _a14;
188
+ _a14 = symbol14;
189
+
10
190
  // ../../node_modules/.pnpm/zod@4.1.9/node_modules/zod/v4/classic/external.js
11
191
  var external_exports = {};
12
192
  __export(external_exports, {
@@ -211,7 +391,7 @@ __export(external_exports, {
211
391
  stringbool: () => stringbool,
212
392
  success: () => success,
213
393
  superRefine: () => superRefine,
214
- symbol: () => symbol,
394
+ symbol: () => symbol15,
215
395
  templateLiteral: () => templateLiteral,
216
396
  toJSONSchema: () => toJSONSchema,
217
397
  toLowerCase: () => _toLowerCase,
@@ -11786,7 +11966,7 @@ var ZodSymbol = /* @__PURE__ */ $constructor("ZodSymbol", (inst, def) => {
11786
11966
  $ZodSymbol.init(inst, def);
11787
11967
  ZodType.init(inst, def);
11788
11968
  });
11789
- function symbol(params) {
11969
+ function symbol15(params) {
11790
11970
  return _symbol(ZodSymbol, params);
11791
11971
  }
11792
11972
  var ZodUndefined = /* @__PURE__ */ $constructor("ZodUndefined", (inst, def) => {
@@ -12429,198 +12609,6 @@ var classic_default = external_exports;
12429
12609
  // ../../node_modules/.pnpm/zod@4.1.9/node_modules/zod/v4/index.js
12430
12610
  var v4_default = classic_default;
12431
12611
 
12432
- // src/lib/tool.ts
12433
- var tool = (t) => t;
12434
- var prepareUserTools = (toolRegistry) => Object.keys(toolRegistry).reduce(
12435
- (acc, key) => ({
12436
- ...acc,
12437
- [key]: {
12438
- name: toolRegistry[key].name ?? key,
12439
- description: toolRegistry[key].description,
12440
- inputSchema: v4_default.toJSONSchema(toolRegistry[key].inputSchema),
12441
- outputSchema: toolRegistry[key].outputSchema ? v4_default.toJSONSchema(toolRegistry[key].outputSchema) : null
12442
- }
12443
- }),
12444
- {}
12445
- );
12446
-
12447
- // ../../node_modules/.pnpm/@ai-sdk+provider@2.0.0/node_modules/@ai-sdk/provider/dist/index.mjs
12448
- var marker = "vercel.ai.error";
12449
- var symbol2 = Symbol.for(marker);
12450
- var _a;
12451
- var _AISDKError = class _AISDKError2 extends Error {
12452
- /**
12453
- * Creates an AI SDK Error.
12454
- *
12455
- * @param {Object} params - The parameters for creating the error.
12456
- * @param {string} params.name - The name of the error.
12457
- * @param {string} params.message - The error message.
12458
- * @param {unknown} [params.cause] - The underlying cause of the error.
12459
- */
12460
- constructor({
12461
- name: name143,
12462
- message,
12463
- cause
12464
- }) {
12465
- super(message);
12466
- this[_a] = true;
12467
- this.name = name143;
12468
- this.cause = cause;
12469
- }
12470
- /**
12471
- * Checks if the given error is an AI SDK Error.
12472
- * @param {unknown} error - The error to check.
12473
- * @returns {boolean} True if the error is an AI SDK Error, false otherwise.
12474
- */
12475
- static isInstance(error45) {
12476
- return _AISDKError2.hasMarker(error45, marker);
12477
- }
12478
- static hasMarker(error45, marker153) {
12479
- const markerSymbol = Symbol.for(marker153);
12480
- return error45 != null && typeof error45 === "object" && markerSymbol in error45 && typeof error45[markerSymbol] === "boolean" && error45[markerSymbol] === true;
12481
- }
12482
- };
12483
- _a = symbol2;
12484
- var AISDKError = _AISDKError;
12485
- var name = "AI_APICallError";
12486
- var marker2 = `vercel.ai.error.${name}`;
12487
- var symbol22 = Symbol.for(marker2);
12488
- var _a2;
12489
- _a2 = symbol22;
12490
- var name2 = "AI_EmptyResponseBodyError";
12491
- var marker3 = `vercel.ai.error.${name2}`;
12492
- var symbol3 = Symbol.for(marker3);
12493
- var _a3;
12494
- _a3 = symbol3;
12495
- function getErrorMessage(error45) {
12496
- if (error45 == null) {
12497
- return "unknown error";
12498
- }
12499
- if (typeof error45 === "string") {
12500
- return error45;
12501
- }
12502
- if (error45 instanceof Error) {
12503
- return error45.message;
12504
- }
12505
- return JSON.stringify(error45);
12506
- }
12507
- var name3 = "AI_InvalidArgumentError";
12508
- var marker4 = `vercel.ai.error.${name3}`;
12509
- var symbol4 = Symbol.for(marker4);
12510
- var _a4;
12511
- var InvalidArgumentError = class extends AISDKError {
12512
- constructor({
12513
- message,
12514
- cause,
12515
- argument
12516
- }) {
12517
- super({ name: name3, message, cause });
12518
- this[_a4] = true;
12519
- this.argument = argument;
12520
- }
12521
- static isInstance(error45) {
12522
- return AISDKError.hasMarker(error45, marker4);
12523
- }
12524
- };
12525
- _a4 = symbol4;
12526
- var name4 = "AI_InvalidPromptError";
12527
- var marker5 = `vercel.ai.error.${name4}`;
12528
- var symbol5 = Symbol.for(marker5);
12529
- var _a5;
12530
- _a5 = symbol5;
12531
- var name5 = "AI_InvalidResponseDataError";
12532
- var marker6 = `vercel.ai.error.${name5}`;
12533
- var symbol6 = Symbol.for(marker6);
12534
- var _a6;
12535
- _a6 = symbol6;
12536
- var name6 = "AI_JSONParseError";
12537
- var marker7 = `vercel.ai.error.${name6}`;
12538
- var symbol7 = Symbol.for(marker7);
12539
- var _a7;
12540
- var JSONParseError = class extends AISDKError {
12541
- constructor({ text: text2, cause }) {
12542
- super({
12543
- name: name6,
12544
- message: `JSON parsing failed: Text: ${text2}.
12545
- Error message: ${getErrorMessage(cause)}`,
12546
- cause
12547
- });
12548
- this[_a7] = true;
12549
- this.text = text2;
12550
- }
12551
- static isInstance(error45) {
12552
- return AISDKError.hasMarker(error45, marker7);
12553
- }
12554
- };
12555
- _a7 = symbol7;
12556
- var name7 = "AI_LoadAPIKeyError";
12557
- var marker8 = `vercel.ai.error.${name7}`;
12558
- var symbol8 = Symbol.for(marker8);
12559
- var _a8;
12560
- _a8 = symbol8;
12561
- var name8 = "AI_LoadSettingError";
12562
- var marker9 = `vercel.ai.error.${name8}`;
12563
- var symbol9 = Symbol.for(marker9);
12564
- var _a9;
12565
- _a9 = symbol9;
12566
- var name9 = "AI_NoContentGeneratedError";
12567
- var marker10 = `vercel.ai.error.${name9}`;
12568
- var symbol10 = Symbol.for(marker10);
12569
- var _a10;
12570
- _a10 = symbol10;
12571
- var name10 = "AI_NoSuchModelError";
12572
- var marker11 = `vercel.ai.error.${name10}`;
12573
- var symbol11 = Symbol.for(marker11);
12574
- var _a11;
12575
- _a11 = symbol11;
12576
- var name11 = "AI_TooManyEmbeddingValuesForCallError";
12577
- var marker12 = `vercel.ai.error.${name11}`;
12578
- var symbol12 = Symbol.for(marker12);
12579
- var _a12;
12580
- _a12 = symbol12;
12581
- var name12 = "AI_TypeValidationError";
12582
- var marker13 = `vercel.ai.error.${name12}`;
12583
- var symbol13 = Symbol.for(marker13);
12584
- var _a13;
12585
- var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
12586
- constructor({ value, cause }) {
12587
- super({
12588
- name: name12,
12589
- message: `Type validation failed: Value: ${JSON.stringify(value)}.
12590
- Error message: ${getErrorMessage(cause)}`,
12591
- cause
12592
- });
12593
- this[_a13] = true;
12594
- this.value = value;
12595
- }
12596
- static isInstance(error45) {
12597
- return AISDKError.hasMarker(error45, marker13);
12598
- }
12599
- /**
12600
- * Wraps an error into a TypeValidationError.
12601
- * If the cause is already a TypeValidationError with the same value, it returns the cause.
12602
- * Otherwise, it creates a new TypeValidationError.
12603
- *
12604
- * @param {Object} params - The parameters for wrapping the error.
12605
- * @param {unknown} params.value - The value that failed validation.
12606
- * @param {unknown} params.cause - The original error or cause of the validation failure.
12607
- * @returns {TypeValidationError} A TypeValidationError instance.
12608
- */
12609
- static wrap({
12610
- value,
12611
- cause
12612
- }) {
12613
- return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
12614
- }
12615
- };
12616
- _a13 = symbol13;
12617
- var TypeValidationError = _TypeValidationError;
12618
- var name13 = "AI_UnsupportedFunctionalityError";
12619
- var marker14 = `vercel.ai.error.${name13}`;
12620
- var symbol14 = Symbol.for(marker14);
12621
- var _a14;
12622
- _a14 = symbol14;
12623
-
12624
12612
  // ../../node_modules/.pnpm/zod@4.1.9/node_modules/zod/v3/helpers/util.js
12625
12613
  var util;
12626
12614
  (function(util2) {
@@ -17833,14 +17821,14 @@ var __export2 = (target, all) => {
17833
17821
  };
17834
17822
  var name14 = "AI_NoOutputSpecifiedError";
17835
17823
  var marker15 = `vercel.ai.error.${name14}`;
17836
- var symbol15 = Symbol.for(marker15);
17824
+ var symbol16 = Symbol.for(marker15);
17837
17825
  var _a15;
17838
- _a15 = symbol15;
17826
+ _a15 = symbol16;
17839
17827
  var name22 = "AI_InvalidArgumentError";
17840
17828
  var marker22 = `vercel.ai.error.${name22}`;
17841
- var symbol23 = Symbol.for(marker22);
17829
+ var symbol22 = Symbol.for(marker22);
17842
17830
  var _a22;
17843
- _a22 = symbol23;
17831
+ _a22 = symbol22;
17844
17832
  var name32 = "AI_InvalidStreamPartError";
17845
17833
  var marker32 = `vercel.ai.error.${name32}`;
17846
17834
  var symbol32 = Symbol.for(marker32);
@@ -19237,9 +19225,9 @@ var object2 = ({
19237
19225
  };
19238
19226
  var name16 = "AI_NoSuchProviderError";
19239
19227
  var marker16 = `vercel.ai.error.${name16}`;
19240
- var symbol16 = Symbol.for(marker16);
19228
+ var symbol162 = Symbol.for(marker16);
19241
19229
  var _a16;
19242
- _a16 = symbol16;
19230
+ _a16 = symbol162;
19243
19231
  var ClientOrServerImplementationSchema = external_exports.looseObject({
19244
19232
  name: external_exports.string(),
19245
19233
  version: external_exports.string()
@@ -19592,8 +19580,64 @@ function createUIMessageStream({
19592
19580
  });
19593
19581
  }
19594
19582
 
19595
- // src/lib/chat.ts
19596
- async function* iterSSE(body) {
19583
+ // src/lib/prepare-user-tools.ts
19584
+ var prepareUserTools = (toolRegistry) => Object.keys(toolRegistry).reduce(
19585
+ (acc, key) => ({
19586
+ ...acc,
19587
+ [key]: {
19588
+ name: toolRegistry[key].name ?? key,
19589
+ description: toolRegistry[key].description,
19590
+ inputSchema: v4_default.toJSONSchema(toolRegistry[key].inputSchema),
19591
+ outputSchema: toolRegistry[key].outputSchema ? v4_default.toJSONSchema(toolRegistry[key].outputSchema) : null
19592
+ }
19593
+ }),
19594
+ {}
19595
+ );
19596
+
19597
+ // src/lib/get-api-key.ts
19598
+ var getApiKey = () => process.env.PUCK_API_KEY;
19599
+
19600
+ // src/lib/process-user-tool.ts
19601
+ var processUserTool = async ({
19602
+ data,
19603
+ tools,
19604
+ options
19605
+ }) => {
19606
+ const { toolCallId, tools: requestTools } = data;
19607
+ const { apiKey = getApiKey(), host = "https://cloud.puckeditor.com/api" } = options;
19608
+ if (!apiKey) {
19609
+ throw new Error(
19610
+ "No Puck API key specified. Set the PUCK_API_KEY environment variable, or provide one"
19611
+ );
19612
+ }
19613
+ const responses = [];
19614
+ for (let i = 0; i < requestTools.length; i++) {
19615
+ const { name: name17, input } = requestTools[i];
19616
+ const tool2 = tools?.[name17];
19617
+ if (!tool2) {
19618
+ throw new Error(`An unknown tool was called: ${name17}`);
19619
+ }
19620
+ if (!input) {
19621
+ throw new Error(`Input was not present in tool call: ${toolCallId}`);
19622
+ }
19623
+ const output = await tool2.execute(input);
19624
+ responses.push({ name: name17, input, output });
19625
+ }
19626
+ const res = await fetch(`${host}/tool`, {
19627
+ headers: { "x-api-key": apiKey },
19628
+ method: "post",
19629
+ body: JSON.stringify({
19630
+ id: toolCallId,
19631
+ responses
19632
+ })
19633
+ });
19634
+ if (res.status !== 200) {
19635
+ throw new Error("Tool result could not be sent to server");
19636
+ }
19637
+ };
19638
+
19639
+ // src/lib/iterate-sse.ts
19640
+ async function* iterateSSE(body) {
19597
19641
  const reader = body.getReader();
19598
19642
  const dec = new TextDecoder();
19599
19643
  let buf = "";
@@ -19614,81 +19658,84 @@ async function* iterSSE(body) {
19614
19658
  }
19615
19659
  }
19616
19660
  }
19617
- var getApiKey = () => process.env.PUCK_API_KEY;
19618
- function chat({ chatId, messages: initialMessages, config: config2, pageData }, {
19619
- ai = {},
19620
- apiKey = getApiKey(),
19621
- host = "https://cloud.puckeditor.com/api"
19622
- }) {
19661
+
19662
+ // src/lib/cloud-api.ts
19663
+ var cloudApi = async (path, body, options = {}, onChunk) => {
19664
+ const {
19665
+ ai = {},
19666
+ apiKey = getApiKey(),
19667
+ host = "https://cloud.puckeditor.com/api"
19668
+ } = options;
19623
19669
  if (!apiKey) {
19624
19670
  throw new Error(
19625
- `No Puck API key specified. Set the PUCK_API_KEY environment variable, or provide one to the function.
19626
-
19627
- Visit https://cloud.puckeditor.com/sign-up to register for an account and generate a key.`
19671
+ "No Puck API key specified. Set the PUCK_API_KEY environment variable, or provide one to the function"
19628
19672
  );
19629
19673
  }
19630
19674
  const { context, tools = {} } = ai;
19631
- let messages = initialMessages;
19675
+ const res = await fetch(`${host}/${path}`, {
19676
+ headers: { "x-api-key": apiKey },
19677
+ method: "post",
19678
+ body: JSON.stringify({
19679
+ ...body,
19680
+ context,
19681
+ tools: prepareUserTools(tools)
19682
+ })
19683
+ });
19684
+ if (!res.body) {
19685
+ throw new Error(`Puck ${res.status} (${res.statusText})`);
19686
+ } else if (!res.ok) {
19687
+ const body2 = await res.json();
19688
+ throw new Error(
19689
+ `Puck ${res.status} (${res.statusText}): ${body2.error ?? "Unknown reason"}`
19690
+ );
19691
+ }
19692
+ for await (const chunk of iterateSSE(res.body)) {
19693
+ if (chunk.type === "data-user-tool") {
19694
+ await processUserTool({ data: chunk.data, tools, options });
19695
+ } else {
19696
+ onChunk(chunk);
19697
+ }
19698
+ }
19699
+ return res;
19700
+ };
19701
+
19702
+ // src/api/chat.ts
19703
+ function chat({ chatId, messages, config: config2, pageData }, options = {}) {
19632
19704
  const stream = createUIMessageStream({
19633
19705
  execute: async ({ writer }) => {
19634
- const res = await fetch(`${host}/chat`, {
19635
- headers: { "x-api-key": apiKey },
19636
- method: "post",
19637
- body: JSON.stringify({
19638
- chatId,
19639
- context,
19640
- messages,
19641
- config: config2,
19642
- pageData,
19643
- tools: prepareUserTools(tools)
19644
- })
19645
- });
19646
- if (!res.body) {
19647
- throw new Error(`Puck ${res.status} (${res.statusText})`);
19648
- } else if (!res.ok) {
19649
- const body = await res.json();
19650
- throw new Error(
19651
- `Puck ${res.status} (${res.statusText}): ${body.error ?? "Unknown reason"}`
19652
- );
19653
- }
19654
- for await (const chunk of iterSSE(res.body)) {
19655
- if (chunk.type === "data-user-tool") {
19656
- const { toolCallId, tools: requestTools } = chunk.data;
19657
- const responses = [];
19658
- for (let i = 0; i < requestTools.length; i++) {
19659
- const { name: name17, input } = requestTools[i];
19660
- const tool2 = tools?.[name17];
19661
- if (!tool2) {
19662
- throw new Error(`An unknown tool was called: ${name17}`);
19663
- }
19664
- if (!input) {
19665
- throw new Error(
19666
- `Input was not present in tool call: ${toolCallId}`
19667
- );
19668
- }
19669
- const output = await tool2.execute(input);
19670
- responses.push({ name: name17, input, output });
19671
- }
19672
- const res2 = await fetch(`${host}/chat/tool`, {
19673
- headers: { "x-api-key": apiKey },
19674
- method: "post",
19675
- body: JSON.stringify({
19676
- id: toolCallId,
19677
- responses
19678
- })
19679
- });
19680
- if (res2.status !== 200) {
19681
- throw new Error("Tool result could not be sent to server");
19682
- }
19683
- } else {
19706
+ await cloudApi(
19707
+ "chat",
19708
+ { chatId, config: config2, messages, pageData },
19709
+ options,
19710
+ (chunk) => {
19684
19711
  writer.write(chunk);
19685
19712
  }
19686
- }
19713
+ );
19687
19714
  }
19688
19715
  });
19689
19716
  return createUIMessageStreamResponse({ stream });
19690
19717
  }
19691
19718
 
19719
+ // src/api/generate.ts
19720
+ async function generate({
19721
+ prompt,
19722
+ config: config2,
19723
+ pageData,
19724
+ context,
19725
+ tools,
19726
+ apiKey,
19727
+ host
19728
+ }) {
19729
+ let result = null;
19730
+ const options = { ai: { context, tools }, apiKey, host };
19731
+ await cloudApi("generate", { prompt, config: config2, pageData }, options, (chunk) => {
19732
+ if (chunk.type === "data-page") {
19733
+ result = chunk.data;
19734
+ }
19735
+ });
19736
+ return result;
19737
+ }
19738
+
19692
19739
  // src/routes.ts
19693
19740
  var routeRegistry = {
19694
19741
  "/api/puck/chat": {
@@ -19699,7 +19746,7 @@ var findRoute = (pathname, method) => {
19699
19746
  return routeRegistry[pathname][method];
19700
19747
  };
19701
19748
 
19702
- // src/lib/puck-handler.ts
19749
+ // src/api/puck-handler.ts
19703
19750
  async function puckHandler(request, options) {
19704
19751
  const cloned = request.clone();
19705
19752
  const url2 = new URL(cloned.url);
@@ -19725,6 +19772,7 @@ var endpoints = ["chat"];
19725
19772
  export {
19726
19773
  chat,
19727
19774
  endpoints,
19775
+ generate,
19728
19776
  puckHandler,
19729
19777
  tool
19730
19778
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puckeditor/cloud-client",
3
- "version": "0.3.0-canary.41e65b36",
3
+ "version": "0.3.0-canary.78ff547c",
4
4
  "author": "Chris Villa <chris@puckeditor.com>",
5
5
  "repository": "puckeditor/puck",
6
6
  "bugs": "https://github.com/puckeditor/puck/issues",
@@ -24,7 +24,7 @@
24
24
  "dist"
25
25
  ],
26
26
  "devDependencies": {
27
- "@measured/puck": "0.21.0-canary.8416d520",
27
+ "@measured/puck": "0.21.0-canary.c0db75c1",
28
28
  "@types/node": "^24.3.0",
29
29
  "ai": "^5.0.29",
30
30
  "eslint": "^7.32.0",