@bodhiapp/bodhi-browser-types 0.0.26 → 0.0.28
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/dist/bodhiext.d.ts +3 -36
- package/dist/errors.d.ts +33 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +30 -9
- package/package.json +1 -1
package/dist/bodhiext.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OpenAiApiError, PingResponse, CreateChatCompletionRequest, CreateChatCompletionResponse, CreateChatCompletionStreamResponse } from '@bodhiapp/ts-client';
|
|
1
|
+
import { AppStatus, OpenAiApiError, PingResponse, CreateChatCompletionRequest, CreateChatCompletionResponse, CreateChatCompletionStreamResponse } from '@bodhiapp/ts-client';
|
|
2
2
|
/**
|
|
3
3
|
* HTTP response wrapper - body can be success type OR error type
|
|
4
4
|
* Use isApiErrorResponse() to narrow the type based on status
|
|
@@ -25,7 +25,7 @@ export interface StreamChunk {
|
|
|
25
25
|
*/
|
|
26
26
|
export interface ServerStateInfo {
|
|
27
27
|
/** Current application status */
|
|
28
|
-
status:
|
|
28
|
+
status: AppStatus | 'error' | 'unreachable';
|
|
29
29
|
/** Application version */
|
|
30
30
|
version?: string;
|
|
31
31
|
/** Server URL (added by extension) */
|
|
@@ -38,39 +38,6 @@ export interface ServerStateInfo {
|
|
|
38
38
|
param?: string;
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
|
-
/**
|
|
42
|
-
* API error thrown when server returns HTTP 4xx/5xx
|
|
43
|
-
* Used for streaming responses (non-streaming returns ApiResponse)
|
|
44
|
-
*/
|
|
45
|
-
export interface ApiError extends Error {
|
|
46
|
-
response: {
|
|
47
|
-
status: number;
|
|
48
|
-
body: OpenAiApiError;
|
|
49
|
-
headers?: Record<string, string>;
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Operation error thrown when HTTP request couldn't complete
|
|
54
|
-
* (network unreachable, timeout, extension error)
|
|
55
|
-
*/
|
|
56
|
-
export interface OperationError extends Error {
|
|
57
|
-
error: {
|
|
58
|
-
message: string;
|
|
59
|
-
type: string;
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Union of all extension-thrown errors
|
|
64
|
-
*/
|
|
65
|
-
export type ExtensionError = ApiError | OperationError;
|
|
66
|
-
/**
|
|
67
|
-
* Type guard: API error (has response field)
|
|
68
|
-
*/
|
|
69
|
-
export declare function isApiError(err: unknown): err is ApiError;
|
|
70
|
-
/**
|
|
71
|
-
* Type guard: Operation error (has error field, no response)
|
|
72
|
-
*/
|
|
73
|
-
export declare function isOperationError(err: unknown): err is OperationError;
|
|
74
41
|
/**
|
|
75
42
|
* Chat API uses types from @bodhiapp/ts-client.
|
|
76
43
|
* Consumers should import these types directly from @bodhiapp/ts-client:
|
|
@@ -91,7 +58,7 @@ export interface ChatCompletionsApi {
|
|
|
91
58
|
* Create a chat completion
|
|
92
59
|
*
|
|
93
60
|
* Non-streaming: Returns ApiResponse - caller checks status for success/error
|
|
94
|
-
* Streaming: Yields chunks via AsyncIterable - throws
|
|
61
|
+
* Streaming: Yields chunks via AsyncIterable - throws BodhiApiError or BodhiError on error
|
|
95
62
|
*
|
|
96
63
|
* @param params - Chat completion parameters
|
|
97
64
|
* @returns Non-streaming: Promise<ApiResponse<CreateChatCompletionResponse>>, Streaming: AsyncIterable<CreateChatCompletionStreamResponse>
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { OpenAiApiError } from '@bodhiapp/ts-client';
|
|
2
|
+
import { ApiResponse } from './bodhiext';
|
|
3
|
+
/**
|
|
4
|
+
* Error codes for BodhiError
|
|
5
|
+
* Open-ended union allows custom codes via (string & {})
|
|
6
|
+
*/
|
|
7
|
+
export type BodhiErrorCode = 'network_error' | 'timeout_error' | 'extension_error' | 'auth_error' | 'not_initialized' | 'connection_closed' | 'parse_error' | 'api_error' | (string & {});
|
|
8
|
+
/**
|
|
9
|
+
* Base error class for operational errors
|
|
10
|
+
* (network unreachable, timeout, extension error, auth error)
|
|
11
|
+
*
|
|
12
|
+
* Use `instanceof BodhiError` to detect — no type guards needed.
|
|
13
|
+
*/
|
|
14
|
+
export declare class BodhiError extends Error {
|
|
15
|
+
readonly code: BodhiErrorCode;
|
|
16
|
+
constructor(code: BodhiErrorCode, message: string);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* HTTP API error (server returned 4xx/5xx)
|
|
20
|
+
*
|
|
21
|
+
* Use `instanceof BodhiApiError` to detect — no type guards needed.
|
|
22
|
+
* Always has code = 'api_error'.
|
|
23
|
+
*/
|
|
24
|
+
export declare class BodhiApiError extends BodhiError {
|
|
25
|
+
readonly status: number;
|
|
26
|
+
readonly body: OpenAiApiError;
|
|
27
|
+
readonly headers?: Record<string, string>;
|
|
28
|
+
constructor(status: number, body: OpenAiApiError, message: string, headers?: Record<string, string>);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Extract response body or throw BodhiApiError if status >= 400
|
|
32
|
+
*/
|
|
33
|
+
export declare function unwrapResponse<T>(response: ApiResponse<T>): T;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export type { ApiResponse, StreamChunk, ServerStateInfo,
|
|
2
|
-
export {
|
|
1
|
+
export type { ApiResponse, StreamChunk, ServerStateInfo, ChatCompletionsApi, ChatApi, BodhiExtPublicApi } from './bodhiext';
|
|
2
|
+
export { BodhiError, BodhiApiError, unwrapResponse, type BodhiErrorCode } from './errors';
|
|
3
3
|
export type { ApiRequest, ApiRequestMessage, OperationErrorResponse, ApiResponseSuccessMessage, OperationErrorResponseMessage, ApiResponseMessage, ErrorMessage, StreamChunkMessage, StreamApiErrorMessage, StreamErrorMessage, StreamMessage, StreamController, SSEChunk, ExtRequest, ExtRequestMessage, ExtError, ExtErrorResponse, ExtResponse, ExtResponseMessage, GetExtensionIdRequest, GetExtensionIdResponse, TestConnectionRequest, TestConnectionResponse, } from './protocol';
|
|
4
4
|
export { MESSAGE_TYPES, isOperationErrorResponse, isApiErrorResponse, isApiSuccessResponse, isStreamChunk, isStreamApiError, isStreamError, isExtError, isOpenAiApiErrorBody, isOperationErrorStructure, } from './protocol';
|
|
5
5
|
export { CONTENT_TYPE_JSON, CONTENT_TYPE_EVENT_STREAM, CONTENT_TYPE_HEADER, HTTP_METHOD_GET, HTTP_METHOD_POST, ENDPOINT_PING, ENDPOINT_CHAT_COMPLETIONS, DEFAULT_API_BASE_URL, DEFAULT_API_TIMEOUT, DEFAULT_STREAM_TIMEOUT, STORAGE_KEY_BACKEND_URL, SSE_DONE_MARKER, SSE_DATA_PREFIX, SSE_CHUNK_DELIMITER, EXT_ACTIONS, ERROR_TYPES, DOCUMENT_STATE_COMPLETE, EVENT_INITIALIZED, BODHI_STREAM_PORT, ORIGIN_WILDCARD, ERROR_MISSING_REQUEST_ID, ERROR_CONNECTION_CLOSED, } from './common';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
class BodhiError extends Error {
|
|
2
|
+
constructor(code, message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.code = code;
|
|
5
|
+
this.name = "BodhiError";
|
|
6
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
class BodhiApiError extends BodhiError {
|
|
10
|
+
constructor(status, body, message, headers) {
|
|
11
|
+
super("api_error", message);
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.body = body;
|
|
14
|
+
this.headers = headers;
|
|
15
|
+
this.name = "BodhiApiError";
|
|
16
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function unwrapResponse(response) {
|
|
20
|
+
if (response.status >= 400) {
|
|
21
|
+
const body = response.body;
|
|
22
|
+
const message = body?.error?.message || `HTTP ${response.status}`;
|
|
23
|
+
throw new BodhiApiError(response.status, body, message, response.headers);
|
|
24
|
+
}
|
|
25
|
+
return response.body;
|
|
26
|
+
}
|
|
1
27
|
function isNonNullObject(value) {
|
|
2
28
|
return value !== null && typeof value === "object";
|
|
3
29
|
}
|
|
@@ -39,12 +65,6 @@ function isStreamError(msg) {
|
|
|
39
65
|
function isExtError(res) {
|
|
40
66
|
return res !== null && typeof res === "object" && "error" in res;
|
|
41
67
|
}
|
|
42
|
-
function isApiError(err) {
|
|
43
|
-
return err instanceof Error && "response" in err && typeof err.response === "object" && err.response !== null && typeof err.response.status === "number" && "body" in err.response;
|
|
44
|
-
}
|
|
45
|
-
function isOperationError(err) {
|
|
46
|
-
return err instanceof Error && "error" in err && !("response" in err) && isOperationErrorStructure(err.error);
|
|
47
|
-
}
|
|
48
68
|
const CONTENT_TYPE_JSON = "application/json";
|
|
49
69
|
const CONTENT_TYPE_EVENT_STREAM = "text/event-stream";
|
|
50
70
|
const CONTENT_TYPE_HEADER = "Content-Type";
|
|
@@ -77,6 +97,8 @@ const ERROR_MISSING_REQUEST_ID = "Invalid message format: missing requestId or r
|
|
|
77
97
|
const ERROR_CONNECTION_CLOSED = "Connection closed unexpectedly";
|
|
78
98
|
export {
|
|
79
99
|
BODHI_STREAM_PORT,
|
|
100
|
+
BodhiApiError,
|
|
101
|
+
BodhiError,
|
|
80
102
|
CONTENT_TYPE_EVENT_STREAM,
|
|
81
103
|
CONTENT_TYPE_HEADER,
|
|
82
104
|
CONTENT_TYPE_JSON,
|
|
@@ -99,15 +121,14 @@ export {
|
|
|
99
121
|
SSE_DATA_PREFIX,
|
|
100
122
|
SSE_DONE_MARKER,
|
|
101
123
|
STORAGE_KEY_BACKEND_URL,
|
|
102
|
-
isApiError,
|
|
103
124
|
isApiErrorResponse,
|
|
104
125
|
isApiSuccessResponse,
|
|
105
126
|
isExtError,
|
|
106
127
|
isOpenAiApiErrorBody,
|
|
107
|
-
isOperationError,
|
|
108
128
|
isOperationErrorResponse,
|
|
109
129
|
isOperationErrorStructure,
|
|
110
130
|
isStreamApiError,
|
|
111
131
|
isStreamChunk,
|
|
112
|
-
isStreamError
|
|
132
|
+
isStreamError,
|
|
133
|
+
unwrapResponse
|
|
113
134
|
};
|