@node-llm/core 0.1.0 → 0.2.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 +119 -132
- package/dist/chat/Chat.d.ts +29 -10
- package/dist/chat/Chat.d.ts.map +1 -1
- package/dist/chat/Chat.js +61 -23
- package/dist/chat/Content.d.ts +1 -1
- package/dist/chat/Content.d.ts.map +1 -1
- package/dist/chat/Message.d.ts +2 -0
- package/dist/chat/Message.d.ts.map +1 -1
- package/dist/chat/Stream.d.ts +21 -0
- package/dist/chat/Stream.d.ts.map +1 -0
- package/dist/chat/Stream.js +57 -0
- package/dist/errors/index.d.ts +66 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +97 -0
- package/dist/executor/Executor.d.ts.map +1 -1
- package/dist/executor/Executor.js +4 -1
- package/dist/image/GeneratedImage.d.ts +25 -0
- package/dist/image/GeneratedImage.d.ts.map +1 -0
- package/dist/image/GeneratedImage.js +59 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/llm.d.ts +6 -0
- package/dist/llm.d.ts.map +1 -1
- package/dist/llm.js +15 -1
- package/dist/providers/Provider.d.ts +21 -0
- package/dist/providers/Provider.d.ts.map +1 -1
- package/dist/providers/openai/Chat.d.ts.map +1 -1
- package/dist/providers/openai/Chat.js +9 -3
- package/dist/providers/openai/Errors.d.ts +2 -0
- package/dist/providers/openai/Errors.d.ts.map +1 -0
- package/dist/providers/openai/Errors.js +34 -0
- package/dist/providers/openai/Image.d.ts +8 -0
- package/dist/providers/openai/Image.d.ts.map +1 -0
- package/dist/providers/openai/Image.js +39 -0
- package/dist/providers/openai/OpenAIProvider.d.ts +3 -1
- package/dist/providers/openai/OpenAIProvider.d.ts.map +1 -1
- package/dist/providers/openai/OpenAIProvider.js +6 -0
- package/dist/providers/openai/Streaming.d.ts.map +1 -1
- package/dist/providers/openai/Streaming.js +4 -0
- package/dist/providers/openai/types.d.ts +8 -0
- package/dist/providers/openai/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for all node-llm errors
|
|
3
|
+
*/
|
|
4
|
+
export declare class LLMError extends Error {
|
|
5
|
+
readonly code?: string | undefined;
|
|
6
|
+
constructor(message: string, code?: string | undefined);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Errors occurring during API calls to providers
|
|
10
|
+
*/
|
|
11
|
+
export declare class APIError extends LLMError {
|
|
12
|
+
readonly status: number;
|
|
13
|
+
readonly body: any;
|
|
14
|
+
readonly provider?: string | undefined;
|
|
15
|
+
readonly model?: string | undefined;
|
|
16
|
+
constructor(message: string, status: number, body: any, provider?: string | undefined, model?: string | undefined);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 400 - Invalid request parameters
|
|
20
|
+
*/
|
|
21
|
+
export declare class BadRequestError extends APIError {
|
|
22
|
+
constructor(message: string, body: any, provider?: string, model?: string);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 401/403 - API key or permission issues
|
|
26
|
+
*/
|
|
27
|
+
export declare class AuthenticationError extends APIError {
|
|
28
|
+
constructor(message: string, status: number, body: any, provider?: string);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 429 - Rate limit exceeded
|
|
32
|
+
*/
|
|
33
|
+
export declare class RateLimitError extends APIError {
|
|
34
|
+
constructor(message: string, body: any, provider?: string, model?: string);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 500+ - Provider server error
|
|
38
|
+
*/
|
|
39
|
+
export declare class ServerError extends APIError {
|
|
40
|
+
constructor(message: string, status: number, body: any, provider?: string, model?: string);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 502/503/529 - Service overloaded/unavailable
|
|
44
|
+
*/
|
|
45
|
+
export declare class ServiceUnavailableError extends ServerError {
|
|
46
|
+
constructor(message: string, status: number, body: any, provider?: string, model?: string);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Misconfiguration (e.g. missing API key)
|
|
50
|
+
*/
|
|
51
|
+
export declare class ConfigurationError extends LLMError {
|
|
52
|
+
constructor(message: string);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Requested model or provider not found
|
|
56
|
+
*/
|
|
57
|
+
export declare class NotFoundError extends LLMError {
|
|
58
|
+
constructor(message: string);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Model does not support requested capability
|
|
62
|
+
*/
|
|
63
|
+
export declare class CapabilityError extends LLMError {
|
|
64
|
+
constructor(message: string);
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/errors/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,QAAS,SAAQ,KAAK;aACY,IAAI,CAAC,EAAE,MAAM;gBAA9C,OAAO,EAAE,MAAM,EAAkB,IAAI,CAAC,EAAE,MAAM,YAAA;CAK3D;AAED;;GAEG;AACH,qBAAa,QAAS,SAAQ,QAAQ;aAGlB,MAAM,EAAE,MAAM;aACd,IAAI,EAAE,GAAG;aACT,QAAQ,CAAC,EAAE,MAAM;aACjB,KAAK,CAAC,EAAE,MAAM;gBAJ9B,OAAO,EAAE,MAAM,EACC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,GAAG,EACT,QAAQ,CAAC,EAAE,MAAM,YAAA,EACjB,KAAK,CAAC,EAAE,MAAM,YAAA;CAIjC;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,QAAQ;gBAC/B,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM;CAI1E;AAED;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,QAAQ;gBACnC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,MAAM;CAI1E;AAED;;GAEG;AACH,qBAAa,cAAe,SAAQ,QAAQ;gBAC9B,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM;CAI1E;AAED;;GAEG;AACH,qBAAa,WAAY,SAAQ,QAAQ;gBAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM;CAI1F;AAED;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,WAAW;gBAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM;CAI1F;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,QAAQ;gBAClC,OAAO,EAAE,MAAM;CAG5B;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,QAAQ;gBAC7B,OAAO,EAAE,MAAM;CAG5B;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,QAAQ;gBAC/B,OAAO,EAAE,MAAM;CAG5B"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for all node-llm errors
|
|
3
|
+
*/
|
|
4
|
+
export class LLMError extends Error {
|
|
5
|
+
code;
|
|
6
|
+
constructor(message, code) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.name = this.constructor.name;
|
|
10
|
+
Error.captureStackTrace(this, this.constructor);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Errors occurring during API calls to providers
|
|
15
|
+
*/
|
|
16
|
+
export class APIError extends LLMError {
|
|
17
|
+
status;
|
|
18
|
+
body;
|
|
19
|
+
provider;
|
|
20
|
+
model;
|
|
21
|
+
constructor(message, status, body, provider, model) {
|
|
22
|
+
super(message, "API_ERROR");
|
|
23
|
+
this.status = status;
|
|
24
|
+
this.body = body;
|
|
25
|
+
this.provider = provider;
|
|
26
|
+
this.model = model;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 400 - Invalid request parameters
|
|
31
|
+
*/
|
|
32
|
+
export class BadRequestError extends APIError {
|
|
33
|
+
constructor(message, body, provider, model) {
|
|
34
|
+
super(message, 400, body, provider, model);
|
|
35
|
+
this.name = "BadRequestError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 401/403 - API key or permission issues
|
|
40
|
+
*/
|
|
41
|
+
export class AuthenticationError extends APIError {
|
|
42
|
+
constructor(message, status, body, provider) {
|
|
43
|
+
super(message, status, body, provider);
|
|
44
|
+
this.name = "AuthenticationError";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 429 - Rate limit exceeded
|
|
49
|
+
*/
|
|
50
|
+
export class RateLimitError extends APIError {
|
|
51
|
+
constructor(message, body, provider, model) {
|
|
52
|
+
super(message, 429, body, provider, model);
|
|
53
|
+
this.name = "RateLimitError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 500+ - Provider server error
|
|
58
|
+
*/
|
|
59
|
+
export class ServerError extends APIError {
|
|
60
|
+
constructor(message, status, body, provider, model) {
|
|
61
|
+
super(message, status, body, provider, model);
|
|
62
|
+
this.name = "ServerError";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 502/503/529 - Service overloaded/unavailable
|
|
67
|
+
*/
|
|
68
|
+
export class ServiceUnavailableError extends ServerError {
|
|
69
|
+
constructor(message, status, body, provider, model) {
|
|
70
|
+
super(message, status, body, provider, model);
|
|
71
|
+
this.name = "ServiceUnavailableError";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Misconfiguration (e.g. missing API key)
|
|
76
|
+
*/
|
|
77
|
+
export class ConfigurationError extends LLMError {
|
|
78
|
+
constructor(message) {
|
|
79
|
+
super(message, "CONFIGURATION_ERROR");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Requested model or provider not found
|
|
84
|
+
*/
|
|
85
|
+
export class NotFoundError extends LLMError {
|
|
86
|
+
constructor(message) {
|
|
87
|
+
super(message, "NOT_FOUND_ERROR");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Model does not support requested capability
|
|
92
|
+
*/
|
|
93
|
+
export class CapabilityError extends LLMError {
|
|
94
|
+
constructor(message) {
|
|
95
|
+
super(message, "CAPABILITY_ERROR");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Executor.d.ts","sourceRoot":"","sources":["../../src/executor/Executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"Executor.d.ts","sourceRoot":"","sources":["../../src/executor/Executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAG/E,qBAAa,QAAQ;IAEjB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,KAAK;gBADL,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;IAGzD,WAAW,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;CAwB/D"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RateLimitError, ServerError } from "../errors/index.js";
|
|
1
2
|
export class Executor {
|
|
2
3
|
provider;
|
|
3
4
|
retry;
|
|
@@ -13,7 +14,9 @@ export class Executor {
|
|
|
13
14
|
}
|
|
14
15
|
catch (error) {
|
|
15
16
|
lastError = error;
|
|
16
|
-
|
|
17
|
+
// If it's a fatal error (BadRequest, Authentication), don't retry
|
|
18
|
+
const isRetryable = error instanceof RateLimitError || error instanceof ServerError;
|
|
19
|
+
if (!isRetryable || attempt >= this.retry.attempts) {
|
|
17
20
|
throw error;
|
|
18
21
|
}
|
|
19
22
|
if (this.retry.delayMs > 0) {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Readable } from "stream";
|
|
2
|
+
import { ImageResponse } from "../providers/Provider.js";
|
|
3
|
+
export declare class GeneratedImage extends String {
|
|
4
|
+
private readonly response;
|
|
5
|
+
constructor(response: ImageResponse);
|
|
6
|
+
get url(): string | undefined;
|
|
7
|
+
get data(): string | undefined;
|
|
8
|
+
get revisedPrompt(): string | undefined;
|
|
9
|
+
get mimeType(): string | undefined;
|
|
10
|
+
get isBase64(): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Returns the raw binary image data as a Buffer.
|
|
13
|
+
*/
|
|
14
|
+
toBuffer(): Promise<Buffer>;
|
|
15
|
+
/**
|
|
16
|
+
* Returns a Readable stream of the image data.
|
|
17
|
+
*/
|
|
18
|
+
toStream(): Promise<Readable>;
|
|
19
|
+
/**
|
|
20
|
+
* Saves the image to the specified local path.
|
|
21
|
+
*/
|
|
22
|
+
save(path: string): Promise<string>;
|
|
23
|
+
toString(): string;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=GeneratedImage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeneratedImage.d.ts","sourceRoot":"","sources":["../../src/image/GeneratedImage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,qBAAa,cAAe,SAAQ,MAAM;IAC5B,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,aAAa;IAIpD,IAAI,GAAG,IAAI,MAAM,GAAG,SAAS,CAE5B;IAED,IAAI,IAAI,IAAI,MAAM,GAAG,SAAS,CAE7B;IAED,IAAI,aAAa,IAAI,MAAM,GAAG,SAAS,CAEtC;IAED,IAAI,QAAQ,IAAI,MAAM,GAAG,SAAS,CAEjC;IAED,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC;IAiBjC;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAC;IAKnC;;OAEG;IACG,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAMzC,QAAQ;CAGT"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import fs from "fs/promises";
|
|
2
|
+
import { Readable } from "stream";
|
|
3
|
+
export class GeneratedImage extends String {
|
|
4
|
+
response;
|
|
5
|
+
constructor(response) {
|
|
6
|
+
super(response.url || "");
|
|
7
|
+
this.response = response;
|
|
8
|
+
}
|
|
9
|
+
get url() {
|
|
10
|
+
return this.response.url;
|
|
11
|
+
}
|
|
12
|
+
get data() {
|
|
13
|
+
return this.response.data;
|
|
14
|
+
}
|
|
15
|
+
get revisedPrompt() {
|
|
16
|
+
return this.response.revised_prompt;
|
|
17
|
+
}
|
|
18
|
+
get mimeType() {
|
|
19
|
+
return this.response.mime_type;
|
|
20
|
+
}
|
|
21
|
+
get isBase64() {
|
|
22
|
+
return !!this.data;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Returns the raw binary image data as a Buffer.
|
|
26
|
+
*/
|
|
27
|
+
async toBuffer() {
|
|
28
|
+
if (this.data) {
|
|
29
|
+
return Buffer.from(this.data, "base64");
|
|
30
|
+
}
|
|
31
|
+
if (this.url) {
|
|
32
|
+
const resp = await fetch(this.url);
|
|
33
|
+
if (!resp.ok) {
|
|
34
|
+
throw new Error(`Failed to download image from ${this.url}: ${resp.statusText}`);
|
|
35
|
+
}
|
|
36
|
+
const arrayBuffer = await resp.arrayBuffer();
|
|
37
|
+
return Buffer.from(arrayBuffer);
|
|
38
|
+
}
|
|
39
|
+
throw new Error("No image data or URL available");
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns a Readable stream of the image data.
|
|
43
|
+
*/
|
|
44
|
+
async toStream() {
|
|
45
|
+
const buffer = await this.toBuffer();
|
|
46
|
+
return Readable.from(buffer);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Saves the image to the specified local path.
|
|
50
|
+
*/
|
|
51
|
+
async save(path) {
|
|
52
|
+
const buffer = await this.toBuffer();
|
|
53
|
+
await fs.writeFile(path, buffer);
|
|
54
|
+
return path;
|
|
55
|
+
}
|
|
56
|
+
toString() {
|
|
57
|
+
return this.valueOf();
|
|
58
|
+
}
|
|
59
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { Chat } from "./chat/Chat.js";
|
|
2
|
+
export { Stream } from "./chat/Stream.js";
|
|
3
|
+
export { GeneratedImage } from "./image/GeneratedImage.js";
|
|
2
4
|
export type { Message } from "./chat/Message.js";
|
|
3
5
|
export type { Role } from "./chat/Role.js";
|
|
4
6
|
export type { ChatOptions } from "./chat/ChatOptions.js";
|
|
@@ -8,4 +10,6 @@ export { LLM } from "./llm.js";
|
|
|
8
10
|
export { providerRegistry } from "./providers/registry.js";
|
|
9
11
|
export { OpenAIProvider } from "./providers/openai/OpenAIProvider.js";
|
|
10
12
|
export { registerOpenAIProvider } from "./providers/openai/index.js";
|
|
13
|
+
export type { ImageRequest, ImageResponse } from "./providers/Provider.js";
|
|
14
|
+
export * from "./errors/index.js";
|
|
11
15
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,YAAY,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC3C,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACzD,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACrD,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErE,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,YAAY,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC3C,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACzD,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACrD,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErE,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAC3E,cAAc,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export { Chat } from "./chat/Chat.js";
|
|
2
|
+
export { Stream } from "./chat/Stream.js";
|
|
3
|
+
export { GeneratedImage } from "./image/GeneratedImage.js";
|
|
2
4
|
export { LLM } from "./llm.js";
|
|
3
5
|
export { providerRegistry } from "./providers/registry.js";
|
|
4
6
|
export { OpenAIProvider } from "./providers/openai/OpenAIProvider.js";
|
|
5
7
|
export { registerOpenAIProvider } from "./providers/openai/index.js";
|
|
8
|
+
export * from "./errors/index.js";
|
package/dist/llm.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Chat } from "./chat/Chat.js";
|
|
2
2
|
import { ChatOptions } from "./chat/ChatOptions.js";
|
|
3
3
|
import { Provider } from "./providers/Provider.js";
|
|
4
|
+
import { GeneratedImage } from "./image/GeneratedImage.js";
|
|
4
5
|
export interface RetryOptions {
|
|
5
6
|
attempts?: number;
|
|
6
7
|
delayMs?: number;
|
|
@@ -18,6 +19,11 @@ declare class LLMCore {
|
|
|
18
19
|
configure(config: LLMConfig): void;
|
|
19
20
|
chat(model: string, options?: ChatOptions): Chat;
|
|
20
21
|
listModels(): Promise<import("./providers/Provider.js").ModelInfo[]>;
|
|
22
|
+
paint(prompt: string, options?: {
|
|
23
|
+
model?: string;
|
|
24
|
+
size?: string;
|
|
25
|
+
quality?: string;
|
|
26
|
+
}): Promise<GeneratedImage>;
|
|
21
27
|
getRetryConfig(): Required<RetryOptions>;
|
|
22
28
|
}
|
|
23
29
|
export declare const LLM: LLMCore;
|
package/dist/llm.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAGnD,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,KAAK,SAAS,GACV;IAAE,QAAQ,EAAE,QAAQ,CAAC;IAAC,KAAK,CAAC,EAAE,YAAY,CAAA;CAAE,GAC5C;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,YAAY,CAAA;CAAE,CAAC;AAE/C,cAAM,OAAO;IACX,OAAO,CAAC,QAAQ,CAAC,CAAW;IAE5B,OAAO,CAAC,KAAK,CAGX;IAEF,SAAS,CAAC,MAAM,EAAE,SAAS;IAmB3B,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,IAAI;IAQ1C,UAAU;IAUV,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,cAAc,CAAC;IAgBnH,cAAc;CAIf;AAED,eAAO,MAAM,GAAG,SAAgB,CAAC"}
|
package/dist/llm.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Chat } from "./chat/Chat.js";
|
|
2
2
|
import { providerRegistry } from "./providers/registry.js";
|
|
3
3
|
import { ensureOpenAIRegistered } from "./providers/openai/register.js";
|
|
4
|
+
import { GeneratedImage } from "./image/GeneratedImage.js";
|
|
4
5
|
class LLMCore {
|
|
5
6
|
provider;
|
|
6
7
|
retry = {
|
|
@@ -35,10 +36,23 @@ class LLMCore {
|
|
|
35
36
|
throw new Error("LLM provider not configured");
|
|
36
37
|
}
|
|
37
38
|
if (!this.provider.listModels) {
|
|
38
|
-
throw new Error(`Provider
|
|
39
|
+
throw new Error(`Provider does not support listModels`);
|
|
39
40
|
}
|
|
40
41
|
return this.provider.listModels();
|
|
41
42
|
}
|
|
43
|
+
async paint(prompt, options) {
|
|
44
|
+
if (!this.provider) {
|
|
45
|
+
throw new Error("LLM provider not configured");
|
|
46
|
+
}
|
|
47
|
+
if (!this.provider.paint) {
|
|
48
|
+
throw new Error(`Provider does not support paint`);
|
|
49
|
+
}
|
|
50
|
+
const response = await this.provider.paint({
|
|
51
|
+
prompt,
|
|
52
|
+
...options,
|
|
53
|
+
});
|
|
54
|
+
return new GeneratedImage(response);
|
|
55
|
+
}
|
|
42
56
|
getRetryConfig() {
|
|
43
57
|
return this.retry;
|
|
44
58
|
}
|
|
@@ -11,9 +11,16 @@ export interface ChatChunk {
|
|
|
11
11
|
content: string;
|
|
12
12
|
done?: boolean;
|
|
13
13
|
}
|
|
14
|
+
export interface Usage {
|
|
15
|
+
input_tokens: number;
|
|
16
|
+
output_tokens: number;
|
|
17
|
+
total_tokens: number;
|
|
18
|
+
cached_tokens?: number;
|
|
19
|
+
}
|
|
14
20
|
export interface ChatResponse {
|
|
15
21
|
content: string | null;
|
|
16
22
|
tool_calls?: ToolCall[];
|
|
23
|
+
usage?: Usage;
|
|
17
24
|
}
|
|
18
25
|
export interface ProviderCapabilities {
|
|
19
26
|
supportsVision: (model: string) => boolean;
|
|
@@ -36,10 +43,24 @@ export interface ModelInfo {
|
|
|
36
43
|
pricing: any;
|
|
37
44
|
metadata?: Record<string, any>;
|
|
38
45
|
}
|
|
46
|
+
export interface ImageRequest {
|
|
47
|
+
model?: string;
|
|
48
|
+
prompt: string;
|
|
49
|
+
size?: string;
|
|
50
|
+
quality?: string;
|
|
51
|
+
n?: number;
|
|
52
|
+
}
|
|
53
|
+
export interface ImageResponse {
|
|
54
|
+
url?: string;
|
|
55
|
+
data?: string;
|
|
56
|
+
mime_type?: string;
|
|
57
|
+
revised_prompt?: string;
|
|
58
|
+
}
|
|
39
59
|
export interface Provider {
|
|
40
60
|
chat(request: ChatRequest): Promise<ChatResponse>;
|
|
41
61
|
stream?(request: ChatRequest): AsyncIterable<ChatChunk>;
|
|
42
62
|
listModels?(): Promise<ModelInfo[]>;
|
|
63
|
+
paint?(request: ImageRequest): Promise<ImageResponse>;
|
|
43
64
|
capabilities?: ProviderCapabilities;
|
|
44
65
|
}
|
|
45
66
|
//# sourceMappingURL=Provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../../src/providers/Provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAGjD,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,UAAU,CAAC,EAAE,QAAQ,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../../src/providers/Provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAGjD,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,WAAW,KAAK;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,UAAU,CAAC,EAAE,QAAQ,EAAE,CAAC;IACxB,KAAK,CAAC,EAAE,KAAK,CAAC;CACf;AAED,MAAM,WAAW,oBAAoB;IACnC,cAAc,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IAC3C,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IAC1C,wBAAwB,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IACrD,gBAAgB,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;CACpD;AAED,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,UAAU,EAAE;QAAE,KAAK,EAAE,MAAM,EAAE,CAAC;QAAC,MAAM,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAClD,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,OAAO,EAAE,GAAG,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,YAAY;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,CAAC,CAAC,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,aAAa;IAC5B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,QAAQ;IACvB,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAClD,MAAM,CAAC,CAAC,OAAO,EAAE,WAAW,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;IACxD,UAAU,CAAC,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;IACpC,KAAK,CAAC,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACtD,YAAY,CAAC,EAAE,oBAAoB,CAAC;CACrC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Chat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Chat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAK3D,qBAAa,UAAU;IACT,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAmB,MAAM,EAAE,MAAM;IAEvE,OAAO,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;CAqD3D"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Capabilities } from "./Capabilities.js";
|
|
2
|
+
import { handleOpenAIError } from "./Errors.js";
|
|
2
3
|
export class OpenAIChat {
|
|
3
4
|
baseUrl;
|
|
4
5
|
apiKey;
|
|
@@ -32,16 +33,21 @@ export class OpenAIChat {
|
|
|
32
33
|
body: JSON.stringify(body),
|
|
33
34
|
});
|
|
34
35
|
if (!response.ok) {
|
|
35
|
-
|
|
36
|
-
throw new Error(`OpenAI error (${response.status}): ${errorText}`);
|
|
36
|
+
await handleOpenAIError(response, request.model);
|
|
37
37
|
}
|
|
38
38
|
const json = (await response.json());
|
|
39
39
|
const message = json.choices[0]?.message;
|
|
40
40
|
const content = message?.content ?? null;
|
|
41
41
|
const tool_calls = message?.tool_calls;
|
|
42
|
+
const usage = json.usage ? {
|
|
43
|
+
input_tokens: json.usage.prompt_tokens,
|
|
44
|
+
output_tokens: json.usage.completion_tokens,
|
|
45
|
+
total_tokens: json.usage.total_tokens,
|
|
46
|
+
cached_tokens: json.usage.prompt_tokens_details?.cached_tokens,
|
|
47
|
+
} : undefined;
|
|
42
48
|
if (!content && !tool_calls) {
|
|
43
49
|
throw new Error("OpenAI returned empty response");
|
|
44
50
|
}
|
|
45
|
-
return { content, tool_calls };
|
|
51
|
+
return { content, tool_calls, usage };
|
|
46
52
|
}
|
|
47
53
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Errors.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Errors.ts"],"names":[],"mappings":"AASA,wBAAsB,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,CAuC1F"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { BadRequestError, AuthenticationError, RateLimitError, ServerError, ServiceUnavailableError, APIError } from "../../errors/index.js";
|
|
2
|
+
export async function handleOpenAIError(response, model) {
|
|
3
|
+
const status = response.status;
|
|
4
|
+
let body;
|
|
5
|
+
let message = `OpenAI error (${status})`;
|
|
6
|
+
try {
|
|
7
|
+
body = await response.json();
|
|
8
|
+
if (body?.error?.message) {
|
|
9
|
+
message = body.error.message;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
// If not JSON, use the status text
|
|
14
|
+
body = await response.text().catch(() => "Unknown error");
|
|
15
|
+
message = `OpenAI error (${status}): ${body}`;
|
|
16
|
+
}
|
|
17
|
+
const provider = "openai";
|
|
18
|
+
if (status === 400) {
|
|
19
|
+
throw new BadRequestError(message, body, provider, model);
|
|
20
|
+
}
|
|
21
|
+
if (status === 401 || status === 403) {
|
|
22
|
+
throw new AuthenticationError(message, status, body, provider);
|
|
23
|
+
}
|
|
24
|
+
if (status === 429) {
|
|
25
|
+
throw new RateLimitError(message, body, provider, model);
|
|
26
|
+
}
|
|
27
|
+
if (status === 502 || status === 503) {
|
|
28
|
+
throw new ServiceUnavailableError(message, status, body, provider, model);
|
|
29
|
+
}
|
|
30
|
+
if (status >= 500) {
|
|
31
|
+
throw new ServerError(message, status, body, provider, model);
|
|
32
|
+
}
|
|
33
|
+
throw new APIError(message, status, body, provider, model);
|
|
34
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ImageRequest, ImageResponse } from "../Provider.js";
|
|
2
|
+
export declare class OpenAIImage {
|
|
3
|
+
private readonly baseUrl;
|
|
4
|
+
private readonly apiKey;
|
|
5
|
+
constructor(baseUrl: string, apiKey: string);
|
|
6
|
+
execute(request: ImageRequest): Promise<ImageResponse>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=Image.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Image.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Image.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAG7D,qBAAa,WAAW;IACV,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAmB,MAAM,EAAE,MAAM;IAEvE,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;CAmC7D"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { handleOpenAIError } from "./Errors.js";
|
|
2
|
+
export class OpenAIImage {
|
|
3
|
+
baseUrl;
|
|
4
|
+
apiKey;
|
|
5
|
+
constructor(baseUrl, apiKey) {
|
|
6
|
+
this.baseUrl = baseUrl;
|
|
7
|
+
this.apiKey = apiKey;
|
|
8
|
+
}
|
|
9
|
+
async execute(request) {
|
|
10
|
+
const body = {
|
|
11
|
+
model: request.model || "dall-e-3",
|
|
12
|
+
prompt: request.prompt,
|
|
13
|
+
size: request.size || "1024x1024",
|
|
14
|
+
quality: request.quality || "standard",
|
|
15
|
+
n: request.n || 1,
|
|
16
|
+
};
|
|
17
|
+
const response = await fetch(`${this.baseUrl}/images/generations`, {
|
|
18
|
+
method: "POST",
|
|
19
|
+
headers: {
|
|
20
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
21
|
+
"Content-Type": "application/json",
|
|
22
|
+
},
|
|
23
|
+
body: JSON.stringify(body),
|
|
24
|
+
});
|
|
25
|
+
if (!response.ok) {
|
|
26
|
+
await handleOpenAIError(response, request.model);
|
|
27
|
+
}
|
|
28
|
+
const json = await response.json();
|
|
29
|
+
const data = json.data?.[0];
|
|
30
|
+
if (!data) {
|
|
31
|
+
throw new Error("OpenAI returned empty image response");
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
url: data.url,
|
|
35
|
+
revised_prompt: data.revised_prompt,
|
|
36
|
+
mime_type: "image/png",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Provider, ChatRequest, ChatResponse, ModelInfo, ChatChunk } from "../Provider.js";
|
|
1
|
+
import { Provider, ChatRequest, ChatResponse, ModelInfo, ChatChunk, ImageRequest, ImageResponse } from "../Provider.js";
|
|
2
2
|
export interface OpenAIProviderOptions {
|
|
3
3
|
apiKey: string;
|
|
4
4
|
baseUrl?: string;
|
|
@@ -9,6 +9,7 @@ export declare class OpenAIProvider implements Provider {
|
|
|
9
9
|
private readonly chatHandler;
|
|
10
10
|
private readonly streamingHandler;
|
|
11
11
|
private readonly modelsHandler;
|
|
12
|
+
private readonly imageHandler;
|
|
12
13
|
capabilities: {
|
|
13
14
|
supportsVision: (model: string) => boolean;
|
|
14
15
|
supportsTools: (model: string) => boolean;
|
|
@@ -19,5 +20,6 @@ export declare class OpenAIProvider implements Provider {
|
|
|
19
20
|
chat(request: ChatRequest): Promise<ChatResponse>;
|
|
20
21
|
stream(request: ChatRequest): AsyncGenerator<ChatChunk>;
|
|
21
22
|
listModels(): Promise<ModelInfo[]>;
|
|
23
|
+
paint(request: ImageRequest): Promise<ImageResponse>;
|
|
22
24
|
}
|
|
23
25
|
//# sourceMappingURL=OpenAIProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OpenAIProvider.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/OpenAIProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"OpenAIProvider.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/OpenAIProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAOxH,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,cAAe,YAAW,QAAQ;IAcjC,OAAO,CAAC,QAAQ,CAAC,OAAO;IAbpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAa;IACzC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAkB;IACnD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAc;IAEpC,YAAY;gCACO,MAAM;+BACP,MAAM;0CACK,MAAM;kCACd,MAAM;MAChC;gBAE2B,OAAO,EAAE,qBAAqB;IAQrD,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAIhD,MAAM,CAAC,OAAO,EAAE,WAAW,GAAG,cAAc,CAAC,SAAS,CAAC;IAIxD,UAAU,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;IAIlC,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;CAG3D"}
|
|
@@ -2,12 +2,14 @@ import { Capabilities } from "./Capabilities.js";
|
|
|
2
2
|
import { OpenAIChat } from "./Chat.js";
|
|
3
3
|
import { OpenAIStreaming } from "./Streaming.js";
|
|
4
4
|
import { OpenAIModels } from "./Models.js";
|
|
5
|
+
import { OpenAIImage } from "./Image.js";
|
|
5
6
|
export class OpenAIProvider {
|
|
6
7
|
options;
|
|
7
8
|
baseUrl;
|
|
8
9
|
chatHandler;
|
|
9
10
|
streamingHandler;
|
|
10
11
|
modelsHandler;
|
|
12
|
+
imageHandler;
|
|
11
13
|
capabilities = {
|
|
12
14
|
supportsVision: (model) => Capabilities.supportsVision(model),
|
|
13
15
|
supportsTools: (model) => Capabilities.supportsTools(model),
|
|
@@ -20,6 +22,7 @@ export class OpenAIProvider {
|
|
|
20
22
|
this.chatHandler = new OpenAIChat(this.baseUrl, options.apiKey);
|
|
21
23
|
this.streamingHandler = new OpenAIStreaming(this.baseUrl, options.apiKey);
|
|
22
24
|
this.modelsHandler = new OpenAIModels(this.baseUrl, options.apiKey);
|
|
25
|
+
this.imageHandler = new OpenAIImage(this.baseUrl, options.apiKey);
|
|
23
26
|
}
|
|
24
27
|
async chat(request) {
|
|
25
28
|
return this.chatHandler.execute(request);
|
|
@@ -30,4 +33,7 @@ export class OpenAIProvider {
|
|
|
30
33
|
async listModels() {
|
|
31
34
|
return this.modelsHandler.execute();
|
|
32
35
|
}
|
|
36
|
+
async paint(request) {
|
|
37
|
+
return this.imageHandler.execute(request);
|
|
38
|
+
}
|
|
33
39
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Streaming.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Streaming.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"Streaming.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Streaming.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAIxD,qBAAa,eAAe;IACd,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAmB,MAAM,EAAE,MAAM;IAEtE,OAAO,CAAC,OAAO,EAAE,WAAW,GAAG,cAAc,CAAC,SAAS,CAAC;CA0DhE"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Capabilities } from "./Capabilities.js";
|
|
2
|
+
import { handleOpenAIError } from "./Errors.js";
|
|
2
3
|
export class OpenAIStreaming {
|
|
3
4
|
baseUrl;
|
|
4
5
|
apiKey;
|
|
@@ -27,6 +28,9 @@ export class OpenAIStreaming {
|
|
|
27
28
|
},
|
|
28
29
|
body: JSON.stringify(body),
|
|
29
30
|
});
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
await handleOpenAIError(response, request.model);
|
|
33
|
+
}
|
|
30
34
|
if (!response.body) {
|
|
31
35
|
throw new Error("No response body for streaming");
|
|
32
36
|
}
|
|
@@ -19,5 +19,13 @@ export interface OpenAIChatResponse {
|
|
|
19
19
|
};
|
|
20
20
|
finish_reason: string;
|
|
21
21
|
}>;
|
|
22
|
+
usage?: {
|
|
23
|
+
prompt_tokens: number;
|
|
24
|
+
completion_tokens: number;
|
|
25
|
+
total_tokens: number;
|
|
26
|
+
prompt_tokens_details?: {
|
|
27
|
+
cached_tokens?: number;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
22
30
|
}
|
|
23
31
|
//# sourceMappingURL=types.d.ts.map
|