@krutai/ai-provider 0.2.12 → 0.2.15
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/AI_REFERENCE.md +53 -14
- package/README.md +52 -47
- package/dist/index.d.mts +32 -68
- package/dist/index.d.ts +32 -68
- package/dist/index.js +21 -248
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +21 -248
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
|
2
2
|
export { ApiKeyValidationError as KrutAIKeyValidationError, validateApiKeyWithService as validateApiKey, validateApiKeyFormat } from 'krutai';
|
|
3
3
|
|
|
4
4
|
// src/types.ts
|
|
5
|
-
var DEFAULT_MODEL = "
|
|
5
|
+
var DEFAULT_MODEL = "gemini-3.1-pro-preview";
|
|
6
6
|
var DEFAULT_SERVER_URL = "http://localhost:8000";
|
|
7
7
|
var KrutAIProvider = class {
|
|
8
8
|
apiKey;
|
|
@@ -66,241 +66,6 @@ var KrutAIProvider = class {
|
|
|
66
66
|
// ---------------------------------------------------------------------------
|
|
67
67
|
// Public AI Methods
|
|
68
68
|
// ---------------------------------------------------------------------------
|
|
69
|
-
/**
|
|
70
|
-
* Generate a response for a prompt (non-streaming).
|
|
71
|
-
*
|
|
72
|
-
* Calls: POST {serverUrl}/generate
|
|
73
|
-
* Body: { prompt, model, system?, maxTokens?, temperature? }
|
|
74
|
-
* Expected response: { text: string } or { content: string } or { message: string }
|
|
75
|
-
*
|
|
76
|
-
* @param prompt - The user prompt string
|
|
77
|
-
* @param options - Optional overrides (model, system, maxTokens, temperature)
|
|
78
|
-
* @returns The assistant's response text
|
|
79
|
-
*/
|
|
80
|
-
async generate(prompt, options = {}) {
|
|
81
|
-
this.assertInitialized();
|
|
82
|
-
const model = options.model ?? this.resolvedModel;
|
|
83
|
-
const response = await fetch(`${this.serverUrl}/generate`, {
|
|
84
|
-
method: "POST",
|
|
85
|
-
headers: this.authHeaders(),
|
|
86
|
-
body: JSON.stringify({
|
|
87
|
-
prompt,
|
|
88
|
-
model,
|
|
89
|
-
...options.system !== void 0 ? { system: options.system } : {},
|
|
90
|
-
...options.images !== void 0 ? { images: options.images } : {},
|
|
91
|
-
...options.documents !== void 0 ? { documents: options.documents } : {},
|
|
92
|
-
...options.pdf !== void 0 ? { pdf: options.pdf } : {},
|
|
93
|
-
...options.maxTokens !== void 0 ? { maxTokens: options.maxTokens } : {},
|
|
94
|
-
...options.temperature !== void 0 ? { temperature: options.temperature } : {}
|
|
95
|
-
})
|
|
96
|
-
});
|
|
97
|
-
if (!response.ok) {
|
|
98
|
-
let errorMessage = `AI server returned HTTP ${response.status} for /generate`;
|
|
99
|
-
try {
|
|
100
|
-
const errorData = await response.json();
|
|
101
|
-
if (errorData?.error) errorMessage = errorData.error;
|
|
102
|
-
else if (errorData?.message) errorMessage = errorData.message;
|
|
103
|
-
} catch {
|
|
104
|
-
}
|
|
105
|
-
throw new Error(errorMessage);
|
|
106
|
-
}
|
|
107
|
-
const data = await response.json();
|
|
108
|
-
return data.text ?? data.content ?? data.message ?? "";
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Generate a streaming response for a prompt via Server-Sent Events (SSE).
|
|
112
|
-
*
|
|
113
|
-
* Calls: POST {serverUrl}/stream
|
|
114
|
-
* Body: { prompt, model, system?, maxTokens?, temperature? }
|
|
115
|
-
* Expected response: `text/event-stream` with `data: <chunk>` lines.
|
|
116
|
-
*
|
|
117
|
-
* @param prompt - The user prompt string
|
|
118
|
-
* @param options - Optional overrides (model, system, maxTokens, temperature)
|
|
119
|
-
* @returns An async generator yielding string chunks from the server
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* ```typescript
|
|
123
|
-
* const stream = ai.stream('Tell me a story');
|
|
124
|
-
* for await (const chunk of stream) {
|
|
125
|
-
* process.stdout.write(chunk);
|
|
126
|
-
* }
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
|
-
async *stream(prompt, options = {}) {
|
|
130
|
-
this.assertInitialized();
|
|
131
|
-
const model = options.model ?? this.resolvedModel;
|
|
132
|
-
const response = await fetch(`${this.serverUrl}/stream`, {
|
|
133
|
-
method: "POST",
|
|
134
|
-
headers: {
|
|
135
|
-
...this.authHeaders(),
|
|
136
|
-
Accept: "text/event-stream"
|
|
137
|
-
},
|
|
138
|
-
body: JSON.stringify({
|
|
139
|
-
prompt,
|
|
140
|
-
model,
|
|
141
|
-
...options.system !== void 0 ? { system: options.system } : {},
|
|
142
|
-
...options.images !== void 0 ? { images: options.images } : {},
|
|
143
|
-
...options.documents !== void 0 ? { documents: options.documents } : {},
|
|
144
|
-
...options.pdf !== void 0 ? { pdf: options.pdf } : {},
|
|
145
|
-
...options.maxTokens !== void 0 ? { maxTokens: options.maxTokens } : {},
|
|
146
|
-
...options.temperature !== void 0 ? { temperature: options.temperature } : {}
|
|
147
|
-
})
|
|
148
|
-
});
|
|
149
|
-
if (!response.ok) {
|
|
150
|
-
let errorMessage = `AI server returned HTTP ${response.status} for /stream`;
|
|
151
|
-
try {
|
|
152
|
-
const errorData = await response.json();
|
|
153
|
-
if (errorData?.error) errorMessage = errorData.error;
|
|
154
|
-
else if (errorData?.message) errorMessage = errorData.message;
|
|
155
|
-
} catch {
|
|
156
|
-
}
|
|
157
|
-
throw new Error(errorMessage);
|
|
158
|
-
}
|
|
159
|
-
if (!response.body) {
|
|
160
|
-
throw new Error("AI server returned no response body for /stream");
|
|
161
|
-
}
|
|
162
|
-
const reader = response.body.getReader();
|
|
163
|
-
const decoder = new TextDecoder();
|
|
164
|
-
let buffer = "";
|
|
165
|
-
try {
|
|
166
|
-
while (true) {
|
|
167
|
-
const { done, value } = await reader.read();
|
|
168
|
-
if (done) break;
|
|
169
|
-
buffer += decoder.decode(value, { stream: true });
|
|
170
|
-
const lines = buffer.split("\n");
|
|
171
|
-
buffer = lines.pop() ?? "";
|
|
172
|
-
for (const line of lines) {
|
|
173
|
-
if (line.startsWith("data: ")) {
|
|
174
|
-
const raw = line.slice(6).trim();
|
|
175
|
-
if (raw === "[DONE]") return;
|
|
176
|
-
try {
|
|
177
|
-
const parsed = JSON.parse(raw);
|
|
178
|
-
const chunk = parsed.text ?? parsed.content ?? parsed.delta?.content ?? "";
|
|
179
|
-
if (chunk) yield chunk;
|
|
180
|
-
} catch {
|
|
181
|
-
if (raw) yield raw;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
} finally {
|
|
187
|
-
reader.releaseLock();
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Similar to stream() but returns the raw fetch Response object.
|
|
192
|
-
* Useful when you want to proxy the Server-Sent Events stream directly to a frontend client
|
|
193
|
-
* (e.g., returning this directly from a Next.js API route).
|
|
194
|
-
*
|
|
195
|
-
* @param prompt - The user prompt string
|
|
196
|
-
* @param options - Optional overrides (model, system, maxTokens, temperature)
|
|
197
|
-
* @returns A Promise resolving to the native fetch Response
|
|
198
|
-
*/
|
|
199
|
-
async streamResponse(prompt, options = {}) {
|
|
200
|
-
this.assertInitialized();
|
|
201
|
-
const model = options.model ?? this.resolvedModel;
|
|
202
|
-
const response = await fetch(`${this.serverUrl}/stream`, {
|
|
203
|
-
method: "POST",
|
|
204
|
-
headers: {
|
|
205
|
-
...this.authHeaders(),
|
|
206
|
-
Accept: "text/event-stream"
|
|
207
|
-
},
|
|
208
|
-
body: JSON.stringify({
|
|
209
|
-
prompt,
|
|
210
|
-
model,
|
|
211
|
-
...options.system !== void 0 ? { system: options.system } : {},
|
|
212
|
-
...options.images !== void 0 ? { images: options.images } : {},
|
|
213
|
-
...options.documents !== void 0 ? { documents: options.documents } : {},
|
|
214
|
-
...options.pdf !== void 0 ? { pdf: options.pdf } : {},
|
|
215
|
-
...options.maxTokens !== void 0 ? { maxTokens: options.maxTokens } : {},
|
|
216
|
-
...options.temperature !== void 0 ? { temperature: options.temperature } : {}
|
|
217
|
-
})
|
|
218
|
-
});
|
|
219
|
-
if (!response.ok) {
|
|
220
|
-
let errorMessage = `AI server returned HTTP ${response.status} for /stream`;
|
|
221
|
-
try {
|
|
222
|
-
const errorData = await response.json();
|
|
223
|
-
if (errorData?.error) errorMessage = errorData.error;
|
|
224
|
-
else if (errorData?.message) errorMessage = errorData.message;
|
|
225
|
-
} catch {
|
|
226
|
-
}
|
|
227
|
-
throw new Error(errorMessage);
|
|
228
|
-
}
|
|
229
|
-
console.log(response);
|
|
230
|
-
return response;
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Multi-turn conversation streaming: pass a full message history.
|
|
234
|
-
* Calls POST /stream with the full { messages } payload.
|
|
235
|
-
*
|
|
236
|
-
* @param messages - Full conversation history
|
|
237
|
-
* @param options - Optional overrides (model, maxTokens, temperature)
|
|
238
|
-
* @returns An async generator yielding string chunks from the server
|
|
239
|
-
*/
|
|
240
|
-
async *streamChat(messages, options = {}) {
|
|
241
|
-
this.assertInitialized();
|
|
242
|
-
if (!messages.length) {
|
|
243
|
-
throw new Error("Messages array cannot be empty for streamChat");
|
|
244
|
-
}
|
|
245
|
-
const model = options.model ?? this.resolvedModel;
|
|
246
|
-
const response = await fetch(`${this.serverUrl}/stream`, {
|
|
247
|
-
method: "POST",
|
|
248
|
-
headers: {
|
|
249
|
-
...this.authHeaders(),
|
|
250
|
-
Accept: "text/event-stream"
|
|
251
|
-
},
|
|
252
|
-
body: JSON.stringify({
|
|
253
|
-
messages,
|
|
254
|
-
model,
|
|
255
|
-
...options.system !== void 0 ? { system: options.system } : {},
|
|
256
|
-
...options.images !== void 0 ? { images: options.images } : {},
|
|
257
|
-
...options.documents !== void 0 ? { documents: options.documents } : {},
|
|
258
|
-
...options.pdf !== void 0 ? { pdf: options.pdf } : {},
|
|
259
|
-
...options.maxTokens !== void 0 ? { maxTokens: options.maxTokens } : {},
|
|
260
|
-
...options.temperature !== void 0 ? { temperature: options.temperature } : {}
|
|
261
|
-
})
|
|
262
|
-
});
|
|
263
|
-
if (!response.ok) {
|
|
264
|
-
let errorMessage = `AI server returned HTTP ${response.status} for /stream`;
|
|
265
|
-
try {
|
|
266
|
-
const errorData = await response.json();
|
|
267
|
-
if (errorData?.error) errorMessage = errorData.error;
|
|
268
|
-
else if (errorData?.message) errorMessage = errorData.message;
|
|
269
|
-
} catch {
|
|
270
|
-
}
|
|
271
|
-
throw new Error(errorMessage);
|
|
272
|
-
}
|
|
273
|
-
if (!response.body) {
|
|
274
|
-
throw new Error("AI server returned no response body for /stream");
|
|
275
|
-
}
|
|
276
|
-
const reader = response.body.getReader();
|
|
277
|
-
const decoder = new TextDecoder();
|
|
278
|
-
let buffer = "";
|
|
279
|
-
try {
|
|
280
|
-
while (true) {
|
|
281
|
-
const { done, value } = await reader.read();
|
|
282
|
-
if (done) break;
|
|
283
|
-
buffer += decoder.decode(value, { stream: true });
|
|
284
|
-
const lines = buffer.split("\n");
|
|
285
|
-
buffer = lines.pop() ?? "";
|
|
286
|
-
for (const line of lines) {
|
|
287
|
-
if (line.startsWith("data: ")) {
|
|
288
|
-
const raw = line.slice(6).trim();
|
|
289
|
-
if (raw === "[DONE]") return;
|
|
290
|
-
try {
|
|
291
|
-
const parsed = JSON.parse(raw);
|
|
292
|
-
const chunk = parsed.text ?? parsed.content ?? parsed.delta?.content ?? "";
|
|
293
|
-
if (chunk) yield chunk;
|
|
294
|
-
} catch {
|
|
295
|
-
if (raw) yield raw;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
} finally {
|
|
301
|
-
reader.releaseLock();
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
69
|
/**
|
|
305
70
|
* Similar to streamChat() but returns the raw fetch Response object.
|
|
306
71
|
* Useful for proxying the Server-Sent Events stream directly to a frontend client.
|
|
@@ -345,34 +110,39 @@ var KrutAIProvider = class {
|
|
|
345
110
|
return response;
|
|
346
111
|
}
|
|
347
112
|
/**
|
|
348
|
-
*
|
|
113
|
+
* Generate a response for a prompt (non-streaming).
|
|
349
114
|
*
|
|
350
|
-
* Calls: POST {serverUrl}/
|
|
351
|
-
* Body: {
|
|
115
|
+
* Calls: POST {serverUrl}/generate
|
|
116
|
+
* Body: { prompt, model, system?, maxTokens?, temperature? }
|
|
352
117
|
* Expected response: { text: string } or { content: string } or { message: string }
|
|
353
118
|
*
|
|
354
|
-
* @param
|
|
355
|
-
* @param options - Optional overrides (model, maxTokens, temperature)
|
|
356
|
-
* @returns The assistant's response text
|
|
119
|
+
* @param prompt - The user prompt string
|
|
120
|
+
* @param options - Optional overrides (model, system, maxTokens, temperature)
|
|
121
|
+
* @returns The assistant's response text (or an object if structured)
|
|
357
122
|
*/
|
|
358
|
-
async chat(
|
|
123
|
+
async chat(prompt, options = {}) {
|
|
359
124
|
this.assertInitialized();
|
|
360
125
|
const model = options.model ?? this.resolvedModel;
|
|
361
|
-
const response = await fetch(`${this.serverUrl}/
|
|
126
|
+
const response = await fetch(`${this.serverUrl}/generate`, {
|
|
362
127
|
method: "POST",
|
|
363
128
|
headers: this.authHeaders(),
|
|
364
129
|
body: JSON.stringify({
|
|
365
|
-
|
|
130
|
+
prompt,
|
|
366
131
|
model,
|
|
132
|
+
...options.system !== void 0 ? { system: options.system } : {},
|
|
367
133
|
...options.images !== void 0 ? { images: options.images } : {},
|
|
368
134
|
...options.documents !== void 0 ? { documents: options.documents } : {},
|
|
369
135
|
...options.pdf !== void 0 ? { pdf: options.pdf } : {},
|
|
370
136
|
...options.maxTokens !== void 0 ? { maxTokens: options.maxTokens } : {},
|
|
371
|
-
...options.temperature !== void 0 ? { temperature: options.temperature } : {}
|
|
137
|
+
...options.temperature !== void 0 ? { temperature: options.temperature } : {},
|
|
138
|
+
...options.isStructure !== void 0 ? { isStructure: options.isStructure } : {},
|
|
139
|
+
...options.output_structure !== void 0 ? { output_structure: options.output_structure } : {},
|
|
140
|
+
...options.history !== void 0 ? { history: options.history } : {},
|
|
141
|
+
...options.attachments !== void 0 ? { attachments: options.attachments } : {}
|
|
372
142
|
})
|
|
373
143
|
});
|
|
374
144
|
if (!response.ok) {
|
|
375
|
-
let errorMessage = `AI server returned HTTP ${response.status} for /
|
|
145
|
+
let errorMessage = `AI server returned HTTP ${response.status} for /generate`;
|
|
376
146
|
try {
|
|
377
147
|
const errorData = await response.json();
|
|
378
148
|
if (errorData?.error) errorMessage = errorData.error;
|
|
@@ -382,6 +152,9 @@ var KrutAIProvider = class {
|
|
|
382
152
|
throw new Error(errorMessage);
|
|
383
153
|
}
|
|
384
154
|
const data = await response.json();
|
|
155
|
+
if (options.isStructure) {
|
|
156
|
+
return data;
|
|
157
|
+
}
|
|
385
158
|
return data.text ?? data.content ?? data.message ?? "";
|
|
386
159
|
}
|
|
387
160
|
};
|
|
@@ -391,7 +164,7 @@ function krutAI(config) {
|
|
|
391
164
|
...config
|
|
392
165
|
});
|
|
393
166
|
}
|
|
394
|
-
var VERSION = "0.2.
|
|
167
|
+
var VERSION = "0.2.15";
|
|
395
168
|
|
|
396
169
|
export { DEFAULT_MODEL, KrutAIProvider, VERSION, krutAI };
|
|
397
170
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts","../src/client.ts","../src/index.ts"],"names":["validateApiKey"],"mappings":";;;;AAQO,IAAM,aAAA,GAAgB;AAMtB,IAAM,kBAAA,GAAqB,uBAAA;ACuB3B,IAAM,iBAAN,MAAqB;AAAA,EACP,MAAA;AAAA,EACA,SAAA;AAAA,EACA,aAAA;AAAA,EACA,MAAA;AAAA,EAET,WAAA,GAAc,KAAA;AAAA,EAEtB,YAAY,MAAA,EAA8B;AACtC,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AACd,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA,CAAO,MAAA,IAAU,OAAA,CAAQ,IAAI,cAAA,IAAkB,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GAAe,CAAA,wBAAA,EAA2B,QAAA,CAAS,MAAM,CAAA,UAAA,CAAA;AAC7D,MAAA,IAAI;AACA,QAAA,MAAM,SAAA,GAAa,MAAM,QAAA,CAAS,IAAA,EAAK;AACvC,QAAA,IAAI,SAAA,EAAW,KAAA,EAAO,YAAA,GAAe,SAAA,CAAU,KAAA;AAAA,aAAA,IACtC,SAAA,EAAW,OAAA,EAAS,YAAA,GAAe,SAAA,CAAU,OAAA;AAAA,MAC1D,CAAA,CAAA,MAAQ;AAAA,MAAE;AACV,MAAA,MAAM,IAAI,MAAM,YAAY,CAAA;AAAA,IAChC;AAEA,IAAA,MAAM,IAAA,GAAQ,MAAM,QAAA,CAAS,IAAA,EAAK;AAMlC,IAAA,OAAO,IAAA,CAAK,IAAA,IAAQ,IAAA,CAAK,OAAA,IAAW,KAAK,OAAA,IAAW,EAAA;AAAA,EACxD;AACJ;ACtZO,SAAS,OACZ,MAAA,EACc;AACd,EAAA,OAAO,IAAI,cAAA,CAAe;AAAA,IACtB,KAAA,EAAO,aAAA;AAAA,IACP,GAAG;AAAA,GACN,CAAA;AACL;AAGO,IAAM,OAAA,GAAU","file":"index.mjs","sourcesContent":["/**\n * Types for @krutai/ai-provider\n */\n\n/**\n * Default model identifier sent to the LangChain server when no model is specified.\n * Your server can use this value to route to its own default model.\n */\nexport const DEFAULT_MODEL = 'default' as const;\n\n/**\n * Default base URL for the LangChain backend server.\n * Used when no serverUrl is provided in the config.\n */\nexport const DEFAULT_SERVER_URL = 'http://localhost:8000' as const;\n\n/**\n * Configuration options for KrutAIProvider\n */\nexport interface KrutAIProviderConfig {\n /**\n * KrutAI API key.\n * Validated against the LangChain server before use.\n * Optional: defaults to process.env.KRUTAI_API_KEY\n */\n apiKey?: string;\n\n /**\n * Base URL of your deployed LangChain backend server.\n * @default \"http://localhost:8000\"\n * @example \"https://ai.krut.ai\"\n */\n serverUrl?: string;\n\n /**\n * The AI model to use (passed to the server).\n * The server decides what to do with this value.\n * @default \"default\"\n */\n model?: string;\n\n /**\n * Whether to validate the API key against the server on initialization.\n * Set to false to skip the validation round-trip (e.g. in tests).\n * @default true\n */\n validateOnInit?: boolean;\n}\n\n/**\n * A part of a multimodal message\n */\nexport interface TextContentPart {\n type: 'text';\n text: string;\n}\n\nexport interface ImageContentPart {\n type: 'image_url';\n image_url: {\n url: string; // Base64 data URI or HTTP(S) URL\n detail?: 'low' | 'high' | 'auto';\n };\n}\n\nexport type ContentPart = TextContentPart | ImageContentPart;\nexport type MessageContent = string | ContentPart[];\n\n/**\n * A single chat message\n */\nexport interface ChatMessage {\n role: 'user' | 'assistant' | 'system';\n content: MessageContent;\n}\n\n/**\n * Options for a single generate / stream / chat call\n */\nexport interface GenerateOptions {\n /**\n * Override the model for this specific call.\n */\n model?: string;\n\n /**\n * System prompt (prepended as a system message).\n */\n system?: string;\n\n /**\n * Maximum tokens to generate.\n */\n maxTokens?: number;\n\n /**\n * Temperature (0–2).\n */\n temperature?: number;\n\n /**\n * Array of image URLs or base64 data URIs to include with the request.\n */\n images?: string[];\n\n /**\n * Array of document URLs or base64 data URIs (e.g. PDFs) to include with the request.\n */\n documents?: string[];\n\n /**\n * Array of PDF URLs or base64 data URIs to include with the request.\n */\n pdf?: string[];\n}\n","import type { KrutAIProviderConfig, GenerateOptions, ChatMessage } from './types';\nimport { DEFAULT_MODEL, DEFAULT_SERVER_URL } from './types';\nimport {\n validateApiKeyWithService as validateApiKey,\n validateApiKeyFormat,\n ApiKeyValidationError as KrutAIKeyValidationError,\n} from 'krutai';\n\nexport { KrutAIKeyValidationError };\n\n/**\n * KrutAIProvider — fetch-based AI provider for KrutAI\n *\n * Calls your deployed LangChain backend server for all AI operations.\n * The API key is validated against the server before use.\n *\n * @example\n * ```typescript\n * import { KrutAIProvider } from '@krutai/ai-provider';\n *\n * // Using local dev server (http://localhost:8000 by default)\n * const ai = new KrutAIProvider({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * });\n *\n * // Or point to a production server\n * const aiProd = new KrutAIProvider({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://ai.krut.ai',\n * });\n *\n * await ai.initialize(); // validates key against server\n *\n * const text = await ai.generate('Tell me a joke');\n * console.log(text);\n * ```\n */\nexport class KrutAIProvider {\n private readonly apiKey: string;\n private readonly serverUrl: string;\n private readonly resolvedModel: string;\n private readonly config: KrutAIProviderConfig;\n\n private initialized = false;\n\n constructor(config: KrutAIProviderConfig) {\n this.config = config;\n this.apiKey = config.apiKey || process.env.KRUTAI_API_KEY || '';\n this.serverUrl = (config.serverUrl ?? DEFAULT_SERVER_URL).replace(/\\/$/, ''); // strip trailing slash\n this.resolvedModel = config.model ?? DEFAULT_MODEL;\n\n // Basic format check immediately on construction\n validateApiKeyFormat(this.apiKey);\n\n // If validation is disabled, mark as ready immediately\n if (config.validateOnInit === false) {\n this.initialized = true;\n }\n }\n\n /**\n * Initialize the provider.\n * Validates the API key against the LangChain server, then marks provider as ready.\n *\n * @throws {KrutAIKeyValidationError} if the key is rejected or the server is unreachable\n */\n async initialize(): Promise<void> {\n if (this.initialized) return;\n\n if (this.config.validateOnInit !== false) {\n await validateApiKey(this.apiKey, this.serverUrl);\n }\n\n this.initialized = true;\n }\n\n /**\n * Returns the currently configured default model.\n */\n getModel(): string {\n return this.resolvedModel;\n }\n\n /**\n * Returns whether the provider has been initialized.\n */\n isInitialized(): boolean {\n return this.initialized;\n }\n\n // ---------------------------------------------------------------------------\n // Private helpers\n // ---------------------------------------------------------------------------\n\n private assertInitialized(): void {\n if (!this.initialized) {\n throw new Error(\n 'KrutAIProvider not initialized. Call initialize() first or set validateOnInit to false.'\n );\n }\n }\n\n /** Common request headers sent to the server on every AI call. */\n private authHeaders(): Record<string, string> {\n return {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.apiKey}`,\n 'x-api-key': this.apiKey,\n };\n }\n\n // ---------------------------------------------------------------------------\n // Public AI Methods\n // ---------------------------------------------------------------------------\n\n /**\n * Generate a response for a prompt (non-streaming).\n *\n * Calls: POST {serverUrl}/generate\n * Body: { prompt, model, system?, maxTokens?, temperature? }\n * Expected response: { text: string } or { content: string } or { message: string }\n *\n * @param prompt - The user prompt string\n * @param options - Optional overrides (model, system, maxTokens, temperature)\n * @returns The assistant's response text\n */\n async generate(prompt: string, options: GenerateOptions = {}): Promise<string> {\n this.assertInitialized();\n const model = options.model ?? this.resolvedModel;\n\n const response = await fetch(`${this.serverUrl}/generate`, {\n method: 'POST',\n headers: this.authHeaders(),\n body: JSON.stringify({\n prompt,\n model,\n ...(options.system !== undefined ? { system: options.system } : {}),\n ...(options.images !== undefined ? { images: options.images } : {}),\n ...(options.documents !== undefined ? { documents: options.documents } : {}),\n ...(options.pdf !== undefined ? { pdf: options.pdf } : {}),\n ...(options.maxTokens !== undefined ? { maxTokens: options.maxTokens } : {}),\n ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),\n }),\n });\n\n if (!response.ok) {\n let errorMessage = `AI server returned HTTP ${response.status} for /generate`;\n try {\n const errorData = (await response.json()) as { message?: string; error?: string };\n if (errorData?.error) errorMessage = errorData.error;\n else if (errorData?.message) errorMessage = errorData.message;\n } catch { }\n throw new Error(errorMessage);\n }\n\n const data = (await response.json()) as {\n text?: string;\n content?: string;\n message?: string;\n };\n\n return data.text ?? data.content ?? data.message ?? '';\n }\n\n /**\n * Generate a streaming response for a prompt via Server-Sent Events (SSE).\n *\n * Calls: POST {serverUrl}/stream\n * Body: { prompt, model, system?, maxTokens?, temperature? }\n * Expected response: `text/event-stream` with `data: <chunk>` lines.\n *\n * @param prompt - The user prompt string\n * @param options - Optional overrides (model, system, maxTokens, temperature)\n * @returns An async generator yielding string chunks from the server\n *\n * @example\n * ```typescript\n * const stream = ai.stream('Tell me a story');\n * for await (const chunk of stream) {\n * process.stdout.write(chunk);\n * }\n * ```\n */\n async *stream(prompt: string, options: GenerateOptions = {}): AsyncGenerator<string> {\n this.assertInitialized();\n const model = options.model ?? this.resolvedModel;\n\n const response = await fetch(`${this.serverUrl}/stream`, {\n method: 'POST',\n headers: {\n ...this.authHeaders(),\n Accept: 'text/event-stream',\n },\n body: JSON.stringify({\n prompt,\n model,\n ...(options.system !== undefined ? { system: options.system } : {}),\n ...(options.images !== undefined ? { images: options.images } : {}),\n ...(options.documents !== undefined ? { documents: options.documents } : {}),\n ...(options.pdf !== undefined ? { pdf: options.pdf } : {}),\n ...(options.maxTokens !== undefined ? { maxTokens: options.maxTokens } : {}),\n ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),\n }),\n });\n\n if (!response.ok) {\n let errorMessage = `AI server returned HTTP ${response.status} for /stream`;\n try {\n const errorData = (await response.json()) as { message?: string; error?: string };\n if (errorData?.error) errorMessage = errorData.error;\n else if (errorData?.message) errorMessage = errorData.message;\n } catch { }\n throw new Error(errorMessage);\n }\n\n if (!response.body) {\n throw new Error('AI server returned no response body for /stream');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n const lines = buffer.split('\\n');\n // Keep the last (potentially incomplete) line in the buffer\n buffer = lines.pop() ?? '';\n\n for (const line of lines) {\n if (line.startsWith('data: ')) {\n const raw = line.slice(6).trim();\n if (raw === '[DONE]') return;\n try {\n const parsed = JSON.parse(raw) as {\n text?: string;\n content?: string;\n delta?: { content?: string };\n };\n const chunk =\n parsed.text ??\n parsed.content ??\n parsed.delta?.content ??\n '';\n if (chunk) yield chunk;\n } catch {\n // raw string chunk (non-JSON SSE)\n if (raw) yield raw;\n }\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n }\n\n /**\n * Similar to stream() but returns the raw fetch Response object.\n * Useful when you want to proxy the Server-Sent Events stream directly to a frontend client\n * (e.g., returning this directly from a Next.js API route).\n *\n * @param prompt - The user prompt string\n * @param options - Optional overrides (model, system, maxTokens, temperature)\n * @returns A Promise resolving to the native fetch Response\n */\n async streamResponse(prompt: string, options: GenerateOptions = {}): Promise<Response> {\n this.assertInitialized();\n const model = options.model ?? this.resolvedModel;\n\n const response = await fetch(`${this.serverUrl}/stream`, {\n method: 'POST',\n headers: {\n ...this.authHeaders(),\n Accept: 'text/event-stream',\n },\n body: JSON.stringify({\n prompt,\n model,\n ...(options.system !== undefined ? { system: options.system } : {}),\n ...(options.images !== undefined ? { images: options.images } : {}),\n ...(options.documents !== undefined ? { documents: options.documents } : {}),\n ...(options.pdf !== undefined ? { pdf: options.pdf } : {}),\n ...(options.maxTokens !== undefined ? { maxTokens: options.maxTokens } : {}),\n ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),\n }),\n });\n\n if (!response.ok) {\n let errorMessage = `AI server returned HTTP ${response.status} for /stream`;\n try {\n const errorData = (await response.json()) as { message?: string; error?: string };\n if (errorData?.error) errorMessage = errorData.error;\n else if (errorData?.message) errorMessage = errorData.message;\n } catch { }\n throw new Error(errorMessage);\n }\n console.log(response)\n return response;\n }\n\n /**\n * Multi-turn conversation streaming: pass a full message history.\n * Calls POST /stream with the full { messages } payload.\n *\n * @param messages - Full conversation history\n * @param options - Optional overrides (model, maxTokens, temperature)\n * @returns An async generator yielding string chunks from the server\n */\n async *streamChat(messages: ChatMessage[], options: GenerateOptions = {}): AsyncGenerator<string> {\n this.assertInitialized();\n\n if (!messages.length) {\n throw new Error('Messages array cannot be empty for streamChat');\n }\n\n const model = options.model ?? this.resolvedModel;\n\n const response = await fetch(`${this.serverUrl}/stream`, {\n method: 'POST',\n headers: {\n ...this.authHeaders(),\n Accept: 'text/event-stream',\n },\n body: JSON.stringify({\n messages,\n model,\n ...(options.system !== undefined ? { system: options.system } : {}),\n ...(options.images !== undefined ? { images: options.images } : {}),\n ...(options.documents !== undefined ? { documents: options.documents } : {}),\n ...(options.pdf !== undefined ? { pdf: options.pdf } : {}),\n ...(options.maxTokens !== undefined ? { maxTokens: options.maxTokens } : {}),\n ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),\n }),\n });\n\n if (!response.ok) {\n let errorMessage = `AI server returned HTTP ${response.status} for /stream`;\n try {\n const errorData = (await response.json()) as { message?: string; error?: string };\n if (errorData?.error) errorMessage = errorData.error;\n else if (errorData?.message) errorMessage = errorData.message;\n } catch { }\n throw new Error(errorMessage);\n }\n\n if (!response.body) {\n throw new Error('AI server returned no response body for /stream');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n const lines = buffer.split('\\n');\n buffer = lines.pop() ?? '';\n\n for (const line of lines) {\n if (line.startsWith('data: ')) {\n const raw = line.slice(6).trim();\n if (raw === '[DONE]') return;\n try {\n const parsed = JSON.parse(raw) as {\n text?: string;\n content?: string;\n delta?: { content?: string };\n };\n const chunk =\n parsed.text ??\n parsed.content ??\n parsed.delta?.content ??\n '';\n if (chunk) yield chunk;\n } catch {\n if (raw) yield raw;\n }\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n }\n\n /**\n * Similar to streamChat() but returns the raw fetch Response object.\n * Useful for proxying the Server-Sent Events stream directly to a frontend client.\n *\n * @param messages - Full conversation history\n * @param options - Optional overrides (model, maxTokens, temperature)\n * @returns A Promise resolving to the native fetch Response\n */\n async streamChatResponse(messages: ChatMessage[], options: GenerateOptions = {}): Promise<Response> {\n this.assertInitialized();\n\n if (!messages.length) {\n throw new Error('Messages array cannot be empty for streamChatResponse');\n }\n\n const model = options.model ?? this.resolvedModel;\n\n const response = await fetch(`${this.serverUrl}/stream`, {\n method: 'POST',\n headers: {\n ...this.authHeaders(),\n Accept: 'text/event-stream',\n },\n body: JSON.stringify({\n messages,\n model,\n ...(options.system !== undefined ? { system: options.system } : {}),\n ...(options.images !== undefined ? { images: options.images } : {}),\n ...(options.documents !== undefined ? { documents: options.documents } : {}),\n ...(options.pdf !== undefined ? { pdf: options.pdf } : {}),\n ...(options.maxTokens !== undefined ? { maxTokens: options.maxTokens } : {}),\n ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),\n }),\n });\n\n if (!response.ok) {\n let errorMessage = `AI server returned HTTP ${response.status} for /stream`;\n try {\n const errorData = (await response.json()) as { message?: string; error?: string };\n if (errorData?.error) errorMessage = errorData.error;\n else if (errorData?.message) errorMessage = errorData.message;\n } catch { }\n throw new Error(errorMessage);\n }\n\n return response;\n }\n\n /**\n * Multi-turn conversation: pass a full message history.\n *\n * Calls: POST {serverUrl}/chat\n * Body: { messages, model, maxTokens?, temperature? }\n * Expected response: { text: string } or { content: string } or { message: string }\n *\n * @param messages - Full conversation history\n * @param options - Optional overrides (model, maxTokens, temperature)\n * @returns The assistant's response text\n */\n async chat(messages: ChatMessage[], options: GenerateOptions = {}): Promise<string> {\n this.assertInitialized();\n const model = options.model ?? this.resolvedModel;\n\n const response = await fetch(`${this.serverUrl}/chat`, {\n method: 'POST',\n headers: this.authHeaders(),\n body: JSON.stringify({\n messages,\n model,\n ...(options.images !== undefined ? { images: options.images } : {}),\n ...(options.documents !== undefined ? { documents: options.documents } : {}),\n ...(options.pdf !== undefined ? { pdf: options.pdf } : {}),\n ...(options.maxTokens !== undefined ? { maxTokens: options.maxTokens } : {}),\n ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),\n }),\n });\n\n if (!response.ok) {\n let errorMessage = `AI server returned HTTP ${response.status} for /chat`;\n try {\n const errorData = (await response.json()) as { message?: string; error?: string };\n if (errorData?.error) errorMessage = errorData.error;\n else if (errorData?.message) errorMessage = errorData.message;\n } catch { }\n throw new Error(errorMessage);\n }\n\n const data = (await response.json()) as {\n text?: string;\n content?: string;\n message?: string;\n };\n\n return data.text ?? data.content ?? data.message ?? '';\n }\n}\n","/**\n * @krutai/ai-provider — AI Provider package for KrutAI\n *\n * A fetch-based wrapper that calls your deployed LangChain backend server.\n * The user's API key is validated against the server before any AI call is made.\n *\n * @example Basic usage\n * ```typescript\n * import { krutAI } from '@krutai/ai-provider';\n *\n * const ai = krutAI({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://krut.ai',\n * });\n *\n * await ai.initialize(); // validates key with server\n *\n * const text = await ai.generate('Write a poem about TypeScript');\n * console.log(text);\n * ```\n *\n * @example With custom model\n * ```typescript\n * const ai = krutAI({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://krut.ai',\n * model: 'gpt-4o',\n * });\n * await ai.initialize();\n * const text = await ai.generate('Hello!');\n * ```\n *\n * @example Streaming\n * ```typescript\n * const ai = krutAI({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://krut.ai',\n * });\n * await ai.initialize();\n *\n * const stream = ai.stream('Tell me a story');\n * for await (const chunk of stream) {\n * process.stdout.write(chunk);\n * }\n * ```\n *\n * @packageDocumentation\n */\n\nimport type { KrutAIProviderConfig } from './types';\nimport { DEFAULT_MODEL } from './types';\nimport { KrutAIProvider } from './client';\n\nexport { KrutAIProvider } from './client';\nexport { KrutAIKeyValidationError } from './client';\nexport {\n validateApiKeyWithService as validateApiKey,\n validateApiKeyFormat,\n} from 'krutai';\nexport type { KrutAIProviderConfig, GenerateOptions, ChatMessage } from './types';\nexport { DEFAULT_MODEL } from './types';\n\n/**\n * krutAI — convenience factory (mirrors `krutAuth` in @krutai/auth).\n *\n * Creates a `KrutAIProvider` instance configured to call your LangChain server.\n *\n * @param config - Provider configuration (`apiKey` and `serverUrl` are required)\n * @returns A `KrutAIProvider` instance — call `.initialize()` before use\n *\n * @example\n * ```typescript\n * import { krutAI } from '@krutai/ai-provider';\n *\n * const ai = krutAI({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://krut.ai',\n * });\n *\n * await ai.initialize();\n * const text = await ai.generate('Hello!');\n * ```\n */\nexport function krutAI(\n config: KrutAIProviderConfig & { model?: string }\n): KrutAIProvider {\n return new KrutAIProvider({\n model: DEFAULT_MODEL,\n ...config,\n });\n}\n\n// Package metadata\nexport const VERSION = '0.2.0';\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","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AA,CAAQ,WAAA,EAAY,GAAI;AAAC,OACnF;AAAA,KACJ,CAAA;AAED,IAAA,IAAI,CAAC,SAAS,EAAA,EAAI;AACd,MAAA,IAAI,YAAA,GAAe,CAAA,wBAAA,EAA2B,QAAA,CAAS,MAAM,CAAA,cAAA,CAAA;AAC7D,MAAA,IAAI;AACA,QAAA,MAAM,SAAA,GAAa,MAAM,QAAA,CAAS,IAAA,EAAK;AACvC,QAAA,IAAI,SAAA,EAAW,KAAA,EAAO,YAAA,GAAe,SAAA,CAAU,KAAA;AAAA,aAAA,IACtC,SAAA,EAAW,OAAA,EAAS,YAAA,GAAe,SAAA,CAAU,OAAA;AAAA,MAC1D,CAAA,CAAA,MAAQ;AAAA,MAAE;AACV,MAAA,MAAM,IAAI,MAAM,YAAY,CAAA;AAAA,IAChC;AAEA,IAAA,MAAM,IAAA,GAAQ,MAAM,QAAA,CAAS,IAAA,EAAK;AAGlC,IAAA,IAAI,QAAQ,WAAA,EAAa;AACrB,MAAA,OAAO,IAAA;AAAA,IACX;AAGA,IAAA,OAAQ,IAAA,CAAK,IAAA,IAAQ,IAAA,CAAK,OAAA,IAAW,KAAK,OAAA,IAAW,EAAA;AAAA,EACzD;AACJ;ACxIO,SAAS,OACZ,MAAA,EACc;AACd,EAAA,OAAO,IAAI,cAAA,CAAe;AAAA,IACtB,KAAA,EAAO,aAAA;AAAA,IACP,GAAG;AAAA,GACN,CAAA;AACL;AAGO,IAAM,OAAA,GAAU","file":"index.mjs","sourcesContent":["/**\n * Types for @krutai/ai-provider\n */\n\n/**\n * Default model identifier sent to the LangChain server when no model is specified.\n * Your server can use this value to route to its own default model.\n */\nexport const DEFAULT_MODEL = 'gemini-3.1-pro-preview' as const;\n\n/**\n * Default base URL for the LangChain backend server.\n * Used when no serverUrl is provided in the config.\n */\nexport const DEFAULT_SERVER_URL = 'http://localhost:8000' as const;\n\n/**\n * Configuration options for KrutAIProvider\n */\nexport interface KrutAIProviderConfig {\n /**\n * KrutAI API key.\n * Validated against the LangChain server before use.\n * Optional: defaults to process.env.KRUTAI_API_KEY\n */\n apiKey?: string;\n\n /**\n * Base URL of your deployed LangChain backend server.\n * @default \"http://localhost:8000\"\n * @example \"https://ai.krut.ai\"\n */\n serverUrl?: string;\n\n /**\n * The AI model to use (passed to the server).\n * The server decides what to do with this value.\n * @default \"default\"\n */\n model?: string;\n\n /**\n * Whether to validate the API key against the server on initialization.\n * Set to false to skip the validation round-trip (e.g. in tests).\n * @default true\n */\n validateOnInit?: boolean;\n}\n\n/**\n * A part of a multimodal message\n */\nexport interface TextContentPart {\n type: 'text';\n text: string;\n}\n\nexport interface ImageContentPart {\n type: 'image_url';\n image_url: {\n url: string; // Base64 data URI or HTTP(S) URL\n detail?: 'low' | 'high' | 'auto';\n };\n}\n\nexport type ContentPart = TextContentPart | ImageContentPart;\nexport type MessageContent = string | ContentPart[];\n\n/**\n * A single chat message\n */\nexport interface ChatMessage {\n role: 'user' | 'assistant' | 'system';\n content: MessageContent;\n}\n\n/**\n * Options for a single generate / stream / chat call\n */\nexport interface GenerateOptions {\n /**\n * Override the model for this specific call.\n */\n model?: string;\n\n /**\n * System prompt (prepended as a system message).\n */\n system?: string;\n\n /**\n * Maximum tokens to generate.\n */\n maxTokens?: number;\n\n /**\n * Temperature (0–2).\n */\n temperature?: number;\n\n /**\n * Array of image URLs or base64 data URIs to include with the request.\n */\n images?: string[];\n\n /**\n * Array of document URLs or base64 data URIs (e.g. PDFs) to include with the request.\n */\n documents?: string[];\n\n /**\n * Array of PDF URLs or base64 data URIs to include with the request.\n */\n pdf?: string[];\n\n /**\n * Optional conversation history.\n */\n history?: ChatMessage[];\n\n /**\n * Optional attachments.\n */\n attachments?: any[];\n\n /**\n * Whether to return structured output.\n */\n isStructure?: boolean;\n\n /**\n * The schema for structured output.\n * Can be a JSON Schema object or an array of field names.\n */\n output_structure?: any;\n}\n","import type { KrutAIProviderConfig, GenerateOptions, ChatMessage } from './types';\nimport { DEFAULT_MODEL, DEFAULT_SERVER_URL } from './types';\nimport {\n validateApiKeyWithService as validateApiKey,\n validateApiKeyFormat,\n ApiKeyValidationError as KrutAIKeyValidationError,\n} from 'krutai';\n\nexport { KrutAIKeyValidationError };\n\n/**\n * KrutAIProvider — fetch-based AI provider for KrutAI\n *\n * Calls your deployed LangChain backend server for all AI operations.\n * The API key is validated against the server before use.\n *\n * @example\n * ```typescript\n * import { KrutAIProvider } from '@krutai/ai-provider';\n *\n * // Using local dev server (http://localhost:8000 by default)\n * const ai = new KrutAIProvider({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * });\n *\n * // Or point to a production server\n * const aiProd = new KrutAIProvider({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://ai.krut.ai',\n * });\n *\n * await ai.initialize(); // validates key against server\n *\n * const text = await ai.generate('Tell me a joke');\n * console.log(text);\n * ```\n */\nexport class KrutAIProvider {\n private readonly apiKey: string;\n private readonly serverUrl: string;\n private readonly resolvedModel: string;\n private readonly config: KrutAIProviderConfig;\n\n private initialized = false;\n\n constructor(config: KrutAIProviderConfig) {\n this.config = config;\n this.apiKey = config.apiKey || process.env.KRUTAI_API_KEY || '';\n this.serverUrl = (config.serverUrl ?? DEFAULT_SERVER_URL).replace(/\\/$/, ''); // strip trailing slash\n this.resolvedModel = config.model ?? DEFAULT_MODEL;\n\n // Basic format check immediately on construction\n validateApiKeyFormat(this.apiKey);\n\n // If validation is disabled, mark as ready immediately\n if (config.validateOnInit === false) {\n this.initialized = true;\n }\n }\n\n /**\n * Initialize the provider.\n * Validates the API key against the LangChain server, then marks provider as ready.\n *\n * @throws {KrutAIKeyValidationError} if the key is rejected or the server is unreachable\n */\n async initialize(): Promise<void> {\n if (this.initialized) return;\n\n if (this.config.validateOnInit !== false) {\n await validateApiKey(this.apiKey, this.serverUrl);\n }\n\n this.initialized = true;\n }\n\n /**\n * Returns the currently configured default model.\n */\n getModel(): string {\n return this.resolvedModel;\n }\n\n /**\n * Returns whether the provider has been initialized.\n */\n isInitialized(): boolean {\n return this.initialized;\n }\n\n // ---------------------------------------------------------------------------\n // Private helpers\n // ---------------------------------------------------------------------------\n\n private assertInitialized(): void {\n if (!this.initialized) {\n throw new Error(\n 'KrutAIProvider not initialized. Call initialize() first or set validateOnInit to false.'\n );\n }\n }\n\n /** Common request headers sent to the server on every AI call. */\n private authHeaders(): Record<string, string> {\n return {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.apiKey}`,\n 'x-api-key': this.apiKey,\n };\n }\n\n // ---------------------------------------------------------------------------\n // Public AI Methods\n // ---------------------------------------------------------------------------\n\n /**\n * Similar to streamChat() but returns the raw fetch Response object.\n * Useful for proxying the Server-Sent Events stream directly to a frontend client.\n *\n * @param messages - Full conversation history\n * @param options - Optional overrides (model, maxTokens, temperature)\n * @returns A Promise resolving to the native fetch Response\n */\n async streamChatResponse(messages: ChatMessage[], options: GenerateOptions = {}): Promise<Response> {\n this.assertInitialized();\n\n if (!messages.length) {\n throw new Error('Messages array cannot be empty for streamChatResponse');\n }\n\n const model = options.model ?? this.resolvedModel;\n\n const response = await fetch(`${this.serverUrl}/stream`, {\n method: 'POST',\n headers: {\n ...this.authHeaders(),\n Accept: 'text/event-stream',\n },\n body: JSON.stringify({\n messages,\n model,\n ...(options.system !== undefined ? { system: options.system } : {}),\n ...(options.images !== undefined ? { images: options.images } : {}),\n ...(options.documents !== undefined ? { documents: options.documents } : {}),\n ...(options.pdf !== undefined ? { pdf: options.pdf } : {}),\n ...(options.maxTokens !== undefined ? { maxTokens: options.maxTokens } : {}),\n ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),\n }),\n });\n\n if (!response.ok) {\n let errorMessage = `AI server returned HTTP ${response.status} for /stream`;\n try {\n const errorData = (await response.json()) as { message?: string; error?: string };\n if (errorData?.error) errorMessage = errorData.error;\n else if (errorData?.message) errorMessage = errorData.message;\n } catch { }\n throw new Error(errorMessage);\n }\n\n return response;\n }\n\n /**\n * Generate a response for a prompt (non-streaming).\n *\n * Calls: POST {serverUrl}/generate\n * Body: { prompt, model, system?, maxTokens?, temperature? }\n * Expected response: { text: string } or { content: string } or { message: string }\n *\n * @param prompt - The user prompt string\n * @param options - Optional overrides (model, system, maxTokens, temperature)\n * @returns The assistant's response text (or an object if structured)\n */\n async chat<T = any>(prompt: string, options: GenerateOptions = {}): Promise<T> {\n this.assertInitialized();\n const model = options.model ?? this.resolvedModel;\n\n const response = await fetch(`${this.serverUrl}/generate`, {\n method: 'POST',\n headers: this.authHeaders(),\n body: JSON.stringify({\n prompt,\n model,\n ...(options.system !== undefined ? { system: options.system } : {}),\n ...(options.images !== undefined ? { images: options.images } : {}),\n ...(options.documents !== undefined ? { documents: options.documents } : {}),\n ...(options.pdf !== undefined ? { pdf: options.pdf } : {}),\n ...(options.maxTokens !== undefined ? { maxTokens: options.maxTokens } : {}),\n ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),\n ...(options.isStructure !== undefined ? { isStructure: options.isStructure } : {}),\n ...(options.output_structure !== undefined ? { output_structure: options.output_structure } : {}),\n ...(options.history !== undefined ? { history: options.history } : {}),\n ...(options.attachments !== undefined ? { attachments: options.attachments } : {}),\n }),\n });\n\n if (!response.ok) {\n let errorMessage = `AI server returned HTTP ${response.status} for /generate`;\n try {\n const errorData = (await response.json()) as { message?: string; error?: string };\n if (errorData?.error) errorMessage = errorData.error;\n else if (errorData?.message) errorMessage = errorData.message;\n } catch { }\n throw new Error(errorMessage);\n }\n\n const data = (await response.json()) as any;\n\n // If isStructure was set, return the full object.\n if (options.isStructure) {\n return data as T;\n }\n\n // Otherwise return text/content/message or empty string\n return (data.text ?? data.content ?? data.message ?? '') as T;\n }\n}\n","/**\n * @krutai/ai-provider — AI Provider package for KrutAI\n *\n * A fetch-based wrapper that calls your deployed LangChain backend server.\n * The user's API key is validated against the server before any AI call is made.\n *\n * @example Basic usage\n * ```typescript\n * import { krutAI } from '@krutai/ai-provider';\n *\n * const ai = krutAI({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://krut.ai',\n * });\n *\n * await ai.initialize(); // validates key with server\n *\n * const text = await ai.chat('Write a poem about TypeScript');\n * console.log(text);\n * ```\n *\n * @example With custom model\n * ```typescript\n * const ai = krutAI({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://krut.ai',\n * model: 'gemini-3.1-pro-preview',\n * });\n * await ai.initialize();\n * const text = await ai.chat('Hello!');\n * ```\n *\n * @example Streaming\n * ```typescript\n * const ai = krutAI({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://krut.ai',\n * });\n * await ai.initialize();\n *\n * const response = await ai.streamChatResponse([{ role: 'user', content: 'Tell me a story' }]);\n * // Example assumes you handle the SSE stream from the response body\n * ```\n *\n * @packageDocumentation\n */\n\nimport type { KrutAIProviderConfig } from './types';\nimport { DEFAULT_MODEL } from './types';\nimport { KrutAIProvider } from './client';\n\nexport { KrutAIProvider } from './client';\nexport { KrutAIKeyValidationError } from './client';\nexport {\n validateApiKeyWithService as validateApiKey,\n validateApiKeyFormat,\n} from 'krutai';\nexport type { KrutAIProviderConfig, GenerateOptions, ChatMessage } from './types';\nexport { DEFAULT_MODEL } from './types';\n\n/**\n * krutAI — convenience factory (mirrors `krutAuth` in @krutai/auth).\n *\n * Creates a `KrutAIProvider` instance configured to call your LangChain server.\n *\n * @param config - Provider configuration (`apiKey` and `serverUrl` are required)\n * @returns A `KrutAIProvider` instance — call `.initialize()` before use\n *\n * @example\n * ```typescript\n * import { krutAI } from '@krutai/ai-provider';\n *\n * const ai = krutAI({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * serverUrl: 'https://krut.ai',\n * });\n *\n * await ai.initialize();\n * const text = await ai.chat('Hello!');\n * ```\n */\nexport function krutAI(\n config: KrutAIProviderConfig & { model?: string }\n): KrutAIProvider {\n return new KrutAIProvider({\n model: DEFAULT_MODEL,\n ...config,\n });\n}\n\n// Package metadata\nexport const VERSION = '0.2.15';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krutai/ai-provider",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.15",
|
|
4
4
|
"description": "AI provider package for KrutAI — fetch-based client for your deployed LangChain server with API key validation",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|