plp-client 1.0.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 +214 -0
- package/dist/index.d.mts +67 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.js +140 -0
- package/dist/index.mjs +114 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# @plp/client
|
|
2
|
+
|
|
3
|
+
[](https://github.com/gorealai/plp)
|
|
4
|
+
[](https://www.npmjs.com/package/@plp/client)
|
|
5
|
+
[](../../LICENSE)
|
|
6
|
+
|
|
7
|
+
Official JavaScript/TypeScript client for **PLP (Prompt Library Protocol)**.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @plp/client
|
|
13
|
+
# or
|
|
14
|
+
yarn add @plp/client
|
|
15
|
+
# or
|
|
16
|
+
pnpm add @plp/client
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { PLPClient } from '@plp/client';
|
|
23
|
+
|
|
24
|
+
const client = new PLPClient('https://prompts.goreal.ai', {
|
|
25
|
+
apiKey: 'your-api-key' // Optional
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Get latest version of a prompt
|
|
29
|
+
const prompt = await client.get('marketing/welcome-email');
|
|
30
|
+
console.log(prompt.content); // "Hello {{name}}..."
|
|
31
|
+
|
|
32
|
+
// Get specific version
|
|
33
|
+
const oldPrompt = await client.get('marketing/welcome-email', '1.0.0');
|
|
34
|
+
|
|
35
|
+
// Save a new prompt
|
|
36
|
+
await client.put('support/faq-bot', {
|
|
37
|
+
content: 'You are a helpful FAQ bot. Answer: {{question}}',
|
|
38
|
+
meta: {
|
|
39
|
+
version: '1.0.0',
|
|
40
|
+
author: 'yoad'
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Delete a prompt
|
|
45
|
+
await client.delete('deprecated/old-prompt');
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API Reference
|
|
49
|
+
|
|
50
|
+
### `PLPClient`
|
|
51
|
+
|
|
52
|
+
#### Constructor
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
new PLPClient(baseUrl: string, options?: PLPClientOptions)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Options:**
|
|
59
|
+
- `apiKey?: string` - Optional Bearer token for authentication
|
|
60
|
+
- `headers?: Record<string, string>` - Additional HTTP headers
|
|
61
|
+
- `timeout?: number` - Request timeout in milliseconds (default: 10000)
|
|
62
|
+
|
|
63
|
+
#### Methods
|
|
64
|
+
|
|
65
|
+
##### `get(promptId, version?)`
|
|
66
|
+
|
|
67
|
+
Retrieve a prompt by ID and optional version.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
async get(promptId: string, version?: string): Promise<PromptEnvelope>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
- **`promptId`**: Unique prompt identifier (e.g., `"marketing/welcome-email"`)
|
|
74
|
+
- **`version`**: Optional version string (e.g., `"1.2.0"`). If omitted, returns latest.
|
|
75
|
+
|
|
76
|
+
**Returns:** `PromptEnvelope`
|
|
77
|
+
|
|
78
|
+
**Throws:** `PLPError` if not found (404) or other errors
|
|
79
|
+
|
|
80
|
+
##### `put(promptId, input)`
|
|
81
|
+
|
|
82
|
+
Create or update a prompt (idempotent upsert).
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
async put(promptId: string, input: PromptInput): Promise<PromptEnvelope>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
- **`promptId`**: Unique prompt identifier
|
|
89
|
+
- **`input`**: Object with `content` (string, required) and `meta` (object, optional)
|
|
90
|
+
|
|
91
|
+
**Returns:** The saved `PromptEnvelope`
|
|
92
|
+
|
|
93
|
+
##### `delete(promptId)`
|
|
94
|
+
|
|
95
|
+
Delete a prompt and all its versions.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
async delete(promptId: string): Promise<void>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
- **`promptId`**: Unique prompt identifier
|
|
102
|
+
|
|
103
|
+
**Returns:** `void` (204 No Content on success)
|
|
104
|
+
|
|
105
|
+
##### Aliases
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
fetch(promptId, version?) // Alias for get()
|
|
109
|
+
save(promptId, input) // Alias for put()
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## TypeScript Types
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
interface PromptEnvelope {
|
|
116
|
+
id: string;
|
|
117
|
+
content: string;
|
|
118
|
+
meta: {
|
|
119
|
+
version?: string;
|
|
120
|
+
author?: string;
|
|
121
|
+
description?: string;
|
|
122
|
+
model_config?: Record<string, any>;
|
|
123
|
+
[key: string]: any;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
interface PromptInput {
|
|
128
|
+
content: string;
|
|
129
|
+
meta?: Record<string, any>;
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Error Handling
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import { PLPClient, PLPError } from '@plp/client';
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
const prompt = await client.get('missing/prompt');
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (error instanceof PLPError) {
|
|
142
|
+
console.error('Status:', error.statusCode);
|
|
143
|
+
console.error('Message:', error.message);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Examples
|
|
149
|
+
|
|
150
|
+
### Using with OpenAI
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import { PLPClient } from '@plp/client';
|
|
154
|
+
import OpenAI from 'openai';
|
|
155
|
+
|
|
156
|
+
const plp = new PLPClient('https://prompts.goreal.ai');
|
|
157
|
+
const openai = new OpenAI();
|
|
158
|
+
|
|
159
|
+
const prompt = await plp.get('assistant/code-reviewer');
|
|
160
|
+
|
|
161
|
+
const response = await openai.chat.completions.create({
|
|
162
|
+
model: 'gpt-4',
|
|
163
|
+
messages: [{ role: 'user', content: prompt.content }],
|
|
164
|
+
temperature: prompt.meta.model_config?.temperature || 0.7
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Using with Anthropic
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { PLPClient } from '@plp/client';
|
|
172
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
173
|
+
|
|
174
|
+
const plp = new PLPClient('https://prompts.goreal.ai');
|
|
175
|
+
const anthropic = new Anthropic();
|
|
176
|
+
|
|
177
|
+
const prompt = await plp.get('creative/story-generator');
|
|
178
|
+
|
|
179
|
+
const message = await anthropic.messages.create({
|
|
180
|
+
model: 'claude-3-opus-20240229',
|
|
181
|
+
max_tokens: prompt.meta.model_config?.max_tokens || 1024,
|
|
182
|
+
messages: [{ role: 'user', content: prompt.content }]
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Development
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
# Install dependencies
|
|
190
|
+
npm install
|
|
191
|
+
|
|
192
|
+
# Build
|
|
193
|
+
npm run build
|
|
194
|
+
|
|
195
|
+
# Test
|
|
196
|
+
npm test
|
|
197
|
+
|
|
198
|
+
# Lint
|
|
199
|
+
npm run lint
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
MIT © [GoReal.AI](https://goreal.ai)
|
|
205
|
+
|
|
206
|
+
## Contributing
|
|
207
|
+
|
|
208
|
+
See [CONTRIBUTING.md](../../CONTRIBUTING.md)
|
|
209
|
+
|
|
210
|
+
## Learn More
|
|
211
|
+
|
|
212
|
+
- [PLP Specification](../../spec/SPEC.md)
|
|
213
|
+
- [OpenAPI Documentation](../../spec/openapi.yaml)
|
|
214
|
+
- [GitHub Repository](https://github.com/gorealai/plp)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* plp-client - Official JavaScript/TypeScript client for PLP (Prompt Library Protocol)
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
interface PromptEnvelope {
|
|
6
|
+
id: string;
|
|
7
|
+
content: string;
|
|
8
|
+
meta: {
|
|
9
|
+
version?: string;
|
|
10
|
+
author?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
model_config?: Record<string, unknown>;
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
interface PromptInput {
|
|
17
|
+
content: string;
|
|
18
|
+
meta?: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
interface PLPClientOptions {
|
|
21
|
+
apiKey?: string;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
timeout?: number;
|
|
24
|
+
}
|
|
25
|
+
declare class PLPError extends Error {
|
|
26
|
+
statusCode?: number | undefined;
|
|
27
|
+
response?: unknown | undefined;
|
|
28
|
+
constructor(message: string, statusCode?: number | undefined, response?: unknown | undefined);
|
|
29
|
+
}
|
|
30
|
+
declare class PLPClient {
|
|
31
|
+
private baseUrl;
|
|
32
|
+
private apiKey?;
|
|
33
|
+
private headers;
|
|
34
|
+
private timeout;
|
|
35
|
+
constructor(baseUrl: string, options?: PLPClientOptions);
|
|
36
|
+
private request;
|
|
37
|
+
/**
|
|
38
|
+
* Retrieve a prompt by ID and optional version
|
|
39
|
+
* @param promptId - The unique prompt identifier
|
|
40
|
+
* @param version - Optional version string (e.g., "1.2.0"). If omitted, returns latest.
|
|
41
|
+
* @returns The prompt envelope
|
|
42
|
+
*/
|
|
43
|
+
get(promptId: string, version?: string): Promise<PromptEnvelope>;
|
|
44
|
+
/**
|
|
45
|
+
* Create or update a prompt (idempotent upsert)
|
|
46
|
+
* @param promptId - The unique prompt identifier
|
|
47
|
+
* @param input - The prompt content and metadata
|
|
48
|
+
* @returns The saved prompt envelope
|
|
49
|
+
*/
|
|
50
|
+
put(promptId: string, input: PromptInput): Promise<PromptEnvelope>;
|
|
51
|
+
/**
|
|
52
|
+
* Delete a prompt and all its versions
|
|
53
|
+
* @param promptId - The unique prompt identifier
|
|
54
|
+
* @returns void (204 No Content on success)
|
|
55
|
+
*/
|
|
56
|
+
delete(promptId: string): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Alias for get() - more intuitive naming
|
|
59
|
+
*/
|
|
60
|
+
fetch(promptId: string, version?: string): Promise<PromptEnvelope>;
|
|
61
|
+
/**
|
|
62
|
+
* Alias for put() - more intuitive naming
|
|
63
|
+
*/
|
|
64
|
+
save(promptId: string, input: PromptInput): Promise<PromptEnvelope>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { PLPClient, type PLPClientOptions, PLPError, type PromptEnvelope, type PromptInput, PLPClient as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* plp-client - Official JavaScript/TypeScript client for PLP (Prompt Library Protocol)
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
interface PromptEnvelope {
|
|
6
|
+
id: string;
|
|
7
|
+
content: string;
|
|
8
|
+
meta: {
|
|
9
|
+
version?: string;
|
|
10
|
+
author?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
model_config?: Record<string, unknown>;
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
interface PromptInput {
|
|
17
|
+
content: string;
|
|
18
|
+
meta?: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
interface PLPClientOptions {
|
|
21
|
+
apiKey?: string;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
timeout?: number;
|
|
24
|
+
}
|
|
25
|
+
declare class PLPError extends Error {
|
|
26
|
+
statusCode?: number | undefined;
|
|
27
|
+
response?: unknown | undefined;
|
|
28
|
+
constructor(message: string, statusCode?: number | undefined, response?: unknown | undefined);
|
|
29
|
+
}
|
|
30
|
+
declare class PLPClient {
|
|
31
|
+
private baseUrl;
|
|
32
|
+
private apiKey?;
|
|
33
|
+
private headers;
|
|
34
|
+
private timeout;
|
|
35
|
+
constructor(baseUrl: string, options?: PLPClientOptions);
|
|
36
|
+
private request;
|
|
37
|
+
/**
|
|
38
|
+
* Retrieve a prompt by ID and optional version
|
|
39
|
+
* @param promptId - The unique prompt identifier
|
|
40
|
+
* @param version - Optional version string (e.g., "1.2.0"). If omitted, returns latest.
|
|
41
|
+
* @returns The prompt envelope
|
|
42
|
+
*/
|
|
43
|
+
get(promptId: string, version?: string): Promise<PromptEnvelope>;
|
|
44
|
+
/**
|
|
45
|
+
* Create or update a prompt (idempotent upsert)
|
|
46
|
+
* @param promptId - The unique prompt identifier
|
|
47
|
+
* @param input - The prompt content and metadata
|
|
48
|
+
* @returns The saved prompt envelope
|
|
49
|
+
*/
|
|
50
|
+
put(promptId: string, input: PromptInput): Promise<PromptEnvelope>;
|
|
51
|
+
/**
|
|
52
|
+
* Delete a prompt and all its versions
|
|
53
|
+
* @param promptId - The unique prompt identifier
|
|
54
|
+
* @returns void (204 No Content on success)
|
|
55
|
+
*/
|
|
56
|
+
delete(promptId: string): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Alias for get() - more intuitive naming
|
|
59
|
+
*/
|
|
60
|
+
fetch(promptId: string, version?: string): Promise<PromptEnvelope>;
|
|
61
|
+
/**
|
|
62
|
+
* Alias for put() - more intuitive naming
|
|
63
|
+
*/
|
|
64
|
+
save(promptId: string, input: PromptInput): Promise<PromptEnvelope>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { PLPClient, type PLPClientOptions, PLPError, type PromptEnvelope, type PromptInput, PLPClient as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
PLPClient: () => PLPClient,
|
|
24
|
+
PLPError: () => PLPError,
|
|
25
|
+
default: () => index_default
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var PLPError = class extends Error {
|
|
29
|
+
constructor(message, statusCode, response) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.statusCode = statusCode;
|
|
32
|
+
this.response = response;
|
|
33
|
+
this.name = "PLPError";
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var PLPClient = class {
|
|
37
|
+
constructor(baseUrl, options = {}) {
|
|
38
|
+
this.baseUrl = baseUrl.replace(/\/$/, "");
|
|
39
|
+
this.apiKey = options.apiKey;
|
|
40
|
+
this.headers = options.headers || {};
|
|
41
|
+
this.timeout = options.timeout || 1e4;
|
|
42
|
+
}
|
|
43
|
+
async request(method, path, body) {
|
|
44
|
+
const url = `${this.baseUrl}${path}`;
|
|
45
|
+
const headers = {
|
|
46
|
+
"Content-Type": "application/json",
|
|
47
|
+
...this.headers
|
|
48
|
+
};
|
|
49
|
+
if (this.apiKey) {
|
|
50
|
+
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
51
|
+
}
|
|
52
|
+
const controller = new AbortController();
|
|
53
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
54
|
+
try {
|
|
55
|
+
const response = await fetch(url, {
|
|
56
|
+
method,
|
|
57
|
+
headers,
|
|
58
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
59
|
+
signal: controller.signal
|
|
60
|
+
});
|
|
61
|
+
clearTimeout(timeoutId);
|
|
62
|
+
if (response.status === 204) {
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
const data = await response.json();
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
const errorData = data;
|
|
68
|
+
throw new PLPError(
|
|
69
|
+
errorData.error || `HTTP ${response.status}`,
|
|
70
|
+
response.status,
|
|
71
|
+
data
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return data;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
clearTimeout(timeoutId);
|
|
77
|
+
if (error instanceof PLPError) {
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
if (error instanceof Error) {
|
|
81
|
+
if (error.name === "AbortError") {
|
|
82
|
+
throw new PLPError(`Request timeout after ${this.timeout}ms`);
|
|
83
|
+
}
|
|
84
|
+
throw new PLPError(`Network error: ${error.message}`);
|
|
85
|
+
}
|
|
86
|
+
throw new PLPError("Unknown error occurred");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Retrieve a prompt by ID and optional version
|
|
91
|
+
* @param promptId - The unique prompt identifier
|
|
92
|
+
* @param version - Optional version string (e.g., "1.2.0"). If omitted, returns latest.
|
|
93
|
+
* @returns The prompt envelope
|
|
94
|
+
*/
|
|
95
|
+
async get(promptId, version) {
|
|
96
|
+
const path = version ? `/v1/prompts/${promptId}/${version}` : `/v1/prompts/${promptId}`;
|
|
97
|
+
return this.request("GET", path);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Create or update a prompt (idempotent upsert)
|
|
101
|
+
* @param promptId - The unique prompt identifier
|
|
102
|
+
* @param input - The prompt content and metadata
|
|
103
|
+
* @returns The saved prompt envelope
|
|
104
|
+
*/
|
|
105
|
+
async put(promptId, input) {
|
|
106
|
+
const path = `/v1/prompts/${promptId}`;
|
|
107
|
+
return this.request("PUT", path, input);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Delete a prompt and all its versions
|
|
111
|
+
* @param promptId - The unique prompt identifier
|
|
112
|
+
* @returns void (204 No Content on success)
|
|
113
|
+
*/
|
|
114
|
+
async delete(promptId) {
|
|
115
|
+
const path = `/v1/prompts/${promptId}`;
|
|
116
|
+
return this.request("DELETE", path);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Alias for get() - more intuitive naming
|
|
120
|
+
*/
|
|
121
|
+
async fetch(promptId, version) {
|
|
122
|
+
return this.get(promptId, version);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Alias for put() - more intuitive naming
|
|
126
|
+
*/
|
|
127
|
+
async save(promptId, input) {
|
|
128
|
+
return this.put(promptId, input);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var index_default = PLPClient;
|
|
132
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
133
|
+
0 && (module.exports = {
|
|
134
|
+
PLPClient,
|
|
135
|
+
PLPError
|
|
136
|
+
});
|
|
137
|
+
/**
|
|
138
|
+
* plp-client - Official JavaScript/TypeScript client for PLP (Prompt Library Protocol)
|
|
139
|
+
* @license MIT
|
|
140
|
+
*/
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var PLPError = class extends Error {
|
|
3
|
+
constructor(message, statusCode, response) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.statusCode = statusCode;
|
|
6
|
+
this.response = response;
|
|
7
|
+
this.name = "PLPError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var PLPClient = class {
|
|
11
|
+
constructor(baseUrl, options = {}) {
|
|
12
|
+
this.baseUrl = baseUrl.replace(/\/$/, "");
|
|
13
|
+
this.apiKey = options.apiKey;
|
|
14
|
+
this.headers = options.headers || {};
|
|
15
|
+
this.timeout = options.timeout || 1e4;
|
|
16
|
+
}
|
|
17
|
+
async request(method, path, body) {
|
|
18
|
+
const url = `${this.baseUrl}${path}`;
|
|
19
|
+
const headers = {
|
|
20
|
+
"Content-Type": "application/json",
|
|
21
|
+
...this.headers
|
|
22
|
+
};
|
|
23
|
+
if (this.apiKey) {
|
|
24
|
+
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
25
|
+
}
|
|
26
|
+
const controller = new AbortController();
|
|
27
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
28
|
+
try {
|
|
29
|
+
const response = await fetch(url, {
|
|
30
|
+
method,
|
|
31
|
+
headers,
|
|
32
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
33
|
+
signal: controller.signal
|
|
34
|
+
});
|
|
35
|
+
clearTimeout(timeoutId);
|
|
36
|
+
if (response.status === 204) {
|
|
37
|
+
return void 0;
|
|
38
|
+
}
|
|
39
|
+
const data = await response.json();
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
const errorData = data;
|
|
42
|
+
throw new PLPError(
|
|
43
|
+
errorData.error || `HTTP ${response.status}`,
|
|
44
|
+
response.status,
|
|
45
|
+
data
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return data;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
clearTimeout(timeoutId);
|
|
51
|
+
if (error instanceof PLPError) {
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
if (error.name === "AbortError") {
|
|
56
|
+
throw new PLPError(`Request timeout after ${this.timeout}ms`);
|
|
57
|
+
}
|
|
58
|
+
throw new PLPError(`Network error: ${error.message}`);
|
|
59
|
+
}
|
|
60
|
+
throw new PLPError("Unknown error occurred");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Retrieve a prompt by ID and optional version
|
|
65
|
+
* @param promptId - The unique prompt identifier
|
|
66
|
+
* @param version - Optional version string (e.g., "1.2.0"). If omitted, returns latest.
|
|
67
|
+
* @returns The prompt envelope
|
|
68
|
+
*/
|
|
69
|
+
async get(promptId, version) {
|
|
70
|
+
const path = version ? `/v1/prompts/${promptId}/${version}` : `/v1/prompts/${promptId}`;
|
|
71
|
+
return this.request("GET", path);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create or update a prompt (idempotent upsert)
|
|
75
|
+
* @param promptId - The unique prompt identifier
|
|
76
|
+
* @param input - The prompt content and metadata
|
|
77
|
+
* @returns The saved prompt envelope
|
|
78
|
+
*/
|
|
79
|
+
async put(promptId, input) {
|
|
80
|
+
const path = `/v1/prompts/${promptId}`;
|
|
81
|
+
return this.request("PUT", path, input);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Delete a prompt and all its versions
|
|
85
|
+
* @param promptId - The unique prompt identifier
|
|
86
|
+
* @returns void (204 No Content on success)
|
|
87
|
+
*/
|
|
88
|
+
async delete(promptId) {
|
|
89
|
+
const path = `/v1/prompts/${promptId}`;
|
|
90
|
+
return this.request("DELETE", path);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Alias for get() - more intuitive naming
|
|
94
|
+
*/
|
|
95
|
+
async fetch(promptId, version) {
|
|
96
|
+
return this.get(promptId, version);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Alias for put() - more intuitive naming
|
|
100
|
+
*/
|
|
101
|
+
async save(promptId, input) {
|
|
102
|
+
return this.put(promptId, input);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
var index_default = PLPClient;
|
|
106
|
+
export {
|
|
107
|
+
PLPClient,
|
|
108
|
+
PLPError,
|
|
109
|
+
index_default as default
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* plp-client - Official JavaScript/TypeScript client for PLP (Prompt Library Protocol)
|
|
113
|
+
* @license MIT
|
|
114
|
+
*/
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "plp-client",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official JavaScript/TypeScript client for PLP (Prompt Library Protocol)",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"test:watch": "vitest",
|
|
23
|
+
"lint": "eslint src --ext .ts",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"plp",
|
|
28
|
+
"prompt",
|
|
29
|
+
"library",
|
|
30
|
+
"protocol",
|
|
31
|
+
"ai",
|
|
32
|
+
"llm",
|
|
33
|
+
"openai",
|
|
34
|
+
"anthropic",
|
|
35
|
+
"prompt-management"
|
|
36
|
+
],
|
|
37
|
+
"author": "GoReal.AI",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/gorealai/plp.git",
|
|
42
|
+
"directory": "sdks/js"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/gorealai/plp/issues"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/gorealai/plp#readme",
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^20.10.0",
|
|
50
|
+
"@typescript-eslint/eslint-plugin": "^6.13.0",
|
|
51
|
+
"@typescript-eslint/parser": "^6.13.0",
|
|
52
|
+
"eslint": "^8.54.0",
|
|
53
|
+
"tsup": "^8.0.1",
|
|
54
|
+
"typescript": "^5.3.3",
|
|
55
|
+
"vitest": "^1.0.4"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=16"
|
|
59
|
+
}
|
|
60
|
+
}
|