@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 ADDED
@@ -0,0 +1,156 @@
1
+ # @kloddy/kloddy-js
2
+
3
+ Kloddy is the ultimate platform for Prompt Engineering and LLM Analytics. This SDK allows you to fetch, compile, and execute prompts directly from your Node.js or React applications.
4
+
5
+ ## installation
6
+
7
+ ```bash
8
+ # via npm
9
+ npm install @kloddy/kloddy-js
10
+
11
+ # via yarn
12
+ yarn add @kloddy/kloddy-js
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ### Basic Usage (Node.js)
18
+
19
+ ```javascript
20
+ import { Kloddy } from 'kloddy-js';
21
+
22
+ const kloddy = new Kloddy({
23
+ apiKey: '<your_project_api_key>',
24
+ apiSecret: '<your_personal_api_key>',
25
+ host: 'https://api.kloddy.com' // Optional
26
+ });
27
+
28
+ // Fetch a prompt template
29
+ const template = await kloddy.prompts.get('customer-support-agent', {
30
+ fallback: 'You are a helpful assistant.'
31
+ });
32
+
33
+ // Compile with variables
34
+ const systemPrompt = kloddy.prompts.compile(template, {
35
+ userName: 'Alice',
36
+ company: 'Acme Corp'
37
+ });
38
+
39
+ console.log(systemPrompt);
40
+ ```
41
+
42
+ ### React Usage
43
+
44
+ Wrap your app with `KloddyProvider`. For security, it is highly recommended to generate an **accessToken** on your server and pass it to the frontend instead of using your `apiSecret` in the browser.
45
+
46
+ ```tsx
47
+ import { KloddyProvider, usePrompt } from '@kloddy/kloddy-js';
48
+
49
+ function MyComponent() {
50
+ const { getPrompt, getAwnser } = usePrompt();
51
+
52
+ const handleWelcome = async () => {
53
+ const prompt = await getPrompt('welcome-message');
54
+ const response = await getAwnser('welcome-message', {
55
+ variables: { name: 'Alice' }
56
+ });
57
+ console.log(response.result);
58
+ };
59
+
60
+ return <button onClick={handleWelcome}>Say Hello</button>;
61
+ }
62
+
63
+ function App() {
64
+ // The token should be fetched from your own API
65
+ const [token, setToken] = useState('');
66
+
67
+ return (
68
+ <KloddyProvider apiKey="YOUR_PROJECT_ID" token={token}>
69
+ <MyComponent />
70
+ </KloddyProvider>
71
+ );
72
+ }
73
+ ```
74
+
75
+ ## Advanced Usage
76
+
77
+ ### Organization & Feature Context
78
+ You can set a default organization or feature ID during initialization to simplify your calls.
79
+
80
+ ```javascript
81
+ const kloddy = new Kloddy({
82
+ apiKey: '...',
83
+ defaultOrgId: 'org_123',
84
+ defaultFeatureId: 'feat_456'
85
+ });
86
+
87
+ // These will now use the defaults automatically
88
+ const prompts = await kloddy.prompts.list();
89
+ const features = await kloddy.listFeatures();
90
+ ```
91
+
92
+ ### Account Information
93
+ ```javascript
94
+ const user = await kloddy.whoAmI();
95
+ const orgs = await kloddy.listOrganizations();
96
+ const features = await kloddy.listFeatures(orgs[0].id);
97
+ ```
98
+
99
+ ### Prompts Management
100
+ ```javascript
101
+ // List all prompts
102
+ const allPrompts = await kloddy.prompts.list({ pageSize: 50 });
103
+
104
+ // Update/Sync (alias for list)
105
+ const synced = await kloddy.prompts.update();
106
+
107
+ // Play (Direct Execution)
108
+ const result = await kloddy.prompts.play('my-prompt', {
109
+ variables: { user: 'Alice' },
110
+ model: 'gpt-4'
111
+ });
112
+ ```
113
+
114
+ ### Evaluations
115
+ ```javascript
116
+ const evalResult = await kloddy.evaluations.evaluate({
117
+ name: 'model-comparison',
118
+ models: ['gpt-4', 'claude-3'],
119
+ judge: 'gpt-4-judge',
120
+ variables: { input: '...' },
121
+ temperature: 0.7
122
+ });
123
+ ```
124
+
125
+ ## API Reference
126
+
127
+ ### `Kloddy` Client
128
+ - `whoAmI()`: Get current user details.
129
+ - `listOrganizations()`: List organizations.
130
+ - `listFeatures(orgId?)`: List features.
131
+ - `prompts.list(filters)`: List prompts with pagination and filters.
132
+ - `prompts.get(name, options)`: Fetch a template.
133
+ - `prompts.play(name, options)`: Execute a prompt directly.
134
+ - `prompts.update()`: Sync all prompts.
135
+ - `evaluations.evaluate(options)`: Run model evaluations.
136
+
137
+ ### React Hooks
138
+ - `usePrompt()`: Returns `getPrompt`, `getAwnser`, `getEvaluation`, `compile`.
139
+
140
+ ## Integration with Vercel AI Gateway
141
+
142
+ You can use Kloddy to manage your prompts and Vercel AI Gateway to route your LLM calls.
143
+
144
+ ```javascript
145
+ const template = await kloddy.prompts.get('support-agent');
146
+ const systemPrompt = kloddy.prompts.compile(template, { user: 'Alice' });
147
+
148
+ // Use with OpenAI via Vercel AI Gateway
149
+ const openai = new OpenAI({
150
+ baseURL: 'https://gateway.ai.cloudflare.com/v1/YOUR_ACCOUNT_ID/YOUR_GATEWAY_ID/openai'
151
+ });
152
+ ```
153
+
154
+ ## License
155
+
156
+ MIT © [Kloddy](https://kloddy.com)
@@ -0,0 +1,27 @@
1
+ import { KloddyOptions, User, Organization, Feature } from './types';
2
+ export declare class KloddyClient {
3
+ private apiKey;
4
+ private apiSecret;
5
+ private host;
6
+ defaultOrgId: string | null;
7
+ defaultFeatureId: string | null;
8
+ private token;
9
+ private tokenExpires;
10
+ constructor(apiKeyOrOptions: string | KloddyOptions, options?: KloddyOptions);
11
+ login(): Promise<string>;
12
+ getToken(): Promise<string>;
13
+ request<T>(path: string, options?: RequestInit): Promise<T>;
14
+ /**
15
+ * Get current user information.
16
+ */
17
+ whoAmI(): Promise<User>;
18
+ /**
19
+ * List organizations for the current user.
20
+ */
21
+ listOrganizations(): Promise<Organization[]>;
22
+ /**
23
+ * List features, optionally filtered by organization.
24
+ */
25
+ listFeatures(orgId?: string): Promise<Feature[]>;
26
+ }
27
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAgB,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAEnF,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,IAAI,CAAS;IACd,YAAY,EAAE,MAAM,GAAG,IAAI,CAAQ;IACnC,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAQ;IAC9C,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,YAAY,CAAuB;gBAE/B,eAAe,EAAE,MAAM,GAAG,aAAa,EAAE,OAAO,CAAC,EAAE,aAAa;IAsBtE,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;IA2BxB,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC;IAY3B,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,WAAgB,GAAG,OAAO,CAAC,CAAC,CAAC;IAqBrE;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAI7B;;OAEG;IACG,iBAAiB,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;IAIlD;;OAEG;IACG,YAAY,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;CAIvD"}
package/dist/client.js ADDED
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.KloddyClient = void 0;
7
+ const cross_fetch_1 = __importDefault(require("cross-fetch"));
8
+ class KloddyClient {
9
+ apiKey;
10
+ apiSecret;
11
+ host;
12
+ defaultOrgId = null;
13
+ defaultFeatureId = null;
14
+ token = null;
15
+ tokenExpires = null;
16
+ constructor(apiKeyOrOptions, options) {
17
+ if (typeof apiKeyOrOptions === 'string') {
18
+ this.apiKey = apiKeyOrOptions;
19
+ this.apiSecret = options?.apiSecret || options?.personalApiKey || options?.secretKey || '';
20
+ this.token = options?.token || null;
21
+ this.host = options?.host || 'https://api.kloddy.com';
22
+ this.defaultOrgId = options?.defaultOrgId || null;
23
+ this.defaultFeatureId = options?.defaultFeatureId || null;
24
+ }
25
+ else {
26
+ this.apiKey = apiKeyOrOptions.apiKey || apiKeyOrOptions.projectApiKey || apiKeyOrOptions.applicationId || '';
27
+ this.apiSecret = apiKeyOrOptions.apiSecret || apiKeyOrOptions.personalApiKey || apiKeyOrOptions.secretKey || '';
28
+ this.token = apiKeyOrOptions.token || null;
29
+ this.host = apiKeyOrOptions.host || 'https://api.kloddy.com';
30
+ this.defaultOrgId = apiKeyOrOptions.defaultOrgId || null;
31
+ this.defaultFeatureId = apiKeyOrOptions.defaultFeatureId || null;
32
+ }
33
+ if (!this.token && (!this.apiKey || !this.apiSecret)) {
34
+ console.warn('KloddyClient: token or credentials missing. API calls will fail.');
35
+ }
36
+ }
37
+ async login() {
38
+ if (!this.apiKey || !this.apiSecret) {
39
+ throw new Error('KloddyClient: Cannot login without apiKey and apiSecret.');
40
+ }
41
+ const response = await (0, cross_fetch_1.default)(`${this.host}/api/login`, {
42
+ method: 'POST',
43
+ headers: { 'Content-Type': 'application/json' },
44
+ body: JSON.stringify({
45
+ applicationId: this.apiKey,
46
+ secretKey: this.apiSecret,
47
+ }),
48
+ });
49
+ if (!response.ok) {
50
+ const error = await response.text();
51
+ throw new Error(`Kloddy Auth failed: ${response.status} ${error}`);
52
+ }
53
+ const data = (await response.json());
54
+ this.token = data.token;
55
+ // Set expiry to 1 hour from now as a fallback if not provided
56
+ this.tokenExpires = Date.now() + (data.expiresAt ? new Date(data.expiresAt).getTime() - Date.now() : 3600000);
57
+ return this.token;
58
+ }
59
+ async getToken() {
60
+ // If we have a static token (passed in constructor) and no secretKey, don't try to refresh
61
+ if (this.token && !this.apiSecret) {
62
+ return this.token;
63
+ }
64
+ if (!this.token || (this.tokenExpires && Date.now() >= this.tokenExpires - 60000)) {
65
+ return this.login();
66
+ }
67
+ return this.token;
68
+ }
69
+ async request(path, options = {}) {
70
+ const token = await this.getToken();
71
+ const url = path.startsWith('http') ? path : `${this.host}${path}`;
72
+ const response = await (0, cross_fetch_1.default)(url, {
73
+ ...options,
74
+ headers: {
75
+ ...options.headers,
76
+ Authorization: `Bearer ${token}`,
77
+ 'Content-Type': 'application/json',
78
+ },
79
+ });
80
+ if (!response.ok) {
81
+ const error = await response.text();
82
+ throw new Error(`Kloddy API error: ${response.status} ${error}`);
83
+ }
84
+ return (await response.json());
85
+ }
86
+ /**
87
+ * Get current user information.
88
+ */
89
+ async whoAmI() {
90
+ return this.request('/api/whoiam');
91
+ }
92
+ /**
93
+ * List organizations for the current user.
94
+ */
95
+ async listOrganizations() {
96
+ return this.request('/api/organizations');
97
+ }
98
+ /**
99
+ * List features, optionally filtered by organization.
100
+ */
101
+ async listFeatures(orgId) {
102
+ const path = orgId ? `/api/features?org_id=${orgId}` : '/api/features';
103
+ return this.request(path);
104
+ }
105
+ }
106
+ exports.KloddyClient = KloddyClient;
107
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAgC;AAGhC,MAAa,YAAY;IACf,MAAM,CAAS;IACf,SAAS,CAAS;IAClB,IAAI,CAAS;IACd,YAAY,GAAkB,IAAI,CAAC;IACnC,gBAAgB,GAAkB,IAAI,CAAC;IACtC,KAAK,GAAkB,IAAI,CAAC;IAC5B,YAAY,GAAkB,IAAI,CAAC;IAE3C,YAAY,eAAuC,EAAE,OAAuB;QAC1E,IAAI,OAAO,eAAe,KAAK,QAAQ,EAAE,CAAC;YACxC,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC;YAC9B,IAAI,CAAC,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,OAAO,EAAE,cAAc,IAAI,OAAO,EAAE,SAAS,IAAI,EAAE,CAAC;YAC3F,IAAI,CAAC,KAAK,GAAG,OAAO,EAAE,KAAK,IAAI,IAAI,CAAC;YACpC,IAAI,CAAC,IAAI,GAAG,OAAO,EAAE,IAAI,IAAI,wBAAwB,CAAC;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AM,IAAI,CAAC,QAAQ,EAAE,CAAC;QACpC,MAAM,GAAG,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,GAAG,IAAI,EAAE,CAAC;QAEnE,MAAM,QAAQ,GAAG,MAAM,IAAA,qBAAK,EAAC,GAAG,EAAE;YAChC,GAAG,OAAO;YACV,OAAO,EAAE;gBACP,GAAG,OAAO,CAAC,OAAO;gBAClB,aAAa,EAAE,UAAU,KAAK,EAAE;gBAChC,cAAc,EAAE,kBAAkB;aACnC;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,KAAK,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACpC,MAAM,IAAI,KAAK,CAAC,qBAAqB,QAAQ,CAAC,MAAM,IAAI,KAAK,EAAE,CAAC,CAAC;QACnE,CAAC;QAED,OAAO,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAM,CAAC;IACtC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM;QACV,OAAO,IAAI,CAAC,OAAO,CAAO,aAAa,CAAC,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,iBAAiB;QACrB,OAAO,IAAI,CAAC,OAAO,CAAiB,oBAAoB,CAAC,CAAC;IAC5D,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY,CAAC,KAAc;QAC/B,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,wBAAwB,KAAK,EAAE,CAAC,CAAC,CAAC,eAAe,CAAC;QACvE,OAAO,IAAI,CAAC,OAAO,CAAY,IAAI,CAAC,CAAC;IACvC,CAAC;CACF;AAhHD,oCAgHC"}
@@ -0,0 +1,21 @@
1
+ import { KloddyClient } from './client';
2
+ import { EvaluationOptions, EvaluationResult, KloddyOptions } from './types';
3
+ export declare class Evaluations {
4
+ private client;
5
+ constructor(options: {
6
+ posthog?: KloddyClient;
7
+ } | KloddyOptions);
8
+ /**
9
+ * Run or retrieve an evaluation.
10
+ */
11
+ run(options: EvaluationOptions): Promise<EvaluationResult>;
12
+ /**
13
+ * Alias for run() as requested.
14
+ */
15
+ evaluate(options: EvaluationOptions): Promise<EvaluationResult>;
16
+ /**
17
+ * Legacy alias for run(name) as requested in the hook example.
18
+ */
19
+ get(name: string, variables?: Record<string, any>): Promise<EvaluationResult>;
20
+ }
21
+ //# sourceMappingURL=evaluations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluations.d.ts","sourceRoot":"","sources":["../src/evaluations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7E,qBAAa,WAAW;IACtB,OAAO,CAAC,MAAM,CAAe;gBAEjB,OAAO,EAAE;QAAE,OAAO,CAAC,EAAE,YAAY,CAAA;KAAE,GAAG,aAAa;IAQ/D;;OAEG;IACG,GAAG,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAOhE;;OAEG;IACG,QAAQ,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAIrE;;OAEG;IACG,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAGxF"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Evaluations = void 0;
4
+ const client_1 = require("./client");
5
+ class Evaluations {
6
+ client;
7
+ constructor(options) {
8
+ if ('posthog' in options && options.posthog) {
9
+ this.client = options.posthog;
10
+ }
11
+ else {
12
+ this.client = new client_1.KloddyClient(options);
13
+ }
14
+ }
15
+ /**
16
+ * Run or retrieve an evaluation.
17
+ */
18
+ async run(options) {
19
+ return this.client.request('/api/evaluate', {
20
+ method: 'POST',
21
+ body: JSON.stringify(options),
22
+ });
23
+ }
24
+ /**
25
+ * Alias for run() as requested.
26
+ */
27
+ async evaluate(options) {
28
+ return this.run(options);
29
+ }
30
+ /**
31
+ * Legacy alias for run(name) as requested in the hook example.
32
+ */
33
+ async get(name, variables = {}) {
34
+ return this.run({ name, variables });
35
+ }
36
+ }
37
+ exports.Evaluations = Evaluations;
38
+ //# sourceMappingURL=evaluations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluations.js","sourceRoot":"","sources":["../src/evaluations.ts"],"names":[],"mappings":";;;AAAA,qCAAwC;AAGxC,MAAa,WAAW;IACd,MAAM,CAAe;IAE7B,YAAY,OAAmD;QAC7D,IAAI,SAAS,IAAI,OAAO,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;YAC5C,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,OAAuB,CAAC;QAChD,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,MAAM,GAAG,IAAI,qBAAY,CAAC,OAAwB,CAAC,CAAC;QAC3D,CAAC;IACH,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAG,CAAC,OAA0B;QAClC,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAmB,eAAe,EAAE;YAC5D,MAAM,EAAE,MAAM;YACd,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;SAC9B,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,QAAQ,CAAC,OAA0B;QACvC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAG,CAAC,IAAY,EAAE,YAAiC,EAAE;QACzD,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC;IACvC,CAAC;CACF;AAlCD,kCAkCC"}
@@ -0,0 +1,18 @@
1
+ import React from 'react';
2
+ import { KloddyClient } from '../client';
3
+ import { KloddyOptions } from '../types';
4
+ export interface KloddyProviderProps {
5
+ children: React.ReactNode;
6
+ client?: KloddyClient;
7
+ options?: KloddyOptions;
8
+ apiKey?: string;
9
+ token?: string;
10
+ }
11
+ export declare const KloddyProvider: React.FC<KloddyProviderProps>;
12
+ export declare const usePrompt: () => {
13
+ getPrompt: (id: string, options?: {}) => Promise<import("../types").PromptTemplate>;
14
+ getAwnser: (id: string, options?: {}) => Promise<import("../types").ExecuteResult>;
15
+ getEvaluation: (id: string, variables?: {}) => Promise<import("../types").EvaluationResult>;
16
+ compile: (template: any, variables: any) => string;
17
+ };
18
+ //# sourceMappingURL=use-prompt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-prompt.d.ts","sourceRoot":"","sources":["../../src/hooks/use-prompt.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA6C,MAAM,OAAO,CAAC;AAGlE,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AASzC,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CAUxD,CAAC;AAEF,eAAO,MAAM,SAAS;oBASF,MAAM;oBACN,MAAM;wBACF,MAAM;wBACN,GAAG,aAAa,GAAG;CAE1C,CAAC"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePrompt = exports.KloddyProvider = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const prompts_1 = require("../prompts");
7
+ const evaluations_1 = require("../evaluations");
8
+ const client_1 = require("../client");
9
+ const KloddyContext = (0, react_1.createContext)(null);
10
+ const KloddyProvider = ({ children, client, options, apiKey, token }) => {
11
+ const value = (0, react_1.useMemo)(() => {
12
+ const activeClient = client || new client_1.KloddyClient({ ...options, apiKey, token });
13
+ return {
14
+ prompts: new prompts_1.Prompts({ posthog: activeClient }),
15
+ evaluations: new evaluations_1.Evaluations({ posthog: activeClient }),
16
+ };
17
+ }, [client, options, token]);
18
+ return (0, jsx_runtime_1.jsx)(KloddyContext.Provider, { value: value, children: children });
19
+ };
20
+ exports.KloddyProvider = KloddyProvider;
21
+ const usePrompt = () => {
22
+ const context = (0, react_1.useContext)(KloddyContext);
23
+ if (!context) {
24
+ throw new Error('usePrompt must be used within a KloddyProvider');
25
+ }
26
+ const { prompts, evaluations } = context;
27
+ return {
28
+ getPrompt: (id, options = {}) => prompts.get(id, options),
29
+ getAwnser: (id, options = {}) => prompts.execute(id, options), // Use user's spelling "getAwnser"
30
+ getEvaluation: (id, variables = {}) => evaluations.get(id, variables),
31
+ compile: (template, variables) => prompts.compile(template, variables),
32
+ };
33
+ };
34
+ exports.usePrompt = usePrompt;
35
+ //# sourceMappingURL=use-prompt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-prompt.js","sourceRoot":"","sources":["../../src/hooks/use-prompt.tsx"],"names":[],"mappings":";;;;AAAA,iCAAkE;AAClE,wCAAqC;AACrC,gDAA6C;AAC7C,sCAAyC;AAQzC,MAAM,aAAa,GAAG,IAAA,qBAAa,EAA2B,IAAI,CAAC,CAAC;AAU7D,MAAM,cAAc,GAAkC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE;IAC5G,MAAM,KAAK,GAAG,IAAA,eAAO,EAAC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AAC,QAAQ,EAAE,SAAS,CAAC;KACjF,CAAC;AACJ,CAAC,CAAC;AAdW,QAAA,SAAS,aAcpB"}
@@ -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 };