@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.js CHANGED
@@ -32,6 +32,7 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  chat: () => chat,
34
34
  endpoints: () => endpoints,
35
+ generate: () => generate,
35
36
  puckHandler: () => puckHandler,
36
37
  tool: () => tool
37
38
  });
@@ -40,6 +41,186 @@ module.exports = __toCommonJS(index_exports);
40
41
  // ../tsup-config/react-import.js
41
42
  var import_react = __toESM(require("react"));
42
43
 
44
+ // src/api/tool.ts
45
+ var tool = (t) => t;
46
+
47
+ // ../../node_modules/.pnpm/@ai-sdk+provider@2.0.0/node_modules/@ai-sdk/provider/dist/index.mjs
48
+ var marker = "vercel.ai.error";
49
+ var symbol = Symbol.for(marker);
50
+ var _a;
51
+ var _AISDKError = class _AISDKError2 extends Error {
52
+ /**
53
+ * Creates an AI SDK Error.
54
+ *
55
+ * @param {Object} params - The parameters for creating the error.
56
+ * @param {string} params.name - The name of the error.
57
+ * @param {string} params.message - The error message.
58
+ * @param {unknown} [params.cause] - The underlying cause of the error.
59
+ */
60
+ constructor({
61
+ name: name143,
62
+ message,
63
+ cause
64
+ }) {
65
+ super(message);
66
+ this[_a] = true;
67
+ this.name = name143;
68
+ this.cause = cause;
69
+ }
70
+ /**
71
+ * Checks if the given error is an AI SDK Error.
72
+ * @param {unknown} error - The error to check.
73
+ * @returns {boolean} True if the error is an AI SDK Error, false otherwise.
74
+ */
75
+ static isInstance(error45) {
76
+ return _AISDKError2.hasMarker(error45, marker);
77
+ }
78
+ static hasMarker(error45, marker153) {
79
+ const markerSymbol = Symbol.for(marker153);
80
+ return error45 != null && typeof error45 === "object" && markerSymbol in error45 && typeof error45[markerSymbol] === "boolean" && error45[markerSymbol] === true;
81
+ }
82
+ };
83
+ _a = symbol;
84
+ var AISDKError = _AISDKError;
85
+ var name = "AI_APICallError";
86
+ var marker2 = `vercel.ai.error.${name}`;
87
+ var symbol2 = Symbol.for(marker2);
88
+ var _a2;
89
+ _a2 = symbol2;
90
+ var name2 = "AI_EmptyResponseBodyError";
91
+ var marker3 = `vercel.ai.error.${name2}`;
92
+ var symbol3 = Symbol.for(marker3);
93
+ var _a3;
94
+ _a3 = symbol3;
95
+ function getErrorMessage(error45) {
96
+ if (error45 == null) {
97
+ return "unknown error";
98
+ }
99
+ if (typeof error45 === "string") {
100
+ return error45;
101
+ }
102
+ if (error45 instanceof Error) {
103
+ return error45.message;
104
+ }
105
+ return JSON.stringify(error45);
106
+ }
107
+ var name3 = "AI_InvalidArgumentError";
108
+ var marker4 = `vercel.ai.error.${name3}`;
109
+ var symbol4 = Symbol.for(marker4);
110
+ var _a4;
111
+ var InvalidArgumentError = class extends AISDKError {
112
+ constructor({
113
+ message,
114
+ cause,
115
+ argument
116
+ }) {
117
+ super({ name: name3, message, cause });
118
+ this[_a4] = true;
119
+ this.argument = argument;
120
+ }
121
+ static isInstance(error45) {
122
+ return AISDKError.hasMarker(error45, marker4);
123
+ }
124
+ };
125
+ _a4 = symbol4;
126
+ var name4 = "AI_InvalidPromptError";
127
+ var marker5 = `vercel.ai.error.${name4}`;
128
+ var symbol5 = Symbol.for(marker5);
129
+ var _a5;
130
+ _a5 = symbol5;
131
+ var name5 = "AI_InvalidResponseDataError";
132
+ var marker6 = `vercel.ai.error.${name5}`;
133
+ var symbol6 = Symbol.for(marker6);
134
+ var _a6;
135
+ _a6 = symbol6;
136
+ var name6 = "AI_JSONParseError";
137
+ var marker7 = `vercel.ai.error.${name6}`;
138
+ var symbol7 = Symbol.for(marker7);
139
+ var _a7;
140
+ var JSONParseError = class extends AISDKError {
141
+ constructor({ text: text2, cause }) {
142
+ super({
143
+ name: name6,
144
+ message: `JSON parsing failed: Text: ${text2}.
145
+ Error message: ${getErrorMessage(cause)}`,
146
+ cause
147
+ });
148
+ this[_a7] = true;
149
+ this.text = text2;
150
+ }
151
+ static isInstance(error45) {
152
+ return AISDKError.hasMarker(error45, marker7);
153
+ }
154
+ };
155
+ _a7 = symbol7;
156
+ var name7 = "AI_LoadAPIKeyError";
157
+ var marker8 = `vercel.ai.error.${name7}`;
158
+ var symbol8 = Symbol.for(marker8);
159
+ var _a8;
160
+ _a8 = symbol8;
161
+ var name8 = "AI_LoadSettingError";
162
+ var marker9 = `vercel.ai.error.${name8}`;
163
+ var symbol9 = Symbol.for(marker9);
164
+ var _a9;
165
+ _a9 = symbol9;
166
+ var name9 = "AI_NoContentGeneratedError";
167
+ var marker10 = `vercel.ai.error.${name9}`;
168
+ var symbol10 = Symbol.for(marker10);
169
+ var _a10;
170
+ _a10 = symbol10;
171
+ var name10 = "AI_NoSuchModelError";
172
+ var marker11 = `vercel.ai.error.${name10}`;
173
+ var symbol11 = Symbol.for(marker11);
174
+ var _a11;
175
+ _a11 = symbol11;
176
+ var name11 = "AI_TooManyEmbeddingValuesForCallError";
177
+ var marker12 = `vercel.ai.error.${name11}`;
178
+ var symbol12 = Symbol.for(marker12);
179
+ var _a12;
180
+ _a12 = symbol12;
181
+ var name12 = "AI_TypeValidationError";
182
+ var marker13 = `vercel.ai.error.${name12}`;
183
+ var symbol13 = Symbol.for(marker13);
184
+ var _a13;
185
+ var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
186
+ constructor({ value, cause }) {
187
+ super({
188
+ name: name12,
189
+ message: `Type validation failed: Value: ${JSON.stringify(value)}.
190
+ Error message: ${getErrorMessage(cause)}`,
191
+ cause
192
+ });
193
+ this[_a13] = true;
194
+ this.value = value;
195
+ }
196
+ static isInstance(error45) {
197
+ return AISDKError.hasMarker(error45, marker13);
198
+ }
199
+ /**
200
+ * Wraps an error into a TypeValidationError.
201
+ * If the cause is already a TypeValidationError with the same value, it returns the cause.
202
+ * Otherwise, it creates a new TypeValidationError.
203
+ *
204
+ * @param {Object} params - The parameters for wrapping the error.
205
+ * @param {unknown} params.value - The value that failed validation.
206
+ * @param {unknown} params.cause - The original error or cause of the validation failure.
207
+ * @returns {TypeValidationError} A TypeValidationError instance.
208
+ */
209
+ static wrap({
210
+ value,
211
+ cause
212
+ }) {
213
+ return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
214
+ }
215
+ };
216
+ _a13 = symbol13;
217
+ var TypeValidationError = _TypeValidationError;
218
+ var name13 = "AI_UnsupportedFunctionalityError";
219
+ var marker14 = `vercel.ai.error.${name13}`;
220
+ var symbol14 = Symbol.for(marker14);
221
+ var _a14;
222
+ _a14 = symbol14;
223
+
43
224
  // ../../node_modules/.pnpm/zod@4.1.9/node_modules/zod/v4/classic/external.js
