@puckeditor/cloud-client 0.0.1 → 0.0.2-canary.53ffa8a3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puckeditor/cloud-client",
3
- "version": "0.0.1",
3
+ "version": "0.0.2-canary.53ffa8a3",
4
4
  "author": "Chris Villa <chris@puckeditor.com>",
5
5
  "repository": "puckeditor/puck",
6
6
  "bugs": "https://github.com/puckeditor/puck/issues",
package/dist/index.d.mts DELETED
@@ -1,38 +0,0 @@
1
- import { UIMessage } from 'ai';
2
- import z from 'zod/v4';
3
- import { Config, Data } from '@measured/puck';
4
-
5
- /**
6
- * Generator function that streams the response body from a fetch request.
7
- */
8
- declare function streamingFetch(input: RequestInfo | URL, init?: RequestInit): AsyncGenerator<string, void, unknown>;
9
- type UserTool<INPUT extends z.ZodTypeAny = z.ZodTypeAny, OUTPUT extends z.ZodTypeAny = z.ZodTypeAny> = {
10
- name?: string;
11
- description: string;
12
- inputSchema: INPUT;
13
- outputSchema?: OUTPUT;
14
- execute: (input: z.infer<INPUT>) => Promise<z.infer<OUTPUT>> | z.infer<OUTPUT>;
15
- };
16
- declare const tool: <INPUT extends z.ZodTypeAny, OUTPUT extends z.ZodTypeAny>(t: UserTool<INPUT, OUTPUT>) => UserTool<INPUT, OUTPUT>;
17
- type UserToolRegistry = Record<string, UserTool>;
18
- type ApiParams = {
19
- chat: {
20
- instructions?: string;
21
- chatId?: string;
22
- messages: UIMessage[];
23
- config: Config;
24
- pageData: Data;
25
- tools?: UserToolRegistry;
26
- };
27
- };
28
- declare const createPuckApi: ({ apiKey, host, }?: {
29
- host?: string;
30
- apiKey?: string;
31
- }) => {
32
- auto: (path: string[], request: Request) => Promise<Response>;
33
- ai: {
34
- chat: ({ chatId, instructions, messages: initialMessages, config, pageData, tools, }: ApiParams["chat"]) => Promise<Response>;
35
- };
36
- };
37
-
38
- export { type ApiParams, createPuckApi, streamingFetch, tool };
package/dist/index.d.ts DELETED
@@ -1,38 +0,0 @@
1
- import { UIMessage } from 'ai';
2
- import z from 'zod/v4';
3
- import { Config, Data } from '@measured/puck';
4
-
5
- /**
6
- * Generator function that streams the response body from a fetch request.
7
- */
8
- declare function streamingFetch(input: RequestInfo | URL, init?: RequestInit): AsyncGenerator<string, void, unknown>;
9
- type UserTool<INPUT extends z.ZodTypeAny = z.ZodTypeAny, OUTPUT extends z.ZodTypeAny = z.ZodTypeAny> = {
10
- name?: string;
11
- description: string;
12
- inputSchema: INPUT;
13
- outputSchema?: OUTPUT;
14
- execute: (input: z.infer<INPUT>) => Promise<z.infer<OUTPUT>> | z.infer<OUTPUT>;
15
- };
16
- declare const tool: <INPUT extends z.ZodTypeAny, OUTPUT extends z.ZodTypeAny>(t: UserTool<INPUT, OUTPUT>) => UserTool<INPUT, OUTPUT>;
17
- type UserToolRegistry = Record<string, UserTool>;
18
- type ApiParams = {
19
- chat: {
20
- instructions?: string;
21
- chatId?: string;
22
- messages: UIMessage[];
23
- config: Config;
24
- pageData: Data;
25
- tools?: UserToolRegistry;
26
- };
27
- };
28
- declare const createPuckApi: ({ apiKey, host, }?: {
29
- host?: string;
30
- apiKey?: string;
31
- }) => {
32
- auto: (path: string[], request: Request) => Promise<Response>;
33
- ai: {
34
- chat: ({ chatId, instructions, messages: initialMessages, config, pageData, tools, }: ApiParams["chat"]) => Promise<Response>;
35
- };
36
- };
37
-
38
- export { type ApiParams, createPuckApi, streamingFetch, tool };
package/dist/index.js DELETED
@@ -1,270 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __defProps = Object.defineProperties;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __getProtoOf = Object.getPrototypeOf;
10
- var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
- var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
13
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
- var __spreadValues = (a, b) => {
15
- for (var prop in b || (b = {}))
16
- if (__hasOwnProp.call(b, prop))
17
- __defNormalProp(a, prop, b[prop]);
18
- if (__getOwnPropSymbols)
19
- for (var prop of __getOwnPropSymbols(b)) {
20
- if (__propIsEnum.call(b, prop))
21
- __defNormalProp(a, prop, b[prop]);
22
- }
23
- return a;
24
- };
25
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
26
- var __export = (target, all) => {
27
- for (var name in all)
28
- __defProp(target, name, { get: all[name], enumerable: true });
29
- };
30
- var __copyProps = (to, from, except, desc) => {
31
- if (from && typeof from === "object" || typeof from === "function") {
32
- for (let key of __getOwnPropNames(from))
33
- if (!__hasOwnProp.call(to, key) && key !== except)
34
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
35
- }
36
- return to;
37
- };
38
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
39
- // If the importer is in node compatibility mode or this is not an ESM
40
- // file that has been converted to a CommonJS file using a Babel-
41
- // compatible transform (i.e. "__esModule" has not been set), then set
42
- // "default" to the CommonJS "module.exports" for node compatibility.
43
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
44
- mod
45
- ));
46
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
47
- var __async = (__this, __arguments, generator) => {
48
- return new Promise((resolve, reject) => {
49
- var fulfilled = (value) => {
50
- try {
51
- step(generator.next(value));
52
- } catch (e) {
53
- reject(e);
54
- }
55
- };
56
- var rejected = (value) => {
57
- try {
58
- step(generator.throw(value));
59
- } catch (e) {
60
- reject(e);
61
- }
62
- };
63
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
64
- step((generator = generator.apply(__this, __arguments)).next());
65
- });
66
- };
67
- var __await = function(promise, isYieldStar) {
68
- this[0] = promise;
69
- this[1] = isYieldStar;
70
- };
71
- var __asyncGenerator = (__this, __arguments, generator) => {
72
- var resume = (k, v, yes, no) => {
73
- try {
74
- var x = generator[k](v), isAwait = (v = x.value) instanceof __await, done = x.done;
75
- Promise.resolve(isAwait ? v[0] : v).then((y) => isAwait ? resume(k === "return" ? k : "next", v[1] ? { done: y.done, value: y.value } : y, yes, no) : yes({ value: y, done })).catch((e) => resume("throw", e, yes, no));
76
- } catch (e) {
77
- no(e);
78
- }
79
- }, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
80
- return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
81
- };
82
- var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
83
-
84
- // index.ts
85
- var index_exports = {};
86
- __export(index_exports, {
87
- createPuckApi: () => createPuckApi,
88
- streamingFetch: () => streamingFetch,
89
- tool: () => tool
90
- });
91
- module.exports = __toCommonJS(index_exports);
92
-
93
- // ../tsup-config/react-import.js
94
- var import_react = __toESM(require("react"));
95
-
96
- // src/index.ts
97
- var import_ai = require("ai");
98
- var import_v4 = __toESM(require("zod/v4"));
99
- function streamingFetch(input, init) {
100
- return __asyncGenerator(this, null, function* () {
101
- var _a;
102
- const response = yield new __await(fetch(input, init));
103
- const reader = (_a = response == null ? void 0 : response.body) == null ? void 0 : _a.getReader();
104
- const decoder = new TextDecoder("utf-8");
105
- if (reader) {
106
- while (true) {
107
- const { done, value } = yield new __await(reader.read());
108
- if (done) break;
109
- try {
110
- const lines = decoder.decode(value).split("\n");
111
- for (const line of lines) {
112
- if (line.trim()) {
113
- if (line) {
114
- yield line;
115
- }
116
- }
117
- }
118
- } catch (e) {
119
- console.warn(e.message);
120
- }
121
- }
122
- }
123
- });
124
- }
125
- function iterSSE(body) {
126
- return __asyncGenerator(this, null, function* () {
127
- const reader = body.getReader();
128
- const dec = new TextDecoder();
129
- let buf = "";
130
- while (true) {
131
- const { done, value } = yield new __await(reader.read());
132
- if (done) break;
133
- buf += dec.decode(value, { stream: true });
134
- let idx;
135
- while ((idx = buf.indexOf("\n\n")) >= 0) {
136
- const chunk = buf.slice(0, idx).trim();
137
- buf = buf.slice(idx + 2);
138
- if (!chunk) continue;
139
- const line = chunk.startsWith("data: ") ? chunk.slice(6) : chunk;
140
- try {
141
- yield JSON.parse(line);
142
- } catch (e) {
143
- }
144
- }
145
- }
146
- });
147
- }
148
- var getApiKey = () => process.env.PUCK_API_KEY;
149
- var tool = (t) => t;
150
- var prepareUserTools = (toolRegistry) => Object.keys(toolRegistry).reduce(
151
- (acc, key) => {
152
- var _a;
153
- return __spreadProps(__spreadValues({}, acc), {
154
- [key]: {
155
- name: (_a = toolRegistry[key].name) != null ? _a : key,
156
- description: toolRegistry[key].description,
157
- inputSchema: import_v4.default.toJSONSchema(toolRegistry[key].inputSchema),
158
- outputSchema: toolRegistry[key].outputSchema ? import_v4.default.toJSONSchema(toolRegistry[key].outputSchema) : null
159
- }
160
- });
161
- },
162
- {}
163
- );
164
- var createPuckApi = ({
165
- apiKey = getApiKey(),
166
- host = "https://app.puckbuild.com/api"
167
- } = {}) => {
168
- if (!apiKey) {
169
- throw new Error(
170
- "No Puck API key specified. Set the PUCK_API_KEY environment variable, or provide one to createPuckApi()"
171
- );
172
- }
173
- const ai = {
174
- chat: function(_0) {
175
- return __async(this, arguments, function* ({
176
- chatId,
177
- instructions,
178
- messages: initialMessages,
179
- config,
180
- pageData,
181
- tools = {}
182
- }) {
183
- let messages = initialMessages;
184
- const stream = (0, import_ai.createUIMessageStream)({
185
- execute: (_02) => __async(null, [_02], function* ({ writer }) {
186
- const res = yield fetch(`${host}/chat`, {
187
- headers: { "x-api-key": apiKey },
188
- method: "post",
189
- body: JSON.stringify({
190
- chatId,
191
- instructions,
192
- messages,
193
- config,
194
- pageData,
195
- tools: prepareUserTools(tools)
196
- })
197
- });
198
- if (!res.ok || !res.body) {
199
- throw new Error(`Puck ${res.status}`);
200
- }
201
- try {
202
- for (var iter = __forAwait(iterSSE(res.body)), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
203
- const chunk = temp.value;
204
- if (chunk.type === "data-user-tool") {
205
- const { toolCallId, tools: requestTools } = chunk.data;
206
- const responses = [];
207
- for (let i = 0; i < requestTools.length; i++) {
208
- const { name, input } = requestTools[i];
209
- const tool2 = tools == null ? void 0 : tools[name];
210
- if (!tool2) {
211
- throw new Error(`An unknown tool was called: ${name}`);
212
- }
213
- if (!input) {
214
- throw new Error(
215
- `Input was not present in tool call: ${toolCallId}`
216
- );
217
- }
218
- const output = yield tool2.execute(input);
219
- responses.push({ name, input, output });
220
- }
221
- const res2 = yield fetch(`${host}/chat/tool`, {
222
- headers: { "x-api-key": apiKey },
223
- method: "post",
224
- body: JSON.stringify({
225
- id: toolCallId,
226
- responses
227
- })
228
- });
229
- if (res2.status !== 200) {
230
- throw new Error("Tool result could not be sent to server");
231
- }
232
- } else {
233
- writer.write(chunk);
234
- }
235
- }
236
- } catch (temp) {
237
- error = [temp];
238
- } finally {
239
- try {
240
- more && (temp = iter.return) && (yield temp.call(iter));
241
- } finally {
242
- if (error)
243
- throw error[0];
244
- }
245
- }
246
- })
247
- });
248
- return (0, import_ai.createUIMessageStreamResponse)({ stream });
249
- });
250
- }
251
- };
252
- const auto = (path, request) => __async(null, null, function* () {
253
- const pathName = path[0];
254
- const body = yield request.json();
255
- if (pathName === "chat") {
256
- return ai.chat(body);
257
- }
258
- throw new Error(`Invalid path "${pathName}" provided to auto()`);
259
- });
260
- return {
261
- auto,
262
- ai
263
- };
264
- };
265
- // Annotate the CommonJS export names for ESM import in node:
266
- 0 && (module.exports = {
267
- createPuckApi,
268
- streamingFetch,
269
- tool
270
- });
package/dist/index.mjs DELETED
@@ -1,237 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __spreadValues = (a, b) => {
10
- for (var prop in b || (b = {}))
11
- if (__hasOwnProp.call(b, prop))
12
- __defNormalProp(a, prop, b[prop]);
13
- if (__getOwnPropSymbols)
14
- for (var prop of __getOwnPropSymbols(b)) {
15
- if (__propIsEnum.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- }
18
- return a;
19
- };
20
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
- var __async = (__this, __arguments, generator) => {
22
- return new Promise((resolve, reject) => {
23
- var fulfilled = (value) => {
24
- try {
25
- step(generator.next(value));
26
- } catch (e) {
27
- reject(e);
28
- }
29
- };
30
- var rejected = (value) => {
31
- try {
32
- step(generator.throw(value));
33
- } catch (e) {
34
- reject(e);
35
- }
36
- };
37
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
38
- step((generator = generator.apply(__this, __arguments)).next());
39
- });
40
- };
41
- var __await = function(promise, isYieldStar) {
42
- this[0] = promise;
43
- this[1] = isYieldStar;
44
- };
45
- var __asyncGenerator = (__this, __arguments, generator) => {
46
- var resume = (k, v, yes, no) => {
47
- try {
48
- var x = generator[k](v), isAwait = (v = x.value) instanceof __await, done = x.done;
49
- Promise.resolve(isAwait ? v[0] : v).then((y) => isAwait ? resume(k === "return" ? k : "next", v[1] ? { done: y.done, value: y.value } : y, yes, no) : yes({ value: y, done })).catch((e) => resume("throw", e, yes, no));
50
- } catch (e) {
51
- no(e);
52
- }
53
- }, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
54
- return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
55
- };
56
- var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
57
-
58
- // ../tsup-config/react-import.js
59
- import React from "react";
60
-
61
- // src/index.ts
62
- import {
63
- createUIMessageStream,
64
- createUIMessageStreamResponse
65
- } from "ai";
66
- import z from "zod/v4";
67
- function streamingFetch(input, init) {
68
- return __asyncGenerator(this, null, function* () {
69
- var _a;
70
- const response = yield new __await(fetch(input, init));
71
- const reader = (_a = response == null ? void 0 : response.body) == null ? void 0 : _a.getReader();
72
- const decoder = new TextDecoder("utf-8");
73
- if (reader) {
74
- while (true) {
75
- const { done, value } = yield new __await(reader.read());
76
- if (done) break;
77
- try {
78
- const lines = decoder.decode(value).split("\n");
79
- for (const line of lines) {
80
- if (line.trim()) {
81
- if (line) {
82
- yield line;
83
- }
84
- }
85
- }
86
- } catch (e) {
87
- console.warn(e.message);
88
- }
89
- }
90
- }
91
- });
92
- }
93
- function iterSSE(body) {
94
- return __asyncGenerator(this, null, function* () {
95
- const reader = body.getReader();
96
- const dec = new TextDecoder();
97
- let buf = "";
98
- while (true) {
99
- const { done, value } = yield new __await(reader.read());
100
- if (done) break;
101
- buf += dec.decode(value, { stream: true });
102
- let idx;
103
- while ((idx = buf.indexOf("\n\n")) >= 0) {
104
- const chunk = buf.slice(0, idx).trim();
105
- buf = buf.slice(idx + 2);
106
- if (!chunk) continue;
107
- const line = chunk.startsWith("data: ") ? chunk.slice(6) : chunk;
108
- try {
109
- yield JSON.parse(line);
110
- } catch (e) {
111
- }
112
- }
113
- }
114
- });
115
- }
116
- var getApiKey = () => process.env.PUCK_API_KEY;
117
- var tool = (t) => t;
118
- var prepareUserTools = (toolRegistry) => Object.keys(toolRegistry).reduce(
119
- (acc, key) => {
120
- var _a;
121
- return __spreadProps(__spreadValues({}, acc), {
122
- [key]: {
123
- name: (_a = toolRegistry[key].name) != null ? _a : key,
124
- description: toolRegistry[key].description,
125
- inputSchema: z.toJSONSchema(toolRegistry[key].inputSchema),
126
- outputSchema: toolRegistry[key].outputSchema ? z.toJSONSchema(toolRegistry[key].outputSchema) : null
127
- }
128
- });
129
- },
130
- {}
131
- );
132
- var createPuckApi = ({
133
- apiKey = getApiKey(),
134
- host = "https://app.puckbuild.com/api"
135
- } = {}) => {
136
- if (!apiKey) {
137
- throw new Error(
138
- "No Puck API key specified. Set the PUCK_API_KEY environment variable, or provide one to createPuckApi()"
139
- );
140
- }
141
- const ai = {
142
- chat: function(_0) {
143
- return __async(this, arguments, function* ({
144
- chatId,
145
- instructions,
146
- messages: initialMessages,
147
- config,
148
- pageData,
149
- tools = {}
150
- }) {
151
- let messages = initialMessages;
152
- const stream = createUIMessageStream({
153
- execute: (_02) => __async(null, [_02], function* ({ writer }) {
154
- const res = yield fetch(`${host}/chat`, {
155
- headers: { "x-api-key": apiKey },
156
- method: "post",
157
- body: JSON.stringify({
158
- chatId,
159
- instructions,
160
- messages,
161
- config,
162
- pageData,
163
- tools: prepareUserTools(tools)
164
- })
165
- });
166
- if (!res.ok || !res.body) {
167
- throw new Error(`Puck ${res.status}`);
168
- }
169
- try {
170
- for (var iter = __forAwait(iterSSE(res.body)), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
171
- const chunk = temp.value;
172
- if (chunk.type === "data-user-tool") {
173
- const { toolCallId, tools: requestTools } = chunk.data;
174
- const responses = [];
175
- for (let i = 0; i < requestTools.length; i++) {
176
- const { name, input } = requestTools[i];
177
- const tool2 = tools == null ? void 0 : tools[name];
178
- if (!tool2) {
179
- throw new Error(`An unknown tool was called: ${name}`);
180
- }
181
- if (!input) {
182
- throw new Error(
183
- `Input was not present in tool call: ${toolCallId}`
184
- );
185
- }
186
- const output = yield tool2.execute(input);
187
- responses.push({ name, input, output });
188
- }
189
- const res2 = yield fetch(`${host}/chat/tool`, {
190
- headers: { "x-api-key": apiKey },
191
- method: "post",
192
- body: JSON.stringify({
193
- id: toolCallId,
194
- responses
195
- })
196
- });
197
- if (res2.status !== 200) {
198
- throw new Error("Tool result could not be sent to server");
199
- }
200
- } else {
201
- writer.write(chunk);
202
- }
203
- }
204
- } catch (temp) {
205
- error = [temp];
206
- } finally {
207
- try {
208
- more && (temp = iter.return) && (yield temp.call(iter));
209
- } finally {
210
- if (error)
211
- throw error[0];
212
- }
213
- }
214
- })
215
- });
216
- return createUIMessageStreamResponse({ stream });
217
- });
218
- }
219
- };
220
- const auto = (path, request) => __async(null, null, function* () {
221
- const pathName = path[0];
222
- const body = yield request.json();
223
- if (pathName === "chat") {
224
- return ai.chat(body);
225
- }
226
- throw new Error(`Invalid path "${pathName}" provided to auto()`);
227
- });
228
- return {
229
- auto,
230
- ai
231
- };
232
- };
233
- export {
234
- createPuckApi,
235
- streamingFetch,
236
- tool
237
- };