@bodhiapp/bodhi-browser-types 0.0.27 → 0.0.29

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.
@@ -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: 'setup' | 'ready' | 'resource_admin' | 'tenant_selection' | 'error' | 'unreachable';
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 ApiError or OperationError on error
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>
@@ -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, ApiError, OperationError, ExtensionError, ChatCompletionsApi, ChatApi, BodhiExtPublicApi } from './bodhiext';
2
- export { isApiError, isOperationError } from './bodhiext';
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bodhiapp/bodhi-browser-types",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "description": "TypeScript types for Bodhi Browser Extension",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",