@kloddy/kloddy-js 0.1.0 → 0.1.1
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/README.md +71 -102
- package/dist/chunk-WSN7MLKR.mjs +166 -0
- package/dist/chunk-WWAXYTZO.mjs +265 -0
- package/dist/index-BaQw1DKg.d.ts +178 -0
- package/dist/index-CqX8s53x.d.mts +178 -0
- package/dist/index.d.mts +48 -180
- package/dist/index.d.ts +48 -180
- package/dist/index.js +215 -33
- package/dist/index.mjs +37 -257
- package/dist/libs/react/index.d.mts +3 -0
- package/dist/libs/react/index.d.ts +3 -0
- package/dist/libs/react/index.js +458 -0
- package/dist/libs/react/index.mjs +14 -0
- package/dist/next.d.mts +21 -0
- package/dist/next.d.ts +21 -0
- package/dist/next.js +220 -0
- package/dist/next.mjs +26 -0
- package/dist/types-BrCFkSyd.d.mts +91 -0
- package/dist/types-BrCFkSyd.d.ts +91 -0
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,274 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.host = options?.host || "https://api.kloddy.com";
|
|
17
|
-
this.defaultOrgId = options?.defaultOrgId || null;
|
|
18
|
-
this.defaultFeatureId = options?.defaultFeatureId || null;
|
|
19
|
-
} else {
|
|
20
|
-
this.apiKey = apiKeyOrOptions.apiKey || apiKeyOrOptions.projectApiKey || apiKeyOrOptions.applicationId || "";
|
|
21
|
-
this.apiSecret = apiKeyOrOptions.apiSecret || apiKeyOrOptions.personalApiKey || apiKeyOrOptions.secretKey || "";
|
|
22
|
-
this.token = apiKeyOrOptions.token || null;
|
|
23
|
-
this.host = apiKeyOrOptions.host || "https://api.kloddy.com";
|
|
24
|
-
this.defaultOrgId = apiKeyOrOptions.defaultOrgId || null;
|
|
25
|
-
this.defaultFeatureId = apiKeyOrOptions.defaultFeatureId || null;
|
|
26
|
-
}
|
|
27
|
-
if (!this.token && (!this.apiKey || !this.apiSecret)) {
|
|
28
|
-
console.warn("KloddyClient: token or credentials missing. API calls will fail.");
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
async login() {
|
|
32
|
-
if (!this.apiKey || !this.apiSecret) {
|
|
33
|
-
throw new Error("KloddyClient: Cannot login without apiKey and apiSecret.");
|
|
34
|
-
}
|
|
35
|
-
const response = await fetch(`${this.host}/api/login`, {
|
|
36
|
-
method: "POST",
|
|
37
|
-
headers: { "Content-Type": "application/json" },
|
|
38
|
-
body: JSON.stringify({
|
|
39
|
-
applicationId: this.apiKey,
|
|
40
|
-
secretKey: this.apiSecret
|
|
41
|
-
})
|
|
42
|
-
});
|
|
43
|
-
if (!response.ok) {
|
|
44
|
-
const error = await response.text();
|
|
45
|
-
throw new Error(`Kloddy Auth failed: ${response.status} ${error}`);
|
|
46
|
-
}
|
|
47
|
-
const data = await response.json();
|
|
48
|
-
this.token = data.token;
|
|
49
|
-
this.tokenExpires = Date.now() + (data.expiresAt ? new Date(data.expiresAt).getTime() - Date.now() : 36e5);
|
|
50
|
-
return this.token;
|
|
51
|
-
}
|
|
52
|
-
async getToken() {
|
|
53
|
-
if (this.token && !this.apiSecret) {
|
|
54
|
-
return this.token;
|
|
55
|
-
}
|
|
56
|
-
if (!this.token || this.tokenExpires && Date.now() >= this.tokenExpires - 6e4) {
|
|
57
|
-
return this.login();
|
|
58
|
-
}
|
|
59
|
-
return this.token;
|
|
60
|
-
}
|
|
61
|
-
async request(path, options = {}) {
|
|
62
|
-
const token = await this.getToken();
|
|
63
|
-
const url = path.startsWith("http") ? path : `${this.host}${path}`;
|
|
64
|
-
const response = await fetch(url, {
|
|
65
|
-
...options,
|
|
66
|
-
headers: {
|
|
67
|
-
...options.headers,
|
|
68
|
-
Authorization: `Bearer ${token}`,
|
|
69
|
-
"Content-Type": "application/json"
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
if (!response.ok) {
|
|
73
|
-
const error = await response.text();
|
|
74
|
-
throw new Error(`Kloddy API error: ${response.status} ${error}`);
|
|
75
|
-
}
|
|
76
|
-
return await response.json();
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Get current user information.
|
|
80
|
-
*/
|
|
81
|
-
async whoAmI() {
|
|
82
|
-
return this.request("/api/whoiam");
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* List organizations for the current user.
|
|
86
|
-
*/
|
|
87
|
-
async listOrganizations() {
|
|
88
|
-
return this.request("/api/organizations");
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* List features, optionally filtered by organization.
|
|
92
|
-
*/
|
|
93
|
-
async listFeatures(orgId) {
|
|
94
|
-
const path = orgId ? `/api/features?org_id=${orgId}` : "/api/features";
|
|
95
|
-
return this.request(path);
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
// src/prompts.ts
|
|
100
|
-
var Prompts = class {
|
|
101
|
-
client;
|
|
102
|
-
constructor(options) {
|
|
103
|
-
if ("posthog" in options && options.posthog) {
|
|
104
|
-
this.client = options.posthog;
|
|
105
|
-
} else {
|
|
106
|
-
this.client = new KloddyClient(options);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* List prompts with filters.
|
|
111
|
-
*/
|
|
112
|
-
async list(options = {}) {
|
|
113
|
-
const params = new URLSearchParams();
|
|
114
|
-
if (options.page) params.append("page", options.page.toString());
|
|
115
|
-
if (options.pageSize) params.append("pageSize", options.pageSize.toString());
|
|
116
|
-
if (options.name) params.append("name", options.name);
|
|
117
|
-
const orgId = options.org_id || this.client.defaultOrgId;
|
|
118
|
-
if (orgId) params.append("org_id", orgId);
|
|
119
|
-
const featureId = options.feature_id || this.client.defaultFeatureId;
|
|
120
|
-
if (featureId) params.append("feature_id", featureId);
|
|
121
|
-
const queryString = params.toString() ? `?${params.toString()}` : "";
|
|
122
|
-
return this.client.request(`/api/prompts${queryString}`);
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Fetch a prompt template.
|
|
126
|
-
*/
|
|
127
|
-
async get(name, options = {}) {
|
|
128
|
-
const params = new URLSearchParams();
|
|
129
|
-
if (options.version) params.append("version", options.version.toString());
|
|
130
|
-
const resolve = options.resolve !== void 0 ? options.resolve : true;
|
|
131
|
-
params.append("resolve", resolve.toString());
|
|
132
|
-
const queryString = params.toString() ? `?${params.toString()}` : "";
|
|
133
|
-
try {
|
|
134
|
-
return await this.client.request(`/api/prompt/${name}${queryString}`, {
|
|
135
|
-
method: "GET"
|
|
136
|
-
});
|
|
137
|
-
} catch (error) {
|
|
138
|
-
if (options.fallback) {
|
|
139
|
-
return {
|
|
140
|
-
id: "fallback",
|
|
141
|
-
name,
|
|
142
|
-
content: options.fallback,
|
|
143
|
-
version: 0
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
throw error;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Execute a prompt via the Kloddy API.
|
|
151
|
-
*/
|
|
152
|
-
async execute(name, options = {}) {
|
|
153
|
-
return this.client.request(`/api/prompt/${name}`, {
|
|
154
|
-
method: "POST",
|
|
155
|
-
body: JSON.stringify({
|
|
156
|
-
...options,
|
|
157
|
-
resolve: options.resolve !== void 0 ? options.resolve : true
|
|
158
|
-
})
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Play: Direct execution for a single model/version.
|
|
163
|
-
* Same as execute but follows specific naming/body requirements.
|
|
164
|
-
*/
|
|
165
|
-
async play(name, options = {}) {
|
|
166
|
-
return this.client.request("/api/play", {
|
|
167
|
-
method: "POST",
|
|
168
|
-
body: JSON.stringify({
|
|
169
|
-
name,
|
|
170
|
-
...options,
|
|
171
|
-
resolve: options.resolve !== void 0 ? options.resolve : true
|
|
172
|
-
})
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Update: Download all prompts for the user/organization.
|
|
177
|
-
*/
|
|
178
|
-
async update(options = {}) {
|
|
179
|
-
return this.list(options);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Local compilation of a template string with variables.
|
|
183
|
-
*/
|
|
184
|
-
compile(template, variables) {
|
|
185
|
-
let content = typeof template === "string" ? template : template.content;
|
|
186
|
-
for (const [key, value] of Object.entries(variables)) {
|
|
187
|
-
const regex = new RegExp(`{{${key}}}|{${key}}`, "g");
|
|
188
|
-
content = content.replace(regex, String(value));
|
|
189
|
-
}
|
|
190
|
-
return content;
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
// src/evaluations.ts
|
|
195
|
-
var Evaluations = class {
|
|
196
|
-
client;
|
|
197
|
-
constructor(options) {
|
|
198
|
-
if ("posthog" in options && options.posthog) {
|
|
199
|
-
this.client = options.posthog;
|
|
200
|
-
} else {
|
|
201
|
-
this.client = new KloddyClient(options);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Run or retrieve an evaluation.
|
|
206
|
-
*/
|
|
207
|
-
async run(options) {
|
|
208
|
-
return this.client.request("/api/evaluate", {
|
|
209
|
-
method: "POST",
|
|
210
|
-
body: JSON.stringify(options)
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Alias for run() as requested.
|
|
215
|
-
*/
|
|
216
|
-
async evaluate(options) {
|
|
217
|
-
return this.run(options);
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Legacy alias for run(name) as requested in the hook example.
|
|
221
|
-
*/
|
|
222
|
-
async get(name, variables = {}) {
|
|
223
|
-
return this.run({ name, variables });
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
// src/hooks/use-prompt.tsx
|
|
228
|
-
import { createContext, useContext, useMemo } from "react";
|
|
229
|
-
import { jsx } from "react/jsx-runtime";
|
|
230
|
-
var KloddyContext = createContext(null);
|
|
231
|
-
var KloddyProvider = ({ children, client, options, apiKey, token }) => {
|
|
232
|
-
const value = useMemo(() => {
|
|
233
|
-
const activeClient = client || new KloddyClient({ ...options, apiKey, token });
|
|
234
|
-
return {
|
|
235
|
-
prompts: new Prompts({ posthog: activeClient }),
|
|
236
|
-
evaluations: new Evaluations({ posthog: activeClient })
|
|
237
|
-
};
|
|
238
|
-
}, [client, options, token]);
|
|
239
|
-
return /* @__PURE__ */ jsx(KloddyContext.Provider, { value, children });
|
|
240
|
-
};
|
|
241
|
-
var usePrompt = () => {
|
|
242
|
-
const context = useContext(KloddyContext);
|
|
243
|
-
if (!context) {
|
|
244
|
-
throw new Error("usePrompt must be used within a KloddyProvider");
|
|
245
|
-
}
|
|
246
|
-
const { prompts, evaluations } = context;
|
|
247
|
-
return {
|
|
248
|
-
getPrompt: (id, options = {}) => prompts.get(id, options),
|
|
249
|
-
getAwnser: (id, options = {}) => prompts.execute(id, options),
|
|
250
|
-
// Use user's spelling "getAwnser"
|
|
251
|
-
getEvaluation: (id, variables = {}) => evaluations.get(id, variables),
|
|
252
|
-
compile: (template, variables) => prompts.compile(template, variables)
|
|
253
|
-
};
|
|
254
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
Evaluations,
|
|
3
|
+
KloddyProvider,
|
|
4
|
+
Prompts,
|
|
5
|
+
useEvaluations,
|
|
6
|
+
usePrompt,
|
|
7
|
+
usePromptStream
|
|
8
|
+
} from "./chunk-WWAXYTZO.mjs";
|
|
9
|
+
import {
|
|
10
|
+
KloddyAuthError,
|
|
11
|
+
KloddyClient,
|
|
12
|
+
KloddyError,
|
|
13
|
+
KloddyNotFoundError,
|
|
14
|
+
KloddyRateLimitError
|
|
15
|
+
} from "./chunk-WSN7MLKR.mjs";
|
|
255
16
|
|
|
256
17
|
// src/index.ts
|
|
257
18
|
var Kloddy = class {
|
|
258
19
|
client;
|
|
259
20
|
prompts;
|
|
260
21
|
evaluations;
|
|
22
|
+
/**
|
|
23
|
+
* Initialize the Kloddy SDK.
|
|
24
|
+
* If no arguments are provided, it will attempt to use KLODDY_API_KEY and KLODDY_API_SECRET from process.env.
|
|
25
|
+
*/
|
|
261
26
|
constructor(apiKeyOrOptions, options) {
|
|
262
27
|
this.client = new KloddyClient(apiKeyOrOptions, options);
|
|
263
|
-
this.prompts = new Prompts({
|
|
264
|
-
this.evaluations = new Evaluations({
|
|
28
|
+
this.prompts = new Prompts({ client: this.client });
|
|
29
|
+
this.evaluations = new Evaluations({ client: this.client });
|
|
265
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Get information about the current authenticated user/application.
|
|
33
|
+
*/
|
|
266
34
|
async whoAmI() {
|
|
267
35
|
return this.client.whoAmI();
|
|
268
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* List organizations accessible by the current credentials.
|
|
39
|
+
*/
|
|
269
40
|
async listOrganizations() {
|
|
270
41
|
return this.client.listOrganizations();
|
|
271
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* List features, optionally filtered by organization.
|
|
45
|
+
*/
|
|
272
46
|
async listFeatures(orgId) {
|
|
273
47
|
return this.client.listFeatures(orgId);
|
|
274
48
|
}
|
|
@@ -277,9 +51,15 @@ var index_default = Kloddy;
|
|
|
277
51
|
export {
|
|
278
52
|
Evaluations,
|
|
279
53
|
Kloddy,
|
|
54
|
+
KloddyAuthError,
|
|
280
55
|
KloddyClient,
|
|
56
|
+
KloddyError,
|
|
57
|
+
KloddyNotFoundError,
|
|
281
58
|
KloddyProvider,
|
|
59
|
+
KloddyRateLimitError,
|
|
282
60
|
Prompts,
|
|
283
61
|
index_default as default,
|
|
284
|
-
|
|
62
|
+
useEvaluations,
|
|
63
|
+
usePrompt,
|
|
64
|
+
usePromptStream
|
|
285
65
|
};
|