@kloddy/kloddy-js 0.1.0
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 +156 -0
- package/dist/client.d.ts +27 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +107 -0
- package/dist/client.js.map +1 -0
- package/dist/evaluations.d.ts +21 -0
- package/dist/evaluations.d.ts.map +1 -0
- package/dist/evaluations.js +38 -0
- package/dist/evaluations.js.map +1 -0
- package/dist/hooks/use-prompt.d.ts +18 -0
- package/dist/hooks/use-prompt.d.ts.map +1 -0
- package/dist/hooks/use-prompt.js +35 -0
- package/dist/hooks/use-prompt.js.map +1 -0
- package/dist/index.d.mts +195 -0
- package/dist/index.d.ts +195 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +327 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +285 -0
- package/dist/prompts.d.ts +34 -0
- package/dist/prompts.d.ts.map +1 -0
- package/dist/prompts.js +108 -0
- package/dist/prompts.js.map +1 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +43 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
interface KloddyOptions {
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
apiSecret?: string;
|
|
6
|
+
token?: string;
|
|
7
|
+
personalApiKey?: string;
|
|
8
|
+
projectApiKey?: string;
|
|
9
|
+
applicationId?: string;
|
|
10
|
+
secretKey?: string;
|
|
11
|
+
host?: string;
|
|
12
|
+
cacheTtlSeconds?: number;
|
|
13
|
+
defaultOrgId?: string;
|
|
14
|
+
defaultFeatureId?: string;
|
|
15
|
+
}
|
|
16
|
+
interface User {
|
|
17
|
+
id: string;
|
|
18
|
+
email: string;
|
|
19
|
+
name: string;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
}
|
|
22
|
+
interface Organization {
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
[key: string]: any;
|
|
26
|
+
}
|
|
27
|
+
interface Feature {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
org_id: string;
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
}
|
|
33
|
+
interface PromptListOptions {
|
|
34
|
+
page?: number;
|
|
35
|
+
pageSize?: number;
|
|
36
|
+
name?: string;
|
|
37
|
+
org_id?: string;
|
|
38
|
+
feature_id?: string;
|
|
39
|
+
}
|
|
40
|
+
interface PromptOptions {
|
|
41
|
+
version?: number | string;
|
|
42
|
+
fallback?: string;
|
|
43
|
+
cacheTtlSeconds?: number;
|
|
44
|
+
resolve?: boolean;
|
|
45
|
+
}
|
|
46
|
+
interface PromptTemplate {
|
|
47
|
+
id: string;
|
|
48
|
+
name: string;
|
|
49
|
+
content: string;
|
|
50
|
+
version: number;
|
|
51
|
+
variables?: string[];
|
|
52
|
+
[key: string]: any;
|
|
53
|
+
}
|
|
54
|
+
interface ExecuteOptions {
|
|
55
|
+
variables?: Record<string, any>;
|
|
56
|
+
model?: string;
|
|
57
|
+
version?: string | number;
|
|
58
|
+
resolve?: boolean;
|
|
59
|
+
}
|
|
60
|
+
interface ExecuteResult {
|
|
61
|
+
result: string;
|
|
62
|
+
model: string;
|
|
63
|
+
version: string | number;
|
|
64
|
+
usage?: {
|
|
65
|
+
prompt_tokens: number;
|
|
66
|
+
completion_tokens: number;
|
|
67
|
+
total_tokens: number;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
interface EvaluationOptions {
|
|
71
|
+
name: string;
|
|
72
|
+
models?: string[];
|
|
73
|
+
judge?: string;
|
|
74
|
+
version?: (string | number)[];
|
|
75
|
+
variables?: Record<string, any>;
|
|
76
|
+
evaluate_id?: string;
|
|
77
|
+
temperature?: number;
|
|
78
|
+
}
|
|
79
|
+
interface EvaluationResult {
|
|
80
|
+
result: string;
|
|
81
|
+
winner?: string;
|
|
82
|
+
answers: {
|
|
83
|
+
model: string;
|
|
84
|
+
answer: string;
|
|
85
|
+
score?: number;
|
|
86
|
+
}[];
|
|
87
|
+
}
|
|
88
|
+
interface AuthResponse {
|
|
89
|
+
token: string;
|
|
90
|
+
expiresAt?: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
declare class KloddyClient {
|
|
94
|
+
private apiKey;
|
|
95
|
+
private apiSecret;
|
|
96
|
+
private host;
|
|
97
|
+
defaultOrgId: string | null;
|
|
98
|
+
defaultFeatureId: string | null;
|
|
99
|
+
private token;
|
|
100
|
+
private tokenExpires;
|
|
101
|
+
constructor(apiKeyOrOptions: string | KloddyOptions, options?: KloddyOptions);
|
|
102
|
+
login(): Promise<string>;
|
|
103
|
+
getToken(): Promise<string>;
|
|
104
|
+
request<T>(path: string, options?: RequestInit): Promise<T>;
|
|
105
|
+
/**
|
|
106
|
+
* Get current user information.
|
|
107
|
+
*/
|
|
108
|
+
whoAmI(): Promise<User>;
|
|
109
|
+
/**
|
|
110
|
+
* List organizations for the current user.
|
|
111
|
+
*/
|
|
112
|
+
listOrganizations(): Promise<Organization[]>;
|
|
113
|
+
/**
|
|
114
|
+
* List features, optionally filtered by organization.
|
|
115
|
+
*/
|
|
116
|
+
listFeatures(orgId?: string): Promise<Feature[]>;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
declare class Prompts {
|
|
120
|
+
private client;
|
|
121
|
+
constructor(options: {
|
|
122
|
+
posthog?: KloddyClient;
|
|
123
|
+
} | KloddyOptions);
|
|
124
|
+
/**
|
|
125
|
+
* List prompts with filters.
|
|
126
|
+
*/
|
|
127
|
+
list(options?: PromptListOptions): Promise<PromptTemplate[]>;
|
|
128
|
+
/**
|
|
129
|
+
* Fetch a prompt template.
|
|
130
|
+
*/
|
|
131
|
+
get(name: string, options?: PromptOptions): Promise<PromptTemplate>;
|
|
132
|
+
/**
|
|
133
|
+
* Execute a prompt via the Kloddy API.
|
|
134
|
+
*/
|
|
135
|
+
execute(name: string, options?: ExecuteOptions): Promise<ExecuteResult>;
|
|
136
|
+
/**
|
|
137
|
+
* Play: Direct execution for a single model/version.
|
|
138
|
+
* Same as execute but follows specific naming/body requirements.
|
|
139
|
+
*/
|
|
140
|
+
play(name: string, options?: Omit<ExecuteOptions, 'judge' | 'evaluate_id'>): Promise<ExecuteResult>;
|
|
141
|
+
/**
|
|
142
|
+
* Update: Download all prompts for the user/organization.
|
|
143
|
+
*/
|
|
144
|
+
update(options?: PromptListOptions): Promise<PromptTemplate[]>;
|
|
145
|
+
/**
|
|
146
|
+
* Local compilation of a template string with variables.
|
|
147
|
+
*/
|
|
148
|
+
compile(template: string | PromptTemplate, variables: Record<string, any>): string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
declare class Evaluations {
|
|
152
|
+
private client;
|
|
153
|
+
constructor(options: {
|
|
154
|
+
posthog?: KloddyClient;
|
|
155
|
+
} | KloddyOptions);
|
|
156
|
+
/**
|
|
157
|
+
* Run or retrieve an evaluation.
|
|
158
|
+
*/
|
|
159
|
+
run(options: EvaluationOptions): Promise<EvaluationResult>;
|
|
160
|
+
/**
|
|
161
|
+
* Alias for run() as requested.
|
|
162
|
+
*/
|
|
163
|
+
evaluate(options: EvaluationOptions): Promise<EvaluationResult>;
|
|
164
|
+
/**
|
|
165
|
+
* Legacy alias for run(name) as requested in the hook example.
|
|
166
|
+
*/
|
|
167
|
+
get(name: string, variables?: Record<string, any>): Promise<EvaluationResult>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
interface KloddyProviderProps {
|
|
171
|
+
children: React.ReactNode;
|
|
172
|
+
client?: KloddyClient;
|
|
173
|
+
options?: KloddyOptions;
|
|
174
|
+
apiKey?: string;
|
|
175
|
+
token?: string;
|
|
176
|
+
}
|
|
177
|
+
declare const KloddyProvider: React.FC<KloddyProviderProps>;
|
|
178
|
+
declare const usePrompt: () => {
|
|
179
|
+
getPrompt: (id: string, options?: {}) => Promise<PromptTemplate>;
|
|
180
|
+
getAwnser: (id: string, options?: {}) => Promise<ExecuteResult>;
|
|
181
|
+
getEvaluation: (id: string, variables?: {}) => Promise<EvaluationResult>;
|
|
182
|
+
compile: (template: any, variables: any) => string;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
declare class Kloddy {
|
|
186
|
+
client: KloddyClient;
|
|
187
|
+
prompts: Prompts;
|
|
188
|
+
evaluations: Evaluations;
|
|
189
|
+
constructor(apiKeyOrOptions: string | KloddyOptions, options?: KloddyOptions);
|
|
190
|
+
whoAmI(): Promise<User>;
|
|
191
|
+
listOrganizations(): Promise<Organization[]>;
|
|
192
|
+
listFeatures(orgId?: string): Promise<Feature[]>;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export { type AuthResponse, type EvaluationOptions, type EvaluationResult, Evaluations, type ExecuteOptions, type ExecuteResult, type Feature, Kloddy, KloddyClient, type KloddyOptions, KloddyProvider, type KloddyProviderProps, type Organization, type PromptListOptions, type PromptOptions, type PromptTemplate, Prompts, type User, Kloddy as default, usePrompt };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AAEnC,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,qBAAa,MAAM;IACV,MAAM,EAAE,YAAY,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,WAAW,CAAC;gBAEpB,eAAe,EAAE,MAAM,GAAG,aAAa,EAAE,OAAO,CAAC,EAAE,aAAa;IAMtE,MAAM;IAIN,iBAAiB;IAIjB,YAAY,CAAC,KAAK,CAAC,EAAE,MAAM;CAGlC;AAED,eAAe,MAAM,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Evaluations: () => Evaluations,
|
|
34
|
+
Kloddy: () => Kloddy,
|
|
35
|
+
KloddyClient: () => KloddyClient,
|
|
36
|
+
KloddyProvider: () => KloddyProvider,
|
|
37
|
+
Prompts: () => Prompts,
|
|
38
|
+
default: () => index_default,
|
|
39
|
+
usePrompt: () => usePrompt
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(index_exports);
|
|
42
|
+
|
|
43
|
+
// src/client.ts
|
|
44
|
+
var import_cross_fetch = __toESM(require("cross-fetch"));
|
|
45
|
+
var KloddyClient = class {
|
|
46
|
+
apiKey;
|
|
47
|
+
apiSecret;
|
|
48
|
+
host;
|
|
49
|
+
defaultOrgId = null;
|
|
50
|
+
defaultFeatureId = null;
|
|
51
|
+
token = null;
|
|
52
|
+
tokenExpires = null;
|
|
53
|
+
constructor(apiKeyOrOptions, options) {
|
|
54
|
+
if (typeof apiKeyOrOptions === "string") {
|
|
55
|
+
this.apiKey = apiKeyOrOptions;
|
|
56
|
+
this.apiSecret = options?.apiSecret || options?.personalApiKey || options?.secretKey || "";
|
|
57
|
+
this.token = options?.token || null;
|
|
58
|
+
this.host = options?.host || "https://api.kloddy.com";
|
|
59
|
+
this.defaultOrgId = options?.defaultOrgId || null;
|
|
60
|
+
this.defaultFeatureId = options?.defaultFeatureId || null;
|
|
61
|
+
} else {
|
|
62
|
+
this.apiKey = apiKeyOrOptions.apiKey || apiKeyOrOptions.projectApiKey || apiKeyOrOptions.applicationId || "";
|
|
63
|
+
this.apiSecret = apiKeyOrOptions.apiSecret || apiKeyOrOptions.personalApiKey || apiKeyOrOptions.secretKey || "";
|
|
64
|
+
this.token = apiKeyOrOptions.token || null;
|
|
65
|
+
this.host = apiKeyOrOptions.host || "https://api.kloddy.com";
|
|
66
|
+
this.defaultOrgId = apiKeyOrOptions.defaultOrgId || null;
|
|
67
|
+
this.defaultFeatureId = apiKeyOrOptions.defaultFeatureId || null;
|
|
68
|
+
}
|
|
69
|
+
if (!this.token && (!this.apiKey || !this.apiSecret)) {
|
|
70
|
+
console.warn("KloddyClient: token or credentials missing. API calls will fail.");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async login() {
|
|
74
|
+
if (!this.apiKey || !this.apiSecret) {
|
|
75
|
+
throw new Error("KloddyClient: Cannot login without apiKey and apiSecret.");
|
|
76
|
+
}
|
|
77
|
+
const response = await (0, import_cross_fetch.default)(`${this.host}/api/login`, {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: { "Content-Type": "application/json" },
|
|
80
|
+
body: JSON.stringify({
|
|
81
|
+
applicationId: this.apiKey,
|
|
82
|
+
secretKey: this.apiSecret
|
|
83
|
+
})
|
|
84
|
+
});
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
const error = await response.text();
|
|
87
|
+
throw new Error(`Kloddy Auth failed: ${response.status} ${error}`);
|
|
88
|
+
}
|
|
89
|
+
const data = await response.json();
|
|
90
|
+
this.token = data.token;
|
|
91
|
+
this.tokenExpires = Date.now() + (data.expiresAt ? new Date(data.expiresAt).getTime() - Date.now() : 36e5);
|
|
92
|
+
return this.token;
|
|
93
|
+
}
|
|
94
|
+
async getToken() {
|
|
95
|
+
if (this.token && !this.apiSecret) {
|
|
96
|
+
return this.token;
|
|
97
|
+
}
|
|
98
|
+
if (!this.token || this.tokenExpires && Date.now() >= this.tokenExpires - 6e4) {
|
|
99
|
+
return this.login();
|
|
100
|
+
}
|
|
101
|
+
return this.token;
|
|
102
|
+
}
|
|
103
|
+
async request(path, options = {}) {
|
|
104
|
+
const token = await this.getToken();
|
|
105
|
+
const url = path.startsWith("http") ? path : `${this.host}${path}`;
|
|
106
|
+
const response = await (0, import_cross_fetch.default)(url, {
|
|
107
|
+
...options,
|
|
108
|
+
headers: {
|
|
109
|
+
...options.headers,
|
|
110
|
+
Authorization: `Bearer ${token}`,
|
|
111
|
+
"Content-Type": "application/json"
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
const error = await response.text();
|
|
116
|
+
throw new Error(`Kloddy API error: ${response.status} ${error}`);
|
|
117
|
+
}
|
|
118
|
+
return await response.json();
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get current user information.
|
|
122
|
+
*/
|
|
123
|
+
async whoAmI() {
|
|
124
|
+
return this.request("/api/whoiam");
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* List organizations for the current user.
|
|
128
|
+
*/
|
|
129
|
+
async listOrganizations() {
|
|
130
|
+
return this.request("/api/organizations");
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* List features, optionally filtered by organization.
|
|
134
|
+
*/
|
|
135
|
+
async listFeatures(orgId) {
|
|
136
|
+
const path = orgId ? `/api/features?org_id=${orgId}` : "/api/features";
|
|
137
|
+
return this.request(path);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// src/prompts.ts
|
|
142
|
+
var Prompts = class {
|
|
143
|
+
client;
|
|
144
|
+
constructor(options) {
|
|
145
|
+
if ("posthog" in options && options.posthog) {
|
|
146
|
+
this.client = options.posthog;
|
|
147
|
+
} else {
|
|
148
|
+
this.client = new KloddyClient(options);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* List prompts with filters.
|
|
153
|
+
*/
|
|
154
|
+
async list(options = {}) {
|
|
155
|
+
const params = new URLSearchParams();
|
|
156
|
+
if (options.page) params.append("page", options.page.toString());
|
|
157
|
+
if (options.pageSize) params.append("pageSize", options.pageSize.toString());
|
|
158
|
+
if (options.name) params.append("name", options.name);
|
|
159
|
+
const orgId = options.org_id || this.client.defaultOrgId;
|
|
160
|
+
if (orgId) params.append("org_id", orgId);
|
|
161
|
+
const featureId = options.feature_id || this.client.defaultFeatureId;
|
|
162
|
+
if (featureId) params.append("feature_id", featureId);
|
|
163
|
+
const queryString = params.toString() ? `?${params.toString()}` : "";
|
|
164
|
+
return this.client.request(`/api/prompts${queryString}`);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Fetch a prompt template.
|
|
168
|
+
*/
|
|
169
|
+
async get(name, options = {}) {
|
|
170
|
+
const params = new URLSearchParams();
|
|
171
|
+
if (options.version) params.append("version", options.version.toString());
|
|
172
|
+
const resolve = options.resolve !== void 0 ? options.resolve : true;
|
|
173
|
+
params.append("resolve", resolve.toString());
|
|
174
|
+
const queryString = params.toString() ? `?${params.toString()}` : "";
|
|
175
|
+
try {
|
|
176
|
+
return await this.client.request(`/api/prompt/${name}${queryString}`, {
|
|
177
|
+
method: "GET"
|
|
178
|
+
});
|
|
179
|
+
} catch (error) {
|
|
180
|
+
if (options.fallback) {
|
|
181
|
+
return {
|
|
182
|
+
id: "fallback",
|
|
183
|
+
name,
|
|
184
|
+
content: options.fallback,
|
|
185
|
+
version: 0
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Execute a prompt via the Kloddy API.
|
|
193
|
+
*/
|
|
194
|
+
async execute(name, options = {}) {
|
|
195
|
+
return this.client.request(`/api/prompt/${name}`, {
|
|
196
|
+
method: "POST",
|
|
197
|
+
body: JSON.stringify({
|
|
198
|
+
...options,
|
|
199
|
+
resolve: options.resolve !== void 0 ? options.resolve : true
|
|
200
|
+
})
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Play: Direct execution for a single model/version.
|
|
205
|
+
* Same as execute but follows specific naming/body requirements.
|
|
206
|
+
*/
|
|
207
|
+
async play(name, options = {}) {
|
|
208
|
+
return this.client.request("/api/play", {
|
|
209
|
+
method: "POST",
|
|
210
|
+
body: JSON.stringify({
|
|
211
|
+
name,
|
|
212
|
+
...options,
|
|
213
|
+
resolve: options.resolve !== void 0 ? options.resolve : true
|
|
214
|
+
})
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Update: Download all prompts for the user/organization.
|
|
219
|
+
*/
|
|
220
|
+
async update(options = {}) {
|
|
221
|
+
return this.list(options);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Local compilation of a template string with variables.
|
|
225
|
+
*/
|
|
226
|
+
compile(template, variables) {
|
|
227
|
+
let content = typeof template === "string" ? template : template.content;
|
|
228
|
+
for (const [key, value] of Object.entries(variables)) {
|
|
229
|
+
const regex = new RegExp(`{{${key}}}|{${key}}`, "g");
|
|
230
|
+
content = content.replace(regex, String(value));
|
|
231
|
+
}
|
|
232
|
+
return content;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// src/evaluations.ts
|
|
237
|
+
var Evaluations = class {
|
|
238
|
+
client;
|
|
239
|
+
constructor(options) {
|
|
240
|
+
if ("posthog" in options && options.posthog) {
|
|
241
|
+
this.client = options.posthog;
|
|
242
|
+
} else {
|
|
243
|
+
this.client = new KloddyClient(options);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Run or retrieve an evaluation.
|
|
248
|
+
*/
|
|
249
|
+
async run(options) {
|
|
250
|
+
return this.client.request("/api/evaluate", {
|
|
251
|
+
method: "POST",
|
|
252
|
+
body: JSON.stringify(options)
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Alias for run() as requested.
|
|
257
|
+
*/
|
|
258
|
+
async evaluate(options) {
|
|
259
|
+
return this.run(options);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Legacy alias for run(name) as requested in the hook example.
|
|
263
|
+
*/
|
|
264
|
+
async get(name, variables = {}) {
|
|
265
|
+
return this.run({ name, variables });
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// src/hooks/use-prompt.tsx
|
|
270
|
+
var import_react = require("react");
|
|
271
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
272
|
+
var KloddyContext = (0, import_react.createContext)(null);
|
|
273
|
+
var KloddyProvider = ({ children, client, options, apiKey, token }) => {
|
|
274
|
+
const value = (0, import_react.useMemo)(() => {
|
|
275
|
+
const activeClient = client || new KloddyClient({ ...options, apiKey, token });
|
|
276
|
+
return {
|
|
277
|
+
prompts: new Prompts({ posthog: activeClient }),
|
|
278
|
+
evaluations: new Evaluations({ posthog: activeClient })
|
|
279
|
+
};
|
|
280
|
+
}, [client, options, token]);
|
|
281
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(KloddyContext.Provider, { value, children });
|
|
282
|
+
};
|
|
283
|
+
var usePrompt = () => {
|
|
284
|
+
const context = (0, import_react.useContext)(KloddyContext);
|
|
285
|
+
if (!context) {
|
|
286
|
+
throw new Error("usePrompt must be used within a KloddyProvider");
|
|
287
|
+
}
|
|
288
|
+
const { prompts, evaluations } = context;
|
|
289
|
+
return {
|
|
290
|
+
getPrompt: (id, options = {}) => prompts.get(id, options),
|
|
291
|
+
getAwnser: (id, options = {}) => prompts.execute(id, options),
|
|
292
|
+
// Use user's spelling "getAwnser"
|
|
293
|
+
getEvaluation: (id, variables = {}) => evaluations.get(id, variables),
|
|
294
|
+
compile: (template, variables) => prompts.compile(template, variables)
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// src/index.ts
|
|
299
|
+
var Kloddy = class {
|
|
300
|
+
client;
|
|
301
|
+
prompts;
|
|
302
|
+
evaluations;
|
|
303
|
+
constructor(apiKeyOrOptions, options) {
|
|
304
|
+
this.client = new KloddyClient(apiKeyOrOptions, options);
|
|
305
|
+
this.prompts = new Prompts({ posthog: this.client });
|
|
306
|
+
this.evaluations = new Evaluations({ posthog: this.client });
|
|
307
|
+
}
|
|
308
|
+
async whoAmI() {
|
|
309
|
+
return this.client.whoAmI();
|
|
310
|
+
}
|
|
311
|
+
async listOrganizations() {
|
|
312
|
+
return this.client.listOrganizations();
|
|
313
|
+
}
|
|
314
|
+
async listFeatures(orgId) {
|
|
315
|
+
return this.client.listFeatures(orgId);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
var index_default = Kloddy;
|
|
319
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
320
|
+
0 && (module.exports = {
|
|
321
|
+
Evaluations,
|
|
322
|
+
Kloddy,
|
|
323
|
+
KloddyClient,
|
|
324
|
+
KloddyProvider,
|
|
325
|
+
Prompts,
|
|
326
|
+
usePrompt
|
|
327
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,4CAA0B;AAC1B,gDAA8B;AAC9B,qDAAmC;AAEnC,qCAAwC;AACxC,uCAAoC;AACpC,+CAA4C;AAG5C,MAAa,MAAM;IACV,MAAM,CAAe;IACrB,OAAO,CAAU;IACjB,WAAW,CAAc;IAEhC,YAAY,eAAuC,EAAE,OAAuB;QAC1E,IAAI,CAAC,MAAM,GAAG,IAAI,qBAAY,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;QACzD,IAAI,CAAC,OAAO,GAAG,IAAI,iBAAO,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QACrD,IAAI,CAAC,WAAW,GAAG,IAAI,yBAAW,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IAC/D,CAAC;IAED,KAAK,CAAC,MAAM;QACV,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,iBAAiB;QACrB,OAAO,IAAI,CAAC,MAAM,CAAC,iBAAiB,EAAE,CAAC;IACzC,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,KAAc;QAC/B,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IACzC,CAAC;CACF;AAtBD,wBAsBC;AAED,kBAAe,MAAM,CAAC"}
|