44
225
  var external_exports = {};
45
226
  __export(external_exports, {
@@ -244,7 +425,7 @@ __export(external_exports, {
244
425
  stringbool: () => stringbool,
245
426
  success: () => success,
246
427
  superRefine: () => superRefine,
247
- symbol: () => symbol,
428
+ symbol: () => symbol15,
248
429
  templateLiteral: () => templateLiteral,
249
430
  toJSONSchema: () => toJSONSchema,
250
431
  toLowerCase: () => _toLowerCase,
@@ -11819,7 +12000,7 @@ var ZodSymbol = /* @__PURE__ */ $constructor("ZodSymbol", (inst, def) => {
11819
12000
  $ZodSymbol.init(inst, def);
11820
12001
  ZodType.init(inst, def);
11821
12002
  });
11822
- function symbol(params) {
12003
+ function symbol15(params) {
11823
12004
  return _symbol(ZodSymbol, params);
11824
12005
  }
11825
12006
  var ZodUndefined = /* @__PURE__ */ $constructor("ZodUndefined", (inst, def) => {
@@ -12462,198 +12643,6 @@ var classic_default = external_exports;
12462
12643
  // ../../node_modules/.pnpm/zod@4.1.9/node_modules/zod/v4/index.js
12463
12644
  var v4_default = classic_default;
12464
12645
 
12465
- // src/lib/tool.ts
12466
- var tool = (t) => t;
12467
- var prepareUserTools = (toolRegistry) => Object.keys(toolRegistry).reduce(
12468
- (acc, key) => ({
12469
- ...acc,
12470
- [key]: {
12471
- name: toolRegistry[key].name ?? key,
12472
- description: toolRegistry[key].description,
12473
- inputSchema: v4_default.toJSONSchema(toolRegistry[key].inputSchema),
12474
- outputSchema: toolRegistry[key].outputSchema ? v4_default.toJSONSchema(toolRegistry[key].outputSchema) : null
12475
- }
12476
- }),
12477
- {}
12478
- );
12479
-
12480
- // ../../node_modules/.pnpm/@ai-sdk+provider@2.0.0/node_modules/@ai-sdk/provider/dist/index.mjs
12481
- var marker = "vercel.ai.error";
12482
- var symbol2 = Symbol.for(marker);
12483
- var _a;
12484
- var _AISDKError = class _AISDKError2 extends Error {
12485
- /**
12486
- * Creates an AI SDK Error.
12487
- *
12488
- * @param {Object} params - The parameters for creating the error.
12489
- * @param {string} params.name - The name of the error.
12490
- * @param {string} params.message - The error message.
12491
- * @param {unknown} [params.cause] - The underlying cause of the error.
12492
- */
12493
- constructor({
12494
- name: name143,
12495
- message,
12496
- cause
12497
- }) {
12498
- super(message);
12499
- this[_a] = true;
12500
- this.name = name143;
12501
- this.cause = cause;
12502
- }
12503
- /**
12504
- * Checks if the given error is an AI SDK Error.
12505
- * @param {unknown} error - The error to check.
12506
- * @returns {boolean} True if the error is an AI SDK Error, false otherwise.
12507
- */
12508
- static isInstance(error45) {
12509
- return _AISDKError2.hasMarker(error45, marker);
12510
- }
12511
- static hasMarker(error45, marker153) {
12512
- const markerSymbol = Symbol.for(marker153);
12513
- return error45 != null && typeof error45 === "object" && markerSymbol in error45 && typeof error45[markerSymbol] === "boolean" && error45[markerSymbol] === true;
12514
- }
12515
- };
12516
- _a = symbol2;
12517
- var AISDKError = _AISDKError;
12518
- var name = "AI_APICallError";
12519
- var marker2 = `vercel.ai.error.${name}`;
12520
- var symbol22 = Symbol.for(marker2);
12521
- var _a2;
12522
- _a2 = symbol22;
12523
- var name2 = "AI_EmptyResponseBodyError";
12524
- var marker3 = `vercel.ai.error.${name2}`;
12525
- var symbol3 = Symbol.for(marker3);
12526
- var _a3;
12527
- _a3 = symbol3;
12528
- function getErrorMessage(error45) {
12529
- if (error45 == null) {
12530
- return "unknown error";
12531
- }
12532
- if (typeof error45 === "string") {
12533
- return error45;
12534
- }
12535
- if (error45 instanceof Error) {
12536
- return error45.message;
12537
- }
12538
- return JSON.stringify(error45);
12539
- }
12540
- var name3 = "AI_InvalidArgumentError";
12541
- var marker4 = `vercel.ai.error.${name3}`;
12542
- var symbol4 = Symbol.for(marker4);
12543
- var _a4;
12544
- var InvalidArgumentError = class extends AISDKError {
12545
- constructor({
12546
- message,
12547
- cause,
12548
- argument
12549
- }) {
12550
- super({ name: name3, message, cause });
12551
- this[_a4] = true;
12552
- this.argument = argument;
12553
- }
12554
- static isInstance(error45) {
12555
- return AISDKError.hasMarker(error45, marker4);
12556
- }
12557
- };
12558
- _a4 = symbol4;
12559
- var name4 = "AI_InvalidPromptError";
12560
- var marker5 = `vercel.ai.error.${name4}`;
12561
- var symbol5 = Symbol.for(marker5);
12562
- var _a5;
12563
- _a5 = symbol5;
12564
- var name5 = "AI_InvalidResponseDataError";
12565
- var marker6 = `vercel.ai.error.${name5}`;
12566
- var symbol6 = Symbol.for(marker6);
12567
- var _a6;
12568
- _a6 = symbol6;
12569
- var name6 = "AI_JSONParseError";
12570
- var marker7 = `vercel.ai.error.${name6}`;
12571
- var symbol7 = Symbol.for(marker7);
12572
- var _a7;
12573
- var JSONParseError = class extends AISDKError {
12574
- constructor({ text: text2, cause }) {
12575
- super({
12576
- name: name6,
12577
- message: `JSON parsing failed: Text: ${text2}.
12578
- Error message: ${getErrorMessage(cause)}`,
12579
- cause
12580
- });
12581
- this[_a7] = true;
12582
- this.text = text2;
12583
- }
12584
- static isInstance(error45) {
12585
- return AISDKError.hasMarker(error45, marker7);
12586
- }
12587
- };
12588
- _a7 = symbol7;
12589
- var name7 = "AI_LoadAPIKeyError";
12590
- var marker8 = `vercel.ai.error.${name7}`;
12591
- var symbol8 = Symbol.for(marker8);
12592
- var _a8;
12593
- _a8 = symbol8;
12594
- var name8 = "AI_LoadSettingError";
12595
- var marker9 = `vercel.ai.error.${name8}`;
12596
- var symbol9 = Symbol.for(marker9);
12597
- var _a9;
12598
- _a9 = symbol9;
12599
- var name9 = "AI_NoContentGeneratedError";
12600
- var marker10 = `vercel.ai.error.${name9}`;
12601
- var symbol10 = Symbol.for(marker10);
12602
- var _a10;
12603
- _a10 = symbol10;
12604
- var name10 = "AI_NoSuchModelError";
12605
- var marker11 = `vercel.ai.error.${name10}`;
12606
- var symbol11 = Symbol.for(marker11);
12607
- var _a11;
12608
- _a11 = symbol11;
12609
- var name11 = "AI_TooManyEmbeddingValuesForCallError";
12610
- var marker12 = `vercel.ai.error.${name11}`;
12611
- var symbol12 = Symbol.for(marker12);
12612
- var _a12;
12613
- _a12 = symbol12;
12614
- var name12 = "AI_TypeValidationError";
12615
- var marker13 = `vercel.ai.error.${name12}`;
12616
- var symbol13 = Symbol.for(marker13);
12617
- var _a13;
12618
- var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
12619
- constructor({ value, cause }) {
12620
- super({
12621
- name: name12,
12622
- message: `Type validation failed: Value: ${JSON.stringify(value)}.
12623
- Error message: ${getErrorMessage(cause)}`,
12624
- cause
12625
- });
12626
- this[_a13] = true;
12627
- this.value = value;
12628
- }
12629
- static isInstance(error45) {
12630
- return AISDKError.hasMarker(error45, marker13);
12631
- }
12632
- /**
12633
- * Wraps an error into a TypeValidationError.
12634
- * If the cause is already a TypeValidationError with the same value, it returns the cause.
12635
- * Otherwise, it creates a new TypeValidationError.
12636
- *
12637
- * @param {Object} params - The parameters for wrapping the error.
12638
- * @param {unknown} params.value - The value that failed validation.
12639
- * @param {unknown} params.cause - The original error or cause of the validation failure.
12640
- * @returns {TypeValidationError} A TypeValidationError instance.
12641
- */
12642
- static wrap({
12643
- value,
12644
- cause
12645
- }) {
12646
- return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
12647
- }
12648
- };
12649
- _a13 = symbol13;
12650
- var TypeValidationError = _TypeValidationError;
12651
- var name13 = "AI_UnsupportedFunctionalityError";
12652
- var marker14 = `vercel.ai.error.${name13}`;
12653
- var symbol14 = Symbol.for(marker14);
12654
- var _a14;
12655
- _a14 = symbol14;
12656
-
12657
12646
  // ../../node_modules/.pnpm/zod@4.1.9/node_modules/zod/v3/helpers/util.js
12658
12647
  var util;
12659
12648
  (function(util2) {
@@ -17866,14 +17855,14 @@ var __export2 = (target, all) => {
17866
17855
  };
17867
17856
  var name14 = "AI_NoOutputSpecifiedError";
17868
17857
  var marker15 = `vercel.ai.error.${name14}`;
17869
- var symbol15 = Symbol.for(marker15);
17858
+ var symbol16 = Symbol.for(marker15);
17870
17859
  var _a15;
17871
- _a15 = symbol15;
17860
+ _a15 = symbol16;
17872
17861
  var name22 = "AI_InvalidArgumentError";
17873
17862
  var marker22 = `vercel.ai.error.${name22}`;
17874
- var symbol23 = Symbol.for(marker22);
17863
+ var symbol22 = Symbol.for(marker22);
17875
17864
  var _a22;
17876
- _a22 = symbol23;
17865
+ _a22 = symbol22;
17877
17866
  var name32 = "AI_InvalidStreamPartError";
17878
17867
  var marker32 = `vercel.ai.error.${name32}`;
17879
17868
  var symbol32 = Symbol.for(marker32);
@@ -19270,9 +19259,9 @@ var object2 = ({
19270
19259
  };
19271
19260
  var name16 = "AI_NoSuchProviderError";
19272
19261
  var marker16 = `vercel.ai.error.${name16}`;
19273
- var symbol16 = Symbol.for(marker16);
19262
+ var symbol162 = Symbol.for(marker16);
19274
19263
  var _a16;
19275
- _a16 = symbol16;
19264
+ _a16 = symbol162;
19276
19265
  var ClientOrServerImplementationSchema = external_exports.looseObject({
19277
19266
  name: external_exports.string(),
19278
19267
  version: external_exports.string()
@@ -19625,8 +19614,64 @@ function createUIMessageStream({
19625
19614
  });
19626
19615
  }
19627
19616
 
19628
- // src/lib/chat.ts
19629
- async function* iterSSE(body) {
19617
+ // src/lib/prepare-user-tools.ts
19618
+ var prepareUserTools = (toolRegistry) => Object.keys(toolRegistry).reduce(
19619
+ (acc, key) => ({
19620
+ ...acc,
19621
+ [key]: {
19622
+ name: toolRegistry[key].name ?? key,
19623
+ description: toolRegistry[key].description,
19624
+ inputSchema: v4_default.toJSONSchema(toolRegistry[key].inputSchema),
19625
+ outputSchema: toolRegistry[key].outputSchema ? v4_default.toJSONSchema(toolRegistry[key].outputSchema) : null
19626
+ }
19627
+ }),
19628
+ {}
19629
+ );
19630
+
19631
+ // src/lib/get-api-key.ts
19632
+ var getApiKey = () => process.env.PUCK_API_KEY;
19633
+
19634
+ // src/lib/process-user-tool.ts
19635
+ var processUserTool = async ({
19636
+ data,
19637
+ tools,
19638
+ options
19639
+ }) => {
19640
+ const { toolCallId, tools: requestTools } = data;
19641
+ const { apiKey = getApiKey(), host = "https://cloud.puckeditor.com/api" } = options;
19642
+ if (!apiKey) {
19643
+ throw new Error(
19644
+ "No Puck API key specified. Set the PUCK_API_KEY environment variable, or provide one"
19645
+ );
19646
+ }
19647
+ const responses = [];
19648
+ for (let i = 0; i < requestTools.length; i++) {
19649
+ const { name: name17, input } = requestTools[i];
19650
+ const tool2 = tools?.[name17];
19651
+ if (!tool2) {
19652
+ throw new Error(`An unknown tool was called: ${name17}`);
19653
+ }
19654
+ if (!input) {
19655
+ throw new Error(`Input was not present in tool call: ${toolCallId}`);
19656
+ }
19657
+ const output = await tool2.execute(input);
19658
+ responses.push({ name: name17, input, output });
19659
+ }
19660
+ const res = await fetch(`${host}/tool`, {
19661
+ headers: { "x-api-key": apiKey },
19662
+ method: "post",
19663
+ body: JSON.stringify({
19664
+ id: toolCallId,
19665
+ responses
19666
+ })
19667
+ });
19668
+ if (res.status !== 200) {
19669
+ throw new Error("Tool result could not be sent to server");
19670
+ }
19671
+ };
19672
+
19673
+ // src/lib/iterate-sse.ts
19674
+ async function* iterateSSE(body) {
19630
19675
  const reader = body.getReader();
19631
19676
  const dec = new TextDecoder();
19632
19677
  let buf = "";
@@ -19647,81 +19692,84 @@ async function* iterSSE(body) {
19647
19692
  }
19648
19693
  }
19649
19694
  }
19650
- var getApiKey = () => process.env.PUCK_API_KEY;
19651
- function chat({ chatId, messages: initialMessages, config: config2, pageData }, {
19652
- ai = {},
19653
- apiKey = getApiKey(),
19654
- host = "https://cloud.puckeditor.com/api"
19655
- }) {
19695
+
19696
+ // src/lib/cloud-api.ts
19697
+ var cloudApi = async (path, body, options = {}, onChunk) => {
19698
+ const {
19699
+ ai = {},
19700
+ apiKey = getApiKey(),
19701
+ host = "https://cloud.puckeditor.com/api"
19702
+ } = options;
19656
19703
  if (!apiKey) {
19657
19704
  throw new Error(
19658
- `No Puck API key specified. Set the PUCK_API_KEY environment variable, or provide one to the function.
19659
-
19660
- Visit https://cloud.puckeditor.com/sign-up to register for an account and generate a key.`
19705
+ "No Puck API key specified. Set the PUCK_API_KEY environment variable, or provide one to the function"
19661
19706
  );
19662
19707
  }
19663
19708
  const { context, tools = {} } = ai;
19664
- let messages = initialMessages;
19709
+ const res = await fetch(`${host}/${path}`, {
19710
+ headers: { "x-api-key": apiKey },
19711
+ method: "post",
19712
+ body: JSON.stringify({
19713
+ ...body,
19714
+ context,
19715
+ tools: prepareUserTools(tools)
19716
+ })
19717
+ });
19718
+ if (!res.body) {
19719
+ throw new Error(`Puck ${res.status} (${res.statusText})`);
19720
+ } else if (!res.ok) {
19721
+ const body2 = await res.json();
19722
+ throw new Error(
19723
+ `Puck ${res.status} (${res.statusText}): ${body2.error ?? "Unknown reason"}`
19724
+ );
19725
+ }
19726
+ for await (const chunk of iterateSSE(res.body)) {
19727
+ if (chunk.type === "data-user-tool") {
19728
+ await processUserTool({ data: chunk.data, tools, options });
19729
+ } else {
19730
+ onChunk(chunk);
19731
+ }
19732
+ }
19733
+ return res;
19734
+ };
19735
+
19736
+ // src/api/chat.ts
19737
+ function chat({ chatId, messages, config: config2, pageData }, options = {}) {
19665
19738
  const stream = createUIMessageStream({
19666
19739
  execute: async ({ writer }) => {
19667
- const res = await fetch(`${host}/chat`, {
19668
- headers: { "x-api-key": apiKey },
19669
- method: "post",
19670
- body: JSON.stringify({
19671
- chatId,
19672
- context,
19673
- messages,
19674
- config: config2,
19675
- pageData,
19676
- tools: prepareUserTools(tools)
19677
- })
19678
- });
19679
- if (!res.body) {
19680
- throw new Error(`Puck ${res.status} (${res.statusText})`);
19681
- } else if (!res.ok) {
19682
- const body = await res.json();
19683
- throw new Error(
19684
- `Puck ${res.status} (${res.statusText}): ${body.error ?? "Unknown reason"}`
19685
- );
19686
- }
19687
- for await (const chunk of iterSSE(res.body)) {
19688
- if (chunk.type === "data-user-tool") {
19689
- const { toolCallId, tools: requestTools } = chunk.data;
19690
- const responses = [];
19691
- for (let i = 0; i < requestTools.length; i++) {
19692
- const { name: name17, input } = requestTools[i];
19693
- const tool2 = tools?.[name17];
19694
- if (!tool2) {
19695
- throw new Error(`An unknown tool was called: ${name17}`);
19696
- }
19697
- if (!input) {
19698
- throw new Error(
19699
- `Input was not present in tool call: ${toolCallId}`
19700
- );
19701
- }
19702
- const output = await tool2.execute(input);
19703
- responses.push({ name: name17, input, output });
19704
- }
19705
- const res2 = await fetch(`${host}/chat/tool`, {
19706
- headers: { "x-api-key": apiKey },
19707
- method: "post",
19708
- body: JSON.stringify({
19709
- id: toolCallId,
19710
- responses
19711
- })
19712
- });
19713
- if (res2.status !== 200) {
19714
- throw new Error("Tool result could not be sent to server");
19715
- }
19716
- } else {
19740
+ await cloudApi(
19741
+ "chat",
19742
+ { chatId, config: config2, messages, pageData },
19743
+ options,
19744
+ (chunk) => {
19717
19745
  writer.write(chunk);
19718
19746
  }
19719
- }
19747
+ );
19720
19748
  }
19721
19749
  });
19722
19750
  return createUIMessageStreamResponse({ stream });
19723
19751
  }
19724
19752
 
19753
+ // src/api/generate.ts
19754
+ async function generate({
19755
+ prompt,
19756
+ config: config2,
19757
+ pageData,
19758
+ context,
19759
+ tools,
19760
+ apiKey,
19761
+ host
19762
+ }) {
19763
+ let result = null;
19764
+ const options = { ai: { context, tools }, apiKey, host };
19765
+ await cloudApi("generate", { prompt, config: config2, pageData }, options, (chunk) => {
19766
+ if (chunk.type === "data-page") {
19767
+ result = chunk.data;
19768
+ }
19769
+ });
19770
+ return result;
19771
+ }
19772
+
19725
19773
  // src/routes.ts
19726
19774
  var routeRegistry = {
19727
19775
  "/api/puck/chat": {
@@ -19732,7 +19780,7 @@ var findRoute = (pathname, method) => {
19732
19780
  return routeRegistry[pathname][method];
19733
19781
  };
19734
19782
 
19735
- // src/lib/puck-handler.ts
19783
+ // src/api/puck-handler.ts
19736
19784
  async function puckHandler(request, options) {
19737
19785
  const cloned = request.clone();
19738
19786
  const url2 = new URL(cloned.url);
@@ -19759,6 +19807,7 @@ var endpoints = ["chat"];
19759
19807
  0 && (module.exports = {
19760
19808
  chat,
19761
19809
  endpoints,
19810
+ generate,
19762
19811
  puckHandler,
19763
19812
  tool
19764
19813
  });