promptwallet-sdk 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 +94 -0
- package/dist/chunk-IKUCY76J.mjs +55 -0
- package/dist/client-Q6AFL5TB.mjs +10 -0
- package/dist/index.d.mts +258 -0
- package/dist/index.d.ts +258 -0
- package/dist/index.js +283 -0
- package/dist/index.mjs +191 -0
- package/package.json +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# @promptwallet/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for PromptWallet — chat, artifacts, and workspace indexing.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @promptwallet/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { PromptWallet } from '@promptwallet/sdk';
|
|
15
|
+
|
|
16
|
+
const pw = new PromptWallet({
|
|
17
|
+
baseUrl: 'http://localhost:8080',
|
|
18
|
+
apiKey: process.env.PROMPTWALLET_API_KEY,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// Chat with RAG context
|
|
22
|
+
const response = await pw.chat({
|
|
23
|
+
model: 'llama-3.1-70b-versatile',
|
|
24
|
+
messages: [{ role: 'user', content: 'How does auth work?' }],
|
|
25
|
+
rag_namespaces: ['codebase', 'chats'],
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Stream chat
|
|
29
|
+
for await (const chunk of pw.chatStream({
|
|
30
|
+
model: 'llama-3.1-70b-versatile',
|
|
31
|
+
messages: [{ role: 'user', content: 'Explain the login flow' }],
|
|
32
|
+
rag_namespaces: ['codebase'],
|
|
33
|
+
})) {
|
|
34
|
+
if (chunk.content) process.stdout.write(chunk.content);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Store artifact
|
|
38
|
+
await pw.artifacts.store({
|
|
39
|
+
type: 'component_tree',
|
|
40
|
+
title: 'Dashboard',
|
|
41
|
+
content: JSON.stringify({ components: [...] }),
|
|
42
|
+
namespace: 'mastercanvas:proj1',
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Search artifacts
|
|
46
|
+
const results = await pw.artifacts.search({
|
|
47
|
+
query: 'button component',
|
|
48
|
+
artifact_type: 'component_tree',
|
|
49
|
+
limit: 5,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Workspace: preview → index → poll status
|
|
53
|
+
const preview = await pw.workspace.preview({ path: '/projects/my-app' });
|
|
54
|
+
const { job_id } = await pw.workspace.index({ path: '/projects/my-app' });
|
|
55
|
+
const { jobs } = await pw.workspace.status(job_id);
|
|
56
|
+
|
|
57
|
+
// Search indexed code
|
|
58
|
+
const codeResults = await pw.workspace.search({ q: 'auth middleware', limit: 10 });
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API
|
|
62
|
+
|
|
63
|
+
- **chat(options)** — Chat with RAG context injection
|
|
64
|
+
- **chatStream(options)** — Async generator for streaming
|
|
65
|
+
- **artifacts.store(options)** — Store artifact (document, code_snippet, component_tree, etc.)
|
|
66
|
+
- **artifacts.search(options)** — Semantic search
|
|
67
|
+
- **artifacts.get(id)** — Get single artifact
|
|
68
|
+
- **artifacts.getFull(id)** — Get all chunks
|
|
69
|
+
- **artifacts.delete(id)** — Delete artifact
|
|
70
|
+
- **workspace.preview(options)** — Estimate indexing
|
|
71
|
+
- **workspace.index(options)** — Start indexing (returns job_id)
|
|
72
|
+
- **workspace.status(jobId?)** — Poll index status
|
|
73
|
+
- **workspace.search(options)** — Search indexed code
|
|
74
|
+
- **workspace.deleteFiles(options)** — Remove files from index
|
|
75
|
+
|
|
76
|
+
## RAG Namespaces
|
|
77
|
+
|
|
78
|
+
Use `rag_namespaces` in chat to filter context:
|
|
79
|
+
|
|
80
|
+
| Namespace | Artifact type |
|
|
81
|
+
|-----------|---------------|
|
|
82
|
+
| `codebase`, `code` | CodeSnippet |
|
|
83
|
+
| `chats`, `chat` | ChatResponse |
|
|
84
|
+
| `docs`, `document` | Document |
|
|
85
|
+
| `mastercanvas`, `component_tree` | ComponentTree |
|
|
86
|
+
| `*` or omit | All types |
|
|
87
|
+
|
|
88
|
+
## Development
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
cd packages/sdk
|
|
92
|
+
npm install
|
|
93
|
+
npm run build
|
|
94
|
+
```
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
var PromptWalletError = class extends Error {
|
|
3
|
+
constructor(message, status, body) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.status = status;
|
|
6
|
+
this.body = body;
|
|
7
|
+
this.name = "PromptWalletError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
async function createHeaders(config) {
|
|
11
|
+
const headers = {
|
|
12
|
+
"Content-Type": "application/json"
|
|
13
|
+
};
|
|
14
|
+
let token = null;
|
|
15
|
+
if (config.apiKey) {
|
|
16
|
+
token = config.apiKey;
|
|
17
|
+
} else if (config.getToken) {
|
|
18
|
+
token = await config.getToken();
|
|
19
|
+
}
|
|
20
|
+
if (token) {
|
|
21
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
22
|
+
}
|
|
23
|
+
return headers;
|
|
24
|
+
}
|
|
25
|
+
async function fetchApi(config, path, init = {}) {
|
|
26
|
+
const base = (config.baseUrl ?? "https://api.promptwallet.dev").replace(/\/$/, "");
|
|
27
|
+
const url = `${base}${path.startsWith("/") ? path : `/${path}`}`;
|
|
28
|
+
const headers = await createHeaders(config);
|
|
29
|
+
const res = await fetch(url, {
|
|
30
|
+
...init,
|
|
31
|
+
headers: { ...headers, ...init.headers }
|
|
32
|
+
});
|
|
33
|
+
if (!res.ok) {
|
|
34
|
+
let body;
|
|
35
|
+
try {
|
|
36
|
+
body = await res.json();
|
|
37
|
+
} catch {
|
|
38
|
+
body = await res.text();
|
|
39
|
+
}
|
|
40
|
+
throw new PromptWalletError(
|
|
41
|
+
`PromptWallet API error: ${res.status} ${res.statusText}`,
|
|
42
|
+
res.status,
|
|
43
|
+
body
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const text = await res.text();
|
|
47
|
+
if (!text) return {};
|
|
48
|
+
return JSON.parse(text);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export {
|
|
52
|
+
PromptWalletError,
|
|
53
|
+
createHeaders,
|
|
54
|
+
fetchApi
|
|
55
|
+
};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base HTTP client for PromptWallet API
|
|
3
|
+
*/
|
|
4
|
+
type PromptWalletConfig = {
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
apiKey?: string;
|
|
7
|
+
/** Async function to get auth token (e.g. Clerk session) */
|
|
8
|
+
getToken?: () => Promise<string | null>;
|
|
9
|
+
};
|
|
10
|
+
declare class PromptWalletError extends Error {
|
|
11
|
+
status?: number | undefined;
|
|
12
|
+
body?: unknown | undefined;
|
|
13
|
+
constructor(message: string, status?: number | undefined, body?: unknown | undefined);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Chat completions - OpenAI-compatible with RAG
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
type ChatMessage$1 = {
|
|
21
|
+
role: "system" | "user" | "assistant";
|
|
22
|
+
content: string | Array<{
|
|
23
|
+
type: "text";
|
|
24
|
+
text: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "image_url";
|
|
27
|
+
image_url: {
|
|
28
|
+
url: string;
|
|
29
|
+
};
|
|
30
|
+
}>;
|
|
31
|
+
};
|
|
32
|
+
type ChatOptions$1 = {
|
|
33
|
+
model: string;
|
|
34
|
+
messages: ChatMessage$1[];
|
|
35
|
+
temperature?: number;
|
|
36
|
+
max_tokens?: number;
|
|
37
|
+
stream?: boolean;
|
|
38
|
+
/** RAG namespaces: codebase, chats, docs, mastercanvas, etc. */
|
|
39
|
+
rag_namespaces?: string[];
|
|
40
|
+
/** Max tokens for injected context */
|
|
41
|
+
max_context_tokens?: number;
|
|
42
|
+
};
|
|
43
|
+
type ChatResponse$1 = {
|
|
44
|
+
id: string;
|
|
45
|
+
model: string;
|
|
46
|
+
choices: Array<{
|
|
47
|
+
message: {
|
|
48
|
+
role: string;
|
|
49
|
+
content: string | null;
|
|
50
|
+
};
|
|
51
|
+
finish_reason: string;
|
|
52
|
+
}>;
|
|
53
|
+
usage?: {
|
|
54
|
+
prompt_tokens: number;
|
|
55
|
+
completion_tokens: number;
|
|
56
|
+
total_tokens: number;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Artifact storage and search
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
type StoreArtifactOptions$1 = {
|
|
65
|
+
type: string;
|
|
66
|
+
title?: string;
|
|
67
|
+
content: string;
|
|
68
|
+
namespace?: string;
|
|
69
|
+
metadata?: Record<string, unknown>;
|
|
70
|
+
};
|
|
71
|
+
type StoreArtifactResponse$1 = {
|
|
72
|
+
ids: string[];
|
|
73
|
+
chunks: number;
|
|
74
|
+
message: string;
|
|
75
|
+
};
|
|
76
|
+
type SearchResult$1 = {
|
|
77
|
+
id: string;
|
|
78
|
+
artifact_type: string;
|
|
79
|
+
title: string | null;
|
|
80
|
+
content: string;
|
|
81
|
+
similarity: number;
|
|
82
|
+
highlights: string[];
|
|
83
|
+
metadata: Record<string, unknown>;
|
|
84
|
+
created_at: string;
|
|
85
|
+
};
|
|
86
|
+
type SearchArtifactsOptions$1 = {
|
|
87
|
+
query: string;
|
|
88
|
+
namespaces?: string[];
|
|
89
|
+
limit?: number;
|
|
90
|
+
minScore?: number;
|
|
91
|
+
/** Filter by artifact type (e.g. document, code_snippet, component_tree) */
|
|
92
|
+
artifact_type?: string;
|
|
93
|
+
};
|
|
94
|
+
type SearchArtifactsResponse$1 = {
|
|
95
|
+
results: SearchResult$1[];
|
|
96
|
+
total: number;
|
|
97
|
+
query: string;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Workspace indexing and search
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
type WorkspacePreviewResponse$1 = {
|
|
105
|
+
file_count: number;
|
|
106
|
+
total_size_bytes: number;
|
|
107
|
+
estimated_chunks: number;
|
|
108
|
+
estimated_seconds: number;
|
|
109
|
+
};
|
|
110
|
+
type IndexWorkspaceOptions$1 = {
|
|
111
|
+
path: string;
|
|
112
|
+
include_patterns?: string[];
|
|
113
|
+
exclude_patterns?: string[];
|
|
114
|
+
watch?: boolean;
|
|
115
|
+
};
|
|
116
|
+
type IndexWorkspaceResponse$1 = {
|
|
117
|
+
job_id: string;
|
|
118
|
+
path: string;
|
|
119
|
+
workspace_id: string;
|
|
120
|
+
status: IndexStatus$1;
|
|
121
|
+
};
|
|
122
|
+
type IndexStatus$1 = {
|
|
123
|
+
state: "idle" | "indexing" | "error";
|
|
124
|
+
total_files?: number;
|
|
125
|
+
indexed_files?: number;
|
|
126
|
+
total_chunks?: number;
|
|
127
|
+
error?: string;
|
|
128
|
+
};
|
|
129
|
+
type WorkspaceSearchResult$1 = {
|
|
130
|
+
path: string;
|
|
131
|
+
language: string | null;
|
|
132
|
+
content: string;
|
|
133
|
+
chunk_index: number;
|
|
134
|
+
total_chunks: number;
|
|
135
|
+
score: number;
|
|
136
|
+
workspace_path?: string;
|
|
137
|
+
git_url?: string;
|
|
138
|
+
repo_name?: string;
|
|
139
|
+
};
|
|
140
|
+
type WorkspaceSearchResponse$1 = {
|
|
141
|
+
results: WorkspaceSearchResult$1[];
|
|
142
|
+
total: number;
|
|
143
|
+
query: string;
|
|
144
|
+
};
|
|
145
|
+
type DeleteWorkspaceFilesOptions = {
|
|
146
|
+
workspace_path: string;
|
|
147
|
+
path?: string;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @promptwallet/sdk - PromptWallet TypeScript SDK
|
|
152
|
+
*
|
|
153
|
+
* Chat, artifacts, and workspace for MasterCanvas, OpenClaw, and custom apps.
|
|
154
|
+
*/
|
|
155
|
+
|
|
156
|
+
type ChatMessage = ChatMessage$1;
|
|
157
|
+
type ChatOptions = ChatOptions$1;
|
|
158
|
+
type ChatResponse = ChatResponse$1;
|
|
159
|
+
type StoreArtifactOptions = StoreArtifactOptions$1;
|
|
160
|
+
type StoreArtifactResponse = StoreArtifactResponse$1;
|
|
161
|
+
type SearchResult = SearchResult$1;
|
|
162
|
+
type SearchArtifactsOptions = SearchArtifactsOptions$1;
|
|
163
|
+
type SearchArtifactsResponse = SearchArtifactsResponse$1;
|
|
164
|
+
type WorkspacePreviewResponse = WorkspacePreviewResponse$1;
|
|
165
|
+
type IndexWorkspaceOptions = IndexWorkspaceOptions$1;
|
|
166
|
+
type IndexWorkspaceResponse = IndexWorkspaceResponse$1;
|
|
167
|
+
type IndexStatus = IndexStatus$1;
|
|
168
|
+
type WorkspaceSearchResult = WorkspaceSearchResult$1;
|
|
169
|
+
type WorkspaceSearchResponse = WorkspaceSearchResponse$1;
|
|
170
|
+
/**
|
|
171
|
+
* PromptWallet client for chat, artifacts, and workspace.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { PromptWallet } from '@promptwallet/sdk';
|
|
176
|
+
*
|
|
177
|
+
* const pw = new PromptWallet({
|
|
178
|
+
* baseUrl: 'http://localhost:8080',
|
|
179
|
+
* apiKey: process.env.PROMPTWALLET_API_KEY,
|
|
180
|
+
* });
|
|
181
|
+
*
|
|
182
|
+
* const response = await pw.chat({
|
|
183
|
+
* model: 'llama-3.1-70b-versatile',
|
|
184
|
+
* messages: [{ role: 'user', content: 'How does auth work?' }],
|
|
185
|
+
* rag_namespaces: ['codebase', 'chats'],
|
|
186
|
+
* });
|
|
187
|
+
*
|
|
188
|
+
* const results = await pw.artifacts.search({
|
|
189
|
+
* query: 'login flow',
|
|
190
|
+
* namespaces: ['codebase'],
|
|
191
|
+
* limit: 5,
|
|
192
|
+
* });
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
declare class PromptWallet {
|
|
196
|
+
private config;
|
|
197
|
+
constructor(config: PromptWalletConfig);
|
|
198
|
+
/** Chat with RAG context injection */
|
|
199
|
+
chat(options: ChatOptions$1): Promise<ChatResponse$1>;
|
|
200
|
+
/** Stream chat response */
|
|
201
|
+
chatStream(options: Omit<ChatOptions$1, "stream">): AsyncGenerator<{
|
|
202
|
+
content?: string;
|
|
203
|
+
done?: boolean;
|
|
204
|
+
}>;
|
|
205
|
+
/** Artifact operations */
|
|
206
|
+
artifacts: {
|
|
207
|
+
store: (options: StoreArtifactOptions$1) => Promise<StoreArtifactResponse$1>;
|
|
208
|
+
search: (options: SearchArtifactsOptions$1) => Promise<SearchArtifactsResponse$1>;
|
|
209
|
+
get: (id: string) => Promise<{
|
|
210
|
+
id: string;
|
|
211
|
+
artifact_type: string;
|
|
212
|
+
title: string | null;
|
|
213
|
+
content: string;
|
|
214
|
+
metadata: Record<string, unknown>;
|
|
215
|
+
chunk_index: number;
|
|
216
|
+
total_chunks: number;
|
|
217
|
+
created_at: string;
|
|
218
|
+
}>;
|
|
219
|
+
getFull: (id: string) => Promise<{
|
|
220
|
+
id: string;
|
|
221
|
+
content: string;
|
|
222
|
+
chunk_index: number;
|
|
223
|
+
total_chunks: number;
|
|
224
|
+
}[]>;
|
|
225
|
+
delete: (id: string) => Promise<{
|
|
226
|
+
success: boolean;
|
|
227
|
+
message: string;
|
|
228
|
+
}>;
|
|
229
|
+
};
|
|
230
|
+
/** Workspace indexing and search */
|
|
231
|
+
workspace: {
|
|
232
|
+
preview: (options: {
|
|
233
|
+
path: string;
|
|
234
|
+
}) => Promise<WorkspacePreviewResponse$1>;
|
|
235
|
+
index: (options: IndexWorkspaceOptions$1) => Promise<IndexWorkspaceResponse$1>;
|
|
236
|
+
status: (jobId?: string) => Promise<{
|
|
237
|
+
jobs: Array<{
|
|
238
|
+
job_id: string;
|
|
239
|
+
path: string;
|
|
240
|
+
workspace_id: string;
|
|
241
|
+
status: IndexStatus$1;
|
|
242
|
+
created_at: string;
|
|
243
|
+
}>;
|
|
244
|
+
}>;
|
|
245
|
+
search: (options: {
|
|
246
|
+
q: string;
|
|
247
|
+
limit?: number;
|
|
248
|
+
language?: string;
|
|
249
|
+
path?: string;
|
|
250
|
+
}) => Promise<WorkspaceSearchResponse$1>;
|
|
251
|
+
deleteFiles: (options: DeleteWorkspaceFilesOptions) => Promise<{
|
|
252
|
+
deleted_count: number;
|
|
253
|
+
message: string;
|
|
254
|
+
}>;
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export { type ChatMessage, type ChatOptions, type ChatResponse, type IndexStatus, type IndexWorkspaceOptions, type IndexWorkspaceResponse, PromptWallet, type PromptWalletConfig, PromptWalletError, type SearchArtifactsOptions, type SearchArtifactsResponse, type SearchResult, type StoreArtifactOptions, type StoreArtifactResponse, type WorkspacePreviewResponse, type WorkspaceSearchResponse, type WorkspaceSearchResult };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base HTTP client for PromptWallet API
|
|
3
|
+
*/
|
|
4
|
+
type PromptWalletConfig = {
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
apiKey?: string;
|
|
7
|
+
/** Async function to get auth token (e.g. Clerk session) */
|
|
8
|
+
getToken?: () => Promise<string | null>;
|
|
9
|
+
};
|
|
10
|
+
declare class PromptWalletError extends Error {
|
|
11
|
+
status?: number | undefined;
|
|
12
|
+
body?: unknown | undefined;
|
|
13
|
+
constructor(message: string, status?: number | undefined, body?: unknown | undefined);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Chat completions - OpenAI-compatible with RAG
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
type ChatMessage$1 = {
|
|
21
|
+
role: "system" | "user" | "assistant";
|
|
22
|
+
content: string | Array<{
|
|
23
|
+
type: "text";
|
|
24
|
+
text: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "image_url";
|
|
27
|
+
image_url: {
|
|
28
|
+
url: string;
|
|
29
|
+
};
|
|
30
|
+
}>;
|
|
31
|
+
};
|
|
32
|
+
type ChatOptions$1 = {
|
|
33
|
+
model: string;
|
|
34
|
+
messages: ChatMessage$1[];
|
|
35
|
+
temperature?: number;
|
|
36
|
+
max_tokens?: number;
|
|
37
|
+
stream?: boolean;
|
|
38
|
+
/** RAG namespaces: codebase, chats, docs, mastercanvas, etc. */
|
|
39
|
+
rag_namespaces?: string[];
|
|
40
|
+
/** Max tokens for injected context */
|
|
41
|
+
max_context_tokens?: number;
|
|
42
|
+
};
|
|
43
|
+
type ChatResponse$1 = {
|
|
44
|
+
id: string;
|
|
45
|
+
model: string;
|
|
46
|
+
choices: Array<{
|
|
47
|
+
message: {
|
|
48
|
+
role: string;
|
|
49
|
+
content: string | null;
|
|
50
|
+
};
|
|
51
|
+
finish_reason: string;
|
|
52
|
+
}>;
|
|
53
|
+
usage?: {
|
|
54
|
+
prompt_tokens: number;
|
|
55
|
+
completion_tokens: number;
|
|
56
|
+
total_tokens: number;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Artifact storage and search
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
type StoreArtifactOptions$1 = {
|
|
65
|
+
type: string;
|
|
66
|
+
title?: string;
|
|
67
|
+
content: string;
|
|
68
|
+
namespace?: string;
|
|
69
|
+
metadata?: Record<string, unknown>;
|
|
70
|
+
};
|
|
71
|
+
type StoreArtifactResponse$1 = {
|
|
72
|
+
ids: string[];
|
|
73
|
+
chunks: number;
|
|
74
|
+
message: string;
|
|
75
|
+
};
|
|
76
|
+
type SearchResult$1 = {
|
|
77
|
+
id: string;
|
|
78
|
+
artifact_type: string;
|
|
79
|
+
title: string | null;
|
|
80
|
+
content: string;
|
|
81
|
+
similarity: number;
|
|
82
|
+
highlights: string[];
|
|
83
|
+
metadata: Record<string, unknown>;
|
|
84
|
+
created_at: string;
|
|
85
|
+
};
|
|
86
|
+
type SearchArtifactsOptions$1 = {
|
|
87
|
+
query: string;
|
|
88
|
+
namespaces?: string[];
|
|
89
|
+
limit?: number;
|
|
90
|
+
minScore?: number;
|
|
91
|
+
/** Filter by artifact type (e.g. document, code_snippet, component_tree) */
|
|
92
|
+
artifact_type?: string;
|
|
93
|
+
};
|
|
94
|
+
type SearchArtifactsResponse$1 = {
|
|
95
|
+
results: SearchResult$1[];
|
|
96
|
+
total: number;
|
|
97
|
+
query: string;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Workspace indexing and search
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
type WorkspacePreviewResponse$1 = {
|
|
105
|
+
file_count: number;
|
|
106
|
+
total_size_bytes: number;
|
|
107
|
+
estimated_chunks: number;
|
|
108
|
+
estimated_seconds: number;
|
|
109
|
+
};
|
|
110
|
+
type IndexWorkspaceOptions$1 = {
|
|
111
|
+
path: string;
|
|
112
|
+
include_patterns?: string[];
|
|
113
|
+
exclude_patterns?: string[];
|
|
114
|
+
watch?: boolean;
|
|
115
|
+
};
|
|
116
|
+
type IndexWorkspaceResponse$1 = {
|
|
117
|
+
job_id: string;
|
|
118
|
+
path: string;
|
|
119
|
+
workspace_id: string;
|
|
120
|
+
status: IndexStatus$1;
|
|
121
|
+
};
|
|
122
|
+
type IndexStatus$1 = {
|
|
123
|
+
state: "idle" | "indexing" | "error";
|
|
124
|
+
total_files?: number;
|
|
125
|
+
indexed_files?: number;
|
|
126
|
+
total_chunks?: number;
|
|
127
|
+
error?: string;
|
|
128
|
+
};
|
|
129
|
+
type WorkspaceSearchResult$1 = {
|
|
130
|
+
path: string;
|
|
131
|
+
language: string | null;
|
|
132
|
+
content: string;
|
|
133
|
+
chunk_index: number;
|
|
134
|
+
total_chunks: number;
|
|
135
|
+
score: number;
|
|
136
|
+
workspace_path?: string;
|
|
137
|
+
git_url?: string;
|
|
138
|
+
repo_name?: string;
|
|
139
|
+
};
|
|
140
|
+
type WorkspaceSearchResponse$1 = {
|
|
141
|
+
results: WorkspaceSearchResult$1[];
|
|
142
|
+
total: number;
|
|
143
|
+
query: string;
|
|
144
|
+
};
|
|
145
|
+
type DeleteWorkspaceFilesOptions = {
|
|
146
|
+
workspace_path: string;
|
|
147
|
+
path?: string;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @promptwallet/sdk - PromptWallet TypeScript SDK
|
|
152
|
+
*
|
|
153
|
+
* Chat, artifacts, and workspace for MasterCanvas, OpenClaw, and custom apps.
|
|
154
|
+
*/
|
|
155
|
+
|
|
156
|
+
type ChatMessage = ChatMessage$1;
|
|
157
|
+
type ChatOptions = ChatOptions$1;
|
|
158
|
+
type ChatResponse = ChatResponse$1;
|
|
159
|
+
type StoreArtifactOptions = StoreArtifactOptions$1;
|
|
160
|
+
type StoreArtifactResponse = StoreArtifactResponse$1;
|
|
161
|
+
type SearchResult = SearchResult$1;
|
|
162
|
+
type SearchArtifactsOptions = SearchArtifactsOptions$1;
|
|
163
|
+
type SearchArtifactsResponse = SearchArtifactsResponse$1;
|
|
164
|
+
type WorkspacePreviewResponse = WorkspacePreviewResponse$1;
|
|
165
|
+
type IndexWorkspaceOptions = IndexWorkspaceOptions$1;
|
|
166
|
+
type IndexWorkspaceResponse = IndexWorkspaceResponse$1;
|
|
167
|
+
type IndexStatus = IndexStatus$1;
|
|
168
|
+
type WorkspaceSearchResult = WorkspaceSearchResult$1;
|
|
169
|
+
type WorkspaceSearchResponse = WorkspaceSearchResponse$1;
|
|
170
|
+
/**
|
|
171
|
+
* PromptWallet client for chat, artifacts, and workspace.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { PromptWallet } from '@promptwallet/sdk';
|
|
176
|
+
*
|
|
177
|
+
* const pw = new PromptWallet({
|
|
178
|
+
* baseUrl: 'http://localhost:8080',
|
|
179
|
+
* apiKey: process.env.PROMPTWALLET_API_KEY,
|
|
180
|
+
* });
|
|
181
|
+
*
|
|
182
|
+
* const response = await pw.chat({
|
|
183
|
+
* model: 'llama-3.1-70b-versatile',
|
|
184
|
+
* messages: [{ role: 'user', content: 'How does auth work?' }],
|
|
185
|
+
* rag_namespaces: ['codebase', 'chats'],
|
|
186
|
+
* });
|
|
187
|
+
*
|
|
188
|
+
* const results = await pw.artifacts.search({
|
|
189
|
+
* query: 'login flow',
|
|
190
|
+
* namespaces: ['codebase'],
|
|
191
|
+
* limit: 5,
|
|
192
|
+
* });
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
declare class PromptWallet {
|
|
196
|
+
private config;
|
|
197
|
+
constructor(config: PromptWalletConfig);
|
|
198
|
+
/** Chat with RAG context injection */
|
|
199
|
+
chat(options: ChatOptions$1): Promise<ChatResponse$1>;
|
|
200
|
+
/** Stream chat response */
|
|
201
|
+
chatStream(options: Omit<ChatOptions$1, "stream">): AsyncGenerator<{
|
|
202
|
+
content?: string;
|
|
203
|
+
done?: boolean;
|
|
204
|
+
}>;
|
|
205
|
+
/** Artifact operations */
|
|
206
|
+
artifacts: {
|
|
207
|
+
store: (options: StoreArtifactOptions$1) => Promise<StoreArtifactResponse$1>;
|
|
208
|
+
search: (options: SearchArtifactsOptions$1) => Promise<SearchArtifactsResponse$1>;
|
|
209
|
+
get: (id: string) => Promise<{
|
|
210
|
+
id: string;
|
|
211
|
+
artifact_type: string;
|
|
212
|
+
title: string | null;
|
|
213
|
+
content: string;
|
|
214
|
+
metadata: Record<string, unknown>;
|
|
215
|
+
chunk_index: number;
|
|
216
|
+
total_chunks: number;
|
|
217
|
+
created_at: string;
|
|
218
|
+
}>;
|
|
219
|
+
getFull: (id: string) => Promise<{
|
|
220
|
+
id: string;
|
|
221
|
+
content: string;
|
|
222
|
+
chunk_index: number;
|
|
223
|
+
total_chunks: number;
|
|
224
|
+
}[]>;
|
|
225
|
+
delete: (id: string) => Promise<{
|
|
226
|
+
success: boolean;
|
|
227
|
+
message: string;
|
|
228
|
+
}>;
|
|
229
|
+
};
|
|
230
|
+
/** Workspace indexing and search */
|
|
231
|
+
workspace: {
|
|
232
|
+
preview: (options: {
|
|
233
|
+
path: string;
|
|
234
|
+
}) => Promise<WorkspacePreviewResponse$1>;
|
|
235
|
+
index: (options: IndexWorkspaceOptions$1) => Promise<IndexWorkspaceResponse$1>;
|
|
236
|
+
status: (jobId?: string) => Promise<{
|
|
237
|
+
jobs: Array<{
|
|
238
|
+
job_id: string;
|
|
239
|
+
path: string;
|
|
240
|
+
workspace_id: string;
|
|
241
|
+
status: IndexStatus$1;
|
|
242
|
+
created_at: string;
|
|
243
|
+
}>;
|
|
244
|
+
}>;
|
|
245
|
+
search: (options: {
|
|
246
|
+
q: string;
|
|
247
|
+
limit?: number;
|
|
248
|
+
language?: string;
|
|
249
|
+
path?: string;
|
|
250
|
+
}) => Promise<WorkspaceSearchResponse$1>;
|
|
251
|
+
deleteFiles: (options: DeleteWorkspaceFilesOptions) => Promise<{
|
|
252
|
+
deleted_count: number;
|
|
253
|
+
message: string;
|
|
254
|
+
}>;
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export { type ChatMessage, type ChatOptions, type ChatResponse, type IndexStatus, type IndexWorkspaceOptions, type IndexWorkspaceResponse, PromptWallet, type PromptWalletConfig, PromptWalletError, type SearchArtifactsOptions, type SearchArtifactsResponse, type SearchResult, type StoreArtifactOptions, type StoreArtifactResponse, type WorkspacePreviewResponse, type WorkspaceSearchResponse, type WorkspaceSearchResult };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
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 __esm = (fn, res) => function __init() {
|
|
7
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
8
|
+
};
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
|
+
|
|
23
|
+
// src/client.ts
|
|
24
|
+
var client_exports = {};
|
|
25
|
+
__export(client_exports, {
|
|
26
|
+
PromptWalletError: () => PromptWalletError,
|
|
27
|
+
createHeaders: () => createHeaders,
|
|
28
|
+
fetchApi: () => fetchApi
|
|
29
|
+
});
|
|
30
|
+
async function createHeaders(config) {
|
|
31
|
+
const headers = {
|
|
32
|
+
"Content-Type": "application/json"
|
|
33
|
+
};
|
|
34
|
+
let token = null;
|
|
35
|
+
if (config.apiKey) {
|
|
36
|
+
token = config.apiKey;
|
|
37
|
+
} else if (config.getToken) {
|
|
38
|
+
token = await config.getToken();
|
|
39
|
+
}
|
|
40
|
+
if (token) {
|
|
41
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
42
|
+
}
|
|
43
|
+
return headers;
|
|
44
|
+
}
|
|
45
|
+
async function fetchApi(config, path, init = {}) {
|
|
46
|
+
const base = (config.baseUrl ?? "https://api.promptwallet.dev").replace(/\/$/, "");
|
|
47
|
+
const url = `${base}${path.startsWith("/") ? path : `/${path}`}`;
|
|
48
|
+
const headers = await createHeaders(config);
|
|
49
|
+
const res = await fetch(url, {
|
|
50
|
+
...init,
|
|
51
|
+
headers: { ...headers, ...init.headers }
|
|
52
|
+
});
|
|
53
|
+
if (!res.ok) {
|
|
54
|
+
let body;
|
|
55
|
+
try {
|
|
56
|
+
body = await res.json();
|
|
57
|
+
} catch {
|
|
58
|
+
body = await res.text();
|
|
59
|
+
}
|
|
60
|
+
throw new PromptWalletError(
|
|
61
|
+
`PromptWallet API error: ${res.status} ${res.statusText}`,
|
|
62
|
+
res.status,
|
|
63
|
+
body
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
const text = await res.text();
|
|
67
|
+
if (!text) return {};
|
|
68
|
+
return JSON.parse(text);
|
|
69
|
+
}
|
|
70
|
+
var PromptWalletError;
|
|
71
|
+
var init_client = __esm({
|
|
72
|
+
"src/client.ts"() {
|
|
73
|
+
"use strict";
|
|
74
|
+
PromptWalletError = class extends Error {
|
|
75
|
+
constructor(message, status, body) {
|
|
76
|
+
super(message);
|
|
77
|
+
this.status = status;
|
|
78
|
+
this.body = body;
|
|
79
|
+
this.name = "PromptWalletError";
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// src/index.ts
|
|
86
|
+
var index_exports = {};
|
|
87
|
+
__export(index_exports, {
|
|
88
|
+
PromptWallet: () => PromptWallet,
|
|
89
|
+
PromptWalletError: () => PromptWalletError
|
|
90
|
+
});
|
|
91
|
+
module.exports = __toCommonJS(index_exports);
|
|
92
|
+
init_client();
|
|
93
|
+
|
|
94
|
+
// src/chat.ts
|
|
95
|
+
init_client();
|
|
96
|
+
async function chat(config, options) {
|
|
97
|
+
const body = {
|
|
98
|
+
model: options.model,
|
|
99
|
+
messages: options.messages,
|
|
100
|
+
temperature: options.temperature ?? 1,
|
|
101
|
+
max_tokens: options.max_tokens,
|
|
102
|
+
stream: false,
|
|
103
|
+
rag_namespaces: options.rag_namespaces,
|
|
104
|
+
max_context_tokens: options.max_context_tokens
|
|
105
|
+
};
|
|
106
|
+
return fetchApi(config, "/v1/chat/completions", {
|
|
107
|
+
method: "POST",
|
|
108
|
+
body: JSON.stringify(body)
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
async function* chatStream(config, options) {
|
|
112
|
+
const base = (config.baseUrl ?? "https://api.promptwallet.dev").replace(/\/$/, "");
|
|
113
|
+
const url = `${base}/v1/chat/completions`;
|
|
114
|
+
const { createHeaders: createHeaders2 } = await Promise.resolve().then(() => (init_client(), client_exports));
|
|
115
|
+
const headers = await createHeaders2(config);
|
|
116
|
+
const res = await fetch(url, {
|
|
117
|
+
method: "POST",
|
|
118
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
119
|
+
body: JSON.stringify({
|
|
120
|
+
model: options.model,
|
|
121
|
+
messages: options.messages,
|
|
122
|
+
temperature: options.temperature ?? 1,
|
|
123
|
+
max_tokens: options.max_tokens,
|
|
124
|
+
stream: true,
|
|
125
|
+
rag_namespaces: options.rag_namespaces,
|
|
126
|
+
max_context_tokens: options.max_context_tokens
|
|
127
|
+
})
|
|
128
|
+
});
|
|
129
|
+
if (!res.ok) {
|
|
130
|
+
const text = await res.text();
|
|
131
|
+
throw new Error(`PromptWallet API error: ${res.status} ${text}`);
|
|
132
|
+
}
|
|
133
|
+
const reader = res.body?.getReader();
|
|
134
|
+
if (!reader) throw new Error("No response body");
|
|
135
|
+
const decoder = new TextDecoder();
|
|
136
|
+
let buffer = "";
|
|
137
|
+
while (true) {
|
|
138
|
+
const { done, value } = await reader.read();
|
|
139
|
+
if (done) break;
|
|
140
|
+
buffer += decoder.decode(value, { stream: true });
|
|
141
|
+
const lines = buffer.split("\n");
|
|
142
|
+
buffer = lines.pop() ?? "";
|
|
143
|
+
for (const line of lines) {
|
|
144
|
+
if (line.startsWith("data: ")) {
|
|
145
|
+
const data = line.slice(6);
|
|
146
|
+
if (data === "[DONE]") return;
|
|
147
|
+
try {
|
|
148
|
+
const parsed = JSON.parse(data);
|
|
149
|
+
const delta = parsed.choices?.[0]?.delta?.content;
|
|
150
|
+
if (delta) yield { content: delta };
|
|
151
|
+
} catch {
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/artifacts.ts
|
|
159
|
+
init_client();
|
|
160
|
+
async function storeArtifact(config, options) {
|
|
161
|
+
const metadata = { ...options.metadata };
|
|
162
|
+
if (options.namespace) {
|
|
163
|
+
metadata.namespace = options.namespace;
|
|
164
|
+
}
|
|
165
|
+
const body = {
|
|
166
|
+
artifact_type: options.type,
|
|
167
|
+
title: options.title,
|
|
168
|
+
content: options.content,
|
|
169
|
+
metadata
|
|
170
|
+
};
|
|
171
|
+
const res = await fetchApi(
|
|
172
|
+
config,
|
|
173
|
+
"/v1/artifacts",
|
|
174
|
+
{ method: "POST", body: JSON.stringify(body) }
|
|
175
|
+
);
|
|
176
|
+
return {
|
|
177
|
+
ids: res.ids.map(String),
|
|
178
|
+
chunks: res.chunks,
|
|
179
|
+
message: res.message
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
async function searchArtifacts(config, options) {
|
|
183
|
+
const params = new URLSearchParams();
|
|
184
|
+
params.set("q", options.query);
|
|
185
|
+
params.set("limit", String(options.limit ?? 10));
|
|
186
|
+
if (options.minScore != null) params.set("min_similarity", String(options.minScore));
|
|
187
|
+
if (options.artifact_type) params.set("artifact_type", options.artifact_type);
|
|
188
|
+
const res = await fetchApi(config, `/v1/artifacts/search?${params}`);
|
|
189
|
+
return {
|
|
190
|
+
results: res.results.map((r) => ({ ...r, id: String(r.id) })),
|
|
191
|
+
total: res.total,
|
|
192
|
+
query: res.query
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
async function getArtifact(config, id) {
|
|
196
|
+
const res = await fetchApi(config, `/v1/artifacts/${id}`);
|
|
197
|
+
return { ...res, id: String(res.id) };
|
|
198
|
+
}
|
|
199
|
+
async function getFullArtifact(config, id) {
|
|
200
|
+
const chunks = await fetchApi(config, `/v1/artifacts/${id}/full`);
|
|
201
|
+
return chunks.map((c) => ({ ...c, id: String(c.id) }));
|
|
202
|
+
}
|
|
203
|
+
async function deleteArtifact(config, id) {
|
|
204
|
+
return fetchApi(config, `/v1/artifacts/${id}`, { method: "DELETE" });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// src/workspace.ts
|
|
208
|
+
init_client();
|
|
209
|
+
async function previewWorkspace(config, options) {
|
|
210
|
+
return fetchApi(config, "/v1/workspace/preview", {
|
|
211
|
+
method: "POST",
|
|
212
|
+
body: JSON.stringify({ path: options.path })
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
async function indexWorkspace(config, options) {
|
|
216
|
+
const body = {
|
|
217
|
+
path: options.path,
|
|
218
|
+
include_patterns: options.include_patterns,
|
|
219
|
+
exclude_patterns: options.exclude_patterns,
|
|
220
|
+
watch: options.watch ?? true
|
|
221
|
+
};
|
|
222
|
+
return fetchApi(config, "/v1/workspace/index", {
|
|
223
|
+
method: "POST",
|
|
224
|
+
body: JSON.stringify(body)
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
async function workspaceStatus(config, jobId) {
|
|
228
|
+
const params = jobId ? `?job_id=${encodeURIComponent(jobId)}` : "";
|
|
229
|
+
return fetchApi(config, `/v1/workspace/status${params}`);
|
|
230
|
+
}
|
|
231
|
+
async function searchWorkspace(config, options) {
|
|
232
|
+
const params = new URLSearchParams();
|
|
233
|
+
params.set("q", options.q);
|
|
234
|
+
params.set("limit", String(options.limit ?? 10));
|
|
235
|
+
if (options.language) params.set("language", options.language);
|
|
236
|
+
if (options.path) params.set("path", options.path);
|
|
237
|
+
return fetchApi(config, `/v1/workspace/search?${params}`);
|
|
238
|
+
}
|
|
239
|
+
async function deleteWorkspaceFiles(config, options) {
|
|
240
|
+
return fetchApi(config, "/v1/workspace/files", {
|
|
241
|
+
method: "DELETE",
|
|
242
|
+
body: JSON.stringify({
|
|
243
|
+
workspace_path: options.workspace_path,
|
|
244
|
+
path: options.path
|
|
245
|
+
})
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/index.ts
|
|
250
|
+
var PromptWallet = class {
|
|
251
|
+
constructor(config) {
|
|
252
|
+
this.config = config;
|
|
253
|
+
/** Artifact operations */
|
|
254
|
+
this.artifacts = {
|
|
255
|
+
store: (options) => storeArtifact(this.config, options),
|
|
256
|
+
search: (options) => searchArtifacts(this.config, options),
|
|
257
|
+
get: (id) => getArtifact(this.config, id),
|
|
258
|
+
getFull: (id) => getFullArtifact(this.config, id),
|
|
259
|
+
delete: (id) => deleteArtifact(this.config, id)
|
|
260
|
+
};
|
|
261
|
+
/** Workspace indexing and search */
|
|
262
|
+
this.workspace = {
|
|
263
|
+
preview: (options) => previewWorkspace(this.config, options),
|
|
264
|
+
index: (options) => indexWorkspace(this.config, options),
|
|
265
|
+
status: (jobId) => workspaceStatus(this.config, jobId),
|
|
266
|
+
search: (options) => searchWorkspace(this.config, options),
|
|
267
|
+
deleteFiles: (options) => deleteWorkspaceFiles(this.config, options)
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/** Chat with RAG context injection */
|
|
271
|
+
async chat(options) {
|
|
272
|
+
return chat(this.config, options);
|
|
273
|
+
}
|
|
274
|
+
/** Stream chat response */
|
|
275
|
+
async *chatStream(options) {
|
|
276
|
+
yield* chatStream(this.config, options);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
280
|
+
0 && (module.exports = {
|
|
281
|
+
PromptWallet,
|
|
282
|
+
PromptWalletError
|
|
283
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PromptWalletError,
|
|
3
|
+
fetchApi
|
|
4
|
+
} from "./chunk-IKUCY76J.mjs";
|
|
5
|
+
|
|
6
|
+
// src/chat.ts
|
|
7
|
+
async function chat(config, options) {
|
|
8
|
+
const body = {
|
|
9
|
+
model: options.model,
|
|
10
|
+
messages: options.messages,
|
|
11
|
+
temperature: options.temperature ?? 1,
|
|
12
|
+
max_tokens: options.max_tokens,
|
|
13
|
+
stream: false,
|
|
14
|
+
rag_namespaces: options.rag_namespaces,
|
|
15
|
+
max_context_tokens: options.max_context_tokens
|
|
16
|
+
};
|
|
17
|
+
return fetchApi(config, "/v1/chat/completions", {
|
|
18
|
+
method: "POST",
|
|
19
|
+
body: JSON.stringify(body)
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
async function* chatStream(config, options) {
|
|
23
|
+
const base = (config.baseUrl ?? "https://api.promptwallet.dev").replace(/\/$/, "");
|
|
24
|
+
const url = `${base}/v1/chat/completions`;
|
|
25
|
+
const { createHeaders } = await import("./client-Q6AFL5TB.mjs");
|
|
26
|
+
const headers = await createHeaders(config);
|
|
27
|
+
const res = await fetch(url, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
30
|
+
body: JSON.stringify({
|
|
31
|
+
model: options.model,
|
|
32
|
+
messages: options.messages,
|
|
33
|
+
temperature: options.temperature ?? 1,
|
|
34
|
+
max_tokens: options.max_tokens,
|
|
35
|
+
stream: true,
|
|
36
|
+
rag_namespaces: options.rag_namespaces,
|
|
37
|
+
max_context_tokens: options.max_context_tokens
|
|
38
|
+
})
|
|
39
|
+
});
|
|
40
|
+
if (!res.ok) {
|
|
41
|
+
const text = await res.text();
|
|
42
|
+
throw new Error(`PromptWallet API error: ${res.status} ${text}`);
|
|
43
|
+
}
|
|
44
|
+
const reader = res.body?.getReader();
|
|
45
|
+
if (!reader) throw new Error("No response body");
|
|
46
|
+
const decoder = new TextDecoder();
|
|
47
|
+
let buffer = "";
|
|
48
|
+
while (true) {
|
|
49
|
+
const { done, value } = await reader.read();
|
|
50
|
+
if (done) break;
|
|
51
|
+
buffer += decoder.decode(value, { stream: true });
|
|
52
|
+
const lines = buffer.split("\n");
|
|
53
|
+
buffer = lines.pop() ?? "";
|
|
54
|
+
for (const line of lines) {
|
|
55
|
+
if (line.startsWith("data: ")) {
|
|
56
|
+
const data = line.slice(6);
|
|
57
|
+
if (data === "[DONE]") return;
|
|
58
|
+
try {
|
|
59
|
+
const parsed = JSON.parse(data);
|
|
60
|
+
const delta = parsed.choices?.[0]?.delta?.content;
|
|
61
|
+
if (delta) yield { content: delta };
|
|
62
|
+
} catch {
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/artifacts.ts
|
|
70
|
+
async function storeArtifact(config, options) {
|
|
71
|
+
const metadata = { ...options.metadata };
|
|
72
|
+
if (options.namespace) {
|
|
73
|
+
metadata.namespace = options.namespace;
|
|
74
|
+
}
|
|
75
|
+
const body = {
|
|
76
|
+
artifact_type: options.type,
|
|
77
|
+
title: options.title,
|
|
78
|
+
content: options.content,
|
|
79
|
+
metadata
|
|
80
|
+
};
|
|
81
|
+
const res = await fetchApi(
|
|
82
|
+
config,
|
|
83
|
+
"/v1/artifacts",
|
|
84
|
+
{ method: "POST", body: JSON.stringify(body) }
|
|
85
|
+
);
|
|
86
|
+
return {
|
|
87
|
+
ids: res.ids.map(String),
|
|
88
|
+
chunks: res.chunks,
|
|
89
|
+
message: res.message
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
async function searchArtifacts(config, options) {
|
|
93
|
+
const params = new URLSearchParams();
|
|
94
|
+
params.set("q", options.query);
|
|
95
|
+
params.set("limit", String(options.limit ?? 10));
|
|
96
|
+
if (options.minScore != null) params.set("min_similarity", String(options.minScore));
|
|
97
|
+
if (options.artifact_type) params.set("artifact_type", options.artifact_type);
|
|
98
|
+
const res = await fetchApi(config, `/v1/artifacts/search?${params}`);
|
|
99
|
+
return {
|
|
100
|
+
results: res.results.map((r) => ({ ...r, id: String(r.id) })),
|
|
101
|
+
total: res.total,
|
|
102
|
+
query: res.query
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async function getArtifact(config, id) {
|
|
106
|
+
const res = await fetchApi(config, `/v1/artifacts/${id}`);
|
|
107
|
+
return { ...res, id: String(res.id) };
|
|
108
|
+
}
|
|
109
|
+
async function getFullArtifact(config, id) {
|
|
110
|
+
const chunks = await fetchApi(config, `/v1/artifacts/${id}/full`);
|
|
111
|
+
return chunks.map((c) => ({ ...c, id: String(c.id) }));
|
|
112
|
+
}
|
|
113
|
+
async function deleteArtifact(config, id) {
|
|
114
|
+
return fetchApi(config, `/v1/artifacts/${id}`, { method: "DELETE" });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/workspace.ts
|
|
118
|
+
async function previewWorkspace(config, options) {
|
|
119
|
+
return fetchApi(config, "/v1/workspace/preview", {
|
|
120
|
+
method: "POST",
|
|
121
|
+
body: JSON.stringify({ path: options.path })
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
async function indexWorkspace(config, options) {
|
|
125
|
+
const body = {
|
|
126
|
+
path: options.path,
|
|
127
|
+
include_patterns: options.include_patterns,
|
|
128
|
+
exclude_patterns: options.exclude_patterns,
|
|
129
|
+
watch: options.watch ?? true
|
|
130
|
+
};
|
|
131
|
+
return fetchApi(config, "/v1/workspace/index", {
|
|
132
|
+
method: "POST",
|
|
133
|
+
body: JSON.stringify(body)
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
async function workspaceStatus(config, jobId) {
|
|
137
|
+
const params = jobId ? `?job_id=${encodeURIComponent(jobId)}` : "";
|
|
138
|
+
return fetchApi(config, `/v1/workspace/status${params}`);
|
|
139
|
+
}
|
|
140
|
+
async function searchWorkspace(config, options) {
|
|
141
|
+
const params = new URLSearchParams();
|
|
142
|
+
params.set("q", options.q);
|
|
143
|
+
params.set("limit", String(options.limit ?? 10));
|
|
144
|
+
if (options.language) params.set("language", options.language);
|
|
145
|
+
if (options.path) params.set("path", options.path);
|
|
146
|
+
return fetchApi(config, `/v1/workspace/search?${params}`);
|
|
147
|
+
}
|
|
148
|
+
async function deleteWorkspaceFiles(config, options) {
|
|
149
|
+
return fetchApi(config, "/v1/workspace/files", {
|
|
150
|
+
method: "DELETE",
|
|
151
|
+
body: JSON.stringify({
|
|
152
|
+
workspace_path: options.workspace_path,
|
|
153
|
+
path: options.path
|
|
154
|
+
})
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/index.ts
|
|
159
|
+
var PromptWallet = class {
|
|
160
|
+
constructor(config) {
|
|
161
|
+
this.config = config;
|
|
162
|
+
/** Artifact operations */
|
|
163
|
+
this.artifacts = {
|
|
164
|
+
store: (options) => storeArtifact(this.config, options),
|
|
165
|
+
search: (options) => searchArtifacts(this.config, options),
|
|
166
|
+
get: (id) => getArtifact(this.config, id),
|
|
167
|
+
getFull: (id) => getFullArtifact(this.config, id),
|
|
168
|
+
delete: (id) => deleteArtifact(this.config, id)
|
|
169
|
+
};
|
|
170
|
+
/** Workspace indexing and search */
|
|
171
|
+
this.workspace = {
|
|
172
|
+
preview: (options) => previewWorkspace(this.config, options),
|
|
173
|
+
index: (options) => indexWorkspace(this.config, options),
|
|
174
|
+
status: (jobId) => workspaceStatus(this.config, jobId),
|
|
175
|
+
search: (options) => searchWorkspace(this.config, options),
|
|
176
|
+
deleteFiles: (options) => deleteWorkspaceFiles(this.config, options)
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
/** Chat with RAG context injection */
|
|
180
|
+
async chat(options) {
|
|
181
|
+
return chat(this.config, options);
|
|
182
|
+
}
|
|
183
|
+
/** Stream chat response */
|
|
184
|
+
async *chatStream(options) {
|
|
185
|
+
yield* chatStream(this.config, options);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
export {
|
|
189
|
+
PromptWallet,
|
|
190
|
+
PromptWalletError
|
|
191
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "promptwallet-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PromptWallet TypeScript SDK for chat, artifacts, and workspace",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
21
|
+
"typecheck": "tsc --noEmit"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"promptwallet",
|
|
25
|
+
"ai",
|
|
26
|
+
"rag",
|
|
27
|
+
"llm"
|
|
28
|
+
],
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"tsup": "^8.0.0",
|
|
32
|
+
"typescript": "^5.3.0"
|
|
33
|
+
}
|
|
34
|
+
}
|